2 * lib/cache.c Caching Module
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
14 * @defgroup cache Cache
17 * Cache Management | | Type Specific Cache Operations
19 * | | +----------------+ +------------+
20 * | request update | | msg_parser |
21 * | | +----------------+ +------------+
22 * +- - - - -^- - - - - - - -^- -|- - - -
23 * nl_cache_update: | | | |
24 * 1) --------- co_request_update ------+ | |
26 * 2) destroy old cache +----------- pp_cb ---------|---+
28 * 3) ---------- nl_recvmsgs ----------+ +- cb_valid -+
29 * +--------------+ | | | |
30 * | nl_cache_add |<-----+ + - - -v- -|- - - - - - - - - - -
31 * +--------------+ | | +-------------+
43 #include <netlink-local.h>
44 #include <netlink/netlink.h>
45 #include <netlink/cache.h>
46 #include <netlink/object.h>
47 #include <netlink/utils.h>
50 * @name Cache Creation/Deletion
55 * Allocate an empty cache
56 * @arg ops cache operations to base the cache on
58 * @return A newly allocated and initialized cache.
60 struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
62 struct nl_cache *cache;
64 cache = calloc(1, sizeof(*cache));
68 nl_init_list_head(&cache->c_items);
71 NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
76 int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock,
77 struct nl_cache **result)
79 struct nl_cache *cache;
82 if (!(cache = nl_cache_alloc(ops)))
85 if (sock && (err = nl_cache_refill(sock, cache)) < 0) {
96 * @arg cache cache to clear
98 * Removes all elements of a cache.
100 void nl_cache_clear(struct nl_cache *cache)
102 struct nl_object *obj, *tmp;
104 NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
106 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
107 nl_cache_remove(obj);
112 * @arg cache Cache to free.
114 * Removes all elements of a cache and frees all memory.
116 * @note Use this function if you are working with allocated caches.
118 void nl_cache_free(struct nl_cache *cache)
123 nl_cache_clear(cache);
124 NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
131 * @name Cache Modifications
135 static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
137 obj->ce_cache = cache;
139 nl_list_add_tail(&obj->ce_list, &cache->c_items);
142 NL_DBG(1, "Added %p to cache %p <%s>.\n",
143 obj, cache, nl_cache_name(cache));
149 * Add object to a cache.
150 * @arg cache Cache to add object to
151 * @arg obj Object to be added to the cache
153 * Adds the given object to the specified cache. The object is cloned
154 * if it has been added to another cache already.
156 * @return 0 or a negative error code.
158 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
160 struct nl_object *new;
162 if (cache->c_ops->co_obj_ops != obj->ce_ops)
163 return -NLE_OBJ_MISMATCH;
165 if (!nl_list_empty(&obj->ce_list)) {
166 new = nl_object_clone(obj);
174 return __cache_add(cache, new);
178 * Removes an object from a cache.
179 * @arg obj Object to remove from its cache
181 * Removes the object \c obj from the cache it is assigned to, since
182 * an object can only be assigned to one cache at a time, the cache
183 * must ne be passed along with it.
185 void nl_cache_remove(struct nl_object *obj)
187 struct nl_cache *cache = obj->ce_cache;
192 nl_list_del(&obj->ce_list);
193 obj->ce_cache = NULL;
197 NL_DBG(1, "Deleted %p from cache %p <%s>.\n",
198 obj, cache, nl_cache_name(cache));
204 * @name Synchronization
209 * Request a full dump from the kernel to fill a cache
210 * @arg sk Netlink socket.
211 * @arg cache Cache subjected to be filled.
213 * Send a dumping request to the kernel causing it to dump all objects
214 * related to the specified cache to the netlink socket.
216 * Use nl_cache_pickup() to read the objects from the socket and fill them
219 int nl_cache_request_full_dump(struct nl_sock *sk, struct nl_cache *cache)
221 NL_DBG(2, "Requesting dump from kernel for cache %p <%s>...\n",
222 cache, nl_cache_name(cache));
224 if (cache->c_ops->co_request_update == NULL)
225 return -NLE_OPNOTSUPP;
227 return cache->c_ops->co_request_update(cache, sk);
231 struct update_xdata {
232 struct nl_cache_ops *ops;
233 struct nl_parser_param *params;
236 static int update_msg_parser(struct nl_msg *msg, void *arg)
238 struct update_xdata *x = arg;
240 return nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
244 int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache,
245 struct nl_parser_param *param)
249 struct update_xdata x = {
254 NL_DBG(1, "Picking up answer for cache %p <%s>...\n",
255 cache, nl_cache_name(cache));
257 cb = nl_cb_clone(sk->s_cb);
261 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x);
263 err = nl_recvmsgs(sk, cb);
265 NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned " \
266 "%d: %s", cache, nl_cache_name(cache),
267 err, nl_geterror(err));
274 static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
276 return nl_cache_add((struct nl_cache *) p->pp_arg, c);
280 * Pickup a netlink dump response and put it into a cache.
281 * @arg sk Netlink socket.
282 * @arg cache Cache to put items into.
284 * Waits for netlink messages to arrive, parses them and puts them into
285 * the specified cache.
287 * @return 0 on success or a negative error code.
289 int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
291 struct nl_parser_param p = {
296 return __cache_pickup(sk, cache, &p);
308 int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who,
309 struct nlmsghdr *nlh, struct nl_parser_param *params)
313 if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize))
314 return -NLE_MSG_TOOSHORT;
316 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
317 if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
318 err = ops->co_msg_parser(ops, who, nlh, params);
319 if (err != -NLE_OPNOTSUPP)
325 err = -NLE_MSGTYPE_NOSUPPORT;
332 * Parse a netlink message and add it to the cache.
333 * @arg cache cache to add element to
334 * @arg msg netlink message
336 * Parses a netlink message by calling the cache specific message parser
337 * and adds the new element to the cache.
339 * @return 0 or a negative error code.
341 int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
343 struct nl_parser_param p = {
348 return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
352 * (Re)fill a cache with the contents in the kernel.
353 * @arg sk Netlink socket.
354 * @arg cache cache to update
356 * Clears the specified cache and fills it with the current state in
359 * @return 0 or a negative error code.
361 int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
365 err = nl_cache_request_full_dump(sk, cache);
369 NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n",
370 cache, nl_cache_name(cache));
371 nl_cache_clear(cache);
373 return nl_cache_pickup(sk, cache);