* [PATCH v6 00/30] Introduce discontiguous frames management
@ 2025-06-04 13:08 Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 01/30] arp: Don't mix incoming and outgoing buffers Laurent Vivier
` (29 more replies)
0 siblings, 30 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
This series introduces iov_tail to convey frame information
between functions.
This is only an API change, for the moment the memory pool
is only able to store contiguous buffer, so, except for
vhost-user in a special case, we only play with iovec array
with only one entry.
v6:
- Replaced iov_slice() with the clearer iov_tail_clone()
for creating iovec subsets.
- Standardized local header variable names (to *_storage suffix).
- Renamed functions for better semantics (e.g., ignore_arp to
accept_arp, packet_data to packet_get).
- Corrected OPTLEN_MAX definition in TCP.
- Addressed minor logic issues (e.g., DHCPv6 FQDN flags, NDP null check).
- Updated ipv6_l4hdr() return type to boolean.
- Improved comments and documentation across several modules.
v5:
- store in the pool iovec array with several entries
v4:
Prepare to introduce iovec array in the pool:
- passe iov_tail rather than pool to ndp,icmp, dhcp, dhcpv6 and arp
- remove unused pool macros
- add memory regions in the pool structure, this will allow us to use
the buf pointer to store the iovec array for vhost-user
v3:
Address comments from David
Laurent Vivier (30):
arp: Don't mix incoming and outgoing buffers
iov: Introduce iov_tail_clone() and iov_tail_drop().
iov: Update IOV_REMOVE_HEADER() and IOV_PEEK_HEADER()
tap: Use iov_tail with tap_add_packet()
packet: Use iov_tail with packet_add()
packet: Add packet_data()
arp: Convert to iov_tail
ndp: Convert to iov_tail
icmp: Convert to iov_tail
udp: Convert to iov_tail
tcp: Convert tcp_tap_handler() to use iov_tail
tcp: Convert tcp_data_from_tap() to use iov_tail
dhcpv6: move offset initialization out of dhcpv6_opt()
dhcpv6: Extract sending of NotOnLink status
dhcpv6: Convert to iov_tail
dhcpv6: Use iov_tail in dhcpv6_opt()
dhcp: Convert to iov_tail
ip: Use iov_tail in ipv6_l4hdr()
tap: Convert tap4_handler() to iov_tail
tap: Convert tap6_handler() to iov_tail
packet: rename packet_data() to packet_get()
arp: use iov_tail rather than pool
dhcp: use iov_tail rather than pool
dhcpv6: use iov_tail rather than pool
icmp: use iov_tail rather than pool
ndp: use iov_tail rather than pool
packet: remove PACKET_POOL() and PACKET_POOL_P()
packet: remove unused parameter from PACKET_POOL_DECL()
packet: add memory regions information into pool
packet: use buf to store iovec array
arp.c | 86 ++++++++++++-------
arp.h | 2 +-
dhcp.c | 47 ++++++-----
dhcp.h | 2 +-
dhcpv6.c | 222 +++++++++++++++++++++++++++++--------------------
dhcpv6.h | 2 +-
icmp.c | 41 ++++++----
icmp.h | 2 +-
iov.c | 102 +++++++++++++++++++++--
iov.h | 58 +++++++++----
ip.c | 32 ++++----
ip.h | 3 +-
ndp.c | 16 +++-
ndp.h | 4 +-
packet.c | 227 ++++++++++++++++++++++++++++++++++++++-------------
packet.h | 57 ++++++-------
pcap.c | 1 +
tap.c | 134 +++++++++++++++++-------------
tap.h | 4 +-
tcp.c | 61 ++++++++++----
tcp_buf.c | 2 +-
udp.c | 33 +++++---
vhost_user.c | 28 +++----
virtio.c | 4 +-
virtio.h | 18 +++-
vu_common.c | 48 ++++-------
26 files changed, 799 insertions(+), 437 deletions(-)
--
2.49.0
^ permalink raw reply [flat|nested] 31+ messages in thread
* [PATCH v6 01/30] arp: Don't mix incoming and outgoing buffers
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 02/30] iov: Introduce iov_tail_clone() and iov_tail_drop() Laurent Vivier
` (28 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, David Gibson
Don't use the memory of the incoming packet to build the outgoing buffer
as it can be memory of the TX queue in the case of vhost-user.
Moreover with vhost-user, the packet can be split across several
iovec and it's easier to rebuild it in a buffer than updating an
existing iovec array.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
arp.c | 84 ++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 55 insertions(+), 29 deletions(-)
diff --git a/arp.c b/arp.c
index fc482bbd9938..721ff2d529b5 100644
--- a/arp.c
+++ b/arp.c
@@ -31,56 +31,82 @@
#include "tap.h"
/**
- * arp() - Check if this is a supported ARP message, reply as needed
+ * accept_arp() - Check if we should accept this ARP message
* @c: Execution context
- * @p: Packet pool, single packet with Ethernet buffer
+ * @ah: ARP header
+ * @am: ARP message
*
- * Return: 1 if handled, -1 on failure
+ * Return: true if the message is supported, false otherwise.
*/
-int arp(const struct ctx *c, const struct pool *p)
+static bool accept_arp(const struct ctx *c,
+ const struct arphdr *ah, const struct arpmsg *am)
{
- unsigned char swap[4];
- struct ethhdr *eh;
- struct arphdr *ah;
- struct arpmsg *am;
- size_t l2len;
-
- eh = packet_get(p, 0, 0, sizeof(*eh), NULL);
- ah = packet_get(p, 0, sizeof(*eh), sizeof(*ah), NULL);
- am = packet_get(p, 0, sizeof(*eh) + sizeof(*ah), sizeof(*am), NULL);
-
- if (!eh || !ah || !am)
- return -1;
-
if (ah->ar_hrd != htons(ARPHRD_ETHER) ||
ah->ar_pro != htons(ETH_P_IP) ||
ah->ar_hln != ETH_ALEN ||
ah->ar_pln != 4 ||
ah->ar_op != htons(ARPOP_REQUEST))
- return 1;
+ return true;
/* Discard announcements, but not 0.0.0.0 "probes" */
if (memcmp(am->sip, &in4addr_any, sizeof(am->sip)) &&
!memcmp(am->sip, am->tip, sizeof(am->sip)))
- return 1;
+ return true;
/* Don't resolve the guest's assigned address, either. */
if (!memcmp(am->tip, &c->ip4.addr, sizeof(am->tip)))
+ return true;
+
+ return false;
+}
+
+/**
+ * arp() - Check if this is a supported ARP message, reply as needed
+ * @c: Execution context
+ * @p: Packet pool, single packet with Ethernet buffer
+ *
+ * Return: 1 if handled, -1 on failure
+ */
+int arp(const struct ctx *c, const struct pool *p)
+{
+ struct {
+ struct ethhdr eh;
+ struct arphdr ah;
+ struct arpmsg am;
+ } __attribute__((__packed__)) resp;
+ const struct ethhdr *eh;
+ const struct arphdr *ah;
+ const struct arpmsg *am;
+
+ eh = packet_get(p, 0, 0, sizeof(*eh), NULL);
+ ah = packet_get(p, 0, sizeof(*eh), sizeof(*ah), NULL);
+ am = packet_get(p, 0, sizeof(*eh) + sizeof(*ah), sizeof(*am), NULL);
+
+ if (!eh || !ah || !am)
+ return -1;
+
+ if (accept_arp(c, ah, am))
return 1;
- ah->ar_op = htons(ARPOP_REPLY);
- memcpy(am->tha, am->sha, sizeof(am->tha));
- memcpy(am->sha, c->our_tap_mac, sizeof(am->sha));
+ /* Ethernet header */
+ resp.eh.h_proto = htons(ETH_P_ARP);
+ memcpy(resp.eh.h_dest, eh->h_source, sizeof(resp.eh.h_dest));
+ memcpy(resp.eh.h_source, c->our_tap_mac, sizeof(resp.eh.h_source));
- memcpy(swap, am->tip, sizeof(am->tip));
- memcpy(am->tip, am->sip, sizeof(am->tip));
- memcpy(am->sip, swap, sizeof(am->sip));
+ /* ARP header */
+ resp.ah.ar_op = htons(ARPOP_REPLY);
+ resp.ah.ar_hrd = ah->ar_hrd;
+ resp.ah.ar_pro = ah->ar_pro;
+ resp.ah.ar_hln = ah->ar_hln;
+ resp.ah.ar_pln = ah->ar_pln;
- l2len = sizeof(*eh) + sizeof(*ah) + sizeof(*am);
- memcpy(eh->h_dest, eh->h_source, sizeof(eh->h_dest));
- memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source));
+ /* ARP message */
+ memcpy(resp.am.sha, c->our_tap_mac, sizeof(resp.am.sha));
+ memcpy(resp.am.sip, am->tip, sizeof(resp.am.sip));
+ memcpy(resp.am.tha, am->sha, sizeof(resp.am.tha));
+ memcpy(resp.am.tip, am->sip, sizeof(resp.am.tip));
- tap_send_single(c, eh, l2len);
+ tap_send_single(c, &resp, sizeof(resp));
return 1;
}
--
@@ -31,56 +31,82 @@
#include "tap.h"
/**
- * arp() - Check if this is a supported ARP message, reply as needed
+ * accept_arp() - Check if we should accept this ARP message
* @c: Execution context
- * @p: Packet pool, single packet with Ethernet buffer
+ * @ah: ARP header
+ * @am: ARP message
*
- * Return: 1 if handled, -1 on failure
+ * Return: true if the message is supported, false otherwise.
*/
-int arp(const struct ctx *c, const struct pool *p)
+static bool accept_arp(const struct ctx *c,
+ const struct arphdr *ah, const struct arpmsg *am)
{
- unsigned char swap[4];
- struct ethhdr *eh;
- struct arphdr *ah;
- struct arpmsg *am;
- size_t l2len;
-
- eh = packet_get(p, 0, 0, sizeof(*eh), NULL);
- ah = packet_get(p, 0, sizeof(*eh), sizeof(*ah), NULL);
- am = packet_get(p, 0, sizeof(*eh) + sizeof(*ah), sizeof(*am), NULL);
-
- if (!eh || !ah || !am)
- return -1;
-
if (ah->ar_hrd != htons(ARPHRD_ETHER) ||
ah->ar_pro != htons(ETH_P_IP) ||
ah->ar_hln != ETH_ALEN ||
ah->ar_pln != 4 ||
ah->ar_op != htons(ARPOP_REQUEST))
- return 1;
+ return true;
/* Discard announcements, but not 0.0.0.0 "probes" */
if (memcmp(am->sip, &in4addr_any, sizeof(am->sip)) &&
!memcmp(am->sip, am->tip, sizeof(am->sip)))
- return 1;
+ return true;
/* Don't resolve the guest's assigned address, either. */
if (!memcmp(am->tip, &c->ip4.addr, sizeof(am->tip)))
+ return true;
+
+ return false;
+}
+
+/**
+ * arp() - Check if this is a supported ARP message, reply as needed
+ * @c: Execution context
+ * @p: Packet pool, single packet with Ethernet buffer
+ *
+ * Return: 1 if handled, -1 on failure
+ */
+int arp(const struct ctx *c, const struct pool *p)
+{
+ struct {
+ struct ethhdr eh;
+ struct arphdr ah;
+ struct arpmsg am;
+ } __attribute__((__packed__)) resp;
+ const struct ethhdr *eh;
+ const struct arphdr *ah;
+ const struct arpmsg *am;
+
+ eh = packet_get(p, 0, 0, sizeof(*eh), NULL);
+ ah = packet_get(p, 0, sizeof(*eh), sizeof(*ah), NULL);
+ am = packet_get(p, 0, sizeof(*eh) + sizeof(*ah), sizeof(*am), NULL);
+
+ if (!eh || !ah || !am)
+ return -1;
+
+ if (accept_arp(c, ah, am))
return 1;
- ah->ar_op = htons(ARPOP_REPLY);
- memcpy(am->tha, am->sha, sizeof(am->tha));
- memcpy(am->sha, c->our_tap_mac, sizeof(am->sha));
+ /* Ethernet header */
+ resp.eh.h_proto = htons(ETH_P_ARP);
+ memcpy(resp.eh.h_dest, eh->h_source, sizeof(resp.eh.h_dest));
+ memcpy(resp.eh.h_source, c->our_tap_mac, sizeof(resp.eh.h_source));
- memcpy(swap, am->tip, sizeof(am->tip));
- memcpy(am->tip, am->sip, sizeof(am->tip));
- memcpy(am->sip, swap, sizeof(am->sip));
+ /* ARP header */
+ resp.ah.ar_op = htons(ARPOP_REPLY);
+ resp.ah.ar_hrd = ah->ar_hrd;
+ resp.ah.ar_pro = ah->ar_pro;
+ resp.ah.ar_hln = ah->ar_hln;
+ resp.ah.ar_pln = ah->ar_pln;
- l2len = sizeof(*eh) + sizeof(*ah) + sizeof(*am);
- memcpy(eh->h_dest, eh->h_source, sizeof(eh->h_dest));
- memcpy(eh->h_source, c->our_tap_mac, sizeof(eh->h_source));
+ /* ARP message */
+ memcpy(resp.am.sha, c->our_tap_mac, sizeof(resp.am.sha));
+ memcpy(resp.am.sip, am->tip, sizeof(resp.am.sip));
+ memcpy(resp.am.tha, am->sha, sizeof(resp.am.tha));
+ memcpy(resp.am.tip, am->sip, sizeof(resp.am.tip));
- tap_send_single(c, eh, l2len);
+ tap_send_single(c, &resp, sizeof(resp));
return 1;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 02/30] iov: Introduce iov_tail_clone() and iov_tail_drop().
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 01/30] arp: Don't mix incoming and outgoing buffers Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 03/30] iov: Update IOV_REMOVE_HEADER() and IOV_PEEK_HEADER() Laurent Vivier
` (27 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
These utilities enhance iov_tail manipulation, useful for
efficient packet processing by enabling iovec array cloning and
header stripping without data copies.
- iov_tail_drop(): Discards a specified number of bytes from the
beginning of an iov_tail by advancing its internal offset and pruning
consumed elements.
- iov_tail_clone(): Clone an iov_tail into an iovec array, adjusting the
first iovec entry to remove the iov_tail offset.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
iov.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
iov.h | 3 +++
2 files changed, 55 insertions(+)
diff --git a/iov.c b/iov.c
index 91e87a740f0a..3b89f8126ee7 100644
--- a/iov.c
+++ b/iov.c
@@ -192,6 +192,21 @@ size_t iov_tail_size(struct iov_tail *tail)
return iov_size(tail->iov, tail->cnt) - tail->off;
}
+/**
+ * iov_tail_drop() - Discard a header from an IOV tail
+ * @tail: IO vector tail
+ * @len: length to move the head of the tail
+ *
+ * Returns: true if the item still contains any bytes, otherwise false
+ */
+/* cppcheck-suppress unusedFunction */
+bool iov_tail_drop(struct iov_tail *tail, size_t len)
+{
+ tail->off = tail->off + len;
+
+ return iov_tail_prune(tail);
+}
+
/**
* iov_peek_header_() - Get pointer to a header from an IOV tail
* @tail: IOV tail to get header from
@@ -248,3 +263,40 @@ void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align)
tail->off = tail->off + len;
return p;
}
+
+/**
+ * iov_tail_clone() - Assign iov references referencing a subset of the data
+ * in an iov_tail
+ *
+ * @dst_iov: Pointer to the destination array of struct iovec describing
+ * the scatter/gather I/O vector to copy to.
+ * @dst_iov_cnt: Maximum number of elements in the destination iov array.
+ * @tail: Pointer to the source iov_tail
+ *
+ * Returns: The number of elements successfully copied to the destination
+ * iov array, a negative value if there is not enough room in the
+ * destination iov array
+ */
+/* cppcheck-suppress unusedFunction */
+ssize_t iov_tail_clone(struct iovec *dst_iov, size_t dst_iov_cnt,
+ struct iov_tail *tail)
+{
+ const struct iovec *iov = &tail->iov[0];
+ size_t iov_cnt = tail->cnt;
+ size_t offset = tail->off;
+ unsigned int i, j;
+
+ i = iov_skip_bytes(iov, iov_cnt, offset, &offset);
+
+ /* assign iov references referencing a subset of the source one */
+ for (j = 0; i < iov_cnt && j < dst_iov_cnt; i++, j++) {
+ dst_iov[j].iov_base = (char *)iov[i].iov_base + offset;
+ dst_iov[j].iov_len = iov[i].iov_len - offset;
+ offset = 0;
+ }
+
+ if (j == dst_iov_cnt && i != iov_cnt)
+ return -1;
+
+ return j;
+}
diff --git a/iov.h b/iov.h
index 9855bf0c0c32..11a8567aa183 100644
--- a/iov.h
+++ b/iov.h
@@ -72,8 +72,11 @@ struct iov_tail {
bool iov_tail_prune(struct iov_tail *tail);
size_t iov_tail_size(struct iov_tail *tail);
+bool iov_tail_drop(struct iov_tail *tail, size_t len);
void *iov_peek_header_(struct iov_tail *tail, size_t len, size_t align);
void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align);
+ssize_t iov_tail_clone(struct iovec *dst_iov, size_t dst_iov_cnt,
+ struct iov_tail *tail);
/**
* IOV_PEEK_HEADER() - Get typed pointer to a header from an IOV tail
--
@@ -72,8 +72,11 @@ struct iov_tail {
bool iov_tail_prune(struct iov_tail *tail);
size_t iov_tail_size(struct iov_tail *tail);
+bool iov_tail_drop(struct iov_tail *tail, size_t len);
void *iov_peek_header_(struct iov_tail *tail, size_t len, size_t align);
void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align);
+ssize_t iov_tail_clone(struct iovec *dst_iov, size_t dst_iov_cnt,
+ struct iov_tail *tail);
/**
* IOV_PEEK_HEADER() - Get typed pointer to a header from an IOV tail
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 03/30] iov: Update IOV_REMOVE_HEADER() and IOV_PEEK_HEADER()
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 01/30] arp: Don't mix incoming and outgoing buffers Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 02/30] iov: Introduce iov_tail_clone() and iov_tail_drop() Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 04/30] tap: Use iov_tail with tap_add_packet() Laurent Vivier
` (26 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Provide a temporary variable of the wanted type to store
the header if the memory in the iovec array is not contiguous.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
iov.c | 53 ++++++++++++++++++++++++++++++++++++++++++++---------
iov.h | 55 +++++++++++++++++++++++++++++++++++++++++--------------
tcp_buf.c | 2 +-
3 files changed, 86 insertions(+), 24 deletions(-)
diff --git a/iov.c b/iov.c
index 3b89f8126ee7..541ca8aa9119 100644
--- a/iov.c
+++ b/iov.c
@@ -109,7 +109,7 @@ size_t iov_from_buf(const struct iovec *iov, size_t iov_cnt,
*
* Returns: The number of bytes successfully copied.
*/
-/* cppcheck-suppress unusedFunction */
+/* cppcheck-suppress [staticFunction] */
size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt,
size_t offset, void *buf, size_t bytes)
{
@@ -127,6 +127,7 @@ size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt,
/* copying data */
for (copied = 0; copied < bytes && i < iov_cnt; i++) {
size_t len = MIN(iov[i].iov_len - offset, bytes - copied);
+ /* NOLINTNEXTLINE(clang-analyzer-core.NonNullParamChecker) */
memcpy((char *)buf + copied, (char *)iov[i].iov_base + offset,
len);
copied += len;
@@ -208,7 +209,7 @@ bool iov_tail_drop(struct iov_tail *tail, size_t len)
}
/**
- * iov_peek_header_() - Get pointer to a header from an IOV tail
+ * iov_check_header() - Check if a header can be accessed
* @tail: IOV tail to get header from
* @len: Length of header to get, in bytes
* @align: Required alignment of header, in bytes
@@ -219,8 +220,7 @@ bool iov_tail_drop(struct iov_tail *tail, size_t len)
* overruns the IO vector, is not contiguous or doesn't have the
* requested alignment.
*/
-/* cppcheck-suppress [staticFunction,unmatchedSuppression] */
-void *iov_peek_header_(struct iov_tail *tail, size_t len, size_t align)
+static void *iov_check_header(struct iov_tail *tail, size_t len, size_t align)
{
char *p;
@@ -240,27 +240,62 @@ void *iov_peek_header_(struct iov_tail *tail, size_t len, size_t align)
return p;
}
+/**
+ * iov_peek_header_() - Get pointer to a header from an IOV tail
+ * @tail: IOV tail to get header from
+ * @v: Temporary memory to use if the memory in @tail
+ * is discontinuous
+ * @len: Length of header to get, in bytes
+ * @align: Required alignment of header, in bytes
+ *
+ * @tail may be pruned, but will represent the same bytes as before.
+ *
+ * Returns: Pointer to the first @len logical bytes of the tail, or to
+ * a copy if that overruns the IO vector, is not contiguous or
+ * doesn't have the requested alignment. NULL if that overruns the
+ * IO vector.
+ */
+/* cppcheck-suppress [staticFunction,unmatchedSuppression] */
+void *iov_peek_header_(struct iov_tail *tail, void *v, size_t len, size_t align)
+{
+ char *p = iov_check_header(tail, len, align);
+ size_t l;
+
+ if (p)
+ return p;
+
+ l = iov_to_buf(tail->iov, tail->cnt, tail->off, v, len);
+ if (l != len)
+ return NULL;
+
+ return v;
+}
+
/**
* iov_remove_header_() - Remove a header from an IOV tail
* @tail: IOV tail to remove header from (modified)
+ * @v: Temporary memory to use if the memory in @tail
+ * is discontinuous
* @len: Length of header to remove, in bytes
* @align: Required alignment of header, in bytes
*
* On success, @tail is updated so that it longer includes the bytes of the
* returned header.
*
- * Returns: Pointer to the first @len logical bytes of the tail, NULL if that
- * overruns the IO vector, is not contiguous or doesn't have the
- * requested alignment.
+ * Returns: Pointer to the first @len logical bytes of the tail, or to
+ * a copy if that overruns the IO vector, is not contiguous or
+ * doesn't have the requested alignment. NULL if that overruns the
+ * IO vector.
*/
-void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align)
+void *iov_remove_header_(struct iov_tail *tail, void *v, size_t len, size_t align)
{
- char *p = iov_peek_header_(tail, len, align);
+ char *p = iov_peek_header_(tail, v, len, align);
if (!p)
return NULL;
tail->off = tail->off + len;
+
return p;
}
diff --git a/iov.h b/iov.h
index 11a8567aa183..7d5bce2f841c 100644
--- a/iov.h
+++ b/iov.h
@@ -70,41 +70,68 @@ struct iov_tail {
#define IOV_TAIL(iov_, cnt_, off_) \
(struct iov_tail){ .iov = (iov_), .cnt = (cnt_), .off = (off_) }
+/**
+ * IOV_TAIL_FROM_BUF() - Create a new IOV tail from a buffer
+ * @buf_: Buffer address to use in the iovec
+ * @len_: Buffer size
+ * @off_: Byte offset in the buffer where the tail begins
+ */
+#define IOV_TAIL_FROM_BUF(buf_, len_, off_) \
+ IOV_TAIL((&(const struct iovec){ .iov_base = (buf_), \
+ .iov_len = (len_) }), \
+ 1, \
+ (off_))
+
bool iov_tail_prune(struct iov_tail *tail);
size_t iov_tail_size(struct iov_tail *tail);
bool iov_tail_drop(struct iov_tail *tail, size_t len);
-void *iov_peek_header_(struct iov_tail *tail, size_t len, size_t align);
-void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align);
+void *iov_peek_header_(struct iov_tail *tail, void *v, size_t len, size_t align);
+void *iov_remove_header_(struct iov_tail *tail, void *v, size_t len, size_t align);
ssize_t iov_tail_clone(struct iovec *dst_iov, size_t dst_iov_cnt,
struct iov_tail *tail);
/**
* IOV_PEEK_HEADER() - Get typed pointer to a header from an IOV tail
* @tail_: IOV tail to get header from
- * @type_: Data type of the header
+ * @var_: Temporary buffer of the type of the header to use if
+ * the memory in the iovec array is not contiguous.
*
* @tail_ may be pruned, but will represent the same bytes as before.
*
- * Returns: Pointer of type (@type_ *) located at the start of @tail_, NULL if
- * we can't get a contiguous and aligned pointer.
+ * Returns: Pointer of type (@type_ *) located at the start of @tail_
+ * or to @var_ if iovec memory is not contiguous, NULL if
+ * that overruns the iovec.
*/
-#define IOV_PEEK_HEADER(tail_, type_) \
- ((type_ *)(iov_peek_header_((tail_), \
- sizeof(type_), __alignof__(type_))))
+
+#define IOV_PEEK_HEADER(tail_, var_) \
+ ((__typeof__(var_) *)(iov_peek_header_((tail_), &(var_), \
+ sizeof(var_), \
+ __alignof__(var_))))
/**
* IOV_REMOVE_HEADER() - Remove and return typed header from an IOV tail
* @tail_: IOV tail to remove header from (modified)
- * @type_: Data type of the header to remove
+ * @var_: Temporary buffer of the type of the header to use if
+ * the memory in the iovec array is not contiguous.
*
* On success, @tail_ is updated so that it longer includes the bytes of the
* returned header.
*
- * Returns: Pointer of type (@type_ *) located at the old start of @tail_, NULL
- * if we can't get a contiguous and aligned pointer.
+ * Returns: Pointer of type (@type_ *) located at the start of @tail_
+ * or to @var_ if iovec memory is not contiguous, NULL if
+ * that overruns the iovec.
+ */
+
+#define IOV_REMOVE_HEADER(tail_, var_) \
+ ((__typeof__(var_) *)(iov_remove_header_((tail_), &(var_), \
+ sizeof(var_), __alignof__(var_))))
+
+/** IOV_DROP_HEADER() - Remove a typed header from an IOV tail
+ * @tail_: IOV tail to remove header from (modified)
+ * @type_: Data type of the header to remove
+ *
+ * Returns: true if the tail still contains any bytes, otherwise false
*/
-#define IOV_REMOVE_HEADER(tail_, type_) \
- ((type_ *)(iov_remove_header_((tail_), \
- sizeof(type_), __alignof__(type_))))
+#define IOV_DROP_HEADER(tail_, type_) iov_tail_drop((tail_), sizeof(type_))
#endif /* IOVEC_H */
diff --git a/tcp_buf.c b/tcp_buf.c
index 05305636b503..ac6d73130f55 100644
--- a/tcp_buf.c
+++ b/tcp_buf.c
@@ -160,7 +160,7 @@ static void tcp_l2_buf_fill_headers(const struct tcp_tap_conn *conn,
uint32_t seq, bool no_tcp_csum)
{
struct iov_tail tail = IOV_TAIL(&iov[TCP_IOV_PAYLOAD], 1, 0);
- struct tcphdr *th = IOV_REMOVE_HEADER(&tail, struct tcphdr);
+ struct tcphdr th_storage, *th = IOV_REMOVE_HEADER(&tail, th_storage);
struct tap_hdr *taph = iov[TCP_IOV_TAP].iov_base;
const struct flowside *tapside = TAPFLOW(conn);
const struct in_addr *a4 = inany_v4(&tapside->oaddr);
--
@@ -160,7 +160,7 @@ static void tcp_l2_buf_fill_headers(const struct tcp_tap_conn *conn,
uint32_t seq, bool no_tcp_csum)
{
struct iov_tail tail = IOV_TAIL(&iov[TCP_IOV_PAYLOAD], 1, 0);
- struct tcphdr *th = IOV_REMOVE_HEADER(&tail, struct tcphdr);
+ struct tcphdr th_storage, *th = IOV_REMOVE_HEADER(&tail, th_storage);
struct tap_hdr *taph = iov[TCP_IOV_TAP].iov_base;
const struct flowside *tapside = TAPFLOW(conn);
const struct in_addr *a4 = inany_v4(&tapside->oaddr);
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 04/30] tap: Use iov_tail with tap_add_packet()
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (2 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 03/30] iov: Update IOV_REMOVE_HEADER() and IOV_PEEK_HEADER() Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 05/30] packet: Use iov_tail with packet_add() Laurent Vivier
` (25 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, David Gibson
Use IOV_PEEK_HEADER() to get the ethernet header from the iovec.
Move the workaround about multiple iovec array from vu_handle_tx() to
tap_add_packet(). Removing the offset out of the iovec array should
reduce the iovec count to 1.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
iov.c | 1 -
pcap.c | 1 +
tap.c | 30 +++++++++++++++++++++---------
tap.h | 3 +--
vu_common.c | 26 +++++---------------------
5 files changed, 28 insertions(+), 33 deletions(-)
diff --git a/iov.c b/iov.c
index 541ca8aa9119..cc50ea9e3e6c 100644
--- a/iov.c
+++ b/iov.c
@@ -200,7 +200,6 @@ size_t iov_tail_size(struct iov_tail *tail)
*
* Returns: true if the item still contains any bytes, otherwise false
*/
-/* cppcheck-suppress unusedFunction */
bool iov_tail_drop(struct iov_tail *tail, size_t len)
{
tail->off = tail->off + len;
diff --git a/pcap.c b/pcap.c
index e95aa6fe29a6..404043a27e22 100644
--- a/pcap.c
+++ b/pcap.c
@@ -76,6 +76,7 @@ static void pcap_frame(const struct iovec *iov, size_t iovcnt,
* @pkt: Pointer to data buffer, including L2 headers
* @l2len: L2 frame length
*/
+/* cppcheck-suppress unusedFunction */
void pcap(const char *pkt, size_t l2len)
{
struct iovec iov = { (char *)pkt, l2len };
diff --git a/tap.c b/tap.c
index d630f6df97f1..db4f6e40781f 100644
--- a/tap.c
+++ b/tap.c
@@ -1068,24 +1068,29 @@ void tap_handler(struct ctx *c, const struct timespec *now)
/**
* tap_add_packet() - Queue/capture packet, update notion of guest MAC address
* @c: Execution context
- * @l2len: Total L2 packet length
- * @p: Packet buffer
+ * @data: Packet to add to the pool
* @now: Current timestamp
*/
-void tap_add_packet(struct ctx *c, ssize_t l2len, char *p,
+void tap_add_packet(struct ctx *c, struct iov_tail *data,
const struct timespec *now)
{
+ struct ethhdr eh_storage;
const struct ethhdr *eh;
- pcap(p, l2len);
+ pcap_iov(data->iov, data->cnt, data->off);
- eh = (struct ethhdr *)p;
+ eh = IOV_PEEK_HEADER(data, eh_storage);
+ if (!eh)
+ return;
if (memcmp(c->guest_mac, eh->h_source, ETH_ALEN)) {
memcpy(c->guest_mac, eh->h_source, ETH_ALEN);
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:
@@ -1093,14 +1098,16 @@ void tap_add_packet(struct ctx *c, ssize_t l2len, char *p,
tap4_handler(c, pool_tap4, now);
pool_flush(pool_tap4);
}
- packet_add(pool_tap4, l2len, p);
+ packet_add(pool_tap4, data->iov[0].iov_len - data->off,
+ (char *)data->iov[0].iov_base + data->off);
break;
case ETH_P_IPV6:
if (pool_full(pool_tap6)) {
tap6_handler(c, pool_tap6, now);
pool_flush(pool_tap6);
}
- packet_add(pool_tap6, l2len, p);
+ packet_add(pool_tap6, data->iov[0].iov_len - data->off,
+ (char *)data->iov[0].iov_base + data->off);
break;
default:
break;
@@ -1166,6 +1173,7 @@ static void tap_passt_input(struct ctx *c, const struct timespec *now)
while (n >= (ssize_t)sizeof(uint32_t)) {
uint32_t l2len = ntohl_unaligned(p);
+ struct iov_tail data;
if (l2len < sizeof(struct ethhdr) || l2len > L2_MAX_LEN_PASST) {
err("Bad frame size from guest, resetting connection");
@@ -1180,7 +1188,8 @@ static void tap_passt_input(struct ctx *c, const struct timespec *now)
p += sizeof(uint32_t);
n -= sizeof(uint32_t);
- tap_add_packet(c, l2len, p, now);
+ data = IOV_TAIL_FROM_BUF(p, l2len, 0);
+ tap_add_packet(c, &data, now);
p += l2len;
n -= l2len;
@@ -1224,6 +1233,8 @@ static void tap_pasta_input(struct ctx *c, const struct timespec *now)
for (n = 0;
n <= (ssize_t)(sizeof(pkt_buf) - L2_MAX_LEN_PASTA);
n += len) {
+ struct iov_tail data;
+
len = read(c->fd_tap, pkt_buf + n, L2_MAX_LEN_PASTA);
if (len == 0) {
@@ -1245,7 +1256,8 @@ static void tap_pasta_input(struct ctx *c, const struct timespec *now)
len > (ssize_t)L2_MAX_LEN_PASTA)
continue;
- tap_add_packet(c, len, pkt_buf + n, now);
+ data = IOV_TAIL_FROM_BUF(pkt_buf + n, len, 0);
+ tap_add_packet(c, &data, now);
}
tap_handler(c, now);
diff --git a/tap.h b/tap.h
index 6fe3d15d1337..954ad440a287 100644
--- a/tap.h
+++ b/tap.h
@@ -119,7 +119,6 @@ void tap_sock_update_pool(void *base, size_t size);
void tap_backend_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,
+void tap_add_packet(struct ctx *c, struct iov_tail *data,
const struct timespec *now);
-
#endif /* TAP_H */
diff --git a/vu_common.c b/vu_common.c
index 5e6fd4a8261f..b77b21420c57 100644
--- a/vu_common.c
+++ b/vu_common.c
@@ -163,7 +163,6 @@ static void vu_handle_tx(struct vu_dev *vdev, int index,
struct vu_virtq_element elem[VIRTQUEUE_MAX_SIZE];
struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
struct vu_virtq *vq = &vdev->vq[index];
- int hdrlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
int out_sg_count;
int count;
@@ -176,6 +175,7 @@ static void vu_handle_tx(struct vu_dev *vdev, int index,
while (count < VIRTQUEUE_MAX_SIZE &&
out_sg_count + VU_MAX_TX_BUFFER_NB <= VIRTQUEUE_MAX_SIZE) {
int ret;
+ struct iov_tail data;
elem[count].out_num = VU_MAX_TX_BUFFER_NB;
elem[count].out_sg = &out_sg[out_sg_count];
@@ -191,26 +191,10 @@ static void vu_handle_tx(struct vu_dev *vdev, int index,
warn("virtio-net transmit queue contains no out buffers");
break;
}
- if (elem[count].out_num == 1) {
- tap_add_packet(vdev->context,
- elem[count].out_sg[0].iov_len - hdrlen,
- (char *)elem[count].out_sg[0].iov_base +
- hdrlen, now);
- } else {
- /* vnet header can be in a separate iovec */
- if (elem[count].out_num != 2) {
- debug("virtio-net transmit queue contains more than one buffer ([%d]: %u)",
- count, elem[count].out_num);
- } else if (elem[count].out_sg[0].iov_len != (size_t)hdrlen) {
- debug("virtio-net transmit queue entry not aligned on hdrlen ([%d]: %d != %zu)",
- count, hdrlen, elem[count].out_sg[0].iov_len);
- } else {
- tap_add_packet(vdev->context,
- elem[count].out_sg[1].iov_len,
- (char *)elem[count].out_sg[1].iov_base,
- now);
- }
- }
+
+ data = IOV_TAIL(elem[count].out_sg, elem[count].out_num, 0);
+ if (IOV_DROP_HEADER(&data, struct virtio_net_hdr_mrg_rxbuf))
+ tap_add_packet(vdev->context, &data, now);
count++;
}
--
@@ -163,7 +163,6 @@ static void vu_handle_tx(struct vu_dev *vdev, int index,
struct vu_virtq_element elem[VIRTQUEUE_MAX_SIZE];
struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
struct vu_virtq *vq = &vdev->vq[index];
- int hdrlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
int out_sg_count;
int count;
@@ -176,6 +175,7 @@ static void vu_handle_tx(struct vu_dev *vdev, int index,
while (count < VIRTQUEUE_MAX_SIZE &&
out_sg_count + VU_MAX_TX_BUFFER_NB <= VIRTQUEUE_MAX_SIZE) {
int ret;
+ struct iov_tail data;
elem[count].out_num = VU_MAX_TX_BUFFER_NB;
elem[count].out_sg = &out_sg[out_sg_count];
@@ -191,26 +191,10 @@ static void vu_handle_tx(struct vu_dev *vdev, int index,
warn("virtio-net transmit queue contains no out buffers");
break;
}
- if (elem[count].out_num == 1) {
- tap_add_packet(vdev->context,
- elem[count].out_sg[0].iov_len - hdrlen,
- (char *)elem[count].out_sg[0].iov_base +
- hdrlen, now);
- } else {
- /* vnet header can be in a separate iovec */
- if (elem[count].out_num != 2) {
- debug("virtio-net transmit queue contains more than one buffer ([%d]: %u)",
- count, elem[count].out_num);
- } else if (elem[count].out_sg[0].iov_len != (size_t)hdrlen) {
- debug("virtio-net transmit queue entry not aligned on hdrlen ([%d]: %d != %zu)",
- count, hdrlen, elem[count].out_sg[0].iov_len);
- } else {
- tap_add_packet(vdev->context,
- elem[count].out_sg[1].iov_len,
- (char *)elem[count].out_sg[1].iov_base,
- now);
- }
- }
+
+ data = IOV_TAIL(elem[count].out_sg, elem[count].out_num, 0);
+ if (IOV_DROP_HEADER(&data, struct virtio_net_hdr_mrg_rxbuf))
+ tap_add_packet(vdev->context, &data, now);
count++;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 05/30] packet: Use iov_tail with packet_add()
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (3 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 04/30] tap: Use iov_tail with tap_add_packet() Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 06/30] packet: Add packet_data() Laurent Vivier
` (24 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, David Gibson
Modify the interface of packet_add_do() to take an iov_tail
rather than a memory pointer and length.
Internally it only supports iovec array with only one entry,
after being pruned. We can accept iovec array with several
entries if the offset allows the function to reduce the number
of entries to 1.
tap4_handler() is updated to create an iov_tail value using
IOV_TAIL_FROM_BUF() from the buffer and the length.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
packet.c | 15 ++++++++++++---
packet.h | 7 ++++---
tap.c | 32 ++++++++++++++++++--------------
3 files changed, 34 insertions(+), 20 deletions(-)
diff --git a/packet.c b/packet.c
index 72c61580be1e..98ded4e27aae 100644
--- a/packet.c
+++ b/packet.c
@@ -87,15 +87,16 @@ bool pool_full(const struct pool *p)
/**
* 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
* @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 (pool_full(p)) {
debug("add packet index %zu to pool with size %zu, %s:%i",
@@ -103,6 +104,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 c94780a5ea54..af40b39b5251 100644
--- a/packet.h
+++ b/packet.h
@@ -7,6 +7,7 @@
#define PACKET_H
#include <stdbool.h>
+#include "iov.h"
/* Maximum size of a single packet stored in pool, including headers */
#define PACKET_MAX_LEN ((size_t)UINT16_MAX)
@@ -30,7 +31,7 @@ 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_try_do(const struct pool *p, const size_t idx,
size_t offset, size_t len, size_t *left,
@@ -41,8 +42,8 @@ void *packet_get_do(const struct pool *p, const size_t idx,
bool pool_full(const struct pool *p);
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_try(p, idx, offset, len, left) \
packet_get_try_do(p, idx, offset, len, left, __func__, __LINE__)
diff --git a/tap.c b/tap.c
index db4f6e40781f..6a3dd37a0c45 100644
--- a/tap.c
+++ b/tap.c
@@ -707,6 +707,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;
@@ -718,7 +719,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;
}
@@ -763,7 +765,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);
@@ -777,7 +780,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;
}
@@ -828,7 +832,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++) {
@@ -884,6 +889,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;
@@ -937,7 +943,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;
@@ -956,7 +963,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;
@@ -1012,7 +1020,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++) {
@@ -1088,9 +1097,6 @@ 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:
@@ -1098,16 +1104,14 @@ void tap_add_packet(struct ctx *c, struct iov_tail *data,
tap4_handler(c, pool_tap4, now);
pool_flush(pool_tap4);
}
- 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:
if (pool_full(pool_tap6)) {
tap6_handler(c, pool_tap6, now);
pool_flush(pool_tap6);
}
- 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;
--
@@ -707,6 +707,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;
@@ -718,7 +719,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;
}
@@ -763,7 +765,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);
@@ -777,7 +780,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;
}
@@ -828,7 +832,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++) {
@@ -884,6 +889,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;
@@ -937,7 +943,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;
@@ -956,7 +963,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;
@@ -1012,7 +1020,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++) {
@@ -1088,9 +1097,6 @@ 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:
@@ -1098,16 +1104,14 @@ void tap_add_packet(struct ctx *c, struct iov_tail *data,
tap4_handler(c, pool_tap4, now);
pool_flush(pool_tap4);
}
- 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:
if (pool_full(pool_tap6)) {
tap6_handler(c, pool_tap6, now);
pool_flush(pool_tap6);
}
- 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
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 06/30] packet: Add packet_data()
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (4 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 05/30] packet: Use iov_tail with packet_add() Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 07/30] arp: Convert to iov_tail Laurent Vivier
` (23 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
packet_data() gets the data range from a packet descriptor from a
given pool.
It uses iov_tail to return the packet memory.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
packet.c | 42 ++++++++++++++++++++++++++++++++++++++++++
packet.h | 5 +++++
2 files changed, 47 insertions(+)
diff --git a/packet.c b/packet.c
index 98ded4e27aae..82adc9fd1a39 100644
--- a/packet.c
+++ b/packet.c
@@ -190,6 +190,48 @@ void *packet_get_do(const struct pool *p, const size_t idx,
return r;
}
+/**
+ * packet_data_do() - Get data range from packet descriptor from given pool
+ * @p: Packet pool
+ * @idx: Index of packet descriptor in pool
+ * @data: IOV tail to store the address of the data (output)
+ * @func: For tracing: name of calling function, NULL means no trace()
+ * @line: For tracing: caller line of function call
+ *
+ * Return: false if packet index is invalid, true otherwise.
+ * If something wrong with @data, don't return at all (assert).
+ */
+/* cppcheck-suppress unusedFunction */
+bool packet_data_do(const struct pool *p, size_t idx,
+ struct iov_tail *data,
+ const char *func, int line)
+{
+ size_t i;
+
+ ASSERT_WITH_MSG(p->count <= p->size,
+ "Corrupted pool count: %zu, size: %zu, %s:%i",
+ p->count, p->size, func, line);
+
+ if (idx >= p->count) {
+ debug("packet %zu from pool size: %zu, count: %zu, "
+ "%s:%i", idx, p->size, p->count, func, line);
+ return false;
+ }
+
+ data->cnt = 1;
+ data->off = 0;
+ data->iov = &p->pkt[idx];
+
+ for (i = 0; i < data->cnt; i++) {
+ ASSERT_WITH_MSG(!packet_check_range(p, data->iov[i].iov_base,
+ data->iov[i].iov_len,
+ func, line),
+ "Corrupt packet pool, %s:%i", func, line);
+ }
+
+ return true;
+}
+
/**
* pool_flush() - Flush a packet pool
* @p: Pointer to packet pool
diff --git a/packet.h b/packet.h
index af40b39b5251..062afb978124 100644
--- a/packet.h
+++ b/packet.h
@@ -39,6 +39,9 @@ void *packet_get_try_do(const struct pool *p, const size_t idx,
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);
+bool packet_data_do(const struct pool *p, const size_t idx,
+ struct iov_tail *data,
+ const char *func, int line);
bool pool_full(const struct pool *p);
void pool_flush(struct pool *p);
@@ -49,6 +52,8 @@ void pool_flush(struct pool *p);
packet_get_try_do(p, idx, offset, len, left, __func__, __LINE__)
#define packet_get(p, idx, offset, len, left) \
packet_get_do(p, idx, offset, len, left, __func__, __LINE__)
+#define packet_data(p, idx, data) \
+ packet_data_do(p, idx, data, __func__, __LINE__)
#define PACKET_POOL_DECL(_name, _size, _buf) \
struct _name ## _t { \
--
@@ -39,6 +39,9 @@ void *packet_get_try_do(const struct pool *p, const size_t idx,
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);
+bool packet_data_do(const struct pool *p, const size_t idx,
+ struct iov_tail *data,
+ const char *func, int line);
bool pool_full(const struct pool *p);
void pool_flush(struct pool *p);
@@ -49,6 +52,8 @@ void pool_flush(struct pool *p);
packet_get_try_do(p, idx, offset, len, left, __func__, __LINE__)
#define packet_get(p, idx, offset, len, left) \
packet_get_do(p, idx, offset, len, left, __func__, __LINE__)
+#define packet_data(p, idx, data) \
+ packet_data_do(p, idx, data, __func__, __LINE__)
#define PACKET_POOL_DECL(_name, _size, _buf) \
struct _name ## _t { \
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 07/30] arp: Convert to iov_tail
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (5 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 06/30] packet: Add packet_data() Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 08/30] ndp: " Laurent Vivier
` (22 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, David Gibson
Use packet_data() and extract headers using IOV_REMOVE_HEADER()
rather than packet_get().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
arp.c | 12 +++++++++---
packet.c | 1 -
2 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/arp.c b/arp.c
index 721ff2d529b5..6617698f125b 100644
--- a/arp.c
+++ b/arp.c
@@ -74,14 +74,20 @@ int arp(const struct ctx *c, const struct pool *p)
struct arphdr ah;
struct arpmsg am;
} __attribute__((__packed__)) resp;
+ struct arphdr ah_storage;
+ struct ethhdr eh_storage;
+ struct arpmsg am_storage;
const struct ethhdr *eh;
const struct arphdr *ah;
const struct arpmsg *am;
+ struct iov_tail data;
- eh = packet_get(p, 0, 0, sizeof(*eh), NULL);
- ah = packet_get(p, 0, sizeof(*eh), sizeof(*ah), NULL);
- am = packet_get(p, 0, sizeof(*eh) + sizeof(*ah), sizeof(*am), NULL);
+ if (!packet_data(p, 0, &data))
+ return -1;
+ eh = IOV_REMOVE_HEADER(&data, eh_storage);
+ ah = IOV_REMOVE_HEADER(&data, ah_storage);
+ am = IOV_REMOVE_HEADER(&data, am_storage);
if (!eh || !ah || !am)
return -1;
diff --git a/packet.c b/packet.c
index 82adc9fd1a39..34b1722b9a03 100644
--- a/packet.c
+++ b/packet.c
@@ -201,7 +201,6 @@ void *packet_get_do(const struct pool *p, const size_t idx,
* Return: false if packet index is invalid, true otherwise.
* If something wrong with @data, don't return at all (assert).
*/
-/* cppcheck-suppress unusedFunction */
bool packet_data_do(const struct pool *p, size_t idx,
struct iov_tail *data,
const char *func, int line)
--
@@ -201,7 +201,6 @@ void *packet_get_do(const struct pool *p, const size_t idx,
* Return: false if packet index is invalid, true otherwise.
* If something wrong with @data, don't return at all (assert).
*/
-/* cppcheck-suppress unusedFunction */
bool packet_data_do(const struct pool *p, size_t idx,
struct iov_tail *data,
const char *func, int line)
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 08/30] ndp: Convert to iov_tail
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (6 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 07/30] arp: Convert to iov_tail Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 09/30] icmp: " Laurent Vivier
` (21 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, David Gibson
Use packet_data() and extract headers using IOV_REMOVE_HEADER()
rather than packet_get().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
ndp.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/ndp.c b/ndp.c
index b664034b00b1..77722353be7b 100644
--- a/ndp.c
+++ b/ndp.c
@@ -351,9 +351,14 @@ int ndp(const struct ctx *c, const struct icmp6hdr *ih,
return 1;
if (ih->icmp6_type == NS) {
+ struct ndp_ns ns_storage;
const struct ndp_ns *ns;
+ struct iov_tail data;
- ns = packet_get(p, 0, 0, sizeof(struct ndp_ns), NULL);
+ if (!packet_data(p, 0, &data))
+ return -1;
+
+ ns = IOV_REMOVE_HEADER(&data, ns_storage);
if (!ns)
return -1;
--
@@ -351,9 +351,14 @@ int ndp(const struct ctx *c, const struct icmp6hdr *ih,
return 1;
if (ih->icmp6_type == NS) {
+ struct ndp_ns ns_storage;
const struct ndp_ns *ns;
+ struct iov_tail data;
- ns = packet_get(p, 0, 0, sizeof(struct ndp_ns), NULL);
+ if (!packet_data(p, 0, &data))
+ return -1;
+
+ ns = IOV_REMOVE_HEADER(&data, ns_storage);
if (!ns)
return -1;
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 09/30] icmp: Convert to iov_tail
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (7 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 08/30] ndp: " Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 10/30] udp: " Laurent Vivier
` (20 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Use packet_data() and extract headers using IOV_PEEK_HEADER()
rather than packet_get().
Introduce iov_tail_msghdr() to convert iov_tail to msghdr
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
icmp.c | 25 ++++++++++++++-----------
iov.c | 23 +++++++++++++++++++++++
iov.h | 2 ++
3 files changed, 39 insertions(+), 11 deletions(-)
diff --git a/icmp.c b/icmp.c
index 7e2b3423a8d1..37edf6c81b89 100644
--- a/icmp.c
+++ b/icmp.c
@@ -241,25 +241,27 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
struct icmp_ping_flow *pingf;
const struct flowside *tgt;
union sockaddr_inany sa;
- size_t dlen, l4len;
+ struct iov_tail data;
+ struct msghdr msh;
uint16_t id, seq;
union flow *flow;
uint8_t proto;
socklen_t sl;
- void *pkt;
(void)saddr;
ASSERT(pif == PIF_TAP);
+ if (!packet_data(p, 0, &data))
+ return -1;
+
if (af == AF_INET) {
+ struct icmphdr ih_storage;
const struct icmphdr *ih;
- if (!(pkt = packet_get(p, 0, 0, sizeof(*ih), &dlen)))
+ ih = IOV_PEEK_HEADER(&data, ih_storage);
+ if (!ih)
return 1;
- ih = (struct icmphdr *)pkt;
- l4len = dlen + sizeof(*ih);
-
if (ih->type != ICMP_ECHO)
return 1;
@@ -267,14 +269,13 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
id = ntohs(ih->un.echo.id);
seq = ntohs(ih->un.echo.sequence);
} else if (af == AF_INET6) {
+ struct icmp6hdr ih_storage;
const struct icmp6hdr *ih;
- if (!(pkt = packet_get(p, 0, 0, sizeof(*ih), &dlen)))
+ ih = IOV_PEEK_HEADER(&data, ih_storage);
+ if (!ih)
return 1;
- ih = (struct icmp6hdr *)pkt;
- l4len = dlen + sizeof(*ih);
-
if (ih->icmp6_type != ICMPV6_ECHO_REQUEST)
return 1;
@@ -298,8 +299,10 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
ASSERT(flow_proto[pingf->f.type] == proto);
pingf->ts = now->tv_sec;
+
pif_sockaddr(c, &sa, &sl, PIF_HOST, &tgt->eaddr, 0);
- if (sendto(pingf->sock, pkt, l4len, MSG_NOSIGNAL, &sa.sa, sl) < 0) {
+ iov_tail_msghdr(&msh, &data, &sa, sl);
+ if (sendmsg(pingf->sock, &msh, MSG_NOSIGNAL) < 0) {
flow_dbg_perror(pingf, "failed to relay request to socket");
} else {
flow_dbg(pingf,
diff --git a/iov.c b/iov.c
index cc50ea9e3e6c..910103883827 100644
--- a/iov.c
+++ b/iov.c
@@ -158,6 +158,29 @@ size_t iov_size(const struct iovec *iov, size_t iov_cnt)
return len;
}
+/**
+ * iov_tail_msghdr - Initialize a msghdr from an IOV tail structure
+ * @msh: msghdr to initialize
+ * @tail: iov_tail to use to set msg_iov and msg_iovlen
+ * @msg_name: Pointer to set to msg_name
+ * @msg_namelen: Size of @msg_name
+ */
+void iov_tail_msghdr(struct msghdr *msh, struct iov_tail *tail,
+ void *msg_name, socklen_t msg_namelen)
+{
+ iov_tail_prune(tail);
+
+ ASSERT(tail->off == 0);
+
+ msh->msg_name = msg_name;
+ msh->msg_namelen = msg_namelen;
+ msh->msg_iov = (struct iovec *)tail->iov;
+ msh->msg_iovlen = tail->cnt;
+ msh->msg_control = NULL;
+ msh->msg_controllen = 0;
+ msh->msg_flags = 0;
+}
+
/**
* iov_tail_prune() - Remove any unneeded buffers from an IOV tail
* @tail: IO vector tail (modified)
diff --git a/iov.h b/iov.h
index 7d5bce2f841c..863b37857324 100644
--- a/iov.h
+++ b/iov.h
@@ -82,6 +82,8 @@ struct iov_tail {
1, \
(off_))
+void iov_tail_msghdr(struct msghdr *msh, struct iov_tail *tail,
+ void *msg_name, socklen_t msg_namelen);
bool iov_tail_prune(struct iov_tail *tail);
size_t iov_tail_size(struct iov_tail *tail);
bool iov_tail_drop(struct iov_tail *tail, size_t len);
--
@@ -82,6 +82,8 @@ struct iov_tail {
1, \
(off_))
+void iov_tail_msghdr(struct msghdr *msh, struct iov_tail *tail,
+ void *msg_name, socklen_t msg_namelen);
bool iov_tail_prune(struct iov_tail *tail);
size_t iov_tail_size(struct iov_tail *tail);
bool iov_tail_drop(struct iov_tail *tail, size_t len);
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 10/30] udp: Convert to iov_tail
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (8 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 09/30] icmp: " Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 11/30] tcp: Convert tcp_tap_handler() to use iov_tail Laurent Vivier
` (19 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Use packet_data() and extract headers using IOV_REMOVE_HEADER()
and IOV_PEEK_HEADER() rather than packet_get().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
iov.c | 1 -
udp.c | 33 ++++++++++++++++++++++-----------
2 files changed, 22 insertions(+), 12 deletions(-)
diff --git a/iov.c b/iov.c
index 910103883827..ca6a35574f1b 100644
--- a/iov.c
+++ b/iov.c
@@ -334,7 +334,6 @@ void *iov_remove_header_(struct iov_tail *tail, void *v, size_t len, size_t alig
* iov array, a negative value if there is not enough room in the
* destination iov array
*/
-/* cppcheck-suppress unusedFunction */
ssize_t iov_tail_clone(struct iovec *dst_iov, size_t dst_iov_cnt,
struct iov_tail *tail)
{
diff --git a/udp.c b/udp.c
index ca28b37c142d..cfa653508d9c 100644
--- a/udp.c
+++ b/udp.c
@@ -978,9 +978,11 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
struct mmsghdr mm[UIO_MAXIOV];
union sockaddr_inany to_sa;
struct iovec m[UIO_MAXIOV];
+ struct udphdr uh_storage;
const struct udphdr *uh;
struct udp_flow *uflow;
- int i, s, count = 0;
+ int i, j, s, count = 0;
+ struct iov_tail data;
flow_sidx_t tosidx;
in_port_t src, dst;
uint8_t topif;
@@ -988,7 +990,10 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
ASSERT(!c->no_udp);
- uh = packet_get(p, idx, 0, sizeof(*uh), NULL);
+ if (!packet_data(p, idx, &data))
+ return 1;
+
+ uh = IOV_PEEK_HEADER(&data, uh_storage);
if (!uh)
return 1;
@@ -1025,23 +1030,29 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
pif_sockaddr(c, &to_sa, &sl, topif, &toside->eaddr, toside->eport);
- for (i = 0; i < (int)p->count - idx; i++) {
- struct udphdr *uh_send;
- size_t len;
+ for (i = 0, j = 0; i < (int)p->count - idx && j < UIO_MAXIOV; i++) {
+ const struct udphdr *uh_send;
- uh_send = packet_get(p, idx + i, 0, sizeof(*uh), &len);
+ if (!packet_data(p, idx + i, &data))
+ return p->count - idx;
+
+ uh_send = IOV_REMOVE_HEADER(&data, uh_storage);
if (!uh_send)
return p->count - idx;
mm[i].msg_hdr.msg_name = &to_sa;
mm[i].msg_hdr.msg_namelen = sl;
- if (len) {
- m[i].iov_base = (char *)(uh_send + 1);
- m[i].iov_len = len;
+ if (data.cnt) {
+ int cnt;
+
+ cnt = iov_tail_clone(&m[j], UIO_MAXIOV - j, &data);
+ if (cnt < 0)
+ return p->count - idx;
- mm[i].msg_hdr.msg_iov = m + i;
- mm[i].msg_hdr.msg_iovlen = 1;
+ mm[i].msg_hdr.msg_iov = &m[j];
+ mm[i].msg_hdr.msg_iovlen = cnt;
+ j += cnt;
} else {
mm[i].msg_hdr.msg_iov = NULL;
mm[i].msg_hdr.msg_iovlen = 0;
--
@@ -978,9 +978,11 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
struct mmsghdr mm[UIO_MAXIOV];
union sockaddr_inany to_sa;
struct iovec m[UIO_MAXIOV];
+ struct udphdr uh_storage;
const struct udphdr *uh;
struct udp_flow *uflow;
- int i, s, count = 0;
+ int i, j, s, count = 0;
+ struct iov_tail data;
flow_sidx_t tosidx;
in_port_t src, dst;
uint8_t topif;
@@ -988,7 +990,10 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
ASSERT(!c->no_udp);
- uh = packet_get(p, idx, 0, sizeof(*uh), NULL);
+ if (!packet_data(p, idx, &data))
+ return 1;
+
+ uh = IOV_PEEK_HEADER(&data, uh_storage);
if (!uh)
return 1;
@@ -1025,23 +1030,29 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
pif_sockaddr(c, &to_sa, &sl, topif, &toside->eaddr, toside->eport);
- for (i = 0; i < (int)p->count - idx; i++) {
- struct udphdr *uh_send;
- size_t len;
+ for (i = 0, j = 0; i < (int)p->count - idx && j < UIO_MAXIOV; i++) {
+ const struct udphdr *uh_send;
- uh_send = packet_get(p, idx + i, 0, sizeof(*uh), &len);
+ if (!packet_data(p, idx + i, &data))
+ return p->count - idx;
+
+ uh_send = IOV_REMOVE_HEADER(&data, uh_storage);
if (!uh_send)
return p->count - idx;
mm[i].msg_hdr.msg_name = &to_sa;
mm[i].msg_hdr.msg_namelen = sl;
- if (len) {
- m[i].iov_base = (char *)(uh_send + 1);
- m[i].iov_len = len;
+ if (data.cnt) {
+ int cnt;
+
+ cnt = iov_tail_clone(&m[j], UIO_MAXIOV - j, &data);
+ if (cnt < 0)
+ return p->count - idx;
- mm[i].msg_hdr.msg_iov = m + i;
- mm[i].msg_hdr.msg_iovlen = 1;
+ mm[i].msg_hdr.msg_iov = &m[j];
+ mm[i].msg_hdr.msg_iovlen = cnt;
+ j += cnt;
} else {
mm[i].msg_hdr.msg_iov = NULL;
mm[i].msg_hdr.msg_iovlen = 0;
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 11/30] tcp: Convert tcp_tap_handler() to use iov_tail
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (9 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 10/30] udp: " Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 12/30] tcp: Convert tcp_data_from_tap() " Laurent Vivier
` (18 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Use packet_data() and extract headers using IOV_REMOVE_HEADER()
and iov_remove_header_() rather than packet_get().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
tcp.c | 31 ++++++++++++++++++++++++-------
1 file changed, 24 insertions(+), 7 deletions(-)
diff --git a/tcp.c b/tcp.c
index 0ac298a74125..7c543e4a5a57 100644
--- a/tcp.c
+++ b/tcp.c
@@ -310,6 +310,16 @@
#include "tcp_buf.h"
#include "tcp_vu.h"
+/*
+ * The size of TCP header (including options) is given by doff (Data Offset)
+ * that is a 4-bit value specifying the number of 32-bit words in the header.
+ * The maximum value of doff is 15 [(1 << 4) - 1].
+ * The maximum length in bytes of options is 15 minus the number of 32-bit
+ * words in the minimal TCP header (5) multiplied by the length of a 32-bit
+ * word (4).
+ */
+#define OPTLEN_MAX (((1UL << 4) - 1 - 5) * 4UL)
+
#ifndef __USE_MISC
/* From Linux UAPI, missing in netinet/tcp.h provided by musl */
struct tcp_repair_opt {
@@ -1955,8 +1965,11 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
const struct pool *p, int idx, const struct timespec *now)
{
struct tcp_tap_conn *conn;
+ struct tcphdr th_storage;
const struct tcphdr *th;
- size_t optlen, len;
+ char optsc[OPTLEN_MAX];
+ struct iov_tail data;
+ size_t optlen, l4len;
const char *opts;
union flow *flow;
flow_sidx_t sidx;
@@ -1965,15 +1978,19 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
(void)pif;
- th = packet_get(p, idx, 0, sizeof(*th), &len);
+ if (!packet_data(p, idx, &data))
+ return 1;
+
+ l4len = iov_tail_size(&data);
+
+ th = IOV_REMOVE_HEADER(&data, th_storage);
if (!th)
return 1;
- len += sizeof(*th);
optlen = th->doff * 4UL - sizeof(*th);
/* Static checkers might fail to see this: */
- optlen = MIN(optlen, ((1UL << 4) /* from doff width */ - 6) * 4UL);
- opts = packet_get(p, idx, sizeof(*th), optlen, NULL);
+ optlen = MIN(optlen, OPTLEN_MAX);
+ opts = (char *)iov_remove_header_(&data, &optsc[0], optlen, 1);
sidx = flow_lookup_af(c, IPPROTO_TCP, PIF_TAP, af, saddr, daddr,
ntohs(th->source), ntohs(th->dest));
@@ -1985,7 +2002,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
tcp_conn_from_tap(c, af, saddr, daddr, th,
opts, optlen, now);
else
- tcp_rst_no_conn(c, af, saddr, daddr, flow_lbl, th, len);
+ tcp_rst_no_conn(c, af, saddr, daddr, flow_lbl, th, l4len);
return 1;
}
@@ -1993,7 +2010,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
ASSERT(pif_at_sidx(sidx) == PIF_TAP);
conn = &flow->tcp;
- flow_trace(conn, "packet length %zu from tap", len);
+ flow_trace(conn, "packet length %zu from tap", l4len);
if (th->rst) {
conn_event(c, conn, CLOSED);
--
@@ -310,6 +310,16 @@
#include "tcp_buf.h"
#include "tcp_vu.h"
+/*
+ * The size of TCP header (including options) is given by doff (Data Offset)
+ * that is a 4-bit value specifying the number of 32-bit words in the header.
+ * The maximum value of doff is 15 [(1 << 4) - 1].
+ * The maximum length in bytes of options is 15 minus the number of 32-bit
+ * words in the minimal TCP header (5) multiplied by the length of a 32-bit
+ * word (4).
+ */
+#define OPTLEN_MAX (((1UL << 4) - 1 - 5) * 4UL)
+
#ifndef __USE_MISC
/* From Linux UAPI, missing in netinet/tcp.h provided by musl */
struct tcp_repair_opt {
@@ -1955,8 +1965,11 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
const struct pool *p, int idx, const struct timespec *now)
{
struct tcp_tap_conn *conn;
+ struct tcphdr th_storage;
const struct tcphdr *th;
- size_t optlen, len;
+ char optsc[OPTLEN_MAX];
+ struct iov_tail data;
+ size_t optlen, l4len;
const char *opts;
union flow *flow;
flow_sidx_t sidx;
@@ -1965,15 +1978,19 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
(void)pif;
- th = packet_get(p, idx, 0, sizeof(*th), &len);
+ if (!packet_data(p, idx, &data))
+ return 1;
+
+ l4len = iov_tail_size(&data);
+
+ th = IOV_REMOVE_HEADER(&data, th_storage);
if (!th)
return 1;
- len += sizeof(*th);
optlen = th->doff * 4UL - sizeof(*th);
/* Static checkers might fail to see this: */
- optlen = MIN(optlen, ((1UL << 4) /* from doff width */ - 6) * 4UL);
- opts = packet_get(p, idx, sizeof(*th), optlen, NULL);
+ optlen = MIN(optlen, OPTLEN_MAX);
+ opts = (char *)iov_remove_header_(&data, &optsc[0], optlen, 1);
sidx = flow_lookup_af(c, IPPROTO_TCP, PIF_TAP, af, saddr, daddr,
ntohs(th->source), ntohs(th->dest));
@@ -1985,7 +2002,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
tcp_conn_from_tap(c, af, saddr, daddr, th,
opts, optlen, now);
else
- tcp_rst_no_conn(c, af, saddr, daddr, flow_lbl, th, len);
+ tcp_rst_no_conn(c, af, saddr, daddr, flow_lbl, th, l4len);
return 1;
}
@@ -1993,7 +2010,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
ASSERT(pif_at_sidx(sidx) == PIF_TAP);
conn = &flow->tcp;
- flow_trace(conn, "packet length %zu from tap", len);
+ flow_trace(conn, "packet length %zu from tap", l4len);
if (th->rst) {
conn_event(c, conn, CLOSED);
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 12/30] tcp: Convert tcp_data_from_tap() to use iov_tail
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (10 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 11/30] tcp: Convert tcp_tap_handler() to use iov_tail Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 13/30] dhcpv6: move offset initialization out of dhcpv6_opt() Laurent Vivier
` (17 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Use packet_data() and extract headers using IOV_PEEK_HEADER()
rather than packet_get().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
tcp.c | 30 +++++++++++++++++++-----------
1 file changed, 19 insertions(+), 11 deletions(-)
diff --git a/tcp.c b/tcp.c
index 7c543e4a5a57..be1d3e0c0d32 100644
--- a/tcp.c
+++ b/tcp.c
@@ -1649,16 +1649,22 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
for (i = idx, iov_i = 0; i < (int)p->count; i++) {
uint32_t seq, seq_offset, ack_seq;
+ struct tcphdr th_storage;
const struct tcphdr *th;
- char *data;
- size_t off;
+ struct iov_tail data;
+ size_t off, size;
+ int count;
- th = packet_get(p, i, 0, sizeof(*th), &len);
+ if (!packet_data(p, i, &data))
+ return -1;
+
+ th = IOV_PEEK_HEADER(&data, th_storage);
if (!th)
return -1;
- len += sizeof(*th);
+ len = iov_tail_size(&data);
off = th->doff * 4UL;
+
if (off < sizeof(*th) || off > len)
return -1;
@@ -1668,9 +1674,7 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
}
len -= off;
- data = packet_get(p, i, off, len, NULL);
- if (!data)
- continue;
+ iov_tail_drop(&data, off);
seq = ntohl(th->seq);
if (SEQ_LT(seq, conn->seq_from_tap) && len <= 1) {
@@ -1744,10 +1748,14 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
continue;
}
- tcp_iov[iov_i].iov_base = data + seq_offset;
- tcp_iov[iov_i].iov_len = len - seq_offset;
- seq_from_tap += tcp_iov[iov_i].iov_len;
- iov_i++;
+ iov_tail_drop(&data, seq_offset);
+ size = len - seq_offset;
+ count = iov_tail_clone(&tcp_iov[iov_i], UIO_MAXIOV - iov_i,
+ &data);
+ if (count < 0)
+ break;
+ seq_from_tap += size;
+ iov_i += count;
if (keep == i)
keep = -1;
--
@@ -1649,16 +1649,22 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
for (i = idx, iov_i = 0; i < (int)p->count; i++) {
uint32_t seq, seq_offset, ack_seq;
+ struct tcphdr th_storage;
const struct tcphdr *th;
- char *data;
- size_t off;
+ struct iov_tail data;
+ size_t off, size;
+ int count;
- th = packet_get(p, i, 0, sizeof(*th), &len);
+ if (!packet_data(p, i, &data))
+ return -1;
+
+ th = IOV_PEEK_HEADER(&data, th_storage);
if (!th)
return -1;
- len += sizeof(*th);
+ len = iov_tail_size(&data);
off = th->doff * 4UL;
+
if (off < sizeof(*th) || off > len)
return -1;
@@ -1668,9 +1674,7 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
}
len -= off;
- data = packet_get(p, i, off, len, NULL);
- if (!data)
- continue;
+ iov_tail_drop(&data, off);
seq = ntohl(th->seq);
if (SEQ_LT(seq, conn->seq_from_tap) && len <= 1) {
@@ -1744,10 +1748,14 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
continue;
}
- tcp_iov[iov_i].iov_base = data + seq_offset;
- tcp_iov[iov_i].iov_len = len - seq_offset;
- seq_from_tap += tcp_iov[iov_i].iov_len;
- iov_i++;
+ iov_tail_drop(&data, seq_offset);
+ size = len - seq_offset;
+ count = iov_tail_clone(&tcp_iov[iov_i], UIO_MAXIOV - iov_i,
+ &data);
+ if (count < 0)
+ break;
+ seq_from_tap += size;
+ iov_i += count;
if (keep == i)
keep = -1;
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 13/30] dhcpv6: move offset initialization out of dhcpv6_opt()
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (11 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 12/30] tcp: Convert tcp_data_from_tap() " Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 14/30] dhcpv6: Extract sending of NotOnLink status Laurent Vivier
` (16 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, David Gibson
No functional change.
Currently, if dhcpv6_opt() is called with offset set to 0, it will set the
offset to point to DHCPv6 options offset.
To simplify the use of iovec_tail in a later patch, move the initialization
out of the function. Replace all the call using 0 by a call using
the offset of the DHCPv6 options.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
dhcpv6.c | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/dhcpv6.c b/dhcpv6.c
index ba16c664ee24..1e540bb84f0d 100644
--- a/dhcpv6.c
+++ b/dhcpv6.c
@@ -54,14 +54,14 @@ struct opt_hdr {
uint16_t l;
} __attribute__((packed));
+#define UDP_MSG_HDR_SIZE (sizeof(struct udphdr) + sizeof(struct msg_hdr))
# define OPT_SIZE_CONV(x) (htons_constant(x))
#define OPT_SIZE(x) OPT_SIZE_CONV(sizeof(struct opt_##x) - \
sizeof(struct opt_hdr))
#define OPT_VSIZE(x) (sizeof(struct opt_##x) - \
sizeof(struct opt_hdr))
#define OPT_MAX_SIZE IPV6_MIN_MTU - (sizeof(struct ipv6hdr) + \
- sizeof(struct udphdr) + \
- sizeof(struct msg_hdr))
+ UDP_MSG_HDR_SIZE)
/**
* struct opt_client_id - DHCPv6 Client Identifier option
@@ -292,8 +292,7 @@ static struct opt_hdr *dhcpv6_opt(const struct pool *p, size_t *offset,
struct opt_hdr *o;
size_t left;
- if (!*offset)
- *offset = sizeof(struct udphdr) + sizeof(struct msg_hdr);
+ ASSERT(*offset >= UDP_MSG_HDR_SIZE);
while ((o = packet_get_try(p, 0, *offset, sizeof(*o), &left))) {
unsigned int opt_len = ntohs(o->l) + sizeof(*o);
@@ -329,7 +328,7 @@ static struct opt_hdr *dhcpv6_ia_notonlink(const struct pool *p,
size_t offset;
foreach(ia_type, ia_types) {
- offset = 0;
+ offset = UDP_MSG_HDR_SIZE;
while ((ia = dhcpv6_opt(p, &offset, *ia_type))) {
if (ntohs(ia->l) < OPT_VSIZE(ia_na))
return NULL;
@@ -466,8 +465,9 @@ static size_t dhcpv6_client_fqdn_fill(const struct pool *p, const struct ctx *c,
o = (struct opt_client_fqdn *)(buf + offset);
encode_domain_name(o->domain_name, c->fqdn);
- req_opt = (struct opt_client_fqdn *)dhcpv6_opt(p, &(size_t){ 0 },
- OPT_CLIENT_FQDN);
+ req_opt = (struct opt_client_fqdn *)dhcpv6_opt(p,
+ &(size_t){ UDP_MSG_HDR_SIZE },
+ OPT_CLIENT_FQDN);
if (req_opt && req_opt->flags & 0x01 /* S flag */)
o->flags = 0x02 /* O flag */;
else
@@ -524,15 +524,15 @@ int dhcpv6(struct ctx *c, const struct pool *p,
if (!mh)
return -1;
- client_id = dhcpv6_opt(p, &(size_t){ 0 }, OPT_CLIENTID);
+ client_id = dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_CLIENTID);
if (!client_id || ntohs(client_id->l) > OPT_VSIZE(client_id))
return -1;
- server_id = dhcpv6_opt(p, &(size_t){ 0 }, OPT_SERVERID);
+ server_id = dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_SERVERID);
if (server_id && ntohs(server_id->l) != OPT_VSIZE(server_id))
return -1;
- ia = dhcpv6_opt(p, &(size_t){ 0 }, OPT_IA_NA);
+ ia = dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_IA_NA);
if (ia && ntohs(ia->l) < MIN(OPT_VSIZE(ia_na), OPT_VSIZE(ia_ta)))
return -1;
@@ -582,7 +582,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
memcmp(&resp.server_id, server_id, sizeof(resp.server_id)))
return -1;
- if (ia || dhcpv6_opt(p, &(size_t){ 0 }, OPT_IA_TA))
+ if (ia || dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_IA_TA))
return -1;
info("DHCPv6: received INFORMATION_REQUEST, sending REPLY");
--
@@ -54,14 +54,14 @@ struct opt_hdr {
uint16_t l;
} __attribute__((packed));
+#define UDP_MSG_HDR_SIZE (sizeof(struct udphdr) + sizeof(struct msg_hdr))
# define OPT_SIZE_CONV(x) (htons_constant(x))
#define OPT_SIZE(x) OPT_SIZE_CONV(sizeof(struct opt_##x) - \
sizeof(struct opt_hdr))
#define OPT_VSIZE(x) (sizeof(struct opt_##x) - \
sizeof(struct opt_hdr))
#define OPT_MAX_SIZE IPV6_MIN_MTU - (sizeof(struct ipv6hdr) + \
- sizeof(struct udphdr) + \
- sizeof(struct msg_hdr))
+ UDP_MSG_HDR_SIZE)
/**
* struct opt_client_id - DHCPv6 Client Identifier option
@@ -292,8 +292,7 @@ static struct opt_hdr *dhcpv6_opt(const struct pool *p, size_t *offset,
struct opt_hdr *o;
size_t left;
- if (!*offset)
- *offset = sizeof(struct udphdr) + sizeof(struct msg_hdr);
+ ASSERT(*offset >= UDP_MSG_HDR_SIZE);
while ((o = packet_get_try(p, 0, *offset, sizeof(*o), &left))) {
unsigned int opt_len = ntohs(o->l) + sizeof(*o);
@@ -329,7 +328,7 @@ static struct opt_hdr *dhcpv6_ia_notonlink(const struct pool *p,
size_t offset;
foreach(ia_type, ia_types) {
- offset = 0;
+ offset = UDP_MSG_HDR_SIZE;
while ((ia = dhcpv6_opt(p, &offset, *ia_type))) {
if (ntohs(ia->l) < OPT_VSIZE(ia_na))
return NULL;
@@ -466,8 +465,9 @@ static size_t dhcpv6_client_fqdn_fill(const struct pool *p, const struct ctx *c,
o = (struct opt_client_fqdn *)(buf + offset);
encode_domain_name(o->domain_name, c->fqdn);
- req_opt = (struct opt_client_fqdn *)dhcpv6_opt(p, &(size_t){ 0 },
- OPT_CLIENT_FQDN);
+ req_opt = (struct opt_client_fqdn *)dhcpv6_opt(p,
+ &(size_t){ UDP_MSG_HDR_SIZE },
+ OPT_CLIENT_FQDN);
if (req_opt && req_opt->flags & 0x01 /* S flag */)
o->flags = 0x02 /* O flag */;
else
@@ -524,15 +524,15 @@ int dhcpv6(struct ctx *c, const struct pool *p,
if (!mh)
return -1;
- client_id = dhcpv6_opt(p, &(size_t){ 0 }, OPT_CLIENTID);
+ client_id = dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_CLIENTID);
if (!client_id || ntohs(client_id->l) > OPT_VSIZE(client_id))
return -1;
- server_id = dhcpv6_opt(p, &(size_t){ 0 }, OPT_SERVERID);
+ server_id = dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_SERVERID);
if (server_id && ntohs(server_id->l) != OPT_VSIZE(server_id))
return -1;
- ia = dhcpv6_opt(p, &(size_t){ 0 }, OPT_IA_NA);
+ ia = dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_IA_NA);
if (ia && ntohs(ia->l) < MIN(OPT_VSIZE(ia_na), OPT_VSIZE(ia_ta)))
return -1;
@@ -582,7 +582,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
memcmp(&resp.server_id, server_id, sizeof(resp.server_id)))
return -1;
- if (ia || dhcpv6_opt(p, &(size_t){ 0 }, OPT_IA_TA))
+ if (ia || dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_IA_TA))
return -1;
info("DHCPv6: received INFORMATION_REQUEST, sending REPLY");
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 14/30] dhcpv6: Extract sending of NotOnLink status
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (12 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 13/30] dhcpv6: move offset initialization out of dhcpv6_opt() Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 15/30] dhcpv6: Convert to iov_tail Laurent Vivier
` (15 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, David Gibson
Extract code from dhcpv6() into a new function, dhcpv6_send_ia_notonlink()
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
dhcpv6.c | 60 ++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 39 insertions(+), 21 deletions(-)
diff --git a/dhcpv6.c b/dhcpv6.c
index 1e540bb84f0d..8e6c29df205d 100644
--- a/dhcpv6.c
+++ b/dhcpv6.c
@@ -357,6 +357,44 @@ err:
return ia;
}
+/**
+ * dhcpv6_send_ia_notonlink() - Send NotOnLink status
+ * @c: Execution context
+ * @ia: Pointer to non-appropriate IA_NA or IA_TA
+ * @client_id: Client ID message option
+ * xid: Transaction ID for message exchange
+ */
+static void dhcpv6_send_ia_notonlink(struct ctx *c, struct opt_hdr *ia,
+ const struct opt_hdr *client_id,
+ uint32_t xid)
+{
+ const struct in6_addr *src = &c->ip6.our_tap_ll;
+ size_t n;
+
+ info("DHCPv6: received CONFIRM with inappropriate IA,"
+ " sending NotOnLink status in REPLY");
+
+ ia->l = htons(OPT_VSIZE(ia_na) + sizeof(sc_not_on_link));
+
+ n = sizeof(struct opt_ia_na);
+ memcpy(resp_not_on_link.var, ia, n);
+ memcpy(resp_not_on_link.var + n, &sc_not_on_link,
+ sizeof(sc_not_on_link));
+
+ n += sizeof(sc_not_on_link);
+ memcpy(resp_not_on_link.var + n, client_id,
+ sizeof(struct opt_hdr) + ntohs(client_id->l));
+
+ n += sizeof(struct opt_hdr) + ntohs(client_id->l);
+
+ n = offsetof(struct resp_not_on_link_t, var) + n;
+
+ resp_not_on_link.hdr.xid = xid;
+
+ tap_udp6_send(c, src, 547, tap_ip6_daddr(c, src), 546,
+ xid, &resp_not_on_link, n);
+}
+
/**
* dhcpv6_dns_fill() - Fill in DNS Servers and Domain Search list options
* @c: Execution context
@@ -549,28 +587,8 @@ int dhcpv6(struct ctx *c, const struct pool *p,
return -1;
if ((bad_ia = dhcpv6_ia_notonlink(p, &c->ip6.addr))) {
- info("DHCPv6: received CONFIRM with inappropriate IA,"
- " sending NotOnLink status in REPLY");
-
- bad_ia->l = htons(OPT_VSIZE(ia_na) +
- sizeof(sc_not_on_link));
- n = sizeof(struct opt_ia_na);
- memcpy(resp_not_on_link.var, bad_ia, n);
-
- memcpy(resp_not_on_link.var + n,
- &sc_not_on_link, sizeof(sc_not_on_link));
- n += sizeof(sc_not_on_link);
-
- memcpy(resp_not_on_link.var + n, client_id,
- sizeof(struct opt_hdr) + ntohs(client_id->l));
- n += sizeof(struct opt_hdr) + ntohs(client_id->l);
-
- n = offsetof(struct resp_not_on_link_t, var) + n;
-
- resp_not_on_link.hdr.xid = mh->xid;
- tap_udp6_send(c, src, 547, tap_ip6_daddr(c, src), 546,
- mh->xid, &resp_not_on_link, n);
+ dhcpv6_send_ia_notonlink(c, bad_ia, client_id, mh->xid);
return 1;
}
--
@@ -357,6 +357,44 @@ err:
return ia;
}
+/**
+ * dhcpv6_send_ia_notonlink() - Send NotOnLink status
+ * @c: Execution context
+ * @ia: Pointer to non-appropriate IA_NA or IA_TA
+ * @client_id: Client ID message option
+ * xid: Transaction ID for message exchange
+ */
+static void dhcpv6_send_ia_notonlink(struct ctx *c, struct opt_hdr *ia,
+ const struct opt_hdr *client_id,
+ uint32_t xid)
+{
+ const struct in6_addr *src = &c->ip6.our_tap_ll;
+ size_t n;
+
+ info("DHCPv6: received CONFIRM with inappropriate IA,"
+ " sending NotOnLink status in REPLY");
+
+ ia->l = htons(OPT_VSIZE(ia_na) + sizeof(sc_not_on_link));
+
+ n = sizeof(struct opt_ia_na);
+ memcpy(resp_not_on_link.var, ia, n);
+ memcpy(resp_not_on_link.var + n, &sc_not_on_link,
+ sizeof(sc_not_on_link));
+
+ n += sizeof(sc_not_on_link);
+ memcpy(resp_not_on_link.var + n, client_id,
+ sizeof(struct opt_hdr) + ntohs(client_id->l));
+
+ n += sizeof(struct opt_hdr) + ntohs(client_id->l);
+
+ n = offsetof(struct resp_not_on_link_t, var) + n;
+
+ resp_not_on_link.hdr.xid = xid;
+
+ tap_udp6_send(c, src, 547, tap_ip6_daddr(c, src), 546,
+ xid, &resp_not_on_link, n);
+}
+
/**
* dhcpv6_dns_fill() - Fill in DNS Servers and Domain Search list options
* @c: Execution context
@@ -549,28 +587,8 @@ int dhcpv6(struct ctx *c, const struct pool *p,
return -1;
if ((bad_ia = dhcpv6_ia_notonlink(p, &c->ip6.addr))) {
- info("DHCPv6: received CONFIRM with inappropriate IA,"
- " sending NotOnLink status in REPLY");
-
- bad_ia->l = htons(OPT_VSIZE(ia_na) +
- sizeof(sc_not_on_link));
- n = sizeof(struct opt_ia_na);
- memcpy(resp_not_on_link.var, bad_ia, n);
-
- memcpy(resp_not_on_link.var + n,
- &sc_not_on_link, sizeof(sc_not_on_link));
- n += sizeof(sc_not_on_link);
-
- memcpy(resp_not_on_link.var + n, client_id,
- sizeof(struct opt_hdr) + ntohs(client_id->l));
- n += sizeof(struct opt_hdr) + ntohs(client_id->l);
-
- n = offsetof(struct resp_not_on_link_t, var) + n;
-
- resp_not_on_link.hdr.xid = mh->xid;
- tap_udp6_send(c, src, 547, tap_ip6_daddr(c, src), 546,
- mh->xid, &resp_not_on_link, n);
+ dhcpv6_send_ia_notonlink(c, bad_ia, client_id, mh->xid);
return 1;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 15/30] dhcpv6: Convert to iov_tail
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (13 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 14/30] dhcpv6: Extract sending of NotOnLink status Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 16/30] dhcpv6: Use iov_tail in dhcpv6_opt() Laurent Vivier
` (14 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, David Gibson
Use packet_data() and extract headers using IOV_REMOVE_HEADER()
and IOV_PEEK_HEADER() rather than packet_get().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
---
dhcpv6.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/dhcpv6.c b/dhcpv6.c
index 8e6c29df205d..ae06e646f92f 100644
--- a/dhcpv6.c
+++ b/dhcpv6.c
@@ -533,12 +533,18 @@ int dhcpv6(struct ctx *c, const struct pool *p,
{
const struct opt_hdr *client_id, *server_id, *ia;
const struct in6_addr *src;
+ struct msg_hdr mh_storage;
const struct msg_hdr *mh;
+ struct udphdr uh_storage;
const struct udphdr *uh;
struct opt_hdr *bad_ia;
+ struct iov_tail data;
size_t mlen, n;
- uh = packet_get(p, 0, 0, sizeof(*uh), &mlen);
+ if (!packet_data(p, 0, &data))
+ return -1;
+
+ uh = IOV_REMOVE_HEADER(&data, uh_storage);
if (!uh)
return -1;
@@ -551,6 +557,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
if (!IN6_IS_ADDR_MULTICAST(daddr))
return -1;
+ mlen = iov_tail_size(&data);
if (mlen + sizeof(*uh) != ntohs(uh->len) || mlen < sizeof(*mh))
return -1;
@@ -558,7 +565,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
src = &c->ip6.our_tap_ll;
- mh = packet_get(p, 0, sizeof(*uh), sizeof(*mh), NULL);
+ mh = IOV_PEEK_HEADER(&data, mh_storage);
if (!mh)
return -1;
--
@@ -533,12 +533,18 @@ int dhcpv6(struct ctx *c, const struct pool *p,
{
const struct opt_hdr *client_id, *server_id, *ia;
const struct in6_addr *src;
+ struct msg_hdr mh_storage;
const struct msg_hdr *mh;
+ struct udphdr uh_storage;
const struct udphdr *uh;
struct opt_hdr *bad_ia;
+ struct iov_tail data;
size_t mlen, n;
- uh = packet_get(p, 0, 0, sizeof(*uh), &mlen);
+ if (!packet_data(p, 0, &data))
+ return -1;
+
+ uh = IOV_REMOVE_HEADER(&data, uh_storage);
if (!uh)
return -1;
@@ -551,6 +557,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
if (!IN6_IS_ADDR_MULTICAST(daddr))
return -1;
+ mlen = iov_tail_size(&data);
if (mlen + sizeof(*uh) != ntohs(uh->len) || mlen < sizeof(*mh))
return -1;
@@ -558,7 +565,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
src = &c->ip6.our_tap_ll;
- mh = packet_get(p, 0, sizeof(*uh), sizeof(*mh), NULL);
+ mh = IOV_PEEK_HEADER(&data, mh_storage);
if (!mh)
return -1;
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 16/30] dhcpv6: Use iov_tail in dhcpv6_opt()
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (14 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 15/30] dhcpv6: Convert to iov_tail Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 17/30] dhcp: Convert to iov_tail Laurent Vivier
` (13 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
dhcpv6_opt() and its callers are refactored for iov_tail option parsing,
replacing direct offset management for improved robustness.
Its signature is now `bool dhcpv6_opt(iov_tail *data, type)`. `*data` (in/out)
points to a found option on `true` return or is restored on `false`.
The main dhcpv6() function uses IOV_REMOVE_HEADER for the msg_hdr, then
passes the iov_tail (now at options start) to the new dhcpv6_opt().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
dhcpv6.c | 178 +++++++++++++++++++++++++++++++------------------------
iov.c | 1 -
2 files changed, 102 insertions(+), 77 deletions(-)
diff --git a/dhcpv6.c b/dhcpv6.c
index ae06e646f92f..bd6268804d95 100644
--- a/dhcpv6.c
+++ b/dhcpv6.c
@@ -280,112 +280,122 @@ static struct resp_not_on_link_t {
/**
* dhcpv6_opt() - Get option from DHCPv6 message
- * @p: Packet pool, single packet with UDP header
- * @offset: Offset to look at, 0: end of header, set to option start
+ * @data: Buffer with options, set to matching option on return
* @type: Option type to look up, network order
*
- * Return: pointer to option header, or NULL on malformed or missing option
+ * Return: true if found and @data points to the option header,
+ * or false on malformed or missing option and @data is
+ * unmodified.
*/
-static struct opt_hdr *dhcpv6_opt(const struct pool *p, size_t *offset,
- uint16_t type)
+static bool dhcpv6_opt(struct iov_tail *data, uint16_t type)
{
- struct opt_hdr *o;
- size_t left;
+ struct iov_tail head = *data;
+ struct opt_hdr o_storage;
+ const struct opt_hdr *o;
- ASSERT(*offset >= UDP_MSG_HDR_SIZE);
-
- while ((o = packet_get_try(p, 0, *offset, sizeof(*o), &left))) {
+ while ((o = IOV_PEEK_HEADER(data, o_storage))) {
unsigned int opt_len = ntohs(o->l) + sizeof(*o);
- if (ntohs(o->l) > left)
- return NULL;
+ if (opt_len > iov_tail_size(data))
+ break;
if (o->t == type)
- return o;
+ return true;
- *offset += opt_len;
+ iov_tail_drop(data, opt_len);
}
- return NULL;
+ *data = head;
+ return false;
}
/**
* dhcpv6_ia_notonlink() - Check if any IA contains non-appropriate addresses
- * @p: Packet pool, single packet starting from UDP header
+ * @data: Data to look at, packet starting from UDP header (input/output)
* @la: Address we want to lease to the client
*
- * Return: pointer to non-appropriate IA_NA or IA_TA, if any, NULL otherwise
+ * Return: true and @data points to non-appropriate IA_NA or IA_TA, if any,
+ * false otherwise and @data is unmodified
*/
-static struct opt_hdr *dhcpv6_ia_notonlink(const struct pool *p,
- struct in6_addr *la)
+static bool dhcpv6_ia_notonlink(struct iov_tail *data,
+ struct in6_addr *la)
{
int ia_types[2] = { OPT_IA_NA, OPT_IA_TA }, *ia_type;
+ struct opt_ia_addr opt_addr_storage;
const struct opt_ia_addr *opt_addr;
+ struct iov_tail current, ia_base;
+ struct opt_ia_na ia_storage;
char buf[INET6_ADDRSTRLEN];
+ const struct opt_ia_na *ia;
struct in6_addr req_addr;
+ struct opt_hdr h_storage;
const struct opt_hdr *h;
- struct opt_hdr *ia;
- size_t offset;
foreach(ia_type, ia_types) {
- offset = UDP_MSG_HDR_SIZE;
- while ((ia = dhcpv6_opt(p, &offset, *ia_type))) {
- if (ntohs(ia->l) < OPT_VSIZE(ia_na))
- return NULL;
-
- offset += sizeof(struct opt_ia_na);
-
- while ((h = dhcpv6_opt(p, &offset, OPT_IAAADR))) {
- if (ntohs(h->l) != OPT_VSIZE(ia_addr))
- return NULL;
-
- opt_addr = (const struct opt_ia_addr *)h;
+ current = *data;
+ while (dhcpv6_opt(¤t, *ia_type)) {
+ ia_base = current;
+ ia = IOV_REMOVE_HEADER(¤t, ia_storage);
+ if (!ia || ntohs(ia->hdr.l) < OPT_VSIZE(ia_na))
+ goto notfound;
+
+ while (dhcpv6_opt(¤t, OPT_IAAADR)) {
+ h = IOV_PEEK_HEADER(¤t, h_storage);
+ if (!h || ntohs(h->l) != OPT_VSIZE(ia_addr))
+ goto notfound;
+
+ opt_addr = IOV_REMOVE_HEADER(¤t,
+ opt_addr_storage);
req_addr = opt_addr->addr;
if (!IN6_ARE_ADDR_EQUAL(la, &req_addr))
- goto err;
-
- offset += sizeof(struct opt_ia_addr);
+ goto notonlink;
}
}
}
- return NULL;
+notfound:
+ return false;
-err:
+notonlink:
info("DHCPv6: requested address %s not on link",
inet_ntop(AF_INET6, &req_addr, buf, sizeof(buf)));
- return ia;
+ *data = ia_base;
+ return true;
}
/**
* dhcpv6_send_ia_notonlink() - Send NotOnLink status
- * @c: Execution context
- * @ia: Pointer to non-appropriate IA_NA or IA_TA
- * @client_id: Client ID message option
- * xid: Transaction ID for message exchange
+ * @c: Execution context
+ * @ia_base: Non-appropriate IA_NA or IA_TA base
+ * @client_id_base: Client ID message option base
+ * @len: Client ID length
+ * @xid: Transaction ID for message exchange
*/
-static void dhcpv6_send_ia_notonlink(struct ctx *c, struct opt_hdr *ia,
- const struct opt_hdr *client_id,
- uint32_t xid)
+static void dhcpv6_send_ia_notonlink(struct ctx *c,
+ const struct iov_tail *ia_base,
+ const struct iov_tail *client_id_base,
+ int len, uint32_t xid)
{
const struct in6_addr *src = &c->ip6.our_tap_ll;
+ struct opt_hdr *ia = (struct opt_hdr *)resp_not_on_link.var;
size_t n;
info("DHCPv6: received CONFIRM with inappropriate IA,"
" sending NotOnLink status in REPLY");
- ia->l = htons(OPT_VSIZE(ia_na) + sizeof(sc_not_on_link));
-
n = sizeof(struct opt_ia_na);
- memcpy(resp_not_on_link.var, ia, n);
+ iov_to_buf(&ia_base->iov[0], ia_base->cnt, ia_base->off,
+ resp_not_on_link.var, n);
+ ia->l = htons(OPT_VSIZE(ia_na) + sizeof(sc_not_on_link));
memcpy(resp_not_on_link.var + n, &sc_not_on_link,
sizeof(sc_not_on_link));
n += sizeof(sc_not_on_link);
- memcpy(resp_not_on_link.var + n, client_id,
- sizeof(struct opt_hdr) + ntohs(client_id->l));
+ iov_to_buf(&client_id_base->iov[0], client_id_base->cnt,
+ client_id_base->off, resp_not_on_link.var + n,
+ sizeof(struct opt_hdr) + len);
- n += sizeof(struct opt_hdr) + ntohs(client_id->l);
+ n += sizeof(struct opt_hdr) + len;
n = offsetof(struct resp_not_on_link_t, var) + n;
@@ -474,17 +484,19 @@ search:
/**
* dhcpv6_client_fqdn_fill() - Fill in client FQDN option
+ * @data: Data to look at
* @c: Execution context
* @buf: Response message buffer where options will be appended
* @offset: Offset in message buffer for new options
*
* Return: updated length of response message buffer.
*/
-static size_t dhcpv6_client_fqdn_fill(const struct pool *p, const struct ctx *c,
+static size_t dhcpv6_client_fqdn_fill(const struct iov_tail *data,
+ const struct ctx *c,
char *buf, int offset)
{
- struct opt_client_fqdn const *req_opt;
+ struct iov_tail current = *data;
struct opt_client_fqdn *o;
size_t opt_len;
@@ -502,14 +514,16 @@ static size_t dhcpv6_client_fqdn_fill(const struct pool *p, const struct ctx *c,
}
o = (struct opt_client_fqdn *)(buf + offset);
+ o->flags = 0x00;
encode_domain_name(o->domain_name, c->fqdn);
- req_opt = (struct opt_client_fqdn *)dhcpv6_opt(p,
- &(size_t){ UDP_MSG_HDR_SIZE },
- OPT_CLIENT_FQDN);
- if (req_opt && req_opt->flags & 0x01 /* S flag */)
- o->flags = 0x02 /* O flag */;
- else
- o->flags = 0x00;
+ if (dhcpv6_opt(¤t, OPT_CLIENT_FQDN)) {
+ struct opt_client_fqdn req_opt_storage;
+ struct opt_client_fqdn const *req_opt;
+
+ req_opt = IOV_PEEK_HEADER(¤t, req_opt_storage);
+ if (req_opt && req_opt->flags & 0x01 /* S flag */)
+ o->flags = 0x02 /* O flag */;
+ }
opt_len++;
@@ -531,14 +545,18 @@ static size_t dhcpv6_client_fqdn_fill(const struct pool *p, const struct ctx *c,
int dhcpv6(struct ctx *c, const struct pool *p,
const struct in6_addr *saddr, const struct in6_addr *daddr)
{
- const struct opt_hdr *client_id, *server_id, *ia;
+ const struct opt_server_id *server_id = NULL;
+ struct iov_tail data, opt, client_id_base;
+ const struct opt_hdr *client_id = NULL;
+ struct opt_server_id server_id_storage;
+ const struct opt_ia_na *ia = NULL;
+ struct opt_hdr client_id_storage;
+ struct opt_ia_na ia_storage;
const struct in6_addr *src;
struct msg_hdr mh_storage;
const struct msg_hdr *mh;
struct udphdr uh_storage;
const struct udphdr *uh;
- struct opt_hdr *bad_ia;
- struct iov_tail data;
size_t mlen, n;
if (!packet_data(p, 0, &data))
@@ -565,20 +583,26 @@ int dhcpv6(struct ctx *c, const struct pool *p,
src = &c->ip6.our_tap_ll;
- mh = IOV_PEEK_HEADER(&data, mh_storage);
+ mh = IOV_REMOVE_HEADER(&data, mh_storage);
if (!mh)
return -1;
- client_id = dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_CLIENTID);
+ client_id_base = data;
+ if (dhcpv6_opt(&client_id_base, OPT_CLIENTID))
+ client_id = IOV_PEEK_HEADER(&client_id_base, client_id_storage);
if (!client_id || ntohs(client_id->l) > OPT_VSIZE(client_id))
return -1;
- server_id = dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_SERVERID);
- if (server_id && ntohs(server_id->l) != OPT_VSIZE(server_id))
+ opt = data;
+ if (dhcpv6_opt(&opt, OPT_SERVERID))
+ server_id = IOV_PEEK_HEADER(&opt, server_id_storage);
+ if (server_id && ntohs(server_id->hdr.l) != OPT_VSIZE(server_id))
return -1;
- ia = dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_IA_NA);
- if (ia && ntohs(ia->l) < MIN(OPT_VSIZE(ia_na), OPT_VSIZE(ia_ta)))
+ opt = data;
+ if (dhcpv6_opt(&opt, OPT_IA_NA))
+ ia = IOV_PEEK_HEADER(&opt, ia_storage);
+ if (ia && ntohs(ia->hdr.l) < MIN(OPT_VSIZE(ia_na), OPT_VSIZE(ia_ta)))
return -1;
resp.hdr.type = TYPE_REPLY;
@@ -593,9 +617,10 @@ int dhcpv6(struct ctx *c, const struct pool *p,
if (mh->type == TYPE_CONFIRM && server_id)
return -1;
- if ((bad_ia = dhcpv6_ia_notonlink(p, &c->ip6.addr))) {
+ if (dhcpv6_ia_notonlink(&data, &c->ip6.addr)) {
- dhcpv6_send_ia_notonlink(c, bad_ia, client_id, mh->xid);
+ dhcpv6_send_ia_notonlink(c, &data, &client_id_base,
+ ntohs(client_id->l), mh->xid);
return 1;
}
@@ -607,7 +632,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
memcmp(&resp.server_id, server_id, sizeof(resp.server_id)))
return -1;
- if (ia || dhcpv6_opt(p, &(size_t){ UDP_MSG_HDR_SIZE }, OPT_IA_TA))
+ if (ia || dhcpv6_opt(&data, OPT_IA_TA))
return -1;
info("DHCPv6: received INFORMATION_REQUEST, sending REPLY");
@@ -633,13 +658,14 @@ int dhcpv6(struct ctx *c, const struct pool *p,
if (ia)
resp.ia_na.iaid = ((struct opt_ia_na *)ia)->iaid;
- memcpy(&resp.client_id, client_id,
- ntohs(client_id->l) + sizeof(struct opt_hdr));
+ iov_to_buf(&client_id_base.iov[0], client_id_base.cnt,
+ client_id_base.off, &resp.client_id,
+ ntohs(client_id->l) + sizeof(struct opt_hdr));
n = offsetof(struct resp_t, client_id) +
sizeof(struct opt_hdr) + ntohs(client_id->l);
n = dhcpv6_dns_fill(c, (char *)&resp, n);
- n = dhcpv6_client_fqdn_fill(p, c, (char *)&resp, n);
+ n = dhcpv6_client_fqdn_fill(&data, c, (char *)&resp, n);
resp.hdr.xid = mh->xid;
diff --git a/iov.c b/iov.c
index ca6a35574f1b..76d261f804ca 100644
--- a/iov.c
+++ b/iov.c
@@ -109,7 +109,6 @@ size_t iov_from_buf(const struct iovec *iov, size_t iov_cnt,
*
* Returns: The number of bytes successfully copied.
*/
-/* cppcheck-suppress [staticFunction] */
size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt,
size_t offset, void *buf, size_t bytes)
{
--
@@ -109,7 +109,6 @@ size_t iov_from_buf(const struct iovec *iov, size_t iov_cnt,
*
* Returns: The number of bytes successfully copied.
*/
-/* cppcheck-suppress [staticFunction] */
size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt,
size_t offset, void *buf, size_t bytes)
{
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 17/30] dhcp: Convert to iov_tail
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (15 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 16/30] dhcpv6: Use iov_tail in dhcpv6_opt() Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 18/30] ip: Use iov_tail in ipv6_l4hdr() Laurent Vivier
` (12 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Use packet_data() and extract headers using IOV_REMOVE_HEADER()
and IOV_PEEK_HEADER() rather than packet_get().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
dhcp.c | 45 +++++++++++++++++++++++++++------------------
1 file changed, 27 insertions(+), 18 deletions(-)
diff --git a/dhcp.c b/dhcp.c
index b0de04be6f27..7c92f30d0b39 100644
--- a/dhcp.c
+++ b/dhcp.c
@@ -302,27 +302,33 @@ static void opt_set_dns_search(const struct ctx *c, size_t max_len)
*/
int dhcp(const struct ctx *c, const struct pool *p)
{
- size_t mlen, dlen, offset = 0, opt_len, opt_off = 0;
char macstr[ETH_ADDRSTRLEN];
+ size_t mlen, dlen, opt_len;
struct in_addr mask, dst;
+ struct ethhdr eh_storage;
+ struct iphdr iph_storage;
+ struct udphdr uh_storage;
const struct ethhdr *eh;
const struct iphdr *iph;
const struct udphdr *uh;
+ struct iov_tail data;
struct msg const *m;
struct msg reply;
unsigned int i;
+ struct msg mc;
- eh = packet_get(p, 0, offset, sizeof(*eh), NULL);
- offset += sizeof(*eh);
+ if (!packet_data(p, 0, &data))
+ return -1;
- iph = packet_get(p, 0, offset, sizeof(*iph), NULL);
+ eh = IOV_REMOVE_HEADER(&data, eh_storage);
+ iph = IOV_PEEK_HEADER(&data, iph_storage);
if (!eh || !iph)
return -1;
- offset += iph->ihl * 4UL;
- uh = packet_get(p, 0, offset, sizeof(*uh), &mlen);
- offset += sizeof(*uh);
+ if (!iov_tail_drop(&data, iph->ihl * 4UL))
+ return -1;
+ uh = IOV_REMOVE_HEADER(&data, uh_storage);
if (!uh)
return -1;
@@ -332,7 +338,9 @@ int dhcp(const struct ctx *c, const struct pool *p)
if (c->no_dhcp)
return 1;
- m = packet_get(p, 0, offset, offsetof(struct msg, o), &opt_len);
+ mlen = iov_tail_size(&data);
+ m = (struct msg const *)iov_remove_header_(&data, &mc, offsetof(struct msg, o),
+ __alignof__(struct msg));
if (!m ||
mlen != ntohs(uh->len) - sizeof(*uh) ||
mlen < offsetof(struct msg, o) ||
@@ -355,27 +363,28 @@ int dhcp(const struct ctx *c, const struct pool *p)
memset(&reply.file, 0, sizeof(reply.file));
reply.magic = m->magic;
- offset += offsetof(struct msg, o);
-
for (i = 0; i < ARRAY_SIZE(opts); i++)
opts[i].clen = -1;
- while (opt_off + 2 < opt_len) {
- const uint8_t *olen, *val;
+ opt_len = iov_tail_size(&data);
+ while (opt_len >= 2) {
+ uint8_t olen_storage, type_storage;
+ const uint8_t *olen;
uint8_t *type;
- type = packet_get(p, 0, offset + opt_off, 1, NULL);
- olen = packet_get(p, 0, offset + opt_off + 1, 1, NULL);
+ type = IOV_REMOVE_HEADER(&data, type_storage);
+ olen = IOV_REMOVE_HEADER(&data, olen_storage);
if (!type || !olen)
return -1;
- val = packet_get(p, 0, offset + opt_off + 2, *olen, NULL);
- if (!val)
+ opt_len = iov_tail_size(&data);
+ if (opt_len < *olen)
return -1;
- memcpy(&opts[*type].c, val, *olen);
+ iov_to_buf(&data.iov[0], data.cnt, data.off, &opts[*type].c, *olen);
opts[*type].clen = *olen;
- opt_off += *olen + 2;
+ iov_tail_drop(&data, *olen);
+ opt_len -= *olen;
}
opts[80].slen = -1;
--
@@ -302,27 +302,33 @@ static void opt_set_dns_search(const struct ctx *c, size_t max_len)
*/
int dhcp(const struct ctx *c, const struct pool *p)
{
- size_t mlen, dlen, offset = 0, opt_len, opt_off = 0;
char macstr[ETH_ADDRSTRLEN];
+ size_t mlen, dlen, opt_len;
struct in_addr mask, dst;
+ struct ethhdr eh_storage;
+ struct iphdr iph_storage;
+ struct udphdr uh_storage;
const struct ethhdr *eh;
const struct iphdr *iph;
const struct udphdr *uh;
+ struct iov_tail data;
struct msg const *m;
struct msg reply;
unsigned int i;
+ struct msg mc;
- eh = packet_get(p, 0, offset, sizeof(*eh), NULL);
- offset += sizeof(*eh);
+ if (!packet_data(p, 0, &data))
+ return -1;
- iph = packet_get(p, 0, offset, sizeof(*iph), NULL);
+ eh = IOV_REMOVE_HEADER(&data, eh_storage);
+ iph = IOV_PEEK_HEADER(&data, iph_storage);
if (!eh || !iph)
return -1;
- offset += iph->ihl * 4UL;
- uh = packet_get(p, 0, offset, sizeof(*uh), &mlen);
- offset += sizeof(*uh);
+ if (!iov_tail_drop(&data, iph->ihl * 4UL))
+ return -1;
+ uh = IOV_REMOVE_HEADER(&data, uh_storage);
if (!uh)
return -1;
@@ -332,7 +338,9 @@ int dhcp(const struct ctx *c, const struct pool *p)
if (c->no_dhcp)
return 1;
- m = packet_get(p, 0, offset, offsetof(struct msg, o), &opt_len);
+ mlen = iov_tail_size(&data);
+ m = (struct msg const *)iov_remove_header_(&data, &mc, offsetof(struct msg, o),
+ __alignof__(struct msg));
if (!m ||
mlen != ntohs(uh->len) - sizeof(*uh) ||
mlen < offsetof(struct msg, o) ||
@@ -355,27 +363,28 @@ int dhcp(const struct ctx *c, const struct pool *p)
memset(&reply.file, 0, sizeof(reply.file));
reply.magic = m->magic;
- offset += offsetof(struct msg, o);
-
for (i = 0; i < ARRAY_SIZE(opts); i++)
opts[i].clen = -1;
- while (opt_off + 2 < opt_len) {
- const uint8_t *olen, *val;
+ opt_len = iov_tail_size(&data);
+ while (opt_len >= 2) {
+ uint8_t olen_storage, type_storage;
+ const uint8_t *olen;
uint8_t *type;
- type = packet_get(p, 0, offset + opt_off, 1, NULL);
- olen = packet_get(p, 0, offset + opt_off + 1, 1, NULL);
+ type = IOV_REMOVE_HEADER(&data, type_storage);
+ olen = IOV_REMOVE_HEADER(&data, olen_storage);
if (!type || !olen)
return -1;
- val = packet_get(p, 0, offset + opt_off + 2, *olen, NULL);
- if (!val)
+ opt_len = iov_tail_size(&data);
+ if (opt_len < *olen)
return -1;
- memcpy(&opts[*type].c, val, *olen);
+ iov_to_buf(&data.iov[0], data.cnt, data.off, &opts[*type].c, *olen);
opts[*type].clen = *olen;
- opt_off += *olen + 2;
+ iov_tail_drop(&data, *olen);
+ opt_len -= *olen;
}
opts[80].slen = -1;
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 18/30] ip: Use iov_tail in ipv6_l4hdr()
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (16 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 17/30] dhcp: Convert to iov_tail Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 19/30] tap: Convert tap4_handler() to iov_tail Laurent Vivier
` (11 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Use packet_data() and extract headers using IOV_REMOVE_HEADER()
and IOV_PEEK_HEADER() rather than packet_get().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
ip.c | 32 +++++++++++++++-----------------
ip.h | 3 +--
packet.c | 1 +
tap.c | 4 +++-
4 files changed, 20 insertions(+), 20 deletions(-)
diff --git a/ip.c b/ip.c
index 2cc7f6548aff..50bd69a70596 100644
--- a/ip.c
+++ b/ip.c
@@ -23,50 +23,48 @@
/**
* ipv6_l4hdr() - Find pointer to L4 header in IPv6 packet and extract protocol
- * @p: Packet pool, packet number @idx has IPv6 header at @offset
- * @idx: Index of packet in pool
- * @offset: Pre-calculated IPv6 header offset
+ * @data: IPv6 packet
* @proto: Filled with L4 protocol number
* @dlen: Data length (payload excluding header extensions), set on return
*
- * Return: pointer to L4 header, NULL if not found
+ * Return: true if the L4 header is found and @data, @proto, @dlen are set,
+ * false on error. Outputs are indeterminate on failure.
*/
-char *ipv6_l4hdr(const struct pool *p, int idx, size_t offset, uint8_t *proto,
- size_t *dlen)
+bool ipv6_l4hdr(struct iov_tail *data, uint8_t *proto, size_t *dlen)
{
+ struct ipv6_opt_hdr o_storage;
const struct ipv6_opt_hdr *o;
+ struct ipv6hdr ip6h_storage;
const struct ipv6hdr *ip6h;
- char *base;
int hdrlen;
uint8_t nh;
- base = packet_get(p, idx, 0, 0, NULL);
- ip6h = packet_get(p, idx, offset, sizeof(*ip6h), dlen);
+ ip6h = IOV_REMOVE_HEADER(data, ip6h_storage);
if (!ip6h)
- return NULL;
-
- offset += sizeof(*ip6h);
+ return false;
+ *dlen = iov_tail_size(data);
nh = ip6h->nexthdr;
if (!IPV6_NH_OPT(nh))
goto found;
- while ((o = packet_get_try(p, idx, offset, sizeof(*o), dlen))) {
+ while ((o = IOV_PEEK_HEADER(data, o_storage))) {
+ *dlen = iov_tail_size(data) - sizeof(*o);
nh = o->nexthdr;
hdrlen = (o->hdrlen + 1) * 8;
if (IPV6_NH_OPT(nh))
- offset += hdrlen;
+ iov_tail_drop(data, hdrlen);
else
goto found;
}
- return NULL;
+ return false;
found:
if (nh == 59)
- return NULL;
+ return false;
*proto = nh;
- return base + offset;
+ return true;
}
diff --git a/ip.h b/ip.h
index 24509d9c11cd..5830b92302e2 100644
--- a/ip.h
+++ b/ip.h
@@ -115,8 +115,7 @@ static inline uint32_t ip6_get_flow_lbl(const struct ipv6hdr *ip6h)
ip6h->flow_lbl[2];
}
-char *ipv6_l4hdr(const struct pool *p, int idx, size_t offset, uint8_t *proto,
- size_t *dlen);
+bool ipv6_l4hdr(struct iov_tail *data, uint8_t *proto, size_t *dlen);
/* IPv6 link-local all-nodes multicast address, ff02::1 */
static const struct in6_addr in6addr_ll_all_nodes = {
diff --git a/packet.c b/packet.c
index 34b1722b9a03..014b353cdf8b 100644
--- a/packet.c
+++ b/packet.c
@@ -133,6 +133,7 @@ void packet_add_do(struct pool *p, struct iov_tail *data,
*
* Return: pointer to start of data range, NULL on invalid range or descriptor
*/
+/* cppcheck-suppress [staticFunction] */
void *packet_get_try_do(const struct pool *p, size_t idx, size_t offset,
size_t len, size_t *left, const char *func, int line)
{
diff --git a/tap.c b/tap.c
index 6a3dd37a0c45..3f090e1bf65d 100644
--- a/tap.c
+++ b/tap.c
@@ -909,8 +909,10 @@ resume:
if (plen != check)
continue;
- if (!(l4h = ipv6_l4hdr(in, i, sizeof(*eh), &proto, &l4len)))
+ data = IOV_TAIL_FROM_BUF(ip6h, sizeof(*ip6h) + check, 0);
+ if (!ipv6_l4hdr(&data, &proto, &l4len))
continue;
+ l4h = (char *)data.iov[0].iov_base + data.off;
if (IN6_IS_ADDR_LOOPBACK(saddr) || IN6_IS_ADDR_LOOPBACK(daddr)) {
char sstr[INET6_ADDRSTRLEN], dstr[INET6_ADDRSTRLEN];
--
@@ -909,8 +909,10 @@ resume:
if (plen != check)
continue;
- if (!(l4h = ipv6_l4hdr(in, i, sizeof(*eh), &proto, &l4len)))
+ data = IOV_TAIL_FROM_BUF(ip6h, sizeof(*ip6h) + check, 0);
+ if (!ipv6_l4hdr(&data, &proto, &l4len))
continue;
+ l4h = (char *)data.iov[0].iov_base + data.off;
if (IN6_IS_ADDR_LOOPBACK(saddr) || IN6_IS_ADDR_LOOPBACK(daddr)) {
char sstr[INET6_ADDRSTRLEN], dstr[INET6_ADDRSTRLEN];
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 19/30] tap: Convert tap4_handler() to iov_tail
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (17 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 18/30] ip: Use iov_tail in ipv6_l4hdr() Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 20/30] tap: Convert tap6_handler() " Laurent Vivier
` (10 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Use packet_data() and extract headers using IOV_PEEK_HEADER()
rather than packet_get().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
tap.c | 33 ++++++++++++++++++++-------------
1 file changed, 20 insertions(+), 13 deletions(-)
diff --git a/tap.c b/tap.c
index 3f090e1bf65d..30031c6e514e 100644
--- a/tap.c
+++ b/tap.c
@@ -704,28 +704,34 @@ static int tap4_handler(struct ctx *c, const struct pool *in,
i = 0;
resume:
for (seq_count = 0, seq = NULL; i < in->count; i++) {
- size_t l2len, l3len, hlen, l4len;
+ size_t l3len, hlen, l4len;
+ struct ethhdr eh_storage;
+ struct iphdr iph_storage;
+ struct udphdr uh_storage;
const struct ethhdr *eh;
const struct udphdr *uh;
struct iov_tail data;
struct iphdr *iph;
- const char *l4h;
- packet_get(in, i, 0, 0, &l2len);
+ if (!packet_data(in, i, &data))
+ continue;
- eh = packet_get(in, i, 0, sizeof(*eh), &l3len);
+ eh = IOV_PEEK_HEADER(&data, eh_storage);
if (!eh)
continue;
if (ntohs(eh->h_proto) == ETH_P_ARP) {
PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
- data = IOV_TAIL_FROM_BUF((void *)eh, l2len, 0);
packet_add(pkt, &data);
arp(c, pkt);
continue;
}
- iph = packet_get(in, i, sizeof(*eh), sizeof(*iph), NULL);
+ if (!iov_tail_drop(&data, sizeof(*eh)))
+ continue;
+ l3len = iov_tail_size(&data);
+
+ iph = IOV_PEEK_HEADER(&data, iph_storage);
if (!iph)
continue;
@@ -753,8 +759,9 @@ resume:
if (iph->saddr && c->ip4.addr_seen.s_addr != iph->saddr)
c->ip4.addr_seen.s_addr = iph->saddr;
- l4h = packet_get(in, i, sizeof(*eh) + hlen, l4len, NULL);
- if (!l4h)
+ if (!iov_tail_drop(&data, hlen))
+ continue;
+ if (iov_tail_size(&data) != l4len)
continue;
if (iph->protocol == IPPROTO_ICMP) {
@@ -765,7 +772,6 @@ resume:
tap_packet_debug(iph, NULL, NULL, 0, NULL, 1);
- 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,
@@ -773,15 +779,17 @@ resume:
continue;
}
- uh = packet_get(in, i, sizeof(*eh) + hlen, sizeof(*uh), NULL);
+ uh = IOV_PEEK_HEADER(&data, uh_storage);
if (!uh)
continue;
if (iph->protocol == IPPROTO_UDP) {
+ struct iov_tail eh_data;
+
PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
- data = IOV_TAIL_FROM_BUF((void *)eh, l2len, 0);
- packet_add(pkt, &data);
+ packet_data(in, i, &eh_data);
+ packet_add(pkt, &eh_data);
if (dhcp(c, pkt))
continue;
}
@@ -832,7 +840,6 @@ resume:
#undef L4_SET
append:
- data = IOV_TAIL_FROM_BUF((void *)l4h, l4len, 0);
packet_add((struct pool *)&seq->p, &data);
}
--
@@ -704,28 +704,34 @@ static int tap4_handler(struct ctx *c, const struct pool *in,
i = 0;
resume:
for (seq_count = 0, seq = NULL; i < in->count; i++) {
- size_t l2len, l3len, hlen, l4len;
+ size_t l3len, hlen, l4len;
+ struct ethhdr eh_storage;
+ struct iphdr iph_storage;
+ struct udphdr uh_storage;
const struct ethhdr *eh;
const struct udphdr *uh;
struct iov_tail data;
struct iphdr *iph;
- const char *l4h;
- packet_get(in, i, 0, 0, &l2len);
+ if (!packet_data(in, i, &data))
+ continue;
- eh = packet_get(in, i, 0, sizeof(*eh), &l3len);
+ eh = IOV_PEEK_HEADER(&data, eh_storage);
if (!eh)
continue;
if (ntohs(eh->h_proto) == ETH_P_ARP) {
PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
- data = IOV_TAIL_FROM_BUF((void *)eh, l2len, 0);
packet_add(pkt, &data);
arp(c, pkt);
continue;
}
- iph = packet_get(in, i, sizeof(*eh), sizeof(*iph), NULL);
+ if (!iov_tail_drop(&data, sizeof(*eh)))
+ continue;
+ l3len = iov_tail_size(&data);
+
+ iph = IOV_PEEK_HEADER(&data, iph_storage);
if (!iph)
continue;
@@ -753,8 +759,9 @@ resume:
if (iph->saddr && c->ip4.addr_seen.s_addr != iph->saddr)
c->ip4.addr_seen.s_addr = iph->saddr;
- l4h = packet_get(in, i, sizeof(*eh) + hlen, l4len, NULL);
- if (!l4h)
+ if (!iov_tail_drop(&data, hlen))
+ continue;
+ if (iov_tail_size(&data) != l4len)
continue;
if (iph->protocol == IPPROTO_ICMP) {
@@ -765,7 +772,6 @@ resume:
tap_packet_debug(iph, NULL, NULL, 0, NULL, 1);
- 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,
@@ -773,15 +779,17 @@ resume:
continue;
}
- uh = packet_get(in, i, sizeof(*eh) + hlen, sizeof(*uh), NULL);
+ uh = IOV_PEEK_HEADER(&data, uh_storage);
if (!uh)
continue;
if (iph->protocol == IPPROTO_UDP) {
+ struct iov_tail eh_data;
+
PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
- data = IOV_TAIL_FROM_BUF((void *)eh, l2len, 0);
- packet_add(pkt, &data);
+ packet_data(in, i, &eh_data);
+ packet_add(pkt, &eh_data);
if (dhcp(c, pkt))
continue;
}
@@ -832,7 +840,6 @@ resume:
#undef L4_SET
append:
- data = IOV_TAIL_FROM_BUF((void *)l4h, l4len, 0);
packet_add((struct pool *)&seq->p, &data);
}
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 20/30] tap: Convert tap6_handler() to iov_tail
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (18 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 19/30] tap: Convert tap4_handler() to iov_tail Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 21/30] packet: rename packet_data() to packet_get() Laurent Vivier
` (9 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
Use packet_data() and extract headers using IOV_REMOVE_HEADER()
and IOV_PEEK_HEADER() rather than packet_get().
Remove packet_get() as it is not used anymore.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
packet.c | 70 --------------------------------------------------------
packet.h | 11 ---------
tap.c | 25 ++++++++++++--------
3 files changed, 16 insertions(+), 90 deletions(-)
diff --git a/packet.c b/packet.c
index 014b353cdf8b..5da18bafa576 100644
--- a/packet.c
+++ b/packet.c
@@ -121,76 +121,6 @@ void packet_add_do(struct pool *p, struct iov_tail *data,
p->count++;
}
-/**
- * packet_get_try_do() - Get data range from packet descriptor from given pool
- * @p: Packet pool
- * @idx: Index of packet descriptor in pool
- * @offset: Offset of data range in packet descriptor
- * @len: Length of desired data range
- * @left: Length of available data after range, set on return, can be NULL
- * @func: For tracing: name of calling function
- * @line: For tracing: caller line of function call
- *
- * Return: pointer to start of data range, NULL on invalid range or descriptor
- */
-/* cppcheck-suppress [staticFunction] */
-void *packet_get_try_do(const struct pool *p, size_t idx, size_t offset,
- size_t len, size_t *left, const char *func, int line)
-{
- char *ptr;
-
- ASSERT_WITH_MSG(p->count <= p->size,
- "Corrupt pool count: %zu, size: %zu, %s:%i",
- p->count, p->size, func, line);
-
- if (idx >= p->count) {
- debug("packet %zu from pool count: %zu, %s:%i",
- idx, p->count, func, line);
- return NULL;
- }
-
- if (offset > p->pkt[idx].iov_len ||
- len > (p->pkt[idx].iov_len - offset))
- return NULL;
-
- ptr = (char *)p->pkt[idx].iov_base + offset;
-
- ASSERT_WITH_MSG(!packet_check_range(p, ptr, len, func, line),
- "Corrupt packet pool, %s:%i", func, line);
-
- if (left)
- *left = p->pkt[idx].iov_len - offset - len;
-
- return ptr;
-}
-
-/**
- * packet_get_do() - Get data range from packet descriptor from given pool
- * @p: Packet pool
- * @idx: Index of packet descriptor in pool
- * @offset: Offset of data range in packet descriptor
- * @len: Length of desired data range
- * @left: Length of available data after range, set on return, can be NULL
- * @func: For tracing: name of calling function
- * @line: For tracing: caller line of function call
- *
- * Return: as packet_get_try_do() but log a trace message when returning NULL
- */
-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 *r = packet_get_try_do(p, idx, offset, len, left, func, line);
-
- if (!r) {
- trace("missing packet data length %zu, offset %zu from "
- "length %zu, %s:%i",
- len, offset, p->pkt[idx].iov_len, func, line);
- }
-
- return r;
-}
-
/**
* packet_data_do() - Get data range from packet descriptor from given pool
* @p: Packet pool
diff --git a/packet.h b/packet.h
index 062afb978124..dab8274fa5c5 100644
--- a/packet.h
+++ b/packet.h
@@ -33,12 +33,6 @@ struct pool {
int vu_packet_check_range(void *buf, const char *ptr, size_t len);
void packet_add_do(struct pool *p, struct iov_tail *data,
const char *func, int line);
-void *packet_get_try_do(const struct pool *p, const size_t idx,
- size_t offset, size_t len, size_t *left,
- 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);
bool packet_data_do(const struct pool *p, const size_t idx,
struct iov_tail *data,
const char *func, int line);
@@ -47,11 +41,6 @@ void pool_flush(struct pool *p);
#define packet_add(p, data) \
packet_add_do(p, data, __func__, __LINE__)
-
-#define packet_get_try(p, idx, offset, len, left) \
- packet_get_try_do(p, idx, offset, len, left, __func__, __LINE__)
-#define packet_get(p, idx, offset, len, left) \
- packet_get_do(p, idx, offset, len, left, __func__, __LINE__)
#define packet_data(p, idx, data) \
packet_data_do(p, idx, data, __func__, __LINE__)
diff --git a/tap.c b/tap.c
index 30031c6e514e..7cf807149879 100644
--- a/tap.c
+++ b/tap.c
@@ -894,21 +894,28 @@ resume:
for (seq_count = 0, seq = NULL; i < in->count; i++) {
size_t l4len, plen, check;
struct in6_addr *saddr, *daddr;
+ struct ipv6hdr ip6h_storage;
+ struct ethhdr eh_storage;
+ struct udphdr uh_storage;
const struct ethhdr *eh;
const struct udphdr *uh;
struct iov_tail data;
struct ipv6hdr *ip6h;
uint8_t proto;
- char *l4h;
- eh = packet_get(in, i, 0, sizeof(*eh), NULL);
+ if (!packet_data(in, i, &data))
+ return -1;
+
+ eh = IOV_REMOVE_HEADER(&data, eh_storage);
if (!eh)
continue;
- ip6h = packet_get(in, i, sizeof(*eh), sizeof(*ip6h), &check);
+ ip6h = IOV_PEEK_HEADER(&data, ip6h_storage);
if (!ip6h)
continue;
+ check = iov_tail_size(&data) - sizeof(*ip6h);
+
saddr = &ip6h->saddr;
daddr = &ip6h->daddr;
@@ -916,10 +923,8 @@ resume:
if (plen != check)
continue;
- data = IOV_TAIL_FROM_BUF(ip6h, sizeof(*ip6h) + check, 0);
if (!ipv6_l4hdr(&data, &proto, &l4len))
continue;
- l4h = (char *)data.iov[0].iov_base + data.off;
if (IN6_IS_ADDR_LOOPBACK(saddr) || IN6_IS_ADDR_LOOPBACK(daddr)) {
char sstr[INET6_ADDRSTRLEN], dstr[INET6_ADDRSTRLEN];
@@ -944,6 +949,8 @@ resume:
}
if (proto == IPPROTO_ICMPV6) {
+ struct icmp6hdr l4h_storage;
+ const struct icmp6hdr *l4h;
PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
if (c->no_icmp)
@@ -952,9 +959,9 @@ resume:
if (l4len < sizeof(struct icmp6hdr))
continue;
- data = IOV_TAIL_FROM_BUF(l4h, l4len, 0);
packet_add(pkt, &data);
+ l4h = IOV_PEEK_HEADER(&data, l4h_storage);
if (ndp(c, (struct icmp6hdr *)l4h, saddr, pkt))
continue;
@@ -967,12 +974,13 @@ resume:
if (l4len < sizeof(*uh))
continue;
- uh = (struct udphdr *)l4h;
+ uh = IOV_PEEK_HEADER(&data, uh_storage);
+ if (!uh)
+ continue;
if (proto == IPPROTO_UDP) {
PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
- data = IOV_TAIL_FROM_BUF(l4h, l4len, 0);
packet_add(pkt, &data);
if (dhcpv6(c, pkt, saddr, daddr))
@@ -1029,7 +1037,6 @@ resume:
#undef L4_SET
append:
- data = IOV_TAIL_FROM_BUF(l4h, l4len, 0);
packet_add((struct pool *)&seq->p, &data);
}
--
@@ -894,21 +894,28 @@ resume:
for (seq_count = 0, seq = NULL; i < in->count; i++) {
size_t l4len, plen, check;
struct in6_addr *saddr, *daddr;
+ struct ipv6hdr ip6h_storage;
+ struct ethhdr eh_storage;
+ struct udphdr uh_storage;
const struct ethhdr *eh;
const struct udphdr *uh;
struct iov_tail data;
struct ipv6hdr *ip6h;
uint8_t proto;
- char *l4h;
- eh = packet_get(in, i, 0, sizeof(*eh), NULL);
+ if (!packet_data(in, i, &data))
+ return -1;
+
+ eh = IOV_REMOVE_HEADER(&data, eh_storage);
if (!eh)
continue;
- ip6h = packet_get(in, i, sizeof(*eh), sizeof(*ip6h), &check);
+ ip6h = IOV_PEEK_HEADER(&data, ip6h_storage);
if (!ip6h)
continue;
+ check = iov_tail_size(&data) - sizeof(*ip6h);
+
saddr = &ip6h->saddr;
daddr = &ip6h->daddr;
@@ -916,10 +923,8 @@ resume:
if (plen != check)
continue;
- data = IOV_TAIL_FROM_BUF(ip6h, sizeof(*ip6h) + check, 0);
if (!ipv6_l4hdr(&data, &proto, &l4len))
continue;
- l4h = (char *)data.iov[0].iov_base + data.off;
if (IN6_IS_ADDR_LOOPBACK(saddr) || IN6_IS_ADDR_LOOPBACK(daddr)) {
char sstr[INET6_ADDRSTRLEN], dstr[INET6_ADDRSTRLEN];
@@ -944,6 +949,8 @@ resume:
}
if (proto == IPPROTO_ICMPV6) {
+ struct icmp6hdr l4h_storage;
+ const struct icmp6hdr *l4h;
PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
if (c->no_icmp)
@@ -952,9 +959,9 @@ resume:
if (l4len < sizeof(struct icmp6hdr))
continue;
- data = IOV_TAIL_FROM_BUF(l4h, l4len, 0);
packet_add(pkt, &data);
+ l4h = IOV_PEEK_HEADER(&data, l4h_storage);
if (ndp(c, (struct icmp6hdr *)l4h, saddr, pkt))
continue;
@@ -967,12 +974,13 @@ resume:
if (l4len < sizeof(*uh))
continue;
- uh = (struct udphdr *)l4h;
+ uh = IOV_PEEK_HEADER(&data, uh_storage);
+ if (!uh)
+ continue;
if (proto == IPPROTO_UDP) {
PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
- data = IOV_TAIL_FROM_BUF(l4h, l4len, 0);
packet_add(pkt, &data);
if (dhcpv6(c, pkt, saddr, daddr))
@@ -1029,7 +1037,6 @@ resume:
#undef L4_SET
append:
- data = IOV_TAIL_FROM_BUF(l4h, l4len, 0);
packet_add((struct pool *)&seq->p, &data);
}
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 21/30] packet: rename packet_data() to packet_get()
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (19 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 20/30] tap: Convert tap6_handler() " Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 22/30] arp: use iov_tail rather than pool Laurent Vivier
` (8 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
As we have removed packet_get(), we can rename packet_data() to packet_get()
as the name is clearer.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
arp.c | 2 +-
dhcp.c | 2 +-
dhcpv6.c | 2 +-
icmp.c | 2 +-
ndp.c | 2 +-
packet.c | 8 ++++----
packet.h | 9 ++++-----
tap.c | 6 +++---
tcp.c | 4 ++--
udp.c | 4 ++--
10 files changed, 20 insertions(+), 21 deletions(-)
diff --git a/arp.c b/arp.c
index 6617698f125b..65f0f2627db6 100644
--- a/arp.c
+++ b/arp.c
@@ -82,7 +82,7 @@ int arp(const struct ctx *c, const struct pool *p)
const struct arpmsg *am;
struct iov_tail data;
- if (!packet_data(p, 0, &data))
+ if (!packet_get(p, 0, &data))
return -1;
eh = IOV_REMOVE_HEADER(&data, eh_storage);
diff --git a/dhcp.c b/dhcp.c
index 7c92f30d0b39..4fdf9a7d0264 100644
--- a/dhcp.c
+++ b/dhcp.c
@@ -317,7 +317,7 @@ int dhcp(const struct ctx *c, const struct pool *p)
unsigned int i;
struct msg mc;
- if (!packet_data(p, 0, &data))
+ if (!packet_get(p, 0, &data))
return -1;
eh = IOV_REMOVE_HEADER(&data, eh_storage);
diff --git a/dhcpv6.c b/dhcpv6.c
index bd6268804d95..f73aa68a298e 100644
--- a/dhcpv6.c
+++ b/dhcpv6.c
@@ -559,7 +559,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
const struct udphdr *uh;
size_t mlen, n;
- if (!packet_data(p, 0, &data))
+ if (!packet_get(p, 0, &data))
return -1;
uh = IOV_REMOVE_HEADER(&data, uh_storage);
diff --git a/icmp.c b/icmp.c
index 37edf6c81b89..393675d44f0e 100644
--- a/icmp.c
+++ b/icmp.c
@@ -251,7 +251,7 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
(void)saddr;
ASSERT(pif == PIF_TAP);
- if (!packet_data(p, 0, &data))
+ if (!packet_get(p, 0, &data))
return -1;
if (af == AF_INET) {
diff --git a/ndp.c b/ndp.c
index 77722353be7b..fb2bf1161b66 100644
--- a/ndp.c
+++ b/ndp.c
@@ -355,7 +355,7 @@ int ndp(const struct ctx *c, const struct icmp6hdr *ih,
const struct ndp_ns *ns;
struct iov_tail data;
- if (!packet_data(p, 0, &data))
+ if (!packet_get(p, 0, &data))
return -1;
ns = IOV_REMOVE_HEADER(&data, ns_storage);
diff --git a/packet.c b/packet.c
index 5da18bafa576..cbc43c2fc22d 100644
--- a/packet.c
+++ b/packet.c
@@ -122,7 +122,7 @@ void packet_add_do(struct pool *p, struct iov_tail *data,
}
/**
- * packet_data_do() - Get data range from packet descriptor from given pool
+ * packet_get_do() - Get data range from packet descriptor from given pool
* @p: Packet pool
* @idx: Index of packet descriptor in pool
* @data: IOV tail to store the address of the data (output)
@@ -132,9 +132,9 @@ void packet_add_do(struct pool *p, struct iov_tail *data,
* Return: false if packet index is invalid, true otherwise.
* If something wrong with @data, don't return at all (assert).
*/
-bool packet_data_do(const struct pool *p, size_t idx,
- struct iov_tail *data,
- const char *func, int line)
+bool packet_get_do(const struct pool *p, size_t idx,
+ struct iov_tail *data,
+ const char *func, int line)
{
size_t i;
diff --git a/packet.h b/packet.h
index dab8274fa5c5..7afe80ef3fcf 100644
--- a/packet.h
+++ b/packet.h
@@ -33,16 +33,15 @@ struct pool {
int vu_packet_check_range(void *buf, const char *ptr, size_t len);
void packet_add_do(struct pool *p, struct iov_tail *data,
const char *func, int line);
-bool packet_data_do(const struct pool *p, const size_t idx,
- struct iov_tail *data,
- const char *func, int line);
+bool packet_get_do(const struct pool *p, const size_t idx,
+ struct iov_tail *data, const char *func, int line);
bool pool_full(const struct pool *p);
void pool_flush(struct pool *p);
#define packet_add(p, data) \
packet_add_do(p, data, __func__, __LINE__)
-#define packet_data(p, idx, data) \
- packet_data_do(p, idx, data, __func__, __LINE__)
+#define packet_get(p, idx, data) \
+ packet_get_do(p, idx, data, __func__, __LINE__)
#define PACKET_POOL_DECL(_name, _size, _buf) \
struct _name ## _t { \
diff --git a/tap.c b/tap.c
index 7cf807149879..09da76ea23bc 100644
--- a/tap.c
+++ b/tap.c
@@ -713,7 +713,7 @@ resume:
struct iov_tail data;
struct iphdr *iph;
- if (!packet_data(in, i, &data))
+ if (!packet_get(in, i, &data))
continue;
eh = IOV_PEEK_HEADER(&data, eh_storage);
@@ -788,7 +788,7 @@ resume:
PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
- packet_data(in, i, &eh_data);
+ packet_get(in, i, &eh_data);
packet_add(pkt, &eh_data);
if (dhcp(c, pkt))
continue;
@@ -903,7 +903,7 @@ resume:
struct ipv6hdr *ip6h;
uint8_t proto;
- if (!packet_data(in, i, &data))
+ if (!packet_get(in, i, &data))
return -1;
eh = IOV_REMOVE_HEADER(&data, eh_storage);
diff --git a/tcp.c b/tcp.c
index be1d3e0c0d32..bfc1c38f573d 100644
--- a/tcp.c
+++ b/tcp.c
@@ -1655,7 +1655,7 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
size_t off, size;
int count;
- if (!packet_data(p, i, &data))
+ if (!packet_get(p, i, &data))
return -1;
th = IOV_PEEK_HEADER(&data, th_storage);
@@ -1986,7 +1986,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
(void)pif;
- if (!packet_data(p, idx, &data))
+ if (!packet_get(p, idx, &data))
return 1;
l4len = iov_tail_size(&data);
diff --git a/udp.c b/udp.c
index cfa653508d9c..0b0a88ad0d1c 100644
--- a/udp.c
+++ b/udp.c
@@ -990,7 +990,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
ASSERT(!c->no_udp);
- if (!packet_data(p, idx, &data))
+ if (!packet_get(p, idx, &data))
return 1;
uh = IOV_PEEK_HEADER(&data, uh_storage);
@@ -1033,7 +1033,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
for (i = 0, j = 0; i < (int)p->count - idx && j < UIO_MAXIOV; i++) {
const struct udphdr *uh_send;
- if (!packet_data(p, idx + i, &data))
+ if (!packet_get(p, idx + i, &data))
return p->count - idx;
uh_send = IOV_REMOVE_HEADER(&data, uh_storage);
--
@@ -990,7 +990,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
ASSERT(!c->no_udp);
- if (!packet_data(p, idx, &data))
+ if (!packet_get(p, idx, &data))
return 1;
uh = IOV_PEEK_HEADER(&data, uh_storage);
@@ -1033,7 +1033,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
for (i = 0, j = 0; i < (int)p->count - idx && j < UIO_MAXIOV; i++) {
const struct udphdr *uh_send;
- if (!packet_data(p, idx + i, &data))
+ if (!packet_get(p, idx + i, &data))
return p->count - idx;
uh_send = IOV_REMOVE_HEADER(&data, uh_storage);
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 22/30] arp: use iov_tail rather than pool
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (20 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 21/30] packet: rename packet_data() to packet_get() Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 23/30] dhcp: " Laurent Vivier
` (7 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
The arp() function signature is changed to accept `struct iov_tail *data`
directly, replacing the previous `const struct pool *p` parameter.
Consequently, arp() no longer fetches packet data internally using
packet_data(), streamlining its logic.
This simplifies callers like tap4_handler(), which now pass the iov_tail
for the L2 ARP frame directly, removing intermediate pool handling.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
arp.c | 14 +++++---------
arp.h | 2 +-
tap.c | 5 +----
3 files changed, 7 insertions(+), 14 deletions(-)
diff --git a/arp.c b/arp.c
index 65f0f2627db6..b255e30b739d 100644
--- a/arp.c
+++ b/arp.c
@@ -63,11 +63,11 @@ static bool accept_arp(const struct ctx *c,
/**
* arp() - Check if this is a supported ARP message, reply as needed
* @c: Execution context
- * @p: Packet pool, single packet with Ethernet buffer
+ * @data: Single packet with Ethernet buffer
*
* Return: 1 if handled, -1 on failure
*/
-int arp(const struct ctx *c, const struct pool *p)
+int arp(const struct ctx *c, struct iov_tail *data)
{
struct {
struct ethhdr eh;
@@ -80,14 +80,10 @@ int arp(const struct ctx *c, const struct pool *p)
const struct ethhdr *eh;
const struct arphdr *ah;
const struct arpmsg *am;
- struct iov_tail data;
- if (!packet_get(p, 0, &data))
- return -1;
-
- eh = IOV_REMOVE_HEADER(&data, eh_storage);
- ah = IOV_REMOVE_HEADER(&data, ah_storage);
- am = IOV_REMOVE_HEADER(&data, am_storage);
+ eh = IOV_REMOVE_HEADER(data, eh_storage);
+ ah = IOV_REMOVE_HEADER(data, ah_storage);
+ am = IOV_REMOVE_HEADER(data, am_storage);
if (!eh || !ah || !am)
return -1;
diff --git a/arp.h b/arp.h
index ac5cd16e47f4..86bcbf878eda 100644
--- a/arp.h
+++ b/arp.h
@@ -20,6 +20,6 @@ struct arpmsg {
unsigned char tip[4];
} __attribute__((__packed__));
-int arp(const struct ctx *c, const struct pool *p);
+int arp(const struct ctx *c, struct iov_tail *data);
#endif /* ARP_H */
diff --git a/tap.c b/tap.c
index 09da76ea23bc..1daa3009cc65 100644
--- a/tap.c
+++ b/tap.c
@@ -720,10 +720,7 @@ resume:
if (!eh)
continue;
if (ntohs(eh->h_proto) == ETH_P_ARP) {
- PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
-
- packet_add(pkt, &data);
- arp(c, pkt);
+ arp(c, &data);
continue;
}
--
@@ -720,10 +720,7 @@ resume:
if (!eh)
continue;
if (ntohs(eh->h_proto) == ETH_P_ARP) {
- PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
-
- packet_add(pkt, &data);
- arp(c, pkt);
+ arp(c, &data);
continue;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 23/30] dhcp: use iov_tail rather than pool
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (21 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 22/30] arp: use iov_tail rather than pool Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 24/30] dhcpv6: " Laurent Vivier
` (6 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
This patch refactors the dhcp() function to accept `struct iov_tail *data`
directly as its packet input, replacing the previous `const struct pool *p`
parameter. Consequently, dhcp() no longer fetches packet data internally
using packet_data().
This change simplifies callers, such as tap4_handler(), which now pass
the iov_tail representing the L2 frame directly to dhcp(). This removes
the need for intermediate packet pool handling for DHCP processing.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
dhcp.c | 32 ++++++++++++++------------------
dhcp.h | 2 +-
tap.c | 5 +----
3 files changed, 16 insertions(+), 23 deletions(-)
diff --git a/dhcp.c b/dhcp.c
index 4fdf9a7d0264..96fa802d8b7b 100644
--- a/dhcp.c
+++ b/dhcp.c
@@ -296,11 +296,11 @@ static void opt_set_dns_search(const struct ctx *c, size_t max_len)
/**
* dhcp() - Check if this is a DHCP message, reply as needed
* @c: Execution context
- * @p: Packet pool, single packet with Ethernet buffer
+ * @data: Single packet with Ethernet buffer
*
* Return: 0 if it's not a DHCP message, 1 if handled, -1 on failure
*/
-int dhcp(const struct ctx *c, const struct pool *p)
+int dhcp(const struct ctx *c, struct iov_tail *data)
{
char macstr[ETH_ADDRSTRLEN];
size_t mlen, dlen, opt_len;
@@ -311,24 +311,20 @@ int dhcp(const struct ctx *c, const struct pool *p)
const struct ethhdr *eh;
const struct iphdr *iph;
const struct udphdr *uh;
- struct iov_tail data;
struct msg const *m;
struct msg reply;
unsigned int i;
struct msg mc;
- if (!packet_get(p, 0, &data))
- return -1;
-
- eh = IOV_REMOVE_HEADER(&data, eh_storage);
- iph = IOV_PEEK_HEADER(&data, iph_storage);
+ eh = IOV_REMOVE_HEADER(data, eh_storage);
+ iph = IOV_PEEK_HEADER(data, iph_storage);
if (!eh || !iph)
return -1;
- if (!iov_tail_drop(&data, iph->ihl * 4UL))
+ if (!iov_tail_drop(data, iph->ihl * 4UL))
return -1;
- uh = IOV_REMOVE_HEADER(&data, uh_storage);
+ uh = IOV_REMOVE_HEADER(data, uh_storage);
if (!uh)
return -1;
@@ -338,8 +334,8 @@ int dhcp(const struct ctx *c, const struct pool *p)
if (c->no_dhcp)
return 1;
- mlen = iov_tail_size(&data);
- m = (struct msg const *)iov_remove_header_(&data, &mc, offsetof(struct msg, o),
+ mlen = iov_tail_size(data);
+ m = (struct msg const *)iov_remove_header_(data, &mc, offsetof(struct msg, o),
__alignof__(struct msg));
if (!m ||
mlen != ntohs(uh->len) - sizeof(*uh) ||
@@ -366,24 +362,24 @@ int dhcp(const struct ctx *c, const struct pool *p)
for (i = 0; i < ARRAY_SIZE(opts); i++)
opts[i].clen = -1;
- opt_len = iov_tail_size(&data);
+ opt_len = iov_tail_size(data);
while (opt_len >= 2) {
uint8_t olen_storage, type_storage;
const uint8_t *olen;
uint8_t *type;
- type = IOV_REMOVE_HEADER(&data, type_storage);
- olen = IOV_REMOVE_HEADER(&data, olen_storage);
+ type = IOV_REMOVE_HEADER(data, type_storage);
+ olen = IOV_REMOVE_HEADER(data, olen_storage);
if (!type || !olen)
return -1;
- opt_len = iov_tail_size(&data);
+ opt_len = iov_tail_size(data);
if (opt_len < *olen)
return -1;
- iov_to_buf(&data.iov[0], data.cnt, data.off, &opts[*type].c, *olen);
+ iov_to_buf(&data->iov[0], data->cnt, data->off, &opts[*type].c, *olen);
opts[*type].clen = *olen;
- iov_tail_drop(&data, *olen);
+ iov_tail_drop(data, *olen);
opt_len -= *olen;
}
diff --git a/dhcp.h b/dhcp.h
index 87aeecd8dec8..cd50c99b8856 100644
--- a/dhcp.h
+++ b/dhcp.h
@@ -6,7 +6,7 @@
#ifndef DHCP_H
#define DHCP_H
-int dhcp(const struct ctx *c, const struct pool *p);
+int dhcp(const struct ctx *c, struct iov_tail *data);
void dhcp_init(void);
#endif /* DHCP_H */
diff --git a/tap.c b/tap.c
index 1daa3009cc65..daeba0eb6078 100644
--- a/tap.c
+++ b/tap.c
@@ -783,11 +783,8 @@ resume:
if (iph->protocol == IPPROTO_UDP) {
struct iov_tail eh_data;
- PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
-
packet_get(in, i, &eh_data);
- packet_add(pkt, &eh_data);
- if (dhcp(c, pkt))
+ if (dhcp(c, &eh_data))
continue;
}
--
@@ -783,11 +783,8 @@ resume:
if (iph->protocol == IPPROTO_UDP) {
struct iov_tail eh_data;
- PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
-
packet_get(in, i, &eh_data);
- packet_add(pkt, &eh_data);
- if (dhcp(c, pkt))
+ if (dhcp(c, &eh_data))
continue;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 24/30] dhcpv6: use iov_tail rather than pool
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (22 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 23/30] dhcp: " Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 25/30] icmp: " Laurent Vivier
` (5 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
This patch refactors the dhcpv6() function to accept `struct iov_tail *data`
directly as its packet input, replacing the `const struct pool *p` parameter.
Consequently, dhcpv6() no longer fetches packet data internally using
packet_data().
This change simplifies callers, such as tap6_handler(), which now pass
the iov_tail representing the L4 UDP segment (DHCPv6 message) directly.
This removes the need for intermediate packet pool handling.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
dhcpv6.c | 29 +++++++++++++----------------
dhcpv6.h | 2 +-
tap.c | 6 ++----
3 files changed, 16 insertions(+), 21 deletions(-)
diff --git a/dhcpv6.c b/dhcpv6.c
index f73aa68a298e..a54db137061c 100644
--- a/dhcpv6.c
+++ b/dhcpv6.c
@@ -536,19 +536,19 @@ static size_t dhcpv6_client_fqdn_fill(const struct iov_tail *data,
/**
* dhcpv6() - Check if this is a DHCPv6 message, reply as needed
* @c: Execution context
- * @p: Packet pool, single packet starting from UDP header
+ * @data: Single packet starting from UDP header
* @saddr: Source IPv6 address of original message
* @daddr: Destination IPv6 address of original message
*
* Return: 0 if it's not a DHCPv6 message, 1 if handled, -1 on failure
*/
-int dhcpv6(struct ctx *c, const struct pool *p,
+int dhcpv6(struct ctx *c, struct iov_tail *data,
const struct in6_addr *saddr, const struct in6_addr *daddr)
{
const struct opt_server_id *server_id = NULL;
- struct iov_tail data, opt, client_id_base;
const struct opt_hdr *client_id = NULL;
struct opt_server_id server_id_storage;
+ struct iov_tail opt, client_id_base;
const struct opt_ia_na *ia = NULL;
struct opt_hdr client_id_storage;
struct opt_ia_na ia_storage;
@@ -559,10 +559,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
const struct udphdr *uh;
size_t mlen, n;
- if (!packet_get(p, 0, &data))
- return -1;
-
- uh = IOV_REMOVE_HEADER(&data, uh_storage);
+ uh = IOV_REMOVE_HEADER(data, uh_storage);
if (!uh)
return -1;
@@ -575,7 +572,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
if (!IN6_IS_ADDR_MULTICAST(daddr))
return -1;
- mlen = iov_tail_size(&data);
+ mlen = iov_tail_size(data);
if (mlen + sizeof(*uh) != ntohs(uh->len) || mlen < sizeof(*mh))
return -1;
@@ -583,23 +580,23 @@ int dhcpv6(struct ctx *c, const struct pool *p,
src = &c->ip6.our_tap_ll;
- mh = IOV_REMOVE_HEADER(&data, mh_storage);
+ mh = IOV_REMOVE_HEADER(data, mh_storage);
if (!mh)
return -1;
- client_id_base = data;
+ client_id_base = *data;
if (dhcpv6_opt(&client_id_base, OPT_CLIENTID))
client_id = IOV_PEEK_HEADER(&client_id_base, client_id_storage);
if (!client_id || ntohs(client_id->l) > OPT_VSIZE(client_id))
return -1;
- opt = data;
+ opt = *data;
if (dhcpv6_opt(&opt, OPT_SERVERID))
server_id = IOV_PEEK_HEADER(&opt, server_id_storage);
if (server_id && ntohs(server_id->hdr.l) != OPT_VSIZE(server_id))
return -1;
- opt = data;
+ opt = *data;
if (dhcpv6_opt(&opt, OPT_IA_NA))
ia = IOV_PEEK_HEADER(&opt, ia_storage);
if (ia && ntohs(ia->hdr.l) < MIN(OPT_VSIZE(ia_na), OPT_VSIZE(ia_ta)))
@@ -617,9 +614,9 @@ int dhcpv6(struct ctx *c, const struct pool *p,
if (mh->type == TYPE_CONFIRM && server_id)
return -1;
- if (dhcpv6_ia_notonlink(&data, &c->ip6.addr)) {
+ if (dhcpv6_ia_notonlink(data, &c->ip6.addr)) {
- dhcpv6_send_ia_notonlink(c, &data, &client_id_base,
+ dhcpv6_send_ia_notonlink(c, data, &client_id_base,
ntohs(client_id->l), mh->xid);
return 1;
@@ -632,7 +629,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
memcmp(&resp.server_id, server_id, sizeof(resp.server_id)))
return -1;
- if (ia || dhcpv6_opt(&data, OPT_IA_TA))
+ if (ia || dhcpv6_opt(data, OPT_IA_TA))
return -1;
info("DHCPv6: received INFORMATION_REQUEST, sending REPLY");
@@ -665,7 +662,7 @@ int dhcpv6(struct ctx *c, const struct pool *p,
n = offsetof(struct resp_t, client_id) +
sizeof(struct opt_hdr) + ntohs(client_id->l);
n = dhcpv6_dns_fill(c, (char *)&resp, n);
- n = dhcpv6_client_fqdn_fill(&data, c, (char *)&resp, n);
+ n = dhcpv6_client_fqdn_fill(data, c, (char *)&resp, n);
resp.hdr.xid = mh->xid;
diff --git a/dhcpv6.h b/dhcpv6.h
index 580998862227..c706dfdbb2ac 100644
--- a/dhcpv6.h
+++ b/dhcpv6.h
@@ -6,7 +6,7 @@
#ifndef DHCPV6_H
#define DHCPV6_H
-int dhcpv6(struct ctx *c, const struct pool *p,
+int dhcpv6(struct ctx *c, struct iov_tail *data,
struct in6_addr *saddr, struct in6_addr *daddr);
void dhcpv6_init(const struct ctx *c);
diff --git a/tap.c b/tap.c
index daeba0eb6078..f0dc4d157cea 100644
--- a/tap.c
+++ b/tap.c
@@ -973,11 +973,9 @@ resume:
continue;
if (proto == IPPROTO_UDP) {
- PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
-
- packet_add(pkt, &data);
+ struct iov_tail uh_data = data;
- if (dhcpv6(c, pkt, saddr, daddr))
+ if (dhcpv6(c, &uh_data, saddr, daddr))
continue;
}
--
@@ -973,11 +973,9 @@ resume:
continue;
if (proto == IPPROTO_UDP) {
- PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
-
- packet_add(pkt, &data);
+ struct iov_tail uh_data = data;
- if (dhcpv6(c, pkt, saddr, daddr))
+ if (dhcpv6(c, &uh_data, saddr, daddr))
continue;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 25/30] icmp: use iov_tail rather than pool
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (23 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 24/30] dhcpv6: " Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 26/30] ndp: " Laurent Vivier
` (4 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
As the iov_tail has a non zero offset (because of the presence of
packet headers in the iov array), we must copy it to a new
iov array (using iov_tail_splice()) to pass it to sendmsg().
We cannot use anymore iov_tail_msghdr(), so remove it.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
icmp.c | 30 +++++++++++++++++++-----------
icmp.h | 2 +-
iov.c | 23 -----------------------
iov.h | 2 --
tap.c | 7 ++-----
5 files changed, 22 insertions(+), 42 deletions(-)
diff --git a/icmp.c b/icmp.c
index 393675d44f0e..21cfedc91cbd 100644
--- a/icmp.c
+++ b/icmp.c
@@ -44,6 +44,7 @@
#define ICMP_ECHO_TIMEOUT 60 /* s, timeout for ICMP socket activity */
#define ICMP_NUM_IDS (1U << 16)
+#define MAX_IOV_ICMP 16 /* Arbitrary, should be enough */
/**
* ping_at_sidx() - Get ping specific flow at given sidx
@@ -229,36 +230,33 @@ cancel:
* @af: Address family, AF_INET or AF_INET6
* @saddr: Source address
* @daddr: Destination address
- * @p: Packet pool, single packet with ICMP/ICMPv6 header
+ * @data: Single packet with ICMP/ICMPv6 header
* @now: Current timestamp
*
* Return: count of consumed packets (always 1, even if malformed)
*/
int icmp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
const void *saddr, const void *daddr,
- const struct pool *p, const struct timespec *now)
+ struct iov_tail *data, const struct timespec *now)
{
+ struct iovec iov[MAX_IOV_ICMP];
struct icmp_ping_flow *pingf;
const struct flowside *tgt;
union sockaddr_inany sa;
- struct iov_tail data;
struct msghdr msh;
uint16_t id, seq;
union flow *flow;
uint8_t proto;
- socklen_t sl;
+ int cnt;
(void)saddr;
ASSERT(pif == PIF_TAP);
- if (!packet_get(p, 0, &data))
- return -1;
-
if (af == AF_INET) {
struct icmphdr ih_storage;
const struct icmphdr *ih;
- ih = IOV_PEEK_HEADER(&data, ih_storage);
+ ih = IOV_PEEK_HEADER(data, ih_storage);
if (!ih)
return 1;
@@ -272,7 +270,7 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
struct icmp6hdr ih_storage;
const struct icmp6hdr *ih;
- ih = IOV_PEEK_HEADER(&data, ih_storage);
+ ih = IOV_PEEK_HEADER(data, ih_storage);
if (!ih)
return 1;
@@ -286,6 +284,10 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
ASSERT(0);
}
+ cnt = iov_tail_clone(&iov[0], MAX_IOV_ICMP, data);
+ if (cnt < 0)
+ return 1;
+
flow = flow_at_sidx(flow_lookup_af(c, proto, PIF_TAP,
af, saddr, daddr, id, id));
@@ -300,8 +302,14 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
pingf->ts = now->tv_sec;
- pif_sockaddr(c, &sa, &sl, PIF_HOST, &tgt->eaddr, 0);
- iov_tail_msghdr(&msh, &data, &sa, sl);
+ pif_sockaddr(c, &sa, &msh.msg_namelen, PIF_HOST, &tgt->eaddr, 0);
+ msh.msg_name = &sa;
+ msh.msg_iov = iov;
+ msh.msg_iovlen = cnt;
+ msh.msg_control = NULL;
+ msh.msg_controllen = 0;
+ msh.msg_flags = 0;
+
if (sendmsg(pingf->sock, &msh, MSG_NOSIGNAL) < 0) {
flow_dbg_perror(pingf, "failed to relay request to socket");
} else {
diff --git a/icmp.h b/icmp.h
index 5ce22b5eca1f..d1cecb20e29d 100644
--- a/icmp.h
+++ b/icmp.h
@@ -14,7 +14,7 @@ struct icmp_ping_flow;
void icmp_sock_handler(const struct ctx *c, union epoll_ref ref);
int icmp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
const void *saddr, const void *daddr,
- const struct pool *p, const struct timespec *now);
+ struct iov_tail *data, const struct timespec *now);
void icmp_init(void);
/**
diff --git a/iov.c b/iov.c
index 76d261f804ca..0aa08c141965 100644
--- a/iov.c
+++ b/iov.c
@@ -157,29 +157,6 @@ size_t iov_size(const struct iovec *iov, size_t iov_cnt)
return len;
}
-/**
- * iov_tail_msghdr - Initialize a msghdr from an IOV tail structure
- * @msh: msghdr to initialize
- * @tail: iov_tail to use to set msg_iov and msg_iovlen
- * @msg_name: Pointer to set to msg_name
- * @msg_namelen: Size of @msg_name
- */
-void iov_tail_msghdr(struct msghdr *msh, struct iov_tail *tail,
- void *msg_name, socklen_t msg_namelen)
-{
- iov_tail_prune(tail);
-
- ASSERT(tail->off == 0);
-
- msh->msg_name = msg_name;
- msh->msg_namelen = msg_namelen;
- msh->msg_iov = (struct iovec *)tail->iov;
- msh->msg_iovlen = tail->cnt;
- msh->msg_control = NULL;
- msh->msg_controllen = 0;
- msh->msg_flags = 0;
-}
-
/**
* iov_tail_prune() - Remove any unneeded buffers from an IOV tail
* @tail: IO vector tail (modified)
diff --git a/iov.h b/iov.h
index 863b37857324..7d5bce2f841c 100644
--- a/iov.h
+++ b/iov.h
@@ -82,8 +82,6 @@ struct iov_tail {
1, \
(off_))
-void iov_tail_msghdr(struct msghdr *msh, struct iov_tail *tail,
- void *msg_name, socklen_t msg_namelen);
bool iov_tail_prune(struct iov_tail *tail);
size_t iov_tail_size(struct iov_tail *tail);
bool iov_tail_drop(struct iov_tail *tail, size_t len);
diff --git a/tap.c b/tap.c
index f0dc4d157cea..683da5b6d431 100644
--- a/tap.c
+++ b/tap.c
@@ -762,17 +762,14 @@ resume:
continue;
if (iph->protocol == IPPROTO_ICMP) {
- PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
-
if (c->no_icmp)
continue;
tap_packet_debug(iph, NULL, NULL, 0, NULL, 1);
- packet_add(pkt, &data);
icmp_tap_handler(c, PIF_TAP, AF_INET,
&iph->saddr, &iph->daddr,
- pkt, now);
+ &data, now);
continue;
}
@@ -962,7 +959,7 @@ resume:
tap_packet_debug(NULL, ip6h, NULL, proto, NULL, 1);
icmp_tap_handler(c, PIF_TAP, AF_INET6,
- saddr, daddr, pkt, now);
+ saddr, daddr, &data, now);
continue;
}
--
@@ -762,17 +762,14 @@ resume:
continue;
if (iph->protocol == IPPROTO_ICMP) {
- PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
-
if (c->no_icmp)
continue;
tap_packet_debug(iph, NULL, NULL, 0, NULL, 1);
- packet_add(pkt, &data);
icmp_tap_handler(c, PIF_TAP, AF_INET,
&iph->saddr, &iph->daddr,
- pkt, now);
+ &data, now);
continue;
}
@@ -962,7 +959,7 @@ resume:
tap_packet_debug(NULL, ip6h, NULL, proto, NULL, 1);
icmp_tap_handler(c, PIF_TAP, AF_INET6,
- saddr, daddr, pkt, now);
+ saddr, daddr, &data, now);
continue;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 26/30] ndp: use iov_tail rather than pool
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (24 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 25/30] icmp: " Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 27/30] packet: remove PACKET_POOL() and PACKET_POOL_P() Laurent Vivier
` (3 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
The ndp() function signature is changed to accept `struct iov_tail *data`
directly, replacing the previous `const struct pool *p` and
`const struct icmp6hdr *ih` parameters.
This change simplifies callers, like tap6_handler(), which now provide
the iov_tail representing the L4 ICMPv6 segment directly to ndp().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
ndp.c | 19 +++++++++++--------
ndp.h | 4 ++--
tap.c | 10 +++-------
3 files changed, 16 insertions(+), 17 deletions(-)
diff --git a/ndp.c b/ndp.c
index fb2bf1161b66..09d43546b91e 100644
--- a/ndp.c
+++ b/ndp.c
@@ -337,13 +337,20 @@ static void ndp_ra(const struct ctx *c, const struct in6_addr *dst)
* @c: Execution context
* @ih: ICMPv6 header
* @saddr: Source IPv6 address
- * @p: Packet pool
+ * @data: Single packet with ICMPv6 header
*
* Return: 0 if not handled here, 1 if handled, -1 on failure
*/
-int ndp(const struct ctx *c, const struct icmp6hdr *ih,
- const struct in6_addr *saddr, const struct pool *p)
+int ndp(const struct ctx *c, const struct in6_addr *saddr,
+ struct iov_tail *data)
{
+ struct icmp6hdr ih_storage;
+ const struct icmp6hdr *ih;
+
+ ih = IOV_PEEK_HEADER(data, ih_storage);
+ if (!ih)
+ return -1;
+
if (ih->icmp6_type < RS || ih->icmp6_type > NA)
return 0;
@@ -353,12 +360,8 @@ int ndp(const struct ctx *c, const struct icmp6hdr *ih,
if (ih->icmp6_type == NS) {
struct ndp_ns ns_storage;
const struct ndp_ns *ns;
- struct iov_tail data;
-
- if (!packet_get(p, 0, &data))
- return -1;
- ns = IOV_REMOVE_HEADER(&data, ns_storage);
+ ns = IOV_REMOVE_HEADER(data, ns_storage);
if (!ns)
return -1;
diff --git a/ndp.h b/ndp.h
index 41c2000356ec..b1dd5e82c085 100644
--- a/ndp.h
+++ b/ndp.h
@@ -8,8 +8,8 @@
struct icmp6hdr;
-int ndp(const struct ctx *c, const struct icmp6hdr *ih,
- const struct in6_addr *saddr, const struct pool *p);
+int ndp(const struct ctx *c, const struct in6_addr *saddr,
+ struct iov_tail *data);
void ndp_timer(const struct ctx *c, const struct timespec *now);
#endif /* NDP_H */
diff --git a/tap.c b/tap.c
index 683da5b6d431..964e036d8de5 100644
--- a/tap.c
+++ b/tap.c
@@ -940,9 +940,7 @@ resume:
}
if (proto == IPPROTO_ICMPV6) {
- struct icmp6hdr l4h_storage;
- const struct icmp6hdr *l4h;
- PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
+ struct iov_tail ndp_data;
if (c->no_icmp)
continue;
@@ -950,10 +948,8 @@ resume:
if (l4len < sizeof(struct icmp6hdr))
continue;
- packet_add(pkt, &data);
-
- l4h = IOV_PEEK_HEADER(&data, l4h_storage);
- if (ndp(c, (struct icmp6hdr *)l4h, saddr, pkt))
+ ndp_data = data;
+ if (ndp(c, saddr, &ndp_data))
continue;
tap_packet_debug(NULL, ip6h, NULL, proto, NULL, 1);
--
@@ -940,9 +940,7 @@ resume:
}
if (proto == IPPROTO_ICMPV6) {
- struct icmp6hdr l4h_storage;
- const struct icmp6hdr *l4h;
- PACKET_POOL_P(pkt, 1, in->buf, in->buf_size);
+ struct iov_tail ndp_data;
if (c->no_icmp)
continue;
@@ -950,10 +948,8 @@ resume:
if (l4len < sizeof(struct icmp6hdr))
continue;
- packet_add(pkt, &data);
-
- l4h = IOV_PEEK_HEADER(&data, l4h_storage);
- if (ndp(c, (struct icmp6hdr *)l4h, saddr, pkt))
+ ndp_data = data;
+ if (ndp(c, saddr, &ndp_data))
continue;
tap_packet_debug(NULL, ip6h, NULL, proto, NULL, 1);
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 27/30] packet: remove PACKET_POOL() and PACKET_POOL_P()
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (25 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 26/30] ndp: " Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 28/30] packet: remove unused parameter from PACKET_POOL_DECL() Laurent Vivier
` (2 subsequent siblings)
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
These macros are no longer used following the refactoring of packet
handlers to directly use iov_tail. Callers no longer require PACKET_POOL_P
for temporary pools, and PACKET_POOL can be replaced by PACKET_POOL_DECL
and separate initialization if needed.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
packet.h | 9 ---------
1 file changed, 9 deletions(-)
diff --git a/packet.h b/packet.h
index 7afe80ef3fcf..286b6b9994db 100644
--- a/packet.h
+++ b/packet.h
@@ -59,19 +59,10 @@ struct _name ## _t { \
.size = _size, \
}
-#define PACKET_POOL(name, size, buf, buf_size) \
- PACKET_POOL_DECL(name, size, buf) name = \
- PACKET_POOL_INIT_NOCAST(size, buf, buf_size)
-
#define PACKET_INIT(name, size, buf, buf_size) \
(struct name ## _t) PACKET_POOL_INIT_NOCAST(size, buf, buf_size)
#define PACKET_POOL_NOINIT(name, size, buf) \
PACKET_POOL_DECL(name, size, buf) name ## _storage; \
static struct pool *name = (struct pool *)&name ## _storage
-
-#define PACKET_POOL_P(name, size, buf, buf_size) \
- PACKET_POOL(name ## _storage, size, buf, buf_size); \
- struct pool *name = (struct pool *)&name ## _storage
-
#endif /* PACKET_H */
--
@@ -59,19 +59,10 @@ struct _name ## _t { \
.size = _size, \
}
-#define PACKET_POOL(name, size, buf, buf_size) \
- PACKET_POOL_DECL(name, size, buf) name = \
- PACKET_POOL_INIT_NOCAST(size, buf, buf_size)
-
#define PACKET_INIT(name, size, buf, buf_size) \
(struct name ## _t) PACKET_POOL_INIT_NOCAST(size, buf, buf_size)
#define PACKET_POOL_NOINIT(name, size, buf) \
PACKET_POOL_DECL(name, size, buf) name ## _storage; \
static struct pool *name = (struct pool *)&name ## _storage
-
-#define PACKET_POOL_P(name, size, buf, buf_size) \
- PACKET_POOL(name ## _storage, size, buf, buf_size); \
- struct pool *name = (struct pool *)&name ## _storage
-
#endif /* PACKET_H */
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 28/30] packet: remove unused parameter from PACKET_POOL_DECL()
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (26 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 27/30] packet: remove PACKET_POOL() and PACKET_POOL_P() Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 29/30] packet: add memory regions information into pool Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 30/30] packet: use buf to store iovec array Laurent Vivier
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
_buf is not used in the macro. Remove it.
Remove it also from PACKET_POOL_NOINIT() as it was needed
for PACKET_POOL_DECL().
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
packet.h | 6 +++---
tap.c | 6 +++---
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/packet.h b/packet.h
index 286b6b9994db..43b9022075d1 100644
--- a/packet.h
+++ b/packet.h
@@ -43,7 +43,7 @@ void pool_flush(struct pool *p);
#define packet_get(p, idx, data) \
packet_get_do(p, idx, data, __func__, __LINE__)
-#define PACKET_POOL_DECL(_name, _size, _buf) \
+#define PACKET_POOL_DECL(_name, _size) \
struct _name ## _t { \
char *buf; \
size_t buf_size; \
@@ -62,7 +62,7 @@ struct _name ## _t { \
#define PACKET_INIT(name, size, buf, buf_size) \
(struct name ## _t) PACKET_POOL_INIT_NOCAST(size, buf, buf_size)
-#define PACKET_POOL_NOINIT(name, size, buf) \
- PACKET_POOL_DECL(name, size, buf) name ## _storage; \
+#define PACKET_POOL_NOINIT(name, size) \
+ PACKET_POOL_DECL(name, size) name ## _storage; \
static struct pool *name = (struct pool *)&name ## _storage
#endif /* PACKET_H */
diff --git a/tap.c b/tap.c
index 964e036d8de5..bbcdeeaae54d 100644
--- a/tap.c
+++ b/tap.c
@@ -95,8 +95,8 @@ CHECK_FRAME_LEN(L2_MAX_LEN_VU);
ETH_HLEN + sizeof(struct ipv6hdr) + sizeof(struct udphdr))
/* IPv4 (plus ARP) and IPv6 message batches from tap/guest to IP handlers */
-static PACKET_POOL_NOINIT(pool_tap4, TAP_MSGS_IP4, pkt_buf);
-static PACKET_POOL_NOINIT(pool_tap6, TAP_MSGS_IP6, pkt_buf);
+static PACKET_POOL_NOINIT(pool_tap4, TAP_MSGS_IP4);
+static PACKET_POOL_NOINIT(pool_tap6, TAP_MSGS_IP6);
#define TAP_SEQS 128 /* Different L4 tuples in one batch */
#define FRAGMENT_MSG_RATE 10 /* # seconds between fragment warnings */
@@ -553,7 +553,7 @@ void eth_update_mac(struct ethhdr *eh,
memcpy(eh->h_source, eth_s, sizeof(eh->h_source));
}
-PACKET_POOL_DECL(pool_l4, UIO_MAXIOV, pkt_buf);
+PACKET_POOL_DECL(pool_l4, UIO_MAXIOV);
/**
* struct l4_seq4_t - Message sequence for one protocol handler call, IPv4
--
@@ -95,8 +95,8 @@ CHECK_FRAME_LEN(L2_MAX_LEN_VU);
ETH_HLEN + sizeof(struct ipv6hdr) + sizeof(struct udphdr))
/* IPv4 (plus ARP) and IPv6 message batches from tap/guest to IP handlers */
-static PACKET_POOL_NOINIT(pool_tap4, TAP_MSGS_IP4, pkt_buf);
-static PACKET_POOL_NOINIT(pool_tap6, TAP_MSGS_IP6, pkt_buf);
+static PACKET_POOL_NOINIT(pool_tap4, TAP_MSGS_IP4);
+static PACKET_POOL_NOINIT(pool_tap6, TAP_MSGS_IP6);
#define TAP_SEQS 128 /* Different L4 tuples in one batch */
#define FRAGMENT_MSG_RATE 10 /* # seconds between fragment warnings */
@@ -553,7 +553,7 @@ void eth_update_mac(struct ethhdr *eh,
memcpy(eh->h_source, eth_s, sizeof(eh->h_source));
}
-PACKET_POOL_DECL(pool_l4, UIO_MAXIOV, pkt_buf);
+PACKET_POOL_DECL(pool_l4, UIO_MAXIOV);
/**
* struct l4_seq4_t - Message sequence for one protocol handler call, IPv4
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 29/30] packet: add memory regions information into pool
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (27 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 28/30] packet: remove unused parameter from PACKET_POOL_DECL() Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 30/30] packet: use buf to store iovec array Laurent Vivier
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
This patch introduces a dedicated `struct vdev_memory *memory` field
to the `pool` structure. This field explicitly stores memory region
information, primarily for vhost-user mode, replacing the previous
hack of using `pool->buf` and `pool->buf_size == 0` for this purpose.
Pool initialization macros and `vu_packet_check_range` are updated
to use this new `memory` field, leading to cleaner code.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
packet.c | 4 ++--
packet.h | 20 ++++++++++++--------
tap.c | 23 +++++++++++++++--------
tap.h | 1 -
vhost_user.c | 28 +++++++++++-----------------
virtio.c | 4 ++--
virtio.h | 18 ++++++++++++++----
vu_common.c | 22 ++++++++++++----------
8 files changed, 68 insertions(+), 52 deletions(-)
diff --git a/packet.c b/packet.c
index cbc43c2fc22d..fd3b6db57c30 100644
--- a/packet.c
+++ b/packet.c
@@ -41,10 +41,10 @@ static int packet_check_range(const struct pool *p, const char *ptr, size_t len,
return -1;
}
- if (p->buf_size == 0) {
+ if (p->memory) {
int ret;
- ret = vu_packet_check_range((void *)p->buf, ptr, len);
+ ret = vu_packet_check_range(p->memory, ptr, len);
if (ret == -1)
debug("cannot find region, %s:%i", func, line);
diff --git a/packet.h b/packet.h
index 43b9022075d1..45843a6775ab 100644
--- a/packet.h
+++ b/packet.h
@@ -14,15 +14,15 @@
/**
* struct pool - Generic pool of packets stored in a buffer
- * @buf: Buffer storing packet descriptors,
- * a struct vu_dev_region array for passt vhost-user mode
- * @buf_size: Total size of buffer,
- * 0 for passt vhost-user mode
+ * @memory: Memory regions defined for vhost-user, NULL otherwise
+ * @buf: Buffer storing packet descriptors or iovec entries
+ * @buf_size: Total size of buffer
* @size: Number of usable descriptors for the pool
* @count: Number of used descriptors for the pool
* @pkt: Descriptors: see macros below
*/
struct pool {
+ struct vdev_memory *memory;
char *buf;
size_t buf_size;
size_t size;
@@ -30,7 +30,8 @@ struct pool {
struct iovec pkt[];
};
-int vu_packet_check_range(void *buf, const char *ptr, size_t len);
+int vu_packet_check_range(struct vdev_memory *memory,
+ const char *ptr, size_t len);
void packet_add_do(struct pool *p, struct iov_tail *data,
const char *func, int line);
bool packet_get_do(const struct pool *p, const size_t idx,
@@ -45,6 +46,7 @@ void pool_flush(struct pool *p);
#define PACKET_POOL_DECL(_name, _size) \
struct _name ## _t { \
+ struct vdev_memory *memory; \
char *buf; \
size_t buf_size; \
size_t size; \
@@ -52,15 +54,17 @@ struct _name ## _t { \
struct iovec pkt[_size]; \
}
-#define PACKET_POOL_INIT_NOCAST(_size, _buf, _buf_size) \
+#define PACKET_POOL_INIT_NOCAST(_size, _buf, _buf_size, _memory) \
{ \
.buf_size = _buf_size, \
.buf = _buf, \
.size = _size, \
+ .memory = _memory \
}
-#define PACKET_INIT(name, size, buf, buf_size) \
- (struct name ## _t) PACKET_POOL_INIT_NOCAST(size, buf, buf_size)
+#define PACKET_INIT(name, size, buf, buf_size, memory) \
+ (struct name ## _t) \
+ PACKET_POOL_INIT_NOCAST(size, buf, buf_size, memory)
#define PACKET_POOL_NOINIT(name, size) \
PACKET_POOL_DECL(name, size) name ## _storage; \
diff --git a/tap.c b/tap.c
index bbcdeeaae54d..3080ffb6806e 100644
--- a/tap.c
+++ b/tap.c
@@ -1455,17 +1455,23 @@ static void tap_sock_tun_init(struct ctx *c)
* tap_sock_update_pool() - Set the buffer base and size for the pool of packets
* @base: Buffer base
* @size Buffer size
+ * @memory: vhost-user memory regions
*/
-void tap_sock_update_pool(void *base, size_t size)
+static void tap_sock_update_pool(void *base, size_t size,
+ struct vdev_memory *memory)
{
int i;
- pool_tap4_storage = PACKET_INIT(pool_tap4, TAP_MSGS_IP4, base, size);
- pool_tap6_storage = PACKET_INIT(pool_tap6, TAP_MSGS_IP6, base, size);
+ pool_tap4_storage = PACKET_INIT(pool_tap4, TAP_MSGS_IP4, base, size,
+ memory);
+ pool_tap6_storage = PACKET_INIT(pool_tap6, TAP_MSGS_IP6, base, size,
+ memory);
for (i = 0; i < TAP_SEQS; i++) {
- tap4_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, base, size);
- tap6_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, base, size);
+ tap4_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, base, size,
+ memory);
+ tap6_l4[i].p = PACKET_INIT(pool_l4, UIO_MAXIOV, base, size,
+ memory);
}
}
@@ -1476,12 +1482,13 @@ void tap_sock_update_pool(void *base, size_t size)
*/
void tap_backend_init(struct ctx *c)
{
+ struct vdev_memory *memory = NULL;
+
if (c->mode == MODE_VU) {
- tap_sock_update_pool(NULL, 0);
vu_init(c);
- } else {
- tap_sock_update_pool(pkt_buf, sizeof(pkt_buf));
+ memory = &c->vdev->memory;
}
+ tap_sock_update_pool(pkt_buf, sizeof(pkt_buf), memory);
if (c->fd_tap != -1) { /* Passed as --fd */
ASSERT(c->one_off);
diff --git a/tap.h b/tap.h
index 954ad440a287..655b6ed9d2fd 100644
--- a/tap.h
+++ b/tap.h
@@ -115,7 +115,6 @@ 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_reset(struct ctx *c);
-void tap_sock_update_pool(void *base, size_t size);
void tap_backend_init(struct ctx *c);
void tap_flush_pools(void);
void tap_handler(struct ctx *c, const struct timespec *now);
diff --git a/vhost_user.c b/vhost_user.c
index e8377bb68608..272582ecf839 100644
--- a/vhost_user.c
+++ b/vhost_user.c
@@ -137,8 +137,8 @@ static void *qva_to_va(struct vu_dev *dev, uint64_t qemu_addr)
unsigned int i;
/* Find matching memory region. */
- for (i = 0; i < dev->nregions; i++) {
- const struct vu_dev_region *r = &dev->regions[i];
+ for (i = 0; i < dev->memory.nregions; i++) {
+ const struct vu_dev_region *r = &dev->memory.regions[i];
if ((qemu_addr >= r->qva) && (qemu_addr < (r->qva + r->size))) {
/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
@@ -428,8 +428,8 @@ static bool vu_set_mem_table_exec(struct vu_dev *vdev,
struct vhost_user_memory m = vmsg->payload.memory, *memory = &m;
unsigned int i;
- for (i = 0; i < vdev->nregions; i++) {
- const struct vu_dev_region *r = &vdev->regions[i];
+ for (i = 0; i < vdev->memory.nregions; i++) {
+ const struct vu_dev_region *r = &vdev->memory.regions[i];
if (r->mmap_addr) {
/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
@@ -437,12 +437,12 @@ static bool vu_set_mem_table_exec(struct vu_dev *vdev,
r->size + r->mmap_offset);
}
}
- vdev->nregions = memory->nregions;
+ vdev->memory.nregions = memory->nregions;
debug("vhost-user nregions: %u", memory->nregions);
- for (i = 0; i < vdev->nregions; i++) {
+ for (i = 0; i < vdev->memory.nregions; i++) {
struct vhost_user_memory_region *msg_region = &memory->regions[i];
- struct vu_dev_region *dev_region = &vdev->regions[i];
+ struct vu_dev_region *dev_region = &vdev->memory.regions[i];
void *mmap_addr;
debug("vhost-user region %d", i);
@@ -484,13 +484,7 @@ static bool vu_set_mem_table_exec(struct vu_dev *vdev,
}
}
- /* As vu_packet_check_range() has no access to the number of
- * memory regions, mark the end of the array with mmap_addr = 0
- */
- ASSERT(vdev->nregions < VHOST_USER_MAX_RAM_SLOTS - 1);
- vdev->regions[vdev->nregions].mmap_addr = 0;
-
- tap_sock_update_pool(vdev->regions, 0);
+ ASSERT(vdev->memory.nregions < VHOST_USER_MAX_RAM_SLOTS);
return false;
}
@@ -1106,8 +1100,8 @@ void vu_cleanup(struct vu_dev *vdev)
vq->vring.avail = 0;
}
- for (i = 0; i < vdev->nregions; i++) {
- const struct vu_dev_region *r = &vdev->regions[i];
+ for (i = 0; i < vdev->memory.nregions; i++) {
+ const struct vu_dev_region *r = &vdev->memory.regions[i];
if (r->mmap_addr) {
/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
@@ -1115,7 +1109,7 @@ void vu_cleanup(struct vu_dev *vdev)
r->size + r->mmap_offset);
}
}
- vdev->nregions = 0;
+ vdev->memory.nregions = 0;
vu_close_log(vdev);
diff --git a/virtio.c b/virtio.c
index 83906aab8d02..4c5326e4847b 100644
--- a/virtio.c
+++ b/virtio.c
@@ -102,8 +102,8 @@ static void *vu_gpa_to_va(const struct vu_dev *dev, uint64_t *plen,
return NULL;
/* Find matching memory region. */
- for (i = 0; i < dev->nregions; i++) {
- const struct vu_dev_region *r = &dev->regions[i];
+ for (i = 0; i < dev->memory.nregions; i++) {
+ const struct vu_dev_region *r = &dev->memory.regions[i];
if ((guest_addr >= r->gpa) &&
(guest_addr < (r->gpa + r->size))) {
diff --git a/virtio.h b/virtio.h
index d8beb884c61d..8291ff80b596 100644
--- a/virtio.h
+++ b/virtio.h
@@ -96,11 +96,22 @@ struct vu_dev_region {
*/
#define VHOST_USER_MAX_RAM_SLOTS 32
+/**
+ * struct vdev_memory - Describes the shared memory regions for a vhost-user
+ * device
+ * @nregions: Number of shared memory regions
+ * @regions: Guest shared memory regions
+ */
+struct vdev_memory {
+ uint32_t nregions;
+ struct vu_dev_region regions[VHOST_USER_MAX_RAM_SLOTS];
+};
+
/**
* struct vu_dev - vhost-user device information
* @context: Execution context
- * @nregions: Number of shared memory regions
- * @regions: Guest shared memory regions
+ * @memory: Shared memory regions
+ * @vq: Virtqueues of the device
* @features: Vhost-user features
* @protocol_features: Vhost-user protocol features
* @log_call_fd: Eventfd to report logging update
@@ -109,8 +120,7 @@ struct vu_dev_region {
*/
struct vu_dev {
struct ctx *context;
- uint32_t nregions;
- struct vu_dev_region regions[VHOST_USER_MAX_RAM_SLOTS];
+ struct vdev_memory memory;
struct vu_virtq vq[VHOST_USER_MAX_QUEUES];
uint64_t features;
uint64_t protocol_features;
diff --git a/vu_common.c b/vu_common.c
index b77b21420c57..b716070ea3c3 100644
--- a/vu_common.c
+++ b/vu_common.c
@@ -25,26 +25,28 @@
/**
* vu_packet_check_range() - Check if a given memory zone is contained in
* a mapped guest memory region
- * @buf: Array of the available memory regions
+ * @memory: Array of the available memory regions
* @ptr: Start of desired data range
- * @size: Length of desired data range
+ * @len: Length of desired data range
*
* Return: 0 if the zone is in a mapped memory region, -1 otherwise
*/
-int vu_packet_check_range(void *buf, const char *ptr, size_t len)
+int vu_packet_check_range(struct vdev_memory *memory,
+ const char *ptr, size_t len)
{
- struct vu_dev_region *dev_region;
+ struct vu_dev_region *dev_region = memory->regions;
+ unsigned int i;
- for (dev_region = buf; dev_region->mmap_addr; dev_region++) {
- uintptr_t base_addr = dev_region->mmap_addr +
- dev_region->mmap_offset;
+ for (i = 0; i < memory->nregions; i++) {
+ uintptr_t base_addr = dev_region[i].mmap_addr +
+ dev_region[i].mmap_offset;
/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
const char *base = (const char *)base_addr;
- ASSERT(base_addr >= dev_region->mmap_addr);
+ ASSERT(base_addr >= dev_region[i].mmap_addr);
- if (len <= dev_region->size && base <= ptr &&
- (size_t)(ptr - base) <= dev_region->size - len)
+ if (len <= dev_region[i].size && base <= ptr &&
+ (size_t)(ptr - base) <= dev_region[i].size - len)
return 0;
}
--
@@ -25,26 +25,28 @@
/**
* vu_packet_check_range() - Check if a given memory zone is contained in
* a mapped guest memory region
- * @buf: Array of the available memory regions
+ * @memory: Array of the available memory regions
* @ptr: Start of desired data range
- * @size: Length of desired data range
+ * @len: Length of desired data range
*
* Return: 0 if the zone is in a mapped memory region, -1 otherwise
*/
-int vu_packet_check_range(void *buf, const char *ptr, size_t len)
+int vu_packet_check_range(struct vdev_memory *memory,
+ const char *ptr, size_t len)
{
- struct vu_dev_region *dev_region;
+ struct vu_dev_region *dev_region = memory->regions;
+ unsigned int i;
- for (dev_region = buf; dev_region->mmap_addr; dev_region++) {
- uintptr_t base_addr = dev_region->mmap_addr +
- dev_region->mmap_offset;
+ for (i = 0; i < memory->nregions; i++) {
+ uintptr_t base_addr = dev_region[i].mmap_addr +
+ dev_region[i].mmap_offset;
/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
const char *base = (const char *)base_addr;
- ASSERT(base_addr >= dev_region->mmap_addr);
+ ASSERT(base_addr >= dev_region[i].mmap_addr);
- if (len <= dev_region->size && base <= ptr &&
- (size_t)(ptr - base) <= dev_region->size - len)
+ if (len <= dev_region[i].size && base <= ptr &&
+ (size_t)(ptr - base) <= dev_region[i].size - len)
return 0;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
* [PATCH v6 30/30] packet: use buf to store iovec array
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
` (28 preceding siblings ...)
2025-06-04 13:08 ` [PATCH v6 29/30] packet: add memory regions information into pool Laurent Vivier
@ 2025-06-04 13:08 ` Laurent Vivier
29 siblings, 0 replies; 31+ messages in thread
From: Laurent Vivier @ 2025-06-04 13:08 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier
In vhost-user mode, where `pool->buf` doesn't store packet data directly,
this patch repurposes it to hold the `struct iovec` array describing
a scattered packet. This enables pools to manage true scatter-gather
descriptors, with iovecs pointing to guest memory.
`p->pkt[idx].iov_base` is now an index into this `pool->buf` iovec array,
and `p->pkt[idx].iov_len` is the count of iovecs. `packet_add_do` uses
`iov_tail_slice` to store these iovec descriptors from an input iov_tail
into `pool->buf`. `packet_data_do` reconstructs the iov_tail for a
packet by pointing to this stored array of iovecs.
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
packet.c | 164 +++++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 148 insertions(+), 16 deletions(-)
diff --git a/packet.c b/packet.c
index fd3b6db57c30..8dbe00af12c6 100644
--- a/packet.c
+++ b/packet.c
@@ -84,6 +84,122 @@ bool pool_full(const struct pool *p)
return p->count >= p->size;
}
+/**
+ * packet_iov_max_cnt() - Return the maximum number of iovec entries we can
+ * store
+ * @p: Pointer to packet pool
+ *
+ * Return: the maximum number of iovec entries we can store in the memory of
+ * the pool buffer
+ */
+static size_t packet_iov_max_cnt(const struct pool *p)
+{
+ return p->buf_size / sizeof(struct iovec);
+}
+
+/**
+ * packet_iov_idx() - For a given packet index, return the iovec index and
+ * the number of iovec entry of the packet
+ * @p: Pointer to packet pool
+ * @idx: Index of packet descriptor in pool
+ * @iov_cnt: Pointer to store the number of the iovec entry of the packet
+ * @func: For tracing: name of calling function
+ * @line: For tracing: caller line of function call
+ *
+ * Return: the iovec index for the given packet index, @iov_cnt is set
+ * to the number of the iovec entry of the packet
+ */
+static int packet_iov_idx(const struct pool *p, size_t idx, size_t *iov_cnt,
+ const char *func, int line)
+{
+ size_t iov_idx, max = packet_iov_max_cnt(p);
+
+ iov_idx = (size_t)p->pkt[idx].iov_base;
+ *iov_cnt = p->pkt[idx].iov_len;
+
+ ASSERT_WITH_MSG(iov_idx + *iov_cnt <= max,
+ "Corrupt iov entry: (%zu, %zu), max: %zu, %s:%i",
+ iov_idx, *iov_cnt, max, func, line);
+
+ return iov_idx;
+}
+
+/**
+ * packet_iov_next_idx() - Give the the next available iovec index
+ * @p: Pointer to packet pool
+ * @idx: Index of packet descriptor in pool
+ * @func: For tracing: name of calling function
+ * @line: For tracing: caller line of function call
+ *
+ * Return: the next available iovec index
+ */
+static size_t packet_iov_next_idx(const struct pool *p, size_t idx,
+ const char *func, int line)
+{
+ size_t iov_idx, iov_cnt;
+
+ if (idx == 0)
+ return 0;
+
+ iov_idx = packet_iov_idx(p, idx - 1, &iov_cnt, func, line);
+
+ return iov_idx + iov_cnt;
+}
+
+/**
+ * packet_iov_data() - For a given packet index, provide the iovec array
+ * @p: Pointer to packet pool
+ * @idx: Index of packet descriptor in pool
+ * @data: iov_tail to store iovec array and count
+ * (offset is always set to 0)
+ * @func: For tracing: name of calling function
+ * @line: For tracing: caller line of function call
+ */
+static void packet_iov_data(const struct pool *p, size_t idx,
+ struct iov_tail *data,
+ const char *func, int line)
+{
+ struct iovec *iov = (struct iovec *)p->buf;
+ size_t iov_idx, iov_cnt;
+
+ iov_idx = packet_iov_idx(p, idx, &iov_cnt, func, line);
+
+ data->iov = &iov[iov_idx];
+ data->cnt = iov_cnt;
+ data->off = 0;
+}
+
+/**
+ * packet_iov_check_range() - Check if iovec array is valid for a pool
+ * @p: Pointer to packet pool
+ * @data: iov_tail that stores the iovec array to check
+ * @func: For tracing: name of calling function
+ * @line: For tracing: caller line of function call
+ *
+ * Return: 0 if the range is valid, -1 otherwise
+ */
+static int packet_iov_check_range(const struct pool *p,
+ const struct iov_tail *data,
+ const char *func, int line)
+{
+ size_t offset, i;
+
+ offset = data->off;
+ for (i = 0; i < data->cnt; i++) {
+ int ret;
+
+ ret = packet_check_range(p,
+ (char *)data->iov[i].iov_base + offset,
+ data->iov[i].iov_len - offset,
+ func, line);
+ if (ret)
+ return ret;
+ offset = 0;
+ }
+
+ return 0;
+}
+
/**
* packet_add_do() - Add data as packet descriptor to given pool
* @p: Existing pool
@@ -107,14 +223,32 @@ void packet_add_do(struct pool *p, struct iov_tail *data,
if (!iov_tail_prune(data))
return;
- ASSERT(data->cnt == 1); /* we don't support iovec */
+ if (packet_iov_check_range(p, data, func, line))
+ return;
+
+ if (p->memory) {
+ size_t iov_max_cnt = packet_iov_max_cnt(p);
+ struct iovec *iov = (struct iovec *)p->buf;
+ size_t iov_idx;
+ int iov_cnt;
- len = data->iov[0].iov_len - data->off;
- start = (char *)data->iov[0].iov_base + data->off;
+ iov_idx = packet_iov_next_idx(p, idx, func, line);
- if (packet_check_range(p, start, len, func, line))
- return;
+ iov_cnt = iov_tail_clone(&iov[iov_idx], iov_max_cnt - iov_idx,
+ data);
+ if (iov_cnt < 0) {
+ debug("add iov (%zu,%zu) to buf with size %zu, %s:%i",
+ iov_idx, data->cnt, iov_max_cnt, func, line);
+ return;
+ }
+ len = iov_cnt;
+ /* NOLINTNEXTLINE(performance-no-int-to-ptr) */
+ start = (char *)iov_idx;
+ } else {
+ len = data->iov[0].iov_len - data->off;
+ start = (char *)data->iov[0].iov_base + data->off;
+ }
p->pkt[idx].iov_base = (void *)start;
p->pkt[idx].iov_len = len;
@@ -136,8 +270,6 @@ bool packet_get_do(const struct pool *p, size_t idx,
struct iov_tail *data,
const char *func, int line)
{
- size_t i;
-
ASSERT_WITH_MSG(p->count <= p->size,
"Corrupted pool count: %zu, size: %zu, %s:%i",
p->count, p->size, func, line);
@@ -148,17 +280,17 @@ bool packet_get_do(const struct pool *p, size_t idx,
return false;
}
- data->cnt = 1;
- data->off = 0;
- data->iov = &p->pkt[idx];
-
- for (i = 0; i < data->cnt; i++) {
- ASSERT_WITH_MSG(!packet_check_range(p, data->iov[i].iov_base,
- data->iov[i].iov_len,
- func, line),
- "Corrupt packet pool, %s:%i", func, line);
+ if (p->memory) {
+ packet_iov_data(p, idx, data, func, line);
+ } else {
+ data->cnt = 1;
+ data->off = 0;
+ data->iov = &p->pkt[idx];
}
+ ASSERT_WITH_MSG(!packet_iov_check_range(p, data, func, line),
+ "Corrupt packet pool, %s:%i", func, line);
+
return true;
}
--
@@ -84,6 +84,122 @@ bool pool_full(const struct pool *p)
return p->count >= p->size;
}
+/**
+ * packet_iov_max_cnt() - Return the maximum number of iovec entries we can
+ * store
+ * @p: Pointer to packet pool
+ *
+ * Return: the maximum number of iovec entries we can store in the memory of
+ * the pool buffer
+ */
+static size_t packet_iov_max_cnt(const struct pool *p)
+{
+ return p->buf_size / sizeof(struct iovec);
+}
+
+/**
+ * packet_iov_idx() - For a given packet index, return the iovec index and
+ * the number of iovec entry of the packet
+ * @p: Pointer to packet pool
+ * @idx: Index of packet descriptor in pool
+ * @iov_cnt: Pointer to store the number of the iovec entry of the packet
+ * @func: For tracing: name of calling function
+ * @line: For tracing: caller line of function call
+ *
+ * Return: the iovec index for the given packet index, @iov_cnt is set
+ * to the number of the iovec entry of the packet
+ */
+static int packet_iov_idx(const struct pool *p, size_t idx, size_t *iov_cnt,
+ const char *func, int line)
+{
+ size_t iov_idx, max = packet_iov_max_cnt(p);
+
+ iov_idx = (size_t)p->pkt[idx].iov_base;
+ *iov_cnt = p->pkt[idx].iov_len;
+
+ ASSERT_WITH_MSG(iov_idx + *iov_cnt <= max,
+ "Corrupt iov entry: (%zu, %zu), max: %zu, %s:%i",
+ iov_idx, *iov_cnt, max, func, line);
+
+ return iov_idx;
+}
+
+/**
+ * packet_iov_next_idx() - Give the the next available iovec index
+ * @p: Pointer to packet pool
+ * @idx: Index of packet descriptor in pool
+ * @func: For tracing: name of calling function
+ * @line: For tracing: caller line of function call
+ *
+ * Return: the next available iovec index
+ */
+static size_t packet_iov_next_idx(const struct pool *p, size_t idx,
+ const char *func, int line)
+{
+ size_t iov_idx, iov_cnt;
+
+ if (idx == 0)
+ return 0;
+
+ iov_idx = packet_iov_idx(p, idx - 1, &iov_cnt, func, line);
+
+ return iov_idx + iov_cnt;
+}
+
+/**
+ * packet_iov_data() - For a given packet index, provide the iovec array
+ * @p: Pointer to packet pool
+ * @idx: Index of packet descriptor in pool
+ * @data: iov_tail to store iovec array and count
+ * (offset is always set to 0)
+ * @func: For tracing: name of calling function
+ * @line: For tracing: caller line of function call
+ */
+static void packet_iov_data(const struct pool *p, size_t idx,
+ struct iov_tail *data,
+ const char *func, int line)
+{
+ struct iovec *iov = (struct iovec *)p->buf;
+ size_t iov_idx, iov_cnt;
+
+ iov_idx = packet_iov_idx(p, idx, &iov_cnt, func, line);
+
+ data->iov = &iov[iov_idx];
+ data->cnt = iov_cnt;
+ data->off = 0;
+}
+
+/**
+ * packet_iov_check_range() - Check if iovec array is valid for a pool
+ * @p: Pointer to packet pool
+ * @data: iov_tail that stores the iovec array to check
+ * @func: For tracing: name of calling function
+ * @line: For tracing: caller line of function call
+ *
+ * Return: 0 if the range is valid, -1 otherwise
+ */
+static int packet_iov_check_range(const struct pool *p,
+ const struct iov_tail *data,
+ const char *func, int line)
+{
+ size_t offset, i;
+
+ offset = data->off;
+ for (i = 0; i < data->cnt; i++) {
+ int ret;
+
+ ret = packet_check_range(p,
+ (char *)data->iov[i].iov_base + offset,
+ data->iov[i].iov_len - offset,
+ func, line);
+ if (ret)
+ return ret;
+ offset = 0;
+ }
+
+ return 0;
+}
+
/**
* packet_add_do() - Add data as packet descriptor to given pool
* @p: Existing pool
@@ -107,14 +223,32 @@ void packet_add_do(struct pool *p, struct iov_tail *data,
if (!iov_tail_prune(data))
return;
- ASSERT(data->cnt == 1); /* we don't support iovec */
+ if (packet_iov_check_range(p, data, func, line))
+ return;
+
+ if (p->memory) {
+ size_t iov_max_cnt = packet_iov_max_cnt(p);
+ struct iovec *iov = (struct iovec *)p->buf;
+ size_t iov_idx;
+ int iov_cnt;
- len = data->iov[0].iov_len - data->off;
- start = (char *)data->iov[0].iov_base + data->off;
+ iov_idx = packet_iov_next_idx(p, idx, func, line);
- if (packet_check_range(p, start, len, func, line))
- return;
+ iov_cnt = iov_tail_clone(&iov[iov_idx], iov_max_cnt - iov_idx,
+ data);
+ if (iov_cnt < 0) {
+ debug("add iov (%zu,%zu) to buf with size %zu, %s:%i",
+ iov_idx, data->cnt, iov_max_cnt, func, line);
+ return;
+ }
+ len = iov_cnt;
+ /* NOLINTNEXTLINE(performance-no-int-to-ptr) */
+ start = (char *)iov_idx;
+ } else {
+ len = data->iov[0].iov_len - data->off;
+ start = (char *)data->iov[0].iov_base + data->off;
+ }
p->pkt[idx].iov_base = (void *)start;
p->pkt[idx].iov_len = len;
@@ -136,8 +270,6 @@ bool packet_get_do(const struct pool *p, size_t idx,
struct iov_tail *data,
const char *func, int line)
{
- size_t i;
-
ASSERT_WITH_MSG(p->count <= p->size,
"Corrupted pool count: %zu, size: %zu, %s:%i",
p->count, p->size, func, line);
@@ -148,17 +280,17 @@ bool packet_get_do(const struct pool *p, size_t idx,
return false;
}
- data->cnt = 1;
- data->off = 0;
- data->iov = &p->pkt[idx];
-
- for (i = 0; i < data->cnt; i++) {
- ASSERT_WITH_MSG(!packet_check_range(p, data->iov[i].iov_base,
- data->iov[i].iov_len,
- func, line),
- "Corrupt packet pool, %s:%i", func, line);
+ if (p->memory) {
+ packet_iov_data(p, idx, data, func, line);
+ } else {
+ data->cnt = 1;
+ data->off = 0;
+ data->iov = &p->pkt[idx];
}
+ ASSERT_WITH_MSG(!packet_iov_check_range(p, data, func, line),
+ "Corrupt packet pool, %s:%i", func, line);
+
return true;
}
--
2.49.0
^ permalink raw reply related [flat|nested] 31+ messages in thread
end of thread, other threads:[~2025-06-04 13:09 UTC | newest]
Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-04 13:08 [PATCH v6 00/30] Introduce discontiguous frames management Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 01/30] arp: Don't mix incoming and outgoing buffers Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 02/30] iov: Introduce iov_tail_clone() and iov_tail_drop() Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 03/30] iov: Update IOV_REMOVE_HEADER() and IOV_PEEK_HEADER() Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 04/30] tap: Use iov_tail with tap_add_packet() Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 05/30] packet: Use iov_tail with packet_add() Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 06/30] packet: Add packet_data() Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 07/30] arp: Convert to iov_tail Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 08/30] ndp: " Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 09/30] icmp: " Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 10/30] udp: " Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 11/30] tcp: Convert tcp_tap_handler() to use iov_tail Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 12/30] tcp: Convert tcp_data_from_tap() " Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 13/30] dhcpv6: move offset initialization out of dhcpv6_opt() Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 14/30] dhcpv6: Extract sending of NotOnLink status Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 15/30] dhcpv6: Convert to iov_tail Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 16/30] dhcpv6: Use iov_tail in dhcpv6_opt() Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 17/30] dhcp: Convert to iov_tail Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 18/30] ip: Use iov_tail in ipv6_l4hdr() Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 19/30] tap: Convert tap4_handler() to iov_tail Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 20/30] tap: Convert tap6_handler() " Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 21/30] packet: rename packet_data() to packet_get() Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 22/30] arp: use iov_tail rather than pool Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 23/30] dhcp: " Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 24/30] dhcpv6: " Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 25/30] icmp: " Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 26/30] ndp: " Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 27/30] packet: remove PACKET_POOL() and PACKET_POOL_P() Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 28/30] packet: remove unused parameter from PACKET_POOL_DECL() Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 29/30] packet: add memory regions information into pool Laurent Vivier
2025-06-04 13:08 ` [PATCH v6 30/30] packet: use buf to store iovec array Laurent Vivier
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).