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>
70 return cache->c_nitems;
80 struct nl_object *obj;
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,
125 struct nl_object, ce_list);
134 if (nl_list_empty(&cache->c_items))
137 return nl_list_entry(cache->c_items.prev,
138 struct nl_object, ce_list);
147 if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
150 return nl_list_entry(obj->ce_list.next,
151 struct nl_object, ce_list);
160 if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
163 return nl_list_entry(obj->ce_list.prev,
164 struct nl_object, ce_list);
185 struct nl_cache *cache;
187 cache = calloc(1,
sizeof(*cache));
191 nl_init_list_head(&cache->c_items);
193 cache->c_flags |= ops->co_flags;
201 if (ops->co_obj_ops->oo_keygen) {
204 if (ops->co_hash_size)
205 hashtable_size = ops->co_hash_size;
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;
266 struct nl_cache_ops *ops;
267 struct nl_cache *cache;
298 struct nl_object *filter)
300 struct nl_cache *cache;
301 struct nl_object *obj;
310 nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
337 struct nl_cache *clone;
338 struct nl_object *obj;
344 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
363 struct nl_object *obj, *tmp;
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));
472 struct nl_object *
new;
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 {
644 struct nl_cache_ops *ops;
645 struct nl_parser_param *params;
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,
668 struct nl_parser_param *param)
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));
697 static int pickup_cb(
struct nl_object *c,
struct nl_parser_param *p)
699 struct nl_cache *cache = (
struct nl_cache *)p->pp_arg;
700 struct nl_object *old;
731 struct nl_parser_param p = {
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)
742 struct nl_object *old;
744 switch (type->mt_act) {
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)
792 struct nl_cache_ops *ops = cache->c_ops;
795 if (ops->co_obj_ops != obj->ce_ops)
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;
806 static int resync_cb(
struct nl_object *c,
struct nl_parser_param *p)
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)
816 struct nl_object *obj, *next;
817 struct nl_af_group *grp;
818 struct nl_cache_assoc ca = {
820 .ca_change = change_cb,
821 .ca_change_data = data,
823 struct nl_parser_param p = {
829 NL_DBG(1,
"Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
834 grp = cache->c_ops->co_groups;
836 if (grp && grp->ag_group &&
841 err = nl_cache_request_full_dump(sk, cache);
845 err = __cache_pickup(sk, cache, &p);
846 if (err == -NLE_DUMP_INTR)
853 }
while (grp && grp->ag_group &&
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,
882 struct nlmsghdr *nlh,
struct nl_parser_param *params)
886 if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize))
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) {
891 err = ops->co_msg_parser(ops, who, nlh, params);
892 if (err != -NLE_OPNOTSUPP)
898 err = -NLE_MSGTYPE_NOSUPPORT;
919 struct nl_parser_param p = {
924 return nl_cache_parse(cache->c_ops, NULL,
nlmsg_hdr(msg), &p);
939 struct nl_af_group *grp;
943 grp = cache->c_ops->co_groups;
945 if (grp && grp->ag_group &&
950 err = nl_cache_request_full_dump(sk, cache);
955 if (err == -NLE_DUMP_INTR) {
956 NL_DBG(1,
"dump interrupted, restarting!\n");
963 }
while (grp && grp->ag_group &&
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,
979 struct nl_object *needle)
981 struct nl_object *obj;
1008 struct nl_object *needle)
1010 struct nl_object *obj;
1012 if (cache->hashtable)
1013 return __cache_fast_lookup(cache, needle);
1015 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1043 struct nl_object *filter)
1045 struct nl_object *obj;
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) {
1073 struct nl_object *obj;
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)
1112 struct nl_object *filter)
1115 struct nl_object_ops *ops;
1116 struct nl_object *obj;
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;
1128 if (!ops->oo_dump[type])
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)
1179 struct nl_object *obj, *tmp;
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",