rtci-sctp 0.5.0

Sans-I/O SCTP (RFC 9260) with partial reliability and stream reconfiguration, for WebRTC data channels
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
//! RFC 9260 section 7.2: the congestion window. Slow start, congestion avoidance, what a loss
//! does, Fast Recovery, the idle window, Max.Burst, and a change of path.

use super::robustness_test::*;
use super::*;
use crate::chunk::chunk_type::*;
use crate::endpoint::link_test::Link;

const PPI: PayloadProtocolIdentifier = PayloadProtocolIdentifier::Binary;
/// The payload of a full DATA chunk here: `create_association` gives 1400 bytes.
const CHUNK: usize = 1400;
/// The same chunk on the wire, which the congestion control counts (RFC 9260 7.2): the DATA
/// chunk header (16 bytes) with it, no padding needed.
const WIRE: usize = CHUNK + 16;

fn config() -> TransportConfig {
    TransportConfig::default().with_heartbeat_interval(None)
}

/// An established association with `messages` full-size messages written on stream 1 and a
/// peer window that never limits it.
fn sender(config: TransportConfig, messages: usize, now: Instant) -> Association {
    let mut a = create_association(config);
    a.control_queue.clear();
    a.timers.stop(Timer::T1Init);
    a.set_state(AssociationState::Established);
    a.use_forward_tsn = true;
    a.rwnd = u32::MAX / 2;
    a.ssthresh = u32::MAX / 2;
    a.rto_mgr.set_rto(1000, true);
    let payload = Bytes::from(vec![7u8; CHUNK]);
    let mut s = a.open_stream(1, PPI).unwrap();
    for _ in 0..messages {
        s.write_sctp(now, &payload, PPI).unwrap();
    }
    a
}

/// What `a` sends now: the TSNs of its DATA chunks, packet by packet.
fn send(a: &mut Association, now: Instant) -> Vec<Vec<u32>> {
    a.gather_outbound(now)
        .0
        .iter()
        .map(|raw| {
            Packet::unmarshal(raw)
                .unwrap()
                .chunks
                .iter()
                .filter_map(|c| c.as_any().downcast_ref::<ChunkPayloadData>().map(|d| d.tsn))
                .collect::<Vec<u32>>()
        })
        .filter(|tsns| !tsns.is_empty())
        .collect()
}

fn sack(a: &mut Association, cumulative: u32, gaps: &[(u16, u16)], now: Instant) {
    a.handle_sack(
        &ChunkSelectiveAck {
            cumulative_tsn_ack: cumulative,
            advertised_receiver_window_credit: u32::MAX / 2,
            gap_ack_blocks: gaps
                .iter()
                .map(
                    |(start, end)| crate::chunk::chunk_selective_ack::GapAckBlock {
                        start: *start,
                        end: *end,
                    },
                )
                .collect(),
            duplicate_tsn: vec![],
        },
        now,
    )
    .unwrap();
}

fn flight(a: &Association) -> u32 {
    a.inflight_queue.get_num_wire_bytes() as u32
}

/// RFC 9260 7.2.1: the initial cwnd is min(4 x PMDCS, max(2 x PMDCS, 4404)), 4344 over
/// IPv6, where PMDCS is the largest DATA chunk that fits a packet. It was the RFC 4960
/// formula on the MTU, with 4380 for both families.
#[test]
fn the_initial_congestion_window_is_the_rfc_9260_formula() {
    let v4: SocketAddr = "192.0.2.1:5000".parse().unwrap();
    let v6: SocketAddr = "[2001:db8::1]:5000".parse().unwrap();
    // (payload size, peer, PMDCS, expected cwnd)
    for (payload, peer, pmdcs, cwnd) in [
        (1400u32, v4, 1416u32, 4404u32), // 2 x PMDCS < 4404 < 4 x PMDCS
        (1400, v6, 1416, 4344),
        (484, v4, 500, 2000),   // 4 x PMDCS < 4404
        (2984, v4, 3000, 6000), // 2 x PMDCS > 4404
    ] {
        let a = Association::new(
            None,
            Arc::new(TransportConfig::default()),
            payload,
            0,
            peer,
            "0.0.0.0:0".parse().unwrap(),
            TransportProtocol::UDP,
            Instant::now(),
            crate::util::RandomSource::default(),
        );
        assert_eq!(pmdcs, a.pmdcs, "a packet less the common header");
        assert_eq!(cwnd, a.congestion_window(), "payload {payload} to {peer}");
    }
}

/// RFC 9260 7.2.1: the initial ssthresh is arbitrarily high: the peer's advertised window.
#[test]
fn the_initial_slow_start_threshold_is_the_peers_window() {
    let server = TransportConfig::default().with_max_receive_buffer_size(777_777);
    let mut link = Link::new(TransportConfig::default(), server, 301);
    link.connect();
    assert_eq!(777_777, link.client.conn().slow_start_threshold());
    assert_eq!(
        777_777,
        link.client.conn().rwnd,
        "and rwnd starts there (6.2.1 A)"
    );
}

/// RFC 9260 6.1 B, 7.2.1: new DATA is sent while less than cwnd is outstanding, so the last
/// chunk may cross cwnd, by less than one PMDCS and in one packet. A window that the next
/// chunk did not fit exactly was never in full use.
#[test]
fn the_last_chunk_sent_may_cross_the_window_by_less_than_a_packet() {
    let now = Instant::now();
    let mut a = sender(config(), 20, now);
    let cwnd = a.cwnd;
    assert_eq!(4404, cwnd);
    let sent = send(&mut a, now);
    assert_eq!(
        4,
        sent.len(),
        "three chunks stay under 4404, the fourth crosses it"
    );
    assert!(flight(&a) >= cwnd);
    assert!(flight(&a) - cwnd < a.pmdcs);
    assert!(send(&mut a, now).is_empty(), "nothing more until a SACK");
}

/// RFC 9260 7.2.1: in slow start a SACK grows cwnd by at most the bytes it acknowledges and
/// at most one PMDCS, and only when cwnd was in full use, the SACK advances the cumulative
/// TSN, and the sender is not in Fast Recovery. One SACK could double the window.
#[test]
fn slow_start_grows_by_at_most_one_packet_per_sack() {
    let now = Instant::now();
    let mut a = sender(config(), 50, now);
    let first = a.my_next_tsn;
    let pmdcs = a.pmdcs;
    assert_eq!(4, send(&mut a, now).len());
    let cwnd = a.cwnd;

    // A SACK for three packets at once: one PMDCS, not three.
    sack(&mut a, first.wrapping_add(2), &[], now);
    assert_eq!(cwnd + pmdcs, a.cwnd);

    // A SACK for less than a packet's worth grows it by what it acknowledges.
    let mut small = sender(config(), 0, now);
    let small_first = small.my_next_tsn;
    {
        let mut s = small.stream(1).unwrap();
        for _ in 0..200 {
            s.write_sctp(now, &Bytes::from_static(&[1; 100]), PPI)
                .unwrap();
        }
    }
    let _ = send(&mut small, now);
    let cwnd = small.cwnd;
    assert!(flight(&small) >= cwnd);
    sack(&mut small, small_first, &[], now);
    assert_eq!(
        cwnd + 116,
        small.cwnd,
        "the bytes acknowledged, the chunk header with them (RFC 9260 7.2)"
    );

    // Not in full use: no growth.
    let mut idle = sender(config(), 1, now);
    let idle_first = idle.my_next_tsn;
    let _ = send(&mut idle, now);
    let cwnd = idle.cwnd;
    assert!(flight(&idle) < cwnd);
    sack(&mut idle, idle_first, &[], now);
    assert_eq!(cwnd, idle.cwnd, "a window that was not full does not grow");

    // The cumulative TSN does not advance (only a gap is acknowledged): no growth.
    let mut gap = sender(config(), 50, now);
    let gap_first = gap.my_next_tsn;
    let _ = send(&mut gap, now);
    let cwnd = gap.cwnd;
    sack(&mut gap, gap_first.wrapping_sub(1), &[(2, 2)], now);
    assert_eq!(cwnd, gap.cwnd, "no growth without a new cumulative TSN");
}

/// L of RFC 9260 7.2.1 is 1 by default and may be configured larger.
#[test]
fn the_slow_start_growth_limit_is_configurable() {
    let now = Instant::now();
    let mut a = sender(config().with_slow_start_growth_limit(2), 50, now);
    let first = a.my_next_tsn;
    let pmdcs = a.pmdcs;
    let _ = send(&mut a, now);
    let cwnd = a.cwnd;
    sack(&mut a, first.wrapping_add(2), &[], now);
    assert_eq!(cwnd + 2 * pmdcs, a.cwnd, "at most L x PMDCS");
}

/// RFC 9260 7.2.2: above ssthresh, partial_bytes_acked counts what every SACK newly
/// acknowledges, gap blocks included; when it reaches cwnd, and cwnd was in full use, cwnd
/// grows by one PMDCS. So at most one PMDCS per round trip.
#[test]
fn congestion_avoidance_grows_one_packet_per_window() {
    let now = Instant::now();
    let mut a = sender(config(), 100, now);
    a.ssthresh = 1000; // below cwnd: congestion avoidance
    let first = a.my_next_tsn;
    let pmdcs = a.pmdcs;
    let cwnd = a.cwnd;
    assert_eq!(4, send(&mut a, now).len());

    // A gap block acknowledges one chunk: counted, though the cumulative TSN stays.
    sack(&mut a, first.wrapping_sub(1), &[(2, 2)], now);
    assert_eq!(WIRE as u32, a.partial_bytes_acked);
    assert_eq!(cwnd, a.cwnd);
    let _ = send(&mut a, now);

    // Then one chunk per SACK, the window refilled each time. cwnd grows once a window's
    // worth has been acknowledged, by one PMDCS, and the count keeps the remainder.
    let mut acknowledged = WIRE as u32;
    let mut cumulative = first.wrapping_sub(1);
    let mut sacks = 0;
    while a.cwnd == cwnd {
        cumulative = cumulative.wrapping_add(1);
        // The second chunk was acknowledged by the gap block already.
        if cumulative != first.wrapping_add(1) {
            acknowledged += WIRE as u32;
        }
        assert!(
            acknowledged - (WIRE as u32) < cwnd,
            "no growth after a full window was acknowledged"
        );
        // The receiver keeps reporting the gap-acknowledged chunk until the cumulative TSN
        // covers it: a SACK without it would be a renege (RFC 9260 6.2.1 D iii).
        let gaps: &[(u16, u16)] = if cumulative == first { &[(1, 1)] } else { &[] };
        sack(&mut a, cumulative, gaps, now);
        let _ = send(&mut a, now);
        sacks += 1;
        assert!(sacks < 20);
    }
    assert!(acknowledged >= cwnd, "not before a window's worth");
    assert_eq!(cwnd + pmdcs, a.cwnd, "one PMDCS");
    assert_eq!(acknowledged - cwnd, a.partial_bytes_acked);
}

/// RFC 9260 7.2.2: partial_bytes_acked is held at cwnd while cwnd is not in full use, and
/// starts over when everything sent has been acknowledged.
#[test]
fn partial_bytes_acked_is_capped_and_reset() {
    let now = Instant::now();
    let mut a = sender(config(), 3, now);
    a.ssthresh = 1000;
    a.cwnd = 100_000;
    let first = a.my_next_tsn;
    assert_eq!(3, send(&mut a, now).len());
    assert!(flight(&a) < a.cwnd);

    a.partial_bytes_acked = 99_500;
    sack(&mut a, first, &[], now);
    assert_eq!(100_000, a.partial_bytes_acked, "held at cwnd");
    assert_eq!(100_000, a.cwnd, "an unused window does not grow");

    sack(&mut a, first.wrapping_add(2), &[], now);
    assert!(a.inflight_queue.is_empty());
    assert_eq!(0, a.partial_bytes_acked, "all data acknowledged");
}

/// RFC 9260 7.2.3, 6.3.3: a T3-rtx expiry sets ssthresh to max(cwnd / 2, 4 x PMDCS), cwnd to
/// one PMDCS and partial_bytes_acked to 0, and retransmits what fits one packet: one packet
/// is in flight until it is acknowledged.
#[test]
fn a_retransmission_timeout_leaves_one_packet_in_flight() {
    let now = Instant::now();
    let mut a = sender(config(), 0, now);
    a.cwnd = 20 * a.pmdcs;
    a.ssthresh = 1000;
    a.partial_bytes_acked = 5000;
    let first = a.my_next_tsn;
    {
        // Small chunks: several fit a packet, and more than a PMDCS of them would not.
        let mut s = a.stream(1).unwrap();
        for _ in 0..60 {
            s.write_sctp(now, &Bytes::from_static(&[1; 300]), PPI)
                .unwrap();
        }
    }
    let sent: usize = send(&mut a, now).iter().map(Vec::len).sum();
    assert_eq!(60, sent);
    let cwnd = a.cwnd;
    let pmdcs = a.pmdcs;

    let at = a.timers.get(Timer::T3RTX).expect("T3-rtx runs");
    a.handle_timeout(at);
    assert_eq!(std::cmp::max(cwnd / 2, 4 * pmdcs), a.ssthresh);
    assert_eq!(pmdcs, a.cwnd);
    assert_eq!(0, a.partial_bytes_acked);

    let packets = send(&mut a, at);
    assert_eq!(1, packets.len(), "one packet");
    assert_eq!(first, packets[0][0], "of the earliest outstanding chunks");
    assert_eq!(4, packets[0].len(), "as many as fit it");
    assert!(
        send(&mut a, at).is_empty(),
        "and nothing else until it is acknowledged"
    );

    // The acknowledgement opens the window again, in slow start.
    sack(&mut a, packets[0][3], &[], at);
    assert!(a.cwnd > pmdcs);
    assert!(!send(&mut a, at).is_empty());
}

/// Sends `n` full chunks and reports the first missing three times, which puts the sender in
/// Fast Recovery. Returns the first TSN.
fn into_fast_recovery(a: &mut Association, n: u32, now: Instant) -> u32 {
    let first = a.my_next_tsn;
    a.cwnd = n * CHUNK as u32;
    let sent: usize = send(a, now).iter().map(Vec::len).sum();
    assert_eq!(n as usize, sent);
    for gap_end in 2..=4u16 {
        sack(a, first.wrapping_sub(1), &[(2, gap_end)], now);
    }
    assert!(a.in_fast_recovery);
    first
}

/// RFC 9260 7.2.3, 7.2.4: a loss found by SACKs sets ssthresh to max(cwnd / 2, 4 x PMDCS)
/// and cwnd to ssthresh, once. Fast Recovery then lasts until everything that was
/// outstanding is acknowledged: a second loss in that window does not cut cwnd again. The
/// exit point was the highest TSN newly acknowledged, so recovery ended early.
#[test]
fn fast_recovery_lasts_until_what_was_outstanding_is_acknowledged() {
    let now = Instant::now();
    let mut a = sender(config(), 40, now);
    let pmdcs = a.pmdcs;
    let first = into_fast_recovery(&mut a, 20, now);
    let cwnd_before = 20 * CHUNK as u32;
    assert_eq!(std::cmp::max(cwnd_before / 2, 4 * pmdcs), a.ssthresh);
    assert_eq!(a.ssthresh, a.cwnd);
    assert_eq!(0, a.partial_bytes_acked);
    assert_eq!(
        first.wrapping_add(19),
        a.fast_recover_exit_point,
        "the highest TSN outstanding"
    );
    let cut = a.cwnd;
    let retransmitted = send(&mut a, now);
    assert_eq!(
        first, retransmitted[0][0],
        "the lost chunk is retransmitted"
    );

    // The retransmission arrives; a second chunk of the same window (the 9th) is lost too.
    sack(&mut a, first.wrapping_add(7), &[(2, 3)], now);
    sack(&mut a, first.wrapping_add(7), &[(2, 4)], now);
    sack(&mut a, first.wrapping_add(7), &[(2, 5)], now);
    assert!(a.in_fast_recovery, "still recovering the same window");
    assert_eq!(cut, a.ssthresh, "not reduced again");
    assert!(a.cwnd >= cut);

    // Everything that was outstanding at the loss is acknowledged: recovery is over.
    sack(&mut a, first.wrapping_add(19), &[], now);
    assert!(!a.in_fast_recovery);
}

/// RFC 9260 7.2.4 step 4: T3-rtx starts over when the first outstanding chunk is fast
/// retransmitted.
#[test]
fn fast_retransmit_of_the_first_outstanding_chunk_restarts_t3() {
    let start = Instant::now();
    let mut a = sender(config(), 40, start);
    let first = a.my_next_tsn;
    a.cwnd = 20 * CHUNK as u32;
    let _ = send(&mut a, start);
    let t3 = a.timers.get(Timer::T3RTX).expect("T3-rtx runs");

    let later = start + Duration::from_millis(600);
    for gap_end in 2..=4u16 {
        sack(&mut a, first.wrapping_sub(1), &[(2, gap_end)], later);
    }
    let retransmitted = send(&mut a, later);
    assert_eq!(vec![first], retransmitted[0]);
    assert_eq!(
        Some(later + Duration::from_millis(1000)),
        a.timers.get(Timer::T3RTX),
        "a full RTO from the retransmission, not {t3:?}"
    );
}

/// RFC 9260 7.2.4 steps 3 and 5: a Fast Retransmit is one packet that ignores cwnd. Chunks
/// marked for it that do not fit are retransmitted when cwnd allows, not in a second burst
/// that ignores cwnd too.
#[test]
fn chunks_that_do_not_fit_the_fast_retransmit_packet_wait_for_the_window() {
    let now = Instant::now();
    let mut a = sender(config(), 40, now);
    let first = a.my_next_tsn;
    a.cwnd = 20 * CHUNK as u32;
    let _ = send(&mut a, now);
    // The first three chunks are missing: three reports for each.
    for gap_end in 4..=6u16 {
        sack(&mut a, first.wrapping_sub(1), &[(4, gap_end)], now);
    }
    assert!(a.in_fast_recovery);

    let packets = send(&mut a, now);
    assert_eq!(vec![first], packets[0], "one full chunk is the one packet");
    // cwnd is in full use, so the other two wait; they are no Fast Retransmit candidates.
    assert_eq!(1, packets.len());
    for tsn in [first.wrapping_add(1), first.wrapping_add(2)] {
        let c = a.inflight_queue.get(tsn).unwrap();
        assert!(c.retransmit, "marked for retransmission");
        assert!(!c.is_fast_retransmit_candidate());
    }
    // Another SACK in Fast Recovery does not send them past the window either.
    sack(&mut a, first.wrapping_sub(1), &[(4, 7)], now);
    assert!(
        send(&mut a, now)
            .iter()
            .flatten()
            .all(|tsn| *tsn != first.wrapping_add(1))
    );

    // Once the window opens they go out.
    a.cwnd = u32::MAX / 2;
    let sent: Vec<u32> = send(&mut a, now).into_iter().flatten().collect();
    assert!(sent.contains(&first.wrapping_add(1)) && sent.contains(&first.wrapping_add(2)));
}

/// RFC 9260 7.2.1, 7.1: a window that is not used decays, to max(cwnd / 2, 4 x PMDCS) once
/// per RTO, and ssthresh keeps what cwnd was. A burst after a pause used the old window.
#[test]
fn an_idle_congestion_window_decays() {
    let start = Instant::now();
    let mut a = sender(config(), 1, start);
    let first = a.my_next_tsn;
    let pmdcs = a.pmdcs;
    let _ = send(&mut a, start);
    sack(&mut a, first, &[], start);
    assert!(a.inflight_queue.is_empty());
    a.cwnd = 64 * pmdcs;
    a.ssthresh = 1000;

    // Three RTOs (1 s each) without sending: three halvings.
    let later = start + Duration::from_millis(3500);
    a.stream(1)
        .unwrap()
        .write_sctp(later, &Bytes::from_static(b"after the pause"), PPI)
        .unwrap();
    let _ = send(&mut a, later);
    assert_eq!(8 * pmdcs, a.cwnd);
    assert_eq!(
        64 * pmdcs,
        a.ssthresh,
        "what cwnd was before the first step"
    );

    // A long pause stops at 4 x PMDCS.
    sack(&mut a, first.wrapping_add(1), &[], later);
    let much_later = later + Duration::from_secs(3600);
    a.stream(1)
        .unwrap()
        .write_sctp(much_later, &Bytes::from_static(b"x"), PPI)
        .unwrap();
    let _ = send(&mut a, much_later);
    assert_eq!(4 * pmdcs, a.cwnd);

    // A window in use does not decay, however long its DATA has been outstanding.
    let mut busy = sender(config(), 100, start);
    busy.cwnd = 64 * pmdcs;
    let _ = send(&mut busy, start);
    let _ = send(&mut busy, start + Duration::from_secs(30));
    assert_eq!(64 * pmdcs, busy.cwnd);
}

/// RFC 9260 6.1 D, 16: with Max.Burst, cwnd is brought down to what is outstanding plus
/// Max.Burst packets before DATA is sent. Four packets by default.
#[test]
fn max_burst_limits_what_is_released_at_once() {
    assert_eq!(Some(4), TransportConfig::default().max_burst());
    let unlimited = Association::new(
        None,
        Arc::new(TransportConfig::default().with_max_burst(None)),
        1400,
        0,
        SocketAddr::from_str("0.0.0.0:0").unwrap(),
        SocketAddr::from_str("0.0.0.0:0").unwrap(),
        TransportProtocol::UDP,
        Instant::now(),
        crate::util::RandomSource::default(),
    );
    assert_eq!(None, unlimited.max_burst);
    let now = Instant::now();
    // With the chunks counted on the wire (RFC 9260 7.2), Max.Burst 3 lets out three packets.
    for (max_burst, packets) in [(Some(3), 3), (None, 40)] {
        let mut a = sender(config().with_max_burst(max_burst), 40, now);
        let pmdcs = a.pmdcs;
        a.cwnd = 40 * CHUNK as u32;
        let sent = send(&mut a, now);
        assert_eq!(packets, sent.len(), "Max.Burst {max_burst:?}");
        if max_burst.is_some() {
            assert_eq!(3 * pmdcs, a.cwnd, "nothing outstanding plus three packets");
        }
    }
}

/// A bound from outside on the send rate (`set_max_send_rate`, for a controller that shares one
/// estimate among the flows of a path: RFC 8836 section 2, requirement 6a): with cwnd and the
/// peer's window wide open, new DATA leaves at the rate (50 chunks a second here: one per 20 ms
/// step, give or take the first burst), not all at once; the bound lifted, the rest leaves.
#[test]
fn a_bound_on_the_send_rate_paces_new_data() {
    let now = Instant::now();
    let mut a = sender(config().with_max_burst(None), 200, now);
    a.cwnd = 1000 * CHUNK as u32;
    let rate = (WIRE * 8 * 50) as f64;
    a.set_max_send_rate(Some(rate));
    assert_eq!(Some(rate), a.max_send_rate());
    let mut sent: usize = send(&mut a, now).iter().map(Vec::len).sum();
    assert!((1..=2).contains(&sent), "the first burst: {sent}");
    for step in 1..=50u64 {
        sent += send(&mut a, now + Duration::from_millis(20 * step))
            .iter()
            .map(Vec::len)
            .sum::<usize>();
    }
    assert!(
        (45..=56).contains(&sent),
        "about 50 chunks in one second at 50 a second: {sent}"
    );
    a.set_max_send_rate(None);
    assert_eq!(None, a.max_send_rate());
    let rest: usize = send(&mut a, now + Duration::from_millis(1020))
        .iter()
        .map(Vec::len)
        .sum();
    assert_eq!(
        200 - sent,
        rest,
        "the rest at once once the bound is lifted"
    );
}

/// RFC 9260 7.2.5: when the path changes, congestion control and the RTO start again from
/// their initial values, and the timer of what is outstanding runs on the initial RTO.
#[test]
fn a_path_change_restarts_congestion_control_and_the_rto() {
    let now = Instant::now();
    let mut a = sender(config(), 40, now);
    let first = into_fast_recovery(&mut a, 20, now);
    a.rto_mgr.set_rto(8000, false);
    a.rto_mgr.srtt = 900;
    a.partial_bytes_acked = 777;
    assert!(a.in_fast_recovery && a.cwnd != 4404);

    let later = now + Duration::from_millis(100);
    a.on_path_change(later);

    assert_eq!(4404, a.congestion_window(), "the initial cwnd");
    assert!(a.slow_start_threshold() >= a.rwnd, "arbitrarily high again");
    assert_eq!(0, a.partial_bytes_acked);
    assert!(!a.in_fast_recovery);
    assert_eq!(Duration::from_secs(1), a.rto(), "RTO.Initial");
    assert_eq!(Duration::ZERO, a.rtt(), "no RTT of the old path");
    assert_eq!(
        Some(later + Duration::from_secs(1)),
        a.timers.get(Timer::T3RTX),
        "the outstanding DATA is timed on the new path"
    );
    assert!(
        a.inflight_queue.get(first).is_some(),
        "it stays outstanding"
    );
}

// ---- The receive window and when a SACK is sent (RFC 9260 6.2) ------------------------------

/// A connected link whose server has a receive buffer of `buffer` bytes and an application
/// that does not read.
fn link_with_receive_buffer(buffer: u32, seed: u64) -> Link {
    let server = config().with_max_receive_buffer_size(buffer);
    let mut link = Link::new(config(), server, seed);
    link.connect();
    link.server.auto_read = false;
    link
}

fn a_rwnd_of(sack: &[u8]) -> u32 {
    u32::from_be_bytes(sack[8..12].try_into().unwrap())
}

fn cumulative_of(sack: &[u8]) -> u32 {
    u32::from_be_bytes(sack[4..8].try_into().unwrap())
}

/// RFC 9260 6.2: when the application drains a receive buffer whose window was advertised as
/// closed, a SACK announces the window at once. Reading never sent a SACK, so the sender,
/// stopped by a_rwnd 0, found out from its next window probe, a retransmission timeout later.
#[test]
fn a_sack_announces_the_window_when_the_application_drains_it() {
    let mut link = link_with_receive_buffer(4000, 311);
    let now = link.now;
    {
        let mut s = link.client.conn().open_stream(1, PPI).unwrap();
        for i in 0..30u8 {
            s.write_sctp(now, &Bytes::from(vec![i; 1000]), PPI).unwrap();
        }
    }
    link.run_for(Duration::from_millis(400));
    assert_eq!(
        0,
        link.server.conn().last_advertised_window,
        "the window closed"
    );

    // The application reads everything that is there.
    let mut read = 0;
    while let Ok(Some(_)) = link.server.conn().stream(1).unwrap().read_sctp() {
        read += 1;
    }
    assert!(read > 0);
    let drained_at = link.now;
    let sent = sent_datagrams(link.server.conn(), drained_at);
    let sacks = chunks_of_type(&sent, CT_SACK.0);
    assert_eq!(1, sacks.len(), "a SACK at once");
    assert!(a_rwnd_of(&sacks[0]) > 0, "that announces the window");

    // From the same state, the sender is going again well within its retransmission timeout.
    let mut link = link_with_receive_buffer(4000, 311);
    let now = link.now;
    {
        let mut s = link.client.conn().open_stream(1, PPI).unwrap();
        for i in 0..30u8 {
            s.write_sctp(now, &Bytes::from(vec![i; 1000]), PPI).unwrap();
        }
    }
    link.run_for(Duration::from_millis(400));
    let mut read = 0;
    while let Ok(Some(_)) = link.server.conn().stream(1).unwrap().read_sctp() {
        read += 1;
    }
    let drained_at = link.now;
    let before = link.bytes_sent[0];
    link.run_for(Duration::from_millis(100));
    assert!(read > 0 && link.now - drained_at == Duration::from_millis(100));
    assert!(
        link.bytes_sent[0] - before >= 1000,
        "the sender sent no new DATA in the 100 ms after the window reopened"
    );
}

/// RFC 9260 6.2: "The receiver MUST avoid sending a large number of window updates -- in
/// particular, large bursts of them." A window update goes only when the window as the
/// sender last saw it has fallen below min(half the buffer, one PMDCS): an application that
/// reads message by message from a buffer whose window stays wide open makes no SACK of its
/// own with any read.
#[test]
fn reads_that_leave_the_window_open_send_no_window_update() {
    let mut link = link_with_receive_buffer(1 << 20, 313);
    let now = link.now;
    {
        let mut s = link.client.conn().open_stream(1, PPI).unwrap();
        for i in 0..10u8 {
            s.write_sctp(now, &Bytes::from(vec![i; 1000]), PPI).unwrap();
        }
    }
    link.run_for(Duration::from_millis(400));
    let (mut read, mut sacks) = (0, 0);
    while let Ok(Some(_)) = link.server.conn().stream(1).unwrap().read_sctp() {
        read += 1;
        let at = link.now;
        sacks += chunks_of_type(&sent_datagrams(link.server.conn(), at), CT_SACK.0).len();
    }
    assert_eq!(10, read, "every message arrived");
    assert_eq!(0, sacks, "no window update for a window that stayed open");
}

/// RFC 9260 6.2: a DATA chunk dropped for lack of receive window is answered with a SACK at
/// once, and that SACK does not acknowledge the chunk.
#[test]
fn a_chunk_dropped_for_lack_of_window_is_answered_at_once() {
    let mut link = link_with_receive_buffer(3000, 312);
    let now = link.now;
    let a = link.server.conn();
    let tsn = a.peer_last_tsn.wrapping_add(1);
    for i in 0..2u32 {
        let raw = packet_to(
            a,
            &[data_chunk(tsn.wrapping_add(i), 1, i as u16, &[7u8; 1500])],
        );
        inject(a, now, raw);
    }
    let _ = sent_after(a, now, Duration::from_secs(1));
    assert_eq!(0, a.get_my_receiver_window_credit(), "the buffer is full");

    let later = now + Duration::from_secs(1);
    let raw = packet_to(a, &[data_chunk(tsn.wrapping_add(2), 1, 2, &[7u8; 1500])]);
    inject(a, later, raw);
    let sacks = chunks_of_type(&sent_datagrams(a, later), CT_SACK.0);
    assert_eq!(1, sacks.len(), "a SACK without delay");
    assert_eq!(
        tsn.wrapping_add(1),
        cumulative_of(&sacks[0]),
        "the dropped chunk is not in it"
    );
    assert_eq!(
        0,
        u16::from_be_bytes([sacks[0][12], sacks[0][13]]),
        "nor in a Gap Ack Block"
    );
    assert_eq!(0, a_rwnd_of(&sacks[0]));
}

/// RFC 9260 6.2 (RFC 1122 4.2.3.3): the receiver avoids the silly window syndrome. A window
/// too small for a full packet is advertised as closed; the raw free space was advertised.
#[test]
fn a_window_smaller_than_a_packet_is_advertised_as_closed() {
    let mut link = link_with_receive_buffer(4000, 313);
    let now = link.now;
    let a = link.server.conn();
    let pmdcs = a.pmdcs;
    let tsn = a.peer_last_tsn.wrapping_add(1);
    let raw = packet_to(a, &[data_chunk(tsn, 1, 0, &[7u8; 1000])]);
    inject(a, now, raw);
    let sacks = chunks_of_type(&sent_datagrams(a, now), CT_SACK.0);
    assert_eq!(
        3000,
        a_rwnd_of(&sacks[0]),
        "room for more than a packet: as it is"
    );

    let free = 4000 - 1000 - 2400;
    assert!(free < pmdcs && free > 0);
    let raw = packet_to(a, &[data_chunk(tsn.wrapping_add(1), 1, 1, &[7u8; 2400])]);
    inject(a, now, raw);
    let sacks = chunks_of_type(&sent_after(a, now, Duration::from_secs(1)), CT_SACK.0);
    assert_eq!(0, a_rwnd_of(&sacks[0]), "{free} bytes are not offered");
    assert_eq!(
        free,
        a.get_my_receiver_window_credit(),
        "though they are there"
    );
}

/// RFC 9260 6.2, 16: SACK.Delay is 200 ms, configurable, and never more than 500 ms.
#[test]
fn the_sack_delay_is_configurable_up_to_half_a_second() {
    assert_eq!(
        Duration::from_millis(500),
        TransportConfig::default()
            .with_sack_delay(Duration::from_secs(3))
            .sack_delay()
    );
    for (configured, delay) in [
        (None, 200u64),
        (Some(Duration::from_millis(50)), 50),
        (Some(Duration::from_secs(3)), 500),
    ] {
        let mut server = config();
        if let Some(value) = configured {
            server = server.with_sack_delay(value);
        }
        let mut link = Link::new(config(), server, 314);
        link.connect();
        let now = link.now;
        let a = link.server.conn();
        let tsn = a.peer_last_tsn.wrapping_add(1);
        // The first DATA of the association is acknowledged at once; the second waits.
        let raw = packet_to(a, &[data_chunk(tsn, 1, 0, b"first")]);
        inject(a, now, raw);
        let _ = sent_datagrams(a, now);
        let raw = packet_to(a, &[data_chunk(tsn.wrapping_add(1), 1, 1, b"second")]);
        inject(a, now, raw);
        assert!(chunks_of_type(&sent_datagrams(a, now), CT_SACK.0).is_empty());
        assert_eq!(
            Some(now + Duration::from_millis(delay)),
            a.timers.get(Timer::Ack),
            "SACK.Delay {configured:?}"
        );
        let at = now + Duration::from_millis(delay);
        a.handle_timeout(at);
        assert_eq!(1, chunks_of_type(&sent_datagrams(a, at), CT_SACK.0).len());
    }
}

/// RFC 9260 6.2 (RFC 7053): a DATA chunk with the I bit is acknowledged without delay.
#[test]
fn a_data_chunk_with_the_i_bit_is_acknowledged_at_once() {
    let mut link = connected_link(315);
    let now = link.now;
    let a = link.server.conn();
    a.first_data_received = true;
    let tsn = a.peer_last_tsn.wrapping_add(1);

    let raw = packet_to(a, &[data_chunk(tsn, 1, 0, b"no hurry")]);
    inject(a, now, raw);
    assert!(chunks_of_type(&sent_datagrams(a, now), CT_SACK.0).is_empty());

    let mut urgent = data_chunk(tsn.wrapping_add(1), 1, 1, b"at once");
    urgent[1] |= 0x08;
    let raw = packet_to(a, &[urgent]);
    inject(a, now, raw);
    let sacks = chunks_of_type(&sent_datagrams(a, now), CT_SACK.0);
    assert_eq!(1, sacks.len());
    assert_eq!(tsn.wrapping_add(1), cumulative_of(&sacks[0]));
}

/// RFC 9260 7.2: "When calculating one of these variables [cwnd, ssthresh,
/// partial_bytes_acked], the length of the DATA chunk, including the padding, SHOULD be used."
/// The flight size compared with cwnd is the chunks' size on the wire: a window of 100 bytes
/// takes five one-byte messages (16 bytes of header, 1 of data, 3 of padding each), not 100.
#[test]
fn the_flight_size_counts_chunk_headers_and_padding() {
    let now = Instant::now();
    let mut a = sender(config(), 0, now);
    a.cwnd = 100;
    {
        let mut s = a.stream(1).unwrap();
        for i in 0..10u8 {
            s.write_sctp(now, &Bytes::from(vec![i]), PPI).unwrap();
        }
    }
    let sent: usize = send(&mut a, now).iter().map(Vec::len).sum();
    assert_eq!(
        5, sent,
        "20 bytes each on the wire, the fifth fills the window"
    );
    assert_eq!(100, flight(&a));
    assert_eq!(
        5,
        a.inflight_queue.get_num_bytes(),
        "rwnd still counts user data"
    );
}

/// RFC 9260 7.2.2: in congestion avoidance partial_bytes_acked grows by "the total number of
/// bytes (including the chunk header and the padding) of all new DATA chunks acknowledged in
/// that SACK chunk ... and by the number of bytes of duplicated chunks reported in Duplicate
/// TSNs". A chunk reported as a duplicate counts once, whatever the receiver repeats, and a TSN
/// that was never sent and acknowledged counts nothing.
#[test]
fn partial_bytes_acked_counts_wire_bytes_and_duplicates_once() {
    let now = Instant::now();
    let mut a = sender(config(), 6, now);
    a.ssthresh = 1000; // below cwnd: congestion avoidance
    a.cwnd = 4 * WIRE as u32;
    let first = a.my_next_tsn;
    assert_eq!(4, send(&mut a, now).iter().map(Vec::len).sum::<usize>());

    sack(&mut a, first, &[], now);
    assert_eq!(
        WIRE as u32, a.partial_bytes_acked,
        "the chunk with its header"
    );
    let _ = send(&mut a, now);

    let with_duplicates = |dups: Vec<u32>| ChunkSelectiveAck {
        cumulative_tsn_ack: first,
        advertised_receiver_window_credit: u32::MAX / 2,
        gap_ack_blocks: vec![],
        duplicate_tsn: dups,
    };
    // The first chunk again, twice in this SACK and once more in the next; and a TSN never
    // acknowledged.
    a.handle_sack(
        &with_duplicates(vec![first, first, first.wrapping_add(40)]),
        now,
    )
    .unwrap();
    assert_eq!(
        2 * WIRE as u32,
        a.partial_bytes_acked,
        "the duplicate, once"
    );
    a.handle_sack(&with_duplicates(vec![first]), now).unwrap();
    assert_eq!(2 * WIRE as u32, a.partial_bytes_acked, "not again");
}