29#include "nl-default.h"
31#include <netlink/netlink.h>
32#include <netlink/cache.h>
33#include <netlink/utils.h>
36#include "nl-priv-dynamic-core/nl-core.h"
37#include "nl-priv-dynamic-core/cache-api.h"
38#include "nl-aux-core/nl-core.h"
40#define NL_ALLOCATED_SYNC_SOCK 4
48 struct nl_sock * cm_sock;
49 struct nl_sock * cm_sync_sock;
50 struct nl_cache_assoc * cm_assocs;
54#define NASSOC_EXPAND 8
57static int include_cb(
struct nl_object *obj,
struct nl_parser_param *p)
59 struct nl_cache_assoc *ca = p->pp_arg;
60 struct nl_cache_ops *ops = ca->ca_cache->c_ops;
62 NL_DBG(2,
"Including object %p into cache %p\n", obj, ca->ca_cache);
67 if (ops->co_event_filter)
68 if (ops->co_event_filter(ca->ca_cache, obj) !=
NL_OK)
71 if (ops->co_include_event)
72 return ops->co_include_event(ca->ca_cache, obj, ca->ca_change,
77 return nl_cache_include_v2(ca->ca_cache, obj, ca->ca_change_v2, ca->ca_change_data);
79 return nl_cache_include(ca->ca_cache, obj, ca->ca_change, ca->ca_change_data);
84static int event_input(
struct nl_msg *msg,
void *arg)
86 struct nl_cache_mngr *mngr = arg;
87 int protocol = nlmsg_get_proto(msg);
89 struct nl_cache_ops *ops;
91 struct nl_parser_param p = {
95 NL_DBG(2,
"Cache manager %p, handling new message %p as event\n",
101 if (mngr->cm_protocol != protocol)
104 for (i = 0; i < mngr->cm_nassocs; i++) {
105 if (mngr->cm_assocs[i].ca_cache) {
106 ops = mngr->cm_assocs[i].ca_cache->c_ops;
107 for (n = 0; ops->co_msgtypes[n].mt_id >= 0; n++)
108 if (ops->co_msgtypes[n].mt_id == type)
116 NL_DBG(2,
"Associated message %p to cache %p\n",
117 msg, mngr->cm_assocs[i].ca_cache);
118 p.pp_arg = &mngr->cm_assocs[i];
120 return nl_cache_parse(ops, NULL,
nlmsg_hdr(msg), &p);
152 struct nl_cache_mngr **result)
169 struct nl_cache_mngr **result)
171 _nl_auto_nl_cache_mngr
struct nl_cache_mngr *mngr = NULL;
175 if (flags & NL_ALLOCATED_SOCK)
177 flags = flags & NL_AUTO_PROVIDE;
179 mngr = calloc(1,
sizeof(*mngr));
183 mngr->cm_flags = flags;
188 mngr->cm_flags |= NL_ALLOCATED_SOCK;
195 mngr->cm_flags |= NL_ALLOCATED_SYNC_SOCK;
197 mngr->cm_sync_sock = sync_sk;
199 mngr->cm_nassocs = NASSOC_INIT;
200 mngr->cm_protocol = protocol;
201 mngr->cm_assocs = calloc(mngr->cm_nassocs,
202 sizeof(
struct nl_cache_assoc));
203 if (!mngr->cm_assocs)
209 if ((err =
nl_connect(mngr->cm_sock, protocol)) < 0)
215 if ((err =
nl_connect(mngr->cm_sync_sock, protocol)) < 0)
218 NL_DBG(1,
"Allocated cache manager %p, protocol %d, %d caches\n",
219 mngr, protocol, mngr->cm_nassocs);
221 *result = _nl_steal_pointer(&mngr);
258static int nl_cache_mngr_set_change_func_v2(
struct nl_cache_mngr *mngr,
259 struct nl_cache *cache,
260 change_func_v2_t cb,
void *data)
262 struct nl_cache_ops *ops;
269 if (ops->co_protocol != mngr->cm_protocol)
270 return -NLE_PROTO_MISMATCH;
272 if (ops->co_groups == NULL)
273 return -NLE_OPNOTSUPP;
275 for (i = 0; i < mngr->cm_nassocs; i++)
276 if (mngr->cm_assocs[i].ca_cache == cache)
279 if (i >= mngr->cm_nassocs) {
283 mngr->cm_assocs[i].ca_change_v2 = cb;
284 mngr->cm_assocs[i].ca_change_data = data;
315 change_func_t cb,
void *data)
317 struct nl_cache_ops *ops;
318 struct nl_af_group *grp;
325 if (ops->co_protocol != mngr->cm_protocol)
326 return -NLE_PROTO_MISMATCH;
328 if (ops->co_groups == NULL)
329 return -NLE_OPNOTSUPP;
331 for (i = 0; i < mngr->cm_nassocs; i++)
332 if (mngr->cm_assocs[i].ca_cache &&
333 mngr->cm_assocs[i].ca_cache->c_ops == ops)
336 for (i = 0; i < mngr->cm_nassocs; i++)
337 if (!mngr->cm_assocs[i].ca_cache)
340 if (i >= mngr->cm_nassocs) {
341 struct nl_cache_assoc *cm_assocs;
342 int cm_nassocs = mngr->cm_nassocs + NASSOC_EXPAND;
344 cm_assocs = realloc(mngr->cm_assocs,
345 cm_nassocs *
sizeof(
struct nl_cache_assoc));
346 if (cm_assocs == NULL)
349 memset(cm_assocs + mngr->cm_nassocs, 0,
350 NASSOC_EXPAND *
sizeof(
struct nl_cache_assoc));
351 mngr->cm_assocs = cm_assocs;
352 mngr->cm_nassocs = cm_nassocs;
354 NL_DBG(1,
"Increased capacity of cache manager %p " \
355 "to %d\n", mngr, mngr->cm_nassocs);
358 for (grp = ops->co_groups; grp->ag_group; grp++) {
359 err = nl_socket_add_membership(mngr->cm_sock, grp->ag_group);
366 goto errout_drop_membership;
368 mngr->cm_assocs[i].ca_cache = cache;
369 mngr->cm_assocs[i].ca_change = cb;
370 mngr->cm_assocs[i].ca_change_data = data;
372 if (mngr->cm_flags & NL_AUTO_PROVIDE)
375 NL_DBG(1,
"Added cache %p <%s> to cache manager %p\n",
376 cache, nl_cache_name(cache), mngr);
380errout_drop_membership:
381 for (grp = ops->co_groups; grp->ag_group; grp++)
382 nl_socket_drop_membership(mngr->cm_sock, grp->ag_group);
413 change_func_v2_t cb,
void *data) {
419 return nl_cache_mngr_set_change_func_v2(mngr, cache, cb, data);
454 change_func_t cb,
void *data,
struct nl_cache **result)
456 struct nl_cache_ops *ops;
457 struct nl_cache *cache;
471 goto errout_free_cache;
519 struct pollfd fds = {
524 NL_DBG(3,
"Cache manager %p, poll() fd %d\n", mngr, fds.fd);
525 ret = poll(&fds, 1, timeout);
526 NL_DBG(3,
"Cache manager %p, poll() returned %d\n", mngr, ret);
528 NL_DBG(4,
"nl_cache_mngr_poll(%p): poll() failed with %d (%s)\n",
529 mngr, errno, nl_strerror_l(errno));
530 return -nl_syserr2nlerr(errno);
560 NL_DBG(2,
"Cache manager %p, reading new data from fd %d\n",
570 NL_DBG(2,
"Cache manager %p, recvmsgs read %d messages\n",
576 if (err < 0 && err != -NLE_AGAIN)
596 nl_dump_line(p,
"cache-manager <%p>\n", mngr);
597 nl_dump_line(p,
" .protocol = %s\n",
598 nl_nlfamily2str(mngr->cm_protocol, buf,
sizeof(buf)));
599 nl_dump_line(p,
" .flags = %#x\n", mngr->cm_flags);
600 nl_dump_line(p,
" .nassocs = %u\n", mngr->cm_nassocs);
601 nl_dump_line(p,
" .sock = <%p>\n", mngr->cm_sock);
603 for (i = 0; i < mngr->cm_nassocs; i++) {
604 struct nl_cache_assoc *assoc = &mngr->cm_assocs[i];
606 if (assoc->ca_cache) {
607 nl_dump_line(p,
" .cache[%d] = <%p> {\n", i, assoc->ca_cache);
608 nl_dump_line(p,
" .name = %s\n", assoc->ca_cache->c_ops->co_name);
609 nl_dump_line(p,
" .change_func = <%p>\n", assoc->ca_change);
610 nl_dump_line(p,
" .change_data = <%p>\n", assoc->ca_change_data);
612 nl_dump_line(p,
" .objects = {\n");
618 nl_dump_line(p,
" }\n");
619 nl_dump_line(p,
" }\n");
640 if (mngr->cm_sync_sock)
643 if (mngr->cm_flags & NL_ALLOCATED_SOCK)
646 if (mngr->cm_flags & NL_ALLOCATED_SYNC_SOCK)
649 for (i = 0; i < mngr->cm_nassocs; i++) {
650 if (mngr->cm_assocs[i].ca_cache) {
656 free(mngr->cm_assocs);
658 NL_DBG(1,
"Cache manager %p freed\n", mngr);
int nl_cache_mngr_alloc(struct nl_sock *sk, int protocol, int flags, struct nl_cache_mngr **result)
Allocate new cache manager.
void nl_cache_mngr_free(struct nl_cache_mngr *mngr)
Free cache manager and all caches.
void nl_cache_mngr_info(struct nl_cache_mngr *mngr, struct nl_dump_params *p)
Print information about cache manager.
int nl_cache_mngr_poll(struct nl_cache_mngr *mngr, int timeout)
Check for event notifications.
int nl_cache_mngr_add_cache_v2(struct nl_cache_mngr *mngr, struct nl_cache *cache, change_func_v2_t cb, void *data)
Add cache to cache manager.
int nl_cache_mngr_alloc_ex(struct nl_sock *sk, struct nl_sock *sync_sk, int protocol, int flags, struct nl_cache_mngr **result)
Allocate new cache manager, with custom callback on refill socket.
int nl_cache_mngr_add(struct nl_cache_mngr *mngr, const char *name, change_func_t cb, void *data, struct nl_cache **result)
Add cache to cache manager.
int nl_cache_mngr_get_fd(struct nl_cache_mngr *mngr)
Get socket file descriptor.
int nl_cache_mngr_data_ready(struct nl_cache_mngr *mngr)
Receive available event notifications.
int nl_cache_mngr_add_cache(struct nl_cache_mngr *mngr, struct nl_cache *cache, change_func_t cb, void *data)
Add cache to cache manager.
void nl_cache_mngt_unprovide(struct nl_cache *cache)
Unprovide a cache for global use.
void nl_cache_mngt_provide(struct nl_cache *cache)
Provide a cache for global use.
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.
int nl_cache_nitems(struct nl_cache *cache)
Return the number of items in the cache.
void nl_cache_free(struct nl_cache *cache)
Free a cache.
struct nl_cache * nl_cache_alloc(struct nl_cache_ops *ops)
Allocate new cache.
void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
Dump all elements 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_OK
Proceed with whatever would come next.
@ NL_SKIP
Skip this message.
@ NL_CB_VALID
Message is valid.
@ NL_CB_CUSTOM
Customized handler specified by the user.
void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
Dump message in human readable format to file descriptor.
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
int nl_connect(struct nl_sock *sk, int protocol)
Create file descriptor and bind socket.
int nl_recvmsgs_report(struct nl_sock *sk, struct nl_cb *cb)
Receive a set of messages from a netlink socket and report parsed messages.
void nl_close(struct nl_sock *sk)
Close Netlink socket.
int nl_socket_get_fd(const struct nl_sock *sk)
Return the file descriptor of the backing socket.
struct nl_sock * nl_socket_alloc(void)
Allocate new netlink socket.
int nl_socket_set_nonblocking(const struct nl_sock *sk)
Set file descriptor of socket to non-blocking state.
void nl_socket_disable_seq_check(struct nl_sock *sk)
Disable sequence number checking.
void nl_socket_free(struct nl_sock *sk)
Free a netlink socket.
int nl_debug
Global variable indicating the desired level of debugging output.
int dp_prefix
Specifies the number of whitespaces to be put in front of every new line (indentation).