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;
644 static int nl_cache_request_full_dump(
struct nl_sock *sk,
645 struct nl_cache *cache)
647 if (sk->s_proto != cache->c_ops->co_protocol)
648 return -NLE_PROTO_MISMATCH;
650 if (cache->c_ops->co_request_update == NULL)
651 return -NLE_OPNOTSUPP;
653 NL_DBG(2,
"Requesting update from kernel for cache %p <%s>\n",
654 cache, nl_cache_name(cache));
656 return cache->c_ops->co_request_update(cache, sk);
660 struct update_xdata {
661 struct nl_cache_ops *ops;
662 struct nl_parser_param *params;
665 static int update_msg_parser(
struct nl_msg *msg,
void *arg)
667 struct update_xdata *x = arg;
670 ret = nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
671 if (ret == -NLE_EXIST)
684 static int __cache_pickup(
struct nl_sock *sk,
struct nl_cache *cache,
685 struct nl_parser_param *param)
689 struct update_xdata x = {
694 NL_DBG(2,
"Picking up answer for cache %p <%s>\n",
695 cache, nl_cache_name(cache));
705 NL_DBG(2,
"While picking up for %p <%s>, recvmsgs() returned %d: %s\n",
706 cache, nl_cache_name(cache), err, nl_geterror(err));
713 static int pickup_checkdup_cb(
struct nl_object *c,
struct nl_parser_param *p)
715 struct nl_cache *cache = (
struct nl_cache *)p->pp_arg;
716 struct nl_object *old;
732 static int pickup_cb(
struct nl_object *c,
struct nl_parser_param *p)
734 struct nl_cache *cache = p->pp_arg;
739 static int __nl_cache_pickup(
struct nl_sock *sk,
struct nl_cache *cache,
742 struct nl_parser_param p;
744 p.pp_cb = checkdup ? pickup_checkdup_cb : pickup_cb;
747 if (sk->s_proto != cache->c_ops->co_protocol)
748 return -NLE_PROTO_MISMATCH;
750 return __cache_pickup(sk, cache, &p);
765 return __nl_cache_pickup(sk, cache, 1);
783 return __nl_cache_pickup(sk, cache, 0);
786 static int cache_include(
struct nl_cache *cache,
struct nl_object *obj,
787 struct nl_msgtype *type, change_func_t cb,
void *data)
789 struct nl_object *old;
791 switch (type->mt_act) {
803 cb(cache, old, NL_ACT_CHANGE, data);
809 if (type->mt_act == NL_ACT_DEL) {
811 cb(cache, old, NL_ACT_DEL, data);
816 if (type->mt_act == NL_ACT_NEW) {
818 if (old == NULL && cb)
819 cb(cache, obj, NL_ACT_NEW, data);
822 cb(cache, obj, NL_ACT_CHANGE, data);
829 NL_DBG(2,
"Unknown action associated to object %p\n", obj);
836 int nl_cache_include(
struct nl_cache *cache,
struct nl_object *obj,
837 change_func_t change_cb,
void *data)
839 struct nl_cache_ops *ops = cache->c_ops;
842 if (ops->co_obj_ops != obj->ce_ops)
843 return -NLE_OBJ_MISMATCH;
845 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
846 if (ops->co_msgtypes[i].mt_id == obj->ce_msgtype)
847 return cache_include(cache, obj, &ops->co_msgtypes[i],
850 NL_DBG(3,
"Object %p does not seem to belong to cache %p <%s>\n",
851 obj, cache, nl_cache_name(cache));
853 return -NLE_MSGTYPE_NOSUPPORT;
856 static int resync_cb(
struct nl_object *c,
struct nl_parser_param *p)
858 struct nl_cache_assoc *ca = p->pp_arg;
860 return nl_cache_include(ca->ca_cache, c, ca->ca_change, ca->ca_change_data);
863 int nl_cache_resync(
struct nl_sock *sk,
struct nl_cache *cache,
864 change_func_t change_cb,
void *data)
866 struct nl_object *obj, *next;
867 struct nl_af_group *grp;
868 struct nl_cache_assoc ca = {
870 .ca_change = change_cb,
871 .ca_change_data = data,
873 struct nl_parser_param p = {
879 if (sk->s_proto != cache->c_ops->co_protocol)
880 return -NLE_PROTO_MISMATCH;
882 NL_DBG(1,
"Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
887 grp = cache->c_ops->co_groups;
889 if (grp && grp->ag_group &&
894 err = nl_cache_request_full_dump(sk, cache);
898 err = __cache_pickup(sk, cache, &p);
899 if (err == -NLE_DUMP_INTR)
906 }
while (grp && grp->ag_group &&
909 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list) {
914 change_cb(cache, obj, NL_ACT_DEL, data);
919 NL_DBG(1,
"Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
934 int nl_cache_parse(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
935 struct nlmsghdr *nlh,
struct nl_parser_param *params)
939 if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize))
940 return -NLE_MSG_TOOSHORT;
942 for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
943 if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
944 err = ops->co_msg_parser(ops, who, nlh, params);
945 if (err != -NLE_OPNOTSUPP)
951 err = -NLE_MSGTYPE_NOSUPPORT;
972 struct nl_parser_param p = {
977 return nl_cache_parse(cache->c_ops, NULL,
nlmsg_hdr(msg), &p);
992 struct nl_af_group *grp;
995 if (sk->s_proto != cache->c_ops->co_protocol)
996 return -NLE_PROTO_MISMATCH;
999 grp = cache->c_ops->co_groups;
1001 if (grp && grp->ag_group &&
1006 err = nl_cache_request_full_dump(sk, cache);
1010 NL_DBG(2,
"Updating cache %p <%s> for family %u, request sent, waiting for reply\n",
1011 cache, nl_cache_name(cache), grp ? grp->ag_family : AF_UNSPEC);
1014 if (err == -NLE_DUMP_INTR) {
1015 NL_DBG(2,
"Dump interrupted, restarting!\n");
1022 }
while (grp && grp->ag_group &&
1034 static struct nl_object *__cache_fast_lookup(
struct nl_cache *cache,
1035 struct nl_object *needle)
1037 struct nl_object *obj;
1064 struct nl_object *needle)
1066 struct nl_object *obj;
1068 if (cache->hashtable)
1069 return __cache_fast_lookup(cache, needle);
1071 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1099 struct nl_object *filter)
1101 struct nl_object *obj;
1103 if (cache->c_ops == NULL)
1107 && cache->hashtable)
1108 return __cache_fast_lookup(cache, filter);
1110 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1129 struct nl_object *obj;
1131 NL_DBG(2,
"Marking all objects in cache %p <%s>\n",
1132 cache, nl_cache_name(cache));
1134 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
1168 struct nl_object *filter)
1171 struct nl_object_ops *ops;
1172 struct nl_object *obj;
1174 NL_DBG(2,
"Dumping cache %p <%s> with filter %p\n",
1175 cache, nl_cache_name(cache), filter);
1177 if (type > NL_DUMP_MAX || type < 0)
1180 if (cache->c_ops == NULL)
1183 ops = cache->c_ops->co_obj_ops;
1184 if (!ops->oo_dump[type])
1187 if (params && params->
dp_buf)
1190 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
1194 NL_DBG(4,
"Dumping object %p...\n", obj);
1195 dump_from_ops(obj, params);
1216 void (*cb)(
struct nl_object *,
void *),
void *arg)
1233 void (*cb)(
struct nl_object *,
void *),
void *arg)
1235 struct nl_object *obj, *tmp;
1237 if (cache->c_ops == NULL)
1240 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
1244 NL_DBG(3,
"%p<->%p object difference: %x\n",
char * dp_buf
Alternatively the output may be redirected into a buffer.
void nl_cache_ops_put(struct nl_cache_ops *ops)
Decrement reference counter.
struct nl_object * nl_cache_get_prev(struct nl_object *obj)
Return the previous element in the cache.
int nl_cache_alloc_name(const char *kind, struct nl_cache **result)
Allocate new cache based on type name.
struct nl_cache * nl_cache_subset(struct nl_cache *orig, struct nl_object *filter)
Allocate new cache containing a subset of an existing cache.
Customized handler specified by the user.
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
void nl_cache_get(struct nl_cache *cache)
Increase reference counter of cache.
void nl_hash_table_free(nl_hash_table_t *ht)
Free hashtable including all nodes.
struct nl_object * nl_cache_find(struct nl_cache *cache, struct nl_object *filter)
Find object in 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).
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).
struct nl_object * nl_cache_get_last(struct nl_cache *cache)
Return the last element in the cache.
nl_hash_table_t * nl_hash_table_alloc(int size)
Allocate hashtable.
Dump all attributes but no statistics.
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
uint32_t nl_object_get_id_attrs(struct nl_object *obj)
Return object id attribute mask.
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.
int nl_cache_pickup_checkdup(struct nl_sock *sk, struct nl_cache *cache)
Pickup a netlink dump response and put it into a cache.
int nl_recvmsgs(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket.
struct nl_object * nl_hash_table_lookup(nl_hash_table_t *ht, struct nl_object *obj)
Lookup identical object in hashtable.
void nl_cache_free(struct nl_cache *cache)
Free a cache.
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.
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
void nl_cache_remove(struct nl_object *obj)
Remove object from cache.
int nl_hash_table_add(nl_hash_table_t *ht, struct nl_object *obj)
Add object to hashtable.
int nl_cache_is_empty(struct nl_cache *cache)
Returns true if the cache is empty.
void nl_cache_clear(struct nl_cache *cache)
Remove all objects of a cache.
struct nl_cache_ops * nl_cache_ops_lookup_safe(const char *name)
Lookup cache operations by name.
void nl_cache_set_arg2(struct nl_cache *cache, int arg)
Set synchronization arg2 of cache.
void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
Dump all elements of a 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.
int nl_cache_nitems(struct nl_cache *cache)
Return the number of items in the cache.
struct nl_object * nl_cache_get_next(struct nl_object *obj)
Return the next element in the cache.
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
void nl_cache_mark_all(struct nl_cache *cache)
Mark all objects of a cache.
int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
Move object from one cache to another.
int nl_object_identical(struct nl_object *a, struct nl_object *b)
Check if the identifiers of two objects are identical.
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_set_flags(struct nl_cache *cache, unsigned int flags)
Set cache flags.
int nl_hash_table_del(nl_hash_table_t *ht, struct nl_object *obj)
Remove object from hashtable.
struct nl_cache_ops * nl_cache_get_ops(struct nl_cache *cache)
Return the operations set of the cache.
size_t dp_buflen
Length of the buffer dp_buf.
#define NL_CACHE_AF_ITER
Explicitely iterate over all address families when updating the cache.
int nl_object_match_filter(struct nl_object *obj, struct nl_object *filter)
Match a filter against an object.
void nl_object_mark(struct nl_object *obj)
Add mark to object.
struct nl_cache * nl_cache_clone(struct nl_cache *cache)
Allocate new cache and copy the contents of an existing cache.
int nl_object_update(struct nl_object *dst, struct nl_object *src)
Merge a cacheable object.
int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
Add object to cache.
struct nl_object * nl_object_clone(struct nl_object *obj)
Allocate a new object and copy all data from an existing object.
int nl_object_is_marked(struct nl_object *obj)
Return true if object is marked.
struct nl_object * nl_cache_get_first(struct nl_cache *cache)
Return the first element in the cache.
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_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate new cache.
uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
Compute bitmask representing difference in attribute values.
int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
Pickup a netlink dump response and put it into a cache.