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" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 27 | #include "rtc_base/socket_address.h" |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 28 | #include "rtc_base/thread.h" |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 29 | #include "system_wrappers/include/clock.h" |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 30 | |
| 31 | namespace webrtc { |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 32 | namespace test { |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 33 | |
| 34 | struct EmulatedIpPacket { |
| 35 | public: |
| 36 | EmulatedIpPacket(const rtc::SocketAddress& from, |
| 37 | const rtc::SocketAddress& to, |
| 38 | uint64_t dest_endpoint_id, |
| 39 | rtc::CopyOnWriteBuffer data, |
| 40 | Timestamp arrival_time); |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 41 | ~EmulatedIpPacket(); |
| 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. |
| 46 | EmulatedIpPacket(EmulatedIpPacket&&); |
| 47 | EmulatedIpPacket& operator=(EmulatedIpPacket&&); |
| 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; |
| 54 | uint64_t dest_endpoint_id; |
| 55 | rtc::CopyOnWriteBuffer data; |
| 56 | Timestamp arrival_time; |
| 57 | }; |
| 58 | |
| 59 | class EmulatedNetworkReceiverInterface { |
| 60 | public: |
| 61 | virtual ~EmulatedNetworkReceiverInterface() = default; |
| 62 | |
| 63 | virtual void OnPacketReceived(EmulatedIpPacket packet) = 0; |
| 64 | }; |
| 65 | |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 66 | // Represents node in the emulated network. Nodes can be connected with each |
| 67 | // other to form different networks with different behavior. The behavior of |
| 68 | // the node itself is determined by a concrete implementation of |
| 69 | // NetworkBehaviorInterface that is provided on construction. |
| 70 | class EmulatedNetworkNode : public EmulatedNetworkReceiverInterface { |
| 71 | public: |
| 72 | // Creates node based on |network_behavior|. The specified |packet_overhead| |
| 73 | // is added to the size of each packet in the information provided to |
| 74 | // |network_behavior|. |
Sebastian Jansson | 8c8feb9 | 2019-01-29 15:59:17 +0100 | [diff] [blame] | 75 | explicit EmulatedNetworkNode( |
| 76 | std::unique_ptr<NetworkBehaviorInterface> network_behavior); |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 77 | ~EmulatedNetworkNode() override; |
| 78 | RTC_DISALLOW_COPY_AND_ASSIGN(EmulatedNetworkNode); |
| 79 | |
| 80 | void OnPacketReceived(EmulatedIpPacket packet) override; |
| 81 | void Process(Timestamp at_time); |
| 82 | void SetReceiver(uint64_t dest_endpoint_id, |
| 83 | EmulatedNetworkReceiverInterface* receiver); |
| 84 | void RemoveReceiver(uint64_t dest_endpoint_id); |
| 85 | |
| 86 | // Creates a route for the given receiver_id over all the given nodes to the |
| 87 | // given receiver. |
| 88 | static void CreateRoute(uint64_t receiver_id, |
| 89 | std::vector<EmulatedNetworkNode*> nodes, |
| 90 | EmulatedNetworkReceiverInterface* receiver); |
| 91 | static void ClearRoute(uint64_t receiver_id, |
| 92 | std::vector<EmulatedNetworkNode*> nodes); |
| 93 | |
| 94 | private: |
| 95 | struct StoredPacket { |
| 96 | uint64_t id; |
| 97 | EmulatedIpPacket packet; |
| 98 | bool removed; |
| 99 | }; |
| 100 | |
| 101 | rtc::CriticalSection lock_; |
| 102 | std::map<uint64_t, EmulatedNetworkReceiverInterface*> routing_ |
| 103 | RTC_GUARDED_BY(lock_); |
| 104 | const std::unique_ptr<NetworkBehaviorInterface> network_behavior_ |
| 105 | RTC_GUARDED_BY(lock_); |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 106 | std::deque<StoredPacket> packets_ RTC_GUARDED_BY(lock_); |
| 107 | |
| 108 | uint64_t next_packet_id_ RTC_GUARDED_BY(lock_) = 1; |
| 109 | }; |
| 110 | |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 111 | // Represents single network interface on the device. |
| 112 | // It will be used as sender from socket side to send data to the network and |
| 113 | // will act as packet receiver from emulated network side to receive packets |
| 114 | // from other EmulatedNetworkNodes. |
Artem Titov | aba8dc2 | 2019-03-11 10:08:40 +0100 | [diff] [blame] | 115 | class EmulatedEndpoint : public EmulatedNetworkReceiverInterface { |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 116 | public: |
Artem Titov | aba8dc2 | 2019-03-11 10:08:40 +0100 | [diff] [blame] | 117 | EmulatedEndpoint(uint64_t id, rtc::IPAddress, Clock* clock); |
| 118 | ~EmulatedEndpoint() override; |
Artem Titov | 0774bd9 | 2019-01-30 15:26:05 +0100 | [diff] [blame] | 119 | |
| 120 | uint64_t GetId() const; |
| 121 | |
| 122 | // Set network node, that will be used to send packets to the network. |
| 123 | void SetSendNode(EmulatedNetworkNode* send_node); |
| 124 | // Send packet into network. |
| 125 | // |from| will be used to set source address for the packet in destination |
| 126 | // socket. |
| 127 | // |to| will be used for routing verification and picking right socket by port |
| 128 | // on destination endpoint. |
| 129 | void SendPacket(const rtc::SocketAddress& from, |
| 130 | const rtc::SocketAddress& to, |
| 131 | rtc::CopyOnWriteBuffer packet); |
| 132 | |
| 133 | // Binds receiver to this endpoint to send and receive data. |
| 134 | // |desired_port| is a port that should be used. If it is equal to 0, |
| 135 | // endpoint will pick the first available port starting from |
| 136 | // |kFirstEphemeralPort|. |
| 137 | // |
| 138 | // Returns the port, that should be used (it will be equals to desired, if |
| 139 | // |desired_port| != 0 and is free or will be the one, selected by endpoint) |
| 140 | // or absl::nullopt if desired_port in used. Also fails if there are no more |
| 141 | // free ports to bind to. |
| 142 | absl::optional<uint16_t> BindReceiver( |
| 143 | uint16_t desired_port, |
| 144 | EmulatedNetworkReceiverInterface* receiver); |
| 145 | void UnbindReceiver(uint16_t port); |
| 146 | |
| 147 | rtc::IPAddress GetPeerLocalAddress() const; |
| 148 | |
| 149 | // Will be called to deliver packet into endpoint from network node. |
| 150 | void OnPacketReceived(EmulatedIpPacket packet) override; |
| 151 | |
| 152 | protected: |
| 153 | friend class NetworkEmulationManager; |
| 154 | |
| 155 | EmulatedNetworkNode* GetSendNode() const; |
| 156 | void SetConnectedEndpointId(uint64_t endpoint_id); |
| 157 | |
| 158 | private: |
| 159 | static constexpr uint16_t kFirstEphemeralPort = 49152; |
| 160 | uint16_t NextPort() RTC_EXCLUSIVE_LOCKS_REQUIRED(receiver_lock_); |
| 161 | |
| 162 | rtc::CriticalSection receiver_lock_; |
| 163 | |
| 164 | uint64_t id_; |
| 165 | // Peer's local IP address for this endpoint network interface. |
| 166 | const rtc::IPAddress peer_local_addr_; |
| 167 | EmulatedNetworkNode* send_node_; |
| 168 | Clock* const clock_; |
| 169 | |
| 170 | uint16_t next_port_ RTC_GUARDED_BY(receiver_lock_); |
| 171 | std::map<uint16_t, EmulatedNetworkReceiverInterface*> port_to_receiver_ |
| 172 | RTC_GUARDED_BY(receiver_lock_); |
| 173 | |
| 174 | absl::optional<uint64_t> connected_endpoint_id_; |
| 175 | }; |
| 176 | |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 177 | } // namespace test |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 178 | } // namespace webrtc |
| 179 | |
| 180 | #endif // TEST_SCENARIO_NETWORK_NETWORK_EMULATION_H_ |