]> git.proxmox.com Git - ovs.git/blob - lib/rtbsd.c
rtbsd: support RTM_IFANNOUNCE messages
[ovs.git] / lib / rtbsd.c
1 /*
2 * Copyright (c) 2011, 2013 Gaetano Catalli.
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 "rtbsd.h"
20
21 #include <unistd.h>
22 #include <errno.h>
23 #include <sys/socket.h>
24 #include <net/if.h>
25 #include <net/route.h>
26 #include <poll.h>
27
28 #include "coverage.h"
29 #include "socket-util.h"
30 #include "poll-loop.h"
31 #include "openvswitch/vlog.h"
32
33 VLOG_DEFINE_THIS_MODULE(rtbsd);
34 COVERAGE_DEFINE(rtbsd_changed);
35
36 static struct ovs_mutex rtbsd_mutex = OVS_MUTEX_INITIALIZER;
37
38 /* PF_ROUTE socket. */
39 static int notify_sock = -1;
40
41 /* All registered notifiers. */
42 static struct ovs_list all_notifiers = OVS_LIST_INITIALIZER(&all_notifiers);
43
44 static void rtbsd_report_change(const struct if_msghdr *)
45 OVS_REQUIRES(rtbsd_mutex);
46 static void rtbsd_report_notify_error(void) OVS_REQUIRES(rtbsd_mutex);
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 * Returns 0 if successful, otherwise a positive errno value. */
53 int
54 rtbsd_notifier_register(struct rtbsd_notifier *notifier,
55 rtbsd_notify_func *cb, void *aux)
56 OVS_EXCLUDED(rtbsd_mutex)
57 {
58 int error = 0;
59
60 ovs_mutex_lock(&rtbsd_mutex);
61 if (notify_sock < 0) {
62 notify_sock = socket(PF_ROUTE, SOCK_RAW, 0);
63 if (notify_sock < 0) {
64 VLOG_WARN("could not create PF_ROUTE socket: %s",
65 ovs_strerror(errno));
66 error = errno;
67 goto out;
68 }
69 error = set_nonblocking(notify_sock);
70 if (error) {
71 VLOG_WARN("error set_nonblocking PF_ROUTE socket: %s",
72 ovs_strerror(error));
73 goto out;
74 }
75 }
76
77 list_push_back(&all_notifiers, &notifier->node);
78 notifier->cb = cb;
79 notifier->aux = aux;
80
81 out:
82 ovs_mutex_unlock(&rtbsd_mutex);
83 return error;
84 }
85
86 /* Cancels notification on 'notifier', which must have previously been
87 * registered with rtbsd_notifier_register(). */
88 void
89 rtbsd_notifier_unregister(struct rtbsd_notifier *notifier)
90 OVS_EXCLUDED(rtbsd_mutex)
91 {
92 ovs_mutex_lock(&rtbsd_mutex);
93 list_remove(&notifier->node);
94 if (list_is_empty(&all_notifiers)) {
95 close(notify_sock);
96 notify_sock = -1;
97 }
98 ovs_mutex_unlock(&rtbsd_mutex);
99 }
100
101 /* Calls all of the registered notifiers, passing along any as-yet-unreported
102 * netdev change events. */
103 void
104 rtbsd_notifier_run(void)
105 OVS_EXCLUDED(rtbsd_mutex)
106 {
107 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
108 struct if_msghdr msg;
109
110 ovs_mutex_lock(&rtbsd_mutex);
111 if (notify_sock < 0) {
112 ovs_mutex_unlock(&rtbsd_mutex);
113 return;
114 }
115
116 for (;;) {
117 int retval;
118
119 msg.ifm_type = RTM_IFINFO;
120 msg.ifm_version = RTM_VERSION; //XXX check if necessary
121
122 /* read from PF_ROUTE socket */
123 retval = read(notify_sock, (char *)&msg, sizeof(msg));
124 if (retval >= 0) {
125 /* received packet from PF_ROUTE socket
126 * XXX check for bad packets */
127 switch (msg.ifm_type) {
128 case RTM_IFINFO:
129 /* Since RTM_IFANNOUNCE messages are smaller than RTM_IFINFO
130 * messages, the same buffer may be used. */
131 case RTM_IFANNOUNCE:
132 rtbsd_report_change(&msg);
133 break;
134 default:
135 break;
136 }
137 } else if (errno == EAGAIN) {
138 ovs_mutex_unlock(&rtbsd_mutex);
139 return;
140 } else {
141 if (errno == ENOBUFS) {
142 VLOG_WARN_RL(&rl, "PF_ROUTE receive buffer overflowed");
143 } else {
144 VLOG_WARN_RL(&rl, "error reading PF_ROUTE socket: %s",
145 ovs_strerror(errno));
146 }
147 rtbsd_report_notify_error();
148 }
149 }
150 }
151
152 /* Causes poll_block() to wake up when network device change notifications are
153 * ready. */
154 void
155 rtbsd_notifier_wait(void)
156 OVS_EXCLUDED(rtbsd_mutex)
157 {
158 ovs_mutex_lock(&rtbsd_mutex);
159 if (notify_sock >= 0) {
160 poll_fd_wait(notify_sock, POLLIN);
161 }
162 ovs_mutex_unlock(&rtbsd_mutex);
163 }
164
165 static void
166 rtbsd_report_change(const struct if_msghdr *msg)
167 OVS_REQUIRES(rtbsd_mutex)
168 {
169 struct rtbsd_notifier *notifier;
170 struct rtbsd_change change;
171 const struct if_announcemsghdr *ahdr;
172
173 COVERAGE_INC(rtbsd_changed);
174
175 change.msg_type = msg->ifm_type; //XXX
176 change.master_ifindex = 0; //XXX
177
178 switch (msg->ifm_type) {
179 case RTM_IFINFO:
180 change.if_index = msg->ifm_index;
181 if_indextoname(msg->ifm_index, change.if_name);
182 break;
183 case RTM_IFANNOUNCE:
184 ahdr = (const struct if_announcemsghdr *) msg;
185 change.if_index = ahdr->ifan_index;
186 strncpy(change.if_name, ahdr->ifan_name, IF_NAMESIZE);
187 break;
188 }
189
190 LIST_FOR_EACH (notifier, node, &all_notifiers) {
191 notifier->cb(&change, notifier->aux);
192 }
193 }
194
195 /* If an error occurs the notifiers' callbacks are called with NULL changes */
196 static void
197 rtbsd_report_notify_error(void)
198 OVS_REQUIRES(rtbsd_mutex)
199 {
200 struct rtbsd_notifier *notifier;
201
202 LIST_FOR_EACH (notifier, node, &all_notifiers) {
203 notifier->cb(NULL, notifier->aux);
204 }
205 }