1b7529dd7726ce9f2c358d780f536c994aa80dd4
[sliver-openvswitch.git] / lib / netlink-notifier.c
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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 "netlink-notifier.h"
20
21 #include <errno.h>
22 #include <poll.h>
23 #include <stdlib.h>
24
25 #include "coverage.h"
26 #include "netlink.h"
27 #include "netlink-socket.h"
28 #include "ofpbuf.h"
29 #include "vlog.h"
30
31 VLOG_DEFINE_THIS_MODULE(netlink_notifier);
32
33 COVERAGE_DEFINE(nln_changed);
34
35 static void nln_report(struct nln *nln, void *change);
36
37 struct nln {
38     struct nl_sock *notify_sock; /* Netlink socket. */
39     struct list all_notifiers;   /* All nln notifiers. */
40     bool has_run;                /* Guard for run and wait functions. */
41
42     /* Passed in by nln_create(). */
43     int multicast_group;         /* Multicast group we listen on. */
44     int protocol;                /* Protocal passed to nl_sock_create(). */
45     nln_parse_func *parse;       /* Message parsing function. */
46     void *change;                /* Change passed to parse. */
47 };
48
49 /* Creates an nln handle which may be used to manage change notifications.  The
50  * created handle will listen for netlink messages on 'multicast_group' using
51  * netlink protocol 'protocol' (e.g. NETLINK_ROUTE, NETLINK_GENERIC, ...).
52  * Incoming messages will be parsed with 'parse' which will be passed 'change'
53  * as an argument. */
54 struct nln *
55 nln_create(int protocol, int multicast_group, nln_parse_func *parse,
56            void *change)
57 {
58     struct nln *nln;
59
60     nln = xzalloc(sizeof *nln);
61     nln->notify_sock = NULL;
62     nln->protocol = protocol;
63     nln->multicast_group = multicast_group;
64     nln->parse = parse;
65     nln->change = change;
66     nln->has_run = false;
67
68     list_init(&nln->all_notifiers);
69     return nln;
70 }
71
72 /* Destroys 'nln' by freeing any memory it has reserved and closing any sockets
73  * it has opened. */
74 void
75 nln_destroy(struct nln *nln)
76 {
77     if (nln) {
78         nl_sock_destroy(nln->notify_sock);
79         free(nln);
80     }
81 }
82
83 /* Registers 'cb' to be called with auxiliary data 'aux' with change
84  * notifications.  The notifier is stored in 'notifier', which the caller must
85  * not modify or free.
86  *
87  * This is probably not the function you want.  You should probably be using
88  * message specific notifiers like rtnetlink_link_notifier_register().
89  *
90  * Returns 0 if successful, otherwise a positive errno value. */
91 int
92 nln_notifier_register(struct nln *nln, struct nln_notifier *notifier,
93                       nln_notify_func *cb, void *aux)
94 {
95     if (!nln->notify_sock) {
96         struct nl_sock *sock;
97         int error;
98
99         error = nl_sock_create(nln->protocol, &sock);
100         if (!error) {
101             error = nl_sock_join_mcgroup(sock, nln->multicast_group);
102         }
103         if (error) {
104             nl_sock_destroy(sock);
105             VLOG_WARN("could not create netlink socket: %s", strerror(error));
106             return error;
107         }
108         nln->notify_sock = sock;
109     } else {
110         /* Catch up on notification work so that the new notifier won't
111          * receive any stale notifications. */
112         nln_notifier_run(nln);
113     }
114
115     list_push_back(&nln->all_notifiers, &notifier->node);
116     notifier->cb = cb;
117     notifier->aux = aux;
118     return 0;
119 }
120
121 /* Cancels notification on 'notifier', which must have previously been
122  * registered with nln_notifier_register(). */
123 void
124 nln_notifier_unregister(struct nln *nln, struct nln_notifier *notifier)
125 {
126     list_remove(&notifier->node);
127     if (list_is_empty(&nln->all_notifiers)) {
128         nl_sock_destroy(nln->notify_sock);
129         nln->notify_sock = NULL;
130     }
131 }
132
133 /* Calls all of the registered notifiers, passing along any as-yet-unreported
134  * change events. */
135 void
136 nln_notifier_run(struct nln *nln)
137 {
138     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
139
140     if (!nln->notify_sock || nln->has_run) {
141         return;
142     }
143
144     nln->has_run = true;
145     for (;;) {
146         struct ofpbuf *buf;
147         int error;
148
149         error = nl_sock_recv(nln->notify_sock, &buf, false);
150         if (!error) {
151             if (nln->parse(buf, nln->change)) {
152                 nln_report(nln, nln->change);
153             } else {
154                 VLOG_WARN_RL(&rl, "received bad netlink message");
155                 nln_report(nln, NULL);
156             }
157             ofpbuf_delete(buf);
158         } else if (error == EAGAIN) {
159             return;
160         } else {
161             if (error == ENOBUFS) {
162                 VLOG_WARN_RL(&rl, "netlink receive buffer overflowed");
163             } else {
164                 VLOG_WARN_RL(&rl, "error reading netlink socket: %s",
165                              strerror(error));
166             }
167             nln_report(nln, NULL);
168         }
169     }
170 }
171
172 /* Causes poll_block() to wake up when change notifications are ready. */
173 void
174 nln_notifier_wait(struct nln *nln)
175 {
176     nln->has_run = false;
177     if (nln->notify_sock) {
178         nl_sock_wait(nln->notify_sock, POLLIN);
179     }
180 }
181
182 static void
183 nln_report(struct nln *nln, void *change)
184 {
185     struct nln_notifier *notifier;
186
187     if (change) {
188         COVERAGE_INC(nln_changed);
189     }
190
191     LIST_FOR_EACH (notifier, node, &nln->all_notifiers) {
192         notifier->cb(change, notifier->aux);
193     }
194 }