• Home
  • Raw
  • Download

Lines Matching full:cache

3  * lib/cache.c		Caching Module
15 * @defgroup cache Cache
18 * Cache Management | | Type Specific Cache Operations
27 * 2) destroy old cache +----------- pp_cb ---------|---+
49 * #include <netlink/cache.h>
55 #include <netlink/cache.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
156 * Return the previous element in the cache
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()
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()
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
274 cache = nl_cache_alloc(ops); in nl_cache_alloc_name()
276 if (!cache) 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.
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()
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.
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
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.
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()
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
473 * The type of the object and cache must match, otherwise an error is
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()
489 NL_DBG(3, "Object %p already in cache, cloning new object\n", obj); 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.
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
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()
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()
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
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()
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
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
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()
622 * @arg cache Cache
624 * This function causes the \e request-update function of the cache
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).
637 * and fill them into the cache.
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()
651 if (cache->c_ops->co_request_update == NULL) 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()
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()
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()
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.
760 * the specified cache.
764 int nl_cache_pickup_checkdup(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_pickup_checkdup() argument
766 return __nl_cache_pickup(sk, cache, 1); in nl_cache_pickup_checkdup()
770 * Pickup a netlink dump response and put it into a cache.
772 * @arg cache Cache to put items into.
775 * the specified cache. If an old object with same key attributes is
776 * present in the cache, it is replaced with the new object.
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
798 old = nl_cache_search(cache, obj); in cache_include()
806 * object with the old existing cache object. in cache_include()
811 cb_v2(cache, clone, obj, diff, in cache_include()
815 cb(cache, old, NL_ACT_CHANGE, data); 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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
1049 nl_cache_clear(cache); in nl_cache_refill()
1050 grp = cache->c_ops->co_groups; 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()
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
1104 * Searches the cache for an object which matches the object \p needle.
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
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()
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
1225 NL_DBG(2, "Dumping cache %p <%s> with filter %p\n", in nl_cache_dump_filter()
1226 cache, nl_cache_name(cache), filter); in nl_cache_dump_filter()
1231 if (cache->c_ops == NULL) in nl_cache_dump_filter()
1234 ops = cache->c_ops->co_obj_ops; in nl_cache_dump_filter()
1241 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_dump_filter()
1258 * Call a callback on each element of the cache.
1259 * @arg cache cache to iterate on
1263 * Calls a callback function \a cb on each element of the \a cache.
1266 void nl_cache_foreach(struct nl_cache *cache, in nl_cache_foreach() argument
1269 nl_cache_foreach_filter(cache, NULL, cb, arg); in nl_cache_foreach()
1273 * Call a callback on each element of the cache (filtered).
1274 * @arg cache cache to iterate on
1279 * Calls a callback function \a cb on each element of the \a cache
1283 void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, in nl_cache_foreach_filter() argument
1288 if (cache->c_ops == NULL) in nl_cache_foreach_filter()
1291 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) { in nl_cache_foreach_filter()