mirror of
https://github.com/openvswitch/ovs
synced 2025-10-25 15:07:05 +00:00
rtnetlink: Remove LINK specific messages from rtnetlink
Abstracted rtnetlink so that it may be used for messages other than RTM LINK messages. Created a new rtnetlink-link module which specifically deals with these kinds of messages and follows the old rtnetlink API.
This commit is contained in:
@@ -189,7 +189,9 @@ lib_libopenvswitch_a_SOURCES += \
|
|||||||
lib/netlink-socket.c \
|
lib/netlink-socket.c \
|
||||||
lib/netlink-socket.h \
|
lib/netlink-socket.h \
|
||||||
lib/rtnetlink.c \
|
lib/rtnetlink.c \
|
||||||
lib/rtnetlink.h
|
lib/rtnetlink.h \
|
||||||
|
lib/rtnetlink-link.c \
|
||||||
|
lib/rtnetlink-link.h
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if HAVE_OPENSSL
|
if HAVE_OPENSSL
|
||||||
|
|||||||
@@ -40,6 +40,7 @@
|
|||||||
#include "openvswitch/tunnel.h"
|
#include "openvswitch/tunnel.h"
|
||||||
#include "poll-loop.h"
|
#include "poll-loop.h"
|
||||||
#include "rtnetlink.h"
|
#include "rtnetlink.h"
|
||||||
|
#include "rtnetlink-link.h"
|
||||||
#include "shash.h"
|
#include "shash.h"
|
||||||
#include "svec.h"
|
#include "svec.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
@@ -72,7 +73,7 @@ static int get_openvswitch_major(void);
|
|||||||
static int create_minor(const char *name, int minor, struct dpif **dpifp);
|
static int create_minor(const char *name, int minor, struct dpif **dpifp);
|
||||||
static int open_minor(int minor, struct dpif **dpifp);
|
static int open_minor(int minor, struct dpif **dpifp);
|
||||||
static int make_openvswitch_device(int minor, char **fnp);
|
static int make_openvswitch_device(int minor, char **fnp);
|
||||||
static void dpif_linux_port_changed(const struct rtnetlink_change *,
|
static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
|
||||||
void *dpif);
|
void *dpif);
|
||||||
|
|
||||||
static struct dpif_linux *
|
static struct dpif_linux *
|
||||||
@@ -177,7 +178,7 @@ static void
|
|||||||
dpif_linux_close(struct dpif *dpif_)
|
dpif_linux_close(struct dpif *dpif_)
|
||||||
{
|
{
|
||||||
struct dpif_linux *dpif = dpif_linux_cast(dpif_);
|
struct dpif_linux *dpif = dpif_linux_cast(dpif_);
|
||||||
rtnetlink_notifier_unregister(&dpif->port_notifier);
|
rtnetlink_link_notifier_unregister(&dpif->port_notifier);
|
||||||
shash_destroy(&dpif->changed_ports);
|
shash_destroy(&dpif->changed_ports);
|
||||||
free(dpif->local_ifname);
|
free(dpif->local_ifname);
|
||||||
close(dpif->fd);
|
close(dpif->fd);
|
||||||
@@ -367,7 +368,7 @@ dpif_linux_port_poll_wait(const struct dpif *dpif_)
|
|||||||
if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
|
if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
|
||||||
poll_immediate_wake();
|
poll_immediate_wake();
|
||||||
} else {
|
} else {
|
||||||
rtnetlink_notifier_wait();
|
rtnetlink_link_notifier_wait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -770,8 +771,9 @@ open_minor(int minor, struct dpif **dpifp)
|
|||||||
fd = open(fn, O_RDONLY | O_NONBLOCK);
|
fd = open(fn, O_RDONLY | O_NONBLOCK);
|
||||||
if (fd >= 0) {
|
if (fd >= 0) {
|
||||||
struct dpif_linux *dpif = xmalloc(sizeof *dpif);
|
struct dpif_linux *dpif = xmalloc(sizeof *dpif);
|
||||||
error = rtnetlink_notifier_register(&dpif->port_notifier,
|
error = rtnetlink_link_notifier_register(&dpif->port_notifier,
|
||||||
dpif_linux_port_changed, dpif);
|
dpif_linux_port_changed,
|
||||||
|
dpif);
|
||||||
if (!error) {
|
if (!error) {
|
||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
@@ -799,7 +801,8 @@ open_minor(int minor, struct dpif **dpifp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
|
dpif_linux_port_changed(const struct rtnetlink_link_change *change,
|
||||||
|
void *dpif_)
|
||||||
{
|
{
|
||||||
struct dpif_linux *dpif = dpif_;
|
struct dpif_linux *dpif = dpif_;
|
||||||
|
|
||||||
|
|||||||
@@ -59,6 +59,7 @@
|
|||||||
#include "packets.h"
|
#include "packets.h"
|
||||||
#include "poll-loop.h"
|
#include "poll-loop.h"
|
||||||
#include "rtnetlink.h"
|
#include "rtnetlink.h"
|
||||||
|
#include "rtnetlink-link.h"
|
||||||
#include "socket-util.h"
|
#include "socket-util.h"
|
||||||
#include "shash.h"
|
#include "shash.h"
|
||||||
#include "svec.h"
|
#include "svec.h"
|
||||||
@@ -457,17 +458,17 @@ netdev_linux_init(void)
|
|||||||
static void
|
static void
|
||||||
netdev_linux_run(void)
|
netdev_linux_run(void)
|
||||||
{
|
{
|
||||||
rtnetlink_notifier_run();
|
rtnetlink_link_notifier_run();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
netdev_linux_wait(void)
|
netdev_linux_wait(void)
|
||||||
{
|
{
|
||||||
rtnetlink_notifier_wait();
|
rtnetlink_link_notifier_wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
netdev_linux_cache_cb(const struct rtnetlink_change *change,
|
netdev_linux_cache_cb(const struct rtnetlink_link_change *change,
|
||||||
void *aux OVS_UNUSED)
|
void *aux OVS_UNUSED)
|
||||||
{
|
{
|
||||||
struct netdev_dev_linux *dev;
|
struct netdev_dev_linux *dev;
|
||||||
@@ -511,8 +512,8 @@ netdev_linux_create(const struct netdev_class *class,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!cache_notifier_refcount) {
|
if (!cache_notifier_refcount) {
|
||||||
error = rtnetlink_notifier_register(&netdev_linux_cache_notifier,
|
error = rtnetlink_link_notifier_register(&netdev_linux_cache_notifier,
|
||||||
netdev_linux_cache_cb, NULL);
|
netdev_linux_cache_cb, NULL);
|
||||||
if (error) {
|
if (error) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
@@ -608,7 +609,7 @@ netdev_linux_destroy(struct netdev_dev *netdev_dev_)
|
|||||||
cache_notifier_refcount--;
|
cache_notifier_refcount--;
|
||||||
|
|
||||||
if (!cache_notifier_refcount) {
|
if (!cache_notifier_refcount) {
|
||||||
rtnetlink_notifier_unregister(&netdev_linux_cache_notifier);
|
rtnetlink_link_notifier_unregister(&netdev_linux_cache_notifier);
|
||||||
}
|
}
|
||||||
} else if (class == &netdev_tap_class) {
|
} else if (class == &netdev_tap_class) {
|
||||||
destroy_tap(netdev_dev);
|
destroy_tap(netdev_dev);
|
||||||
@@ -2049,7 +2050,7 @@ poll_notify(struct list *list)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
netdev_linux_poll_cb(const struct rtnetlink_change *change,
|
netdev_linux_poll_cb(const struct rtnetlink_link_change *change,
|
||||||
void *aux OVS_UNUSED)
|
void *aux OVS_UNUSED)
|
||||||
{
|
{
|
||||||
if (change) {
|
if (change) {
|
||||||
@@ -2076,8 +2077,9 @@ netdev_linux_poll_add(struct netdev *netdev,
|
|||||||
struct list *list;
|
struct list *list;
|
||||||
|
|
||||||
if (shash_is_empty(&netdev_linux_notifiers)) {
|
if (shash_is_empty(&netdev_linux_notifiers)) {
|
||||||
int error = rtnetlink_notifier_register(&netdev_linux_poll_notifier,
|
int error;
|
||||||
netdev_linux_poll_cb, NULL);
|
error = rtnetlink_link_notifier_register(&netdev_linux_poll_notifier,
|
||||||
|
netdev_linux_poll_cb, NULL);
|
||||||
if (error) {
|
if (error) {
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
@@ -2117,7 +2119,7 @@ netdev_linux_poll_remove(struct netdev_notifier *notifier_)
|
|||||||
|
|
||||||
/* If that was the last notifier, unregister. */
|
/* If that was the last notifier, unregister. */
|
||||||
if (shash_is_empty(&netdev_linux_notifiers)) {
|
if (shash_is_empty(&netdev_linux_notifiers)) {
|
||||||
rtnetlink_notifier_unregister(&netdev_linux_poll_notifier);
|
rtnetlink_link_notifier_unregister(&netdev_linux_poll_notifier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
120
lib/rtnetlink-link.c
Normal file
120
lib/rtnetlink-link.c
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2009, 2010 Nicira Networks.
|
||||||
|
*
|
||||||
|
* 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>
|
||||||
|
|
||||||
|
#include "rtnetlink-link.h"
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <linux/rtnetlink.h>
|
||||||
|
#include <net/if.h>
|
||||||
|
|
||||||
|
#include "netlink.h"
|
||||||
|
#include "ofpbuf.h"
|
||||||
|
#include "rtnetlink.h"
|
||||||
|
|
||||||
|
static struct rtnetlink *rtn = NULL;
|
||||||
|
static struct rtnetlink_link_change rtn_change;
|
||||||
|
|
||||||
|
static bool
|
||||||
|
rtnetlink_link_parse(struct ofpbuf *buf,
|
||||||
|
struct rtnetlink_link_change *change)
|
||||||
|
{
|
||||||
|
bool parsed;
|
||||||
|
|
||||||
|
/* Policy for RTNLGRP_LINK messages.
|
||||||
|
*
|
||||||
|
* There are *many* more fields in these messages, but currently we
|
||||||
|
* only care about these fields. */
|
||||||
|
static const struct nl_policy policy[] = {
|
||||||
|
[IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
|
||||||
|
[IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct nlattr *attrs[ARRAY_SIZE(policy)];
|
||||||
|
|
||||||
|
parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
|
||||||
|
policy, attrs, ARRAY_SIZE(policy));
|
||||||
|
|
||||||
|
if (parsed) {
|
||||||
|
const struct nlmsghdr *nlmsg;
|
||||||
|
const struct ifinfomsg *ifinfo;
|
||||||
|
|
||||||
|
nlmsg = buf->data;
|
||||||
|
ifinfo = ((const struct ifinfomsg *)
|
||||||
|
((const char *) buf->data + NLMSG_HDRLEN));
|
||||||
|
|
||||||
|
change->nlmsg_type = nlmsg->nlmsg_type;
|
||||||
|
change->ifi_index = ifinfo->ifi_index;
|
||||||
|
change->ifname = nl_attr_get_string(attrs[IFLA_IFNAME]);
|
||||||
|
change->master_ifindex = (attrs[IFLA_MASTER]
|
||||||
|
? nl_attr_get_u32(attrs[IFLA_MASTER])
|
||||||
|
: 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Registers 'cb' to be called with auxiliary data 'aux' with network device
|
||||||
|
* change notifications. The notifier is stored in 'notifier', which the
|
||||||
|
* caller must not modify or free.
|
||||||
|
*
|
||||||
|
* This is probably not the function that you want. You should probably be
|
||||||
|
* using dpif_port_poll() or netdev_monitor_create(), which unlike this
|
||||||
|
* function are not Linux-specific.
|
||||||
|
*
|
||||||
|
* Returns 0 if successful, otherwise a positive errno value. */
|
||||||
|
int
|
||||||
|
rtnetlink_link_notifier_register(struct rtnetlink_notifier *notifier,
|
||||||
|
rtnetlink_link_notify_func *cb, void *aux)
|
||||||
|
{
|
||||||
|
rtnetlink_parse_func *pf = (rtnetlink_parse_func *) rtnetlink_link_parse;
|
||||||
|
rtnetlink_notify_func *nf = (rtnetlink_notify_func *) cb;
|
||||||
|
|
||||||
|
if (!rtn) {
|
||||||
|
rtn = rtnetlink_create(RTNLGRP_LINK, pf, &rtn_change);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rtnetlink_notifier_register(rtn, notifier, nf, aux);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Cancels notification on 'notifier', which must have previously been
|
||||||
|
* registered with rtnetlink_link_notifier_register(). */
|
||||||
|
void
|
||||||
|
rtnetlink_link_notifier_unregister(struct rtnetlink_notifier *notifier)
|
||||||
|
{
|
||||||
|
rtnetlink_notifier_unregister(rtn, notifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Calls all of the registered notifiers, passing along any as-yet-unreported
|
||||||
|
* netdev change events. */
|
||||||
|
void
|
||||||
|
rtnetlink_link_notifier_run(void)
|
||||||
|
{
|
||||||
|
if (rtn) {
|
||||||
|
rtnetlink_notifier_run(rtn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Causes poll_block() to wake up when network device change notifications are
|
||||||
|
* ready. */
|
||||||
|
void
|
||||||
|
rtnetlink_link_notifier_wait(void)
|
||||||
|
{
|
||||||
|
if (rtn) {
|
||||||
|
rtnetlink_notifier_wait(rtn);
|
||||||
|
}
|
||||||
|
}
|
||||||
53
lib/rtnetlink-link.h
Normal file
53
lib/rtnetlink-link.h
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2009 Nicira Networks.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef RTNETLINK_LINK_H
|
||||||
|
#define RTNETLINK_LINK_H 1
|
||||||
|
|
||||||
|
struct rtnetlink_notifier;
|
||||||
|
|
||||||
|
/* These functions are Linux specific, so they should be used directly only by
|
||||||
|
* Linux-specific code. */
|
||||||
|
|
||||||
|
/* A digested version of an rtnetlink_link message sent down by the kernel to
|
||||||
|
* indicate that a network device has been created, destroyed or changed. */
|
||||||
|
struct rtnetlink_link_change {
|
||||||
|
/* Copied from struct nlmsghdr. */
|
||||||
|
int nlmsg_type; /* e.g. RTM_NEWLINK, RTM_DELLINK. */
|
||||||
|
|
||||||
|
/* Copied from struct ifinfomsg. */
|
||||||
|
int ifi_index; /* Index of network device. */
|
||||||
|
|
||||||
|
/* Extracted from Netlink attributes. */
|
||||||
|
const char *ifname; /* Name of network device. */
|
||||||
|
int master_ifindex; /* Ifindex of datapath master (0 if none). */
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Function called to report that a netdev has changed. 'change' describes the
|
||||||
|
* specific change. It may be null if the buffer of change information
|
||||||
|
* overflowed, in which case the function must assume that every device may
|
||||||
|
* have changed. 'aux' is as specified in the call to
|
||||||
|
* rtnetlink_link_notifier_register(). */
|
||||||
|
typedef
|
||||||
|
void rtnetlink_link_notify_func(const struct rtnetlink_link_change *change,
|
||||||
|
void *aux);
|
||||||
|
|
||||||
|
int rtnetlink_link_notifier_register(struct rtnetlink_notifier *,
|
||||||
|
rtnetlink_link_notify_func *, void *aux);
|
||||||
|
void rtnetlink_link_notifier_unregister(struct rtnetlink_notifier *);
|
||||||
|
void rtnetlink_link_notifier_run(void);
|
||||||
|
void rtnetlink_link_notifier_wait(void);
|
||||||
|
#endif /* rtnetlink-link.h */
|
||||||
145
lib/rtnetlink.c
145
lib/rtnetlink.c
@@ -19,9 +19,6 @@
|
|||||||
#include "rtnetlink.h"
|
#include "rtnetlink.h"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <linux/rtnetlink.h>
|
|
||||||
#include <net/if.h>
|
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
|
|
||||||
#include "coverage.h"
|
#include "coverage.h"
|
||||||
@@ -34,33 +31,54 @@ VLOG_DEFINE_THIS_MODULE(rtnetlink);
|
|||||||
|
|
||||||
COVERAGE_DEFINE(rtnetlink_changed);
|
COVERAGE_DEFINE(rtnetlink_changed);
|
||||||
|
|
||||||
/* rtnetlink socket. */
|
static void rtnetlink_report(struct rtnetlink *rtn, void *change);
|
||||||
static struct nl_sock *notify_sock;
|
|
||||||
|
|
||||||
/* All registered notifiers. */
|
struct rtnetlink {
|
||||||
static struct list all_notifiers = LIST_INITIALIZER(&all_notifiers);
|
struct nl_sock *notify_sock; /* Rtnetlink socket. */
|
||||||
|
struct list all_notifiers; /* All rtnetlink notifiers. */
|
||||||
|
|
||||||
static void rtnetlink_report_change(const struct nlmsghdr *,
|
/* Passed in by rtnetlink_create(). */
|
||||||
const struct ifinfomsg *,
|
int multicast_group; /* Multicast group we listen on. */
|
||||||
struct nlattr *attrs[]);
|
rtnetlink_parse_func *parse; /* Message parsing function. */
|
||||||
static void rtnetlink_report_notify_error(void);
|
void *change; /* Change passed to parse. */
|
||||||
|
};
|
||||||
|
|
||||||
/* Registers 'cb' to be called with auxiliary data 'aux' with network device
|
/* Creates an rtnetlink handle which may be used to manage change
|
||||||
* change notifications. The notifier is stored in 'notifier', which the
|
* notifications. The created handle will listen for rtnetlink messages on
|
||||||
* caller must not modify or free.
|
* 'multicast_group'. Incoming messages will be parsed with 'parse' which will
|
||||||
|
* be passed 'change' as an argument. */
|
||||||
|
struct rtnetlink *
|
||||||
|
rtnetlink_create(int multicast_group, rtnetlink_parse_func *parse,
|
||||||
|
void *change)
|
||||||
|
{
|
||||||
|
struct rtnetlink *rtn;
|
||||||
|
|
||||||
|
rtn = xzalloc(sizeof *rtn);
|
||||||
|
rtn->notify_sock = 0;
|
||||||
|
rtn->multicast_group = multicast_group;
|
||||||
|
rtn->parse = parse;
|
||||||
|
rtn->change = change;
|
||||||
|
|
||||||
|
list_init(&rtn->all_notifiers);
|
||||||
|
return rtn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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.
|
||||||
*
|
*
|
||||||
* This is probably not the function that you want. You should probably be
|
* This is probably not the function you want. You should probably be using
|
||||||
* using dpif_port_poll() or netdev_monitor_create(), which unlike this
|
* message specific notifiers like rtnetlink_link_notifier_register().
|
||||||
* function are not Linux-specific.
|
|
||||||
*
|
*
|
||||||
* Returns 0 if successful, otherwise a positive errno value. */
|
* Returns 0 if successful, otherwise a positive errno value. */
|
||||||
int
|
int
|
||||||
rtnetlink_notifier_register(struct rtnetlink_notifier *notifier,
|
rtnetlink_notifier_register(struct rtnetlink *rtn,
|
||||||
|
struct rtnetlink_notifier *notifier,
|
||||||
rtnetlink_notify_func *cb, void *aux)
|
rtnetlink_notify_func *cb, void *aux)
|
||||||
{
|
{
|
||||||
if (!notify_sock) {
|
if (!rtn->notify_sock) {
|
||||||
int error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0,
|
int error = nl_sock_create(NETLINK_ROUTE, rtn->multicast_group, 0, 0,
|
||||||
¬ify_sock);
|
&rtn->notify_sock);
|
||||||
if (error) {
|
if (error) {
|
||||||
VLOG_WARN("could not create rtnetlink socket: %s",
|
VLOG_WARN("could not create rtnetlink socket: %s",
|
||||||
strerror(error));
|
strerror(error));
|
||||||
@@ -69,10 +87,10 @@ rtnetlink_notifier_register(struct rtnetlink_notifier *notifier,
|
|||||||
} else {
|
} else {
|
||||||
/* Catch up on notification work so that the new notifier won't
|
/* Catch up on notification work so that the new notifier won't
|
||||||
* receive any stale notifications. */
|
* receive any stale notifications. */
|
||||||
rtnetlink_notifier_run();
|
rtnetlink_notifier_run(rtn);
|
||||||
}
|
}
|
||||||
|
|
||||||
list_push_back(&all_notifiers, ¬ifier->node);
|
list_push_back(&rtn->all_notifiers, ¬ifier->node);
|
||||||
notifier->cb = cb;
|
notifier->cb = cb;
|
||||||
notifier->aux = aux;
|
notifier->aux = aux;
|
||||||
return 0;
|
return 0;
|
||||||
@@ -81,52 +99,38 @@ rtnetlink_notifier_register(struct rtnetlink_notifier *notifier,
|
|||||||
/* Cancels notification on 'notifier', which must have previously been
|
/* Cancels notification on 'notifier', which must have previously been
|
||||||
* registered with rtnetlink_notifier_register(). */
|
* registered with rtnetlink_notifier_register(). */
|
||||||
void
|
void
|
||||||
rtnetlink_notifier_unregister(struct rtnetlink_notifier *notifier)
|
rtnetlink_notifier_unregister(struct rtnetlink *rtn,
|
||||||
|
struct rtnetlink_notifier *notifier)
|
||||||
{
|
{
|
||||||
list_remove(¬ifier->node);
|
list_remove(¬ifier->node);
|
||||||
if (list_is_empty(&all_notifiers)) {
|
if (list_is_empty(&rtn->all_notifiers)) {
|
||||||
nl_sock_destroy(notify_sock);
|
nl_sock_destroy(rtn->notify_sock);
|
||||||
notify_sock = NULL;
|
rtn->notify_sock = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Calls all of the registered notifiers, passing along any as-yet-unreported
|
/* Calls all of the registered notifiers, passing along any as-yet-unreported
|
||||||
* netdev change events. */
|
* change events. */
|
||||||
void
|
void
|
||||||
rtnetlink_notifier_run(void)
|
rtnetlink_notifier_run(struct rtnetlink *rtn)
|
||||||
{
|
{
|
||||||
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
|
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
|
||||||
|
|
||||||
if (!notify_sock) {
|
if (!rtn->notify_sock) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
/* Policy for RTNLGRP_LINK messages.
|
|
||||||
*
|
|
||||||
* There are *many* more fields in these messages, but currently we
|
|
||||||
* only care about these fields. */
|
|
||||||
static const struct nl_policy rtnetlink_policy[] = {
|
|
||||||
[IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
|
|
||||||
[IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
|
|
||||||
};
|
|
||||||
|
|
||||||
struct nlattr *attrs[ARRAY_SIZE(rtnetlink_policy)];
|
|
||||||
struct ofpbuf *buf;
|
struct ofpbuf *buf;
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
error = nl_sock_recv(notify_sock, &buf, false);
|
error = nl_sock_recv(rtn->notify_sock, &buf, false);
|
||||||
if (!error) {
|
if (!error) {
|
||||||
if (nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
|
if (rtn->parse(buf, rtn->change)) {
|
||||||
rtnetlink_policy,
|
rtnetlink_report(rtn, rtn->change);
|
||||||
attrs, ARRAY_SIZE(rtnetlink_policy))) {
|
|
||||||
struct ifinfomsg *ifinfo;
|
|
||||||
|
|
||||||
ifinfo = (void *) ((char *) buf->data + NLMSG_HDRLEN);
|
|
||||||
rtnetlink_report_change(buf->data, ifinfo, attrs);
|
|
||||||
} else {
|
} else {
|
||||||
VLOG_WARN_RL(&rl, "received bad rtnl message");
|
VLOG_WARN_RL(&rl, "received bad rtnl message");
|
||||||
rtnetlink_report_notify_error();
|
rtnetlink_report(rtn, NULL);
|
||||||
}
|
}
|
||||||
ofpbuf_delete(buf);
|
ofpbuf_delete(buf);
|
||||||
} else if (error == EAGAIN) {
|
} else if (error == EAGAIN) {
|
||||||
@@ -138,48 +142,31 @@ rtnetlink_notifier_run(void)
|
|||||||
VLOG_WARN_RL(&rl, "error reading rtnetlink socket: %s",
|
VLOG_WARN_RL(&rl, "error reading rtnetlink socket: %s",
|
||||||
strerror(error));
|
strerror(error));
|
||||||
}
|
}
|
||||||
rtnetlink_report_notify_error();
|
rtnetlink_report(rtn, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Causes poll_block() to wake up when network device change notifications are
|
/* Causes poll_block() to wake up when change notifications are ready. */
|
||||||
* ready. */
|
|
||||||
void
|
void
|
||||||
rtnetlink_notifier_wait(void)
|
rtnetlink_notifier_wait(struct rtnetlink *rtn)
|
||||||
{
|
{
|
||||||
if (notify_sock) {
|
if (rtn->notify_sock) {
|
||||||
nl_sock_wait(notify_sock, POLLIN);
|
nl_sock_wait(rtn->notify_sock, POLLIN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
rtnetlink_report_change(const struct nlmsghdr *nlmsg,
|
rtnetlink_report(struct rtnetlink *rtn, void *change)
|
||||||
const struct ifinfomsg *ifinfo,
|
|
||||||
struct nlattr *attrs[])
|
|
||||||
{
|
|
||||||
struct rtnetlink_notifier *notifier;
|
|
||||||
struct rtnetlink_change change;
|
|
||||||
|
|
||||||
COVERAGE_INC(rtnetlink_changed);
|
|
||||||
|
|
||||||
change.nlmsg_type = nlmsg->nlmsg_type;
|
|
||||||
change.ifi_index = ifinfo->ifi_index;
|
|
||||||
change.ifname = nl_attr_get_string(attrs[IFLA_IFNAME]);
|
|
||||||
change.master_ifindex = (attrs[IFLA_MASTER]
|
|
||||||
? nl_attr_get_u32(attrs[IFLA_MASTER]) : 0);
|
|
||||||
|
|
||||||
LIST_FOR_EACH (notifier, node, &all_notifiers) {
|
|
||||||
notifier->cb(&change, notifier->aux);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
rtnetlink_report_notify_error(void)
|
|
||||||
{
|
{
|
||||||
struct rtnetlink_notifier *notifier;
|
struct rtnetlink_notifier *notifier;
|
||||||
|
|
||||||
LIST_FOR_EACH (notifier, node, &all_notifiers) {
|
if (change) {
|
||||||
notifier->cb(NULL, notifier->aux);
|
COVERAGE_INC(rtnetlink_changed);
|
||||||
|
}
|
||||||
|
|
||||||
|
LIST_FOR_EACH (notifier, node, &rtn->all_notifiers) {
|
||||||
|
notifier->cb(change, notifier->aux);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,26 +22,22 @@
|
|||||||
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
||||||
/* A digested version of an rtnetlink message sent down by the kernel to
|
struct rtnetlink;
|
||||||
* indicate that a network device has been created or destroyed or changed. */
|
struct nlattr;
|
||||||
struct rtnetlink_change {
|
struct ofpbuf;
|
||||||
/* Copied from struct nlmsghdr. */
|
|
||||||
int nlmsg_type; /* e.g. RTM_NEWLINK, RTM_DELLINK. */
|
|
||||||
|
|
||||||
/* Copied from struct ifinfomsg. */
|
/* Function called to report rtnetlink notifications. 'change' describes the
|
||||||
int ifi_index; /* Index of network device. */
|
* specific change filled out by an rtnetlink_parse_func. It may be null if
|
||||||
|
* the buffer of change information overflowed, in which case the function must
|
||||||
|
* assume that everything may have changed. 'aux' is as specified in
|
||||||
|
* rtnetlink_notifier_register().
|
||||||
|
*/
|
||||||
|
typedef void rtnetlink_notify_func(const void *change, void *aux);
|
||||||
|
|
||||||
/* Extracted from Netlink attributes. */
|
/* Function called to parse incoming rtnetlink notifications. The 'buf'
|
||||||
const char *ifname; /* Name of network device. */
|
* message should be parsed into 'change' as specified in rtnetlink_create().
|
||||||
int master_ifindex; /* Ifindex of datapath master (0 if none). */
|
*/
|
||||||
};
|
typedef bool rtnetlink_parse_func(struct ofpbuf *buf, void *change);
|
||||||
|
|
||||||
/* Function called to report that a netdev has changed. 'change' describes the
|
|
||||||
* specific change. It may be null if the buffer of change information
|
|
||||||
* overflowed, in which case the function must assume that every device may
|
|
||||||
* have changed. 'aux' is as specified in the call to
|
|
||||||
* rtnetlink_notifier_register(). */
|
|
||||||
typedef void rtnetlink_notify_func(const struct rtnetlink_change *, void *aux);
|
|
||||||
|
|
||||||
struct rtnetlink_notifier {
|
struct rtnetlink_notifier {
|
||||||
struct list node;
|
struct list node;
|
||||||
@@ -49,10 +45,14 @@ struct rtnetlink_notifier {
|
|||||||
void *aux;
|
void *aux;
|
||||||
};
|
};
|
||||||
|
|
||||||
int rtnetlink_notifier_register(struct rtnetlink_notifier *,
|
struct rtnetlink *rtnetlink_create(int multicast_group,
|
||||||
|
rtnetlink_parse_func *,
|
||||||
|
void *change);
|
||||||
|
int rtnetlink_notifier_register(struct rtnetlink *,
|
||||||
|
struct rtnetlink_notifier *,
|
||||||
rtnetlink_notify_func *, void *aux);
|
rtnetlink_notify_func *, void *aux);
|
||||||
void rtnetlink_notifier_unregister(struct rtnetlink_notifier *);
|
void rtnetlink_notifier_unregister(struct rtnetlink *,
|
||||||
void rtnetlink_notifier_run(void);
|
struct rtnetlink_notifier *);
|
||||||
void rtnetlink_notifier_wait(void);
|
void rtnetlink_notifier_run(struct rtnetlink *);
|
||||||
|
void rtnetlink_notifier_wait(struct rtnetlink *);
|
||||||
#endif /* rtnetlink.h */
|
#endif /* rtnetlink.h */
|
||||||
|
|||||||
Reference in New Issue
Block a user