2 * Copyright (c) 2011 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"
32 VLOG_DEFINE_THIS_MODULE(rtbsd);
33 COVERAGE_DEFINE(rtbsd_changed);
35 /* PF_ROUTE socket. */
36 static int notify_sock = -1;
38 /* All registered notifiers. */
39 static struct list all_notifiers = LIST_INITIALIZER(&all_notifiers);
41 static void rtbsd_report_change(const struct if_msghdr *);
42 static void rtbsd_report_notify_error(void);
44 /* Registers 'cb' to be called with auxiliary data 'aux' with network device
45 * change notifications. The notifier is stored in 'notifier', which the
46 * caller must not modify or free.
48 * Returns 0 if successful, otherwise a positive errno value. */
50 rtbsd_notifier_register(struct rtbsd_notifier *notifier,
51 rtbsd_notify_func *cb, void *aux)
53 if (notify_sock < 0) {
55 notify_sock = socket(PF_ROUTE, SOCK_RAW, 0);
56 if (notify_sock < 0) {
57 VLOG_WARN("could not create PF_ROUTE socket: %s",
61 error = set_nonblocking(notify_sock);
63 VLOG_WARN("error set_nonblocking PF_ROUTE socket: %s",
68 /* Catch up on notification work so that the new notifier won't
69 * receive any stale notifications. XXX*/
73 list_push_back(&all_notifiers, ¬ifier->node);
79 /* Cancels notification on 'notifier', which must have previously been
80 * registered with rtbsd_notifier_register(). */
82 rtbsd_notifier_unregister(struct rtbsd_notifier *notifier)
84 list_remove(¬ifier->node);
85 if (list_is_empty(&all_notifiers)) {
91 /* Calls all of the registered notifiers, passing along any as-yet-unreported
92 * netdev change events. */
94 rtbsd_notifier_run(void)
96 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
98 if (notify_sock < 0) {
105 msg.ifm_type = RTM_IFINFO;
106 msg.ifm_version = RTM_VERSION; //XXX check if necessary
108 /* read from PF_ROUTE socket */
109 retval = read(notify_sock, (char *)&msg, sizeof(msg));
111 /* received packet from PF_ROUTE socket
112 * XXX check for bad packets */
113 if (msg.ifm_type == RTM_IFINFO) {
114 rtbsd_report_change(&msg);
116 } else if (errno == EAGAIN) {
119 if (errno == ENOBUFS) {
120 VLOG_WARN_RL(&rl, "PF_ROUTE receive buffer overflowed");
122 VLOG_WARN_RL(&rl, "error reading PF_ROUTE socket: %s",
125 rtbsd_report_notify_error();
130 /* Causes poll_block() to wake up when network device change notifications are
133 rtbsd_notifier_wait(void)
135 if (notify_sock >= 0) {
136 poll_fd_wait(notify_sock, POLLIN);
141 rtbsd_report_change(const struct if_msghdr *msg)
143 struct rtbsd_notifier *notifier;
144 struct rtbsd_change change;
146 COVERAGE_INC(rtbsd_changed);
148 change.msg_type = msg->ifm_type; //XXX
149 change.if_index = msg->ifm_index;
150 if_indextoname(msg->ifm_index, change.if_name);
151 change.master_ifindex = 0; //XXX
153 LIST_FOR_EACH (notifier, node, &all_notifiers) {
154 notifier->cb(&change, notifier->aux);
158 /* If an error occurs the notifiers' callbacks are called with NULL changes */
160 rtbsd_report_notify_error(void)
162 struct rtbsd_notifier *notifier;
164 LIST_FOR_EACH (notifier, node, &all_notifiers) {
165 notifier->cb(NULL, notifier->aux);