46#include "nl-default.h"
48#include <netlink/netlink.h>
49#include <netlink/cache.h>
50#include <netlink/object.h>
51#include <netlink/hashtable.h>
52#include <netlink/utils.h>
55#include "nl-priv-dynamic-core/nl-core.h"
56#include "nl-priv-dynamic-core/object-api.h"
57#include "nl-priv-dynamic-core/cache-api.h"
58#include "hashtable-api.h"
59#include "nl-aux-core/nl-core.h"
72 return cache->c_nitems;
82 struct nl_object *obj;
85 if (cache->c_ops == NULL)
88 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
105 return nl_list_empty(&cache->c_items);
123 if (nl_list_empty(&cache->c_items))
126 return nl_list_entry(cache->c_items.next,
127 struct nl_object, ce_list);
136 if (nl_list_empty(&cache->c_items))
139 return nl_list_entry(cache->c_items.prev,
140 struct nl_object, ce_list);
149 if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
152 return nl_list_entry(obj->ce_list.next,
153 struct nl_object, ce_list);
162 if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
165 return nl_list_entry(obj->ce_list.prev,
166 struct nl_object, ce_list);
187 struct nl_cache *cache;
189 cache = calloc(1,
sizeof(*cache));
193 nl_init_list_head(&cache->c_items);
195 cache->c_flags |= ops->co_flags;
203 if (ops->co_obj_ops->oo_keygen) {
207 NL_DBG(2,
"Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
229 struct nl_cache **result)
231 struct nl_cache *cache;
261 struct nl_cache_ops *ops;
262 struct nl_cache *cache;
293 struct nl_object *filter)
295 struct nl_cache *cache;
296 struct nl_object *obj;
305 NL_DBG(2,
"Filling subset of cache %p <%s> with filter %p into %p\n",
306 orig, nl_cache_name(orig), filter, cache);
308 nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
335 struct nl_cache *clone;
336 struct nl_object *obj;
342 NL_DBG(2,
"Cloning %p into %p\n", cache, clone);
344 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
363 struct nl_object *obj, *tmp;
365 NL_DBG(2,
"Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
367 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
371static void __nl_cache_free(
struct nl_cache *cache)
375 if (cache->hashtable)
378 NL_DBG(2,
"Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
390 NL_DBG(3,
"Incremented cache %p <%s> reference count to %d\n",
391 cache, nl_cache_name(cache), cache->c_refcnt);
410 NL_DBG(3,
"Decremented cache %p <%s> reference count, %d remaining\n",
411 cache, nl_cache_name(cache), cache->c_refcnt);
413 if (cache->c_refcnt <= 0)
414 __nl_cache_free(cache);
417void nl_cache_put(
struct nl_cache *cache)
429static int __cache_add(
struct nl_cache *cache,
struct nl_object *obj)
433 obj->ce_cache = cache;
435 if (cache->hashtable) {
438 obj->ce_cache = NULL;
443 nl_list_add_tail(&obj->ce_list, &cache->c_items);
446 NL_DBG(3,
"Added object %p to cache %p <%s>, nitems %d\n",
447 obj, cache, nl_cache_name(cache), cache->c_nitems);
476 struct nl_object *
new;
479 if (cache->c_ops->co_obj_ops != obj->ce_ops)
480 return -NLE_OBJ_MISMATCH;
482 if (!nl_list_empty(&obj->ce_list)) {
483 NL_DBG(3,
"Object %p already in cache, cloning new object\n", obj);
493 ret = __cache_add(cache,
new);
520 if (cache->c_ops->co_obj_ops != obj->ce_ops)
521 return -NLE_OBJ_MISMATCH;
523 NL_DBG(3,
"Moving object %p from cache %p to cache %p\n",
524 obj, obj->ce_cache, cache);
530 if (!nl_list_empty(&obj->ce_list))
533 return __cache_add(cache, obj);
549 struct nl_cache *cache = obj->ce_cache;
554 if (cache->hashtable) {
557 NL_DBG(2,
"Failed to delete %p from cache %p <%s>.\n",
558 obj, cache, nl_cache_name(cache));
561 nl_list_del(&obj->ce_list);
562 obj->ce_cache = NULL;
566 NL_DBG(2,
"Deleted object %p from cache %p <%s>.\n",
567 obj, cache, nl_cache_name(cache));
587 cache->c_iarg1 = arg;
600 cache->c_iarg2 = arg;
610 cache->c_flags |= flags;
639static int nl_cache_request_full_dump(
struct nl_sock *sk,
640 struct nl_cache *cache)
642 if (sk->s_proto != cache->c_ops->co_protocol)
643 return -NLE_PROTO_MISMATCH;
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);
656 struct nl_cache_ops *ops;
657 struct nl_parser_param *params;
660static 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)
679static 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));
708static int pickup_checkdup_cb(
struct nl_object *c,
struct nl_parser_param *p)
710 struct nl_cache *cache = (
struct nl_cache *)p->pp_arg;
711 _nl_auto_nl_object
struct nl_object *old = NULL;
724static int pickup_cb(
struct nl_object *c,
struct nl_parser_param *p)
726 struct nl_cache *cache = p->pp_arg;
731static int __nl_cache_pickup(
struct nl_sock *sk,
struct nl_cache *cache,
734 struct nl_parser_param p;
736 p.pp_cb = checkdup ? pickup_checkdup_cb : pickup_cb;
739 if (sk->s_proto != cache->c_ops->co_protocol)
740 return -NLE_PROTO_MISMATCH;
742 return __cache_pickup(sk, cache, &p);
760 return __nl_cache_pickup(sk, cache, 1);
775 return __nl_cache_pickup(sk, cache, 0);
778static int cache_include(
struct nl_cache *cache,
struct nl_object *obj,
779 struct nl_msgtype *type, change_func_t cb,
780 change_func_v2_t cb_v2,
void *data)
782 _nl_auto_nl_object
struct nl_object *old = NULL;
783 _nl_auto_nl_object
struct nl_object *clone = NULL;
786 switch (type->mt_act) {
791 if (cb_v2 && old->ce_ops->oo_update) {
802 cb_v2(cache, clone, old, diff,
803 NL_ACT_CHANGE, data);
805 cb(cache, old, NL_ACT_CHANGE, data);
809 if (type->mt_act == NL_ACT_DEL) {
811 cb_v2(cache, old, NULL, 0, NL_ACT_DEL,
814 cb(cache, old, NL_ACT_DEL, data);
818 if (type->mt_act == NL_ACT_NEW) {
822 cb_v2(cache, NULL, obj, 0, NL_ACT_NEW,
825 cb(cache, obj, NL_ACT_NEW, data);
831 cb_v2(cache, old, obj, diff, NL_ACT_CHANGE,
833 }
else if (diff && cb)
834 cb(cache, obj, NL_ACT_CHANGE, data);
840 NL_DBG(2,
"Unknown action associated to object %p\n", obj);
847int nl_cache_include(
struct nl_cache *cache,
struct nl_object *obj,
848 change_func_t change_cb,
void *data)
850 struct nl_cache_ops *ops = cache->c_ops;
853 if (ops->co_obj_ops != obj->ce_ops)
854 return -NLE_OBJ_MISMATCH;
856 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
857 if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
858 return cache_include(cache, obj, &ops->co_msgtypes[i],
859 change_cb, NULL, data);
861 NL_DBG(3,
"Object %p does not seem to belong to cache %p <%s>\n",
862 obj, cache, nl_cache_name(cache));
864 return -NLE_MSGTYPE_NOSUPPORT;
867int nl_cache_include_v2(
struct nl_cache *cache,
struct nl_object *obj,
868 change_func_v2_t change_cb,
void *data)
870 struct nl_cache_ops *ops = cache->c_ops;
873 if (ops->co_obj_ops != obj->ce_ops)
874 return -NLE_OBJ_MISMATCH;
876 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
877 if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
878 return cache_include(cache, obj, &ops->co_msgtypes[i],
879 NULL, change_cb, data);
881 NL_DBG(3,
"Object %p does not seem to belong to cache %p <%s>\n",
882 obj, cache, nl_cache_name(cache));
884 return -NLE_MSGTYPE_NOSUPPORT;
887static int resync_cb(
struct nl_object *c,
struct nl_parser_param *p)
889 struct nl_cache_assoc *ca = p->pp_arg;
891 if (ca->ca_change_v2)
892 return nl_cache_include_v2(ca->ca_cache, c, ca->ca_change_v2,
895 return nl_cache_include(ca->ca_cache, c, ca->ca_change,
899static int cache_resync(
struct nl_sock *sk,
struct nl_cache *cache,
900 change_func_t change_cb, change_func_v2_t change_cb_v2,
903 struct nl_object *obj, *next;
904 struct nl_af_group *grp;
905 struct nl_cache_assoc ca = {
907 .ca_change = change_cb,
908 .ca_change_v2 = change_cb_v2,
909 .ca_change_data = data,
911 struct nl_parser_param p = {
917 if (sk->s_proto != cache->c_ops->co_protocol)
918 return -NLE_PROTO_MISMATCH;
920 NL_DBG(1,
"Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
925 grp = cache->c_ops->co_groups;
927 if (grp && grp->ag_group &&
932 err = nl_cache_request_full_dump(sk, cache);
936 err = __cache_pickup(sk, cache, &p);
937 if (err == -NLE_DUMP_INTR)
944 }
while (grp && grp->ag_group &&
947 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) {
952 change_cb(cache, obj, NL_ACT_DEL, data);
953 else if (change_cb_v2)
954 change_cb_v2(cache, obj, NULL, 0, NL_ACT_DEL,
960 NL_DBG(1,
"Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
967int nl_cache_resync(
struct nl_sock *sk,
struct nl_cache *cache,
968 change_func_t change_cb,
void *data)
970 return cache_resync(sk, cache, change_cb, NULL, data);
973int nl_cache_resync_v2(
struct nl_sock *sk,
struct nl_cache *cache,
974 change_func_v2_t change_cb_v2,
void *data)
976 return cache_resync(sk, cache, NULL, change_cb_v2, data);
987int nl_cache_parse(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
988 struct nlmsghdr *nlh,
struct nl_parser_param *params)
992 if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize))
993 return -NLE_MSG_TOOSHORT;
995 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
996 if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
997 err = ops->co_msg_parser(ops, who, nlh, params);
998 if (err != -NLE_OPNOTSUPP)
1004 err = -NLE_MSGTYPE_NOSUPPORT;
1025 struct nl_parser_param p = {
1030 return nl_cache_parse(cache->c_ops, NULL,
nlmsg_hdr(msg), &p);
1045 struct nl_af_group *grp;
1048 if (sk->s_proto != cache->c_ops->co_protocol)
1049 return -NLE_PROTO_MISMATCH;
1052 grp = cache->c_ops->co_groups;
1054 if (grp && grp->ag_group &&
1059 err = nl_cache_request_full_dump(sk, cache);
1063 NL_DBG(2,
"Updating cache %p <%s> for family %u, request sent, waiting for reply\n",
1064 cache, nl_cache_name(cache), grp ? grp->ag_family : AF_UNSPEC);
1067 if (err == -NLE_DUMP_INTR) {
1068 NL_DBG(2,
"Dump interrupted, restarting!\n");
1075 }
while (grp && grp->ag_group &&
1087static struct nl_object *__cache_fast_lookup(
struct nl_cache *cache,
1088 struct nl_object *needle)
1090 struct nl_object *obj;
1117 struct nl_object *needle)
1119 struct nl_object *obj;
1121 if (cache->hashtable)
1122 return __cache_fast_lookup(cache, needle);
1124 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1152 struct nl_object *filter)
1154 struct nl_object *obj;
1156 if (cache->c_ops == NULL)
1160 && cache->hashtable)
1161 return __cache_fast_lookup(cache, filter);
1163 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1182 struct nl_object *obj;
1184 NL_DBG(2,
"Marking all objects in cache %p <%s>\n",
1185 cache, nl_cache_name(cache));
1187 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
1221 struct nl_object *filter)
1224 struct nl_object_ops *ops;
1225 struct nl_object *obj;
1228 NL_DBG(2,
"Dumping cache %p <%s> with filter %p\n",
1229 cache, nl_cache_name(cache), filter);
1238 _nl_assert_not_reached ();
1242 params = ¶ms_copy;
1247 if (type > NL_DUMP_MAX || type < 0)
1250 if (cache->c_ops == NULL)
1253 ops = cache->c_ops->co_obj_ops;
1254 if (!ops->oo_dump[type])
1258 memset(params->dp_buf, 0, params->dp_buflen);
1260 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1264 NL_DBG(4,
"Dumping object %p...\n", obj);
1265 dump_from_ops(obj, params);
1286 void (*cb)(
struct nl_object *,
void *),
void *arg)
1303 void (*cb)(
struct nl_object *,
void *),
void *arg)
1305 struct nl_object *obj, *tmp;
1307 if (cache->c_ops == NULL)
1310 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
1314 NL_DBG(3,
"%p<->%p object difference: %x\n",
struct nl_cache_ops * nl_cache_ops_lookup_safe(const char *name)
Lookup cache operations by name.
void nl_cache_ops_put(struct nl_cache_ops *ops)
Decrement reference counter.
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
void nl_cache_set_flags(struct nl_cache *cache, unsigned int flags)
Set cache flags.
void nl_cache_set_arg1(struct nl_cache *cache, int arg)
Set synchronization arg1 of cache.
struct nl_object * nl_cache_search(struct nl_cache *cache, struct nl_object *needle)
Search object in cache.
void nl_cache_foreach(struct nl_cache *cache, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache.
int nl_cache_nitems(struct nl_cache *cache)
Return the number of items in the cache.
struct nl_object * nl_cache_find(struct nl_cache *cache, struct nl_object *filter)
Find object in cache.
void nl_cache_free(struct nl_cache *cache)
Free a cache.
void nl_cache_get(struct nl_cache *cache)
Increase reference counter of cache.
int nl_cache_nitems_filter(struct nl_cache *cache, struct nl_object *filter)
Return the number of items matching a filter in the cache.
void nl_cache_dump_filter(struct nl_cache *cache, struct nl_dump_params *params, struct nl_object *filter)
Dump all elements of a cache (filtered).
int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
Add object to cache.
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
Pickup a netlink dump response and put it into a cache.
#define NL_CACHE_AF_ITER
Explicitely iterate over all address families when updating the cache.
int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
Parse a netlink message and add it to the cache.
struct nl_object * nl_cache_get_last(struct nl_cache *cache)
Return the last element in the cache.
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate new cache.
struct nl_object * nl_cache_get_next(struct nl_object *obj)
Return the next element in the cache.
void nl_cache_remove(struct nl_object *obj)
Remove object from cache.
void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
Dump all elements of a cache.
int nl_cache_is_empty(struct nl_cache *cache)
Returns true if the cache is empty.
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, struct nl_cache **result)
Allocate new cache and fill it.
struct nl_object * nl_cache_get_first(struct nl_cache *cache)
Return the first element in the cache.
int nl_cache_pickup_checkdup(struct nl_sock *sk, struct nl_cache *cache)
Pickup a netlink dump response and put it into a cache.
struct nl_object * nl_cache_get_prev(struct nl_object *obj)
Return the previous element in the cache.
struct nl_cache * nl_cache_subset(struct nl_cache *orig, struct nl_object *filter)
Allocate new cache containing a subset of an existing cache.
int nl_cache_alloc_name(const char *kind, struct nl_cache **result)
Allocate new cache based on type name.
void nl_cache_clear(struct nl_cache *cache)
Remove all objects of a cache.
struct nl_cache * nl_cache_clone(struct nl_cache *cache)
Allocate new cache and copy the contents of an existing cache.
void nl_cache_set_arg2(struct nl_cache *cache, int arg)
Set synchronization arg2 of cache.
struct nl_cache_ops * nl_cache_get_ops(struct nl_cache *cache)
Return the operations set of the cache.
int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
Move object from one cache to another.
void nl_cache_mark_all(struct nl_cache *cache)
Mark all objects of a cache.
int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Set up a callback.
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
@ NL_SKIP
Skip this message.
@ NL_CB_VALID
Message is valid.
@ NL_CB_CUSTOM
Customized handler specified by the user.
void nl_rhash_table_free(nl_rhash_table_t *ht)
Free resizeable hashtable including all nodes.
int nl_rhash_table_add(nl_rhash_table_t *ht, struct nl_object *obj)
Add object to resizeable hashtable.
int nl_rhash_table_del(nl_rhash_table_t *ht, struct nl_object *obj)
Remove object from resizeable hashtable.
struct nl_object * nl_rhash_table_lookup(nl_rhash_table_t *ht, struct nl_object *obj)
Lookup identical object in resizeable hashtable.
nl_rhash_table_t * nl_rhash_table_alloc(void)
Allocate resizeable hashtable.
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
int nl_object_update(struct nl_object *dst, struct nl_object *src)
Merge a cacheable object.
struct nl_object * nl_object_clone(struct nl_object *obj)
Allocate a new object and copy all data from an existing object.
uint64_t nl_object_diff64(struct nl_object *a, struct nl_object *b)
Compute bitmask representing difference in attribute values.
uint32_t nl_object_get_id_attrs(struct nl_object *obj)
Return object id attribute mask.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
int nl_object_identical(struct nl_object *a, struct nl_object *b)
Check if the identifiers of two objects are identical.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
Match a filter against an object.
int nl_object_is_marked(struct nl_object *obj)
Return true if object is marked.
void nl_object_mark(struct nl_object *obj)
Add mark to object.
int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
enum nl_dump_type dp_type
Specifies the type of dump that is requested.