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