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-2006 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 Access Functions
55 * Return the number of items in the cache
56 * @arg cache cache handle
58 int nl_cache_nitems(struct nl_cache *cache)
60 return cache->c_nitems;
64 * Return the number of items matching a filter in the cache
65 * @arg cache Cache object.
66 * @arg filter Filter object.
68 int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
70 struct nl_object_ops *ops;
71 struct nl_object *obj;
74 if (cache->c_ops == NULL)
77 ops = cache->c_ops->co_obj_ops;
79 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
80 if (filter && !nl_object_match_filter(obj, filter))
90 * Returns \b true if the cache is empty.
91 * @arg cache Cache to check
92 * @return \a true if the cache is empty, otherwise \b false is returned.
94 int nl_cache_is_empty(struct nl_cache *cache)
96 return nl_list_empty(&cache->c_items);
100 * Return the operations set of the cache
101 * @arg cache cache handle
103 struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache)
109 * Return the first element in the cache
110 * @arg cache cache handle
112 struct nl_object *nl_cache_get_first(struct nl_cache *cache)
114 if (nl_list_empty(&cache->c_items))
117 return nl_list_entry(cache->c_items.next,
118 struct nl_object, ce_list);
122 * Return the last element in the cache
123 * @arg cache cache handle
125 struct nl_object *nl_cache_get_last(struct nl_cache *cache)
127 if (nl_list_empty(&cache->c_items))
130 return nl_list_entry(cache->c_items.prev,
131 struct nl_object, ce_list);
135 * Return the next element in the cache
136 * @arg obj current object
138 struct nl_object *nl_cache_get_next(struct nl_object *obj)
140 if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
143 return nl_list_entry(obj->ce_list.next,
144 struct nl_object, ce_list);
148 * Return the previous element in the cache
149 * @arg obj current object
151 struct nl_object *nl_cache_get_prev(struct nl_object *obj)
153 if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
156 return nl_list_entry(obj->ce_list.prev,
157 struct nl_object, ce_list);
163 * @name Cache Creation/Deletion
168 * Allocate an empty cache
169 * @arg ops cache operations to base the cache on
171 * @return A newly allocated and initialized cache.
173 struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
175 struct nl_cache *cache;
177 cache = calloc(1, sizeof(*cache));
183 nl_init_list_head(&cache->c_items);
186 NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
192 * Allocate an empty cache based on type name
193 * @arg kind Name of cache type
194 * @return A newly allocated and initialized cache.
196 struct nl_cache *nl_cache_alloc_name(const char *kind)
198 struct nl_cache_ops *ops;
200 ops = nl_cache_ops_lookup(kind);
202 nl_error(ENOENT, "Unable to lookup cache \"%s\"", kind);
206 return nl_cache_alloc(ops);
210 * Allocate a new cache containing a subset of a cache
211 * @arg orig Original cache to be based on
212 * @arg filter Filter defining the subset to be filled into new cache
213 * @return A newly allocated cache or NULL.
215 struct nl_cache *nl_cache_subset(struct nl_cache *orig,
216 struct nl_object *filter)
218 struct nl_cache *cache;
219 struct nl_object_ops *ops;
220 struct nl_object *obj;
225 cache = nl_cache_alloc(orig->c_ops);
229 ops = orig->c_ops->co_obj_ops;
231 nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
232 if (!nl_object_match_filter(obj, filter))
235 nl_cache_add(cache, obj);
243 * @arg cache cache to clear
245 * Removes all elements of a cache.
247 void nl_cache_clear(struct nl_cache *cache)
249 struct nl_object *obj, *tmp;
251 NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
253 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
254 nl_cache_remove(obj);
259 * @arg cache Cache to free.
261 * Removes all elements of a cache and frees all memory.
263 * @note Use this function if you are working with allocated caches.
265 void nl_cache_free(struct nl_cache *cache)
267 nl_cache_clear(cache);
268 NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
275 * @name Cache Modifications
279 static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
281 obj->ce_cache = cache;
283 nl_list_add_tail(&obj->ce_list, &cache->c_items);
286 NL_DBG(1, "Added %p to cache %p <%s>.\n",
287 obj, cache, nl_cache_name(cache));
293 * Add object to a cache.
294 * @arg cache Cache to add object to
295 * @arg obj Object to be added to the cache
297 * Adds the given object to the specified cache. The object is cloned
298 * if it has been added to another cache already.
300 * @return 0 or a negative error code.
302 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
304 struct nl_object *new;
306 if (cache->c_ops->co_obj_ops != obj->ce_ops)
307 return nl_error(EINVAL, "Object mismatches cache type");
309 if (!nl_list_empty(&obj->ce_list)) {
310 new = nl_object_clone(obj);
312 return nl_errno(ENOMEM);
318 return __cache_add(cache, new);
322 * Move object from one cache to another
323 * @arg cache Cache to move object to.
324 * @arg obj Object subject to be moved
326 * Removes the given object from its associated cache if needed
327 * and adds it to the new cache.
329 * @return 0 on success or a negative error code.
331 int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
333 if (cache->c_ops->co_obj_ops != obj->ce_ops)
334 return nl_error(EINVAL, "Object mismatches cache type");
336 NL_DBG(3, "Moving object %p to cache %p\n", obj, cache);
338 /* Acquire reference, if already in a cache this will be
339 * reverted during removal */
342 if (!nl_list_empty(&obj->ce_list))
343 nl_cache_remove(obj);
345 return __cache_add(cache, obj);
349 * Removes an object from a cache.
350 * @arg obj Object to remove from its cache
352 * Removes the object \c obj from the cache it is assigned to, since
353 * an object can only be assigned to one cache at a time, the cache
354 * must ne be passed along with it.
356 void nl_cache_remove(struct nl_object *obj)
358 struct nl_cache *cache = obj->ce_cache;
363 nl_list_del(&obj->ce_list);
364 obj->ce_cache = NULL;
368 NL_DBG(1, "Deleted %p from cache %p <%s>.\n",
369 obj, cache, nl_cache_name(cache));
373 * Search for an object in a cache
374 * @arg cache Cache to search in.
375 * @arg needle Object to look for.
377 * Iterates over the cache and looks for an object with identical
378 * identifiers as the needle.
380 * @return Reference to object or NULL if not found.
381 * @note The returned object must be returned via nl_object_put().
383 struct nl_object *nl_cache_search(struct nl_cache *cache,
384 struct nl_object *needle)
386 struct nl_object *obj;
388 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
389 if (nl_object_identical(obj, needle)) {
402 * @name Synchronization
407 * Request a full dump from the kernel to fill a cache
408 * @arg handle Netlink handle
409 * @arg cache Cache subjected to be filled.
411 * Send a dumping request to the kernel causing it to dump all objects
412 * related to the specified cache to the netlink socket.
414 * Use nl_cache_pickup() to read the objects from the socket and fill them
417 int nl_cache_request_full_dump(struct nl_handle *handle, struct nl_cache *cache)
419 NL_DBG(2, "Requesting dump from kernel for cache %p <%s>...\n",
420 cache, nl_cache_name(cache));
422 if (cache->c_ops->co_request_update == NULL)
423 return nl_error(EOPNOTSUPP, "Operation not supported");
425 return cache->c_ops->co_request_update(cache, handle);
429 struct update_xdata {
430 struct nl_cache_ops *ops;
431 struct nl_parser_param *params;
434 static int update_msg_parser(struct nl_msg *msg, void *arg)
436 struct update_xdata *x = arg;
438 return nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
442 int __cache_pickup(struct nl_handle *handle, struct nl_cache *cache,
443 struct nl_parser_param *param)
447 struct update_xdata x = {
452 NL_DBG(1, "Picking up answer for cache %p <%s>...\n",
453 cache, nl_cache_name(cache));
455 cb = nl_cb_clone(handle->h_cb);
457 return nl_get_errno();
459 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x);
461 err = nl_recvmsgs(handle, cb);
463 NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned " \
464 "%d: %s", cache, nl_cache_name(cache),
472 static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
474 return nl_cache_add((struct nl_cache *) p->pp_arg, c);
478 * Pickup a netlink dump response and put it into a cache.
479 * @arg handle Netlink handle.
480 * @arg cache Cache to put items into.
482 * Waits for netlink messages to arrive, parses them and puts them into
483 * the specified cache.
485 * @return 0 on success or a negative error code.
487 int nl_cache_pickup(struct nl_handle *handle, struct nl_cache *cache)
489 struct nl_parser_param p = {
494 return __cache_pickup(handle, cache, &p);
497 static int cache_include(struct nl_cache *cache, struct nl_object *obj,
498 struct nl_msgtype *type, change_func_t cb)
500 struct nl_object *old;
502 switch (type->mt_act) {
505 old = nl_cache_search(cache, obj);
507 nl_cache_remove(old);
508 if (type->mt_act == NL_ACT_DEL) {
510 cb(cache, old, NL_ACT_DEL);
515 if (type->mt_act == NL_ACT_NEW) {
516 nl_cache_move(cache, obj);
517 if (old == NULL && cb)
518 cb(cache, obj, NL_ACT_NEW);
520 if (nl_object_diff(old, obj) && cb)
521 cb(cache, obj, NL_ACT_CHANGE);
528 NL_DBG(2, "Unknown action associated to object %p\n", obj);
535 int nl_cache_include(struct nl_cache *cache, struct nl_object *obj,
536 change_func_t change_cb)
538 struct nl_cache_ops *ops = cache->c_ops;
541 if (ops->co_obj_ops != obj->ce_ops)
542 return nl_error(EINVAL, "Object mismatches cache type");
544 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
545 if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
546 return cache_include(cache, obj, &ops->co_msgtypes[i],
549 return nl_errno(EINVAL);
552 static int resync_cb(struct nl_object *c, struct nl_parser_param *p)
554 struct nl_cache_assoc *ca = p->pp_arg;
556 return nl_cache_include(ca->ca_cache, c, ca->ca_change);
559 int nl_cache_resync(struct nl_handle *handle, struct nl_cache *cache,
560 change_func_t change_cb)
562 struct nl_object *obj, *next;
563 struct nl_cache_assoc ca = {
565 .ca_change = change_cb,
567 struct nl_parser_param p = {
573 NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
575 /* Mark all objects so we can see if some of them are obsolete */
576 nl_cache_mark_all(cache);
578 err = nl_cache_request_full_dump(handle, cache);
582 err = __cache_pickup(handle, cache, &p);
586 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list)
587 if (nl_object_is_marked(obj))
588 nl_cache_remove(obj);
590 NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
605 int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who,
606 struct nlmsghdr *nlh, struct nl_parser_param *params)
610 if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize)) {
611 err = nl_error(EINVAL, "netlink message too short to be "
612 "of kind %s", ops->co_name);
616 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
617 if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
618 err = ops->co_msg_parser(ops, who, nlh, params);
625 err = nl_error(EINVAL, "Unsupported netlink message type %d",
633 * Parse a netlink message and add it to the cache.
634 * @arg cache cache to add element to
635 * @arg msg netlink message
637 * Parses a netlink message by calling the cache specific message parser
638 * and adds the new element to the cache.
640 * @return 0 or a negative error code.
642 int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
644 struct nl_parser_param p = {
649 return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
653 * (Re)fill a cache with the contents in the kernel.
654 * @arg handle netlink handle
655 * @arg cache cache to update
657 * Clears the specified cache and fills it with the current state in
660 * @return 0 or a negative error code.
662 int nl_cache_refill(struct nl_handle *handle, struct nl_cache *cache)
666 err = nl_cache_request_full_dump(handle, cache);
670 NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n",
671 cache, nl_cache_name(cache));
672 nl_cache_clear(cache);
674 return nl_cache_pickup(handle, cache);
685 * Mark all objects in a cache
686 * @arg cache Cache to mark all objects in
688 void nl_cache_mark_all(struct nl_cache *cache)
690 struct nl_object *obj;
692 NL_DBG(2, "Marking all objects in cache %p <%s>...\n",
693 cache, nl_cache_name(cache));
695 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
707 * Dump all elements of a cache.
708 * @arg cache cache to dump
709 * @arg params dumping parameters
711 * Dumps all elements of the \a cache to the file descriptor \a fd.
713 void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
715 nl_cache_dump_filter(cache, params, NULL);
719 * Dump all elements of a cache (filtered).
720 * @arg cache cache to dump
721 * @arg params dumping parameters (optional)
722 * @arg filter filter object
724 * Dumps all elements of the \a cache to the file descriptor \a fd
725 * given they match the given filter \a filter.
727 void nl_cache_dump_filter(struct nl_cache *cache,
728 struct nl_dump_params *params,
729 struct nl_object *filter)
731 int type = params ? params->dp_type : NL_DUMP_FULL;
732 struct nl_object_ops *ops;
733 struct nl_object *obj;
735 NL_DBG(2, "Dumping cache %p <%s> filter %p\n",
736 cache, nl_cache_name(cache), filter);
738 if (type > NL_DUMP_MAX || type < 0)
741 if (cache->c_ops == NULL)
744 ops = cache->c_ops->co_obj_ops;
745 if (!ops->oo_dump[type])
748 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
749 if (filter && !nl_object_match_filter(obj, filter))
752 NL_DBG(4, "Dumping object %p...\n", obj);
753 dump_from_ops(obj, params);
765 * Call a callback on each element of the cache.
766 * @arg cache cache to iterate on
767 * @arg cb callback function
768 * @arg arg argument passed to callback function
770 * Calls a callback function \a cb on each element of the \a cache.
771 * The argument \a arg is passed on the callback function.
773 void nl_cache_foreach(struct nl_cache *cache,
774 void (*cb)(struct nl_object *, void *), void *arg)
776 nl_cache_foreach_filter(cache, NULL, cb, arg);
780 * Call a callback on each element of the cache (filtered).
781 * @arg cache cache to iterate on
782 * @arg filter filter object
783 * @arg cb callback function
784 * @arg arg argument passed to callback function
786 * Calls a callback function \a cb on each element of the \a cache
787 * that matches the \a filter. The argument \a arg is passed on
788 * to the callback function.
790 void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter,
791 void (*cb)(struct nl_object *, void *), void *arg)
793 struct nl_object *obj, *tmp;
794 struct nl_object_ops *ops;
796 if (cache->c_ops == NULL)
799 ops = cache->c_ops->co_obj_ops;
801 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
802 if (filter && !nl_object_match_filter(obj, filter))