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