Lines Matching full:cache
8 * @defgroup cache Cache
11 * Cache Management | | Type Specific Cache Operations
20 * 2) destroy old cache +----------- pp_cb ---------|---+
42 * #include <netlink/cache.h>
49 #include <netlink/cache.h>
60 * Return the number of items in the cache
61 * @arg cache cache handle
63 int nl_cache_nitems(struct nl_cache *cache) in nl_cache_nitems() argument
65 return cache->c_nitems; in nl_cache_nitems()
69 * Return the number of items matching a filter in the cache
70 * @arg cache Cache object.
73 int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter) in nl_cache_nitems_filter() argument
78 if (cache->c_ops == NULL) in nl_cache_nitems_filter()
81 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_nitems_filter()
92 * Returns \b true if the cache is empty.
93 * @arg cache Cache to check
94 * @return \a true if the cache is empty, otherwise \b false is returned.
96 int nl_cache_is_empty(struct nl_cache *cache) in nl_cache_is_empty() argument
98 return nl_list_empty(&cache->c_items); in nl_cache_is_empty()
102 * Return the operations set of the cache
103 * @arg cache cache handle
105 struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache) in nl_cache_get_ops() argument
107 return cache->c_ops; in nl_cache_get_ops()
111 * Return the first element in the cache
112 * @arg cache cache handle
114 struct nl_object *nl_cache_get_first(struct nl_cache *cache) in nl_cache_get_first() argument
116 if (nl_list_empty(&cache->c_items)) in nl_cache_get_first()
119 return nl_list_entry(cache->c_items.next, in nl_cache_get_first()
124 * Return the last element in the cache
125 * @arg cache cache handle
127 struct nl_object *nl_cache_get_last(struct nl_cache *cache) in nl_cache_get_last() argument
129 if (nl_list_empty(&cache->c_items)) in nl_cache_get_last()
132 return nl_list_entry(cache->c_items.prev, in nl_cache_get_last()
137 * Return the next element in the cache
150 * Return the previous element in the cache
165 * @name Cache Allocation/Deletion
170 * Allocate new cache
171 * @arg ops Cache operations
173 * Allocate and initialize a new cache based on the cache operations
176 * @return Allocated cache or NULL if allocation failed.
180 struct nl_cache *cache; in nl_cache_alloc() local
182 cache = calloc(1, sizeof(*cache)); in nl_cache_alloc()
183 if (!cache) in nl_cache_alloc()
186 nl_init_list_head(&cache->c_items); in nl_cache_alloc()
187 cache->c_ops = ops; in nl_cache_alloc()
188 cache->c_flags |= ops->co_flags; in nl_cache_alloc()
189 cache->c_refcnt = 1; in nl_cache_alloc()
194 * cache objects for faster lookups in nl_cache_alloc()
204 cache->hashtable = nl_hash_table_alloc(hashtable_size); in nl_cache_alloc()
207 NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache)); in nl_cache_alloc()
209 return cache; in nl_cache_alloc()
213 * Allocate new cache and fill it
214 * @arg ops Cache operations
218 * Allocate new cache and fill it. Equivalent to calling:
220 * cache = nl_cache_alloc(ops);
221 * nl_cache_refill(sock, cache);
231 struct nl_cache *cache; in nl_cache_alloc_and_fill() local
234 if (!(cache = nl_cache_alloc(ops))) in nl_cache_alloc_and_fill()
237 if (sock && (err = nl_cache_refill(sock, cache)) < 0) { in nl_cache_alloc_and_fill()
238 nl_cache_free(cache); in nl_cache_alloc_and_fill()
242 *result = cache; in nl_cache_alloc_and_fill()
247 * Allocate new cache based on type name
248 * @arg kind Name of cache type
251 * Lookup cache ops via nl_cache_ops_lookup() and allocate the cache
252 * by calling nl_cache_alloc(). Stores the allocated cache in the
262 struct nl_cache *cache; in nl_cache_alloc_name() local
268 cache = nl_cache_alloc(ops); in nl_cache_alloc_name()
270 if (!cache) in nl_cache_alloc_name()
273 *result = cache; in nl_cache_alloc_name()
278 * Allocate new cache containing a subset of an existing cache
279 * @arg orig Original cache to base new cache on
280 * @arg filter Filter defining the subset to be filled into the new cache
282 * Allocates a new cache matching the type of the cache specified by
283 * \p orig. Iterates over the \p orig cache applying the specified
284 * \p filter and copies all objects that match to the new cache.
287 * other. Later modifications to objects in the original cache will
288 * not affect objects in the new cache.
290 * @return A newly allocated cache or NULL.
295 struct nl_cache *cache; in nl_cache_subset() local
301 cache = nl_cache_alloc(orig->c_ops); in nl_cache_subset()
302 if (!cache) in nl_cache_subset()
305 NL_DBG(2, "Filling subset of cache %p <%s> with filter %p into %p\n", in nl_cache_subset()
306 orig, nl_cache_name(orig), filter, cache); in nl_cache_subset()
312 nl_cache_add(cache, obj); in nl_cache_subset()
315 return cache; in nl_cache_subset()
319 * Allocate new cache and copy the contents of an existing cache
320 * @arg cache Original cache to base new cache on
322 * Allocates a new cache matching the type of the cache specified by
323 * \p cache. Iterates over the \p cache cache and copies all objects
324 * to the new cache.
327 * other. Later modifications to objects in the original cache will
328 * not affect objects in the new cache.
330 * @return A newly allocated cache or NULL.
332 struct nl_cache *nl_cache_clone(struct nl_cache *cache) in nl_cache_clone() argument
334 struct nl_cache_ops *ops = nl_cache_get_ops(cache); in nl_cache_clone()
342 NL_DBG(2, "Cloning %p into %p\n", cache, clone); in nl_cache_clone()
344 nl_list_for_each_entry(obj, &cache->c_items, ce_list) in nl_cache_clone()
351 * Remove all objects of a cache.
352 * @arg cache Cache to clear
354 * The objects are unliked/removed from the cache by calling
355 * nl_cache_remove() on each object in the cache. If any of the objects
359 * Unlike with nl_cache_free(), the cache is not freed just emptied.
361 void nl_cache_clear(struct nl_cache *cache) in nl_cache_clear() argument
365 NL_DBG(2, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache)); in nl_cache_clear()
367 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) in nl_cache_clear()
371 static void __nl_cache_free(struct nl_cache *cache) in __nl_cache_free() argument
373 nl_cache_clear(cache); in __nl_cache_free()
375 if (cache->hashtable) in __nl_cache_free()
376 nl_hash_table_free(cache->hashtable); in __nl_cache_free()
378 NL_DBG(2, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache)); in __nl_cache_free()
379 free(cache); in __nl_cache_free()
383 * Increase reference counter of cache
384 * @arg cache Cache
386 void nl_cache_get(struct nl_cache *cache) in nl_cache_get() argument
388 cache->c_refcnt++; in nl_cache_get()
390 NL_DBG(3, "Incremented cache %p <%s> reference count to %d\n", in nl_cache_get()
391 cache, nl_cache_name(cache), cache->c_refcnt); in nl_cache_get()
395 * Free a cache.
396 * @arg cache Cache to free.
399 * cache and frees the cache afterwards.
403 void nl_cache_free(struct nl_cache *cache) in nl_cache_free() argument
405 if (!cache) in nl_cache_free()
408 cache->c_refcnt--; in nl_cache_free()
410 NL_DBG(3, "Decremented cache %p <%s> reference count, %d remaining\n", in nl_cache_free()
411 cache, nl_cache_name(cache), cache->c_refcnt); in nl_cache_free()
413 if (cache->c_refcnt <= 0) in nl_cache_free()
414 __nl_cache_free(cache); in nl_cache_free()
417 void nl_cache_put(struct nl_cache *cache) in nl_cache_put() argument
419 nl_cache_free(cache); in nl_cache_put()
425 * @name Cache Modifications
429 static int __cache_add(struct nl_cache *cache, struct nl_object *obj) in __cache_add() argument
433 obj->ce_cache = cache; in __cache_add()
435 if (cache->hashtable) { in __cache_add()
436 ret = nl_hash_table_add(cache->hashtable, obj); in __cache_add()
443 nl_list_add_tail(&obj->ce_list, &cache->c_items); in __cache_add()
444 cache->c_nitems++; in __cache_add()
446 NL_DBG(3, "Added object %p to cache %p <%s>, nitems %d\n", in __cache_add()
447 obj, cache, nl_cache_name(cache), cache->c_nitems); in __cache_add()
453 * Add object to cache.
454 * @arg cache Cache
455 * @arg obj Object to be added to the cache
457 * Adds the object \p obj to the specified \p cache. In case the object
458 * is already associated with another cache, the object is cloned before
459 * adding it to the cache. In this case, the sole reference to the object
460 * will be the one of the cache. Therefore clearing/freeing the cache
463 * If the object has not been associated with a cache yet, the reference
467 * The type of the object and cache must match, otherwise an error is
474 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj) in nl_cache_add() argument
479 if (cache->c_ops->co_obj_ops != obj->ce_ops) in nl_cache_add()
483 NL_DBG(3, "Object %p already in cache, cloning new object\n", obj); in nl_cache_add()
493 ret = __cache_add(cache, new); in nl_cache_add()
501 * Move object from one cache to another
502 * @arg cache Cache to move object to.
505 * Removes the the specified object \p obj from its associated cache
506 * and moves it to another cache.
508 * If the object is not associated with a cache, the function behaves
511 * The type of the object and cache must match, otherwise an error is
518 int nl_cache_move(struct nl_cache *cache, struct nl_object *obj) in nl_cache_move() argument
520 if (cache->c_ops->co_obj_ops != obj->ce_ops) in nl_cache_move()
523 NL_DBG(3, "Moving object %p from cache %p to cache %p\n", in nl_cache_move()
524 obj, obj->ce_cache, cache); in nl_cache_move()
526 /* Acquire reference, if already in a cache this will be in nl_cache_move()
533 return __cache_add(cache, obj); in nl_cache_move()
537 * Remove object from cache.
538 * @arg obj Object to remove from cache
540 * Removes the object \c obj from the cache it is associated with. The
544 * If no cache is associated with the object, this function is a NOP.
549 struct nl_cache *cache = obj->ce_cache; in nl_cache_remove() local
551 if (cache == NULL) in nl_cache_remove()
554 if (cache->hashtable) { in nl_cache_remove()
555 ret = nl_hash_table_del(cache->hashtable, obj); in nl_cache_remove()
557 NL_DBG(2, "Failed to delete %p from cache %p <%s>.\n", in nl_cache_remove()
558 obj, cache, nl_cache_name(cache)); in nl_cache_remove()
564 cache->c_nitems--; in nl_cache_remove()
566 NL_DBG(2, "Deleted object %p from cache %p <%s>.\n", in nl_cache_remove()
567 obj, cache, nl_cache_name(cache)); in nl_cache_remove()
578 * Set synchronization arg1 of cache
579 * @arg cache Cache
585 void nl_cache_set_arg1(struct nl_cache *cache, int arg) in nl_cache_set_arg1() argument
587 cache->c_iarg1 = arg; in nl_cache_set_arg1()
591 * Set synchronization arg2 of cache
592 * @arg cache Cache
598 void nl_cache_set_arg2(struct nl_cache *cache, int arg) in nl_cache_set_arg2() argument
600 cache->c_iarg2 = arg; in nl_cache_set_arg2()
604 * Set cache flags
605 * @arg cache Cache
608 void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags) in nl_cache_set_flags() argument
610 cache->c_flags |= flags; in nl_cache_set_flags()
616 * @arg cache Cache
618 * This function causes the \e request-update function of the cache
625 * the \e request_update function of each individual type of cache.
627 * This function will not have any effects on the cache (unless the
628 * request_update implementation of the cache operations does so).
631 * and fill them into the cache.
640 struct nl_cache *cache) in nl_cache_request_full_dump() argument
642 if (sk->s_proto != cache->c_ops->co_protocol) in nl_cache_request_full_dump()
645 if (cache->c_ops->co_request_update == NULL) in nl_cache_request_full_dump()
648 NL_DBG(2, "Requesting update from kernel for cache %p <%s>\n", in nl_cache_request_full_dump()
649 cache, nl_cache_name(cache)); in nl_cache_request_full_dump()
651 return cache->c_ops->co_request_update(cache, sk); in nl_cache_request_full_dump()
676 * @arg cache Cache
679 static int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache, in __cache_pickup() argument
685 .ops = cache->c_ops, in __cache_pickup()
689 NL_DBG(2, "Picking up answer for cache %p <%s>\n", in __cache_pickup()
690 cache, nl_cache_name(cache)); in __cache_pickup()
701 cache, nl_cache_name(cache), err, nl_geterror(err)); in __cache_pickup()
710 struct nl_cache *cache = (struct nl_cache *)p->pp_arg; in pickup_checkdup_cb() local
713 old = nl_cache_search(cache, c); in pickup_checkdup_cb()
724 return nl_cache_add(cache, c); in pickup_checkdup_cb()
729 struct nl_cache *cache = p->pp_arg; in pickup_cb() local
731 return nl_cache_add(cache, c); in pickup_cb()
734 static int __nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache, in __nl_cache_pickup() argument
740 p.pp_arg = cache; in __nl_cache_pickup()
742 if (sk->s_proto != cache->c_ops->co_protocol) in __nl_cache_pickup()
745 return __cache_pickup(sk, cache, &p); in __nl_cache_pickup()
749 * Pickup a netlink dump response and put it into a cache.
751 * @arg cache Cache to put items into.
754 * the specified cache. If an old object with same key attributes is
755 * present in the cache, it is replaced with the new object.
761 int nl_cache_pickup_checkdup(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_pickup_checkdup() argument
763 return __nl_cache_pickup(sk, cache, 1); in nl_cache_pickup_checkdup()
767 * Pickup a netlink dump response and put it into a cache.
769 * @arg cache Cache to put items into.
772 * the specified cache.
776 int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_pickup() argument
778 return __nl_cache_pickup(sk, cache, 0); in nl_cache_pickup()
781 static int cache_include(struct nl_cache *cache, struct nl_object *obj, in cache_include() argument
792 old = nl_cache_search(cache, obj); in cache_include()
800 * object with the old existing cache object. in cache_include()
805 cb_v2(cache, clone, obj, diff, in cache_include()
809 cb(cache, old, NL_ACT_CHANGE, data); in cache_include()
818 cb_v2(cache, old, NULL, 0, NL_ACT_DEL, in cache_include()
821 cb(cache, old, NL_ACT_DEL, data); in cache_include()
827 nl_cache_move(cache, obj); in cache_include()
830 cb_v2(cache, NULL, obj, 0, NL_ACT_NEW, in cache_include()
833 cb(cache, obj, NL_ACT_NEW, data); in cache_include()
839 cb_v2(cache, old, obj, diff, NL_ACT_CHANGE, in cache_include()
842 cb(cache, obj, NL_ACT_CHANGE, data); in cache_include()
856 int nl_cache_include(struct nl_cache *cache, struct nl_object *obj, in nl_cache_include() argument
859 struct nl_cache_ops *ops = cache->c_ops; in nl_cache_include()
867 return cache_include(cache, obj, &ops->co_msgtypes[i], in nl_cache_include()
870 NL_DBG(3, "Object %p does not seem to belong to cache %p <%s>\n", in nl_cache_include()
871 obj, cache, nl_cache_name(cache)); in nl_cache_include()
876 int nl_cache_include_v2(struct nl_cache *cache, struct nl_object *obj, in nl_cache_include_v2() argument
879 struct nl_cache_ops *ops = cache->c_ops; in nl_cache_include_v2()
887 return cache_include(cache, obj, &ops->co_msgtypes[i], in nl_cache_include_v2()
890 NL_DBG(3, "Object %p does not seem to belong to cache %p <%s>\n", in nl_cache_include_v2()
891 obj, cache, nl_cache_name(cache)); in nl_cache_include_v2()
908 int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache, in nl_cache_resync() argument
914 .ca_cache = cache, in nl_cache_resync()
924 if (sk->s_proto != cache->c_ops->co_protocol) in nl_cache_resync()
927 NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache)); in nl_cache_resync()
930 nl_cache_mark_all(cache); in nl_cache_resync()
932 grp = cache->c_ops->co_groups; in nl_cache_resync()
935 (cache->c_flags & NL_CACHE_AF_ITER)) in nl_cache_resync()
936 nl_cache_set_arg1(cache, grp->ag_family); in nl_cache_resync()
939 err = nl_cache_request_full_dump(sk, cache); in nl_cache_resync()
943 err = __cache_pickup(sk, cache, &p); in nl_cache_resync()
952 (cache->c_flags & NL_CACHE_AF_ITER)); in nl_cache_resync()
954 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) { in nl_cache_resync()
959 change_cb(cache, obj, NL_ACT_DEL, data); in nl_cache_resync()
964 NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache)); in nl_cache_resync()
1003 * Parse a netlink message and add it to the cache.
1004 * @arg cache cache to add element to
1007 * Parses a netlink message by calling the cache specific message parser
1008 * and adds the new element to the cache. If an old object with same key
1009 * attributes is present in the cache, it is replaced with the new object.
1015 int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg) in nl_cache_parse_and_add() argument
1019 .pp_arg = cache, in nl_cache_parse_and_add()
1022 return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p); in nl_cache_parse_and_add()
1026 * (Re)fill a cache with the contents in the kernel.
1028 * @arg cache cache to update
1030 * Clears the specified cache and fills it with the current state in
1035 int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_refill() argument
1040 if (sk->s_proto != cache->c_ops->co_protocol) in nl_cache_refill()
1043 nl_cache_clear(cache); in nl_cache_refill()
1044 grp = cache->c_ops->co_groups; in nl_cache_refill()
1047 (cache->c_flags & NL_CACHE_AF_ITER)) in nl_cache_refill()
1048 nl_cache_set_arg1(cache, grp->ag_family); in nl_cache_refill()
1051 err = nl_cache_request_full_dump(sk, cache); in nl_cache_refill()
1055 NL_DBG(2, "Updating cache %p <%s> for family %u, request sent, waiting for reply\n", in nl_cache_refill()
1056 cache, nl_cache_name(cache), grp ? grp->ag_family : AF_UNSPEC); in nl_cache_refill()
1058 err = nl_cache_pickup(sk, cache); in nl_cache_refill()
1068 (cache->c_flags & NL_CACHE_AF_ITER)); in nl_cache_refill()
1079 static struct nl_object *__cache_fast_lookup(struct nl_cache *cache, in __cache_fast_lookup() argument
1084 obj = nl_hash_table_lookup(cache->hashtable, needle); in __cache_fast_lookup()
1094 * Search object in cache
1095 * @arg cache Cache
1098 * Searches the cache for an object which matches the object \p needle.
1108 struct nl_object *nl_cache_search(struct nl_cache *cache, in nl_cache_search() argument
1113 if (cache->hashtable) in nl_cache_search()
1114 return __cache_fast_lookup(cache, needle); in nl_cache_search()
1116 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_search()
1127 * Find object in cache
1128 * @arg cache Cache
1131 * Searches the cache for an object which matches the object filter.
1133 * and the cache supports hash lookups, a faster hashtable lookup
1143 struct nl_object *nl_cache_find(struct nl_cache *cache, in nl_cache_find() argument
1148 if (cache->c_ops == NULL) in nl_cache_find()
1152 && cache->hashtable) in nl_cache_find()
1153 return __cache_fast_lookup(cache, filter); in nl_cache_find()
1155 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_find()
1166 * Mark all objects of a cache
1167 * @arg cache Cache
1169 * Marks all objects of a cache by calling nl_object_mark() on each
1170 * object associated with the cache.
1172 void nl_cache_mark_all(struct nl_cache *cache) in nl_cache_mark_all() argument
1176 NL_DBG(2, "Marking all objects in cache %p <%s>\n", in nl_cache_mark_all()
1177 cache, nl_cache_name(cache)); in nl_cache_mark_all()
1179 nl_list_for_each_entry(obj, &cache->c_items, ce_list) in nl_cache_mark_all()
1191 * Dump all elements of a cache.
1192 * @arg cache cache to dump
1195 * Dumps all elements of the \a cache to the file descriptor \a fd.
1197 void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params) in nl_cache_dump() argument
1199 nl_cache_dump_filter(cache, params, NULL); in nl_cache_dump()
1203 * Dump all elements of a cache (filtered).
1204 * @arg cache cache to dump
1208 * Dumps all elements of the \a cache to the file descriptor \a fd
1211 void nl_cache_dump_filter(struct nl_cache *cache, in nl_cache_dump_filter() argument
1220 NL_DBG(2, "Dumping cache %p <%s> with filter %p\n", in nl_cache_dump_filter()
1221 cache, nl_cache_name(cache), filter); in nl_cache_dump_filter()
1242 if (cache->c_ops == NULL) in nl_cache_dump_filter()
1245 ops = cache->c_ops->co_obj_ops; in nl_cache_dump_filter()
1252 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_dump_filter()
1269 * Call a callback on each element of the cache.
1270 * @arg cache cache to iterate on
1274 * Calls a callback function \a cb on each element of the \a cache.
1277 void nl_cache_foreach(struct nl_cache *cache, in nl_cache_foreach() argument
1280 nl_cache_foreach_filter(cache, NULL, cb, arg); in nl_cache_foreach()
1284 * Call a callback on each element of the cache (filtered).
1285 * @arg cache cache to iterate on
1290 * Calls a callback function \a cb on each element of the \a cache
1294 void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, in nl_cache_foreach_filter() argument
1299 if (cache->c_ops == NULL) in nl_cache_foreach_filter()
1302 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) { in nl_cache_foreach_filter()