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 ---------|---+
48 * #include <netlink/cache.h>
54 #include <netlink/cache.h>
65 * Return the number of items in the cache
66 * @arg cache cache handle
68 int nl_cache_nitems(struct nl_cache *cache) in nl_cache_nitems() argument
70 return cache->c_nitems; in nl_cache_nitems()
74 * Return the number of items matching a filter in the cache
75 * @arg cache Cache object.
78 int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter) in nl_cache_nitems_filter() argument
83 if (cache->c_ops == NULL) in nl_cache_nitems_filter()
86 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_nitems_filter()
97 * Returns \b true if the cache is empty.
98 * @arg cache Cache to check
99 * @return \a true if the cache is empty, otherwise \b false is returned.
101 int nl_cache_is_empty(struct nl_cache *cache) in nl_cache_is_empty() argument
103 return nl_list_empty(&cache->c_items); in nl_cache_is_empty()
107 * Return the operations set of the cache
108 * @arg cache cache handle
110 struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache) in nl_cache_get_ops() argument
112 return cache->c_ops; in nl_cache_get_ops()
116 * Return the first element in the cache
117 * @arg cache cache handle
119 struct nl_object *nl_cache_get_first(struct nl_cache *cache) in nl_cache_get_first() argument
121 if (nl_list_empty(&cache->c_items)) in nl_cache_get_first()
124 return nl_list_entry(cache->c_items.next, in nl_cache_get_first()
129 * Return the last element in the cache
130 * @arg cache cache handle
132 struct nl_object *nl_cache_get_last(struct nl_cache *cache) in nl_cache_get_last() argument
134 if (nl_list_empty(&cache->c_items)) in nl_cache_get_last()
137 return nl_list_entry(cache->c_items.prev, in nl_cache_get_last()
142 * Return the next element in the cache
155 * Return the previous element in the cache
170 * @name Cache Allocation/Deletion
175 * Allocate new cache
176 * @arg ops Cache operations
178 * Allocate and initialize a new cache based on the cache operations
181 * @return Allocated cache or NULL if allocation failed.
185 struct nl_cache *cache; in nl_cache_alloc() local
187 cache = calloc(1, sizeof(*cache)); in nl_cache_alloc()
188 if (!cache) in nl_cache_alloc()
191 nl_init_list_head(&cache->c_items); in nl_cache_alloc()
192 cache->c_ops = ops; in nl_cache_alloc()
193 cache->c_flags |= ops->co_flags; in nl_cache_alloc()
194 cache->c_refcnt = 1; in nl_cache_alloc()
199 * cache objects for faster lookups in nl_cache_alloc()
209 cache->hashtable = nl_hash_table_alloc(hashtable_size); in nl_cache_alloc()
212 NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache)); in nl_cache_alloc()
214 return cache; in nl_cache_alloc()
218 * Allocate new cache and fill it
219 * @arg ops Cache operations
223 * Allocate new cache and fill it. Equivalent to calling:
225 * cache = nl_cache_alloc(ops);
226 * nl_cache_refill(sock, cache);
236 struct nl_cache *cache; in nl_cache_alloc_and_fill() local
239 if (!(cache = nl_cache_alloc(ops))) in nl_cache_alloc_and_fill()
242 if (sock && (err = nl_cache_refill(sock, cache)) < 0) { in nl_cache_alloc_and_fill()
243 nl_cache_free(cache); in nl_cache_alloc_and_fill()
247 *result = cache; in nl_cache_alloc_and_fill()
252 * Allocate new cache based on type name
253 * @arg kind Name of cache type
256 * Lookup cache ops via nl_cache_ops_lookup() and allocate the cache
257 * by calling nl_cache_alloc(). Stores the allocated cache in the
267 struct nl_cache *cache; in nl_cache_alloc_name() local
273 cache = nl_cache_alloc(ops); in nl_cache_alloc_name()
275 if (!cache) in nl_cache_alloc_name()
278 *result = cache; in nl_cache_alloc_name()
283 * Allocate new cache containing a subset of an existing cache
284 * @arg orig Original cache to base new cache on
285 * @arg filter Filter defining the subset to be filled into the new cache
287 * Allocates a new cache matching the type of the cache specified by
288 * \p orig. Iterates over the \p orig cache applying the specified
289 * \p filter and copies all objects that match to the new cache.
292 * other. Later modifications to objects in the original cache will
293 * not affect objects in the new cache.
295 * @return A newly allocated cache or NULL.
300 struct nl_cache *cache; in nl_cache_subset() local
306 cache = nl_cache_alloc(orig->c_ops); in nl_cache_subset()
307 if (!cache) in nl_cache_subset()
310 NL_DBG(2, "Filling subset of cache %p <%s> with filter %p into %p\n", in nl_cache_subset()
311 orig, nl_cache_name(orig), filter, cache); in nl_cache_subset()
317 nl_cache_add(cache, obj); in nl_cache_subset()
320 return cache; in nl_cache_subset()
324 * Allocate new cache and copy the contents of an existing cache
325 * @arg cache Original cache to base new cache on
327 * Allocates a new cache matching the type of the cache specified by
328 * \p cache. Iterates over the \p cache cache and copies all objects
329 * to the new cache.
332 * other. Later modifications to objects in the original cache will
333 * not affect objects in the new cache.
335 * @return A newly allocated cache or NULL.
337 struct nl_cache *nl_cache_clone(struct nl_cache *cache) in nl_cache_clone() argument
339 struct nl_cache_ops *ops = nl_cache_get_ops(cache); in nl_cache_clone()
347 NL_DBG(2, "Cloning %p into %p\n", cache, clone); in nl_cache_clone()
349 nl_list_for_each_entry(obj, &cache->c_items, ce_list) in nl_cache_clone()
356 * Remove all objects of a cache.
357 * @arg cache Cache to clear
359 * The objects are unliked/removed from the cache by calling
360 * nl_cache_remove() on each object in the cache. If any of the objects
364 * Unlike with nl_cache_free(), the cache is not freed just emptied.
366 void nl_cache_clear(struct nl_cache *cache) in nl_cache_clear() argument
370 NL_DBG(2, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache)); in nl_cache_clear()
372 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) in nl_cache_clear()
376 static void __nl_cache_free(struct nl_cache *cache) in __nl_cache_free() argument
378 nl_cache_clear(cache); in __nl_cache_free()
380 if (cache->hashtable) in __nl_cache_free()
381 nl_hash_table_free(cache->hashtable); in __nl_cache_free()
383 NL_DBG(2, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache)); in __nl_cache_free()
384 free(cache); in __nl_cache_free()
388 * Increase reference counter of cache
389 * @arg cache Cache
391 void nl_cache_get(struct nl_cache *cache) in nl_cache_get() argument
393 cache->c_refcnt++; in nl_cache_get()
395 NL_DBG(3, "Incremented cache %p <%s> reference count to %d\n", in nl_cache_get()
396 cache, nl_cache_name(cache), cache->c_refcnt); in nl_cache_get()
400 * Free a cache.
401 * @arg cache Cache to free.
404 * cache and frees the cache afterwards.
408 void nl_cache_free(struct nl_cache *cache) in nl_cache_free() argument
410 if (!cache) in nl_cache_free()
413 cache->c_refcnt--; in nl_cache_free()
415 NL_DBG(3, "Decremented cache %p <%s> reference count, %d remaining\n", in nl_cache_free()
416 cache, nl_cache_name(cache), cache->c_refcnt); in nl_cache_free()
418 if (cache->c_refcnt <= 0) in nl_cache_free()
419 __nl_cache_free(cache); in nl_cache_free()
422 void nl_cache_put(struct nl_cache *cache) in nl_cache_put() argument
424 return nl_cache_free(cache); in nl_cache_put()
430 * @name Cache Modifications
434 static int __cache_add(struct nl_cache *cache, struct nl_object *obj) in __cache_add() argument
438 obj->ce_cache = cache; in __cache_add()
440 if (cache->hashtable) { in __cache_add()
441 ret = nl_hash_table_add(cache->hashtable, obj); in __cache_add()
448 nl_list_add_tail(&obj->ce_list, &cache->c_items); in __cache_add()
449 cache->c_nitems++; in __cache_add()
451 NL_DBG(3, "Added object %p to cache %p <%s>, nitems %d\n", in __cache_add()
452 obj, cache, nl_cache_name(cache), cache->c_nitems); in __cache_add()
458 * Add object to cache.
459 * @arg cache Cache
460 * @arg obj Object to be added to the cache
462 * Adds the object \p obj to the specified \p cache. In case the object
463 * is already associated with another cache, the object is cloned before
464 * adding it to the cache. In this case, the sole reference to the object
465 * will be the one of the cache. Therefore clearing/freeing the cache
468 * If the object has not been associated with a cache yet, the reference
472 * The type of the object and cache must match, otherwise an error is
479 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj) in nl_cache_add() argument
484 if (cache->c_ops->co_obj_ops != obj->ce_ops) in nl_cache_add()
488 NL_DBG(3, "Object %p already in cache, cloning new object\n", obj); in nl_cache_add()
498 ret = __cache_add(cache, new); in nl_cache_add()
506 * Move object from one cache to another
507 * @arg cache Cache to move object to.
510 * Removes the the specified object \p obj from its associated cache
511 * and moves it to another cache.
513 * If the object is not associated with a cache, the function behaves
516 * The type of the object and cache must match, otherwise an error is
523 int nl_cache_move(struct nl_cache *cache, struct nl_object *obj) in nl_cache_move() argument
525 if (cache->c_ops->co_obj_ops != obj->ce_ops) in nl_cache_move()
528 NL_DBG(3, "Moving object %p from cache %p to cache %p\n", in nl_cache_move()
529 obj, obj->ce_cache, cache); in nl_cache_move()
531 /* Acquire reference, if already in a cache this will be in nl_cache_move()
538 return __cache_add(cache, obj); in nl_cache_move()
542 * Remove object from cache.
543 * @arg obj Object to remove from cache
545 * Removes the object \c obj from the cache it is associated with. The
549 * If no cache is associated with the object, this function is a NOP.
554 struct nl_cache *cache = obj->ce_cache; in nl_cache_remove() local
556 if (cache == NULL) in nl_cache_remove()
559 if (cache->hashtable) { in nl_cache_remove()
560 ret = nl_hash_table_del(cache->hashtable, obj); in nl_cache_remove()
562 NL_DBG(2, "Failed to delete %p from cache %p <%s>.\n", in nl_cache_remove()
563 obj, cache, nl_cache_name(cache)); in nl_cache_remove()
569 cache->c_nitems--; in nl_cache_remove()
571 NL_DBG(2, "Deleted object %p from cache %p <%s>.\n", in nl_cache_remove()
572 obj, cache, nl_cache_name(cache)); in nl_cache_remove()
583 * Set synchronization arg1 of cache
584 * @arg cache Cache
590 void nl_cache_set_arg1(struct nl_cache *cache, int arg) in nl_cache_set_arg1() argument
592 cache->c_iarg1 = arg; in nl_cache_set_arg1()
596 * Set synchronization arg2 of cache
597 * @arg cache Cache
603 void nl_cache_set_arg2(struct nl_cache *cache, int arg) in nl_cache_set_arg2() argument
605 cache->c_iarg2 = arg; in nl_cache_set_arg2()
609 * Set cache flags
610 * @arg cache Cache
613 void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags) in nl_cache_set_flags() argument
615 cache->c_flags |= flags; in nl_cache_set_flags()
621 * @arg cache Cache
623 * This function causes the \e request-update function of the cache
630 * the \e request_update function of each individual type of cache.
632 * This function will not have any effects on the cache (unless the
633 * request_update implementation of the cache operations does so).
636 * and fill them into the cache.
643 struct nl_cache *cache) in nl_cache_request_full_dump() argument
645 if (sk->s_proto != cache->c_ops->co_protocol) in nl_cache_request_full_dump()
648 if (cache->c_ops->co_request_update == NULL) in nl_cache_request_full_dump()
651 NL_DBG(2, "Requesting update from kernel for cache %p <%s>\n", in nl_cache_request_full_dump()
652 cache, nl_cache_name(cache)); in nl_cache_request_full_dump()
654 return cache->c_ops->co_request_update(cache, sk); in nl_cache_request_full_dump()
679 * @arg cache Cache
682 static int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache, in __cache_pickup() argument
688 .ops = cache->c_ops, in __cache_pickup()
692 NL_DBG(2, "Picking up answer for cache %p <%s>\n", in __cache_pickup()
693 cache, nl_cache_name(cache)); in __cache_pickup()
704 cache, nl_cache_name(cache), err, nl_geterror(err)); in __cache_pickup()
713 struct nl_cache *cache = (struct nl_cache *)p->pp_arg; in pickup_cb() local
716 old = nl_cache_search(cache, c); in pickup_cb()
727 return nl_cache_add(cache, c); in pickup_cb()
731 * Pickup a netlink dump response and put it into a cache.
733 * @arg cache Cache to put items into.
736 * the specified cache. If an old object with same key attributes is
737 * present in the cache, it is replaced with the new object.
743 int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_pickup() argument
747 .pp_arg = cache, in nl_cache_pickup()
750 if (sk->s_proto != cache->c_ops->co_protocol) in nl_cache_pickup()
753 return __cache_pickup(sk, cache, &p); in nl_cache_pickup()
756 static int cache_include(struct nl_cache *cache, struct nl_object *obj, in cache_include() argument
764 old = nl_cache_search(cache, obj); in cache_include()
768 * object with the old existing cache object. in cache_include()
773 cb(cache, old, NL_ACT_CHANGE, data); in cache_include()
781 cb(cache, old, NL_ACT_DEL, data); in cache_include()
787 nl_cache_move(cache, obj); in cache_include()
789 cb(cache, obj, NL_ACT_NEW, data); in cache_include()
792 cb(cache, obj, NL_ACT_CHANGE, data); in cache_include()
806 int nl_cache_include(struct nl_cache *cache, struct nl_object *obj, in nl_cache_include() argument
809 struct nl_cache_ops *ops = cache->c_ops; in nl_cache_include()
817 return cache_include(cache, obj, &ops->co_msgtypes[i], in nl_cache_include()
820 NL_DBG(3, "Object %p does not seem to belong to cache %p <%s>\n", in nl_cache_include()
821 obj, cache, nl_cache_name(cache)); in nl_cache_include()
833 int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache, in nl_cache_resync() argument
839 .ca_cache = cache, in nl_cache_resync()
849 if (sk->s_proto != cache->c_ops->co_protocol) in nl_cache_resync()
852 NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache)); in nl_cache_resync()
855 nl_cache_mark_all(cache); in nl_cache_resync()
857 grp = cache->c_ops->co_groups; in nl_cache_resync()
860 (cache->c_flags & NL_CACHE_AF_ITER)) in nl_cache_resync()
861 nl_cache_set_arg1(cache, grp->ag_family); in nl_cache_resync()
864 err = nl_cache_request_full_dump(sk, cache); in nl_cache_resync()
868 err = __cache_pickup(sk, cache, &p); in nl_cache_resync()
877 (cache->c_flags & NL_CACHE_AF_ITER)); in nl_cache_resync()
879 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) { in nl_cache_resync()
884 change_cb(cache, obj, NL_ACT_DEL, data); in nl_cache_resync()
889 NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache)); in nl_cache_resync()
928 * Parse a netlink message and add it to the cache.
929 * @arg cache cache to add element to
932 * Parses a netlink message by calling the cache specific message parser
933 * and adds the new element to the cache. If an old object with same key
934 * attributes is present in the cache, it is replaced with the new object.
940 int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg) in nl_cache_parse_and_add() argument
944 .pp_arg = cache, in nl_cache_parse_and_add()
947 return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p); in nl_cache_parse_and_add()
951 * (Re)fill a cache with the contents in the kernel.
953 * @arg cache cache to update
955 * Clears the specified cache and fills it with the current state in
960 int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache) in nl_cache_refill() argument
965 if (sk->s_proto != cache->c_ops->co_protocol) in nl_cache_refill()
968 nl_cache_clear(cache); in nl_cache_refill()
969 grp = cache->c_ops->co_groups; in nl_cache_refill()
972 (cache->c_flags & NL_CACHE_AF_ITER)) in nl_cache_refill()
973 nl_cache_set_arg1(cache, grp->ag_family); in nl_cache_refill()
976 err = nl_cache_request_full_dump(sk, cache); in nl_cache_refill()
980 NL_DBG(2, "Updating cache %p <%s> for family %u, request sent, waiting for reply\n", in nl_cache_refill()
981 cache, nl_cache_name(cache), grp ? grp->ag_family : AF_UNSPEC); in nl_cache_refill()
983 err = nl_cache_pickup(sk, cache); in nl_cache_refill()
993 (cache->c_flags & NL_CACHE_AF_ITER)); in nl_cache_refill()
1004 static struct nl_object *__cache_fast_lookup(struct nl_cache *cache, in __cache_fast_lookup() argument
1009 obj = nl_hash_table_lookup(cache->hashtable, needle); in __cache_fast_lookup()
1019 * Search object in cache
1020 * @arg cache Cache
1023 * Searches the cache for an object which matches the object \p needle.
1033 struct nl_object *nl_cache_search(struct nl_cache *cache, in nl_cache_search() argument
1038 if (cache->hashtable) in nl_cache_search()
1039 return __cache_fast_lookup(cache, needle); in nl_cache_search()
1041 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_search()
1052 * Find object in cache
1053 * @arg cache Cache
1056 * Searches the cache for an object which matches the object filter.
1058 * and the cache supports hash lookups, a faster hashtable lookup
1068 struct nl_object *nl_cache_find(struct nl_cache *cache, in nl_cache_find() argument
1073 if (cache->c_ops == NULL) in nl_cache_find()
1077 && cache->hashtable) in nl_cache_find()
1078 return __cache_fast_lookup(cache, filter); in nl_cache_find()
1080 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_find()
1091 * Mark all objects of a cache
1092 * @arg cache Cache
1094 * Marks all objects of a cache by calling nl_object_mark() on each
1095 * object associated with the cache.
1097 void nl_cache_mark_all(struct nl_cache *cache) in nl_cache_mark_all() argument
1101 NL_DBG(2, "Marking all objects in cache %p <%s>\n", in nl_cache_mark_all()
1102 cache, nl_cache_name(cache)); in nl_cache_mark_all()
1104 nl_list_for_each_entry(obj, &cache->c_items, ce_list) in nl_cache_mark_all()
1116 * Dump all elements of a cache.
1117 * @arg cache cache to dump
1120 * Dumps all elements of the \a cache to the file descriptor \a fd.
1122 void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params) in nl_cache_dump() argument
1124 nl_cache_dump_filter(cache, params, NULL); in nl_cache_dump()
1128 * Dump all elements of a cache (filtered).
1129 * @arg cache cache to dump
1133 * Dumps all elements of the \a cache to the file descriptor \a fd
1136 void nl_cache_dump_filter(struct nl_cache *cache, in nl_cache_dump_filter() argument
1144 NL_DBG(2, "Dumping cache %p <%s> with filter %p\n", in nl_cache_dump_filter()
1145 cache, nl_cache_name(cache), filter); in nl_cache_dump_filter()
1150 if (cache->c_ops == NULL) in nl_cache_dump_filter()
1153 ops = cache->c_ops->co_obj_ops; in nl_cache_dump_filter()
1160 nl_list_for_each_entry(obj, &cache->c_items, ce_list) { in nl_cache_dump_filter()
1177 * Call a callback on each element of the cache.
1178 * @arg cache cache to iterate on
1182 * Calls a callback function \a cb on each element of the \a cache.
1185 void nl_cache_foreach(struct nl_cache *cache, in nl_cache_foreach() argument
1188 nl_cache_foreach_filter(cache, NULL, cb, arg); in nl_cache_foreach()
1192 * Call a callback on each element of the cache (filtered).
1193 * @arg cache cache to iterate on
1198 * Calls a callback function \a cb on each element of the \a cache
1202 void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, in nl_cache_foreach_filter() argument
1207 if (cache->c_ops == NULL) in nl_cache_foreach_filter()
1210 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) { in nl_cache_foreach_filter()