getting notification of new links and addresses
Michael Richardson
mcr at sandelman.ca
Fri May 9 06:56:25 PDT 2014
I am writing a routing advertisement / DHCPv6 daemon.
It has to handle thousands of (ppp) interfaces that come and go.
I scan the list of interfaces (and their addresseses) when I start
up using rtnl_link_alloc_cache()/rtnl_addr_alloc_cache(), and
nl_cache_foreach(). This works fine, and I see all the existing
interfaces, and I see the list of addresses, and I connect them to
the links in my data structure.
I subscribe to some multicast groups:
/* subscribe to link info and address info */
nl_socket_disable_seq_check(netlink_handle);
nl_socket_add_memberships(netlink_handle, RTNLGRP_LINK, 0);
nl_socket_add_memberships(netlink_handle, RTNLGRP_IPV6_IFADDR, 0);
I also mark the caches as being provided with nl_cache_mgmt_provide()
on the link and address cache I've created.
I enable libevent for the libnl3 socket, code which I append below, mostly
for google. And with NLDBG=3 set, I can see messages coming up to
tell me about new interfaces. ("ip monitor all" in another window also shows
things).
When I scan I see nice things like:
DBG<3> nl.c:743 recvmsgs: Attempting to read from 0x9fec448
DBG<3> nl.c:752 recvmsgs: recvmsgs(0x9fec448): Read 3320 bytes
DBG<3> nl.c:756 recvmsgs: recvmsgs(0x9fec448): Processing valid message...
DBG<2> msg.c:282 __nlmsg_alloc: msg 0x9fec490: Allocated new message, maxlen=548
DBG<3> cache.c:452 __cache_add: Added object 0x9ff18a0 to cache
0x9fec018 <route/link>, nitems 1
when a new interface shows up, I see only:
[debug] event_process_active: event: 0x9ff17c0, EV_READ call 0x804efdb
DBG<3> nl.c:743 recvmsgs: Attempting to read from 0x9fec448
DBG<3> nl.c:752 recvmsgs: recvmsgs(0x9fec448): Read 1052 bytes
DBG<3> nl.c:756 recvmsgs: recvmsgs(0x9fec448): Processing valid message...
DBG<2> msg.c:282 __nlmsg_alloc: msg 0x9fec490: Allocated new message, maxlen=1052
DBG<2> msg.c:576 nlmsg_free: msg 0x9fec490: Freed
so the new interface is not going into any cache.
Do I have to write my own callback function? If so, that would surprise me.
I'm about to "use the source", but I wanted to post this, perhaps I'll
manage to reply with an answer.
=========
(some of code. C++ for fun)
void network_interface::process_netlink_input(evutil_socket_t fd, short what,
void *arg)
{
/* process messages from netlink */
fprintf(stderr, "Processing messages due to event\n");
nl_recvmsgs_default(netlink_handle);
}
void network_interface::setup_libevent(struct event_base *base)
{
open_netlink();
nl_socket_set_nonblocking(netlink_handle);
int fd = nl_socket_get_fd(netlink_handle);
netlink_read_event = event_new(base, fd,
/* what:*/ EV_READ|EV_PERSIST,
/* callback_fn: */ process_netlink_input,
NULL);
evutil_make_socket_nonblocking(fd);
/* add the event with no time out */
event_add(netlink_read_event, NULL);
}
More information about the libnl
mailing list