@@ -38,6 +38,7 @@ struct nln {
bool has_run; /* Guard for run and wait functions. */
/* Passed in by nln_create(). */
+ char *netns; /* The network namespace. */
int protocol; /* Protocol passed to nl_sock_create(). */
nln_parse_func *parse; /* Message parsing function. */
void *change; /* Change passed to parse. */
@@ -58,12 +59,14 @@ struct nln_notifier {
* Incoming messages will be parsed with 'parse' which will be passed 'change'
* as an argument. */
struct nln *
-nln_create(int protocol, nln_parse_func *parse, void *change)
+nln_create(const char *netns, int protocol, nln_parse_func *parse,
+ void *change)
{
struct nln *nln;
nln = xzalloc(sizeof *nln);
nln->notify_sock = NULL;
+ nln->netns = nullable_xstrdup(netns);
nln->protocol = protocol;
nln->parse = parse;
nln->change = change;
@@ -84,6 +87,7 @@ nln_destroy(struct nln *nln)
if (nln) {
ovs_assert(ovs_list_is_empty(&nln->all_notifiers));
nl_sock_destroy(nln->notify_sock);
+ free(nln->netns);
free(nln);
}
}
@@ -106,7 +110,7 @@ nln_notifier_create(struct nln *nln, int multicast_group, nln_notify_func *cb,
if (!nln->notify_sock) {
struct nl_sock *sock;
- error = nl_sock_create(nln->protocol, &sock);
+ error = nl_sock_ns_create(nln->netns, nln->protocol, &sock);
if (error) {
VLOG_WARN("could not create netlink socket: %s",
ovs_strerror(error));
@@ -187,7 +191,7 @@ nln_run(struct nln *nln)
ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
error = nl_sock_recv(nln->notify_sock, &buf, NULL, false);
if (!error) {
- int group = nln->parse(&buf, nln->change);
+ int group = nln->parse(&buf, nln->change, nln->netns);
if (group != 0) {
nln_report(nln, nln->change, group);
@@ -37,11 +37,15 @@ typedef void nln_notify_func(const void *change, void *aux);
/* Function called to parse incoming nln notifications. The 'buf' message
* should be parsed into 'change' as specified in nln_create().
+ * If the nln is running in a network namespaces this information is passed as
+ * the parameter netns. Otherwise it is NULL.
* Returns the multicast_group the change belongs to, or 0 for a parse error.
*/
-typedef int nln_parse_func(struct ofpbuf *buf, void *change);
+typedef int nln_parse_func(struct ofpbuf *buf, void *change,
+ const char *netns);
-struct nln *nln_create(int protocol, nln_parse_func *, void *change);
+struct nln *nln_create(const char *netns, int protocol,
+ nln_parse_func *, void *change);
void nln_destroy(struct nln *);
struct nln_notifier *nln_notifier_create(struct nln *, int multicast_group,
nln_notify_func *, void *aux);
@@ -1749,7 +1749,7 @@ static struct ovs_mutex pool_mutex = OVS_MUTEX_INITIALIZER;
static struct shash pools OVS_GUARDED_BY(pool_mutex) =
SHASH_INITIALIZER(&pools);
-static int
+int
nl_sock_ns_create(const char *netns, int protocol, struct nl_sock **sockp) {
#ifndef __linux__
if (netns) {
@@ -207,6 +207,7 @@ struct nl_sock;
/* Netlink sockets. */
int nl_sock_create(int protocol, struct nl_sock **);
+int nl_sock_ns_create(const char *netns, int protocol, struct nl_sock **sockp);
int nl_sock_clone(const struct nl_sock *, struct nl_sock **);
void nl_sock_destroy(struct nl_sock *);
@@ -85,7 +85,8 @@ static bool route_table_valid = false;
static void route_table_reset(void);
static void route_table_handle_msg(const struct route_table_msg *);
-static int route_table_parse(struct ofpbuf *, void *change);
+static int route_table_parse_ns(struct ofpbuf *, void *change,
+ const char *netns);
static void route_table_change(const struct route_table_msg *, void *);
static void route_map_clear(void);
@@ -110,7 +111,7 @@ route_table_init(void)
ovs_assert(!route6_notifier);
ovs_router_init();
- nln = nln_create(NETLINK_ROUTE, route_table_parse, &rtmsg);
+ nln = nln_create(NULL, NETLINK_ROUTE, route_table_parse_ns, &rtmsg);
route_notifier =
nln_notifier_create(nln, RTNLGRP_IPV4_ROUTE,
@@ -179,7 +180,7 @@ route_table_dump_one_table(const char *netns, unsigned char id)
while (nl_dump_next(&dump, &reply, &buf)) {
struct route_table_msg msg;
- if (route_table_parse(&reply, &msg)) {
+ if (route_table_parse_ns(&reply, &msg, netns)) {
struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(&reply);
/* Older kernels do not support filtering. */
@@ -222,7 +223,8 @@ route_table_reset(void)
/* Return RTNLGRP_IPV4_ROUTE or RTNLGRP_IPV6_ROUTE on success, 0 on parse
* error. */
static int
-route_table_parse(struct ofpbuf *buf, void *change_)
+route_table_parse_ns(struct ofpbuf *buf, void *change_,
+ const char *netns OVS_UNUSED)
{
struct route_table_msg *change = change_;
bool parsed, ipv4 = false;
@@ -190,7 +190,8 @@ rtnetlink_parse(struct ofpbuf *buf, struct rtnetlink_change *change)
/* Return RTNLGRP_LINK on success, 0 on parse error. */
static int
-rtnetlink_parse_cb(struct ofpbuf *buf, void *change)
+rtnetlink_parse_cb(struct ofpbuf *buf, void *change,
+ const char *netns OVS_UNUSED)
{
return rtnetlink_parse(buf, change) ? RTNLGRP_LINK : 0;
}
@@ -210,7 +211,7 @@ struct nln_notifier *
rtnetlink_notifier_create(rtnetlink_notify_func *cb, void *aux)
{
if (!nln) {
- nln = nln_create(NETLINK_ROUTE, rtnetlink_parse_cb, &rtn_change);
+ nln = nln_create(NULL, NETLINK_ROUTE, rtnetlink_parse_cb, &rtn_change);
}
return nln_notifier_create(nln, RTNLGRP_LINK, (nln_notify_func *) cb, aux);
@@ -32,7 +32,7 @@ struct test_change {
};
static int
-event_parse(struct ofpbuf *buf, void *change_)
+event_parse(struct ofpbuf *buf, void *change_, const char *netns OVS_UNUSED)
{
struct test_change *change = change_;
@@ -80,7 +80,7 @@ test_nl_ct_monitor(struct ovs_cmdl_context *ctx OVS_UNUSED)
unsigned i;
- nln = nln_create(NETLINK_NETFILTER, event_parse, &change);
+ nln = nln_create(NULL, NETLINK_NETFILTER, event_parse, &change);
for (i = 0; i < ARRAY_SIZE(groups); i++) {
notifiers[i] = nln_notifier_create(nln, groups[i], event_print, NULL);
Extend network namespace support also to the netlink-notifier. This is needed on the OVN side for watching routes. Signed-off-by: Felix Huettner <felix.huettner@stackit.cloud> --- lib/netlink-notifier.c | 10 +++++++--- lib/netlink-notifier.h | 8 ++++++-- lib/netlink-socket.c | 2 +- lib/netlink-socket.h | 1 + lib/route-table.c | 10 ++++++---- lib/rtnetlink.c | 5 +++-- tests/test-netlink-conntrack.c | 4 ++-- 7 files changed, 26 insertions(+), 14 deletions(-)