blob: 309d51163f299576df63f22aa2db42e9b8f5e3be [file] [log] [blame]
Artem Titov40f51152019-01-04 15:45:01 +01001/*
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 Titov37d18482019-01-08 15:41:45 +010015#include <map>
Artem Titov40f51152019-01-04 15:45:01 +010016#include <memory>
17#include <string>
18#include <utility>
19#include <vector>
20
21#include "absl/types/optional.h"
Artem Titov37d18482019-01-08 15:41:45 +010022#include "api/test/simulated_network.h"
Artem Titov40f51152019-01-04 15:45:01 +010023#include "api/units/timestamp.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/async_socket.h"
25#include "rtc_base/copy_on_write_buffer.h"
Artem Titov0774bd92019-01-30 15:26:05 +010026#include "rtc_base/critical_section.h"
Artem Titove5cc85b2019-03-28 12:11:09 +010027#include "rtc_base/network.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/socket_address.h"
Artem Titov40f51152019-01-04 15:45:01 +010029#include "rtc_base/thread.h"
Artem Titove5cc85b2019-03-28 12:11:09 +010030#include "rtc_base/thread_checker.h"
Artem Titov0774bd92019-01-30 15:26:05 +010031#include "system_wrappers/include/clock.h"
Artem Titov40f51152019-01-04 15:45:01 +010032
33namespace webrtc {
Artem Titov37d18482019-01-08 15:41:45 +010034namespace test {
Artem Titov40f51152019-01-04 15:45:01 +010035
Artem Titov7bf8c7f2019-03-15 15:00:37 +010036// Forward declare NetworkEmulationManagerImpl for friend access from
37// EmulatedEndpoint.
38class NetworkEmulationManagerImpl;
39
40} // namespace test
41
Artem Titov40f51152019-01-04 15:45:01 +010042struct EmulatedIpPacket {
43 public:
44 EmulatedIpPacket(const rtc::SocketAddress& from,
45 const rtc::SocketAddress& to,
46 uint64_t dest_endpoint_id,
47 rtc::CopyOnWriteBuffer data,
48 Timestamp arrival_time);
Artem Titov40f51152019-01-04 15:45:01 +010049 ~EmulatedIpPacket();
50 // This object is not copyable or assignable.
51 EmulatedIpPacket(const EmulatedIpPacket&) = delete;
52 EmulatedIpPacket& operator=(const EmulatedIpPacket&) = delete;
53 // This object is only moveable.
54 EmulatedIpPacket(EmulatedIpPacket&&);
55 EmulatedIpPacket& operator=(EmulatedIpPacket&&);
56
57 size_t size() const { return data.size(); }
58 const uint8_t* cdata() const { return data.cdata(); }
59
60 rtc::SocketAddress from;
61 rtc::SocketAddress to;
62 uint64_t dest_endpoint_id;
63 rtc::CopyOnWriteBuffer data;
64 Timestamp arrival_time;
65};
66
67class EmulatedNetworkReceiverInterface {
68 public:
69 virtual ~EmulatedNetworkReceiverInterface() = default;
70
71 virtual void OnPacketReceived(EmulatedIpPacket packet) = 0;
72};
73
Artem Titov37d18482019-01-08 15:41:45 +010074// Represents node in the emulated network. Nodes can be connected with each
75// other to form different networks with different behavior. The behavior of
76// the node itself is determined by a concrete implementation of
77// NetworkBehaviorInterface that is provided on construction.
78class EmulatedNetworkNode : public EmulatedNetworkReceiverInterface {
79 public:
80 // Creates node based on |network_behavior|. The specified |packet_overhead|
81 // is added to the size of each packet in the information provided to
82 // |network_behavior|.
Sebastian Jansson8c8feb92019-01-29 15:59:17 +010083 explicit EmulatedNetworkNode(
84 std::unique_ptr<NetworkBehaviorInterface> network_behavior);
Artem Titov37d18482019-01-08 15:41:45 +010085 ~EmulatedNetworkNode() override;
86 RTC_DISALLOW_COPY_AND_ASSIGN(EmulatedNetworkNode);
87
88 void OnPacketReceived(EmulatedIpPacket packet) override;
89 void Process(Timestamp at_time);
90 void SetReceiver(uint64_t dest_endpoint_id,
91 EmulatedNetworkReceiverInterface* receiver);
92 void RemoveReceiver(uint64_t dest_endpoint_id);
93
94 // Creates a route for the given receiver_id over all the given nodes to the
95 // given receiver.
96 static void CreateRoute(uint64_t receiver_id,
97 std::vector<EmulatedNetworkNode*> nodes,
98 EmulatedNetworkReceiverInterface* receiver);
99 static void ClearRoute(uint64_t receiver_id,
100 std::vector<EmulatedNetworkNode*> nodes);
101
102 private:
103 struct StoredPacket {
104 uint64_t id;
105 EmulatedIpPacket packet;
106 bool removed;
107 };
108
109 rtc::CriticalSection lock_;
110 std::map<uint64_t, EmulatedNetworkReceiverInterface*> routing_
111 RTC_GUARDED_BY(lock_);
112 const std::unique_ptr<NetworkBehaviorInterface> network_behavior_
113 RTC_GUARDED_BY(lock_);
Artem Titov37d18482019-01-08 15:41:45 +0100114 std::deque<StoredPacket> packets_ RTC_GUARDED_BY(lock_);
115
116 uint64_t next_packet_id_ RTC_GUARDED_BY(lock_) = 1;
117};
118
Artem Titov0774bd92019-01-30 15:26:05 +0100119// Represents single network interface on the device.
120// It will be used as sender from socket side to send data to the network and
121// will act as packet receiver from emulated network side to receive packets
122// from other EmulatedNetworkNodes.
Artem Titovaba8dc22019-03-11 10:08:40 +0100123class EmulatedEndpoint : public EmulatedNetworkReceiverInterface {
Artem Titov0774bd92019-01-30 15:26:05 +0100124 public:
Artem Titove5cc85b2019-03-28 12:11:09 +0100125 EmulatedEndpoint(uint64_t id,
126 rtc::IPAddress ip,
127 bool is_enabled,
128 Clock* clock);
Artem Titovaba8dc22019-03-11 10:08:40 +0100129 ~EmulatedEndpoint() override;
Artem Titov0774bd92019-01-30 15:26:05 +0100130
131 uint64_t GetId() const;
132
133 // Set network node, that will be used to send packets to the network.
134 void SetSendNode(EmulatedNetworkNode* send_node);
135 // Send packet into network.
136 // |from| will be used to set source address for the packet in destination
137 // socket.
138 // |to| will be used for routing verification and picking right socket by port
139 // on destination endpoint.
140 void SendPacket(const rtc::SocketAddress& from,
141 const rtc::SocketAddress& to,
142 rtc::CopyOnWriteBuffer packet);
143
144 // Binds receiver to this endpoint to send and receive data.
145 // |desired_port| is a port that should be used. If it is equal to 0,
146 // endpoint will pick the first available port starting from
147 // |kFirstEphemeralPort|.
148 //
149 // Returns the port, that should be used (it will be equals to desired, if
150 // |desired_port| != 0 and is free or will be the one, selected by endpoint)
151 // or absl::nullopt if desired_port in used. Also fails if there are no more
152 // free ports to bind to.
153 absl::optional<uint16_t> BindReceiver(
154 uint16_t desired_port,
155 EmulatedNetworkReceiverInterface* receiver);
156 void UnbindReceiver(uint16_t port);
157
158 rtc::IPAddress GetPeerLocalAddress() const;
159
160 // Will be called to deliver packet into endpoint from network node.
161 void OnPacketReceived(EmulatedIpPacket packet) override;
162
Artem Titove5cc85b2019-03-28 12:11:09 +0100163 void Enable();
164 void Disable();
165 bool Enabled() const;
166
167 const rtc::Network& network() const { return *network_.get(); }
168
Artem Titov0774bd92019-01-30 15:26:05 +0100169 protected:
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100170 friend class test::NetworkEmulationManagerImpl;
Artem Titov0774bd92019-01-30 15:26:05 +0100171
172 EmulatedNetworkNode* GetSendNode() const;
173 void SetConnectedEndpointId(uint64_t endpoint_id);
174
175 private:
176 static constexpr uint16_t kFirstEphemeralPort = 49152;
177 uint16_t NextPort() RTC_EXCLUSIVE_LOCKS_REQUIRED(receiver_lock_);
178
179 rtc::CriticalSection receiver_lock_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100180 rtc::ThreadChecker enabled_state_checker_;
Artem Titov0774bd92019-01-30 15:26:05 +0100181
182 uint64_t id_;
183 // Peer's local IP address for this endpoint network interface.
184 const rtc::IPAddress peer_local_addr_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100185 bool is_enabled_ RTC_GUARDED_BY(enabled_state_checker_);
Artem Titov0774bd92019-01-30 15:26:05 +0100186 EmulatedNetworkNode* send_node_;
187 Clock* const clock_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100188 std::unique_ptr<rtc::Network> network_;
Artem Titov0774bd92019-01-30 15:26:05 +0100189
190 uint16_t next_port_ RTC_GUARDED_BY(receiver_lock_);
191 std::map<uint16_t, EmulatedNetworkReceiverInterface*> port_to_receiver_
192 RTC_GUARDED_BY(receiver_lock_);
193
194 absl::optional<uint64_t> connected_endpoint_id_;
195};
196
Artem Titovfc6ab002019-03-12 13:48:32 +0100197class 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 Titove5cc85b2019-03-28 12:11:09 +0100210class 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 Titov40f51152019-01-04 15:45:01 +0100224} // namespace webrtc
225
226#endif // TEST_SCENARIO_NETWORK_NETWORK_EMULATION_H_