blob: dd4c83549b7824ef34a0317f01d9460fe2dda7ba [file] [log] [blame]
Artem Titov7bf8c7f2019-03-15 15:00:37 +01001/*
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
11#ifndef API_TEST_NETWORK_EMULATION_MANAGER_H_
12#define API_TEST_NETWORK_EMULATION_MANAGER_H_
13
Artem Titov3e0b65d2020-07-23 02:19:02 +020014#include <functional>
Artem Titov7bf8c7f2019-03-15 15:00:37 +010015#include <memory>
Jonas Oreland97050112020-11-17 21:30:33 +010016#include <string>
Artem Titov7bf8c7f2019-03-15 15:00:37 +010017#include <vector>
18
Artem Titovcf781282020-07-28 13:45:16 +020019#include "api/array_view.h"
Andrey Logvinf9ee0e02021-01-14 09:50:32 +000020#include "api/test/network_emulation/cross_traffic.h"
Sebastian Janssoncec24332019-12-04 14:26:50 +010021#include "api/test/network_emulation/network_emulation_interfaces.h"
Artem Titov7bf8c7f2019-03-15 15:00:37 +010022#include "api/test/simulated_network.h"
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010023#include "api/test/time_controller.h"
Artem Titov806299e2019-04-12 12:17:19 +020024#include "api/units/timestamp.h"
Artem Titov7bf8c7f2019-03-15 15:00:37 +010025#include "rtc_base/network.h"
Artem Titov1e023392020-01-23 15:46:45 +010026#include "rtc_base/network_constants.h"
Artem Titov7bf8c7f2019-03-15 15:00:37 +010027#include "rtc_base/thread.h"
28
29namespace webrtc {
30
31// This API is still in development and can be changed without prior notice.
32
33// These classes are forward declared here, because they used as handles, to
34// make it possible for client code to operate with these abstractions and build
35// required network configuration. With forward declaration here implementation
36// is more readable, than with interfaces approach and cause user needn't any
37// API methods on these abstractions it is acceptable here.
38
39// EmulatedNetworkNode is an abstraction for some network in the real world,
40// like 3G network between peers, or Wi-Fi for one peer and LTE for another.
41// Multiple networks can be joined into chain emulating a network path from
42// one peer to another.
43class EmulatedNetworkNode;
Sebastian Janssoncec24332019-12-04 14:26:50 +010044
Artem Titov7bf8c7f2019-03-15 15:00:37 +010045// EmulatedRoute is handle for single route from one network interface on one
46// peer device to another network interface on another peer device.
47class EmulatedRoute;
48
49struct EmulatedEndpointConfig {
50 enum class IpAddressFamily { kIpv4, kIpv6 };
Artem Titovcbe6e8a2020-09-22 15:45:00 +020051 enum class StatsGatheringMode {
52 // Gather main network stats counters.
53 kDefault,
54 // kDefault + also gather per packet statistics. In this mode more memory
55 // will be used.
56 kDebug
57 };
Artem Titov7bf8c7f2019-03-15 15:00:37 +010058
Artem Titovd2dd7322021-01-21 17:28:17 +010059 // If specified will be used to name endpoint for logging purposes.
60 absl::optional<std::string> name = absl::nullopt;
Artem Titov7bf8c7f2019-03-15 15:00:37 +010061 IpAddressFamily generated_ip_family = IpAddressFamily::kIpv4;
62 // If specified will be used as IP address for endpoint node. Must be unique
63 // among all created nodes.
64 absl::optional<rtc::IPAddress> ip;
Artem Titove5cc85b2019-03-28 12:11:09 +010065 // Should endpoint be enabled or not, when it will be created.
66 // Enabled endpoints will be available for webrtc to send packets.
67 bool start_as_enabled = true;
Artem Titov1e023392020-01-23 15:46:45 +010068 // Network type which will be used to represent endpoint to WebRTC.
69 rtc::AdapterType type = rtc::AdapterType::ADAPTER_TYPE_UNKNOWN;
Artem Titovcbe6e8a2020-09-22 15:45:00 +020070 StatsGatheringMode stats_gathering_mode = StatsGatheringMode::kDefault;
Artem Titove5cc85b2019-03-28 12:11:09 +010071};
72
Jonas Oreland97050112020-11-17 21:30:33 +010073struct EmulatedTURNServerConfig {
74 EmulatedEndpointConfig client_config;
75 EmulatedEndpointConfig peer_config;
76};
77
78// EmulatedTURNServer is an abstraction for a TURN server.
79class EmulatedTURNServerInterface {
80 public:
81 struct IceServerConfig {
82 std::string username;
83 std::string password;
84 std::string url;
85 };
86
87 virtual ~EmulatedTURNServerInterface() {}
88
89 // Get an IceServer configuration suitable to add to a PeerConnection.
90 virtual IceServerConfig GetIceServerConfig() const = 0;
91
92 // Get non-null client endpoint, an endpoint that accepts TURN allocations.
93 // This shall typically be connected to one or more webrtc endpoint.
94 virtual EmulatedEndpoint* GetClientEndpoint() const = 0;
95
96 // Returns socket address, which client should use to connect to TURN server
97 // and do TURN allocation.
98 virtual rtc::SocketAddress GetClientEndpointAddress() const = 0;
99
100 // Get non-null peer endpoint, that is "connected to the internet".
101 // This shall typically be connected to another TURN server.
102 virtual EmulatedEndpoint* GetPeerEndpoint() const = 0;
103};
Artem Titov806299e2019-04-12 12:17:19 +0200104
Artem Titove5cc85b2019-03-28 12:11:09 +0100105// Provide interface to obtain all required objects to inject network emulation
Artem Titov806299e2019-04-12 12:17:19 +0200106// layer into PeerConnection. Also contains information about network interfaces
107// accessible by PeerConnection.
Artem Titove5cc85b2019-03-28 12:11:09 +0100108class EmulatedNetworkManagerInterface {
109 public:
110 virtual ~EmulatedNetworkManagerInterface() = default;
111
Artem Titovcf781282020-07-28 13:45:16 +0200112 // Returns non-null pointer to thread that have to be used as network thread
113 // for WebRTC to properly setup network emulation. Returned thread is owned
114 // by EmulatedNetworkManagerInterface implementation.
Artem Titove5cc85b2019-03-28 12:11:09 +0100115 virtual rtc::Thread* network_thread() = 0;
Artem Titovcf781282020-07-28 13:45:16 +0200116 // Returns non-null pointer to network manager that have to be injected into
117 // WebRTC to properly setup network emulation. Returned manager is owned by
118 // EmulatedNetworkManagerInterface implementation.
Artem Titove5cc85b2019-03-28 12:11:09 +0100119 virtual rtc::NetworkManager* network_manager() = 0;
Artem Titovcf781282020-07-28 13:45:16 +0200120 // Returns list of endpoints that are associated with this instance. Pointers
121 // are guaranteed to be non-null and are owned by NetworkEmulationManager.
122 virtual std::vector<EmulatedEndpoint*> endpoints() const = 0;
Artem Titov806299e2019-04-12 12:17:19 +0200123
Artem Titovcf781282020-07-28 13:45:16 +0200124 // Passes summarized network stats for endpoints for this manager into
Artem Titov5501cef2020-08-04 11:49:19 +0200125 // specified |stats_callback|. Callback will be executed on network emulation
126 // internal task queue.
Artem Titov806299e2019-04-12 12:17:19 +0200127 virtual void GetStats(
Artem Titov3e0b65d2020-07-23 02:19:02 +0200128 std::function<void(std::unique_ptr<EmulatedNetworkStats>)> stats_callback)
129 const = 0;
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100130};
131
Sebastian Jansson6ce033a2020-01-22 10:12:56 +0100132enum class TimeMode { kRealTime, kSimulated };
133
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100134// Provides an API for creating and configuring emulated network layer.
135// All objects returned by this API are owned by NetworkEmulationManager itself
136// and will be deleted when manager will be deleted.
137class NetworkEmulationManager {
138 public:
Sebastian Janssoncec24332019-12-04 14:26:50 +0100139 // Helper struct to simplify creation of simulated network behaviors. Contains
140 // non-owning pointers as the underlying instances are owned by the manager.
141 struct SimulatedNetworkNode {
142 SimulatedNetworkInterface* simulation;
143 EmulatedNetworkNode* node;
144
145 class Builder {
146 public:
147 explicit Builder(NetworkEmulationManager* net) : net_(net) {}
Sebastian Janssonce911262019-12-11 19:08:40 +0100148 Builder() : net_(nullptr) {}
Sebastian Janssoncec24332019-12-04 14:26:50 +0100149 Builder(const Builder&) = default;
150 // Sets the config state, note that this will replace any previously set
151 // values.
152 Builder& config(BuiltInNetworkBehaviorConfig config);
153 Builder& delay_ms(int queue_delay_ms);
154 Builder& capacity_kbps(int link_capacity_kbps);
155 Builder& capacity_Mbps(int link_capacity_Mbps);
156 Builder& loss(double loss_rate);
Sebastian Jansson39272982019-12-11 19:29:57 +0100157 Builder& packet_queue_length(int max_queue_length_in_packets);
Artem Titovec9b2812021-01-07 15:49:31 +0100158 SimulatedNetworkNode Build(uint64_t random_seed = 1) const;
159 SimulatedNetworkNode Build(NetworkEmulationManager* net,
160 uint64_t random_seed = 1) const;
Sebastian Janssoncec24332019-12-04 14:26:50 +0100161
162 private:
163 NetworkEmulationManager* const net_;
164 BuiltInNetworkBehaviorConfig config_;
165 };
166 };
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100167 virtual ~NetworkEmulationManager() = default;
168
Sebastian Jansson6ce033a2020-01-22 10:12:56 +0100169 virtual TimeController* time_controller() = 0;
170
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100171 // Creates an emulated network node, which represents single network in
Artem Titovec9b2812021-01-07 15:49:31 +0100172 // the emulated network layer. Uses default implementation on network behavior
173 // which can be configured with |config|. |random_seed| can be provided to
174 // alter randomization behavior.
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100175 virtual EmulatedNetworkNode* CreateEmulatedNode(
Artem Titovec9b2812021-01-07 15:49:31 +0100176 BuiltInNetworkBehaviorConfig config,
177 uint64_t random_seed = 1) = 0;
178 // Creates an emulated network node, which represents single network in
179 // the emulated network layer. |network_behavior| determines how created node
180 // will forward incoming packets to the next receiver.
Artem Titov48b1b182019-07-05 13:09:48 +0200181 virtual EmulatedNetworkNode* CreateEmulatedNode(
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100182 std::unique_ptr<NetworkBehaviorInterface> network_behavior) = 0;
183
Sebastian Janssoncec24332019-12-04 14:26:50 +0100184 virtual SimulatedNetworkNode::Builder NodeBuilder() = 0;
185
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100186 // Creates an emulated endpoint, which represents single network interface on
187 // the peer's device.
188 virtual EmulatedEndpoint* CreateEndpoint(EmulatedEndpointConfig config) = 0;
Artem Titove5cc85b2019-03-28 12:11:09 +0100189 // Enable emulated endpoint to make it available for webrtc.
190 // Caller mustn't enable currently enabled endpoint.
191 virtual void EnableEndpoint(EmulatedEndpoint* endpoint) = 0;
192 // Disable emulated endpoint to make it unavailable for webrtc.
193 // Caller mustn't disable currently disabled endpoint.
194 virtual void DisableEndpoint(EmulatedEndpoint* endpoint) = 0;
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100195
196 // Creates a route between endpoints going through specified network nodes.
197 // This route is single direction only and describe how traffic that was
198 // sent by network interface |from| have to be delivered to the network
Artem Titovff393122019-04-05 11:19:52 +0200199 // interface |to|. Return object can be used to remove created route. The
200 // route must contains at least one network node inside it.
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100201 //
Artem Titovff393122019-04-05 11:19:52 +0200202 // Assume that E{0-9} are endpoints and N{0-9} are network nodes, then
203 // creation of the route have to follow these rules:
204 // 1. A route consists of a source endpoint, an ordered list of one or
205 // more network nodes, and a destination endpoint.
206 // 2. If (E1, ..., E2) is a route, then E1 != E2.
207 // In other words, the source and the destination may not be the same.
208 // 3. Given two simultaneously existing routes (E1, ..., E2) and
209 // (E3, ..., E4), either E1 != E3 or E2 != E4.
210 // In other words, there may be at most one route from any given source
211 // endpoint to any given destination endpoint.
212 // 4. Given two simultaneously existing routes (E1, ..., N1, ..., E2)
213 // and (E3, ..., N2, ..., E4), either N1 != N2 or E2 != E4.
214 // In other words, a network node may not belong to two routes that lead
215 // to the same destination endpoint.
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100216 virtual EmulatedRoute* CreateRoute(
217 EmulatedEndpoint* from,
218 const std::vector<EmulatedNetworkNode*>& via_nodes,
219 EmulatedEndpoint* to) = 0;
Sebastian Janssoncec24332019-12-04 14:26:50 +0100220
221 // Creates a route over the given |via_nodes| creating the required endpoints
222 // in the process. The returned EmulatedRoute pointer can be used in other
223 // calls as a transport route for message or cross traffic.
224 virtual EmulatedRoute* CreateRoute(
225 const std::vector<EmulatedNetworkNode*>& via_nodes) = 0;
226
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100227 // Removes route previously created by CreateRoute(...).
228 // Caller mustn't call this function with route, that have been already
Andrey Logvinf9ee0e02021-01-14 09:50:32 +0000229 // removed earlier. Removing a route that is currently in use will lead to
230 // packets being dropped.
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100231 virtual void ClearRoute(EmulatedRoute* route) = 0;
232
Sebastian Janssoncec24332019-12-04 14:26:50 +0100233 // Creates a simulated TCP connection using |send_route| for traffic and
234 // |ret_route| for feedback. This can be used to emulate HTTP cross traffic
235 // and to implement realistic reliable signaling over lossy networks.
236 // TODO(srte): Handle clearing of the routes involved.
237 virtual TcpMessageRoute* CreateTcpRoute(EmulatedRoute* send_route,
238 EmulatedRoute* ret_route) = 0;
239
Andrey Logvinf9ee0e02021-01-14 09:50:32 +0000240 // Creates a route over the given |via_nodes|. Returns an object that can be
241 // used to emulate network load with cross traffic over the created route.
242 virtual CrossTrafficRoute* CreateCrossTrafficRoute(
243 const std::vector<EmulatedNetworkNode*>& via_nodes) = 0;
244
245 // Starts generating cross traffic using given |generator|. Takes ownership
246 // over the generator.
247 virtual CrossTrafficGenerator* StartCrossTraffic(
248 std::unique_ptr<CrossTrafficGenerator> generator) = 0;
249
250 // Stops generating cross traffic that was started using given |generator|.
251 // The |generator| shouldn't be used after and the reference may be invalid.
252 virtual void StopCrossTraffic(CrossTrafficGenerator* generator) = 0;
253
Artem Titove5cc85b2019-03-28 12:11:09 +0100254 // Creates EmulatedNetworkManagerInterface which can be used then to inject
255 // network emulation layer into PeerConnection. |endpoints| - are available
256 // network interfaces for PeerConnection. If endpoint is enabled, it will be
257 // immediately available for PeerConnection, otherwise user will be able to
258 // enable endpoint later to make it available for PeerConnection.
259 virtual EmulatedNetworkManagerInterface*
260 CreateEmulatedNetworkManagerInterface(
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100261 const std::vector<EmulatedEndpoint*>& endpoints) = 0;
Artem Titovcf781282020-07-28 13:45:16 +0200262
Artem Titov5501cef2020-08-04 11:49:19 +0200263 // Passes summarized network stats for specified |endpoints| into specified
264 // |stats_callback|. Callback will be executed on network emulation
265 // internal task queue.
Artem Titovcf781282020-07-28 13:45:16 +0200266 virtual void GetStats(
267 rtc::ArrayView<EmulatedEndpoint*> endpoints,
268 std::function<void(std::unique_ptr<EmulatedNetworkStats>)>
269 stats_callback) = 0;
Jonas Oreland97050112020-11-17 21:30:33 +0100270
271 // Create a EmulatedTURNServer.
272 // The TURN server has 2 endpoints that need to be connected with routes,
273 // - GetClientEndpoint() - the endpoint that accepts TURN allocations.
274 // - GetPeerEndpoint() - the endpoint that is "connected to the internet".
275 virtual EmulatedTURNServerInterface* CreateTURNServer(
276 EmulatedTURNServerConfig config) = 0;
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100277};
278
279} // namespace webrtc
280
281#endif // API_TEST_NETWORK_EMULATION_MANAGER_H_