firmware-utils: move bcm_tag.h here
[openwrt/svn-archive/archive.git] / package / libnl-tiny / src / 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-2008 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 * @{
41 */
42
43 #include <netlink-local.h>
44 #include <netlink/netlink.h>
45 #include <netlink/cache.h>
46 #include <netlink/object.h>
47 #include <netlink/utils.h>
48
49 /**
50 * @name Access Functions
51 * @{
52 */
53
54 #ifdef disabled
55 /**
56 * Return the number of items in the cache
57 * @arg cache cache handle
58 */
59 int nl_cache_nitems(struct nl_cache *cache)
60 {
61 return cache->c_nitems;
62 }
63
64 /**
65 * Return the number of items matching a filter in the cache
66 * @arg cache Cache object.
67 * @arg filter Filter object.
68 */
69 int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
70 {
71 struct nl_object_ops *ops;
72 struct nl_object *obj;
73 int nitems = 0;
74
75 if (cache->c_ops == NULL)
76 BUG();
77
78 ops = cache->c_ops->co_obj_ops;
79
80 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
81 if (filter && !nl_object_match_filter(obj, filter))
82 continue;
83
84 nitems++;
85 }
86
87 return nitems;
88 }
89
90 /**
91 * Returns \b true if the cache is empty.
92 * @arg cache Cache to check
93 * @return \a true if the cache is empty, otherwise \b false is returned.
94 */
95 int nl_cache_is_empty(struct nl_cache *cache)
96 {
97 return nl_list_empty(&cache->c_items);
98 }
99
100 /**
101 * Return the operations set of the cache
102 * @arg cache cache handle
103 */
104 struct nl_cache_ops *nl_cache_get_ops(struct nl_cache *cache)
105 {
106 return cache->c_ops;
107 }
108
109 /**
110 * Return the first element in the cache
111 * @arg cache cache handle
112 */
113 struct nl_object *nl_cache_get_first(struct nl_cache *cache)
114 {
115 if (nl_list_empty(&cache->c_items))
116 return NULL;
117
118 return nl_list_entry(cache->c_items.next,
119 struct nl_object, ce_list);
120 }
121
122 /**
123 * Return the last element in the cache
124 * @arg cache cache handle
125 */
126 struct nl_object *nl_cache_get_last(struct nl_cache *cache)
127 {
128 if (nl_list_empty(&cache->c_items))
129 return NULL;
130
131 return nl_list_entry(cache->c_items.prev,
132 struct nl_object, ce_list);
133 }
134
135 /**
136 * Return the next element in the cache
137 * @arg obj current object
138 */
139 struct nl_object *nl_cache_get_next(struct nl_object *obj)
140 {
141 if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
142 return NULL;
143 else
144 return nl_list_entry(obj->ce_list.next,
145 struct nl_object, ce_list);
146 }
147
148 /**
149 * Return the previous element in the cache
150 * @arg obj current object
151 */
152 struct nl_object *nl_cache_get_prev(struct nl_object *obj)
153 {
154 if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
155 return NULL;
156 else
157 return nl_list_entry(obj->ce_list.prev,
158 struct nl_object, ce_list);
159 }
160 #endif
161
162 /** @} */
163
164 /**
165 * @name Cache Creation/Deletion
166 * @{
167 */
168
169 /**
170 * Allocate an empty cache
171 * @arg ops cache operations to base the cache on
172 *
173 * @return A newly allocated and initialized cache.
174 */
175 struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
176 {
177 struct nl_cache *cache;
178
179 cache = calloc(1, sizeof(*cache));
180 if (!cache)
181 return NULL;
182
183 nl_init_list_head(&cache->c_items);
184 cache->c_ops = ops;
185
186 NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
187
188 return cache;
189 }
190
191 int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock,
192 struct nl_cache **result)
193 {
194 struct nl_cache *cache;
195 int err;
196
197 if (!(cache = nl_cache_alloc(ops)))
198 return -NLE_NOMEM;
199
200 if (sock && (err = nl_cache_refill(sock, cache)) < 0) {
201 nl_cache_free(cache);
202 return err;
203 }
204
205 *result = cache;
206 return 0;
207 }
208
209 #ifdef disabled
210 /**
211 * Allocate an empty cache based on type name
212 * @arg kind Name of cache type
213 * @return A newly allocated and initialized cache.
214 */
215 int nl_cache_alloc_name(const char *kind, struct nl_cache **result)
216 {
217 struct nl_cache_ops *ops;
218 struct nl_cache *cache;
219
220 ops = nl_cache_ops_lookup(kind);
221 if (!ops)
222 return -NLE_NOCACHE;
223
224 if (!(cache = nl_cache_alloc(ops)))
225 return -NLE_NOMEM;
226
227 *result = cache;
228 return 0;
229 }
230
231 /**
232 * Allocate a new cache containing a subset of a cache
233 * @arg orig Original cache to be based on
234 * @arg filter Filter defining the subset to be filled into new cache
235 * @return A newly allocated cache or NULL.
236 */
237 struct nl_cache *nl_cache_subset(struct nl_cache *orig,
238 struct nl_object *filter)
239 {
240 struct nl_cache *cache;
241 struct nl_object_ops *ops;
242 struct nl_object *obj;
243
244 if (!filter)
245 BUG();
246
247 cache = nl_cache_alloc(orig->c_ops);
248 if (!cache)
249 return NULL;
250
251 ops = orig->c_ops->co_obj_ops;
252
253 nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
254 if (!nl_object_match_filter(obj, filter))
255 continue;
256
257 nl_cache_add(cache, obj);
258 }
259
260 return cache;
261 }
262 #endif
263
264 /**
265 * Clear a cache.
266 * @arg cache cache to clear
267 *
268 * Removes all elements of a cache.
269 */
270 void nl_cache_clear(struct nl_cache *cache)
271 {
272 struct nl_object *obj, *tmp;
273
274 NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
275
276 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
277 nl_cache_remove(obj);
278 }
279
280 /**
281 * Free a cache.
282 * @arg cache Cache to free.
283 *
284 * Removes all elements of a cache and frees all memory.
285 *
286 * @note Use this function if you are working with allocated caches.
287 */
288 void nl_cache_free(struct nl_cache *cache)
289 {
290 if (!cache)
291 return;
292
293 nl_cache_clear(cache);
294 NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
295 free(cache);
296 }
297
298 /** @} */
299
300 /**
301 * @name Cache Modifications
302 * @{
303 */
304
305 static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
306 {
307 obj->ce_cache = cache;
308
309 nl_list_add_tail(&obj->ce_list, &cache->c_items);
310 cache->c_nitems++;
311
312 NL_DBG(1, "Added %p to cache %p <%s>.\n",
313 obj, cache, nl_cache_name(cache));
314
315 return 0;
316 }
317
318 /**
319 * Add object to a cache.
320 * @arg cache Cache to add object to
321 * @arg obj Object to be added to the cache
322 *
323 * Adds the given object to the specified cache. The object is cloned
324 * if it has been added to another cache already.
325 *
326 * @return 0 or a negative error code.
327 */
328 int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
329 {
330 struct nl_object *new;
331
332 if (cache->c_ops->co_obj_ops != obj->ce_ops)
333 return -NLE_OBJ_MISMATCH;
334
335 if (!nl_list_empty(&obj->ce_list)) {
336 new = nl_object_clone(obj);
337 if (!new)
338 return -NLE_NOMEM;
339 } else {
340 nl_object_get(obj);
341 new = obj;
342 }
343
344 return __cache_add(cache, new);
345 }
346
347 #ifdef disabled
348 /**
349 * Move object from one cache to another
350 * @arg cache Cache to move object to.
351 * @arg obj Object subject to be moved
352 *
353 * Removes the given object from its associated cache if needed
354 * and adds it to the new cache.
355 *
356 * @return 0 on success or a negative error code.
357 */
358 int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
359 {
360 if (cache->c_ops->co_obj_ops != obj->ce_ops)
361 return -NLE_OBJ_MISMATCH;
362
363 NL_DBG(3, "Moving object %p to cache %p\n", obj, cache);
364
365 /* Acquire reference, if already in a cache this will be
366 * reverted during removal */
367 nl_object_get(obj);
368
369 if (!nl_list_empty(&obj->ce_list))
370 nl_cache_remove(obj);
371
372 return __cache_add(cache, obj);
373 }
374 #endif
375
376 /**
377 * Removes an object from a cache.
378 * @arg obj Object to remove from its cache
379 *
380 * Removes the object \c obj from the cache it is assigned to, since
381 * an object can only be assigned to one cache at a time, the cache
382 * must ne be passed along with it.
383 */
384 void nl_cache_remove(struct nl_object *obj)
385 {
386 struct nl_cache *cache = obj->ce_cache;
387
388 if (cache == NULL)
389 return;
390
391 nl_list_del(&obj->ce_list);
392 obj->ce_cache = NULL;
393 nl_object_put(obj);
394 cache->c_nitems--;
395
396 NL_DBG(1, "Deleted %p from cache %p <%s>.\n",
397 obj, cache, nl_cache_name(cache));
398 }
399
400 #ifdef disabled
401 /**
402 * Search for an object in a cache
403 * @arg cache Cache to search in.
404 * @arg needle Object to look for.
405 *
406 * Iterates over the cache and looks for an object with identical
407 * identifiers as the needle.
408 *
409 * @return Reference to object or NULL if not found.
410 * @note The returned object must be returned via nl_object_put().
411 */
412 struct nl_object *nl_cache_search(struct nl_cache *cache,
413 struct nl_object *needle)
414 {
415 struct nl_object *obj;
416
417 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
418 if (nl_object_identical(obj, needle)) {
419 nl_object_get(obj);
420 return obj;
421 }
422 }
423
424 return NULL;
425 }
426 #endif
427
428 /** @} */
429
430 /**
431 * @name Synchronization
432 * @{
433 */
434
435 /**
436 * Request a full dump from the kernel to fill a cache
437 * @arg sk Netlink socket.
438 * @arg cache Cache subjected to be filled.
439 *
440 * Send a dumping request to the kernel causing it to dump all objects
441 * related to the specified cache to the netlink socket.
442 *
443 * Use nl_cache_pickup() to read the objects from the socket and fill them
444 * into a cache.
445 */
446 int nl_cache_request_full_dump(struct nl_sock *sk, struct nl_cache *cache)
447 {
448 NL_DBG(2, "Requesting dump from kernel for cache %p <%s>...\n",
449 cache, nl_cache_name(cache));
450
451 if (cache->c_ops->co_request_update == NULL)
452 return -NLE_OPNOTSUPP;
453
454 return cache->c_ops->co_request_update(cache, sk);
455 }
456
457 /** @cond SKIP */
458 struct update_xdata {
459 struct nl_cache_ops *ops;
460 struct nl_parser_param *params;
461 };
462
463 static int update_msg_parser(struct nl_msg *msg, void *arg)
464 {
465 struct update_xdata *x = arg;
466
467 return nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
468 }
469 /** @endcond */
470
471 int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache,
472 struct nl_parser_param *param)
473 {
474 int err;
475 struct nl_cb *cb;
476 struct update_xdata x = {
477 .ops = cache->c_ops,
478 .params = param,
479 };
480
481 NL_DBG(1, "Picking up answer for cache %p <%s>...\n",
482 cache, nl_cache_name(cache));
483
484 cb = nl_cb_clone(sk->s_cb);
485 if (cb == NULL)
486 return -NLE_NOMEM;
487
488 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x);
489
490 err = nl_recvmsgs(sk, cb);
491 if (err < 0)
492 NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned " \
493 "%d: %s", cache, nl_cache_name(cache),
494 err, nl_geterror(err));
495
496 nl_cb_put(cb);
497
498 return err;
499 }
500
501 static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
502 {
503 return nl_cache_add((struct nl_cache *) p->pp_arg, c);
504 }
505
506 /**
507 * Pickup a netlink dump response and put it into a cache.
508 * @arg sk Netlink socket.
509 * @arg cache Cache to put items into.
510 *
511 * Waits for netlink messages to arrive, parses them and puts them into
512 * the specified cache.
513 *
514 * @return 0 on success or a negative error code.
515 */
516 int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
517 {
518 struct nl_parser_param p = {
519 .pp_cb = pickup_cb,
520 .pp_arg = cache,
521 };
522
523 return __cache_pickup(sk, cache, &p);
524 }
525
526 #ifdef disabled
527 static int cache_include(struct nl_cache *cache, struct nl_object *obj,
528 struct nl_msgtype *type, change_func_t cb)
529 {
530 struct nl_object *old;
531
532 switch (type->mt_act) {
533 case NL_ACT_NEW:
534 case NL_ACT_DEL:
535 old = nl_cache_search(cache, obj);
536 if (old) {
537 nl_cache_remove(old);
538 if (type->mt_act == NL_ACT_DEL) {
539 if (cb)
540 cb(cache, old, NL_ACT_DEL);
541 nl_object_put(old);
542 }
543 }
544
545 if (type->mt_act == NL_ACT_NEW) {
546 nl_cache_move(cache, obj);
547 if (old == NULL && cb)
548 cb(cache, obj, NL_ACT_NEW);
549 else if (old) {
550 if (nl_object_diff(old, obj) && cb)
551 cb(cache, obj, NL_ACT_CHANGE);
552
553 nl_object_put(old);
554 }
555 }
556 break;
557 default:
558 NL_DBG(2, "Unknown action associated to object %p\n", obj);
559 return 0;
560 }
561
562 return 0;
563 }
564
565 int nl_cache_include(struct nl_cache *cache, struct nl_object *obj,
566 change_func_t change_cb)
567 {
568 struct nl_cache_ops *ops = cache->c_ops;
569 int i;
570
571 if (ops->co_obj_ops != obj->ce_ops)
572 return -NLE_OBJ_MISMATCH;
573
574 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
575 if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
576 return cache_include(cache, obj, &ops->co_msgtypes[i],
577 change_cb);
578
579 return -NLE_MSGTYPE_NOSUPPORT;
580 }
581
582 static int resync_cb(struct nl_object *c, struct nl_parser_param *p)
583 {
584 struct nl_cache_assoc *ca = p->pp_arg;
585
586 return nl_cache_include(ca->ca_cache, c, ca->ca_change);
587 }
588
589 int nl_cache_resync(struct nl_sock *sk, struct nl_cache *cache,
590 change_func_t change_cb)
591 {
592 struct nl_object *obj, *next;
593 struct nl_cache_assoc ca = {
594 .ca_cache = cache,
595 .ca_change = change_cb,
596 };
597 struct nl_parser_param p = {
598 .pp_cb = resync_cb,
599 .pp_arg = &ca,
600 };
601 int err;
602
603 NL_DBG(1, "Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
604
605 /* Mark all objects so we can see if some of them are obsolete */
606 nl_cache_mark_all(cache);
607
608 err = nl_cache_request_full_dump(sk, cache);
609 if (err < 0)
610 goto errout;
611
612 err = __cache_pickup(sk, cache, &p);
613 if (err < 0)
614 goto errout;
615
616 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list)
617 if (nl_object_is_marked(obj))
618 nl_cache_remove(obj);
619
620 NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
621
622 err = 0;
623 errout:
624 return err;
625 }
626 #endif
627
628 /** @} */
629
630 /**
631 * @name Parsing
632 * @{
633 */
634
635 /** @cond SKIP */
636 int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who,
637 struct nlmsghdr *nlh, struct nl_parser_param *params)
638 {
639 int i, err;
640
641 if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize))
642 return -NLE_MSG_TOOSHORT;
643
644 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
645 if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
646 err = ops->co_msg_parser(ops, who, nlh, params);
647 if (err != -NLE_OPNOTSUPP)
648 goto errout;
649 }
650 }
651
652
653 err = -NLE_MSGTYPE_NOSUPPORT;
654 errout:
655 return err;
656 }
657 /** @endcond */
658
659 /**
660 * Parse a netlink message and add it to the cache.
661 * @arg cache cache to add element to
662 * @arg msg netlink message
663 *
664 * Parses a netlink message by calling the cache specific message parser
665 * and adds the new element to the cache.
666 *
667 * @return 0 or a negative error code.
668 */
669 int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
670 {
671 struct nl_parser_param p = {
672 .pp_cb = pickup_cb,
673 .pp_arg = cache,
674 };
675
676 return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
677 }
678
679 /**
680 * (Re)fill a cache with the contents in the kernel.
681 * @arg sk Netlink socket.
682 * @arg cache cache to update
683 *
684 * Clears the specified cache and fills it with the current state in
685 * the kernel.
686 *
687 * @return 0 or a negative error code.
688 */
689 int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
690 {
691 int err;
692
693 err = nl_cache_request_full_dump(sk, cache);
694 if (err < 0)
695 return err;
696
697 NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n",
698 cache, nl_cache_name(cache));
699 nl_cache_clear(cache);
700
701 return nl_cache_pickup(sk, cache);
702 }
703
704 /** @} */
705 #ifdef disabled
706
707 /**
708 * @name Utillities
709 * @{
710 */
711
712 /**
713 * Mark all objects in a cache
714 * @arg cache Cache to mark all objects in
715 */
716 void nl_cache_mark_all(struct nl_cache *cache)
717 {
718 struct nl_object *obj;
719
720 NL_DBG(2, "Marking all objects in cache %p <%s>...\n",
721 cache, nl_cache_name(cache));
722
723 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
724 nl_object_mark(obj);
725 }
726
727 /** @} */
728
729 /**
730 * @name Dumping
731 * @{
732 */
733 /**
734 * Dump all elements of a cache.
735 * @arg cache cache to dump
736 * @arg params dumping parameters
737 *
738 * Dumps all elements of the \a cache to the file descriptor \a fd.
739 */
740 void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
741 {
742 nl_cache_dump_filter(cache, params, NULL);
743 }
744
745 /**
746 * Dump all elements of a cache (filtered).
747 * @arg cache cache to dump
748 * @arg params dumping parameters (optional)
749 * @arg filter filter object
750 *
751 * Dumps all elements of the \a cache to the file descriptor \a fd
752 * given they match the given filter \a filter.
753 */
754 void nl_cache_dump_filter(struct nl_cache *cache,
755 struct nl_dump_params *params,
756 struct nl_object *filter)
757 {
758 int type = params ? params->dp_type : NL_DUMP_DETAILS;
759 struct nl_object_ops *ops;
760 struct nl_object *obj;
761
762 NL_DBG(2, "Dumping cache %p <%s> filter %p\n",
763 cache, nl_cache_name(cache), filter);
764
765 if (type > NL_DUMP_MAX || type < 0)
766 BUG();
767
768 if (cache->c_ops == NULL)
769 BUG();
770
771 ops = cache->c_ops->co_obj_ops;
772 if (!ops->oo_dump[type])
773 return;
774
775 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
776 if (filter && !nl_object_match_filter(obj, filter))
777 continue;
778
779 NL_DBG(4, "Dumping object %p...\n", obj);
780 dump_from_ops(obj, params);
781 }
782 }
783
784 /** @} */
785
786 /**
787 * @name Iterators
788 * @{
789 */
790
791 /**
792 * Call a callback on each element of the cache.
793 * @arg cache cache to iterate on
794 * @arg cb callback function
795 * @arg arg argument passed to callback function
796 *
797 * Calls a callback function \a cb on each element of the \a cache.
798 * The argument \a arg is passed on the callback function.
799 */
800 void nl_cache_foreach(struct nl_cache *cache,
801 void (*cb)(struct nl_object *, void *), void *arg)
802 {
803 nl_cache_foreach_filter(cache, NULL, cb, arg);
804 }
805
806 /**
807 * Call a callback on each element of the cache (filtered).
808 * @arg cache cache to iterate on
809 * @arg filter filter object
810 * @arg cb callback function
811 * @arg arg argument passed to callback function
812 *
813 * Calls a callback function \a cb on each element of the \a cache
814 * that matches the \a filter. The argument \a arg is passed on
815 * to the callback function.
816 */
817 void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter,
818 void (*cb)(struct nl_object *, void *), void *arg)
819 {
820 struct nl_object *obj, *tmp;
821 struct nl_object_ops *ops;
822
823 if (cache->c_ops == NULL)
824 BUG();
825
826 ops = cache->c_ops->co_obj_ops;
827
828 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
829 if (filter && !nl_object_match_filter(obj, filter))
830 continue;
831
832 cb(obj, arg);
833 }
834 }
835
836 /** @} */
837 #endif
838
839 /** @} */