Add support for extended netdev statistics based on RFC 2819.
[cascardo/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     ovs_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     ovs_list_remove(&notifier->node);
94     if (ovs_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 #ifndef __MACH__ /* OS X does not implement RTM_IFANNOUNCE */
132             case RTM_IFANNOUNCE:
133 #endif
134                 rtbsd_report_change(&msg);
135                 break;
136             default:
137                 break;
138             }
139         } else if (errno == EAGAIN) {
140             ovs_mutex_unlock(&rtbsd_mutex);
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",
147                              ovs_strerror(errno));
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. */
156 void
157 rtbsd_notifier_wait(void)
158     OVS_EXCLUDED(rtbsd_mutex)
159 {
160     ovs_mutex_lock(&rtbsd_mutex);
161     if (notify_sock >= 0) {
162         poll_fd_wait(notify_sock, POLLIN);
163     }
164     ovs_mutex_unlock(&rtbsd_mutex);
165 }
166
167 static void
168 rtbsd_report_change(const struct if_msghdr *msg)
169     OVS_REQUIRES(rtbsd_mutex)
170 {
171     struct rtbsd_notifier *notifier;
172     struct rtbsd_change change;
173 #ifndef __MACH__
174     const struct if_announcemsghdr *ahdr;
175 #endif
176
177     COVERAGE_INC(rtbsd_changed);
178
179     change.msg_type = msg->ifm_type; //XXX
180     change.master_ifindex = 0; //XXX
181
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;
187 #ifndef __MACH__ /* OS X does not implement RTM_IFANNOUNCE */
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;
193 #endif
194     }
195
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 */
202 static void
203 rtbsd_report_notify_error(void)
204     OVS_REQUIRES(rtbsd_mutex)
205 {
206     struct rtbsd_notifier *notifier;
207
208     LIST_FOR_EACH (notifier, node, &all_notifiers) {
209         notifier->cb(NULL, notifier->aux);
210     }
211 }