* [PATCH] packet: Fix pool traversal for multi-iovec packets
@ 2026-11-26 16:56 Laurent Vivier
2026-11-27 8:30 ` Stefano Brivio
0 siblings, 1 reply; 4+ messages in thread
From: Laurent Vivier @ 2026-11-26 16:56 UTC (permalink / raw)
To: passt-dev; +Cc: luffyluo, Laurent Vivier
From: luffyluo <luffyluo@tencent.com>
With vhost-user a guest frame can span several descriptors. The pool
stores such a packet as a marker slot (iov_base == NULL, iov_len = N)
followed by N data slots, and packet_get() uses the marker to return
all N iovecs at once.
The traversal loops in tap4_handler(), tap6_handler(),
tcp_data_from_tap() and udp_tap_handler() don't know about markers and
advance one slot at a time: after handling the packet from its marker
they land on its first data slot, where iov_base != NULL, so
packet_get() hands back that slot alone, as if it were a whole
single-iovec packet. Parsing that fragment as a complete frame is
what breaks: in tcp_data_from_tap() a fragment shorter than a TCP
header makes IOV_PEEK_HEADER() fail ("TCP header not contiguous"), and
a longer one gives a th->doff read from payload bytes, so
off = th->doff * 4 exceeds the fragment length. In udp_tap_handler()
the fragments go out as extra datagrams.
Fix it with packet_next(), returning the index of the packet following
the one at a given index, and use it to advance those loops, so they
skip the data slots of the packet they just handled. For single-iovec
packets it returns idx + 1, so tap and pasta, which always build
single-iovec tails with IOV_TAIL_FROM_BUF(), are unaffected.
Fixes: ca38be0141d1 ("packet: Add support for multi-vector packets")
Signed-off-by: luffyluo <luffyluo@tencent.com>
[lvivier: Fix multi-iovec return values and TCP reordering scan restart]
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
packet.c | 16 ++++++++++++++++
packet.h | 1 +
tap.c | 4 ++--
tcp.c | 30 ++++++++++++++++--------------
udp.c | 23 +++++++++++++++--------
5 files changed, 50 insertions(+), 24 deletions(-)
diff --git a/packet.c b/packet.c
index 7a347be6fd14..f202245c12c6 100644
--- a/packet.c
+++ b/packet.c
@@ -207,3 +207,19 @@ void pool_flush(struct pool *p)
{
p->count = 0;
}
+
+/**
+ * packet_next() - Index of the packet following the one at @idx
+ * @p: Packet pool
+ * @idx: Slot index of the current packet (marker or single iovec)
+ *
+ * A multi-iovec packet is stored as a marker slot (iov_base == NULL) whose
+ * iov_len holds the number of iovec slots that follow it. Return the slot
+ * index of the next packet.
+ */
+size_t packet_next(const struct pool *p, size_t idx)
+{
+ if (p->pkt[idx].iov_base == NULL)
+ return idx + p->pkt[idx].iov_len + 1;
+ return idx + 1;
+}
diff --git a/packet.h b/packet.h
index 7b7fa7189ecd..03821f61cee3 100644
--- a/packet.h
+++ b/packet.h
@@ -41,6 +41,7 @@ bool packet_get_do(const struct pool *p, const size_t idx,
struct iov_tail *data, const char *func, int line);
bool pool_can_fit(const struct pool *p, struct iov_tail *data);
void pool_flush(struct pool *p);
+size_t packet_next(const struct pool *p, size_t idx);
#define packet_add(p, data) \
packet_add_do(p, data, __func__, __LINE__)
diff --git a/tap.c b/tap.c
index 100afc83d289..43ce7d4f3d00 100644
--- a/tap.c
+++ b/tap.c
@@ -700,7 +700,7 @@ static int tap4_handler(struct ctx *c, const struct pool *in,
i = 0;
resume:
- for (seq_count = 0, seq = NULL; i < in->count; i++) {
+ for (seq_count = 0, seq = NULL; i < in->count; i = packet_next(in, i)) {
struct iovec trim_iov[UIO_MAXIOV];
size_t l3len, hlen, l4len, check;
struct ethhdr eh_storage;
@@ -948,7 +948,7 @@ static int tap6_handler(struct ctx *c, const struct pool *in,
i = 0;
resume:
- for (seq_count = 0, seq = NULL; i < in->count; i++) {
+ for (seq_count = 0, seq = NULL; i < in->count; i = packet_next(in, i)) {
size_t l4len, plen, check;
struct in6_addr *saddr, *daddr;
struct ipv6hdr ip6h_storage;
diff --git a/tcp.c b/tcp.c
index 9390e5bf909e..409f4ce18ea9 100644
--- a/tcp.c
+++ b/tcp.c
@@ -1954,7 +1954,8 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
assert(conn->events & ESTABLISHED);
- for (i = idx, iov_i = 0; i < (int)p->count; i++) {
+ for (i = idx, iov_i = 0; i < (int)p->count;
+ i = (i == -1) ? 0 : packet_next(p, i)) {
uint32_t seq, seq_offset, ack_seq;
struct tcphdr th_storage;
const struct tcphdr *th;
@@ -1977,7 +1978,7 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
if (th->rst) {
tcp_sock_rst(c, conn, now);
- return 1;
+ return packet_next(p, i) - i;
}
len -= off;
@@ -1998,10 +1999,10 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
&((int){ 1 }), sizeof(int)))
flow_trace(conn, "failed to set SO_KEEPALIVE");
- if (p->count == 1) {
+ if (packet_next(p, i) == p->count) {
tcp_tap_window_update(c, conn,
ntohs(th->window), now);
- return 1;
+ return packet_next(p, i) - i;
}
continue;
@@ -2329,13 +2330,13 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
(void)pif;
if (!packet_get(p, idx, &data))
- return 1;
+ return packet_next(p, idx) - idx;
l4len = iov_tail_size(&data);
th = IOV_REMOVE_HEADER(&data, th_storage);
if (!th)
- return 1;
+ return packet_next(p, idx) - idx;
optlen = th->doff * 4UL - sizeof(*th);
/* Static checkers might fail to see this: */
@@ -2353,7 +2354,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
opts, optlen, now);
else
tcp_rst_no_conn(c, af, saddr, daddr, flow_lbl, th, l4len);
- return 1;
+ return packet_next(p, idx) - idx;
}
assert(flow->f.type == FLOW_TCP);
@@ -2364,7 +2365,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
if (th->rst) {
tcp_sock_rst(c, conn, now);
- return 1;
+ return packet_next(p, idx) - idx;
}
conn->inactive = false;
@@ -2378,7 +2379,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
if (th->syn && th->ack && !th->fin) {
tcp_conn_from_sock_finish(c, conn, th, opts, optlen,
now);
- return 1;
+ return packet_next(p, idx) - idx;
}
goto reset;
@@ -2386,8 +2387,9 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
/* Establishing connection from tap */
if (conn->events & TAP_SYN_RCVD) {
+ /* SYN retry: ignore and keep waiting */
if (th->syn && !th->ack && !th->fin)
- return 1; /* SYN retry: ignore and keep waiting */
+ return packet_next(p, idx) - idx;
if (!(conn->events & TAP_SYN_ACK_SENT))
goto reset;
@@ -2410,7 +2412,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
conn_event(c, conn, SOCK_FIN_SENT, now);
- return 1;
+ return packet_next(p, idx) - idx;
}
if (!th->ack)
@@ -2419,8 +2421,8 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
if (tcp_tap_window_update(c, conn, ntohs(th->window), now))
tcp_data_from_sock(c, conn, now);
- if (p->count - idx == 1)
- return 1;
+ if (packet_next(p, idx) == p->count)
+ return packet_next(p, idx) - idx;
}
/* Established connections not accepting data from tap */
@@ -2473,7 +2475,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
conn_event(c, conn, CLOSED, now);
}
- return 1;
+ return packet_next(p, idx) - idx;
}
/* Established connections accepting data from tap */
diff --git a/udp.c b/udp.c
index 505e55407fb4..cda0272f427b 100644
--- a/udp.c
+++ b/udp.c
@@ -1023,12 +1023,14 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
{
const struct flowside *toside;
struct mmsghdr mm[UIO_MAXIOV];
+ size_t packet_idx[UIO_MAXIOV + 1];
union sockaddr_inany to_sa;
struct iovec m[UIO_MAXIOV];
struct udphdr uh_storage;
const struct udphdr *uh;
struct udp_flow *uflow;
int i, j, s, count = 0;
+ size_t slot;
struct iov_tail data;
flow_sidx_t tosidx;
in_port_t src, dst;
@@ -1037,11 +1039,11 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
assert(!c->no_udp);
if (!packet_get(p, idx, &data))
- return 1;
+ return packet_next(p, idx) - idx;
uh = IOV_PEEK_HEADER(&data, uh_storage);
if (!uh)
- return 1;
+ return packet_next(p, idx) - idx;
/* The caller already checks that all the messages have the same source
* and destination, so we can just take those from the first message.
@@ -1057,7 +1059,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
pif_name(pif),
inet_ntop(af, saddr, sstr, sizeof(sstr)), src,
inet_ntop(af, daddr, dstr, sizeof(dstr)), dst);
- return 1;
+ return packet_next(p, idx) - idx;
}
topif = pif_at_sidx(tosidx);
@@ -1068,7 +1070,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
flow_err_ratelimit(uflow, now,
"No support for forwarding UDP from %s to %s",
pif_name(frompif), pif_name(topif));
- return 1;
+ return packet_next(p, idx) - idx;
}
toside = flowside_at_sidx(tosidx);
@@ -1077,10 +1079,14 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
pif_sockaddr(c, &to_sa, topif, &toside->eaddr, toside->eport);
- for (i = 0, j = 0; i < (int)p->count - idx && j < UIO_MAXIOV; i++) {
+ for (i = 0, j = 0, slot = idx;
+ slot < p->count && j < UIO_MAXIOV && i < UIO_MAXIOV;
+ slot = packet_next(p, slot), i++) {
const struct udphdr *uh_send;
- if (!packet_get(p, idx + i, &data))
+ packet_idx[count] = slot;
+
+ if (!packet_get(p, slot, &data))
return p->count - idx;
uh_send = IOV_REMOVE_HEADER(&data, uh_storage);
@@ -1129,12 +1135,13 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
count++;
}
+ packet_idx[count] = slot;
count = sendmmsg(s, mm, count, MSG_NOSIGNAL);
if (count < 0)
- return 1;
+ return packet_next(p, idx) - idx;
- return count;
+ return packet_idx[count] - idx;
}
/**
--
2.56.0
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] packet: Fix pool traversal for multi-iovec packets
2026-11-26 16:56 [PATCH] packet: Fix pool traversal for multi-iovec packets Laurent Vivier
@ 2026-11-27 8:30 ` Stefano Brivio
0 siblings, 0 replies; 4+ messages in thread
From: Stefano Brivio @ 2026-11-27 8:30 UTC (permalink / raw)
To: Laurent Vivier
Cc: passt-dev, luffyluo, luochunsheng, Agustín Dall'Alba
On Thu, 26 Nov 2026 17:56:10 +0100
Laurent Vivier <lvivier@redhat.com> wrote:
> From: luffyluo <luffyluo@tencent.com>
>
> With vhost-user a guest frame can span several descriptors. The pool
> stores such a packet as a marker slot (iov_base == NULL, iov_len = N)
> followed by N data slots, and packet_get() uses the marker to return
> all N iovecs at once.
>
> The traversal loops in tap4_handler(), tap6_handler(),
> tcp_data_from_tap() and udp_tap_handler() don't know about markers and
> advance one slot at a time: after handling the packet from its marker
> they land on its first data slot, where iov_base != NULL, so
> packet_get() hands back that slot alone, as if it were a whole
> single-iovec packet. Parsing that fragment as a complete frame is
> what breaks: in tcp_data_from_tap() a fragment shorter than a TCP
> header makes IOV_PEEK_HEADER() fail ("TCP header not contiguous"), and
> a longer one gives a th->doff read from payload bytes, so
> off = th->doff * 4 exceeds the fragment length. In udp_tap_handler()
> the fragments go out as extra datagrams.
>
> Fix it with packet_next(), returning the index of the packet following
> the one at a given index, and use it to advance those loops, so they
> skip the data slots of the packet they just handled. For single-iovec
> packets it returns idx + 1, so tap and pasta, which always build
> single-iovec tails with IOV_TAIL_FROM_BUF(), are unaffected.
>
> Fixes: ca38be0141d1 ("packet: Add support for multi-vector packets")
> Signed-off-by: luffyluo <luffyluo@tencent.com>
> [lvivier: Fix multi-iovec return values and TCP reordering scan restart]
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
I took the liberty to make a couple of style fixes, described below,
and applied right away because it looks correct to me and it fixes a
critical issue, but I think it's a bit scary like this, some
suggestions also below.
> ---
> packet.c | 16 ++++++++++++++++
> packet.h | 1 +
> tap.c | 4 ++--
> tcp.c | 30 ++++++++++++++++--------------
> udp.c | 23 +++++++++++++++--------
> 5 files changed, 50 insertions(+), 24 deletions(-)
>
> diff --git a/packet.c b/packet.c
> index 7a347be6fd14..f202245c12c6 100644
> --- a/packet.c
> +++ b/packet.c
> @@ -207,3 +207,19 @@ void pool_flush(struct pool *p)
> {
> p->count = 0;
> }
> +
> +/**
> + * packet_next() - Index of the packet following the one at @idx
> + * @p: Packet pool
> + * @idx: Slot index of the current packet (marker or single iovec)
> + *
> + * A multi-iovec packet is stored as a marker slot (iov_base == NULL) whose
> + * iov_len holds the number of iovec slots that follow it. Return the slot
> + * index of the next packet.
Added the usual "Return: ..." specification.
> + */
> +size_t packet_next(const struct pool *p, size_t idx)
> +{
> + if (p->pkt[idx].iov_base == NULL)
> + return idx + p->pkt[idx].iov_len + 1;
Added extra newline here.
> + return idx + 1;
> +}
> diff --git a/packet.h b/packet.h
> index 7b7fa7189ecd..03821f61cee3 100644
> --- a/packet.h
> +++ b/packet.h
> @@ -41,6 +41,7 @@ bool packet_get_do(const struct pool *p, const size_t idx,
> struct iov_tail *data, const char *func, int line);
> bool pool_can_fit(const struct pool *p, struct iov_tail *data);
> void pool_flush(struct pool *p);
> +size_t packet_next(const struct pool *p, size_t idx);
>
> #define packet_add(p, data) \
> packet_add_do(p, data, __func__, __LINE__)
> diff --git a/tap.c b/tap.c
> index 100afc83d289..43ce7d4f3d00 100644
> --- a/tap.c
> +++ b/tap.c
> @@ -700,7 +700,7 @@ static int tap4_handler(struct ctx *c, const struct pool *in,
>
> i = 0;
> resume:
> - for (seq_count = 0, seq = NULL; i < in->count; i++) {
> + for (seq_count = 0, seq = NULL; i < in->count; i = packet_next(in, i)) {
I wonder if we want to define an iterator for this type of loops. I
think it's just too easy to add another loop like this and accidentally
use i++ as increment. Maybe something like (not tested):
#define foreach_pkt_from(p, i) for (int i_ = (i); i_ < (p); i_ = packet_next((p), _i))
#define foreach_pkt(p) foreach_pkt_from((p), 0)
?
I haven't checked how / if it can be used or adapted for the other
usages below.
> struct iovec trim_iov[UIO_MAXIOV];
> size_t l3len, hlen, l4len, check;
> struct ethhdr eh_storage;
> @@ -948,7 +948,7 @@ static int tap6_handler(struct ctx *c, const struct pool *in,
>
> i = 0;
> resume:
> - for (seq_count = 0, seq = NULL; i < in->count; i++) {
> + for (seq_count = 0, seq = NULL; i < in->count; i = packet_next(in, i)) {
> size_t l4len, plen, check;
> struct in6_addr *saddr, *daddr;
> struct ipv6hdr ip6h_storage;
> diff --git a/tcp.c b/tcp.c
> index 9390e5bf909e..409f4ce18ea9 100644
> --- a/tcp.c
> +++ b/tcp.c
> @@ -1954,7 +1954,8 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
>
> assert(conn->events & ESTABLISHED);
>
> - for (i = idx, iov_i = 0; i < (int)p->count; i++) {
> + for (i = idx, iov_i = 0; i < (int)p->count;
> + i = (i == -1) ? 0 : packet_next(p, i)) {
> uint32_t seq, seq_offset, ack_seq;
> struct tcphdr th_storage;
> const struct tcphdr *th;
> @@ -1977,7 +1978,7 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
>
> if (th->rst) {
> tcp_sock_rst(c, conn, now);
> - return 1;
> + return packet_next(p, i) - i;
...what about:
/**
* packet_slots() - Count of slots for possibly multi-iovec packet
* @p: Packet pool
* @idx: Slot index of the current packet (marker or single iovec)
*
* Return: pool slots occupied by packet at @idx
*/
size_t packet_slots(const struct pool *p, size_t idx)
{
return packet_next(p, i) - i;
}
so that this becomes:
return packet_slots(p, i);
?
Not tested either, and I didn't really think it through.
By the way, while I haven't found any additional problem with that, I was
wondering (again?) if the "empty" marker is making reasoning about counts
of elements somewhat complicated (I was re-checking stuff like
packet_check_range() and packet_add_do()).
An alternative representation, which might makes iterating in natural ways
more awkward (but if we use iterators anyway...?) could be something like:
[...]
* @cont: Bitmap: if set, continuation of previous multi-iovec packet
[...]
struct pool {
[...]
uint8_t cont[UIO_MAXIOV / 8];
[...]
};
as it stands, I do *not* think it would actually be an improvement, and it
would require a hard limit on the length of a pool (which we don't have
anymore), but I'm sharing it anyway in case it might be helpful for
some reason.
> }
>
> len -= off;
> @@ -1998,10 +1999,10 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
> &((int){ 1 }), sizeof(int)))
> flow_trace(conn, "failed to set SO_KEEPALIVE");
>
> - if (p->count == 1) {
> + if (packet_next(p, i) == p->count) {
> tcp_tap_window_update(c, conn,
> ntohs(th->window), now);
> - return 1;
> + return packet_next(p, i) - i;
> }
>
> continue;
> @@ -2329,13 +2330,13 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
> (void)pif;
>
> if (!packet_get(p, idx, &data))
> - return 1;
> + return packet_next(p, idx) - idx;
>
> l4len = iov_tail_size(&data);
>
> th = IOV_REMOVE_HEADER(&data, th_storage);
> if (!th)
> - return 1;
> + return packet_next(p, idx) - idx;
>
> optlen = th->doff * 4UL - sizeof(*th);
> /* Static checkers might fail to see this: */
> @@ -2353,7 +2354,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
> opts, optlen, now);
> else
> tcp_rst_no_conn(c, af, saddr, daddr, flow_lbl, th, l4len);
> - return 1;
> + return packet_next(p, idx) - idx;
> }
>
> assert(flow->f.type == FLOW_TCP);
> @@ -2364,7 +2365,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
>
> if (th->rst) {
> tcp_sock_rst(c, conn, now);
> - return 1;
> + return packet_next(p, idx) - idx;
> }
>
> conn->inactive = false;
> @@ -2378,7 +2379,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
> if (th->syn && th->ack && !th->fin) {
> tcp_conn_from_sock_finish(c, conn, th, opts, optlen,
> now);
> - return 1;
> + return packet_next(p, idx) - idx;
> }
>
> goto reset;
> @@ -2386,8 +2387,9 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
>
> /* Establishing connection from tap */
> if (conn->events & TAP_SYN_RCVD) {
> + /* SYN retry: ignore and keep waiting */
> if (th->syn && !th->ack && !th->fin)
> - return 1; /* SYN retry: ignore and keep waiting */
> + return packet_next(p, idx) - idx;
>
> if (!(conn->events & TAP_SYN_ACK_SENT))
> goto reset;
> @@ -2410,7 +2412,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
>
> conn_event(c, conn, SOCK_FIN_SENT, now);
>
> - return 1;
> + return packet_next(p, idx) - idx;
> }
>
> if (!th->ack)
> @@ -2419,8 +2421,8 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
> if (tcp_tap_window_update(c, conn, ntohs(th->window), now))
> tcp_data_from_sock(c, conn, now);
>
> - if (p->count - idx == 1)
> - return 1;
> + if (packet_next(p, idx) == p->count)
I think that this becomes quite complicated to understand as well.
Maybe a packet_is_last() helper would... help.
> + return packet_next(p, idx) - idx;
> }
>
> /* Established connections not accepting data from tap */
> @@ -2473,7 +2475,7 @@ int tcp_tap_handler(const struct ctx *c, uint8_t pif, sa_family_t af,
> conn_event(c, conn, CLOSED, now);
> }
>
> - return 1;
> + return packet_next(p, idx) - idx;
> }
>
> /* Established connections accepting data from tap */
> diff --git a/udp.c b/udp.c
> index 505e55407fb4..cda0272f427b 100644
> --- a/udp.c
> +++ b/udp.c
> @@ -1023,12 +1023,14 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
> {
> const struct flowside *toside;
> struct mmsghdr mm[UIO_MAXIOV];
> + size_t packet_idx[UIO_MAXIOV + 1];
> union sockaddr_inany to_sa;
> struct iovec m[UIO_MAXIOV];
> struct udphdr uh_storage;
> const struct udphdr *uh;
> struct udp_flow *uflow;
> int i, j, s, count = 0;
> + size_t slot;
I moved this down.
> struct iov_tail data;
> flow_sidx_t tosidx;
> in_port_t src, dst;
> @@ -1037,11 +1039,11 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
> assert(!c->no_udp);
>
> if (!packet_get(p, idx, &data))
> - return 1;
> + return packet_next(p, idx) - idx;
>
> uh = IOV_PEEK_HEADER(&data, uh_storage);
> if (!uh)
> - return 1;
> + return packet_next(p, idx) - idx;
>
> /* The caller already checks that all the messages have the same source
> * and destination, so we can just take those from the first message.
> @@ -1057,7 +1059,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
> pif_name(pif),
> inet_ntop(af, saddr, sstr, sizeof(sstr)), src,
> inet_ntop(af, daddr, dstr, sizeof(dstr)), dst);
> - return 1;
> + return packet_next(p, idx) - idx;
> }
>
> topif = pif_at_sidx(tosidx);
> @@ -1068,7 +1070,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
> flow_err_ratelimit(uflow, now,
> "No support for forwarding UDP from %s to %s",
> pif_name(frompif), pif_name(topif));
> - return 1;
> + return packet_next(p, idx) - idx;
> }
> toside = flowside_at_sidx(tosidx);
>
> @@ -1077,10 +1079,14 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
>
> pif_sockaddr(c, &to_sa, topif, &toside->eaddr, toside->eport);
>
> - for (i = 0, j = 0; i < (int)p->count - idx && j < UIO_MAXIOV; i++) {
> + for (i = 0, j = 0, slot = idx;
> + slot < p->count && j < UIO_MAXIOV && i < UIO_MAXIOV;
> + slot = packet_next(p, slot), i++) {
We should clean this up somehow. Maybe the iterator I outlined above
would help. I didn't try.
> const struct udphdr *uh_send;
>
> - if (!packet_get(p, idx + i, &data))
> + packet_idx[count] = slot;
> +
> + if (!packet_get(p, slot, &data))
> return p->count - idx;
>
> uh_send = IOV_REMOVE_HEADER(&data, uh_storage);
> @@ -1129,12 +1135,13 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
>
> count++;
> }
> + packet_idx[count] = slot;
>
> count = sendmmsg(s, mm, count, MSG_NOSIGNAL);
> if (count < 0)
> - return 1;
> + return packet_next(p, idx) - idx;
>
> - return count;
> + return packet_idx[count] - idx;
> }
>
> /**
--
Stefano
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] packet: Fix pool traversal for multi-iovec packets
@ 2026-11-02 7:30 luochunsheng
2026-11-02 13:03 ` Laurent Vivier
0 siblings, 1 reply; 4+ messages in thread
From: luochunsheng @ 2026-11-02 7:30 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, luffyluo
From: luffyluo <luffyluo@tencent.com>
With vhost-user a guest frame can span several descriptors. The pool
stores such a packet as a marker slot (iov_base == NULL, iov_len = N)
followed by N data slots, and packet_get() uses the marker to return
all N iovecs at once.
The traversal loops in tap4_handler(), tap6_handler(),
tcp_data_from_tap() and udp_tap_handler() don't know about markers and
advance one slot at a time: after handling the packet from its marker
they land on its first data slot, where iov_base != NULL, so
packet_get() hands back that slot alone, as if it were a whole
single-iovec packet. Parsing that fragment as a complete frame is
what breaks: in tcp_data_from_tap() a fragment shorter than a TCP
header makes IOV_PEEK_HEADER() fail ("TCP header not contiguous"), and
a longer one gives a th->doff read from payload bytes, so
off = th->doff * 4 exceeds the fragment length. In udp_tap_handler()
the fragments go out as extra datagrams.
Fix it with packet_next(), returning the index of the packet following
the one at a given index, and use it to advance those loops, so they
skip the data slots of the packet they just handled. For single-iovec
packets it returns idx + 1, so tap and pasta, which always build
single-iovec tails with IOV_TAIL_FROM_BUF(), are unaffected.
Fixes: ca38be0141d1 ("packet: Add support for multi-vector packets")
Signed-off-by: luffyluo <luffyluo@tencent.com>
---
packet.c | 16 ++++++++++++++++
packet.h | 1 +
tap.c | 4 ++--
tcp.c | 2 +-
udp.c | 7 +++++--
5 files changed, 25 insertions(+), 5 deletions(-)
diff --git a/packet.c b/packet.c
index 7a347be..f202245 100644
--- a/packet.c
+++ b/packet.c
@@ -207,3 +207,19 @@ void pool_flush(struct pool *p)
{
p->count = 0;
}
+
+/**
+ * packet_next() - Index of the packet following the one at @idx
+ * @p: Packet pool
+ * @idx: Slot index of the current packet (marker or single iovec)
+ *
+ * A multi-iovec packet is stored as a marker slot (iov_base == NULL) whose
+ * iov_len holds the number of iovec slots that follow it. Return the slot
+ * index of the next packet.
+ */
+size_t packet_next(const struct pool *p, size_t idx)
+{
+ if (p->pkt[idx].iov_base == NULL)
+ return idx + p->pkt[idx].iov_len + 1;
+ return idx + 1;
+}
diff --git a/packet.h b/packet.h
index 7b7fa71..03821f6 100644
--- a/packet.h
+++ b/packet.h
@@ -41,6 +41,7 @@ bool packet_get_do(const struct pool *p, const size_t idx,
struct iov_tail *data, const char *func, int line);
bool pool_can_fit(const struct pool *p, struct iov_tail *data);
void pool_flush(struct pool *p);
+size_t packet_next(const struct pool *p, size_t idx);
#define packet_add(p, data) \
packet_add_do(p, data, __func__, __LINE__)
diff --git a/tap.c b/tap.c
index dfa66c7..1a310fe 100644
--- a/tap.c
+++ b/tap.c
@@ -700,7 +700,7 @@ static int tap4_handler(struct ctx *c, const struct pool *in,
i = 0;
resume:
- for (seq_count = 0, seq = NULL; i < in->count; i++) {
+ for (seq_count = 0, seq = NULL; i < in->count; i = packet_next(in, i)) {
struct iovec trim_iov[UIO_MAXIOV];
size_t l3len, hlen, l4len, check;
struct ethhdr eh_storage;
@@ -948,7 +948,7 @@ static int tap6_handler(struct ctx *c, const struct pool *in,
i = 0;
resume:
- for (seq_count = 0, seq = NULL; i < in->count; i++) {
+ for (seq_count = 0, seq = NULL; i < in->count; i = packet_next(in, i)) {
size_t l4len, plen, check;
struct in6_addr *saddr, *daddr;
struct ipv6hdr ip6h_storage;
diff --git a/tcp.c b/tcp.c
index 95f4f3e..a3bb8b9 100644
--- a/tcp.c
+++ b/tcp.c
@@ -1946,7 +1946,7 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
assert(conn->events & ESTABLISHED);
- for (i = idx, iov_i = 0; i < (int)p->count; i++) {
+ for (i = idx, iov_i = 0; i < (int)p->count; i = packet_next(p, i)) {
uint32_t seq, seq_offset, ack_seq;
struct tcphdr th_storage;
const struct tcphdr *th;
diff --git a/udp.c b/udp.c
index 505e554..bd5e241 100644
--- a/udp.c
+++ b/udp.c
@@ -1029,6 +1029,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
const struct udphdr *uh;
struct udp_flow *uflow;
int i, j, s, count = 0;
+ size_t slot;
struct iov_tail data;
flow_sidx_t tosidx;
in_port_t src, dst;
@@ -1077,10 +1078,12 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
pif_sockaddr(c, &to_sa, topif, &toside->eaddr, toside->eport);
- for (i = 0, j = 0; i < (int)p->count - idx && j < UIO_MAXIOV; i++) {
+ for (i = 0, j = 0, slot = idx;
+ slot < p->count && j < UIO_MAXIOV;
+ slot = packet_next(p, slot), i++) {
const struct udphdr *uh_send;
- if (!packet_get(p, idx + i, &data))
+ if (!packet_get(p, slot, &data))
return p->count - idx;
uh_send = IOV_REMOVE_HEADER(&data, uh_storage);
--
2.43.7
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] packet: Fix pool traversal for multi-iovec packets
2026-11-02 7:30 luochunsheng
@ 2026-11-02 13:03 ` Laurent Vivier
0 siblings, 0 replies; 4+ messages in thread
From: Laurent Vivier @ 2026-11-02 13:03 UTC (permalink / raw)
To: luochunsheng, passt-dev; +Cc: luffyluo
On 11/2/26 08:30, luochunsheng@ustc.edu wrote:
> From: luffyluo <luffyluo@tencent.com>
>
> With vhost-user a guest frame can span several descriptors. The pool
> stores such a packet as a marker slot (iov_base == NULL, iov_len = N)
> followed by N data slots, and packet_get() uses the marker to return
> all N iovecs at once.
>
> The traversal loops in tap4_handler(), tap6_handler(),
> tcp_data_from_tap() and udp_tap_handler() don't know about markers and
> advance one slot at a time: after handling the packet from its marker
> they land on its first data slot, where iov_base != NULL, so
> packet_get() hands back that slot alone, as if it were a whole
> single-iovec packet. Parsing that fragment as a complete frame is
> what breaks: in tcp_data_from_tap() a fragment shorter than a TCP
> header makes IOV_PEEK_HEADER() fail ("TCP header not contiguous"), and
> a longer one gives a th->doff read from payload bytes, so
> off = th->doff * 4 exceeds the fragment length. In udp_tap_handler()
> the fragments go out as extra datagrams.
>
> Fix it with packet_next(), returning the index of the packet following
> the one at a given index, and use it to advance those loops, so they
> skip the data slots of the packet they just handled. For single-iovec
> packets it returns idx + 1, so tap and pasta, which always build
> single-iovec tails with IOV_TAIL_FROM_BUF(), are unaffected.
>
> Fixes: ca38be0141d1 ("packet: Add support for multi-vector packets")
> Signed-off-by: luffyluo <luffyluo@tencent.com>
Thank you for your patch, it looks correct.
There is a remaining problem:
the handlers return the number of consumed slots (generally they comsume everything so
they return p->count - idx) but udp_tap_handler() returns the return value of sendmmsg()
that doesn't reflect the actual number of consumed slot.
Thanks,
Laurent
> ---
> packet.c | 16 ++++++++++++++++
> packet.h | 1 +
> tap.c | 4 ++--
> tcp.c | 2 +-
> udp.c | 7 +++++--
> 5 files changed, 25 insertions(+), 5 deletions(-)
>
> diff --git a/packet.c b/packet.c
> index 7a347be..f202245 100644
> --- a/packet.c
> +++ b/packet.c
> @@ -207,3 +207,19 @@ void pool_flush(struct pool *p)
> {
> p->count = 0;
> }
> +
> +/**
> + * packet_next() - Index of the packet following the one at @idx
> + * @p: Packet pool
> + * @idx: Slot index of the current packet (marker or single iovec)
> + *
> + * A multi-iovec packet is stored as a marker slot (iov_base == NULL) whose
> + * iov_len holds the number of iovec slots that follow it. Return the slot
> + * index of the next packet.
> + */
> +size_t packet_next(const struct pool *p, size_t idx)
> +{
> + if (p->pkt[idx].iov_base == NULL)
> + return idx + p->pkt[idx].iov_len + 1;
> + return idx + 1;
> +}
> diff --git a/packet.h b/packet.h
> index 7b7fa71..03821f6 100644
> --- a/packet.h
> +++ b/packet.h
> @@ -41,6 +41,7 @@ bool packet_get_do(const struct pool *p, const size_t idx,
> struct iov_tail *data, const char *func, int line);
> bool pool_can_fit(const struct pool *p, struct iov_tail *data);
> void pool_flush(struct pool *p);
> +size_t packet_next(const struct pool *p, size_t idx);
>
> #define packet_add(p, data) \
> packet_add_do(p, data, __func__, __LINE__)
> diff --git a/tap.c b/tap.c
> index dfa66c7..1a310fe 100644
> --- a/tap.c
> +++ b/tap.c
> @@ -700,7 +700,7 @@ static int tap4_handler(struct ctx *c, const struct pool *in,
>
> i = 0;
> resume:
> - for (seq_count = 0, seq = NULL; i < in->count; i++) {
> + for (seq_count = 0, seq = NULL; i < in->count; i = packet_next(in, i)) {
> struct iovec trim_iov[UIO_MAXIOV];
> size_t l3len, hlen, l4len, check;
> struct ethhdr eh_storage;
> @@ -948,7 +948,7 @@ static int tap6_handler(struct ctx *c, const struct pool *in,
>
> i = 0;
> resume:
> - for (seq_count = 0, seq = NULL; i < in->count; i++) {
> + for (seq_count = 0, seq = NULL; i < in->count; i = packet_next(in, i)) {
> size_t l4len, plen, check;
> struct in6_addr *saddr, *daddr;
> struct ipv6hdr ip6h_storage;
> diff --git a/tcp.c b/tcp.c
> index 95f4f3e..a3bb8b9 100644
> --- a/tcp.c
> +++ b/tcp.c
> @@ -1946,7 +1946,7 @@ static int tcp_data_from_tap(const struct ctx *c, struct tcp_tap_conn *conn,
>
> assert(conn->events & ESTABLISHED);
>
> - for (i = idx, iov_i = 0; i < (int)p->count; i++) {
> + for (i = idx, iov_i = 0; i < (int)p->count; i = packet_next(p, i)) {
> uint32_t seq, seq_offset, ack_seq;
> struct tcphdr th_storage;
> const struct tcphdr *th;
> diff --git a/udp.c b/udp.c
> index 505e554..bd5e241 100644
> --- a/udp.c
> +++ b/udp.c
> @@ -1029,6 +1029,7 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
> const struct udphdr *uh;
> struct udp_flow *uflow;
> int i, j, s, count = 0;
> + size_t slot;
> struct iov_tail data;
> flow_sidx_t tosidx;
> in_port_t src, dst;
> @@ -1077,10 +1078,12 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
>
> pif_sockaddr(c, &to_sa, topif, &toside->eaddr, toside->eport);
>
> - for (i = 0, j = 0; i < (int)p->count - idx && j < UIO_MAXIOV; i++) {
> + for (i = 0, j = 0, slot = idx;
> + slot < p->count && j < UIO_MAXIOV;
> + slot = packet_next(p, slot), i++) {
> const struct udphdr *uh_send;
>
> - if (!packet_get(p, idx + i, &data))
> + if (!packet_get(p, slot, &data))
> return p->count - idx;
>
> uh_send = IOV_REMOVE_HEADER(&data, uh_storage);
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-27 8:30 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 16:56 [PATCH] packet: Fix pool traversal for multi-iovec packets Laurent Vivier
2026-11-27 8:30 ` Stefano Brivio
-- strict thread matches above, loose matches on Subject: below --
2026-11-02 7:30 luochunsheng
2026-11-02 13:03 ` 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).