Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

authored by

Marcus Wichelmann and committed by
Martin KaFai Lau
d5ca409c 0ca23a4d

+31 -29
+28
tools/testing/selftests/bpf/network_helpers.c
··· 548 548 free(token); 549 549 } 550 550 551 + int open_tuntap(const char *dev_name, bool need_mac) 552 + { 553 + int err = 0; 554 + struct ifreq ifr; 555 + int fd = open("/dev/net/tun", O_RDWR); 556 + 557 + if (!ASSERT_GT(fd, 0, "open(/dev/net/tun)")) 558 + return -1; 559 + 560 + ifr.ifr_flags = IFF_NO_PI | (need_mac ? IFF_TAP : IFF_TUN); 561 + strncpy(ifr.ifr_name, dev_name, IFNAMSIZ - 1); 562 + ifr.ifr_name[IFNAMSIZ - 1] = '\0'; 563 + 564 + err = ioctl(fd, TUNSETIFF, &ifr); 565 + if (!ASSERT_OK(err, "ioctl(TUNSETIFF)")) { 566 + close(fd); 567 + return -1; 568 + } 569 + 570 + err = fcntl(fd, F_SETFL, O_NONBLOCK); 571 + if (!ASSERT_OK(err, "fcntl(O_NONBLOCK)")) { 572 + close(fd); 573 + return -1; 574 + } 575 + 576 + return fd; 577 + } 578 + 551 579 int get_socket_local_port(int sock_fd) 552 580 { 553 581 struct sockaddr_storage addr;
+3
tools/testing/selftests/bpf/network_helpers.h
··· 8 8 typedef __u16 __sum16; 9 9 #include <linux/if_ether.h> 10 10 #include <linux/if_packet.h> 11 + #include <linux/if_tun.h> 11 12 #include <linux/ip.h> 12 13 #include <linux/ipv6.h> 13 14 #include <linux/ethtool.h> ··· 85 84 int get_socket_local_port(int sock_fd); 86 85 int get_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param); 87 86 int set_hw_ring_size(char *ifname, struct ethtool_ringparam *ring_param); 87 + 88 + int open_tuntap(const char *dev_name, bool need_mac); 88 89 89 90 struct nstoken; 90 91 /**
-29
tools/testing/selftests/bpf/prog_tests/lwt_helpers.h
··· 5 5 6 6 #include <time.h> 7 7 #include <net/if.h> 8 - #include <linux/if_tun.h> 9 8 #include <linux/icmp.h> 10 9 11 10 #include "test_progs.h" ··· 34 35 static inline int netns_delete(void) 35 36 { 36 37 return system("ip netns del " NETNS ">/dev/null 2>&1"); 37 - } 38 - 39 - static int open_tuntap(const char *dev_name, bool need_mac) 40 - { 41 - int err = 0; 42 - struct ifreq ifr; 43 - int fd = open("/dev/net/tun", O_RDWR); 44 - 45 - if (!ASSERT_GT(fd, 0, "open(/dev/net/tun)")) 46 - return -1; 47 - 48 - ifr.ifr_flags = IFF_NO_PI | (need_mac ? IFF_TAP : IFF_TUN); 49 - strncpy(ifr.ifr_name, dev_name, IFNAMSIZ - 1); 50 - ifr.ifr_name[IFNAMSIZ - 1] = '\0'; 51 - 52 - err = ioctl(fd, TUNSETIFF, &ifr); 53 - if (!ASSERT_OK(err, "ioctl(TUNSETIFF)")) { 54 - close(fd); 55 - return -1; 56 - } 57 - 58 - err = fcntl(fd, F_SETFL, O_NONBLOCK); 59 - if (!ASSERT_OK(err, "fcntl(O_NONBLOCK)")) { 60 - close(fd); 61 - return -1; 62 - } 63 - 64 - return fd; 65 38 } 66 39 67 40 #define ICMP_PAYLOAD_SIZE 100