From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 227615A0279 for ; Mon, 19 Feb 2024 04:02:18 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1708311735; bh=3K+i/Kf1Mol+oFrRLttBkY9ZDwiVA4KpvZ23H54maOY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=huTbixyirDHFRjkJWuma/r8XUispzE7dOkhLkxqUubjWrlE5/yZLrfZGyCA4zC1Lm XwGIG0YIL+HYJqXGOqT3PVBJ5oK8W2rAzpDrhTcBwgamu4XgdIZqQuuGIoRIBJ8yq8 7bfDWmcXfgRVGRieb8XGGdsC5pqf/HiGa+Hclj44hGRWYXZH7hdJk6rkgOCcq/nBVR mHJdY3fS5Mjn3CrnJZ+OTMWUlYFku4nsqdnOWr4SfcVmgQp4YTLEy8zywZoQpT06+H 0vWP8FKXUAYSctM1MdwLP8MD1vbWLHxsD5X4o4t6LAEQvRkpzh9nDM7iLgShZE4y2q aRknVEayLhz7Q== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TdS4l1Mdwz4wcK; Mon, 19 Feb 2024 14:02:15 +1100 (AEDT) Date: Mon, 19 Feb 2024 14:01:18 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v3 6/9] checksum: use csum_ip4_header() in udp.c and tcp.c Message-ID: References: <20240217150725.661467-1-lvivier@redhat.com> <20240217150725.661467-7-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="RsNt4tBKc0QjBTci" Content-Disposition: inline In-Reply-To: <20240217150725.661467-7-lvivier@redhat.com> Message-ID-Hash: AJL22LBO4RVNS66T7BHSKIQXV5Q665ZR X-Message-ID-Hash: AJL22LBO4RVNS66T7BHSKIQXV5Q665ZR 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: --RsNt4tBKc0QjBTci Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Feb 17, 2024 at 04:07:22PM +0100, Laurent Vivier wrote: > We can find the same function to compute the IPv4 header > checksum in tcp.c, udp.c and tap.c >=20 > Use the function defined for tap.c, csum_ip4_header(), but > with the code used in tcp.c and udp.c as it doesn't need a fully > initialiazed IPv4 header, only protocol, tot_len, saddr and daddr. >=20 > Signed-off-by: Laurent Vivier Reviewed-by: David Gibson > --- >=20 > Notes: > v3: > - function parameters provide tot_len, saddr, daddr and protocol > rather than an iphdr > =20 > v2: > - use csum_ip4_header() from checksum.c > - use code from tcp.c and udp.c in csum_ip4_header() > - use "const struct iphfr *", check is not updated by the > function but by the caller. >=20 > checksum.c | 17 +++++++++++++---- > checksum.h | 3 ++- > tap.c | 3 ++- > tcp.c | 24 +++--------------------- > udp.c | 20 ++------------------ > 5 files changed, 22 insertions(+), 45 deletions(-) >=20 > diff --git a/checksum.c b/checksum.c > index 74e3742bc6f6..511b296a9a80 100644 > --- a/checksum.c > +++ b/checksum.c > @@ -57,6 +57,7 @@ > #include > =20 > #include "util.h" > +#include "ip.h" > #include "checksum.h" > =20 > /* Checksums are optional for UDP over IPv4, so we usually just set > @@ -116,13 +117,21 @@ uint16_t csum_fold(uint32_t sum) > uint16_t csum(const void *buf, size_t len, uint32_t init); > =20 > /** > - * csum_ip4_header() - Calculate and set IPv4 header checksum > + * csum_ip4_header() - Calculate IPv4 header checksum > * @ip4h: IPv4 header > */ > -void csum_ip4_header(struct iphdr *ip4h) > +uint16_t csum_ip4_header(uint16_t tot_len, uint8_t protocol, > + uint32_t saddr, uint32_t daddr) > { > - ip4h->check =3D 0; > - ip4h->check =3D csum(ip4h, (size_t)ip4h->ihl * 4, 0); > + uint32_t sum =3D L2_BUF_IP4_PSUM(protocol); > + > + sum +=3D tot_len; > + sum +=3D (saddr >> 16) & 0xffff; > + sum +=3D saddr & 0xffff; > + sum +=3D (daddr >> 16) & 0xffff; > + sum +=3D daddr & 0xffff; > + > + return ~csum_fold(sum); > } > =20 > /** > diff --git a/checksum.h b/checksum.h > index dfa705a04a24..92db73612b6e 100644 > --- a/checksum.h > +++ b/checksum.h > @@ -13,7 +13,8 @@ struct icmp6hdr; > uint32_t sum_16b(const void *buf, size_t len); > uint16_t csum_fold(uint32_t sum); > uint16_t csum_unaligned(const void *buf, size_t len, uint32_t init); > -void csum_ip4_header(struct iphdr *ip4h); > +uint16_t csum_ip4_header(uint16_t tot_len, uint8_t protocol, > + uint32_t saddr, uint32_t daddr); > void csum_udp4(struct udphdr *udp4hr, > struct in_addr saddr, struct in_addr daddr, > const void *payload, size_t len); > diff --git a/tap.c b/tap.c > index 3ea03f720d6d..2d590e8525a0 100644 > --- a/tap.c > +++ b/tap.c > @@ -160,7 +160,8 @@ static void *tap_push_ip4h(char *buf, struct in_addr = src, struct in_addr dst, > ip4h->protocol =3D proto; > ip4h->saddr =3D src.s_addr; > ip4h->daddr =3D dst.s_addr; > - csum_ip4_header(ip4h); > + ip4h->check =3D csum_ip4_header(ip4h->tot_len, proto, > + src.s_addr, dst.s_addr); > return ip4h + 1; > } > =20 > diff --git a/tcp.c b/tcp.c > index 45ef5146729a..8887656d3ee8 100644 > --- a/tcp.c > +++ b/tcp.c > @@ -934,23 +934,6 @@ static void tcp_sock_set_bufsize(const struct ctx *c= , int s) > trace("TCP: failed to set SO_SNDBUF to %i", v); > } > =20 > -/** > - * tcp_update_check_ip4() - Update IPv4 with variable parts from stored = one > - * @buf: L2 packet buffer with final IPv4 header > - */ > -static void tcp_update_check_ip4(struct tcp4_l2_buf_t *buf) > -{ > - uint32_t sum =3D L2_BUF_IP4_PSUM(IPPROTO_TCP); > - > - sum +=3D buf->iph.tot_len; > - sum +=3D (buf->iph.saddr >> 16) & 0xffff; > - sum +=3D buf->iph.saddr & 0xffff; > - sum +=3D (buf->iph.daddr >> 16) & 0xffff; > - sum +=3D buf->iph.daddr & 0xffff; > - > - buf->iph.check =3D (uint16_t)~csum_fold(sum); > -} > - > /** > * tcp_update_check_tcp4() - Update TCP checksum from stored one > * @buf: L2 packet buffer with final IPv4 header > @@ -1393,10 +1376,9 @@ do { \ > b->iph.saddr =3D a4->s_addr; > b->iph.daddr =3D c->ip4.addr_seen.s_addr; > =20 > - if (check) > - b->iph.check =3D *check; > - else > - tcp_update_check_ip4(b); > + b->iph.check =3D check ? *check : > + csum_ip4_header(b->iph.tot_len, IPPROTO_TCP, > + b->iph.saddr, b->iph.daddr); > =20 > SET_TCP_HEADER_COMMON_V4_V6(b, conn, seq); > =20 > diff --git a/udp.c b/udp.c > index 56b58bd8b43a..cd34f659210b 100644 > --- a/udp.c > +++ b/udp.c > @@ -270,23 +270,6 @@ static void udp_invert_portmap(struct udp_port_fwd *= fwd) > } > } > =20 > -/** > - * udp_update_check4() - Update checksum with variable parts from stored= one > - * @buf: L2 packet buffer with final IPv4 header > - */ > -static void udp_update_check4(struct udp4_l2_buf_t *buf) > -{ > - uint32_t sum =3D L2_BUF_IP4_PSUM(IPPROTO_UDP); > - > - sum +=3D buf->iph.tot_len; > - sum +=3D (buf->iph.saddr >> 16) & 0xffff; > - sum +=3D buf->iph.saddr & 0xffff; > - sum +=3D (buf->iph.daddr >> 16) & 0xffff; > - sum +=3D buf->iph.daddr & 0xffff; > - > - buf->iph.check =3D (uint16_t)~csum_fold(sum); > -} > - > /** > * udp_update_l2_buf() - Update L2 buffers with Ethernet and IPv4 addres= ses > * @eth_d: Ethernet destination address, NULL if unchanged > @@ -614,7 +597,8 @@ static size_t udp_update_hdr4(const struct ctx *c, in= t n, in_port_t dstport, > b->iph.saddr =3D b->s_in.sin_addr.s_addr; > } > =20 > - udp_update_check4(b); > + b->iph.check =3D csum_ip4_header(b->iph.tot_len, IPPROTO_UDP, > + b->iph.saddr, b->iph.daddr); > b->uh.source =3D b->s_in.sin_port; > b->uh.dest =3D htons(dstport); > b->uh.len =3D htons(udp4_l2_mh_sock[n].msg_len + sizeof(b->uh)); --=20 David Gibson | 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 --RsNt4tBKc0QjBTci Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXSxH0ACgkQzQJF27ox 2Gd1gQ/+JVTOKzTX7ZKWE5reWIZKDTqKoLl8Py41gSjdny5xU9NVWJDaagBWxqwH 6ZV6ewzhNLQoiLHFctHM889zOarWMrPme5d/3K6MHyM2PXQ51ELHZ2ivD/K51y7p 02Bus5uW2oYTBWPdXR2brmz1oqqOINKabzYi7yuSvmCsPQYbnRj6yxcEQN8rfOZN dnQwOw7WSbWYaGd37cCIB7BLBRNHH+cqZ6FqXlhkFZ7rUk9K12ujXR4OATGF/m6J gcpvAJEEz5gQCoyyUw+Aj900C2vV4BtB+b60ync6Ua920EwQkrokvsT0KZbA9Vxa CSBWkbf8Eheeiwd/JV5lyD655HaTFaUIsO+9MjkMGs6DAVjchVUnM5rHl93RIy/l OB7a/nwuyihuKwZrzdKoadWD5xW2aVsjlRxUoVLry7U0rvDuGwkwSrIbDvh7MBuv Z0HKhJO4YAXloDM2CqWD7tfsd+SBDe2bwxit/PbULGAqe4Okn/qmr1+mmS5N/CL8 +T+J3391o4/Pm3KDy0ylX/HKJTbOfugx3sWJi8AvKRUQOip73kKNH8qbetVHJJn6 oW1+UpMt+rCV9jWsHwdcAQjEztaCRR8iDFpM2eXmQPjZ1giesX5AsV7VScrGdNbV sWa4UUTktAEK+8UK6kzBIvIuS02XxeE0yZPJij3mqlEXXgEyYyg= =qH6E -----END PGP SIGNATURE----- --RsNt4tBKc0QjBTci--