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=SyhbENKB; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 1A0805A0262 for ; Tue, 24 Mar 2026 04:54:09 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1774324445; bh=LRod9oJVD3Gwd38d0IAt1uxJVH+G5CQTK+vOPZLTjVo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=SyhbENKBD2sgo4996S/6i+aNcCcs5B/iQdnuL/a93PhXcYZzViubbdCK28NXk/WI0 aVnO2kdd1H0HRxNWocRJhrjHBqUqnhOaxRaf4fMDzPkimk7sGCq+fvYi0CHjfI2GbM ghBof5l904W4pm8lnbq3Y8H/mz5ank5rDaoy+4CPpUeUdt9R5c89Ru45HMWBqf6IBA haQWxUYP8kDmDQbnBxdKo00Qyy0txCdSQ1iilNv90iMo18h2Cm/dFDJOTUFAP7UV64 DPuaZ+Zkxkb3Jo6041RvKdVRF2/r4h0Sk0Eo1csQ2fxHkLh5Tx1PSKs66i8oq4PHdM nT8VCfyspOJPg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4ffx3x1Npkz4wCG; Tue, 24 Mar 2026 14:54:05 +1100 (AEDT) Date: Tue, 24 Mar 2026 14:53:58 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH 1/7] tcp: pass ipv4h checksum, not a pointer to the checksum Message-ID: References: <20260323165259.1253482-1-lvivier@redhat.com> <20260323165259.1253482-2-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="Y8upnGnAoYwIG5Y2" Content-Disposition: inline In-Reply-To: <20260323165259.1253482-2-lvivier@redhat.com> Message-ID-Hash: UWXS64TQYIIGQHURDN75I734VNJN7CCK X-Message-ID-Hash: UWXS64TQYIIGQHURDN75I734VNJN7CCK 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: --Y8upnGnAoYwIG5Y2 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Mar 23, 2026 at 05:52:53PM +0100, 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. 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 dangling after the with_header() block. Oof, that kind of indicates the dangers with the with_header() structure. Is that change merged already? If not we should probably fix it before merge rather than after the fact. > Pass the checksum by value as an int instead, using -1 as the sentinel > to indicate that the checksum should be computed from scratch (replacing > the NULL pointer sentinel). As the checksum is a uint16_t, -1 cannot be > a valid checksum value in an int. That said, passing this by value I think is cleaner than the pointer, regardless of other reasons. Would it also make sense to flag no_tcp_csum with an additional special value (a #define to -2, or whatever) instead of using an extra parameter? Logically the checksum parameter would be: CALCULATE | UNNEEDED | specific value Reviewed-by: David Gibson > Signed-off-by: Laurent Vivier > --- > tcp.c | 7 +++---- > tcp_buf.c | 10 +++++----- > tcp_internal.h | 3 +-- > tcp_vu.c | 12 ++++++------ > 4 files changed, 15 insertions(+), 17 deletions(-) >=20 > diff --git a/tcp.c b/tcp.c > index b14586249c4e..158a5be0327e 100644 > --- a/tcp.c > +++ b/tcp.c > @@ -953,8 +953,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, > - const uint16_t *ip4_check, uint32_t seq, > - bool no_tcp_csum) > + int ip4_check, uint32_t seq, bool no_tcp_csum) > { > const struct flowside *tapside =3D TAPFLOW(conn); > size_t l4len =3D iov_tail_size(payload) + sizeof(*th); > @@ -974,8 +973,8 @@ size_t tcp_fill_headers(const struct ctx *c, struct t= cp_tap_conn *conn, > ip4h->saddr =3D src4->s_addr; > ip4h->daddr =3D dst4->s_addr; > =20 > - if (ip4_check) > - ip4h->check =3D *ip4_check; > + if (ip4_check !=3D -1) > + ip4h->check =3D ip4_check; > else > ip4h->check =3D csum_ip4_header(l3len, IPPROTO_TCP, > *src4, *dst4); > diff --git a/tcp_buf.c b/tcp_buf.c > index 41965b107567..bc0f58dd7a5e 100644 > --- a/tcp_buf.c > +++ b/tcp_buf.c > @@ -172,7 +172,7 @@ static void tcp_l2_buf_pad(struct iovec *iov) > */ > static void tcp_l2_buf_fill_headers(const struct ctx *c, > struct tcp_tap_conn *conn, > - struct iovec *iov, const uint16_t *check, > + struct iovec *iov, int check, > uint32_t seq, bool no_tcp_csum) > { > struct iov_tail tail =3D IOV_TAIL(&iov[TCP_IOV_PAYLOAD], 1, 0); > @@ -233,7 +233,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, -1, seq, false); > =20 > tcp_l2_buf_pad(iov); > =20 > @@ -270,7 +270,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; > + int check =3D -1; > struct iovec *iov; > =20 > conn->seq_to_tap =3D seq + dlen; > @@ -279,9 +279,9 @@ static void tcp_data_to_tap(const struct ctx *c, stru= ct 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; > + check =3D iph->check; > } > iov[TCP_IOV_IP] =3D IOV_OF_LVALUE(tcp4_payload_ip[tcp_payload_used]); > } else if (CONN_V6(conn)) { > diff --git a/tcp_internal.h b/tcp_internal.h > index d9408852571f..bb7a6629839c 100644 > --- a/tcp_internal.h > +++ b/tcp_internal.h > @@ -187,8 +187,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, > - const uint16_t *ip4_check, uint32_t seq, > - bool no_tcp_csum); > + int ip4_check, uint32_t seq, bool no_tcp_csum); > =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 3001defb5467..a21ee3499aed 100644 > --- a/tcp_vu.c > +++ b/tcp_vu.c > @@ -138,7 +138,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, > - NULL, seq, !*c->pcap); > + -1, seq, !*c->pcap); > =20 > if (*c->pcap) > pcap_iov(&flags_elem[0].in_sg[0], 1, VNET_HLEN); > @@ -283,7 +283,7 @@ static ssize_t tcp_vu_sock_recv(const struct ctx *c, = struct vu_virtq *vq, > */ > static void tcp_vu_prepare(const struct ctx *c, struct tcp_tap_conn *con= n, > struct iovec *iov, size_t iov_cnt, > - const uint16_t **check, bool no_tcp_csum, bool push) > + int *check, bool no_tcp_csum, bool push) > { > const struct flowside *toside =3D TAPFLOW(conn); > bool v6 =3D !(inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr)); > @@ -329,7 +329,7 @@ static void tcp_vu_prepare(const struct ctx *c, struc= t tcp_tap_conn *conn, > tcp_fill_headers(c, conn, eh, ip4h, ip6h, th, &payload, > *check, conn->seq_to_tap, no_tcp_csum); > if (ip4h) > - *check =3D &ip4h->check; > + *check =3D ip4h->check; > } > =20 > /** > @@ -350,7 +350,7 @@ int tcp_vu_data_from_sock(const struct ctx *c, struct= tcp_tap_conn *conn) > size_t hdrlen, fillsize; > int v6 =3D CONN_V6(conn); > uint32_t already_sent; > - const uint16_t *check; > + int check; > =20 > if (!vu_queue_enabled(vq) || !vu_queue_started(vq)) { > debug("Got packet, but RX virtqueue not usable yet"); > @@ -437,7 +437,7 @@ int tcp_vu_data_from_sock(const struct ctx *c, struct= 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++) { > + for (i =3D 0, previous_dlen =3D -1, check =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); > @@ -451,7 +451,7 @@ int tcp_vu_data_from_sock(const struct ctx *c, struct= tcp_tap_conn *conn) > =20 > /* The IPv4 header checksum varies only with dlen */ > if (previous_dlen !=3D dlen) > - check =3D NULL; > + check =3D -1; > previous_dlen =3D dlen; > =20 > tcp_vu_prepare(c, conn, iov, buf_cnt, &check, !*c->pcap, push); > --=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 --Y8upnGnAoYwIG5Y2 Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmnCCtUACgkQzQJF27ox 2GfquQ//YYCWSV8c2da7fT59+c59nBRzNdCsM297iQESWAT7On9ygPbBGbqygF+h CIYG+/Yr4YP1wBt5b6bH+kAmrvp80qtJSzmE2by+36KmWKBjm6Cf2iALww4gHxLQ zGjPC59E74GhoKM8w1J+DNf8/tMnNAS0J9aTIE/XzylJ4IPE6ZvkZ3QBxUPO5Hx0 SGhOfkNW29uTPbSkK97+/FX6vgeKRwvjRs63dRxS0TUoCRELD/1xh8sSm64BcWsF JmX14UWjwZz0wTFQ62zFN47ivmUTEB/9m13zlfW1uczgZclDKbta/n6OraadUbFG LrQe8nJpkp4SGs/9gdTm+hvlR4rcCbbFCuShdezNgDhvmfVPIUeyvYo0YwtL+P5n uWDU1c6akP0XxptpMj3gxAmxe1UMSv9FaZke78NgrOp+UQ2sRk0dGJJNwv/18t8X QCEYmIBNhxQajIZH2yF5mfuqSOLhtU9o7mtGqyLgpwJFcltq6BtUl38V+7Sy7JDn vSTiedn7thkUgdn1YlCDKlx6lQV1UysuaVdp0Ubu5ly3bd4QZJXKTlBqXcDfWLqT 2YCbanOaJhrYTn/2O78z75O610yovIidNR4zIrVMe8HuXavcd58jMsqjrVJYAxar 1h7PjXYnSnYIMcg2+M/SJUo0tssFCy2EZPUfpfAITk0GBjcnhXM= =JqNC -----END PGP SIGNATURE----- --Y8upnGnAoYwIG5Y2--