]>
git.proxmox.com Git - ovs.git/blob - lib/rtbsd.c
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.
23 #include <sys/socket.h>
25 #include <net/route.h>
29 #include "socket-util.h"
30 #include "poll-loop.h"
31 #include "openvswitch/vlog.h"
33 VLOG_DEFINE_THIS_MODULE(rtbsd
);
34 COVERAGE_DEFINE(rtbsd_changed
);
36 static struct ovs_mutex rtbsd_mutex
= OVS_MUTEX_INITIALIZER
;
38 /* PF_ROUTE socket. */
39 static int notify_sock
= -1;
41 /* All registered notifiers. */
42 static struct ovs_list all_notifiers
= OVS_LIST_INITIALIZER(&all_notifiers
);
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
);
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.
52 * Returns 0 if successful, otherwise a positive errno value. */
54 rtbsd_notifier_register(struct rtbsd_notifier
*notifier
,
55 rtbsd_notify_func
*cb
, void *aux
)
56 OVS_EXCLUDED(rtbsd_mutex
)
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",
69 error
= set_nonblocking(notify_sock
);
71 VLOG_WARN("error set_nonblocking PF_ROUTE socket: %s",
77 list_push_back(&all_notifiers
, ¬ifier
->node
);
82 ovs_mutex_unlock(&rtbsd_mutex
);
86 /* Cancels notification on 'notifier', which must have previously been
87 * registered with rtbsd_notifier_register(). */
89 rtbsd_notifier_unregister(struct rtbsd_notifier
*notifier
)
90 OVS_EXCLUDED(rtbsd_mutex
)
92 ovs_mutex_lock(&rtbsd_mutex
);
93 list_remove(¬ifier
->node
);
94 if (list_is_empty(&all_notifiers
)) {
98 ovs_mutex_unlock(&rtbsd_mutex
);
101 /* Calls all of the registered notifiers, passing along any as-yet-unreported
102 * netdev change events. */
104 rtbsd_notifier_run(void)
105 OVS_EXCLUDED(rtbsd_mutex
)
107 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
108 struct if_msghdr msg
;
110 ovs_mutex_lock(&rtbsd_mutex
);
111 if (notify_sock
< 0) {
112 ovs_mutex_unlock(&rtbsd_mutex
);
119 msg
.ifm_type
= RTM_IFINFO
;
120 msg
.ifm_version
= RTM_VERSION
; //XXX check if necessary
122 /* read from PF_ROUTE socket */
123 retval
= read(notify_sock
, (char *)&msg
, sizeof(msg
));
125 /* received packet from PF_ROUTE socket
126 * XXX check for bad packets */
127 switch (msg
.ifm_type
) {
129 /* Since RTM_IFANNOUNCE messages are smaller than RTM_IFINFO
130 * messages, the same buffer may be used. */
132 rtbsd_report_change(&msg
);
137 } else if (errno
== EAGAIN
) {
138 ovs_mutex_unlock(&rtbsd_mutex
);
141 if (errno
== ENOBUFS
) {
142 VLOG_WARN_RL(&rl
, "PF_ROUTE receive buffer overflowed");
144 VLOG_WARN_RL(&rl
, "error reading PF_ROUTE socket: %s",
145 ovs_strerror(errno
));
147 rtbsd_report_notify_error();
152 /* Causes poll_block() to wake up when network device change notifications are
155 rtbsd_notifier_wait(void)
156 OVS_EXCLUDED(rtbsd_mutex
)
158 ovs_mutex_lock(&rtbsd_mutex
);
159 if (notify_sock
>= 0) {
160 poll_fd_wait(notify_sock
, POLLIN
);
162 ovs_mutex_unlock(&rtbsd_mutex
);
166 rtbsd_report_change(const struct if_msghdr
*msg
)
167 OVS_REQUIRES(rtbsd_mutex
)
169 struct rtbsd_notifier
*notifier
;
170 struct rtbsd_change change
;
171 const struct if_announcemsghdr
*ahdr
;
173 COVERAGE_INC(rtbsd_changed
);
175 change
.msg_type
= msg
->ifm_type
; //XXX
176 change
.master_ifindex
= 0; //XXX
178 switch (msg
->ifm_type
) {
180 change
.if_index
= msg
->ifm_index
;
181 if_indextoname(msg
->ifm_index
, change
.if_name
);
184 ahdr
= (const struct if_announcemsghdr
*) msg
;
185 change
.if_index
= ahdr
->ifan_index
;
186 strncpy(change
.if_name
, ahdr
->ifan_name
, IF_NAMESIZE
);
190 LIST_FOR_EACH (notifier
, node
, &all_notifiers
) {
191 notifier
->cb(&change
, notifier
->aux
);
195 /* If an error occurs the notifiers' callbacks are called with NULL changes */
197 rtbsd_report_notify_error(void)
198 OVS_REQUIRES(rtbsd_mutex
)
200 struct rtbsd_notifier
*notifier
;
202 LIST_FOR_EACH (notifier
, node
, &all_notifiers
) {
203 notifier
->cb(NULL
, notifier
->aux
);