blob: d2e6417a9d1c4efcabfd3414b9b2c9c24a46bdf6 [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>
16#include <vector>
17
Artem Titovcf781282020-07-28 13:45:16 +020018#include "api/array_view.h"
Sebastian Janssoncec24332019-12-04 14:26:50 +010019#include "api/test/network_emulation/network_emulation_interfaces.h"
Artem Titov7bf8c7f2019-03-15 15:00:37 +010020#include "api/test/simulated_network.h"
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010021#include "api/test/time_controller.h"
Artem Titov806299e2019-04-12 12:17:19 +020022#include "api/units/timestamp.h"
Artem Titov7bf8c7f2019-03-15 15:00:37 +010023#include "rtc_base/network.h"
Artem Titov1e023392020-01-23 15:46:45 +010024#include "rtc_base/network_constants.h"
Artem Titov7bf8c7f2019-03-15 15:00:37 +010025#include "rtc_base/thread.h"
26
27namespace webrtc {
28
29// This API is still in development and can be changed without prior notice.
30
31// These classes are forward declared here, because they used as handles, to
32// make it possible for client code to operate with these abstractions and build
33// required network configuration. With forward declaration here implementation
34// is more readable, than with interfaces approach and cause user needn't any
35// API methods on these abstractions it is acceptable here.
36
37// EmulatedNetworkNode is an abstraction for some network in the real world,
38// like 3G network between peers, or Wi-Fi for one peer and LTE for another.
39// Multiple networks can be joined into chain emulating a network path from
40// one peer to another.
41class EmulatedNetworkNode;
Sebastian Janssoncec24332019-12-04 14:26:50 +010042
Artem Titov7bf8c7f2019-03-15 15:00:37 +010043// EmulatedRoute is handle for single route from one network interface on one
44// peer device to another network interface on another peer device.
45class EmulatedRoute;
46
47struct EmulatedEndpointConfig {
48 enum class IpAddressFamily { kIpv4, kIpv6 };
49
50 IpAddressFamily generated_ip_family = IpAddressFamily::kIpv4;
51 // If specified will be used as IP address for endpoint node. Must be unique
52 // among all created nodes.
53 absl::optional<rtc::IPAddress> ip;
Artem Titove5cc85b2019-03-28 12:11:09 +010054 // Should endpoint be enabled or not, when it will be created.
55 // Enabled endpoints will be available for webrtc to send packets.
56 bool start_as_enabled = true;
Artem Titov1e023392020-01-23 15:46:45 +010057 // Network type which will be used to represent endpoint to WebRTC.
58 rtc::AdapterType type = rtc::AdapterType::ADAPTER_TYPE_UNKNOWN;
Artem Titove5cc85b2019-03-28 12:11:09 +010059};
60
Artem Titov806299e2019-04-12 12:17:19 +020061
Artem Titove5cc85b2019-03-28 12:11:09 +010062// Provide interface to obtain all required objects to inject network emulation
Artem Titov806299e2019-04-12 12:17:19 +020063// layer into PeerConnection. Also contains information about network interfaces
64// accessible by PeerConnection.
Artem Titove5cc85b2019-03-28 12:11:09 +010065class EmulatedNetworkManagerInterface {
66 public:
67 virtual ~EmulatedNetworkManagerInterface() = default;
68
Artem Titovcf781282020-07-28 13:45:16 +020069 // Returns non-null pointer to thread that have to be used as network thread
70 // for WebRTC to properly setup network emulation. Returned thread is owned
71 // by EmulatedNetworkManagerInterface implementation.
Artem Titove5cc85b2019-03-28 12:11:09 +010072 virtual rtc::Thread* network_thread() = 0;
Artem Titovcf781282020-07-28 13:45:16 +020073 // Returns non-null pointer to network manager that have to be injected into
74 // WebRTC to properly setup network emulation. Returned manager is owned by
75 // EmulatedNetworkManagerInterface implementation.
Artem Titove5cc85b2019-03-28 12:11:09 +010076 virtual rtc::NetworkManager* network_manager() = 0;
Artem Titovcf781282020-07-28 13:45:16 +020077 // Returns list of endpoints that are associated with this instance. Pointers
78 // are guaranteed to be non-null and are owned by NetworkEmulationManager.
79 virtual std::vector<EmulatedEndpoint*> endpoints() const = 0;
Artem Titov806299e2019-04-12 12:17:19 +020080
Artem Titovcf781282020-07-28 13:45:16 +020081 // Passes summarized network stats for endpoints for this manager into
Artem Titov5501cef2020-08-04 11:49:19 +020082 // specified |stats_callback|. Callback will be executed on network emulation
83 // internal task queue.
Artem Titov806299e2019-04-12 12:17:19 +020084 virtual void GetStats(
Artem Titov3e0b65d2020-07-23 02:19:02 +020085 std::function<void(std::unique_ptr<EmulatedNetworkStats>)> stats_callback)
86 const = 0;
Artem Titov7bf8c7f2019-03-15 15:00:37 +010087};
88
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010089enum class TimeMode { kRealTime, kSimulated };
90
Artem Titov7bf8c7f2019-03-15 15:00:37 +010091// Provides an API for creating and configuring emulated network layer.
92// All objects returned by this API are owned by NetworkEmulationManager itself
93// and will be deleted when manager will be deleted.
94class NetworkEmulationManager {
95 public:
Sebastian Janssoncec24332019-12-04 14:26:50 +010096 // Helper struct to simplify creation of simulated network behaviors. Contains
97 // non-owning pointers as the underlying instances are owned by the manager.
98 struct SimulatedNetworkNode {
99 SimulatedNetworkInterface* simulation;
100 EmulatedNetworkNode* node;
101
102 class Builder {
103 public:
104 explicit Builder(NetworkEmulationManager* net) : net_(net) {}
Sebastian Janssonce911262019-12-11 19:08:40 +0100105 Builder() : net_(nullptr) {}
Sebastian Janssoncec24332019-12-04 14:26:50 +0100106 Builder(const Builder&) = default;
107 // Sets the config state, note that this will replace any previously set
108 // values.
109 Builder& config(BuiltInNetworkBehaviorConfig config);
110 Builder& delay_ms(int queue_delay_ms);
111 Builder& capacity_kbps(int link_capacity_kbps);
112 Builder& capacity_Mbps(int link_capacity_Mbps);
113 Builder& loss(double loss_rate);
Sebastian Jansson39272982019-12-11 19:29:57 +0100114 Builder& packet_queue_length(int max_queue_length_in_packets);
Sebastian Janssoncec24332019-12-04 14:26:50 +0100115 SimulatedNetworkNode Build() const;
Sebastian Janssonce911262019-12-11 19:08:40 +0100116 SimulatedNetworkNode Build(NetworkEmulationManager* net) const;
Sebastian Janssoncec24332019-12-04 14:26:50 +0100117
118 private:
119 NetworkEmulationManager* const net_;
120 BuiltInNetworkBehaviorConfig config_;
121 };
122 };
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100123 virtual ~NetworkEmulationManager() = default;
124
Sebastian Jansson6ce033a2020-01-22 10:12:56 +0100125 virtual TimeController* time_controller() = 0;
126
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100127 // Creates an emulated network node, which represents single network in
128 // the emulated network layer.
129 virtual EmulatedNetworkNode* CreateEmulatedNode(
Artem Titov48b1b182019-07-05 13:09:48 +0200130 BuiltInNetworkBehaviorConfig config) = 0;
131 virtual EmulatedNetworkNode* CreateEmulatedNode(
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100132 std::unique_ptr<NetworkBehaviorInterface> network_behavior) = 0;
133
Sebastian Janssoncec24332019-12-04 14:26:50 +0100134 virtual SimulatedNetworkNode::Builder NodeBuilder() = 0;
135
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100136 // Creates an emulated endpoint, which represents single network interface on
137 // the peer's device.
138 virtual EmulatedEndpoint* CreateEndpoint(EmulatedEndpointConfig config) = 0;
Artem Titove5cc85b2019-03-28 12:11:09 +0100139 // Enable emulated endpoint to make it available for webrtc.
140 // Caller mustn't enable currently enabled endpoint.
141 virtual void EnableEndpoint(EmulatedEndpoint* endpoint) = 0;
142 // Disable emulated endpoint to make it unavailable for webrtc.
143 // Caller mustn't disable currently disabled endpoint.
144 virtual void DisableEndpoint(EmulatedEndpoint* endpoint) = 0;
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100145
146 // Creates a route between endpoints going through specified network nodes.
147 // This route is single direction only and describe how traffic that was
148 // sent by network interface |from| have to be delivered to the network
Artem Titovff393122019-04-05 11:19:52 +0200149 // interface |to|. Return object can be used to remove created route. The
150 // route must contains at least one network node inside it.
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100151 //
Artem Titovff393122019-04-05 11:19:52 +0200152 // Assume that E{0-9} are endpoints and N{0-9} are network nodes, then
153 // creation of the route have to follow these rules:
154 // 1. A route consists of a source endpoint, an ordered list of one or
155 // more network nodes, and a destination endpoint.
156 // 2. If (E1, ..., E2) is a route, then E1 != E2.
157 // In other words, the source and the destination may not be the same.
158 // 3. Given two simultaneously existing routes (E1, ..., E2) and
159 // (E3, ..., E4), either E1 != E3 or E2 != E4.
160 // In other words, there may be at most one route from any given source
161 // endpoint to any given destination endpoint.
162 // 4. Given two simultaneously existing routes (E1, ..., N1, ..., E2)
163 // and (E3, ..., N2, ..., E4), either N1 != N2 or E2 != E4.
164 // In other words, a network node may not belong to two routes that lead
165 // to the same destination endpoint.
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100166 virtual EmulatedRoute* CreateRoute(
167 EmulatedEndpoint* from,
168 const std::vector<EmulatedNetworkNode*>& via_nodes,
169 EmulatedEndpoint* to) = 0;
Sebastian Janssoncec24332019-12-04 14:26:50 +0100170
171 // Creates a route over the given |via_nodes| creating the required endpoints
172 // in the process. The returned EmulatedRoute pointer can be used in other
173 // calls as a transport route for message or cross traffic.
174 virtual EmulatedRoute* CreateRoute(
175 const std::vector<EmulatedNetworkNode*>& via_nodes) = 0;
176
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100177 // Removes route previously created by CreateRoute(...).
178 // Caller mustn't call this function with route, that have been already
179 // removed earlier.
180 virtual void ClearRoute(EmulatedRoute* route) = 0;
181
Sebastian Janssoncec24332019-12-04 14:26:50 +0100182 // Creates a simulated TCP connection using |send_route| for traffic and
183 // |ret_route| for feedback. This can be used to emulate HTTP cross traffic
184 // and to implement realistic reliable signaling over lossy networks.
185 // TODO(srte): Handle clearing of the routes involved.
186 virtual TcpMessageRoute* CreateTcpRoute(EmulatedRoute* send_route,
187 EmulatedRoute* ret_route) = 0;
188
Artem Titove5cc85b2019-03-28 12:11:09 +0100189 // Creates EmulatedNetworkManagerInterface which can be used then to inject
190 // network emulation layer into PeerConnection. |endpoints| - are available
191 // network interfaces for PeerConnection. If endpoint is enabled, it will be
192 // immediately available for PeerConnection, otherwise user will be able to
193 // enable endpoint later to make it available for PeerConnection.
194 virtual EmulatedNetworkManagerInterface*
195 CreateEmulatedNetworkManagerInterface(
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100196 const std::vector<EmulatedEndpoint*>& endpoints) = 0;
Artem Titovcf781282020-07-28 13:45:16 +0200197
Artem Titov5501cef2020-08-04 11:49:19 +0200198 // Passes summarized network stats for specified |endpoints| into specified
199 // |stats_callback|. Callback will be executed on network emulation
200 // internal task queue.
Artem Titovcf781282020-07-28 13:45:16 +0200201 virtual void GetStats(
202 rtc::ArrayView<EmulatedEndpoint*> endpoints,
203 std::function<void(std::unique_ptr<EmulatedNetworkStats>)>
204 stats_callback) = 0;
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100205};
206
207} // namespace webrtc
208
209#endif // API_TEST_NETWORK_EMULATION_MANAGER_H_