Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +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 | |
| 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/simulated_network.h" |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 18 | #include "api/units/data_rate.h" |
| 19 | #include "api/units/data_size.h" |
| 20 | #include "api/units/timestamp.h" |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 21 | #include "rtc_base/network.h" |
| 22 | #include "rtc_base/thread.h" |
| 23 | |
| 24 | namespace webrtc { |
| 25 | |
| 26 | // This API is still in development and can be changed without prior notice. |
| 27 | |
| 28 | // These classes are forward declared here, because they used as handles, to |
| 29 | // make it possible for client code to operate with these abstractions and build |
| 30 | // required network configuration. With forward declaration here implementation |
| 31 | // is more readable, than with interfaces approach and cause user needn't any |
| 32 | // API methods on these abstractions it is acceptable here. |
| 33 | |
| 34 | // EmulatedNetworkNode is an abstraction for some network in the real world, |
| 35 | // like 3G network between peers, or Wi-Fi for one peer and LTE for another. |
| 36 | // Multiple networks can be joined into chain emulating a network path from |
| 37 | // one peer to another. |
| 38 | class EmulatedNetworkNode; |
| 39 | // EmulatedEndpoint is and abstraction for network interface on device. |
| 40 | class EmulatedEndpoint; |
| 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; |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 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 | }; |
| 56 | |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 57 | struct EmulatedNetworkStats { |
| 58 | int64_t packets_sent = 0; |
| 59 | DataSize bytes_sent = DataSize::Zero(); |
| 60 | // Total amount of packets received with or without destination. |
| 61 | int64_t packets_received = 0; |
| 62 | // Total amount of bytes in received packets. |
| 63 | DataSize bytes_received = DataSize::Zero(); |
| 64 | // Total amount of packets that were received, but no destination was found. |
| 65 | int64_t packets_dropped = 0; |
| 66 | // Total amount of bytes in dropped packets. |
| 67 | DataSize bytes_dropped = DataSize::Zero(); |
| 68 | |
| 69 | DataSize first_received_packet_size = DataSize::Zero(); |
| 70 | DataSize first_sent_packet_size = DataSize::Zero(); |
| 71 | |
| 72 | Timestamp first_packet_sent_time = Timestamp::PlusInfinity(); |
| 73 | Timestamp last_packet_sent_time = Timestamp::PlusInfinity(); |
| 74 | Timestamp first_packet_received_time = Timestamp::PlusInfinity(); |
| 75 | Timestamp last_packet_received_time = Timestamp::PlusInfinity(); |
| 76 | |
| 77 | DataRate AverageSendRate() const { |
| 78 | RTC_DCHECK_GE(packets_sent, 2); |
| 79 | return (bytes_sent - first_sent_packet_size) / |
| 80 | (last_packet_sent_time - first_packet_sent_time); |
| 81 | } |
| 82 | DataRate AverageReceiveRate() const { |
| 83 | RTC_DCHECK_GE(packets_received, 2); |
| 84 | return (bytes_received - first_received_packet_size) / |
| 85 | (last_packet_received_time - first_packet_received_time); |
| 86 | } |
| 87 | }; |
| 88 | |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 89 | // Provide interface to obtain all required objects to inject network emulation |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 90 | // layer into PeerConnection. Also contains information about network interfaces |
| 91 | // accessible by PeerConnection. |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 92 | class EmulatedNetworkManagerInterface { |
| 93 | public: |
| 94 | virtual ~EmulatedNetworkManagerInterface() = default; |
| 95 | |
| 96 | virtual rtc::Thread* network_thread() = 0; |
| 97 | virtual rtc::NetworkManager* network_manager() = 0; |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 98 | |
| 99 | // Returns summarized network stats for endpoints for this manager. |
| 100 | virtual void GetStats( |
| 101 | std::function<void(EmulatedNetworkStats)> stats_callback) const = 0; |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | // Provides an API for creating and configuring emulated network layer. |
| 105 | // All objects returned by this API are owned by NetworkEmulationManager itself |
| 106 | // and will be deleted when manager will be deleted. |
| 107 | class NetworkEmulationManager { |
| 108 | public: |
| 109 | virtual ~NetworkEmulationManager() = default; |
| 110 | |
| 111 | // Creates an emulated network node, which represents single network in |
| 112 | // the emulated network layer. |
| 113 | virtual EmulatedNetworkNode* CreateEmulatedNode( |
| 114 | std::unique_ptr<NetworkBehaviorInterface> network_behavior) = 0; |
| 115 | |
| 116 | // Creates an emulated endpoint, which represents single network interface on |
| 117 | // the peer's device. |
| 118 | virtual EmulatedEndpoint* CreateEndpoint(EmulatedEndpointConfig config) = 0; |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 119 | // Enable emulated endpoint to make it available for webrtc. |
| 120 | // Caller mustn't enable currently enabled endpoint. |
| 121 | virtual void EnableEndpoint(EmulatedEndpoint* endpoint) = 0; |
| 122 | // Disable emulated endpoint to make it unavailable for webrtc. |
| 123 | // Caller mustn't disable currently disabled endpoint. |
| 124 | virtual void DisableEndpoint(EmulatedEndpoint* endpoint) = 0; |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 125 | |
| 126 | // Creates a route between endpoints going through specified network nodes. |
| 127 | // This route is single direction only and describe how traffic that was |
| 128 | // sent by network interface |from| have to be delivered to the network |
Artem Titov | ff39312 | 2019-04-05 11:19:52 +0200 | [diff] [blame] | 129 | // interface |to|. Return object can be used to remove created route. The |
| 130 | // route must contains at least one network node inside it. |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 131 | // |
Artem Titov | ff39312 | 2019-04-05 11:19:52 +0200 | [diff] [blame] | 132 | // Assume that E{0-9} are endpoints and N{0-9} are network nodes, then |
| 133 | // creation of the route have to follow these rules: |
| 134 | // 1. A route consists of a source endpoint, an ordered list of one or |
| 135 | // more network nodes, and a destination endpoint. |
| 136 | // 2. If (E1, ..., E2) is a route, then E1 != E2. |
| 137 | // In other words, the source and the destination may not be the same. |
| 138 | // 3. Given two simultaneously existing routes (E1, ..., E2) and |
| 139 | // (E3, ..., E4), either E1 != E3 or E2 != E4. |
| 140 | // In other words, there may be at most one route from any given source |
| 141 | // endpoint to any given destination endpoint. |
| 142 | // 4. Given two simultaneously existing routes (E1, ..., N1, ..., E2) |
| 143 | // and (E3, ..., N2, ..., E4), either N1 != N2 or E2 != E4. |
| 144 | // In other words, a network node may not belong to two routes that lead |
| 145 | // to the same destination endpoint. |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 146 | virtual EmulatedRoute* CreateRoute( |
| 147 | EmulatedEndpoint* from, |
| 148 | const std::vector<EmulatedNetworkNode*>& via_nodes, |
| 149 | EmulatedEndpoint* to) = 0; |
| 150 | // Removes route previously created by CreateRoute(...). |
| 151 | // Caller mustn't call this function with route, that have been already |
| 152 | // removed earlier. |
| 153 | virtual void ClearRoute(EmulatedRoute* route) = 0; |
| 154 | |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 155 | // Creates EmulatedNetworkManagerInterface which can be used then to inject |
| 156 | // network emulation layer into PeerConnection. |endpoints| - are available |
| 157 | // network interfaces for PeerConnection. If endpoint is enabled, it will be |
| 158 | // immediately available for PeerConnection, otherwise user will be able to |
| 159 | // enable endpoint later to make it available for PeerConnection. |
| 160 | virtual EmulatedNetworkManagerInterface* |
| 161 | CreateEmulatedNetworkManagerInterface( |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 162 | const std::vector<EmulatedEndpoint*>& endpoints) = 0; |
| 163 | }; |
| 164 | |
| 165 | } // namespace webrtc |
| 166 | |
| 167 | #endif // API_TEST_NETWORK_EMULATION_MANAGER_H_ |