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