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>
60 return cache->c_nitems;
71 struct nl_object *obj;
74 if (cache->c_ops == NULL)
77 ops = cache->c_ops->co_obj_ops;
79 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
96 return nl_list_empty(&cache->c_items);
114 if (nl_list_empty(&cache->c_items))
117 return nl_list_entry(cache->c_items.next,
118 struct nl_object, ce_list);
127 if (nl_list_empty(&cache->c_items))
130 return nl_list_entry(cache->c_items.prev,
131 struct nl_object, ce_list);
140 if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
143 return nl_list_entry(obj->ce_list.next,
144 struct nl_object, ce_list);
153 if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
156 return nl_list_entry(obj->ce_list.prev,
157 struct nl_object, ce_list);
175 struct nl_cache *cache;
177 cache = calloc(1,
sizeof(*cache));
183 nl_init_list_head(&cache->c_items);
186 NL_DBG(2,
"Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
202 nl_error(ENOENT,
"Unable to lookup cache \"%s\"", kind);
216 struct nl_object *filter)
218 struct nl_cache *cache;
220 struct nl_object *obj;
229 ops = orig->c_ops->co_obj_ops;
231 nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
249 struct nl_object *obj, *tmp;
251 NL_DBG(1,
"Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
253 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
268 NL_DBG(1,
"Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
279 static int __cache_add(
struct nl_cache *cache,
struct nl_object *obj)
281 obj->ce_cache = cache;
283 nl_list_add_tail(&obj->ce_list, &cache->c_items);
286 NL_DBG(1,
"Added %p to cache %p <%s>.\n",
287 obj, cache, nl_cache_name(cache));
304 struct nl_object *
new;
306 if (cache->c_ops->co_obj_ops != obj->ce_ops)
307 return nl_error(EINVAL,
"Object mismatches cache type");
309 if (!nl_list_empty(&obj->ce_list)) {
312 return nl_errno(ENOMEM);
318 return __cache_add(cache,
new);
333 if (cache->c_ops->co_obj_ops != obj->ce_ops)
334 return nl_error(EINVAL,
"Object mismatches cache type");
336 NL_DBG(3,
"Moving object %p to cache %p\n", obj, cache);
342 if (!nl_list_empty(&obj->ce_list))
345 return __cache_add(cache, obj);
358 struct nl_cache *cache = obj->ce_cache;
363 nl_list_del(&obj->ce_list);
364 obj->ce_cache = NULL;
368 NL_DBG(1,
"Deleted %p from cache %p <%s>.\n",
369 obj, cache, nl_cache_name(cache));
384 struct nl_object *needle)
386 struct nl_object *obj;
388 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
419 NL_DBG(2,
"Requesting dump from kernel for cache %p <%s>...\n",
420 cache, nl_cache_name(cache));
422 if (cache->c_ops->co_request_update == NULL)
423 return nl_error(EOPNOTSUPP,
"Operation not supported");
425 return cache->c_ops->co_request_update(cache, handle);
429 struct update_xdata {
434 static int update_msg_parser(
struct nl_msg *msg,
void *arg)
436 struct update_xdata *x = arg;
438 return nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
442 int __cache_pickup(
struct nl_handle *handle,
struct nl_cache *cache,
447 struct update_xdata x = {
452 NL_DBG(1,
"Picking up answer for cache %p <%s>...\n",
453 cache, nl_cache_name(cache));
457 return nl_get_errno();
463 NL_DBG(2,
"While picking up for %p <%s>, recvmsgs() returned " \
464 "%d: %s", cache, nl_cache_name(cache),
494 return __cache_pickup(handle, cache, &p);
497 static int cache_include(
struct nl_cache *cache,
struct nl_object *obj,
500 struct nl_object *old;
508 if (type->
mt_act == NL_ACT_DEL) {
510 cb(cache, old, NL_ACT_DEL);
515 if (type->
mt_act == NL_ACT_NEW) {
517 if (old == NULL && cb)
518 cb(cache, obj, NL_ACT_NEW);
521 cb(cache, obj, NL_ACT_CHANGE);
528 NL_DBG(2,
"Unknown action associated to object %p\n", obj);
535 int nl_cache_include(
struct nl_cache *cache,
struct nl_object *obj,
536 change_func_t change_cb)
541 if (ops->co_obj_ops != obj->ce_ops)
542 return nl_error(EINVAL,
"Object mismatches cache type");
544 for (i = 0; ops->co_msgtypes[i].
mt_id >= 0; i++)
545 if (ops->co_msgtypes[i].
mt_id == obj->ce_msgtype)
546 return cache_include(cache, obj, &ops->co_msgtypes[i],
549 return nl_errno(EINVAL);
554 struct nl_cache_assoc *ca = p->pp_arg;
556 return nl_cache_include(ca->ca_cache, c, ca->ca_change);
559 int nl_cache_resync(
struct nl_handle *handle,
struct nl_cache *cache,
560 change_func_t change_cb)
562 struct nl_object *obj, *next;
563 struct nl_cache_assoc ca = {
565 .ca_change = change_cb,
573 NL_DBG(1,
"Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
582 err = __cache_pickup(handle, cache, &p);
586 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list)
590 NL_DBG(1, "Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
610 if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize)) {
611 err = nl_error(EINVAL,
"netlink message too short to be "
612 "of kind %s", ops->co_name);
616 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
617 if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
618 err = ops->co_msg_parser(ops, who, nlh, params);
625 err = nl_error(EINVAL,
"Unsupported netlink message type %d",
649 return nl_cache_parse(cache->c_ops, NULL,
nlmsg_hdr(msg), &p);
670 NL_DBG(2,
"Upading cache %p <%s>, request sent, waiting for dump...\n",
671 cache, nl_cache_name(cache));
690 struct nl_object *obj;
692 NL_DBG(2,
"Marking all objects in cache %p <%s>...\n",
693 cache, nl_cache_name(cache));
695 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
729 struct nl_object *filter)
733 struct nl_object *obj;
735 NL_DBG(2,
"Dumping cache %p <%s> filter %p\n",
736 cache, nl_cache_name(cache), filter);
738 if (type > NL_DUMP_MAX || type < 0)
741 if (cache->c_ops == NULL)
744 ops = cache->c_ops->co_obj_ops;
748 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
752 NL_DBG(4,
"Dumping object %p...\n", obj);
753 dump_from_ops(obj, params);
774 void (*cb)(
struct nl_object *,
void *),
void *arg)
791 void (*cb)(
struct nl_object *,
void *),
void *arg)
793 struct nl_object *obj, *tmp;
796 if (cache->c_ops == NULL)
799 ops = cache->c_ops->co_obj_ops;
801 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {