From: Robin Jarry <rjarry@redhat.com>
To: dev@dpdk.org, Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH dpdk v3 3/3] net/tap: detect namespace change
Date: Mon, 27 Oct 2025 23:16:25 +0100 [thread overview]
Message-ID: <20251027221622.494080-8-rjarry@redhat.com> (raw)
In-Reply-To: <20251027221622.494080-5-rjarry@redhat.com>
When an interface is moved to another network namespace, the kernel
sends RTM_DELLINK. Detect this case by using TUNGETDEVNETNS ioctl on the
keep-alive fd. If successful, the interface still exists but in
a different namespace.
To handle this, temporarily switch to the new namespace using setns(),
query the new ifindex, recreate netlink and LSC interrupt sockets in
that namespace, then switch back. Replace the old netlink socket with
the new one so subsequent operations work in the target namespace.
This allows the driver to track interfaces across namespace changes
without losing control.
Signed-off-by: Robin Jarry <rjarry@redhat.com>
---
doc/guides/rel_notes/release_25_11.rst | 1 +
drivers/net/tap/rte_eth_tap.c | 114 ++++++++++++++++++++++++-
2 files changed, 112 insertions(+), 3 deletions(-)
diff --git a/doc/guides/rel_notes/release_25_11.rst b/doc/guides/rel_notes/release_25_11.rst
index 41b6131c80f3..fe191bd78aa2 100644
--- a/doc/guides/rel_notes/release_25_11.rst
+++ b/doc/guides/rel_notes/release_25_11.rst
@@ -171,6 +171,7 @@ New Features
* Replaced ``ioctl`` based link control with a Netlink based implementation.
* Linux net devices can now be renamed without breaking link control.
+ * Linux net devices can now be moved to different namespaces without breaking link control.
Removed Items
diff --git a/drivers/net/tap/rte_eth_tap.c b/drivers/net/tap/rte_eth_tap.c
index e006c71989a8..bb96aa7e61ec 100644
--- a/drivers/net/tap/rte_eth_tap.c
+++ b/drivers/net/tap/rte_eth_tap.c
@@ -33,6 +33,7 @@
#include <unistd.h>
#include <net/if.h>
#include <linux/if_tun.h>
+#include <linux/sched.h>
#include <fcntl.h>
#include <tap_rss.h>
@@ -1638,17 +1639,118 @@ tap_set_mc_addr_list(struct rte_eth_dev *dev __rte_unused,
return 0;
}
+#ifdef TUNGETDEVNETNS
+static void tap_dev_intr_handler(void *cb_arg);
+static int tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set);
+
+static int
+tap_netns_change(struct rte_eth_dev *dev)
+{
+ struct pmd_internals *pmd = dev->data->dev_private;
+ int netns_fd, orig_netns_fd, new_nlsk_fd;
+
+ netns_fd = ioctl(pmd->ka_fd, TUNGETDEVNETNS);
+ if (netns_fd < 0) {
+ TAP_LOG(INFO, "%s: interface deleted", pmd->name);
+ return 0;
+ }
+
+ /* Interface was moved to another namespace */
+ pmd->if_index = 0;
+
+ /* Save current namespace */
+ orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
+ if (orig_netns_fd < 0) {
+ TAP_LOG(ERR, "%s: failed to open original netns: %s",
+ pmd->name, strerror(errno));
+ close(netns_fd);
+ return -1;
+ }
+
+ /* Switch to new namespace */
+ if (setns(netns_fd, CLONE_NEWNET) < 0) {
+ TAP_LOG(ERR, "%s: failed to enter new netns: %s",
+ pmd->name, strerror(errno));
+ close(netns_fd);
+ close(orig_netns_fd);
+ return -1;
+ }
+
+ /*
+ * Update ifindex by querying interface name.
+ * The interface now has a new ifindex in the new namespace.
+ */
+ pmd->if_index = if_nametoindex(pmd->name);
+
+ /* Recreate netlink socket in new namespace */
+ new_nlsk_fd = tap_nl_init(0);
+
+ /* Recreate LSC interrupt netlink socket in new namespace */
+ rte_intr_callback_unregister_pending(pmd->intr_handle, tap_dev_intr_handler, dev, NULL);
+ if (tap_lsc_intr_handle_set(dev, 1) < 0)
+ TAP_LOG(WARNING, "%s: failed to recreate LSC interrupt socket",
+ pmd->name);
+
+ /* Switch back to original namespace */
+ if (setns(orig_netns_fd, CLONE_NEWNET) < 0)
+ TAP_LOG(ERR, "%s: failed to return to original netns: %s",
+ pmd->name, strerror(errno));
+
+ close(orig_netns_fd);
+ close(netns_fd);
+
+ if (pmd->if_index == 0) {
+ TAP_LOG(WARNING, "%s: interface moved to another namespace, "
+ "failed to get new ifindex",
+ pmd->name);
+ if (new_nlsk_fd >= 0)
+ close(new_nlsk_fd);
+ return -1;
+ }
+
+ if (new_nlsk_fd < 0) {
+ TAP_LOG(WARNING, "%s: failed to recreate netlink socket in new namespace",
+ pmd->name);
+ return -1;
+ }
+
+ /* Close old netlink socket and replace with new one */
+ if (pmd->nlsk_fd >= 0)
+ tap_nl_final(pmd->nlsk_fd);
+ pmd->nlsk_fd = new_nlsk_fd;
+
+ TAP_LOG(INFO, "%s: interface moved to another namespace, new ifindex: %u",
+ pmd->name, pmd->if_index);
+
+ return 0;
+}
+#endif
+
static int
tap_nl_msg_handler(struct nlmsghdr *nh, void *arg)
{
struct rte_eth_dev *dev = arg;
struct pmd_internals *pmd = dev->data->dev_private;
struct ifinfomsg *info = NLMSG_DATA(nh);
+ int is_local = (info->ifi_index == pmd->if_index);
+ int is_remote = (info->ifi_index == pmd->remote_if_index);
- if (nh->nlmsg_type != RTM_NEWLINK ||
- (info->ifi_index != pmd->if_index &&
- info->ifi_index != pmd->remote_if_index))
+ /* Ignore messages not for our interfaces */
+ if (!is_local && !is_remote)
return 0;
+
+#ifdef TUNGETDEVNETNS
+ if (nh->nlmsg_type == RTM_DELLINK && is_local) {
+ /*
+ * RTM_DELLINK may indicate the interface was moved to another
+ * network namespace. Check if the device still exists by
+ * querying its namespace via the keep-alive fd.
+ */
+ int ret = tap_netns_change(dev);
+ if (ret < 0)
+ return ret;
+ }
+#endif
return tap_link_update(dev, 0);
}
@@ -1677,6 +1779,12 @@ tap_lsc_intr_handle_set(struct rte_eth_dev *dev, int set)
return 0;
}
if (set) {
+ /*
+ * Subscribe to RTMGRP_LINK to receive RTM_NEWLINK (link state
+ * changes) events. Also receives RTM_DELLINK events which are
+ * used for namespace change detection when TUNGETDEVNETNS is
+ * available.
+ */
rte_intr_fd_set(pmd->intr_handle, tap_nl_init(RTMGRP_LINK));
if (unlikely(rte_intr_fd_get(pmd->intr_handle) == -1))
return -EBADF;
--
2.51.0
prev parent reply other threads:[~2025-10-27 22:18 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-27 15:37 [PATCH dpdk 0/4] net/tap: add network namespace support Robin Jarry
2025-10-27 15:37 ` [PATCH dpdk 1/4] net/tap: add netlink helpers Robin Jarry
2025-10-27 15:37 ` [PATCH dpdk 2/4] net/tap: rename internal ioctl wrapper Robin Jarry
2025-10-27 15:37 ` [PATCH dpdk 3/4] net/tap: use netlink if possible Robin Jarry
2025-10-27 16:06 ` Stephen Hemminger
2025-10-27 16:10 ` Robin Jarry
2025-10-27 16:58 ` Stephen Hemminger
2025-10-27 15:37 ` [PATCH dpdk 4/4] net/tap: detect namespace change Robin Jarry
2025-10-27 18:19 ` [PATCH dpdk v2 0/3] net/tap: add network namespace support Robin Jarry
2025-10-27 18:19 ` [PATCH dpdk v2 1/3] net/tap: add netlink helpers Robin Jarry
2025-10-27 18:19 ` [PATCH dpdk v2 2/3] net/tap: replace ioctl with netlink Robin Jarry
2025-10-27 18:19 ` [PATCH dpdk v2 3/3] net/tap: detect namespace change Robin Jarry
2025-10-27 21:55 ` [PATCH dpdk v2 0/3] net/tap: add network namespace support Stephen Hemminger
2025-10-27 22:16 ` [PATCH dpdk v3 " Robin Jarry
2025-10-27 22:16 ` [PATCH dpdk v3 1/3] net/tap: add netlink helpers Robin Jarry
2025-10-27 22:16 ` [PATCH dpdk v3 2/3] net/tap: replace ioctl with netlink Robin Jarry
2025-10-27 22:16 ` Robin Jarry [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251027221622.494080-8-rjarry@redhat.com \
--to=rjarry@redhat.com \
--cc=dev@dpdk.org \
--cc=stephen@networkplumber.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).