libnl  1.1.4
Modules
Cache

Modules

 Object
 
 Cache Implementation
 

Access Functions

int nl_cache_nitems (struct nl_cache *cache)
 Return the number of items in the cache. More...
 
int nl_cache_nitems_filter (struct nl_cache *cache, struct nl_object *filter)
 Return the number of items matching a filter in the cache. More...
 
int nl_cache_is_empty (struct nl_cache *cache)
 Returns true if the cache is empty. More...
 
struct nl_cache_opsnl_cache_get_ops (struct nl_cache *cache)
 Return the operations set of the cache. More...
 
struct nl_object * nl_cache_get_first (struct nl_cache *cache)
 Return the first element in the cache. More...
 
struct nl_object * nl_cache_get_last (struct nl_cache *cache)
 Return the last element in the cache. More...
 
struct nl_object * nl_cache_get_next (struct nl_object *obj)
 Return the next element in the cache. More...
 
struct nl_object * nl_cache_get_prev (struct nl_object *obj)
 Return the previous element in the cache. More...
 

Cache Creation/Deletion

struct nl_cache * nl_cache_alloc (struct nl_cache_ops *ops)
 Allocate an empty cache. More...
 
struct nl_cache * nl_cache_alloc_name (const char *kind)
 Allocate an empty cache based on type name. More...
 
struct nl_cache * nl_cache_subset (struct nl_cache *orig, struct nl_object *filter)
 Allocate a new cache containing a subset of a cache. More...
 
void nl_cache_clear (struct nl_cache *cache)
 Clear a cache. More...
 
void nl_cache_get (struct nl_cache *cache)
 Increase reference counter of cache. More...
 
void nl_cache_free (struct nl_cache *cache)
 Free a cache. More...
 

Cache Modifications

int nl_cache_add (struct nl_cache *cache, struct nl_object *obj)
 Add object to a cache. More...
 
int nl_cache_move (struct nl_cache *cache, struct nl_object *obj)
 Move object from one cache to another. More...
 
void nl_cache_remove (struct nl_object *obj)
 Removes an object from a cache. More...
 
struct nl_object * nl_cache_search (struct nl_cache *cache, struct nl_object *needle)
 Search for an object in a cache. More...
 

Synchronization

int nl_cache_request_full_dump (struct nl_handle *handle, struct nl_cache *cache)
 Request a full dump from the kernel to fill a cache. More...
 
int __cache_pickup (struct nl_handle *handle, struct nl_cache *cache, struct nl_parser_param *param)
 
int nl_cache_pickup (struct nl_handle *handle, struct nl_cache *cache)
 Pickup a netlink dump response and put it into a cache. More...
 
int nl_cache_include (struct nl_cache *cache, struct nl_object *obj, change_func_t change_cb)
 
int nl_cache_resync (struct nl_handle *handle, struct nl_cache *cache, change_func_t change_cb)
 

Parsing

int nl_cache_parse_and_add (struct nl_cache *cache, struct nl_msg *msg)
 Parse a netlink message and add it to the cache. More...
 
int nl_cache_refill (struct nl_handle *handle, struct nl_cache *cache)
 (Re)fill a cache with the contents in the kernel. More...
 

Utillities

void nl_cache_mark_all (struct nl_cache *cache)
 Mark all objects in a cache. More...
 

Dumping

void nl_cache_dump (struct nl_cache *cache, struct nl_dump_params *params)
 Dump all elements of a cache. More...
 
void nl_cache_dump_filter (struct nl_cache *cache, struct nl_dump_params *params, struct nl_object *filter)
 Dump all elements of a cache (filtered). More...
 

Iterators

void nl_cache_foreach (struct nl_cache *cache, void(*cb)(struct nl_object *, void *), void *arg)
 Call a callback on each element of the cache. More...
 
void nl_cache_foreach_filter (struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
 Call a callback on each element of the cache (filtered). More...
 

Detailed Description

* Cache Management | | Type Specific Cache Operations
*
* | | +----------------+ +------------+
* | request update | | msg_parser |
* | | +----------------+ +------------+
* +- - - - -^- - - - - - - -^- -|- - - -
* nl_cache_update: | | | |
* 1) --------- co_request_update ------+ | |
* | | |
* 2) destroy old cache +----------- pp_cb ---------|---+
* | | |
* 3) ---------- nl_recvmsgs ----------+ +- cb_valid -+
* +--------------+ | | | |
* | nl_cache_add |<-----+ + - - -v- -|- - - - - - - - - - -
* +--------------+ | | +-------------+
* | | +-----|-^-----+
* +---v-|---+
* | | | nl_recv |
* +---------+
* | | Core Netlink
*

Function Documentation

int nl_cache_nitems ( struct nl_cache *  cache)
Parameters
cachecache handle

Definition at line 58 of file cache.c.

59 {
60  return cache->c_nitems;
61 }
int nl_cache_nitems_filter ( struct nl_cache *  cache,
struct nl_object *  filter 
)
Parameters
cacheCache object.
filterFilter object.

Definition at line 68 of file cache.c.

References nl_object_match_filter().

69 {
70  struct nl_object *obj;
71  int nitems = 0;
72 
73  if (cache->c_ops == NULL)
74  BUG();
75 
76  nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
77  if (filter && !nl_object_match_filter(obj, filter))
78  continue;
79 
80  nitems++;
81  }
82 
83  return nitems;
84 }
int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
Match a filter against an object.
Definition: object.c:318
int nl_cache_is_empty ( struct nl_cache *  cache)
Parameters
cacheCache to check
Returns
true if the cache is empty, otherwise false is returned.

Definition at line 91 of file cache.c.

92 {
93  return nl_list_empty(&cache->c_items);
94 }
struct nl_cache_ops* nl_cache_get_ops ( struct nl_cache *  cache)
Parameters
cachecache handle

Definition at line 100 of file cache.c.

101 {
102  return cache->c_ops;
103 }
struct nl_object* nl_cache_get_first ( struct nl_cache *  cache)
Parameters
cachecache handle

Definition at line 109 of file cache.c.

110 {
111  if (nl_list_empty(&cache->c_items))
112  return NULL;
113 
114  return nl_list_entry(cache->c_items.next,
115  struct nl_object, ce_list);
116 }
struct nl_object* nl_cache_get_last ( struct nl_cache *  cache)
Parameters
cachecache handle

Definition at line 122 of file cache.c.

123 {
124  if (nl_list_empty(&cache->c_items))
125  return NULL;
126 
127  return nl_list_entry(cache->c_items.prev,
128  struct nl_object, ce_list);
129 }
struct nl_object* nl_cache_get_next ( struct nl_object *  obj)
Parameters
objcurrent object

Definition at line 135 of file cache.c.

136 {
137  if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
138  return NULL;
139  else
140  return nl_list_entry(obj->ce_list.next,
141  struct nl_object, ce_list);
142 }
struct nl_object* nl_cache_get_prev ( struct nl_object *  obj)
Parameters
objcurrent object

Definition at line 148 of file cache.c.

149 {
150  if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
151  return NULL;
152  else
153  return nl_list_entry(obj->ce_list.prev,
154  struct nl_object, ce_list);
155 }
struct nl_cache* nl_cache_alloc ( struct nl_cache_ops ops)
Parameters
opscache operations to base the cache on
Returns
A newly allocated and initialized cache.

Definition at line 170 of file cache.c.

Referenced by flnl_result_alloc_cache(), nfnl_ct_alloc_cache(), nl_cache_alloc_name(), nl_cache_mngr_add(), nl_cache_subset(), rtnl_class_alloc_cache(), rtnl_cls_alloc_cache(), rtnl_link_alloc_cache(), rtnl_neigh_alloc_cache(), rtnl_neightbl_alloc_cache(), rtnl_qdisc_alloc_cache(), rtnl_route_alloc_cache(), and rtnl_rule_alloc_cache_by_family().

171 {
172  struct nl_cache *cache;
173 
174  cache = calloc(1, sizeof(*cache));
175  if (!cache) {
176  nl_errno(ENOMEM);
177  return NULL;
178  }
179 
180  nl_init_list_head(&cache->c_items);
181  cache->c_ops = ops;
182  cache->c_refcnt = 1;
183 
184  NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
185 
186  return cache;
187 }
struct nl_cache* nl_cache_alloc_name ( const char *  kind)
Parameters
kindName of cache type
Returns
A newly allocated and initialized cache.

Definition at line 194 of file cache.c.

References nl_cache_alloc(), and nl_cache_ops_lookup().

195 {
196  struct nl_cache_ops *ops;
197 
198  ops = nl_cache_ops_lookup(kind);
199  if (!ops) {
200  nl_error(ENOENT, "Unable to lookup cache \"%s\"", kind);
201  return NULL;
202  }
203 
204  return nl_cache_alloc(ops);
205 }
struct nl_cache_ops * nl_cache_ops_lookup(const char *name)
Lookup cache operations by name.
Definition: cache_mngt.c:68
Cache Operations.
Definition: cache-api.h:163
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate an empty cache.
Definition: cache.c:170
struct nl_cache* nl_cache_subset ( struct nl_cache *  orig,
struct nl_object *  filter 
)
Parameters
origOriginal cache to be based on
filterFilter defining the subset to be filled into new cache
Returns
A newly allocated cache or NULL.

Definition at line 213 of file cache.c.

References nl_cache_add(), nl_cache_alloc(), and nl_object_match_filter().

215 {
216  struct nl_cache *cache;
217  struct nl_object *obj;
218 
219  if (!filter)
220  BUG();
221 
222  cache = nl_cache_alloc(orig->c_ops);
223  if (!cache)
224  return NULL;
225 
226  nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
227  if (!nl_object_match_filter(obj, filter))
228  continue;
229 
230  nl_cache_add(cache, obj);
231  }
232 
233  return cache;
234 }
int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
Match a filter against an object.
Definition: object.c:318
int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
Add object to a cache.
Definition: cache.c:320
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate an empty cache.
Definition: cache.c:170
void nl_cache_clear ( struct nl_cache *  cache)
Parameters
cachecache to clear

Removes all elements of a cache.

Definition at line 242 of file cache.c.

References nl_cache_remove().

Referenced by nl_cache_refill().

243 {
244  struct nl_object *obj, *tmp;
245 
246  NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
247 
248  nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
249  nl_cache_remove(obj);
250 }
void nl_cache_remove(struct nl_object *obj)
Removes an object from a cache.
Definition: cache.c:374
void nl_cache_get ( struct nl_cache *  cache)
Parameters
cacheCache

Definition at line 264 of file cache.c.

Referenced by nl_cache_mngt_provide().

265 {
266  cache->c_refcnt++;
267 }
void nl_cache_free ( struct nl_cache *  cache)
Parameters
cacheCache to free.

Removes all elements of a cache and frees all memory.

Note
Use this function if you are working with allocated caches.

Definition at line 277 of file cache.c.

Referenced by genl_ctrl_resolve(), nl_cache_mngr_add(), nl_cache_mngt_unprovide(), rtnl_class_alloc_cache(), rtnl_cls_alloc_cache(), rtnl_link_alloc_cache(), rtnl_neigh_alloc_cache(), rtnl_neightbl_alloc_cache(), and rtnl_qdisc_alloc_cache().

278 {
279  if (!cache)
280  return;
281 
282  cache->c_refcnt--;
283  NL_DBG(4, "Returned cache reference %p, %d remaining\n",
284  cache, cache->c_refcnt);
285 
286  if (cache->c_refcnt <= 0)
287  __nl_cache_free(cache);
288 }
int nl_cache_add ( struct nl_cache *  cache,
struct nl_object *  obj 
)
Parameters
cacheCache to add object to
objObject to be added to the cache

Adds the given object to the specified cache. The object is cloned if it has been added to another cache already.

Returns
0 or a negative error code.

Definition at line 320 of file cache.c.

References nl_object_clone(), and nl_object_get().

Referenced by nl_cache_subset().

321 {
322  struct nl_object *new;
323 
324  if (cache->c_ops->co_obj_ops != obj->ce_ops)
325  return nl_error(EINVAL, "Object mismatches cache type");
326 
327  if (!nl_list_empty(&obj->ce_list)) {
328  new = nl_object_clone(obj);
329  if (!new)
330  return nl_errno(ENOMEM);
331  } else {
332  nl_object_get(obj);
333  new = obj;
334  }
335 
336  return __cache_add(cache, new);
337 }
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition: object.c:167
struct nl_object * nl_object_clone(struct nl_object *obj)
Allocate a new object and copy all data from an existing object.
Definition: object.c:95
int nl_cache_move ( struct nl_cache *  cache,
struct nl_object *  obj 
)
Parameters
cacheCache to move object to.
objObject subject to be moved

Removes the given object from its associated cache if needed and adds it to the new cache.

Returns
0 on success or a negative error code.

Definition at line 349 of file cache.c.

References nl_cache_remove(), and nl_object_get().

350 {
351  if (cache->c_ops->co_obj_ops != obj->ce_ops)
352  return nl_error(EINVAL, "Object mismatches cache type");
353 
354  NL_DBG(3, "Moving object %p to cache %p\n", obj, cache);
355 
356  /* Acquire reference, if already in a cache this will be
357  * reverted during removal */
358  nl_object_get(obj);
359 
360  if (!nl_list_empty(&obj->ce_list))
361  nl_cache_remove(obj);
362 
363  return __cache_add(cache, obj);
364 }
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition: object.c:167
void nl_cache_remove(struct nl_object *obj)
Removes an object from a cache.
Definition: cache.c:374
void nl_cache_remove ( struct nl_object *  obj)
Parameters
objObject to remove from its cache

Removes the object obj from the cache it is assigned to, since an object can only be assigned to one cache at a time, the cache must ne be passed along with it.

Definition at line 374 of file cache.c.

References nl_object_put().

Referenced by nl_cache_clear(), nl_cache_move(), and nl_object_free().

375 {
376  struct nl_cache *cache = obj->ce_cache;
377 
378  if (cache == NULL)
379  return;
380 
381  nl_list_del(&obj->ce_list);
382  obj->ce_cache = NULL;
383  nl_object_put(obj);
384  cache->c_nitems--;
385 
386  NL_DBG(1, "Deleted %p from cache %p <%s>.\n",
387  obj, cache, nl_cache_name(cache));
388 }
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition: object.c:178
struct nl_object* nl_cache_search ( struct nl_cache *  cache,
struct nl_object *  needle 
)
Parameters
cacheCache to search in.
needleObject to look for.

Iterates over the cache and looks for an object with identical identifiers as the needle.

Returns
Reference to object or NULL if not found.
Note
The returned object must be returned via nl_object_put().

Definition at line 401 of file cache.c.

References nl_object_get(), and nl_object_identical().

403 {
404  struct nl_object *obj;
405 
406  nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
407  if (nl_object_identical(obj, needle)) {
408  nl_object_get(obj);
409  return obj;
410  }
411  }
412 
413  return NULL;
414 }
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition: object.c:167
int nl_object_identical(struct nl_object *a, struct nl_object *b)
Check if the identifiers of two objects are identical.
Definition: object.c:263
int nl_cache_request_full_dump ( struct nl_handle *  handle,
struct nl_cache *  cache 
)
Parameters
handleNetlink handle
cacheCache subjected to be filled.

Send a dumping request to the kernel causing it to dump all objects related to the specified cache to the netlink socket.

Use nl_cache_pickup() to read the objects from the socket and fill them into a cache.

Definition at line 435 of file cache.c.

Referenced by nl_cache_refill().

436 {
437  NL_DBG(2, "Requesting dump from kernel for cache %p <%s>...\n",
438  cache, nl_cache_name(cache));
439 
440  if (cache->c_ops->co_request_update == NULL)
441  return nl_error(EOPNOTSUPP, "Operation not supported");
442 
443  return cache->c_ops->co_request_update(cache, handle);
444 }
int nl_cache_pickup ( struct nl_handle *  handle,
struct nl_cache *  cache 
)
Parameters
handleNetlink handle.
cacheCache to put items into.

Waits for netlink messages to arrive, parses them and puts them into the specified cache.

Returns
0 on success or a negative error code.

Definition at line 505 of file cache.c.

Referenced by flnl_lookup(), and nl_cache_refill().

506 {
507  struct nl_parser_param p = {
508  .pp_cb = pickup_cb,
509  .pp_arg = cache,
510  };
511 
512  return __cache_pickup(handle, cache, &p);
513 }
int nl_cache_parse_and_add ( struct nl_cache *  cache,
struct nl_msg *  msg 
)
Parameters
cachecache to add element to
msgnetlink message

Parses a netlink message by calling the cache specific message parser and adds the new element to the cache.

Returns
0 or a negative error code.

Definition at line 660 of file cache.c.

References nlmsg_hdr().

661 {
662  struct nl_parser_param p = {
663  .pp_cb = pickup_cb,
664  .pp_arg = cache,
665  };
666 
667  return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
668 }
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Definition: msg.c:643
int nl_cache_refill ( struct nl_handle *  handle,
struct nl_cache *  cache 
)
Parameters
handlenetlink handle
cachecache to update

Clears the specified cache and fills it with the current state in the kernel.

Returns
0 or a negative error code.

Definition at line 680 of file cache.c.

References nl_cache_clear(), nl_cache_pickup(), and nl_cache_request_full_dump().

Referenced by nfnl_ct_alloc_cache(), nl_cache_mngr_add(), rtnl_class_alloc_cache(), rtnl_cls_alloc_cache(), rtnl_link_alloc_cache(), rtnl_neigh_alloc_cache(), rtnl_neightbl_alloc_cache(), rtnl_qdisc_alloc_cache(), rtnl_route_alloc_cache(), and rtnl_rule_alloc_cache_by_family().

681 {
682  int err;
683 
684  err = nl_cache_request_full_dump(handle, cache);
685  if (err < 0)
686  return err;
687 
688  NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n",
689  cache, nl_cache_name(cache));
690  nl_cache_clear(cache);
691 
692  return nl_cache_pickup(handle, cache);
693 }
int nl_cache_pickup(struct nl_handle *handle, struct nl_cache *cache)
Pickup a netlink dump response and put it into a cache.
Definition: cache.c:505
int nl_cache_request_full_dump(struct nl_handle *handle, struct nl_cache *cache)
Request a full dump from the kernel to fill a cache.
Definition: cache.c:435
void nl_cache_clear(struct nl_cache *cache)
Clear a cache.
Definition: cache.c:242
void nl_cache_mark_all ( struct nl_cache *  cache)
Parameters
cacheCache to mark all objects in

Definition at line 706 of file cache.c.

References nl_object_mark().

707 {
708  struct nl_object *obj;
709 
710  NL_DBG(2, "Marking all objects in cache %p <%s>...\n",
711  cache, nl_cache_name(cache));
712 
713  nl_list_for_each_entry(obj, &cache->c_items, ce_list)
714  nl_object_mark(obj);
715 }
void nl_object_mark(struct nl_object *obj)
Add mark to object.
Definition: object.c:215
void nl_cache_dump ( struct nl_cache *  cache,
struct nl_dump_params params 
)
Parameters
cachecache to dump
paramsdumping parameters

Dumps all elements of the cache to the file descriptor fd.

Definition at line 731 of file cache.c.

References nl_cache_dump_filter().

732 {
733  nl_cache_dump_filter(cache, params, NULL);
734 }
void nl_cache_dump_filter(struct nl_cache *cache, struct nl_dump_params *params, struct nl_object *filter)
Dump all elements of a cache (filtered).
Definition: cache.c:745
void nl_cache_dump_filter ( struct nl_cache *  cache,
struct nl_dump_params params,
struct nl_object *  filter 
)
Parameters
cachecache to dump
paramsdumping parameters (optional)
filterfilter object

Dumps all elements of the cache to the file descriptor fd given they match the given filter filter.

Definition at line 745 of file cache.c.

References nl_dump_params::dp_type, NL_DUMP_FULL, nl_object_match_filter(), and nl_object_ops::oo_dump.

Referenced by nl_cache_dump().

748 {
749  int type = params ? params->dp_type : NL_DUMP_FULL;
750  struct nl_object_ops *ops;
751  struct nl_object *obj;
752 
753  NL_DBG(2, "Dumping cache %p <%s> filter %p\n",
754  cache, nl_cache_name(cache), filter);
755 
756  if (type > NL_DUMP_MAX || type < 0)
757  BUG();
758 
759  if (cache->c_ops == NULL)
760  BUG();
761 
762  ops = cache->c_ops->co_obj_ops;
763  if (!ops->oo_dump[type])
764  return;
765 
766  nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
767  if (filter && !nl_object_match_filter(obj, filter))
768  continue;
769 
770  NL_DBG(4, "Dumping object %p...\n", obj);
771  dump_from_ops(obj, params);
772  }
773 }
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:41
int(* oo_dump[NL_DUMP_MAX+1])(struct nl_object *, struct nl_dump_params *)
Dumping functions.
Definition: object-api.h:307
Object Operations.
Definition: object-api.h:254
int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
Match a filter against an object.
Definition: object.c:318
Dump all attributes but no statistics.
Definition: types.h:23
void nl_cache_foreach ( struct nl_cache *  cache,
void(*)(struct nl_object *, void *)  cb,
void *  arg 
)
Parameters
cachecache to iterate on
cbcallback function
argargument passed to callback function

Calls a callback function cb on each element of the cache. The argument arg is passed on the callback function.

Definition at line 791 of file cache.c.

References nl_cache_foreach_filter().

793 {
794  nl_cache_foreach_filter(cache, NULL, cb, arg);
795 }
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
Definition: cache.c:808
void nl_cache_foreach_filter ( struct nl_cache *  cache,
struct nl_object *  filter,
void(*)(struct nl_object *, void *)  cb,
void *  arg 
)
Parameters
cachecache to iterate on
filterfilter object
cbcallback function
argargument passed to callback function

Calls a callback function cb on each element of the cache that matches the filter. The argument arg is passed on to the callback function.

Definition at line 808 of file cache.c.

References nl_object_match_filter().

Referenced by nl_cache_foreach(), rtnl_class_foreach_child(), rtnl_class_foreach_cls(), rtnl_qdisc_foreach_child(), and rtnl_qdisc_foreach_cls().

810 {
811  struct nl_object *obj, *tmp;
812 
813  if (cache->c_ops == NULL)
814  BUG();
815 
816  nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
817  if (filter && !nl_object_match_filter(obj, filter))
818  continue;
819 
820  cb(obj, arg);
821  }
822 }
int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
Match a filter against an object.
Definition: object.c:318