diff options
author | Marcus Wichelmann <marcus.wichelmann@hetzner-cloud.de> | 2025-03-05 21:34:35 +0000 |
---|---|---|
committer | Martin KaFai Lau <martin.lau@kernel.org> | 2025-03-06 12:31:08 -0800 |
commit | d5ca409c86d35ebe60fad08ed554797abd8e53c0 (patch) | |
tree | c2f83d852be6ed8d5109515671c97bf744916ca4 /tools/testing/selftests/bpf/network_helpers.c | |
parent | 0ca23a4d64ce6db0ec1e1f66559c115eb100e426 (diff) |
selftests/bpf: Move open_tuntap to network helpers
To test the XDP metadata functionality of the tun driver, it's necessary
to create a new tap device first. A helper function for this already
exists in lwt_helpers.h. Move it to the common network helpers header,
so it can be reused in other tests.
Signed-off-by: Marcus Wichelmann <marcus.wichelmann@hetzner-cloud.de>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://patch.msgid.link/20250305213438.3863922-4-marcus.wichelmann@hetzner-cloud.de
Diffstat (limited to 'tools/testing/selftests/bpf/network_helpers.c')
-rw-r--r-- | tools/testing/selftests/bpf/network_helpers.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 80844a5fb1fe..fcee2c4a637a 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -548,6 +548,34 @@ void close_netns(struct nstoken *token) free(token); } +int open_tuntap(const char *dev_name, bool need_mac) +{ + int err = 0; + struct ifreq ifr; + int fd = open("/dev/net/tun", O_RDWR); + + if (!ASSERT_GT(fd, 0, "open(/dev/net/tun)")) + return -1; + + ifr.ifr_flags = IFF_NO_PI | (need_mac ? IFF_TAP : IFF_TUN); + strncpy(ifr.ifr_name, dev_name, IFNAMSIZ - 1); + ifr.ifr_name[IFNAMSIZ - 1] = '\0'; + + err = ioctl(fd, TUNSETIFF, &ifr); + if (!ASSERT_OK(err, "ioctl(TUNSETIFF)")) { + close(fd); + return -1; + } + + err = fcntl(fd, F_SETFL, O_NONBLOCK); + if (!ASSERT_OK(err, "fcntl(O_NONBLOCK)")) { + close(fd); + return -1; + } + + return fd; +} + int get_socket_local_port(int sock_fd) { struct sockaddr_storage addr; |