aboutsummaryrefslogtreecommitdiff
path: root/lib/rtnetlink.c
blob: 6ed85abbba2b5e51f35ca969e75f041f6c9ddb1b (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
/*
 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
 *
 * 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 "rtnetlink.h"

#include <errno.h>
#include <poll.h>
#include <stdlib.h>

#include "coverage.h"
#include "netlink.h"
#include "netlink-socket.h"
#include "ofpbuf.h"
#include "vlog.h"

VLOG_DEFINE_THIS_MODULE(rtnetlink);

COVERAGE_DEFINE(rtnetlink_changed);

static void rtnetlink_report(struct rtnetlink *rtn, void *change);

struct rtnetlink {
    struct nl_sock *notify_sock; /* Rtnetlink socket. */
    struct list all_notifiers;   /* All rtnetlink notifiers. */

    /* Passed in by rtnetlink_create(). */
    int multicast_group;         /* Multicast group we listen on. */
    rtnetlink_parse_func *parse; /* Message parsing function. */
    void *change;                /* Change passed to parse. */
};

/* Creates an rtnetlink handle which may be used to manage change
 * notifications.  The created handle will listen for rtnetlink messages on
 * 'multicast_group'.  Incoming messages will be parsed with 'parse' which will
 * be passed 'change' as an argument. */
struct rtnetlink *
rtnetlink_create(int multicast_group, rtnetlink_parse_func *parse,
                 void *change)
{
    struct rtnetlink *rtn;

    rtn                  = xzalloc(sizeof *rtn);
    rtn->notify_sock     = 0;
    rtn->multicast_group = multicast_group;
    rtn->parse           = parse;
    rtn->change          = change;

    list_init(&rtn->all_notifiers);
    return rtn;
}

/* Destroys 'rtn' by freeing any memory it has reserved and closing any sockets
 * it has opened. */
void
rtnetlink_destroy(struct rtnetlink *rtn)
{
    if (rtn) {
        nl_sock_destroy(rtn->notify_sock);
        free(rtn);
    }
}

/* Registers 'cb' to be called with auxiliary data 'aux' with change
 * notifications.  The notifier is stored in 'notifier', which the caller must
 * not modify or free.
 *
 * This is probably not the function you want.  You should probably be using
 * message specific notifiers like rtnetlink_link_notifier_register().
 *
 * Returns 0 if successful, otherwise a positive errno value. */
int
rtnetlink_notifier_register(struct rtnetlink *rtn,
                            struct rtnetlink_notifier *notifier,
                            rtnetlink_notify_func *cb, void *aux)
{
    if (!rtn->notify_sock) {
        struct nl_sock *sock;
        int error;

        error = nl_sock_create(NETLINK_ROUTE, &sock);
        if (!error) {
            error = nl_sock_join_mcgroup(sock, rtn->multicast_group);
        }
        if (error) {
            nl_sock_destroy(sock);
            VLOG_WARN("could not create rtnetlink socket: %s",
                      strerror(error));
            return error;
        }
        rtn->notify_sock = sock;
    } else {
        /* Catch up on notification work so that the new notifier won't
         * receive any stale notifications. */
        rtnetlink_notifier_run(rtn);
    }

    list_push_back(&rtn->all_notifiers, &notifier->node);
    notifier->cb = cb;
    notifier->aux = aux;
    return 0;
}

/* Cancels notification on 'notifier', which must have previously been
 * registered with rtnetlink_notifier_register(). */
void
rtnetlink_notifier_unregister(struct rtnetlink *rtn,
                              struct rtnetlink_notifier *notifier)
{
    list_remove(&notifier->node);
    if (list_is_empty(&rtn->all_notifiers)) {
        nl_sock_destroy(rtn->notify_sock);
        rtn->notify_sock = NULL;
    }
}

/* Calls all of the registered notifiers, passing along any as-yet-unreported
 * change events. */
void
rtnetlink_notifier_run(struct rtnetlink *rtn)
{
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);

    if (!rtn->notify_sock) {
        return;
    }

    for (;;) {
        struct ofpbuf *buf;
        int error;

        error = nl_sock_recv(rtn->notify_sock, &buf, false);
        if (!error) {
            if (rtn->parse(buf, rtn->change)) {
                rtnetlink_report(rtn, rtn->change);
            } else {
                VLOG_WARN_RL(&rl, "received bad rtnl message");
                rtnetlink_report(rtn, NULL);
            }
            ofpbuf_delete(buf);
        } else if (error == EAGAIN) {
            return;
        } else {
            if (error == ENOBUFS) {
                VLOG_WARN_RL(&rl, "rtnetlink receive buffer overflowed");
            } else {
                VLOG_WARN_RL(&rl, "error reading rtnetlink socket: %s",
                             strerror(error));
            }
            rtnetlink_report(rtn, NULL);
        }
    }
}

/* Causes poll_block() to wake up when change notifications are ready. */
void
rtnetlink_notifier_wait(struct rtnetlink *rtn)
{
    if (rtn->notify_sock) {
        nl_sock_wait(rtn->notify_sock, POLLIN);
    }
}

static void
rtnetlink_report(struct rtnetlink *rtn, void *change)
{
    struct rtnetlink_notifier *notifier;

    if (change) {
        COVERAGE_INC(rtnetlink_changed);
    }

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