Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 TEST_SCENARIO_NETWORK_NETWORK_EMULATION_H_ |
| 12 | #define TEST_SCENARIO_NETWORK_NETWORK_EMULATION_H_ |
| 13 | |
| 14 | #include <cstdint> |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 15 | #include <map> |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 16 | #include <memory> |
| 17 | #include <string> |
| 18 | #include <utility> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "absl/types/optional.h" |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 22 | #include "api/test/network_emulation_manager.h" |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 23 | #include "api/test/simulated_network.h" |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 24 | #include "api/units/timestamp.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/copy_on_write_buffer.h" |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 26 | #include "rtc_base/network.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 27 | #include "rtc_base/socket_address.h" |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 28 | #include "rtc_base/task_queue_for_test.h" |
| 29 | #include "rtc_base/task_utils/repeating_task.h" |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 30 | #include "rtc_base/thread_checker.h" |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 31 | #include "system_wrappers/include/clock.h" |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 32 | |
| 33 | namespace webrtc { |
Artem Titov | 7bf8c7f | 2019-03-15 15:00:37 +0100 | [diff] [blame] | 34 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 35 | struct EmulatedIpPacket { |
| 36 | public: |
| 37 | EmulatedIpPacket(const rtc::SocketAddress& from, |
| 38 | const rtc::SocketAddress& to, |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 39 | rtc::CopyOnWriteBuffer data, |
| 40 | Timestamp arrival_time); |
Artem Titov | 612e179 | 2019-04-01 14:43:38 +0200 | [diff] [blame] | 41 | ~EmulatedIpPacket() = default; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 42 | // This object is not copyable or assignable. |
| 43 | EmulatedIpPacket(const EmulatedIpPacket&) = delete; |
| 44 | EmulatedIpPacket& operator=(const EmulatedIpPacket&) = delete; |
| 45 | // This object is only moveable. |
Artem Titov | 612e179 | 2019-04-01 14:43:38 +0200 | [diff] [blame] | 46 | EmulatedIpPacket(EmulatedIpPacket&&) = default; |
| 47 | EmulatedIpPacket& operator=(EmulatedIpPacket&&) = default; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 48 | |
| 49 | size_t size() const { return data.size(); } |
| 50 | const uint8_t* cdata() const { return data.cdata(); } |
| 51 | |
| 52 | rtc::SocketAddress from; |
| 53 | rtc::SocketAddress to; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 54 | rtc::CopyOnWriteBuffer data; |
| 55 | Timestamp arrival_time; |
| 56 | }; |
| 57 | |
| 58 | class EmulatedNetworkReceiverInterface { |
| 59 | public: |
| 60 | virtual ~EmulatedNetworkReceiverInterface() = default; |
| 61 | |
| 62 | virtual void OnPacketReceived(EmulatedIpPacket packet) = 0; |
| 63 | }; |
| 64 | |
Sebastian Jansson | 62bb47f | 2019-04-01 18:23:58 +0200 | [diff] [blame] | 65 | class LinkEmulation : public EmulatedNetworkReceiverInterface { |
| 66 | public: |
| 67 | LinkEmulation(Clock* clock, |
| 68 | rtc::TaskQueue* task_queue, |
| 69 | std::unique_ptr<NetworkBehaviorInterface> network_behavior, |
| 70 | EmulatedNetworkReceiverInterface* receiver) |
| 71 | : clock_(clock), |
| 72 | task_queue_(task_queue), |
| 73 | network_behavior_(std::move(network_behavior)), |
| 74 | receiver_(receiver) {} |
| 75 | void OnPacketReceived(EmulatedIpPacket packet) override; |
| 76 | |
| 77 | private: |
| 78 | struct StoredPacket { |
| 79 | uint64_t id; |
| 80 | EmulatedIpPacket packet; |
| 81 | bool removed; |
| 82 | }; |
| 83 | void Process(Timestamp at_time) RTC_RUN_ON(task_queue_); |
| 84 | void HandlePacketReceived(EmulatedIpPacket packet) RTC_RUN_ON(task_queue_); |
| 85 | |
| 86 | Clock* const clock_; |
| 87 | rtc::TaskQueue* const task_queue_; |
| 88 | const std::unique_ptr<NetworkBehaviorInterface> network_behavior_ |
| 89 | RTC_GUARDED_BY(task_queue_); |
| 90 | EmulatedNetworkReceiverInterface* const receiver_; |
| 91 | RepeatingTaskHandle process_task_ RTC_GUARDED_BY(task_queue_); |
| 92 | std::deque<StoredPacket> packets_ RTC_GUARDED_BY(task_queue_); |
| 93 | uint64_t next_packet_id_ RTC_GUARDED_BY(task_queue_) = 1; |
| 94 | }; |
| 95 | |
| 96 | class NetworkRouterNode : public EmulatedNetworkReceiverInterface { |
| 97 | public: |
| 98 | explicit NetworkRouterNode(rtc::TaskQueue* task_queue); |
| 99 | |
| 100 | void OnPacketReceived(EmulatedIpPacket packet) override; |
| 101 | void SetReceiver(rtc::IPAddress dest_ip, |
| 102 | EmulatedNetworkReceiverInterface* receiver); |
| 103 | void RemoveReceiver(rtc::IPAddress dest_ip); |
| 104 | |
| 105 | private: |
| 106 | rtc::TaskQueue* const task_queue_; |
| 107 | std::map<rtc::IPAddress, EmulatedNetworkReceiverInterface*> routing_ |
| 108 | RTC_GUARDED_BY(task_queue_); |
| 109 | }; |
| 110 | |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 111 | // Represents node in the emulated network. Nodes can be connected with each |
| 112 | // other to form different networks with different behavior. The behavior of |
| 113 | // the node itself is determined by a concrete implementation of |
| 114 | // NetworkBehaviorInterface that is provided on construction. |
| 115 | class EmulatedNetworkNode : public EmulatedNetworkReceiverInterface { |
| 116 | public: |
| 117 | // Creates node based on |network_behavior|. The specified |packet_overhead| |
| 118 | // is added to the size of each packet in the information provided to |
| 119 | // |network_behavior|. |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 120 | // |task_queue| is used to process packets and to forward the packets when |
| 121 | // they are ready. |
Sebastian Jansson | 62bb47f | 2019-04-01 18:23:58 +0200 | [diff] [blame] | 122 | EmulatedNetworkNode( |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 123 | Clock* clock, |
| 124 | rtc::TaskQueue* task_queue, |
Sebastian Jansson | 8c8feb9 | 2019-01-29 15:59:17 +0100 | [diff] [blame] | 125 | std::unique_ptr<NetworkBehaviorInterface> network_behavior); |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 126 | ~EmulatedNetworkNode() override; |
| 127 | RTC_DISALLOW_COPY_AND_ASSIGN(EmulatedNetworkNode); |
| 128 | |
| 129 | void OnPacketReceived(EmulatedIpPacket packet) override; |
Sebastian Jansson | 62bb47f | 2019-04-01 18:23:58 +0200 | [diff] [blame] | 130 | |
| 131 | LinkEmulation* link() { return &link_; } |
| 132 | NetworkRouterNode* router() { return &router_; } |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 133 | |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 134 | // Creates a route for the given receiver_ip over all the given nodes to the |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 135 | // given receiver. |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 136 | static void CreateRoute(rtc::IPAddress receiver_ip, |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 137 | std::vector<EmulatedNetworkNode*> nodes, |
| 138 | EmulatedNetworkReceiverInterface* receiver); |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 139 | static void ClearRoute(rtc::IPAddress receiver_ip, |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 140 | std::vector<EmulatedNetworkNode*> nodes); |
| 141 | |
| 142 | private: |
Sebastian Jansson | 62bb47f | 2019-04-01 18:23:58 +0200 | [diff] [blame] | 143 | NetworkRouterNode router_; |
| 144 | LinkEmulation link_; |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 147 | // Represents single network interface on the device. |
| 148 | // It will be used as sender from socket side to send data to the network and |
| 149 | // will act as packet receiver from emulated network side to receive packets |
| 150 | // from other EmulatedNetworkNodes. |
Artem Titov | aba8dc2 | 2019-03-11 10:08:40 +0100 | [diff] [blame] | 151 | class EmulatedEndpoint : public EmulatedNetworkReceiverInterface { |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 152 | public: |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 153 | EmulatedEndpoint(uint64_t id, |
Artem Titov | 612e179 | 2019-04-01 14:43:38 +0200 | [diff] [blame] | 154 | const rtc::IPAddress& ip, |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 155 | bool is_enabled, |
Artem Titov | ff39312 | 2019-04-05 11:19:52 +0200 | [diff] [blame] | 156 | rtc::TaskQueue* task_queue, |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 157 | Clock* clock); |
Artem Titov | aba8dc2 | 2019-03-11 10:08:40 +0100 | [diff] [blame] | 158 | ~EmulatedEndpoint() override; |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 159 | |
| 160 | uint64_t GetId() const; |
| 161 | |
Artem Titov | ff39312 | 2019-04-05 11:19:52 +0200 | [diff] [blame] | 162 | NetworkRouterNode* router() { return &router_; } |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 163 | // Send packet into network. |
| 164 | // |from| will be used to set source address for the packet in destination |
| 165 | // socket. |
| 166 | // |to| will be used for routing verification and picking right socket by port |
| 167 | // on destination endpoint. |
| 168 | void SendPacket(const rtc::SocketAddress& from, |
| 169 | const rtc::SocketAddress& to, |
| 170 | rtc::CopyOnWriteBuffer packet); |
| 171 | |
| 172 | // Binds receiver to this endpoint to send and receive data. |
| 173 | // |desired_port| is a port that should be used. If it is equal to 0, |
| 174 | // endpoint will pick the first available port starting from |
| 175 | // |kFirstEphemeralPort|. |
| 176 | // |
| 177 | // Returns the port, that should be used (it will be equals to desired, if |
| 178 | // |desired_port| != 0 and is free or will be the one, selected by endpoint) |
| 179 | // or absl::nullopt if desired_port in used. Also fails if there are no more |
| 180 | // free ports to bind to. |
| 181 | absl::optional<uint16_t> BindReceiver( |
| 182 | uint16_t desired_port, |
| 183 | EmulatedNetworkReceiverInterface* receiver); |
| 184 | void UnbindReceiver(uint16_t port); |
| 185 | |
| 186 | rtc::IPAddress GetPeerLocalAddress() const; |
| 187 | |
| 188 | // Will be called to deliver packet into endpoint from network node. |
| 189 | void OnPacketReceived(EmulatedIpPacket packet) override; |
| 190 | |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 191 | void Enable(); |
| 192 | void Disable(); |
| 193 | bool Enabled() const; |
| 194 | |
| 195 | const rtc::Network& network() const { return *network_.get(); } |
| 196 | |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 197 | EmulatedNetworkStats stats(); |
| 198 | |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 199 | private: |
| 200 | static constexpr uint16_t kFirstEphemeralPort = 49152; |
| 201 | uint16_t NextPort() RTC_EXCLUSIVE_LOCKS_REQUIRED(receiver_lock_); |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 202 | void UpdateSendStats(const EmulatedIpPacket& packet); |
| 203 | void UpdateReceiveStats(const EmulatedIpPacket& packet); |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 204 | |
| 205 | rtc::CriticalSection receiver_lock_; |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 206 | rtc::ThreadChecker enabled_state_checker_; |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 207 | |
| 208 | uint64_t id_; |
| 209 | // Peer's local IP address for this endpoint network interface. |
| 210 | const rtc::IPAddress peer_local_addr_; |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 211 | bool is_enabled_ RTC_GUARDED_BY(enabled_state_checker_); |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 212 | Clock* const clock_; |
Artem Titov | ff39312 | 2019-04-05 11:19:52 +0200 | [diff] [blame] | 213 | rtc::TaskQueue* const task_queue_; |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 214 | std::unique_ptr<rtc::Network> network_; |
Artem Titov | ff39312 | 2019-04-05 11:19:52 +0200 | [diff] [blame] | 215 | NetworkRouterNode router_; |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 216 | |
| 217 | uint16_t next_port_ RTC_GUARDED_BY(receiver_lock_); |
| 218 | std::map<uint16_t, EmulatedNetworkReceiverInterface*> port_to_receiver_ |
| 219 | RTC_GUARDED_BY(receiver_lock_); |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 220 | |
| 221 | EmulatedNetworkStats stats_ RTC_GUARDED_BY(task_queue_); |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 222 | }; |
| 223 | |
Artem Titov | fc6ab00 | 2019-03-12 13:48:32 +0100 | [diff] [blame] | 224 | class EmulatedRoute { |
| 225 | public: |
| 226 | EmulatedRoute(EmulatedEndpoint* from, |
| 227 | std::vector<EmulatedNetworkNode*> via_nodes, |
| 228 | EmulatedEndpoint* to) |
| 229 | : from(from), via_nodes(std::move(via_nodes)), to(to), active(true) {} |
| 230 | |
| 231 | EmulatedEndpoint* from; |
| 232 | std::vector<EmulatedNetworkNode*> via_nodes; |
| 233 | EmulatedEndpoint* to; |
| 234 | bool active; |
| 235 | }; |
| 236 | |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 237 | class EndpointsContainer { |
| 238 | public: |
| 239 | EndpointsContainer(const std::vector<EmulatedEndpoint*>& endpoints); |
| 240 | |
| 241 | EmulatedEndpoint* LookupByLocalAddress(const rtc::IPAddress& local_ip) const; |
| 242 | bool HasEndpoint(EmulatedEndpoint* endpoint) const; |
| 243 | // Returns list of networks for enabled endpoints. Caller takes ownership of |
| 244 | // returned rtc::Network objects. |
| 245 | std::vector<std::unique_ptr<rtc::Network>> GetEnabledNetworks() const; |
Artem Titov | 806299e | 2019-04-12 12:17:19 +0200 | [diff] [blame] | 246 | EmulatedNetworkStats GetStats() const; |
Artem Titov | e5cc85b | 2019-03-28 12:11:09 +0100 | [diff] [blame] | 247 | |
| 248 | private: |
| 249 | const std::vector<EmulatedEndpoint*> endpoints_; |
| 250 | }; |
| 251 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 252 | } // namespace webrtc |
| 253 | |
| 254 | #endif // TEST_SCENARIO_NETWORK_NETWORK_EMULATION_H_ |