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 59D515A004F for ; Wed, 12 Jun 2024 08:21:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1718173284; bh=hjoDXhE7AGJxJ5YogjOyhxkhNqIUNDwXJJV4yD2EOiE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=mLP92Nge2VFUtdA6jxBNDPD+xundi5OjzbHImVOEMRkWCOluZi2G378Rq5IC7pfGr /ahxK3Tr60XpZHuIr4UuR4JJMtvqwcVceaG1NpaSjVwIK3Lab0czPXb+rPiEgh5oqE vYihp8TWLgyo94d/w7lfgjFDByFYeaZabxFM2NJN5EHeGI1mnm20CcwZECypuwthoQ EoFD9XsPRdanQbe6fw2+XVDIjdnp2+lCnjMTYhIQsB3KlB4dUgMHa36OcH1JTCel+d BJF0tPouymPh811ZEqUE6b7FCKeq27j0KjoOV7CkUMDMgYarDMl90KzWtspsvsVN5t V37CVbtIjiSIg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Vzb5w4Yf9z4wcv; Wed, 12 Jun 2024 16:21:24 +1000 (AEST) Date: Wed, 12 Jun 2024 16:21:17 +1000 From: David Gibson To: Laurent Vivier 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> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="QHhoUfOd4+KLWPhL" Content-Disposition: inline In-Reply-To: <20240605152129.1641658-4-lvivier@redhat.com> Message-ID-Hash: SQG2MP6S5FZELLDQUEI7EJ3SJSACJS7C X-Message-ID-Hash: SQG2MP6S5FZELLDQUEI7EJ3SJSACJS7C 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: --QHhoUfOd4+KLWPhL Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jun 05, 2024 at 05:21:24PM +0200, Laurent Vivier wrote: > 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); > +} This is a public function that doesn't follow the usual namespacing conventions. Maybe tap_flush_pools()? > + > +/** > + * tap_handler_all() - IPv4/IPv4 and ARP packet handler for tap file des= criptor > + * @c: Execution context > + * @now: Current timestamp > + */ > +void tap_handler_all(struct ctx *c, const struct timespec *now) > +{ > + tap4_handler(c, pool_tap4, now); > + tap6_handler(c, pool_tap6, now); > +} > + > +/** > + * packet_add_all_do() - Add a packet to the appropriate TAP pool > + * @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 socket > * @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 ev= ents, > 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 events, > 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 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 --QHhoUfOd4+KLWPhL Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmZpPlwACgkQzQJF27ox 2Gchqg//fvEfFfAHDXFIuyO4ZZdF0zBSw0sZd7L+WoheZEkna/I9Fc2jm1UWEFI2 j+41X2cUP7f57WvHZPZWFTj+tWB1yaSovfUrzFtSq29Vh/CA2/2Wf2Xi5XbHiR0E 0uA93AeD+wr9/0rkdzeurYpS5e0mHLLZlEPWngpEm45mxEl7QI7ee/hzmPNYiltv tEkTpIjkpiXwC+Fj4GwOm24c1qQlB1hvNZmpVV7Z6hmh/lJn+vd78PVvG52BmLP3 rFUmAe36D30nJrguCrW5BjcW/vOK7+u5Zf5hLaA05pBj/mkw5TSGdi2PNKY7isfs BT74ctcV4p+kJCKwX2BT27Ux0BqEWN+qMybsd6Y+vSNlZ8vr1VEy/6sU3gJavPEq B46x3kGpHaKykT4HD2UqVhvPa5CTmv3QqhtCn++sfBIP6EFBIA1sjFRB6SwBeeZV VPiy7Slfrqza1b9PNNyh4ja5KlvF6AvG1xa7XkuHbHbYqYfa0POv7mmQS+mttWPr mEc0H82+EfhA2oPe7FwhSX1tMAjOXm4aoRRN4+lAy088hX9Y2fi3sc4KDDMOlZOG FC5e1jIZoQHzEM/uGodB/A8Y5F7/gLMi75tj9zemuG7LpiQNIISFAYihmrZYqthb 4Bke9TGrLWA+xJEZbB0TfgqE4nUPSlmXdbO4/7g2gBJQx62yX1Q= =Mc2O -----END PGP SIGNATURE----- --QHhoUfOd4+KLWPhL--