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