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