From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id C07205A004F for ; Wed, 12 Jun 2024 08:27:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1718173630; bh=fR2J/638KnWTVwkB6me2MZgqw/+Uan8R7yaH8LUybR4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=n1SKuKioj43eep/EyJRcw1Bqt9hufBWay4e/VVPQ2s4Tn48MJiwzR4/EWfI/j5tYb /rICG+l/t1uHXpB4YtmxkDNCmA0siXHuiq2DvDxWd35NjUyhp4xlMf+bVKdJ4k39ta Mfuqu/wijh6fjRqOVgWt1D100JDbB4aZVNfO5AEnn2lWS7UfIf5Ny1ViTPJtnwLycB KmIZum8UIZHGQWpfYVaNY39QJG+oBxK8nGRQfzYKGwbT845cWryhRPKByCQmp6FtlR Jmd6pCTzkVGwq9JuhEiXlgIgM+gHyjS5LsXls+4SHLtrcZRYyFkohcPNA+H7uY/Sur LDdDQL8ugEjGg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4VzbDZ3vMVz4wb7; Wed, 12 Jun 2024 16:27:10 +1000 (AEST) Date: Wed, 12 Jun 2024 16:27:04 +1000 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v5 4/8] udp: refactor UDP header update functions Message-ID: References: <20240605152129.1641658-1-lvivier@redhat.com> <20240605152129.1641658-5-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="UIRJtIJtf/GT0he6" Content-Disposition: inline In-Reply-To: <20240605152129.1641658-5-lvivier@redhat.com> Message-ID-Hash: RQKDUO4YZSER5JFOUN6B7QKMLTUNHVTV X-Message-ID-Hash: RQKDUO4YZSER5JFOUN6B7QKMLTUNHVTV 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: --UIRJtIJtf/GT0he6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jun 05, 2024 at 05:21:25PM +0200, Laurent Vivier wrote: > This commit refactors the udp_update_hdr4() and udp_update_hdr6() functio= ns > to improve code portability by replacing the udp_meta_t parameter with > more specific parameters for the IPv4 and IPv6 headers (iphdr/ipv6hdr) > and the source socket address (sockaddr_in/sockaddr_in6). > It also moves the tap_hdr_update() function call inside the udp_tap_send() > function not to have to pass the TAP header to udp_update_hdr4() and > udp_update_hdr6() >=20 > This refactor reduces complexity by making the functions more modular and > ensuring that each function operates on more narrowly scoped data structu= res. > This will facilitate future backend introduction like vhost-user. >=20 > Signed-off-by: Laurent Vivier Reviewed-by: David Gibson With the exception of the trivial nits that Stefano noted. Again, it would be great to get this merged quickly, so I can get my rebasing of the flow table stuff out of the way. > --- > udp.c | 60 +++++++++++++++++++++++++++++++++-------------------------- > 1 file changed, 34 insertions(+), 26 deletions(-) >=20 > diff --git a/udp.c b/udp.c > index 3abafc994537..4295d48046a6 100644 > --- a/udp.c > +++ b/udp.c > @@ -556,7 +556,8 @@ static void udp_splice_sendfrom(const struct ctx *c, = unsigned start, unsigned n, > /** > * udp_update_hdr4() - Update headers for one IPv4 datagram > * @c: Execution context > - * @bm: Pointer to udp_meta_t to update > + * @ip4h: Pre-filled IPv4 header (except for tot_len and saddr) > + * @s_in: Source socket address, filled in by recvmmsg() > * @bp: Pointer to udp_payload_t to update > * @dstport: Destination port number > * @dlen: Length of UDP payload > @@ -565,15 +566,16 @@ static void udp_splice_sendfrom(const struct ctx *c= , unsigned start, unsigned n, > * Return: size of IPv4 payload (UDP header + data) > */ > static size_t udp_update_hdr4(const struct ctx *c, > - struct udp_meta_t *bm, struct udp_payload_t *bp, > + struct iphdr *ip4h, const struct sockaddr_in *s_in, > + struct udp_payload_t *bp, > in_port_t dstport, size_t dlen, > const struct timespec *now) > { > - in_port_t srcport =3D ntohs(bm->s_in.sa4.sin_port); > + in_port_t srcport =3D ntohs(s_in->sin_port); > const struct in_addr dst =3D c->ip4.addr_seen; > - struct in_addr src =3D bm->s_in.sa4.sin_addr; > + struct in_addr src =3D s_in->sin_addr; > size_t l4len =3D dlen + sizeof(bp->uh); > - size_t l3len =3D l4len + sizeof(bm->ip4h); > + size_t l3len =3D l4len + sizeof(*ip4h); > =20 > if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_match) && > IN4_ARE_ADDR_EQUAL(&src, &c->ip4.dns_host) && srcport =3D=3D 53 && > @@ -594,24 +596,24 @@ static size_t udp_update_hdr4(const struct ctx *c, > src =3D c->ip4.gw; > } > =20 > - bm->ip4h.tot_len =3D htons(l3len); > - bm->ip4h.daddr =3D dst.s_addr; > - bm->ip4h.saddr =3D src.s_addr; > - bm->ip4h.check =3D csum_ip4_header(l3len, IPPROTO_UDP, src, dst); > + ip4h->tot_len =3D htons(l3len); > + ip4h->daddr =3D dst.s_addr; > + ip4h->saddr =3D src.s_addr; > + ip4h->check =3D csum_ip4_header(l3len, IPPROTO_UDP, src, dst); > =20 > - bp->uh.source =3D bm->s_in.sa4.sin_port; > + bp->uh.source =3D s_in->sin_port; > bp->uh.dest =3D htons(dstport); > bp->uh.len =3D htons(l4len); > csum_udp4(&bp->uh, src, dst, bp->data, dlen); > =20 > - tap_hdr_update(&bm->taph, l3len + sizeof(udp4_eth_hdr)); > return l4len; > } > =20 > /** > * udp_update_hdr6() - Update headers for one IPv6 datagram > * @c: Execution context > - * @bm: Pointer to udp_meta_t to update > + * @ip6h: Pre-filled IPv6 header (except for payload_len and addresses) > + * @s_in: Source socket address, filled in by recvmmsg() > * @bp: Pointer to udp_payload_t to update > * @dstport: Destination port number > * @dlen: Length of UDP payload > @@ -620,13 +622,14 @@ static size_t udp_update_hdr4(const struct ctx *c, > * Return: size of IPv6 payload (UDP header + data) > */ > static size_t udp_update_hdr6(const struct ctx *c, > - struct udp_meta_t *bm, struct udp_payload_t *bp, > + struct ipv6hdr *ip6h, struct sockaddr_in6 *s_in6, > + struct udp_payload_t *bp, > in_port_t dstport, size_t dlen, > const struct timespec *now) > { > - const struct in6_addr *src =3D &bm->s_in.sa6.sin6_addr; > + const struct in6_addr *src =3D &s_in6->sin6_addr; > const struct in6_addr *dst =3D &c->ip6.addr_seen; > - in_port_t srcport =3D ntohs(bm->s_in.sa6.sin6_port); > + in_port_t srcport =3D ntohs(s_in6->sin6_port); > uint16_t l4len =3D dlen + sizeof(bp->uh); > =20 > if (IN6_IS_ADDR_LINKLOCAL(src)) { > @@ -663,19 +666,18 @@ static size_t udp_update_hdr6(const struct ctx *c, > =20 > } > =20 > - bm->ip6h.payload_len =3D htons(l4len); > - bm->ip6h.daddr =3D *dst; > - bm->ip6h.saddr =3D *src; > - bm->ip6h.version =3D 6; > - bm->ip6h.nexthdr =3D IPPROTO_UDP; > - bm->ip6h.hop_limit =3D 255; > + ip6h->payload_len =3D htons(l4len); > + ip6h->daddr =3D *dst; > + ip6h->saddr =3D *src; > + ip6h->version =3D 6; > + ip6h->nexthdr =3D IPPROTO_UDP; > + ip6h->hop_limit =3D 255; > =20 > - bp->uh.source =3D bm->s_in.sa6.sin6_port; > + bp->uh.source =3D s_in6->sin6_port; > bp->uh.dest =3D htons(dstport); > - bp->uh.len =3D bm->ip6h.payload_len; > + bp->uh.len =3D ip6h->payload_len; > csum_udp6(&bp->uh, src, dst, bp->data, dlen); > =20 > - tap_hdr_update(&bm->taph, l4len + sizeof(bm->ip6h) + sizeof(udp6_eth_hd= r)); > return l4len; > } > =20 > @@ -708,11 +710,17 @@ static void udp_tap_send(const struct ctx *c, > size_t l4len; > =20 > if (v6) { > - l4len =3D udp_update_hdr6(c, bm, bp, dstport, > + l4len =3D udp_update_hdr6(c, &bm->ip6h, > + &bm->s_in.sa6, bp, dstport, > udp6_l2_mh_sock[i].msg_len, now); > + tap_hdr_update(&bm->taph, l4len + sizeof(bm->ip6h) + > + sizeof(udp6_eth_hdr)); > } else { > - l4len =3D udp_update_hdr4(c, bm, bp, dstport, > + l4len =3D udp_update_hdr4(c, &bm->ip4h, > + &bm->s_in.sa4, bp, dstport, > udp4_l2_mh_sock[i].msg_len, now); > + tap_hdr_update(&bm->taph, l4len + sizeof(bm->ip4h) + > + sizeof(udp4_eth_hdr)); > } > tap_iov[i][UDP_IOV_PAYLOAD].iov_len =3D l4len; > } --=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 --UIRJtIJtf/GT0he6 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmZpP7gACgkQzQJF27ox 2GcStA/8Cdcg/BtKrROkf6rIvPUtMPrLvODZQGfDLSKsnrIk4kVTnx3+b+6JpCKl LN5qnJEZd8nkO+dSfJe9PnHWkdcSLMs4EW0RVxXgmHfbxr3rCosyIoJox1HDRTsd 8WWWz6yGoIh/GvbqTvM+W6ZSmDPotxCZYptX3zGfbrVIkRhhdXuqjkH8nqqXl5Km Cpgmo6gRXsdDsoyxvthvIAqLeO4T7CrH/q4gK48iQxtdBDS5IBzBmBjZ6/XRGVCn eH3yXs3ck65kJdevRwC4V1aBgZMpHE7RzR0GXg0+hFnX+Bg3Hua6HpWVsybI2mjZ IzKEnbOr+W/xVgAQORbpvLbPvnWts+ZKr9yL6qRgNJNqVj9UuxX/p3FAp3R6BqZZ FLx5VC0BZKFFMpYzNA87HYqAb2mZ83Psq/sBh9XDUzbSNN7FJeNxuQ2KENp4txXZ Mw0f6aN24onxYwv8SHIcdGc97OJ3aiPp3YTcCtzPkFIg1A+VYV/ou1+dC79k7cqT muH+gQLqQcbzhxEVQldgyMLlHzzX8sMOE3ILbB7AfsoSaWPcQJe+f0xNi50ralfi OeqTuSbaYMZVPlWioq++qEBelhCK1yacmenlEG92WetfwRGc1qNi40H4woZKbWM/ 1a7UJez86SjggpCPeNilDtvg8ezk13NB03C7TQo5MjOgCxvvbtY= =Nf1r -----END PGP SIGNATURE----- --UIRJtIJtf/GT0he6--