]> git.proxmox.com Git - mirror_ovs.git/blob - lib/rtnetlink.c
Merge citrix branch into master.
[mirror_ovs.git] / lib / rtnetlink.c
1 /*
2 * Copyright (c) 2009 Nicira Networks.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18
19 #include "rtnetlink.h"
20
21 #include <errno.h>
22 #include <sys/socket.h>
23 #include <linux/rtnetlink.h>
24 #include <net/if.h>
25 #include <poll.h>
26
27 #include "coverage.h"
28 #include "netlink.h"
29 #include "ofpbuf.h"
30
31 #define THIS_MODULE VLM_rtnetlink
32 #include "vlog.h"
33
34 /* rtnetlink socket. */
35 static struct nl_sock *notify_sock;
36
37 /* All registered notifiers. */
38 static struct list all_notifiers = LIST_INITIALIZER(&all_notifiers);
39
40 static void rtnetlink_report_change(const struct nlmsghdr *,
41 const struct ifinfomsg *,
42 struct nlattr *attrs[]);
43 static void rtnetlink_report_notify_error(void);
44
45 /* Registers 'cb' to be called with auxiliary data 'aux' with network device
46 * change notifications. The notifier is stored in 'notifier', which the
47 * caller must not modify or free.
48 *
49 * This is probably not the function that you want. You should probably be
50 * using dpif_port_poll() or netdev_monitor_create(), which unlike this
51 * function are not Linux-specific.
52 *
53 * Returns 0 if successful, otherwise a positive errno value. */
54 int
55 rtnetlink_notifier_register(struct rtnetlink_notifier *notifier,
56 rtnetlink_notify_func *cb, void *aux)
57 {
58 if (!notify_sock) {
59 int error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0,
60 &notify_sock);
61 if (error) {
62 VLOG_WARN("could not create rtnetlink socket: %s",
63 strerror(error));
64 return error;
65 }
66 } else {
67 /* Catch up on notification work so that the new notifier won't
68 * receive any stale notifications. */
69 rtnetlink_notifier_run();
70 }
71
72 list_push_back(&all_notifiers, &notifier->node);
73 notifier->cb = cb;
74 notifier->aux = aux;
75 return 0;
76 }
77
78 /* Cancels notification on 'notifier', which must have previously been
79 * registered with lxnetdev_notifier_register(). */
80 void
81 rtnetlink_notifier_unregister(struct rtnetlink_notifier *notifier)
82 {
83 list_remove(&notifier->node);
84 if (list_is_empty(&all_notifiers)) {
85 nl_sock_destroy(notify_sock);
86 notify_sock = NULL;
87 }
88 }
89
90 /* Calls all of the registered notifiers, passing along any as-yet-unreported
91 * netdev change events. */
92 void
93 rtnetlink_notifier_run(void)
94 {
95 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
96
97 if (!notify_sock) {
98 return;
99 }
100
101 for (;;) {
102 /* Policy for RTNLGRP_LINK messages.
103 *
104 * There are *many* more fields in these messages, but currently we
105 * only care about these fields. */
106 static const struct nl_policy rtnetlink_policy[] = {
107 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
108 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
109 };
110
111 struct nlattr *attrs[ARRAY_SIZE(rtnetlink_policy)];
112 struct ofpbuf *buf;
113 int error;
114
115 error = nl_sock_recv(notify_sock, &buf, false);
116 if (!error) {
117 if (nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
118 rtnetlink_policy,
119 attrs, ARRAY_SIZE(rtnetlink_policy))) {
120 struct ifinfomsg *ifinfo;
121
122 ifinfo = (void *) ((char *) buf->data + NLMSG_HDRLEN);
123 rtnetlink_report_change(buf->data, ifinfo, attrs);
124 } else {
125 VLOG_WARN_RL(&rl, "received bad rtnl message");
126 rtnetlink_report_notify_error();
127 }
128 ofpbuf_delete(buf);
129 } else if (error == EAGAIN) {
130 return;
131 } else {
132 if (error == ENOBUFS) {
133 VLOG_WARN_RL(&rl, "rtnetlink receive buffer overflowed");
134 } else {
135 VLOG_WARN_RL(&rl, "error reading rtnetlink socket: %s",
136 strerror(error));
137 }
138 rtnetlink_report_notify_error();
139 }
140 }
141 }
142
143 /* Causes poll_block() to wake up when network device change notifications are
144 * ready. */
145 void
146 rtnetlink_notifier_wait(void)
147 {
148 if (notify_sock) {
149 nl_sock_wait(notify_sock, POLLIN);
150 }
151 }
152
153 static void
154 rtnetlink_report_change(const struct nlmsghdr *nlmsg,
155 const struct ifinfomsg *ifinfo,
156 struct nlattr *attrs[])
157 {
158 struct rtnetlink_notifier *notifier;
159 struct rtnetlink_change change;
160
161 COVERAGE_INC(rtnetlink_changed);
162
163 change.nlmsg_type = nlmsg->nlmsg_type;
164 change.ifi_index = ifinfo->ifi_index;
165 change.ifname = nl_attr_get_string(attrs[IFLA_IFNAME]);
166 change.master_ifindex = (attrs[IFLA_MASTER]
167 ? nl_attr_get_u32(attrs[IFLA_MASTER]) : 0);
168
169 LIST_FOR_EACH (notifier, struct rtnetlink_notifier, node,
170 &all_notifiers) {
171 notifier->cb(&change, notifier->aux);
172 }
173 }
174
175 static void
176 rtnetlink_report_notify_error(void)
177 {
178 struct rtnetlink_notifier *notifier;
179
180 LIST_FOR_EACH (notifier, struct rtnetlink_notifier, node,
181 &all_notifiers) {
182 notifier->cb(NULL, notifier->aux);
183 }
184 }