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 "netlink-notifier.h"
27 #include "netlink-socket.h"
31 VLOG_DEFINE_THIS_MODULE(netlink_notifier);
33 COVERAGE_DEFINE(nln_changed);
35 static void nln_report(struct nln *nln, void *change);
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. */
42 /* Passed in by nln_create(). */
43 int multicast_group; /* Multicast group we listen on. */
44 int protocol; /* Protocal passed to nl_sock_create(). */
45 nln_parse_func *parse; /* Message parsing function. */
46 void *change; /* Change passed to parse. */
49 /* Creates an nln handle which may be used to manage change notifications. The
50 * created handle will listen for netlink messages on 'multicast_group' using
51 * netlink protocol 'protocol' (e.g. NETLINK_ROUTE, NETLINK_GENERIC, ...).
52 * Incoming messages will be parsed with 'parse' which will be passed 'change'
55 nln_create(int protocol, int multicast_group, nln_parse_func *parse,
60 nln = xzalloc(sizeof *nln);
61 nln->notify_sock = NULL;
62 nln->protocol = protocol;
63 nln->multicast_group = multicast_group;
68 list_init(&nln->all_notifiers);
72 /* Destroys 'nln' by freeing any memory it has reserved and closing any sockets
75 nln_destroy(struct nln *nln)
78 nl_sock_destroy(nln->notify_sock);
83 /* Registers 'cb' to be called with auxiliary data 'aux' with change
84 * notifications. The notifier is stored in 'notifier', which the caller must
87 * This is probably not the function you want. You should probably be using
88 * message specific notifiers like rtnetlink_link_notifier_register().
90 * Returns 0 if successful, otherwise a positive errno value. */
92 nln_notifier_register(struct nln *nln, struct nln_notifier *notifier,
93 nln_notify_func *cb, void *aux)
95 if (!nln->notify_sock) {
99 error = nl_sock_create(nln->protocol, &sock);
101 error = nl_sock_join_mcgroup(sock, nln->multicast_group);
104 nl_sock_destroy(sock);
105 VLOG_WARN("could not create netlink socket: %s", strerror(error));
108 nln->notify_sock = sock;
110 /* Catch up on notification work so that the new notifier won't
111 * receive any stale notifications. */
112 nln_notifier_run(nln);
115 list_push_back(&nln->all_notifiers, ¬ifier->node);
121 /* Cancels notification on 'notifier', which must have previously been
122 * registered with nln_notifier_register(). */
124 nln_notifier_unregister(struct nln *nln, struct nln_notifier *notifier)
126 list_remove(¬ifier->node);
127 if (list_is_empty(&nln->all_notifiers)) {
128 nl_sock_destroy(nln->notify_sock);
129 nln->notify_sock = NULL;
133 /* Calls all of the registered notifiers, passing along any as-yet-unreported
136 nln_notifier_run(struct nln *nln)
138 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
140 if (!nln->notify_sock || nln->has_run) {
149 error = nl_sock_recv(nln->notify_sock, &buf, false);
151 if (nln->parse(buf, nln->change)) {
152 nln_report(nln, nln->change);
154 VLOG_WARN_RL(&rl, "received bad netlink message");
155 nln_report(nln, NULL);
158 } else if (error == EAGAIN) {
161 if (error == ENOBUFS) {
162 VLOG_WARN_RL(&rl, "netlink receive buffer overflowed");
164 VLOG_WARN_RL(&rl, "error reading netlink socket: %s",
167 nln_report(nln, NULL);
172 /* Causes poll_block() to wake up when change notifications are ready. */
174 nln_notifier_wait(struct nln *nln)
176 nln->has_run = false;
177 if (nln->notify_sock) {
178 nl_sock_wait(nln->notify_sock, POLLIN);
183 nln_report(struct nln *nln, void *change)
185 struct nln_notifier *notifier;
188 COVERAGE_INC(nln_changed);
191 LIST_FOR_EACH (notifier, node, &nln->all_notifiers) {
192 notifier->cb(change, notifier->aux);