2009-07-28 13:05:20 -07:00
|
|
|
/*
|
2012-05-02 15:21:36 -07:00
|
|
|
* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
|
2009-07-28 13:05:20 -07:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2011-08-25 14:06:54 -07:00
|
|
|
#include "netlink-notifier.h"
|
2009-07-28 13:05:20 -07:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <poll.h>
|
2011-01-13 11:54:33 -08:00
|
|
|
#include <stdlib.h>
|
2009-07-28 13:05:20 -07:00
|
|
|
|
|
|
|
#include "coverage.h"
|
|
|
|
#include "netlink.h"
|
2010-12-10 09:51:03 -08:00
|
|
|
#include "netlink-socket.h"
|
2009-07-28 13:05:20 -07:00
|
|
|
#include "ofpbuf.h"
|
|
|
|
#include "vlog.h"
|
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
VLOG_DEFINE_THIS_MODULE(netlink_notifier);
|
2010-07-16 11:02:49 -07:00
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
COVERAGE_DEFINE(nln_changed);
|
coverage: Make the coverage counters catalog program-specific.
Until now, the collection of coverage counters supported by a given OVS
program was not specific to that program. That means that, for example,
even though ovs-dpctl does not have anything to do with mac_learning, it
still has a coverage counter for it. This is confusing, at best.
This commit fixes the problem on some systems, in particular on ones that
use GCC and the GNU linker. It uses the feature of the GNU linker
described in its manual as:
If an orphaned section's name is representable as a C identifier then
the linker will automatically see PROVIDE two symbols: __start_SECNAME
and __end_SECNAME, where SECNAME is the name of the section. These
indicate the start address and end address of the orphaned section
respectively.
Systems that don't support these features retain the earlier behavior.
This commit also fixes the annoyance that files that include coverage
counters must be listed on COVERAGE_FILES in lib/automake.mk.
This commit also fixes the annoyance that modifying any source file that
includes a coverage counter caused all programs that link against
libopenvswitch.a to relink, even programs that the source file was not
linked into. For example, modifying ofproto/ofproto.c (which includes
coverage counters) caused tests/test-aes128 to relink, even though
test-aes128 does not link again ofproto.o.
2010-11-01 14:14:27 -07:00
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
static void nln_report(struct nln *nln, void *change);
|
2010-12-21 13:44:37 -08:00
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
struct nln {
|
|
|
|
struct nl_sock *notify_sock; /* Netlink socket. */
|
|
|
|
struct list all_notifiers; /* All nln notifiers. */
|
2011-08-22 12:47:43 -07:00
|
|
|
bool has_run; /* Guard for run and wait functions. */
|
2010-12-21 13:44:37 -08:00
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
/* Passed in by nln_create(). */
|
2010-12-21 13:44:37 -08:00
|
|
|
int multicast_group; /* Multicast group we listen on. */
|
2013-06-04 17:35:36 -05:00
|
|
|
int protocol; /* Protocol passed to nl_sock_create(). */
|
2011-08-24 15:17:32 -07:00
|
|
|
nln_parse_func *parse; /* Message parsing function. */
|
2010-12-21 13:44:37 -08:00
|
|
|
void *change; /* Change passed to parse. */
|
|
|
|
};
|
|
|
|
|
2011-09-15 11:21:23 -07:00
|
|
|
struct nln_notifier {
|
|
|
|
struct nln *nln; /* Parent nln. */
|
|
|
|
|
|
|
|
struct list node;
|
|
|
|
nln_notify_func *cb;
|
|
|
|
void *aux;
|
|
|
|
};
|
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
/* Creates an nln handle which may be used to manage change notifications. The
|
|
|
|
* created handle will listen for netlink messages on 'multicast_group' using
|
|
|
|
* netlink protocol 'protocol' (e.g. NETLINK_ROUTE, NETLINK_GENERIC, ...).
|
|
|
|
* Incoming messages will be parsed with 'parse' which will be passed 'change'
|
|
|
|
* as an argument. */
|
|
|
|
struct nln *
|
|
|
|
nln_create(int protocol, int multicast_group, nln_parse_func *parse,
|
|
|
|
void *change)
|
2010-12-21 13:44:37 -08:00
|
|
|
{
|
2011-08-24 15:17:32 -07:00
|
|
|
struct nln *nln;
|
|
|
|
|
|
|
|
nln = xzalloc(sizeof *nln);
|
|
|
|
nln->notify_sock = NULL;
|
|
|
|
nln->protocol = protocol;
|
|
|
|
nln->multicast_group = multicast_group;
|
|
|
|
nln->parse = parse;
|
|
|
|
nln->change = change;
|
|
|
|
nln->has_run = false;
|
|
|
|
|
|
|
|
list_init(&nln->all_notifiers);
|
|
|
|
return nln;
|
2010-12-21 13:44:37 -08:00
|
|
|
}
|
2009-07-28 13:05:20 -07:00
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
/* Destroys 'nln' by freeing any memory it has reserved and closing any sockets
|
2011-09-15 11:21:23 -07:00
|
|
|
* it has opened.
|
|
|
|
*
|
|
|
|
* The caller is responsible for destroying any notifiers created by this
|
|
|
|
* 'nln' before destroying 'nln'. */
|
2011-01-13 11:54:33 -08:00
|
|
|
void
|
2011-08-24 15:17:32 -07:00
|
|
|
nln_destroy(struct nln *nln)
|
2011-01-13 11:54:33 -08:00
|
|
|
{
|
2011-08-24 15:17:32 -07:00
|
|
|
if (nln) {
|
2012-11-06 13:14:55 -08:00
|
|
|
ovs_assert(list_is_empty(&nln->all_notifiers));
|
2011-08-24 15:17:32 -07:00
|
|
|
nl_sock_destroy(nln->notify_sock);
|
|
|
|
free(nln);
|
2011-01-13 11:54:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-21 13:44:37 -08:00
|
|
|
/* Registers 'cb' to be called with auxiliary data 'aux' with change
|
|
|
|
* notifications. The notifier is stored in 'notifier', which the caller must
|
|
|
|
* not modify or free.
|
2009-07-28 13:05:20 -07:00
|
|
|
*
|
2010-12-21 13:44:37 -08:00
|
|
|
* This is probably not the function you want. You should probably be using
|
|
|
|
* message specific notifiers like rtnetlink_link_notifier_register().
|
2009-07-28 13:05:20 -07:00
|
|
|
*
|
2011-09-15 11:21:23 -07:00
|
|
|
* Returns an initialized nln_notifier if successful, otherwise NULL. */
|
|
|
|
struct nln_notifier *
|
|
|
|
nln_notifier_create(struct nln *nln, nln_notify_func *cb, void *aux)
|
2009-07-28 13:05:20 -07:00
|
|
|
{
|
2011-09-15 11:21:23 -07:00
|
|
|
struct nln_notifier *notifier;
|
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
if (!nln->notify_sock) {
|
2011-01-09 16:57:45 -08:00
|
|
|
struct nl_sock *sock;
|
|
|
|
int error;
|
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
error = nl_sock_create(nln->protocol, &sock);
|
2011-01-09 16:57:45 -08:00
|
|
|
if (!error) {
|
2011-08-24 15:17:32 -07:00
|
|
|
error = nl_sock_join_mcgroup(sock, nln->multicast_group);
|
2011-01-09 16:57:45 -08:00
|
|
|
}
|
2009-07-28 13:05:20 -07:00
|
|
|
if (error) {
|
2011-01-09 16:57:45 -08:00
|
|
|
nl_sock_destroy(sock);
|
2011-08-24 15:17:32 -07:00
|
|
|
VLOG_WARN("could not create netlink socket: %s", strerror(error));
|
2011-09-15 11:21:23 -07:00
|
|
|
return NULL;
|
2009-07-28 13:05:20 -07:00
|
|
|
}
|
2011-08-24 15:17:32 -07:00
|
|
|
nln->notify_sock = sock;
|
2009-07-28 13:05:20 -07:00
|
|
|
} else {
|
|
|
|
/* Catch up on notification work so that the new notifier won't
|
|
|
|
* receive any stale notifications. */
|
2011-09-15 11:23:08 -07:00
|
|
|
nln_run(nln);
|
2009-07-28 13:05:20 -07:00
|
|
|
}
|
|
|
|
|
2011-09-15 11:21:23 -07:00
|
|
|
notifier = xmalloc(sizeof *notifier);
|
2011-08-24 15:17:32 -07:00
|
|
|
list_push_back(&nln->all_notifiers, ¬ifier->node);
|
2009-07-28 13:05:20 -07:00
|
|
|
notifier->cb = cb;
|
|
|
|
notifier->aux = aux;
|
2011-09-15 11:21:23 -07:00
|
|
|
notifier->nln = nln;
|
|
|
|
return notifier;
|
2009-07-28 13:05:20 -07:00
|
|
|
}
|
|
|
|
|
2011-09-15 11:21:23 -07:00
|
|
|
/* Destroys 'notifier', which must have previously been created with
|
|
|
|
* nln_notifier_register(). */
|
2009-07-28 13:05:20 -07:00
|
|
|
void
|
2011-09-15 11:21:23 -07:00
|
|
|
nln_notifier_destroy(struct nln_notifier *notifier)
|
2009-07-28 13:05:20 -07:00
|
|
|
{
|
2011-09-15 11:21:23 -07:00
|
|
|
if (notifier) {
|
|
|
|
struct nln *nln = notifier->nln;
|
|
|
|
|
|
|
|
list_remove(¬ifier->node);
|
|
|
|
if (list_is_empty(&nln->all_notifiers)) {
|
|
|
|
nl_sock_destroy(nln->notify_sock);
|
|
|
|
nln->notify_sock = NULL;
|
|
|
|
}
|
|
|
|
free(notifier);
|
2009-07-28 13:05:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calls all of the registered notifiers, passing along any as-yet-unreported
|
2010-12-21 13:44:37 -08:00
|
|
|
* change events. */
|
2009-07-28 13:05:20 -07:00
|
|
|
void
|
2011-09-15 11:23:08 -07:00
|
|
|
nln_run(struct nln *nln)
|
2009-07-28 13:05:20 -07:00
|
|
|
{
|
|
|
|
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
|
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
if (!nln->notify_sock || nln->has_run) {
|
2009-07-28 13:05:20 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
nln->has_run = true;
|
2009-07-28 13:05:20 -07:00
|
|
|
for (;;) {
|
2012-04-09 15:35:29 -07:00
|
|
|
uint64_t buf_stub[4096 / 8];
|
|
|
|
struct ofpbuf buf;
|
2009-07-28 13:05:20 -07:00
|
|
|
int error;
|
|
|
|
|
2012-04-09 15:35:29 -07:00
|
|
|
ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
|
2011-08-24 15:17:32 -07:00
|
|
|
error = nl_sock_recv(nln->notify_sock, &buf, false);
|
2009-07-28 13:05:20 -07:00
|
|
|
if (!error) {
|
2012-04-09 15:35:29 -07:00
|
|
|
if (nln->parse(&buf, nln->change)) {
|
2011-08-24 15:17:32 -07:00
|
|
|
nln_report(nln, nln->change);
|
2009-07-28 13:05:20 -07:00
|
|
|
} else {
|
2011-08-24 15:17:32 -07:00
|
|
|
VLOG_WARN_RL(&rl, "received bad netlink message");
|
|
|
|
nln_report(nln, NULL);
|
2009-07-28 13:05:20 -07:00
|
|
|
}
|
2012-04-09 15:35:29 -07:00
|
|
|
ofpbuf_uninit(&buf);
|
2009-07-28 13:05:20 -07:00
|
|
|
} else if (error == EAGAIN) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (error == ENOBUFS) {
|
2011-08-24 15:17:32 -07:00
|
|
|
VLOG_WARN_RL(&rl, "netlink receive buffer overflowed");
|
2009-07-28 13:05:20 -07:00
|
|
|
} else {
|
2011-08-24 15:17:32 -07:00
|
|
|
VLOG_WARN_RL(&rl, "error reading netlink socket: %s",
|
2009-07-28 13:05:20 -07:00
|
|
|
strerror(error));
|
|
|
|
}
|
2011-08-24 15:17:32 -07:00
|
|
|
nln_report(nln, NULL);
|
2009-07-28 13:05:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-21 13:44:37 -08:00
|
|
|
/* Causes poll_block() to wake up when change notifications are ready. */
|
2009-07-28 13:05:20 -07:00
|
|
|
void
|
2011-09-15 11:23:08 -07:00
|
|
|
nln_wait(struct nln *nln)
|
2009-07-28 13:05:20 -07:00
|
|
|
{
|
2011-08-24 15:17:32 -07:00
|
|
|
nln->has_run = false;
|
|
|
|
if (nln->notify_sock) {
|
|
|
|
nl_sock_wait(nln->notify_sock, POLLIN);
|
2009-07-28 13:05:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-08-24 15:17:32 -07:00
|
|
|
nln_report(struct nln *nln, void *change)
|
2009-07-28 13:05:20 -07:00
|
|
|
{
|
2011-08-24 15:17:32 -07:00
|
|
|
struct nln_notifier *notifier;
|
2009-07-28 13:05:20 -07:00
|
|
|
|
2010-12-21 13:44:37 -08:00
|
|
|
if (change) {
|
2011-08-24 15:17:32 -07:00
|
|
|
COVERAGE_INC(nln_changed);
|
2009-07-28 13:05:20 -07:00
|
|
|
}
|
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
LIST_FOR_EACH (notifier, node, &nln->all_notifiers) {
|
2010-12-21 13:44:37 -08:00
|
|
|
notifier->cb(change, notifier->aux);
|
2009-07-28 13:05:20 -07:00
|
|
|
}
|
|
|
|
}
|