Lines Matching full:cache
2 * lib/cache.c Caching Module
14 * @defgroup cache Cache
17 * Cache Management | | Type Specific Cache Operations
26 * 2) destroy old cache +----------- pp_cb ---------|---+
45 #include <netlink/cache.h>
55 * Return the number of items in the cache
56 * @arg cache cache handle
58 int nl_cache_nitems(struct nl_cache *cache) in nl_cache_nitems() argument
60 return cache->c_nitems; in nl_cache_nitems()
64 * Return the number of items matching a filter in the cache
65 * @arg cache Cache object.
68 int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter) in nl_cache_nitems_filter() argument
74 if (cache->c_ops == NULL) in nl_cache_nitems_filter()
77 ops = cache->c_ops->co_obj_ops; in nl_cache_nitems_filter()
79 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_nitems_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) in nl_cache_is_empty() argument
96 return nl_list_empty(&cache->c_items); in nl_cache_is_empty()
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) in nl_cache_get_ops() argument
105 return cache->c_ops; in nl_cache_get_ops()
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) in nl_cache_get_first() argument
114 if (nl_list_empty(&cache->c_items)) in nl_cache_get_first()
117 return nl_list_entry(cache->c_items.next, in nl_cache_get_first()
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) in nl_cache_get_last() argument
127 if (nl_list_empty(&cache->c_items)) in nl_cache_get_last()
130 return nl_list_entry(cache->c_items.prev, in nl_cache_get_last()
135 * Return the next element in the cache
148 * Return the previous element in the cache
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.
175 struct nl_cache *cache; in nl_cache_alloc() local
177 cache = calloc(1, sizeof(*cache)); in nl_cache_alloc()
178 if (!cache) in nl_cache_alloc()
181 nl_init_list_head(&cache->c_items); in nl_cache_alloc()
182 cache->c_ops = ops; in nl_cache_alloc()
184 NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache)); in nl_cache_alloc()
186 return cache; in nl_cache_alloc()
192 struct nl_cache *cache; in nl_cache_alloc_and_fill() local
195 if (!(cache = nl_cache_alloc(ops))) in nl_cache_alloc_and_fill()
198 if (sock && (err = nl_cache_refill(sock, cache)) < 0) { in nl_cache_alloc_and_fill()
199 nl_cache_free(cache); in nl_cache_alloc_and_fill()
203 *result = cache; in nl_cache_alloc_and_fill()
208 * Allocate an empty cache based on type name
209 * @arg kind Name of cache type
210 * @return A newly allocated and initialized cache.
215 struct nl_cache *cache; in nl_cache_alloc_name() local
221 if (!(cache = nl_cache_alloc(ops))) in nl_cache_alloc_name()
224 *result = cache; in nl_cache_alloc_name()
229 * Allocate a new cache containing a subset of a cache
230 * @arg orig Original cache to be based on
231 * @arg filter Filter defining the subset to be filled into new cache
232 * @return A newly allocated cache or NULL.
237 struct nl_cache *cache; in nl_cache_subset() local
244 cache = nl_cache_alloc(orig->c_ops); in nl_cache_subset()
245 if (!cache) in nl_cache_subset()
254 nl_cache_add(cache, obj); in nl_cache_subset()
257 return cache; in nl_cache_subset()
261 * Clear a cache.
262 * @arg cache cache to clear
264 * Removes all elements of a cache.
266 void nl_cache_clear(struct nl_cache *cache) in nl_cache_clear() argument
270 NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache)); in nl_cache_clear()
272 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) in nl_cache_clear()
277 * Free a cache.
278 * @arg cache Cache to free.
280 * Removes all elements of a cache and frees all memory.
284 void nl_cache_free(struct nl_cache *cache) in nl_cache_free() argument
286 if (!cache) in nl_cache_free()
289 nl_cache_clear(cache); in nl_cache_free()
290 NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache)); in nl_cache_free()
291 free(cache); in nl_cache_free()
297 * @name Cache Modifications
301 static int __cache_add(struct nl_cache *cache, struct nl_object *obj) in __cache_add() argument
303 obj->ce_cache = cache; in __cache_add()
305 nl_list_add_tail(&obj->ce_list, &cache->c_items); in __cache_add()
306 cache->c_nitems++; in __cache_add()
308 NL_DBG(1, "Added %p to cache %p <%s>.\n", in __cache_add()
309 obj, cache, nl_cache_name(cache)); in __cache_add()
315 * Add object to a cache.
316 * @arg cache Cache to add object to
317 * @arg obj Object to be added to the cache
319 * Adds the given object to the specified cache. The object is cloned
320 * if it has been added to another cache already.
324 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj) in nl_cache_add() argument
328 if (cache->c_ops->co_obj_ops != obj->ce_ops) in nl_cache_add()
340 return __cache_add(cache, new); in nl_cache_add()
344 * Move object from one cache to another
345 * @arg cache Cache to move object to.
348 * Removes the given object from its associated cache if needed
349 * and adds it to the new cache.
353 int nl_cache_move(struct nl_cache *cache, struct nl_object *obj) in nl_cache_move() argument
355 if (cache->c_ops->co_obj_ops != obj->ce_ops) in nl_cache_move()
358 NL_DBG(3, "Moving object %p to cache %p\n", obj, cache); in nl_cache_move()
360 /* Acquire reference, if already in a cache this will be in nl_cache_move()
367 return __cache_add(cache, obj); in nl_cache_move()
371 * Removes an object from a cache.
372 * @arg obj Object to remove from its cache
374 * Removes the object \c obj from the cache it is assigned to, since
375 * an object can only be assigned to one cache at a time, the cache
380 struct nl_cache *cache = obj->ce_cache; in nl_cache_remove() local
382 if (cache == NULL) in nl_cache_remove()
388 cache->c_nitems--; in nl_cache_remove()
390 NL_DBG(1, "Deleted %p from cache %p <%s>.\n", in nl_cache_remove()
391 obj, cache, nl_cache_name(cache)); in nl_cache_remove()
395 * Search for an object in a cache
396 * @arg cache Cache to search in.
399 * Iterates over the cache and looks for an object with identical
405 struct nl_object *nl_cache_search(struct nl_cache *cache, in nl_cache_search() argument
410 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_search()
429 * Request a full dump from the kernel to fill a cache
431 * @arg cache Cache subjected to be filled.
434 * related to the specified cache to the netlink socket.
437 * into a cache.
439 int nl_cache_request_full_dump(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_request_full_dump() argument
441 NL_DBG(2, "Requesting dump from kernel for cache %p <%s>...\n", in nl_cache_request_full_dump()
442 cache, nl_cache_name(cache)); in nl_cache_request_full_dump()
444 if (cache->c_ops->co_request_update == NULL) in nl_cache_request_full_dump()
447 return cache->c_ops->co_request_update(cache, sk); in nl_cache_request_full_dump()
464 int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache, in __cache_pickup() argument
470 .ops = cache->c_ops, in __cache_pickup()
474 NL_DBG(1, "Picking up answer for cache %p <%s>...\n", in __cache_pickup()
475 cache, nl_cache_name(cache)); in __cache_pickup()
486 "%d: %s", cache, nl_cache_name(cache), in __cache_pickup()
500 * Pickup a netlink dump response and put it into a cache.
502 * @arg cache Cache to put items into.
505 * the specified cache.
509 int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_pickup() argument
513 .pp_arg = cache, in nl_cache_pickup()
516 return __cache_pickup(sk, cache, &p); in nl_cache_pickup()
519 static int cache_include(struct nl_cache *cache, struct nl_object *obj, in cache_include() argument
527 old = nl_cache_search(cache, obj); in cache_include()
532 cb(cache, old, NL_ACT_DEL, data); in cache_include()
538 nl_cache_move(cache, obj); in cache_include()
540 cb(cache, obj, NL_ACT_NEW, data); in cache_include()
543 cb(cache, obj, NL_ACT_CHANGE, data); in cache_include()
557 int nl_cache_include(struct nl_cache *cache, struct nl_object *obj, in nl_cache_include() argument
560 struct nl_cache_ops *ops = cache->c_ops; in nl_cache_include()
568 return cache_include(cache, obj, &ops->co_msgtypes[i], in nl_cache_include()
581 int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache, in nl_cache_resync() argument
586 .ca_cache = cache, in nl_cache_resync()
596 NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache)); in nl_cache_resync()
599 nl_cache_mark_all(cache); in nl_cache_resync()
601 err = nl_cache_request_full_dump(sk, cache); in nl_cache_resync()
605 err = __cache_pickup(sk, cache, &p); in nl_cache_resync()
609 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) { in nl_cache_resync()
614 change_cb(cache, obj, NL_ACT_DEL, data); in nl_cache_resync()
619 NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache)); in nl_cache_resync()
658 * Parse a netlink message and add it to the cache.
659 * @arg cache cache to add element to
662 * Parses a netlink message by calling the cache specific message parser
663 * and adds the new element to the cache.
667 int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg) in nl_cache_parse_and_add() argument
671 .pp_arg = cache, in nl_cache_parse_and_add()
674 return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p); in nl_cache_parse_and_add()
678 * (Re)fill a cache with the contents in the kernel.
680 * @arg cache cache to update
682 * Clears the specified cache and fills it with the current state in
687 int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_refill() argument
691 err = nl_cache_request_full_dump(sk, cache); in nl_cache_refill()
695 NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n", in nl_cache_refill()
696 cache, nl_cache_name(cache)); in nl_cache_refill()
697 nl_cache_clear(cache); in nl_cache_refill()
699 return nl_cache_pickup(sk, cache); in nl_cache_refill()
710 * Mark all objects in a cache
711 * @arg cache Cache to mark all objects in
713 void nl_cache_mark_all(struct nl_cache *cache) in nl_cache_mark_all() argument
717 NL_DBG(2, "Marking all objects in cache %p <%s>...\n", in nl_cache_mark_all()
718 cache, nl_cache_name(cache)); in nl_cache_mark_all()
720 nl_list_for_each_entry(obj, &cache->c_items, ce_list) in nl_cache_mark_all()
732 * Dump all elements of a cache.
733 * @arg cache cache to dump
736 * Dumps all elements of the \a cache to the file descriptor \a fd.
738 void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params) in nl_cache_dump() argument
740 nl_cache_dump_filter(cache, params, NULL); in nl_cache_dump()
744 * Dump all elements of a cache (filtered).
745 * @arg cache cache to dump
749 * Dumps all elements of the \a cache to the file descriptor \a fd
752 void nl_cache_dump_filter(struct nl_cache *cache, in nl_cache_dump_filter() argument
760 NL_DBG(2, "Dumping cache %p <%s> filter %p\n", in nl_cache_dump_filter()
761 cache, nl_cache_name(cache), filter); in nl_cache_dump_filter()
766 if (cache->c_ops == NULL) in nl_cache_dump_filter()
769 ops = cache->c_ops->co_obj_ops; in nl_cache_dump_filter()
773 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_dump_filter()
790 * Call a callback on each element of the cache.
791 * @arg cache cache to iterate on
795 * Calls a callback function \a cb on each element of the \a cache.
798 void nl_cache_foreach(struct nl_cache *cache, in nl_cache_foreach() argument
801 nl_cache_foreach_filter(cache, NULL, cb, arg); in nl_cache_foreach()
805 * Call a callback on each element of the cache (filtered).
806 * @arg cache cache to iterate on
811 * Calls a callback function \a cb on each element of the \a cache
815 void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, in nl_cache_foreach_filter() argument
821 if (cache->c_ops == NULL) in nl_cache_foreach_filter()
824 ops = cache->c_ops->co_obj_ops; in nl_cache_foreach_filter()
826 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) { in nl_cache_foreach_filter()