From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=VYvGZo0B; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id DEE055A0272 for ; Wed, 02 Apr 2025 19:23:54 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743614633; 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=HqF+QXD/tdawLwfpnPqnLu8+w75Z10kXhHxGcFw3NaI=; b=VYvGZo0BSDkdIZ2W8sI11LNvHTv9W5s8eyzxIHMEo7fHmFrodMA/ys0r9tLyZOm76mchP5 Vmp6LqFYpdnQGDnxpZKX2VhTweCxz9CirJI0B8AWMUZjc8ERW5rVFp7WrQ0L66abPzF+ds YEnOJjDnogcQqY/4FC1v/B1bFQYwy7g= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-553-9jf1ayHFO3-mEN_a4JJrtg-1; Wed, 02 Apr 2025 13:23:52 -0400 X-MC-Unique: 9jf1ayHFO3-mEN_a4JJrtg-1 X-Mimecast-MFC-AGG-ID: 9jf1ayHFO3-mEN_a4JJrtg_1743614632 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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 mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id ECC1019560B3 for ; Wed, 2 Apr 2025 17:23:51 +0000 (UTC) Received: from lenovo-t14s.redhat.com (unknown [10.44.33.101]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DC43C19560AD; Wed, 2 Apr 2025 17:23:50 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH 04/18] packet: Use iov_tail with packet_add() Date: Wed, 2 Apr 2025 19:23:29 +0200 Message-ID: <20250402172343.858187-5-lvivier@redhat.com> In-Reply-To: <20250402172343.858187-1-lvivier@redhat.com> References: <20250402172343.858187-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: VXWq_HT4anq9xGzcrv8qLIk7tcVrYDRplxkN20Zgpso_1743614632 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: TFWQDROMXKJGKTSRDXR2SDAUGAS4CBUH X-Message-ID-Hash: TFWQDROMXKJGKTSRDXR2SDAUGAS4CBUH 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: Signed-off-by: Laurent Vivier --- iov.h | 1 + packet.c | 15 ++++++++++++--- packet.h | 8 +++++--- tap.c | 32 ++++++++++++++++++-------------- 4 files changed, 36 insertions(+), 20 deletions(-) diff --git a/iov.h b/iov.h index f9b5fdf0803e..99442d031549 100644 --- a/iov.h +++ b/iov.h @@ -17,6 +17,7 @@ #include #include +#include #define IOV_OF_LVALUE(lval) \ (struct iovec){ .iov_base = &(lval), .iov_len = sizeof(lval) } diff --git a/packet.c b/packet.c index bcac03750dc0..8b11e0850ff6 100644 --- a/packet.c +++ b/packet.c @@ -64,15 +64,16 @@ static int packet_check_range(const struct pool *p, const char *ptr, size_t len, /** * packet_add_do() - Add data as packet descriptor to given pool * @p: Existing pool - * @len: Length of new descriptor - * @start: Start of data + * @data: Data to add * @func: For tracing: name of calling function, NULL means no trace() * @line: For tracing: caller line of function call */ -void packet_add_do(struct pool *p, size_t len, const char *start, +void packet_add_do(struct pool *p, struct iov_tail *data, const char *func, int line) { size_t idx = p->count; + const char *start; + size_t len; if (idx >= p->size) { trace("add packet index %zu to pool with size %zu, %s:%i", @@ -80,6 +81,14 @@ void packet_add_do(struct pool *p, size_t len, const char *start, return; } + if (!iov_tail_prune(data)) + return; + + ASSERT(data->cnt == 1); /* we don't support iovec */ + + len = data->iov[0].iov_len - data->off; + start = (char *)data->iov[0].iov_base + data->off; + if (packet_check_range(p, start, len, func, line)) return; diff --git a/packet.h b/packet.h index d099f026631b..f386295da203 100644 --- a/packet.h +++ b/packet.h @@ -6,6 +6,8 @@ #ifndef PACKET_H #define PACKET_H +#include "iov.h" + /* Maximum size of a single packet stored in pool, including headers */ #define PACKET_MAX_LEN UINT16_MAX @@ -28,15 +30,15 @@ struct pool { }; int vu_packet_check_range(void *buf, const char *ptr, size_t len); -void packet_add_do(struct pool *p, size_t len, const char *start, +void packet_add_do(struct pool *p, struct iov_tail *data, const char *func, int line); void *packet_get_do(const struct pool *p, const size_t idx, size_t offset, size_t len, size_t *left, const char *func, int line); void pool_flush(struct pool *p); -#define packet_add(p, len, start) \ - packet_add_do(p, len, start, __func__, __LINE__) +#define packet_add(p, data) \ + packet_add_do(p, data, __func__, __LINE__) #define packet_get(p, idx, offset, len, left) \ packet_get_do(p, idx, offset, len, left, __func__, __LINE__) diff --git a/tap.c b/tap.c index ab3effe80f89..4b54807c4101 100644 --- a/tap.c +++ b/tap.c @@ -683,6 +683,7 @@ resume: size_t l2len, l3len, hlen, l4len; const struct ethhdr *eh; const struct udphdr *uh; + struct iov_tail data; struct iphdr *iph; const char *l4h; @@ -694,7 +695,8 @@ resume: if (ntohs(eh->h_proto) == ETH_P_ARP) { PACKET_POOL_P(pkt, 1, in->buf, in->buf_size); - packet_add(pkt, l2len, (char *)eh); + data = IOV_TAIL_FROM_BUF((void *)eh, l2len, 0); + packet_add(pkt, &data); arp(c, pkt); continue; } @@ -739,7 +741,8 @@ resume: tap_packet_debug(iph, NULL, NULL, 0, NULL, 1); - packet_add(pkt, l4len, l4h); + data = IOV_TAIL_FROM_BUF((void *)l4h, l4len, 0); + packet_add(pkt, &data); icmp_tap_handler(c, PIF_TAP, AF_INET, &iph->saddr, &iph->daddr, pkt, now); @@ -753,7 +756,8 @@ resume: if (iph->protocol == IPPROTO_UDP) { PACKET_POOL_P(pkt, 1, in->buf, in->buf_size); - packet_add(pkt, l2len, (char *)eh); + data = IOV_TAIL_FROM_BUF((void *)eh, l2len, 0); + packet_add(pkt, &data); if (dhcp(c, pkt)) continue; } @@ -802,7 +806,8 @@ resume: #undef L4_SET append: - packet_add((struct pool *)&seq->p, l4len, l4h); + data = IOV_TAIL_FROM_BUF((void *)l4h, l4len, 0); + packet_add((struct pool *)&seq->p, &data); } for (j = 0, seq = tap4_l4; j < seq_count; j++, seq++) { @@ -858,6 +863,7 @@ resume: struct in6_addr *saddr, *daddr; const struct ethhdr *eh; const struct udphdr *uh; + struct iov_tail data; struct ipv6hdr *ip6h; uint8_t proto; char *l4h; @@ -911,7 +917,8 @@ resume: if (l4len < sizeof(struct icmp6hdr)) continue; - packet_add(pkt, l4len, l4h); + data = IOV_TAIL_FROM_BUF(l4h, l4len, 0); + packet_add(pkt, &data); if (ndp(c, (struct icmp6hdr *)l4h, saddr, pkt)) continue; @@ -930,7 +937,8 @@ resume: if (proto == IPPROTO_UDP) { PACKET_POOL_P(pkt, 1, in->buf, in->buf_size); - packet_add(pkt, l4len, l4h); + data = IOV_TAIL_FROM_BUF(l4h, l4len, 0); + packet_add(pkt, &data); if (dhcpv6(c, pkt, saddr, daddr)) continue; @@ -984,7 +992,8 @@ resume: #undef L4_SET append: - packet_add((struct pool *)&seq->p, l4len, l4h); + data = IOV_TAIL_FROM_BUF(l4h, l4len, 0); + packet_add((struct pool *)&seq->p, &data); } for (j = 0, seq = tap6_l4; j < seq_count; j++, seq++) { @@ -1058,18 +1067,13 @@ void tap_add_packet(struct ctx *c, struct iov_tail *data) proto_update_l2_buf(c->guest_mac, NULL); } - iov_tail_prune(data); - ASSERT(data->cnt == 1); /* packet_add() doesn't support iovec */ - switch (ntohs(eh->h_proto)) { case ETH_P_ARP: case ETH_P_IP: - packet_add(pool_tap4, data->iov[0].iov_len - data->off, - (char *)data->iov[0].iov_base + data->off); + packet_add(pool_tap4, data); break; case ETH_P_IPV6: - packet_add(pool_tap6, data->iov[0].iov_len - data->off, - (char *)data->iov[0].iov_base + data->off); + packet_add(pool_tap6, data); break; default: break; -- 2.49.0