2 * Copyright (c) 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "rtnetlink-link.h"
21 #include <sys/socket.h>
22 #include <linux/rtnetlink.h>
26 #include "netlink-notifier.h"
29 static struct nln *nln = NULL;
30 static struct rtnetlink_link_change rtn_change;
32 /* Parses a rtnetlink message 'buf' into 'change'. If 'buf' is unparseable,
33 * leaves 'change' untouched and returns false. Otherwise, populates 'change'
34 * and returns true. */
36 rtnetlink_link_parse(struct ofpbuf *buf,
37 struct rtnetlink_link_change *change)
41 /* Policy for RTNLGRP_LINK messages.
43 * There are *many* more fields in these messages, but currently we
44 * only care about these fields. */
45 static const struct nl_policy policy[] = {
46 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
47 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
50 static struct nlattr *attrs[ARRAY_SIZE(policy)];
52 parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
53 policy, attrs, ARRAY_SIZE(policy));
56 const struct nlmsghdr *nlmsg;
57 const struct ifinfomsg *ifinfo;
60 ifinfo = ((const struct ifinfomsg *)
61 ((const char *) buf->data + NLMSG_HDRLEN));
63 change->nlmsg_type = nlmsg->nlmsg_type;
64 change->ifi_index = ifinfo->ifi_index;
65 change->ifname = nl_attr_get_string(attrs[IFLA_IFNAME]);
66 change->master_ifindex = (attrs[IFLA_MASTER]
67 ? nl_attr_get_u32(attrs[IFLA_MASTER])
75 rtnetlink_link_parse_cb(struct ofpbuf *buf, void *change)
77 return rtnetlink_link_parse(buf, change);
80 /* Registers 'cb' to be called with auxiliary data 'aux' with network device
81 * change notifications. The notifier is stored in 'notifier', which the
82 * caller must not modify or free.
84 * This is probably not the function that you want. You should probably be
85 * using dpif_port_poll() or netdev_change_seq(), which unlike this function
86 * are not Linux-specific.
88 * Returns 0 if successful, otherwise a positive errno value. */
90 rtnetlink_link_notifier_register(struct nln_notifier *notifier,
91 rtnetlink_link_notify_func *cb, void *aux)
94 nln = nln_create(NETLINK_ROUTE, RTNLGRP_LINK, rtnetlink_link_parse_cb,
98 return nln_notifier_register(nln, notifier, (nln_notify_func *) cb, aux);
101 /* Cancels notification on 'notifier', which must have previously been
102 * registered with rtnetlink_link_notifier_register(). */
104 rtnetlink_link_notifier_unregister(struct nln_notifier *notifier)
106 nln_notifier_unregister(nln, notifier);
109 /* Calls all of the registered notifiers, passing along any as-yet-unreported
110 * netdev change events. */
112 rtnetlink_link_notifier_run(void)
115 nln_notifier_run(nln);
119 /* Causes poll_block() to wake up when network device change notifications are
122 rtnetlink_link_notifier_wait(void)
125 nln_notifier_wait(nln);