From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202602 header.b=kQl4gkiO; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id B26AA5A0265 for ; Mon, 11 May 2026 09:49:13 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1778485749; bh=fdpdonBu8aH1tpG10cFdcS/2kP8ef516TT2L6krucx4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=kQl4gkiOKJ4le2PqkQWO1iZ0JRHLjcjorzbWoCUGtEkzQnlb/VXDfPe9zXJTMJURc HAR04TXV1Kt1fpepd+FUz856Mu/r9pu5MYnJ5+PJcFxw+e0Hz+A37QNiqfs2CqY9+i /nTc3kERN7eVqcAGX1DPucQ3B5HX5XhX5IRS0YnQ4lFc9dEhKJ8YrdKvqJV810mzQ4 rz5R9JSFrsM65Ppy/Cvk2DFlBdoaWi1q1lCpGRRBSTxmXIKl0Y77OPK4WuQMJ/ZnKo 5Q9c6B4oVm3ESkZWfhUVgmnPhIzrbYJa/j36zpNZIIxD7PHu1TvSneg3p10amQZXTK 5rpDUEoQNCjXg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4gDX1147Mjz4wJc; Mon, 11 May 2026 17:49:09 +1000 (AEST) Date: Mon, 11 May 2026 17:49:04 +1000 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v6 1/4] tcp: Encode checksum computation flags in a single parameter Message-ID: References: <20260416161618.3826904-1-lvivier@redhat.com> <20260416161618.3826904-2-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="ptre+gHSv7jb5CTV" Content-Disposition: inline In-Reply-To: <20260416161618.3826904-2-lvivier@redhat.com> Message-ID-Hash: ALQTJWT4CYDKEYHE3CWNAOGF4SORDFMQ X-Message-ID-Hash: ALQTJWT4CYDKEYHE3CWNAOGF4SORDFMQ X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --ptre+gHSv7jb5CTV Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Apr 16, 2026 at 06:16:15PM +0200, Laurent Vivier wrote: > tcp_fill_headers() takes a pointer to a previously computed IPv4 header > checksum to avoid recalculating it when the payload length doesn't > change, and a separate bool to skip TCP checksum computation. >=20 > Replace both parameters with a single uint32_t csum_flags that encodes: > - IP4_CSUM (bit 31): compute IPv4 header checksum from scratch > - TCP_CSUM (bit 30): compute TCP checksum > - IP4_CMASK (low 16 bits): cached IPv4 header checksum value >=20 > When IP4_CSUM is not set, the cached checksum is extracted from the low > 16 bits. This is cleaner than the pointer-based approach, and also > avoids a potential dangling pointer issue: a subsequent patch makes > tcp_fill_headers() access ip4h via with_header(), which scopes it to a > temporary variable, so a pointer to ip4h->check would become invalid > after the with_header() block. >=20 > Suggested-by: David Gibson > Signed-off-by: Laurent Vivier Reviewed-by: David Gibson > --- > tcp.c | 25 +++++++++++++------------ > tcp_buf.c | 23 ++++++++++++----------- > tcp_internal.h | 7 +++++-- > tcp_vu.c | 28 +++++++++++++++++----------- > 4 files changed, 47 insertions(+), 36 deletions(-) >=20 > diff --git a/tcp.c b/tcp.c > index 45bcc19375fe..de362290b034 100644 > --- a/tcp.c > +++ b/tcp.c > @@ -946,9 +946,10 @@ static void tcp_fill_header(struct tcphdr *th, > * @th: Pointer to TCP header > * @payload: TCP payload > * @dlen: TCP payload length > - * @ip4_check: IPv4 checksum, if already known > + * @csum_flags: TCP_CSUM if TCP checksum must be computed, > + * IP4_CSUM if IPv4 checksum must be computed, > + * otherwise IPv4 checksum is provided in IP4_CMASK > * @seq: Sequence number for this segment > - * @no_tcp_csum: Do not set TCP checksum > * > * Return: frame length (including L2 headers) > */ > @@ -956,8 +957,7 @@ size_t tcp_fill_headers(const struct ctx *c, struct t= cp_tap_conn *conn, > struct ethhdr *eh, > struct iphdr *ip4h, struct ipv6hdr *ip6h, > struct tcphdr *th, struct iov_tail *payload, > - size_t dlen, const uint16_t *ip4_check, uint32_t seq, > - bool no_tcp_csum) > + size_t dlen, uint32_t csum_flags, uint32_t seq) > { > const struct flowside *tapside =3D TAPFLOW(conn); > size_t l4len =3D dlen + sizeof(*th); > @@ -977,13 +977,14 @@ size_t tcp_fill_headers(const struct ctx *c, struct= tcp_tap_conn *conn, > ip4h->saddr =3D src4->s_addr; > ip4h->daddr =3D dst4->s_addr; > =20 > - if (ip4_check) > - ip4h->check =3D *ip4_check; > - else > + if (csum_flags & IP4_CSUM) { > ip4h->check =3D csum_ip4_header(l3len, IPPROTO_TCP, > *src4, *dst4); > + } else { > + ip4h->check =3D csum_flags & IP4_CMASK; > + } > =20 > - if (!no_tcp_csum) { > + if (csum_flags & TCP_CSUM) { > psum =3D proto_ipv4_header_psum(l4len, IPPROTO_TCP, > *src4, *dst4); > } > @@ -1003,7 +1004,7 @@ size_t tcp_fill_headers(const struct ctx *c, struct= tcp_tap_conn *conn, > =20 > ip6_set_flow_lbl(ip6h, conn->sock); > =20 > - if (!no_tcp_csum) { > + if (csum_flags & TCP_CSUM) { > psum =3D proto_ipv6_header_psum(l4len, IPPROTO_TCP, > &ip6h->saddr, > &ip6h->daddr); > @@ -1018,10 +1019,10 @@ size_t tcp_fill_headers(const struct ctx *c, stru= ct tcp_tap_conn *conn, > =20 > tcp_fill_header(th, conn, seq); > =20 > - if (no_tcp_csum) > - th->check =3D 0; > - else > + if (csum_flags & TCP_CSUM) > tcp_update_csum(psum, th, payload, dlen); > + else > + th->check =3D 0; > =20 > return MAX(l3len + sizeof(struct ethhdr), ETH_ZLEN); > } > diff --git a/tcp_buf.c b/tcp_buf.c > index 27151854033c..a27d9733616c 100644 > --- a/tcp_buf.c > +++ b/tcp_buf.c > @@ -166,14 +166,15 @@ static void tcp_l2_buf_pad(struct iovec *iov) > * @c: Execution context > * @conn: Connection pointer > * @iov: Pointer to an array of iovec of TCP pre-cooked buffers > - * @check: Checksum, if already known > + * @csum_flags: TCP_CSUM if TCP checksum must be computed, > + * IP4_CSUM if IPv4 checksum must be computed, > + * otherwise IPv4 checksum is provided in IP4_CMASK > * @seq: Sequence number for this segment > - * @no_tcp_csum: Do not set TCP checksum > */ > static void tcp_l2_buf_fill_headers(const struct ctx *c, > struct tcp_tap_conn *conn, > - struct iovec *iov, const uint16_t *check, > - uint32_t seq, bool no_tcp_csum) > + struct iovec *iov, uint32_t csum_flags, > + uint32_t seq) > { > struct iov_tail tail =3D IOV_TAIL(&iov[TCP_IOV_PAYLOAD], 1, 0); > struct tcphdr th_storage, *th =3D IOV_REMOVE_HEADER(&tail, th_storage); > @@ -191,8 +192,7 @@ static void tcp_l2_buf_fill_headers(const struct ctx = *c, > ip6h =3D iov[TCP_IOV_IP].iov_base; > =20 > l2len =3D tcp_fill_headers(c, conn, eh, ip4h, ip6h, th, &tail, > - iov_tail_size(&tail), check, seq, > - no_tcp_csum); > + iov_tail_size(&tail), csum_flags, seq); > tap_hdr_update(taph, l2len); > } > =20 > @@ -234,7 +234,7 @@ int tcp_buf_send_flag(const struct ctx *c, struct tcp= _tap_conn *conn, int flags) > if (flags & KEEPALIVE) > seq--; > =20 > - tcp_l2_buf_fill_headers(c, conn, iov, NULL, seq, false); > + tcp_l2_buf_fill_headers(c, conn, iov, IP4_CSUM | TCP_CSUM, seq); > =20 > tcp_l2_buf_pad(iov); > =20 > @@ -271,7 +271,7 @@ static void tcp_data_to_tap(const struct ctx *c, stru= ct tcp_tap_conn *conn, > ssize_t dlen, int no_csum, uint32_t seq, bool push) > { > struct tcp_payload_t *payload; > - const uint16_t *check =3D NULL; > + uint32_t check =3D IP4_CSUM; > struct iovec *iov; > =20 > conn->seq_to_tap =3D seq + dlen; > @@ -280,9 +280,10 @@ static void tcp_data_to_tap(const struct ctx *c, str= uct tcp_tap_conn *conn, > if (CONN_V4(conn)) { > if (no_csum) { > struct iovec *iov_prev =3D tcp_l2_iov[tcp_payload_used - 1]; > - struct iphdr *iph =3D iov_prev[TCP_IOV_IP].iov_base; > + const struct iphdr *iph =3D iov_prev[TCP_IOV_IP].iov_base; > =20 > - check =3D &iph->check; > + /* overwrite IP4_CSUM flag as we set the checksum */ > + check =3D iph->check; > } > iov[TCP_IOV_IP] =3D IOV_OF_LVALUE(tcp4_payload_ip[tcp_payload_used]); > } else if (CONN_V6(conn)) { > @@ -296,7 +297,7 @@ static void tcp_data_to_tap(const struct ctx *c, stru= ct tcp_tap_conn *conn, > payload->th.ack =3D 1; > payload->th.psh =3D push; > iov[TCP_IOV_PAYLOAD].iov_len =3D dlen + sizeof(struct tcphdr); > - tcp_l2_buf_fill_headers(c, conn, iov, check, seq, false); > + tcp_l2_buf_fill_headers(c, conn, iov, TCP_CSUM | check, seq); > =20 > tcp_l2_buf_pad(iov); > =20 > diff --git a/tcp_internal.h b/tcp_internal.h > index a0fa19f4ed11..40472c9973c8 100644 > --- a/tcp_internal.h > +++ b/tcp_internal.h > @@ -183,12 +183,15 @@ void tcp_rst_do(const struct ctx *c, struct tcp_tap= _conn *conn); > =20 > struct tcp_info_linux; > =20 > +#define IP4_CSUM 0x80000000 > +#define IP4_CMASK 0x0000FFFF > +#define TCP_CSUM 0x40000000 > + > size_t tcp_fill_headers(const struct ctx *c, struct tcp_tap_conn *conn, > struct ethhdr *eh, > struct iphdr *ip4h, struct ipv6hdr *ip6h, > struct tcphdr *th, struct iov_tail *payload, > - size_t dlen, const uint16_t *ip4_check, uint32_t seq, > - bool no_tcp_csum); > + size_t dlen, uint32_t csum_flags, uint32_t seq); > =20 > int tcp_update_seqack_wnd(const struct ctx *c, struct tcp_tap_conn *conn, > bool force_seq, struct tcp_info_linux *tinfo); > diff --git a/tcp_vu.c b/tcp_vu.c > index 2dfe14485eee..3e399c20f0d7 100644 > --- a/tcp_vu.c > +++ b/tcp_vu.c > @@ -134,7 +134,7 @@ int tcp_vu_send_flag(const struct ctx *c, struct tcp_= tap_conn *conn, int flags) > seq--; > =20 > tcp_fill_headers(c, conn, eh, ip4h, ip6h, th, &payload, > - optlen, NULL, seq, !*c->pcap); > + optlen, IP4_CSUM | (*c->pcap ? TCP_CSUM : 0), seq); > =20 > vu_pad(flags_elem[0].in_sg, 1, hdrlen + optlen); > vu_flush(vdev, vq, flags_elem, 1, hdrlen + optlen); > @@ -282,13 +282,15 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c= , struct vu_virtq *vq, > * @iov: Pointer to the array of IO vectors > * @iov_cnt: Number of entries in @iov > * @dlen: Data length > - * @check: Checksum, if already known > - * @no_tcp_csum: Do not set TCP checksum > + * @csum_flags: Pointer to checksum flags (input/output) > + * TCP_CSUM if TCP checksum must be computed, > + * IP4_CSUM if IPv4 checksum must be computed, > + * otherwise IPv4 checksum is provided in IP4_CMASK > * @push: Set PSH flag, last segment in a batch > */ > static void tcp_vu_prepare(const struct ctx *c, struct tcp_tap_conn *con= n, > struct iovec *iov, size_t iov_cnt, size_t dlen, > - const uint16_t **check, bool no_tcp_csum, bool push) > + uint32_t *csum_flags, bool push) > { > const struct flowside *toside =3D TAPFLOW(conn); > bool v6 =3D !(inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)); > @@ -332,9 +334,11 @@ static void tcp_vu_prepare(const struct ctx *c, stru= ct tcp_tap_conn *conn, > th->psh =3D push; > =20 > tcp_fill_headers(c, conn, eh, ip4h, ip6h, th, &payload, dlen, > - *check, conn->seq_to_tap, no_tcp_csum); > + *csum_flags, conn->seq_to_tap); > + > + /* Preserve TCP_CSUM, overwrite IP4_CSUM as we set the checksum */ > if (ip4h) > - *check =3D &ip4h->check; > + *csum_flags =3D (*csum_flags & TCP_CSUM) | ip4h->check; > } > =20 > /** > @@ -350,12 +354,11 @@ int tcp_vu_data_from_sock(const struct ctx *c, stru= ct tcp_tap_conn *conn) > uint32_t wnd_scaled =3D conn->wnd_from_tap << conn->ws_from_tap; > struct vu_dev *vdev =3D c->vdev; > struct vu_virtq *vq =3D &vdev->vq[VHOST_USER_RX_QUEUE]; > + uint32_t already_sent, check; > ssize_t len, previous_dlen; > int i, iov_cnt, head_cnt; > size_t hdrlen, fillsize; > int v6 =3D CONN_V6(conn); > - uint32_t already_sent; > - const uint16_t *check; > =20 > if (!vu_queue_enabled(vq) || !vu_queue_started(vq)) { > debug("Got packet, but RX virtqueue not usable yet"); > @@ -442,7 +445,10 @@ int tcp_vu_data_from_sock(const struct ctx *c, struc= t tcp_tap_conn *conn) > */ > =20 > hdrlen =3D tcp_vu_hdrlen(v6); > - for (i =3D 0, previous_dlen =3D -1, check =3D NULL; i < head_cnt; i++) { > + check =3D IP4_CSUM; > + if (*c->pcap) > + check |=3D TCP_CSUM; > + for (i =3D 0, previous_dlen =3D -1; i < head_cnt; i++) { > struct iovec *iov =3D &elem[head[i]].in_sg[0]; > int buf_cnt =3D head[i + 1] - head[i]; > size_t frame_size =3D iov_size(iov, buf_cnt); > @@ -458,10 +464,10 @@ int tcp_vu_data_from_sock(const struct ctx *c, stru= ct tcp_tap_conn *conn) > =20 > /* The IPv4 header checksum varies only with dlen */ > if (previous_dlen !=3D dlen) > - check =3D NULL; > + check |=3D IP4_CSUM; > previous_dlen =3D dlen; > =20 > - tcp_vu_prepare(c, conn, iov, buf_cnt, dlen, &check, !*c->pcap, push); > + tcp_vu_prepare(c, conn, iov, buf_cnt, dlen, &check, push); > =20 > vu_pad(elem[head[i]].in_sg, buf_cnt, dlen + hdrlen); > vu_flush(vdev, vq, &elem[head[i]], buf_cnt, dlen + hdrlen); > --=20 > 2.53.0 >=20 --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --ptre+gHSv7jb5CTV Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmoBieMACgkQzQJF27ox 2GdSqw/+K3fAcCx7fbeqqMS+H24sIuFPnt3gHVenj3hPos8nLh+K0tRKxfSLEayX y7YmYKw3OyQiuU1ReHkGVxzAkrQsr0zvBGacmhfADfXbzHWrFXblMuc41eoTh3NU /zZmi3sUmLNhF0/rBHNppXWdYTmqZzFpf7+aICLI0YcGZHD9qpuxfb1v/qKZKJvT ZZHaWqL6gu5xYqt0BgLGFZCtgTUefHrjQD2+EVGv8nbHI23AwQ8XOTsVL3hxHv6p a9+892YxVXbAxCl+hK88rN3eFaYpe3966mc0jRV7wnlT7pOPHMvyanOeRDswKsu7 olLiyqKS3Ot6EbRXQhe1B4y5iqcDXZ74TNClXnOaQHXEtA31B8If68Fa7vmxD/5W oU6tEioasb7sfCGDN1fJqDdRRdlL3B4FitsLnZucSOX4z/dOi7/e1md+xEn9aJv/ hDX73ZwpovquPhQEMRM2FryM8MqH8767NvmeGJmC3eo+4gzz7m9BCDMk34vCtmPr 0g2cK72ADsw407D1duCCJVPQJHq/qcaPr0jW+4sry3DOeprtOIR1I0/q5sXbsdFL zzGycwZ27aa90dngElOGU9bt/0lD5PV/J2HWEUW5430wSAmzKRwAgRtrDpJUq36U GLiDzBbA6p2wESQtlJGVZW0pwXxwykxiEwjvD9X+4TDjIci30xA= =vWw8 -----END PGP SIGNATURE----- --ptre+gHSv7jb5CTV--