Skip to content

bgpd: fix packets left in the output queue until the next send - #24601

Open
nick-bouliane wants to merge 1 commit into
FRRouting:masterfrom
nick-bouliane:nbouliane/fix_bgp_lost_write_wakeup
Open

nick-bouliane wants to merge 1 commit into
FRRouting:masterfrom
nick-bouliane:nbouliane/fix_bgp_lost_write_wakeup

Conversation

@nick-bouliane

Copy link
Copy Markdown
Contributor

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")

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>
@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Moves write-event cancellation inside io_mtx in bgp_process_writes().

  • BGP peers keep queued packets moving when new packets arrive as a write ends.

No actionable issues found.

Reviews (1) · Last reviewed commit: "bgpd: fix packets left in the output que..." · Reviewed by Greptile

@bsalmicw bsalmicw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nick-bouliane

Copy link
Copy Markdown
Contributor Author

ci:rerun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants