Skip to content

Commit e37bbb0

Browse files
authored
feat(tcp): add getter methods for TCP socket metrics (#3)
- Add remote_mss, remote_win_len, remote_win_scale getters - Add rtt, rtt_var, rto, retransmits getters - Add cwnd, ssthresh getters via congestion controller trait - Add local_seq_no, remote_last_ack getters - Implement cwnd and ssthresh for Cubic, Reno, and NoControl controllers Signed-off-by: longjin <longjin@dragonos.org>
1 parent a6576c0 commit e37bbb0

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

src/socket/tcp.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,50 @@ impl<'a> Socket<'a> {
683683
self.nagle
684684
}
685685

686+
pub fn remote_mss(&self) -> usize {
687+
self.remote_mss
688+
}
689+
690+
pub fn remote_win_len(&self) -> usize {
691+
self.remote_win_len
692+
}
693+
694+
pub fn remote_win_scale(&self) -> Option<u8> {
695+
self.remote_win_scale
696+
}
697+
698+
pub fn rtt(&self) -> u32 {
699+
self.rtte.rtt
700+
}
701+
702+
pub fn rtt_var(&self) -> u32 {
703+
self.rtte.deviation
704+
}
705+
706+
pub fn rto(&self) -> Duration {
707+
self.rtte.retransmission_timeout()
708+
}
709+
710+
pub fn retransmits(&self) -> u8 {
711+
self.rtte.rto_count
712+
}
713+
714+
pub fn cwnd(&self) -> usize {
715+
self.congestion_controller.cwnd()
716+
}
717+
718+
pub fn ssthresh(&self) -> usize {
719+
self.congestion_controller.ssthresh()
720+
}
721+
722+
pub fn local_seq_no(&self) -> TcpSeqNumber {
723+
self.local_seq_no
724+
}
725+
726+
pub fn remote_last_ack(&self) -> Option<TcpSeqNumber> {
727+
self.remote_last_ack
728+
}
729+
686730
/// Return the current window field value, including scaling according to RFC 1323.
687731
///
688732
/// Used in internal calculations as well as packet generation.

src/socket/tcp/congestion.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ pub(super) trait Controller {
3030

3131
/// Set the maximum segment size.
3232
fn set_mss(&mut self, mss: usize) {}
33+
34+
/// Returns the current congestion window size.
35+
fn cwnd(&self) -> usize;
36+
37+
/// Returns the current slow start threshold.
38+
fn ssthresh(&self) -> usize;
3339
}
3440

3541
#[derive(Debug)]
@@ -73,6 +79,16 @@ impl AnyController {
7379
AnyController::None(no_control::NoControl)
7480
}
7581

82+
#[inline]
83+
pub fn cwnd(&self) -> usize {
84+
self.inner().cwnd()
85+
}
86+
87+
#[inline]
88+
pub fn ssthresh(&self) -> usize {
89+
self.inner().ssthresh()
90+
}
91+
7692
#[inline]
7793
pub fn inner_mut(&mut self) -> &mut dyn Controller {
7894
match self {

src/socket/tcp/congestion/cubic.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ impl Controller for Cubic {
3838
self.cwnd
3939
}
4040

41+
fn cwnd(&self) -> usize {
42+
self.cwnd
43+
}
44+
45+
fn ssthresh(&self) -> usize {
46+
self.ssthresh
47+
}
48+
4149
fn on_retransmit(&mut self, now: Instant) {
4250
self.w_max = self.cwnd;
4351
self.ssthresh = self.cwnd >> 1;

src/socket/tcp/congestion/no_control.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ impl Controller for NoControl {
88
fn window(&self) -> usize {
99
usize::MAX
1010
}
11+
12+
fn cwnd(&self) -> usize {
13+
0
14+
}
15+
16+
fn ssthresh(&self) -> usize {
17+
usize::MAX
18+
}
1119
}

src/socket/tcp/congestion/reno.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ impl Controller for Reno {
4747
self.ssthresh = (self.cwnd >> 1).max(self.min_cwnd);
4848
}
4949

50+
fn cwnd(&self) -> usize {
51+
self.cwnd
52+
}
53+
54+
fn ssthresh(&self) -> usize {
55+
self.ssthresh
56+
}
57+
5058
fn on_retransmit(&mut self, _now: Instant) {
5159
self.cwnd = (self.cwnd >> 1).max(self.min_cwnd);
5260
}

0 commit comments

Comments
 (0)