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 D3D275A0272 for ; Tue, 6 Feb 2024 03:00:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1707184826; bh=kv5ajS3CZg2Ve0n4iD5vqmAmMEWPA9RIWFevlXDZp5M=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=jhDgtG/4RsWbgqJWSS7HMvRg4K6kK0QoZ/lUda64fgAuqR1FLiHJaffPRRjZf5Xx3 Rs4mqHVb4ZN8K5tRHrR52vlPkHL7BRc0sCfzP8sIklR38V76s80G5ebE+HT6Gpx1Yl 6jFUVoEf8b4M2F7Imu0xznPpbrqQjlh3cmLcavGXU0WoPvcd8uN2aaG4No/JfEIyRd 2e/j1q+vnrJAA07Zr3G9/57L+AiPJMDupPz7nYlsfjxCSYRhVFKFi/YFC2omLrnCmd 2xl+2noHBogXWGkoFrtu4pLpzNt9vskb+0Lbtyj3A2X1Ijugv+1+dnvSvvzZeBHE/k eecApCuHEcr9g== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TTRKQ6WzKz4wd0; Tue, 6 Feb 2024 13:00:26 +1100 (AEDT) Date: Tue, 6 Feb 2024 12:59:50 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH 14/24] udp: move udpX_l2_buf_t and udpX_l2_mh_sock out of udp_update_hdrX() Message-ID: References: <20240202141151.3762941-1-lvivier@redhat.com> <20240202141151.3762941-15-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="2JAwek4KOgG3N4zC" Content-Disposition: inline In-Reply-To: <20240202141151.3762941-15-lvivier@redhat.com> Message-ID-Hash: ZK4UJH2MM7TXHFF75KNFGD6YPOXQQ7BS X-Message-ID-Hash: ZK4UJH2MM7TXHFF75KNFGD6YPOXQQ7BS 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: --2JAwek4KOgG3N4zC Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Feb 02, 2024 at 03:11:41PM +0100, Laurent Vivier wrote: Commit message please. > Signed-off-by: Laurent Vivier > --- > udp.c | 126 ++++++++++++++++++++++++++++++++++------------------------ > 1 file changed, 73 insertions(+), 53 deletions(-) >=20 > diff --git a/udp.c b/udp.c > index db635742319b..77168fb0a2af 100644 > --- a/udp.c > +++ b/udp.c > @@ -562,47 +562,48 @@ static void udp_splice_sendfrom(const struct ctx *c= , unsigned start, unsigned n, > * > * Return: size of tap frame with headers > */ > -static size_t udp_update_hdr4(const struct ctx *c, int n, in_port_t dstp= ort, > - const struct timespec *now) > +static size_t udp_update_hdr4(const struct ctx *c, struct iphdr *iph, > + size_t data_len, struct sockaddr_in *s_in, > + in_port_t dstport, const struct timespec *now) This is a much better interface, nice change. > { > - struct udp4_l2_buf_t *b =3D &udp4_l2_buf[n]; > + struct udphdr *uh =3D (struct udphdr *)(iph + 1); > in_port_t src_port; > size_t ip_len; > =20 > - ip_len =3D udp4_l2_mh_sock[n].msg_len + sizeof(b->iph) + sizeof(b->uh); > + ip_len =3D data_len + sizeof(struct iphdr) + sizeof(struct udphdr); > =20 > - b->iph.tot_len =3D htons(ip_len); > - b->iph.daddr =3D c->ip4.addr_seen.s_addr; > + iph->tot_len =3D htons(ip_len); > + iph->daddr =3D c->ip4.addr_seen.s_addr; > =20 > - src_port =3D ntohs(b->s_in.sin_port); > + src_port =3D ntohs(s_in->sin_port); > =20 > if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.dns_match) && > - IN4_ARE_ADDR_EQUAL(&b->s_in.sin_addr, &c->ip4.dns_host) && > + IN4_ARE_ADDR_EQUAL(&s_in->sin_addr, &c->ip4.dns_host) && > src_port =3D=3D 53) { > - b->iph.saddr =3D c->ip4.dns_match.s_addr; > - } else if (IN4_IS_ADDR_LOOPBACK(&b->s_in.sin_addr) || > - IN4_IS_ADDR_UNSPECIFIED(&b->s_in.sin_addr)|| > - IN4_ARE_ADDR_EQUAL(&b->s_in.sin_addr, &c->ip4.addr_seen)) { > - b->iph.saddr =3D c->ip4.gw.s_addr; > + iph->saddr =3D c->ip4.dns_match.s_addr; > + } else if (IN4_IS_ADDR_LOOPBACK(&s_in->sin_addr) || > + IN4_IS_ADDR_UNSPECIFIED(&s_in->sin_addr)|| > + IN4_ARE_ADDR_EQUAL(&s_in->sin_addr, &c->ip4.addr_seen)) { > + iph->saddr =3D c->ip4.gw.s_addr; > udp_tap_map[V4][src_port].ts =3D now->tv_sec; > udp_tap_map[V4][src_port].flags |=3D PORT_LOCAL; > =20 > - if (IN4_ARE_ADDR_EQUAL(&b->s_in.sin_addr.s_addr, &c->ip4.addr_seen)) > + if (IN4_ARE_ADDR_EQUAL(&s_in->sin_addr.s_addr, &c->ip4.addr_seen)) > udp_tap_map[V4][src_port].flags &=3D ~PORT_LOOPBACK; > else > udp_tap_map[V4][src_port].flags |=3D PORT_LOOPBACK; > =20 > bitmap_set(udp_act[V4][UDP_ACT_TAP], src_port); > } else { > - b->iph.saddr =3D b->s_in.sin_addr.s_addr; > + iph->saddr =3D s_in->sin_addr.s_addr; > } > =20 > - 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)); > + iph->check =3D ipv4_hdr_checksum(iph, IPPROTO_UDP); > + uh->source =3D s_in->sin_port; > + uh->dest =3D htons(dstport); > + uh->len=3D htons(data_len + sizeof(struct udphdr)); > =20 > - return tap_iov_len(c, &b->taph, ip_len); > + return ip_len; > } > =20 > /** > @@ -614,38 +615,39 @@ static size_t udp_update_hdr4(const struct ctx *c, = int n, in_port_t dstport, > * > * Return: size of tap frame with headers > */ > -static size_t udp_update_hdr6(const struct ctx *c, int n, in_port_t dstp= ort, > - const struct timespec *now) > +static size_t udp_update_hdr6(const struct ctx *c, struct ipv6hdr *ip6h, > + size_t data_len, struct sockaddr_in6 *s_in6, > + in_port_t dstport, const struct timespec *now) > { > - struct udp6_l2_buf_t *b =3D &udp6_l2_buf[n]; > + struct udphdr *uh =3D (struct udphdr *)(ip6h + 1); > struct in6_addr *src; > in_port_t src_port; > size_t ip_len; > =20 > - src =3D &b->s_in6.sin6_addr; > - src_port =3D ntohs(b->s_in6.sin6_port); > + src =3D &s_in6->sin6_addr; > + src_port =3D ntohs(s_in6->sin6_port); > =20 > - ip_len =3D udp6_l2_mh_sock[n].msg_len + sizeof(b->ip6h) + sizeof(b->uh); > + ip_len =3D data_len + sizeof(struct ipv6hdr) + sizeof(struct udphdr); > =20 > - b->ip6h.payload_len =3D htons(udp6_l2_mh_sock[n].msg_len + sizeof(b->uh= )); > + ip6h->payload_len =3D htons(data_len + sizeof(struct udphdr)); > =20 > if (IN6_IS_ADDR_LINKLOCAL(src)) { > - b->ip6h.daddr =3D c->ip6.addr_ll_seen; > - b->ip6h.saddr =3D b->s_in6.sin6_addr; > + ip6h->daddr =3D c->ip6.addr_ll_seen; > + ip6h->saddr =3D s_in6->sin6_addr; > } else if (!IN6_IS_ADDR_UNSPECIFIED(&c->ip6.dns_match) && > IN6_ARE_ADDR_EQUAL(src, &c->ip6.dns_host) && > src_port =3D=3D 53) { > - b->ip6h.daddr =3D c->ip6.addr_seen; > - b->ip6h.saddr =3D c->ip6.dns_match; > + ip6h->daddr =3D c->ip6.addr_seen; > + ip6h->saddr =3D c->ip6.dns_match; > } else if (IN6_IS_ADDR_LOOPBACK(src) || > IN6_ARE_ADDR_EQUAL(src, &c->ip6.addr_seen) || > IN6_ARE_ADDR_EQUAL(src, &c->ip6.addr)) { > - b->ip6h.daddr =3D c->ip6.addr_ll_seen; > + ip6h->daddr =3D c->ip6.addr_ll_seen; > =20 > if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw)) > - b->ip6h.saddr =3D c->ip6.gw; > + ip6h->saddr =3D c->ip6.gw; > else > - b->ip6h.saddr =3D c->ip6.addr_ll; > + ip6h->saddr =3D c->ip6.addr_ll; > =20 > udp_tap_map[V6][src_port].ts =3D now->tv_sec; > udp_tap_map[V6][src_port].flags |=3D PORT_LOCAL; > @@ -662,20 +664,20 @@ static size_t udp_update_hdr6(const struct ctx *c, = int n, in_port_t dstport, > =20 > bitmap_set(udp_act[V6][UDP_ACT_TAP], src_port); > } else { > - b->ip6h.daddr =3D c->ip6.addr_seen; > - b->ip6h.saddr =3D b->s_in6.sin6_addr; > + ip6h->daddr =3D c->ip6.addr_seen; > + ip6h->saddr =3D s_in6->sin6_addr; > } > =20 > - b->uh.source =3D b->s_in6.sin6_port; > - b->uh.dest =3D htons(dstport); > - b->uh.len =3D b->ip6h.payload_len; > - b->uh.check =3D csum(&b->uh, ntohs(b->ip6h.payload_len), > - proto_ipv6_header_checksum(&b->ip6h, IPPROTO_UDP)); > - b->ip6h.version =3D 6; > - b->ip6h.nexthdr =3D IPPROTO_UDP; > - b->ip6h.hop_limit =3D 255; > + uh->source =3D s_in6->sin6_port; > + uh->dest =3D htons(dstport); > + uh->len =3D ip6h->payload_len; > + uh->check =3D csum(uh, ntohs(ip6h->payload_len), > + proto_ipv6_header_checksum(ip6h, IPPROTO_UDP)); > + ip6h->version =3D 6; > + ip6h->nexthdr =3D IPPROTO_UDP; > + ip6h->hop_limit =3D 255; > =20 > - return tap_iov_len(c, &b->taph, ip_len); > + return ip_len; > } > =20 > /** > @@ -689,6 +691,11 @@ static size_t udp_update_hdr6(const struct ctx *c, i= nt n, in_port_t dstport, > * > * Return: size of tap frame with headers > */ > +#pragma GCC diagnostic push > +/* ignore unaligned pointer value warning for &udp6_l2_buf[i].ip6h and= =20 > + * &udp4_l2_buf[i].iph I feel like this needs more explanation: why is it unaligned? why can't we make it aligned? why is it safe to ignore the warning? > + */ > +#pragma GCC diagnostic ignored "-Waddress-of-packed-member" > static void udp_tap_send(const struct ctx *c, > unsigned int start, unsigned int n, > in_port_t dstport, bool v6, const struct timespec *now) > @@ -702,18 +709,31 @@ static void udp_tap_send(const struct ctx *c, > tap_iov =3D udp4_l2_iov_tap; > =20 > for (i =3D start; i < start + n; i++) { > - size_t buf_len; > - > - if (v6) > - buf_len =3D udp_update_hdr6(c, i, dstport, now); > - else > - buf_len =3D udp_update_hdr4(c, i, dstport, now); > - > - tap_iov[i].iov_len =3D buf_len; > + size_t ip_len; > + > + if (v6) { > + ip_len =3D udp_update_hdr6(c, &udp6_l2_buf[i].ip6h, > + udp6_l2_mh_sock[i].msg_len, > + &udp6_l2_buf[i].s_in6, dstport, > + now); > + tap_iov[i].iov_len =3D tap_iov_len(c, > + &udp6_l2_buf[i].taph, > + ip_len); > + } else { > + ip_len =3D udp_update_hdr4(c, &udp4_l2_buf[i].iph, > + udp4_l2_mh_sock[i].msg_len, > + &udp4_l2_buf[i].s_in, > + dstport, now); > + > + tap_iov[i].iov_len =3D tap_iov_len(c, > + &udp4_l2_buf[i].taph, > + ip_len); > + } > } > =20 > tap_send_frames(c, tap_iov + start, n); > } > +#pragma GCC diagnostic pop > =20 > /** > * udp_sock_handler() - Handle new data from socket --=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 --2JAwek4KOgG3N4zC Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXBkpUACgkQzQJF27ox 2GdfsQ/6A/sky/s2roKVoBaFAzmxSelc6fk9Et6OxISvP/0zIXiMroRUk6DKhKCC 7Ie2tEc8xTjWyMiuV84HXbARD0NKl5/e8vYKNT25LE/N978NcIYl3Bpy3v1+0fEq V7Y512Y6/L1MbXs3KOP9C80tVPG7fg13fYjiZmHsZiTHZ4qelR30wPEjymQgIr0T Q0kthyIIZEbv7s3+xZzuojGBFXYe3QoEJsoEnlx0vRcKqhIv3OwpUZNNh0vjWHXL BQR7n5BIhVJVLFXrs5kOKcBz8VYTL3YiNY2fYMxMgV3XGTkGYRi1k8wKtCVi6s5+ 7U+NCI/QQdNRk7fX2thNEihc31/EyXXlWlY0cEk92pJ1rjBtNyDmh0jwVQGyYvnP H8tnaw3Dfnex6DeRstOuzlwoAsPCRU3cfLI4Y6s2dPK5iLfLYgdeYG+qZb5sMbc1 ew/1og0D8CAVyiKkmfnaY0ffWd4hDXFz27Io91Ete9GOKJnUQY76KVYRtvCNGvNw Wh2YNMlvMLJxulXsgZ1kLx4Tdql6m0nyW2NRDkFnl9ht4qyJc6njrjDRPqGxQs2I 1Eoap/1B78QAjZgyRiU324eV2jvkSFKbzZs6t4pf0kWEreEvRGjotsslrMjid74Z kwmq75a2+7RjlOlf/2ob8si7LpYgjyZYnhNVK2zGhshO+WDbP+8= =3Som -----END PGP SIGNATURE----- --2JAwek4KOgG3N4zC--