• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #include "net/dcsctp/tx/retransmission_queue.h"
11 
12 #include <algorithm>
13 #include <cstdint>
14 #include <functional>
15 #include <iterator>
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include "absl/algorithm/container.h"
23 #include "absl/strings/string_view.h"
24 #include "absl/types/optional.h"
25 #include "api/array_view.h"
26 #include "net/dcsctp/common/math.h"
27 #include "net/dcsctp/common/sequence_numbers.h"
28 #include "net/dcsctp/common/str_join.h"
29 #include "net/dcsctp/packet/chunk/data_chunk.h"
30 #include "net/dcsctp/packet/chunk/forward_tsn_chunk.h"
31 #include "net/dcsctp/packet/chunk/forward_tsn_common.h"
32 #include "net/dcsctp/packet/chunk/idata_chunk.h"
33 #include "net/dcsctp/packet/chunk/iforward_tsn_chunk.h"
34 #include "net/dcsctp/packet/chunk/sack_chunk.h"
35 #include "net/dcsctp/packet/data.h"
36 #include "net/dcsctp/public/dcsctp_options.h"
37 #include "net/dcsctp/public/types.h"
38 #include "net/dcsctp/timer/timer.h"
39 #include "net/dcsctp/tx/outstanding_data.h"
40 #include "net/dcsctp/tx/send_queue.h"
41 #include "rtc_base/checks.h"
42 #include "rtc_base/logging.h"
43 #include "rtc_base/strings/string_builder.h"
44 
45 namespace dcsctp {
46 namespace {
47 
48 // Allow sending only slightly less than an MTU, to account for headers.
49 constexpr float kMinBytesRequiredToSendFactor = 0.9;
50 }  // namespace
51 
RetransmissionQueue(absl::string_view log_prefix,DcSctpSocketCallbacks * callbacks,TSN my_initial_tsn,size_t a_rwnd,SendQueue & send_queue,std::function<void (DurationMs rtt)> on_new_rtt,std::function<void ()> on_clear_retransmission_counter,Timer & t3_rtx,const DcSctpOptions & options,bool supports_partial_reliability,bool use_message_interleaving)52 RetransmissionQueue::RetransmissionQueue(
53     absl::string_view log_prefix,
54     DcSctpSocketCallbacks* callbacks,
55     TSN my_initial_tsn,
56     size_t a_rwnd,
57     SendQueue& send_queue,
58     std::function<void(DurationMs rtt)> on_new_rtt,
59     std::function<void()> on_clear_retransmission_counter,
60     Timer& t3_rtx,
61     const DcSctpOptions& options,
62     bool supports_partial_reliability,
63     bool use_message_interleaving)
64     : callbacks_(*callbacks),
65       options_(options),
66       min_bytes_required_to_send_(options.mtu * kMinBytesRequiredToSendFactor),
67       partial_reliability_(supports_partial_reliability),
68       log_prefix_(std::string(log_prefix) + "tx: "),
69       data_chunk_header_size_(use_message_interleaving
70                                   ? IDataChunk::kHeaderSize
71                                   : DataChunk::kHeaderSize),
72       on_new_rtt_(std::move(on_new_rtt)),
73       on_clear_retransmission_counter_(
74           std::move(on_clear_retransmission_counter)),
75       t3_rtx_(t3_rtx),
76       cwnd_(options_.cwnd_mtus_initial * options_.mtu),
77       rwnd_(a_rwnd),
78       // https://tools.ietf.org/html/rfc4960#section-7.2.1
79       // "The initial value of ssthresh MAY be arbitrarily high (for
80       // example, implementations MAY use the size of the receiver advertised
81       // window).""
82       ssthresh_(rwnd_),
83       partial_bytes_acked_(0),
84       send_queue_(send_queue),
85       outstanding_data_(
86           data_chunk_header_size_,
87           tsn_unwrapper_.Unwrap(my_initial_tsn),
88           tsn_unwrapper_.Unwrap(TSN(*my_initial_tsn - 1)),
89           [this](IsUnordered unordered, StreamID stream_id, MID message_id) {
90             return send_queue_.Discard(unordered, stream_id, message_id);
91           }) {}
92 
IsConsistent() const93 bool RetransmissionQueue::IsConsistent() const {
94   return true;
95 }
96 
97 // Returns how large a chunk will be, serialized, carrying the data
GetSerializedChunkSize(const Data & data) const98 size_t RetransmissionQueue::GetSerializedChunkSize(const Data& data) const {
99   return RoundUpTo4(data_chunk_header_size_ + data.size());
100 }
101 
MaybeExitFastRecovery(UnwrappedTSN cumulative_tsn_ack)102 void RetransmissionQueue::MaybeExitFastRecovery(
103     UnwrappedTSN cumulative_tsn_ack) {
104   // https://tools.ietf.org/html/rfc4960#section-7.2.4
105   // "When a SACK acknowledges all TSNs up to and including this [fast
106   // recovery] exit point, Fast Recovery is exited."
107   if (fast_recovery_exit_tsn_.has_value() &&
108       cumulative_tsn_ack >= *fast_recovery_exit_tsn_) {
109     RTC_DLOG(LS_VERBOSE) << log_prefix_
110                          << "exit_point=" << *fast_recovery_exit_tsn_->Wrap()
111                          << " reached - exiting fast recovery";
112     fast_recovery_exit_tsn_ = absl::nullopt;
113   }
114 }
115 
HandleIncreasedCumulativeTsnAck(size_t outstanding_bytes,size_t total_bytes_acked)116 void RetransmissionQueue::HandleIncreasedCumulativeTsnAck(
117     size_t outstanding_bytes,
118     size_t total_bytes_acked) {
119   // Allow some margin for classifying as fully utilized, due to e.g. that too
120   // small packets (less than kMinimumFragmentedPayload) are not sent +
121   // overhead.
122   bool is_fully_utilized = outstanding_bytes + options_.mtu >= cwnd_;
123   size_t old_cwnd = cwnd_;
124   if (phase() == CongestionAlgorithmPhase::kSlowStart) {
125     if (is_fully_utilized && !is_in_fast_recovery()) {
126       // https://tools.ietf.org/html/rfc4960#section-7.2.1
127       // "Only when these three conditions are met can the cwnd be
128       // increased; otherwise, the cwnd MUST not be increased. If these
129       // conditions are met, then cwnd MUST be increased by, at most, the
130       // lesser of 1) the total size of the previously outstanding DATA
131       // chunk(s) acknowledged, and 2) the destination's path MTU."
132       cwnd_ += std::min(total_bytes_acked, options_.mtu);
133       RTC_DLOG(LS_VERBOSE) << log_prefix_ << "SS increase cwnd=" << cwnd_
134                            << " (" << old_cwnd << ")";
135     }
136   } else if (phase() == CongestionAlgorithmPhase::kCongestionAvoidance) {
137     // https://tools.ietf.org/html/rfc4960#section-7.2.2
138     // "Whenever cwnd is greater than ssthresh, upon each SACK arrival
139     // that advances the Cumulative TSN Ack Point, increase
140     // partial_bytes_acked by the total number of bytes of all new chunks
141     // acknowledged in that SACK including chunks acknowledged by the new
142     // Cumulative TSN Ack and by Gap Ack Blocks."
143     size_t old_pba = partial_bytes_acked_;
144     partial_bytes_acked_ += total_bytes_acked;
145 
146     if (partial_bytes_acked_ >= cwnd_ && is_fully_utilized) {
147       // https://tools.ietf.org/html/rfc4960#section-7.2.2
148       // "When partial_bytes_acked is equal to or greater than cwnd and
149       // before the arrival of the SACK the sender had cwnd or more bytes of
150       // data outstanding (i.e., before arrival of the SACK, flightsize was
151       // greater than or equal to cwnd), increase cwnd by MTU, and reset
152       // partial_bytes_acked to (partial_bytes_acked - cwnd)."
153 
154       // Errata: https://datatracker.ietf.org/doc/html/rfc8540#section-3.12
155       partial_bytes_acked_ -= cwnd_;
156       cwnd_ += options_.mtu;
157       RTC_DLOG(LS_VERBOSE) << log_prefix_ << "CA increase cwnd=" << cwnd_
158                            << " (" << old_cwnd << ") ssthresh=" << ssthresh_
159                            << ", pba=" << partial_bytes_acked_ << " ("
160                            << old_pba << ")";
161     } else {
162       RTC_DLOG(LS_VERBOSE) << log_prefix_ << "CA unchanged cwnd=" << cwnd_
163                            << " (" << old_cwnd << ") ssthresh=" << ssthresh_
164                            << ", pba=" << partial_bytes_acked_ << " ("
165                            << old_pba << ")";
166     }
167   }
168 }
169 
HandlePacketLoss(UnwrappedTSN highest_tsn_acked)170 void RetransmissionQueue::HandlePacketLoss(UnwrappedTSN highest_tsn_acked) {
171   if (!is_in_fast_recovery()) {
172     // https://tools.ietf.org/html/rfc4960#section-7.2.4
173     // "If not in Fast Recovery, adjust the ssthresh and cwnd of the
174     // destination address(es) to which the missing DATA chunks were last
175     // sent, according to the formula described in Section 7.2.3."
176     size_t old_cwnd = cwnd_;
177     size_t old_pba = partial_bytes_acked_;
178     ssthresh_ = std::max(cwnd_ / 2, options_.cwnd_mtus_min * options_.mtu);
179     cwnd_ = ssthresh_;
180     partial_bytes_acked_ = 0;
181 
182     RTC_DLOG(LS_VERBOSE) << log_prefix_
183                          << "packet loss detected (not fast recovery). cwnd="
184                          << cwnd_ << " (" << old_cwnd
185                          << "), ssthresh=" << ssthresh_
186                          << ", pba=" << partial_bytes_acked_ << " (" << old_pba
187                          << ")";
188 
189     // https://tools.ietf.org/html/rfc4960#section-7.2.4
190     // "If not in Fast Recovery, enter Fast Recovery and mark the highest
191     // outstanding TSN as the Fast Recovery exit point."
192     fast_recovery_exit_tsn_ = outstanding_data_.highest_outstanding_tsn();
193     RTC_DLOG(LS_VERBOSE) << log_prefix_
194                          << "fast recovery initiated with exit_point="
195                          << *fast_recovery_exit_tsn_->Wrap();
196   } else {
197     // https://tools.ietf.org/html/rfc4960#section-7.2.4
198     // "While in Fast Recovery, the ssthresh and cwnd SHOULD NOT change for
199     // any destinations due to a subsequent Fast Recovery event (i.e., one
200     // SHOULD NOT reduce the cwnd further due to a subsequent Fast Retransmit)."
201     RTC_DLOG(LS_VERBOSE) << log_prefix_
202                          << "packet loss detected (fast recovery). No changes.";
203   }
204 }
205 
UpdateReceiverWindow(uint32_t a_rwnd)206 void RetransmissionQueue::UpdateReceiverWindow(uint32_t a_rwnd) {
207   rwnd_ = outstanding_data_.outstanding_bytes() >= a_rwnd
208               ? 0
209               : a_rwnd - outstanding_data_.outstanding_bytes();
210 }
211 
StartT3RtxTimerIfOutstandingData()212 void RetransmissionQueue::StartT3RtxTimerIfOutstandingData() {
213   // Note: Can't use `outstanding_bytes()` as that one doesn't count chunks to
214   // be retransmitted.
215   if (outstanding_data_.empty()) {
216     // https://tools.ietf.org/html/rfc4960#section-6.3.2
217     // "Whenever all outstanding data sent to an address have been
218     // acknowledged, turn off the T3-rtx timer of that address.
219     // Note: Already stopped in `StopT3RtxTimerOnIncreasedCumulativeTsnAck`."
220   } else {
221     // https://tools.ietf.org/html/rfc4960#section-6.3.2
222     // "Whenever a SACK is received that acknowledges the DATA chunk
223     // with the earliest outstanding TSN for that address, restart the T3-rtx
224     // timer for that address with its current RTO (if there is still
225     // outstanding data on that address)."
226     // "Whenever a SACK is received missing a TSN that was previously
227     // acknowledged via a Gap Ack Block, start the T3-rtx for the destination
228     // address to which the DATA chunk was originally transmitted if it is not
229     // already running."
230     if (!t3_rtx_.is_running()) {
231       t3_rtx_.Start();
232     }
233   }
234 }
235 
IsSackValid(const SackChunk & sack) const236 bool RetransmissionQueue::IsSackValid(const SackChunk& sack) const {
237   // https://tools.ietf.org/html/rfc4960#section-6.2.1
238   // "If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
239   // then drop the SACK.  Since Cumulative TSN Ack is monotonically increasing,
240   // a SACK whose Cumulative TSN Ack is less than the Cumulative TSN Ack Point
241   // indicates an out-of- order SACK."
242   //
243   // Note: Important not to drop SACKs with identical TSN to that previously
244   // received, as the gap ack blocks or dup tsn fields may have changed.
245   UnwrappedTSN cumulative_tsn_ack =
246       tsn_unwrapper_.PeekUnwrap(sack.cumulative_tsn_ack());
247   if (cumulative_tsn_ack < outstanding_data_.last_cumulative_tsn_ack()) {
248     // https://tools.ietf.org/html/rfc4960#section-6.2.1
249     // "If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
250     // then drop the SACK.  Since Cumulative TSN Ack is monotonically
251     // increasing, a SACK whose Cumulative TSN Ack is less than the Cumulative
252     // TSN Ack Point indicates an out-of- order SACK."
253     return false;
254   } else if (cumulative_tsn_ack > outstanding_data_.highest_outstanding_tsn()) {
255     return false;
256   }
257   return true;
258 }
259 
HandleSack(TimeMs now,const SackChunk & sack)260 bool RetransmissionQueue::HandleSack(TimeMs now, const SackChunk& sack) {
261   if (!IsSackValid(sack)) {
262     return false;
263   }
264 
265   UnwrappedTSN old_last_cumulative_tsn_ack =
266       outstanding_data_.last_cumulative_tsn_ack();
267   size_t old_outstanding_bytes = outstanding_data_.outstanding_bytes();
268   size_t old_rwnd = rwnd_;
269   UnwrappedTSN cumulative_tsn_ack =
270       tsn_unwrapper_.Unwrap(sack.cumulative_tsn_ack());
271 
272   if (sack.gap_ack_blocks().empty()) {
273     UpdateRTT(now, cumulative_tsn_ack);
274   }
275 
276   // Exit fast recovery before continuing processing, in case it needs to go
277   // into fast recovery again due to new reported packet loss.
278   MaybeExitFastRecovery(cumulative_tsn_ack);
279 
280   OutstandingData::AckInfo ack_info = outstanding_data_.HandleSack(
281       cumulative_tsn_ack, sack.gap_ack_blocks(), is_in_fast_recovery());
282 
283   // Add lifecycle events for delivered messages.
284   for (LifecycleId lifecycle_id : ack_info.acked_lifecycle_ids) {
285     RTC_DLOG(LS_VERBOSE) << "Triggering OnLifecycleMessageDelivered("
286                          << lifecycle_id.value() << ")";
287     callbacks_.OnLifecycleMessageDelivered(lifecycle_id);
288     callbacks_.OnLifecycleEnd(lifecycle_id);
289   }
290   for (LifecycleId lifecycle_id : ack_info.abandoned_lifecycle_ids) {
291     RTC_DLOG(LS_VERBOSE) << "Triggering OnLifecycleMessageExpired("
292                          << lifecycle_id.value() << ", true)";
293     callbacks_.OnLifecycleMessageExpired(lifecycle_id,
294                                          /*maybe_delivered=*/true);
295     callbacks_.OnLifecycleEnd(lifecycle_id);
296   }
297 
298   // Update of outstanding_data_ is now done. Congestion control remains.
299   UpdateReceiverWindow(sack.a_rwnd());
300 
301   RTC_DLOG(LS_VERBOSE) << log_prefix_ << "Received SACK, cum_tsn_ack="
302                        << *cumulative_tsn_ack.Wrap() << " ("
303                        << *old_last_cumulative_tsn_ack.Wrap()
304                        << "), outstanding_bytes="
305                        << outstanding_data_.outstanding_bytes() << " ("
306                        << old_outstanding_bytes << "), rwnd=" << rwnd_ << " ("
307                        << old_rwnd << ")";
308 
309   if (cumulative_tsn_ack > old_last_cumulative_tsn_ack) {
310     // https://tools.ietf.org/html/rfc4960#section-6.3.2
311     // "Whenever a SACK is received that acknowledges the DATA chunk
312     // with the earliest outstanding TSN for that address, restart the T3-rtx
313     // timer for that address with its current RTO (if there is still
314     // outstanding data on that address)."
315     // Note: It may be started again in a bit further down.
316     t3_rtx_.Stop();
317 
318     HandleIncreasedCumulativeTsnAck(old_outstanding_bytes,
319                                     ack_info.bytes_acked);
320   }
321 
322   if (ack_info.has_packet_loss) {
323     HandlePacketLoss(ack_info.highest_tsn_acked);
324   }
325 
326   // https://tools.ietf.org/html/rfc4960#section-8.2
327   // "When an outstanding TSN is acknowledged [...] the endpoint shall clear
328   // the error counter ..."
329   if (ack_info.bytes_acked > 0) {
330     on_clear_retransmission_counter_();
331   }
332 
333   StartT3RtxTimerIfOutstandingData();
334   RTC_DCHECK(IsConsistent());
335   return true;
336 }
337 
UpdateRTT(TimeMs now,UnwrappedTSN cumulative_tsn_ack)338 void RetransmissionQueue::UpdateRTT(TimeMs now,
339                                     UnwrappedTSN cumulative_tsn_ack) {
340   // RTT updating is flawed in SCTP, as explained in e.g. Pedersen J, Griwodz C,
341   // Halvorsen P (2006) Considerations of SCTP retransmission delays for thin
342   // streams.
343   // Due to delayed acknowledgement, the SACK may be sent much later which
344   // increases the calculated RTT.
345   // TODO(boivie): Consider occasionally sending DATA chunks with I-bit set and
346   // use only those packets for measurement.
347 
348   absl::optional<DurationMs> rtt =
349       outstanding_data_.MeasureRTT(now, cumulative_tsn_ack);
350 
351   if (rtt.has_value()) {
352     on_new_rtt_(*rtt);
353   }
354 }
355 
HandleT3RtxTimerExpiry()356 void RetransmissionQueue::HandleT3RtxTimerExpiry() {
357   size_t old_cwnd = cwnd_;
358   size_t old_outstanding_bytes = outstanding_bytes();
359   // https://tools.ietf.org/html/rfc4960#section-6.3.3
360   // "For the destination address for which the timer expires, adjust
361   // its ssthresh with rules defined in Section 7.2.3 and set the cwnd <- MTU."
362   ssthresh_ = std::max(cwnd_ / 2, 4 * options_.mtu);
363   cwnd_ = 1 * options_.mtu;
364   // Errata: https://datatracker.ietf.org/doc/html/rfc8540#section-3.11
365   partial_bytes_acked_ = 0;
366 
367   // https://tools.ietf.org/html/rfc4960#section-6.3.3
368   // "For the destination address for which the timer expires, set RTO
369   // <- RTO * 2 ("back off the timer").  The maximum value discussed in rule C7
370   // above (RTO.max) may be used to provide an upper bound to this doubling
371   // operation."
372 
373   // Already done by the Timer implementation.
374 
375   // https://tools.ietf.org/html/rfc4960#section-6.3.3
376   // "Determine how many of the earliest (i.e., lowest TSN) outstanding
377   // DATA chunks for the address for which the T3-rtx has expired will fit into
378   // a single packet"
379 
380   // https://tools.ietf.org/html/rfc4960#section-6.3.3
381   // "Note: Any DATA chunks that were sent to the address for which the
382   // T3-rtx timer expired but did not fit in one MTU (rule E3 above) should be
383   // marked for retransmission and sent as soon as cwnd allows (normally, when a
384   // SACK arrives)."
385   outstanding_data_.NackAll();
386 
387   // https://tools.ietf.org/html/rfc4960#section-6.3.3
388   // "Start the retransmission timer T3-rtx on the destination address
389   // to which the retransmission is sent, if rule R1 above indicates to do so."
390 
391   // Already done by the Timer implementation.
392 
393   RTC_DLOG(LS_INFO) << log_prefix_ << "t3-rtx expired. new cwnd=" << cwnd_
394                     << " (" << old_cwnd << "), ssthresh=" << ssthresh_
395                     << ", outstanding_bytes " << outstanding_bytes() << " ("
396                     << old_outstanding_bytes << ")";
397   RTC_DCHECK(IsConsistent());
398 }
399 
400 std::vector<std::pair<TSN, Data>>
GetChunksForFastRetransmit(size_t bytes_in_packet)401 RetransmissionQueue::GetChunksForFastRetransmit(size_t bytes_in_packet) {
402   RTC_DCHECK(outstanding_data_.has_data_to_be_fast_retransmitted());
403   RTC_DCHECK(IsDivisibleBy4(bytes_in_packet));
404   std::vector<std::pair<TSN, Data>> to_be_sent;
405   size_t old_outstanding_bytes = outstanding_bytes();
406 
407   to_be_sent =
408       outstanding_data_.GetChunksToBeFastRetransmitted(bytes_in_packet);
409   RTC_DCHECK(!to_be_sent.empty());
410 
411   // https://tools.ietf.org/html/rfc4960#section-7.2.4
412   // "4)  Restart the T3-rtx timer only if ... the endpoint is retransmitting
413   // the first outstanding DATA chunk sent to that address."
414   if (to_be_sent[0].first ==
415       outstanding_data_.last_cumulative_tsn_ack().next_value().Wrap()) {
416     RTC_DLOG(LS_VERBOSE)
417         << log_prefix_
418         << "First outstanding DATA to be retransmitted - restarting T3-RTX";
419     t3_rtx_.Stop();
420   }
421 
422   // https://tools.ietf.org/html/rfc4960#section-6.3.2
423   // "Every time a DATA chunk is sent to any address (including a
424   // retransmission), if the T3-rtx timer of that address is not running,
425   // start it running so that it will expire after the RTO of that address."
426   if (!t3_rtx_.is_running()) {
427     t3_rtx_.Start();
428   }
429   RTC_DLOG(LS_VERBOSE) << log_prefix_ << "Fast-retransmitting TSN "
430                        << StrJoin(to_be_sent, ",",
431                                   [&](rtc::StringBuilder& sb,
432                                       const std::pair<TSN, Data>& c) {
433                                     sb << *c.first;
434                                   })
435                        << " - "
436                        << absl::c_accumulate(
437                               to_be_sent, 0,
438                               [&](size_t r, const std::pair<TSN, Data>& d) {
439                                 return r + GetSerializedChunkSize(d.second);
440                               })
441                        << " bytes. outstanding_bytes=" << outstanding_bytes()
442                        << " (" << old_outstanding_bytes << ")";
443 
444   RTC_DCHECK(IsConsistent());
445   return to_be_sent;
446 }
447 
GetChunksToSend(TimeMs now,size_t bytes_remaining_in_packet)448 std::vector<std::pair<TSN, Data>> RetransmissionQueue::GetChunksToSend(
449     TimeMs now,
450     size_t bytes_remaining_in_packet) {
451   // Chunks are always padded to even divisible by four.
452   RTC_DCHECK(IsDivisibleBy4(bytes_remaining_in_packet));
453 
454   std::vector<std::pair<TSN, Data>> to_be_sent;
455   size_t old_outstanding_bytes = outstanding_bytes();
456   size_t old_rwnd = rwnd_;
457 
458   // Calculate the bandwidth budget (how many bytes that is
459   // allowed to be sent), and fill that up first with chunks that are
460   // scheduled to be retransmitted. If there is still budget, send new chunks
461   // (which will have their TSN assigned here.)
462   size_t max_bytes =
463       RoundDownTo4(std::min(max_bytes_to_send(), bytes_remaining_in_packet));
464 
465   to_be_sent = outstanding_data_.GetChunksToBeRetransmitted(max_bytes);
466   max_bytes -= absl::c_accumulate(to_be_sent, 0,
467                                   [&](size_t r, const std::pair<TSN, Data>& d) {
468                                     return r + GetSerializedChunkSize(d.second);
469                                   });
470 
471   while (max_bytes > data_chunk_header_size_) {
472     RTC_DCHECK(IsDivisibleBy4(max_bytes));
473     absl::optional<SendQueue::DataToSend> chunk_opt =
474         send_queue_.Produce(now, max_bytes - data_chunk_header_size_);
475     if (!chunk_opt.has_value()) {
476       break;
477     }
478 
479     size_t chunk_size = GetSerializedChunkSize(chunk_opt->data);
480     max_bytes -= chunk_size;
481     rwnd_ -= chunk_size;
482 
483     absl::optional<UnwrappedTSN> tsn = outstanding_data_.Insert(
484         chunk_opt->data, now,
485         partial_reliability_ ? chunk_opt->max_retransmissions
486                              : MaxRetransmits::NoLimit(),
487         partial_reliability_ ? chunk_opt->expires_at : TimeMs::InfiniteFuture(),
488         chunk_opt->lifecycle_id);
489 
490     if (tsn.has_value()) {
491       if (chunk_opt->lifecycle_id.IsSet()) {
492         RTC_DCHECK(chunk_opt->data.is_end);
493         callbacks_.OnLifecycleMessageFullySent(chunk_opt->lifecycle_id);
494       }
495       to_be_sent.emplace_back(tsn->Wrap(), std::move(chunk_opt->data));
496     }
497   }
498 
499   if (!to_be_sent.empty()) {
500     // https://tools.ietf.org/html/rfc4960#section-6.3.2
501     // "Every time a DATA chunk is sent to any address (including a
502     // retransmission), if the T3-rtx timer of that address is not running,
503     // start it running so that it will expire after the RTO of that address."
504     if (!t3_rtx_.is_running()) {
505       t3_rtx_.Start();
506     }
507     RTC_DLOG(LS_VERBOSE) << log_prefix_ << "Sending TSN "
508                          << StrJoin(to_be_sent, ",",
509                                     [&](rtc::StringBuilder& sb,
510                                         const std::pair<TSN, Data>& c) {
511                                       sb << *c.first;
512                                     })
513                          << " - "
514                          << absl::c_accumulate(
515                                 to_be_sent, 0,
516                                 [&](size_t r, const std::pair<TSN, Data>& d) {
517                                   return r + GetSerializedChunkSize(d.second);
518                                 })
519                          << " bytes. outstanding_bytes=" << outstanding_bytes()
520                          << " (" << old_outstanding_bytes << "), cwnd=" << cwnd_
521                          << ", rwnd=" << rwnd_ << " (" << old_rwnd << ")";
522   }
523   RTC_DCHECK(IsConsistent());
524   return to_be_sent;
525 }
526 
can_send_data() const527 bool RetransmissionQueue::can_send_data() const {
528   return cwnd_ < options_.avoid_fragmentation_cwnd_mtus * options_.mtu ||
529          max_bytes_to_send() >= min_bytes_required_to_send_;
530 }
531 
ShouldSendForwardTsn(TimeMs now)532 bool RetransmissionQueue::ShouldSendForwardTsn(TimeMs now) {
533   if (!partial_reliability_) {
534     return false;
535   }
536   outstanding_data_.ExpireOutstandingChunks(now);
537   bool ret = outstanding_data_.ShouldSendForwardTsn();
538   RTC_DCHECK(IsConsistent());
539   return ret;
540 }
541 
max_bytes_to_send() const542 size_t RetransmissionQueue::max_bytes_to_send() const {
543   size_t left = outstanding_bytes() >= cwnd_ ? 0 : cwnd_ - outstanding_bytes();
544 
545   if (outstanding_bytes() == 0) {
546     // https://datatracker.ietf.org/doc/html/rfc4960#section-6.1
547     // ... However, regardless of the value of rwnd (including if it is 0), the
548     // data sender can always have one DATA chunk in flight to the receiver if
549     // allowed by cwnd (see rule B, below).
550     return left;
551   }
552 
553   return std::min(rwnd(), left);
554 }
555 
PrepareResetStream(StreamID stream_id)556 void RetransmissionQueue::PrepareResetStream(StreamID stream_id) {
557   // TODO(boivie): These calls are now only affecting the send queue. The
558   // packet buffer can also change behavior - for example draining the chunk
559   // producer and eagerly assign TSNs so that an "Outgoing SSN Reset Request"
560   // can be sent quickly, with a known `sender_last_assigned_tsn`.
561   send_queue_.PrepareResetStream(stream_id);
562 }
HasStreamsReadyToBeReset() const563 bool RetransmissionQueue::HasStreamsReadyToBeReset() const {
564   return send_queue_.HasStreamsReadyToBeReset();
565 }
CommitResetStreams()566 void RetransmissionQueue::CommitResetStreams() {
567   send_queue_.CommitResetStreams();
568 }
RollbackResetStreams()569 void RetransmissionQueue::RollbackResetStreams() {
570   send_queue_.RollbackResetStreams();
571 }
572 
GetHandoverReadiness() const573 HandoverReadinessStatus RetransmissionQueue::GetHandoverReadiness() const {
574   HandoverReadinessStatus status;
575   if (!outstanding_data_.empty()) {
576     status.Add(HandoverUnreadinessReason::kRetransmissionQueueOutstandingData);
577   }
578   if (fast_recovery_exit_tsn_.has_value()) {
579     status.Add(HandoverUnreadinessReason::kRetransmissionQueueFastRecovery);
580   }
581   if (outstanding_data_.has_data_to_be_retransmitted()) {
582     status.Add(HandoverUnreadinessReason::kRetransmissionQueueNotEmpty);
583   }
584   return status;
585 }
586 
AddHandoverState(DcSctpSocketHandoverState & state)587 void RetransmissionQueue::AddHandoverState(DcSctpSocketHandoverState& state) {
588   state.tx.next_tsn = next_tsn().value();
589   state.tx.rwnd = rwnd_;
590   state.tx.cwnd = cwnd_;
591   state.tx.ssthresh = ssthresh_;
592   state.tx.partial_bytes_acked = partial_bytes_acked_;
593 }
594 
RestoreFromState(const DcSctpSocketHandoverState & state)595 void RetransmissionQueue::RestoreFromState(
596     const DcSctpSocketHandoverState& state) {
597   // Validate that the component is in pristine state.
598   RTC_DCHECK(outstanding_data_.empty());
599   RTC_DCHECK(!t3_rtx_.is_running());
600   RTC_DCHECK(partial_bytes_acked_ == 0);
601 
602   cwnd_ = state.tx.cwnd;
603   rwnd_ = state.tx.rwnd;
604   ssthresh_ = state.tx.ssthresh;
605   partial_bytes_acked_ = state.tx.partial_bytes_acked;
606 
607   outstanding_data_.ResetSequenceNumbers(
608       tsn_unwrapper_.Unwrap(TSN(state.tx.next_tsn)),
609       tsn_unwrapper_.Unwrap(TSN(state.tx.next_tsn - 1)));
610 }
611 }  // namespace dcsctp
612