summaryrefslogtreecommitdiff
path: root/lib/rtbsd.c
blob: 564595c3a511ced1418003304c1d127c765cb122 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright (c) 2011, 2013 Gaetano Catalli.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>

#include "rtbsd.h"

#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/route.h>
#include <poll.h>

#include "coverage.h"
#include "socket-util.h"
#include "openvswitch/poll-loop.h"
#include "openvswitch/vlog.h"

VLOG_DEFINE_THIS_MODULE(rtbsd);
COVERAGE_DEFINE(rtbsd_changed);

static struct ovs_mutex rtbsd_mutex = OVS_MUTEX_INITIALIZER;

/* PF_ROUTE socket. */
static int notify_sock = -1;

/* All registered notifiers. */
static struct ovs_list all_notifiers = OVS_LIST_INITIALIZER(&all_notifiers);

static void rtbsd_report_change(const struct if_msghdr *)
    OVS_REQUIRES(rtbsd_mutex);
static void rtbsd_report_notify_error(void) OVS_REQUIRES(rtbsd_mutex);

/* Registers 'cb' to be called with auxiliary data 'aux' with network device
 * change notifications.  The notifier is stored in 'notifier', which the
 * caller must not modify or free.
 *
 * Returns 0 if successful, otherwise a positive errno value. */
int
rtbsd_notifier_register(struct rtbsd_notifier *notifier,
                            rtbsd_notify_func *cb, void *aux)
    OVS_EXCLUDED(rtbsd_mutex)
{
    int error = 0;

    ovs_mutex_lock(&rtbsd_mutex);
    if (notify_sock < 0) {
        notify_sock = socket(PF_ROUTE, SOCK_RAW, 0);
        if (notify_sock < 0) {
            VLOG_WARN("could not create PF_ROUTE socket: %s",
                      ovs_strerror(errno));
            error = errno;
            goto out;
        }
        error = set_nonblocking(notify_sock);
        if (error) {
            VLOG_WARN("error set_nonblocking PF_ROUTE socket: %s",
                    ovs_strerror(error));
            goto out;
        }
    }

    ovs_list_push_back(&all_notifiers, &notifier->node);
    notifier->cb = cb;
    notifier->aux = aux;

out:
    ovs_mutex_unlock(&rtbsd_mutex);
    return error;
}

/* Cancels notification on 'notifier', which must have previously been
 * registered with rtbsd_notifier_register(). */
void
rtbsd_notifier_unregister(struct rtbsd_notifier *notifier)
    OVS_EXCLUDED(rtbsd_mutex)
{
    ovs_mutex_lock(&rtbsd_mutex);
    ovs_list_remove(&notifier->node);
    if (ovs_list_is_empty(&all_notifiers)) {
        close(notify_sock);
        notify_sock = -1;
    }
    ovs_mutex_unlock(&rtbsd_mutex);
}

/* Calls all of the registered notifiers, passing along any as-yet-unreported
 * netdev change events. */
void
rtbsd_notifier_run(void)
    OVS_EXCLUDED(rtbsd_mutex)
{
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
    struct if_msghdr msg;

    ovs_mutex_lock(&rtbsd_mutex);
    if (notify_sock < 0) {
        ovs_mutex_unlock(&rtbsd_mutex);
        return;
    }

    for (;;) {
        int retval;

        msg.ifm_type = RTM_IFINFO;
        msg.ifm_version = RTM_VERSION; /* XXX Check if necessary. */

        /* read from PF_ROUTE socket */
        retval = read(notify_sock, (char *)&msg, sizeof(msg));
        if (retval >= 0) {
            /* received packet from PF_ROUTE socket
             * XXX check for bad packets */
            switch (msg.ifm_type) {
            case RTM_IFINFO:
            /* Since RTM_IFANNOUNCE messages are smaller than RTM_IFINFO
             * messages, the same buffer may be used. */
#ifndef __MACH__ /* OS X does not implement RTM_IFANNOUNCE */
            case RTM_IFANNOUNCE:
#endif
                rtbsd_report_change(&msg);
                break;
            default:
                break;
            }
        } else if (errno == EAGAIN) {
            ovs_mutex_unlock(&rtbsd_mutex);
            return;
        } else {
            if (errno == ENOBUFS) {
                VLOG_WARN_RL(&rl, "PF_ROUTE receive buffer overflowed");
            } else {
                VLOG_WARN_RL(&rl, "error reading PF_ROUTE socket: %s",
                             ovs_strerror(errno));
            }
            rtbsd_report_notify_error();
        }
    }
}

/* Causes poll_block() to wake up when network device change notifications are
 * ready. */
void
rtbsd_notifier_wait(void)
    OVS_EXCLUDED(rtbsd_mutex)
{
    ovs_mutex_lock(&rtbsd_mutex);
    if (notify_sock >= 0) {
        poll_fd_wait(notify_sock, POLLIN);
    }
    ovs_mutex_unlock(&rtbsd_mutex);
}

static void
rtbsd_report_change(const struct if_msghdr *msg)
    OVS_REQUIRES(rtbsd_mutex)
{
    struct rtbsd_notifier *notifier;
    struct rtbsd_change change;
#ifndef __MACH__
    const struct if_announcemsghdr *ahdr;
#endif

    COVERAGE_INC(rtbsd_changed);

    change.msg_type = msg->ifm_type; /* XXX */
    change.master_ifindex = 0; /* XXX */

    switch (msg->ifm_type) {
    case RTM_IFINFO:
        change.if_index = msg->ifm_index;
        if_indextoname(msg->ifm_index, change.if_name);
        break;
#ifndef __MACH__ /* OS X does not implement RTM_IFANNOUNCE */
    case RTM_IFANNOUNCE:
        ahdr = (const struct if_announcemsghdr *) msg;
        change.if_index = ahdr->ifan_index;
        strncpy(change.if_name, ahdr->ifan_name, IF_NAMESIZE);
        break;
#endif
    }

    LIST_FOR_EACH (notifier, node, &all_notifiers) {
        notifier->cb(&change, notifier->aux);
    }
}

/* If an error occurs the notifiers' callbacks are called with NULL changes */
static void
rtbsd_report_notify_error(void)
    OVS_REQUIRES(rtbsd_mutex)
{
    struct rtbsd_notifier *notifier;

    LIST_FOR_EACH (notifier, node, &all_notifiers) {
        notifier->cb(NULL, notifier->aux);
    }
}