Article View: linux.kernel.netdev
Article #23101Re: [PATCH net-next v3 02/12] psp: take the psp nexthdr from the packet on encap
From: Björn Töpel
Date: Thu, 26 Nov 2026 14:32
Date: Thu, 26 Nov 2026 14:32
235 lines
18528 bytes
18528 bytes
Todd Freed <toddfreed@meta.com> writes:
> PSP socket support has thus far been limited to TCP, and
> psp_dev_encapsulate() hardcodes IPPROTO_TCP as the PSP next header.
>
> Socket-less PSP associations will also support UDP traffic. Populate the
> PSP next header from the IP header of the original packet instead.
>
> With the next header taken from the fixed header, psp_dev_encapsulate()
> now refuses some cases which are unsupported:
>
> - IPv4 and IPv6 fragments. Non-initial fragments have no L4 header, so
> this cannot work. Not a behavior change for PSP sockets.
> - IPv6 extension headers. More generally, any packet whose transport
> header isn't right after the IP header (IPv4 options are fine). Some
> of these could be supported in the future. Not a behavior change for
> PSP sockets.
> - Already encapsulated for a tunnel (skb->encapsulation). Their inner
> headers are already set up.
> - A checksum to complete (CHECKSUM_PARTIAL) for a next header other than
> TCP or UDP, which the device can't complete behind the PSP header.
> Software GSO of a tunnel with an outer checksum, e.g. GRE with csum,
> clears skb->encapsulation on the segments but leaves the outer checksum
> to the device.
>
> Both callers of psp_dev_encapsulate(), mlx5 and netdevsim, drop the
> packet when it returns false.
>
> mlx5e also fixes up the TCP checksum of GSO skbs for the device, which
> is only valid for TCP. Have the stack segment other GSO skbs, e.g. UDP
> from socket-less associations, in software. Likewise the device only
> completes a TCP or UDP checksum behind the PSP header: have the stack
> checksum the other CHECKSUM_PARTIAL skbs.
>
> For the outer UDP source port, psp_write_headers() already branches to
> use TCP ehash if the skb has a socket, with fallback to
> udp_flow_src_port(). Previously, sk always meant TCP. Add an explicit
> check on nexthdr, and on the socket being a TCP one (a raw or packet
> socket can send TCP segments, and has no sk_hash), so that the fallback
> branch is taken for other packets. udp_flow_src_port() hashes the flow
> if the skb has no hash yet: compute it in psp_dev_encapsulate() before
> the headers are moved and the IP protocol rewritten, so that it covers
> the packet's own flow rather than the outer headers being written.
>
> Signed-off-by: Todd Freed <toddfreed@meta.com>
> ---
> .../net/ethernet/mellanox/mlx5/core/en_main.c | 33 +++++++++
> net/psp/psp_main.c | 67 ++++++++++++++++---
> 2 files changed, 90 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_main.c b/drivers/net/ethernet/mellanox/mlx5/core/en_main.c
> index 54f4f5a097c8..0fac06651e5b 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/en_main.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/en_main.c
> @@ -5248,6 +5248,23 @@ static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv,
> return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
> }
>
> +/* The L4 protocol right after the IP header (and its IPv4 options), which
> + * the PSP Tx path requires, is one the device can complete the checksum of
> + */
> +static bool mlx5e_psp_l4_csum_ok(const struct sk_buff *skb)
> +{
> + u8 proto;
> +
> + if (skb->protocol == htons(ETH_P_IP))
> + proto = ip_hdr(skb)->protocol;
> + else if (skb->protocol == htons(ETH_P_IPV6))
> + proto = ipv6_hdr(skb)->nexthdr;
> + else
> + return false;
> +
> + return proto == IPPROTO_TCP || proto == IPPROTO_UDP;
> +}
> +
> netdev_features_t mlx5e_features_check(struct sk_buff *skb,
> struct net_device *netdev,
> netdev_features_t features)
> @@ -5256,6 +5273,22 @@ netdev_features_t mlx5e_features_check(struct sk_buff *skb,
>
> features = vlan_features_check(skb, features);
>
> + /* The PSP tx path fixes up the TCP checksum of a GSO skb for the
> + * device, which is only valid for TCP, and the device only completes
> + * a TCP or UDP checksum behind the PSP header. Have the stack segment
> + * and checksum other skbs.
> + */
> + if ((skb_is_gso(skb) || skb->ip_summed == CHECKSUM_PARTIAL) &&
> + mlx5e_psp_is_offload(skb, netdev)) {
> + if (skb_is_gso(skb) &&
> + !(skb_shinfo(skb)->gso_type &
> + (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
> + features &= ~NETIF_F_GSO_MASK;
> + if (skb->ip_summed == CHECKSUM_PARTIAL &&
> + !mlx5e_psp_l4_csum_ok(skb))
> + features &= ~NETIF_F_CSUM_MASK;
> + }
Hmm, that's a large check in the hotpath -- maybe it's cheap? So the
cost for small UDP packets with Tx checksumming enabled and no PSP don't
blow up.
> +
> /* Validate if the tunneled packet is being offloaded by HW */
> if (skb->encapsulation &&
> (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
> diff --git a/net/psp/psp_main.c b/net/psp/psp_main.c
> index 964749a865b1..dc6144469647 100644
> --- a/net/psp/psp_main.c
> +++ b/net/psp/psp_main.c
> @@ -214,7 +214,8 @@ unsigned int psp_key_size(u32 version)
> EXPORT_SYMBOL(psp_key_size);
>
> static void psp_write_headers(struct net *net, struct sk_buff *skb, __be32 spi,
> - u8 ver, unsigned int udp_len, __be16 sport)
> + u8 ver, u8 nexthdr, unsigned int udp_len,
> + __be16 sport)
> {
> struct udphdr *uh = udp_hdr(skb);
> struct psphdr *psph = (struct psphdr *)(uh + 1);
> @@ -236,10 +237,11 @@ static void psp_write_headers(struct net *net, struct sk_buff *skb, __be32 spi,
> * This field is subject to reshuffling, thanks to
> * sk_rethink_txhash() calls in various TCP functions.
> *
> - * Instead, use sk->sk_hash which is constant through
> + * Instead, use sk->sk_hash for TCP which is constant through
> * the whole flow duration.
> */
> - if (likely(sk)) {
> + if (likely(sk && nexthdr == IPPROTO_TCP &&
> + (!sk_fullsock(sk) || sk_is_tcp(sk)))) {
> u32 hash = sk->sk_hash;
> int min, max;
>
> @@ -261,7 +263,7 @@ static void psp_write_headers(struct net *net, struct sk_buff *skb, __be32 spi,
> uh->check = 0;
> udp_set_len(uh, udp_len);
>
> - psph->nexthdr = IPPROTO_TCP;
> + psph->nexthdr = nexthdr;
> psph->hdrlen = PSP_HDRLEN_NOOPT;
> psph->crypt_offset = 0;
> psph->verfl = FIELD_PREP(PSPHDR_VERFL_VERSION, ver) |
> @@ -270,8 +272,34 @@ static void psp_write_headers(struct net *net, struct sk_buff *skb, __be32 spi,
> memset(&psph->iv, 0, sizeof(psph->iv));
> }
>
> -/* Encapsulate a TCP packet with PSP by adding the UDP+PSP headers and filling
> - * them in.
> +/* UDP+PSP goes in at the transport header, the protocol of the IP header
> + * becomes UDP, and the PSP next header takes the protocol it had. The
> + * transport header must directly follow the IP header: IPv4 options are fine,
> + * IPv6 extension headers aren't.
> + */
> +static bool psp_dev_encap_ok(const struct sk_buff *skb)
> +{
> + u32 network_len = skb_network_header_len(skb);
> +
> + /* inner headers already set up */
> + if (skb->encapsulation)
> + return false;
> + /* fragments not supported for transport mode */
> + if (skb->protocol == htons(ETH_P_IP))
> + return network_len == ip_hdrlen(skb) &&
> + !ip_is_fragment(ip_hdr(skb));
> + if (skb->protocol == htons(ETH_P_IPV6))
> + return !ipv6_ext_hdr(ipv6_hdr(skb)->nexthdr) &&
> + network_len == sizeof(struct ipv6hdr);
> + return false;
> +}
> +
> +/* Encapsulate a packet with PSP by adding the UDP+PSP headers and filling
> + * them in. The PSP next header is taken from the IP header: packets with
> + * IPv6 extension headers, or whose transport header isn't right after the IP
> + * header, are refused, as are IP fragments, packets already encapsulated
> + * for a tunnel, and packets with a checksum to complete for a protocol other
> + * than TCP or UDP.
> */
> bool psp_dev_encapsulate(struct net *net, struct sk_buff *skb, __be32 spi,
> u8 ver, __be16 sport)
> @@ -279,11 +307,30 @@ bool psp_dev_encapsulate(struct net *net, struct sk_buff *skb, __be32 spi,
> u32 network_len = skb_network_header_len(skb);
> u32 ethr_len = skb_mac_header_len(skb);
> u32 bufflen = ethr_len + network_len;
> + u8 nexthdr;
>
> - if (skb->protocol != htons(ETH_P_IP) &&
> - skb->protocol != htons(ETH_P_IPV6))
> + if (!psp_dev_encap_ok(skb))
> return false;
>
> + if (skb->protocol == htons(ETH_P_IP))
> + nexthdr = ip_hdr(skb)->protocol;
> + else
> + nexthdr = ipv6_hdr(skb)->nexthdr;
> +
> + /* Devices complete only a TCP or UDP checksum behind the PSP header.
> + * Software GSO of a tunnel with an outer checksum, e.g. GRE, leaves
> + * the segments here with encapsulation cleared and CHECKSUM_PARTIAL
> + * at the tunnel header.
> + */
> + if (skb->ip_summed == CHECKSUM_PARTIAL &&
> + nexthdr != IPPROTO_TCP && nexthdr != IPPROTO_UDP)
> + return false;
> +
> + /* udp_flow_src_port() may need to hash the flow: do it on the
> + * original headers, before they are moved and rewritten
> + */
> + skb_get_hash(skb);
Wdyt about adding a helper that does source port selection here?
Then pass that sport to the psp_write_headers (unused argument btw). The
block in the header writer is then just uh->source = sport.
Interesting series!
Björn
Technical Details
Message-ID:
<87h5h3irxp.fsf@all.your.base.are.belong.to.us>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-372145-linux-kernel-netdev=dewy.ruReferences:
<20261124221758.3230929-1-toddfreed@meta.com> <20261124221758.3230929-3-toddfreed@meta.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-372145-linux-kernel-netdev=dewy.ru From: bjorn@kernel.org (Björn Töpel) Newsgroups: linux.kernel.netdev Subject: Re: [PATCH net-next v3 02/12] psp: take the psp nexthdr from the packet on encap Date: Thu, 26 Nov 2026 15:32:50 +0100 Organization: Usenet Network Message-ID: <87h5h3irxp.fsf@all.your.base.are.belong.to.us> References: <20261124221758.3230929-1-toddfreed@meta.com> <20261124221758.3230929-3-toddfreed@meta.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: usenet.network; logging-data="485456"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:VNaKiTFphzDR9UBt2md4a8FAWEA= sha256:ikji1Nqo9fcDuH4bAiRlxVVGQKwTcq09GVrHONLgEvA= sha1:ZnBz4MkQkrEKvfN4H0IfVnSKxOA= sha256:Tn582KEd4d7EReJj+iKl7uezulMNkxJsCgEUOHngGLQ= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id A079D125EE1; Thu, 26 Nov 2026 17:33:11 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.401 X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id uqxvK-rIAJ1X; Thu, 26 Nov 2026 17:33:08 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-372145-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id BE665125EDF; Thu, 26 Nov 2026 17:32:58 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 3CB4246918; Thu, 26 Nov 2026 14:32:56 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795703576; b=vXv5p0/4I5XTR0JVN2XuEYmw5P9XInmbFe5MfOdAB090Y5V/eHkRfH+h6drbOTgLkX9e KTqi+KEX9cx2NrPjE3Ch8OQ/byxBa+MTSIjnxyFKeS3gjfDLVHoc1ydASOOAQKTE028G6 6vNhS80xJt7XZu1tv90A3+17DsbGov1k8ySOoGnLiKtCkIJVBFuZKlnIBiJGsef8A2h0y oN7MUuNWSfPwvCv/pKxj/F8S3wsBU45+gef4FGrz8NqDFePZ7B/10b8pO7XONUK9JeWGR N53N1KJrfPfXmlxlI5o4Xg7fyM9PQbaKBylPBYUKnoGoaSsDsM9493YcbhWs0Qb5e2g== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795703576; h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject: In-Reply-To:References:Date:Message-ID:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=lh7RPirw1fOLix78b5McENzWSQPv2ScyL9X0ufdtQao=; b=FHb5eEgYyhWO84Gb53OusJtZym6Ntyv7kg/0Lnufi9eQwIeAwmOBP5OPbdPvkzV7Fs1i +U0bJpjtRtPOzadG/T+/+QqvKYn7JUWzbPKY8T5tQ7+U0np4P9CBkEeaCdNTXV07f6z/A YNfUU/l1Q/d4KQUIKVeoZYId6veB0ZdjU9VVfoAXKL4VzvAWuRVZ73m/hSWX1cfmimND0 q9OAEtQm+hyiK091t6WFXgNVXp9BYnFh/G2tcu9j9fakt5gF8RZYbfl+sPCDXaY0okCH2 j/mYRnPZiNjXHdvZi+UthWe748vwDC0hZuYw5agzBrwKHfc2EdbE0kawweB7/VPFuqg== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=HrNCUEeT; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 143E7606A32C; Thu, 26 Nov 2026 14:32:56 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=HrNCUEeT X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795703575; b=3ljPzOjcbI6gq5UP+golZFABptT9i3bR/Dyv4rjAeNNKUGecb7fp2zLDSKW8CgViln7r 8A7y0ynJlJW3m3Iv9S1y0eKUSBWoRwZouU3SdnoCIt3K2MuHBoTcScy16mwHcwmjwCd/8 Iq4ILif8ER6HbKmqgFuA1oujKbOjQmTelX37KSuiB1WMMlcWC8DJdITCiyxpYdNx1q0V6 7Rg/yazUTVd40ZN17cc2KbphGht0gjD+5msYSEWebjFX1IN7+1J5AXY3R73Pj7uyFLrEV NAUpJethZKAYbwDqQsAU0RwsVZydTq4nt4uG1H29dX/BpsSOj6NSGmb7l/uXuJTgsAQ== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795703575; h=Received:DKIM-Signature:From:To:Cc:Subject:In-Reply-To:References: Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=lh7RPirw1fOLix78b5McENzWSQPv2ScyL9X0ufdtQao=; b=RtfF0jTxz52UoQrZ2YuxDTjTdKfQzlnDACaR0yrMgjpKyoVhSZvuGdQM+OQ8rKI1lFmB xtbEKH9q+uV5o3ov28SjObsyhFf2UbUZSFXQx9Z57AzV77O/LgbY4XMmwZ/QJC4UKxNeQ Et5ibvDjG/wqElNe9YZJvti0v/nCoNDh/xdRcfA0zUTaODMluZwjBkY26Pepk1yrdXbtI /YrODVYJA+nwm33w58+PZLlglJddpVsLFkTRgcbiC6in+WIAAzA6xAofdm564pnklckiR OCsHrxX6IpSR4lmOAx18A0XZR/WiKbnka8gY50tS6DUp84Qk+f+NqGvR9+ArzTExiSg== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=HrNCUEeT; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28024606A683; Thu, 26 Nov 2026 14:32:54 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id E28361F000C4; Thu, 26 Nov 2026 14:32:53 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795703574; bh=lh7RPirw1fOLix78b5McENzWSQPv2ScyL9X0ufdtQao=; h=From:To:Cc:Subject:In-Reply-To:References:Date; b=HrNCUEeTAhOWR1IQilGFsl5Z0M6KEHvI3p4ZWhkWJYc1d/3XH4HfTdnseq1layVul lOtNqTbJpsHUKstbpbjjtYqQX69nCKvS5+PsgDzzf+Cq+KWqngjIzTc3iBme6+zoSc M39ST3oF5CJCTOEMq0tDLWmyrVIJ5lRYcY+rJT1R90HPfLeCv7IHwb3gD6G2G+sPI0 9Nn2gQBWu0jjP6GTuO2XWveelOPUmUaPer/YNTzdl6IbM75hivo+kH4u8Q0eQc4KkN HtTLKBmIhLXuqmepKnj0ZdFVE3zXyITbbn4DdOgQnH9ZLCmw0FlfSW5p2//I9bEqF4 Gz6P9fwNyolBg== X-Cc: "David S. Miller" <davem@davemloft.net>, Andrew Lunn <andrew+netdev@lunn.ch>, Boris Pismenny <borisp@nvidia.com>, Cosmin Ratiu <cratiu@nvidia.com>, Daniel Zahka <daniel.zahka@gmail.com>, Donald Hunter <donald.hunter@gmail.com>, Dragos Tatulea <dtatulea@nvidia.com>, Eric Dumazet <edumazet@kernel.org>, Jakub Kicinski <kuba@kernel.org>, Jamal Hadi Salim <jhs@mojatatu.com>, Jiri Pirko <jiri@resnulli.us>, Leon Romanovsky <leon@kernel.org>, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-rdma@vger.kernel.org, Mark Bloch <mbloch@nvidia.com>, Paolo Abeni <pabeni@redhat.com>, Pedro Tammela <pctammela@mojatatu.com>, Saeed Mahameed <saeedm@nvidia.com>, Simon Horman <horms@kernel.org>, Tariq Toukan <tariqt@nvidia.com>, Wei Wang <weiwan@google.com>, Willem de Bruijn <willemdebruijn.kernel@gmail.com>, Kuniyuki Iwashima <kuniyu@google.com>, bpf@vger.kernel.org, Asbjørn Sloth Tønnesen <ast@fiberby.net>, Qingfang Deng <qingfang.deng@linux.dev>, Toke Høiland-Jørgensen <toke@redhat.com>, Shuah Khan <shuah@kernel.org>, Jonathan Corbet <corbet@lwn.net>, Shuah Khan <skhan@linuxfoundation.org>, Randy Dunlap <rdunlap@infradead.org>, Matthieu Baerts <matttbe@kernel.org>, David Ahern <dsahern@kernel.org>, Jonas Köppeler <j.koeppeler@tu-berlin.de>, Hangbin Liu <hangbin.liu@linux.dev> X-In-Reply-To: <20261124221758.3230929-3-toddfreed@meta.com> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list