Flow 1.0.2
Flow project: Full implementation reference.
cong_ctl.cpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
21
22namespace flow::net_flow
23{
24
25// Class Congestion_control_strategy.
26
27// Implementations.
28
30 Log_context(logger_ptr, Flow_log_component::S_NET_FLOW_CC),
31 m_sock(sock) // Get a weak_ptr from a shared_ptr.
32{
33 assert(sock);
34}
35
36void Congestion_control_strategy::on_acks(size_t, size_t) // Virtual.
37{
38 // The vacuous congestion control module just ignores acknowledgments.
39}
40
42 // Virtual.
43{
44 // The vacuous congestion control module just ignores loss.
45}
46
48 // Virtual.
49{
50 // The vacuous congestion control module just ignores loss.
51}
52
54{
55 // The vacuous congestion control module just ignores idleness.
56}
57
58void Congestion_control_strategy::on_individual_ack(const Fine_duration&, size_t, size_t) // Virtual.
59{
60 // The vacuous congestion control module just ignores individual RTTs. (Some actual strategies do too.)
61}
62
64{
65 const Peer_socket::Const_ptr sock = m_sock.lock();
66
67 /* This trips only if Node violates the "Peer_socket exists while Congestion_control_strategy
68 * exists" rule. This assert() is the only reason sock is a weak_ptr<const Peer_socket> instead
69 * of a "const Peer_socket*". Without that we couldn't perform the assert() and would just
70 * crash later. */
71 assert(sock);
72
73 return sock;
74}
75
76// Class Congestion_control_selector.
77
78// Static initializations.
79
80const std::map<std::string, Congestion_control_selector::Strategy_choice>
82 ({
83 { "classic",
84 Strategy_choice::S_CLASSIC },
85 { "classic_with_bandwidth_est",
86 Strategy_choice::S_CLASSIC_BANDWIDTH_ESTIMATED }
87 });
88// Annoying we have to copy/paste/flip S_ID_TO_STRATEGY_MAP, but it's not that bad.
89const std::map<Congestion_control_selector::Strategy_choice, std::string>
91 ({
92 { Strategy_choice::S_CLASSIC,
93 "classic" },
94 { Strategy_choice::S_CLASSIC_BANDWIDTH_ESTIMATED,
95 "classic_with_bandwidth_est" }
96 });
97
98// Methods.
99
101 (Strategy_choice strategy_choice,
102 log::Logger* logger_ptr, Peer_socket::Const_ptr sock) // Static
103{
104 switch (strategy_choice)
105 {
107 return new Congestion_control_classic(logger_ptr, sock);
109 return new Congestion_control_classic_with_bandwidth_est(logger_ptr, sock);
110 }
111 assert(false);
112 return 0;
113}
114
115void Congestion_control_selector::get_ids(std::vector<std::string>* ids) // Static.
116{
117 ids->clear();
118 for (const auto& id_and_strategy : S_ID_TO_STRATEGY_MAP)
119 {
120 ids->push_back(id_and_strategy.first);
121 }
122}
123
124std::istream& operator>>(std::istream& is, Congestion_control_selector::Strategy_choice& strategy_choice)
125{
126 /* It's slightly weird, but the prototype for this is in options.hpp, so that it is available
127 * to the user. But the implementation is here, since we need access to Congestion_control_selector....
128 * @todo It's irregular but seems OK. Revisit. */
129
130 using std::string;
131
132 // Read a token (space-delimited word) from the input stream into the string.
133
134 string id;
135 is >> id;
136 // If there was nothing left in stream, or other error, is.good() is false now. Not our problem.
137
138 // Try to map that token to the known IDs. If unknown, choose the default (Reno for now).
140 strategy_choice = (it == Congestion_control_selector::S_ID_TO_STRATEGY_MAP.end())
142 : it->second;
143 return is;
144}
145
146std::ostream& operator<<(std::ostream& os, const Congestion_control_selector::Strategy_choice& strategy_choice)
147{
148 // See comment at top of operator<<().
149
150 // Just print the text ID of the given enum value.
151 auto it = Congestion_control_selector::S_STRATEGY_TO_ID_MAP.find(strategy_choice);
153
154 return os << it->second;
155}
156
157} // namespace flow::net_flow
Interface that the user should implement, passing the implementing Logger into logging classes (Flow'...
Definition: log.hpp:1291
Classic congestion control but with backoff to bandwidth estimate-based pipe size.
Classic congestion control, based on Reno (TCP RFC 5681), with congestion avoidance,...
static Congestion_control_strategy * create_strategy(Strategy_choice strategy_choice, log::Logger *logger_ptr, Peer_socket::Const_ptr sock)
Factory method that, given an enum identifying the desired strategy, allocates the appropriate Conges...
Definition: cong_ctl.cpp:101
static const std::map< Strategy_choice, std::string > S_STRATEGY_TO_ID_MAP
The inverse of S_ID_TO_STRATEGY_MAP.
Definition: cong_ctl.hpp:498
static const std::map< std::string, Strategy_choice > S_ID_TO_STRATEGY_MAP
Maps each ID to the corresponding Strategy_choice enum value.
Definition: cong_ctl.hpp:496
static void get_ids(std::vector< std::string > *ids)
Returns a list of strings, called IDs, each of which textually represents a distinct Congestion_contr...
Definition: cong_ctl.cpp:115
The abstract interface for a per-socket module that determines the socket's congestion control behavi...
Definition: cong_ctl.hpp:180
Congestion_control_strategy(log::Logger *logger_ptr, Peer_socket::Const_ptr sock)
Constructs object by setting up logging and saving a pointer to the containing Peer_socket.
Definition: cong_ctl.cpp:29
boost::weak_ptr< Peer_socket::Const_ptr::element_type > m_sock
The containing socket (read-only access).
Definition: cong_ctl.hpp:433
virtual void on_loss_event(size_t bytes, size_t packets)
Informs the congestion control strategy that 1 or more previously sent packets whose status was In-fl...
Definition: cong_ctl.cpp:41
Peer_socket::Const_ptr socket() const
Utility for subclasses that returns a handle to the containing Peer_socket.
Definition: cong_ctl.cpp:63
virtual void on_idle_timeout()
Informs the congestion control strategy that Node considers the connection to be "idle" by virtue of ...
Definition: cong_ctl.cpp:53
virtual void on_acks(size_t bytes, size_t packets)
Informs the congestion control strategy that 1 or more previously sent packets whose status was In-fl...
Definition: cong_ctl.cpp:36
virtual void on_individual_ack(const Fine_duration &packet_rtt, const size_t bytes, const size_t sent_cwnd_bytes)
Informs the congestion control strategy that exactly 1 previously sent packet whose status was In-fli...
Definition: cong_ctl.cpp:58
virtual void on_drop_timeout(size_t bytes, size_t packets)
Informs the congestion control strategy that 1 or more previously sent packets whose status was In-fl...
Definition: cong_ctl.cpp:47
Const_target_ptr Const_ptr
Short-hand for ref-counted pointer to immutable values of type Target_type::element_type (a-la T cons...
Flow module containing the API and implementation of the Flow network protocol, a TCP-inspired stream...
Definition: node.cpp:25
std::istream & operator>>(std::istream &is, Congestion_control_selector::Strategy_choice &strategy_choice)
Deserializes a Peer_socket_options::Congestion_control_strategy_choice enum from a standard input str...
Definition: cong_ctl.cpp:124
std::ostream & operator<<(std::ostream &os, const Congestion_control_selector::Strategy_choice &strategy_choice)
Serializes a Peer_socket_options::Congestion_control_strategy_choice enum to a standard ostream – the...
Definition: cong_ctl.cpp:146
Flow_log_component
The flow::log::Component payload enumeration comprising various log components used by Flow's own int...
Definition: common.hpp:633
Fine_clock::duration Fine_duration
A high-res time duration as computed from two Fine_time_pts.
Definition: common.hpp:411
Congestion_control_strategy_choice
The possible choices for congestion control strategy for the socket.
Definition: options.hpp:63
@ S_CLASSIC
Classic (Reno-style) AIMD congestion control.
@ S_CLASSIC_BANDWIDTH_ESTIMATED
Classic congestion control but with loss resulting in a window chosen from an outgoing bandwidth esti...