52 #include <netlink-local.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>
70 return cache->c_nitems;
83 if (cache->c_ops == NULL)
86 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
103 return nl_list_empty(&cache->c_items);
121 if (nl_list_empty(&cache->c_items))
124 return nl_list_entry(cache->c_items.next,
134 if (nl_list_empty(&cache->c_items))
137 return nl_list_entry(cache->c_items.prev,
147 if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
150 return nl_list_entry(obj->ce_list.next,
160 if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
163 return nl_list_entry(obj->ce_list.prev,
185 struct nl_cache *cache;
187 cache = calloc(1,
sizeof(*cache));
191 nl_init_list_head(&cache->c_items);
207 hashtable_size = NL_MAX_HASH_ENTRIES;
212 NL_DBG(2,
"Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
234 struct nl_cache **result)
236 struct nl_cache *cache;
267 struct nl_cache *cache;
300 struct nl_cache *cache;
310 nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
337 struct nl_cache *clone;
344 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
365 NL_DBG(1,
"Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
367 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
371 static void __nl_cache_free(
struct nl_cache *cache)
375 if (cache->hashtable)
378 NL_DBG(1,
"Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
406 NL_DBG(4,
"Returned cache reference %p, %d remaining\n",
407 cache, cache->c_refcnt);
409 if (cache->c_refcnt <= 0)
410 __nl_cache_free(cache);
413 void nl_cache_put(
struct nl_cache *cache)
425 static int __cache_add(
struct nl_cache *cache,
struct nl_object *obj)
429 obj->ce_cache = cache;
431 if (cache->hashtable) {
434 obj->ce_cache = NULL;
439 nl_list_add_tail(&obj->ce_list, &cache->c_items);
442 NL_DBG(1,
"Added %p to cache %p <%s>.\n",
443 obj, cache, nl_cache_name(cache));
475 if (cache->c_ops->co_obj_ops != obj->ce_ops)
476 return -NLE_OBJ_MISMATCH;
478 if (!nl_list_empty(&obj->ce_list)) {
487 ret = __cache_add(cache,
new);
514 if (cache->c_ops->co_obj_ops != obj->ce_ops)
515 return -NLE_OBJ_MISMATCH;
517 NL_DBG(3,
"Moving object %p to cache %p\n", obj, cache);
523 if (!nl_list_empty(&obj->ce_list))
526 return __cache_add(cache, obj);
542 struct nl_cache *cache = obj->ce_cache;
547 if (cache->hashtable) {
550 NL_DBG(3,
"Failed to delete %p from cache %p <%s>.\n",
551 obj, cache, nl_cache_name(cache));
554 nl_list_del(&obj->ce_list);
555 obj->ce_cache = NULL;
559 NL_DBG(1,
"Deleted %p from cache %p <%s>.\n",
560 obj, cache, nl_cache_name(cache));
580 cache->c_iarg1 = arg;
593 cache->c_iarg2 = arg;
603 cache->c_flags |= flags;
630 static int nl_cache_request_full_dump(
struct nl_sock *sk,
631 struct nl_cache *cache)
633 NL_DBG(2,
"Requesting dump from kernel for cache %p <%s>...\n",
634 cache, nl_cache_name(cache));
636 if (cache->c_ops->co_request_update == NULL)
637 return -NLE_OPNOTSUPP;
639 return cache->c_ops->co_request_update(cache, sk);
643 struct update_xdata {
648 static int update_msg_parser(
struct nl_msg *msg,
void *arg)
650 struct update_xdata *x = arg;
653 ret = nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
654 if (ret == -NLE_EXIST)
667 static int __cache_pickup(
struct nl_sock *sk,
struct nl_cache *cache,
672 struct update_xdata x = {
677 NL_DBG(1,
"Picking up answer for cache %p <%s>...\n",
678 cache, nl_cache_name(cache));
688 NL_DBG(2,
"While picking up for %p <%s>, recvmsgs() returned " \
689 "%d: %s", cache, nl_cache_name(cache),
690 err, nl_geterror(err));
719 return __cache_pickup(sk, cache, &p);
722 static int cache_include(
struct nl_cache *cache,
struct nl_object *obj,
723 struct nl_msgtype *type, change_func_t cb,
void *data)
738 cb(cache, old, NL_ACT_CHANGE, data);
744 if (type->
mt_act == NL_ACT_DEL) {
746 cb(cache, old, NL_ACT_DEL, data);
751 if (type->
mt_act == NL_ACT_NEW) {
753 if (old == NULL && cb)
754 cb(cache, obj, NL_ACT_NEW, data);
757 cb(cache, obj, NL_ACT_CHANGE, data);
764 NL_DBG(2,
"Unknown action associated to object %p\n", obj);
771 int nl_cache_include(
struct nl_cache *cache,
struct nl_object *obj,
772 change_func_t change_cb,
void *data)
778 return -NLE_OBJ_MISMATCH;
780 for (i = 0; ops->co_msgtypes[i].
mt_id >= 0; i++)
781 if (ops->co_msgtypes[i].
mt_id == obj->ce_msgtype)
782 return cache_include(cache, obj, &ops->co_msgtypes[i],
785 return -NLE_MSGTYPE_NOSUPPORT;
790 struct nl_cache_assoc *ca = p->
pp_arg;
792 return nl_cache_include(ca->ca_cache, c, ca->ca_change, ca->ca_change_data);
795 int nl_cache_resync(
struct nl_sock *sk,
struct nl_cache *cache,
796 change_func_t change_cb,
void *data)
800 struct nl_cache_assoc ca = {
802 .ca_change = change_cb,
803 .ca_change_data = data,
811 NL_DBG(1,
"Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
816 grp = cache->c_ops->co_groups;
823 err = nl_cache_request_full_dump(sk, cache);
827 err = __cache_pickup(sk, cache, &p);
828 if (err == -NLE_DUMP_INTR)
836 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) {
841 change_cb(cache, obj, NL_ACT_DEL, data);
846 NL_DBG(1,
"Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
861 int nl_cache_parse(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
867 return -NLE_MSG_TOOSHORT;
869 for (i = 0; ops->co_msgtypes[i].
mt_id >= 0; i++) {
870 if (ops->co_msgtypes[i].
mt_id == nlh->nlmsg_type) {
872 if (err != -NLE_OPNOTSUPP)
878 err = -NLE_MSGTYPE_NOSUPPORT;
901 return nl_cache_parse(cache->c_ops, NULL,
nlmsg_hdr(msg), &p);
920 grp = cache->c_ops->co_groups;
927 err = nl_cache_request_full_dump(sk, cache);
932 if (err == -NLE_DUMP_INTR) {
933 NL_DBG(1,
"dump interrupted, restarting!\n");
942 NL_DBG(2,
"Upading cache %p <%s>, request sent, waiting for dump...\n",
943 cache, nl_cache_name(cache));
954 static struct nl_object *__cache_fast_lookup(
struct nl_cache *cache,
988 if (cache->hashtable)
989 return __cache_fast_lookup(cache, needle);
991 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1012 NL_DBG(2,
"Marking all objects in cache %p <%s>...\n",
1013 cache, nl_cache_name(cache));
1015 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
1055 NL_DBG(2,
"Dumping cache %p <%s> filter %p\n",
1056 cache, nl_cache_name(cache), filter);
1058 if (type > NL_DUMP_MAX || type < 0)
1061 if (cache->c_ops == NULL)
1064 ops = cache->c_ops->co_obj_ops;
1071 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1075 NL_DBG(4,
"Dumping object %p...\n", obj);
1076 dump_from_ops(obj, params);
1097 void (*cb)(
struct nl_object *,
void *),
void *arg)
1114 void (*cb)(
struct nl_object *,
void *),
void *arg)
1118 if (cache->c_ops == NULL)
1121 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
1125 NL_DBG(3,
"%p<->%p object difference: %x\n",