libnl  3.2.26
cache.c
1 /*
2  * lib/cache.c Caching Module
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup cache_mngt
14  * @defgroup cache Cache
15  *
16  * @code
17  * Cache Management | | Type Specific Cache Operations
18  *
19  * | | +----------------+ +------------+
20  * | request update | | msg_parser |
21  * | | +----------------+ +------------+
22  * +- - - - -^- - - - - - - -^- -|- - - -
23  * nl_cache_update: | | | |
24  * 1) --------- co_request_update ------+ | |
25  * | | |
26  * 2) destroy old cache +----------- pp_cb ---------|---+
27  * | | |
28  * 3) ---------- nl_recvmsgs ----------+ +- cb_valid -+
29  * +--------------+ | | | |
30  * | nl_cache_add |<-----+ + - - -v- -|- - - - - - - - - - -
31  * +--------------+ | | +-------------+
32  * | nl_recvmsgs |
33  * | | +-----|-^-----+
34  * +---v-|---+
35  * | | | nl_recv |
36  * +---------+
37  * | | Core Netlink
38  * @endcode
39  *
40  * Related sections in the development guide:
41  * - @core_doc{core_cache, Caching System}
42  *
43  * @{
44  *
45  * Header
46  * ------
47  * ~~~~{.c}
48  * #include <netlink/cache.h>
49  * ~~~~
50  */
51 
52 #include <netlink-private/netlink.h>
53 #include <netlink/netlink.h>
54 #include <netlink/cache.h>
55 #include <netlink/object.h>
56 #include <netlink/hashtable.h>
57 #include <netlink/utils.h>
58 
59 /**
60  * @name Access Functions
61  * @{
62  */
63 
64 /**
65  * Return the number of items in the cache
66  * @arg cache cache handle
67  */
68 int nl_cache_nitems(struct nl_cache *cache)
69 {
70  return cache->c_nitems;
71 }
72 
73 /**
74  * Return the number of items matching a filter in the cache
75  * @arg cache Cache object.
76  * @arg filter Filter object.
77  */
78 int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
79 {
80  struct nl_object *obj;
81  int nitems = 0;
82 
83  if (cache->c_ops == NULL)
84  BUG();
85 
86  nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
87  if (filter && !nl_object_match_filter(obj, filter))
88  continue;
89 
90  nitems++;
91  }
92 
93  return nitems;
94 }
95 
96 /**
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.
100  */
101 int nl_cache_is_empty(struct nl_cache *cache)
102 {
103  return nl_list_empty(&cache->c_items);
104 }
105 
106 /**
107  * Return the operations set of the cache
108  * @arg cache cache handle
109  */
110 struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache)
111 {
112  return cache->c_ops;
113 }
114 
115 /**
116  * Return the first element in the cache
117  * @arg cache cache handle
118  */
119 struct nl_object *nl_cache_get_first(struct nl_cache *cache)
120 {
121  if (nl_list_empty(&cache->c_items))
122  return NULL;
123 
124  return nl_list_entry(cache->c_items.next,
125  struct nl_object, ce_list);
126 }
127 
128 /**
129  * Return the last element in the cache
130  * @arg cache cache handle
131  */
132 struct nl_object *nl_cache_get_last(struct nl_cache *cache)
133 {
134  if (nl_list_empty(&cache->c_items))
135  return NULL;
136 
137  return nl_list_entry(cache->c_items.prev,
138  struct nl_object, ce_list);
139 }
140 
141 /**
142  * Return the next element in the cache
143  * @arg obj current object
144  */
145 struct nl_object *nl_cache_get_next(struct nl_object *obj)
146 {
147  if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
148  return NULL;
149  else
150  return nl_list_entry(obj->ce_list.next,
151  struct nl_object, ce_list);
152 }
153 
154 /**
155  * Return the previous element in the cache
156  * @arg obj current object
157  */
158 struct nl_object *nl_cache_get_prev(struct nl_object *obj)
159 {
160  if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
161  return NULL;
162  else
163  return nl_list_entry(obj->ce_list.prev,
164  struct nl_object, ce_list);
165 }
166 
167 /** @} */
168 
169 /**
170  * @name Cache Allocation/Deletion
171  * @{
172  */
173 
174 /**
175  * Allocate new cache
176  * @arg ops Cache operations
177  *
178  * Allocate and initialize a new cache based on the cache operations
179  * provided.
180  *
181  * @return Allocated cache or NULL if allocation failed.
182  */
183 struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
184 {
185  struct nl_cache *cache;
186 
187  cache = calloc(1, sizeof(*cache));
188  if (!cache)
189  return NULL;
190 
191  nl_init_list_head(&cache->c_items);
192  cache->c_ops = ops;
193  cache->c_flags |= ops->co_flags;
194  cache->c_refcnt = 1;
195 
196  /*
197  * If object type provides a hash keygen
198  * functions, allocate a hash table for the
199  * cache objects for faster lookups
200  */
201  if (ops->co_obj_ops->oo_keygen) {
202  int hashtable_size;
203 
204  if (ops->co_hash_size)
205  hashtable_size = ops->co_hash_size;
206  else
207  hashtable_size = NL_MAX_HASH_ENTRIES;
208 
209  cache->hashtable = nl_hash_table_alloc(hashtable_size);
210  }
211 
212  NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
213 
214  return cache;
215 }
216 
217 /**
218  * Allocate new cache and fill it
219  * @arg ops Cache operations
220  * @arg sock Netlink socket
221  * @arg result Result pointer
222  *
223  * Allocate new cache and fill it. Equivalent to calling:
224  * @code
225  * cache = nl_cache_alloc(ops);
226  * nl_cache_refill(sock, cache);
227  * @endcode
228  *
229  * @see nl_cache_alloc
230  *
231  * @return 0 on success or a negative error code.
232  */
233 int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock,
234  struct nl_cache **result)
235 {
236  struct nl_cache *cache;
237  int err;
238 
239  if (!(cache = nl_cache_alloc(ops)))
240  return -NLE_NOMEM;
241 
242  if (sock && (err = nl_cache_refill(sock, cache)) < 0) {
243  nl_cache_free(cache);
244  return err;
245  }
246 
247  *result = cache;
248  return 0;
249 }
250 
251 /**
252  * Allocate new cache based on type name
253  * @arg kind Name of cache type
254  * @arg result Result pointer
255  *
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
258  * result pointer provided.
259  *
260  * @see nl_cache_alloc
261  *
262  * @return 0 on success or a negative error code.
263  */
264 int nl_cache_alloc_name(const char *kind, struct nl_cache **result)
265 {
266  struct nl_cache_ops *ops;
267  struct nl_cache *cache;
268 
269  ops = nl_cache_ops_lookup_safe(kind);
270  if (!ops)
271  return -NLE_NOCACHE;
272 
273  cache = nl_cache_alloc(ops);
274  nl_cache_ops_put(ops);
275  if (!cache)
276  return -NLE_NOMEM;
277 
278  *result = cache;
279  return 0;
280 }
281 
282 /**
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
286  *
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.
290  *
291  * The copied objects are clones but do not contain a reference to each
292  * other. Later modifications to objects in the original cache will
293  * not affect objects in the new cache.
294  *
295  * @return A newly allocated cache or NULL.
296  */
297 struct nl_cache *nl_cache_subset(struct nl_cache *orig,
298  struct nl_object *filter)
299 {
300  struct nl_cache *cache;
301  struct nl_object *obj;
302 
303  if (!filter)
304  BUG();
305 
306  cache = nl_cache_alloc(orig->c_ops);
307  if (!cache)
308  return NULL;
309 
310  NL_DBG(2, "Filling subset of cache %p <%s> with filter %p into %p\n",
311  orig, nl_cache_name(orig), filter, cache);
312 
313  nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
314  if (!nl_object_match_filter(obj, filter))
315  continue;
316 
317  nl_cache_add(cache, obj);
318  }
319 
320  return cache;
321 }
322 
323 /**
324  * Allocate new cache and copy the contents of an existing cache
325  * @arg cache Original cache to base new cache on
326  *
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.
330  *
331  * The copied objects are clones but do not contain a reference to each
332  * other. Later modifications to objects in the original cache will
333  * not affect objects in the new cache.
334  *
335  * @return A newly allocated cache or NULL.
336  */
337 struct nl_cache *nl_cache_clone(struct nl_cache *cache)
338 {
339  struct nl_cache_ops *ops = nl_cache_get_ops(cache);
340  struct nl_cache *clone;
341  struct nl_object *obj;
342 
343  clone = nl_cache_alloc(ops);
344  if (!clone)
345  return NULL;
346 
347  NL_DBG(2, "Cloning %p into %p\n", cache, clone);
348 
349  nl_list_for_each_entry(obj, &cache->c_items, ce_list)
350  nl_cache_add(clone, obj);
351 
352  return clone;
353 }
354 
355 /**
356  * Remove all objects of a cache.
357  * @arg cache Cache to clear
358  *
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
361  * to not contain any further references to them, those objects will
362  * be freed.
363  *
364  * Unlike with nl_cache_free(), the cache is not freed just emptied.
365  */
366 void nl_cache_clear(struct nl_cache *cache)
367 {
368  struct nl_object *obj, *tmp;
369 
370  NL_DBG(2, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
371 
372  nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
373  nl_cache_remove(obj);
374 }
375 
376 static void __nl_cache_free(struct nl_cache *cache)
377 {
378  nl_cache_clear(cache);
379 
380  if (cache->hashtable)
381  nl_hash_table_free(cache->hashtable);
382 
383  NL_DBG(2, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
384  free(cache);
385 }
386 
387 /**
388  * Increase reference counter of cache
389  * @arg cache Cache
390  */
391 void nl_cache_get(struct nl_cache *cache)
392 {
393  cache->c_refcnt++;
394 
395  NL_DBG(3, "Incremented cache %p <%s> reference count to %d\n",
396  cache, nl_cache_name(cache), cache->c_refcnt);
397 }
398 
399 /**
400  * Free a cache.
401  * @arg cache Cache to free.
402  *
403  * Calls nl_cache_clear() to remove all objects associated with the
404  * cache and frees the cache afterwards.
405  *
406  * @see nl_cache_clear()
407  */
408 void nl_cache_free(struct nl_cache *cache)
409 {
410  if (!cache)
411  return;
412 
413  cache->c_refcnt--;
414 
415  NL_DBG(3, "Decremented cache %p <%s> reference count, %d remaining\n",
416  cache, nl_cache_name(cache), cache->c_refcnt);
417 
418  if (cache->c_refcnt <= 0)
419  __nl_cache_free(cache);
420 }
421 
422 void nl_cache_put(struct nl_cache *cache)
423 {
424  return nl_cache_free(cache);
425 }
426 
427 /** @} */
428 
429 /**
430  * @name Cache Modifications
431  * @{
432  */
433 
434 static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
435 {
436  int ret;
437 
438  obj->ce_cache = cache;
439 
440  if (cache->hashtable) {
441  ret = nl_hash_table_add(cache->hashtable, obj);
442  if (ret < 0) {
443  obj->ce_cache = NULL;
444  return ret;
445  }
446  }
447 
448  nl_list_add_tail(&obj->ce_list, &cache->c_items);
449  cache->c_nitems++;
450 
451  NL_DBG(3, "Added object %p to cache %p <%s>, nitems %d\n",
452  obj, cache, nl_cache_name(cache), cache->c_nitems);
453 
454  return 0;
455 }
456 
457 /**
458  * Add object to cache.
459  * @arg cache Cache
460  * @arg obj Object to be added to the cache
461  *
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
466  * will result in the object being freed again.
467  *
468  * If the object has not been associated with a cache yet, the reference
469  * counter of the object is incremented to account for the additional
470  * reference.
471  *
472  * The type of the object and cache must match, otherwise an error is
473  * returned (-NLE_OBJ_MISMATCH).
474  *
475  * @see nl_cache_move()
476  *
477  * @return 0 or a negative error code.
478  */
479 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
480 {
481  struct nl_object *new;
482  int ret = 0;
483 
484  if (cache->c_ops->co_obj_ops != obj->ce_ops)
485  return -NLE_OBJ_MISMATCH;
486 
487  if (!nl_list_empty(&obj->ce_list)) {
488  NL_DBG(3, "Object %p already in cache, cloning new object\n", obj);
489 
490  new = nl_object_clone(obj);
491  if (!new)
492  return -NLE_NOMEM;
493  } else {
494  nl_object_get(obj);
495  new = obj;
496  }
497 
498  ret = __cache_add(cache, new);
499  if (ret < 0)
500  nl_object_put(new);
501 
502  return ret;
503 }
504 
505 /**
506  * Move object from one cache to another
507  * @arg cache Cache to move object to.
508  * @arg obj Object subject to be moved
509  *
510  * Removes the the specified object \p obj from its associated cache
511  * and moves it to another cache.
512  *
513  * If the object is not associated with a cache, the function behaves
514  * just like nl_cache_add().
515  *
516  * The type of the object and cache must match, otherwise an error is
517  * returned (-NLE_OBJ_MISMATCH).
518  *
519  * @see nl_cache_add()
520  *
521  * @return 0 on success or a negative error code.
522  */
523 int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
524 {
525  if (cache->c_ops->co_obj_ops != obj->ce_ops)
526  return -NLE_OBJ_MISMATCH;
527 
528  NL_DBG(3, "Moving object %p from cache %p to cache %p\n",
529  obj, obj->ce_cache, cache);
530 
531  /* Acquire reference, if already in a cache this will be
532  * reverted during removal */
533  nl_object_get(obj);
534 
535  if (!nl_list_empty(&obj->ce_list))
536  nl_cache_remove(obj);
537 
538  return __cache_add(cache, obj);
539 }
540 
541 /**
542  * Remove object from cache.
543  * @arg obj Object to remove from cache
544  *
545  * Removes the object \c obj from the cache it is associated with. The
546  * reference counter of the object will be decremented. If the reference
547  * to the object was the only one remaining, the object will be freed.
548  *
549  * If no cache is associated with the object, this function is a NOP.
550  */
551 void nl_cache_remove(struct nl_object *obj)
552 {
553  int ret;
554  struct nl_cache *cache = obj->ce_cache;
555 
556  if (cache == NULL)
557  return;
558 
559  if (cache->hashtable) {
560  ret = nl_hash_table_del(cache->hashtable, obj);
561  if (ret < 0)
562  NL_DBG(2, "Failed to delete %p from cache %p <%s>.\n",
563  obj, cache, nl_cache_name(cache));
564  }
565 
566  nl_list_del(&obj->ce_list);
567  obj->ce_cache = NULL;
568  nl_object_put(obj);
569  cache->c_nitems--;
570 
571  NL_DBG(2, "Deleted object %p from cache %p <%s>.\n",
572  obj, cache, nl_cache_name(cache));
573 }
574 
575 /** @} */
576 
577 /**
578  * @name Synchronization
579  * @{
580  */
581 
582 /**
583  * Set synchronization arg1 of cache
584  * @arg cache Cache
585  * @arg arg argument
586  *
587  * Synchronization arguments are used to specify filters when
588  * requesting dumps from the kernel.
589  */
590 void nl_cache_set_arg1(struct nl_cache *cache, int arg)
591 {
592  cache->c_iarg1 = arg;
593 }
594 
595 /**
596  * Set synchronization arg2 of cache
597  * @arg cache Cache
598  * @arg arg argument
599  *
600  * Synchronization arguments are used to specify filters when
601  * requesting dumps from the kernel.
602  */
603 void nl_cache_set_arg2(struct nl_cache *cache, int arg)
604 {
605  cache->c_iarg2 = arg;
606 }
607 
608 /**
609  * Set cache flags
610  * @arg cache Cache
611  * @arg flags Flags
612  */
613 void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags)
614 {
615  cache->c_flags |= flags;
616 }
617 
618 /**
619  * Invoke the request-update operation
620  * @arg sk Netlink socket.
621  * @arg cache Cache
622  *
623  * This function causes the \e request-update function of the cache
624  * operations to be invoked. This usually causes a dump request to
625  * be sent over the netlink socket which triggers the kernel to dump
626  * all objects of a specific type to be dumped onto the netlink
627  * socket for pickup.
628  *
629  * The behaviour of this function depends on the implemenation of
630  * the \e request_update function of each individual type of cache.
631  *
632  * This function will not have any effects on the cache (unless the
633  * request_update implementation of the cache operations does so).
634  *
635  * Use nl_cache_pickup() to pick-up (read) the objects from the socket
636  * and fill them into the cache.
637  *
638  * @see nl_cache_pickup(), nl_cache_resync()
639  *
640  * @return 0 on success or a negative error code.
641  */
642 static int nl_cache_request_full_dump(struct nl_sock *sk,
643  struct nl_cache *cache)
644 {
645  if (sk->s_proto != cache->c_ops->co_protocol)
646  return -NLE_PROTO_MISMATCH;
647 
648  if (cache->c_ops->co_request_update == NULL)
649  return -NLE_OPNOTSUPP;
650 
651  NL_DBG(2, "Requesting update from kernel for cache %p <%s>\n",
652  cache, nl_cache_name(cache));
653 
654  return cache->c_ops->co_request_update(cache, sk);
655 }
656 
657 /** @cond SKIP */
658 struct update_xdata {
659  struct nl_cache_ops *ops;
660  struct nl_parser_param *params;
661 };
662 
663 static int update_msg_parser(struct nl_msg *msg, void *arg)
664 {
665  struct update_xdata *x = arg;
666  int ret = 0;
667 
668  ret = nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
669  if (ret == -NLE_EXIST)
670  return NL_SKIP;
671  else
672  return ret;
673 }
674 /** @endcond */
675 
676 /**
677  * Pick-up a netlink request-update with your own parser
678  * @arg sk Netlink socket
679  * @arg cache Cache
680  * @arg param Parser parameters
681  */
682 static int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache,
683  struct nl_parser_param *param)
684 {
685  int err;
686  struct nl_cb *cb;
687  struct update_xdata x = {
688  .ops = cache->c_ops,
689  .params = param,
690  };
691 
692  NL_DBG(2, "Picking up answer for cache %p <%s>\n",
693  cache, nl_cache_name(cache));
694 
695  cb = nl_cb_clone(sk->s_cb);
696  if (cb == NULL)
697  return -NLE_NOMEM;
698 
699  nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x);
700 
701  err = nl_recvmsgs(sk, cb);
702  if (err < 0)
703  NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned %d: %s\n",
704  cache, nl_cache_name(cache), err, nl_geterror(err));
705 
706  nl_cb_put(cb);
707 
708  return err;
709 }
710 
711 static int pickup_checkdup_cb(struct nl_object *c, struct nl_parser_param *p)
712 {
713  struct nl_cache *cache = (struct nl_cache *)p->pp_arg;
714  struct nl_object *old;
715 
716  old = nl_cache_search(cache, c);
717  if (old) {
718  if (nl_object_update(old, c) == 0) {
719  nl_object_put(old);
720  return 0;
721  }
722 
723  nl_cache_remove(old);
724  nl_object_put(old);
725  }
726 
727  return nl_cache_add(cache, c);
728 }
729 
730 static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
731 {
732  struct nl_cache *cache = p->pp_arg;
733 
734  return nl_cache_add(cache, c);
735 }
736 
737 static int __nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache,
738  int checkdup)
739 {
740  struct nl_parser_param p;
741 
742  p.pp_cb = checkdup ? pickup_checkdup_cb : pickup_cb;
743  p.pp_arg = cache;
744 
745  if (sk->s_proto != cache->c_ops->co_protocol)
746  return -NLE_PROTO_MISMATCH;
747 
748  return __cache_pickup(sk, cache, &p);
749 }
750 
751 /**
752  * Pickup a netlink dump response and put it into a cache.
753  * @arg sk Netlink socket.
754  * @arg cache Cache to put items into.
755  *
756  * Waits for netlink messages to arrive, parses them and puts them into
757  * the specified cache.
758  *
759  * @return 0 on success or a negative error code.
760  */
761 int nl_cache_pickup_checkdup(struct nl_sock *sk, struct nl_cache *cache)
762 {
763  return __nl_cache_pickup(sk, cache, 1);
764 }
765 
766 /**
767  * Pickup a netlink dump response and put it into a cache.
768  * @arg sk Netlink socket.
769  * @arg cache Cache to put items into.
770  *
771  * Waits for netlink messages to arrive, parses them and puts them into
772  * the specified cache. If an old object with same key attributes is
773  * present in the cache, it is replaced with the new object.
774  * If the old object type supports an update operation, an update is
775  * attempted before a replace.
776  *
777  * @return 0 on success or a negative error code.
778  */
779 int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
780 {
781  return __nl_cache_pickup(sk, cache, 0);
782 }
783 
784 static int cache_include(struct nl_cache *cache, struct nl_object *obj,
785  struct nl_msgtype *type, change_func_t cb, void *data)
786 {
787  struct nl_object *old;
788 
789  switch (type->mt_act) {
790  case NL_ACT_NEW:
791  case NL_ACT_DEL:
792  old = nl_cache_search(cache, obj);
793  if (old) {
794  /*
795  * Some objects types might support merging the new
796  * object with the old existing cache object.
797  * Handle them first.
798  */
799  if (nl_object_update(old, obj) == 0) {
800  if (cb)
801  cb(cache, old, NL_ACT_CHANGE, data);
802  nl_object_put(old);
803  return 0;
804  }
805 
806  nl_cache_remove(old);
807  if (type->mt_act == NL_ACT_DEL) {
808  if (cb)
809  cb(cache, old, NL_ACT_DEL, data);
810  nl_object_put(old);
811  }
812  }
813 
814  if (type->mt_act == NL_ACT_NEW) {
815  nl_cache_move(cache, obj);
816  if (old == NULL && cb)
817  cb(cache, obj, NL_ACT_NEW, data);
818  else if (old) {
819  if (nl_object_diff(old, obj) && cb)
820  cb(cache, obj, NL_ACT_CHANGE, data);
821 
822  nl_object_put(old);
823  }
824  }
825  break;
826  default:
827  NL_DBG(2, "Unknown action associated to object %p\n", obj);
828  return 0;
829  }
830 
831  return 0;
832 }
833 
834 int nl_cache_include(struct nl_cache *cache, struct nl_object *obj,
835  change_func_t change_cb, void *data)
836 {
837  struct nl_cache_ops *ops = cache->c_ops;
838  int i;
839 
840  if (ops->co_obj_ops != obj->ce_ops)
841  return -NLE_OBJ_MISMATCH;
842 
843  for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
844  if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
845  return cache_include(cache, obj, &ops->co_msgtypes[i],
846  change_cb, data);
847 
848  NL_DBG(3, "Object %p does not seem to belong to cache %p <%s>\n",
849  obj, cache, nl_cache_name(cache));
850 
851  return -NLE_MSGTYPE_NOSUPPORT;
852 }
853 
854 static int resync_cb(struct nl_object *c, struct nl_parser_param *p)
855 {
856  struct nl_cache_assoc *ca = p->pp_arg;
857 
858  return nl_cache_include(ca->ca_cache, c, ca->ca_change, ca->ca_change_data);
859 }
860 
861 int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache,
862  change_func_t change_cb, void *data)
863 {
864  struct nl_object *obj, *next;
865  struct nl_af_group *grp;
866  struct nl_cache_assoc ca = {
867  .ca_cache = cache,
868  .ca_change = change_cb,
869  .ca_change_data = data,
870  };
871  struct nl_parser_param p = {
872  .pp_cb = resync_cb,
873  .pp_arg = &ca,
874  };
875  int err;
876 
877  if (sk->s_proto != cache->c_ops->co_protocol)
878  return -NLE_PROTO_MISMATCH;
879 
880  NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
881 
882  /* Mark all objects so we can see if some of them are obsolete */
883  nl_cache_mark_all(cache);
884 
885  grp = cache->c_ops->co_groups;
886  do {
887  if (grp && grp->ag_group &&
888  (cache->c_flags & NL_CACHE_AF_ITER))
889  nl_cache_set_arg1(cache, grp->ag_family);
890 
891 restart:
892  err = nl_cache_request_full_dump(sk, cache);
893  if (err < 0)
894  goto errout;
895 
896  err = __cache_pickup(sk, cache, &p);
897  if (err == -NLE_DUMP_INTR)
898  goto restart;
899  else if (err < 0)
900  goto errout;
901 
902  if (grp)
903  grp++;
904  } while (grp && grp->ag_group &&
905  (cache->c_flags & NL_CACHE_AF_ITER));
906 
907  nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) {
908  if (nl_object_is_marked(obj)) {
909  nl_object_get(obj);
910  nl_cache_remove(obj);
911  if (change_cb)
912  change_cb(cache, obj, NL_ACT_DEL, data);
913  nl_object_put(obj);
914  }
915  }
916 
917  NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
918 
919  err = 0;
920 errout:
921  return err;
922 }
923 
924 /** @} */
925 
926 /**
927  * @name Parsing
928  * @{
929  */
930 
931 /** @cond SKIP */
932 int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who,
933  struct nlmsghdr *nlh, struct nl_parser_param *params)
934 {
935  int i, err;
936 
937  if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize))
938  return -NLE_MSG_TOOSHORT;
939 
940  for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
941  if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
942  err = ops->co_msg_parser(ops, who, nlh, params);
943  if (err != -NLE_OPNOTSUPP)
944  goto errout;
945  }
946  }
947 
948 
949  err = -NLE_MSGTYPE_NOSUPPORT;
950 errout:
951  return err;
952 }
953 /** @endcond */
954 
955 /**
956  * Parse a netlink message and add it to the cache.
957  * @arg cache cache to add element to
958  * @arg msg netlink message
959  *
960  * Parses a netlink message by calling the cache specific message parser
961  * and adds the new element to the cache. If an old object with same key
962  * attributes is present in the cache, it is replaced with the new object.
963  * If the old object type supports an update operation, an update is
964  * attempted before a replace.
965  *
966  * @return 0 or a negative error code.
967  */
968 int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
969 {
970  struct nl_parser_param p = {
971  .pp_cb = pickup_cb,
972  .pp_arg = cache,
973  };
974 
975  return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
976 }
977 
978 /**
979  * (Re)fill a cache with the contents in the kernel.
980  * @arg sk Netlink socket.
981  * @arg cache cache to update
982  *
983  * Clears the specified cache and fills it with the current state in
984  * the kernel.
985  *
986  * @return 0 or a negative error code.
987  */
988 int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
989 {
990  struct nl_af_group *grp;
991  int err;
992 
993  if (sk->s_proto != cache->c_ops->co_protocol)
994  return -NLE_PROTO_MISMATCH;
995 
996  nl_cache_clear(cache);
997  grp = cache->c_ops->co_groups;
998  do {
999  if (grp && grp->ag_group &&
1000  (cache->c_flags & NL_CACHE_AF_ITER))
1001  nl_cache_set_arg1(cache, grp->ag_family);
1002 
1003 restart:
1004  err = nl_cache_request_full_dump(sk, cache);
1005  if (err < 0)
1006  return err;
1007 
1008  NL_DBG(2, "Updating cache %p <%s> for family %u, request sent, waiting for reply\n",
1009  cache, nl_cache_name(cache), grp ? grp->ag_family : AF_UNSPEC);
1010 
1011  err = nl_cache_pickup(sk, cache);
1012  if (err == -NLE_DUMP_INTR) {
1013  NL_DBG(2, "Dump interrupted, restarting!\n");
1014  goto restart;
1015  } else if (err < 0)
1016  break;
1017 
1018  if (grp)
1019  grp++;
1020  } while (grp && grp->ag_group &&
1021  (cache->c_flags & NL_CACHE_AF_ITER));
1022 
1023  return err;
1024 }
1025 
1026 /** @} */
1027 
1028 /**
1029  * @name Utillities
1030  * @{
1031  */
1032 static struct nl_object *__cache_fast_lookup(struct nl_cache *cache,
1033  struct nl_object *needle)
1034 {
1035  struct nl_object *obj;
1036 
1037  obj = nl_hash_table_lookup(cache->hashtable, needle);
1038  if (obj) {
1039  nl_object_get(obj);
1040  return obj;
1041  }
1042 
1043  return NULL;
1044 }
1045 
1046 /**
1047  * Search object in cache
1048  * @arg cache Cache
1049  * @arg needle Object to look for.
1050  *
1051  * Searches the cache for an object which matches the object \p needle.
1052  * The function nl_object_identical() is used to determine if the
1053  * objects match. If a matching object is found, the reference counter
1054  * is incremented and the object is returned.
1055  *
1056  * Therefore, if an object is returned, the reference to the object
1057  * must be returned by calling nl_object_put() after usage.
1058  *
1059  * @return Reference to object or NULL if not found.
1060  */
1061 struct nl_object *nl_cache_search(struct nl_cache *cache,
1062  struct nl_object *needle)
1063 {
1064  struct nl_object *obj;
1065 
1066  if (cache->hashtable)
1067  return __cache_fast_lookup(cache, needle);
1068 
1069  nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1070  if (nl_object_identical(obj, needle)) {
1071  nl_object_get(obj);
1072  return obj;
1073  }
1074  }
1075 
1076  return NULL;
1077 }
1078 
1079 /**
1080  * Find object in cache
1081  * @arg cache Cache
1082  * @arg filter object acting as a filter
1083  *
1084  * Searches the cache for an object which matches the object filter.
1085  * If the filter attributes matches the object type id attributes,
1086  * and the cache supports hash lookups, a faster hashtable lookup
1087  * is used to return the object. Else, function nl_object_match_filter() is
1088  * used to determine if the objects match. If a matching object is
1089  * found, the reference counter is incremented and the object is returned.
1090  *
1091  * Therefore, if an object is returned, the reference to the object
1092  * must be returned by calling nl_object_put() after usage.
1093  *
1094  * @return Reference to object or NULL if not found.
1095  */
1096 struct nl_object *nl_cache_find(struct nl_cache *cache,
1097  struct nl_object *filter)
1098 {
1099  struct nl_object *obj;
1100 
1101  if (cache->c_ops == NULL)
1102  BUG();
1103 
1104  if ((nl_object_get_id_attrs(filter) == filter->ce_mask)
1105  && cache->hashtable)
1106  return __cache_fast_lookup(cache, filter);
1107 
1108  nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1109  if (nl_object_match_filter(obj, filter)) {
1110  nl_object_get(obj);
1111  return obj;
1112  }
1113  }
1114 
1115  return NULL;
1116 }
1117 
1118 /**
1119  * Mark all objects of a cache
1120  * @arg cache Cache
1121  *
1122  * Marks all objects of a cache by calling nl_object_mark() on each
1123  * object associated with the cache.
1124  */
1125 void nl_cache_mark_all(struct nl_cache *cache)
1126 {
1127  struct nl_object *obj;
1128 
1129  NL_DBG(2, "Marking all objects in cache %p <%s>\n",
1130  cache, nl_cache_name(cache));
1131 
1132  nl_list_for_each_entry(obj, &cache->c_items, ce_list)
1133  nl_object_mark(obj);
1134 }
1135 
1136 /** @} */
1137 
1138 /**
1139  * @name Dumping
1140  * @{
1141  */
1142 
1143 /**
1144  * Dump all elements of a cache.
1145  * @arg cache cache to dump
1146  * @arg params dumping parameters
1147  *
1148  * Dumps all elements of the \a cache to the file descriptor \a fd.
1149  */
1150 void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
1151 {
1152  nl_cache_dump_filter(cache, params, NULL);
1153 }
1154 
1155 /**
1156  * Dump all elements of a cache (filtered).
1157  * @arg cache cache to dump
1158  * @arg params dumping parameters (optional)
1159  * @arg filter filter object
1160  *
1161  * Dumps all elements of the \a cache to the file descriptor \a fd
1162  * given they match the given filter \a filter.
1163  */
1164 void nl_cache_dump_filter(struct nl_cache *cache,
1165  struct nl_dump_params *params,
1166  struct nl_object *filter)
1167 {
1168  int type = params ? params->dp_type : NL_DUMP_DETAILS;
1169  struct nl_object_ops *ops;
1170  struct nl_object *obj;
1171 
1172  NL_DBG(2, "Dumping cache %p <%s> with filter %p\n",
1173  cache, nl_cache_name(cache), filter);
1174 
1175  if (type > NL_DUMP_MAX || type < 0)
1176  BUG();
1177 
1178  if (cache->c_ops == NULL)
1179  BUG();
1180 
1181  ops = cache->c_ops->co_obj_ops;
1182  if (!ops->oo_dump[type])
1183  return;
1184 
1185  if (params && params->dp_buf)
1186  memset(params->dp_buf, 0, params->dp_buflen);
1187 
1188  nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1189  if (filter && !nl_object_match_filter(obj, filter))
1190  continue;
1191 
1192  NL_DBG(4, "Dumping object %p...\n", obj);
1193  dump_from_ops(obj, params);
1194  }
1195 }
1196 
1197 /** @} */
1198 
1199 /**
1200  * @name Iterators
1201  * @{
1202  */
1203 
1204 /**
1205  * Call a callback on each element of the cache.
1206  * @arg cache cache to iterate on
1207  * @arg cb callback function
1208  * @arg arg argument passed to callback function
1209  *
1210  * Calls a callback function \a cb on each element of the \a cache.
1211  * The argument \a arg is passed on the callback function.
1212  */
1213 void nl_cache_foreach(struct nl_cache *cache,
1214  void (*cb)(struct nl_object *, void *), void *arg)
1215 {
1216  nl_cache_foreach_filter(cache, NULL, cb, arg);
1217 }
1218 
1219 /**
1220  * Call a callback on each element of the cache (filtered).
1221  * @arg cache cache to iterate on
1222  * @arg filter filter object
1223  * @arg cb callback function
1224  * @arg arg argument passed to callback function
1225  *
1226  * Calls a callback function \a cb on each element of the \a cache
1227  * that matches the \a filter. The argument \a arg is passed on
1228  * to the callback function.
1229  */
1230 void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter,
1231  void (*cb)(struct nl_object *, void *), void *arg)
1232 {
1233  struct nl_object *obj, *tmp;
1234 
1235  if (cache->c_ops == NULL)
1236  BUG();
1237 
1238  nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
1239  if (filter) {
1240  int diff = nl_object_match_filter(obj, filter);
1241 
1242  NL_DBG(3, "%p<->%p object difference: %x\n",
1243  obj, filter, diff);
1244 
1245  if (!diff)
1246  continue;
1247  }
1248 
1249  /* Caller may hold obj for a long time */
1250  nl_object_get(obj);
1251 
1252  cb(obj, arg);
1253 
1254  nl_object_put(obj);
1255  }
1256 }
1257 
1258 /** @} */
1259 
1260 /** @} */
char * dp_buf
Alternatively the output may be redirected into a buffer.
Definition: types.h:88
void nl_cache_ops_put(struct nl_cache_ops *ops)
Decrement reference counter.
Definition: cache_mngt.c:65
struct nl_object * nl_cache_get_prev(struct nl_object *obj)
Return the previous element in the cache.
Definition: cache.c:158
int nl_cache_alloc_name(const char *kind, struct nl_cache **result)
Allocate new cache based on type name.
Definition: cache.c:264
struct nl_cache * nl_cache_subset(struct nl_cache *orig, struct nl_object *filter)
Allocate new cache containing a subset of an existing cache.
Definition: cache.c:297
Customized handler specified by the user.
Definition: handlers.h:80
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition: object.c:204
void nl_cache_get(struct nl_cache *cache)
Increase reference counter of cache.
Definition: cache.c:391
void nl_hash_table_free(nl_hash_table_t *ht)
Free hashtable including all nodes.
Definition: hashtable.c:56
struct nl_object * nl_cache_find(struct nl_cache *cache, struct nl_object *filter)
Find object in cache.
Definition: cache.c:1096
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:1164
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:1230
struct nl_object * nl_cache_get_last(struct nl_cache *cache)
Return the last element in the cache.
Definition: cache.c:132
nl_hash_table_t * nl_hash_table_alloc(int size)
Allocate hashtable.
Definition: hashtable.c:29
Dump all attributes but no statistics.
Definition: types.h:23
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
Definition: handlers.c:230
uint32_t nl_object_get_id_attrs(struct nl_object *obj)
Return object id attribute mask.
Definition: object.c:526
int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Set up a callback.
Definition: handlers.c:293
int nl_cache_pickup_checkdup(struct nl_sock *sk, struct nl_cache *cache)
Pickup a netlink dump response and put it into a cache.
Definition: cache.c:761
int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket.
Definition: nl.c:1060
struct nl_object * nl_hash_table_lookup(nl_hash_table_t *ht, struct nl_object *obj)
Lookup identical object in hashtable.
Definition: hashtable.c:86
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:408
void nl_cache_set_arg1(struct nl_cache *cache, int arg)
Set synchronization arg1 of cache.
Definition: cache.c:590
struct nl_object * nl_cache_search(struct nl_cache *cache, struct nl_object *needle)
Search object in cache.
Definition: cache.c:1061
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.
Definition: cache.c:1213
Skip this message.
Definition: handlers.h:63
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Definition: msg.c:536
void nl_cache_remove(struct nl_object *obj)
Remove object from cache.
Definition: cache.c:551
int nl_hash_table_add(nl_hash_table_t *ht, struct nl_object *obj)
Add object to hashtable.
Definition: hashtable.c:117
int nl_cache_is_empty(struct nl_cache *cache)
Returns true if the cache is empty.
Definition: cache.c:101
void nl_cache_clear(struct nl_cache *cache)
Remove all objects of a cache.
Definition: cache.c:366
struct nl_cache_ops * nl_cache_ops_lookup_safe(const char *name)
Lookup cache operations by name.
Definition: cache_mngt.c:99
void nl_cache_set_arg2(struct nl_cache *cache, int arg)
Set synchronization arg2 of cache.
Definition: cache.c:603
void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
Dump all elements of a cache.
Definition: cache.c:1150
Message is valid.
Definition: handlers.h:92
int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
Parse a netlink message and add it to the cache.
Definition: cache.c:968
int nl_cache_nitems(struct nl_cache *cache)
Return the number of items in the cache.
Definition: cache.c:68
struct nl_object * nl_cache_get_next(struct nl_object *obj)
Return the next element in the cache.
Definition: cache.c:145
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
Definition: cache.c:988
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition: object.c:215
void nl_cache_mark_all(struct nl_cache *cache)
Mark all objects of a cache.
Definition: cache.c:1125
int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
Move object from one cache to another.
Definition: cache.c:523
int nl_object_identical(struct nl_object *a, struct nl_object *b)
Check if the identifiers of two objects are identical.
Definition: object.c:313
int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
Return the number of items matching a filter in the cache.
Definition: cache.c:78
Dumping parameters.
Definition: types.h:33
void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags)
Set cache flags.
Definition: cache.c:613
int nl_hash_table_del(nl_hash_table_t *ht, struct nl_object *obj)
Remove object from hashtable.
Definition: hashtable.c:161
struct nl_cache_ops * nl_cache_get_ops(struct nl_cache *cache)
Return the operations set of the cache.
Definition: cache.c:110
size_t dp_buflen
Length of the buffer dp_buf.
Definition: types.h:93
#define NL_CACHE_AF_ITER
Explicitely iterate over all address families when updating the cache.
Definition: cache.h:43
int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
Match a filter against an object.
Definition: object.c:380
void nl_object_mark(struct nl_object *obj)
Add mark to object.
Definition: object.c:252
struct nl_cache * nl_cache_clone(struct nl_cache *cache)
Allocate new cache and copy the contents of an existing cache.
Definition: cache.c:337
int nl_object_update(struct nl_object *dst, struct nl_object *src)
Merge a cacheable object.
Definition: object.c:154
int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
Add object to cache.
Definition: cache.c:479
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:110
int nl_object_is_marked(struct nl_object *obj)
Return true if object is marked.
Definition: object.c:271
struct nl_object * nl_cache_get_first(struct nl_cache *cache)
Return the first element in the cache.
Definition: cache.c:119
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, struct nl_cache **result)
Allocate new cache and fill it.
Definition: cache.c:233
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate new cache.
Definition: cache.c:183
uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
Compute bitmask representing difference in attribute values.
Definition: object.c:361
int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
Pickup a netlink dump response and put it into a cache.
Definition: cache.c:779