blob: 7505cbdccc063d8dc0240164d53aab4923fa9f89 [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/copy_on_write_buffer.h"
Artem Titove5cc85b2019-03-28 12:11:09 +010025#include "rtc_base/network.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/socket_address.h"
Sebastian Jansson4124dab2019-04-01 14:33:53 +020027#include "rtc_base/task_queue_for_test.h"
28#include "rtc_base/task_utils/repeating_task.h"
Artem Titove5cc85b2019-03-28 12:11:09 +010029#include "rtc_base/thread_checker.h"
Artem Titov0774bd92019-01-30 15:26:05 +010030#include "system_wrappers/include/clock.h"
Artem Titov40f51152019-01-04 15:45:01 +010031
32namespace webrtc {
Artem Titov37d18482019-01-08 15:41:45 +010033namespace test {
Artem Titov40f51152019-01-04 15:45:01 +010034
Artem Titov7bf8c7f2019-03-15 15:00:37 +010035// Forward declare NetworkEmulationManagerImpl for friend access from
36// EmulatedEndpoint.
37class NetworkEmulationManagerImpl;
38
39} // namespace test
40
Artem Titov40f51152019-01-04 15:45:01 +010041struct EmulatedIpPacket {
42 public:
43 EmulatedIpPacket(const rtc::SocketAddress& from,
44 const rtc::SocketAddress& to,
Artem Titov40f51152019-01-04 15:45:01 +010045 rtc::CopyOnWriteBuffer data,
46 Timestamp arrival_time);
Artem Titov612e1792019-04-01 14:43:38 +020047 ~EmulatedIpPacket() = default;
Artem Titov40f51152019-01-04 15:45:01 +010048 // 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 Titov612e1792019-04-01 14:43:38 +020052 EmulatedIpPacket(EmulatedIpPacket&&) = default;
53 EmulatedIpPacket& operator=(EmulatedIpPacket&&) = default;
Artem Titov40f51152019-01-04 15:45:01 +010054
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 Titov40f51152019-01-04 15:45:01 +010060 rtc::CopyOnWriteBuffer data;
61 Timestamp arrival_time;
62};
63
64class EmulatedNetworkReceiverInterface {
65 public:
66 virtual ~EmulatedNetworkReceiverInterface() = default;
67
68 virtual void OnPacketReceived(EmulatedIpPacket packet) = 0;
69};
70
Sebastian Jansson62bb47f2019-04-01 18:23:58 +020071class LinkEmulation : public EmulatedNetworkReceiverInterface {
72 public:
73 LinkEmulation(Clock* clock,
74 rtc::TaskQueue* task_queue,
75 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
76 EmulatedNetworkReceiverInterface* receiver)
77 : clock_(clock),
78 task_queue_(task_queue),
79 network_behavior_(std::move(network_behavior)),
80 receiver_(receiver) {}
81 void OnPacketReceived(EmulatedIpPacket packet) override;
82
83 private:
84 struct StoredPacket {
85 uint64_t id;
86 EmulatedIpPacket packet;
87 bool removed;
88 };
89 void Process(Timestamp at_time) RTC_RUN_ON(task_queue_);
90 void HandlePacketReceived(EmulatedIpPacket packet) RTC_RUN_ON(task_queue_);
91
92 Clock* const clock_;
93 rtc::TaskQueue* const task_queue_;
94 const std::unique_ptr<NetworkBehaviorInterface> network_behavior_
95 RTC_GUARDED_BY(task_queue_);
96 EmulatedNetworkReceiverInterface* const receiver_;
97 RepeatingTaskHandle process_task_ RTC_GUARDED_BY(task_queue_);
98 std::deque<StoredPacket> packets_ RTC_GUARDED_BY(task_queue_);
99 uint64_t next_packet_id_ RTC_GUARDED_BY(task_queue_) = 1;
100};
101
102class NetworkRouterNode : public EmulatedNetworkReceiverInterface {
103 public:
104 explicit NetworkRouterNode(rtc::TaskQueue* task_queue);
105
106 void OnPacketReceived(EmulatedIpPacket packet) override;
107 void SetReceiver(rtc::IPAddress dest_ip,
108 EmulatedNetworkReceiverInterface* receiver);
109 void RemoveReceiver(rtc::IPAddress dest_ip);
110
111 private:
112 rtc::TaskQueue* const task_queue_;
113 std::map<rtc::IPAddress, EmulatedNetworkReceiverInterface*> routing_
114 RTC_GUARDED_BY(task_queue_);
115};
116
Artem Titov37d18482019-01-08 15:41:45 +0100117// Represents node in the emulated network. Nodes can be connected with each
118// other to form different networks with different behavior. The behavior of
119// the node itself is determined by a concrete implementation of
120// NetworkBehaviorInterface that is provided on construction.
121class EmulatedNetworkNode : public EmulatedNetworkReceiverInterface {
122 public:
123 // Creates node based on |network_behavior|. The specified |packet_overhead|
124 // is added to the size of each packet in the information provided to
125 // |network_behavior|.
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200126 // |task_queue| is used to process packets and to forward the packets when
127 // they are ready.
Sebastian Jansson62bb47f2019-04-01 18:23:58 +0200128 EmulatedNetworkNode(
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200129 Clock* clock,
130 rtc::TaskQueue* task_queue,
Sebastian Jansson8c8feb92019-01-29 15:59:17 +0100131 std::unique_ptr<NetworkBehaviorInterface> network_behavior);
Artem Titov37d18482019-01-08 15:41:45 +0100132 ~EmulatedNetworkNode() override;
133 RTC_DISALLOW_COPY_AND_ASSIGN(EmulatedNetworkNode);
134
135 void OnPacketReceived(EmulatedIpPacket packet) override;
Sebastian Jansson62bb47f2019-04-01 18:23:58 +0200136
137 LinkEmulation* link() { return &link_; }
138 NetworkRouterNode* router() { return &router_; }
Artem Titov37d18482019-01-08 15:41:45 +0100139
Artem Titov4cd433e2019-04-01 11:01:16 +0200140 // Creates a route for the given receiver_ip over all the given nodes to the
Artem Titov37d18482019-01-08 15:41:45 +0100141 // given receiver.
Artem Titov4cd433e2019-04-01 11:01:16 +0200142 static void CreateRoute(rtc::IPAddress receiver_ip,
Artem Titov37d18482019-01-08 15:41:45 +0100143 std::vector<EmulatedNetworkNode*> nodes,
144 EmulatedNetworkReceiverInterface* receiver);
Artem Titov4cd433e2019-04-01 11:01:16 +0200145 static void ClearRoute(rtc::IPAddress receiver_ip,
Artem Titov37d18482019-01-08 15:41:45 +0100146 std::vector<EmulatedNetworkNode*> nodes);
147
148 private:
Sebastian Jansson62bb47f2019-04-01 18:23:58 +0200149 NetworkRouterNode router_;
150 LinkEmulation link_;
Artem Titov37d18482019-01-08 15:41:45 +0100151};
152
Artem Titov0774bd92019-01-30 15:26:05 +0100153// Represents single network interface on the device.
154// It will be used as sender from socket side to send data to the network and
155// will act as packet receiver from emulated network side to receive packets
156// from other EmulatedNetworkNodes.
Artem Titovaba8dc22019-03-11 10:08:40 +0100157class EmulatedEndpoint : public EmulatedNetworkReceiverInterface {
Artem Titov0774bd92019-01-30 15:26:05 +0100158 public:
Artem Titove5cc85b2019-03-28 12:11:09 +0100159 EmulatedEndpoint(uint64_t id,
Artem Titov612e1792019-04-01 14:43:38 +0200160 const rtc::IPAddress& ip,
Artem Titove5cc85b2019-03-28 12:11:09 +0100161 bool is_enabled,
162 Clock* clock);
Artem Titovaba8dc22019-03-11 10:08:40 +0100163 ~EmulatedEndpoint() override;
Artem Titov0774bd92019-01-30 15:26:05 +0100164
165 uint64_t GetId() const;
166
167 // Set network node, that will be used to send packets to the network.
168 void SetSendNode(EmulatedNetworkNode* send_node);
169 // Send packet into network.
170 // |from| will be used to set source address for the packet in destination
171 // socket.
172 // |to| will be used for routing verification and picking right socket by port
173 // on destination endpoint.
174 void SendPacket(const rtc::SocketAddress& from,
175 const rtc::SocketAddress& to,
176 rtc::CopyOnWriteBuffer packet);
177
178 // Binds receiver to this endpoint to send and receive data.
179 // |desired_port| is a port that should be used. If it is equal to 0,
180 // endpoint will pick the first available port starting from
181 // |kFirstEphemeralPort|.
182 //
183 // Returns the port, that should be used (it will be equals to desired, if
184 // |desired_port| != 0 and is free or will be the one, selected by endpoint)
185 // or absl::nullopt if desired_port in used. Also fails if there are no more
186 // free ports to bind to.
187 absl::optional<uint16_t> BindReceiver(
188 uint16_t desired_port,
189 EmulatedNetworkReceiverInterface* receiver);
190 void UnbindReceiver(uint16_t port);
191
192 rtc::IPAddress GetPeerLocalAddress() const;
193
194 // Will be called to deliver packet into endpoint from network node.
195 void OnPacketReceived(EmulatedIpPacket packet) override;
196
Artem Titove5cc85b2019-03-28 12:11:09 +0100197 void Enable();
198 void Disable();
199 bool Enabled() const;
200
201 const rtc::Network& network() const { return *network_.get(); }
202
Artem Titov0774bd92019-01-30 15:26:05 +0100203 protected:
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100204 friend class test::NetworkEmulationManagerImpl;
Artem Titov0774bd92019-01-30 15:26:05 +0100205
206 EmulatedNetworkNode* GetSendNode() const;
Artem Titov0774bd92019-01-30 15:26:05 +0100207
208 private:
209 static constexpr uint16_t kFirstEphemeralPort = 49152;
210 uint16_t NextPort() RTC_EXCLUSIVE_LOCKS_REQUIRED(receiver_lock_);
211
212 rtc::CriticalSection receiver_lock_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100213 rtc::ThreadChecker enabled_state_checker_;
Artem Titov0774bd92019-01-30 15:26:05 +0100214
215 uint64_t id_;
216 // Peer's local IP address for this endpoint network interface.
217 const rtc::IPAddress peer_local_addr_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100218 bool is_enabled_ RTC_GUARDED_BY(enabled_state_checker_);
Artem Titov0774bd92019-01-30 15:26:05 +0100219 EmulatedNetworkNode* send_node_;
220 Clock* const clock_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100221 std::unique_ptr<rtc::Network> network_;
Artem Titov0774bd92019-01-30 15:26:05 +0100222
223 uint16_t next_port_ RTC_GUARDED_BY(receiver_lock_);
224 std::map<uint16_t, EmulatedNetworkReceiverInterface*> port_to_receiver_
225 RTC_GUARDED_BY(receiver_lock_);
Artem Titov0774bd92019-01-30 15:26:05 +0100226};
227
Artem Titovfc6ab002019-03-12 13:48:32 +0100228class EmulatedRoute {
229 public:
230 EmulatedRoute(EmulatedEndpoint* from,
231 std::vector<EmulatedNetworkNode*> via_nodes,
232 EmulatedEndpoint* to)
233 : from(from), via_nodes(std::move(via_nodes)), to(to), active(true) {}
234
235 EmulatedEndpoint* from;
236 std::vector<EmulatedNetworkNode*> via_nodes;
237 EmulatedEndpoint* to;
238 bool active;
239};
240
Artem Titove5cc85b2019-03-28 12:11:09 +0100241class EndpointsContainer {
242 public:
243 EndpointsContainer(const std::vector<EmulatedEndpoint*>& endpoints);
244
245 EmulatedEndpoint* LookupByLocalAddress(const rtc::IPAddress& local_ip) const;
246 bool HasEndpoint(EmulatedEndpoint* endpoint) const;
247 // Returns list of networks for enabled endpoints. Caller takes ownership of
248 // returned rtc::Network objects.
249 std::vector<std::unique_ptr<rtc::Network>> GetEnabledNetworks() const;
250
251 private:
252 const std::vector<EmulatedEndpoint*> endpoints_;
253};
254
Artem Titov40f51152019-01-04 15:45:01 +0100255} // namespace webrtc
256
257#endif // TEST_SCENARIO_NETWORK_NETWORK_EMULATION_H_