2 * Copyright (c) 2009 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 "netdev-linux.h"
22 #include <sys/socket.h>
23 #include <linux/rtnetlink.h>
30 #define THIS_MODULE VLM_netdev_linux
33 /* rtnetlink socket. */
34 static struct nl_sock *rtnl_sock;
36 /* All registered notifiers. */
37 static struct list all_notifiers = LIST_INITIALIZER(&all_notifiers);
39 static void linux_netdev_report_change(const struct nlmsghdr *,
40 const struct ifinfomsg *,
41 struct nlattr *attrs[]);
42 static void linux_netdev_report_notify_error(int error);
45 linux_netdev_notifier_register(struct linux_netdev_notifier *notifier,
46 linux_netdev_notify_func *cb, void *aux)
49 int error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0,
52 VLOG_WARN("could not create rtnetlink socket: %s",
57 /* Catch up on notification work so that the new notifier won't
58 * receive any stale notifications. */
59 linux_netdev_notifier_run();
62 list_push_back(&all_notifiers, ¬ifier->node);
70 linux_netdev_notifier_unregister(struct linux_netdev_notifier *notifier)
72 list_remove(¬ifier->node);
73 if (list_is_empty(&all_notifiers)) {
74 nl_sock_destroy(rtnl_sock);
80 linux_netdev_notifier_get_error(struct linux_netdev_notifier *notifier)
82 int error = notifier->error;
88 linux_netdev_notifier_peek_error(const struct linux_netdev_notifier *notifier)
90 return notifier->error;
93 static const struct nl_policy rtnlgrp_link_policy[] = {
94 [IFLA_IFNAME] = { .type = NL_A_STRING },
95 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
99 linux_netdev_notifier_run(void)
101 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
108 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
112 error = nl_sock_recv(rtnl_sock, &buf, false);
114 if (nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
116 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
117 struct ifinfomsg *ifinfo;
119 ifinfo = (void *) ((char *) buf->data + NLMSG_HDRLEN);
120 linux_netdev_report_change(buf->data, ifinfo, attrs);
122 VLOG_WARN_RL(&rl, "received bad rtnl message");
123 linux_netdev_report_notify_error(ENOBUFS);
126 } else if (error == EAGAIN) {
129 if (error == ENOBUFS) {
130 VLOG_WARN_RL(&rl, "rtnetlink receive buffer overflowed");
132 VLOG_WARN_RL(&rl, "error reading rtnetlink socket: %s",
135 linux_netdev_report_notify_error(error);
141 linux_netdev_notifier_wait(void)
144 nl_sock_wait(rtnl_sock, POLLIN);
149 linux_netdev_report_change(const struct nlmsghdr *nlmsg,
150 const struct ifinfomsg *ifinfo,
151 struct nlattr *attrs[])
153 struct linux_netdev_notifier *notifier;
154 struct linux_netdev_change change;
156 COVERAGE_INC(linux_netdev_changed);
158 change.nlmsg_type = nlmsg->nlmsg_type;
159 change.ifi_index = ifinfo->ifi_index;
160 change.ifname = nl_attr_get_string(attrs[IFLA_IFNAME]);
161 change.master_ifindex = (attrs[IFLA_MASTER]
162 ? nl_attr_get_u32(attrs[IFLA_MASTER]) : 0);
164 LIST_FOR_EACH (notifier, struct linux_netdev_notifier, node,
166 if (!notifier->error) {
167 notifier->cb(&change, notifier->aux);
173 linux_netdev_report_notify_error(int error)
175 struct linux_netdev_notifier *notifier;
177 LIST_FOR_EACH (notifier, struct linux_netdev_notifier, node,
179 if (error != ENOBUFS || !notifier->error) {
180 notifier->error = error;