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=202408 header.b=kVnoDP4S; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id B86CF5A004E for ; Wed, 25 Sep 2024 03:13:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202408; t=1727226789; bh=xYyfvNAX4N05ZzfnZGuYtHCBRGCE0AaAYh0+yByOKPM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=kVnoDP4ShASoYdESwEpNwzRLoXLpyhg4pQ6BtxP4gXA6vyFVS+I5wKKbfttz3Wbk+ dJyTRq7LxwbtsoyqZSeOmycBcsm1PpOrNqLmVmrJuHY0tcJQru6e9HCjAC5iYVsUVy ChH5svd4mifYhsMSVYBps2vtiCFkN1J6jmCNw/2cMsaQdN1K8Rd9cU0sH3oZhWd5kP IOfC7RV84BZexRKx8PqJjcmjsBMoqvo+BQUHDIcDvQ6iE9d4ZnFZyQSRo3cCDje6Dy pR/03SXTBcMGf2E+9NdVOItnX1sRxO81gjy2DnnPLB8Gv8msYDQ6gYWu5GcTV12RiB xeHaoUm+QiaWA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4XCzHn3VRGz4xN4; Wed, 25 Sep 2024 11:13:09 +1000 (AEST) Date: Wed, 25 Sep 2024 11:12:59 +1000 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH 4/4] tcp: Update TCP checksum using an iovec array Message-ID: References: <20240924154642.182857-1-lvivier@redhat.com> <20240924154642.182857-5-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="lZR47QeB4sKJX54m" Content-Disposition: inline In-Reply-To: <20240924154642.182857-5-lvivier@redhat.com> Message-ID-Hash: APWGT6TXSCFZ5I5NECNZDEQAVV54GWQ7 X-Message-ID-Hash: APWGT6TXSCFZ5I5NECNZDEQAVV54GWQ7 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: --lZR47QeB4sKJX54m Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Sep 24, 2024 at 05:46:42PM +0200, Laurent Vivier wrote: > TCP header and payload are supposed to be in the same buffer, > and tcp_update_check_tcp4()/tcp_update_check_tcp6() compute > the checksum from the base address of the header using the > length of the IP payload. >=20 > In the future (for vhost-user) we need to dispatch the TCP header and > the TCP payload through several buffers. To be able to manage that, we > provide an iovec array that points to the data of the TCP frame. > We provide also an offset to be able to provide an array that contains > the TCP frame embedded in an lower level frame, and this offset points > to the TCP header inside the iovec array. >=20 > Signed-off-by: Laurent Vivier > --- > checksum.c | 1 - > tcp.c | 100 ++++++++++++++++++++++++++++++++++++++--------------- > 2 files changed, 72 insertions(+), 29 deletions(-) >=20 > diff --git a/checksum.c b/checksum.c > index f80db4d309a2..96ccfe2af50b 100644 > --- a/checksum.c > +++ b/checksum.c > @@ -503,7 +503,6 @@ uint16_t csum(const void *buf, size_t len, uint32_t i= nit) > * > * Return: 16-bit folded, complemented checksum > */ > -/* cppcheck-suppress unusedFunction */ > uint16_t csum_iov(const struct iovec *iov, size_t n, size_t offset, > uint32_t init) > { > diff --git a/tcp.c b/tcp.c > index c9472d905520..efd4037ed008 100644 > --- a/tcp.c > +++ b/tcp.c > @@ -755,36 +755,65 @@ static void tcp_sock_set_bufsize(const struct ctx *= c, int s) > } > =20 > /** > - * tcp_update_check_tcp4() - Update TCP checksum from stored one > - * @iph: IPv4 header > - * @bp: TCP header followed by TCP payload > - */ > -static void tcp_update_check_tcp4(const struct iphdr *iph, > - struct tcp_payload_t *bp) > + * tcp_update_check_tcp4() - Calculate TCP checksum for IPv6 > + * @src: IPv4 source address > + * @dst: IPv4 destination address > + * @iov: Pointer to the array of IO vectors > + * @iov_cnt: Length of the array > + * @payload_offset: IPv4 payload offset in the iovec array You explain it here, but "payload_offset" is a bit unclear if you're not sure which layer it's talking about. "l4offset" maybe? > + */ > +void tcp_update_check_tcp4(struct in_addr src, > + struct in_addr dst, > + const struct iovec *iov, int iov_cnt, > + size_t payload_offset) > { > - uint16_t l4len =3D ntohs(iph->tot_len) - sizeof(struct iphdr); > - struct in_addr saddr =3D { .s_addr =3D iph->saddr }; > - struct in_addr daddr =3D { .s_addr =3D iph->daddr }; > - uint32_t sum =3D proto_ipv4_header_psum(l4len, IPPROTO_TCP, saddr, dadd= r); > + size_t check_ofs; > + __sum16 *check; What's a __sum16? > + int check_idx; > + uint32_t sum; > + > + sum =3D proto_ipv4_header_psum(iov_size(iov, iov_cnt) - payload_offset, > + IPPROTO_TCP, src, dst); > + > + check_idx =3D iov_skip_bytes(iov, iov_cnt, > + payload_offset + offsetof(struct tcphdr, check), > + &check_ofs); > + > + check =3D (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs); So.. it's not likely, but it's possible for the first byte of the checksum to be in one iovec and the second byte in another. This whole construction is a bit awkward too. I think we want another helper on top of iov_skip_bytes(). It would retreive a pointer to a field of a given length and offset within the IOV, returning NULL if that can't be found contiguously. It could have a macro wrapper that fills in some of the details based on a type. For now I'd imagine we just give up if it returns NULL, but that's enough to reduce a potential out of bounds memory access to merely breaking one connection. If we ever need it, we can add a slow path to handle that case. There are a couple of other curly cases to consider too, alas: what if the field you request does exist contiguously, but isn't properly aligned for the type we want to access it as? Then there's the question of whether doing this will run afoul of the type-based aliasing rules. > - bp->th.check =3D 0; > - bp->th.check =3D csum(bp, l4len, sum); > + *check =3D 0; > + *check =3D csum_iov(iov, iov_cnt, payload_offset, sum); > } > =20 > /** > * tcp_update_check_tcp6() - Calculate TCP checksum for IPv6 > - * @ip6h: IPv6 header > - * @bp: TCP header followed by TCP payload > - */ > -static void tcp_update_check_tcp6(const struct ipv6hdr *ip6h, > - struct tcp_payload_t *bp) > + * @src: IPv6 source address > + * @dst: IPv6 destination address > + * @iov: Pointer to the array of IO vectors > + * @iov_cnt: Length of the array > + * @payload_offset: IPv6 payload offset in the iovec array > + */ > +void tcp_update_check_tcp6(const struct in6_addr *src, > + const struct in6_addr *dst, > + const struct iovec *iov, int iov_cnt, > + size_t payload_offset) > { > - uint16_t l4len =3D ntohs(ip6h->payload_len); > - uint32_t sum =3D proto_ipv6_header_psum(l4len, IPPROTO_TCP, > - &ip6h->saddr, &ip6h->daddr); > + size_t check_ofs; > + __sum16 *check; > + int check_idx; > + uint32_t sum; > + > + sum =3D proto_ipv6_header_psum(iov_size(iov, iov_cnt) - payload_offset, > + IPPROTO_TCP, src, dst); > + > + check_idx =3D iov_skip_bytes(iov, iov_cnt, > + payload_offset + offsetof(struct tcphdr, check), > + &check_ofs); > + > + check =3D (__sum16 *)((char *)iov[check_idx].iov_base + check_ofs); > =20 > - bp->th.check =3D 0; > - bp->th.check =3D csum(bp, l4len, sum); > + *check =3D 0; > + *check =3D csum_iov(iov, iov_cnt, payload_offset, sum); > } > =20 > /** > @@ -935,10 +964,18 @@ static size_t tcp_fill_headers4(const struct tcp_ta= p_conn *conn, > =20 > tcp_fill_header(&bp->th, conn, seq); > =20 > - if (no_tcp_csum) > + if (no_tcp_csum) { > bp->th.check =3D 0; > - else > - tcp_update_check_tcp4(iph, bp); > + } else { > + const struct iovec iov =3D { > + .iov_base =3D bp, > + .iov_len =3D ntohs(iph->tot_len) - sizeof(struct iphdr), > + }; > + struct in_addr saddr =3D { .s_addr =3D iph->saddr }; > + struct in_addr daddr =3D { .s_addr =3D iph->daddr }; > + > + tcp_update_check_tcp4(saddr, daddr, &iov, 1, 0); > + } > =20 > tap_hdr_update(taph, l3len + sizeof(struct ethhdr)); > =20 > @@ -980,10 +1017,17 @@ static size_t tcp_fill_headers6(const struct tcp_t= ap_conn *conn, > =20 > tcp_fill_header(&bp->th, conn, seq); > =20 > - if (no_tcp_csum) > + if (no_tcp_csum) { > bp->th.check =3D 0; > - else > - tcp_update_check_tcp6(ip6h, bp); > + } else { > + const struct iovec iov =3D { > + .iov_base =3D bp, > + .iov_len =3D ntohs(ip6h->payload_len) > + }; > + > + tcp_update_check_tcp6(&ip6h->saddr, &ip6h->daddr, > + &iov, 1, 0); > + } > =20 > tap_hdr_update(taph, l4len + sizeof(*ip6h) + sizeof(struct ethhdr)); > =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 --lZR47QeB4sKJX54m Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmbzY5oACgkQzQJF27ox 2GdNmw//Y2BpHZ+edAhLjmTOcWRh/ZzYHTMqsKHv1n+qpqWseAZ6T46V99438DDD IKgaOOf9kNNP+tCpTdD8lFf3fxw5xCBTWVKSPW3aD2a4H4UXJoZsYvTluHt2pKqr yf/61glgDHa/ZWgL/q3fL5xi+/sYg7B6U800+AITIxly9FjU4F2JyQi5HQ7ApqTI lzphvKQwHkLnW9K1eKCR/bQF5+0/69dUFx1WqXQfu9DWTXbeEe83bNrVNGalrZkK X7fwz/rYHFttQICBysIbpqYNtUw1ZabdDPvVkPDnwjLoSLyelpT0l6oEWa7L30BP JMvBrs1GBqu8Nen+im8C7zw6NnlS6bE7+qlC/ZvvKWlcrwCp2f68G/pyhA4DB4lq HJoo3GCCHHz40Fz4TJoos/O4suydOQd4ZCaGmGb65L9KxgIpjhtJ8Z7GYRUkfctY FkSWg6HfSuDHh8B6kHXcB4nX8S3+yRLY3xyMwtp3no4OeVHqT33T+y9OuJZ+ikKG y6Y6euxN4fUNPhvTskRd8A6E8ApGuaATlxxQELYWvU79VCAzMcYQ86GzwPKkIVpa FR3YlTSNZywHCo9TXpQ2PsGw4entlB6zpHCHNRc64L7lUNtrYWqg42EBweSLkZjq XvtuauP6bExIIE7OnzH/mH6DiltPtF3aUdVEnq1/uEOSBFEY1MI= =w8bP -----END PGP SIGNATURE----- --lZR47QeB4sKJX54m--