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