From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 286575A004F for ; Wed, 12 Jun 2024 08:21:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1718173284; bh=Vt3XJ3aUkOEGIJC+4gLrEGNYdRlAQoVdmrx3VjwhRQc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=gIyyl4O4mEg+yfO0QTsnT8cD2XW/j4zX0ObV9Dml7RuU0jP8Kh2Gk2jGSlJXWQSfQ cTI/Lht7g+0UszWCnBDNxXpmc7eL3Pjc5x+qPonfkf5HP2e+CtLf+qe5PtGkqlkfNg j4tyl7sn13VW51Nol4dar6/LKucy3rWyuMCOUjId2PEtK9OXn3pg7ByvTICylAMawC llVqmVTum5/3bqrpMhbpnCVygj1yJtIHLuzHUveYOENrcpHTNB5UHcyxVrtfMzV1jy 48zKxiHyQf1UMET0z2CcugqdDHmGiCbRYMmGjZTMC3qIAV6jLYMjneEcRM+E6W5zcE Ik0tF+MKLvK7Q== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Vzb5w4SVbz4wb7; Wed, 12 Jun 2024 16:21:24 +1000 (AEST) Date: Wed, 12 Jun 2024 16:18:59 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v5 3/8] tap: refactor packets handling functions Message-ID: References: <20240605152129.1641658-1-lvivier@redhat.com> <20240605152129.1641658-4-lvivier@redhat.com> <20240612000950.6e8ad4b5@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="bXEEn9OAY+jOVbwG" Content-Disposition: inline In-Reply-To: <20240612000950.6e8ad4b5@elisabeth> Message-ID-Hash: TJKPSJQMSQEXJHPHFOHEFVL2MYX2FYPA X-Message-ID-Hash: TJKPSJQMSQEXJHPHFOHEFVL2MYX2FYPA 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: Laurent Vivier , 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: --bXEEn9OAY+jOVbwG Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jun 12, 2024 at 12:09:50AM +0200, Stefano Brivio wrote: > On Wed, 5 Jun 2024 17:21:24 +0200 > Laurent Vivier wrote: >=20 > > Consolidate pool_tap4() and pool_tap6() into pool_flush_all(), > > and tap4_handler() and tap6_handler() into tap_handler_all(). > > Create a generic packet_add_all() to consolidate packet > > addition logic and reduce code duplication. > >=20 > > The purpose is to ease the export of these functions to use > > them with the vhost-user backend. > >=20 > > Signed-off-by: Laurent Vivier > > --- > > tap.c | 113 +++++++++++++++++++++++++++++++++------------------------- > > tap.h | 7 ++++ > > 2 files changed, 71 insertions(+), 49 deletions(-) > >=20 > > diff --git a/tap.c b/tap.c > > index 2ea08491a51f..5fb3cb83f3d2 100644 > > --- a/tap.c > > +++ b/tap.c > > @@ -920,6 +920,61 @@ append: > > return in->count; > > } > > =20 > > +/** > > + * pool_flush() - Flush both IPv4 and IPv6 packet pools > > + */ > > +void pool_flush_all(void) > > +{ > > + pool_flush(pool_tap4); > > + pool_flush(pool_tap6); > > +} > > + > > +/** > > + * tap_handler_all() - IPv4/IPv4 and ARP packet handler for tap file d= escriptor >=20 > IPv4/IPv6 >=20 > > + * @c: Execution context > > + * @now: Current timestamp > > + */ > > +void tap_handler_all(struct ctx *c, const struct timespec *now) >=20 > I wonder if this shouldn't be named tap_handler() instead. As we > already have tap_handler_passt() and tap_handler_pasta(), it's not > immediately clear what "all" refers to. I concur, I think tap_handler() is a better name. > > +{ > > + tap4_handler(c, pool_tap4, now); > > + tap6_handler(c, pool_tap6, now); > > +} > > + > > +/** > > + * packet_add_all_do() - Add a packet to the appropriate TAP pool >=20 > A couple of remarks here: >=20 > - it's a bit unexpected that this is still in tap.c (it adds packets to > a pool, it should be in packet.c judging by this name/description). > If we call it tap_queue_packet(), then it probably makes more sense? >=20 > - this does more than adding a packet to a pool. It's probably useless > to describe in detail what this does, as the function body is anyway > rather short and clear, but the current description could be a bit > misleading. What about "Queue/capture packet, update notion of > guest MAC address"? So, given this, I think it does make more sense for this to be in tap.c than packet.c. How about calling it tap_add_packets(). > - what happens if you just call packet_add() from here, without dealing > with 'func' and 'line'? I think it's fine to print in tracing output > name and lines from this function, instead of the ones from the > caller. It's obvious who the caller is It is as of this patch, but I believe the idea is this will also be called from VU code down the line. > > + * @c: Execution context > > + * @l2len: Total L2 packet length > > + * @p: Packet buffer > > + * @func: For tracing: name of calling function, NULL means no trace() > > + * @line: For tracing: caller line of function call > > + */ > > +void packet_add_all_do(struct ctx *c, ssize_t l2len, char *p, > > + const char *func, int line) > > +{ > > + const struct ethhdr *eh; > > + > > + pcap(p, l2len); > > + > > + eh =3D (struct ethhdr *)p; > > + > > + if (memcmp(c->mac_guest, eh->h_source, ETH_ALEN)) { > > + memcpy(c->mac_guest, eh->h_source, ETH_ALEN); > > + proto_update_l2_buf(c->mac_guest, NULL); > > + } > > + > > + switch (ntohs(eh->h_proto)) { > > + case ETH_P_ARP: > > + case ETH_P_IP: > > + packet_add_do(pool_tap4, l2len, p, func, line); > > + break; > > + case ETH_P_IPV6: > > + packet_add_do(pool_tap6, l2len, p, func, line); > > + break; > > + default: > > + break; > > + } > > +} > > + > > /** > > * tap_sock_reset() - Handle closing or failure of connect AF_UNIX soc= ket > > * @c: Execution context > > @@ -946,7 +1001,6 @@ static void tap_sock_reset(struct ctx *c) > > void tap_handler_passt(struct ctx *c, uint32_t events, > > const struct timespec *now) > > { > > - const struct ethhdr *eh; > > ssize_t n, rem; > > char *p; > > =20 > > @@ -959,8 +1013,7 @@ redo: > > p =3D pkt_buf; > > rem =3D 0; > > =20 > > - pool_flush(pool_tap4); > > - pool_flush(pool_tap6); > > + pool_flush_all(); > > =20 > > n =3D recv(c->fd_tap, p, TAP_BUF_FILL, MSG_DONTWAIT); > > if (n < 0) { > > @@ -987,38 +1040,18 @@ redo: > > /* Complete the partial read above before discarding a malformed > > * frame, otherwise the stream will be inconsistent. > > */ > > - if (l2len < (ssize_t)sizeof(*eh) || > > + if (l2len < (ssize_t)sizeof(struct ethhdr) || > > l2len > (ssize_t)ETH_MAX_MTU) > > goto next; > > =20 > > - pcap(p, l2len); > > - > > - eh =3D (struct ethhdr *)p; > > - > > - if (memcmp(c->mac_guest, eh->h_source, ETH_ALEN)) { > > - memcpy(c->mac_guest, eh->h_source, ETH_ALEN); > > - proto_update_l2_buf(c->mac_guest, NULL); > > - } > > - > > - switch (ntohs(eh->h_proto)) { > > - case ETH_P_ARP: > > - case ETH_P_IP: > > - packet_add(pool_tap4, l2len, p); > > - break; > > - case ETH_P_IPV6: > > - packet_add(pool_tap6, l2len, p); > > - break; > > - default: > > - break; > > - } > > + packet_add_all(c, l2len, p); > > =20 > > next: > > p +=3D l2len; > > n -=3D l2len; > > } > > =20 > > - tap4_handler(c, pool_tap4, now); > > - tap6_handler(c, pool_tap6, now); > > + tap_handler_all(c, now); > > =20 > > /* We can't use EPOLLET otherwise. */ > > if (rem) > > @@ -1043,35 +1076,18 @@ void tap_handler_pasta(struct ctx *c, uint32_t = events, > > redo: > > n =3D 0; > > =20 > > - pool_flush(pool_tap4); > > - pool_flush(pool_tap6); > > + pool_flush_all(); > > restart: > > while ((len =3D read(c->fd_tap, pkt_buf + n, TAP_BUF_BYTES - n)) > 0)= { > > - const struct ethhdr *eh =3D (struct ethhdr *)(pkt_buf + n); > > =20 > > - if (len < (ssize_t)sizeof(*eh) || len > (ssize_t)ETH_MAX_MTU) { > > + if (len < (ssize_t)sizeof(struct ethhdr) || > > + len > (ssize_t)ETH_MAX_MTU) { > > n +=3D len; > > continue; > > } > > =20 > > - pcap(pkt_buf + n, len); > > =20 > > - if (memcmp(c->mac_guest, eh->h_source, ETH_ALEN)) { > > - memcpy(c->mac_guest, eh->h_source, ETH_ALEN); > > - proto_update_l2_buf(c->mac_guest, NULL); > > - } > > - > > - switch (ntohs(eh->h_proto)) { > > - case ETH_P_ARP: > > - case ETH_P_IP: > > - packet_add(pool_tap4, len, pkt_buf + n); > > - break; > > - case ETH_P_IPV6: > > - packet_add(pool_tap6, len, pkt_buf + n); > > - break; > > - default: > > - break; > > - } > > + packet_add_all(c, len, pkt_buf + n); > > =20 > > if ((n +=3D len) =3D=3D TAP_BUF_BYTES) > > break; > > @@ -1082,8 +1098,7 @@ restart: > > =20 > > ret =3D errno; > > =20 > > - tap4_handler(c, pool_tap4, now); > > - tap6_handler(c, pool_tap6, now); > > + tap_handler_all(c, now); > > =20 > > if (len > 0 || ret =3D=3D EAGAIN) > > return; > > diff --git a/tap.h b/tap.h > > index 2285a87093f9..3ffb7d6c3a91 100644 > > --- a/tap.h > > +++ b/tap.h > > @@ -70,5 +70,12 @@ void tap_handler_passt(struct ctx *c, uint32_t event= s, > > const struct timespec *now); > > int tap_sock_unix_open(char *sock_path); > > void tap_sock_init(struct ctx *c); > > +void pool_flush_all(void); > > +void tap_handler_all(struct ctx *c, const struct timespec *now); > > + > > +void packet_add_all_do(struct ctx *c, ssize_t l2len, char *p, > > + const char *func, int line); > > +#define packet_add_all(p, l2len, start) \ > > + packet_add_all_do(p, l2len, start, __func__, __LINE__) > > =20 > > #endif /* TAP_H */ >=20 --=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 --bXEEn9OAY+jOVbwG Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmZpPdIACgkQzQJF27ox 2GdYDxAAl5y4/+8G8Vv3+v3fPeZgK5qz5zCcHSjOu0rp2y5vqiNnqhap7vHbmqqz xf0e7Ojkb7p5neehZXzEIG1Um0hNmiVKBNqpZ9NyaS2JvKQHVqpzvIpuMLl8yBYe omKbCIIhwFi6XNKDri08sreOyBB1nMvi9ts8uAOHj2cNggUPJgB0rXSPkEIyFO1m BskAFDSS38YINy1Hc35HYCKlCTvAj+SWDfMWc8/7OSeSQPh5oqSUbiaIeN28RhGu oceaJunr+qjgp392DI2O5H/qYbe2mf7pVGV1Ix3Yayk+zm13tyV9Y7WN3BG6+emF EHfFPBC8TaZipR67o+yP9b2Jdqyiq6/Y8cZ4K1jsQL0XpAn559qRLmfMkPA0urQy bvl0dGW2vkT4KNuSdPy0UhHU+p9XGOvQbSh/0GtkiVGRLfR1lke9hvkxntAeDWuV shQ/NtmlN8lrJT3neoGjNIk3I3gVXUEXIqw2qcbygwbbKUEcr1/rO8eTzquE0SvZ jUiIg/iVKfBkZ3lmCviEgyMZfow7XQ6VzhVIotzAVcWH6WxHD4/KZpIuFmSvl5fy 1xaDP1kmNWb6Nfps8+ZUN+NqFHmbpZ8//GzUEdQEuPplHC1cUcuk2YOMW15qStkg 4AcHlW/Xm7h7kuHZ7b0z3rZUHw/iyD1dTA7lnpLYfcN2oOgz12g= =ct0Q -----END PGP SIGNATURE----- --bXEEn9OAY+jOVbwG--