Skip to contentSkip to conversation

tls: only install push_pending_record for a TX context

PATCH Applied Rishikesh Jethwani 4 participants 5 messages Nov 2 – Nov 10

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v5 1/2 tls: only install push_pending_record for a TX context

tls_set_sw_offload() assigns ctx->push_pending_record for both directions, but the hook is a TX-side dispatcher. On a socket whose TX side is HW offloaded, tls_set_device_offload() installs tls_device_push_pending_record(); any later TLS_RX setsockopt overwrites it with tls_sw_push_pending_record(), because tls_set_device_offload_rx() itself calls tls_set_sw_offload(sk, 0, NULL) for the software half of a successful HW RX offload, and so does the fallback when the RX offload is declined (NIC with TX-only offload, tls_dev_add() failure).

From then on tls_handle_open_record() (a TLS_SET_RECORD_TYPE cmsg after a MSG_MORE send, or the close path with a pending writer) runs tls_sw_push_pending_record() against priv_ctx_tx, which is a struct tls_offload_context_tx, not a struct tls_sw_context_tx, and reads open_rec from inside the offload context. On x86_64 without lock debugging that field overlays sg_tx_data[2].dma_address, which is never set, so the push is silently skipped and the data buffered by the MSG_MORE send is later sent under the cmsg's record type, e.g. application data in a handshake or alert record. Enabling CONFIG_DEBUG_SPINLOCK, CONFIG_DEBUG_LOCK_ALLOC and CONFIG_LOCKDEP, but not CONFIG_LOCK_STAT, in the tested x86_64 configuration moves open_rec over sg_tx_data[5].page_link. Once populated, its page pointer and scatterlist flags are interpreted as a tls_rec pointer and dereferenced while reading msg_plaintext.sg.size.

Only set the hook when initialising the TX side.

Found by code review while developing TLS 1.3 HW offload rekey support.

Tested with HW TLS TX and RX offload on Mellanox ConnectX-6 Dx (Crypto Enabled): after setting TLS_RX, send() "AAAA" with MSG_MORE, then sendmsg() "BBBB" with a TLS_SET_RECORD_TYPE cmsg for a handshake record. Without this patch the peer receives a single handshake record "AAAABBBB"; with it, an application data record "AAAA" followed by a handshake record "BBBB".

Fixes e8f69799810c ("net/tls: Add generic NIC offload infrastructure")
---
 net/tls/tls_sw.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/tls/tls_sw.c +2 -1
index 771e02bff519..23eb94d9da6e 100644
@@ -2594,7 +2594,8 @@ int tls_set_sw_offload(struct sock *sk, int tx,
 		}
 	}
 
-	ctx->push_pending_record = tls_sw_push_pending_record;
+	if (tx)
+		ctx->push_pending_record = tls_sw_push_pending_record;
 
 	/* setkey is the last operation that could fail during a
 	 * rekey. if it succeeds, we can start modifying the
Signature · 1 line
2.50.1
#1 lore raw

PATCH v5 2/2 selftests: net: add TLS 1.2 hardware offload tests

Add a two-node kTLS hardware offload test. A C helper runs on both endpoints and installs fixed AES-GCM keys, so no handshake is needed. The DUT runs the TLS client and the peer echoes everything back. The test covers AES-GCM-128/256, IPv4/IPv6, and small, large and random echo payloads.

Each enabled DUT direction must use HW offload: the TlsTxDevice and TlsRxDevice MIB counters must grow and, where the driver exposes them, so must the NIC's TLS packet and context counters. The peer, and any DUT direction without HW offload enabled, may use SW kTLS. Neither side may see a TlsDecryptError.

Add handshake and alert regression cases for the push_pending_record fix. They install TLS_RX after TLS_TX, buffer "AAAA" with MSG_MORE and send "BBBB" with a TLS_SET_RECORD_TYPE cmsg. The peer must receive an application data record "AAAA" followed by a "BBBB" record of the requested type, each with MSG_EOR, and then EOF. These cases require DUT TX offload and work with the DUT's RX in either HW or SW.

Tested with HW TLS TX and RX offload on Mellanox ConnectX-6 Dx (Crypto Enabled).

---
 MAINTAINERS                                   |   1 +
 .../selftests/drivers/net/hw/.gitignore       |   1 +
 .../testing/selftests/drivers/net/hw/Makefile |   2 +
 tools/testing/selftests/drivers/net/hw/config |   2 +
 .../selftests/drivers/net/hw/tls_hw_offload.c | 908 ++++++++++++++++++
 .../drivers/net/hw/tls_hw_offload.py          | 321 +++++++
 6 files changed, 1235 insertions(+)
 create mode 100644 tools/testing/selftests/drivers/net/hw/tls_hw_offload.c
 create mode 100755 tools/testing/selftests/drivers/net/hw/tls_hw_offload.py
MAINTAINERS +1 -0
index 4f24656f7653..5fe9ba0f15f9 100644
@@ -19367,6 +19367,7 @@ F:	Documentation/networking/tls*
 F:	include/net/tls.h
 F:	include/uapi/linux/tls.h
 F:	net/tls/
+F:	tools/testing/selftests/drivers/net/hw/tls*
 F:	tools/testing/selftests/net/tls.c
 
 NETWORKING [SOCKETS]
tools/testing/selftests/drivers/net/hw/.gitignore +1 -0
index 46540468a775..911a9bfeaf41 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
 iou-zcrx
 ncdevmem
+tls_hw_offload
 toeplitz
tools/testing/selftests/drivers/net/hw/Makefile +2 -0
index 9b6b5d15f04d..429cb545a4f0 100644
@@ -47,6 +47,7 @@ TEST_PROGS = \
 	rss_drv.py \
 	rss_flow_label.py \
 	rss_input_xfrm.py \
+	tls_hw_offload.py \
 	toeplitz.py \
 	tso.py \
 	userns_devmem.py \
@@ -81,6 +82,7 @@ YNL_GEN_FILES := \
 	$(COND_GEN_FILES) \
 # end of YNL_GEN_FILES
 TEST_GEN_FILES += $(YNL_GEN_FILES)
+TEST_GEN_FILES += tls_hw_offload
 TEST_GEN_FILES += $(patsubst %.c,%.o,$(wildcard *.bpf.c))
 
 include ../../../lib.mk
tools/testing/selftests/drivers/net/hw/config +2 -0
index c6c2b64bb712..081155bf7847 100644
@@ -22,6 +22,8 @@ CONFIG_NET_IPIP=y
 CONFIG_NETKIT=y
 CONFIG_NET_SCH_INGRESS=y
 CONFIG_SYNC_FILE=y
+CONFIG_TLS=y
+CONFIG_TLS_DEVICE=y
 CONFIG_UDMABUF=y
 CONFIG_USER_NS=y
 CONFIG_VLAN_8021Q=m
tools/testing/selftests/drivers/net/hw/tls_hw_offload.c new file +908 -0

908 lines not on this page · raw message

tools/testing/selftests/drivers/net/hw/tls_hw_offload.py new file +321 -0

321 lines not on this page · raw message

Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH net v5 2/2] selftests: net: add TLS 1.2 hardware offload tests

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

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

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] On offload NICs that only support AES-GCM-128, the tls12_aes256 variant of test_tls_offload reports FAIL when it should…

--

selftests: net: add TLS 1.2 hardware offload tests

This adds a two-node kTLS hardware offload selftest. A C helper installs fixed AES-GCM keys on both ends, and the peer echoes data back to the DUT. It checks that HW offload is used in each enabled DUT direction, covers AES-GCM-128/256, IPv4/IPv6 and several payload sizes, and adds handshake and alert record-type regression cases for the push_pending_record fix.

Quoted 6 lines
tools/testing/selftests/drivers/net/hw/tls_hw_offload.py new file +0 -0
new file mode 100755
index 0000000000000..e720ce6547610
@@ -0,0 +1,321 @@

[ ... ]

Quoted 2 lines
tools/testing/selftests/drivers/net/hw/tls_hw_offload.py new file +2 -0
+def check_tls_support(cfg):
+    """Require kTLS on both hosts and at least one DUT HW direction."""

[ ... ]

tools/testing/selftests/drivers/net/hw/tls_hw_offload.py line 80

Quoted 10 lines
tools/testing/selftests/drivers/net/hw/tls_hw_offload.py new file +10 -0
+    try:
+        features = cmd(f"ethtool -k {cfg.ifname}").stdout
+    except CmdExitFailure as e:
+        raise KsftSkipEx(f"Cannot determine TLS HW offload support: {e}") from e
+    cfg.tls_hw_directions = {
+        direction for direction in ('Tx', 'Rx')
+        if f'tls-hw-{direction.lower()}-offload: on' in features
+    }
+    if not cfg.tls_hw_directions:
+        raise KsftSkipEx("Device has no enabled TLS HW offload direction")

[Severity: Medium]
On NICs whose offload only supports AES-GCM-128, will the tls12_aes256 variant of test_tls_offload() report FAIL instead of SKIP?

cfg.tls_hw_directions comes from the tls-hw-tx-offload and tls-hw-rx-offload feature flags. Those flags don't depend on the cipher. run_tls_test() then requires HW in those directions for every cipher.

Several in-tree drivers that advertise these features reject AES-GCM-256 in tls_dev_add():

drivers/net/ethernet/netronome/nfp/crypto/tls.c:nfp_net_cipher_supported() {
	switch (cipher_type) {
	case TLS_CIPHER_AES_GCM_128:
		...
		break;
	default:
		return false;
	}
	...
}

The other drivers do the same:

- fun_ktls_add() in funeth returns -EOPNOTSUPP for any cipher other than AES-GCM-128.
- chcr_ktls_save_keys() returns -EINVAL for non-128 ciphers.
- mlx5 only supports AES-256 when MLX5_CAP_TLS(tls_1_2_aes_gcm_256) is set.

When tls_dev_add() fails, do_tls_setsockopt_conf() falls back to SW without returning an error:

net/tls/tls_main.c:do_tls_setsockopt_conf() {
	...
		rc = tls_set_device_offload(sk);
		conf = TLS_HW;
		if (!rc) {
			...
		} else {
			rc = tls_set_sw_offload(sk, 1,
						update ? crypto_info : NULL);
	...
}

The helper's setsockopt() calls succeed and the echo passes. Only TlsTxSw/TlsRxSw go up. check_path() then fails with "HW offload not engaged (Device=0, Sw=1)".

The HW_NIC_COUNTERS comment lists nfp, cxgb4 and funeth, so this would hit hardware the test means to cover.

Could the AES-256 variant skip, or accept SW, when the DUT driver doesn't offload that cipher?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261102213011.4191020-1-rjethwani%40purestorage.com
#3 lore raw

Re: [PATCH net v5 2/2] selftests: net: add TLS 1.2 hardware offload tests

Quoted 99 lines · On Tue, Nov 3, 2026 at 1:33 PM <[email protected]> wrote:

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

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] On offload NICs that only support AES-GCM-128, the tls12_aes256 variant of test_tls_offload reports FAIL when it should…

--

selftests: net: add TLS 1.2 hardware offload tests

This adds a two-node kTLS hardware offload selftest. A C helper installs fixed AES-GCM keys on both ends, and the peer echoes data back to the DUT. It checks that HW offload is used in each enabled DUT direction, covers AES-GCM-128/256, IPv4/IPv6 and several payload sizes, and adds handshake and alert record-type regression cases for the push_pending_record fix.

Quoted 6 lines
tools/testing/selftests/drivers/net/hw/tls_hw_offload.py new file +0 -0
new file mode 100755
index 0000000000000..e720ce6547610
@@ -0,0 +1,321 @@

[ ... ]

Quoted 2 lines
tools/testing/selftests/drivers/net/hw/tls_hw_offload.py new file +2 -0
+def check_tls_support(cfg):
+    """Require kTLS on both hosts and at least one DUT HW direction."""

[ ... ]

tools/testing/selftests/drivers/net/hw/tls_hw_offload.py line 80

Quoted 10 lines
tools/testing/selftests/drivers/net/hw/tls_hw_offload.py new file +10 -0
+    try:
+        features = cmd(f"ethtool -k {cfg.ifname}").stdout
+    except CmdExitFailure as e:
+        raise KsftSkipEx(f"Cannot determine TLS HW offload support: {e}") from e
+    cfg.tls_hw_directions = {
+        direction for direction in ('Tx', 'Rx')
+        if f'tls-hw-{direction.lower()}-offload: on' in features
+    }
+    if not cfg.tls_hw_directions:
+        raise KsftSkipEx("Device has no enabled TLS HW offload direction")

[Severity: Medium]
On NICs whose offload only supports AES-GCM-128, will the tls12_aes256 variant of test_tls_offload() report FAIL instead of SKIP?

cfg.tls_hw_directions comes from the tls-hw-tx-offload and tls-hw-rx-offload feature flags. Those flags don't depend on the cipher. run_tls_test() then requires HW in those directions for every cipher.

Several in-tree drivers that advertise these features reject AES-GCM-256 in tls_dev_add():

drivers/net/ethernet/netronome/nfp/crypto/tls.c:nfp_net_cipher_supported() {
        switch (cipher_type) {
        case TLS_CIPHER_AES_GCM_128:
                ...
                break;
        default:
                return false;
        }
        ...
}

The other drivers do the same:

- fun_ktls_add() in funeth returns -EOPNOTSUPP for any cipher other than AES-GCM-128.
- chcr_ktls_save_keys() returns -EINVAL for non-128 ciphers.
- mlx5 only supports AES-256 when MLX5_CAP_TLS(tls_1_2_aes_gcm_256) is set.

When tls_dev_add() fails, do_tls_setsockopt_conf() falls back to SW without returning an error:

net/tls/tls_main.c:do_tls_setsockopt_conf() {
        ...
                rc = tls_set_device_offload(sk);
                conf = TLS_HW;
                if (!rc) {
                        ...
                } else {
                        rc = tls_set_sw_offload(sk, 1,
                                                update ? crypto_info : NULL);
        ...
}

The helper's setsockopt() calls succeed and the echo passes. Only TlsTxSw/TlsRxSw go up. check_path() then fails with "HW offload not engaged (Device=0, Sw=1)".

Quoted 5 lines

The HW_NIC_COUNTERS comment lists nfp, cxgb4 and funeth, so this would hit hardware the test means to cover.

Could the AES-256 variant skip, or accept SW, when the DUT driver doesn't offload that cipher?

An earlier version did skip tls12_aes256 for drivers whose tls_dev_add() accepts only AES-GCM-128
(a GCM128_ONLY_DRIVERS list matched against ethtool -i). Jakub asked for it to be removed:

https://lore.kernel.org/netdev/[email protected]/
"No need, just let them fail. There are automated systems which
check if failures are regressions or not.
We only care about regressions and crashes in HW tests. No device
supports all features and cleanly
skipping for all conditions is a fool's errand."

So on nfp, funeth, cxgb4 and mlx5 without tls_1_2_aes_gcm_256, failing that variant is expected.
Accepting SW instead would hide a real AES-256 regression on devices that do offload it.
The test only skips when an environment prerequisite is missing: kTLS isn't available,
or the device has no TLS HW offload direction enabled.

Quoted 2 lines

--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261102213011.4191020-1-rjethwani%40purestorage.com

#4 lore raw
In reply to Rishikesh Jethwani
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Mon, 2 Nov 2026 14:30:06 -0700 you wrote:

tls_set_sw_offload() assigns ctx->push_pending_record for both directions, but the hook is a TX-side dispatcher. On a socket whose TX side is HW offloaded, tls_set_device_offload() installs tls_device_push_pending_record(); any later TLS_RX setsockopt overwrites it with tls_sw_push_pending_record(), because tls_set_device_offload_rx() itself calls tls_set_sw_offload(sk, 0, NULL) for the software half of a successful HW RX offload, and so does the fallback when the RX offload is declined (NIC with TX-only offload, tls_dev_add() failure).

[...]

Here is the summary with links:
- [net,v5,1/2] tls: only install push_pending_record for a TX context

    https://git.kernel.org/netdev/net/c/c47e766c5307
  - [net,v5,2/2] selftests: net: add TLS 1.2 hardware offload tests
    https://git.kernel.org/netdev/net/c/ec7b283e11e4

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html