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));
699 struct nl_cache *cache = (
struct nl_cache *)p->
pp_arg;
736 return __cache_pickup(sk, cache, &p);
739 static int cache_include(
struct nl_cache *cache,
struct nl_object *obj,
740 struct nl_msgtype *type, change_func_t cb,
void *data)
756 cb(cache, old, NL_ACT_CHANGE, data);
762 if (type->
mt_act == NL_ACT_DEL) {
764 cb(cache, old, NL_ACT_DEL, data);
769 if (type->
mt_act == NL_ACT_NEW) {
771 if (old == NULL && cb)
772 cb(cache, obj, NL_ACT_NEW, data);
775 cb(cache, obj, NL_ACT_CHANGE, data);
782 NL_DBG(2,
"Unknown action associated to object %p\n", obj);
789 int nl_cache_include(
struct nl_cache *cache,
struct nl_object *obj,
790 change_func_t change_cb,
void *data)
796 return -NLE_OBJ_MISMATCH;
798 for (i = 0; ops->co_msgtypes[i].
mt_id >= 0; i++)
799 if (ops->co_msgtypes[i].
mt_id == obj->ce_msgtype)
800 return cache_include(cache, obj, &ops->co_msgtypes[i],
803 return -NLE_MSGTYPE_NOSUPPORT;
808 struct nl_cache_assoc *ca = p->
pp_arg;
810 return nl_cache_include(ca->ca_cache, c, ca->ca_change, ca->ca_change_data);
813 int nl_cache_resync(
struct nl_sock *sk,
struct nl_cache *cache,
814 change_func_t change_cb,
void *data)
818 struct nl_cache_assoc ca = {
820 .ca_change = change_cb,
821 .ca_change_data = data,
829 NL_DBG(1,
"Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
834 grp = cache->c_ops->co_groups;
841 err = nl_cache_request_full_dump(sk, cache);
845 err = __cache_pickup(sk, cache, &p);
846 if (err == -NLE_DUMP_INTR)
856 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) {
861 change_cb(cache, obj, NL_ACT_DEL, data);
866 NL_DBG(1,
"Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
881 int nl_cache_parse(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
887 return -NLE_MSG_TOOSHORT;
889 for (i = 0; ops->co_msgtypes[i].
mt_id >= 0; i++) {
890 if (ops->co_msgtypes[i].
mt_id == nlh->nlmsg_type) {
892 if (err != -NLE_OPNOTSUPP)
898 err = -NLE_MSGTYPE_NOSUPPORT;
924 return nl_cache_parse(cache->c_ops, NULL,
nlmsg_hdr(msg), &p);
943 grp = cache->c_ops->co_groups;
950 err = nl_cache_request_full_dump(sk, cache);
955 if (err == -NLE_DUMP_INTR) {
956 NL_DBG(1,
"dump interrupted, restarting!\n");
966 NL_DBG(2,
"Upading cache %p <%s>, request sent, waiting for dump...\n",
967 cache, nl_cache_name(cache));
978 static struct nl_object *__cache_fast_lookup(
struct nl_cache *cache,
1012 if (cache->hashtable)
1013 return __cache_fast_lookup(cache, needle);
1015 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1047 if (cache->c_ops == NULL)
1051 && cache->hashtable)
1052 return __cache_fast_lookup(cache, filter);
1054 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1075 NL_DBG(2,
"Marking all objects in cache %p <%s>...\n",
1076 cache, nl_cache_name(cache));
1078 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
1118 NL_DBG(2,
"Dumping cache %p <%s> filter %p\n",
1119 cache, nl_cache_name(cache), filter);
1121 if (type > NL_DUMP_MAX || type < 0)
1124 if (cache->c_ops == NULL)
1127 ops = cache->c_ops->co_obj_ops;
1134 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1138 NL_DBG(4,
"Dumping object %p...\n", obj);
1139 dump_from_ops(obj, params);
1160 void (*cb)(
struct nl_object *,
void *),
void *arg)
1177 void (*cb)(
struct nl_object *,
void *),
void *arg)
1181 if (cache->c_ops == NULL)
1184 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
1188 NL_DBG(3,
"%p<->%p object difference: %x\n",