• Home
  • Raw
  • Download

Lines Matching +full:cache +full:- +full:to

1 /* SPDX-License-Identifier: LGPL-2.1-only */
3 * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
8 * @defgroup cache Cache
11 * Cache Management | | Type Specific Cache Operations
13 * | | +----------------+ +------------+
15 * | | +----------------+ +------------+
16 * +- - - - -^- - - - - - - -^- -|- - - -
18 * 1) --------- co_request_update ------+ | |
20 * 2) destroy old cache +----------- pp_cb ---------|---+
22 * 3) ---------- nl_recvmsgs ----------+ +- cb_valid -+
23 * +--------------+ | | | |
24 * | nl_cache_add |<-----+ + - - -v- -|- - - - - - - - - - -
25 * +--------------+ | | +-------------+
27 * | | +-----|-^-----+
28 * +---v-|---+
30 * +---------+
35 * - @core_doc{core_cache, Caching System}
40 * ------
42 * #include <netlink/cache.h>
46 #include "nl-default.h"
49 #include <netlink/cache.h>
54 #include "nl-core.h"
55 #include "nl-priv-dynamic-core/nl-core.h"
56 #include "nl-priv-dynamic-core/object-api.h"
57 #include "nl-priv-dynamic-core/cache-api.h"
58 #include "nl-aux-core/nl-core.h"
66 * Return the number of items in the cache
67 * @arg cache cache handle
69 int nl_cache_nitems(struct nl_cache *cache) in nl_cache_nitems() argument
71 return cache->c_nitems; in nl_cache_nitems()
75 * Return the number of items matching a filter in the cache
76 * @arg cache Cache object.
79 int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter) in nl_cache_nitems_filter() argument
84 if (cache->c_ops == NULL) in nl_cache_nitems_filter()
87 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_nitems_filter()
98 * Returns \b true if the cache is empty.
99 * @arg cache Cache to check
100 * @return \a true if the cache is empty, otherwise \b false is returned.
102 int nl_cache_is_empty(struct nl_cache *cache) in nl_cache_is_empty() argument
104 return nl_list_empty(&cache->c_items); in nl_cache_is_empty()
108 * Return the operations set of the cache
109 * @arg cache cache handle
111 struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache) in nl_cache_get_ops() argument
113 return cache->c_ops; in nl_cache_get_ops()
117 * Return the first element in the cache
118 * @arg cache cache handle
120 struct nl_object *nl_cache_get_first(struct nl_cache *cache) in nl_cache_get_first() argument
122 if (nl_list_empty(&cache->c_items)) in nl_cache_get_first()
125 return nl_list_entry(cache->c_items.next, in nl_cache_get_first()
130 * Return the last element in the cache
131 * @arg cache cache handle
133 struct nl_object *nl_cache_get_last(struct nl_cache *cache) in nl_cache_get_last() argument
135 if (nl_list_empty(&cache->c_items)) in nl_cache_get_last()
138 return nl_list_entry(cache->c_items.prev, in nl_cache_get_last()
143 * Return the next element in the cache
148 if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list)) in nl_cache_get_next()
151 return nl_list_entry(obj->ce_list.next, in nl_cache_get_next()
156 * Return the previous element in the cache
161 if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list)) in nl_cache_get_prev()
164 return nl_list_entry(obj->ce_list.prev, in nl_cache_get_prev()
171 * @name Cache Allocation/Deletion
176 * Allocate new cache
177 * @arg ops Cache operations
179 * Allocate and initialize a new cache based on the cache operations
182 * @return Allocated cache or NULL if allocation failed.
186 struct nl_cache *cache; in nl_cache_alloc() local
188 cache = calloc(1, sizeof(*cache)); in nl_cache_alloc()
189 if (!cache) in nl_cache_alloc()
192 nl_init_list_head(&cache->c_items); in nl_cache_alloc()
193 cache->c_ops = ops; in nl_cache_alloc()
194 cache->c_flags |= ops->co_flags; in nl_cache_alloc()
195 cache->c_refcnt = 1; in nl_cache_alloc()
200 * cache objects for faster lookups in nl_cache_alloc()
202 if (ops->co_obj_ops->oo_keygen) { in nl_cache_alloc()
205 if (ops->co_hash_size) in nl_cache_alloc()
206 hashtable_size = ops->co_hash_size; in nl_cache_alloc()
210 cache->hashtable = nl_hash_table_alloc(hashtable_size); in nl_cache_alloc()
213 NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache)); in nl_cache_alloc()
215 return cache; in nl_cache_alloc()
219 * Allocate new cache and fill it
220 * @arg ops Cache operations
224 * Allocate new cache and fill it. Equivalent to calling:
226 * cache = nl_cache_alloc(ops);
227 * nl_cache_refill(sock, cache);
237 struct nl_cache *cache; in nl_cache_alloc_and_fill() local
240 if (!(cache = nl_cache_alloc(ops))) in nl_cache_alloc_and_fill()
241 return -NLE_NOMEM; in nl_cache_alloc_and_fill()
243 if (sock && (err = nl_cache_refill(sock, cache)) < 0) { in nl_cache_alloc_and_fill()
244 nl_cache_free(cache); in nl_cache_alloc_and_fill()
248 *result = cache; in nl_cache_alloc_and_fill()
253 * Allocate new cache based on type name
254 * @arg kind Name of cache type
257 * Lookup cache ops via nl_cache_ops_lookup() and allocate the cache
258 * by calling nl_cache_alloc(). Stores the allocated cache in the
268 struct nl_cache *cache; in nl_cache_alloc_name() local
272 return -NLE_NOCACHE; in nl_cache_alloc_name()
274 cache = nl_cache_alloc(ops); in nl_cache_alloc_name()
276 if (!cache) in nl_cache_alloc_name()
277 return -NLE_NOMEM; in nl_cache_alloc_name()
279 *result = cache; in nl_cache_alloc_name()
284 * Allocate new cache containing a subset of an existing cache
285 * @arg orig Original cache to base new cache on
286 * @arg filter Filter defining the subset to be filled into the new cache
288 * Allocates a new cache matching the type of the cache specified by
289 * \p orig. Iterates over the \p orig cache applying the specified
290 * \p filter and copies all objects that match to the new cache.
292 * The copied objects are clones but do not contain a reference to each
293 * other. Later modifications to objects in the original cache will
294 * not affect objects in the new cache.
296 * @return A newly allocated cache or NULL.
301 struct nl_cache *cache; in nl_cache_subset() local
307 cache = nl_cache_alloc(orig->c_ops); in nl_cache_subset()
308 if (!cache) in nl_cache_subset()
311 NL_DBG(2, "Filling subset of cache %p <%s> with filter %p into %p\n", in nl_cache_subset()
312 orig, nl_cache_name(orig), filter, cache); in nl_cache_subset()
314 nl_list_for_each_entry(obj, &orig->c_items, ce_list) { in nl_cache_subset()
318 nl_cache_add(cache, obj); in nl_cache_subset()
321 return cache; in nl_cache_subset()
325 * Allocate new cache and copy the contents of an existing cache
326 * @arg cache Original cache to base new cache on
328 * Allocates a new cache matching the type of the cache specified by
329 * \p cache. Iterates over the \p cache cache and copies all objects
330 * to the new cache.
332 * The copied objects are clones but do not contain a reference to each
333 * other. Later modifications to objects in the original cache will
334 * not affect objects in the new cache.
336 * @return A newly allocated cache or NULL.
338 struct nl_cache *nl_cache_clone(struct nl_cache *cache) in nl_cache_clone() argument
340 struct nl_cache_ops *ops = nl_cache_get_ops(cache); in nl_cache_clone()
348 NL_DBG(2, "Cloning %p into %p\n", cache, clone); in nl_cache_clone()
350 nl_list_for_each_entry(obj, &cache->c_items, ce_list) in nl_cache_clone()
357 * Remove all objects of a cache.
358 * @arg cache Cache to clear
360 * The objects are unliked/removed from the cache by calling
361 * nl_cache_remove() on each object in the cache. If any of the objects
362 * to not contain any further references to them, those objects will
365 * Unlike with nl_cache_free(), the cache is not freed just emptied.
367 void nl_cache_clear(struct nl_cache *cache) in nl_cache_clear() argument
371 NL_DBG(2, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache)); in nl_cache_clear()
373 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) in nl_cache_clear()
377 static void __nl_cache_free(struct nl_cache *cache) in __nl_cache_free() argument
379 nl_cache_clear(cache); in __nl_cache_free()
381 if (cache->hashtable) in __nl_cache_free()
382 nl_hash_table_free(cache->hashtable); in __nl_cache_free()
384 NL_DBG(2, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache)); in __nl_cache_free()
385 free(cache); in __nl_cache_free()
389 * Increase reference counter of cache
390 * @arg cache Cache
392 void nl_cache_get(struct nl_cache *cache) in nl_cache_get() argument
394 cache->c_refcnt++; in nl_cache_get()
396 NL_DBG(3, "Incremented cache %p <%s> reference count to %d\n", in nl_cache_get()
397 cache, nl_cache_name(cache), cache->c_refcnt); in nl_cache_get()
401 * Free a cache.
402 * @arg cache Cache to free.
404 * Calls nl_cache_clear() to remove all objects associated with the
405 * cache and frees the cache afterwards.
409 void nl_cache_free(struct nl_cache *cache) in nl_cache_free() argument
411 if (!cache) in nl_cache_free()
414 cache->c_refcnt--; in nl_cache_free()
416 NL_DBG(3, "Decremented cache %p <%s> reference count, %d remaining\n", in nl_cache_free()
417 cache, nl_cache_name(cache), cache->c_refcnt); in nl_cache_free()
419 if (cache->c_refcnt <= 0) in nl_cache_free()
420 __nl_cache_free(cache); in nl_cache_free()
423 void nl_cache_put(struct nl_cache *cache) in nl_cache_put() argument
425 nl_cache_free(cache); in nl_cache_put()
431 * @name Cache Modifications
435 static int __cache_add(struct nl_cache *cache, struct nl_object *obj) in __cache_add() argument
439 obj->ce_cache = cache; in __cache_add()
441 if (cache->hashtable) { in __cache_add()
442 ret = nl_hash_table_add(cache->hashtable, obj); in __cache_add()
444 obj->ce_cache = NULL; in __cache_add()
449 nl_list_add_tail(&obj->ce_list, &cache->c_items); in __cache_add()
450 cache->c_nitems++; in __cache_add()
452 NL_DBG(3, "Added object %p to cache %p <%s>, nitems %d\n", in __cache_add()
453 obj, cache, nl_cache_name(cache), cache->c_nitems); in __cache_add()
459 * Add object to cache.
460 * @arg cache Cache
461 * @arg obj Object to be added to the cache
463 * Adds the object \p obj to the specified \p cache. In case the object
464 * is already associated with another cache, the object is cloned before
465 * adding it to the cache. In this case, the sole reference to the object
466 * will be the one of the cache. Therefore clearing/freeing the cache
469 * If the object has not been associated with a cache yet, the reference
470 * counter of the object is incremented to account for the additional
473 * The type of the object and cache must match, otherwise an error is
474 * returned (-NLE_OBJ_MISMATCH).
480 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj) in nl_cache_add() argument
485 if (cache->c_ops->co_obj_ops != obj->ce_ops) in nl_cache_add()
486 return -NLE_OBJ_MISMATCH; in nl_cache_add()
488 if (!nl_list_empty(&obj->ce_list)) { in nl_cache_add()
489 NL_DBG(3, "Object %p already in cache, cloning new object\n", obj); in nl_cache_add()
493 return -NLE_NOMEM; in nl_cache_add()
499 ret = __cache_add(cache, new); in nl_cache_add()
507 * Move object from one cache to another
508 * @arg cache Cache to move object to.
509 * @arg obj Object subject to be moved
511 * Removes the the specified object \p obj from its associated cache
512 * and moves it to another cache.
514 * If the object is not associated with a cache, the function behaves
517 * The type of the object and cache must match, otherwise an error is
518 * returned (-NLE_OBJ_MISMATCH).
524 int nl_cache_move(struct nl_cache *cache, struct nl_object *obj) in nl_cache_move() argument
526 if (cache->c_ops->co_obj_ops != obj->ce_ops) in nl_cache_move()
527 return -NLE_OBJ_MISMATCH; in nl_cache_move()
529 NL_DBG(3, "Moving object %p from cache %p to cache %p\n", in nl_cache_move()
530 obj, obj->ce_cache, cache); in nl_cache_move()
532 /* Acquire reference, if already in a cache this will be in nl_cache_move()
536 if (!nl_list_empty(&obj->ce_list)) in nl_cache_move()
539 return __cache_add(cache, obj); in nl_cache_move()
543 * Remove object from cache.
544 * @arg obj Object to remove from cache
546 * Removes the object \c obj from the cache it is associated with. The
548 * to the object was the only one remaining, the object will be freed.
550 * If no cache is associated with the object, this function is a NOP.
555 struct nl_cache *cache = obj->ce_cache; in nl_cache_remove() local
557 if (cache == NULL) in nl_cache_remove()
560 if (cache->hashtable) { in nl_cache_remove()
561 ret = nl_hash_table_del(cache->hashtable, obj); in nl_cache_remove()
563 NL_DBG(2, "Failed to delete %p from cache %p <%s>.\n", in nl_cache_remove()
564 obj, cache, nl_cache_name(cache)); in nl_cache_remove()
567 nl_list_del(&obj->ce_list); in nl_cache_remove()
568 obj->ce_cache = NULL; in nl_cache_remove()
570 cache->c_nitems--; in nl_cache_remove()
572 NL_DBG(2, "Deleted object %p from cache %p <%s>.\n", in nl_cache_remove()
573 obj, cache, nl_cache_name(cache)); in nl_cache_remove()
584 * Set synchronization arg1 of cache
585 * @arg cache Cache
588 * Synchronization arguments are used to specify filters when
591 void nl_cache_set_arg1(struct nl_cache *cache, int arg) in nl_cache_set_arg1() argument
593 cache->c_iarg1 = arg; in nl_cache_set_arg1()
597 * Set synchronization arg2 of cache
598 * @arg cache Cache
601 * Synchronization arguments are used to specify filters when
604 void nl_cache_set_arg2(struct nl_cache *cache, int arg) in nl_cache_set_arg2() argument
606 cache->c_iarg2 = arg; in nl_cache_set_arg2()
610 * Set cache flags
611 * @arg cache Cache
614 void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags) in nl_cache_set_flags() argument
616 cache->c_flags |= flags; in nl_cache_set_flags()
620 * Invoke the request-update operation
622 * @arg cache Cache
624 * This function causes the \e request-update function of the cache
625 * operations to be invoked. This usually causes a dump request to
626 * be sent over the netlink socket which triggers the kernel to dump
627 * all objects of a specific type to be dumped onto the netlink
631 * the \e request_update function of each individual type of cache.
633 * This function will not have any effects on the cache (unless the
634 * request_update implementation of the cache operations does so).
636 * Use nl_cache_pickup() to pick-up (read) the objects from the socket
637 * and fill them into the cache.
643 * the number of bytes sent. Treat any non-negative number as success too.
646 struct nl_cache *cache) in nl_cache_request_full_dump() argument
648 if (sk->s_proto != cache->c_ops->co_protocol) in nl_cache_request_full_dump()
649 return -NLE_PROTO_MISMATCH; in nl_cache_request_full_dump()
651 if (cache->c_ops->co_request_update == NULL) in nl_cache_request_full_dump()
652 return -NLE_OPNOTSUPP; in nl_cache_request_full_dump()
654 NL_DBG(2, "Requesting update from kernel for cache %p <%s>\n", in nl_cache_request_full_dump()
655 cache, nl_cache_name(cache)); in nl_cache_request_full_dump()
657 return cache->c_ops->co_request_update(cache, sk); in nl_cache_request_full_dump()
671 ret = nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params); in update_msg_parser()
672 if (ret == -NLE_EXIST) in update_msg_parser()
680 * Pick-up a netlink request-update with your own parser
682 * @arg cache Cache
685 static int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache, in __cache_pickup() argument
691 .ops = cache->c_ops, in __cache_pickup()
695 NL_DBG(2, "Picking up answer for cache %p <%s>\n", in __cache_pickup()
696 cache, nl_cache_name(cache)); in __cache_pickup()
698 cb = nl_cb_clone(sk->s_cb); in __cache_pickup()
700 return -NLE_NOMEM; in __cache_pickup()
707 cache, nl_cache_name(cache), err, nl_geterror(err)); in __cache_pickup()
716 struct nl_cache *cache = (struct nl_cache *)p->pp_arg; in pickup_checkdup_cb() local
719 old = nl_cache_search(cache, c); in pickup_checkdup_cb()
730 return nl_cache_add(cache, c); in pickup_checkdup_cb()
735 struct nl_cache *cache = p->pp_arg; in pickup_cb() local
737 return nl_cache_add(cache, c); in pickup_cb()
740 static int __nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache, in __nl_cache_pickup() argument
746 p.pp_arg = cache; in __nl_cache_pickup()
748 if (sk->s_proto != cache->c_ops->co_protocol) in __nl_cache_pickup()
749 return -NLE_PROTO_MISMATCH; in __nl_cache_pickup()
751 return __cache_pickup(sk, cache, &p); in __nl_cache_pickup()
755 * Pickup a netlink dump response and put it into a cache.
757 * @arg cache Cache to put items into.
759 * Waits for netlink messages to arrive, parses them and puts them into
760 * the specified cache. If an old object with same key attributes is
761 * present in the cache, it is replaced with the new object.
767 int nl_cache_pickup_checkdup(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_pickup_checkdup() argument
769 return __nl_cache_pickup(sk, cache, 1); in nl_cache_pickup_checkdup()
773 * Pickup a netlink dump response and put it into a cache.
775 * @arg cache Cache to put items into.
777 * Waits for netlink messages to arrive, parses them and puts them into
778 * the specified cache.
782 int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_pickup() argument
784 return __nl_cache_pickup(sk, cache, 0); in nl_cache_pickup()
787 static int cache_include(struct nl_cache *cache, struct nl_object *obj, in cache_include() argument
795 switch (type->mt_act) { in cache_include()
798 old = nl_cache_search(cache, obj); in cache_include()
800 if (cb_v2 && old->ce_ops->oo_update) { in cache_include()
806 * object with the old existing cache object. in cache_include()
811 cb_v2(cache, clone, old, diff, in cache_include()
815 cb(cache, old, NL_ACT_CHANGE, data); in cache_include()
822 if (type->mt_act == NL_ACT_DEL) { in cache_include()
824 cb_v2(cache, old, NULL, 0, NL_ACT_DEL, in cache_include()
827 cb(cache, old, NL_ACT_DEL, data); in cache_include()
832 if (type->mt_act == NL_ACT_NEW) { in cache_include()
833 nl_cache_move(cache, obj); in cache_include()
836 cb_v2(cache, NULL, obj, 0, NL_ACT_NEW, in cache_include()
839 cb(cache, obj, NL_ACT_NEW, data); in cache_include()
845 cb_v2(cache, old, obj, diff, NL_ACT_CHANGE, in cache_include()
848 cb(cache, obj, NL_ACT_CHANGE, data); in cache_include()
855 NL_DBG(2, "Unknown action associated to object %p\n", obj); in cache_include()
862 int nl_cache_include(struct nl_cache *cache, struct nl_object *obj, in nl_cache_include() argument
865 struct nl_cache_ops *ops = cache->c_ops; in nl_cache_include()
868 if (ops->co_obj_ops != obj->ce_ops) in nl_cache_include()
869 return -NLE_OBJ_MISMATCH; in nl_cache_include()
871 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) in nl_cache_include()
872 if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype) in nl_cache_include()
873 return cache_include(cache, obj, &ops->co_msgtypes[i], in nl_cache_include()
876 NL_DBG(3, "Object %p does not seem to belong to cache %p <%s>\n", in nl_cache_include()
877 obj, cache, nl_cache_name(cache)); in nl_cache_include()
879 return -NLE_MSGTYPE_NOSUPPORT; in nl_cache_include()
882 int nl_cache_include_v2(struct nl_cache *cache, struct nl_object *obj, in nl_cache_include_v2() argument
885 struct nl_cache_ops *ops = cache->c_ops; in nl_cache_include_v2()
888 if (ops->co_obj_ops != obj->ce_ops) in nl_cache_include_v2()
889 return -NLE_OBJ_MISMATCH; in nl_cache_include_v2()
891 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) in nl_cache_include_v2()
892 if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype) in nl_cache_include_v2()
893 return cache_include(cache, obj, &ops->co_msgtypes[i], in nl_cache_include_v2()
896 NL_DBG(3, "Object %p does not seem to belong to cache %p <%s>\n", in nl_cache_include_v2()
897 obj, cache, nl_cache_name(cache)); in nl_cache_include_v2()
899 return -NLE_MSGTYPE_NOSUPPORT; in nl_cache_include_v2()
904 struct nl_cache_assoc *ca = p->pp_arg; in resync_cb()
906 if (ca->ca_change_v2) in resync_cb()
907 return nl_cache_include_v2(ca->ca_cache, c, ca->ca_change_v2, in resync_cb()
908 ca->ca_change_data); in resync_cb()
910 return nl_cache_include(ca->ca_cache, c, ca->ca_change, in resync_cb()
911 ca->ca_change_data); in resync_cb()
914 int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache, in nl_cache_resync() argument
920 .ca_cache = cache, in nl_cache_resync()
930 if (sk->s_proto != cache->c_ops->co_protocol) in nl_cache_resync()
931 return -NLE_PROTO_MISMATCH; in nl_cache_resync()
933 NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache)); in nl_cache_resync()
936 nl_cache_mark_all(cache); in nl_cache_resync()
938 grp = cache->c_ops->co_groups; in nl_cache_resync()
940 if (grp && grp->ag_group && in nl_cache_resync()
941 (cache->c_flags & NL_CACHE_AF_ITER)) in nl_cache_resync()
942 nl_cache_set_arg1(cache, grp->ag_family); in nl_cache_resync()
945 err = nl_cache_request_full_dump(sk, cache); in nl_cache_resync()
949 err = __cache_pickup(sk, cache, &p); in nl_cache_resync()
950 if (err == -NLE_DUMP_INTR) in nl_cache_resync()
957 } while (grp && grp->ag_group && in nl_cache_resync()
958 (cache->c_flags & NL_CACHE_AF_ITER)); in nl_cache_resync()
960 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) { in nl_cache_resync()
965 change_cb(cache, obj, NL_ACT_DEL, data); in nl_cache_resync()
970 NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache)); in nl_cache_resync()
990 if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize)) in nl_cache_parse()
991 return -NLE_MSG_TOOSHORT; in nl_cache_parse()
993 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) { in nl_cache_parse()
994 if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) { in nl_cache_parse()
995 err = ops->co_msg_parser(ops, who, nlh, params); in nl_cache_parse()
996 if (err != -NLE_OPNOTSUPP) in nl_cache_parse()
1002 err = -NLE_MSGTYPE_NOSUPPORT; in nl_cache_parse()
1009 * Parse a netlink message and add it to the cache.
1010 * @arg cache cache to add element to
1013 * Parses a netlink message by calling the cache specific message parser
1014 * and adds the new element to the cache. If an old object with same key
1015 * attributes is present in the cache, it is replaced with the new object.
1021 int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg) in nl_cache_parse_and_add() argument
1025 .pp_arg = cache, in nl_cache_parse_and_add()
1028 return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p); in nl_cache_parse_and_add()
1032 * (Re)fill a cache with the contents in the kernel.
1034 * @arg cache cache to update
1036 * Clears the specified cache and fills it with the current state in
1041 int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_refill() argument
1046 if (sk->s_proto != cache->c_ops->co_protocol) in nl_cache_refill()
1047 return -NLE_PROTO_MISMATCH; in nl_cache_refill()
1049 nl_cache_clear(cache); in nl_cache_refill()
1050 grp = cache->c_ops->co_groups; in nl_cache_refill()
1052 if (grp && grp->ag_group && in nl_cache_refill()
1053 (cache->c_flags & NL_CACHE_AF_ITER)) in nl_cache_refill()
1054 nl_cache_set_arg1(cache, grp->ag_family); in nl_cache_refill()
1057 err = nl_cache_request_full_dump(sk, cache); in nl_cache_refill()
1061 NL_DBG(2, "Updating cache %p <%s> for family %u, request sent, waiting for reply\n", in nl_cache_refill()
1062 cache, nl_cache_name(cache), grp ? grp->ag_family : AF_UNSPEC); in nl_cache_refill()
1064 err = nl_cache_pickup(sk, cache); in nl_cache_refill()
1065 if (err == -NLE_DUMP_INTR) { in nl_cache_refill()
1073 } while (grp && grp->ag_group && in nl_cache_refill()
1074 (cache->c_flags & NL_CACHE_AF_ITER)); in nl_cache_refill()
1085 static struct nl_object *__cache_fast_lookup(struct nl_cache *cache, in __cache_fast_lookup() argument
1090 obj = nl_hash_table_lookup(cache->hashtable, needle); in __cache_fast_lookup()
1100 * Search object in cache
1101 * @arg cache Cache
1102 * @arg needle Object to look for.
1104 * Searches the cache for an object which matches the object \p needle.
1105 * The function nl_object_identical() is used to determine if the
1109 * Therefore, if an object is returned, the reference to the object
1112 * @return Reference to object or NULL if not found.
1114 struct nl_object *nl_cache_search(struct nl_cache *cache, in nl_cache_search() argument
1119 if (cache->hashtable) in nl_cache_search()
1120 return __cache_fast_lookup(cache, needle); in nl_cache_search()
1122 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_search()
1133 * Find object in cache
1134 * @arg cache Cache
1137 * Searches the cache for an object which matches the object filter.
1139 * and the cache supports hash lookups, a faster hashtable lookup
1140 * is used to return the object. Else, function nl_object_match_filter() is
1141 * used to determine if the objects match. If a matching object is
1144 * Therefore, if an object is returned, the reference to the object
1147 * @return Reference to object or NULL if not found.
1149 struct nl_object *nl_cache_find(struct nl_cache *cache, in nl_cache_find() argument
1154 if (cache->c_ops == NULL) in nl_cache_find()
1157 if ((nl_object_get_id_attrs(filter) == filter->ce_mask) in nl_cache_find()
1158 && cache->hashtable) in nl_cache_find()
1159 return __cache_fast_lookup(cache, filter); in nl_cache_find()
1161 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_find()
1172 * Mark all objects of a cache
1173 * @arg cache Cache
1175 * Marks all objects of a cache by calling nl_object_mark() on each
1176 * object associated with the cache.
1178 void nl_cache_mark_all(struct nl_cache *cache) in nl_cache_mark_all() argument
1182 NL_DBG(2, "Marking all objects in cache %p <%s>\n", in nl_cache_mark_all()
1183 cache, nl_cache_name(cache)); in nl_cache_mark_all()
1185 nl_list_for_each_entry(obj, &cache->c_items, ce_list) in nl_cache_mark_all()
1197 * Dump all elements of a cache.
1198 * @arg cache cache to dump
1201 * Dumps all elements of the \a cache to the file descriptor \a fd.
1203 void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params) in nl_cache_dump() argument
1205 nl_cache_dump_filter(cache, params, NULL); in nl_cache_dump()
1209 * Dump all elements of a cache (filtered).
1210 * @arg cache cache to dump
1214 * Dumps all elements of the \a cache to the file descriptor \a fd
1217 void nl_cache_dump_filter(struct nl_cache *cache, in nl_cache_dump_filter() argument
1226 NL_DBG(2, "Dumping cache %p <%s> with filter %p\n", in nl_cache_dump_filter()
1227 cache, nl_cache_name(cache), filter); in nl_cache_dump_filter()
1232 * try to save it. in nl_cache_dump_filter()
1235 * It only exists not to crash applications that wrongly pass %NULL here. */ in nl_cache_dump_filter()
1243 type = params->dp_type; in nl_cache_dump_filter()
1248 if (cache->c_ops == NULL) in nl_cache_dump_filter()
1251 ops = cache->c_ops->co_obj_ops; in nl_cache_dump_filter()
1252 if (!ops->oo_dump[type]) in nl_cache_dump_filter()
1255 if (params->dp_buf) in nl_cache_dump_filter()
1256 memset(params->dp_buf, 0, params->dp_buflen); in nl_cache_dump_filter()
1258 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_dump_filter()
1275 * Call a callback on each element of the cache.
1276 * @arg cache cache to iterate on
1278 * @arg arg argument passed to callback function
1280 * Calls a callback function \a cb on each element of the \a cache.
1283 void nl_cache_foreach(struct nl_cache *cache, in nl_cache_foreach() argument
1286 nl_cache_foreach_filter(cache, NULL, cb, arg); in nl_cache_foreach()
1290 * Call a callback on each element of the cache (filtered).
1291 * @arg cache cache to iterate on
1294 * @arg arg argument passed to callback function
1296 * Calls a callback function \a cb on each element of the \a cache
1298 * to the callback function.
1300 void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, in nl_cache_foreach_filter() argument
1305 if (cache->c_ops == NULL) in nl_cache_foreach_filter()
1308 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) { in nl_cache_foreach_filter()
1312 NL_DBG(3, "%p<->%p object difference: %x\n", in nl_cache_foreach_filter()