Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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 | |
Artem Titov | 386802e | 2019-07-05 10:48:17 +0200 | [diff] [blame] | 11 | #include "test/network/network_emulation_manager.h" |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <memory> |
| 15 | |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 16 | #include "api/units/time_delta.h" |
| 17 | #include "api/units/timestamp.h" |
Artem Titov | 48b1b18 | 2019-07-05 13:09:48 +0200 | [diff] [blame] | 18 | #include "call/simulated_network.h" |
Artem Titov | 2086347 | 2019-03-13 10:30:51 +0100 | [diff] [blame] | 19 | #include "rtc_base/fake_network.h" |
Jonas Oreland | 9705011 | 2020-11-17 21:30:33 +0100 | [diff] [blame] | 20 | #include "test/network/emulated_turn_server.h" |
Andrey Logvin | f9ee0e0 | 2021-01-14 09:50:32 +0000 | [diff] [blame] | 21 | #include "test/network/traffic_route.h" |
Artem Titov | 386802e | 2019-07-05 10:48:17 +0200 | [diff] [blame] | 22 | #include "test/time_controller/real_time_controller.h" |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 23 | #include "test/time_controller/simulated_time_controller.h" |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | namespace test { |
| 27 | namespace { |
| 28 | |
Artem Titov | 0bf4c29 | 2019-02-26 10:00:07 +0100 | [diff] [blame] | 29 | // uint32_t representation of 192.168.0.0 address |
| 30 | constexpr uint32_t kMinIPv4Address = 0xC0A80000; |
| 31 | // uint32_t representation of 192.168.255.255 address |
| 32 | constexpr uint32_t kMaxIPv4Address = 0xC0A8FFFF; |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 33 | |
| 34 | std::unique_ptr<TimeController> CreateTimeController(TimeMode mode) { |
| 35 | switch (mode) { |
| 36 | case TimeMode::kRealTime: |
| 37 | return std::make_unique<RealTimeController>(); |
| 38 | case TimeMode::kSimulated: |
| 39 | // Using an offset of 100000 to get nice fixed width and readable |
| 40 | // timestamps in typical test scenarios. |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 41 | const Timestamp kSimulatedStartTime = Timestamp::Seconds(100000); |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 42 | return std::make_unique<GlobalSimulatedTimeController>( |
| 43 | kSimulatedStartTime); |
| 44 | } |
| 45 | } |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 46 | } // namespace |
| 47 | |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 48 | NetworkEmulationManagerImpl::NetworkEmulationManagerImpl(TimeMode mode) |
Artem Titov | cc8a1f8 | 2021-01-26 13:58:23 +0100 | [diff] [blame] | 49 | : time_mode_(mode), |
| 50 | time_controller_(CreateTimeController(mode)), |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 51 | clock_(time_controller_->GetClock()), |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 52 | next_node_id_(1), |
Artem Titov | 0bf4c29 | 2019-02-26 10:00:07 +0100 | [diff] [blame] | 53 | next_ip4_address_(kMinIPv4Address), |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 54 | task_queue_(time_controller_->GetTaskQueueFactory()->CreateTaskQueue( |
Sebastian Jansson | 5d97f55 | 2019-04-15 14:43:03 +0200 | [diff] [blame] | 55 | "NetworkEmulation", |
Artem Titov | 386802e | 2019-07-05 10:48:17 +0200 | [diff] [blame] | 56 | TaskQueueFactory::Priority::NORMAL)) {} |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 57 | |
Sebastian Jansson | b00eb19 | 2019-02-11 09:13:01 +0100 | [diff] [blame] | 58 | // TODO(srte): Ensure that any pending task that must be run for consistency |
| 59 | // (such as stats collection tasks) are not cancelled when the task queue is |
| 60 | // destroyed. |
Jonas Oreland | 9705011 | 2020-11-17 21:30:33 +0100 | [diff] [blame] | 61 | NetworkEmulationManagerImpl::~NetworkEmulationManagerImpl() { |
| 62 | for (auto& turn_server : turn_servers_) { |
| 63 | turn_server->Stop(); |
| 64 | } |
| 65 | } |
Sebastian Jansson | b00eb19 | 2019-02-11 09:13:01 +0100 | [diff] [blame] | 66 | |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 67 | EmulatedNetworkNode* NetworkEmulationManagerImpl::CreateEmulatedNode( |
Artem Titov | ec9b281 | 2021-01-07 15:49:31 +0100 | [diff] [blame] | 68 | BuiltInNetworkBehaviorConfig config, |
| 69 | uint64_t random_seed) { |
| 70 | return CreateEmulatedNode( |
| 71 | std::make_unique<SimulatedNetwork>(config, random_seed)); |
Artem Titov | 48b1b18 | 2019-07-05 13:09:48 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | EmulatedNetworkNode* NetworkEmulationManagerImpl::CreateEmulatedNode( |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 75 | std::unique_ptr<NetworkBehaviorInterface> network_behavior) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 76 | auto node = std::make_unique<EmulatedNetworkNode>( |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 77 | clock_, &task_queue_, std::move(network_behavior)); |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 78 | EmulatedNetworkNode* out = node.get(); |
Sebastian Jansson | 86314cf | 2019-09-17 20:29:59 +0200 | [diff] [blame] | 79 | task_queue_.PostTask([this, node = std::move(node)]() mutable { |
| 80 | network_nodes_.push_back(std::move(node)); |
| 81 | }); |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 82 | return out; |
| 83 | } |
| 84 | |
Sebastian Jansson | cec2433 | 2019-12-04 14:26:50 +0100 | [diff] [blame] | 85 | NetworkEmulationManager::SimulatedNetworkNode::Builder |
| 86 | NetworkEmulationManagerImpl::NodeBuilder() { |
Sebastian Jansson | 8d3e4bd | 2019-07-31 18:33:17 +0200 | [diff] [blame] | 87 | return SimulatedNetworkNode::Builder(this); |
| 88 | } |
| 89 | |
Niels Möller | 376cf38 | 2021-02-26 09:24:51 +0100 | [diff] [blame] | 90 | EmulatedEndpointImpl* NetworkEmulationManagerImpl::CreateEndpoint( |
Artem Titov | a268b69 | 2019-03-12 13:37:28 +0100 | [diff] [blame] | 91 | EmulatedEndpointConfig config) { |
Artem Titov | 0bf4c29 | 2019-02-26 10:00:07 +0100 | [diff] [blame] | 92 | absl::optional<rtc::IPAddress> ip = config.ip; |
| 93 | if (!ip) { |
| 94 | switch (config.generated_ip_family) { |
Artem Titov | a268b69 | 2019-03-12 13:37:28 +0100 | [diff] [blame] | 95 | case EmulatedEndpointConfig::IpAddressFamily::kIpv4: |
Artem Titov | 0bf4c29 | 2019-02-26 10:00:07 +0100 | [diff] [blame] | 96 | ip = GetNextIPv4Address(); |
| 97 | RTC_CHECK(ip) << "All auto generated IPv4 addresses exhausted"; |
| 98 | break; |
Artem Titov | a268b69 | 2019-03-12 13:37:28 +0100 | [diff] [blame] | 99 | case EmulatedEndpointConfig::IpAddressFamily::kIpv6: |
Artem Titov | 0bf4c29 | 2019-02-26 10:00:07 +0100 | [diff] [blame] | 100 | ip = GetNextIPv4Address(); |
| 101 | RTC_CHECK(ip) << "All auto generated IPv6 addresses exhausted"; |
| 102 | ip = ip->AsIPv6Address(); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | bool res = used_ip_addresses_.insert(*ip).second; |
| 108 | RTC_CHECK(res) << "IP=" << ip->ToString() << " already in use"; |
Sebastian Jansson | cec2433 | 2019-12-04 14:26:50 +0100 | [diff] [blame] | 109 | auto node = std::make_unique<EmulatedEndpointImpl>( |
Artem Titov | 3d37e06 | 2021-02-19 20:26:32 +0100 | [diff] [blame] | 110 | EmulatedEndpointImpl::Options(next_node_id_++, *ip, config), |
| 111 | config.start_as_enabled, &task_queue_, clock_); |
Niels Möller | 376cf38 | 2021-02-26 09:24:51 +0100 | [diff] [blame] | 112 | EmulatedEndpointImpl* out = node.get(); |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 113 | endpoints_.push_back(std::move(node)); |
| 114 | return out; |
| 115 | } |
| 116 | |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 117 | void NetworkEmulationManagerImpl::EnableEndpoint(EmulatedEndpoint* endpoint) { |
| 118 | EmulatedNetworkManager* network_manager = |
| 119 | endpoint_to_network_manager_[endpoint]; |
| 120 | RTC_CHECK(network_manager); |
Sebastian Jansson | cec2433 | 2019-12-04 14:26:50 +0100 | [diff] [blame] | 121 | network_manager->EnableEndpoint(static_cast<EmulatedEndpointImpl*>(endpoint)); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void NetworkEmulationManagerImpl::DisableEndpoint(EmulatedEndpoint* endpoint) { |
| 125 | EmulatedNetworkManager* network_manager = |
| 126 | endpoint_to_network_manager_[endpoint]; |
| 127 | RTC_CHECK(network_manager); |
Sebastian Jansson | cec2433 | 2019-12-04 14:26:50 +0100 | [diff] [blame] | 128 | network_manager->DisableEndpoint( |
| 129 | static_cast<EmulatedEndpointImpl*>(endpoint)); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 132 | EmulatedRoute* NetworkEmulationManagerImpl::CreateRoute( |
Artem Titov | aba8dc2 | 2019-03-11 10:08:40 +0100 | [diff] [blame] | 133 | EmulatedEndpoint* from, |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 134 | const std::vector<EmulatedNetworkNode*>& via_nodes, |
Artem Titov | aba8dc2 | 2019-03-11 10:08:40 +0100 | [diff] [blame] | 135 | EmulatedEndpoint* to) { |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 136 | // Because endpoint has no send node by default at least one should be |
| 137 | // provided here. |
| 138 | RTC_CHECK(!via_nodes.empty()); |
| 139 | |
Sebastian Jansson | cec2433 | 2019-12-04 14:26:50 +0100 | [diff] [blame] | 140 | static_cast<EmulatedEndpointImpl*>(from)->router()->SetReceiver( |
| 141 | to->GetPeerLocalAddress(), via_nodes[0]); |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 142 | EmulatedNetworkNode* cur_node = via_nodes[0]; |
| 143 | for (size_t i = 1; i < via_nodes.size(); ++i) { |
Sebastian Jansson | 62bb47f | 2019-04-01 18:23:58 +0200 | [diff] [blame] | 144 | cur_node->router()->SetReceiver(to->GetPeerLocalAddress(), via_nodes[i]); |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 145 | cur_node = via_nodes[i]; |
| 146 | } |
Sebastian Jansson | 62bb47f | 2019-04-01 18:23:58 +0200 | [diff] [blame] | 147 | cur_node->router()->SetReceiver(to->GetPeerLocalAddress(), to); |
Artem Titov | fc6ab00 | 2019-03-12 13:48:32 +0100 | [diff] [blame] | 148 | |
Sebastian Jansson | cec2433 | 2019-12-04 14:26:50 +0100 | [diff] [blame] | 149 | std::unique_ptr<EmulatedRoute> route = std::make_unique<EmulatedRoute>( |
| 150 | static_cast<EmulatedEndpointImpl*>(from), std::move(via_nodes), |
Artem Titov | 3d37e06 | 2021-02-19 20:26:32 +0100 | [diff] [blame] | 151 | static_cast<EmulatedEndpointImpl*>(to), /*is_default=*/false); |
Artem Titov | fc6ab00 | 2019-03-12 13:48:32 +0100 | [diff] [blame] | 152 | EmulatedRoute* out = route.get(); |
| 153 | routes_.push_back(std::move(route)); |
| 154 | return out; |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Sebastian Jansson | b13ccc5 | 2019-06-07 13:06:00 +0200 | [diff] [blame] | 157 | EmulatedRoute* NetworkEmulationManagerImpl::CreateRoute( |
| 158 | const std::vector<EmulatedNetworkNode*>& via_nodes) { |
| 159 | EmulatedEndpoint* from = CreateEndpoint(EmulatedEndpointConfig()); |
| 160 | EmulatedEndpoint* to = CreateEndpoint(EmulatedEndpointConfig()); |
| 161 | return CreateRoute(from, via_nodes, to); |
| 162 | } |
| 163 | |
Artem Titov | 3d37e06 | 2021-02-19 20:26:32 +0100 | [diff] [blame] | 164 | EmulatedRoute* NetworkEmulationManagerImpl::CreateDefaultRoute( |
| 165 | EmulatedEndpoint* from, |
| 166 | const std::vector<EmulatedNetworkNode*>& via_nodes, |
| 167 | EmulatedEndpoint* to) { |
| 168 | // Because endpoint has no send node by default at least one should be |
| 169 | // provided here. |
| 170 | RTC_CHECK(!via_nodes.empty()); |
| 171 | |
| 172 | static_cast<EmulatedEndpointImpl*>(from)->router()->SetDefaultReceiver( |
| 173 | via_nodes[0]); |
| 174 | EmulatedNetworkNode* cur_node = via_nodes[0]; |
| 175 | for (size_t i = 1; i < via_nodes.size(); ++i) { |
| 176 | cur_node->router()->SetDefaultReceiver(via_nodes[i]); |
| 177 | cur_node = via_nodes[i]; |
| 178 | } |
| 179 | cur_node->router()->SetDefaultReceiver(to); |
| 180 | |
| 181 | std::unique_ptr<EmulatedRoute> route = std::make_unique<EmulatedRoute>( |
| 182 | static_cast<EmulatedEndpointImpl*>(from), std::move(via_nodes), |
| 183 | static_cast<EmulatedEndpointImpl*>(to), /*is_default=*/true); |
| 184 | EmulatedRoute* out = route.get(); |
| 185 | routes_.push_back(std::move(route)); |
| 186 | return out; |
| 187 | } |
| 188 | |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 189 | void NetworkEmulationManagerImpl::ClearRoute(EmulatedRoute* route) { |
Artem Titov | fc6ab00 | 2019-03-12 13:48:32 +0100 | [diff] [blame] | 190 | RTC_CHECK(route->active) << "Route already cleared"; |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 191 | task_queue_.SendTask( |
| 192 | [route]() { |
| 193 | // Remove receiver from intermediate nodes. |
| 194 | for (auto* node : route->via_nodes) { |
Artem Titov | 3d37e06 | 2021-02-19 20:26:32 +0100 | [diff] [blame] | 195 | if (route->is_default) { |
| 196 | node->router()->RemoveDefaultReceiver(); |
| 197 | } else { |
| 198 | node->router()->RemoveReceiver(route->to->GetPeerLocalAddress()); |
| 199 | } |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 200 | } |
| 201 | // Remove destination endpoint from source endpoint's router. |
Artem Titov | 3d37e06 | 2021-02-19 20:26:32 +0100 | [diff] [blame] | 202 | if (route->is_default) { |
| 203 | route->from->router()->RemoveDefaultReceiver(); |
| 204 | } else { |
| 205 | route->from->router()->RemoveReceiver( |
| 206 | route->to->GetPeerLocalAddress()); |
| 207 | } |
Artem Titov | fc6ab00 | 2019-03-12 13:48:32 +0100 | [diff] [blame] | 208 | |
Danil Chapovalov | eb90e6f | 2019-10-15 10:04:57 +0200 | [diff] [blame] | 209 | route->active = false; |
| 210 | }, |
| 211 | RTC_FROM_HERE); |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Andrey Logvin | f9ee0e0 | 2021-01-14 09:50:32 +0000 | [diff] [blame] | 214 | TcpMessageRoute* NetworkEmulationManagerImpl::CreateTcpRoute( |
| 215 | EmulatedRoute* send_route, |
| 216 | EmulatedRoute* ret_route) { |
| 217 | auto tcp_route = std::make_unique<TcpMessageRouteImpl>( |
| 218 | clock_, task_queue_.Get(), send_route, ret_route); |
| 219 | auto* route_ptr = tcp_route.get(); |
| 220 | task_queue_.PostTask([this, tcp_route = std::move(tcp_route)]() mutable { |
| 221 | tcp_message_routes_.push_back(std::move(tcp_route)); |
| 222 | }); |
| 223 | return route_ptr; |
| 224 | } |
| 225 | |
| 226 | CrossTrafficRoute* NetworkEmulationManagerImpl::CreateCrossTrafficRoute( |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 227 | const std::vector<EmulatedNetworkNode*>& via_nodes) { |
Artem Titov | d3666b2 | 2019-02-11 14:40:17 +0100 | [diff] [blame] | 228 | RTC_CHECK(!via_nodes.empty()); |
Niels Möller | 376cf38 | 2021-02-26 09:24:51 +0100 | [diff] [blame] | 229 | EmulatedEndpointImpl* endpoint = CreateEndpoint(EmulatedEndpointConfig()); |
Artem Titov | d3666b2 | 2019-02-11 14:40:17 +0100 | [diff] [blame] | 230 | |
| 231 | // Setup a route via specified nodes. |
| 232 | EmulatedNetworkNode* cur_node = via_nodes[0]; |
| 233 | for (size_t i = 1; i < via_nodes.size(); ++i) { |
Sebastian Jansson | 62bb47f | 2019-04-01 18:23:58 +0200 | [diff] [blame] | 234 | cur_node->router()->SetReceiver(endpoint->GetPeerLocalAddress(), |
| 235 | via_nodes[i]); |
Artem Titov | d3666b2 | 2019-02-11 14:40:17 +0100 | [diff] [blame] | 236 | cur_node = via_nodes[i]; |
| 237 | } |
Sebastian Jansson | 62bb47f | 2019-04-01 18:23:58 +0200 | [diff] [blame] | 238 | cur_node->router()->SetReceiver(endpoint->GetPeerLocalAddress(), endpoint); |
Artem Titov | d3666b2 | 2019-02-11 14:40:17 +0100 | [diff] [blame] | 239 | |
Andrey Logvin | f9ee0e0 | 2021-01-14 09:50:32 +0000 | [diff] [blame] | 240 | std::unique_ptr<CrossTrafficRoute> traffic_route = |
| 241 | std::make_unique<CrossTrafficRouteImpl>(clock_, via_nodes[0], endpoint); |
| 242 | CrossTrafficRoute* out = traffic_route.get(); |
Artem Titov | d3666b2 | 2019-02-11 14:40:17 +0100 | [diff] [blame] | 243 | traffic_routes_.push_back(std::move(traffic_route)); |
| 244 | return out; |
| 245 | } |
| 246 | |
Andrey Logvin | f9ee0e0 | 2021-01-14 09:50:32 +0000 | [diff] [blame] | 247 | CrossTrafficGenerator* NetworkEmulationManagerImpl::StartCrossTraffic( |
| 248 | std::unique_ptr<CrossTrafficGenerator> generator) { |
| 249 | CrossTrafficGenerator* out = generator.get(); |
| 250 | task_queue_.PostTask([this, generator = std::move(generator)]() mutable { |
| 251 | auto* generator_ptr = generator.get(); |
Sebastian Jansson | f6e6435 | 2019-04-17 18:02:34 +0200 | [diff] [blame] | 252 | |
Andrey Logvin | f9ee0e0 | 2021-01-14 09:50:32 +0000 | [diff] [blame] | 253 | auto repeating_task_handle = |
| 254 | RepeatingTaskHandle::Start(task_queue_.Get(), [this, generator_ptr] { |
| 255 | generator_ptr->Process(Now()); |
| 256 | return generator_ptr->GetProcessInterval(); |
| 257 | }); |
Artem Titov | d3666b2 | 2019-02-11 14:40:17 +0100 | [diff] [blame] | 258 | |
Andrey Logvin | f9ee0e0 | 2021-01-14 09:50:32 +0000 | [diff] [blame] | 259 | cross_traffics_.push_back(CrossTrafficSource( |
| 260 | std::move(generator), std::move(repeating_task_handle))); |
Sebastian Jansson | d8aff21 | 2019-10-11 17:00:39 +0200 | [diff] [blame] | 261 | }); |
Andrey Logvin | f9ee0e0 | 2021-01-14 09:50:32 +0000 | [diff] [blame] | 262 | return out; |
Sebastian Jansson | dcc910a | 2019-11-12 16:36:34 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Sebastian Jansson | d8aff21 | 2019-10-11 17:00:39 +0200 | [diff] [blame] | 265 | void NetworkEmulationManagerImpl::StopCrossTraffic( |
Andrey Logvin | f9ee0e0 | 2021-01-14 09:50:32 +0000 | [diff] [blame] | 266 | CrossTrafficGenerator* generator) { |
Sebastian Jansson | d8aff21 | 2019-10-11 17:00:39 +0200 | [diff] [blame] | 267 | task_queue_.PostTask([=]() { |
Andrey Logvin | f9ee0e0 | 2021-01-14 09:50:32 +0000 | [diff] [blame] | 268 | auto it = std::find_if(cross_traffics_.begin(), cross_traffics_.end(), |
| 269 | [=](const CrossTrafficSource& el) { |
| 270 | return el.first.get() == generator; |
| 271 | }); |
| 272 | it->second.Stop(); |
| 273 | cross_traffics_.erase(it); |
Sebastian Jansson | b13ccc5 | 2019-06-07 13:06:00 +0200 | [diff] [blame] | 274 | }); |
| 275 | } |
| 276 | |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 277 | EmulatedNetworkManagerInterface* |
| 278 | NetworkEmulationManagerImpl::CreateEmulatedNetworkManagerInterface( |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 279 | const std::vector<EmulatedEndpoint*>& endpoints) { |
Sebastian Jansson | cec2433 | 2019-12-04 14:26:50 +0100 | [diff] [blame] | 280 | std::vector<EmulatedEndpointImpl*> endpoint_impls; |
Mirko Bonadei | 14cad9f | 2021-02-03 09:48:01 +0100 | [diff] [blame] | 281 | endpoint_impls.reserve(endpoints.size()); |
Sebastian Jansson | cec2433 | 2019-12-04 14:26:50 +0100 | [diff] [blame] | 282 | for (EmulatedEndpoint* endpoint : endpoints) { |
| 283 | endpoint_impls.push_back(static_cast<EmulatedEndpointImpl*>(endpoint)); |
| 284 | } |
| 285 | auto endpoints_container = |
| 286 | std::make_unique<EndpointsContainer>(endpoint_impls); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 287 | auto network_manager = std::make_unique<EmulatedNetworkManager>( |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 288 | time_controller_.get(), &task_queue_, endpoints_container.get()); |
Artem Titov | 2086347 | 2019-03-13 10:30:51 +0100 | [diff] [blame] | 289 | for (auto* endpoint : endpoints) { |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 290 | // Associate endpoint with network manager. |
| 291 | bool insertion_result = |
| 292 | endpoint_to_network_manager_.insert({endpoint, network_manager.get()}) |
| 293 | .second; |
| 294 | RTC_CHECK(insertion_result) |
| 295 | << "Endpoint ip=" << endpoint->GetPeerLocalAddress().ToString() |
| 296 | << " is already used for another network"; |
Artem Titov | 2086347 | 2019-03-13 10:30:51 +0100 | [diff] [blame] | 297 | } |
Artem Titov | 2086347 | 2019-03-13 10:30:51 +0100 | [diff] [blame] | 298 | |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 299 | EmulatedNetworkManagerInterface* out = network_manager.get(); |
| 300 | |
Artem Titov | 0d32a73 | 2019-04-05 14:49:59 +0200 | [diff] [blame] | 301 | endpoints_containers_.push_back(std::move(endpoints_container)); |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 302 | network_managers_.push_back(std::move(network_manager)); |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 303 | return out; |
| 304 | } |
| 305 | |
Artem Titov | cf78128 | 2020-07-28 13:45:16 +0200 | [diff] [blame] | 306 | void NetworkEmulationManagerImpl::GetStats( |
Per Kjellander | 410c998 | 2021-02-15 11:24:37 +0100 | [diff] [blame] | 307 | rtc::ArrayView<EmulatedEndpoint* const> endpoints, |
Artem Titov | cf78128 | 2020-07-28 13:45:16 +0200 | [diff] [blame] | 308 | std::function<void(std::unique_ptr<EmulatedNetworkStats>)> stats_callback) { |
| 309 | task_queue_.PostTask([endpoints, stats_callback]() { |
| 310 | EmulatedNetworkStatsBuilder stats_builder; |
| 311 | for (auto* endpoint : endpoints) { |
Artem Titov | 9dcab80 | 2020-08-04 11:19:28 +0200 | [diff] [blame] | 312 | // It's safe to cast here because EmulatedEndpointImpl can be the only |
| 313 | // implementation of EmulatedEndpoint, because only it has access to |
| 314 | // EmulatedEndpoint constructor. |
| 315 | auto endpoint_impl = static_cast<EmulatedEndpointImpl*>(endpoint); |
| 316 | stats_builder.AddEmulatedNetworkStats(*endpoint_impl->stats()); |
Artem Titov | cf78128 | 2020-07-28 13:45:16 +0200 | [diff] [blame] | 317 | } |
| 318 | stats_callback(stats_builder.Build()); |
| 319 | }); |
| 320 | } |
| 321 | |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 322 | absl::optional<rtc::IPAddress> |
| 323 | NetworkEmulationManagerImpl::GetNextIPv4Address() { |
Artem Titov | 0bf4c29 | 2019-02-26 10:00:07 +0100 | [diff] [blame] | 324 | uint32_t addresses_count = kMaxIPv4Address - kMinIPv4Address; |
| 325 | for (uint32_t i = 0; i < addresses_count; i++) { |
| 326 | rtc::IPAddress ip(next_ip4_address_); |
| 327 | if (next_ip4_address_ == kMaxIPv4Address) { |
| 328 | next_ip4_address_ = kMinIPv4Address; |
| 329 | } else { |
| 330 | next_ip4_address_++; |
| 331 | } |
| 332 | if (used_ip_addresses_.find(ip) == used_ip_addresses_.end()) { |
| 333 | return ip; |
| 334 | } |
| 335 | } |
| 336 | return absl::nullopt; |
| 337 | } |
| 338 | |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 339 | Timestamp NetworkEmulationManagerImpl::Now() const { |
Sebastian Jansson | b64ad0e | 2019-06-19 09:39:34 +0200 | [diff] [blame] | 340 | return clock_->CurrentTime(); |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 341 | } |
| 342 | |
Jonas Oreland | 9705011 | 2020-11-17 21:30:33 +0100 | [diff] [blame] | 343 | EmulatedTURNServerInterface* NetworkEmulationManagerImpl::CreateTURNServer( |
| 344 | EmulatedTURNServerConfig config) { |
| 345 | auto* client = CreateEndpoint(config.client_config); |
| 346 | auto* peer = CreateEndpoint(config.client_config); |
| 347 | char buf[128]; |
| 348 | rtc::SimpleStringBuilder str(buf); |
| 349 | str.AppendFormat("turn_server_%u", |
| 350 | static_cast<unsigned>(turn_servers_.size())); |
| 351 | auto turn = std::make_unique<EmulatedTURNServer>( |
| 352 | time_controller_->CreateThread(str.str()), client, peer); |
| 353 | auto out = turn.get(); |
| 354 | turn_servers_.push_back(std::move(turn)); |
| 355 | return out; |
| 356 | } |
| 357 | |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 358 | } // namespace test |
| 359 | } // namespace webrtc |