]> git.proxmox.com Git - mirror_ovs.git/blame - lib/rtbsd.c
ovsdb-idl: Remove prototype for function that is not defined or used.
[mirror_ovs.git] / lib / rtbsd.c
CommitLineData
f6eb6b20 1/*
10a89ef0 2 * Copyright (c) 2011, 2013 Gaetano Catalli.
f6eb6b20
GL
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
75f697bd
TLSC
19#include "rtbsd.h"
20
f6eb6b20
GL
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"
fd016ae3 30#include "openvswitch/poll-loop.h"
e6211adc 31#include "openvswitch/vlog.h"
f6eb6b20
GL
32
33VLOG_DEFINE_THIS_MODULE(rtbsd);
34COVERAGE_DEFINE(rtbsd_changed);
35
a1f63b1e
RW
36static struct ovs_mutex rtbsd_mutex = OVS_MUTEX_INITIALIZER;
37
f6eb6b20
GL
38/* PF_ROUTE socket. */
39static int notify_sock = -1;
40
41/* All registered notifiers. */
55951e15 42static struct ovs_list all_notifiers = OVS_LIST_INITIALIZER(&all_notifiers);
f6eb6b20 43
a1f63b1e
RW
44static void rtbsd_report_change(const struct if_msghdr *)
45 OVS_REQUIRES(rtbsd_mutex);
46static void rtbsd_report_notify_error(void) OVS_REQUIRES(rtbsd_mutex);
f6eb6b20
GL
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. */
53int
54rtbsd_notifier_register(struct rtbsd_notifier *notifier,
55 rtbsd_notify_func *cb, void *aux)
a1f63b1e 56 OVS_EXCLUDED(rtbsd_mutex)
f6eb6b20 57{
a1f63b1e
RW
58 int error = 0;
59
60 ovs_mutex_lock(&rtbsd_mutex);
f6eb6b20 61 if (notify_sock < 0) {
f6eb6b20
GL
62 notify_sock = socket(PF_ROUTE, SOCK_RAW, 0);
63 if (notify_sock < 0) {
64 VLOG_WARN("could not create PF_ROUTE socket: %s",
10a89ef0 65 ovs_strerror(errno));
a1f63b1e
RW
66 error = errno;
67 goto out;
f6eb6b20
GL
68 }
69 error = set_nonblocking(notify_sock);
70 if (error) {
71 VLOG_WARN("error set_nonblocking PF_ROUTE socket: %s",
10a89ef0 72 ovs_strerror(error));
a1f63b1e 73 goto out;
f6eb6b20 74 }
f6eb6b20
GL
75 }
76
417e7e66 77 ovs_list_push_back(&all_notifiers, &notifier->node);
f6eb6b20
GL
78 notifier->cb = cb;
79 notifier->aux = aux;
a1f63b1e
RW
80
81out:
82 ovs_mutex_unlock(&rtbsd_mutex);
83 return error;
f6eb6b20
GL
84}
85
86/* Cancels notification on 'notifier', which must have previously been
87 * registered with rtbsd_notifier_register(). */
88void
89rtbsd_notifier_unregister(struct rtbsd_notifier *notifier)
a1f63b1e 90 OVS_EXCLUDED(rtbsd_mutex)
f6eb6b20 91{
a1f63b1e 92 ovs_mutex_lock(&rtbsd_mutex);
417e7e66
BW
93 ovs_list_remove(&notifier->node);
94 if (ovs_list_is_empty(&all_notifiers)) {
f6eb6b20
GL
95 close(notify_sock);
96 notify_sock = -1;
97 }
a1f63b1e 98 ovs_mutex_unlock(&rtbsd_mutex);
f6eb6b20
GL
99}
100
101/* Calls all of the registered notifiers, passing along any as-yet-unreported
102 * netdev change events. */
103void
104rtbsd_notifier_run(void)
a1f63b1e 105 OVS_EXCLUDED(rtbsd_mutex)
f6eb6b20
GL
106{
107 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
108 struct if_msghdr msg;
a1f63b1e
RW
109
110 ovs_mutex_lock(&rtbsd_mutex);
f6eb6b20 111 if (notify_sock < 0) {
a1f63b1e 112 ovs_mutex_unlock(&rtbsd_mutex);
f6eb6b20
GL
113 return;
114 }
115
116 for (;;) {
117 int retval;
118
119 msg.ifm_type = RTM_IFINFO;
b5343307 120 msg.ifm_version = RTM_VERSION; /* XXX Check if necessary. */
f6eb6b20
GL
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 */
f5fb24db
TLSC
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. */
97d0619c 131#ifndef __MACH__ /* OS X does not implement RTM_IFANNOUNCE */
f5fb24db 132 case RTM_IFANNOUNCE:
97d0619c 133#endif
f6eb6b20 134 rtbsd_report_change(&msg);
f5fb24db
TLSC
135 break;
136 default:
137 break;
f6eb6b20
GL
138 }
139 } else if (errno == EAGAIN) {
a1f63b1e 140 ovs_mutex_unlock(&rtbsd_mutex);
f6eb6b20
GL
141 return;
142 } else {
143 if (errno == ENOBUFS) {
144 VLOG_WARN_RL(&rl, "PF_ROUTE receive buffer overflowed");
145 } else {
146 VLOG_WARN_RL(&rl, "error reading PF_ROUTE socket: %s",
10a89ef0 147 ovs_strerror(errno));
f6eb6b20
GL
148 }
149 rtbsd_report_notify_error();
150 }
151 }
152}
153
154/* Causes poll_block() to wake up when network device change notifications are
155 * ready. */
156void
157rtbsd_notifier_wait(void)
a1f63b1e 158 OVS_EXCLUDED(rtbsd_mutex)
f6eb6b20 159{
a1f63b1e 160 ovs_mutex_lock(&rtbsd_mutex);
f6eb6b20
GL
161 if (notify_sock >= 0) {
162 poll_fd_wait(notify_sock, POLLIN);
163 }
a1f63b1e 164 ovs_mutex_unlock(&rtbsd_mutex);
f6eb6b20
GL
165}
166
167static void
168rtbsd_report_change(const struct if_msghdr *msg)
a1f63b1e 169 OVS_REQUIRES(rtbsd_mutex)
f6eb6b20
GL
170{
171 struct rtbsd_notifier *notifier;
172 struct rtbsd_change change;
837351e6 173#ifndef __MACH__
f5fb24db 174 const struct if_announcemsghdr *ahdr;
837351e6 175#endif
f6eb6b20
GL
176
177 COVERAGE_INC(rtbsd_changed);
178
b5343307
BP
179 change.msg_type = msg->ifm_type; /* XXX */
180 change.master_ifindex = 0; /* XXX */
f6eb6b20 181
f5fb24db
TLSC
182 switch (msg->ifm_type) {
183 case RTM_IFINFO:
184 change.if_index = msg->ifm_index;
185 if_indextoname(msg->ifm_index, change.if_name);
186 break;
97d0619c 187#ifndef __MACH__ /* OS X does not implement RTM_IFANNOUNCE */
f5fb24db
TLSC
188 case RTM_IFANNOUNCE:
189 ahdr = (const struct if_announcemsghdr *) msg;
190 change.if_index = ahdr->ifan_index;
191 strncpy(change.if_name, ahdr->ifan_name, IF_NAMESIZE);
192 break;
97d0619c 193#endif
f5fb24db
TLSC
194 }
195
f6eb6b20
GL
196 LIST_FOR_EACH (notifier, node, &all_notifiers) {
197 notifier->cb(&change, notifier->aux);
198 }
199}
200
201/* If an error occurs the notifiers' callbacks are called with NULL changes */
202static void
203rtbsd_report_notify_error(void)
a1f63b1e 204 OVS_REQUIRES(rtbsd_mutex)
f6eb6b20
GL
205{
206 struct rtbsd_notifier *notifier;
207
208 LIST_FOR_EACH (notifier, node, &all_notifiers) {
209 notifier->cb(NULL, notifier->aux);
210 }
211}