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 02EE95A004E for ; Wed, 12 Jun 2024 00:10:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718143828; 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=pW/YLEOUu3SsnE6PoHlCkkce9nn7xXiRGO5ir/PQLN0=; b=iGVK5ajrVx1RyEBxMohnXGBH8df2wHhFthZHRNqwdK5zHL1l6WagOR89oPm2BE3WIe87fM u9YGSUzX+9gsCgQWLi/H+fW6gzpYA41udOHklES5BNqDqLuHmY3VYKBsUf70DIqmhOgF/D dPHJ2ASeX4b26Ahx/gN3aV1ZOLDPEz8= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-467-4aNAfeYFO3CczI9QR7s0HQ-1; Tue, 11 Jun 2024 18:10:27 -0400 X-MC-Unique: 4aNAfeYFO3CczI9QR7s0HQ-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-4411f4617d9so13311031cf.1 for ; Tue, 11 Jun 2024 15:10:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718143826; x=1718748626; 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=pW/YLEOUu3SsnE6PoHlCkkce9nn7xXiRGO5ir/PQLN0=; b=PgyTzDR/wEGvuqSnEoMpT/o/jWvlE2LEz0hM0JWHRDIuooWnVIl6/VbV+1TuyaIZX+ jGRd5HAuJoC/UGoSSuTwQVON3ybCaqgPKg0UHIBbF6RKoCAErAgIDy/+lHgAt9YwfqJe cSyLMx7ZslVah5cA+PmMgSSMyQtO0DAk+viDEpj+r/Z9rBp0iJ895wNeB6X5WUjnN5I0 LFer38HF/rHMjMxCMXoBxg8vGK/lqPLRRwzfku9irxMuUcmVhwYqOaHcflSxA6QKz6RC ikkjHJStWeBPSZ5XyQpf96BziP9RCZInaGyBdft7EXK07YZRNpMk12S8w2YF++ujC9Pc wwUw== X-Gm-Message-State: AOJu0Ywlq6fDAJSHMfM8xc+mMK3xiHv+2OEckHM87JEhQ55gvIPgh63f oBbe5dFrKq4SQkrrMG+pD2vuZ9DrjmwuMjd5u9W2oj+3x5LXhJKW83BdJ5CpAwDLGB2FnJAZ3jv O0R9BZmK3Uqa+g/OE9QZBOgnIvkYU7u7MmA/Ukg6kBj9SxBLoCGPYNuR8pSKmlVH8/rAAjwr+61 dgq+9MP6Ip9JaOYPIIY5bDgJuAsKYPmyASBHQ= X-Received: by 2002:a05:622a:19a3:b0:441:1536:2fb2 with SMTP id d75a77b69052e-4415abad2c9mr370241cf.5.1718143825608; Tue, 11 Jun 2024 15:10:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGhU7oGvXe4iXgwTjBNLEeQAGvq6QEai5xtKtn9LN6wQikVmJPrnlmBKPs5t5CmigB09yR+CQ== X-Received: by 2002:a05:622a:19a3:b0:441:1536:2fb2 with SMTP id d75a77b69052e-4415abad2c9mr369851cf.5.1718143824949; Tue, 11 Jun 2024 15:10:24 -0700 (PDT) Received: from maya.cloud.tilaa.com (maya.cloud.tilaa.com. [164.138.29.33]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4404f5bc3edsm40022941cf.54.2024.06.11.15.10.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Jun 2024 15:10:24 -0700 (PDT) Date: Wed, 12 Jun 2024 00:09:50 +0200 From: Stefano Brivio To: Laurent Vivier Subject: Re: [PATCH v5 3/8] tap: refactor packets handling functions Message-ID: <20240612000950.6e8ad4b5@elisabeth> In-Reply-To: <20240605152129.1641658-4-lvivier@redhat.com> References: <20240605152129.1641658-1-lvivier@redhat.com> <20240605152129.1641658-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: VCDJKUXS43YUGL2ARVSMOGZOSF5KDVBP X-Message-ID-Hash: VCDJKUXS43YUGL2ARVSMOGZOSF5KDVBP 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, 5 Jun 2024 17:21:24 +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. > > The purpose is to ease the export of these functions to use > them with the vhost-user backend. > > Signed-off-by: Laurent Vivier > --- > tap.c | 113 +++++++++++++++++++++++++++++++++------------------------- > tap.h | 7 ++++ > 2 files changed, 71 insertions(+), 49 deletions(-) > > 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; > } > > +/** > + * 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 descriptor IPv4/IPv6 > + * @c: Execution context > + * @now: Current timestamp > + */ > +void tap_handler_all(struct ctx *c, const struct timespec *now) 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. > +{ > + tap4_handler(c, pool_tap4, now); > + tap6_handler(c, pool_tap6, now); > +} > + > +/** > + * packet_add_all_do() - Add a packet to the appropriate TAP pool A couple of remarks here: - 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? - 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"? - 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 > + * @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 = (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; > > @@ -959,8 +1013,7 @@ redo: > p = pkt_buf; > rem = 0; > > - pool_flush(pool_tap4); > - pool_flush(pool_tap6); > + pool_flush_all(); > > n = 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; > > - 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; > - } > + packet_add_all(c, l2len, p); > > next: > p += l2len; > n -= l2len; > } > > - tap4_handler(c, pool_tap4, now); > - tap6_handler(c, pool_tap6, now); > + tap_handler_all(c, now); > > /* We can't use EPOLLET otherwise. */ > if (rem) > @@ -1043,35 +1076,18 @@ void tap_handler_pasta(struct ctx *c, uint32_t events, > redo: > n = 0; > > - pool_flush(pool_tap4); > - pool_flush(pool_tap6); > + pool_flush_all(); > 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; > - } > + packet_add_all(c, len, pkt_buf + n); > > if ((n += len) == TAP_BUF_BYTES) > break; > @@ -1082,8 +1098,7 @@ restart: > > ret = errno; > > - tap4_handler(c, pool_tap4, now); > - tap6_handler(c, pool_tap6, now); > + tap_handler_all(c, now); > > if (len > 0 || ret == 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__) > > #endif /* TAP_H */ -- Stefano