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