dcsctp: Add Transmission Control Block

This is merely a container of components that have their lifetime
bound to when the socket is connected. If the socket gets disconnected
or restarted, this object (and everything it holds) will be released.

Bug: webrtc:12614
Change-Id: Ibd75760b7bf7efe9c26c4eb7cee62de8bba5410c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/214340
Commit-Queue: Victor Boivie <boivie@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33869}
This commit is contained in:
Victor Boivie 2021-04-07 08:57:28 +02:00 committed by WebRTC LUCI CQ
parent abc07c9b95
commit 21509566b8
5 changed files with 408 additions and 0 deletions

View File

@ -35,6 +35,9 @@ using ReconfigRequestSN = StrongAlias<class ReconfigRequestSNTag, uint32_t>;
// Verification Tag, used for packet validation.
using VerificationTag = StrongAlias<class VerificationTagTag, uint32_t>;
// Tie Tag, used as a nonce when connecting.
using TieTag = StrongAlias<class TieTagTag, uint64_t>;
// Hasher for separated ordered/unordered stream identifiers.
struct UnorderedStreamHash {
size_t operator()(const std::pair<IsUnordered, StreamID>& p) const {

View File

@ -48,6 +48,34 @@ rtc_library("stream_reset_handler") {
]
}
rtc_library("transmission_control_block") {
deps = [
":context",
":heartbeat_handler",
":stream_reset_handler",
"../../../api:array_view",
"../../../rtc_base",
"../../../rtc_base:checks",
"../../../rtc_base:rtc_base_approved",
"../common:sequence_numbers",
"../packet:sctp_packet",
"../public:socket",
"../public:types",
"../rx:data_tracker",
"../rx:reassembly_queue",
"../timer:timer",
"../tx:retransmission_error_counter",
"../tx:retransmission_queue",
"../tx:retransmission_timeout",
"../tx:send_queue",
]
sources = [
"capabilities.h",
"transmission_control_block.cc",
"transmission_control_block.h",
]
}
if (rtc_include_tests) {
rtc_source_set("mock_callbacks") {
testonly = true

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef NET_DCSCTP_SOCKET_CAPABILITIES_H_
#define NET_DCSCTP_SOCKET_CAPABILITIES_H_
namespace dcsctp {
// Indicates what the association supports, meaning that both parties
// support it and that feature can be used.
struct Capabilities {
// RFC3758 Partial Reliability Extension
bool partial_reliability = false;
// RFC8260 Stream Schedulers and User Message Interleaving
bool message_interleaving = false;
// RFC6525 Stream Reconfiguration
bool reconfig = false;
};
} // namespace dcsctp
#endif // NET_DCSCTP_SOCKET_CAPABILITIES_H_

View File

@ -0,0 +1,150 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "net/dcsctp/socket/transmission_control_block.h"
#include <algorithm>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/types/optional.h"
#include "net/dcsctp/packet/chunk/data_chunk.h"
#include "net/dcsctp/packet/chunk/forward_tsn_chunk.h"
#include "net/dcsctp/packet/chunk/idata_chunk.h"
#include "net/dcsctp/packet/chunk/iforward_tsn_chunk.h"
#include "net/dcsctp/packet/chunk/reconfig_chunk.h"
#include "net/dcsctp/packet/chunk/sack_chunk.h"
#include "net/dcsctp/packet/sctp_packet.h"
#include "net/dcsctp/public/dcsctp_options.h"
#include "net/dcsctp/rx/data_tracker.h"
#include "net/dcsctp/rx/reassembly_queue.h"
#include "net/dcsctp/socket/capabilities.h"
#include "net/dcsctp/socket/stream_reset_handler.h"
#include "net/dcsctp/timer/timer.h"
#include "net/dcsctp/tx/retransmission_queue.h"
#include "net/dcsctp/tx/retransmission_timeout.h"
#include "rtc_base/logging.h"
#include "rtc_base/strings/string_builder.h"
namespace dcsctp {
void TransmissionControlBlock::ObserveRTT(DurationMs rtt) {
DurationMs prev_rto = rto_.rto();
rto_.ObserveRTT(rtt);
RTC_DLOG(LS_VERBOSE) << log_prefix_ << "new rtt=" << *rtt
<< ", srtt=" << *rto_.srtt() << ", rto=" << *rto_.rto()
<< " (" << *prev_rto << ")";
t3_rtx_->set_duration(rto_.rto());
DurationMs delayed_ack_tmo =
std::min(rto_.rto() * 0.5, options_.delayed_ack_max_timeout);
delayed_ack_timer_->set_duration(delayed_ack_tmo);
}
absl::optional<DurationMs> TransmissionControlBlock::OnRtxTimerExpiry() {
RTC_DLOG(LS_INFO) << log_prefix_ << "Timer " << t3_rtx_->name()
<< " has expired";
if (IncrementTxErrorCounter("t3-rtx expired")) {
retransmission_queue_.HandleT3RtxTimerExpiry();
SendBufferedPackets();
}
return absl::nullopt;
}
absl::optional<DurationMs> TransmissionControlBlock::OnDelayedAckTimerExpiry() {
data_tracker_.HandleDelayedAckTimerExpiry();
MaybeSendSack();
return absl::nullopt;
}
void TransmissionControlBlock::MaybeSendSack() {
if (data_tracker_.ShouldSendAck(/*also_if_delayed=*/false)) {
SctpPacket::Builder builder = PacketBuilder();
builder.Add(
data_tracker_.CreateSelectiveAck(reassembly_queue_.remaining_bytes()));
Send(builder);
}
}
void TransmissionControlBlock::SendBufferedPackets(SctpPacket::Builder& builder,
bool only_one_packet) {
TimeMs now = callbacks_.TimeMillis();
for (int packet_idx = 0;; ++packet_idx) {
// Only add control chunks to the first packet that is sent, if sending
// multiple packets in one go (as allowed by the congestion window).
if (packet_idx == 0) {
// https://tools.ietf.org/html/rfc4960#section-6
// "Before an endpoint transmits a DATA chunk, if any received DATA
// chunks have not been acknowledged (e.g., due to delayed ack), the
// sender should create a SACK and bundle it with the outbound DATA chunk,
// as long as the size of the final SCTP packet does not exceed the
// current MTU."
if (data_tracker_.ShouldSendAck(/*also_if_delayed=*/true)) {
builder.Add(data_tracker_.CreateSelectiveAck(
reassembly_queue_.remaining_bytes()));
}
if (retransmission_queue_.ShouldSendForwardTsn(now)) {
if (capabilities_.message_interleaving) {
builder.Add(retransmission_queue_.CreateIForwardTsn());
} else {
builder.Add(retransmission_queue_.CreateForwardTsn());
}
}
absl::optional<ReConfigChunk> reconfig =
stream_reset_handler_.MakeStreamResetRequest();
if (reconfig.has_value()) {
builder.Add(*reconfig);
}
}
auto chunks =
retransmission_queue_.GetChunksToSend(now, builder.bytes_remaining());
for (auto& elem : chunks) {
TSN tsn = elem.first;
Data data = std::move(elem.second);
if (capabilities_.message_interleaving) {
builder.Add(IDataChunk(tsn, std::move(data), false));
} else {
builder.Add(DataChunk(tsn, std::move(data), false));
}
}
if (builder.empty()) {
break;
}
Send(builder);
if (only_one_packet) {
break;
}
}
}
std::string TransmissionControlBlock::ToString() const {
rtc::StringBuilder sb;
sb.AppendFormat(
"verification_tag=%08x, last_cumulative_ack=%u, capabilities=",
*peer_verification_tag_, *data_tracker_.last_cumulative_acked_tsn());
if (capabilities_.partial_reliability) {
sb << "PR,";
}
if (capabilities_.message_interleaving) {
sb << "IL,";
}
if (capabilities_.reconfig) {
sb << "Reconfig,";
}
return sb.Release();
}
} // namespace dcsctp

View File

@ -0,0 +1,201 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef NET_DCSCTP_SOCKET_TRANSMISSION_CONTROL_BLOCK_H_
#define NET_DCSCTP_SOCKET_TRANSMISSION_CONTROL_BLOCK_H_
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/strings/string_view.h"
#include "net/dcsctp/common/sequence_numbers.h"
#include "net/dcsctp/packet/sctp_packet.h"
#include "net/dcsctp/public/dcsctp_options.h"
#include "net/dcsctp/public/dcsctp_socket.h"
#include "net/dcsctp/rx/data_tracker.h"
#include "net/dcsctp/rx/reassembly_queue.h"
#include "net/dcsctp/socket/capabilities.h"
#include "net/dcsctp/socket/context.h"
#include "net/dcsctp/socket/heartbeat_handler.h"
#include "net/dcsctp/socket/stream_reset_handler.h"
#include "net/dcsctp/timer/timer.h"
#include "net/dcsctp/tx/retransmission_error_counter.h"
#include "net/dcsctp/tx/retransmission_queue.h"
#include "net/dcsctp/tx/retransmission_timeout.h"
#include "net/dcsctp/tx/send_queue.h"
namespace dcsctp {
// The TransmissionControlBlock (TCB) represents an open connection to a peer,
// and holds all the resources for that. If the connection is e.g. shutdown,
// closed or restarted, this object will be deleted and/or replaced.
class TransmissionControlBlock : public Context {
public:
TransmissionControlBlock(TimerManager& timer_manager,
absl::string_view log_prefix,
const DcSctpOptions& options,
const Capabilities& capabilities,
DcSctpSocketCallbacks& callbacks,
SendQueue& send_queue,
VerificationTag my_verification_tag,
TSN my_initial_tsn,
VerificationTag peer_verification_tag,
TSN peer_initial_tsn,
size_t a_rwnd,
TieTag tie_tag,
std::function<bool()> is_connection_established,
std::function<void(SctpPacket::Builder&)> send_fn)
: log_prefix_(log_prefix),
options_(options),
timer_manager_(timer_manager),
capabilities_(capabilities),
callbacks_(callbacks),
t3_rtx_(timer_manager_.CreateTimer(
"t3-rtx",
[this]() { return OnRtxTimerExpiry(); },
TimerOptions(options.rto_initial))),
delayed_ack_timer_(timer_manager_.CreateTimer(
"delayed-ack",
[this]() { return OnDelayedAckTimerExpiry(); },
TimerOptions(options.delayed_ack_max_timeout,
TimerBackoffAlgorithm::kExponential,
/*max_restarts=*/0))),
my_verification_tag_(my_verification_tag),
my_initial_tsn_(my_initial_tsn),
peer_verification_tag_(peer_verification_tag),
peer_initial_tsn_(peer_initial_tsn),
tie_tag_(tie_tag),
is_connection_established_(std::move(is_connection_established)),
send_fn_(std::move(send_fn)),
rto_(options),
tx_error_counter_(log_prefix, options),
data_tracker_(log_prefix, delayed_ack_timer_.get(), peer_initial_tsn),
reassembly_queue_(log_prefix,
peer_initial_tsn,
options.max_receiver_window_buffer_size),
retransmission_queue_(
log_prefix,
my_initial_tsn,
a_rwnd,
send_queue,
[this](DurationMs rtt) { return ObserveRTT(rtt); },
[this]() { callbacks_.NotifyOutgoingMessageBufferEmpty(); },
[this]() { tx_error_counter_.Clear(); },
*t3_rtx_,
options,
capabilities.partial_reliability,
capabilities.message_interleaving),
stream_reset_handler_(log_prefix,
this,
&timer_manager,
&data_tracker_,
&reassembly_queue_,
&retransmission_queue_),
heartbeat_handler_(log_prefix, options, this, &timer_manager_) {}
// Implementation of `Context`.
bool is_connection_established() const override {
return is_connection_established_();
}
TSN my_initial_tsn() const override { return my_initial_tsn_; }
TSN peer_initial_tsn() const override { return peer_initial_tsn_; }
DcSctpSocketCallbacks& callbacks() const override { return callbacks_; }
void ObserveRTT(DurationMs rtt) override;
DurationMs current_rto() const override { return rto_.rto(); }
bool IncrementTxErrorCounter(absl::string_view reason) override {
return tx_error_counter_.Increment(reason);
}
void ClearTxErrorCounter() override { tx_error_counter_.Clear(); }
SctpPacket::Builder PacketBuilder() const override {
return SctpPacket::Builder(peer_verification_tag_, options_);
}
bool HasTooManyTxErrors() const override {
return tx_error_counter_.IsExhausted();
}
void Send(SctpPacket::Builder& builder) override { send_fn_(builder); }
// Other accessors
DataTracker& data_tracker() { return data_tracker_; }
ReassemblyQueue& reassembly_queue() { return reassembly_queue_; }
RetransmissionQueue& retransmission_queue() { return retransmission_queue_; }
StreamResetHandler& stream_reset_handler() { return stream_reset_handler_; }
HeartbeatHandler& heartbeat_handler() { return heartbeat_handler_; }
// Returns this socket's verification tag, set in all packet headers.
VerificationTag my_verification_tag() const { return my_verification_tag_; }
// Returns the peer's verification tag, which should be in received packets.
VerificationTag peer_verification_tag() const {
return peer_verification_tag_;
}
// All negotiated supported capabilities.
const Capabilities& capabilities() const { return capabilities_; }
// A 64-bit tie-tag, used to e.g. detect reconnections.
TieTag tie_tag() const { return tie_tag_; }
// Sends a SACK, if there is a need to.
void MaybeSendSack();
// Fills `builder` (which may already be filled with control chunks) with
// with other control and data chunks, and sends packets as much as can be
// allowed by the congestion control algorithm. If `only_one_packet` is true,
// only a single packet will be sent. Otherwise, zero, one or multiple may be
// sent.
void SendBufferedPackets(SctpPacket::Builder& builder,
bool only_one_packet = false);
// As above, but without passing in a builder and allowing sending many
// packets.
void SendBufferedPackets() {
SctpPacket::Builder builder(peer_verification_tag_, options_);
SendBufferedPackets(builder, /*only_one_packet=*/false);
}
// Returns a textual representation of this object, for logging.
std::string ToString() const;
private:
// Will be called when the retransmission timer (t3-rtx) expires.
absl::optional<DurationMs> OnRtxTimerExpiry();
// Will be called when the delayed ack timer expires.
absl::optional<DurationMs> OnDelayedAckTimerExpiry();
const std::string log_prefix_;
const DcSctpOptions options_;
TimerManager& timer_manager_;
// Negotiated capabilities that both peers support.
const Capabilities capabilities_;
DcSctpSocketCallbacks& callbacks_;
// The data retransmission timer, called t3-rtx in SCTP.
const std::unique_ptr<Timer> t3_rtx_;
// Delayed ack timer, which triggers when acks should be sent (when delayed).
const std::unique_ptr<Timer> delayed_ack_timer_;
const VerificationTag my_verification_tag_;
const TSN my_initial_tsn_;
const VerificationTag peer_verification_tag_;
const TSN peer_initial_tsn_;
// Nonce, used to detect reconnections.
const TieTag tie_tag_;
const std::function<bool()> is_connection_established_;
const std::function<void(SctpPacket::Builder&)> send_fn_;
RetransmissionTimeout rto_;
RetransmissionErrorCounter tx_error_counter_;
DataTracker data_tracker_;
ReassemblyQueue reassembly_queue_;
RetransmissionQueue retransmission_queue_;
StreamResetHandler stream_reset_handler_;
HeartbeatHandler heartbeat_handler_;
};
} // namespace dcsctp
#endif // NET_DCSCTP_SOCKET_TRANSMISSION_CONTROL_BLOCK_H_