* [PATCH] tcp_buf: Always reserve slot for possible duplicate ACK flag in the same batch
@ 2026-11-24 2:41 Stefano Brivio
2026-11-24 7:42 ` Laurent Vivier
0 siblings, 1 reply; 6+ messages in thread
From: Stefano Brivio @ 2026-11-24 2:41 UTC (permalink / raw)
To: passt-dev; +Cc: Laurent Vivier, Jon Maloy
To avoid the risk of splitting two repeated ACKs, the second one being
a duplicate one, onto different batches, as it might significantly
worsen recovery time using fast retransmits, we don't check, in
tcp_buf_send_flag(), if we need to flush the current batch of frames
before we can append a duplicate ACK flag.
So we originally kept one reserved slot at the end of it: for example,
tcp_buf_send_flag() calls tcp_payload_flush() if we have less than two
free slots.
However, with the merging of data and flags buffers, not all the
conditions under which we call tcp_payload_flush() were updated:
tcp_data_to_tap() and tcp_buf_data_from_sock() could return without
ensuring that an extra free slot would be available for a duplicate
ACK flag. If this happens, and we coincidentally append a duplicate
ACK flag to the batch right after a call to those functions, we'll
crash (reliably) in tcp_buf_send_flag().
Fix those two paths to also reserve one extra slot for duplicate
ACK flags.
Note that, recently, https://bugs.passt.top/show_bug.cgi?id=224
raised some compliance issues with RFC 5681, so we might need to
reserve more slots once we implement full compliance, and probably,
at that point, it would make more sense to consider a different
approach, instead of wasting all that space.
Analysed-by: Laurent Vivier <lvivier@redhat.com>
Fixes: 78da088f7bab ("tcp: unify payload and flags l2 frames array")
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
tcp_buf.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tcp_buf.c b/tcp_buf.c
index ee8c7ce..0780a32 100644
--- a/tcp_buf.c
+++ b/tcp_buf.c
@@ -309,7 +309,7 @@ static void tcp_data_to_tap(const struct ctx *c, struct tcp_tap_conn *conn,
tcp_l2_buf_pad(iov);
- if (++tcp_payload_used > TCP_FRAMES_MEM - 1)
+ if (++tcp_payload_used > TCP_FRAMES_MEM - 2)
tcp_payload_flush(c, now);
}
@@ -352,7 +352,7 @@ int tcp_buf_data_from_sock(const struct ctx *c, struct tcp_tap_conn *conn,
return -1;
}
- if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM) {
+ if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM - 1) {
tcp_payload_flush(c, now);
/* Silence Coverity CWE-125 false positive */
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] tcp_buf: Always reserve slot for possible duplicate ACK flag in the same batch 2026-11-24 2:41 [PATCH] tcp_buf: Always reserve slot for possible duplicate ACK flag in the same batch Stefano Brivio @ 2026-11-24 7:42 ` Laurent Vivier 2026-11-25 1:44 ` Jon Maloy 0 siblings, 1 reply; 6+ messages in thread From: Laurent Vivier @ 2026-11-24 7:42 UTC (permalink / raw) To: Stefano Brivio, passt-dev; +Cc: Jon Maloy On 11/24/26 03:41, Stefano Brivio wrote: > To avoid the risk of splitting two repeated ACKs, the second one being > a duplicate one, onto different batches, as it might significantly > worsen recovery time using fast retransmits, we don't check, in > tcp_buf_send_flag(), if we need to flush the current batch of frames > before we can append a duplicate ACK flag. > > So we originally kept one reserved slot at the end of it: for example, > tcp_buf_send_flag() calls tcp_payload_flush() if we have less than two > free slots. > > However, with the merging of data and flags buffers, not all the > conditions under which we call tcp_payload_flush() were updated: > tcp_data_to_tap() and tcp_buf_data_from_sock() could return without > ensuring that an extra free slot would be available for a duplicate > ACK flag. If this happens, and we coincidentally append a duplicate > ACK flag to the batch right after a call to those functions, we'll > crash (reliably) in tcp_buf_send_flag(). > > Fix those two paths to also reserve one extra slot for duplicate > ACK flags. > > Note that, recently, https://bugs.passt.top/show_bug.cgi?id=224 > raised some compliance issues with RFC 5681, so we might need to > reserve more slots once we implement full compliance, and probably, > at that point, it would make more sense to consider a different > approach, instead of wasting all that space. > > Analysed-by: Laurent Vivier <lvivier@redhat.com> > Fixes: 78da088f7bab ("tcp: unify payload and flags l2 frames array") > Signed-off-by: Stefano Brivio <sbrivio@redhat.com> Reviewed-by: Laurent Vivier <lvivier@redhat.com> > --- > tcp_buf.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/tcp_buf.c b/tcp_buf.c > index ee8c7ce..0780a32 100644 > --- a/tcp_buf.c > +++ b/tcp_buf.c > @@ -309,7 +309,7 @@ static void tcp_data_to_tap(const struct ctx *c, struct tcp_tap_conn *conn, > > tcp_l2_buf_pad(iov); > > - if (++tcp_payload_used > TCP_FRAMES_MEM - 1) > + if (++tcp_payload_used > TCP_FRAMES_MEM - 2) > tcp_payload_flush(c, now); > } > > @@ -352,7 +352,7 @@ int tcp_buf_data_from_sock(const struct ctx *c, struct tcp_tap_conn *conn, > return -1; > } > > - if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM) { > + if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM - 1) { > tcp_payload_flush(c, now); > > /* Silence Coverity CWE-125 false positive */ ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tcp_buf: Always reserve slot for possible duplicate ACK flag in the same batch 2026-11-24 7:42 ` Laurent Vivier @ 2026-11-25 1:44 ` Jon Maloy 2026-11-25 14:37 ` Stefano Brivio 0 siblings, 1 reply; 6+ messages in thread From: Jon Maloy @ 2026-11-25 1:44 UTC (permalink / raw) To: Laurent Vivier, Stefano Brivio, passt-dev On 2026-11-24 02:42, Laurent Vivier wrote:> On 11/24/26 03:41, Stefano Brivio wrote: >> To avoid the risk of splitting two repeated ACKs, the second one being >> a duplicate one, onto different batches, as it might significantly >> worsen recovery time using fast retransmits, we don't check, in >> tcp_buf_send_flag(), if we need to flush the current batch of frames >> before we can append a duplicate ACK flag. >> >> So we originally kept one reserved slot at the end of it: for example, >> tcp_buf_send_flag() calls tcp_payload_flush() if we have less than two >> free slots. >> >> However, with the merging of data and flags buffers, not all the >> conditions under which we call tcp_payload_flush() were updated: >> tcp_data_to_tap() and tcp_buf_data_from_sock() could return without >> ensuring that an extra free slot would be available for a duplicate >> ACK flag. If this happens, and we coincidentally append a duplicate >> ACK flag to the batch right after a call to those functions, we'll >> crash (reliably) in tcp_buf_send_flag(). >> >> Fix those two paths to also reserve one extra slot for duplicate >> ACK flags. >> >> Note that, recently, https://bugs.passt.top/show_bug.cgi?id=224 >> raised some compliance issues with RFC 5681, so we might need to >> reserve more slots once we implement full compliance, and probably, >> at that point, it would make more sense to consider a different >> approach, instead of wasting all that space. >> >> Analysed-by: Laurent Vivier <lvivier@redhat.com> >> Fixes: 78da088f7bab ("tcp: unify payload and flags l2 frames array") >> Signed-off-by: Stefano Brivio <sbrivio@redhat.com> > > Reviewed-by: Laurent Vivier <lvivier@redhat.com> > >> --- >> tcp_buf.c | 4 ++-- >> 1 file changed, 2 insertions(+), 2 deletions(-) >> >> diff --git a/tcp_buf.c b/tcp_buf.c >> index ee8c7ce..0780a32 100644 >> --- a/tcp_buf.c >> +++ b/tcp_buf.c >> @@ -309,7 +309,7 @@ static void tcp_data_to_tap(const struct ctx *c, >> struct tcp_tap_conn *conn, >> tcp_l2_buf_pad(iov); >> - if (++tcp_payload_used > TCP_FRAMES_MEM - 1) >> + if (++tcp_payload_used > TCP_FRAMES_MEM - 2) >> tcp_payload_flush(c, now); >> } >> @@ -352,7 +352,7 @@ int tcp_buf_data_from_sock(const struct ctx *c, >> struct tcp_tap_conn *conn, >> return -1; >> } >> - if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM) { >> + if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM - 1) { >> tcp_payload_flush(c, now); >> /* Silence Coverity CWE-125 false positive */ > With this fix applied, clang-tidy 23.1.5 now reports: tcp_buf.c:363:20: error: Out of bound access to memory after the end of 'tcp_payload' [clang-analyzer-security.ArrayBound,-warnings-as-errors] 363 | iov->iov_base = &tcp_payload[tcp_payload_used + i].data; [...] tcp_buf.c:344:15: note: Field 'mode' is not equal to MODE_PASTA 344 | fill_bufs = TCP_FRAMES; [...] tcp_buf.c:355:6: note: Assuming the condition is false 355 | if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM - 1) { [...] tcp_buf.c:363:20: note: Access of 'tcp_payload' at an overflowing index, while it holds only 128 'struct tcp_payload_t' elements The path itself is a false positive. If fill_bufs is set to 128 on line 344, the condition on line 355 will always be true ((tcp_payload_used + 128) > 127) unless the sum wraps around at 2^32, which is impossible. But we still have a problem when fill_bufs == 128: - The check on line 355 is always true, so we flush, tcp_payload_used becomes 0, and we are ready to receive into all 128 buffers. - If the following recvmsg() fills more than 127 buffers, that is, returns more than 127 * mss bytes, send_bufs is 128 (it can't be more than fill_bufs). If so, tcp_data_to_tap() flushes slots [0, 126] after the 127th frame, as ++tcp_payload_used > TCP_FRAMES_MEM - 2. - When this happens, the 128th frame is built based on the payload in slot 0 instead of slot 127, which is where its data was actually received. So we end up sending the guest a frame with the sequence number of the 128th segment, but with the data of the first one, which we already flushed. The guest accepts and acknowledges it, and the stream is silently corrupted. I think limiting fill_bufs to (TCP_FRAMES_MEM - 1) within the context of tcp_buf_data_from_sock() should fix it. Something like this: #define TCP_FRAMES \ (c->mode == MODE_PASTA ? 1 : TCP_FRAMES_MEM - 1) I haven't tried to reproduce this issue, so I am open to the possibility that I am wrong. ///jon ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tcp_buf: Always reserve slot for possible duplicate ACK flag in the same batch 2026-11-25 1:44 ` Jon Maloy @ 2026-11-25 14:37 ` Stefano Brivio 2026-11-25 21:00 ` Jon Maloy 0 siblings, 1 reply; 6+ messages in thread From: Stefano Brivio @ 2026-11-25 14:37 UTC (permalink / raw) To: Jon Maloy; +Cc: Laurent Vivier, passt-dev On Tue, 24 Nov 2026 20:44:19 -0500 Jon Maloy <jmaloy@redhat.com> wrote: > On 2026-11-24 02:42, Laurent Vivier wrote: > > On 11/24/26 03:41, Stefano Brivio wrote: > >> To avoid the risk of splitting two repeated ACKs, the second one being > >> a duplicate one, onto different batches, as it might significantly > >> worsen recovery time using fast retransmits, we don't check, in > >> tcp_buf_send_flag(), if we need to flush the current batch of frames > >> before we can append a duplicate ACK flag. > >> > >> So we originally kept one reserved slot at the end of it: for example, > >> tcp_buf_send_flag() calls tcp_payload_flush() if we have less than two > >> free slots. > >> > >> However, with the merging of data and flags buffers, not all the > >> conditions under which we call tcp_payload_flush() were updated: > >> tcp_data_to_tap() and tcp_buf_data_from_sock() could return without > >> ensuring that an extra free slot would be available for a duplicate > >> ACK flag. If this happens, and we coincidentally append a duplicate > >> ACK flag to the batch right after a call to those functions, we'll > >> crash (reliably) in tcp_buf_send_flag(). > >> > >> Fix those two paths to also reserve one extra slot for duplicate > >> ACK flags. > >> > >> Note that, recently, https://bugs.passt.top/show_bug.cgi?id=224 > >> raised some compliance issues with RFC 5681, so we might need to > >> reserve more slots once we implement full compliance, and probably, > >> at that point, it would make more sense to consider a different > >> approach, instead of wasting all that space. > >> > >> Analysed-by: Laurent Vivier <lvivier@redhat.com> > >> Fixes: 78da088f7bab ("tcp: unify payload and flags l2 frames array") > >> Signed-off-by: Stefano Brivio <sbrivio@redhat.com> > > > > Reviewed-by: Laurent Vivier <lvivier@redhat.com> > > > >> --- > >> tcp_buf.c | 4 ++-- > >> 1 file changed, 2 insertions(+), 2 deletions(-) > >> > >> diff --git a/tcp_buf.c b/tcp_buf.c > >> index ee8c7ce..0780a32 100644 > >> --- a/tcp_buf.c > >> +++ b/tcp_buf.c > >> @@ -309,7 +309,7 @@ static void tcp_data_to_tap(const struct ctx *c, > >> struct tcp_tap_conn *conn, > >> tcp_l2_buf_pad(iov); > >> - if (++tcp_payload_used > TCP_FRAMES_MEM - 1) > >> + if (++tcp_payload_used > TCP_FRAMES_MEM - 2) > >> tcp_payload_flush(c, now); > >> } > >> @@ -352,7 +352,7 @@ int tcp_buf_data_from_sock(const struct ctx *c, > >> struct tcp_tap_conn *conn, > >> return -1; > >> } > >> - if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM) { > >> + if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM - 1) { > >> tcp_payload_flush(c, now); > >> /* Silence Coverity CWE-125 false positive */ > > > > With this fix applied, clang-tidy 23.1.5 now reports: > > tcp_buf.c:363:20: error: Out of bound access to memory after the end > of 'tcp_payload' [clang-analyzer-security.ArrayBound,-warnings-as-errors] > 363 | iov->iov_base = > &tcp_payload[tcp_payload_used + i].data; > [...] > tcp_buf.c:344:15: note: Field 'mode' is not equal to MODE_PASTA > 344 | fill_bufs = TCP_FRAMES; > [...] > tcp_buf.c:355:6: note: Assuming the condition is false > 355 | if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM - > 1) { > [...] > tcp_buf.c:363:20: note: Access of 'tcp_payload' at an overflowing > index, while it holds only 128 'struct tcp_payload_t' elements Oops, sorry, my version of clang-tidy doesn't report this. We should add a suppression, unless you have something more elegant in mind. > The path itself is a false positive. If fill_bufs is set to 128 on > line 344, the condition on line 355 will always be true > ((tcp_payload_used + 128) > 127) unless the sum wraps around > at 2^32, which is impossible. > > But we still have a problem when fill_bufs == 128: > > - The check on line 355 is always true, so we flush, tcp_payload_used > becomes 0, and we are ready to receive into all 128 buffers. > > - If the following recvmsg() fills more than 127 buffers, that is, > returns more than 127 * mss bytes, send_bufs is 128 (it can't be more > than fill_bufs). If so, tcp_data_to_tap() flushes slots [0, 126] after > the 127th frame, as ++tcp_payload_used > TCP_FRAMES_MEM - 2. I don't understand this part (and I don't see an issue here): if send_bufs is 128, we'll eventually enter tcp_data_to_tap() with tcp_payload_used being 126, and flush the whole array. Now, tcp_payload_used becomes 0, and: > - When this happens, the 128th frame is built based on the payload in > slot 0 instead of slot 127, which is where its data was actually > received. So we end up sending the guest a frame with the sequence > number of the 128th segment, but with the data of the first one, > which we already flushed. The guest accepts and acknowledges it, > and the stream is silently corrupted. ...this doesn't actually happen because tcp_payload_used is 0 on the next call, so 'no_csum' will be 0 as well, and we don't use previous frames at all, right? > I think limiting fill_bufs to (TCP_FRAMES_MEM - 1) within the context of > tcp_buf_data_from_sock() should fix it. > > Something like this: > > #define TCP_FRAMES \ > (c->mode == MODE_PASTA ? 1 : TCP_FRAMES_MEM - 1) Regardless of whether there's any real issue, given that this seems to cause confusion, I start thinking whether this would make things clearer. Perhaps we should leave some tail space specifically for flags, though, because actually we should be able to queue up to 4 flags, once we properly fix compliance with RFC 5681, and at that point I don't think we want to risk wasting 256 KiB for that. > I haven't tried to reproduce this issue, so I am open to > the possibility that I am wrong. > > ///jon > -- Stefano ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tcp_buf: Always reserve slot for possible duplicate ACK flag in the same batch 2026-11-25 14:37 ` Stefano Brivio @ 2026-11-25 21:00 ` Jon Maloy 2026-11-25 22:59 ` Stefano Brivio 0 siblings, 1 reply; 6+ messages in thread From: Jon Maloy @ 2026-11-25 21:00 UTC (permalink / raw) To: Stefano Brivio; +Cc: Laurent Vivier, passt-dev On 2026-11-25 09:37, Stefano Brivio wrote: > On Tue, 24 Nov 2026 20:44:19 -0500 > Jon Maloy <jmaloy@redhat.com> wrote: > [...] >> With this fix applied, clang-tidy 23.1.5 now reports: >> >> tcp_buf.c:363:20: error: Out of bound access to memory after the end >> of 'tcp_payload' [clang-analyzer-security.ArrayBound,-warnings-as-errors] >> 363 | iov->iov_base = >> &tcp_payload[tcp_payload_used + i].data; >> [...] >> tcp_buf.c:344:15: note: Field 'mode' is not equal to MODE_PASTA >> 344 | fill_bufs = TCP_FRAMES; >> [...] >> tcp_buf.c:355:6: note: Assuming the condition is false >> 355 | if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM - >> 1) { >> [...] >> tcp_buf.c:363:20: note: Access of 'tcp_payload' at an overflowing >> index, while it holds only 128 'struct tcp_payload_t' elements > > Oops, sorry, my version of clang-tidy doesn't report this. We should > add a suppression, unless you have something more elegant in mind. > If we limit fill_bufs to TCP_FRAMES_MEM - 1 (see below), the warning goes away by itself, as the check can then be false without any wraparound. >> The path itself is a false positive. If fill_bufs is set to 128 on >> line 344, the condition on line 355 will always be true >> ((tcp_payload_used + 128) > 127) unless the sum wraps around >> at 2^32, which is impossible. >> >> But we still have a problem when fill_bufs == 128: >> >> - The check on line 355 is always true, so we flush, tcp_payload_used >> becomes 0, and we are ready to receive into all 128 buffers. >> >> - If the following recvmsg() fills more than 127 buffers, that is, >> returns more than 127 * mss bytes, send_bufs is 128 (it can't be more >> than fill_bufs). If so, tcp_data_to_tap() flushes slots [0, 126] after >> the 127th frame, as ++tcp_payload_used > TCP_FRAMES_MEM - 2. > > I don't understand this part (and I don't see an issue here): if > send_bufs is 128, we'll eventually enter tcp_data_to_tap() with > tcp_payload_used being 126, and flush the whole array. Now, > tcp_payload_used becomes 0, and: > That flush only sends slots 0 to 126, that is, 127 frames, and it happens in the iteration for the 127th frame (i == 126) of the loop in tcp_buf_data_from_sock(). The loop then continues with i == 127, for the 128th frame, whose data we received into tcp_payload[127]. >> - When this happens, the 128th frame is built based on the payload in >> slot 0 instead of slot 127, which is where its data was actually >> received. So we end up sending the guest a frame with the sequence >> number of the 128th segment, but with the data of the first one, >> which we already flushed. The guest accepts and acknowledges it, >> and the stream is silently corrupted. > > ...this doesn't actually happen because tcp_payload_used is 0 on the > next call, so 'no_csum' will be 0 as well, and we don't use previous > frames at all, right? But we aren't in the next call yet: we still have one frame to send, with tcp_payload_used == 0 and i == 127. So no_csum is fine, but the payload of that final frame is now taken from slot 0. More specifically: tcp_data_to_tap() builds the 128th frame in slot 0, and takes its payload from iov[TCP_IOV_PAYLOAD].iov_base, which for slot 0 is always &tcp_payload[0] (it's only set in tcp_sock_iov_init()). But the data for that segment is in tcp_payload[127], and nothing copies it over. So the frame goes out with the header (and sequence number) of the 128th segment, and the payload of the first one. > >> I think limiting fill_bufs to (TCP_FRAMES_MEM - 1) within the context of >> tcp_buf_data_from_sock() should fix it. >> >> Something like this: >> >> #define TCP_FRAMES \ >> (c->mode == MODE_PASTA ? 1 : TCP_FRAMES_MEM - 1) > > Regardless of whether there's any real issue, given that this seems to > cause confusion, I start thinking whether this would make things > clearer. > > Perhaps we should leave some tail space specifically for flags, though, > because actually we should be able to queue up to 4 flags, once we > properly fix compliance with RFC 5681, and at that point I don't think > we want to risk wasting 256 KiB for that. Agreed. I was thinking of a single constant for the number of slots we reserve for flags, used for the TCP_FRAMES limit, the check in tcp_buf_data_from_sock(), and the flush thresholds in tcp_data_to_tap() and tcp_buf_send_flag(), so that they can't get out of sync again. With that, data frames never end up in the last slots, so those could point to smaller, dedicated flag buffers instead of full tcp_payload[] entries, and we wouldn't waste the space. We'd still keep a single counter, so that frames and flags are sent in the order we queue them. Something like this, to be used in the mentioned functions: #define TCP_FRAMES_FLAGS 4 #define TCP_FRAMES_DATA (TCP_FRAMES_MEM - TCP_FRAMES_FLAGS) #define TCP_FRAMES (c->mode == MODE_PASTA ? 1 : TCP_FRAMES_DATA) ///jon ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tcp_buf: Always reserve slot for possible duplicate ACK flag in the same batch 2026-11-25 21:00 ` Jon Maloy @ 2026-11-25 22:59 ` Stefano Brivio 0 siblings, 0 replies; 6+ messages in thread From: Stefano Brivio @ 2026-11-25 22:59 UTC (permalink / raw) To: Jon Maloy; +Cc: Laurent Vivier, passt-dev On Wed, 25 Nov 2026 16:00:17 -0500 Jon Maloy <jmaloy@redhat.com> wrote: > On 2026-11-25 09:37, Stefano Brivio wrote: > > On Tue, 24 Nov 2026 20:44:19 -0500 > > Jon Maloy <jmaloy@redhat.com> wrote: > > [...] > > >> With this fix applied, clang-tidy 23.1.5 now reports: > >> > >> tcp_buf.c:363:20: error: Out of bound access to memory after the end > >> of 'tcp_payload' [clang-analyzer-security.ArrayBound,-warnings-as-errors] > >> 363 | iov->iov_base = > >> &tcp_payload[tcp_payload_used + i].data; > >> [...] > >> tcp_buf.c:344:15: note: Field 'mode' is not equal to MODE_PASTA > >> 344 | fill_bufs = TCP_FRAMES; > >> [...] > >> tcp_buf.c:355:6: note: Assuming the condition is false > >> 355 | if (tcp_payload_used + fill_bufs > TCP_FRAMES_MEM - > >> 1) { > >> [...] > >> tcp_buf.c:363:20: note: Access of 'tcp_payload' at an overflowing > >> index, while it holds only 128 'struct tcp_payload_t' elements > > > > Oops, sorry, my version of clang-tidy doesn't report this. We should > > add a suppression, unless you have something more elegant in mind. > > > If we limit fill_bufs to TCP_FRAMES_MEM - 1 (see below), the warning > goes away by itself, as the check can then be false without any > wraparound. > > >> The path itself is a false positive. If fill_bufs is set to 128 on > >> line 344, the condition on line 355 will always be true > >> ((tcp_payload_used + 128) > 127) unless the sum wraps around > >> at 2^32, which is impossible. > >> > >> But we still have a problem when fill_bufs == 128: > >> > >> - The check on line 355 is always true, so we flush, tcp_payload_used > >> becomes 0, and we are ready to receive into all 128 buffers. > >> > >> - If the following recvmsg() fills more than 127 buffers, that is, > >> returns more than 127 * mss bytes, send_bufs is 128 (it can't be more > >> than fill_bufs). If so, tcp_data_to_tap() flushes slots [0, 126] after > >> the 127th frame, as ++tcp_payload_used > TCP_FRAMES_MEM - 2. > > > > I don't understand this part (and I don't see an issue here): if > > send_bufs is 128, we'll eventually enter tcp_data_to_tap() with > > tcp_payload_used being 126, and flush the whole array. Now, > > tcp_payload_used becomes 0, and: > > > That flush only sends slots 0 to 126, that is, 127 frames, and it > happens in the iteration for the 127th frame (i == 126) of the loop in > tcp_buf_data_from_sock(). The loop then continues with i == 127, for > the 128th frame, whose data we received into tcp_payload[127]. Oh, I see what you mean now! I just had in mind that it would flush *all* the (used) slots, which is also true, but it doesn't flush the all the tcp_l2_iov[] items, and we certainly don't restart refilling *those* from 0. That is, I didn't really consider the fact that the slots used by tcp_data_to_tap() are counted just like tcp_buf_data_from_sock() does. > >> - When this happens, the 128th frame is built based on the payload in > >> slot 0 instead of slot 127, which is where its data was actually > >> received. So we end up sending the guest a frame with the sequence > >> number of the 128th segment, but with the data of the first one, > >> which we already flushed. The guest accepts and acknowledges it, > >> and the stream is silently corrupted. > > > > ...this doesn't actually happen because tcp_payload_used is 0 on the > > next call, so 'no_csum' will be 0 as well, and we don't use previous > > frames at all, right? > > But we aren't in the next call yet: we still have one frame to send, > with tcp_payload_used == 0 and i == 127. So no_csum is fine, but the > payload of that final frame is now taken from slot 0. > > More specifically: tcp_data_to_tap() builds the 128th frame in slot 0, > and takes its payload from iov[TCP_IOV_PAYLOAD].iov_base, which for > slot 0 is always &tcp_payload[0] (it's only set in tcp_sock_iov_init()). > But the data for that segment is in tcp_payload[127], and nothing > copies it over. So the frame goes out with the header (and sequence > number) of the 128th segment, and the payload of the first one. I see, thanks for the details. By the way, this other segment would be discarded (sequence number collision), and we would have to retransmit it. But anyway, luckily, we didn't release this change yet. > >> I think limiting fill_bufs to (TCP_FRAMES_MEM - 1) within the context of > >> tcp_buf_data_from_sock() should fix it. > >> > >> Something like this: > >> > >> #define TCP_FRAMES \ > >> (c->mode == MODE_PASTA ? 1 : TCP_FRAMES_MEM - 1) > > > > Regardless of whether there's any real issue, given that this seems to > > cause confusion, I start thinking whether this would make things > > clearer. > > > > Perhaps we should leave some tail space specifically for flags, though, > > because actually we should be able to queue up to 4 flags, once we > > properly fix compliance with RFC 5681, and at that point I don't think > > we want to risk wasting 256 KiB for that. > > Agreed. I was thinking of a single constant for the number of slots we > reserve for flags, used for the TCP_FRAMES limit, the check in > tcp_buf_data_from_sock(), and the flush thresholds in > tcp_data_to_tap() and tcp_buf_send_flag(), so that they can't get out > of sync again. > > With that, data frames never end up in the last slots, so those could > point to smaller, dedicated flag buffers instead of full tcp_payload[] > entries, and we wouldn't waste the space. We'd still keep a single > counter, so that frames and flags are sent in the order we queue them. > > Something like this, to be used in the mentioned functions: > > #define TCP_FRAMES_FLAGS 4 > #define TCP_FRAMES_DATA (TCP_FRAMES_MEM - TCP_FRAMES_FLAGS) > #define TCP_FRAMES (c->mode == MODE_PASTA ? 1 : TCP_FRAMES_DATA) Right, makes sense. Maybe that first define could be called TCP_FRAMES_EXTRA_FLAGS if it doesn't make usage exceedingly awkward, and we could keep 128 for TCP_FRAMES_DATA so that we can send a "round" number of bytes in every batch. Given that this patch introduces the issue you described, perhaps it would be nicer to have a minimal fix on top of it (like the one with TCP_FRAMES_MEM - 1) first, and this macro rework later. -- Stefano ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-25 22:59 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-11-24 2:41 [PATCH] tcp_buf: Always reserve slot for possible duplicate ACK flag in the same batch Stefano Brivio 2026-11-24 7:42 ` Laurent Vivier 2026-11-25 1:44 ` Jon Maloy 2026-11-25 14:37 ` Stefano Brivio 2026-11-25 21:00 ` Jon Maloy 2026-11-25 22:59 ` Stefano Brivio
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).