diff mbox series

[ovs-dev,v2,2/6] netlink-notifier: Support network namespaces.

Message ID cab2f3624ad9b7118e362df26602f42478d368cb.1732630344.git.felix.huettner@stackit.cloud
State Changes Requested
Delegated to: Eelco Chaudron
Headers show
Series OVS Patches for OVN Fabric Integration | expand

Checks

Context Check Description
ovsrobot/apply-robot warning apply and check: warning

Commit Message

Felix Huettner Nov. 26, 2024, 2:36 p.m. UTC
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(-)
diff mbox series

Patch

diff --git a/lib/netlink-notifier.c b/lib/netlink-notifier.c
index 7ea5a4181..c8397ec73 100644
--- a/lib/netlink-notifier.c
+++ b/lib/netlink-notifier.c
@@ -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);
diff --git a/lib/netlink-notifier.h b/lib/netlink-notifier.h
index dd0c183de..9ac602323 100644
--- a/lib/netlink-notifier.h
+++ b/lib/netlink-notifier.h
@@ -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);
diff --git a/lib/netlink-socket.c b/lib/netlink-socket.c
index 56d29faf5..3fb0a2c70 100644
--- a/lib/netlink-socket.c
+++ b/lib/netlink-socket.c
@@ -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) {
diff --git a/lib/netlink-socket.h b/lib/netlink-socket.h
index fb53284a6..f440af5d1 100644
--- a/lib/netlink-socket.h
+++ b/lib/netlink-socket.h
@@ -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 *);
 
diff --git a/lib/route-table.c b/lib/route-table.c
index bce29ebb2..270ad489a 100644
--- a/lib/route-table.c
+++ b/lib/route-table.c
@@ -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;
diff --git a/lib/rtnetlink.c b/lib/rtnetlink.c
index 37078d00e..9191032a0 100644
--- a/lib/rtnetlink.c
+++ b/lib/rtnetlink.c
@@ -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);
diff --git a/tests/test-netlink-conntrack.c b/tests/test-netlink-conntrack.c
index 2a62615b2..160909bf2 100644
--- a/tests/test-netlink-conntrack.c
+++ b/tests/test-netlink-conntrack.c
@@ -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);