Repository navigation
bgpd: fix packets left in the output queue until the next send - #24601
Open
nick-bouliane wants to merge 1 commit into
Open
nick-bouliane wants to merge 1 commit into
nick-bouliane wants to merge 1 commit into
Conversation
bgp_process_writes() schedules the next write event before it writes,
and cancels it again when the output queue of the connection turns out
to be empty. The queue is examined under io_mtx, but the event is
cancelled after the mutex has been released.
A pthread that queues packets adds them under io_mtx and then calls
bgp_writes_on(). If both happen between the examination of the queue
and the cancel in the I/O pthread, bgp_writes_on() finds the write
event scheduled and does nothing, and the cancel then removes that
event. The packets stay in the queue with nothing scheduled to write
them, until something else is queued for the peer. When the main
pthread has nothing more to send, that is the next keepalive.
Cancel the write event while io_mtx is held. Packets are then either
queued before the queue is examined, in which case the event stays, or
after the event is gone, in which case bgp_writes_on() schedules it.
No caller of bgp_writes_off() or bgp_reads_off(), which wait for the
I/O pthread, holds io_mtx.
This was found on 10.7.1, which has the same code, and then reproduced
on master. On 10.7.1, 1000 dynamic peers were sent the same 2000
prefixes over and over with "clear bgp ipv4 unicast * soft out". A
round takes 0.7 seconds. Without this change 4 of 150 rounds took 13
to 16 seconds, with one peer missing the last UPDATEs of the round
while 5 packets sat in its output queue and its socket was idle. With
this change none of 300 rounds took more than 1.1 seconds.
On master, the window is a few instructions wide, but it is easy to
reproduce once it is made wider. Without this change, with a sleep
before the cancel:
if (!reschedule) {
usleep(1000000);
event_cancel(&connection->t_write);
and with two routers, r1 announcing two prefixes to r2 0.3 second apart
("network 10.0.1.0/24", then "network 10.0.2.0/24"), r1 keeps the
UPDATE of the second prefix in its output queue (OutQ 1 in "show bgp
summary") and r2 receives it 55 seconds later, with the next keepalive
of r1. With this change, and the same sleep right after the mutex is
released, r2 has the second prefix after one second.
Fixes: ba98bba ("bgpd: clear write fifo when disabling io writes")
Signed-off-by: Nick Bouliane <nbouliane@coreweave.com>
Contributor
[Medium impact] The PR appears safe to merge. SummaryMoves write-event cancellation inside
No actionable issues found. Reviews (1) · Last reviewed commit: "bgpd: fix packets left in the output que..." · Reviewed by Greptile |
franckrupin
approved these changes
Nov 27, 2026
Contributor
Author
|
ci:rerun |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
bgp_process_writes() schedules the next write event before it writes, and cancels it again when the output queue of the connection turns out to be empty. The queue is examined under io_mtx, but the event is cancelled after the mutex has been released.
A pthread that queues packets adds them under io_mtx and then calls bgp_writes_on(). If both happen between the examination of the queue and the cancel in the I/O pthread, bgp_writes_on() finds the write event scheduled and does nothing, and the cancel then removes that event. The packets stay in the queue with nothing scheduled to write them, until something else is queued for the peer. When the main pthread has nothing more to send, that is the next keepalive.
Cancel the write event while io_mtx is held. Packets are then either queued before the queue is examined, in which case the event stays, or after the event is gone, in which case bgp_writes_on() schedules it. No caller of bgp_writes_off() or bgp_reads_off(), which wait for the I/O pthread, holds io_mtx.
This was found on 10.7.1, which has the same code, and then reproduced on master. On 10.7.1, 1000 dynamic peers were sent the same 2000 prefixes over and over with "clear bgp ipv4 unicast * soft out". A round takes 0.7 seconds. Without this change 4 of 150 rounds took 13 to 16 seconds, with one peer missing the last UPDATEs of the round while 5 packets sat in its output queue and its socket was idle. With this change none of 300 rounds took more than 1.1 seconds.
On master, the window is a few instructions wide, but it is easy to reproduce once it is made wider. Without this change, with a sleep before the cancel:
and with two routers, r1 announcing two prefixes to r2 0.3 second apart ("network 10.0.1.0/24", then "network 10.0.2.0/24"), r1 keeps the UPDATE of the second prefix in its output queue (OutQ 1 in "show bgp summary") and r2 receives it 55 seconds later, with the next keepalive of r1. With this change, and the same sleep right after the mutex is released, r2 has the second prefix after one second.
Fixes: ba98bba ("bgpd: clear write fifo when disabling io writes")