mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH ipsec 0/2] xfrm: iptfs: charge shared fragments to the inner packet's truesize
@ 2026-11-29  2:00 Andrii Tereshchenko
  2026-11-29  2:00 ` [PATCH ipsec 1/2] " Andrii Tereshchenko
  2026-11-29  2:00 ` [PATCH ipsec 2/2] selftests: net: test the truesize of IP-TFS inner packets Andrii Tereshchenko
  0 siblings, 2 replies; 3+ messages in thread
From: Andrii Tereshchenko @ 2026-11-29  2:00 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman, Christian Hopps, netdev, ipsec, linux-kernel,
	Andrii Tereshchenko

1/2 makes the inner packets that share the outer packet's page fragments
pay for them in truesize. Without it, any sender whose large packets
cross the tunnel can trip a WARN in skb_try_coalesce() on the receiver,
and receive queues hold more than they are charged for. 2/2 adds a
selftest.

Andrii Tereshchenko (2):
  xfrm: iptfs: charge shared fragments to the inner packet's truesize
  selftests: net: test the truesize of IP-TFS inner packets

 MAINTAINERS                                   |   1 +
 net/xfrm/xfrm_iptfs.c                         |   8 +
 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 ++++++++
 7 files changed, 446 insertions(+)
 create mode 100644 tools/testing/selftests/net/xfrm_iptfs_truesize.c
 create mode 100755 tools/testing/selftests/net/xfrm_iptfs_truesize.sh


base-commit: 1213df0bc5993e479706c5eba5830243d778f164
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH ipsec 1/2] xfrm: iptfs: charge shared fragments to the inner packet's truesize
  2026-11-29  2:00 [PATCH ipsec 0/2] xfrm: iptfs: charge shared fragments to the inner packet's truesize Andrii Tereshchenko
@ 2026-11-29  2:00 ` Andrii Tereshchenko
  2026-11-29  2:00 ` [PATCH ipsec 2/2] selftests: net: test the truesize of IP-TFS inner packets Andrii Tereshchenko
  1 sibling, 0 replies; 3+ messages in thread
From: Andrii Tereshchenko @ 2026-11-29  2:00 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman, Christian Hopps, netdev, ipsec, linux-kernel,
	Andrii Tereshchenko, stable

iptfs_skb_add_frags() gives an inner packet references to page fragments
of the outer packet and adds their length to its len and data_len, but
not to its truesize. Socket receive queues and IP fragment queues then
take these packets at a fraction of the memory they pin, and reassembling
inner IP fragments trips the truesize check in skb_try_coalesce(). Any
host whose large packets cross the tunnel can trigger it, on a receiver
whose NIC driver builds skbs with page fragments:

  WARNING: net/core/skbuff.c:6355 at skb_try_coalesce+0x4bd/0x4d0
   inet_frag_reasm_finish+0x12e/0x2e0
   ipv6_frag_rcv+0x9b1/0xda0

With panic_on_warn that brings the receiver down. Charge each shared
fragment the bytes it references, as skb_segment() does for the
fragments it shares; their real truesize is not known here.

Made with an AI coding assistant, reviewed by the submitter.

Fixes: 5f2b6a909574 ("xfrm: iptfs: add skb-fragment sharing code")
Cc: stable@vger.kernel.org
Signed-off-by: Andrii Tereshchenko <opensource@andrii.net>
---
Tested in KVM guests with 2/2 on ipsec master: without this patch it
trips the warning and finds a socket charged 1.9 MB for 2.9 MB of
queued datagrams; with it and panic_on_warn=1 it passes, two boots
each way, with lockdep, KASAN and UBSAN clean and xfrm_policy.sh /
xfrm_state.sh passing. Under KCSAN-strict it reports nothing beyond
the unpatched baseline (tick_nohz boot noise; nothing in xfrm). The
proposed IP-TFS KUnit tests cover this too (iptfs_add_frags_test and
two more).

Found by an AI code review agent. Validated with the 2/2 selftest on
ipsec master, no kernel change or fault injection. Tested against
that selftest.

 net/xfrm/xfrm_iptfs.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index 0313a43aa8a97..0b9642208c71f 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -460,6 +460,12 @@ static bool iptfs_skb_can_add_frags(const struct sk_buff *skb,
  * that the destination @skb is compatible with the walk and has space in the
  * array for the to be added frag references.
  *
+ * The truesize of the source fragments is not known here, so @skb is charged
+ * for the bytes it references, as skb_segment() does for the fragments it
+ * shares. The source keeps its truesize: from here on it never enters
+ * socket, fragment or qdisc memory accounting, so the bytes are charged
+ * once.
+ *
  * Return: The number of bytes not added to @skb b/c we reached the end of the
  * walk before adding all of @len.
  */
@@ -495,12 +499,14 @@ static int iptfs_skb_add_frags(struct sk_buff *skb,
 			tofrag->len = len;
 			skb->len += len;
 			skb->data_len += len;
+			skb->truesize += len;
 			return 0;
 		}
 		/* advance to next source fragment */
 		len -= fraglen;			/* careful, use dst bv_len */
 		skb->len += fraglen;		/* careful, "   "    "     */
 		skb->data_len += fraglen;	/* careful, "   "    "     */
+		skb->truesize += fraglen;	/* careful, "   "    "     */
 		walk->past += frag->len;	/* careful, use src bv_len */
 		walk->fragi++;
 	}
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH ipsec 2/2] selftests: net: test the truesize of IP-TFS inner packets
  2026-11-29  2:00 [PATCH ipsec 0/2] xfrm: iptfs: charge shared fragments to the inner packet's truesize Andrii Tereshchenko
  2026-11-29  2:00 ` [PATCH ipsec 1/2] " Andrii Tereshchenko
@ 2026-11-29  2:00 ` Andrii Tereshchenko
  1 sibling, 0 replies; 3+ messages in thread
From: Andrii Tereshchenko @ 2026-11-29  2:00 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S. Miller
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman, Christian Hopps, netdev, ipsec, linux-kernel,
	Andrii Tereshchenko

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 <opensource@andrii.net>
---
 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 <dsahern@kernel.org>
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 <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/if_packet.h>
+#include <linux/if_tun.h>
+#include <linux/rtnetlink.h>
+#include <linux/sock_diag.h>
+#include <linux/xfrm.h>
+#include <net/if.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#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 <cmd...>: 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 <name> <status>
+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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-29  2:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29  2:00 [PATCH ipsec 0/2] xfrm: iptfs: charge shared fragments to the inner packet's truesize Andrii Tereshchenko
2026-11-29  2:00 ` [PATCH ipsec 1/2] " Andrii Tereshchenko
2026-11-29  2:00 ` [PATCH ipsec 2/2] selftests: net: test the truesize of IP-TFS inner packets Andrii Tereshchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®