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;
70 struct nl_object *obj;
73 if (cache->c_ops == NULL)
76 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
93 return nl_list_empty(&cache->c_items);
111 if (nl_list_empty(&cache->c_items))
114 return nl_list_entry(cache->c_items.next,
115 struct nl_object, ce_list);
124 if (nl_list_empty(&cache->c_items))
127 return nl_list_entry(cache->c_items.prev,
128 struct nl_object, ce_list);
137 if (nl_list_at_tail(obj, &obj->ce_cache->c_items, ce_list))
140 return nl_list_entry(obj->ce_list.next,
141 struct nl_object, ce_list);
150 if (nl_list_at_head(obj, &obj->ce_cache->c_items, ce_list))
153 return nl_list_entry(obj->ce_list.prev,
154 struct nl_object, ce_list);
172 struct nl_cache *cache;
174 cache = calloc(1,
sizeof(*cache));
180 nl_init_list_head(&cache->c_items);
184 NL_DBG(2,
"Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
200 nl_error(ENOENT,
"Unable to lookup cache \"%s\"", kind);
214 struct nl_object *filter)
216 struct nl_cache *cache;
217 struct nl_object *obj;
226 nl_list_for_each_entry(obj, &orig->c_items, ce_list) {
244 struct nl_object *obj, *tmp;
246 NL_DBG(1,
"Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
248 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
252 static void __nl_cache_free(
struct nl_cache *cache)
256 NL_DBG(1,
"Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
283 NL_DBG(4,
"Returned cache reference %p, %d remaining\n",
284 cache, cache->c_refcnt);
286 if (cache->c_refcnt <= 0)
287 __nl_cache_free(cache);
297 static int __cache_add(
struct nl_cache *cache,
struct nl_object *obj)
299 obj->ce_cache = cache;
301 nl_list_add_tail(&obj->ce_list, &cache->c_items);
304 NL_DBG(1,
"Added %p to cache %p <%s>.\n",
305 obj, cache, nl_cache_name(cache));
322 struct nl_object *
new;
324 if (cache->c_ops->co_obj_ops != obj->ce_ops)
325 return nl_error(EINVAL,
"Object mismatches cache type");
327 if (!nl_list_empty(&obj->ce_list)) {
330 return nl_errno(ENOMEM);
336 return __cache_add(cache,
new);
351 if (cache->c_ops->co_obj_ops != obj->ce_ops)
352 return nl_error(EINVAL,
"Object mismatches cache type");
354 NL_DBG(3,
"Moving object %p to cache %p\n", obj, cache);
360 if (!nl_list_empty(&obj->ce_list))
363 return __cache_add(cache, obj);
376 struct nl_cache *cache = obj->ce_cache;
381 nl_list_del(&obj->ce_list);
382 obj->ce_cache = NULL;
386 NL_DBG(1,
"Deleted %p from cache %p <%s>.\n",
387 obj, cache, nl_cache_name(cache));
402 struct nl_object *needle)
404 struct nl_object *obj;
406 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
437 NL_DBG(2,
"Requesting dump from kernel for cache %p <%s>...\n",
438 cache, nl_cache_name(cache));
440 if (cache->c_ops->co_request_update == NULL)
441 return nl_error(EOPNOTSUPP,
"Operation not supported");
443 return cache->c_ops->co_request_update(cache, handle);
447 struct update_xdata {
452 static int update_msg_parser(
struct nl_msg *msg,
void *arg)
454 struct update_xdata *x = arg;
456 return nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
460 int __cache_pickup(
struct nl_handle *handle,
struct nl_cache *cache,
465 struct update_xdata x = {
470 NL_DBG(1,
"Picking up answer for cache %p <%s>...\n",
471 cache, nl_cache_name(cache));
475 return nl_get_errno();
481 NL_DBG(2,
"While picking up for %p <%s>, recvmsgs() returned " \
482 "%d: %s", cache, nl_cache_name(cache),
512 return __cache_pickup(handle, cache, &p);
515 static int cache_include(
struct nl_cache *cache,
struct nl_object *obj,
518 struct nl_object *old;
526 if (type->
mt_act == NL_ACT_DEL) {
528 cb(cache, old, NL_ACT_DEL);
533 if (type->
mt_act == NL_ACT_NEW) {
535 if (old == NULL && cb)
536 cb(cache, obj, NL_ACT_NEW);
539 cb(cache, obj, NL_ACT_CHANGE);
546 NL_DBG(2,
"Unknown action associated to object %p\n", obj);
553 int nl_cache_include(
struct nl_cache *cache,
struct nl_object *obj,
554 change_func_t change_cb)
559 if (ops->co_obj_ops != obj->ce_ops)
560 return nl_error(EINVAL,
"Object mismatches cache type");
562 for (i = 0; ops->co_msgtypes[i].
mt_id >= 0; i++)
563 if (ops->co_msgtypes[i].
mt_id == obj->ce_msgtype)
564 return cache_include(cache, obj, &ops->co_msgtypes[i],
567 return nl_errno(EINVAL);
572 struct nl_cache_assoc *ca = p->pp_arg;
574 return nl_cache_include(ca->ca_cache, c, ca->ca_change);
577 int nl_cache_resync(
struct nl_handle *handle,
struct nl_cache *cache,
578 change_func_t change_cb)
580 struct nl_object *obj, *next;
581 struct nl_cache_assoc ca = {
583 .ca_change = change_cb,
591 NL_DBG(1,
"Resyncing cache %p <%s>...\n", cache, nl_cache_name(cache));
600 err = __cache_pickup(handle, cache, &p);
604 nl_list_for_each_entry_safe(obj, next, &cache->c_items, ce_list)
608 NL_DBG(1,
"Finished resyncing %p <%s>\n", cache, nl_cache_name(cache));
628 if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize)) {
629 err = nl_error(EINVAL,
"netlink message too short to be " 630 "of kind %s", ops->co_name);
634 for (i = 0; ops->co_msgtypes[i].
mt_id >= 0; i++) {
643 err = nl_error(EINVAL,
"Unsupported netlink message type %d",
667 return nl_cache_parse(cache->c_ops, NULL,
nlmsg_hdr(msg), &p);
688 NL_DBG(2,
"Upading cache %p <%s>, request sent, waiting for dump...\n",
689 cache, nl_cache_name(cache));
708 struct nl_object *obj;
710 NL_DBG(2,
"Marking all objects in cache %p <%s>...\n",
711 cache, nl_cache_name(cache));
713 nl_list_for_each_entry(obj, &cache->c_items, ce_list)
747 struct nl_object *filter)
751 struct nl_object *obj;
753 NL_DBG(2,
"Dumping cache %p <%s> filter %p\n",
754 cache, nl_cache_name(cache), filter);
756 if (type > NL_DUMP_MAX || type < 0)
759 if (cache->c_ops == NULL)
762 ops = cache->c_ops->co_obj_ops;
766 nl_list_for_each_entry(obj, &cache->c_items, ce_list) {
770 NL_DBG(4,
"Dumping object %p...\n", obj);
771 dump_from_ops(obj, params);
792 void (*cb)(
struct nl_object *,
void *),
void *arg)
809 void (*cb)(
struct nl_object *,
void *),
void *arg)
811 struct nl_object *obj, *tmp;
813 if (cache->c_ops == NULL)
816 nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list) {
Message type to cache action association.
int nl_recvmsgs(struct nl_handle *handle, struct nl_cb *cb)
Receive a set of messages from a netlink socket.
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 a new cache containing a subset of a cache.
uint16_t nlmsg_type
Message type (content type)
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.
int mt_act
Cache action to take.
int nl_cache_pickup(struct nl_handle *handle, struct nl_cache *cache)
Pickup a netlink dump response and put it into a 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(* co_msg_parser)(struct nl_cache_ops *, struct sockaddr_nl *, struct nlmsghdr *, struct nl_parser_param *)
Called whenever a message was received that needs to be parsed.
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.
struct nl_cache_ops * nl_cache_ops_lookup(const char *name)
Lookup cache operations by name.
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
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.
void nl_cache_free(struct nl_cache *cache)
Free a cache.
int nl_cache_refill(struct nl_handle *handle, struct nl_cache *cache)
(Re)fill a cache with the contents in the kernel.
struct nl_cache * nl_cache_alloc_name(const char *kind)
Allocate an empty cache based on type name.
int nl_cache_request_full_dump(struct nl_handle *handle, struct nl_cache *cache)
Request a full dump from the kernel to fill a cache.
struct nl_object * nl_cache_search(struct nl_cache *cache, struct nl_object *needle)
Search for an object in a 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)
Removes an object from a cache.
int(* oo_dump[NL_DUMP_MAX+1])(struct nl_object *, struct nl_dump_params *)
Dumping functions.
int nl_cache_is_empty(struct nl_cache *cache)
Returns true if the cache is empty.
void nl_cache_clear(struct nl_cache *cache)
Clear a 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.
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 in a cache.
int nl_cache_move(struct nl_cache *cache, struct nl_object *obj)
Move object from one cache to another.
char * nl_geterror(void)
Return error message for an error code.
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.
int mt_id
Netlink message type.
struct nl_cache_ops * nl_cache_get_ops(struct nl_cache *cache)
Return the operations set of 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.
int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
Add object to a 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.
Dump all attributes but no statistics.
struct nl_object * nl_cache_get_first(struct nl_cache *cache)
Return the first element in the cache.
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate an empty cache.
uint32_t nl_object_diff(struct nl_object *a, struct nl_object *b)
Compute bitmask representing difference in attribute values.