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 1E7705A0272 for ; Mon, 5 Feb 2024 07:21:02 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1707114057; bh=3Rqo+EXYCgc3Xd/9fPszsAi3q8HqePpoyTVSKJKCZtA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=GTRUwLF21XGJvkteYDPa9YGlrIoHOeMMgYPmpmhNLcoDa13WVbd8/8j3FKbSi3fls ITrOD9R7Puq5zaTO4O6D6BDct0n0nkdGZR0eRWouMAod6cHPDi0Lng6GR+iz49irMU 1p+PTYVUREv5UtVPVR+7s62ua/Ozrynhb9zRY9yVz9NvbTGekQQgheQjqr/LidPXDg B1SFS0fmJCJey3B/5VsCN4PdH3CNQpomU67FY3TtsLMXZ6NG4VhXjhJyE3oK1FJVXs wBMAcVm5eE9FEZ08rsHq+NOhRplbtJmkrgHv5Fe3lNfPTXZSsolwcjcmz5p7Hi+QYH KsygjMOkse+cg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TSx8T6mpfz4wxx; Mon, 5 Feb 2024 17:20:57 +1100 (AEDT) Date: Mon, 5 Feb 2024 17:16:16 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH 06/24] ip: move duplicate IPv4 checksum function to ip.h Message-ID: References: <20240202141151.3762941-1-lvivier@redhat.com> <20240202141151.3762941-7-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="ZJO/rWV6dpD/Nf77" Content-Disposition: inline In-Reply-To: <20240202141151.3762941-7-lvivier@redhat.com> Message-ID-Hash: 3CRIMV4ZHU73P76X76BFXLJAWRW5SU64 X-Message-ID-Hash: 3CRIMV4ZHU73P76X76BFXLJAWRW5SU64 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: --ZJO/rWV6dpD/Nf77 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Feb 02, 2024 at 03:11:33PM +0100, Laurent Vivier wrote: > We can find the same function to compute the IPv4 header > checksum in tcp.c and udp.c >=20 > Signed-off-by: Laurent Vivier Reviewed-by: David Gibson Nice clean up. > --- > ip.h | 14 ++++++++++++++ > tcp.c | 23 ++--------------------- > udp.c | 19 +------------------ > 3 files changed, 17 insertions(+), 39 deletions(-) >=20 > diff --git a/ip.h b/ip.h > index b2e08bc049f3..ff7902c45a95 100644 > --- a/ip.h > +++ b/ip.h > @@ -9,6 +9,8 @@ > #include > #include > =20 > +#include "checksum.h" > + > #define IN4_IS_ADDR_UNSPECIFIED(a) \ > ((a)->s_addr =3D=3D htonl_constant(INADDR_ANY)) > #define IN4_IS_ADDR_BROADCAST(a) \ > @@ -83,4 +85,16 @@ struct ipv6_opt_hdr { > =20 > char *ipv6_l4hdr(const struct pool *p, int idx, size_t offset, uint8_t *= proto, > size_t *dlen); > +static inline uint16_t ipv4_hdr_checksum(struct iphdr *iph, int proto) > +{ > + uint32_t sum =3D L2_BUF_IP4_PSUM(proto); > + > + sum +=3D iph->tot_len; > + sum +=3D (iph->saddr >> 16) & 0xffff; > + sum +=3D iph->saddr & 0xffff; > + sum +=3D (iph->daddr >> 16) & 0xffff; > + sum +=3D iph->daddr & 0xffff; > + > + return ~csum_fold(sum); > +} > #endif /* IP_H */ > diff --git a/tcp.c b/tcp.c > index 4c9c5fb51c60..293ab12d8c21 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,8 @@ 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 : > + ipv4_hdr_checksum(&b->iph, IPPROTO_TCP); > =20 > SET_TCP_HEADER_COMMON_V4_V6(b, conn, seq); > =20 > diff --git a/udp.c b/udp.c > index d514c864ab5b..6f867df81c05 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,7 @@ 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 ipv4_hdr_checksum(&b->iph, IPPROTO_UDP); > 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 --ZJO/rWV6dpD/Nf77 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXAfS8ACgkQzQJF27ox 2GdHXw/+Irxk5yCZinhkM/7vr4o7EJ0wu8x/pvfBoGJdvS7NOaWf9XWnwhVTb4H4 7zCkNAUWTqVJ4QADJFx53hWNR069fSuDSV7ZDjSr576vw86crzyeSJYD3CaILs0V aLu71KpPYUfrvJ6RqVny+FHCTYwCBNelIlOqvEtVwahqNvoxa0Bnbr9D/YZtnhk+ aHs5V37AfC8VsutRCqOtc8Rca4IaTfq1oOFnawVjCElfsdiC7t6F8Uig1fX9R5GR SvXyOHb9LBb/0Xi+yNSxaYALDBT5UH/8HRtQTpJNk5+r9J/N/C9PFYp19iB3gl6/ UTevIQpBwdlLf8RE7FKalTsXVqCQsCL2JByyT0dqtznVDCxbst3MssWyONghZ50O Jiiom9U3cSD5dz7/iAiJARPgXUrjP0yb6KhI1YqxeVoK+srBSE8b6SJB2EJEnfVR e7GZaa8qsuxJs8cRKp5IuWOPb22mBlZotlgUmgFFoUp8Lxh6ipHuACubXSWbFJrv a9gDAaB9p9LN70KhSwVf9hv8oJmDf2m9WVDaanl98SCkF9GQeKf9/wDsfmhKviog fDCrPf0NghdykinRZje49WYc+hKRiAZ8XTffxBmqrOBYb8QL/OPQAPpQdpBq+PG3 Vjqw1drUqfpvaCCU6ZcR3iVzLrTchx1hXFwMfmi0ZU35g69nVJ8= =9w/X -----END PGP SIGNATURE----- --ZJO/rWV6dpD/Nf77--