6 #include <sys/socket.h>
9 #include <linux/rtnetlink.h>
12 #include "poll-loop.h"
17 static const struct nl_policy rtnlgrp_link_policy[] = {
18 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
19 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
23 main(int argc UNUSED, char *argv[])
28 set_program_name(argv[0]);
31 vlog_set_levels(VLM_ANY_MODULE, VLF_ANY_FACILITY, VLL_DBG);
33 error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0, &sock);
35 ovs_fatal(error, "could not create rtnetlink socket");
41 error = nl_sock_recv(sock, &buf, false);
42 if (error == EAGAIN) {
44 } else if (error == ENOBUFS) {
45 ovs_error(0, "network monitor socket overflowed");
47 ovs_fatal(error, "error on network monitor socket");
49 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
51 struct ifinfomsg *iim;
53 nlh = ofpbuf_at(buf, 0, NLMSG_HDRLEN);
54 iim = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *iim);
56 ovs_error(0, "received bad rtnl message (no ifinfomsg)");
61 if (!nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
63 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
64 ovs_error(0, "received bad rtnl message (policy)");
68 printf("netdev %s changed (%s):\n",
69 nl_attr_get_string(attrs[IFLA_IFNAME]),
70 (nlh->nlmsg_type == RTM_NEWLINK ? "RTM_NEWLINK"
71 : nlh->nlmsg_type == RTM_DELLINK ? "RTM_DELLINK"
72 : nlh->nlmsg_type == RTM_GETLINK ? "RTM_GETLINK"
73 : nlh->nlmsg_type == RTM_SETLINK ? "RTM_SETLINK"
75 if (attrs[IFLA_MASTER]) {
76 uint32_t idx = nl_attr_get_u32(attrs[IFLA_MASTER]);
77 char ifname[IFNAMSIZ];
78 if (!if_indextoname(idx, ifname)) {
79 strcpy(ifname, "unknown");
81 printf("\tmaster=%"PRIu32" (%s)\n", idx, ifname);
86 nl_sock_wait(sock, POLLIN);