Skip to contentSkip to conversation

netdevsim: add TLS device offload emulation and test

PATCH Superseded Jiayuan Chen 4 participants 8 messages Sep 1 – Sep 4

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover netdevsim: add TLS device offload emulation and test

It helped me find
commit b17cf742eaad ("tls: device: fix out-of-bounds write in tls_append_frag()").

So I think it is worth sending a formal patchset.

The kTLS device offload path in net/tls/tls_device.c has no test coverage upstream -- selftests/net/tls.c is software only, and the offload code needs a driver that implements it.

So teach netdevsim to fake it. Patch 1 implements tlsdev_ops and advertises the TLS features, patch 2 adds a selftest.

No crypto is done: TX puts the plaintext record on the wire, RX just sets skb->decrypted. That only works netdevsim to netdevsim, but it covers record assembly and the offload lifecycle. Real ciphertext, and more on top of it, can come later.

Jiayuan Chen (2):
netdevsim: add TLS device offload emulation
selftests: netdevsim: add a kTLS device offload test

 drivers/net/Kconfig                           |   1 +
 drivers/net/netdevsim/Makefile                |   4 +
 drivers/net/netdevsim/netdev.c                |   5 +
 drivers/net/netdevsim/netdevsim.h             |  35 ++
 drivers/net/netdevsim/tls.c                   | 482 +++++++++++++++++
 .../drivers/net/netdevsim/.gitignore          |   2 +
 .../selftests/drivers/net/netdevsim/Makefile  |   7 +
 .../selftests/drivers/net/netdevsim/config    |   2 +
 .../selftests/drivers/net/netdevsim/tls.sh    | 334 ++++++++++++
 .../drivers/net/netdevsim/tls_offload.c       | 505 ++++++++++++++++++
 10 files changed, 1377 insertions(+)
 create mode 100644 drivers/net/netdevsim/tls.c
 create mode 100644 tools/testing/selftests/drivers/net/netdevsim/.gitignore
 create mode 100755 tools/testing/selftests/drivers/net/netdevsim/tls.sh
 create mode 100644 tools/testing/selftests/drivers/net/netdevsim/tls_offload.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 netdevsim: add TLS device offload emulation

Implement tlsdev_ops and advertise NETIF_F_HW_TLS_TX/RX, so kTLS sockets routed over netdevsim take the device path in net/tls/tls_device.c instead of the software one.

The point of this is code coverage and control path testing. Nothing is actually encrypted or decrypted, and no guarantees are made for the corner cases where the crypto would have had to be right.

TX puts the plaintext record on the wire and RX just sets skb->decrypted, so the only supported setup is two netdevsim ports that both have the offload installed before any data flows. Anything else runs the software AEAD over plaintext and fails the connection with -EBADMSG. The one thing a device would still do is write the authentication tag, and the placeholder the stack leaves for it comes from a page frag that is never zeroed, so clear it instead of putting stale page contents on the wire. Records are located with tls_get_record(), the way the real drivers do it; on RX, where there is no socket yet, connections are matched on the tuple from ->tls_dev_add() the way the hardware does.

NETIF_F_RXCSUM comes along because netdev_fix_features() drops NETIF_F_HW_TLS_RX without it, and NETDEVSIM now depends on TLS since it calls tls_get_record().

---
 drivers/net/Kconfig               |   1 +
 drivers/net/netdevsim/Makefile    |   4 +
 drivers/net/netdevsim/netdev.c    |   5 +
 drivers/net/netdevsim/netdevsim.h |  35 +++
 drivers/net/netdevsim/tls.c       | 482 ++++++++++++++++++++++++++++++
 5 files changed, 527 insertions(+)
 create mode 100644 drivers/net/netdevsim/tls.c
drivers/net/Kconfig +1 -0
index ff79c466712d..cb4d4bb87afc 100644
@@ -607,6 +607,7 @@ config NETDEVSIM
 	depends on INET
 	depends on PSAMPLE || PSAMPLE=n
 	depends on PTP_1588_CLOCK_MOCK || PTP_1588_CLOCK_MOCK=n
+	depends on TLS || TLS=n
 	select NET_DEVLINK
 	select PAGE_POOL
 	select NET_SHAPER
drivers/net/netdevsim/Makefile +4 -0
index 87718204fb4d..c7f7621323b2 100644
@@ -25,3 +25,7 @@ endif
 ifneq ($(CONFIG_MACSEC),)
 netdevsim-objs += macsec.o
 endif
+
+ifneq ($(CONFIG_TLS_DEVICE),)
+netdevsim-objs += tls.o
+endif
drivers/net/netdevsim/netdev.c +5 -0
index b4a99f3ceac6..b9e0a2f5b18f 100644
@@ -151,6 +151,8 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
 	if (dr)
 		goto out_drop_free;
 
+	nsim_do_tls(skb, ns, peer_ns);
+
 	rxq = skb_get_queue_mapping(skb);
 	if (rxq >= peer_dev->num_rx_queues)
 		rxq = rxq % peer_dev->num_rx_queues;
@@ -1068,6 +1070,7 @@ static int nsim_init_netdevsim(struct netdevsim *ns)
 
 	nsim_macsec_init(ns);
 	nsim_ipsec_init(ns);
+	nsim_tls_init(ns);
 
 	err = register_netdevice(ns->netdev);
 	if (err)
@@ -1095,6 +1098,7 @@ static int nsim_init_netdevsim(struct netdevsim *ns)
 	RCU_INIT_POINTER(ns->peer, NULL);
 	unregister_netdevice(ns->netdev);
 err_ipsec_teardown:
+	nsim_tls_teardown(ns);
 	nsim_ipsec_teardown(ns);
 	nsim_macsec_teardown(ns);
 	nsim_bpf_uninit(ns);
@@ -1197,6 +1201,7 @@ void nsim_destroy(struct netdevsim *ns)
 	RCU_INIT_POINTER(ns->peer, NULL);
 	unregister_netdevice(dev);
 	if (nsim_dev_port_is_pf(ns->nsim_dev_port)) {
+		nsim_tls_teardown(ns);
 		nsim_macsec_teardown(ns);
 		nsim_ipsec_teardown(ns);
 		nsim_bpf_uninit(ns);
drivers/net/netdevsim/netdevsim.h +35 -0
index 55aec41237b9..b4646c7061bd 100644
@@ -56,6 +56,20 @@ struct nsim_ipsec {
 	u32 tx;
 };
 
+struct nsim_tls {
+	struct list_head conns;
+	spinlock_t lock;	/* protects conns and the counters below */
+	struct dentry *dfile;
+	u32 count;
+	u32 tx_conn;
+	u32 rx_conn;
+	atomic64_t tx_packets;
+	atomic64_t tx_bytes;
+	atomic64_t rx_packets;
+	atomic64_t rx_bytes;
+	atomic64_t resyncs;
+};
+
 #define NSIM_MACSEC_MAX_SECY_COUNT 3
 #define NSIM_MACSEC_MAX_RXSC_COUNT 1
 struct nsim_rxsc {
@@ -141,6 +155,7 @@ struct netdevsim {
 	bool bpf_map_accept;
 	struct nsim_ipsec ipsec;
 	struct nsim_macsec macsec;
+	struct nsim_tls tls;
 	struct nsim_vlan vlan;
 	struct {
 		u32 inject_error;
@@ -434,6 +449,26 @@ static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)
 }
 #endif
 
+#if IS_ENABLED(CONFIG_TLS_DEVICE)
+void nsim_tls_init(struct netdevsim *ns);
+void nsim_tls_teardown(struct netdevsim *ns);
+void nsim_do_tls(struct sk_buff *skb, struct netdevsim *ns,
+		 struct netdevsim *peer_ns);
+#else
+static inline void nsim_tls_init(struct netdevsim *ns)
+{
+}
+
+static inline void nsim_tls_teardown(struct netdevsim *ns)
+{
+}
+
+static inline void nsim_do_tls(struct sk_buff *skb, struct netdevsim *ns,
+			       struct netdevsim *peer_ns)
+{
+}
+#endif
+
 #if IS_ENABLED(CONFIG_MACSEC)
 void nsim_macsec_init(struct netdevsim *ns);
 void nsim_macsec_teardown(struct netdevsim *ns);
drivers/net/netdevsim/tls.c new file +482 -0

482 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

PATCH 2/2 selftests: netdevsim: add a kTLS device offload test

In reply to Jiayuan Chen

Run kTLS over a linked netdevsim pair, one port per netns. The test reads /proc/net/tls_stat around the setsockopt() calls and fails unless both directions landed on the device path, so it cannot quietly pass on the software one. It then does a bulk transfer both ways, small MSG_MORE writes, splice() with TLS_TX_ZEROCOPY_RO, and a run with the record limit at its minimum so that whole records pack several to a segment, checking the payload each time, plus the ethtool off/on path.

  # ./tls.sh
  PASS: tls-hw-tx-offload advertised and on by default
  PASS: tls-hw-rx-offload advertised and on by default
  PASS: per-port debugfs tls file exists
  PASS: offloaded TLS data transfer
  PASS: tx and rx contexts installed on both ports
  PASS: both ends used the device path
  PASS: no silent fallback to the software path
  PASS: no decrypt errors
  PASS: driver counted offloaded packets both ways
  PASS: all offload contexts released on close
  PASS: no device contexts left behind
  PASS: both offload features turned off on both ports
  PASS: offload declined once the feature is off
  PASS: software path used when offload is off
  PASS: both offload features turned back on
  PASS: offload works again after re-enabling

passed: 16 failed: 0

---
 .../drivers/net/netdevsim/.gitignore          |   2 +
 .../selftests/drivers/net/netdevsim/Makefile  |   7 +
 .../selftests/drivers/net/netdevsim/config    |   2 +
 .../selftests/drivers/net/netdevsim/tls.sh    | 334 ++++++++++++
 .../drivers/net/netdevsim/tls_offload.c       | 505 ++++++++++++++++++
 5 files changed, 850 insertions(+)
 create mode 100644 tools/testing/selftests/drivers/net/netdevsim/.gitignore
 create mode 100755 tools/testing/selftests/drivers/net/netdevsim/tls.sh
 create mode 100644 tools/testing/selftests/drivers/net/netdevsim/tls_offload.c
tools/testing/selftests/drivers/net/netdevsim/.gitignore new file +2 -0
new file mode 100644
index 000000000000..9d4f57cb2baa
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+tls_offload
tools/testing/selftests/drivers/net/netdevsim/Makefile +7 -0
index 9808c2fbae9e..5beb4ed64eb5 100644
@@ -1,5 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0+ OR MIT
 
+CFLAGS += $(KHDR_INCLUDES)
+
 TEST_PROGS := \
 	devlink.sh \
 	devlink_in_netns.sh \
@@ -15,9 +17,14 @@ TEST_PROGS := \
 	peer.sh \
 	psample.sh \
 	tc-mq-visibility.sh \
+	tls.sh \
 	udp_tunnel_nic.sh \
 # end of TEST_PROGS
 
+TEST_GEN_FILES := \
+	tls_offload
+# end of TEST_GEN_FILES
+
 TEST_FILES := \
 	ethtool-common.sh
 # end of TEST_FILES
tools/testing/selftests/drivers/net/netdevsim/config +2 -0
index 5117c78ddf0a..5893111ba136 100644
@@ -8,4 +8,6 @@ CONFIG_NET_SCH_MULTIQ=y
 CONFIG_NET_SCH_PRIO=y
 CONFIG_PSAMPLE=y
 CONFIG_PTP_1588_CLOCK_MOCK=y
+CONFIG_TLS=m
+CONFIG_TLS_DEVICE=y
 CONFIG_VXLAN=m
tools/testing/selftests/drivers/net/netdevsim/tls.sh new file +334 -0

334 lines not on this page · raw message

tools/testing/selftests/drivers/net/netdevsim/tls_offload.c new file +505 -0

505 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH net-next v1 1/2] netdevsim: add TLS device offload emulation

In reply to Jiayuan Chen

drivers/net/netdevsim/netdev.c static int nsim_init_netdevsim(struct netdevsim *ns) · line 1101

Quoted 13 lines · Jiayuan Chen
drivers/net/netdevsim/netdev.c +2 -0
@@ -1068,6 +1070,7 @@ static int nsim_init_netdevsim(struct netdevsim *ns)
 
 	nsim_macsec_init(ns);
 	nsim_ipsec_init(ns);
+	nsim_tls_init(ns);
 
 	err = register_netdevice(ns->netdev);
 	if (err)
@@ -1095,6 +1098,7 @@ static int nsim_init_netdevsim(struct netdevsim *ns)
 	RCU_INIT_POINTER(ns->peer, NULL);
 	unregister_netdevice(ns->netdev);
 err_ipsec_teardown:
+	nsim_tls_teardown(ns);

nit: label name drifts a bit with this inserted here.

Quoted 3 lines
drivers/net/netdevsim/netdev.c +0 -0
 	nsim_ipsec_teardown(ns);
 	nsim_macsec_teardown(ns);
 	nsim_bpf_uninit(ns);
maintainer
#6 lore raw
In reply to Jiayuan Chen
Quoted 9 lines · On Tue, 1 Sep 2026 23:48:55 +0800 Jiayuan Chen wrote:

It helped me find
commit b17cf742eaad ("tls: device: fix out-of-bounds write in tls_append_frag()").

So I think it is worth sending a formal patchset.

The kTLS device offload path in net/tls/tls_device.c has no test coverage upstream -- selftests/net/tls.c is software only, and the offload code needs a driver that implements it.

Quoted 7 lines

So teach netdevsim to fake it. Patch 1 implements tlsdev_ops and advertises the TLS features, patch 2 adds a selftest.

No crypto is done: TX puts the plaintext record on the wire, RX just sets skb->decrypted. That only works netdevsim to netdevsim, but it covers record assembly and the offload lifecycle. Real ciphertext, and more on top of it, can come later.

Please don't post selftests to net-next which depend on a fix going to another tree. If we merge this now we will have a failing test until the fix also lands.

If we add TLS support to netdevsim it must be paired with a test which can also run against real HW. There was a selftest posted recently by the person working on TLS 1.3 offload, we can use that as the target (feel free to add that to your series, just keep the original author).

We can add special netdevsim-only cases but the main focus should be writing tests that can be executed against real devices.

Signature · 1 line
pw-bot: cr
#7 lore raw
Quoted 21 lines · Jakub Kicinski
Quoted 9 lines · On Tue, 1 Sep 2026 23:48:55 +0800 Jiayuan Chen wrote:

It helped me find
commit b17cf742eaad ("tls: device: fix out-of-bounds write in tls_append_frag()").

So I think it is worth sending a formal patchset.

The kTLS device offload path in net/tls/tls_device.c has no test coverage upstream -- selftests/net/tls.c is software only, and the offload code needs a driver that implements it.

Quoted 7 lines

So teach netdevsim to fake it. Patch 1 implements tlsdev_ops and advertises the TLS features, patch 2 adds a selftest.

No crypto is done: TX puts the plaintext record on the wire, RX just sets skb->decrypted. That only works netdevsim to netdevsim, but it covers record assembly and the offload lifecycle. Real ciphertext, and more on top of it, can come later.

Please don't post selftests to net-next which depend on a fix going to another tree. If we merge this now we will have a failing test until the fix also lands.

Quoted 8 lines

If we add TLS support to netdevsim it must be paired with a test which can also run against real HW. There was a selftest posted recently by the person working on TLS 1.3 offload, we can use that as the target (feel free to add that to your series, just keep the original author).

We can add special netdevsim-only cases but the main focus should be writing tests that can be executed against real devices.

I think I just wait until that series is merged and then take a next setp.

#3 lore raw

Re: [PATCH net-next v1 1/2] netdevsim: add TLS device offload emulation

In reply to Jiayuan Chen
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Critical: 0 · High: 0 · Medium: 2 · Low: 3

- [Medium] The whole RX half of the emulation is a dead decision: every frame nsim_tls_rx() inspects already has skb->decrypted == 1 set by…
- [Medium] nsim_tls_tx_zero_tags() aborts the whole record walk with 'break' as soon as tls_get_record() returns NULL or a start-marker…
- [Low] Claim that the new TLS emulation has no in-tree consumer / no selftest, so the commit message's stated purpose (code coverage and…
- [Low] The lock-scope annotation on the new struct nsim_tls in drivers/net/netdevsim/netdevsim.h ('spinlock_t lock; /* protects conns…
- [Low] nsim_tls_tx_zero_tags() rewrites tag_size payload bytes (the auth-tag placeholder, memset at drivers/net/netdevsim/tls.c:188) from…

--

netdevsim: add TLS device offload emulation

This adds an emulated kTLS device offload to netdevsim: tlsdev_ops plus NETIF_F_HW_TLS_TX/RX so kTLS sockets over netdevsim take the device path, with TX zeroing the auth tag placeholder and RX marking skbs decrypted. A per-port debugfs "tls" file exposes the connection list and counters.

A few questions below.

Quoted 13 lines
drivers/net/netdevsim/netdev.c +2 -0
index b4a99f3ceac60..b9e0a2f5b18fc 100644
@@ -151,6 +151,8 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
 	if (dr)
 		goto out_drop_free;
 
+	nsim_do_tls(skb, ns, peer_ns);
+
 	rxq = skb_get_queue_mapping(skb);
 	if (rxq >= peer_dev->num_rx_queues)
 		rxq = rxq % peer_dev->num_rx_queues;

[ ... ]

Quoted 11 lines
drivers/net/netdevsim/netdevsim.h +3 -0
index 55aec41237b9b..b4646c7061bd0 100644
@@ -56,6 +56,20 @@ struct nsim_ipsec {
 	u32 tx;
 };
 
+struct nsim_tls {
+	struct list_head conns;
+	spinlock_t lock;	/* protects conns and the counters below */

drivers/net/netdevsim/netdevsim.h struct nsim_ipsec { · line 71

Quoted 10 lines
drivers/net/netdevsim/netdevsim.h +10 -0
+	struct dentry *dfile;
+	u32 count;
+	u32 tx_conn;
+	u32 rx_conn;
+	atomic64_t tx_packets;
+	atomic64_t tx_bytes;
+	atomic64_t rx_packets;
+	atomic64_t rx_bytes;
+	atomic64_t resyncs;
+};

[Severity: Low]
This isn't a bug, but does the annotation match the code? Of the fields below the lock, only count, tx_conn and rx_conn are updated under ns->tls.lock (nsim_tls_dev_add(), nsim_tls_dev_del(), nsim_tls_teardown()).

The five atomic64_t counters are only ever touched from the lockless xmit path, in nsim_tls_tx(), nsim_tls_rx() and nsim_tls_dev_resync():

	atomic64_inc(&ns->tls.tx_packets);
	atomic64_add(skb->len, &ns->tls.tx_bytes);

and count is additionally read with no lock held in nsim_tls_rx():

	if (!READ_ONCE(ns->tls.count))
		return;

Would "protects conns, count, tx_conn and rx_conn" describe the actual contract better, so nobody later adds a plain counter there and updates it from the xmit path?

[ ... ]

Quoted 6 lines
drivers/net/netdevsim/tls.c new file +0 -0
new file mode 100644
index 0000000000000..079d30a86b5d6
@@ -0,0 +1,482 @@

[ ... ]

Quoted 29 lines
drivers/net/netdevsim/tls.c new file +29 -0
+static bool nsim_tls_rx_offloaded(struct netdevsim *ns,
+				  const struct nsim_tls_tuple *t)
+{
+	struct nsim_tls_conn *conn;
+
+	list_for_each_entry_rcu(conn, &ns->tls.conns, list) {
+		if (conn->dir != TLS_OFFLOAD_CTX_DIR_RX ||
+		    conn->lport != t->dport || conn->rport != t->sport ||
+		    !ipv6_addr_equal(&conn->laddr, &t->daddr) ||
+		    !ipv6_addr_equal(&conn->raddr, &t->saddr))
+			continue;
+
+		/* Anything before start_sn predates the offload and has to
+		 * stay encrypted.  Only worth asking once: TCP sequence
+		 * numbers wrap, and a stream that has moved 2G past a
+		 * sequence it is still compared against looks like it went
+		 * backwards.
+		 */
+		if (!READ_ONCE(conn->started)) {
+			if (before(t->seq, conn->start_sn))
+				return false;
+			WRITE_ONCE(conn->started, true);
+		}
+
+		return true;
+	}
+
+	return false;
+}

[ ... ]

Quoted 2 lines
drivers/net/netdevsim/tls.c new file +2 -0
+static void nsim_tls_tx_zero_tags(struct sk_buff *skb)
+{

[ ... ]

drivers/net/netdevsim/tls.c line 185

Quoted 9 lines
drivers/net/netdevsim/tls.c new file +9 -0
+	spin_lock_irqsave(&tx_ctx->lock, flags);
+	while (before(seq, end)) {
+		struct tls_record_info *record;
+		skb_frag_t *frag;
+		u64 rcd_sn;
+
+		record = tls_get_record(tx_ctx, seq, &rcd_sn);
+		if (!record || tls_record_is_start_marker(record))
+			break;

[Severity: Medium]
Can this break put un-zeroed tag placeholders on the wire?

tls_get_record() returns the start marker record for any sequence before start_offload_tcp_sn:

net/tls/tls_device.c:tls_get_record() {
	...
	if (likely(!tls_record_is_start_marker(info))) {
	...
}

include/net/tls.h:
static inline bool tls_record_is_start_marker(struct tls_record_info *rec)

{
	return rec->len == 0;
}

So for a segment that starts with pre-offload bytes and continues into the first offloaded record, the walk breaks on the very first iteration and no record in that skb gets its tag placeholder cleared, even though the start marker's end_seq gives the boundary to continue from.

The helper returns void, nsim_tls_tx() still returns true and nsim_start_xmit() forwards the frame, so the uninitialized page frag bytes reach the peer's receive path where AF_PACKET, tc/BPF or XDP in that netns can read them. That is the leak the commit message says it closes:

"the placeholder the stack leaves for it comes from a page frag that is never zeroed, so clear it instead of putting stale page contents on the wire"

Would it be better for this helper to report failure and have the skb dropped, or to skip past the start marker via record->end_seq and keep walking?

drivers/net/netdevsim/tls.c line 189

Quoted 4 lines
drivers/net/netdevsim/tls.c new file +4 -0
+
+		frag = &record->frags[record->num_frags - 1];
+		memset(skb_frag_address(frag) + skb_frag_size(frag) - tag_size,
+		       0, tag_size);

[Severity: Low]
This isn't a bug in the supported setup, but the memset rewrites tag_size payload bytes from inside ndo_start_xmit without touching the L4 checksum.

netdevsim has NETIF_F_HW_CSUM in hw_features (nsim_setup()), so "ethtool -K nsimX tx off" is accepted. netdev_fix_features() then also clears NETIF_F_HW_TLS_TX, but tls_dev_event() only re-checks that tlsdev_ops exist, so a live socket keeps ctx->netdev pointing at this device and keeps taking the device TX path. With csum offload gone, skb_checksum_help() runs before ndo_start_xmit and computes over the un-zeroed placeholder; nsim_tls_tx_zero_tags() then changes those bytes, and since netdevsim never sets CHECKSUM_UNNECESSARY on receive the peer's TCP drops every segment and the connection stalls.

The sibling emulation in the same driver repairs this, nsim_do_psp():

		uh->check = udp_v4_check(udplen, iph->saddr, iph->daddr, csum);
		...
		skb->ip_summed	= CHECKSUM_NONE;

Would skipping (or compensating for) the memset when skb->ip_summed != CHECKSUM_PARTIAL be worth doing here?

Quoted 5 lines
drivers/net/netdevsim/tls.c new file +5 -0
+
+		seq = record->end_seq;
+	}
+	spin_unlock_irqrestore(&tx_ctx->lock, flags);
+}

[ ... ]

Quoted 12 lines
drivers/net/netdevsim/tls.c new file +12 -0
+static bool nsim_tls_tx(struct netdevsim *ns, struct sk_buff *skb)
+{
+	if (!tls_is_skb_tx_device_offloaded(skb))
+		return false;
+
+	/* The question tls_validate_xmit_skb() asks: on anything other than
+	 * the device the context is attached to, the core has already
+	 * encrypted the record in software, so it is real ciphertext and none
+	 * of our business.
+	 */
+	if (rcu_dereference_bh(tls_get_ctx(skb->sk)->netdev) != ns->netdev)
+		return false;

[ ... ]

Quoted 8 lines
drivers/net/netdevsim/tls.c new file +8 -0
+static void nsim_tls_rx(struct netdevsim *ns, struct sk_buff *skb)
+{
+	struct nsim_tls_tuple t;
+
+	/* Not every port registers the offload, and one that does not never
+	 * initializes the list head, so gate on the count.  It is the
+	 * cheaper test anyway.
+	 */

drivers/net/netdevsim/tls.c line 243

Quoted 10 lines
drivers/net/netdevsim/tls.c new file +10 -0
+	if (!READ_ONCE(ns->tls.count))
+		return;
+
+	if (!nsim_tls_parse(skb, &t))
+		return;
+
+	if (!nsim_tls_rx_offloaded(ns, &t))
+		return;
+
+	skb->decrypted = 1;

[Severity: Medium]
Is skb->decrypted already 1 by the time this runs, making the whole RX decision unobservable?

For a device-offloaded kTLS socket the TX side sets it:

net/tls/tls_device.c:tls_push_data() {
	...
	flags |= MSG_SENDPAGE_DECRYPTED;
	...
}
net/ipv4/tcp.c:tcp_sendmsg_locked() {
	...
	skb->decrypted = !!(flags & MSG_SENDPAGE_DECRYPTED);
	...
}

netdevsim hands that same skb to the peer via nsim_forward_skb() -> __dev_forward_skb() -> skb_scrub_packet(), which clears pkt_type, iif, dst, extensions, ct, mark and tstamp but not decrypted. On the peer, tls_device_decrypted() reads skb->decrypted whenever rx_conf == TLS_HW.

If so, the tuple match and the "predates the offload" rejection in nsim_tls_rx_offloaded():

		if (!READ_ONCE(conn->started)) {
			if (before(t->seq, conn->start_sn))
				return false;

cannot have any effect, and the mid-stream behaviour the commit message describes does not happen:

"Anything else - a peer with no matching context, a context installed mid stream - has the core run the software AEAD over plaintext, which fails the connection with -EBADMSG."

The same inheritance also seems to defeat the ctx->netdev != ns->netdev bail-out in nsim_tls_tx(). If the sending port goes down, tls_device_down() clears ctx->netdev while tx_conf stays TLS_HW, so subsequent sends go through tls_validate_xmit_skb() and the software fallback. decrypted lives inside struct_group(headers, ...) in struct sk_buff, so the fallback skb built by complete_skb()/skb_copy_header() inherits the bit. Would that deliver real ciphertext to a peer that still has a matching RX connection, with tls_device_decrypted() returning 1, the software AEAD skipped and userspace handed ciphertext rather than plaintext or -EBADMSG?

Should the emulation clear skb->decrypted for frames it does not claim, the way a real wire drops an in-kernel-only bit?

Quoted 3 lines
drivers/net/netdevsim/tls.c new file +3 -0
+	atomic64_inc(&ns->tls.rx_packets);
+	atomic64_add(skb->len, &ns->tls.rx_bytes);
+}

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260901154914.11637-1-jiayuan.chen%40linux.dev
#5 lore raw

Re: [PATCH net-next v1 2/2] selftests: netdevsim: add a kTLS device offload test

In reply to Jiayuan Chen
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Critical: 0 · High: 0 (1) · Medium: 2 · Low: 3

- [Medium] The run_pair nosample run (offload features turned off) has no rendezvous between the two helpers: both are expected to abort…
- [Medium] The payload verification for splice fragments generates identical data for all fragments, which silently defeats the test's…
- [Low] tls.sh reuses the global, fixed network-namespace names nssv/nscl and exactly the same random netdevsim device-ID ranges…
- [Low] Two assertions inspect only one of the two ports/namespaces even though their messages imply both: (a) the tls-hw-tx-offload/`tls-hw-…
- [Low] The "per-port debugfs tls file exists" assertion is dead code: a few lines earlier the script already does `if [ !

Pre-existing issues:
- [High] The new test is the first in-tree user of setsockopt(SOL_TLS, TLS_TX_MAX_PAYLOAD_LEN) on a device-offloaded (TLS_HW) socket, and it…

--

selftests: netdevsim: add a kTLS device offload test

Adds tls.sh plus the tls_offload helper under tools/testing/selftests/drivers/net/netdevsim/, running kTLS over a linked netdevsim pair with one port per netns. The helper asserts via /proc/net/tls_stat that both directions landed on the device path, then exercises bulk transfers, MSG_MORE writes, splice() with TLS_TX_ZEROCOPY_RO and a minimal record limit, plus the ethtool off/on path.

A few questions about the new test below.

Quoted 24 lines
tools/testing/selftests/drivers/net/netdevsim/tls.sh new file +18 -0
new file mode 100755
index 0000000000000..59b7d1c0e5148
@@ -0,0 +1,334 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Exercise netdevsim's emulated kTLS device offload over a linked
+# netdevsim pair, one port per network namespace.
+#
+# shellcheck disable=SC2154 # ksft_skip comes from lib.sh
+
+lib_dir=$(dirname "$0")
+# shellcheck source=./../../../net/lib.sh
+# shellcheck disable=SC1091
+source "$lib_dir"/../../../net/lib.sh
+
+# Device IDs and the nssv/nscl namespaces are picked the same way as peer.sh.
+NSIM_DEV_1_ID=$((256 + RANDOM % 256))
+NSIM_DEV_1_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_1_ID
+NSIM_DEV_2_ID=$((512 + RANDOM % 256))
+NSIM_DEV_2_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_2_ID

[ ... ]

Quoted 10 lines
tools/testing/selftests/drivers/net/netdevsim/tls.sh new file +10 -0
+# Each resource is handed to defer only once it exists, so a run that loses a
+# race for one of the global names does not tear down the winner's.
+setup()
+{
+	set -e
+
+	echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_NEW"
+	defer nsim_dev_del "$NSIM_DEV_1_ID"
+	echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_NEW"
+	defer nsim_dev_del "$NSIM_DEV_2_ID"

[ ... ]

tools/testing/selftests/drivers/net/netdevsim/tls.sh line 91

Quoted 4 lines
tools/testing/selftests/drivers/net/netdevsim/tls.sh new file +4 -0
+	ip netns add nssv
+	defer ip netns del nssv
+	ip netns add nscl
+	defer ip netns del nscl

[Severity: Low]
The fixed names nssv and nscl, and the device ID ranges $((256 + RANDOM % 256)) / $((512 + RANDOM % 256)), are exactly the ones peer.sh in the same directory already uses.

What happens when a stale nssv or nscl namespace is left behind by an aborted peer.sh or tls.sh run, or when peer.sh runs concurrently? setup() runs under set -e, so ip netns add nssv (or the new_device write) failing aborts the script mid-setup with a raw shell error and no PASS, FAIL or SKIP line at all.

Would setup_ns() from the already sourced tools/testing/selftests/net/lib.sh be preferable here, since it generates per-run unique namespace names?

[ ... ]

Quoted 4 lines
tools/testing/selftests/drivers/net/netdevsim/tls.sh new file +4 -0
+run_pair()
+{
+	local sample="${1:-sample}"
+	local srv_rc cli_rc ready=1

[ ... ]

Quoted 13 lines
tools/testing/selftests/drivers/net/netdevsim/tls.sh new file +13 -0
+	if [ "$sample" = "sample" ]; then
+		# Comfortably longer than the helpers take to get to the
+		# barrier.  Timing out here means a sample would be stale, so
+		# fail the run rather than assert on it.
+		slowwait "$READY_TIMEOUT_SEC" both_ready >/dev/null || ready=0
+		if [ "$ready" -eq 1 ]; then
+			CONNS_1=$(dbg_field "$NSIM_DEV_1_TLS" count)
+			CONNS_2=$(dbg_field "$NSIM_DEV_2_TLS" count)
+			: "${CONNS_1:=0}"
+			: "${CONNS_2:=0}"
+		fi
+	fi
+	touch "$SYNCDIR/go"

[ ... ]

tools/testing/selftests/drivers/net/netdevsim/tls.sh line 239

Quoted 6 lines
tools/testing/selftests/drivers/net/netdevsim/tls.sh new file +6 -0
+# The offload has to be advertised, and on by default like the other
+# netdevsim crypto offloads.
+for f in tls-hw-tx-offload tls-hw-rx-offload; do
+	[ "$(feature nssv "$NSIM_DEV_1_NAME" "$f")" = "on" ]
+	check "$f advertised and on by default" $?
+done

[Severity: Low]
This loop only queries feature nssv "$NSIM_DEV_1_NAME", but the message says the feature is advertised and on by default without qualifying which port. Should the client port in nscl be checked too, the way set_features() does?

The same one-sided pattern shows up later for the current-context counters:

	[ "$(tls_stat nssv TlsCurrTxDevice)" -eq 0 ] && \
		[ "$(tls_stat nssv TlsCurrRxDevice)" -eq 0 ]
	check "no device contexts left behind" $?

/proc/net/tls_stat is per netns, and enable_ktls() installs TX and RX contexts on both ends, so would a context left behind on the client port in nscl pass unnoticed here?

tools/testing/selftests/drivers/net/netdevsim/tls.sh line 242

Quoted 3 lines
tools/testing/selftests/drivers/net/netdevsim/tls.sh new file +3 -0
+
+[ -e "$NSIM_DEV_1_TLS" ]
+check "per-port debugfs tls file exists" $?

[Severity: Low]
Is this assertion reachable in the failing state? A few lines earlier the script already does:

	if [ ! -e "$NSIM_DEV_1_TLS" ]; then
		echo "SKIP: netdevsim built without TLS device offload (CONFIG_TLS_DEVICE=n)"
		exit "$ksft_skip"
	fi

so by the time [ -e "$NSIM_DEV_1_TLS" ] runs it is always true, and the check only adds to num_pass.

Meanwhile $NSIM_DEV_2_TLS is never checked for existence, although all_ctx_released() and the tx_packets/rx_packets assertions parse it. If that file is missing or a field name changes, dbg_field() returns an empty string and [ "" -eq 0 ] / [ "" -ge 1 ] emit "integer expression expected" rather than a clean FAIL. Should the existence check cover port 2 instead?

[ ... ]

Quoted 3 lines

+[ "$(tls_stat nssv TlsCurrTxDevice)" -eq 0 ] && \

tools/testing/selftests/drivers/net/netdevsim/tls.sh new file +2 -0
+	[ "$(tls_stat nssv TlsCurrRxDevice)" -eq 0 ]
+check "no device contexts left behind" $?

[ ... ]

Quoted 5 lines
tools/testing/selftests/drivers/net/netdevsim/tls.sh new file +5 -0
+# Turning the features off has to make the offload refuse the connection;
+# the test binary insists on the device path, so it must now fail.
+set_features off
+check "both offload features turned off on both ports" $?
+

tools/testing/selftests/drivers/net/netdevsim/tls.sh line 323

Quoted 10 lines
tools/testing/selftests/drivers/net/netdevsim/tls.sh new file +10 -0
+run_pair nosample
+rc=$?
+[ "$rc" -ne 0 ]
+check "offload declined once the feature is off" $?
+
+# Both directions on both ends have to have landed on the software path.
+[ "$(tls_stat nssv TlsTxSw)" -ge 1 ] && [ "$(tls_stat nssv TlsRxSw)" -ge 1 ] && \
+	[ "$(tls_stat nscl TlsTxSw)" -ge 1 ] && \
+	[ "$(tls_stat nscl TlsRxSw)" -ge 1 ]
+check "software path used when offload is off" $?

[Severity: Medium]
Can this assertion fail intermittently? In nosample mode run_pair() skips the slowwait/both_ready barrier entirely, and neither helper reaches rendezvous() because enable_ktls() exits first:

tools/testing/selftests/drivers/net/netdevsim/tls_offload.c:enable_ktls() {
	if (read_tls_stat("TlsTxDevice") != tx_before + 1)
		fail("TX did not land on the device path (TlsTxDevice %lu -> %lu)",
	...
}

So whichever end aborts first closes its socket and sends a FIN. If the peer processes that FIN before it gets to setsockopt(TCP_ULP, "tls"):

net/tls/tls_main.c:tls_init() {
	...
	if (sk->sk_state != TCP_ESTABLISHED)
		return -ENOTCONN;
	...
}

the peer dies in die("setsockopt(TCP_ULP, tls)") and never installs software kTLS, so TlsTxSw/TlsRxSw in that namespace stay at 0 and this check fails. Note the first run already asserted TlsTxSw == 0 in both namespaces, so earlier traffic cannot cover for it.

"offload declined once the feature is off" still passes in that case since it only requires a non-zero exit status, so the failure reason is hidden. Would a rendezvous (or a retry) before the helpers give up make this run deterministic?

Quoted 6 lines
tools/testing/selftests/drivers/net/netdevsim/tls_offload.c new file +0 -0
new file mode 100644
index 0000000000000..0d7c8950ca6ce
@@ -0,0 +1,505 @@

[ ... ]

tools/testing/selftests/drivers/net/netdevsim/tls_offload.c line 332

Quoted 13 lines
tools/testing/selftests/drivers/net/netdevsim/tls_offload.c new file +13 -0
+/* A record can be up to 16K, so normally one segment carries a piece of a
+ * single record.  Shrinking the limit puts a dozen or so whole records in
+ * every segment instead, which is the multi-record path through the driver.
+ */
+static void send_small_records(int fd, unsigned int seed)
+{
+	char buf[SMALL_LEN];
+	uint16_t limit;
+
+	limit = REC_LIM_MIN;
+	if (setsockopt(fd, SOL_TLS, TLS_TX_MAX_PAYLOAD_LEN, &limit,
+		       sizeof(limit)))
+		die("setsockopt(TLS_TX_MAX_PAYLOAD_LEN)");

[Severity: High]
This isn't a bug introduced by this patch, but it looks like the first in-tree caller of TLS_TX_MAX_PAYLOAD_LEN on a socket that the test has just asserted is device offloaded (tx_conf == TLS_HW), and the kernel handler looks type confused:

net/tls/tls_main.c:do_tls_setsockopt_tx_payload_len() {
	struct tls_sw_context_tx *sw_ctx = tls_sw_ctx_tx(ctx);
	...
	if (sw_ctx && sw_ctx->open_rec)
		return -EBUSY;
	...
}

include/net/tls.h:tls_sw_ctx_tx() is an unchecked cast:

	return (struct tls_sw_context_tx *)tls_ctx->priv_ctx_tx;

and for an offloaded socket net/tls/tls_device.c:tls_set_device_offload() stores a struct tls_offload_context_tx there:

	ctx->priv_ctx_tx = offload_ctx;

There is no tx_conf == TLS_SW test, so the ->open_rec read lands inside the offload context's sg_tx_data[MAX_SKB_FRAGS], which tls_push_record() fills for every offloaded record:

net/tls/tls_device.c:tls_push_record() {
	sg_unmark_end(&offload_ctx->sg_tx_data[i]);
	sg_set_page(&offload_ctx->sg_tx_data[i], skb_frag_page(frag),
		    skb_frag_size(frag), skb_frag_off(frag));
	...
}

Two things follow, and which one happens depends on the struct layout:

If the aliased bytes are non-zero (page_link/offset/length, e.g. once the offsets shift with CONFIG_DEBUG_SPINLOCK or LOCKDEP changing the scatterlist stride), setsockopt returns a spurious -EBUSY. That is fatal for this helper, since the call above goes straight to die("setsockopt(TLS_TX_MAX_PAYLOAD_LEN)") after the bulk, MSG_MORE and splice traffic has already populated sg_tx_data.

If they are zero (default x86_64 layout, where the read lands on sg_tx_data[n].dma_address, which tls_device.c never writes), the intended guard never fires, because tls_offload_context_tx->open_record is never consulted. The limit can then be shrunk while an offloaded record is open, and the next send hits:

net/tls/tls_device.c:tls_push_data() {
	...
	copy = min_t(size_t, size, max_open_record_len - record->len);
	...
}

with max_open_record_len < record->len, i.e. a u32 underflow that makes copy == size and lets the record grow past the requested limit and past the 2^14 payload ceiling.

Should the guard check ctx->tx_conf == TLS_SW (and consult tls_offload_ctx_tx(ctx)->open_record for the TLS_HW case) before the test starts relying on this option on offloaded sockets?

Quoted 3 lines
tools/testing/selftests/drivers/net/netdevsim/tls_offload.c new file +3 -0
+
+	fill_pattern(buf, sizeof(buf), seed);
+	write_all(fd, buf, sizeof(buf));

[ ... ]

Quoted 496 lines
tools/testing/selftests/drivers/net/netdevsim/tls_offload.c new file +495 -0
@@ -0,0 +1,505 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * kTLS device offload data path exercise, driven by tls.sh.
+ *
+ * One instance runs as the server and one as the client, each in its own
+ * network namespace, connected back to back by a linked netdevsim pair.
+ * Both ends enable kTLS and rely on netdevsim's emulated TLS offload, so
+ * every record travels through net/tls/tls_device.c rather than the
+ * software path.
+ *
+ * The two processes rendezvous through a shared directory so that neither
+ * side sends before the other has installed its RX offload.
+ */
+
+#define _GNU_SOURCE
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <linux/tls.h>
+
+#ifndef SOL_TLS
+#define SOL_TLS			282
+#endif
+
+#ifndef TCP_ULP
+#define TCP_ULP			31
+#endif
+
+#define BULK_LEN		(200 * 1024)
+#define MORE_FRAGS		64
+#define SPLICE_FRAG_LEN		4096
+#define SPLICE_FRAGS		8
+
+/* TLS_MIN_RECORD_SIZE_LIM and TLS_MAX_PAYLOAD_SIZE, which are not uapi. */
+#define REC_LIM_MIN		64
+#define REC_LIM_MAX		16384
+
+#define SMALL_RECS		100
+#define SMALL_LEN		(REC_LIM_MIN * SMALL_RECS)
+
+#define SYNC_TIMEOUT_MS		20000
+#define CONNECT_TIMEOUT_MS	20000
+
+/* accept() and the transfers have no timeout of their own, and tls.sh waits
+ * on both helpers.  Bound the whole run so a stuck peer cannot hang the test.
+ */
+#define RUN_TIMEOUT_SEC		120
+
+static const char *role;
+
+static void die(const char *what)
+{
+	fprintf(stderr, "%s: %s: %s\n", role, what, strerror(errno));
+	exit(1);
+}
+
+static void fail(const char *fmt, ...)
+{
+	va_list ap;
+
+	fprintf(stderr, "%s: ", role);
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+	fprintf(stderr, "\n");
+	exit(1);
+}
+
+static void msleep(unsigned int ms)
+{
+	struct timespec ts = {
+		.tv_sec = ms / 1000,
+		.tv_nsec = (ms % 1000) * 1000000L,
+	};
+
+	nanosleep(&ts, NULL);
+}
+
+/* /proc/net/tls_stat is per netns, so both ends can check that their own
+ * connection really landed on the device path.
+ */
+static unsigned long read_tls_stat(const char *name)
+{
+	char line[256];
+	unsigned long val;
+	FILE *f;
+
+	f = fopen("/proc/net/tls_stat", "r");
+	if (!f)
+		die("open /proc/net/tls_stat");
+
+	while (fgets(line, sizeof(line), f)) {
+		char key[64];
+
+		if (sscanf(line, "%63s %lu", key, &val) != 2)
+			continue;
+		if (!strcmp(key, name)) {
+			fclose(f);
+			return val;
+		}
+	}
+
+	fclose(f);
+	fail("%s not found in /proc/net/tls_stat", name);
+	return 0;
+}
+
+static void fill_pattern(char *buf, size_t len, unsigned int seed)
+{
+	size_t i;
+
+	for (i = 0; i < len; i++)
+		buf[i] = (char)(seed + i * 31 + (i >> 8) * 7);
+}
+
+static void check_pattern(const char *buf, size_t len, unsigned int seed,
+			  const char *what)
+{
+	char *want = malloc(len);
+	size_t i;
+
+	if (!want)
+		die("malloc");
+
+	fill_pattern(want, len, seed);
+	for (i = 0; i < len; i++) {
+		if (buf[i] != want[i])
+			fail("%s: payload mismatch at byte %zu: got 0x%02x want 0x%02x",
+			     what, i, (unsigned char)buf[i],
+			     (unsigned char)want[i]);
+	}
+
+	free(want);
+}
+
+static void write_all(int fd, const char *buf, size_t len)
+{
+	size_t done = 0;
+
+	while (done < len) {
+		ssize_t n = send(fd, buf + done, len - done, 0);
+
+		if (n < 0) {
+			if (errno == EINTR)
+				continue;
+			die("send");
+		}
+		done += n;
+	}
+}
+
+static void read_all(int fd, char *buf, size_t len)
+{
+	size_t done = 0;
+
+	while (done < len) {
+		ssize_t n = recv(fd, buf + done, len - done, 0);
+
+		if (n < 0) {
+			if (errno == EINTR)
+				continue;
+			die("recv");
+		}
+		if (n == 0)
+			fail("peer closed after %zu of %zu bytes", done, len);
+		done += n;
+	}
+}
+
+static void enable_ktls(int fd)
+{
+	struct tls12_crypto_info_aes_gcm_128 ci = {};
+	unsigned long tx_before, rx_before;
+
+	tx_before = read_tls_stat("TlsTxDevice");
+	rx_before = read_tls_stat("TlsRxDevice");
+
+	if (setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls")))
+		die("setsockopt(TCP_ULP, tls)");
+
+	ci.info.version = TLS_1_2_VERSION;
+	ci.info.cipher_type = TLS_CIPHER_AES_GCM_128;
+	memset(ci.iv, 'i', sizeof(ci.iv));
+	memset(ci.key, 'k', sizeof(ci.key));
+	memset(ci.salt, 's', sizeof(ci.salt));
+	memset(ci.rec_seq, 0, sizeof(ci.rec_seq));
+
+	if (setsockopt(fd, SOL_TLS, TLS_TX, &ci, sizeof(ci)))
+		die("setsockopt(TLS_TX)");
+	if (setsockopt(fd, SOL_TLS, TLS_RX, &ci, sizeof(ci)))
+		die("setsockopt(TLS_RX)");
+
+	/* The whole point of the exercise: refuse to silently fall back to
+	 * the software path, otherwise the test would pass without ever
+	 * touching tls_device.c.
+	 */
+	if (read_tls_stat("TlsTxDevice") != tx_before + 1)
+		fail("TX did not land on the device path (TlsTxDevice %lu -> %lu)",
+		     tx_before, read_tls_stat("TlsTxDevice"));
+	if (read_tls_stat("TlsRxDevice") != rx_before + 1)
+		fail("RX did not land on the device path (TlsRxDevice %lu -> %lu)",
+		     rx_before, read_tls_stat("TlsRxDevice"));
+}
+
+static void sync_path(char *out, size_t len, const char *dir, const char *who)
+{
+	if ((size_t)snprintf(out, len, "%s/%s.ready", dir, who) >= len)
+		fail("sync dir path too long");
+}
+
+static void rendezvous(const char *dir, const char *me, const char *peer)
+{
+	char mine[PATH_MAX], theirs[PATH_MAX];
+	unsigned int waited = 0;
+	int fd;
+
+	sync_path(mine, sizeof(mine), dir, me);
+	sync_path(theirs, sizeof(theirs), dir, peer);
+
+	fd = open(mine, O_CREAT | O_WRONLY, 0600);
+	if (fd < 0)
+		die("create sync file");
+	close(fd);
+
+	while (access(theirs, F_OK)) {
+		if (waited >= SYNC_TIMEOUT_MS)
+			fail("timed out waiting for %s", peer);
+		msleep(20);
+		waited += 20;
+	}
+}
+
+/* Both ends stop here with their offload installed and no data sent yet,
+ * so that the driver state can be inspected from the outside.
+ */
+static void wait_for_go(const char *dir)
+{
+	unsigned int waited = 0;
+	char go[PATH_MAX];
+
+	if ((size_t)snprintf(go, sizeof(go), "%s/go", dir) >= sizeof(go))
+		fail("sync dir path too long");
+
+	while (access(go, F_OK)) {
+		if (waited >= SYNC_TIMEOUT_MS)
+			fail("timed out waiting for go");
+		msleep(20);
+		waited += 20;
+	}
+}
+
+/* Small writes with MSG_MORE accumulate into one open record before it is
+ * pushed, which is the interesting part of tls_push_data().
+ */
+static void send_msg_more(int fd, unsigned int seed)
+{
+	char buf[MORE_FRAGS + 1];
+	int i;
+
+	fill_pattern(buf, sizeof(buf), seed);
+
+	for (i = 0; i < MORE_FRAGS; i++) {
+		if (send(fd, buf + i, 1, MSG_MORE) != 1)
+			die("send(MSG_MORE)");
+	}
+	if (send(fd, buf + MORE_FRAGS, 1, 0) != 1)
+		die("send(last)");
+}
+
+/* splice() reaches tls_push_data() with MSG_SPLICE_PAGES once
+ * TLS_TX_ZEROCOPY_RO is enabled, which is a distinct fragment path.
+ */
+static void send_splice(int fd, unsigned int seed)
+{
+	char buf[SPLICE_FRAG_LEN];
+	int val = 1;
+	int i;
+
+	if (setsockopt(fd, SOL_TLS, TLS_TX_ZEROCOPY_RO, &val, sizeof(val)))
+		die("setsockopt(TLS_TX_ZEROCOPY_RO)");
+
+	for (i = 0; i < SPLICE_FRAGS; i++) {
+		int p[2];
+
+		fill_pattern(buf, sizeof(buf), seed + i * SPLICE_FRAG_LEN);
+
+		if (pipe(p))
+			die("pipe");
+		if (write(p[1], buf, sizeof(buf)) != sizeof(buf))
+			die("write to pipe");
+		if (splice(p[0], NULL, fd, NULL, sizeof(buf),
+			   i == SPLICE_FRAGS - 1 ? 0 : SPLICE_F_MORE) !=
+		    sizeof(buf))
+			die("splice");
+		close(p[0]);
+		close(p[1]);
+	}
+
+	val = 0;
+	if (setsockopt(fd, SOL_TLS, TLS_TX_ZEROCOPY_RO, &val, sizeof(val)))
+		die("setsockopt(TLS_TX_ZEROCOPY_RO off)");
+}
+
+/* A record can be up to 16K, so normally one segment carries a piece of a
+ * single record.  Shrinking the limit puts a dozen or so whole records in
+ * every segment instead, which is the multi-record path through the driver.
+ */
+static void send_small_records(int fd, unsigned int seed)
+{
+	char buf[SMALL_LEN];
+	uint16_t limit;
+
+	limit = REC_LIM_MIN;
+	if (setsockopt(fd, SOL_TLS, TLS_TX_MAX_PAYLOAD_LEN, &limit,
+		       sizeof(limit)))
+		die("setsockopt(TLS_TX_MAX_PAYLOAD_LEN)");
+
+	fill_pattern(buf, sizeof(buf), seed);
+	write_all(fd, buf, sizeof(buf));
+
+	limit = REC_LIM_MAX;
+	if (setsockopt(fd, SOL_TLS, TLS_TX_MAX_PAYLOAD_LEN, &limit,
+		       sizeof(limit)))
+		die("setsockopt(TLS_TX_MAX_PAYLOAD_LEN restore)");
+}
+
+#define SEED_C2S_BULK	0x11
+#define SEED_S2C_BULK	0x22
+#define SEED_C2S_MORE	0x33
+#define SEED_C2S_SPLICE	0x44
+#define SEED_C2S_SMALL	0x55
+
+static void run_client(int fd)
+{
+	char *buf = malloc(BULK_LEN);
+
+	if (!buf)
+		die("malloc");
+
+	fill_pattern(buf, BULK_LEN, SEED_C2S_BULK);
+	write_all(fd, buf, BULK_LEN);
+
+	read_all(fd, buf, BULK_LEN);
+	check_pattern(buf, BULK_LEN, SEED_S2C_BULK, "server -> client bulk");
+
+	send_msg_more(fd, SEED_C2S_MORE);
+	send_splice(fd, SEED_C2S_SPLICE);
+	send_small_records(fd, SEED_C2S_SMALL);
+
+	/* Wait for the server's verdict before tearing anything down. */
+	read_all(fd, buf, 1);
+	if (buf[0] != 'k')
+		fail("server reported a failure");
+
+	free(buf);
+}
+
+static void run_server(int fd)
+{
+	size_t splice_len = (size_t)SPLICE_FRAG_LEN * SPLICE_FRAGS;
+	char *buf = malloc(BULK_LEN);
+	char more[MORE_FRAGS + 1];
+	char *sbuf;
+	char ok = 'k';
+	int i;
+
+	sbuf = malloc(splice_len);
+	if (!buf || !sbuf)
+		die("malloc");
+
+	read_all(fd, buf, BULK_LEN);
+	check_pattern(buf, BULK_LEN, SEED_C2S_BULK, "client -> server bulk");
+
+	fill_pattern(buf, BULK_LEN, SEED_S2C_BULK);
+	write_all(fd, buf, BULK_LEN);
+
+	read_all(fd, more, sizeof(more));
+	check_pattern(more, sizeof(more), SEED_C2S_MORE, "client -> server MSG_MORE");
+
+	read_all(fd, sbuf, splice_len);
+	for (i = 0; i < SPLICE_FRAGS; i++)
+		check_pattern(sbuf + (size_t)i * SPLICE_FRAG_LEN,
+			      SPLICE_FRAG_LEN, SEED_C2S_SPLICE +
+			      i * SPLICE_FRAG_LEN, "client -> server splice");
+
+	read_all(fd, buf, SMALL_LEN);
+	check_pattern(buf, SMALL_LEN, SEED_C2S_SMALL,
+		      "client -> server small records");
+
+	write_all(fd, &ok, 1);
+
+	free(sbuf);
+	free(buf);
+}
+
+static int do_server(const char *ip, int port, const char *syncdir)
+{
+	struct sockaddr_in sa = {};
+	int lfd, fd, one = 1;
+
+	lfd = socket(AF_INET, SOCK_STREAM, 0);
+	if (lfd < 0)
+		die("socket");
+	if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)))
+		die("SO_REUSEADDR");
+
+	sa.sin_family = AF_INET;
+	sa.sin_port = htons(port);
+	if (inet_pton(AF_INET, ip, &sa.sin_addr) != 1)
+		fail("bad bind address %s", ip);
+
+	if (bind(lfd, (struct sockaddr *)&sa, sizeof(sa)))
+		die("bind");
+	if (listen(lfd, 1))
+		die("listen");
+
+	fd = accept(lfd, NULL, NULL);
+	if (fd < 0)
+		die("accept");
+	close(lfd);
+
+	enable_ktls(fd);
+	rendezvous(syncdir, "server", "client");
+	wait_for_go(syncdir);
+
+	run_server(fd);
+
+	close(fd);
+	return 0;
+}
+
+static int do_client(const char *ip, int port, const char *syncdir)
+{
+	struct sockaddr_in sa = {};
+	unsigned int waited = 0;
+	int fd;
+
+	sa.sin_family = AF_INET;
+	sa.sin_port = htons(port);
+	if (inet_pton(AF_INET, ip, &sa.sin_addr) != 1)
+		fail("bad server address %s", ip);
+
+	for (;;) {
+		fd = socket(AF_INET, SOCK_STREAM, 0);
+		if (fd < 0)
+			die("socket");
+		if (!connect(fd, (struct sockaddr *)&sa, sizeof(sa)))
+			break;
+		close(fd);
+		if (waited >= CONNECT_TIMEOUT_MS)
+			die("connect");
+		msleep(20);
+		waited += 20;
+	}
+
+	enable_ktls(fd);
+	rendezvous(syncdir, "client", "server");
+	wait_for_go(syncdir);
+
+	run_client(fd);
+
+	close(fd);
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	int port;
+
+	if (argc != 5) {
+		fprintf(stderr,
+			"usage: %s server|client <ip> <port> <syncdir>\n",
+			argv[0]);
+		return 2;
+	}
+
+	role = argv[1];
+	port = atoi(argv[3]);
+

tools/testing/selftests/drivers/net/netdevsim/tls_offload.c line 505

Quoted 10 lines
tools/testing/selftests/drivers/net/netdevsim/tls_offload.c new file +10 -0
+	alarm(RUN_TIMEOUT_SEC);
+
+	if (!strcmp(role, "server"))
+		return do_server(argv[2], port, argv[4]);
+	if (!strcmp(role, "client"))
+		return do_client(argv[2], port, argv[4]);
+
+	fprintf(stderr, "unknown role %s\n", role);
+	return 2;
+}

[Severity: Medium]
Does the per-fragment seed here actually produce distinct payloads? fill_pattern() truncates every byte to 8 bits:

	buf[i] = (char)(seed + i * 31 + (i >> 8) * 7);

and SPLICE_FRAG_LEN is 4096, a multiple of 256, so seed + i * SPLICE_FRAG_LEN only ever differs from seed in bits 8 and above. All SPLICE_FRAGS fragments therefore get byte-for-byte identical contents, and the matching loop in run_server()

check_pattern(sbuf + (size_t)i * SPLICE_FRAG_LEN,
SPLICE_FRAG_LEN, SEED_C2S_SPLICE +
i * SPLICE_FRAG_LEN, "client -> server splice");

collapses to the same expected buffer for each i.

That is not a correctness problem for the transfer itself, but it does remove most of what the splice case is supposed to prove: with every fragment identical, reordering, duplication or dropping-and-refilling a fragment inside the TLS_TX_ZEROCOPY_RO / MSG_SPLICE_PAGES path still compares equal and the test passes. Given that the whole point of this hunk is to exercise a distinct fragment path in tls_push_data(), it seems worth having the payload distinguish the fragments.

Would a seed step that survives the truncation be preferable here, e.g. seed + i rather than seed + i * SPLICE_FRAG_LEN (with the same change on the server side), or should fill_pattern() fold the seed's high bits into the byte value so that any seed spacing works? Note the other cases are unaffected, since SEED_C2S_BULK/SEED_S2C_BULK/SEED_C2S_MORE/SEED_C2S_SMALL all differ in the low 8 bits.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260901154914.11637-1-jiayuan.chen%40linux.dev