From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.228 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795917656; b=XGl1XlU8UOqryNve1Z7s0cVDP23T05WRAtHle72McozySiBY48jd2w5QDZwEKpOOm7aY UPtFSPMdfkHFyOqye7sdaFrRWOImy0lN+GvUMW+P1lvci7psOz6TZdINYJOv0R80bi0Rj 7FNyqGOc7pK7hCoqfOHXZN/Bzu0g8xRpdJvVMDUO+8hVX9Po9eBb8+htkzNxMuaPbM7cr VhZrobEl4U5jY8b4EkqEuWGJzp3/85gOhmyGXzeXlQl9pXfhyt7hAoxZecyidc8VAKeZ+ BYikaGfegI8+W3pT+e0LOdeIQCi9pznnvGXK5aVHnT0FwEzpTJBU4VFGnjg5L6H2Cig== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795917656; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: X-Relaying-Domain:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=cISaWFyh0rOdQQDdBipv7uHAGVBJ2Ely0yEXUpKpLy0=; b=VH5R4CXPIt4XpCIPTlp2qgtUAMGTkHeHuZVTplu4Sni+Lxxg9LR1FHoDzSIJyajDXesG bVfUL8H82CO68nZSOc+02EbCTumqRciWmfHVvD21maVGJ9/MevS9EXTka028EftISBr3N 4FsQLw+uePNp4+UZCQ7mPHlffTbbc3wVzldY5jsStkx0j2Yptl70wLlUQjSJhzy7ZovRJ zA6zw/mFz3smebEJY2UhAqKCjR3miPA2T+WtdHtJe1PNVRAekwHj+YXE9+WyDVuXYnq8Q 0WmuIH+6F+GG4BAIdGxTeE4p4iV1dzhKn53jBnjk02lKND9FGQcKL+sME8gDhyTxvKw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=andrii.net; spf=pass smtp.mailfrom=andrii.net; dkim=pass header.d=andrii.net header.i=@andrii.net header.a=rsa-sha256 header.s=gmail-2025 header.b="ns9/B9o/"; arc=none smtp.remote-ip=209.85.222.228 Received: from mail-qk1-f228.google.com (mail-qk1-f228.google.com [209.85.222.228]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26336606A68D for ; Sun, 29 Nov 2026 02:00:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 26336606A68D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=andrii.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=andrii.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=andrii.net header.i=@andrii.net header.a=rsa-sha256 header.s=gmail-2025 header.b=ns9/B9o/ Received: by mail-qk1-f228.google.com with SMTP id af79cd13be357-94961ed97bcso930687385a.1 for ; Sat, 28 Nov 2026 18:00:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=andrii.net; s=gmail-2025; t=1795917655; x=1796522455; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=cISaWFyh0rOdQQDdBipv7uHAGVBJ2Ely0yEXUpKpLy0=; b=ns9/B9o/JI2VwUK+QJNf5mUuEQoD11yC55+PDy6kmGMtdDRl0V42FuruXW0CocNa5Q wXJEdMJPbCgCYpq0cLPVZZhp6FgIkz8Dm7gsrRksqDBukSVedBUKgfLAL1NSnZAR0HsH 6cZ7Z+OxYpR8GJRatuy8AMbVFN/lF92JB2oSD3S0nSF/dW4LcBgwP1Z4dSTppaLVZvnD HI3IzVuwpsThQrAwjTIHEwyouEIy26ijU/pzCMcMZXyTOGGIiMcWNQpz0+tJDYVuv1uJ LLte1wCTLXcE+K4mJ5xGbimPiUnp2YykK4HhHZuUuvzXD2MiuqZ6JzcmtfSKh8n2YB/L 3elA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795917655; x=1796522455; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=cISaWFyh0rOdQQDdBipv7uHAGVBJ2Ely0yEXUpKpLy0=; b=0XhPLR/+0fhFwX7POsyNFRiImK8BSrfUPDEqAMj+yv5uXgmqytDwe8eXc7YFEV+96h G/5HbD3aAMhKFKeMbajbznYyAl12Qm1fkWql9mbzU7iPDyJRUI9rtwcBIddw/kYSkMrU /bdV0efpCLgtPR6WYBVDFTvFVtskwcE6onELrYvk4zqZ0uS3GCbdfMlMGUuSmv5tFD4l dLdPrrDrMSER/Wn65eIJ6I0Wtk9JsnCUqqK1nVQSuDleyQMHwXaH710Bb8+8b9x1dshC s/1fS+b4NsLU8N6pHDx1EVNUqNf8ra+35ZKn54Qx/z2mWN3/CjsOe40kHg05qMWJtErQ LbsA== X-Forwarded-Encrypted: i=1; AL+hErE5uq1c372aHX8soSjtvNALhERzUDpSnB/6glZ9MYVijPMpUDESRADHnATmN10XgkdsUXZZ8aToStpZYko=@vger.kernel.org X-Gm-Message-State: APuU4Z+5x5IekLRZOoHknGnWWSC37rNmqHUbrNUjCJrg310LhWOerLla 9x1pTHt4d/AfnZwieKWlX9NL1oGHA3l0H35UatWK2/w1xNTy/WfraSD8IpAWlxpHJief6z6Gl6M GHKbfeopO4XBD7b1jKsAlHdkJM+FplZJLvIUdQ+y2q+cQe2l3IfTh X-Gm-Gg: AZFdTZdBAup/rGf1W6tfRxKMfEDpI4teTcU4VJuBd/UOxRNtVNmOOl/vTFK3JTf+6+l taxoVk/7tmT1crEiQukwGHazH8asUiQt0xcPkL3TcdRXK7RSpYPyLigjViLqy4D1fzNpJABn2nv Uq60Gnnkv+sGjQt5y2rIppP0hdXgIAuW/5BboLB2T+IFeF+DhFp+lEioDZr+16H3n5+qEvslJYc KTtR+wtSzNv0Ia5Yms6CIVbjwZAzfrDI/7xvQoDoHIwn4uamSjVZxVMOK/dAE9rECEJOM2Rhq5T YgZxvp0Vw6BWhS3k8bieBApmUlBuVBy+3uREmFty1GkdKsWcn3wSYSJG4Ly4d4/1o9skBRgTZm/ UdE0fioYbuKtPn4sGi1pazNYLoYA= X-Received: by 2002:a05:620a:260a:b0:944:1291:6cd9 with SMTP id af79cd13be357-949bab7257amr5161455185a.68.1795917654874; Sat, 28 Nov 2026 18:00:54 -0800 (PST) Received: from andrii.ai (mac.hestia.andrii.net. [2602:f337:ed:4865:7374:6961:204d:6163]) by smtp-relay.gmail.com with ESMTPS id af79cd13be357-949bb3de1c9sm246959985a.17.2026.11.28.18.00.52 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 28 Nov 2026 18:00:54 -0800 (PST) X-Relaying-Domain: andrii.net From: Andrii Tereshchenko To: Steffen Klassert , Herbert Xu , "David S. Miller" Cc: Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Simon Horman , Christian Hopps , netdev@vger.kernel.org, ipsec@lists.linux.dev, linux-kernel@vger.kernel.org, Andrii Tereshchenko Subject: [PATCH ipsec 2/2] selftests: net: test the truesize of IP-TFS inner packets Date: Sat, 28 Nov 2026 19:00:44 -0700 Message-ID: <94fca5ab-9c06-4319-8e48-ce3a4f5545ec@mid.andrii.net> X-Mailer: git-send-email 2.56.0 In-Reply-To: <11df3eb5-b410-41bb-9198-b6c64654d029@mid.andrii.net> References: <11df3eb5-b410-41bb-9198-b6c64654d029@mid.andrii.net> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit No selftest covers IP-TFS, and ip(8) cannot configure it yet. Add xfrm_iptfs_truesize.sh and a helper that adds the SAs over xfrm netlink. A sender namespace only sends packets, which reach the receiver through a tap device, as tun builds skbs with a page fragment head: frags 3000-byte IPv6 and 3500-byte IPv4 datagrams, which the sender fragments; their reassembly must not warn queue datagrams for a socket that does not read them; it must be charged at least the bytes it holds Both fail without the fix. Made with an AI coding assistant, reviewed by the submitter. Signed-off-by: Andrii Tereshchenko --- MAINTAINERS | 1 + tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 2 + tools/testing/selftests/net/config | 2 + .../selftests/net/xfrm_iptfs_truesize.c | 297 ++++++++++++++++++ .../selftests/net/xfrm_iptfs_truesize.sh | 135 ++++++++ 6 files changed, 438 insertions(+) create mode 100644 tools/testing/selftests/net/xfrm_iptfs_truesize.c create mode 100755 tools/testing/selftests/net/xfrm_iptfs_truesize.sh diff --git a/MAINTAINERS b/MAINTAINERS index 09713dc4c6a77..8de4e998c559e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -19525,6 +19525,7 @@ F: net/ipv6/xfrm* F: net/key/ F: net/xfrm/ F: tools/testing/selftests/net/ipsec.c +F: tools/testing/selftests/net/xfrm_iptfs_truesize* NETWORKING [IPv4/IPv6] M: David Ahern diff --git a/tools/testing/selftests/net/.gitignore b/tools/testing/selftests/net/.gitignore index 21412510ac26b..ae2234aa76e9e 100644 --- a/tools/testing/selftests/net/.gitignore +++ b/tools/testing/selftests/net/.gitignore @@ -60,3 +60,4 @@ udp_splice_checksum udpgso udpgso_bench_rx udpgso_bench_tx +xfrm_iptfs_truesize diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index 9180b9bec5155..1556c127444f8 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -135,6 +135,7 @@ TEST_PROGS := \ vrf-xfrm-tests.sh \ vrf_route_leaking.sh \ vrf_strict_mode_test.sh \ + xfrm_iptfs_truesize.sh \ xfrm_policy.sh \ xfrm_state.sh \ # end of TEST_PROGS @@ -178,6 +179,7 @@ TEST_GEN_FILES := \ udpgso \ udpgso_bench_rx \ udpgso_bench_tx \ + xfrm_iptfs_truesize \ # end of TEST_GEN_FILES TEST_GEN_PROGS := \ diff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config index d4416f8bdcfd1..05ab1a05bd0dd 100644 --- a/tools/testing/selftests/net/config +++ b/tools/testing/selftests/net/config @@ -18,6 +18,7 @@ CONFIG_DUMMY=y CONFIG_GENEVE=m CONFIG_GTP=m CONFIG_IFB=y +CONFIG_INET6_ESP=m CONFIG_INET_DIAG=y CONFIG_INET_ESP=y CONFIG_INET_ESP_OFFLOAD=y @@ -138,4 +139,5 @@ CONFIG_VETH=y CONFIG_VLAN_8021Q=y CONFIG_VXLAN=m CONFIG_XFRM_INTERFACE=m +CONFIG_XFRM_IPTFS=m CONFIG_XFRM_USER=m diff --git a/tools/testing/selftests/net/xfrm_iptfs_truesize.c b/tools/testing/selftests/net/xfrm_iptfs_truesize.c new file mode 100644 index 0000000000000..2d85bd47e5f39 --- /dev/null +++ b/tools/testing/selftests/net/xfrm_iptfs_truesize.c @@ -0,0 +1,297 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Helper for xfrm_iptfs_truesize.sh. Addresses are IPv4 or IPv6. + * out|in SRC DST + * add one side of an IP-TFS SA from SRC to DST, with AES-GCM and a + * zero key, and a policy for packets to DST through it + * send DST COUNT SIZE... + * send COUNT times one datagram of each SIZE to DST, port 5000, one + * per millisecond, allowing fragmentation + * queue + * let the datagrams for port 5000 queue up without reading them + * until none has arrived for 3 s; pass if the socket was charged at + * least the bytes it held + * wire NETNS DEV TAP + * copy every frame that arrives on DEV in NETNS into a new tap + * device TAP, here, which gives skbs with a page fragment head + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +static char buf[65536]; +static struct nlmsghdr *nh = (void *)buf; + +/* Start a request with a payload of len bytes and return the payload. */ +static void *request(int type, int flags, int len) +{ + memset(buf, 0, 4096); + nh->nlmsg_type = type; + nh->nlmsg_flags = NLM_F_REQUEST | flags; + nh->nlmsg_len = NLMSG_LENGTH(len); + return NLMSG_DATA(nh); +} + +/* Append an attribute with len bytes of data and return the data. */ +static void *attr(int type, int len) +{ + struct rtattr *a = (void *)buf + NLMSG_ALIGN(nh->nlmsg_len); + + a->rta_type = type; + a->rta_len = RTA_LENGTH(len); + nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + RTA_ALIGN(a->rta_len); + return RTA_DATA(a); +} + +/* Send the request and read the reply into buf. Return 0 or -errno. */ +static int talk(int nl) +{ + if (send(nl, buf, nh->nlmsg_len, 0) < 0 || + recv(nl, buf, sizeof(buf), 0) < 0) + return -errno; + if (nh->nlmsg_type == NLMSG_ERROR) + return ((struct nlmsgerr *)NLMSG_DATA(nh))->error; + return 0; +} + +/* Parse an IPv4 or IPv6 address into a, return its family or 0. */ +static int addr(const char *s, xfrm_address_t *a) +{ + memset(a, 0, sizeof(*a)); + if (inet_pton(AF_INET, s, &a->a4) == 1) + return AF_INET; + if (inet_pton(AF_INET6, s, a->a6) == 1) + return AF_INET6; + return 0; +} + +/* Add one side of the SA and its policy. Return 0 or -errno. */ +static int add_sa(char **argv) +{ + struct xfrm_lifetime_cfg unlimited = { + .soft_byte_limit = XFRM_INF, .hard_byte_limit = XFRM_INF, + .soft_packet_limit = XFRM_INF, .hard_packet_limit = XFRM_INF, + }; + int nl = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM); + int out = !strcmp(argv[1], "out"), family, err, len; + struct xfrm_userpolicy_info *pol; + struct xfrm_usersa_info *sa; + struct xfrm_algo_aead *aead; + struct xfrm_user_tmpl *tmpl; + xfrm_address_t src, dst; + struct rtattr *a; + + family = addr(argv[2], &src); + if (nl < 0 || !family || addr(argv[3], &dst) != family) + return -EINVAL; + sa = request(XFRM_MSG_NEWSA, NLM_F_ACK, sizeof(*sa)); + sa->id.daddr = dst; + sa->id.spi = htonl(0x1000); + sa->id.proto = IPPROTO_ESP; + sa->saddr = src; + sa->family = family; + sa->mode = XFRM_MODE_IPTFS; + sa->lft = unlimited; + *(__u8 *)attr(XFRMA_SA_DIR, 1) = out ? XFRM_SA_DIR_OUT : XFRM_SA_DIR_IN; + aead = attr(XFRMA_ALG_AEAD, sizeof(*aead) + 20); + strcpy(aead->alg_name, "rfc4106(gcm(aes))"); + aead->alg_key_len = 160; + aead->alg_icv_len = 128; + /* pack the inner packets into outer ones, splitting many of them */ + if (out) + *(__u32 *)attr(XFRMA_IPTFS_INIT_DELAY, 4) = 5000; + err = talk(nl); + if (err) + return err; + + /* + * A kernel without IP-TFS may accept the SA and then warn on its + * first packet: the SA is IP-TFS only if the kernel reports its + * IP-TFS options. + */ + request(XFRM_MSG_GETSA, NLM_F_DUMP, 0); + err = talk(nl); + if (err) + return err; + a = (void *)buf + NLMSG_SPACE(sizeof(*sa)); + len = nh->nlmsg_len - NLMSG_SPACE(sizeof(*sa)); + for (; RTA_OK(a, len); a = RTA_NEXT(a, len)) + if (a->rta_type >= XFRMA_IPTFS_DROP_TIME && + a->rta_type <= XFRMA_IPTFS_PKT_SIZE) + break; + if (!RTA_OK(a, len)) + return -ENOPROTOOPT; + + pol = request(XFRM_MSG_NEWPOLICY, NLM_F_ACK, sizeof(*pol)); + pol->sel.family = family; + pol->sel.daddr = dst; + pol->sel.prefixlen_d = family == AF_INET ? 32 : 128; + pol->lft = unlimited; + pol->dir = out ? XFRM_POLICY_OUT : XFRM_POLICY_IN; + tmpl = attr(XFRMA_TMPL, sizeof(*tmpl)); + tmpl->id.daddr = dst; + tmpl->id.proto = IPPROTO_ESP; + tmpl->saddr = src; + tmpl->family = family; + tmpl->mode = XFRM_MODE_IPTFS; + tmpl->aalgos = ~0; + tmpl->ealgos = ~0; + tmpl->calgos = ~0; + return talk(nl); +} + +static int cmd_send(int argc, char **argv) +{ + struct sockaddr_in6 to6 = { .sin6_family = AF_INET6, .sin6_port = htons(5000) }; + struct sockaddr_in to = { .sin_family = AF_INET, .sin_port = htons(5000) }; + int count = atoi(argv[3]), i, j, size, udp, val = IP_PMTUDISC_DONT; + int salen = sizeof(to); + void *sa = &to; + + if (inet_pton(AF_INET, argv[2], &to.sin_addr) == 1) { + udp = socket(AF_INET, SOCK_DGRAM, 0); + setsockopt(udp, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val)); + } else if (inet_pton(AF_INET6, argv[2], &to6.sin6_addr) == 1) { + udp = socket(AF_INET6, SOCK_DGRAM, 0); + sa = &to6; + salen = sizeof(to6); + } else { + return KSFT_FAIL; + } + for (j = 0; j < count; j++) { + for (i = 4; i < argc; i++) { + size = atoi(argv[i]); + if (sendto(udp, buf, size, 0, sa, salen) != size) { + perror("sendto"); + return KSFT_FAIL; + } + } + usleep(1000); + } + return KSFT_PASS; +} + +static int sock(void) +{ + struct sockaddr_in6 addr = { .sin6_family = AF_INET6, .sin6_port = htons(5000) }; + int udp = socket(AF_INET6, SOCK_DGRAM, 0), val = 0; + + setsockopt(udp, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val)); + val = 32 << 20; + setsockopt(udp, SOL_SOCKET, SO_RCVBUFFORCE, &val, sizeof(val)); + if (bind(udp, (void *)&addr, sizeof(addr))) + exit(KSFT_FAIL); + return udp; +} + +static unsigned int rmem(int udp) +{ + __u32 mem[SK_MEMINFO_VARS]; + socklen_t len = sizeof(mem); + + if (getsockopt(udp, SOL_SOCKET, SO_MEMINFO, mem, &len)) + exit(KSFT_FAIL); + return mem[SK_MEMINFO_RMEM_ALLOC]; +} + +static int cmd_queue(void) +{ + unsigned int charged, last = 0, quiet = 0; + long long bytes = 0; + int udp = sock(), n = 0, len; + + /* wait for the queue to stop growing */ + while (quiet < 30) { + usleep(100000); + charged = rmem(udp); + quiet = charged == last ? quiet + 1 : 0; + last = charged; + } + while ((len = recv(udp, buf, sizeof(buf), MSG_DONTWAIT)) >= 0) { + bytes += len; + n++; + } + printf("%d datagrams of %lld bytes in all were charged %u bytes\n", + n, bytes, charged); + return n && charged >= bytes ? KSFT_PASS : KSFT_FAIL; +} + +static int cmd_wire(char **argv) +{ + struct sockaddr_ll sll = { .sll_family = AF_PACKET, + .sll_protocol = htons(ETH_P_ALL) }; + int tap = open("/dev/net/tun", O_RDWR | O_NONBLOCK); + int ns = open(argv[2], O_RDONLY); + struct ifreq ifr = { }; + socklen_t slen; + int pkt, len; + + if (tap < 0 || ns < 0 || setns(ns, CLONE_NEWNET)) { + perror("wire"); + return KSFT_FAIL; + } + pkt = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); + sll.sll_ifindex = if_nametoindex(argv[3]); + if (pkt < 0 || !sll.sll_ifindex || bind(pkt, (void *)&sll, sizeof(sll))) { + perror(argv[3]); + return KSFT_FAIL; + } + /* + * The tap appears in the namespace the helper started in, where + * /dev/net/tun was opened. + */ + ifr.ifr_flags = IFF_TAP | IFF_NO_PI; + strncpy(ifr.ifr_name, argv[4], IFNAMSIZ - 1); + if (ioctl(tap, TUNSETIFF, &ifr)) { + perror("TUNSETIFF"); + return KSFT_FAIL; + } + for (;;) { + slen = sizeof(sll); + len = recvfrom(pkt, buf, sizeof(buf), 0, (void *)&sll, &slen); + if (len < 0) + return KSFT_FAIL; + if (sll.sll_pkttype == PACKET_OUTGOING) + continue; + /* A nonblocking write: tun builds an skb with a page frag head. */ + if (write(tap, buf, len) < 0 && errno != EIO) + return KSFT_FAIL; + } +} + +int main(int argc, char **argv) +{ + int err; + + if (argc == 4 && (!strcmp(argv[1], "out") || !strcmp(argv[1], "in"))) { + err = add_sa(argv); + if (err) { + printf("SKIP: no IP-TFS SA: %s\n", strerror(-err)); + return KSFT_SKIP; + } + return KSFT_PASS; + } + if (argc >= 5 && !strcmp(argv[1], "send")) + return cmd_send(argc, argv); + if (argc == 2 && !strcmp(argv[1], "queue")) + return cmd_queue(); + if (argc == 5 && !strcmp(argv[1], "wire")) + return cmd_wire(argv); + fprintf(stderr, "%s: unknown command, see the comment at its top\n", argv[0]); + return KSFT_FAIL; +} diff --git a/tools/testing/selftests/net/xfrm_iptfs_truesize.sh b/tools/testing/selftests/net/xfrm_iptfs_truesize.sh new file mode 100755 index 0000000000000..c71389e0e5178 --- /dev/null +++ b/tools/testing/selftests/net/xfrm_iptfs_truesize.sh @@ -0,0 +1,135 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# shellcheck disable=SC2154 # variables assigned by setup_ns +# +# IP-TFS (RFC 9347) inner packets that share the page fragments of the outer +# packet. c sends through s, the remote IP-TFS peer, which only sends packets +# over its link to w; a tap device on g receives every frame that arrives on +# w, as tun builds skbs with a page fragment head, like many NIC drivers do. +# IP-TFS did not charge the shared fragments to the inner packets' truesize: +# frags c sends 3000-byte IPv6 and 3500-byte IPv4 datagrams, which it +# fragments. Reassembling them on g warned in skb_try_coalesce(). +# queue c sends 1472-byte datagrams, which IP-TFS splits across outer +# packets, to a socket on g that does not read them. It was charged +# less than the bytes it held. +# xfrm_iptfs_truesize adds the SAs, as ip(8) cannot configure IP-TFS yet, and +# sends and counts the datagrams. + +source lib.sh + +ret=$ksft_pass + +sa() +{ + local ns=$1 + + shift + ip netns exec "$ns" ./xfrm_iptfs_truesize "$@" || exit +} + +# c --- s --- w, and g's tap t0 gets every frame that arrives on w0 +setup() +{ + setup_ns c s w g || exit "$ksft_skip" + ip link add c0 netns "$c" type veth peer name s1 netns "$s" + ip link add s0 netns "$s" type veth peer name w0 netns "$w" + ip -n "$c" addr add 192.0.2.1/24 dev c0 + ip -n "$c" addr add 2001:db8:c::1/64 dev c0 nodad + ip -n "$s" addr add 192.0.2.2/24 dev s1 + ip -n "$s" addr add 2001:db8:c::2/64 dev s1 nodad + ip -n "$s" addr add 198.51.100.1/24 dev s0 + ip -n "$s" addr add 2001:db8::1/64 dev s0 nodad + ip -n "$c" link set c0 up + ip -n "$s" link set s1 up + ip -n "$s" link set s0 up + ip -n "$w" link set w0 up + ip -n "$c" route add default via 192.0.2.2 + ip -n "$c" -6 route add default via 2001:db8:c::2 + ip netns exec "$s" sysctl -qw net.ipv4.ip_forward=1 + ip netns exec "$s" sysctl -qw net.ipv6.conf.all.forwarding=1 + ip netns exec "$g" ./xfrm_iptfs_truesize wire "/run/netns/$w" w0 t0 & + wpid=$! + slowwait 5 ip -n "$g" link show t0 > /dev/null || exit "$ksft_skip" + ip -n "$g" link set t0 address 02:00:00:00:00:02 up + ip -n "$g" addr add 198.51.100.2/24 dev t0 + ip -n "$g" addr add 2001:db8::2/64 dev t0 nodad + ip -n "$s" neigh add 198.51.100.2 lladdr 02:00:00:00:00:02 dev s0 + ip -n "$s" neigh add 2001:db8::2 lladdr 02:00:00:00:00:02 dev s0 + # wait until c0 and s0 can send, so that no early packet is lost + slowwait 5 sh -c "! tc -n $c qdisc show dev c0 | grep -q noop" + slowwait 5 sh -c "! tc -n $s qdisc show dev s0 | grep -q noop" + sa "$g" in 198.51.100.1 198.51.100.2 + sa "$s" out 198.51.100.1 198.51.100.2 + sa "$g" in 2001:db8::1 2001:db8::2 + sa "$s" out 2001:db8::1 2001:db8::2 +} + +# start : run the helper on g in the background +start() +{ + ip netns exec "$g" ./xfrm_iptfs_truesize "$@" & + rpid=$! + slowwait 5 sh -c "ip netns exec $g ss -Hlun 'sport = :5000' | grep -q ." +} + +# result +result() +{ + if [ "$2" = 0 ]; then + echo "PASS: $1" + else + echo "FAIL: $1" + ret=$ksft_fail + fi + cleanup +} + +cleanup() +{ + [ -n "${wpid:-}" ] && kill "$wpid" + wpid= + cleanup_all_ns +} + +warnings() +{ + dmesg | grep -c 'WARNING:.*skb_try_coalesce' +} + +# This counts the datagrams that g reassembled; whether they arrive intact is +# for other tests. +test_frags() +{ + local before n + + before=$(warnings) + setup + ip netns exec "$c" ./xfrm_iptfs_truesize send 2001:db8::2 50 3000 + ip netns exec "$c" ./xfrm_iptfs_truesize send 198.51.100.2 50 3500 + sleep 1 + n=$(ip netns exec "$g" nstat -az IpReasmOKs Ip6ReasmOKs | + awk '/ReasmOKs/ { n += $2 } END { print n + 0 }') + echo "g reassembled $n of 100 datagrams" + if [ "$(warnings)" != "$before" ]; then + echo "skb_try_coalesce() warned" + n=0 + fi + [ "$n" = 100 ] + result frags $? +} + +test_queue() +{ + setup + start queue + ip netns exec "$c" ./xfrm_iptfs_truesize send 198.51.100.2 2000 1472 + wait "$rpid" + result queue $? +} + +trap cleanup EXIT + +test_frags +test_queue + +exit "$ret" -- 2.53.0