[PATCH net-next v5 3/7] selftest: tun: Refactor tun_delete to use tuntap_helpers

Xu Du posted 7 patches 2 weeks, 6 days ago
There is a newer version of this series
[PATCH net-next v5 3/7] selftest: tun: Refactor tun_delete to use tuntap_helpers
Posted by Xu Du 2 weeks, 6 days ago
The previous patch introduced common tuntap helpers to simplify
tun test code. This patch refactors the tun_delete function to
use these new helpers.

Signed-off-by: Xu Du <xudu@redhat.com>
---
v4 -> v5:
 - Delete device using the new ip link YNL API.
 - Linked YNL library to the tun program.

 tools/testing/selftests/net/Makefile |  7 ++---
 tools/testing/selftests/net/tun.c    | 39 ++--------------------------
 2 files changed, 6 insertions(+), 40 deletions(-)

diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index b66ba04f19d9..64e941e6a88f 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -180,7 +180,6 @@ TEST_GEN_PROGS := \
 	tap \
 	tcp_port_share \
 	tls \
-	tun \
 # end of TEST_GEN_PROGS
 
 TEST_FILES := \
@@ -192,7 +191,9 @@ TEST_FILES := \
 
 # YNL files, must be before "include ..lib.mk"
 YNL_GEN_FILES := busy_poller
-YNL_GEN_PROGS := netlink-dumps
+YNL_GEN_PROGS := \
+	netlink-dumps \
+	tun \
 TEST_GEN_FILES += $(YNL_GEN_FILES)
 TEST_GEN_PROGS += $(YNL_GEN_PROGS)
 
@@ -203,7 +204,7 @@ TEST_INCLUDES := forwarding/lib.sh
 include ../lib.mk
 
 # YNL build
-YNL_GENS := netdev
+YNL_GENS := netdev rt-route rt-addr rt-neigh rt-link
 include ynl.mk
 
 $(OUTPUT)/epoll_busy_poll: LDLIBS += -lcap
diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
index 128b0a5327d4..d9030bdd2e06 100644
--- a/tools/testing/selftests/net/tun.c
+++ b/tools/testing/selftests/net/tun.c
@@ -8,14 +8,12 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <linux/if.h>
 #include <linux/if_tun.h>
-#include <linux/netlink.h>
-#include <linux/rtnetlink.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 
 #include "kselftest_harness.h"
+#include "tuntap_helpers.h"
 
 static int tun_attach(int fd, char *dev)
 {
@@ -66,40 +64,7 @@ static int tun_alloc(char *dev)
 
 static int tun_delete(char *dev)
 {
-	struct {
-		struct nlmsghdr nh;
-		struct ifinfomsg ifm;
-		unsigned char data[64];
-	} req;
-	struct rtattr *rta;
-	int ret, rtnl;
-
-	rtnl = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
-	if (rtnl < 0) {
-		fprintf(stderr, "can't open rtnl: %s\n", strerror(errno));
-		return 1;
-	}
-
-	memset(&req, 0, sizeof(req));
-	req.nh.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(req.ifm)));
-	req.nh.nlmsg_flags = NLM_F_REQUEST;
-	req.nh.nlmsg_type = RTM_DELLINK;
-
-	req.ifm.ifi_family = AF_UNSPEC;
-
-	rta = (struct rtattr *)(((char *)&req) + NLMSG_ALIGN(req.nh.nlmsg_len));
-	rta->rta_type = IFLA_IFNAME;
-	rta->rta_len = RTA_LENGTH(IFNAMSIZ);
-	req.nh.nlmsg_len += rta->rta_len;
-	memcpy(RTA_DATA(rta), dev, IFNAMSIZ);
-
-	ret = send(rtnl, &req, req.nh.nlmsg_len, 0);
-	if (ret < 0)
-		fprintf(stderr, "can't send: %s\n", strerror(errno));
-	ret = (unsigned int)ret != req.nh.nlmsg_len;
-
-	close(rtnl);
-	return ret;
+	return ip_link_del(dev);
 }
 
 FIXTURE(tun)
-- 
2.49.0
Re: [PATCH net-next v5 3/7] selftest: tun: Refactor tun_delete to use tuntap_helpers
Posted by Jakub Kicinski 2 weeks, 5 days ago
On Mon, 19 Jan 2026 13:40:00 +0800 Xu Du wrote:
>  # YNL files, must be before "include ..lib.mk"
>  YNL_GEN_FILES := busy_poller
> -YNL_GEN_PROGS := netlink-dumps
> +YNL_GEN_PROGS := \
> +	netlink-dumps \
> +	tun \
>  TEST_GEN_FILES += $(YNL_GEN_FILES)

You're missing the terminating comment:

# end of TEST_GEN_PROGS

so the \ at the end makes the next line a part of YNL_GEN_PROGS.

Please try running this series thru:
https://github.com/linux-netdev/nipa/tree/main?tab=readme-ov-file#running-locally

Since it's a selftest-only series you can use:

  --disable-test patch/build_32bit patch/build_allmodconfig_warn \
                 patch/build_clang patch/build_clang_rust

to skip kernel builds which take rather long time.
-- 
pw-bot: cr
Re: [PATCH net-next v5 3/7] selftest: tun: Refactor tun_delete to use tuntap_helpers
Posted by Xu Du 2 weeks, 5 days ago
 Thanks for the guidance. I will work on resolving this issue.

-- 


Regards,

Xu


--

Xu Du

Quality Engineer, RHEL Network QE

Raycom, Beijing, China