2 * Copyright (c) 2011, 2013 Gaetano Catalli.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/socket.h>
23 #include <net/route.h>
27 #include "socket-util.h"
28 #include "poll-loop.h"
29 #include "openvswitch/vlog.h"
32 VLOG_DEFINE_THIS_MODULE(rtbsd);
33 COVERAGE_DEFINE(rtbsd_changed);
35 static struct ovs_mutex rtbsd_mutex = OVS_MUTEX_INITIALIZER;
37 /* PF_ROUTE socket. */
38 static int notify_sock = -1;
40 /* All registered notifiers. */
41 static struct ovs_list all_notifiers = OVS_LIST_INITIALIZER(&all_notifiers);
43 static void rtbsd_report_change(const struct if_msghdr *)
44 OVS_REQUIRES(rtbsd_mutex);
45 static void rtbsd_report_notify_error(void) OVS_REQUIRES(rtbsd_mutex);
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.
51 * Returns 0 if successful, otherwise a positive errno value. */
53 rtbsd_notifier_register(struct rtbsd_notifier *notifier,
54 rtbsd_notify_func *cb, void *aux)
55 OVS_EXCLUDED(rtbsd_mutex)
59 ovs_mutex_lock(&rtbsd_mutex);
60 if (notify_sock < 0) {
61 notify_sock = socket(PF_ROUTE, SOCK_RAW, 0);
62 if (notify_sock < 0) {
63 VLOG_WARN("could not create PF_ROUTE socket: %s",
68 error = set_nonblocking(notify_sock);
70 VLOG_WARN("error set_nonblocking PF_ROUTE socket: %s",
76 list_push_back(&all_notifiers, ¬ifier->node);
81 ovs_mutex_unlock(&rtbsd_mutex);
85 /* Cancels notification on 'notifier', which must have previously been
86 * registered with rtbsd_notifier_register(). */
88 rtbsd_notifier_unregister(struct rtbsd_notifier *notifier)
89 OVS_EXCLUDED(rtbsd_mutex)
91 ovs_mutex_lock(&rtbsd_mutex);
92 list_remove(¬ifier->node);
93 if (list_is_empty(&all_notifiers)) {
97 ovs_mutex_unlock(&rtbsd_mutex);
100 /* Calls all of the registered notifiers, passing along any as-yet-unreported
101 * netdev change events. */
103 rtbsd_notifier_run(void)
104 OVS_EXCLUDED(rtbsd_mutex)
106 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
107 struct if_msghdr msg;
109 ovs_mutex_lock(&rtbsd_mutex);
110 if (notify_sock < 0) {
111 ovs_mutex_unlock(&rtbsd_mutex);
118 msg.ifm_type = RTM_IFINFO;
119 msg.ifm_version = RTM_VERSION; //XXX check if necessary
121 /* read from PF_ROUTE socket */
122 retval = read(notify_sock, (char *)&msg, sizeof(msg));
124 /* received packet from PF_ROUTE socket
125 * XXX check for bad packets */
126 if (msg.ifm_type == RTM_IFINFO) {
127 rtbsd_report_change(&msg);
129 } else if (errno == EAGAIN) {
130 ovs_mutex_unlock(&rtbsd_mutex);
133 if (errno == ENOBUFS) {
134 VLOG_WARN_RL(&rl, "PF_ROUTE receive buffer overflowed");
136 VLOG_WARN_RL(&rl, "error reading PF_ROUTE socket: %s",
137 ovs_strerror(errno));
139 rtbsd_report_notify_error();
144 /* Causes poll_block() to wake up when network device change notifications are
147 rtbsd_notifier_wait(void)
148 OVS_EXCLUDED(rtbsd_mutex)
150 ovs_mutex_lock(&rtbsd_mutex);
151 if (notify_sock >= 0) {
152 poll_fd_wait(notify_sock, POLLIN);
154 ovs_mutex_unlock(&rtbsd_mutex);
158 rtbsd_report_change(const struct if_msghdr *msg)
159 OVS_REQUIRES(rtbsd_mutex)
161 struct rtbsd_notifier *notifier;
162 struct rtbsd_change change;
164 COVERAGE_INC(rtbsd_changed);
166 change.msg_type = msg->ifm_type; //XXX
167 change.if_index = msg->ifm_index;
168 if_indextoname(msg->ifm_index, change.if_name);
169 change.master_ifindex = 0; //XXX
171 LIST_FOR_EACH (notifier, node, &all_notifiers) {
172 notifier->cb(&change, notifier->aux);
176 /* If an error occurs the notifiers' callbacks are called with NULL changes */
178 rtbsd_report_notify_error(void)
179 OVS_REQUIRES(rtbsd_mutex)
181 struct rtbsd_notifier *notifier;
183 LIST_FOR_EACH (notifier, node, &all_notifiers) {
184 notifier->cb(NULL, notifier->aux);