AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/07/29 15:06 flow patch-triage
0m Args:
null

Results:
{
  "EnableConfigs": [
    "NETDEVSIM",
    "TLS",
    "TLS_DEVICE"
  ],
  "FocusSymbols": [
    "nsim_do_tls",
    "nsim_tls_init",
    "nsim_tls_teardown"
  ],
  "Reasoning": "The patch introduces emulated kTLS offload to the netdevsim driver. This is a functional change that adds new reachable code paths for testing TLS hardware offload in a virtualized environment. The code is reachable via standard socket options and network operations when netdevsim is used.",
  "WorthFuzzing": true
}

1/1 2026/07/29 15:06 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit ef6d037b829ae6d74119190a2186b84e54e5a862\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Wed Jul 29 15:06:18 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/drivers/net/Kconfig b/drivers/net/Kconfig\nindex ff79c466712df..cb4d4bb87afc2 100644\n--- a/drivers/net/Kconfig\n+++ b/drivers/net/Kconfig\n@@ -607,6 +607,7 @@ config NETDEVSIM\n \tdepends on INET\n \tdepends on PSAMPLE || PSAMPLE=n\n \tdepends on PTP_1588_CLOCK_MOCK || PTP_1588_CLOCK_MOCK=n\n+\tdepends on TLS || TLS=n\n \tselect NET_DEVLINK\n \tselect PAGE_POOL\n \tselect NET_SHAPER\ndiff --git a/drivers/net/netdevsim/Makefile b/drivers/net/netdevsim/Makefile\nindex 87718204fb4de..c7f7621323b2b 100644\n--- a/drivers/net/netdevsim/Makefile\n+++ b/drivers/net/netdevsim/Makefile\n@@ -25,3 +25,7 @@ endif\n ifneq ($(CONFIG_MACSEC),)\n netdevsim-objs += macsec.o\n endif\n+\n+ifneq ($(CONFIG_TLS_DEVICE),)\n+netdevsim-objs += tls.o\n+endif\ndiff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c\nindex 4e9d7e10b527e..8dc0b0a7d1a13 100644\n--- a/drivers/net/netdevsim/netdev.c\n+++ b/drivers/net/netdevsim/netdev.c\n@@ -151,6 +151,8 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)\n \tif (dr)\n \t\tgoto out_drop_free;\n \n+\tnsim_do_tls(skb, ns, peer_ns);\n+\n \trxq = skb_get_queue_mapping(skb);\n \tif (rxq \u003e= peer_dev-\u003enum_rx_queues)\n \t\trxq = rxq % peer_dev-\u003enum_rx_queues;\n@@ -1066,6 +1068,7 @@ static int nsim_init_netdevsim(struct netdevsim *ns)\n \n \tnsim_macsec_init(ns);\n \tnsim_ipsec_init(ns);\n+\tnsim_tls_init(ns);\n \n \terr = register_netdevice(ns-\u003enetdev);\n \tif (err)\n@@ -1093,6 +1096,7 @@ static int nsim_init_netdevsim(struct netdevsim *ns)\n \tRCU_INIT_POINTER(ns-\u003epeer, NULL);\n \tunregister_netdevice(ns-\u003enetdev);\n err_ipsec_teardown:\n+\tnsim_tls_teardown(ns);\n \tnsim_ipsec_teardown(ns);\n \tnsim_macsec_teardown(ns);\n \tnsim_bpf_uninit(ns);\n@@ -1195,6 +1199,7 @@ void nsim_destroy(struct netdevsim *ns)\n \tRCU_INIT_POINTER(ns-\u003epeer, NULL);\n \tunregister_netdevice(dev);\n \tif (nsim_dev_port_is_pf(ns-\u003ensim_dev_port)) {\n+\t\tnsim_tls_teardown(ns);\n \t\tnsim_macsec_teardown(ns);\n \t\tnsim_ipsec_teardown(ns);\n \t\tnsim_bpf_uninit(ns);\ndiff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h\nindex 64f77f93d937c..582a76ace9d35 100644\n--- a/drivers/net/netdevsim/netdevsim.h\n+++ b/drivers/net/netdevsim/netdevsim.h\n@@ -56,6 +56,20 @@ struct nsim_ipsec {\n \tu32 tx;\n };\n \n+struct nsim_tls {\n+\tstruct list_head conns;\n+\tspinlock_t lock;\t/* protects conns and the counters below */\n+\tstruct dentry *dfile;\n+\tu32 count;\n+\tu32 tx_conn;\n+\tu32 rx_conn;\n+\tatomic64_t tx_packets;\n+\tatomic64_t tx_bytes;\n+\tatomic64_t rx_packets;\n+\tatomic64_t rx_bytes;\n+\tatomic64_t resyncs;\n+};\n+\n #define NSIM_MACSEC_MAX_SECY_COUNT 3\n #define NSIM_MACSEC_MAX_RXSC_COUNT 1\n struct nsim_rxsc {\n@@ -141,6 +155,7 @@ struct netdevsim {\n \tbool bpf_map_accept;\n \tstruct nsim_ipsec ipsec;\n \tstruct nsim_macsec macsec;\n+\tstruct nsim_tls tls;\n \tstruct nsim_vlan vlan;\n \tstruct {\n \t\tu32 inject_error;\n@@ -435,6 +450,26 @@ static inline bool nsim_ipsec_tx(struct netdevsim *ns, struct sk_buff *skb)\n }\n #endif\n \n+#if IS_ENABLED(CONFIG_TLS_DEVICE)\n+void nsim_tls_init(struct netdevsim *ns);\n+void nsim_tls_teardown(struct netdevsim *ns);\n+void nsim_do_tls(struct sk_buff *skb, struct netdevsim *ns,\n+\t\t struct netdevsim *peer_ns);\n+#else\n+static inline void nsim_tls_init(struct netdevsim *ns)\n+{\n+}\n+\n+static inline void nsim_tls_teardown(struct netdevsim *ns)\n+{\n+}\n+\n+static inline void nsim_do_tls(struct sk_buff *skb, struct netdevsim *ns,\n+\t\t\t       struct netdevsim *peer_ns)\n+{\n+}\n+#endif\n+\n #if IS_ENABLED(CONFIG_MACSEC)\n void nsim_macsec_init(struct netdevsim *ns);\n void nsim_macsec_teardown(struct netdevsim *ns);\ndiff --git a/drivers/net/netdevsim/tls.c b/drivers/net/netdevsim/tls.c\nnew file mode 100644\nindex 0000000000000..f695f2e2beb80\n--- /dev/null\n+++ b/drivers/net/netdevsim/tls.c\n@@ -0,0 +1,418 @@\n+// SPDX-License-Identifier: GPL-2.0\n+\n+#include \u003clinux/ipv6.h\u003e\n+#include \u003clinux/list.h\u003e\n+#include \u003clinux/skbuff.h\u003e\n+#include \u003clinux/slab.h\u003e\n+#include \u003clinux/tcp.h\u003e\n+#include \u003cnet/ip.h\u003e\n+#include \u003cnet/ipv6.h\u003e\n+#include \u003cnet/tls.h\u003e\n+\n+#include \"netdevsim.h\"\n+\n+/* Emulated kTLS offload.  No crypto is performed, so this is only self\n+ * consistent between two netdevsim ports.\n+ */\n+\n+#define NSIM_TLS_MAX_CONN\t32\n+\n+/* netdev_fix_features() drops NETIF_F_HW_TLS_RX unless the device also does\n+ * RX checksums, which every offload capable NIC does.\n+ */\n+#define NSIM_TLS_FEATURES\t(NETIF_F_HW_TLS_TX | NETIF_F_HW_TLS_RX | \\\n+\t\t\t\t NETIF_F_RXCSUM)\n+\n+struct nsim_tls_conn {\n+\tstruct list_head list;\n+\tstruct rcu_head rcu;\n+\n+\t/* Identity as seen on the wire, from the point of view of the port\n+\t * the offload was installed on: l* is this side, r* is the peer.\n+\t */\n+\tstruct in6_addr laddr;\n+\tstruct in6_addr raddr;\n+\t__be16 lport;\n+\t__be16 rport;\n+\tu16 family;\n+\n+\tenum tls_offload_ctx_dir dir;\n+\tu32 start_sn;\n+\tu16 cipher_type;\n+\tconst struct tls_context *tls_ctx;\n+};\n+\n+struct nsim_tls_tuple {\n+\tstruct in6_addr saddr;\n+\tstruct in6_addr daddr;\n+\t__be16 sport;\n+\t__be16 dport;\n+};\n+\n+static bool nsim_tls_parse(const struct sk_buff *skb,\n+\t\t\t   struct nsim_tls_tuple *t)\n+{\n+\tconst struct tcphdr *th;\n+\n+\tswitch (skb-\u003eprotocol) {\n+\tcase htons(ETH_P_IP): {\n+\t\tconst struct iphdr *iph = ip_hdr(skb);\n+\n+\t\tif (iph-\u003eprotocol != IPPROTO_TCP)\n+\t\t\treturn false;\n+\t\tipv6_addr_set_v4mapped(iph-\u003esaddr, \u0026t-\u003esaddr);\n+\t\tipv6_addr_set_v4mapped(iph-\u003edaddr, \u0026t-\u003edaddr);\n+\t\tbreak;\n+\t}\n+\tcase htons(ETH_P_IPV6): {\n+\t\tconst struct ipv6hdr *ip6h = ipv6_hdr(skb);\n+\n+\t\tif (ip6h-\u003enexthdr != IPPROTO_TCP)\n+\t\t\treturn false;\n+\t\tt-\u003esaddr = ip6h-\u003esaddr;\n+\t\tt-\u003edaddr = ip6h-\u003edaddr;\n+\t\tbreak;\n+\t}\n+\tdefault:\n+\t\treturn false;\n+\t}\n+\n+\tth = tcp_hdr(skb);\n+\tt-\u003esport = th-\u003esource;\n+\tt-\u003edport = th-\u003edest;\n+\n+\treturn true;\n+}\n+\n+/* On RX there is no socket to look at yet, so match the tuple installed at\n+ * -\u003etls_dev_add() time, which is what the hardware does.  The packet is\n+ * arriving, so the peer of the connection is its source.\n+ */\n+static bool nsim_tls_rx_offloaded(struct netdevsim *ns,\n+\t\t\t\t  const struct nsim_tls_tuple *t)\n+{\n+\tstruct nsim_tls_conn *conn;\n+\n+\tlist_for_each_entry_rcu(conn, \u0026ns-\u003etls.conns, list)\n+\t\tif (conn-\u003edir == TLS_OFFLOAD_CTX_DIR_RX \u0026\u0026\n+\t\t    conn-\u003elport == t-\u003edport \u0026\u0026 conn-\u003erport == t-\u003esport \u0026\u0026\n+\t\t    ipv6_addr_equal(\u0026conn-\u003eladdr, \u0026t-\u003edaddr) \u0026\u0026\n+\t\t    ipv6_addr_equal(\u0026conn-\u003eraddr, \u0026t-\u003esaddr))\n+\t\t\treturn true;\n+\n+\treturn false;\n+}\n+\n+/* The stack leaves a tag sized placeholder at the end of every record for\n+ * the device to write the authentication tag into.  We do not encrypt, so\n+ * nothing ever fills it in, and it comes from a page frag that was never\n+ * zeroed - clear it rather than leak uninitialized memory onto the wire.\n+ *\n+ * tls_append_frag() either grows the last frag or adds one, so the tag is\n+ * always the tail of the record's last frag.  Clearing it there rather than\n+ * in the skb covers every segment the record was split into, and any\n+ * retransmit of them, since they all share these pages.\n+ */\n+static void nsim_tls_tx_zero_tags(struct sk_buff *skb)\n+{\n+\tstruct tls_context *ctx = tls_get_ctx(skb-\u003esk);\n+\tconst struct tcphdr *th = tcp_hdr(skb);\n+\tstruct tls_offload_context_tx *tx_ctx;\n+\tu32 seq, end, tag_size;\n+\tunsigned long flags;\n+\tunsigned int off;\n+\n+\toff = skb_transport_offset(skb) + __tcp_hdrlen(th);\n+\tif (off \u003e= skb-\u003elen)\n+\t\treturn;\n+\n+\ttag_size = ctx-\u003eprot_info.tag_size;\n+\tseq = ntohl(th-\u003eseq);\n+\tend = seq + skb-\u003elen - off;\n+\ttx_ctx = tls_offload_ctx_tx(ctx);\n+\n+\tspin_lock_irqsave(\u0026tx_ctx-\u003elock, flags);\n+\twhile (before(seq, end)) {\n+\t\tstruct tls_record_info *record;\n+\t\tskb_frag_t *frag;\n+\t\tu64 rcd_sn;\n+\n+\t\trecord = tls_get_record(tx_ctx, seq, \u0026rcd_sn);\n+\t\tif (!record || tls_record_is_start_marker(record))\n+\t\t\tbreak;\n+\n+\t\tfrag = \u0026record-\u003efrags[record-\u003enum_frags - 1];\n+\t\tmemset(skb_frag_address(frag) + skb_frag_size(frag) - tag_size,\n+\t\t       0, tag_size);\n+\n+\t\tseq = record-\u003eend_seq;\n+\t}\n+\tspin_unlock_irqrestore(\u0026tx_ctx-\u003elock, flags);\n+}\n+\n+/* Stand in for the inline encryption the hardware would do on the way out.\n+ * The stack has already framed the record and we do not encrypt, so the tag\n+ * is all that is left to deal with.  The socket is right here, so identify\n+ * the connection the way the real drivers do.\n+ */\n+static void nsim_tls_tx(struct netdevsim *ns, struct sk_buff *skb)\n+{\n+\tif (!tls_is_skb_tx_device_offloaded(skb))\n+\t\treturn;\n+\n+\tnsim_tls_tx_zero_tags(skb);\n+\tatomic64_inc(\u0026ns-\u003etls.tx_packets);\n+\tatomic64_add(skb-\u003elen, \u0026ns-\u003etls.tx_bytes);\n+}\n+\n+/* Stand in for the inline decryption the peer's hardware would do, since we\n+ * are about to hand the skb to its stack.  We do not decrypt either, so all\n+ * that is needed is the flag that tells the kTLS core the payload is\n+ * already plaintext.\n+ */\n+static void nsim_tls_rx(struct netdevsim *ns, struct sk_buff *skb)\n+{\n+\tstruct nsim_tls_tuple t;\n+\n+\tif (list_empty(\u0026ns-\u003etls.conns))\n+\t\treturn;\n+\n+\tif (!nsim_tls_parse(skb, \u0026t))\n+\t\treturn;\n+\n+\tif (!nsim_tls_rx_offloaded(ns, \u0026t))\n+\t\treturn;\n+\n+\tskb-\u003edecrypted = 1;\n+\tatomic64_inc(\u0026ns-\u003etls.rx_packets);\n+\tatomic64_add(skb-\u003elen, \u0026ns-\u003etls.rx_bytes);\n+}\n+\n+/* netdevsim has no wire: nsim_start_xmit() hands the skb straight to the\n+ * peer's receive path, so this one call site stands in for the hardware of\n+ * both ports.  It has to run before nsim_forward_skb() moves the skb over,\n+ * which resets the headers.\n+ */\n+void nsim_do_tls(struct sk_buff *skb, struct netdevsim *ns,\n+\t\t struct netdevsim *peer_ns)\n+{\n+\t/* nsim_do_psp() ran first and may have wrapped the record stream, so\n+\t * this is no longer a plain TLS over TCP packet.  No NIC chains the\n+\t * two inline offloads either, so leave it alone.\n+\t */\n+\tif (skb-\u003eencapsulation)\n+\t\treturn;\n+\n+\tnsim_tls_tx(ns, skb);\n+\tnsim_tls_rx(peer_ns, skb);\n+}\n+\n+static int nsim_tls_dev_add(struct net_device *netdev, struct sock *sk,\n+\t\t\t    enum tls_offload_ctx_dir direction,\n+\t\t\t    struct tls_crypto_info *crypto_info,\n+\t\t\t    u32 start_offload_tcp_sn)\n+{\n+\tstruct netdevsim *ns = netdev_priv(netdev);\n+\tstruct nsim_tls_conn *conn;\n+\tint ret = 0;\n+\n+\t/* Mirror the cipher support of a typical offload capable NIC. */\n+\tswitch (crypto_info-\u003ecipher_type) {\n+\tcase TLS_CIPHER_AES_GCM_128:\n+\tcase TLS_CIPHER_AES_GCM_256:\n+\t\tbreak;\n+\tdefault:\n+\t\treturn -EOPNOTSUPP;\n+\t}\n+\n+\tconn = kzalloc_obj(*conn);\n+\tif (!conn)\n+\t\treturn -ENOMEM;\n+\n+\tif (sk-\u003esk_family == AF_INET6) {\n+\t\tconn-\u003eladdr = sk-\u003esk_v6_rcv_saddr;\n+\t\tconn-\u003eraddr = sk-\u003esk_v6_daddr;\n+\t} else {\n+\t\tipv6_addr_set_v4mapped(sk-\u003esk_rcv_saddr, \u0026conn-\u003eladdr);\n+\t\tipv6_addr_set_v4mapped(sk-\u003esk_daddr, \u0026conn-\u003eraddr);\n+\t}\n+\tconn-\u003efamily = sk-\u003esk_family;\n+\tconn-\u003elport = htons(inet_sk(sk)-\u003einet_num);\n+\tconn-\u003erport = sk-\u003esk_dport;\n+\tconn-\u003edir = direction;\n+\tconn-\u003estart_sn = start_offload_tcp_sn;\n+\tconn-\u003ecipher_type = crypto_info-\u003ecipher_type;\n+\tconn-\u003etls_ctx = tls_get_ctx(sk);\n+\n+\tspin_lock_bh(\u0026ns-\u003etls.lock);\n+\tif (ns-\u003etls.count \u003e= NSIM_TLS_MAX_CONN) {\n+\t\tret = -ENOSPC;\n+\t\tgoto out_unlock;\n+\t}\n+\tlist_add_tail_rcu(\u0026conn-\u003elist, \u0026ns-\u003etls.conns);\n+\tns-\u003etls.count++;\n+\tif (direction == TLS_OFFLOAD_CTX_DIR_TX)\n+\t\tns-\u003etls.tx_conn++;\n+\telse\n+\t\tns-\u003etls.rx_conn++;\n+out_unlock:\n+\tspin_unlock_bh(\u0026ns-\u003etls.lock);\n+\n+\tif (ret)\n+\t\tkfree(conn);\n+\n+\treturn ret;\n+}\n+\n+static void nsim_tls_dev_del(struct net_device *netdev,\n+\t\t\t     struct tls_context *tls_ctx,\n+\t\t\t     enum tls_offload_ctx_dir direction)\n+{\n+\tstruct netdevsim *ns = netdev_priv(netdev);\n+\tstruct nsim_tls_conn *conn;\n+\n+\tspin_lock_bh(\u0026ns-\u003etls.lock);\n+\tlist_for_each_entry(conn, \u0026ns-\u003etls.conns, list) {\n+\t\tif (conn-\u003etls_ctx != tls_ctx || conn-\u003edir != direction)\n+\t\t\tcontinue;\n+\n+\t\tlist_del_rcu(\u0026conn-\u003elist);\n+\t\tns-\u003etls.count--;\n+\t\tif (direction == TLS_OFFLOAD_CTX_DIR_TX)\n+\t\t\tns-\u003etls.tx_conn--;\n+\t\telse\n+\t\t\tns-\u003etls.rx_conn--;\n+\t\tspin_unlock_bh(\u0026ns-\u003etls.lock);\n+\n+\t\tkfree_rcu(conn, rcu);\n+\t\treturn;\n+\t}\n+\tspin_unlock_bh(\u0026ns-\u003etls.lock);\n+\n+\tnetdev_err(netdev, \"TLS %s context not found on del\\n\",\n+\t\t   direction == TLS_OFFLOAD_CTX_DIR_TX ? \"tx\" : \"rx\");\n+}\n+\n+/* Nothing is ever out of sync here: the emulation does not decrypt, so it\n+ * never loses the record boundaries and never asks for a resync.  The core\n+ * can still call in on the RX path, so account for it and move on.\n+ */\n+static int nsim_tls_dev_resync(struct net_device *netdev, struct sock *sk,\n+\t\t\t       u32 seq, u8 *rcd_sn,\n+\t\t\t       enum tls_offload_ctx_dir direction)\n+{\n+\tstruct netdevsim *ns = netdev_priv(netdev);\n+\n+\tatomic64_inc(\u0026ns-\u003etls.resyncs);\n+\n+\treturn 0;\n+}\n+\n+static const struct tlsdev_ops nsim_tlsdev_ops = {\n+\t.tls_dev_add\t= nsim_tls_dev_add,\n+\t.tls_dev_del\t= nsim_tls_dev_del,\n+\t.tls_dev_resync\t= nsim_tls_dev_resync,\n+};\n+\n+static ssize_t nsim_tls_dbg_read(struct file *filp, char __user *buffer,\n+\t\t\t\t size_t count, loff_t *ppos)\n+{\n+\tstruct netdevsim *ns = filp-\u003eprivate_data;\n+\tstruct nsim_tls_conn *conn;\n+\tsize_t bufsize;\n+\tchar *buf, *p;\n+\tint len;\n+\n+\t/* Two full IPv6 addresses and ports fit in 160 bytes a line. */\n+\tbufsize = (NSIM_TLS_MAX_CONN * 160) + 200;\n+\tbuf = kzalloc(bufsize, GFP_KERNEL);\n+\tif (!buf)\n+\t\treturn -ENOMEM;\n+\n+\tp = buf;\n+\n+\tspin_lock_bh(\u0026ns-\u003etls.lock);\n+\tp += scnprintf(p, bufsize - (p - buf),\n+\t\t       \"conn count=%u tx=%u rx=%u\\n\",\n+\t\t       ns-\u003etls.count, ns-\u003etls.tx_conn, ns-\u003etls.rx_conn);\n+\tp += scnprintf(p, bufsize - (p - buf),\n+\t\t       \"tx_packets=%llu tx_bytes=%llu rx_packets=%llu rx_bytes=%llu resyncs=%llu\\n\",\n+\t\t       atomic64_read(\u0026ns-\u003etls.tx_packets),\n+\t\t       atomic64_read(\u0026ns-\u003etls.tx_bytes),\n+\t\t       atomic64_read(\u0026ns-\u003etls.rx_packets),\n+\t\t       atomic64_read(\u0026ns-\u003etls.rx_bytes),\n+\t\t       atomic64_read(\u0026ns-\u003etls.resyncs));\n+\n+\tlist_for_each_entry(conn, \u0026ns-\u003etls.conns, list) {\n+\t\tif (conn-\u003efamily == AF_INET6)\n+\t\t\tp += scnprintf(p, bufsize - (p - buf),\n+\t\t\t\t       \"%s [%pI6c]:%u -\u003e [%pI6c]:%u cipher=%u sn=%u\\n\",\n+\t\t\t\t       conn-\u003edir == TLS_OFFLOAD_CTX_DIR_TX ?\n+\t\t\t\t       \"tx\" : \"rx\",\n+\t\t\t\t       \u0026conn-\u003eladdr, ntohs(conn-\u003elport),\n+\t\t\t\t       \u0026conn-\u003eraddr, ntohs(conn-\u003erport),\n+\t\t\t\t       conn-\u003ecipher_type, conn-\u003estart_sn);\n+\t\telse\n+\t\t\tp += scnprintf(p, bufsize - (p - buf),\n+\t\t\t\t       \"%s %pI4:%u -\u003e %pI4:%u cipher=%u sn=%u\\n\",\n+\t\t\t\t       conn-\u003edir == TLS_OFFLOAD_CTX_DIR_TX ?\n+\t\t\t\t       \"tx\" : \"rx\",\n+\t\t\t\t       \u0026conn-\u003eladdr.s6_addr32[3],\n+\t\t\t\t       ntohs(conn-\u003elport),\n+\t\t\t\t       \u0026conn-\u003eraddr.s6_addr32[3],\n+\t\t\t\t       ntohs(conn-\u003erport),\n+\t\t\t\t       conn-\u003ecipher_type, conn-\u003estart_sn);\n+\t}\n+\tspin_unlock_bh(\u0026ns-\u003etls.lock);\n+\n+\tlen = simple_read_from_buffer(buffer, count, ppos, buf, p - buf);\n+\n+\tkfree(buf);\n+\n+\treturn len;\n+}\n+\n+static const struct file_operations nsim_tls_dbg_fops = {\n+\t.owner = THIS_MODULE,\n+\t.open = simple_open,\n+\t.read = nsim_tls_dbg_read,\n+\t.llseek = default_llseek,\n+};\n+\n+void nsim_tls_init(struct netdevsim *ns)\n+{\n+\tINIT_LIST_HEAD(\u0026ns-\u003etls.conns);\n+\tspin_lock_init(\u0026ns-\u003etls.lock);\n+\n+\tns-\u003enetdev-\u003etlsdev_ops = \u0026nsim_tlsdev_ops;\n+\tns-\u003enetdev-\u003efeatures |= NSIM_TLS_FEATURES;\n+\tns-\u003enetdev-\u003ehw_features |= NSIM_TLS_FEATURES;\n+\n+\tns-\u003etls.dfile = debugfs_create_file(\"tls\", 0400,\n+\t\t\t\t\t    ns-\u003ensim_dev_port-\u003eddir, ns,\n+\t\t\t\t\t    \u0026nsim_tls_dbg_fops);\n+}\n+\n+void nsim_tls_teardown(struct netdevsim *ns)\n+{\n+\tstruct nsim_tls_conn *conn, *tmp;\n+\tu32 left;\n+\n+\tdebugfs_remove_recursive(ns-\u003etls.dfile);\n+\n+\tspin_lock_bh(\u0026ns-\u003etls.lock);\n+\tleft = ns-\u003etls.count;\n+\tlist_for_each_entry_safe(conn, tmp, \u0026ns-\u003etls.conns, list) {\n+\t\tlist_del_rcu(\u0026conn-\u003elist);\n+\t\tkfree_rcu(conn, rcu);\n+\t}\n+\tns-\u003etls.count = 0;\n+\tns-\u003etls.tx_conn = 0;\n+\tns-\u003etls.rx_conn = 0;\n+\tspin_unlock_bh(\u0026ns-\u003etls.lock);\n+\n+\tif (left)\n+\t\tnetdev_err(ns-\u003enetdev,\n+\t\t\t   \"tearing down TLS offload with %u connections left\\n\",\n+\t\t\t   left);\n+}\ndiff --git a/tools/testing/selftests/drivers/net/netdevsim/.gitignore b/tools/testing/selftests/drivers/net/netdevsim/.gitignore\nnew file mode 100644\nindex 0000000000000..9d4f57cb2baa9\n--- /dev/null\n+++ b/tools/testing/selftests/drivers/net/netdevsim/.gitignore\n@@ -0,0 +1,2 @@\n+# SPDX-License-Identifier: GPL-2.0-only\n+tls_offload\ndiff --git a/tools/testing/selftests/drivers/net/netdevsim/Makefile b/tools/testing/selftests/drivers/net/netdevsim/Makefile\nindex 9808c2fbae9e8..5beb4ed64eb52 100644\n--- a/tools/testing/selftests/drivers/net/netdevsim/Makefile\n+++ b/tools/testing/selftests/drivers/net/netdevsim/Makefile\n@@ -1,5 +1,7 @@\n # SPDX-License-Identifier: GPL-2.0+ OR MIT\n \n+CFLAGS += $(KHDR_INCLUDES)\n+\n TEST_PROGS := \\\n \tdevlink.sh \\\n \tdevlink_in_netns.sh \\\n@@ -15,9 +17,14 @@ TEST_PROGS := \\\n \tpeer.sh \\\n \tpsample.sh \\\n \ttc-mq-visibility.sh \\\n+\ttls.sh \\\n \tudp_tunnel_nic.sh \\\n # end of TEST_PROGS\n \n+TEST_GEN_FILES := \\\n+\ttls_offload\n+# end of TEST_GEN_FILES\n+\n TEST_FILES := \\\n \tethtool-common.sh\n # end of TEST_FILES\ndiff --git a/tools/testing/selftests/drivers/net/netdevsim/tls.sh b/tools/testing/selftests/drivers/net/netdevsim/tls.sh\nnew file mode 100755\nindex 0000000000000..399e2c4d382df\n--- /dev/null\n+++ b/tools/testing/selftests/drivers/net/netdevsim/tls.sh\n@@ -0,0 +1,271 @@\n+#!/bin/bash\n+# SPDX-License-Identifier: GPL-2.0-only\n+#\n+# Exercise netdevsim's emulated kTLS device offload over a linked\n+# netdevsim pair, one port per network namespace.\n+#\n+# shellcheck disable=SC2154 # ksft_skip comes from lib.sh\n+\n+lib_dir=$(dirname \"$0\")\n+# shellcheck source=./../../../net/lib.sh\n+# shellcheck disable=SC1091\n+source \"$lib_dir\"/../../../net/lib.sh\n+\n+NSIM_DEV_1_ID=$((256 + RANDOM % 256))\n+NSIM_DEV_1_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_1_ID\n+NSIM_DEV_2_ID=$((512 + RANDOM % 256))\n+NSIM_DEV_2_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_2_ID\n+\n+NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device\n+NSIM_DEV_SYS_DEL=/sys/bus/netdevsim/del_device\n+NSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_device\n+\n+DEBUGFS=/sys/kernel/debug/netdevsim\n+NSIM_DEV_1_TLS=$DEBUGFS/netdevsim$NSIM_DEV_1_ID/ports/0/tls\n+NSIM_DEV_2_TLS=$DEBUGFS/netdevsim$NSIM_DEV_2_ID/ports/0/tls\n+\n+SRV_IP=192.168.13.1\n+CLI_IP=192.168.13.2\n+PORT=4433\n+\n+SYNCDIR=\n+BIN=$lib_dir/tls_offload\n+\n+num_pass=0\n+num_fail=0\n+\n+check()\n+{\n+\tlocal msg=\"$1\"\n+\tlocal ret=\"$2\"\n+\n+\tif [ \"$ret\" -eq 0 ]; then\n+\t\techo \"PASS: $msg\"\n+\t\tnum_pass=$((num_pass + 1))\n+\telse\n+\t\techo \"FAIL: $msg\"\n+\t\tnum_fail=$((num_fail + 1))\n+\tfi\n+}\n+\n+# shellcheck disable=SC2317,SC2329 # invoked from the EXIT trap\n+cleanup()\n+{\n+\tip netns del nscl 2\u003e/dev/null\n+\tip netns del nssv 2\u003e/dev/null\n+\techo \"$NSIM_DEV_2_ID\" \u003e \"$NSIM_DEV_SYS_DEL\" 2\u003e/dev/null\n+\techo \"$NSIM_DEV_1_ID\" \u003e \"$NSIM_DEV_SYS_DEL\" 2\u003e/dev/null\n+\t[ -n \"$SYNCDIR\" ] \u0026\u0026 rm -rf \"$SYNCDIR\"\n+}\n+\n+setup()\n+{\n+\tset -e\n+\n+\techo \"$NSIM_DEV_1_ID\" \u003e \"$NSIM_DEV_SYS_NEW\"\n+\techo \"$NSIM_DEV_2_ID\" \u003e \"$NSIM_DEV_SYS_NEW\"\n+\tudevadm settle 2\u003e/dev/null || sleep 1\n+\n+\tNSIM_DEV_1_NAME=$(find \"$NSIM_DEV_1_SYS\"/net -maxdepth 1 -type d ! \\\n+\t\t-path \"$NSIM_DEV_1_SYS\"/net -exec basename {} \\;)\n+\tNSIM_DEV_2_NAME=$(find \"$NSIM_DEV_2_SYS\"/net -maxdepth 1 -type d ! \\\n+\t\t-path \"$NSIM_DEV_2_SYS\"/net -exec basename {} \\;)\n+\n+\tip netns add nssv\n+\tip netns add nscl\n+\n+\tip link set \"$NSIM_DEV_1_NAME\" netns nssv\n+\tip link set \"$NSIM_DEV_2_NAME\" netns nscl\n+\n+\tip netns exec nssv ip addr add \"$SRV_IP/24\" dev \"$NSIM_DEV_1_NAME\"\n+\tip netns exec nscl ip addr add \"$CLI_IP/24\" dev \"$NSIM_DEV_2_NAME\"\n+\n+\tip netns exec nssv ip link set dev \"$NSIM_DEV_1_NAME\" up\n+\tip netns exec nscl ip link set dev \"$NSIM_DEV_2_NAME\" up\n+\n+\tNSIM_DEV_1_FD=$((256 + RANDOM % 256))\n+\texec {NSIM_DEV_1_FD}\u003c/var/run/netns/nssv\n+\tNSIM_DEV_1_IFIDX=$(ip netns exec nssv \\\n+\t\tcat /sys/class/net/\"$NSIM_DEV_1_NAME\"/ifindex)\n+\n+\tNSIM_DEV_2_FD=$((256 + RANDOM % 256))\n+\texec {NSIM_DEV_2_FD}\u003c/var/run/netns/nscl\n+\tNSIM_DEV_2_IFIDX=$(ip netns exec nscl \\\n+\t\tcat /sys/class/net/\"$NSIM_DEV_2_NAME\"/ifindex)\n+\n+\techo \"$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX\" \\\n+\t\t\u003e \"$NSIM_DEV_SYS_LINK\"\n+\n+\tSYNCDIR=$(mktemp -d)\n+\tset +e\n+}\n+\n+feature()\n+{\n+\tlocal netns=\"$1\"\n+\tlocal dev=\"$2\"\n+\tlocal feat=\"$3\"\n+\n+\tip netns exec \"$netns\" ethtool -k \"$dev\" 2\u003e/dev/null | \\\n+\t\tsed -n \"s/^$feat: \\([a-z]*\\).*/\\1/p\"\n+}\n+\n+dbg_field()\n+{\n+\tsed -n \"s/.*\\\u003c$2=\\([0-9]*\\).*/\\1/p\" \"$1\" | head -1\n+}\n+\n+tls_stat()\n+{\n+\tip netns exec \"$1\" cat /proc/net/tls_stat | \\\n+\t\tsed -n \"s/^$2 \\([0-9]*\\)/\\1/p\"\n+}\n+\n+# Both ends park once their offload is installed and before any data is\n+# sent, so the driver's context count can be sampled without racing the\n+# transfer.  Pass \"nosample\" when the offload is expected to be refused,\n+# since then neither end ever reaches the barrier.\n+run_pair()\n+{\n+\tlocal sample=\"${1:-sample}\"\n+\tlocal srv_rc cli_rc waited=0\n+\n+\trm -f \"$SYNCDIR\"/*.ready \"$SYNCDIR\"/go\n+\tCONNS_1=0\n+\tCONNS_2=0\n+\n+\tip netns exec nssv \"$BIN\" server \"$SRV_IP\" \"$PORT\" \"$SYNCDIR\" \u0026\n+\tlocal srv_pid=$!\n+\tip netns exec nscl \"$BIN\" client \"$SRV_IP\" \"$PORT\" \"$SYNCDIR\" \u0026\n+\tlocal cli_pid=$!\n+\n+\tif [ \"$sample\" = \"sample\" ]; then\n+\t\twhile [ ! -e \"$SYNCDIR/server.ready\" ] ||\n+\t\t      [ ! -e \"$SYNCDIR/client.ready\" ]; do\n+\t\t\t[ \"$waited\" -ge 200 ] \u0026\u0026 break\n+\t\t\tsleep 0.05\n+\t\t\twaited=$((waited + 1))\n+\t\tdone\n+\t\tCONNS_1=$(dbg_field \"$NSIM_DEV_1_TLS\" count)\n+\t\tCONNS_2=$(dbg_field \"$NSIM_DEV_2_TLS\" count)\n+\t\t: \"${CONNS_1:=0}\"\n+\t\t: \"${CONNS_2:=0}\"\n+\tfi\n+\ttouch \"$SYNCDIR/go\"\n+\n+\twait \"$srv_pid\"; srv_rc=$?\n+\twait \"$cli_pid\"; cli_rc=$?\n+\n+\t[ \"$srv_rc\" -eq 0 ] \u0026\u0026 [ \"$cli_rc\" -eq 0 ]\n+}\n+\n+###\n+### Code start\n+###\n+\n+if [ \"$(id -u)\" -ne 0 ]; then\n+\techo \"SKIP: need root\"\n+\texit \"$ksft_skip\"\n+fi\n+\n+if ! command -v ethtool \u003e/dev/null; then\n+\techo \"SKIP: ethtool not found\"\n+\texit \"$ksft_skip\"\n+fi\n+\n+if [ ! -x \"$BIN\" ]; then\n+\techo \"SKIP: $BIN not built\"\n+\texit \"$ksft_skip\"\n+fi\n+\n+modprobe netdevsim 2\u003e/dev/null\n+if [ ! -d /sys/bus/netdevsim ]; then\n+\techo \"SKIP: netdevsim not available\"\n+\texit \"$ksft_skip\"\n+fi\n+\n+modprobe tls 2\u003e/dev/null\n+if [ ! -e /proc/net/tls_stat ]; then\n+\techo \"SKIP: kernel TLS not available\"\n+\texit \"$ksft_skip\"\n+fi\n+\n+trap cleanup EXIT\n+setup\n+\n+# The offload has to be advertised, and on by default like the other\n+# netdevsim crypto offloads.\n+for f in tls-hw-tx-offload tls-hw-rx-offload; do\n+\t[ \"$(feature nssv \"$NSIM_DEV_1_NAME\" \"$f\")\" = \"on\" ]\n+\tcheck \"$f advertised and on by default\" $?\n+done\n+\n+[ -e \"$NSIM_DEV_1_TLS\" ]\n+check \"per-port debugfs tls file exists\" $?\n+\n+# Main data path run.\n+run_pair\n+check \"offloaded TLS data transfer\" $?\n+\n+# Sampled at the barrier, so each port must be holding exactly the TX and\n+# the RX context of its own socket.\n+[ \"$CONNS_1\" -eq 2 ] \u0026\u0026 [ \"$CONNS_2\" -eq 2 ]\n+check \"tx and rx contexts installed on both ports\" $?\n+\n+# Both ends must have gone through the device path, not the SW fallback.\n+[ \"$(tls_stat nssv TlsTxDevice)\" -ge 1 ] \u0026\u0026 \\\n+\t[ \"$(tls_stat nssv TlsRxDevice)\" -ge 1 ] \u0026\u0026 \\\n+\t[ \"$(tls_stat nscl TlsTxDevice)\" -ge 1 ] \u0026\u0026 \\\n+\t[ \"$(tls_stat nscl TlsRxDevice)\" -ge 1 ]\n+check \"both ends used the device path\" $?\n+\n+[ \"$(tls_stat nssv TlsTxSw)\" -eq 0 ] \u0026\u0026 [ \"$(tls_stat nscl TlsTxSw)\" -eq 0 ]\n+check \"no silent fallback to the software path\" $?\n+\n+[ \"$(tls_stat nssv TlsDecryptError)\" -eq 0 ] \u0026\u0026 \\\n+\t[ \"$(tls_stat nscl TlsDecryptError)\" -eq 0 ]\n+check \"no decrypt errors\" $?\n+\n+# The driver must have seen the records go by in both directions.\n+[ \"$(dbg_field \"$NSIM_DEV_1_TLS\" tx_packets)\" -ge 1 ] \u0026\u0026 \\\n+\t[ \"$(dbg_field \"$NSIM_DEV_1_TLS\" rx_packets)\" -ge 1 ] \u0026\u0026 \\\n+\t[ \"$(dbg_field \"$NSIM_DEV_2_TLS\" tx_packets)\" -ge 1 ] \u0026\u0026 \\\n+\t[ \"$(dbg_field \"$NSIM_DEV_2_TLS\" rx_packets)\" -ge 1 ]\n+check \"driver counted offloaded packets both ways\" $?\n+\n+# Sockets are closed by now, so every context must have been given back.\n+[ \"$(dbg_field \"$NSIM_DEV_1_TLS\" count)\" -eq 0 ] \u0026\u0026 \\\n+\t[ \"$(dbg_field \"$NSIM_DEV_2_TLS\" count)\" -eq 0 ]\n+check \"all offload contexts released on close\" $?\n+\n+[ \"$(tls_stat nssv TlsCurrTxDevice)\" -eq 0 ] \u0026\u0026 \\\n+\t[ \"$(tls_stat nssv TlsCurrRxDevice)\" -eq 0 ]\n+check \"no device contexts left behind\" $?\n+\n+# Turning the feature off has to make the offload refuse the connection;\n+# the test binary insists on the device path, so it must now fail.\n+ip netns exec nssv ethtool -K \"$NSIM_DEV_1_NAME\" tls-hw-tx-offload off\n+ip netns exec nssv ethtool -K \"$NSIM_DEV_1_NAME\" tls-hw-rx-offload off\n+ip netns exec nscl ethtool -K \"$NSIM_DEV_2_NAME\" tls-hw-tx-offload off\n+ip netns exec nscl ethtool -K \"$NSIM_DEV_2_NAME\" tls-hw-rx-offload off\n+\n+run_pair nosample\n+rc=$?\n+[ \"$rc\" -ne 0 ]\n+check \"offload declined once the feature is off\" $?\n+\n+[ \"$(tls_stat nssv TlsTxSw)\" -ge 1 ]\n+check \"software path used when offload is off\" $?\n+\n+ip netns exec nssv ethtool -K \"$NSIM_DEV_1_NAME\" tls-hw-tx-offload on\n+ip netns exec nssv ethtool -K \"$NSIM_DEV_1_NAME\" tls-hw-rx-offload on\n+ip netns exec nscl ethtool -K \"$NSIM_DEV_2_NAME\" tls-hw-tx-offload on\n+ip netns exec nscl ethtool -K \"$NSIM_DEV_2_NAME\" tls-hw-rx-offload on\n+\n+run_pair\n+check \"offload works again after re-enabling\" $?\n+\n+echo\n+echo \"passed: $num_pass failed: $num_fail\"\n+[ \"$num_fail\" -eq 0 ] \u0026\u0026 exit 0\n+exit 1\ndiff --git a/tools/testing/selftests/drivers/net/netdevsim/tls_offload.c b/tools/testing/selftests/drivers/net/netdevsim/tls_offload.c\nnew file mode 100644\nindex 0000000000000..ebfb4bfcd4f44\n--- /dev/null\n+++ b/tools/testing/selftests/drivers/net/netdevsim/tls_offload.c\n@@ -0,0 +1,498 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/*\n+ * kTLS device offload data path exercise, driven by tls.sh.\n+ *\n+ * One instance runs as the server and one as the client, each in its own\n+ * network namespace, connected back to back by a linked netdevsim pair.\n+ * Both ends enable kTLS and rely on netdevsim's emulated TLS offload, so\n+ * every record travels through net/tls/tls_device.c rather than the\n+ * software path.\n+ *\n+ * The two processes rendezvous through a shared directory so that neither\n+ * side sends before the other has installed its RX offload.\n+ */\n+\n+#define _GNU_SOURCE\n+\n+#include \u003carpa/inet.h\u003e\n+#include \u003cerrno.h\u003e\n+#include \u003cfcntl.h\u003e\n+#include \u003climits.h\u003e\n+#include \u003cnetinet/in.h\u003e\n+#include \u003cnetinet/tcp.h\u003e\n+#include \u003cstdarg.h\u003e\n+#include \u003cstdbool.h\u003e\n+#include \u003cstdint.h\u003e\n+#include \u003cstdio.h\u003e\n+#include \u003cstdlib.h\u003e\n+#include \u003cstring.h\u003e\n+#include \u003csys/socket.h\u003e\n+#include \u003csys/stat.h\u003e\n+#include \u003csys/types.h\u003e\n+#include \u003ctime.h\u003e\n+#include \u003cunistd.h\u003e\n+\n+#include \u003clinux/tls.h\u003e\n+\n+#ifndef SOL_TLS\n+#define SOL_TLS\t\t\t282\n+#endif\n+\n+#ifndef TCP_ULP\n+#define TCP_ULP\t\t\t31\n+#endif\n+\n+#define BULK_LEN\t\t(200 * 1024)\n+#define MORE_FRAGS\t\t64\n+#define SPLICE_FRAG_LEN\t\t4096\n+#define SPLICE_FRAGS\t\t8\n+\n+/* TLS_MIN_RECORD_SIZE_LIM and TLS_MAX_PAYLOAD_SIZE, which are not uapi. */\n+#define REC_LIM_MIN\t\t64\n+#define REC_LIM_MAX\t\t16384\n+\n+#define SMALL_RECS\t\t100\n+#define SMALL_LEN\t\t(REC_LIM_MIN * SMALL_RECS)\n+\n+#define SYNC_TIMEOUT_MS\t\t20000\n+#define CONNECT_TIMEOUT_MS\t20000\n+\n+static const char *role;\n+\n+static void die(const char *what)\n+{\n+\tfprintf(stderr, \"%s: %s: %s\\n\", role, what, strerror(errno));\n+\texit(1);\n+}\n+\n+static void fail(const char *fmt, ...)\n+{\n+\tva_list ap;\n+\n+\tfprintf(stderr, \"%s: \", role);\n+\tva_start(ap, fmt);\n+\tvfprintf(stderr, fmt, ap);\n+\tva_end(ap);\n+\tfprintf(stderr, \"\\n\");\n+\texit(1);\n+}\n+\n+static void msleep(unsigned int ms)\n+{\n+\tstruct timespec ts = {\n+\t\t.tv_sec = ms / 1000,\n+\t\t.tv_nsec = (ms % 1000) * 1000000L,\n+\t};\n+\n+\tnanosleep(\u0026ts, NULL);\n+}\n+\n+/* /proc/net/tls_stat is per netns, so both ends can check that their own\n+ * connection really landed on the device path.\n+ */\n+static unsigned long read_tls_stat(const char *name)\n+{\n+\tchar line[256];\n+\tunsigned long val;\n+\tFILE *f;\n+\n+\tf = fopen(\"/proc/net/tls_stat\", \"r\");\n+\tif (!f)\n+\t\tdie(\"open /proc/net/tls_stat\");\n+\n+\twhile (fgets(line, sizeof(line), f)) {\n+\t\tchar key[64];\n+\n+\t\tif (sscanf(line, \"%63s %lu\", key, \u0026val) != 2)\n+\t\t\tcontinue;\n+\t\tif (!strcmp(key, name)) {\n+\t\t\tfclose(f);\n+\t\t\treturn val;\n+\t\t}\n+\t}\n+\n+\tfclose(f);\n+\tfail(\"%s not found in /proc/net/tls_stat\", name);\n+\treturn 0;\n+}\n+\n+static void fill_pattern(char *buf, size_t len, unsigned int seed)\n+{\n+\tsize_t i;\n+\n+\tfor (i = 0; i \u003c len; i++)\n+\t\tbuf[i] = (char)(seed + i * 31 + (i \u003e\u003e 8) * 7);\n+}\n+\n+static void check_pattern(const char *buf, size_t len, unsigned int seed,\n+\t\t\t  const char *what)\n+{\n+\tchar *want = malloc(len);\n+\tsize_t i;\n+\n+\tif (!want)\n+\t\tdie(\"malloc\");\n+\n+\tfill_pattern(want, len, seed);\n+\tfor (i = 0; i \u003c len; i++) {\n+\t\tif (buf[i] != want[i])\n+\t\t\tfail(\"%s: payload mismatch at byte %zu: got 0x%02x want 0x%02x\",\n+\t\t\t     what, i, (unsigned char)buf[i],\n+\t\t\t     (unsigned char)want[i]);\n+\t}\n+\n+\tfree(want);\n+}\n+\n+static void write_all(int fd, const char *buf, size_t len)\n+{\n+\tsize_t done = 0;\n+\n+\twhile (done \u003c len) {\n+\t\tssize_t n = send(fd, buf + done, len - done, 0);\n+\n+\t\tif (n \u003c 0) {\n+\t\t\tif (errno == EINTR)\n+\t\t\t\tcontinue;\n+\t\t\tdie(\"send\");\n+\t\t}\n+\t\tdone += n;\n+\t}\n+}\n+\n+static void read_all(int fd, char *buf, size_t len)\n+{\n+\tsize_t done = 0;\n+\n+\twhile (done \u003c len) {\n+\t\tssize_t n = recv(fd, buf + done, len - done, 0);\n+\n+\t\tif (n \u003c 0) {\n+\t\t\tif (errno == EINTR)\n+\t\t\t\tcontinue;\n+\t\t\tdie(\"recv\");\n+\t\t}\n+\t\tif (n == 0)\n+\t\t\tfail(\"peer closed after %zu of %zu bytes\", done, len);\n+\t\tdone += n;\n+\t}\n+}\n+\n+static void enable_ktls(int fd)\n+{\n+\tstruct tls12_crypto_info_aes_gcm_128 ci = {};\n+\tunsigned long tx_before, rx_before;\n+\n+\ttx_before = read_tls_stat(\"TlsTxDevice\");\n+\trx_before = read_tls_stat(\"TlsRxDevice\");\n+\n+\tif (setsockopt(fd, IPPROTO_TCP, TCP_ULP, \"tls\", sizeof(\"tls\")))\n+\t\tdie(\"setsockopt(TCP_ULP, tls)\");\n+\n+\tci.info.version = TLS_1_2_VERSION;\n+\tci.info.cipher_type = TLS_CIPHER_AES_GCM_128;\n+\tmemset(ci.iv, 'i', sizeof(ci.iv));\n+\tmemset(ci.key, 'k', sizeof(ci.key));\n+\tmemset(ci.salt, 's', sizeof(ci.salt));\n+\tmemset(ci.rec_seq, 0, sizeof(ci.rec_seq));\n+\n+\tif (setsockopt(fd, SOL_TLS, TLS_TX, \u0026ci, sizeof(ci)))\n+\t\tdie(\"setsockopt(TLS_TX)\");\n+\tif (setsockopt(fd, SOL_TLS, TLS_RX, \u0026ci, sizeof(ci)))\n+\t\tdie(\"setsockopt(TLS_RX)\");\n+\n+\t/* The whole point of the exercise: refuse to silently fall back to\n+\t * the software path, otherwise the test would pass without ever\n+\t * touching tls_device.c.\n+\t */\n+\tif (read_tls_stat(\"TlsTxDevice\") != tx_before + 1)\n+\t\tfail(\"TX did not land on the device path (TlsTxDevice %lu -\u003e %lu)\",\n+\t\t     tx_before, read_tls_stat(\"TlsTxDevice\"));\n+\tif (read_tls_stat(\"TlsRxDevice\") != rx_before + 1)\n+\t\tfail(\"RX did not land on the device path (TlsRxDevice %lu -\u003e %lu)\",\n+\t\t     rx_before, read_tls_stat(\"TlsRxDevice\"));\n+}\n+\n+static void sync_path(char *out, size_t len, const char *dir, const char *who)\n+{\n+\tif ((size_t)snprintf(out, len, \"%s/%s.ready\", dir, who) \u003e= len)\n+\t\tfail(\"sync dir path too long\");\n+}\n+\n+static void rendezvous(const char *dir, const char *me, const char *peer)\n+{\n+\tchar mine[PATH_MAX], theirs[PATH_MAX];\n+\tunsigned int waited = 0;\n+\tint fd;\n+\n+\tsync_path(mine, sizeof(mine), dir, me);\n+\tsync_path(theirs, sizeof(theirs), dir, peer);\n+\n+\tfd = open(mine, O_CREAT | O_WRONLY, 0600);\n+\tif (fd \u003c 0)\n+\t\tdie(\"create sync file\");\n+\tclose(fd);\n+\n+\twhile (access(theirs, F_OK)) {\n+\t\tif (waited \u003e= SYNC_TIMEOUT_MS)\n+\t\t\tfail(\"timed out waiting for %s\", peer);\n+\t\tmsleep(20);\n+\t\twaited += 20;\n+\t}\n+}\n+\n+/* Both ends stop here with their offload installed and no data sent yet,\n+ * so that the driver state can be inspected from the outside.\n+ */\n+static void wait_for_go(const char *dir)\n+{\n+\tunsigned int waited = 0;\n+\tchar go[PATH_MAX];\n+\n+\tif ((size_t)snprintf(go, sizeof(go), \"%s/go\", dir) \u003e= sizeof(go))\n+\t\tfail(\"sync dir path too long\");\n+\n+\twhile (access(go, F_OK)) {\n+\t\tif (waited \u003e= SYNC_TIMEOUT_MS)\n+\t\t\tfail(\"timed out waiting for go\");\n+\t\tmsleep(20);\n+\t\twaited += 20;\n+\t}\n+}\n+\n+/* Small writes with MSG_MORE accumulate into one open record before it is\n+ * pushed, which is the interesting part of tls_push_data().\n+ */\n+static void send_msg_more(int fd, unsigned int seed)\n+{\n+\tchar buf[MORE_FRAGS + 1];\n+\tint i;\n+\n+\tfill_pattern(buf, sizeof(buf), seed);\n+\n+\tfor (i = 0; i \u003c MORE_FRAGS; i++) {\n+\t\tif (send(fd, buf + i, 1, MSG_MORE) != 1)\n+\t\t\tdie(\"send(MSG_MORE)\");\n+\t}\n+\tif (send(fd, buf + MORE_FRAGS, 1, 0) != 1)\n+\t\tdie(\"send(last)\");\n+}\n+\n+/* splice() reaches tls_push_data() with MSG_SPLICE_PAGES once\n+ * TLS_TX_ZEROCOPY_RO is enabled, which is a distinct fragment path.\n+ */\n+static void send_splice(int fd, unsigned int seed)\n+{\n+\tchar buf[SPLICE_FRAG_LEN];\n+\tint val = 1;\n+\tint i;\n+\n+\tif (setsockopt(fd, SOL_TLS, TLS_TX_ZEROCOPY_RO, \u0026val, sizeof(val)))\n+\t\tdie(\"setsockopt(TLS_TX_ZEROCOPY_RO)\");\n+\n+\tfor (i = 0; i \u003c SPLICE_FRAGS; i++) {\n+\t\tint p[2];\n+\n+\t\tfill_pattern(buf, sizeof(buf), seed + i * SPLICE_FRAG_LEN);\n+\n+\t\tif (pipe(p))\n+\t\t\tdie(\"pipe\");\n+\t\tif (write(p[1], buf, sizeof(buf)) != sizeof(buf))\n+\t\t\tdie(\"write to pipe\");\n+\t\tif (splice(p[0], NULL, fd, NULL, sizeof(buf),\n+\t\t\t   i == SPLICE_FRAGS - 1 ? 0 : SPLICE_F_MORE) !=\n+\t\t    sizeof(buf))\n+\t\t\tdie(\"splice\");\n+\t\tclose(p[0]);\n+\t\tclose(p[1]);\n+\t}\n+\n+\tval = 0;\n+\tif (setsockopt(fd, SOL_TLS, TLS_TX_ZEROCOPY_RO, \u0026val, sizeof(val)))\n+\t\tdie(\"setsockopt(TLS_TX_ZEROCOPY_RO off)\");\n+}\n+\n+/* A record can be up to 16K, so normally one segment carries a piece of a\n+ * single record.  Shrinking the limit puts a dozen or so whole records in\n+ * every segment instead, which is the multi-record path through the driver.\n+ */\n+static void send_small_records(int fd, unsigned int seed)\n+{\n+\tchar buf[SMALL_LEN];\n+\tuint16_t limit;\n+\n+\tlimit = REC_LIM_MIN;\n+\tif (setsockopt(fd, SOL_TLS, TLS_TX_MAX_PAYLOAD_LEN, \u0026limit,\n+\t\t       sizeof(limit)))\n+\t\tdie(\"setsockopt(TLS_TX_MAX_PAYLOAD_LEN)\");\n+\n+\tfill_pattern(buf, sizeof(buf), seed);\n+\twrite_all(fd, buf, sizeof(buf));\n+\n+\tlimit = REC_LIM_MAX;\n+\tif (setsockopt(fd, SOL_TLS, TLS_TX_MAX_PAYLOAD_LEN, \u0026limit,\n+\t\t       sizeof(limit)))\n+\t\tdie(\"setsockopt(TLS_TX_MAX_PAYLOAD_LEN restore)\");\n+}\n+\n+#define SEED_C2S_BULK\t0x11\n+#define SEED_S2C_BULK\t0x22\n+#define SEED_C2S_MORE\t0x33\n+#define SEED_C2S_SPLICE\t0x44\n+#define SEED_C2S_SMALL\t0x55\n+\n+static void run_client(int fd)\n+{\n+\tchar *buf = malloc(BULK_LEN);\n+\n+\tif (!buf)\n+\t\tdie(\"malloc\");\n+\n+\tfill_pattern(buf, BULK_LEN, SEED_C2S_BULK);\n+\twrite_all(fd, buf, BULK_LEN);\n+\n+\tread_all(fd, buf, BULK_LEN);\n+\tcheck_pattern(buf, BULK_LEN, SEED_S2C_BULK, \"server -\u003e client bulk\");\n+\n+\tsend_msg_more(fd, SEED_C2S_MORE);\n+\tsend_splice(fd, SEED_C2S_SPLICE);\n+\tsend_small_records(fd, SEED_C2S_SMALL);\n+\n+\t/* Wait for the server's verdict before tearing anything down. */\n+\tread_all(fd, buf, 1);\n+\tif (buf[0] != 'k')\n+\t\tfail(\"server reported a failure\");\n+\n+\tfree(buf);\n+}\n+\n+static void run_server(int fd)\n+{\n+\tsize_t splice_len = (size_t)SPLICE_FRAG_LEN * SPLICE_FRAGS;\n+\tchar *buf = malloc(BULK_LEN);\n+\tchar more[MORE_FRAGS + 1];\n+\tchar *sbuf;\n+\tchar ok = 'k';\n+\tint i;\n+\n+\tsbuf = malloc(splice_len);\n+\tif (!buf || !sbuf)\n+\t\tdie(\"malloc\");\n+\n+\tread_all(fd, buf, BULK_LEN);\n+\tcheck_pattern(buf, BULK_LEN, SEED_C2S_BULK, \"client -\u003e server bulk\");\n+\n+\tfill_pattern(buf, BULK_LEN, SEED_S2C_BULK);\n+\twrite_all(fd, buf, BULK_LEN);\n+\n+\tread_all(fd, more, sizeof(more));\n+\tcheck_pattern(more, sizeof(more), SEED_C2S_MORE, \"client -\u003e server MSG_MORE\");\n+\n+\tread_all(fd, sbuf, splice_len);\n+\tfor (i = 0; i \u003c SPLICE_FRAGS; i++)\n+\t\tcheck_pattern(sbuf + (size_t)i * SPLICE_FRAG_LEN,\n+\t\t\t      SPLICE_FRAG_LEN, SEED_C2S_SPLICE +\n+\t\t\t      i * SPLICE_FRAG_LEN, \"client -\u003e server splice\");\n+\n+\tread_all(fd, buf, SMALL_LEN);\n+\tcheck_pattern(buf, SMALL_LEN, SEED_C2S_SMALL,\n+\t\t      \"client -\u003e server small records\");\n+\n+\twrite_all(fd, \u0026ok, 1);\n+\n+\tfree(sbuf);\n+\tfree(buf);\n+}\n+\n+static int do_server(const char *ip, int port, const char *syncdir)\n+{\n+\tstruct sockaddr_in sa = {};\n+\tint lfd, fd, one = 1;\n+\n+\tlfd = socket(AF_INET, SOCK_STREAM, 0);\n+\tif (lfd \u003c 0)\n+\t\tdie(\"socket\");\n+\tif (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, \u0026one, sizeof(one)))\n+\t\tdie(\"SO_REUSEADDR\");\n+\n+\tsa.sin_family = AF_INET;\n+\tsa.sin_port = htons(port);\n+\tif (inet_pton(AF_INET, ip, \u0026sa.sin_addr) != 1)\n+\t\tfail(\"bad bind address %s\", ip);\n+\n+\tif (bind(lfd, (struct sockaddr *)\u0026sa, sizeof(sa)))\n+\t\tdie(\"bind\");\n+\tif (listen(lfd, 1))\n+\t\tdie(\"listen\");\n+\n+\tfd = accept(lfd, NULL, NULL);\n+\tif (fd \u003c 0)\n+\t\tdie(\"accept\");\n+\tclose(lfd);\n+\n+\tenable_ktls(fd);\n+\trendezvous(syncdir, \"server\", \"client\");\n+\twait_for_go(syncdir);\n+\n+\trun_server(fd);\n+\n+\tclose(fd);\n+\treturn 0;\n+}\n+\n+static int do_client(const char *ip, int port, const char *syncdir)\n+{\n+\tstruct sockaddr_in sa = {};\n+\tunsigned int waited = 0;\n+\tint fd;\n+\n+\tsa.sin_family = AF_INET;\n+\tsa.sin_port = htons(port);\n+\tif (inet_pton(AF_INET, ip, \u0026sa.sin_addr) != 1)\n+\t\tfail(\"bad server address %s\", ip);\n+\n+\tfor (;;) {\n+\t\tfd = socket(AF_INET, SOCK_STREAM, 0);\n+\t\tif (fd \u003c 0)\n+\t\t\tdie(\"socket\");\n+\t\tif (!connect(fd, (struct sockaddr *)\u0026sa, sizeof(sa)))\n+\t\t\tbreak;\n+\t\tclose(fd);\n+\t\tif (waited \u003e= CONNECT_TIMEOUT_MS)\n+\t\t\tdie(\"connect\");\n+\t\tmsleep(20);\n+\t\twaited += 20;\n+\t}\n+\n+\tenable_ktls(fd);\n+\trendezvous(syncdir, \"client\", \"server\");\n+\twait_for_go(syncdir);\n+\n+\trun_client(fd);\n+\n+\tclose(fd);\n+\treturn 0;\n+}\n+\n+int main(int argc, char **argv)\n+{\n+\tint port;\n+\n+\tif (argc != 5) {\n+\t\tfprintf(stderr,\n+\t\t\t\"usage: %s server|client \u003cip\u003e \u003cport\u003e \u003csyncdir\u003e\\n\",\n+\t\t\targv[0]);\n+\t\treturn 2;\n+\t}\n+\n+\trole = argv[1];\n+\tport = atoi(argv[3]);\n+\n+\tif (!strcmp(role, \"server\"))\n+\t\treturn do_server(argv[2], port, argv[4]);\n+\tif (!strcmp(role, \"client\"))\n+\t\treturn do_client(argv[2], port, argv[4]);\n+\n+\tfprintf(stderr, \"unknown role %s\\n\", role);\n+\treturn 2;\n+}\n"
}

2/1 2026/07/29 15:06 agent patch-evaluator
0m Model:
best-expensive

Args:
null

Results:
{
  "EnableConfigs": [
    "NETDEVSIM",
    "TLS",
    "TLS_DEVICE"
  ],
  "FocusSymbols": [
    "nsim_do_tls",
    "nsim_tls_init",
    "nsim_tls_teardown"
  ],
  "Reasoning": "The patch introduces emulated kTLS offload to the netdevsim driver. This is a functional change that adds new reachable code paths for testing TLS hardware offload in a virtualized environment. The code is reachable via standard socket options and network operations when netdevsim is used.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer.
Your job is to review a provided patch series and determine
if it makes functional changes to the kernel that should be fuzzed.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively
use your code access tools to examine the actual source code and confirm any assumptions.

Return WorthFuzzing=false if the patch only contains:
- Modifications to Documentation/, Kconfig files, or code comments.
- Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints.
- Changes to numeric constants or macros that do not functionally alter execution flow.
- Code paths that are impossible to reach in virtualized environments like GCE or QEMU,
  even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim).
- Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed,
  ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU.
- Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove)
  that are executed only during PCI hot-unplug or sysfs driver unbind operations.

If it modifies reachable core kernel logic, drivers, or architectures, use your code search
tools to verify the code can be executed, then return WorthFuzzing=true.

When returning WorthFuzzing=true, you MUST ALSO:
1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols.
   Avoid listing generic hot-path functions to prevent skewed test distributions.
   Prefer non-static, non-inlined API entrypoint functions over internal static helper functions
   (which are inlined by the compiler and do not have distinct symbol addresses).
2. Identify any specific CONFIG_ options required to properly test this new/modified feature.
   Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions.
   Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs
   needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs
   output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
For your convenience, here is the diff of the changes:
commit ef6d037b829ae6d74119190a2186b84e54e5a862
Author: syz-cluster <triage@syzkaller.com>
Date:   Wed Jul 29 15:06:18 2026 +0000

    syz-cluster: applied patch under review

diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index ff79c466712df..cb4d4bb87afc2 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -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
diff --git a/drivers/net/netdevsim/Makefile b/drivers/net/netdevsim/Makefile
index 87718204fb4de..c7f7621323b2b 100644
--- a/drivers/net/netdevsim/Makefile
+++ b/drivers/net/netdevsim/Makefile
@@ -25,3 +25,7 @@ endif
 ifneq ($(CONFIG_MACSEC),)
 netdevsim-objs += macsec.o
 endif
+
+ifneq ($(CONFIG_TLS_DEVICE),)
+netdevsim-objs += tls.o
+endif
diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c
index 4e9d7e10b527e..8dc0b0a7d1a13 100644
--- a/drivers/net/netdevsim/netdev.c
+++ b/drivers/net/netdevsim/netdev.c
@@ -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;
@@ -1066,6 +1068,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)
@@ -1093,6 +1096,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);
@@ -1195,6 +1199,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);
diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h
index 64f77f93d937c..582a76ace9d35 100644
--- a/drivers/net/netdevsim/netdevsim.h
+++ b/drivers/net/netdevsim/netdevsim.h
@@ -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;
@@ -435,6 +450,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);
diff --git a/drivers/net/netdevsim/tls.c b/drivers/net/netdevsim/tls.c
new file mode 100644
index 0000000000000..f695f2e2beb80
--- /dev/null
+++ b/drivers/net/netdevsim/tls.c
@@ -0,0 +1,418 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/ipv6.h>
+#include <linux/list.h>
+#include <linux/skbuff.h>
+#include <linux/slab.h>
+#include <linux/tcp.h>
+#include <net/ip.h>
+#include <net/ipv6.h>
+#include <net/tls.h>
+
+#include "netdevsim.h"
+
+/* Emulated kTLS offload.  No crypto is performed, so this is only self
+ * consistent between two netdevsim ports.
+ */
+
+#define NSIM_TLS_MAX_CONN	32
+
+/* netdev_fix_features() drops NETIF_F_HW_TLS_RX unless the device also does
+ * RX checksums, which every offload capable NIC does.
+ */
+#define NSIM_TLS_FEATURES	(NETIF_F_HW_TLS_TX | NETIF_F_HW_TLS_RX | \
+				 NETIF_F_RXCSUM)
+
+struct nsim_tls_conn {
+	struct list_head list;
+	struct rcu_head rcu;
+
+	/* Identity as seen on the wire, from the point of view of the port
+	 * the offload was installed on: l* is this side, r* is the peer.
+	 */
+	struct in6_addr laddr;
+	struct in6_addr raddr;
+	__be16 lport;
+	__be16 rport;
+	u16 family;
+
+	enum tls_offload_ctx_dir dir;
+	u32 start_sn;
+	u16 cipher_type;
+	const struct tls_context *tls_ctx;
+};
+
+struct nsim_tls_tuple {
+	struct in6_addr saddr;
+	struct in6_addr daddr;
+	__be16 sport;
+	__be16 dport;
+};
+
+static bool nsim_tls_parse(const struct sk_buff *skb,
+			   struct nsim_tls_tuple *t)
+{
+	const struct tcphdr *th;
+
+	switch (skb->protocol) {
+	case htons(ETH_P_IP): {
+		const struct iphdr *iph = ip_hdr(skb);
+
+		if (iph->protocol != IPPROTO_TCP)
+			return false;
+		ipv6_addr_set_v4mapped(iph->saddr, &t->saddr);
+		ipv6_addr_set_v4mapped(iph->daddr, &t->daddr);
+		break;
+	}
+	case htons(ETH_P_IPV6): {
+		const struct ipv6hdr *ip6h = ipv6_hdr(skb);
+
+		if (ip6h->nexthdr != IPPROTO_TCP)
+			return false;
+		t->saddr = ip6h->saddr;
+		t->daddr = ip6h->daddr;
+		break;
+	}
+	default:
+		return false;
+	}
+
+	th = tcp_hdr(skb);
+	t->sport = th->source;
+	t->dport = th->dest;
+
+	return true;
+}
+
+/* On RX there is no socket to look at yet, so match the tuple installed at
+ * ->tls_dev_add() time, which is what the hardware does.  The packet is
+ * arriving, so the peer of the connection is its source.
+ */
+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))
+			return true;
+
+	return false;
+}
+
+/* The stack leaves a tag sized placeholder at the end of every record for
+ * the device to write the authentication tag into.  We do not encrypt, so
+ * nothing ever fills it in, and it comes from a page frag that was never
+ * zeroed - clear it rather than leak uninitialized memory onto the wire.
+ *
+ * tls_append_frag() either grows the last frag or adds one, so the tag is
+ * always the tail of the record's last frag.  Clearing it there rather than
+ * in the skb covers every segment the record was split into, and any
+ * retransmit of them, since they all share these pages.
+ */
+static void nsim_tls_tx_zero_tags(struct sk_buff *skb)
+{
+	struct tls_context *ctx = tls_get_ctx(skb->sk);
+	const struct tcphdr *th = tcp_hdr(skb);
+	struct tls_offload_context_tx *tx_ctx;
+	u32 seq, end, tag_size;
+	unsigned long flags;
+	unsigned int off;
+
+	off = skb_transport_offset(skb) + __tcp_hdrlen(th);
+	if (off >= skb->len)
+		return;
+
+	tag_size = ctx->prot_info.tag_size;
+	seq = ntohl(th->seq);
+	end = seq + skb->len - off;
+	tx_ctx = tls_offload_ctx_tx(ctx);
+
+	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;
+
+		frag = &record->frags[record->num_frags - 1];
+		memset(skb_frag_address(frag) + skb_frag_size(frag) - tag_size,
+		       0, tag_size);
+
+		seq = record->end_seq;
+	}
+	spin_unlock_irqrestore(&tx_ctx->lock, flags);
+}
+
+/* Stand in for the inline encryption the hardware would do on the way out.
+ * The stack has already framed the record and we do not encrypt, so the tag
+ * is all that is left to deal with.  The socket is right here, so identify
+ * the connection the way the real drivers do.
+ */
+static void nsim_tls_tx(struct netdevsim *ns, struct sk_buff *skb)
+{
+	if (!tls_is_skb_tx_device_offloaded(skb))
+		return;
+
+	nsim_tls_tx_zero_tags(skb);
+	atomic64_inc(&ns->tls.tx_packets);
+	atomic64_add(skb->len, &ns->tls.tx_bytes);
+}
+
+/* Stand in for the inline decryption the peer's hardware would do, since we
+ * are about to hand the skb to its stack.  We do not decrypt either, so all
+ * that is needed is the flag that tells the kTLS core the payload is
+ * already plaintext.
+ */
+static void nsim_tls_rx(struct netdevsim *ns, struct sk_buff *skb)
+{
+	struct nsim_tls_tuple t;
+
+	if (list_empty(&ns->tls.conns))
+		return;
+
+	if (!nsim_tls_parse(skb, &t))
+		return;
+
+	if (!nsim_tls_rx_offloaded(ns, &t))
+		return;
+
+	skb->decrypted = 1;
+	atomic64_inc(&ns->tls.rx_packets);
+	atomic64_add(skb->len, &ns->tls.rx_bytes);
+}
+
+/* netdevsim has no wire: nsim_start_xmit() hands the skb straight to the
+ * peer's receive path, so this one call site stands in for the hardware of
+ * both ports.  It has to run before nsim_forward_skb() moves the skb over,
+ * which resets the headers.
+ */
+void nsim_do_tls(struct sk_buff *skb, struct netdevsim *ns,
+		 struct netdevsim *peer_ns)
+{
+	/* nsim_do_psp() ran first and may have wrapped the record stream, so
+	 * this is no longer a plain TLS over TCP packet.  No NIC chains the
+	 * two inline offloads either, so leave it alone.
+	 */
+	if (skb->encapsulation)
+		return;
+
+	nsim_tls_tx(ns, skb);
+	nsim_tls_rx(peer_ns, skb);
+}
+
+static int nsim_tls_dev_add(struct net_device *netdev, struct sock *sk,
+			    enum tls_offload_ctx_dir direction,
+			    struct tls_crypto_info *crypto_info,
+			    u32 start_offload_tcp_sn)
+{
+	struct netdevsim *ns = netdev_priv(netdev);
+	struct nsim_tls_conn *conn;
+	int ret = 0;
+
+	/* Mirror the cipher support of a typical offload capable NIC. */
+	switch (crypto_info->cipher_type) {
+	case TLS_CIPHER_AES_GCM_128:
+	case TLS_CIPHER_AES_GCM_256:
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	conn = kzalloc_obj(*conn);
+	if (!conn)
+		return -ENOMEM;
+
+	if (sk->sk_family == AF_INET6) {
+		conn->laddr = sk->sk_v6_rcv_saddr;
+		conn->raddr = sk->sk_v6_daddr;
+	} else {
+		ipv6_addr_set_v4mapped(sk->sk_rcv_saddr, &conn->laddr);
+		ipv6_addr_set_v4mapped(sk->sk_daddr, &conn->raddr);
+	}
+	conn->family = sk->sk_family;
+	conn->lport = htons(inet_sk(sk)->inet_num);
+	conn->rport = sk->sk_dport;
+	conn->dir = direction;
+	conn->start_sn = start_offload_tcp_sn;
+	conn->cipher_type = crypto_info->cipher_type;
+	conn->tls_ctx = tls_get_ctx(sk);
+
+	spin_lock_bh(&ns->tls.lock);
+	if (ns->tls.count >= NSIM_TLS_MAX_CONN) {
+		ret = -ENOSPC;
+		goto out_unlock;
+	}
+	list_add_tail_rcu(&conn->list, &ns->tls.conns);
+	ns->tls.count++;
+	if (direction == TLS_OFFLOAD_CTX_DIR_TX)
+		ns->tls.tx_conn++;
+	else
+		ns->tls.rx_conn++;
+out_unlock:
+	spin_unlock_bh(&ns->tls.lock);
+
+	if (ret)
+		kfree(conn);
+
+	return ret;
+}
+
+static void nsim_tls_dev_del(struct net_device *netdev,
+			     struct tls_context *tls_ctx,
+			     enum tls_offload_ctx_dir direction)
+{
+	struct netdevsim *ns = netdev_priv(netdev);
+	struct nsim_tls_conn *conn;
+
+	spin_lock_bh(&ns->tls.lock);
+	list_for_each_entry(conn, &ns->tls.conns, list) {
+		if (conn->tls_ctx != tls_ctx || conn->dir != direction)
+			continue;
+
+		list_del_rcu(&conn->list);
+		ns->tls.count--;
+		if (direction == TLS_OFFLOAD_CTX_DIR_TX)
+			ns->tls.tx_conn--;
+		else
+			ns->tls.rx_conn--;
+		spin_unlock_bh(&ns->tls.lock);
+
+		kfree_rcu(conn, rcu);
+		return;
+	}
+	spin_unlock_bh(&ns->tls.lock);
+
+	netdev_err(netdev, "TLS %s context not found on del\n",
+		   direction == TLS_OFFLOAD_CTX_DIR_TX ? "tx" : "rx");
+}
+
+/* Nothing is ever out of sync here: the emulation does not decrypt, so it
+ * never loses the record boundaries and never asks for a resync.  The core
+ * can still call in on the RX path, so account for it and move on.
+ */
+static int nsim_tls_dev_resync(struct net_device *netdev, struct sock *sk,
+			       u32 seq, u8 *rcd_sn,
+			       enum tls_offload_ctx_dir direction)
+{
+	struct netdevsim *ns = netdev_priv(netdev);
+
+	atomic64_inc(&ns->tls.resyncs);
+
+	return 0;
+}
+
+static const struct tlsdev_ops nsim_tlsdev_ops = {
+	.tls_dev_add	= nsim_tls_dev_add,
+	.tls_dev_del	= nsim_tls_dev_del,
+	.tls_dev_resync	= nsim_tls_dev_resync,
+};
+
+static ssize_t nsim_tls_dbg_read(struct file *filp, char __user *buffer,
+				 size_t count, loff_t *ppos)
+{
+	struct netdevsim *ns = filp->private_data;
+	struct nsim_tls_conn *conn;
+	size_t bufsize;
+	char *buf, *p;
+	int len;
+
+	/* Two full IPv6 addresses and ports fit in 160 bytes a line. */
+	bufsize = (NSIM_TLS_MAX_CONN * 160) + 200;
+	buf = kzalloc(bufsize, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	p = buf;
+
+	spin_lock_bh(&ns->tls.lock);
+	p += scnprintf(p, bufsize - (p - buf),
+		       "conn count=%u tx=%u rx=%u\n",
+		       ns->tls.count, ns->tls.tx_conn, ns->tls.rx_conn);
+	p += scnprintf(p, bufsize - (p - buf),
+		       "tx_packets=%llu tx_bytes=%llu rx_packets=%llu rx_bytes=%llu resyncs=%llu\n",
+		       atomic64_read(&ns->tls.tx_packets),
+		       atomic64_read(&ns->tls.tx_bytes),
+		       atomic64_read(&ns->tls.rx_packets),
+		       atomic64_read(&ns->tls.rx_bytes),
+		       atomic64_read(&ns->tls.resyncs));
+
+	list_for_each_entry(conn, &ns->tls.conns, list) {
+		if (conn->family == AF_INET6)
+			p += scnprintf(p, bufsize - (p - buf),
+				       "%s [%pI6c]:%u -> [%pI6c]:%u cipher=%u sn=%u\n",
+				       conn->dir == TLS_OFFLOAD_CTX_DIR_TX ?
+				       "tx" : "rx",
+				       &conn->laddr, ntohs(conn->lport),
+				       &conn->raddr, ntohs(conn->rport),
+				       conn->cipher_type, conn->start_sn);
+		else
+			p += scnprintf(p, bufsize - (p - buf),
+				       "%s %pI4:%u -> %pI4:%u cipher=%u sn=%u\n",
+				       conn->dir == TLS_OFFLOAD_CTX_DIR_TX ?
+				       "tx" : "rx",
+				       &conn->laddr.s6_addr32[3],
+				       ntohs(conn->lport),
+				       &conn->raddr.s6_addr32[3],
+				       ntohs(conn->rport),
+				       conn->cipher_type, conn->start_sn);
+	}
+	spin_unlock_bh(&ns->tls.lock);
+
+	len = simple_read_from_buffer(buffer, count, ppos, buf, p - buf);
+
+	kfree(buf);
+
+	return len;
+}
+
+static const struct file_operations nsim_tls_dbg_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.read = nsim_tls_dbg_read,
+	.llseek = default_llseek,
+};
+
+void nsim_tls_init(struct netdevsim *ns)
+{
+	INIT_LIST_HEAD(&ns->tls.conns);
+	spin_lock_init(&ns->tls.lock);
+
+	ns->netdev->tlsdev_ops = &nsim_tlsdev_ops;
+	ns->netdev->features |= NSIM_TLS_FEATURES;
+	ns->netdev->hw_features |= NSIM_TLS_FEATURES;
+
+	ns->tls.dfile = debugfs_create_file("tls", 0400,
+					    ns->nsim_dev_port->ddir, ns,
+					    &nsim_tls_dbg_fops);
+}
+
+void nsim_tls_teardown(struct netdevsim *ns)
+{
+	struct nsim_tls_conn *conn, *tmp;
+	u32 left;
+
+	debugfs_remove_recursive(ns->tls.dfile);
+
+	spin_lock_bh(&ns->tls.lock);
+	left = ns->tls.count;
+	list_for_each_entry_safe(conn, tmp, &ns->tls.conns, list) {
+		list_del_rcu(&conn->list);
+		kfree_rcu(conn, rcu);
+	}
+	ns->tls.count = 0;
+	ns->tls.tx_conn = 0;
+	ns->tls.rx_conn = 0;
+	spin_unlock_bh(&ns->tls.lock);
+
+	if (left)
+		netdev_err(ns->netdev,
+			   "tearing down TLS offload with %u connections left\n",
+			   left);
+}
diff --git a/tools/testing/selftests/drivers/net/netdevsim/.gitignore b/tools/testing/selftests/drivers/net/netdevsim/.gitignore
new file mode 100644
index 0000000000000..9d4f57cb2baa9
--- /dev/null
+++ b/tools/testing/selftests/drivers/net/netdevsim/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+tls_offload
diff --git a/tools/testing/selftests/drivers/net/netdevsim/Makefile b/tools/testing/selftests/drivers/net/netdevsim/Makefile
index 9808c2fbae9e8..5beb4ed64eb52 100644
--- a/tools/testing/selftests/drivers/net/netdevsim/Makefile
+++ b/tools/testing/selftests/drivers/net/netdevsim/Makefile
@@ -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
diff --git a/tools/testing/selftests/drivers/net/netdevsim/tls.sh b/tools/testing/selftests/drivers/net/netdevsim/tls.sh
new file mode 100755
index 0000000000000..399e2c4d382df
--- /dev/null
+++ b/tools/testing/selftests/drivers/net/netdevsim/tls.sh
@@ -0,0 +1,271 @@
+#!/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
+
+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
+
+NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device
+NSIM_DEV_SYS_DEL=/sys/bus/netdevsim/del_device
+NSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_device
+
+DEBUGFS=/sys/kernel/debug/netdevsim
+NSIM_DEV_1_TLS=$DEBUGFS/netdevsim$NSIM_DEV_1_ID/ports/0/tls
+NSIM_DEV_2_TLS=$DEBUGFS/netdevsim$NSIM_DEV_2_ID/ports/0/tls
+
+SRV_IP=192.168.13.1
+CLI_IP=192.168.13.2
+PORT=4433
+
+SYNCDIR=
+BIN=$lib_dir/tls_offload
+
+num_pass=0
+num_fail=0
+
+check()
+{
+	local msg="$1"
+	local ret="$2"
+
+	if [ "$ret" -eq 0 ]; then
+		echo "PASS: $msg"
+		num_pass=$((num_pass + 1))
+	else
+		echo "FAIL: $msg"
+		num_fail=$((num_fail + 1))
+	fi
+}
+
+# shellcheck disable=SC2317,SC2329 # invoked from the EXIT trap
+cleanup()
+{
+	ip netns del nscl 2>/dev/null
+	ip netns del nssv 2>/dev/null
+	echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_DEL" 2>/dev/null
+	echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_DEL" 2>/dev/null
+	[ -n "$SYNCDIR" ] && rm -rf "$SYNCDIR"
+}
+
+setup()
+{
+	set -e
+
+	echo "$NSIM_DEV_1_ID" > "$NSIM_DEV_SYS_NEW"
+	echo "$NSIM_DEV_2_ID" > "$NSIM_DEV_SYS_NEW"
+	udevadm settle 2>/dev/null || sleep 1
+
+	NSIM_DEV_1_NAME=$(find "$NSIM_DEV_1_SYS"/net -maxdepth 1 -type d ! \
+		-path "$NSIM_DEV_1_SYS"/net -exec basename {} \;)
+	NSIM_DEV_2_NAME=$(find "$NSIM_DEV_2_SYS"/net -maxdepth 1 -type d ! \
+		-path "$NSIM_DEV_2_SYS"/net -exec basename {} \;)
+
+	ip netns add nssv
+	ip netns add nscl
+
+	ip link set "$NSIM_DEV_1_NAME" netns nssv
+	ip link set "$NSIM_DEV_2_NAME" netns nscl
+
+	ip netns exec nssv ip addr add "$SRV_IP/24" dev "$NSIM_DEV_1_NAME"
+	ip netns exec nscl ip addr add "$CLI_IP/24" dev "$NSIM_DEV_2_NAME"
+
+	ip netns exec nssv ip link set dev "$NSIM_DEV_1_NAME" up
+	ip netns exec nscl ip link set dev "$NSIM_DEV_2_NAME" up
+
+	NSIM_DEV_1_FD=$((256 + RANDOM % 256))
+	exec {NSIM_DEV_1_FD}</var/run/netns/nssv
+	NSIM_DEV_1_IFIDX=$(ip netns exec nssv \
+		cat /sys/class/net/"$NSIM_DEV_1_NAME"/ifindex)
+
+	NSIM_DEV_2_FD=$((256 + RANDOM % 256))
+	exec {NSIM_DEV_2_FD}</var/run/netns/nscl
+	NSIM_DEV_2_IFIDX=$(ip netns exec nscl \
+		cat /sys/class/net/"$NSIM_DEV_2_NAME"/ifindex)
+
+	echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX" \
+		> "$NSIM_DEV_SYS_LINK"
+
+	SYNCDIR=$(mktemp -d)
+	set +e
+}
+
+feature()
+{
+	local netns="$1"
+	local dev="$2"
+	local feat="$3"
+
+	ip netns exec "$netns" ethtool -k "$dev" 2>/dev/null | \
+		sed -n "s/^$feat: \([a-z]*\).*/\1/p"
+}
+
+dbg_field()
+{
+	sed -n "s/.*\<$2=\([0-9]*\).*/\1/p" "$1" | head -1
+}
+
+tls_stat()
+{
+	ip netns exec "$1" cat /proc/net/tls_stat | \
+		sed -n "s/^$2 \([0-9]*\)/\1/p"
+}
+
+# Both ends park once their offload is installed and before any data is
+# sent, so the driver's context count can be sampled without racing the
+# transfer.  Pass "nosample" when the offload is expected to be refused,
+# since then neither end ever reaches the barrier.
+run_pair()
+{
+	local sample="${1:-sample}"
+	local srv_rc cli_rc waited=0
+
+	rm -f "$SYNCDIR"/*.ready "$SYNCDIR"/go
+	CONNS_1=0
+	CONNS_2=0
+
+	ip netns exec nssv "$BIN" server "$SRV_IP" "$PORT" "$SYNCDIR" &
+	local srv_pid=$!
+	ip netns exec nscl "$BIN" client "$SRV_IP" "$PORT" "$SYNCDIR" &
+	local cli_pid=$!
+
+	if [ "$sample" = "sample" ]; then
+		while [ ! -e "$SYNCDIR/server.ready" ] ||
+		      [ ! -e "$SYNCDIR/client.ready" ]; do
+			[ "$waited" -ge 200 ] && break
+			sleep 0.05
+			waited=$((waited + 1))
+		done
+		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
+	touch "$SYNCDIR/go"
+
+	wait "$srv_pid"; srv_rc=$?
+	wait "$cli_pid"; cli_rc=$?
+
+	[ "$srv_rc" -eq 0 ] && [ "$cli_rc" -eq 0 ]
+}
+
+###
+### Code start
+###
+
+if [ "$(id -u)" -ne 0 ]; then
+	echo "SKIP: need root"
+	exit "$ksft_skip"
+fi
+
+if ! command -v ethtool >/dev/null; then
+	echo "SKIP: ethtool not found"
+	exit "$ksft_skip"
+fi
+
+if [ ! -x "$BIN" ]; then
+	echo "SKIP: $BIN not built"
+	exit "$ksft_skip"
+fi
+
+modprobe netdevsim 2>/dev/null
+if [ ! -d /sys/bus/netdevsim ]; then
+	echo "SKIP: netdevsim not available"
+	exit "$ksft_skip"
+fi
+
+modprobe tls 2>/dev/null
+if [ ! -e /proc/net/tls_stat ]; then
+	echo "SKIP: kernel TLS not available"
+	exit "$ksft_skip"
+fi
+
+trap cleanup EXIT
+setup
+
+# 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
+
+[ -e "$NSIM_DEV_1_TLS" ]
+check "per-port debugfs tls file exists" $?
+
+# Main data path run.
+run_pair
+check "offloaded TLS data transfer" $?
+
+# Sampled at the barrier, so each port must be holding exactly the TX and
+# the RX context of its own socket.
+[ "$CONNS_1" -eq 2 ] && [ "$CONNS_2" -eq 2 ]
+check "tx and rx contexts installed on both ports" $?
+
+# Both ends must have gone through the device path, not the SW fallback.
+[ "$(tls_stat nssv TlsTxDevice)" -ge 1 ] && \
+	[ "$(tls_stat nssv TlsRxDevice)" -ge 1 ] && \
+	[ "$(tls_stat nscl TlsTxDevice)" -ge 1 ] && \
+	[ "$(tls_stat nscl TlsRxDevice)" -ge 1 ]
+check "both ends used the device path" $?
+
+[ "$(tls_stat nssv TlsTxSw)" -eq 0 ] && [ "$(tls_stat nscl TlsTxSw)" -eq 0 ]
+check "no silent fallback to the software path" $?
+
+[ "$(tls_stat nssv TlsDecryptError)" -eq 0 ] && \
+	[ "$(tls_stat nscl TlsDecryptError)" -eq 0 ]
+check "no decrypt errors" $?
+
+# The driver must have seen the records go by in both directions.
+[ "$(dbg_field "$NSIM_DEV_1_TLS" tx_packets)" -ge 1 ] && \
+	[ "$(dbg_field "$NSIM_DEV_1_TLS" rx_packets)" -ge 1 ] && \
+	[ "$(dbg_field "$NSIM_DEV_2_TLS" tx_packets)" -ge 1 ] && \
+	[ "$(dbg_field "$NSIM_DEV_2_TLS" rx_packets)" -ge 1 ]
+check "driver counted offloaded packets both ways" $?
+
+# Sockets are closed by now, so every context must have been given back.
+[ "$(dbg_field "$NSIM_DEV_1_TLS" count)" -eq 0 ] && \
+	[ "$(dbg_field "$NSIM_DEV_2_TLS" count)" -eq 0 ]
+check "all offload contexts released on close" $?
+
+[ "$(tls_stat nssv TlsCurrTxDevice)" -eq 0 ] && \
+	[ "$(tls_stat nssv TlsCurrRxDevice)" -eq 0 ]
+check "no device contexts left behind" $?
+
+# Turning the feature off has to make the offload refuse the connection;
+# the test binary insists on the device path, so it must now fail.
+ip netns exec nssv ethtool -K "$NSIM_DEV_1_NAME" tls-hw-tx-offload off
+ip netns exec nssv ethtool -K "$NSIM_DEV_1_NAME" tls-hw-rx-offload off
+ip netns exec nscl ethtool -K "$NSIM_DEV_2_NAME" tls-hw-tx-offload off
+ip netns exec nscl ethtool -K "$NSIM_DEV_2_NAME" tls-hw-rx-offload off
+
+run_pair nosample
+rc=$?
+[ "$rc" -ne 0 ]
+check "offload declined once the feature is off" $?
+
+[ "$(tls_stat nssv TlsTxSw)" -ge 1 ]
+check "software path used when offload is off" $?
+
+ip netns exec nssv ethtool -K "$NSIM_DEV_1_NAME" tls-hw-tx-offload on
+ip netns exec nssv ethtool -K "$NSIM_DEV_1_NAME" tls-hw-rx-offload on
+ip netns exec nscl ethtool -K "$NSIM_DEV_2_NAME" tls-hw-tx-offload on
+ip netns exec nscl ethtool -K "$NSIM_DEV_2_NAME" tls-hw-rx-offload on
+
+run_pair
+check "offload works again after re-enabling" $?
+
+echo
+echo "passed: $num_pass failed: $num_fail"
+[ "$num_fail" -eq 0 ] && exit 0
+exit 1
diff --git a/tools/testing/selftests/drivers/net/netdevsim/tls_offload.c b/tools/testing/selftests/drivers/net/netdevsim/tls_offload.c
new file mode 100644
index 0000000000000..ebfb4bfcd4f44
--- /dev/null
+++ b/tools/testing/selftests/drivers/net/netdevsim/tls_offload.c
@@ -0,0 +1,498 @@
+// 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
+
+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]);
+
+	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;
+}

3/2 2026/07/29 15:06 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 17764
							output: 18
							thoughts: 287
						

Thoughts:
**Analyzing the kTLS Offload Patch for netdevsim**

Okay, so I've got this patch here that's adding emulated kTLS offload functionality to `netdevsim`. I'm immediately zeroing in on the implications here. This is a pretty significant functional change, specifically targeting the `netdevsim` driver, which, as we know, is a workhorse for testing and fuzzing in the networking world. Getting this right is crucial for the stability of the kernel and the reliability of our testing infrastructure.

I see the patch touches a handful of files: `Kconfig`, the `Makefile`, `netdev.c`, `netdevsim.h`, `tls.c`, and a new selftest. That's a reasonable scope for this kind of feature addition. Given that `netdevsim` is a virtual network device, the accessibility is clearly there.

Now, I need to verify the configuration dependencies. Let's see... we'll need `CONFIG_NETDEVSIM`, `CONFIG_TLS`, and `CONFIG_TLS_DEVICE`. I'll immediately start checking the `Kconfig` files to confirm the required dependencies are correctly enforced and any potential conflicts are avoided. I'll need to check `drivers/net/Kconfig` and `net/tls/Kconfig`.

Okay, looking at the `drivers/net/Kconfig`, `NETDEVSIM` depends on `INET`, `PSAMPLE || PSAMPLE=n`, `PTP_1588_CLOCK_MOCK || PTP_1588_CLOCK_MOCK=n`, and importantly, `TLS || TLS=n`. This seems logical, but I'll make sure there are no unintended consequences within the dependencies. Now let's check `net/tls/Kconfig` for the `TLS_DEVICE` dependencies. I need to make sure we're not introducing any unexpected behavior. I'll drill down to ensure the dependencies are well-defined and don't introduce any unforeseen issues or dependencies on other features.



4/2 2026/07/29 15:06 tool grepper
0m Args:
{
  "Expression": "config TLS_DEVICE"
}

Results:
{
  "Output": "net/tls/Kconfig=5=config TLS\n--\nnet/tls/Kconfig-18-\nnet/tls/Kconfig:19:config TLS_DEVICE\nnet/tls/Kconfig-20-\tbool \"Transport Layer Security HW offload\"\n"
}

5/2 2026/07/29 15:06 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 18574
							output: 32
							thoughts: 18
						

Thoughts:
Here's my attempt at summarizing the thought process, tailored for an expert audience:

**Diving into TLS Configuration**

Okay, so I'm thinking I need to understand the TLS configuration options within the kernel, specifically how these settings are exposed. I remember that `net/tls/Kconfig` is usually the place to start for this kind of information, so let's navigate to that file. I'm focusing around line 19; that's generally where these things are introduced. I need to get a high-level picture before I start digging into the details. I expect that line 19 will be a good entry point to see what's defined, what the dependencies are, and what the general structure looks like for TLS feature configuration. From there, I can get a feel for how I can influence or integrate this into my work.



6/2 2026/07/29 15:06 tool read-file
0m Args:
{
  "File": "net/tls/Kconfig",
  "FirstLine": 1,
  "LineCount": 30
}

Results:
{
  "Contents": "   1:\t# SPDX-License-Identifier: GPL-2.0-only\n   2:\t#\n   3:\t# TLS configuration\n   4:\t#\n   5:\tconfig TLS\n   6:\t\ttristate \"Transport Layer Security support\"\n   7:\t\tdepends on INET\n   8:\t\tselect CRYPTO\n   9:\t\tselect CRYPTO_AES\n  10:\t\tselect CRYPTO_GCM\n  11:\t\tselect NET_SOCK_MSG\n  12:\t\tdefault n\n  13:\t\thelp\n  14:\t\tEnable kernel support for TLS protocol. This allows symmetric\n  15:\t\tencryption handling of the TLS protocol to be done in-kernel.\n  16:\t\n  17:\t\tIf unsure, say N.\n  18:\t\n  19:\tconfig TLS_DEVICE\n  20:\t\tbool \"Transport Layer Security HW offload\"\n  21:\t\tdepends on TLS\n  22:\t\tselect SKB_DECRYPTED\n  23:\t\tselect SOCK_VALIDATE_XMIT\n  24:\t\tselect SOCK_RX_QUEUE_MAPPING\n  25:\t\tdefault n\n  26:\t\thelp\n  27:\t\tEnable kernel support for HW offload of the TLS protocol.\n  28:\t\n  29:\t\tIf unsure, say N.\n"
}

7/2 2026/07/29 15:06 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 19128
							output: 57
							thoughts: 87
						

Thoughts:
Okay, here's my take on those dependencies and configurations, expressed as my thought process:

**Dependency Deduction and Configuration**

Right, let's break this down systematically. It looks like `TLS_DEVICE` is inherently linked to `TLS`, that's straightforward enough. Now, `TLS` itself is building on top of the `INET` stack, as I'd expect. The interesting bit is `NETDEVSIM`, which apparently depends on `TLS` *or* explicitly disabling `TLS` (`TLS=n`). But since the goal is to *enable* `NETDEVSIM`, the requirement becomes clear: we definitely need `TLS`.

Therefore, the core of the configuration boils down to ensuring that `NETDEVSIM`, `TLS`, and `TLS_DEVICE` are all enabled and properly set up to work together. Essentially, getting the whole stack in order.

Now, let me shift gears and take a look at the actual code – specifically, the functions that were added in `drivers/net/netdevsim/tls.c`. That's where the rubber meets the road, and the real implementation details will tell the full story. I want to see *how* these modules have integrated and how they're handling this specific aspect.



8/2 2026/07/29 15:06 tool grepper
0m Args:
{
  "Expression": "^([a-z_0-9]+ )+nsim_tls_[a-z_0-9]+\\(",
  "PathPrefix": "drivers/net/netdevsim/tls.c"
}

Results:
{
  "Output": "drivers/net/netdevsim/tls.c=45=struct nsim_tls_tuple {\n--\ndrivers/net/netdevsim/tls.c-51-\ndrivers/net/netdevsim/tls.c:52:static bool nsim_tls_parse(const struct sk_buff *skb,\ndrivers/net/netdevsim/tls.c-53-\t\t\t   struct nsim_tls_tuple *t)\n--\ndrivers/net/netdevsim/tls.c-90- */\ndrivers/net/netdevsim/tls.c:91:static bool nsim_tls_rx_offloaded(struct netdevsim *ns,\ndrivers/net/netdevsim/tls.c-92-\t\t\t\t  const struct nsim_tls_tuple *t)\n--\ndrivers/net/netdevsim/tls.c-115- */\ndrivers/net/netdevsim/tls.c:116:static void nsim_tls_tx_zero_tags(struct sk_buff *skb)\ndrivers/net/netdevsim/tls.c-117-{\n--\ndrivers/net/netdevsim/tls.c-157- */\ndrivers/net/netdevsim/tls.c:158:static void nsim_tls_tx(struct netdevsim *ns, struct sk_buff *skb)\ndrivers/net/netdevsim/tls.c-159-{\n--\ndrivers/net/netdevsim/tls.c-172- */\ndrivers/net/netdevsim/tls.c:173:static void nsim_tls_rx(struct netdevsim *ns, struct sk_buff *skb)\ndrivers/net/netdevsim/tls.c-174-{\n--\ndrivers/net/netdevsim/tls.c=196=void nsim_do_tls(struct sk_buff *skb, struct netdevsim *ns,\n--\ndrivers/net/netdevsim/tls.c-209-\ndrivers/net/netdevsim/tls.c:210:static int nsim_tls_dev_add(struct net_device *netdev, struct sock *sk,\ndrivers/net/netdevsim/tls.c-211-\t\t\t    enum tls_offload_ctx_dir direction,\n--\ndrivers/net/netdevsim/tls.c-266-\ndrivers/net/netdevsim/tls.c:267:static void nsim_tls_dev_del(struct net_device *netdev,\ndrivers/net/netdevsim/tls.c-268-\t\t\t     struct tls_context *tls_ctx,\n--\ndrivers/net/netdevsim/tls.c-299- */\ndrivers/net/netdevsim/tls.c:300:static int nsim_tls_dev_resync(struct net_device *netdev, struct sock *sk,\ndrivers/net/netdevsim/tls.c-301-\t\t\t       u32 seq, u8 *rcd_sn,\n--\ndrivers/net/netdevsim/tls.c=311=static const struct tlsdev_ops nsim_tlsdev_ops = {\n--\ndrivers/net/netdevsim/tls.c-316-\ndrivers/net/netdevsim/tls.c:317:static ssize_t nsim_tls_dbg_read(struct file *filp, char __user *buffer,\ndrivers/net/netdevsim/tls.c-318-\t\t\t\t size_t count, loff_t *ppos)\n--\ndrivers/net/netdevsim/tls.c=375=static const struct file_operations nsim_tls_dbg_fops = {\n--\ndrivers/net/netdevsim/tls.c-381-\ndrivers/net/netdevsim/tls.c:382:void nsim_tls_init(struct netdevsim *ns)\ndrivers/net/netdevsim/tls.c-383-{\n--\ndrivers/net/netdevsim/tls.c-395-\ndrivers/net/netdevsim/tls.c:396:void nsim_tls_teardown(struct netdevsim *ns)\ndrivers/net/netdevsim/tls.c-397-{\n"
}

9/2 2026/07/29 15:06 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 20577
							output: 39
							thoughts: 366
						

Thoughts:
**Prioritizing TLS Functionality**

Okay, so I need to zero in on the key TLS functions here. The instructions are leaning me towards focusing on `nsim_do_tls`, `nsim_tls_dev_add`, `nsim_tls_dev_del`, `nsim_tls_dev_resync`, `nsim_tls_dbg_read`, `nsim_tls_init`, and `nsim_tls_teardown`.

Hold on a second, a few of those – `nsim_tls_dev_add`, `nsim_tls_dev_del`, `nsim_tls_dev_resync`, and `nsim_tls_dbg_read` – are declared as static. The guidance suggests preferring non-static, non-inlined API entrypoints to avoid inlined helpers without distinct symbols. However, I see that `nsim_tls_dev_add` is assigned to `nsim_tlsdev_ops`, a struct of function pointers. That's good, meaning it *won't* be inlined and is potentially a solid choice. But `nsim_do_tls` is non-static.

Let's simplify things for now and use `nsim_do_tls`, `nsim_tls_init`, and `nsim_tls_teardown`.

Wait, thinking more about it, `nsim_tls_dev_add` is the core entry point for adding a new TLS context. Even though it's static, its address is taken, so it won't be inlined. It's a valid, perhaps the best target. Although, it is a valid target because it is referenced in a function pointer, and we could also choose `nsim_do_tls`.

Actually, now I remember, `nsim_do_tls` is called from `nsim_start_xmit`.

Time to double-check `nsim_do_tls` in `drivers/net/netdevsim/tls.c` and make sure it's the right fit.



10/2 2026/07/29 15:06 tool read-file
0m Args:
{
  "File": "drivers/net/netdevsim/tls.c",
  "FirstLine": 190,
  "LineCount": 20
}

Results:
{
  "Contents": " 190:\t\n 191:\t/* netdevsim has no wire: nsim_start_xmit() hands the skb straight to the\n 192:\t * peer's receive path, so this one call site stands in for the hardware of\n 193:\t * both ports.  It has to run before nsim_forward_skb() moves the skb over,\n 194:\t * which resets the headers.\n 195:\t */\n 196:\tvoid nsim_do_tls(struct sk_buff *skb, struct netdevsim *ns,\n 197:\t\t\t struct netdevsim *peer_ns)\n 198:\t{\n 199:\t\t/* nsim_do_psp() ran first and may have wrapped the record stream, so\n 200:\t\t * this is no longer a plain TLS over TCP packet.  No NIC chains the\n 201:\t\t * two inline offloads either, so leave it alone.\n 202:\t\t */\n 203:\t\tif (skb-\u003eencapsulation)\n 204:\t\t\treturn;\n 205:\t\n 206:\t\tnsim_tls_tx(ns, skb);\n 207:\t\tnsim_tls_rx(peer_ns, skb);\n 208:\t}\n 209:\t\n"
}

11/2 2026/07/29 15:06 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 21773
							output: 39
							thoughts: 23
						

Thoughts:
Okay, let's dive into this. Here's my thought process on this:

**Initial Investigation into TLS Initialization and Teardown**

Alright, my immediate focus is on understanding the core functionality of these two specific functions: `nsim_tls_init` and `nsim_tls_teardown`. These names immediately suggest they are related to the initialization and finalization of TLS (Transport Layer Security) within the context of the `nsim` framework. Given my expertise, I'm expecting to find details about things like establishing secure connections, setting up TLS contexts, handling certificates, key management, and ultimately, gracefully shutting down those secured sessions. A successful investigation of these functions would involve looking for a number of features like a successful TLS initialization, proper error handling and resource cleanup when tearing down the TLS connection, and the inclusion of well-structured functions such as `SSL_CTX_new`, `SSL_new`, `SSL_connect`, and `SSL_shutdown`. I need to understand how `nsim` manages the intricacies of TLS within its architecture. Now, let's get into the details of these functions.



12/2 2026/07/29 15:06 tool read-file
0m Args:
{
  "File": "drivers/net/netdevsim/tls.c",
  "FirstLine": 380,
  "LineCount": 40
}

Results:
{
  "Contents": " 380:\t};\n 381:\t\n 382:\tvoid nsim_tls_init(struct netdevsim *ns)\n 383:\t{\n 384:\t\tINIT_LIST_HEAD(\u0026ns-\u003etls.conns);\n 385:\t\tspin_lock_init(\u0026ns-\u003etls.lock);\n 386:\t\n 387:\t\tns-\u003enetdev-\u003etlsdev_ops = \u0026nsim_tlsdev_ops;\n 388:\t\tns-\u003enetdev-\u003efeatures |= NSIM_TLS_FEATURES;\n 389:\t\tns-\u003enetdev-\u003ehw_features |= NSIM_TLS_FEATURES;\n 390:\t\n 391:\t\tns-\u003etls.dfile = debugfs_create_file(\"tls\", 0400,\n 392:\t\t\t\t\t\t    ns-\u003ensim_dev_port-\u003eddir, ns,\n 393:\t\t\t\t\t\t    \u0026nsim_tls_dbg_fops);\n 394:\t}\n 395:\t\n 396:\tvoid nsim_tls_teardown(struct netdevsim *ns)\n 397:\t{\n 398:\t\tstruct nsim_tls_conn *conn, *tmp;\n 399:\t\tu32 left;\n 400:\t\n 401:\t\tdebugfs_remove_recursive(ns-\u003etls.dfile);\n 402:\t\n 403:\t\tspin_lock_bh(\u0026ns-\u003etls.lock);\n 404:\t\tleft = ns-\u003etls.count;\n 405:\t\tlist_for_each_entry_safe(conn, tmp, \u0026ns-\u003etls.conns, list) {\n 406:\t\t\tlist_del_rcu(\u0026conn-\u003elist);\n 407:\t\t\tkfree_rcu(conn, rcu);\n 408:\t\t}\n 409:\t\tns-\u003etls.count = 0;\n 410:\t\tns-\u003etls.tx_conn = 0;\n 411:\t\tns-\u003etls.rx_conn = 0;\n 412:\t\tspin_unlock_bh(\u0026ns-\u003etls.lock);\n 413:\t\n 414:\t\tif (left)\n 415:\t\t\tnetdev_err(ns-\u003enetdev,\n 416:\t\t\t\t   \"tearing down TLS offload with %u connections left\\n\",\n 417:\t\t\t\t   left);\n 418:\t}\n"
}

13/2 2026/07/29 15:06 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 22692
							output: 132
							thoughts: 495
						

Thoughts:
**My Analysis of the kTLS Offload Patch for netdevsim**

Okay, so I'm digging into this patch. My initial thought was to just grab `nsim_do_tls` since it's not static and not inlined, along with `nsim_tls_init` and `nsim_tls_teardown`.  `nsim_tls_dev_add` caught my eye initially, since its address is taken - indicating it's *probably* not inlined despite being static. But then I realized that `nsim_tls_dev_add` and `nsim_tls_dev_del` are really the heart of the TLS context management for this netdevsim kTLS offload. They *are* assigned to the `nsim_tlsdev_ops` structure which is the fundamental mechanism for handling the kTLS context addition and deletion, so I should include them. Then, wait, the instructions say to prefer non-static, non-inlined API entrypoint functions over internal static helper functions. I should stick with `nsim_do_tls`, `nsim_tls_init`, and `nsim_tls_teardown`. I'm going to stick with the `nsim_do_tls`, `nsim_tls_init`, and `nsim_tls_teardown`.

Now, I need to verify the configurations. I'm focusing on `NETDEVSIM`, `TLS`, and `TLS_DEVICE`. Checking the dependencies... `TLS` needs `INET`. `NETDEVSIM` needs `INET`,  `PSAMPLE || PSAMPLE=n`, `PTP_1588_CLOCK_MOCK || PTP_1588_CLOCK_MOCK=n`, and `TLS || TLS=n`. So, those three configurations should be sufficient.

The big picture is this: this patch is introducing a new, emulated kTLS offload capability to the `netdevsim` driver. This is a functional addition that provides new, reachable code paths. This means it's for testing TLS hardware offload in a virtualized, simulated environment. The new code should be invoked via the standard socket options and typical network operations when using `netdevsim`.

To recap, the key symbols here are `nsim_do_tls`, `nsim_tls_init`, and `nsim_tls_teardown`.



14/2 2026/07/29 15:07 tool set-results
0m Args:
{
  "EnableConfigs": [
    "NETDEVSIM",
    "TLS",
    "TLS_DEVICE"
  ],
  "FocusSymbols": [
    "nsim_do_tls",
    "nsim_tls_init",
    "nsim_tls_teardown"
  ],
  "Reasoning": "The patch introduces emulated kTLS offload to the netdevsim driver. This is a functional change that adds new reachable code paths for testing TLS hardware offload in a virtualized environment. The code is reachable via standard socket options and network operations when netdevsim is used.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": [
    "NETDEVSIM",
    "TLS",
    "TLS_DEVICE"
  ],
  "FocusSymbols": [
    "nsim_do_tls",
    "nsim_tls_init",
    "nsim_tls_teardown"
  ],
  "Reasoning": "The patch introduces emulated kTLS offload to the netdevsim driver. This is a functional change that adds new reachable code paths for testing TLS hardware offload in a virtualized environment. The code is reachable via standard socket options and network operations when netdevsim is used.",
  "WorthFuzzing": true
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)