From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id AB97C5A027A for ; Fri, 1 Mar 2024 00:42:23 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709250137; bh=GZETV6jEeeC+Etip2kkVFvprWOpFpNYoDiMVCQGCXIY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=r71fEy8uMjbtlu53reyUL5nMyI6Geglbjoj5nunWSfD0UTdx8aP0GlqaMwAR3q/cI ymoDzgEN15nt3WRbeIK/IgDAELIqghkteEEMaG4vezWvQcZViMtUKE/RXA8lPlps5C hWZSphOkjzBYTZVS5+8udmFhcMxxu8JqUETkuWQ2Y9sbjvejlIfaSpa3R60XKZzseC ItOdiU6HS9tY57sV1G6MUnjSGXZsiSL8J4HP2b8g/iP3NmR6K3fvf1Ij/9s3g7SrcY t1iaxaybPkwO4gTdmBGsK3whhewQsQQOdvn1mZowS1y5oXLNbPFdP2nX7p2ZF6W95b /0ccvmwIKhVcg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Tm76x2QCxz4wcl; Fri, 1 Mar 2024 10:42:17 +1100 (AEDT) Date: Fri, 1 Mar 2024 10:25:02 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v4 5/8] checksum: use csum_ip4_header() in udp.c and tcp.c Message-ID: References: <20240229165955.829476-1-lvivier@redhat.com> <20240229165955.829476-6-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="tgHT7gZ8bKwJ38vh" Content-Disposition: inline In-Reply-To: <20240229165955.829476-6-lvivier@redhat.com> Message-ID-Hash: MOK6QV7USYTIBLZR32MS23IPJD6B3X7T X-Message-ID-Hash: MOK6QV7USYTIBLZR32MS23IPJD6B3X7T 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: --tgHT7gZ8bKwJ38vh Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Feb 29, 2024 at 05:59:52PM +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: > v4: > - rebase > =20 > 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 Function comment needs to be updated for the new parameters. In particular it needs to note that tot_len, saddr and daddr are all passed in network order. As noted elsewhere, I kind of hate passing non-host-endian values in plain integer types, but I can see why doing otherwise here would be very awkward. > */ > -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 d35d8944fc41..d4649f0167ab 100644 > --- a/tap.c > +++ b/tap.c > @@ -161,7 +161,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 e0588f92e65f..ea0802c6b102 100644 > --- a/tcp.c > +++ b/tcp.c > @@ -935,23 +935,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 > @@ -1394,10 +1377,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 26774df7018c..d517c99dcc69 100644 > --- a/udp.c > +++ b/udp.c > @@ -275,23 +275,6 @@ static void udp_invert_portmap(struct udp_fwd_ports = *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 > @@ -619,7 +602,8 @@ static size_t udp_update_hdr4(const struct ctx *c, in= t n, in_port_t dstport, > b->iph.saddr =3D src->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 --tgHT7gZ8bKwJ38vh Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXhEk0ACgkQzQJF27ox 2GcRUhAAnJl9x9+QhG4JWc+PyLdSf60E33e7taCYK5m7i3azCDwKyGLj6E6dUplV 5QB6k0DWEOY7G5MQCUFMezL6/CApPMlOSUaXm9Nbl+IVhAs7fS+Lb8w4RdQ/Tnlr LWlkEE+867wfEAtjBHotIBPkw5SZ1Xzzdb6fojZNJzFkHyAhf5U6/bksCitm+U2B HPrkhuEFAK+0WJbwWb6FP8dEgs6plGedacUW0alxcTjYaYX47mKWefyYj2QKyC4E kRNi8kaZVMiduETVOYmzm3yk5t9yjFs1bLWdzQu3Vx+q82/eHrpC06in6ZjpFEEY Cb5/Ne6egpI2B6aDcuH5X8/tBHzsfuh4EtImmSsiumq+MmB4RvASfSKxDLWJvwXe +fPTdTJc4yW4KUVuGlLwDbFRCalc3ctX0TkRcuQF2ebJndd/W6X+SH9QFj14uHxz 6+ORYxm3mreKDx+HaVeN5OfNuMy9T1lo8JpK/VQgrkOTgxGc4DpgkQVSK6Cao8HG urmi+MheKs3ljSUIlAt/1eeucBYIUGwkRqfFryJLa/SNAvovGCyFn4C2LL6ZwA7+ ZA8EI0B5u2Se8lNbnDokyMNkABtB7GpNK1+QCEJycd+QLrs9U3404jSU6z0cPnI6 99MmRCMy4gBuJS84VKo29DRSTg5b/PUfXI4y1cpGWJADjLKvMVc= =dMT5 -----END PGP SIGNATURE----- --tgHT7gZ8bKwJ38vh--