2009-07-28 13:05:20 -07:00
|
|
|
/*
|
2016-07-05 08:33:05 -07:00
|
|
|
* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2016 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"
|
2016-03-25 14:10:24 -07:00
|
|
|
#include "openvswitch/ofpbuf.h"
|
2014-12-15 14:10:38 +01:00
|
|
|
#include "openvswitch/vlog.h"
|
2009-07-28 13:05:20 -07:00
|
|
|
|
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
|
|
|
struct nln {
|
|
|
|
struct nl_sock *notify_sock; /* Netlink socket. */
|
2014-12-15 14:10:38 +01:00
|
|
|
struct ovs_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(). */
|
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 {
|
2016-07-05 08:33:05 -07:00
|
|
|
struct ovs_list node; /* In struct nln's 'all_notifiers' list. */
|
2011-09-15 11:21:23 -07:00
|
|
|
struct nln *nln; /* Parent nln. */
|
|
|
|
|
2016-06-13 14:22:32 -07:00
|
|
|
int multicast_group; /* Multicast group we listen on. */
|
2011-09-15 11:21:23 -07:00
|
|
|
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 *
|
2016-06-13 14:22:32 -07:00
|
|
|
nln_create(int protocol, 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->parse = parse;
|
|
|
|
nln->change = change;
|
|
|
|
nln->has_run = false;
|
|
|
|
|
2016-03-25 14:10:22 -07:00
|
|
|
ovs_list_init(&nln->all_notifiers);
|
2011-08-24 15:17:32 -07:00
|
|
|
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) {
|
2016-03-25 14:10:22 -07:00
|
|
|
ovs_assert(ovs_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 *
|
2016-06-13 14:22:32 -07:00
|
|
|
nln_notifier_create(struct nln *nln, int multicast_group, 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;
|
2016-06-13 14:22:32 -07:00
|
|
|
int error;
|
2011-09-15 11:21:23 -07:00
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
if (!nln->notify_sock) {
|
2011-01-09 16:57:45 -08:00
|
|
|
struct nl_sock *sock;
|
|
|
|
|
2011-08-24 15:17:32 -07:00
|
|
|
error = nl_sock_create(nln->protocol, &sock);
|
2009-07-28 13:05:20 -07:00
|
|
|
if (error) {
|
2013-06-24 10:54:49 -07:00
|
|
|
VLOG_WARN("could not create netlink socket: %s",
|
|
|
|
ovs_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
|
|
|
}
|
|
|
|
|
2016-06-13 14:22:32 -07:00
|
|
|
error = nl_sock_join_mcgroup(nln->notify_sock, multicast_group);
|
|
|
|
if (error) {
|
|
|
|
VLOG_WARN("could not join netlink multicast group: %s",
|
|
|
|
ovs_strerror(error));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-15 11:21:23 -07:00
|
|
|
notifier = xmalloc(sizeof *notifier);
|
2016-06-13 14:22:32 -07:00
|
|
|
notifier->multicast_group = multicast_group;
|
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;
|
2016-06-13 14:22:32 -07:00
|
|
|
|
|
|
|
ovs_list_push_back(&nln->all_notifiers, ¬ifier->node);
|
|
|
|
|
2011-09-15 11:21:23 -07:00
|
|
|
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;
|
2016-06-13 14:22:32 -07:00
|
|
|
struct nln_notifier *iter;
|
|
|
|
int count = 0;
|
2011-09-15 11:21:23 -07:00
|
|
|
|
2016-03-25 14:10:22 -07:00
|
|
|
ovs_list_remove(¬ifier->node);
|
2016-06-13 14:22:32 -07:00
|
|
|
|
|
|
|
/* Leave the group if no other notifier is interested in it. */
|
|
|
|
LIST_FOR_EACH (iter, node, &nln->all_notifiers) {
|
|
|
|
if (iter->multicast_group == notifier->multicast_group) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count == 0) {
|
|
|
|
nl_sock_leave_mcgroup(nln->notify_sock, notifier->multicast_group);
|
|
|
|
}
|
|
|
|
|
2016-03-25 14:10:22 -07:00
|
|
|
if (ovs_list_is_empty(&nln->all_notifiers)) {
|
2011-09-15 11:21:23 -07:00
|
|
|
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);
|
2018-03-29 23:05:26 -03:00
|
|
|
error = nl_sock_recv(nln->notify_sock, &buf, NULL, false);
|
2009-07-28 13:05:20 -07:00
|
|
|
if (!error) {
|
2016-06-13 14:22:32 -07:00
|
|
|
int group = nln->parse(&buf, nln->change);
|
|
|
|
|
|
|
|
if (group != 0) {
|
|
|
|
nln_report(nln, nln->change, group);
|
2009-07-28 13:05:20 -07:00
|
|
|
} else {
|
2016-06-17 16:33:23 -03:00
|
|
|
VLOG_WARN_RL(&rl, "unexpected netlink message contents");
|
2016-06-13 14:22:32 -07:00
|
|
|
nln_report(nln, NULL, 0);
|
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) {
|
2014-07-22 06:38:57 +00:00
|
|
|
/* The socket buffer might be full, there could be too many
|
|
|
|
* notifications, so it makes sense to call nln_report() */
|
2016-06-13 14:22:32 -07:00
|
|
|
nln_report(nln, NULL, 0);
|
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",
|
2013-06-24 10:54:49 -07:00
|
|
|
ovs_strerror(error));
|
2009-07-28 13:05:20 -07:00
|
|
|
}
|
2014-07-22 06:38:57 +00:00
|
|
|
return;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:12:37 +02:00
|
|
|
void OVS_NO_SANITIZE_FUNCTION
|
2016-06-13 14:22:32 -07:00
|
|
|
nln_report(const struct nln *nln, void *change, int group)
|
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) {
|
2016-06-13 14:22:32 -07:00
|
|
|
if (!change || group == notifier->multicast_group) {
|
|
|
|
notifier->cb(change, notifier->aux);
|
|
|
|
}
|
2009-07-28 13:05:20 -07:00
|
|
|
}
|
|
|
|
}
|