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_DBG(2,
"Filling subset of cache %p <%s> with filter %p into %p\n",
311 orig, nl_cache_name(orig), filter, cache);
313 nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
340 struct nl_cache *clone;
341 struct nl_object *obj;
347 NL_DBG(2,
"Cloning %p into %p\n", cache, clone);
349 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
368 struct nl_object *obj, *tmp;
370 NL_DBG(2,
"Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
372 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
376 static void __nl_cache_free(
struct nl_cache *cache)
380 if (cache->hashtable)
383 NL_DBG(2,
"Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
395 NL_DBG(3,
"Incremented cache %p <%s> reference count to %d\n",
396 cache, nl_cache_name(cache), cache->c_refcnt);
415 NL_DBG(3,
"Decremented cache %p <%s> reference count, %d remaining\n",
416 cache, nl_cache_name(cache), cache->c_refcnt);
418 if (cache->c_refcnt <= 0)
419 __nl_cache_free(cache);
422 void nl_cache_put(
struct nl_cache *cache)
434 static int __cache_add(
struct nl_cache *cache,
struct nl_object *obj)
438 obj->ce_cache = cache;
440 if (cache->hashtable) {
443 obj->ce_cache = NULL;
448 nl_list_add_tail(&obj->ce_list, &cache->c_items);
451 NL_DBG(3,
"Added object %p to cache %p <%s>, nitems %d\n",
452 obj, cache, nl_cache_name(cache), cache->c_nitems);
481 struct nl_object *
new;
484 if (cache->c_ops->co_obj_ops != obj->ce_ops)
485 return -NLE_OBJ_MISMATCH;
487 if (!nl_list_empty(&obj->ce_list)) {
488 NL_DBG(3,
"Object %p already in cache, cloning new object\n", obj);
498 ret = __cache_add(cache,
new);
525 if (cache->c_ops->co_obj_ops != obj->ce_ops)
526 return -NLE_OBJ_MISMATCH;
528 NL_DBG(3,
"Moving object %p from cache %p to cache %p\n",
529 obj, obj->ce_cache, cache);
535 if (!nl_list_empty(&obj->ce_list))
538 return __cache_add(cache, obj);
554 struct nl_cache *cache = obj->ce_cache;
559 if (cache->hashtable) {
562 NL_DBG(2,
"Failed to delete %p from cache %p <%s>.\n",
563 obj, cache, nl_cache_name(cache));
566 nl_list_del(&obj->ce_list);
567 obj->ce_cache = NULL;
571 NL_DBG(2,
"Deleted object %p from cache %p <%s>.\n",
572 obj, cache, nl_cache_name(cache));
592 cache->c_iarg1 = arg;
605 cache->c_iarg2 = arg;
615 cache->c_flags |= flags;
642 static int nl_cache_request_full_dump(
struct nl_sock *sk,
643 struct nl_cache *cache)
645 if (cache->c_ops->co_request_update == NULL)
646 return -NLE_OPNOTSUPP;
648 NL_DBG(2,
"Requesting update from kernel for cache %p <%s>\n",
649 cache, nl_cache_name(cache));
651 return cache->c_ops->co_request_update(cache, sk);
655 struct update_xdata {
656 struct nl_cache_ops *ops;
657 struct nl_parser_param *params;
660 static int update_msg_parser(
struct nl_msg *msg,
void *arg)
662 struct update_xdata *x = arg;
665 ret = nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
666 if (ret == -NLE_EXIST)
679 static int __cache_pickup(
struct nl_sock *sk,
struct nl_cache *cache,
680 struct nl_parser_param *param)
684 struct update_xdata x = {
689 NL_DBG(2,
"Picking up answer for cache %p <%s>\n",
690 cache, nl_cache_name(cache));
700 NL_DBG(2,
"While picking up for %p <%s>, recvmsgs() returned %d: %s\n",
701 cache, nl_cache_name(cache), err, nl_geterror(err));
708 static int pickup_cb(
struct nl_object *c,
struct nl_parser_param *p)
710 struct nl_cache *cache = (
struct nl_cache *)p->pp_arg;
711 struct nl_object *old;
742 struct nl_parser_param p = {
747 return __cache_pickup(sk, cache, &p);
750 static int cache_include(
struct nl_cache *cache,
struct nl_object *obj,
751 struct nl_msgtype *type, change_func_t cb,
void *data)
753 struct nl_object *old;
755 switch (type->mt_act) {
767 cb(cache, old, NL_ACT_CHANGE, data);
773 if (type->mt_act == NL_ACT_DEL) {
775 cb(cache, old, NL_ACT_DEL, data);
780 if (type->mt_act == NL_ACT_NEW) {
782 if (old == NULL && cb)
783 cb(cache, obj, NL_ACT_NEW, data);
786 cb(cache, obj, NL_ACT_CHANGE, data);
793 NL_DBG(2,
"Unknown action associated to object %p\n", obj);
800 int nl_cache_include(
struct nl_cache *cache,
struct nl_object *obj,
801 change_func_t change_cb,
void *data)
803 struct nl_cache_ops *ops = cache->c_ops;
806 if (ops->co_obj_ops != obj->ce_ops)
807 return -NLE_OBJ_MISMATCH;
809 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
810 if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
811 return cache_include(cache, obj, &ops->co_msgtypes[i],
814 NL_DBG(3,
"Object %p does not seem to belong to cache %p <%s>\n",
815 obj, cache, nl_cache_name(cache));
817 return -NLE_MSGTYPE_NOSUPPORT;
820 static int resync_cb(
struct nl_object *c,
struct nl_parser_param *p)
822 struct nl_cache_assoc *ca = p->pp_arg;
824 return nl_cache_include(ca->ca_cache, c, ca->ca_change, ca->ca_change_data);
827 int nl_cache_resync(
struct nl_sock *sk,
struct nl_cache *cache,
828 change_func_t change_cb,
void *data)
830 struct nl_object *obj, *next;
831 struct nl_af_group *grp;
832 struct nl_cache_assoc ca = {
834 .ca_change = change_cb,
835 .ca_change_data = data,
837 struct nl_parser_param p = {
843 NL_DBG(1,
"Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
848 grp = cache->c_ops->co_groups;
850 if (grp && grp->ag_group &&
855 err = nl_cache_request_full_dump(sk, cache);
859 err = __cache_pickup(sk, cache, &p);
860 if (err == -NLE_DUMP_INTR)
867 }
while (grp && grp->ag_group &&
870 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) {
875 change_cb(cache, obj, NL_ACT_DEL, data);
880 NL_DBG(1,
"Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
895 int nl_cache_parse(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
896 struct nlmsghdr *nlh,
struct nl_parser_param *params)
900 if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize))
901 return -NLE_MSG_TOOSHORT;
903 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
904 if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
905 err = ops->co_msg_parser(ops, who, nlh, params);
906 if (err != -NLE_OPNOTSUPP)
912 err = -NLE_MSGTYPE_NOSUPPORT;
933 struct nl_parser_param p = {
938 return nl_cache_parse(cache->c_ops, NULL,
nlmsg_hdr(msg), &p);
953 struct nl_af_group *grp;
957 grp = cache->c_ops->co_groups;
959 if (grp && grp->ag_group &&
964 err = nl_cache_request_full_dump(sk, cache);
968 NL_DBG(2,
"Updating cache %p <%s> for family %u, request sent, waiting for reply\n",
969 cache, nl_cache_name(cache), grp ? grp->ag_family : AF_UNSPEC);
972 if (err == -NLE_DUMP_INTR) {
973 NL_DBG(2,
"Dump interrupted, restarting!\n");
980 }
while (grp && grp->ag_group &&
992 static struct nl_object *__cache_fast_lookup(
struct nl_cache *cache,
993 struct nl_object *needle)
995 struct nl_object *obj;
1022 struct nl_object *needle)
1024 struct nl_object *obj;
1026 if (cache->hashtable)
1027 return __cache_fast_lookup(cache, needle);
1029 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1057 struct nl_object *filter)
1059 struct nl_object *obj;
1061 if (cache->c_ops == NULL)
1065 && cache->hashtable)
1066 return __cache_fast_lookup(cache, filter);
1068 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1087 struct nl_object *obj;
1089 NL_DBG(2,
"Marking all objects in cache %p <%s>\n",
1090 cache, nl_cache_name(cache));
1092 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
1126 struct nl_object *filter)
1129 struct nl_object_ops *ops;
1130 struct nl_object *obj;
1132 NL_DBG(2,
"Dumping cache %p <%s> with filter %p\n",
1133 cache, nl_cache_name(cache), filter);
1135 if (type > NL_DUMP_MAX || type < 0)
1138 if (cache->c_ops == NULL)
1141 ops = cache->c_ops->co_obj_ops;
1142 if (!ops->oo_dump[type])
1148 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1152 NL_DBG(4,
"Dumping object %p...\n", obj);
1153 dump_from_ops(obj, params);
1174 void (*cb)(
struct nl_object *,
void *),
void *arg)
1191 void (*cb)(
struct nl_object *,
void *),
void *arg)
1193 struct nl_object *obj, *tmp;
1195 if (cache->c_ops == NULL)
1198 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
1202 NL_DBG(3,
"%p<->%p object difference: %x\n",