2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "rtnetlink.h"
27 #include "netlink-socket.h"
31 VLOG_DEFINE_THIS_MODULE(rtnetlink);
33 COVERAGE_DEFINE(rtnetlink_changed);
35 static void rtnetlink_report(struct rtnetlink *rtn, void *change);
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. */
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. */
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. */
53 rtnetlink_create(int multicast_group, rtnetlink_parse_func *parse,
56 struct rtnetlink *rtn;
58 rtn = xzalloc(sizeof *rtn);
59 rtn->notify_sock = NULL;
60 rtn->multicast_group = multicast_group;
65 list_init(&rtn->all_notifiers);
69 /* Destroys 'rtn' by freeing any memory it has reserved and closing any sockets
72 rtnetlink_destroy(struct rtnetlink *rtn)
75 nl_sock_destroy(rtn->notify_sock);
80 /* Registers 'cb' to be called with auxiliary data 'aux' with change
81 * notifications. The notifier is stored in 'notifier', which the caller must
84 * This is probably not the function you want. You should probably be using
85 * message specific notifiers like rtnetlink_link_notifier_register().
87 * Returns 0 if successful, otherwise a positive errno value. */
89 rtnetlink_notifier_register(struct rtnetlink *rtn,
90 struct rtnetlink_notifier *notifier,
91 rtnetlink_notify_func *cb, void *aux)
93 if (!rtn->notify_sock) {
97 error = nl_sock_create(NETLINK_ROUTE, &sock);
99 error = nl_sock_join_mcgroup(sock, rtn->multicast_group);
102 nl_sock_destroy(sock);
103 VLOG_WARN("could not create rtnetlink socket: %s",
107 rtn->notify_sock = sock;
109 /* Catch up on notification work so that the new notifier won't
110 * receive any stale notifications. */
111 rtnetlink_notifier_run(rtn);
114 list_push_back(&rtn->all_notifiers, ¬ifier->node);
120 /* Cancels notification on 'notifier', which must have previously been
121 * registered with rtnetlink_notifier_register(). */
123 rtnetlink_notifier_unregister(struct rtnetlink *rtn,
124 struct rtnetlink_notifier *notifier)
126 list_remove(¬ifier->node);
127 if (list_is_empty(&rtn->all_notifiers)) {
128 nl_sock_destroy(rtn->notify_sock);
129 rtn->notify_sock = NULL;
133 /* Calls all of the registered notifiers, passing along any as-yet-unreported
136 rtnetlink_notifier_run(struct rtnetlink *rtn)
138 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
140 if (!rtn->notify_sock || rtn->has_run) {
149 error = nl_sock_recv(rtn->notify_sock, &buf, false);
151 if (rtn->parse(buf, rtn->change)) {
152 rtnetlink_report(rtn, rtn->change);
154 VLOG_WARN_RL(&rl, "received bad rtnl message");
155 rtnetlink_report(rtn, NULL);
158 } else if (error == EAGAIN) {
161 if (error == ENOBUFS) {
162 VLOG_WARN_RL(&rl, "rtnetlink receive buffer overflowed");
164 VLOG_WARN_RL(&rl, "error reading rtnetlink socket: %s",
167 rtnetlink_report(rtn, NULL);
172 /* Causes poll_block() to wake up when change notifications are ready. */
174 rtnetlink_notifier_wait(struct rtnetlink *rtn)
176 rtn->has_run = false;
177 if (rtn->notify_sock) {
178 nl_sock_wait(rtn->notify_sock, POLLIN);
183 rtnetlink_report(struct rtnetlink *rtn, void *change)
185 struct rtnetlink_notifier *notifier;
188 COVERAGE_INC(rtnetlink_changed);
191 LIST_FOR_EACH (notifier, node, &rtn->all_notifiers) {
192 notifier->cb(change, notifier->aux);