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