11use super :: seqnum:: SeqNum ;
22use etherparse:: TcpHeader ;
3- use std:: collections:: BTreeMap ;
3+ use std:: { collections:: BTreeMap , time :: Duration } ;
44
5- const MAX_UNACK : u32 = 1024 * 16 ; // 16KB
6- const READ_BUFFER_SIZE : usize = 1024 * 16 ; // 16KB
7- const MAX_COUNT_FOR_DUP_ACK : usize = 3 ; // Maximum number of duplicate ACKs before retransmission
5+ pub ( super ) const MAX_UNACK : u32 = 1024 * 16 ; // 16KB
6+ pub ( super ) const READ_BUFFER_SIZE : usize = 1024 * 16 ; // 16KB
7+ pub ( super ) const MAX_COUNT_FOR_DUP_ACK : usize = 3 ; // Maximum number of duplicate ACKs before retransmission
88
99/// Retransmission timeout
10- const RTO : std:: time:: Duration = std:: time:: Duration :: from_secs ( 1 ) ;
10+ pub ( super ) const RTO : std:: time:: Duration = std:: time:: Duration :: from_secs ( 1 ) ;
1111
1212/// Maximum count of retransmissions before dropping the packet
13- pub ( crate ) const MAX_RETRANSMIT_COUNT : usize = 3 ;
13+ pub ( super ) const MAX_RETRANSMIT_COUNT : usize = 3 ;
1414
1515#[ derive( Debug , PartialEq , Clone , Copy ) ]
1616pub ( crate ) enum TcpState {
@@ -55,10 +55,23 @@ pub(crate) struct Tcb {
5555 unordered_packets : BTreeMap < SeqNum , Vec < u8 > > ,
5656 duplicate_ack_count : usize ,
5757 duplicate_ack_count_helper : SeqNum ,
58+ max_unacked_bytes : u32 ,
59+ read_buffer_size : usize ,
60+ max_count_for_dup_ack : usize ,
61+ rto : std:: time:: Duration ,
62+ max_retransmit_count : usize ,
5863}
5964
6065impl Tcb {
61- pub ( super ) fn new ( ack : SeqNum , mtu : u16 ) -> Tcb {
66+ pub ( super ) fn new (
67+ ack : SeqNum ,
68+ mtu : u16 ,
69+ max_unacked_bytes : u32 ,
70+ read_buffer_size : usize ,
71+ max_count_for_dup_ack : usize ,
72+ rto : std:: time:: Duration ,
73+ max_retransmit_count : usize ,
74+ ) -> Tcb {
6275 #[ cfg( debug_assertions) ]
6376 let seq = 100 ;
6477 #[ cfg( not( debug_assertions) ) ]
@@ -74,6 +87,11 @@ impl Tcb {
7487 unordered_packets : BTreeMap :: new ( ) ,
7588 duplicate_ack_count : 0 ,
7689 duplicate_ack_count_helper : seq. into ( ) ,
90+ max_unacked_bytes,
91+ read_buffer_size,
92+ max_count_for_dup_ack,
93+ rto,
94+ max_retransmit_count,
7795 }
7896 }
7997
@@ -94,7 +112,7 @@ impl Tcb {
94112 }
95113
96114 pub fn is_duplicate_ack_count_exceeded ( & self ) -> bool {
97- self . duplicate_ack_count >= MAX_COUNT_FOR_DUP_ACK
115+ self . duplicate_ack_count >= self . max_count_for_dup_ack
98116 }
99117
100118 pub ( super ) fn add_unordered_packet ( & mut self , seq : SeqNum , buf : Vec < u8 > ) {
@@ -106,7 +124,7 @@ impl Tcb {
106124 self . unordered_packets . insert ( seq, buf) ;
107125 }
108126 pub ( super ) fn get_available_read_buffer_size ( & self ) -> usize {
109- READ_BUFFER_SIZE . saturating_sub ( self . get_unordered_packets_total_len ( ) )
127+ self . read_buffer_size . saturating_sub ( self . get_unordered_packets_total_len ( ) )
110128 }
111129 #[ inline]
112130 pub ( crate ) fn get_unordered_packets_total_len ( & self ) -> usize {
@@ -234,7 +252,7 @@ impl Tcb {
234252 return Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidInput , "Empty payload" ) ) ;
235253 }
236254 let buf_len = buf. len ( ) as u32 ;
237- self . inflight_packets . insert ( self . seq , InflightPacket :: new ( self . seq , buf) ) ;
255+ self . inflight_packets . insert ( self . seq , InflightPacket :: new ( self . seq , buf, self . rto ) ) ;
238256 self . seq += buf_len;
239257 Ok ( ( ) )
240258 }
@@ -275,7 +293,7 @@ impl Tcb {
275293 let mut retransmit_list = Vec :: new ( ) ;
276294
277295 self . inflight_packets . retain ( |_, packet| {
278- if packet. retransmit_count >= MAX_RETRANSMIT_COUNT {
296+ if packet. retransmit_count >= self . max_retransmit_count {
279297 log:: warn!( "Packet with seq {:?} reached max retransmit count, dropping packet" , packet. seq) ;
280298 return false ; // remove this packet
281299 }
@@ -302,7 +320,7 @@ impl Tcb {
302320 pub fn is_send_buffer_full ( & self ) -> bool {
303321 // To respect the receiver's window (remote_window) size and avoid sending too many unacknowledged packets, which may cause packet loss
304322 // Simplified version: min(cwnd, rwnd)
305- self . seq . distance ( self . get_last_received_ack ( ) ) >= MAX_UNACK . min ( self . get_send_window ( ) as u32 )
323+ self . seq . distance ( self . get_last_received_ack ( ) ) >= self . max_unacked_bytes . min ( self . get_send_window ( ) as u32 )
306324 }
307325}
308326
@@ -316,13 +334,13 @@ pub struct InflightPacket {
316334}
317335
318336impl InflightPacket {
319- fn new ( seq : SeqNum , payload : Vec < u8 > ) -> Self {
337+ fn new ( seq : SeqNum , payload : Vec < u8 > , rto : Duration ) -> Self {
320338 Self {
321339 seq,
322340 payload,
323341 send_time : std:: time:: Instant :: now ( ) ,
324342 retransmit_count : 0 ,
325- retransmit_timeout : RTO ,
343+ retransmit_timeout : rto ,
326344 }
327345 }
328346 pub ( crate ) fn contains_seq_num ( & self , seq : SeqNum ) -> bool {
@@ -339,7 +357,7 @@ mod tests {
339357
340358 #[ test]
341359 fn test_in_flight_packet ( ) {
342- let p = InflightPacket :: new ( ( u32:: MAX - 1 ) . into ( ) , vec ! [ 10 , 20 , 30 , 40 , 50 ] ) ;
360+ let p = InflightPacket :: new ( ( u32:: MAX - 1 ) . into ( ) , vec ! [ 10 , 20 , 30 , 40 , 50 ] , RTO ) ;
343361
344362 assert ! ( p. contains_seq_num( ( u32 :: MAX - 1 ) . into( ) ) ) ;
345363 assert ! ( p. contains_seq_num( u32 :: MAX . into( ) ) ) ;
@@ -352,7 +370,15 @@ mod tests {
352370
353371 #[ test]
354372 fn test_get_unordered_packets_with_max_bytes ( ) {
355- let mut tcb = Tcb :: new ( SeqNum ( 1000 ) , 1500 ) ;
373+ let mut tcb = Tcb :: new (
374+ SeqNum ( 1000 ) ,
375+ 1500 ,
376+ MAX_UNACK ,
377+ READ_BUFFER_SIZE ,
378+ MAX_COUNT_FOR_DUP_ACK ,
379+ RTO ,
380+ MAX_RETRANSMIT_COUNT ,
381+ ) ;
356382
357383 // insert 3 consecutive packets
358384 tcb. add_unordered_packet ( SeqNum ( 1000 ) , vec ! [ 1 ; 500 ] ) ; // seq=1000, len=500
@@ -384,7 +410,15 @@ mod tests {
384410
385411 #[ test]
386412 fn test_update_inflight_packet_queue ( ) {
387- let mut tcb = Tcb :: new ( SeqNum ( 1000 ) , 1500 ) ;
413+ let mut tcb = Tcb :: new (
414+ SeqNum ( 1000 ) ,
415+ 1500 ,
416+ MAX_UNACK ,
417+ READ_BUFFER_SIZE ,
418+ MAX_COUNT_FOR_DUP_ACK ,
419+ RTO ,
420+ MAX_RETRANSMIT_COUNT ,
421+ ) ;
388422 tcb. seq = SeqNum ( 100 ) ; // setting the initial seq
389423
390424 // insert 3 consecutive packets
@@ -408,7 +442,15 @@ mod tests {
408442
409443 #[ test]
410444 fn test_update_inflight_packet_queue_cumulative_ack ( ) {
411- let mut tcb = Tcb :: new ( SeqNum ( 1000 ) , 1500 ) ;
445+ let mut tcb = Tcb :: new (
446+ SeqNum ( 1000 ) ,
447+ 1500 ,
448+ MAX_UNACK ,
449+ READ_BUFFER_SIZE ,
450+ MAX_COUNT_FOR_DUP_ACK ,
451+ RTO ,
452+ MAX_RETRANSMIT_COUNT ,
453+ ) ;
412454 tcb. seq = SeqNum ( 1000 ) ;
413455
414456 // Insert 3 consecutive packets
@@ -423,7 +465,15 @@ mod tests {
423465
424466 #[ test]
425467 fn test_retransmit_with_exponential_backoff ( ) {
426- let mut tcb = Tcb :: new ( SeqNum ( 1000 ) , 1500 ) ;
468+ let mut tcb = Tcb :: new (
469+ SeqNum ( 1000 ) ,
470+ 1500 ,
471+ MAX_UNACK ,
472+ READ_BUFFER_SIZE ,
473+ MAX_COUNT_FOR_DUP_ACK ,
474+ RTO ,
475+ MAX_RETRANSMIT_COUNT ,
476+ ) ;
427477
428478 tcb. add_inflight_packet ( vec ! [ 1 ; 500 ] ) . unwrap ( ) ;
429479
0 commit comments