• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #ifndef API_TEST_NETWORK_EMULATION_MANAGER_H_
12 #define API_TEST_NETWORK_EMULATION_MANAGER_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "api/test/network_emulation/network_emulation_interfaces.h"
18 #include "api/test/simulated_network.h"
19 #include "api/test/time_controller.h"
20 #include "api/units/timestamp.h"
21 #include "rtc_base/network.h"
22 #include "rtc_base/network_constants.h"
23 #include "rtc_base/thread.h"
24 
25 namespace webrtc {
26 
27 // This API is still in development and can be changed without prior notice.
28 
29 // These classes are forward declared here, because they used as handles, to
30 // make it possible for client code to operate with these abstractions and build
31 // required network configuration. With forward declaration here implementation
32 // is more readable, than with interfaces approach and cause user needn't any
33 // API methods on these abstractions it is acceptable here.
34 
35 // EmulatedNetworkNode is an abstraction for some network in the real world,
36 // like 3G network between peers, or Wi-Fi for one peer and LTE for another.
37 // Multiple networks can be joined into chain emulating a network path from
38 // one peer to another.
39 class EmulatedNetworkNode;
40 
41 // EmulatedRoute is handle for single route from one network interface on one
42 // peer device to another network interface on another peer device.
43 class EmulatedRoute;
44 
45 struct EmulatedEndpointConfig {
46   enum class IpAddressFamily { kIpv4, kIpv6 };
47 
48   IpAddressFamily generated_ip_family = IpAddressFamily::kIpv4;
49   // If specified will be used as IP address for endpoint node. Must be unique
50   // among all created nodes.
51   absl::optional<rtc::IPAddress> ip;
52   // Should endpoint be enabled or not, when it will be created.
53   // Enabled endpoints will be available for webrtc to send packets.
54   bool start_as_enabled = true;
55   // Network type which will be used to represent endpoint to WebRTC.
56   rtc::AdapterType type = rtc::AdapterType::ADAPTER_TYPE_UNKNOWN;
57 };
58 
59 
60 // Provide interface to obtain all required objects to inject network emulation
61 // layer into PeerConnection. Also contains information about network interfaces
62 // accessible by PeerConnection.
63 class EmulatedNetworkManagerInterface {
64  public:
65   virtual ~EmulatedNetworkManagerInterface() = default;
66 
67   virtual rtc::Thread* network_thread() = 0;
68   virtual rtc::NetworkManager* network_manager() = 0;
69 
70   // Returns summarized network stats for endpoints for this manager.
71   virtual void GetStats(
72       std::function<void(EmulatedNetworkStats)> stats_callback) const = 0;
73 };
74 
75 enum class TimeMode { kRealTime, kSimulated };
76 
77 // Provides an API for creating and configuring emulated network layer.
78 // All objects returned by this API are owned by NetworkEmulationManager itself
79 // and will be deleted when manager will be deleted.
80 class NetworkEmulationManager {
81  public:
82   // Helper struct to simplify creation of simulated network behaviors. Contains
83   // non-owning pointers as the underlying instances are owned by the manager.
84   struct SimulatedNetworkNode {
85     SimulatedNetworkInterface* simulation;
86     EmulatedNetworkNode* node;
87 
88     class Builder {
89      public:
BuilderSimulatedNetworkNode90       explicit Builder(NetworkEmulationManager* net) : net_(net) {}
BuilderSimulatedNetworkNode91       Builder() : net_(nullptr) {}
92       Builder(const Builder&) = default;
93       // Sets the config state, note that this will replace any previously set
94       // values.
95       Builder& config(BuiltInNetworkBehaviorConfig config);
96       Builder& delay_ms(int queue_delay_ms);
97       Builder& capacity_kbps(int link_capacity_kbps);
98       Builder& capacity_Mbps(int link_capacity_Mbps);
99       Builder& loss(double loss_rate);
100       Builder& packet_queue_length(int max_queue_length_in_packets);
101       SimulatedNetworkNode Build() const;
102       SimulatedNetworkNode Build(NetworkEmulationManager* net) const;
103 
104      private:
105       NetworkEmulationManager* const net_;
106       BuiltInNetworkBehaviorConfig config_;
107     };
108   };
109   virtual ~NetworkEmulationManager() = default;
110 
111   virtual TimeController* time_controller() = 0;
112 
113   // Creates an emulated network node, which represents single network in
114   // the emulated network layer.
115   virtual EmulatedNetworkNode* CreateEmulatedNode(
116       BuiltInNetworkBehaviorConfig config) = 0;
117   virtual EmulatedNetworkNode* CreateEmulatedNode(
118       std::unique_ptr<NetworkBehaviorInterface> network_behavior) = 0;
119 
120   virtual SimulatedNetworkNode::Builder NodeBuilder() = 0;
121 
122   // Creates an emulated endpoint, which represents single network interface on
123   // the peer's device.
124   virtual EmulatedEndpoint* CreateEndpoint(EmulatedEndpointConfig config) = 0;
125   // Enable emulated endpoint to make it available for webrtc.
126   // Caller mustn't enable currently enabled endpoint.
127   virtual void EnableEndpoint(EmulatedEndpoint* endpoint) = 0;
128   // Disable emulated endpoint to make it unavailable for webrtc.
129   // Caller mustn't disable currently disabled endpoint.
130   virtual void DisableEndpoint(EmulatedEndpoint* endpoint) = 0;
131 
132   // Creates a route between endpoints going through specified network nodes.
133   // This route is single direction only and describe how traffic that was
134   // sent by network interface |from| have to be delivered to the network
135   // interface |to|. Return object can be used to remove created route. The
136   // route must contains at least one network node inside it.
137   //
138   // Assume that E{0-9} are endpoints and N{0-9} are network nodes, then
139   // creation of the route have to follow these rules:
140   //   1. A route consists of a source endpoint, an ordered list of one or
141   //      more network nodes, and a destination endpoint.
142   //   2. If (E1, ..., E2) is a route, then E1 != E2.
143   //      In other words, the source and the destination may not be the same.
144   //   3. Given two simultaneously existing routes (E1, ..., E2) and
145   //      (E3, ..., E4), either E1 != E3 or E2 != E4.
146   //      In other words, there may be at most one route from any given source
147   //      endpoint to any given destination endpoint.
148   //   4. Given two simultaneously existing routes (E1, ..., N1, ..., E2)
149   //      and (E3, ..., N2, ..., E4), either N1 != N2 or E2 != E4.
150   //      In other words, a network node may not belong to two routes that lead
151   //      to the same destination endpoint.
152   virtual EmulatedRoute* CreateRoute(
153       EmulatedEndpoint* from,
154       const std::vector<EmulatedNetworkNode*>& via_nodes,
155       EmulatedEndpoint* to) = 0;
156 
157   // Creates a route over the given |via_nodes| creating the required endpoints
158   // in the process. The returned EmulatedRoute pointer can be used in other
159   // calls as a transport route for message or cross traffic.
160   virtual EmulatedRoute* CreateRoute(
161       const std::vector<EmulatedNetworkNode*>& via_nodes) = 0;
162 
163   // Removes route previously created by CreateRoute(...).
164   // Caller mustn't call this function with route, that have been already
165   // removed earlier.
166   virtual void ClearRoute(EmulatedRoute* route) = 0;
167 
168   // Creates a simulated TCP connection using |send_route| for traffic and
169   // |ret_route| for feedback. This can be used to emulate HTTP cross traffic
170   // and to implement realistic reliable signaling over lossy networks.
171   // TODO(srte): Handle clearing of the routes involved.
172   virtual TcpMessageRoute* CreateTcpRoute(EmulatedRoute* send_route,
173                                           EmulatedRoute* ret_route) = 0;
174 
175   // Creates EmulatedNetworkManagerInterface which can be used then to inject
176   // network emulation layer into PeerConnection. |endpoints| - are available
177   // network interfaces for PeerConnection. If endpoint is enabled, it will be
178   // immediately available for PeerConnection, otherwise user will be able to
179   // enable endpoint later to make it available for PeerConnection.
180   virtual EmulatedNetworkManagerInterface*
181   CreateEmulatedNetworkManagerInterface(
182       const std::vector<EmulatedEndpoint*>& endpoints) = 0;
183 };
184 
185 }  // namespace webrtc
186 
187 #endif  // API_TEST_NETWORK_EMULATION_MANAGER_H_
188