* [PATCH net-next 1/8] selftest: tun: Format tun.c existing code
2025-11-17 6:24 [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels xu du
@ 2025-11-17 6:24 ` xu du
2025-11-17 6:24 ` [PATCH net-next 2/8] selftest: tun: Introduce tuntap_helpers.h header for TUN/TAP testing xu du
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: xu du @ 2025-11-17 6:24 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni; +Cc: netdev
In preparation for adding new tests for GSO over UDP tunnels,
apply consistently the kernel style to the existing code.
Signed-off-by: xu du <xudu@redhat.com>
---
tools/testing/selftests/net/tun.c | 25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)
diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
index fa83918b62d1..038051346714 100644
--- a/tools/testing/selftests/net/tun.c
+++ b/tools/testing/selftests/net/tun.c
@@ -25,7 +25,7 @@ static int tun_attach(int fd, char *dev)
strcpy(ifr.ifr_name, dev);
ifr.ifr_flags = IFF_ATTACH_QUEUE;
- return ioctl(fd, TUNSETQUEUE, (void *) &ifr);
+ return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
}
static int tun_detach(int fd, char *dev)
@@ -36,7 +36,7 @@ static int tun_detach(int fd, char *dev)
strcpy(ifr.ifr_name, dev);
ifr.ifr_flags = IFF_DETACH_QUEUE;
- return ioctl(fd, TUNSETQUEUE, (void *) &ifr);
+ return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
}
static int tun_alloc(char *dev)
@@ -54,7 +54,7 @@ static int tun_alloc(char *dev)
strcpy(ifr.ifr_name, dev);
ifr.ifr_flags = IFF_TAP | IFF_NAPI | IFF_MULTI_QUEUE;
- err = ioctl(fd, TUNSETIFF, (void *) &ifr);
+ err = ioctl(fd, TUNSETIFF, (void *)&ifr);
if (err < 0) {
fprintf(stderr, "can't TUNSETIFF: %s\n", strerror(errno));
close(fd);
@@ -67,9 +67,9 @@ static int tun_alloc(char *dev)
static int tun_delete(char *dev)
{
struct {
- struct nlmsghdr nh;
+ struct nlmsghdr nh;
struct ifinfomsg ifm;
- unsigned char data[64];
+ unsigned char data[64];
} req;
struct rtattr *rta;
int ret, rtnl;
@@ -127,31 +127,36 @@ FIXTURE_TEARDOWN(tun)
close(self->fd2);
}
-TEST_F(tun, delete_detach_close) {
+TEST_F(tun, delete_detach_close)
+{
EXPECT_EQ(tun_delete(self->ifname), 0);
EXPECT_EQ(tun_detach(self->fd, self->ifname), -1);
EXPECT_EQ(errno, 22);
}
-TEST_F(tun, detach_delete_close) {
+TEST_F(tun, detach_delete_close)
+{
EXPECT_EQ(tun_detach(self->fd, self->ifname), 0);
EXPECT_EQ(tun_delete(self->ifname), 0);
}
-TEST_F(tun, detach_close_delete) {
+TEST_F(tun, detach_close_delete)
+{
EXPECT_EQ(tun_detach(self->fd, self->ifname), 0);
close(self->fd);
self->fd = -1;
EXPECT_EQ(tun_delete(self->ifname), 0);
}
-TEST_F(tun, reattach_delete_close) {
+TEST_F(tun, reattach_delete_close)
+{
EXPECT_EQ(tun_detach(self->fd, self->ifname), 0);
EXPECT_EQ(tun_attach(self->fd, self->ifname), 0);
EXPECT_EQ(tun_delete(self->ifname), 0);
}
-TEST_F(tun, reattach_close_delete) {
+TEST_F(tun, reattach_close_delete)
+{
EXPECT_EQ(tun_detach(self->fd, self->ifname), 0);
EXPECT_EQ(tun_attach(self->fd, self->ifname), 0);
close(self->fd);
--
2.49.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH net-next 2/8] selftest: tun: Introduce tuntap_helpers.h header for TUN/TAP testing
2025-11-17 6:24 [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels xu du
2025-11-17 6:24 ` [PATCH net-next 1/8] selftest: tun: Format tun.c existing code xu du
@ 2025-11-17 6:24 ` xu du
2025-11-17 6:24 ` [PATCH net-next 3/8] selftest: tun: Refactor tun_delete to use tuntap_helpers xu du
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: xu du @ 2025-11-17 6:24 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni; +Cc: netdev
Introduce device manipulation and packet construction helpers that will
simplify the later creation of more related test cases. This avoids
duplicating logic across different test cases.
This new header will contain:
- Netlink socket management utilities.
- Helpers for net device, IP address, and neighbor configuration.
- Packet construction and manipulation helpers.
Signed-off-by: xu du <xudu@redhat.com>
---
tools/testing/selftests/net/tuntap_helpers.h | 495 +++++++++++++++++++
1 file changed, 495 insertions(+)
create mode 100644 tools/testing/selftests/net/tuntap_helpers.h
diff --git a/tools/testing/selftests/net/tuntap_helpers.h b/tools/testing/selftests/net/tuntap_helpers.h
new file mode 100644
index 000000000000..2b0ef8b77100
--- /dev/null
+++ b/tools/testing/selftests/net/tuntap_helpers.h
@@ -0,0 +1,495 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef _TUNTAP_HELPERS_H
+#define _TUNTAP_HELPERS_H
+
+#include <errno.h>
+#include <linux/if_packet.h>
+#include <linux/ipv6.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#include <linux/virtio_net.h>
+#include <net/if.h>
+#include <netinet/if_ether.h>
+#include <netinet/ip.h>
+#include <netinet/udp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#define MAX_RTNL_PAYLOAD (2048)
+#define VXLAN_HLEN 8
+#define PKT_DATA 0xCB
+
+enum nl_op {
+ NL_NEW,
+ NL_DEL,
+};
+
+enum nl_type {
+ NL_ADDR,
+ NL_NEIGH,
+ NL_DEV,
+};
+
+struct nl_req_msg {
+ struct nlmsghdr nh;
+ union {
+ struct ifaddrmsg addr_info;
+ struct ndmsg neigh_info;
+ struct ifinfomsg dev_info;
+ };
+ unsigned char data[MAX_RTNL_PAYLOAD];
+};
+
+struct nl_req_answer {
+ struct nlmsghdr hdr;
+ int error;
+ struct nlmsghdr orig_msg;
+};
+
+struct nl_req_entry {
+ int newtype;
+ int deltype;
+ size_t msgsize;
+ int (*fill_req)(int nl_op, void *params, struct nl_req_msg *req,
+ void **cb, void **cb_data);
+};
+
+struct nl_addr {
+ const char *intf;
+ int family;
+ void *addr;
+ uint8_t prefix;
+};
+
+struct nl_neigh {
+ const char *intf;
+ int family;
+ void *addr;
+ unsigned char *lladdr;
+};
+
+struct nl_dev {
+ const char *intf;
+ char *link_type;
+ int (*fill_rtattr)(struct nlmsghdr *nh, void *cb_data);
+ int (*fill_info_data)(struct nlmsghdr *nh, void *cb_data);
+ void *cb_data;
+};
+
+static inline size_t ip_addr_len(int family)
+{
+ return (family == AF_INET) ? sizeof(struct in_addr) :
+ sizeof(struct in6_addr);
+}
+
+static inline struct rtattr *rtattr_add(struct nlmsghdr *nh,
+ unsigned short type, unsigned short len)
+{
+ struct rtattr *rta =
+ (struct rtattr *)((uint8_t *)nh + RTA_ALIGN(nh->nlmsg_len));
+
+ rta->rta_type = type;
+ rta->rta_len = RTA_LENGTH(len);
+ nh->nlmsg_len = RTA_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
+ return rta;
+}
+
+static inline struct rtattr *rtattr_begin(struct nlmsghdr *nh,
+ unsigned short type)
+{
+ return rtattr_add(nh, type, 0);
+}
+
+static inline void rtattr_end(struct nlmsghdr *nh, struct rtattr *attr)
+{
+ uint8_t *end = (uint8_t *)nh + nh->nlmsg_len;
+
+ attr->rta_len = end - (uint8_t *)attr;
+}
+
+static inline struct rtattr *rtattr_add_str(struct nlmsghdr *nh,
+ unsigned short type, const char *s)
+{
+ struct rtattr *rta = rtattr_add(nh, type, strlen(s));
+
+ memcpy(RTA_DATA(rta), s, strlen(s));
+ return rta;
+}
+
+static inline struct rtattr *
+rtattr_add_strsz(struct nlmsghdr *nh, unsigned short type, const char *s)
+{
+ struct rtattr *rta = rtattr_add(nh, type, strlen(s) + 1);
+
+ strcpy(RTA_DATA(rta), s);
+ return rta;
+}
+
+static inline struct rtattr *rtattr_add_any(struct nlmsghdr *nh,
+ unsigned short type,
+ const void *arr, size_t len)
+{
+ struct rtattr *rta = rtattr_add(nh, type, len);
+
+ memcpy(RTA_DATA(rta), arr, len);
+ return rta;
+}
+
+static inline int fill_addr_req(int nl_op, void *params, struct nl_req_msg *req,
+ void **cb, void **cb_data)
+{
+ struct nl_addr *addrp = params;
+ size_t ipalen = ip_addr_len(addrp->family);
+
+ req->addr_info.ifa_family = addrp->family;
+ req->addr_info.ifa_prefixlen = addrp->prefix;
+ req->addr_info.ifa_index = if_nametoindex(addrp->intf);
+ req->addr_info.ifa_flags = (nl_op == NL_NEW) ? IFA_F_NODAD : 0;
+
+ rtattr_add_any(&req->nh, IFA_LOCAL, addrp->addr, ipalen);
+ if (nl_op == NL_NEW && addrp->addr)
+ rtattr_add_any(&req->nh, IFA_ADDRESS, addrp->addr, ipalen);
+ return 0;
+}
+
+static inline int fill_neigh_req(int nl_op, void *params,
+ struct nl_req_msg *req, void **cb,
+ void **cb_data)
+{
+ struct nl_neigh *neighp = params;
+ size_t ipalen = ip_addr_len(neighp->family);
+
+ req->neigh_info.ndm_family = neighp->family;
+ req->neigh_info.ndm_ifindex = if_nametoindex(neighp->intf);
+ req->neigh_info.ndm_state = (nl_op == NL_NEW) ? NUD_PERMANENT : 0;
+
+ rtattr_add_any(&req->nh, NDA_DST, neighp->addr, ipalen);
+ if (nl_op == NL_NEW && neighp->lladdr)
+ rtattr_add_any(&req->nh, NDA_LLADDR, neighp->lladdr, ETH_ALEN);
+ return 0;
+}
+
+static inline int fill_dev_req(int nl_op, void *params, struct nl_req_msg *req,
+ void **cb, void **cb_data)
+{
+ struct rtattr *link_info, *info_data;
+ struct nl_dev *devp = params;
+ int ret;
+
+ *cb = devp->fill_rtattr;
+ *cb_data = devp->cb_data;
+
+ req->dev_info.ifi_family = AF_UNSPEC;
+ req->dev_info.ifi_type = 1;
+ req->dev_info.ifi_index = 0;
+ req->dev_info.ifi_flags = (nl_op == NL_NEW) ? (IFF_BROADCAST | IFF_UP) :
+ 0;
+ req->dev_info.ifi_change = 0xffffffff;
+
+ rtattr_add_str(&req->nh, IFLA_IFNAME, devp->intf);
+
+ if (nl_op == NL_NEW) {
+ link_info = rtattr_begin(&req->nh, IFLA_LINKINFO);
+ rtattr_add_strsz(&req->nh, IFLA_INFO_KIND, devp->link_type);
+
+ if (devp->fill_info_data) {
+ info_data = rtattr_begin(&req->nh, IFLA_INFO_DATA);
+ ret = devp->fill_info_data(&req->nh, *cb_data);
+ if (ret)
+ return ret;
+ rtattr_end(&req->nh, info_data);
+ }
+ rtattr_end(&req->nh, link_info);
+ }
+ return 0;
+}
+
+static const struct nl_req_entry nl_req_tbl[] = {
+ [NL_ADDR] = { RTM_NEWADDR, RTM_DELADDR, sizeof(struct ifaddrmsg),
+ fill_addr_req },
+ [NL_NEIGH] = { RTM_NEWNEIGH, RTM_DELNEIGH, sizeof(struct ndmsg),
+ fill_neigh_req },
+ [NL_DEV] = { RTM_NEWLINK, RTM_DELLINK, sizeof(struct ifinfomsg),
+ fill_dev_req },
+};
+
+static inline int fill_nl_req_msg(int nl_op, int nl_type, void *params,
+ struct nl_req_msg *req)
+{
+ int (*fill_rtattr)(struct nlmsghdr *, void *) = NULL;
+ void *cb_data = NULL;
+ int ret;
+
+ req->nh.nlmsg_type = (nl_op == NL_NEW) ? nl_req_tbl[nl_type].newtype :
+ nl_req_tbl[nl_type].deltype;
+ req->nh.nlmsg_flags =
+ ((nl_op == NL_NEW) ? (NLM_F_CREATE | NLM_F_EXCL) : 0) |
+ NLM_F_REQUEST | NLM_F_ACK;
+ req->nh.nlmsg_len = NLMSG_LENGTH(nl_req_tbl[nl_type].msgsize);
+
+ ret = nl_req_tbl[nl_type].fill_req(nl_op, params, req,
+ (void **)&fill_rtattr, &cb_data);
+ if (ret)
+ return ret;
+
+ if (fill_rtattr) {
+ ret = fill_rtattr(&req->nh, cb_data);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+static inline int netlink_op(int nl_op, int nl_type, void *params)
+{
+ struct nl_req_answer answer;
+ struct nl_req_msg req;
+ int rtnl, ret;
+
+ rtnl = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
+ if (rtnl < 0) {
+ perror("socket");
+ return 1;
+ }
+
+ ret = fill_nl_req_msg(nl_op, nl_type, params, &req);
+ if (ret)
+ goto out;
+
+ ret = send(rtnl, &req, req.nh.nlmsg_len, 0);
+ if (ret < 0) {
+ perror("send");
+ goto out;
+ }
+
+ ret = recv(rtnl, &answer, sizeof(answer), 0);
+ if (ret < 0) {
+ perror("recv");
+ goto out;
+ } else if (answer.hdr.nlmsg_type != NLMSG_ERROR) {
+ ret = -1;
+ goto out;
+ } else if (answer.error) {
+ ret = answer.error;
+ goto out;
+ }
+ ret = 0;
+
+out:
+ close(rtnl);
+ return ret;
+}
+
+static inline int ip_addr_add(const char *intf, int family, void *addr,
+ uint8_t prefix)
+{
+ struct nl_addr param = { intf, family, addr, prefix };
+
+ return netlink_op(NL_NEW, NL_ADDR, ¶m);
+}
+
+static inline int ip_neigh_add(const char *intf, int family, void *addr,
+ unsigned char *lladdr)
+{
+ struct nl_neigh param = { intf, family, addr, lladdr };
+
+ return netlink_op(NL_NEW, NL_NEIGH, ¶m);
+}
+
+static inline int
+dev_create(const char *dev, char *link_type,
+ int (*fill_rtattr)(struct nlmsghdr *nh, void *cb_data),
+ int (*fill_info_data)(struct nlmsghdr *nh, void *cb_data),
+ void *cb_data)
+{
+ struct nl_dev param = { dev, link_type, fill_rtattr, fill_info_data,
+ cb_data };
+
+ return netlink_op(NL_NEW, NL_DEV, ¶m);
+}
+
+static inline int dev_delete(const char *dev)
+{
+ struct nl_dev param = { dev, NULL, NULL, NULL, NULL };
+
+ return netlink_op(NL_DEL, NL_DEV, ¶m);
+}
+
+static inline size_t build_eth(uint8_t *buf, uint16_t proto, unsigned char *src,
+ unsigned char *dest)
+{
+ struct ethhdr *eth = (struct ethhdr *)buf;
+
+ eth->h_proto = htons(proto);
+ memcpy(eth->h_source, src, ETH_ALEN);
+ memcpy(eth->h_dest, dest, ETH_ALEN);
+
+ return ETH_HLEN;
+}
+
+static inline uint32_t add_csum(const uint8_t *buf, int len)
+{
+ uint32_t sum = 0;
+ uint16_t *sbuf = (uint16_t *)buf;
+
+ while (len > 1) {
+ sum += *sbuf++;
+ len -= 2;
+ }
+
+ if (len)
+ sum += *(uint8_t *)sbuf;
+
+ return sum;
+}
+
+static inline uint16_t finish_ip_csum(uint32_t sum)
+{
+ while (sum >> 16)
+ sum = (sum & 0xffff) + (sum >> 16);
+ return ~((uint16_t)sum);
+}
+
+static inline uint16_t build_ip_csum(const uint8_t *buf, int len, uint32_t sum)
+{
+ sum += add_csum(buf, len);
+ return finish_ip_csum(sum);
+}
+
+static inline int build_ipv4_header(uint8_t *buf, uint8_t proto,
+ int payload_len, struct in_addr *src,
+ struct in_addr *dst)
+{
+ struct iphdr *iph = (struct iphdr *)buf;
+
+ iph->ihl = 5;
+ iph->version = 4;
+ iph->ttl = 8;
+ iph->tot_len = htons(sizeof(*iph) + payload_len);
+ iph->id = htons(1337);
+ iph->protocol = proto;
+ iph->saddr = src->s_addr;
+ iph->daddr = dst->s_addr;
+ iph->check = build_ip_csum(buf, iph->ihl << 2, 0);
+
+ return iph->ihl << 2;
+}
+
+static inline void ipv6_set_dsfield(struct ipv6hdr *ip6h, uint8_t dsfield)
+{
+ uint16_t val, *ptr = (uint16_t *)ip6h;
+
+ val = ntohs(*ptr);
+ val &= 0xF00F;
+ val |= ((uint16_t)dsfield) << 4;
+ *ptr = htons(val);
+}
+
+static inline int build_ipv6_header(uint8_t *buf, uint8_t proto,
+ uint8_t dsfield, int payload_len,
+ struct in6_addr *src, struct in6_addr *dst)
+{
+ struct ipv6hdr *ip6h = (struct ipv6hdr *)buf;
+
+ ip6h->version = 6;
+ ip6h->payload_len = htons(payload_len);
+ ip6h->nexthdr = proto;
+ ip6h->hop_limit = 8;
+ ipv6_set_dsfield(ip6h, dsfield);
+ memcpy(&ip6h->saddr, src, sizeof(ip6h->saddr));
+ memcpy(&ip6h->daddr, dst, sizeof(ip6h->daddr));
+
+ return sizeof(struct ipv6hdr);
+}
+
+static inline int build_vxlan_header(uint8_t *buf, uint32_t vni)
+{
+ uint32_t vx_flags = htonl(0x08000000);
+ uint32_t vx_vni = htonl((vni << 8) & 0xffffff00);
+
+ memcpy(buf, &vx_flags, 4);
+ memcpy(buf + 4, &vx_vni, 4);
+ return VXLAN_HLEN;
+}
+
+static inline int build_udp_header(uint8_t *buf, uint16_t sport, uint16_t dport,
+ int payload_len)
+{
+ struct udphdr *udph = (struct udphdr *)buf;
+
+ udph->source = htons(sport);
+ udph->dest = htons(dport);
+ udph->len = htons(sizeof(*udph) + payload_len);
+ return sizeof(*udph);
+}
+
+static inline void build_udp_packet_csum(uint8_t *buf, int family,
+ bool csum_off)
+{
+ struct udphdr *udph = (struct udphdr *)buf;
+ int ipalen = ip_addr_len(family);
+ uint32_t sum;
+
+ /* No extension IPv4 and IPv6 headers addresses are the last fields */
+ sum = add_csum(buf - 2 * ipalen, 2 * ipalen);
+ sum += htons(IPPROTO_UDP) + udph->len;
+
+ if (!csum_off)
+ sum += add_csum(buf, udph->len);
+
+ udph->check = finish_ip_csum(sum);
+}
+
+static inline int build_udp_packet(uint8_t *buf, uint16_t sport, uint16_t dport,
+ int payload_len, int family, bool csum_off)
+{
+ struct udphdr *udph = (struct udphdr *)buf;
+
+ build_udp_header(buf, sport, dport, payload_len);
+ memset(buf + sizeof(*udph), PKT_DATA, payload_len);
+ build_udp_packet_csum(buf, family, csum_off);
+
+ return sizeof(*udph) + payload_len;
+}
+
+static inline int build_virtio_net_hdr_v1_hash_tunnel(uint8_t *buf, bool is_tap,
+ int hdr_len, int gso_size,
+ int outer_family,
+ int inner_family)
+{
+ struct virtio_net_hdr_v1_hash_tunnel *vh_tunnel = (void *)buf;
+ struct virtio_net_hdr_v1 *vh = &vh_tunnel->hash_hdr.hdr;
+ int outer_iphlen, inner_iphlen, eth_hlen, gso_type;
+
+ eth_hlen = is_tap ? ETH_HLEN : 0;
+ outer_iphlen = (outer_family == AF_INET) ? sizeof(struct iphdr) :
+ sizeof(struct ipv6hdr);
+ inner_iphlen = (inner_family == AF_INET) ? sizeof(struct iphdr) :
+ sizeof(struct ipv6hdr);
+
+ vh_tunnel->outer_th_offset = eth_hlen + outer_iphlen;
+ vh_tunnel->inner_nh_offset = vh_tunnel->outer_th_offset + ETH_HLEN +
+ VXLAN_HLEN + sizeof(struct udphdr);
+
+ vh->csum_start = vh_tunnel->inner_nh_offset + inner_iphlen;
+ vh->csum_offset = __builtin_offsetof(struct udphdr, check);
+ vh->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
+ vh->hdr_len = hdr_len;
+ vh->gso_size = gso_size;
+
+ if (gso_size) {
+ gso_type = outer_family == AF_INET ?
+ VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV4 :
+ VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV6;
+ vh->gso_type = VIRTIO_NET_HDR_GSO_UDP_L4 | gso_type;
+ }
+
+ return sizeof(struct virtio_net_hdr_v1_hash_tunnel);
+}
+
+#endif /* _TUNTAP_HELPERS_H */
--
2.49.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH net-next 3/8] selftest: tun: Refactor tun_delete to use tuntap_helpers
2025-11-17 6:24 [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels xu du
2025-11-17 6:24 ` [PATCH net-next 1/8] selftest: tun: Format tun.c existing code xu du
2025-11-17 6:24 ` [PATCH net-next 2/8] selftest: tun: Introduce tuntap_helpers.h header for TUN/TAP testing xu du
@ 2025-11-17 6:24 ` xu du
2025-11-17 6:25 ` [PATCH net-next 4/8] selftest: tap: Refactor tap test " xu du
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: xu du @ 2025-11-17 6:24 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni; +Cc: netdev
The previous patch introduced common tuntap helpers to simplify
tuntap test code. This patch refactors the tun_delete function
to use these new helpers.
Signed-off-by: xu du <xudu@redhat.com>
---
tools/testing/selftests/net/tun.c | 39 ++-----------------------------
1 file changed, 2 insertions(+), 37 deletions(-)
diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
index 038051346714..2ed439cce423 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 dev_delete(dev);
}
FIXTURE(tun)
--
2.49.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH net-next 4/8] selftest: tap: Refactor tap test to use tuntap_helpers
2025-11-17 6:24 [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels xu du
` (2 preceding siblings ...)
2025-11-17 6:24 ` [PATCH net-next 3/8] selftest: tun: Refactor tun_delete to use tuntap_helpers xu du
@ 2025-11-17 6:25 ` xu du
2025-11-17 6:25 ` [PATCH net-next 5/8] selftest: tun: Add helpers for GSO over UDP tunnel xu du
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: xu du @ 2025-11-17 6:25 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni; +Cc: netdev
Refactor existing TAP test cases to use the previously introduced
tuntap_helpers from tuntap_helpers.h for device setup, packet
construction, and cleanup.
This reduces code duplication, improves test maintainability, and
ensures consistent behavior across test scenarios.
Signed-off-by: xu du <xudu@redhat.com>
---
tools/testing/selftests/net/tap.c | 286 +++++-------------------------
1 file changed, 40 insertions(+), 246 deletions(-)
diff --git a/tools/testing/selftests/net/tap.c b/tools/testing/selftests/net/tap.c
index 247c3b3ac1c9..87dad3f39355 100644
--- a/tools/testing/selftests/net/tap.c
+++ b/tools/testing/selftests/net/tap.c
@@ -10,14 +10,13 @@
#include <unistd.h>
#include <net/if.h>
#include <linux/if_tun.h>
-#include <linux/netlink.h>
-#include <linux/rtnetlink.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/virtio_net.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include "../kselftest_harness.h"
+#include "tuntap_helpers.h"
static const char param_dev_tap_name[] = "xmacvtap0";
static const char param_dev_dummy_name[] = "xdummy0";
@@ -26,167 +25,35 @@ static unsigned char param_hwaddr_dest[] = {
0x00, 0xfe, 0x98, 0x94, 0xd2, 0x43
};
-#define MAX_RTNL_PAYLOAD (2048)
-#define PKT_DATA 0xCB
-#define TEST_PACKET_SZ (sizeof(struct virtio_net_hdr) + ETH_HLEN + ETH_MAX_MTU)
-
-static struct rtattr *rtattr_add(struct nlmsghdr *nh, unsigned short type,
- unsigned short len)
-{
- struct rtattr *rta =
- (struct rtattr *)((uint8_t *)nh + RTA_ALIGN(nh->nlmsg_len));
- rta->rta_type = type;
- rta->rta_len = RTA_LENGTH(len);
- nh->nlmsg_len = RTA_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
- return rta;
-}
-
-static struct rtattr *rtattr_begin(struct nlmsghdr *nh, unsigned short type)
-{
- return rtattr_add(nh, type, 0);
-}
-
-static void rtattr_end(struct nlmsghdr *nh, struct rtattr *attr)
-{
- uint8_t *end = (uint8_t *)nh + nh->nlmsg_len;
-
- attr->rta_len = end - (uint8_t *)attr;
-}
-
-static struct rtattr *rtattr_add_str(struct nlmsghdr *nh, unsigned short type,
- const char *s)
-{
- struct rtattr *rta = rtattr_add(nh, type, strlen(s));
-
- memcpy(RTA_DATA(rta), s, strlen(s));
- return rta;
-}
-
-static struct rtattr *rtattr_add_strsz(struct nlmsghdr *nh, unsigned short type,
- const char *s)
-{
- struct rtattr *rta = rtattr_add(nh, type, strlen(s) + 1);
-
- strcpy(RTA_DATA(rta), s);
- return rta;
-}
-
-static struct rtattr *rtattr_add_any(struct nlmsghdr *nh, unsigned short type,
- const void *arr, size_t len)
-{
- struct rtattr *rta = rtattr_add(nh, type, len);
-
- memcpy(RTA_DATA(rta), arr, len);
- return rta;
-}
-
-static int dev_create(const char *dev, const char *link_type,
- int (*fill_rtattr)(struct nlmsghdr *nh),
- int (*fill_info_data)(struct nlmsghdr *nh))
-{
- struct {
- struct nlmsghdr nh;
- struct ifinfomsg info;
- unsigned char data[MAX_RTNL_PAYLOAD];
- } req;
- struct rtattr *link_info, *info_data;
- int ret, rtnl;
-
- rtnl = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
- if (rtnl < 0) {
- fprintf(stderr, "%s: socket %s\n", __func__, strerror(errno));
- return 1;
- }
-
- memset(&req, 0, sizeof(req));
- req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.info));
- req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
- req.nh.nlmsg_type = RTM_NEWLINK;
-
- req.info.ifi_family = AF_UNSPEC;
- req.info.ifi_type = 1;
- req.info.ifi_index = 0;
- req.info.ifi_flags = IFF_BROADCAST | IFF_UP;
- req.info.ifi_change = 0xffffffff;
-
- rtattr_add_str(&req.nh, IFLA_IFNAME, dev);
-
- if (fill_rtattr) {
- ret = fill_rtattr(&req.nh);
- if (ret)
- return ret;
- }
-
- link_info = rtattr_begin(&req.nh, IFLA_LINKINFO);
-
- rtattr_add_strsz(&req.nh, IFLA_INFO_KIND, link_type);
-
- if (fill_info_data) {
- info_data = rtattr_begin(&req.nh, IFLA_INFO_DATA);
- ret = fill_info_data(&req.nh);
- if (ret)
- return ret;
- rtattr_end(&req.nh, info_data);
- }
-
- rtattr_end(&req.nh, link_info);
-
- ret = send(rtnl, &req, req.nh.nlmsg_len, 0);
- if (ret < 0)
- fprintf(stderr, "%s: send %s\n", __func__, strerror(errno));
- ret = (unsigned int)ret != req.nh.nlmsg_len;
-
- close(rtnl);
- return ret;
-}
-
-static int dev_delete(const char *dev)
-{
- struct {
- struct nlmsghdr nh;
- struct ifinfomsg info;
- unsigned char data[MAX_RTNL_PAYLOAD];
- } req;
- int ret, rtnl;
-
- rtnl = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
- if (rtnl < 0) {
- fprintf(stderr, "%s: socket %s\n", __func__, strerror(errno));
- return 1;
- }
-
- memset(&req, 0, sizeof(req));
- req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.info));
- req.nh.nlmsg_flags = NLM_F_REQUEST;
- req.nh.nlmsg_type = RTM_DELLINK;
-
- req.info.ifi_family = AF_UNSPEC;
-
- rtattr_add_str(&req.nh, IFLA_IFNAME, dev);
+static struct in_addr param_ipaddr_src = {
+ __constant_htonl(0xac110002),
+};
- ret = send(rtnl, &req, req.nh.nlmsg_len, 0);
- if (ret < 0)
- fprintf(stderr, "%s: send %s\n", __func__, strerror(errno));
+static struct in_addr param_ipaddr_dst = {
+ __constant_htonl(0xac110001),
+};
- ret = (unsigned int)ret != req.nh.nlmsg_len;
+#define UDP_SRC_PORT 22
+#define UDP_DST_PORT 58822
+#define TEST_PACKET_SZ (sizeof(struct virtio_net_hdr) + ETH_HLEN + ETH_MAX_MTU)
- close(rtnl);
- return ret;
-}
+struct mactap_setup_config {
+ char name[IFNAMSIZ];
+ unsigned char hwaddr[ETH_ALEN];
+};
-static int macvtap_fill_rtattr(struct nlmsghdr *nh)
+static int macvtap_fill_rtattr(struct nlmsghdr *nh, void *data)
{
+ struct mactap_setup_config *mactap = data;
int ifindex;
- ifindex = if_nametoindex(param_dev_dummy_name);
+ ifindex = if_nametoindex(mactap->name);
if (ifindex == 0) {
fprintf(stderr, "%s: ifindex %s\n", __func__, strerror(errno));
return -errno;
}
rtattr_add_any(nh, IFLA_LINK, &ifindex, sizeof(ifindex));
- rtattr_add_any(nh, IFLA_ADDRESS, param_hwaddr_src, ETH_ALEN);
-
return 0;
}
@@ -218,91 +85,6 @@ static int opentap(const char *devname)
return fd;
}
-size_t build_eth(uint8_t *buf, uint16_t proto)
-{
- struct ethhdr *eth = (struct ethhdr *)buf;
-
- eth->h_proto = htons(proto);
- memcpy(eth->h_source, param_hwaddr_src, ETH_ALEN);
- memcpy(eth->h_dest, param_hwaddr_dest, ETH_ALEN);
-
- return ETH_HLEN;
-}
-
-static uint32_t add_csum(const uint8_t *buf, int len)
-{
- uint32_t sum = 0;
- uint16_t *sbuf = (uint16_t *)buf;
-
- while (len > 1) {
- sum += *sbuf++;
- len -= 2;
- }
-
- if (len)
- sum += *(uint8_t *)sbuf;
-
- return sum;
-}
-
-static uint16_t finish_ip_csum(uint32_t sum)
-{
- uint16_t lo = sum & 0xffff;
- uint16_t hi = sum >> 16;
-
- return ~(lo + hi);
-
-}
-
-static uint16_t build_ip_csum(const uint8_t *buf, int len,
- uint32_t sum)
-{
- sum += add_csum(buf, len);
- return finish_ip_csum(sum);
-}
-
-static int build_ipv4_header(uint8_t *buf, int payload_len)
-{
- struct iphdr *iph = (struct iphdr *)buf;
-
- iph->ihl = 5;
- iph->version = 4;
- iph->ttl = 8;
- iph->tot_len =
- htons(sizeof(*iph) + sizeof(struct udphdr) + payload_len);
- iph->id = htons(1337);
- iph->protocol = IPPROTO_UDP;
- iph->saddr = htonl((172 << 24) | (17 << 16) | 2);
- iph->daddr = htonl((172 << 24) | (17 << 16) | 1);
- iph->check = build_ip_csum(buf, iph->ihl << 2, 0);
-
- return iph->ihl << 2;
-}
-
-static int build_udp_packet(uint8_t *buf, int payload_len, bool csum_off)
-{
- const int ip4alen = sizeof(uint32_t);
- struct udphdr *udph = (struct udphdr *)buf;
- int len = sizeof(*udph) + payload_len;
- uint32_t sum = 0;
-
- udph->source = htons(22);
- udph->dest = htons(58822);
- udph->len = htons(len);
-
- memset(buf + sizeof(struct udphdr), PKT_DATA, payload_len);
-
- sum = add_csum(buf - 2 * ip4alen, 2 * ip4alen);
- sum += htons(IPPROTO_UDP) + udph->len;
-
- if (!csum_off)
- sum += add_csum(buf, len);
-
- udph->check = finish_ip_csum(sum);
-
- return sizeof(*udph) + payload_len;
-}
-
size_t build_test_packet_valid_udp_gso(uint8_t *buf, size_t payload_len)
{
uint8_t *cur = buf;
@@ -316,9 +98,12 @@ size_t build_test_packet_valid_udp_gso(uint8_t *buf, size_t payload_len)
vh->gso_size = ETH_DATA_LEN - sizeof(struct iphdr);
cur += sizeof(*vh);
- cur += build_eth(cur, ETH_P_IP);
- cur += build_ipv4_header(cur, payload_len);
- cur += build_udp_packet(cur, payload_len, true);
+ cur += build_eth(cur, ETH_P_IP, param_hwaddr_dest, param_hwaddr_src);
+ cur += build_ipv4_header(cur, IPPROTO_UDP,
+ payload_len + sizeof(struct udphdr),
+ ¶m_ipaddr_dst, ¶m_ipaddr_src);
+ cur += build_udp_packet(cur, UDP_SRC_PORT, UDP_DST_PORT, payload_len,
+ AF_INET, true);
return cur - buf;
}
@@ -332,9 +117,12 @@ size_t build_test_packet_valid_udp_csum(uint8_t *buf, size_t payload_len)
vh->gso_type = VIRTIO_NET_HDR_GSO_NONE;
cur += sizeof(*vh);
- cur += build_eth(cur, ETH_P_IP);
- cur += build_ipv4_header(cur, payload_len);
- cur += build_udp_packet(cur, payload_len, false);
+ cur += build_eth(cur, ETH_P_IP, param_hwaddr_dest, param_hwaddr_src);
+ cur += build_ipv4_header(cur, IPPROTO_UDP,
+ payload_len + sizeof(struct udphdr),
+ ¶m_ipaddr_dst, ¶m_ipaddr_src);
+ cur += build_udp_packet(cur, UDP_SRC_PORT, UDP_DST_PORT, payload_len,
+ AF_INET, false);
return cur - buf;
}
@@ -351,10 +139,13 @@ size_t build_test_packet_crash_tap_invalid_eth_proto(uint8_t *buf,
vh->gso_size = ETH_DATA_LEN - sizeof(struct iphdr);
cur += sizeof(*vh);
- cur += build_eth(cur, 0);
+ cur += build_eth(cur, ETH_P_IP, param_hwaddr_dest, param_hwaddr_src);
cur += sizeof(struct iphdr) + sizeof(struct udphdr);
- cur += build_ipv4_header(cur, payload_len);
- cur += build_udp_packet(cur, payload_len, true);
+ cur += build_ipv4_header(cur, IPPROTO_UDP,
+ payload_len + sizeof(struct udphdr),
+ ¶m_ipaddr_dst, ¶m_ipaddr_src);
+ cur += build_udp_packet(cur, UDP_SRC_PORT, UDP_DST_PORT, payload_len,
+ AF_INET, true);
cur += payload_len;
return cur - buf;
@@ -368,12 +159,15 @@ FIXTURE(tap)
FIXTURE_SETUP(tap)
{
int ret;
+ struct mactap_setup_config mactap_config;
- ret = dev_create(param_dev_dummy_name, "dummy", NULL, NULL);
+ ret = dev_create(param_dev_dummy_name, "dummy", NULL, NULL, NULL);
EXPECT_EQ(ret, 0);
+ strcpy(mactap_config.name, param_dev_dummy_name);
+ memcpy(mactap_config.hwaddr, param_hwaddr_src, ETH_ALEN);
ret = dev_create(param_dev_tap_name, "macvtap", macvtap_fill_rtattr,
- NULL);
+ NULL, &mactap_config);
EXPECT_EQ(ret, 0);
self->fd = opentap(param_dev_tap_name);
--
2.49.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH net-next 5/8] selftest: tun: Add helpers for GSO over UDP tunnel
2025-11-17 6:24 [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels xu du
` (3 preceding siblings ...)
2025-11-17 6:25 ` [PATCH net-next 4/8] selftest: tap: Refactor tap test " xu du
@ 2025-11-17 6:25 ` xu du
2025-11-17 6:25 ` [PATCH net-next 6/8] selftest: tun: Add test for sending gso packet into tun xu du
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: xu du @ 2025-11-17 6:25 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni; +Cc: netdev
In preparation for testing GSO over UDP tunnels, enhance the test
infrastructure to support a more complex data path involving a TUN
device and a VXLAN tunnel.
This patch introduces a dedicated setup/teardown topology that creates
both a VXLAN tunnel interface and a TUN interface. The TUN device acts
as the VTEP (Virtual Tunnel Endpoint), allowing it to send and receive
virtio-net packets. This setup effectively tests the kernel's data path
for encapsulated traffic.
Additionally, a new data structure is defined to manage test parameters.
This structure is designed to be extensible, allowing different test
data and configurations to be easily added in subsequent patches.
Signed-off-by: xu du <xudu@redhat.com>
---
tools/testing/selftests/net/tun.c | 391 ++++++++++++++++++++++++++++++
1 file changed, 391 insertions(+)
diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
index 2ed439cce423..8f0188ccb9fb 100644
--- a/tools/testing/selftests/net/tun.c
+++ b/tools/testing/selftests/net/tun.c
@@ -15,6 +15,81 @@
#include "../kselftest_harness.h"
#include "tuntap_helpers.h"
+static const char param_dev_vxlan_name[] = "vxlan1";
+static unsigned char param_hwaddr_outer_dst[] = { 0x00, 0xfe, 0x98,
+ 0x14, 0x22, 0x42 };
+static unsigned char param_hwaddr_outer_src[] = { 0x00, 0xfe, 0x98,
+ 0x94, 0xd2, 0x43 };
+static unsigned char param_hwaddr_inner_dst[] = { 0x00, 0xfe, 0x98,
+ 0x94, 0x22, 0xcc };
+static unsigned char param_hwaddr_inner_src[] = { 0x00, 0xfe, 0x98,
+ 0x94, 0xd2, 0xdd };
+
+static struct in_addr param_ipaddr4_outer_dst = {
+ __constant_htonl(0xac100001),
+};
+
+static struct in_addr param_ipaddr4_outer_src = {
+ __constant_htonl(0xac100002),
+};
+
+static struct in_addr param_ipaddr4_inner_dst = {
+ __constant_htonl(0xac100101),
+};
+
+static struct in_addr param_ipaddr4_inner_src = {
+ __constant_htonl(0xac100102),
+};
+
+static struct in6_addr param_ipaddr6_outer_dst = {
+ { { 0x20, 0x02, 0x0d, 0xb8, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } },
+};
+
+static struct in6_addr param_ipaddr6_outer_src = {
+ { { 0x20, 0x02, 0x0d, 0xb8, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 } },
+};
+
+static struct in6_addr param_ipaddr6_inner_dst = {
+ { { 0x20, 0x02, 0x0d, 0xb8, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } },
+};
+
+static struct in6_addr param_ipaddr6_inner_src = {
+ { { 0x20, 0x02, 0x0d, 0xb8, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 } },
+};
+
+#define VN_ID 1
+#define VN_PORT 4789
+#define UDP_SRC_PORT 22
+#define UDP_DST_PORT 48878
+#define IPPREFIX_LEN 24
+#define IP6PREFIX_LEN 64
+#define TIMEOUT_SEC 3
+
+#define UDP_TUNNEL_VXLAN_4IN4 0x01
+#define UDP_TUNNEL_VXLAN_6IN4 0x02
+#define UDP_TUNNEL_VXLAN_4IN6 0x04
+#define UDP_TUNNEL_VXLAN_6IN6 0x08
+
+#define UDP_TUNNEL_OUTER_IPV4 (UDP_TUNNEL_VXLAN_4IN4 | UDP_TUNNEL_VXLAN_6IN4)
+#define UDP_TUNNEL_INNER_IPV4 (UDP_TUNNEL_VXLAN_4IN4 | UDP_TUNNEL_VXLAN_4IN6)
+
+#define TUN_VNET_TNL_SIZE sizeof(struct virtio_net_hdr_v1_hash_tunnel)
+
+union vxlan_addr {
+ struct sockaddr_in sin;
+ struct sockaddr_in6 sin6;
+};
+
+struct vxlan_setup_config {
+ union vxlan_addr local_ip;
+ union vxlan_addr remote_ip;
+ __be32 vni;
+ int remote_ifindex;
+ __be16 dst_port;
+ unsigned char hwaddr[6];
+ uint8_t csum;
+};
+
static int tun_attach(int fd, char *dev)
{
struct ifreq ifr;
@@ -67,6 +142,177 @@ static int tun_delete(char *dev)
return dev_delete(dev);
}
+static size_t sockaddr_len(int family)
+{
+ return (family == AF_INET) ? sizeof(struct sockaddr_in) :
+ sizeof(struct sockaddr_in6);
+}
+
+static int tun_open(char *dev, const int flags, const int hdrlen,
+ const int features, const unsigned char *mac_addr)
+{
+ struct ifreq ifr = { 0 };
+ int fd, sk = -1;
+
+ fd = open("/dev/net/tun", O_RDWR);
+ if (fd < 0) {
+ perror("open");
+ return -1;
+ }
+
+ ifr.ifr_flags = flags;
+ if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) {
+ perror("ioctl(TUNSETIFF)");
+ goto err;
+ }
+ strcpy(dev, ifr.ifr_name);
+
+ if (hdrlen > 0) {
+ if (ioctl(fd, TUNSETVNETHDRSZ, &hdrlen) < 0) {
+ perror("ioctl(TUNSETVNETHDRSZ)");
+ goto err;
+ }
+ }
+
+ if (features) {
+ if (ioctl(fd, TUNSETOFFLOAD, features) < 0) {
+ perror("ioctl(TUNSETOFFLOAD)");
+ goto err;
+ }
+ }
+
+ sk = socket(PF_INET, SOCK_DGRAM, 0);
+ if (sk < 0) {
+ perror("socket");
+ goto err;
+ }
+
+ if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
+ perror("ioctl(SIOCGIFFLAGS)");
+ goto err;
+ }
+
+ ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
+ if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
+ perror("ioctl(SIOCSIFFLAGS)");
+ goto err;
+ }
+
+ if (mac_addr && flags & IFF_TAP) {
+ ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
+ memcpy(ifr.ifr_hwaddr.sa_data, mac_addr, ETH_ALEN);
+
+ if (ioctl(sk, SIOCSIFHWADDR, &ifr) < 0) {
+ perror("ioctl(SIOCSIFHWADDR)");
+ goto err;
+ }
+ }
+
+out:
+ if (sk >= 0)
+ close(sk);
+ return fd;
+
+err:
+ close(fd);
+ fd = -1;
+ goto out;
+}
+
+static int vxlan_fill_rtattr(struct nlmsghdr *nh, void *data)
+{
+ struct vxlan_setup_config *vxlan = data;
+
+ rtattr_add_any(nh, IFLA_ADDRESS, vxlan->hwaddr, ETH_ALEN);
+ return 0;
+}
+
+static int vxlan_fill_info_data(struct nlmsghdr *nh, void *data)
+{
+ struct vxlan_setup_config *vxlan = data;
+
+ rtattr_add_any(nh, IFLA_VXLAN_LINK, &vxlan->remote_ifindex,
+ sizeof(vxlan->remote_ifindex));
+ rtattr_add_any(nh, IFLA_VXLAN_ID, &vxlan->vni, sizeof(vxlan->vni));
+ rtattr_add_any(nh, IFLA_VXLAN_PORT, &vxlan->dst_port,
+ sizeof(vxlan->dst_port));
+ rtattr_add_any(nh, IFLA_VXLAN_UDP_CSUM, &vxlan->csum,
+ sizeof(vxlan->csum));
+
+ if (vxlan->remote_ip.sin.sin_family == AF_INET) {
+ rtattr_add_any(nh, IFLA_VXLAN_GROUP,
+ &vxlan->remote_ip.sin.sin_addr,
+ sizeof(struct in_addr));
+ rtattr_add_any(nh, IFLA_VXLAN_LOCAL,
+ &vxlan->local_ip.sin.sin_addr,
+ sizeof(struct in_addr));
+ } else {
+ rtattr_add_any(nh, IFLA_VXLAN_GROUP6,
+ &vxlan->remote_ip.sin6.sin6_addr,
+ sizeof(struct in6_addr));
+ rtattr_add_any(nh, IFLA_VXLAN_LOCAL6,
+ &vxlan->local_ip.sin6.sin6_addr,
+ sizeof(struct in6_addr));
+ }
+
+ return 0;
+}
+
+static int set_pmtu_discover(int fd, bool is_ipv4)
+{
+ int level, name, val;
+
+ if (is_ipv4) {
+ level = SOL_IP;
+ name = IP_MTU_DISCOVER;
+ val = IP_PMTUDISC_DO;
+ } else {
+ level = SOL_IPV6;
+ name = IPV6_MTU_DISCOVER;
+ val = IPV6_PMTUDISC_DO;
+ }
+
+ return setsockopt(fd, level, name, &val, sizeof(val));
+}
+
+static int udp_socket_open(struct sockaddr_storage *sockaddr, bool can_frag)
+{
+ struct timeval timeout = { .tv_sec = TIMEOUT_SEC };
+ int family = sockaddr->ss_family;
+ int sockfd, alen;
+
+ sockfd = socket(family, SOCK_DGRAM, 0);
+ if (sockfd < 0) {
+ perror("socket");
+ return -1;
+ }
+
+ alen = sockaddr_len(family);
+ if (bind(sockfd, (struct sockaddr *)sockaddr, alen) < 0) {
+ perror("bind");
+ goto err;
+ }
+
+ if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
+ sizeof(timeout)) < 0) {
+ perror("setsockopt(SO_RCVTIMEO)");
+ goto err;
+ }
+
+ if (!can_frag) {
+ if (set_pmtu_discover(sockfd, family == AF_INET) < 0) {
+ perror("set_pmtu_discover");
+ goto err;
+ }
+ }
+
+ return sockfd;
+
+err:
+ close(sockfd);
+ return -1;
+}
+
FIXTURE(tun)
{
char ifname[IFNAMSIZ];
@@ -129,4 +375,149 @@ TEST_F(tun, reattach_close_delete)
EXPECT_EQ(tun_delete(self->ifname), 0);
}
+FIXTURE(tun_vnet_udptnl)
+{
+ char ifname[IFNAMSIZ];
+ int fd, sock;
+};
+
+FIXTURE_VARIANT(tun_vnet_udptnl)
+{
+ int tunnel_type;
+ bool is_tap;
+};
+
+/* clang-format off */
+#define TUN_VNET_UDPTNL_VARIANT_ADD(type, desc) \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##udptnl) { \
+ .tunnel_type = type, \
+ .is_tap = true, \
+ }
+/* clang-format on */
+
+TUN_VNET_UDPTNL_VARIANT_ADD(UDP_TUNNEL_VXLAN_4IN4, 4in4);
+TUN_VNET_UDPTNL_VARIANT_ADD(UDP_TUNNEL_VXLAN_6IN4, 6in4);
+TUN_VNET_UDPTNL_VARIANT_ADD(UDP_TUNNEL_VXLAN_4IN6, 4in6);
+TUN_VNET_UDPTNL_VARIANT_ADD(UDP_TUNNEL_VXLAN_6IN6, 6in6);
+
+FIXTURE_SETUP(tun_vnet_udptnl)
+{
+ const int flags = (variant->is_tap ? IFF_TAP : IFF_TUN) | IFF_VNET_HDR |
+ IFF_MULTI_QUEUE | IFF_NO_PI;
+ const int features = TUN_F_CSUM | TUN_F_UDP_TUNNEL_GSO |
+ TUN_F_UDP_TUNNEL_GSO_CSUM | TUN_F_USO4 |
+ TUN_F_USO6;
+ const int hdrlen = TUN_VNET_TNL_SIZE;
+ int tunnel_type = variant->tunnel_type;
+ struct sockaddr_storage sockaddr;
+ struct vxlan_setup_config vxlan;
+ int ret, family;
+
+ self->fd = tun_open(self->ifname, flags, hdrlen, features,
+ param_hwaddr_outer_src);
+ ASSERT_GE(self->fd, 0);
+
+ family = (tunnel_type & UDP_TUNNEL_OUTER_IPV4) ? AF_INET : AF_INET6;
+ if (family == AF_INET) {
+ ret = ip_addr_add(self->ifname, AF_INET,
+ (void *)¶m_ipaddr4_outer_src,
+ IPPREFIX_LEN);
+ ret += ip_neigh_add(self->ifname, AF_INET,
+ (void *)¶m_ipaddr4_outer_dst,
+ param_hwaddr_outer_dst);
+ } else {
+ ret = ip_addr_add(self->ifname, AF_INET6,
+ (void *)¶m_ipaddr6_outer_src,
+ IP6PREFIX_LEN);
+ ret += ip_neigh_add(self->ifname, AF_INET6,
+ (void *)¶m_ipaddr6_outer_dst,
+ param_hwaddr_outer_dst);
+ }
+ ASSERT_EQ(ret, 0);
+
+ memset(&vxlan, 0, sizeof(vxlan));
+ vxlan.vni = VN_ID;
+ vxlan.dst_port = htons(VN_PORT);
+ vxlan.csum = 1;
+ vxlan.remote_ifindex = if_nametoindex(self->ifname);
+ memcpy(vxlan.hwaddr, param_hwaddr_inner_src, ETH_ALEN);
+
+ if (tunnel_type & UDP_TUNNEL_OUTER_IPV4) {
+ vxlan.remote_ip.sin.sin_family = AF_INET;
+ vxlan.remote_ip.sin.sin_addr = param_ipaddr4_outer_dst;
+ vxlan.local_ip.sin.sin_family = AF_INET;
+ vxlan.local_ip.sin.sin_addr = param_ipaddr4_outer_src;
+ } else {
+ vxlan.remote_ip.sin6.sin6_family = AF_INET6;
+ vxlan.remote_ip.sin6.sin6_addr = param_ipaddr6_outer_dst;
+ vxlan.local_ip.sin6.sin6_family = AF_INET6;
+ vxlan.local_ip.sin6.sin6_addr = param_ipaddr6_outer_src;
+ }
+
+ ret = dev_create(param_dev_vxlan_name, "vxlan", vxlan_fill_rtattr,
+ vxlan_fill_info_data, (void *)&vxlan);
+ ASSERT_EQ(ret, 0);
+
+ family = (tunnel_type & UDP_TUNNEL_INNER_IPV4) ? AF_INET : AF_INET6;
+ if (family == AF_INET) {
+ ret = ip_addr_add(param_dev_vxlan_name, AF_INET,
+ (void *)¶m_ipaddr4_inner_src,
+ IPPREFIX_LEN);
+ ret += ip_neigh_add(param_dev_vxlan_name, AF_INET,
+ (void *)¶m_ipaddr4_inner_dst,
+ param_hwaddr_inner_dst);
+ } else {
+ ret = ip_addr_add(param_dev_vxlan_name, AF_INET6,
+ (void *)¶m_ipaddr6_inner_src,
+ IP6PREFIX_LEN);
+ ret += ip_neigh_add(param_dev_vxlan_name, AF_INET6,
+ (void *)¶m_ipaddr6_inner_dst,
+ param_hwaddr_inner_dst);
+ }
+ ASSERT_EQ(ret, 0);
+
+ memset(&sockaddr, 0, sizeof(sockaddr));
+ sockaddr.ss_family = family;
+ if (family == AF_INET) {
+ struct sockaddr_in *addr4 = (struct sockaddr_in *)&sockaddr;
+
+ addr4->sin_addr = param_ipaddr4_inner_src;
+ addr4->sin_port = htons(UDP_SRC_PORT);
+ } else {
+ struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&sockaddr;
+
+ addr6->sin6_addr = param_ipaddr6_inner_src;
+ addr6->sin6_port = htons(UDP_SRC_PORT);
+ }
+ self->sock = udp_socket_open(&sockaddr, false);
+ ASSERT_GE(self->sock, 0);
+
+ /* give 1000us delay to ensure the interface address is ready */
+ usleep(1000);
+}
+
+FIXTURE_TEARDOWN(tun_vnet_udptnl)
+{
+ int ret;
+
+ if (self->sock != -1)
+ close(self->sock);
+
+ ret = dev_delete(param_dev_vxlan_name);
+ EXPECT_EQ(ret, 0);
+
+ ret = tun_delete(self->ifname);
+ EXPECT_EQ(ret, 0);
+}
+
+TEST_F(tun_vnet_udptnl, basic)
+{
+ int ret;
+ char cmd[256] = { 0 };
+
+ sprintf(cmd, "ip addr show %s > /dev/null 2>&1", param_dev_vxlan_name);
+ ret = system(cmd);
+ ASSERT_EQ(ret, 0);
+}
+
TEST_HARNESS_MAIN
--
2.49.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH net-next 6/8] selftest: tun: Add test for sending gso packet into tun
2025-11-17 6:24 [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels xu du
` (4 preceding siblings ...)
2025-11-17 6:25 ` [PATCH net-next 5/8] selftest: tun: Add helpers for GSO over UDP tunnel xu du
@ 2025-11-17 6:25 ` xu du
2025-11-17 6:25 ` [PATCH net-next 7/8] selftest: tun: Add test for receiving gso packet from tun xu du
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: xu du @ 2025-11-17 6:25 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni; +Cc: netdev
The test constructs a raw packet, prepends a virtio_net_hdr,
and writes the result to the TUN device. This mimics the behavior
of a vm forwarding a guest's packet to the host networking stack.
Signed-off-by: xu du <xudu@redhat.com>
---
tools/testing/selftests/net/tun.c | 149 ++++++++++++++++++++++++++++--
1 file changed, 140 insertions(+), 9 deletions(-)
diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
index 8f0188ccb9fb..870697a0d9e8 100644
--- a/tools/testing/selftests/net/tun.c
+++ b/tools/testing/selftests/net/tun.c
@@ -75,6 +75,31 @@ static struct in6_addr param_ipaddr6_inner_src = {
#define TUN_VNET_TNL_SIZE sizeof(struct virtio_net_hdr_v1_hash_tunnel)
+#define MAX_VNET_TUNNEL_PACKET_SZ (TUN_VNET_TNL_SIZE + ETH_HLEN + ETH_MAX_MTU)
+
+#define UDP_TUNNEL_VXLAN_4IN4_HDRLEN \
+ (ETH_HLEN + 2 * sizeof(struct iphdr) + 8 + 2 * sizeof(struct udphdr))
+#define UDP_TUNNEL_VXLAN_6IN6_HDRLEN \
+ (ETH_HLEN + 2 * sizeof(struct ipv6hdr) + 8 + 2 * sizeof(struct udphdr))
+#define UDP_TUNNEL_VXLAN_4IN6_HDRLEN \
+ (ETH_HLEN + sizeof(struct iphdr) + sizeof(struct ipv6hdr) + 8 + \
+ 2 * sizeof(struct udphdr))
+#define UDP_TUNNEL_VXLAN_6IN4_HDRLEN \
+ (ETH_HLEN + sizeof(struct ipv6hdr) + sizeof(struct iphdr) + 8 + \
+ 2 * sizeof(struct udphdr))
+
+#define UDP_TUNNEL_HDRLEN(type) \
+ ((type) == UDP_TUNNEL_VXLAN_4IN4 ? UDP_TUNNEL_VXLAN_4IN4_HDRLEN : \
+ (type) == UDP_TUNNEL_VXLAN_6IN4 ? UDP_TUNNEL_VXLAN_6IN4_HDRLEN : \
+ (type) == UDP_TUNNEL_VXLAN_4IN6 ? UDP_TUNNEL_VXLAN_4IN6_HDRLEN : \
+ (type) == UDP_TUNNEL_VXLAN_6IN6 ? UDP_TUNNEL_VXLAN_6IN6_HDRLEN : \
+ 0)
+
+#define UDP_TUNNEL_MSS(type) (ETH_DATA_LEN - UDP_TUNNEL_HDRLEN(type))
+
+#define UDP_TUNNEL_MAX(type, is_tap) \
+ (ETH_MAX_MTU - UDP_TUNNEL_HDRLEN(type) - ((is_tap) ? ETH_HLEN : 0))
+
union vxlan_addr {
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
@@ -384,15 +409,23 @@ FIXTURE(tun_vnet_udptnl)
FIXTURE_VARIANT(tun_vnet_udptnl)
{
int tunnel_type;
- bool is_tap;
+ int gso_size;
+ int data_size;
+ int r_num_mss;
+ bool is_tap, no_gso;
};
/* clang-format off */
#define TUN_VNET_UDPTNL_VARIANT_ADD(type, desc) \
- FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##udptnl) { \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_1mss) { \
+ /* send a single MSS: fall back to no GSO */ \
.tunnel_type = type, \
+ .gso_size = UDP_TUNNEL_MSS(type), \
+ .data_size = UDP_TUNNEL_MSS(type), \
+ .r_num_mss = 1, \
.is_tap = true, \
- }
+ .no_gso = true, \
+ };
/* clang-format on */
TUN_VNET_UDPTNL_VARIANT_ADD(UDP_TUNNEL_VXLAN_4IN4, 4in4);
@@ -510,14 +543,112 @@ FIXTURE_TEARDOWN(tun_vnet_udptnl)
EXPECT_EQ(ret, 0);
}
-TEST_F(tun_vnet_udptnl, basic)
+static int build_gso_packet_into_tun(const FIXTURE_VARIANT(tun_vnet_udptnl) *
+ variant,
+ uint8_t *buf)
{
- int ret;
- char cmd[256] = { 0 };
+ int tunnel_type = variant->tunnel_type;
+ int payload_len = variant->data_size;
+ int gso_size = variant->gso_size;
+ int inner_family, outer_family;
+ bool is_tap = variant->is_tap;
+ uint8_t *outer_udph = NULL;
+ uint8_t *cur = buf;
+ int len, proto;
+
+ len = (is_tap ? ETH_HLEN : 0) + UDP_TUNNEL_HDRLEN(tunnel_type);
+ inner_family = (tunnel_type & UDP_TUNNEL_INNER_IPV4) ? AF_INET :
+ AF_INET6;
+ outer_family = (tunnel_type & UDP_TUNNEL_OUTER_IPV4) ? AF_INET :
+ AF_INET6;
+
+ cur += build_virtio_net_hdr_v1_hash_tunnel(cur, is_tap, len, gso_size,
+ outer_family, inner_family);
+
+ if (is_tap) {
+ proto = outer_family == AF_INET ? ETH_P_IP : ETH_P_IPV6;
+ cur += build_eth(cur, proto, param_hwaddr_outer_dst,
+ param_hwaddr_outer_src);
+ len -= ETH_HLEN;
+ }
- sprintf(cmd, "ip addr show %s > /dev/null 2>&1", param_dev_vxlan_name);
- ret = system(cmd);
- ASSERT_EQ(ret, 0);
+ if (outer_family == AF_INET) {
+ len = len - sizeof(struct iphdr) + payload_len;
+ cur += build_ipv4_header(cur, IPPROTO_UDP, len,
+ ¶m_ipaddr4_outer_dst,
+ ¶m_ipaddr4_outer_src);
+ } else {
+ len = len - sizeof(struct ipv6hdr) + payload_len;
+ cur += build_ipv6_header(cur, IPPROTO_UDP, 0, len,
+ ¶m_ipaddr6_outer_dst,
+ ¶m_ipaddr6_outer_src);
+ }
+
+ outer_udph = cur;
+ len -= sizeof(struct udphdr);
+ proto = inner_family == AF_INET ? ETH_P_IP : ETH_P_IPV6;
+ cur += build_udp_header(cur, UDP_SRC_PORT, VN_PORT, len);
+ cur += build_vxlan_header(cur, VN_ID);
+ cur += build_eth(cur, proto, param_hwaddr_inner_dst,
+ param_hwaddr_inner_src);
+
+ len = sizeof(struct udphdr) + payload_len;
+ if (inner_family == AF_INET) {
+ cur += build_ipv4_header(cur, IPPROTO_UDP, len,
+ ¶m_ipaddr4_inner_dst,
+ ¶m_ipaddr4_inner_src);
+ } else {
+ cur += build_ipv6_header(cur, IPPROTO_UDP, 0, len,
+ ¶m_ipaddr6_inner_dst,
+ ¶m_ipaddr6_inner_src);
+ }
+
+ cur += build_udp_packet(cur, UDP_DST_PORT, UDP_SRC_PORT, payload_len,
+ inner_family, false);
+
+ build_udp_packet_csum(outer_udph, outer_family, false);
+
+ return cur - buf;
+}
+
+static int
+recieve_gso_packet_from_tunnel(FIXTURE_DATA(tun_vnet_udptnl) * self,
+ const FIXTURE_VARIANT(tun_vnet_udptnl) * variant,
+ int *r_num_mss)
+{
+ uint8_t packet_buf[MAX_VNET_TUNNEL_PACKET_SZ];
+ int len, total_len = 0, socket = self->sock;
+ int payload_len = variant->data_size;
+
+ while (total_len < payload_len) {
+ len = recv(socket, packet_buf, sizeof(packet_buf), 0);
+ if (len < 0) {
+ if (errno != EAGAIN && errno != EWOULDBLOCK)
+ perror("recv");
+ return total_len;
+ }
+
+ (*r_num_mss)++;
+ total_len += len;
+ }
+
+ return total_len;
+}
+
+TEST_F(tun_vnet_udptnl, send_gso_packet)
+{
+ uint8_t pkt[MAX_VNET_TUNNEL_PACKET_SZ];
+ int r_num_mss = 0;
+ int ret, off;
+
+ memset(pkt, 0, sizeof(pkt));
+ off = build_gso_packet_into_tun(variant, pkt);
+ ret = write(self->fd, pkt, off);
+ ASSERT_EQ(ret, off);
+
+ ret = recieve_gso_packet_from_tunnel(self, variant, &r_num_mss);
+ ASSERT_EQ(ret, variant->data_size);
+ ASSERT_EQ(r_num_mss, variant->r_num_mss);
}
TEST_HARNESS_MAIN
--
2.49.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH net-next 7/8] selftest: tun: Add test for receiving gso packet from tun
2025-11-17 6:24 [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels xu du
` (5 preceding siblings ...)
2025-11-17 6:25 ` [PATCH net-next 6/8] selftest: tun: Add test for sending gso packet into tun xu du
@ 2025-11-17 6:25 ` xu du
2025-11-17 6:25 ` [PATCH net-next 8/8] selftest: tun: Add test data for success and failure paths xu du
2025-11-17 15:40 ` [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels Jakub Kicinski
8 siblings, 0 replies; 10+ messages in thread
From: xu du @ 2025-11-17 6:25 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni; +Cc: netdev
The test validate that GSO information are correctly exposed
when reading packets from a TUN device.
Signed-off-by: xu du <xudu@redhat.com>
---
tools/testing/selftests/net/tun.c | 209 ++++++++++++++++++++++++++++++
1 file changed, 209 insertions(+)
diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
index 870697a0d9e8..19524748db44 100644
--- a/tools/testing/selftests/net/tun.c
+++ b/tools/testing/selftests/net/tun.c
@@ -338,6 +338,116 @@ static int udp_socket_open(struct sockaddr_storage *sockaddr, bool can_frag)
return -1;
}
+static int send_gso_udp_msg(int socket, struct sockaddr_storage *addr,
+ uint8_t *send_buf, int send_len, int gso_size)
+{
+ char control[CMSG_SPACE(sizeof(uint16_t))] = { 0 };
+ int alen = sockaddr_len(addr->ss_family);
+ struct msghdr msg = { 0 };
+ struct iovec iov = { 0 };
+ int ret;
+
+ iov.iov_base = send_buf;
+ iov.iov_len = send_len;
+
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_name = addr;
+ msg.msg_namelen = alen;
+
+ if (gso_size > 0) {
+ struct cmsghdr *cmsg;
+
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof(control);
+
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_UDP;
+ cmsg->cmsg_type = UDP_SEGMENT;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(uint16_t));
+ *(uint16_t *)CMSG_DATA(cmsg) = gso_size;
+ }
+
+ ret = sendmsg(socket, &msg, 0);
+ if (ret < 0)
+ perror("sendmsg");
+
+ return ret;
+}
+
+static int validate_hdrlen(uint8_t **cur, int *len, int x)
+{
+ if (*len < x)
+ return -1;
+ *cur += x;
+ *len -= x;
+ return 0;
+}
+
+static int parse_udp_tunnel_vnet_packet(uint8_t *buf, int len, int tunnel_type,
+ bool is_tap)
+{
+ struct ipv6hdr *iph6;
+ struct udphdr *udph;
+ struct iphdr *iph4;
+ uint8_t *cur = buf;
+
+ if (validate_hdrlen(&cur, &len, TUN_VNET_TNL_SIZE))
+ return -1;
+
+ if (is_tap) {
+ if (validate_hdrlen(&cur, &len, ETH_HLEN))
+ return -1;
+ }
+
+ if (tunnel_type & UDP_TUNNEL_OUTER_IPV4) {
+ iph4 = (struct iphdr *)cur;
+ if (validate_hdrlen(&cur, &len, sizeof(struct iphdr)))
+ return -1;
+ if (iph4->version != 4 || iph4->protocol != IPPROTO_UDP)
+ return -1;
+ } else {
+ iph6 = (struct ipv6hdr *)cur;
+ if (validate_hdrlen(&cur, &len, sizeof(struct ipv6hdr)))
+ return -1;
+ if (iph6->version != 6 || iph6->nexthdr != IPPROTO_UDP)
+ return -1;
+ }
+
+ udph = (struct udphdr *)cur;
+ if (validate_hdrlen(&cur, &len, sizeof(struct udphdr)))
+ return -1;
+ if (ntohs(udph->dest) != VN_PORT)
+ return -1;
+
+ if (validate_hdrlen(&cur, &len, 8))
+ return -1;
+ if (validate_hdrlen(&cur, &len, ETH_HLEN))
+ return -1;
+
+ if (tunnel_type & UDP_TUNNEL_INNER_IPV4) {
+ iph4 = (struct iphdr *)cur;
+ if (validate_hdrlen(&cur, &len, sizeof(struct iphdr)))
+ return -1;
+ if (iph4->version != 4 || iph4->protocol != IPPROTO_UDP)
+ return -1;
+ } else {
+ iph6 = (struct ipv6hdr *)cur;
+ if (validate_hdrlen(&cur, &len, sizeof(struct ipv6hdr)))
+ return -1;
+ if (iph6->version != 6 || iph6->nexthdr != IPPROTO_UDP)
+ return -1;
+ }
+
+ udph = (struct udphdr *)cur;
+ if (validate_hdrlen(&cur, &len, sizeof(struct udphdr)))
+ return -1;
+ if (ntohs(udph->dest) != UDP_DST_PORT)
+ return -1;
+
+ return len;
+}
+
FIXTURE(tun)
{
char ifname[IFNAMSIZ];
@@ -635,6 +745,83 @@ recieve_gso_packet_from_tunnel(FIXTURE_DATA(tun_vnet_udptnl) * self,
return total_len;
}
+static int send_gso_packet_into_tunnel(FIXTURE_DATA(tun_vnet_udptnl) * self,
+ const FIXTURE_VARIANT(tun_vnet_udptnl) *
+ variant)
+{
+ uint8_t buf[MAX_VNET_TUNNEL_PACKET_SZ] = { 0 };
+ struct sockaddr_storage addr = { 0 };
+ int payload_len = variant->data_size;
+ int gso_size = variant->gso_size;
+ int family;
+
+ family = (variant->tunnel_type & UDP_TUNNEL_INNER_IPV4) ? AF_INET :
+ AF_INET6;
+ if (family == AF_INET) {
+ struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
+
+ addr4->sin_family = AF_INET;
+ addr4->sin_addr = param_ipaddr4_inner_dst;
+ addr4->sin_port = htons(UDP_DST_PORT);
+ } else {
+ struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
+
+ addr6->sin6_family = AF_INET6;
+ addr6->sin6_addr = param_ipaddr6_inner_dst;
+ addr6->sin6_port = htons(UDP_DST_PORT);
+ }
+
+ return send_gso_udp_msg(self->sock, &addr, buf, payload_len, gso_size);
+}
+
+static int
+recieve_gso_packet_from_tun(FIXTURE_DATA(tun_vnet_udptnl) * self,
+ const FIXTURE_VARIANT(tun_vnet_udptnl) * variant,
+ struct virtio_net_hdr_v1_hash_tunnel *vnet_hdr)
+{
+ struct timeval timeout = { .tv_sec = TIMEOUT_SEC };
+ uint8_t buf[MAX_VNET_TUNNEL_PACKET_SZ];
+ int tunnel_type = variant->tunnel_type;
+ int payload_len = variant->data_size;
+ bool is_tap = variant->is_tap;
+ int ret, len, total_len = 0;
+ int tun_fd = self->fd;
+ fd_set fdset;
+
+ while (total_len < payload_len) {
+ FD_ZERO(&fdset);
+ FD_SET(tun_fd, &fdset);
+
+ ret = select(tun_fd + 1, &fdset, NULL, NULL, &timeout);
+ if (ret <= 0) {
+ perror("select");
+ break;
+ }
+ if (!FD_ISSET(tun_fd, &fdset))
+ continue;
+
+ len = read(tun_fd, buf, sizeof(buf));
+ if (len < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK)
+ continue;
+ perror("read");
+ break;
+ }
+
+ len = parse_udp_tunnel_vnet_packet(buf, len, tunnel_type,
+ is_tap);
+ if (len < 0)
+ continue;
+
+ if (total_len == 0)
+ memcpy(vnet_hdr, buf, TUN_VNET_TNL_SIZE);
+
+ total_len += len;
+ }
+
+ return total_len;
+}
+
TEST_F(tun_vnet_udptnl, send_gso_packet)
{
uint8_t pkt[MAX_VNET_TUNNEL_PACKET_SZ];
@@ -651,4 +838,26 @@ TEST_F(tun_vnet_udptnl, send_gso_packet)
ASSERT_EQ(r_num_mss, variant->r_num_mss);
}
+TEST_F(tun_vnet_udptnl, recv_gso_packet)
+{
+ struct virtio_net_hdr_v1_hash_tunnel vnet_hdr = { 0 };
+ struct virtio_net_hdr_v1 *vh = &vnet_hdr.hash_hdr.hdr;
+ int ret, gso_type = VIRTIO_NET_HDR_GSO_UDP_L4;
+
+ ret = send_gso_packet_into_tunnel(self, variant);
+ ASSERT_EQ(ret, variant->data_size)
+
+ memset(&vnet_hdr, 0, sizeof(vnet_hdr));
+ ret = recieve_gso_packet_from_tun(self, variant, &vnet_hdr);
+ ASSERT_EQ(ret, variant->data_size);
+
+ if (!variant->no_gso) {
+ ASSERT_EQ(vh->gso_size, variant->gso_size);
+ gso_type |= (variant->tunnel_type & UDP_TUNNEL_OUTER_IPV4) ?
+ (VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV4) :
+ (VIRTIO_NET_HDR_GSO_UDP_TUNNEL_IPV6);
+ ASSERT_EQ(vh->gso_type, gso_type);
+ }
+}
+
TEST_HARNESS_MAIN
--
2.49.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH net-next 8/8] selftest: tun: Add test data for success and failure paths
2025-11-17 6:24 [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels xu du
` (6 preceding siblings ...)
2025-11-17 6:25 ` [PATCH net-next 7/8] selftest: tun: Add test for receiving gso packet from tun xu du
@ 2025-11-17 6:25 ` xu du
2025-11-17 15:40 ` [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels Jakub Kicinski
8 siblings, 0 replies; 10+ messages in thread
From: xu du @ 2025-11-17 6:25 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni; +Cc: netdev
To improve the robustness and coverage of the TUN selftests, this
patch expands the set of test data.
Signed-off-by: xu du <xudu@redhat.com>
---
tools/testing/selftests/net/tun.c | 115 +++++++++++++++++++++++++++++-
1 file changed, 113 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
index 19524748db44..7a2062f87c6a 100644
--- a/tools/testing/selftests/net/tun.c
+++ b/tools/testing/selftests/net/tun.c
@@ -57,6 +57,10 @@ static struct in6_addr param_ipaddr6_inner_src = {
{ { 0x20, 0x02, 0x0d, 0xb8, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 } },
};
+#ifndef BIT
+#define BIT(nr) (1UL << (nr))
+#endif
+
#define VN_ID 1
#define VN_PORT 4789
#define UDP_SRC_PORT 22
@@ -70,6 +74,8 @@ static struct in6_addr param_ipaddr6_inner_src = {
#define UDP_TUNNEL_VXLAN_4IN6 0x04
#define UDP_TUNNEL_VXLAN_6IN6 0x08
+#define UDP_TUNNEL_MAX_SEGMENTS BIT(7)
+
#define UDP_TUNNEL_OUTER_IPV4 (UDP_TUNNEL_VXLAN_4IN4 | UDP_TUNNEL_VXLAN_6IN4)
#define UDP_TUNNEL_INNER_IPV4 (UDP_TUNNEL_VXLAN_4IN4 | UDP_TUNNEL_VXLAN_4IN6)
@@ -527,6 +533,39 @@ FIXTURE_VARIANT(tun_vnet_udptnl)
/* clang-format off */
#define TUN_VNET_UDPTNL_VARIANT_ADD(type, desc) \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_nogsosz_1byte) { \
+ /* no GSO: send a single byte */ \
+ .tunnel_type = type, \
+ .data_size = 1, \
+ .r_num_mss = 1, \
+ .is_tap = true, \
+ .no_gso = true, \
+ }; \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_nogsosz_1mss) { \
+ /* no GSO: send a single MSS, fall back to no GSO */ \
+ .tunnel_type = type, \
+ .data_size = UDP_TUNNEL_MSS(type), \
+ .r_num_mss = 1, \
+ .is_tap = true, \
+ .no_gso = true, \
+ }; \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_nogsosz_gtmss) { \
+ /* no GSO: send a single MSS + 1B: fail */ \
+ .tunnel_type = type, \
+ .data_size = UDP_TUNNEL_MSS(type) + 1, \
+ .r_num_mss = 1, \
+ .is_tap = true, \
+ .no_gso = true, \
+ }; \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_1byte) { \
+ /* GSO: send 1 byte, gso 1 byte, fall back to no GSO */ \
+ .tunnel_type = type, \
+ .gso_size = 1, \
+ .data_size = 1, \
+ .r_num_mss = 1, \
+ .is_tap = true, \
+ .no_gso = true, \
+ }; \
FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_1mss) { \
/* send a single MSS: fall back to no GSO */ \
.tunnel_type = type, \
@@ -535,8 +574,65 @@ FIXTURE_VARIANT(tun_vnet_udptnl)
.r_num_mss = 1, \
.is_tap = true, \
.no_gso = true, \
- };
-/* clang-format on */
+ }; \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_ltgso) { \
+ /* data <= MSS < gso: will fall back to no GSO */ \
+ .tunnel_type = type, \
+ .gso_size = UDP_TUNNEL_MSS(type) + 1, \
+ .data_size = UDP_TUNNEL_MSS(type), \
+ .r_num_mss = 1, \
+ .is_tap = true, \
+ .no_gso = true, \
+ }; \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_gtgso) { \
+ /* GSO: a single MSS + 1B */ \
+ .tunnel_type = type, \
+ .gso_size = UDP_TUNNEL_MSS(type), \
+ .data_size = UDP_TUNNEL_MSS(type) + 1, \
+ .r_num_mss = 2, \
+ .is_tap = true, \
+ }; \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_2mss) { \
+ /* no GSO: send exactly 2 MSS */ \
+ .tunnel_type = type, \
+ .gso_size = UDP_TUNNEL_MSS(type), \
+ .data_size = UDP_TUNNEL_MSS(type) * 2, \
+ .r_num_mss = 2, \
+ .is_tap = true, \
+ }; \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_maxbytes) { \
+ /* GSO: send max bytes */ \
+ .tunnel_type = type, \
+ .gso_size = UDP_TUNNEL_MSS(type), \
+ .data_size = UDP_TUNNEL_MAX(type, true), \
+ .r_num_mss = UDP_TUNNEL_MAX(type, true) / \
+ UDP_TUNNEL_MSS(type) + 1, \
+ .is_tap = true, \
+ }; \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_over_maxbytes) { \
+ /* GSO: send oversize max bytes: fail */ \
+ .tunnel_type = type, \
+ .gso_size = UDP_TUNNEL_MSS(type), \
+ .data_size = ETH_MAX_MTU, \
+ .r_num_mss = ETH_MAX_MTU / UDP_TUNNEL_MSS(type) + 1, \
+ .is_tap = true, \
+ }; \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_maxsegs) { \
+ /* GSO: send max number of min sized segments */ \
+ .tunnel_type = type, \
+ .gso_size = 1, \
+ .data_size = UDP_TUNNEL_MAX_SEGMENTS, \
+ .r_num_mss = UDP_TUNNEL_MAX_SEGMENTS, \
+ .is_tap = true, \
+ }; \
+ FIXTURE_VARIANT_ADD(tun_vnet_udptnl, desc##_5byte) { \
+ /* GSO: send 5 bytes, gso 2 bytes */ \
+ .tunnel_type = type, \
+ .gso_size = 2, \
+ .data_size = 5, \
+ .r_num_mss = 3, \
+ .is_tap = true, \
+ } /* clang-format on */
TUN_VNET_UDPTNL_VARIANT_ADD(UDP_TUNNEL_VXLAN_4IN4, 4in4);
TUN_VNET_UDPTNL_VARIANT_ADD(UDP_TUNNEL_VXLAN_6IN4, 6in4);
@@ -860,4 +956,19 @@ TEST_F(tun_vnet_udptnl, recv_gso_packet)
}
}
+XFAIL_ADD(tun_vnet_udptnl, 4in4_nogsosz_gtmss, recv_gso_packet);
+XFAIL_ADD(tun_vnet_udptnl, 6in4_nogsosz_gtmss, recv_gso_packet);
+XFAIL_ADD(tun_vnet_udptnl, 4in6_nogsosz_gtmss, recv_gso_packet);
+XFAIL_ADD(tun_vnet_udptnl, 6in6_nogsosz_gtmss, recv_gso_packet);
+
+XFAIL_ADD(tun_vnet_udptnl, 4in4_over_maxbytes, send_gso_packet);
+XFAIL_ADD(tun_vnet_udptnl, 6in4_over_maxbytes, send_gso_packet);
+XFAIL_ADD(tun_vnet_udptnl, 4in6_over_maxbytes, send_gso_packet);
+XFAIL_ADD(tun_vnet_udptnl, 6in6_over_maxbytes, send_gso_packet);
+
+XFAIL_ADD(tun_vnet_udptnl, 4in4_over_maxbytes, recv_gso_packet);
+XFAIL_ADD(tun_vnet_udptnl, 6in4_over_maxbytes, recv_gso_packet);
+XFAIL_ADD(tun_vnet_udptnl, 4in6_over_maxbytes, recv_gso_packet);
+XFAIL_ADD(tun_vnet_udptnl, 6in6_over_maxbytes, recv_gso_packet);
+
TEST_HARNESS_MAIN
--
2.49.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels
2025-11-17 6:24 [PATCH net-next 0/8] selftest: Extend tun/virtio coverage for GSO over UDP tunnels xu du
` (7 preceding siblings ...)
2025-11-17 6:25 ` [PATCH net-next 8/8] selftest: tun: Add test data for success and failure paths xu du
@ 2025-11-17 15:40 ` Jakub Kicinski
8 siblings, 0 replies; 10+ messages in thread
From: Jakub Kicinski @ 2025-11-17 15:40 UTC (permalink / raw)
To: xu du; +Cc: davem, edumazet, pabeni, netdev
On Mon, 17 Nov 2025 14:24:56 +0800 xu du wrote:
> This patch series increases test coverage for the tun/virtio use-case.
>
> The primary goal is to add test validation for GSO when operating over
> UDP tunnels, a scenario which is not currently covered.
>
> The design strategy is to extend the existing tun/tap testing infrastructure
> to support this new use-case, rather than introducing a new or parallel framework.
> This allows for better integration and re-use of existing test logic.
Hi! I haven't looked closely but the new cases don't pass in our CI:
https://netdev-3.bots.linux.dev/vmksft-net/results/389682/14-tun/stdout
Please see these instructions for how to repro:
https://github.com/linux-netdev/nipa/wiki/How-to-run-netdev-selftests-CI-style
--
pw-bot: cr
^ permalink raw reply [flat|nested] 10+ messages in thread