Merge citrix branch into master.
[sliver-openvswitch.git] / lib / netdev-linux.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 "netdev-linux.h"
20
21 #include <errno.h>
22 #include <sys/socket.h>
23 #include <linux/rtnetlink.h>
24 #include <poll.h>
25
26 #include "coverage.h"
27 #include "netlink.h"
28 #include "ofpbuf.h"
29
30 #define THIS_MODULE VLM_netdev_linux
31 #include "vlog.h"
32
33 /* rtnetlink socket. */
34 static struct nl_sock *rtnl_sock;
35
36 /* All registered notifiers. */
37 static struct list all_notifiers = LIST_INITIALIZER(&all_notifiers);
38
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);
43
44 int
45 linux_netdev_notifier_register(struct linux_netdev_notifier *notifier,
46                                linux_netdev_notify_func *cb, void *aux)
47 {
48     if (!rtnl_sock) {
49         int error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0,
50                                    &rtnl_sock);
51         if (error) {
52             VLOG_WARN("could not create rtnetlink socket: %s",
53                       strerror(error));
54             return error;
55         }
56     } else {
57         /* Catch up on notification work so that the new notifier won't
58          * receive any stale notifications. */
59         linux_netdev_notifier_run();
60     }
61
62     list_push_back(&all_notifiers, &notifier->node);
63     notifier->error = 0;
64     notifier->cb = cb;
65     notifier->aux = aux;
66     return 0;
67 }
68
69 void
70 linux_netdev_notifier_unregister(struct linux_netdev_notifier *notifier)
71 {
72     list_remove(&notifier->node);
73     if (list_is_empty(&all_notifiers)) {
74         nl_sock_destroy(rtnl_sock);
75         rtnl_sock = NULL;
76     }
77 }
78
79 int
80 linux_netdev_notifier_get_error(struct linux_netdev_notifier *notifier)
81 {
82     int error = notifier->error;
83     notifier->error = 0;
84     return error;
85 }
86
87 int
88 linux_netdev_notifier_peek_error(const struct linux_netdev_notifier *notifier)
89 {
90     return notifier->error;
91 }
92
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 },
96 };
97
98 void
99 linux_netdev_notifier_run(void)
100 {
101     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
102
103     if (!rtnl_sock) {
104         return;
105     }
106
107     for (;;) {
108         struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
109         struct ofpbuf *buf;
110         int error;
111
112         error = nl_sock_recv(rtnl_sock, &buf, false);
113         if (!error) {
114             if (nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
115                                 rtnlgrp_link_policy,
116                                 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
117                 struct ifinfomsg *ifinfo;
118
119                 ifinfo = (void *) ((char *) buf->data + NLMSG_HDRLEN);
120                 linux_netdev_report_change(buf->data, ifinfo, attrs);
121             } else {
122                 VLOG_WARN_RL(&rl, "received bad rtnl message");
123                 linux_netdev_report_notify_error(ENOBUFS);
124             }
125             ofpbuf_delete(buf);
126         } else if (error == EAGAIN) {
127             return;
128         } else {
129             if (error == ENOBUFS) {
130                 VLOG_WARN_RL(&rl, "rtnetlink receive buffer overflowed");
131             } else {
132                 VLOG_WARN_RL(&rl, "error reading rtnetlink socket: %s",
133                              strerror(error));
134             }
135             linux_netdev_report_notify_error(error);
136         }
137     }
138 }
139
140 void
141 linux_netdev_notifier_wait(void)
142 {
143     if (rtnl_sock) {
144         nl_sock_wait(rtnl_sock, POLLIN);
145     }
146 }
147
148 static void
149 linux_netdev_report_change(const struct nlmsghdr *nlmsg,
150                            const struct ifinfomsg *ifinfo,
151                            struct nlattr *attrs[])
152 {
153     struct linux_netdev_notifier *notifier;
154     struct linux_netdev_change change;
155
156     COVERAGE_INC(linux_netdev_changed);
157
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);
163
164     LIST_FOR_EACH (notifier, struct linux_netdev_notifier, node,
165                    &all_notifiers) {
166         if (!notifier->error) {
167             notifier->cb(&change, notifier->aux);
168         }
169     }
170 }
171
172 static void
173 linux_netdev_report_notify_error(int error)
174 {
175     struct linux_netdev_notifier *notifier;
176
177     LIST_FOR_EACH (notifier, struct linux_netdev_notifier, node,
178                    &all_notifiers) {
179         if (error != ENOBUFS || !notifier->error) {
180             notifier->error = error;
181         }
182     }
183 }