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.133.124]) by passt.top (Postfix) with ESMTP id 22B285A0275 for ; Fri, 2 Feb 2024 15:11:58 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706883117; 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=ngd3WV1awmFlbMpFpKd70l/N4p64gU/m5U+kPNI2wi4=; b=HFuTbIS8ir+gQt+Hog4XFhng5hqiHmpdO2kWXRMWwuBM9Rmc8gMvTpOozu4BItouVT8AHz Ff6r63G3xETL/9WGtalLPP6fhgmZDkamZssudxmovcN1GJGiq1GNyamLhq4jJ0YwD6rfYn IpA9lWxRE1rYNKClvSA9Sc4DERCxUv4= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-286-QicW3w5cOfu-K0F5JR8mjg-1; Fri, 02 Feb 2024 09:11:54 -0500 X-MC-Unique: QicW3w5cOfu-K0F5JR8mjg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 786A638562E6; Fri, 2 Feb 2024 14:11:54 +0000 (UTC) Received: from virtlab218.virt.lab.eng.bos.redhat.com (virtlab218.virt.lab.eng.bos.redhat.com [10.19.152.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id 547B03C2E; Fri, 2 Feb 2024 14:11:54 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH 13/24] tap: export pool_flush()/tapX_handler()/packet_add() Date: Fri, 2 Feb 2024 15:11:40 +0100 Message-ID: <20240202141151.3762941-14-lvivier@redhat.com> In-Reply-To: <20240202141151.3762941-1-lvivier@redhat.com> References: <20240202141151.3762941-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: V3O3BNUTJEQGT5KX2VMAQGIAINDJS4Q5 X-Message-ID-Hash: V3O3BNUTJEQGT5KX2VMAQGIAINDJS4Q5 X-MailFrom: lvivier@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: Laurent Vivier 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: From: Laurent Vivier Signed-off-by: Laurent Vivier --- tap.c | 98 +++++++++++++++++++++++++++++------------------------------ tap.h | 7 +++++ 2 files changed, 56 insertions(+), 49 deletions(-) diff --git a/tap.c b/tap.c index 29f389057ac1..5b1b61550c13 100644 --- a/tap.c +++ b/tap.c @@ -911,6 +911,45 @@ append: return in->count; } +void pool_flush_all(void) +{ + pool_flush(pool_tap4); + pool_flush(pool_tap6); +} + +void tap_handler_all(struct ctx *c, const struct timespec *now) +{ + tap4_handler(c, pool_tap4, now); + tap6_handler(c, pool_tap6, now); +} + +void packet_add_all_do(struct ctx *c, ssize_t len, char *p, + const char *func, int line) +{ + const struct ethhdr *eh; + + pcap(p, len); + + 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, len, p, func, line); + break; + case ETH_P_IPV6: + packet_add_do(pool_tap6, len, p, func, line); + break; + default: + break; + } +} + /** * tap_sock_reset() - Handle closing or failure of connect AF_UNIX socket * @c: Execution context @@ -937,7 +976,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; @@ -950,8 +988,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) { @@ -978,37 +1015,18 @@ redo: /* Complete the partial read above before discarding a malformed * frame, otherwise the stream will be inconsistent. */ - 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) goto next; - pcap(p, len); - - 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, len, p); - break; - case ETH_P_IPV6: - packet_add(pool_tap6, len, p); - break; - default: - break; - } + packet_add_all(c, len, p); next: p += len; n -= len; } - 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) @@ -1033,35 +1051,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; @@ -1072,8 +1073,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 437b9aa2b43f..7157ef37ee6e 100644 --- a/tap.h +++ b/tap.h @@ -82,5 +82,12 @@ void tap_handler_pasta(struct ctx *c, uint32_t events, void tap_handler_passt(struct ctx *c, uint32_t events, const struct timespec *now); 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_do(struct pool *p, size_t len, const char *start, + const char *func, int line); +#define packet_add_all(p, len, start) \ + packet_add_all_do(p, len, start, __func__, __LINE__) #endif /* TAP_H */ -- 2.42.0