Skip to content

Commit f6164f8

Browse files
authored
fix(net): queue TCP sends before namespace polling (#2723)
* fix(net): queue TCP sends before namespace polling Admit established writes under the socket locks and hand accepted bytes to the existing namespace worker. This avoids global polling and socket notification scans on the ordinary write path. Keep protocol assistance on the backpressure path, including a recheck before a nonblocking send reports EAGAIN. Give already queued data a chance to reach output before an established close purges the send queue and emits a reset. Bound this work by the existing poll budget and the closing socket's admission progress. Stop when the peer window closes or output no longer advances. Cover both zero linger and unread data at close with a loopback regression that also verifies the resulting ECONNRESET. Related to #2324. Signed-off-by: longjin <longjin@dragonos.org> * fix(net): deliver corked TCP data before close FIN A successful corked write can leave bytes outside the smoltcp send queue when the queue fills. Graceful close previously queued FIN and unbound the socket immediately, so the peer could see EOF before it received those bytes. Serialize cork enqueue and consumption with close. Transfer the remaining cork buffer to the existing deferred close entry and let it queue the tail as ACKs free transport space. Queue FIN only once the tail is in the transport queue. Abortive close and timeout still discard the tail. Keep poll publication outside the send locks and retain the publish-only path for successful established writes. Add regression coverage for both a short corked write and a backpressured send before plain close. Signed-off-by: longjin <longjin@dragonos.org> --------- Signed-off-by: longjin <longjin@dragonos.org>
1 parent 712d26b commit f6164f8

8 files changed

Lines changed: 355 additions & 199 deletions

File tree

‎kernel/crates/smoltcp/src/socket/tcp.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,17 @@ impl<'a> Socket<'a> {
23782378
self.tx_buffer.len()
23792379
}
23802380

2381+
/// Total unsent bytes if the peer window allows any new payload.
2382+
pub fn unsent_bytes_if_window_open(&self) -> usize {
2383+
let end = self.local_seq_no + self.tx_buffer.len();
2384+
let next = self.send_sequence_end();
2385+
if next < end && next < self.local_seq_no + self.remote_win_len {
2386+
end - next
2387+
} else {
2388+
0
2389+
}
2390+
}
2391+
23812392
/// Return the amount of octets queued in the receive buffer. This value can be larger than
23822393
/// the slice read by the next `recv` or `peek` call because it includes all queued octets,
23832394
/// and not only the octets that may be returned as a contiguous slice.

‎kernel/src/net/socket/inet/stream/events.rs‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ impl TcpSocket {
77
pub(crate) fn update_events(&self) -> bool {
88
// If cork was disabled or SHUT_WR was requested while there are still cork-buffered bytes,
99
// opportunistically flush them so the data does not become unreachable and FIN can be sent.
10-
if !self
11-
.cork_flush_in_progress
10+
if (!self
11+
.options
12+
.tcp_cork
1213
.load(core::sync::atomic::Ordering::Relaxed)
13-
&& (!self
14-
.options
15-
.tcp_cork
16-
.load(core::sync::atomic::Ordering::Relaxed)
17-
|| self.is_send_shutdown())
14+
|| self.is_send_shutdown())
1815
&& !self.cork_buf.lock().is_empty()
1916
{
2017
let _ = self.flush_cork_buffer();

0 commit comments

Comments
 (0)