network_emulation_manager.h 9.6 KB

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