From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id 6F9085A004F for ; Wed, 12 Jun 2024 17:53:13 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718207592; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0ws1Ey+613sFgcSB4uVrQ8zVGRerQPa7256uj9xrYBw=; b=WW82zpV3Ym+K7bFh4j6G+EAfbeZZzwWG3zIkuBgdt2fvHgpppMnK+gR4YWw1ZyrOQ8HtkW ZiiPUcBkfhH36EwMDvPkbCF8rmuevp4/FObX2zbhLTWNdCrZVd/bNpX2nmj44BnHHep13W 7bjkV+HjggfLnoQXDOsrShrMpC5pO5M= Received: from mail-ot1-f71.google.com (mail-ot1-f71.google.com [209.85.210.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-505-b9U98pAqOdmCKknfHajpQw-1; Wed, 12 Jun 2024 11:53:11 -0400 X-MC-Unique: b9U98pAqOdmCKknfHajpQw-1 Received: by mail-ot1-f71.google.com with SMTP id 46e09a7af769-6f9d6984f2aso3022339a34.3 for ; Wed, 12 Jun 2024 08:53:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718207589; x=1718812389; h=content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=0ws1Ey+613sFgcSB4uVrQ8zVGRerQPa7256uj9xrYBw=; b=njlSttVBnQaEM1CdzIkzOc21KYDgsT5x4/d3rDU8th+HQmuXVaxh+AIR41fYBL+sWo E9W4YdfTzsf1awm/VIQhZ52Bbpu7cJ6tluHMMiKDpaSmZiI0IpwzSNjSzeFgUp6orgWV xHtoS+GwLBsnFVMlqffnU7tfxyMEbq2fI4RZG9QypE04y6J+0Rtgf5kyqWIR0e33Fniw i89j2JEgyRGZywWHGyTn6fpY9UcRdZIeflgSl6eHdINBXhPPrXBlgJGCbQr0+86Wbh3c 1rC54M3Ow+5myKvbUc7UA+kk1ykhjgVeqw/EnrZN5ywal8vYxu/HN3CYnnuri8yAwbI+ CUsw== X-Gm-Message-State: AOJu0YxWBBV9KwHQFAhdYdfqr7KOVWRpq5Y8rnBZ9Qzq14YuBcA8pw1T kKrkFqxAi/+XyBXDNZNGp2V3jk4uWhJyE6mxE3CfybxbCW6tT84liz/3aQemLxLJJ+W51uEW/sg 9CYFjeHG3juTfhL67Ew7ef9EQPPCQ2ZoQVJ9MeFvTxBPPyqObrxFAN3STS/+qMUwe9FowTH60XB 2zgHfrUQ06SjMdZltnu3wQs3SBNKU1+NdFmNk= X-Received: by 2002:a9d:7cc5:0:b0:6fa:e19:6c01 with SMTP id 46e09a7af769-6fa1c222ca6mr2191542a34.29.1718207588546; Wed, 12 Jun 2024 08:53:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFaJK6pGvBgBbcGF2aP+8gjrHXgjm/T/LIc79UE1Jt62PT/FKI07Euc2LGvsXGtCNBpXC/7Zw== X-Received: by 2002:a9d:7cc5:0:b0:6fa:e19:6c01 with SMTP id 46e09a7af769-6fa1c222ca6mr2191509a34.29.1718207587913; Wed, 12 Jun 2024 08:53:07 -0700 (PDT) Received: from maya.cloud.tilaa.com (maya.cloud.tilaa.com. [164.138.29.33]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7955a9bc90csm373271985a.114.2024.06.12.08.53.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 08:53:07 -0700 (PDT) Date: Wed, 12 Jun 2024 17:52:31 +0200 From: Stefano Brivio To: Laurent Vivier Subject: Re: [PATCH v6 3/8] tap: refactor packets handling functions Message-ID: <20240612175231.029a4344@elisabeth> In-Reply-To: <20240612154734.1044883-4-lvivier@redhat.com> References: <20240612154734.1044883-1-lvivier@redhat.com> <20240612154734.1044883-4-lvivier@redhat.com> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.41; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: 3ZORPRIP7VURWWNJWFO6TSI4QAPIHMTY X-Message-ID-Hash: 3ZORPRIP7VURWWNJWFO6TSI4QAPIHMTY X-MailFrom: sbrivio@redhat.com 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: On Wed, 12 Jun 2024 17:47:29 +0200 Laurent Vivier wrote: > Consolidate pool_tap4() and pool_tap6() into tap_flush_pools(), > and tap4_handler() and tap6_handler() into tap_handler(). > Create a generic tap_add_packet() to consolidate packet > addition logic and reduce code duplication. > > The purpose is to ease the export of these functions to use > them with the vhost-user backend. > > Signed-off-by: Laurent Vivier > --- > > Notes: > v6: > - rename tap_handler_all() to tap_handler() > - rename packet_add_all_do() to tap_add_packet > and remove func and line. Oops, they're still in the comment to the function. I can drop that on merge. > - rename pool_flush_all() to tap_flush_pools() > > v5: > - update commit message and function comments > > tap.c | 112 +++++++++++++++++++++++++++++++++------------------------- > tap.h | 3 ++ > 2 files changed, 66 insertions(+), 49 deletions(-) > > diff --git a/tap.c b/tap.c > index 26084b486519..ec3a0d68ea2f 100644 > --- a/tap.c > +++ b/tap.c > @@ -921,6 +921,60 @@ append: > return in->count; > } > > +/** > + * pool_flush() - Flush both IPv4 and IPv6 packet pools > + */ > +void tap_flush_pools(void) > +{ > + pool_flush(pool_tap4); > + pool_flush(pool_tap6); > +} > + > +/** > + * tap_handler() - IPv4/IPv6 and ARP packet handler for tap file descriptor > + * @c: Execution context > + * @now: Current timestamp > + */ > +void tap_handler(struct ctx *c, const struct timespec *now) > +{ > + tap4_handler(c, pool_tap4, now); > + tap6_handler(c, pool_tap6, now); > +} > + > +/** > + * tap_add_packet() - Queue/capture packet, update notion of guest MAC address > + * @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 tap_add_packet(struct ctx *c, ssize_t l2len, char *p) > +{ > + const struct ethhdr *eh; > + > + pcap(p, l2len); > + > + eh = (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; > + } > +} > + > /** > * tap_sock_reset() - Handle closing or failure of connect AF_UNIX socket > * @c: Execution context > @@ -947,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; > > @@ -960,8 +1013,7 @@ redo: > p = pkt_buf; > rem = 0; > > - pool_flush(pool_tap4); > - pool_flush(pool_tap6); > + tap_flush_pools(); > > n = recv(c->fd_tap, p, TAP_BUF_FILL, MSG_DONTWAIT); > if (n < 0) { > @@ -988,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; > > - pcap(p, l2len); > - > - eh = (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; > - } > + tap_add_packet(c, l2len, p); > > next: > p += l2len; > n -= l2len; > } > > - tap4_handler(c, pool_tap4, now); > - tap6_handler(c, pool_tap6, now); > + tap_handler(c, now); > > /* We can't use EPOLLET otherwise. */ > if (rem) > @@ -1044,35 +1076,18 @@ void tap_handler_pasta(struct ctx *c, uint32_t events, > redo: > n = 0; > > - pool_flush(pool_tap4); > - pool_flush(pool_tap6); > + tap_flush_pools(); > restart: > while ((len = read(c->fd_tap, pkt_buf + n, TAP_BUF_BYTES - n)) > 0) { > - const struct ethhdr *eh = (struct ethhdr *)(pkt_buf + n); > > - 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 += len; > continue; > } > > - pcap(pkt_buf + n, len); > > - 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; > - } > + tap_add_packet(c, len, pkt_buf + n); > > if ((n += len) == TAP_BUF_BYTES) > break; > @@ -1083,8 +1098,7 @@ restart: > > ret = errno; > > - tap4_handler(c, pool_tap4, now); > - tap6_handler(c, pool_tap6, now); > + tap_handler(c, now); > > if (len > 0 || ret == EAGAIN) > return; > diff --git a/tap.h b/tap.h > index 2285a87093f9..d496bd0e4b99 100644 > --- a/tap.h > +++ b/tap.h > @@ -70,5 +70,8 @@ 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 tap_flush_pools(void); > +void tap_handler(struct ctx *c, const struct timespec *now); > +void tap_add_packet(struct ctx *c, ssize_t l2len, char *p); > > #endif /* TAP_H */ -- Stefano