blob: b3fe5ebf43daf99cc89e20cd321ebcb431aefe85 [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 Titov806299e2019-04-12 12:17:19 +020022#include "api/test/network_emulation_manager.h"
Artem Titov37d18482019-01-08 15:41:45 +010023#include "api/test/simulated_network.h"
Artem Titov40f51152019-01-04 15:45:01 +010024#include "api/units/timestamp.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/copy_on_write_buffer.h"
Artem Titove5cc85b2019-03-28 12:11:09 +010026#include "rtc_base/network.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/socket_address.h"
Sebastian Jansson4124dab2019-04-01 14:33:53 +020028#include "rtc_base/task_queue_for_test.h"
29#include "rtc_base/task_utils/repeating_task.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 Titov7bf8c7f2019-03-15 15:00:37 +010034
Artem Titov40f51152019-01-04 15:45:01 +010035struct EmulatedIpPacket {
36 public:
37 EmulatedIpPacket(const rtc::SocketAddress& from,
38 const rtc::SocketAddress& to,
Artem Titov40f51152019-01-04 15:45:01 +010039 rtc::CopyOnWriteBuffer data,
40 Timestamp arrival_time);
Artem Titov612e1792019-04-01 14:43:38 +020041 ~EmulatedIpPacket() = default;
Artem Titov40f51152019-01-04 15:45:01 +010042 // This object is not copyable or assignable.
43 EmulatedIpPacket(const EmulatedIpPacket&) = delete;
44 EmulatedIpPacket& operator=(const EmulatedIpPacket&) = delete;
45 // This object is only moveable.
Artem Titov612e1792019-04-01 14:43:38 +020046 EmulatedIpPacket(EmulatedIpPacket&&) = default;
47 EmulatedIpPacket& operator=(EmulatedIpPacket&&) = default;
Artem Titov40f51152019-01-04 15:45:01 +010048
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;
Artem Titov40f51152019-01-04 15:45:01 +010054 rtc::CopyOnWriteBuffer data;
55 Timestamp arrival_time;
56};
57
58class EmulatedNetworkReceiverInterface {
59 public:
60 virtual ~EmulatedNetworkReceiverInterface() = default;
61
62 virtual void OnPacketReceived(EmulatedIpPacket packet) = 0;
63};
64
Sebastian Jansson62bb47f2019-04-01 18:23:58 +020065class LinkEmulation : public EmulatedNetworkReceiverInterface {
66 public:
67 LinkEmulation(Clock* clock,
68 rtc::TaskQueue* task_queue,
69 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
70 EmulatedNetworkReceiverInterface* receiver)
71 : clock_(clock),
72 task_queue_(task_queue),
73 network_behavior_(std::move(network_behavior)),
74 receiver_(receiver) {}
75 void OnPacketReceived(EmulatedIpPacket packet) override;
76
77 private:
78 struct StoredPacket {
79 uint64_t id;
80 EmulatedIpPacket packet;
81 bool removed;
82 };
83 void Process(Timestamp at_time) RTC_RUN_ON(task_queue_);
84 void HandlePacketReceived(EmulatedIpPacket packet) RTC_RUN_ON(task_queue_);
85
86 Clock* const clock_;
87 rtc::TaskQueue* const task_queue_;
88 const std::unique_ptr<NetworkBehaviorInterface> network_behavior_
89 RTC_GUARDED_BY(task_queue_);
90 EmulatedNetworkReceiverInterface* const receiver_;
91 RepeatingTaskHandle process_task_ RTC_GUARDED_BY(task_queue_);
92 std::deque<StoredPacket> packets_ RTC_GUARDED_BY(task_queue_);
93 uint64_t next_packet_id_ RTC_GUARDED_BY(task_queue_) = 1;
94};
95
96class NetworkRouterNode : public EmulatedNetworkReceiverInterface {
97 public:
98 explicit NetworkRouterNode(rtc::TaskQueue* task_queue);
99
100 void OnPacketReceived(EmulatedIpPacket packet) override;
101 void SetReceiver(rtc::IPAddress dest_ip,
102 EmulatedNetworkReceiverInterface* receiver);
103 void RemoveReceiver(rtc::IPAddress dest_ip);
104
105 private:
106 rtc::TaskQueue* const task_queue_;
107 std::map<rtc::IPAddress, EmulatedNetworkReceiverInterface*> routing_
108 RTC_GUARDED_BY(task_queue_);
109};
110
Artem Titov37d18482019-01-08 15:41:45 +0100111// Represents node in the emulated network. Nodes can be connected with each
112// other to form different networks with different behavior. The behavior of
113// the node itself is determined by a concrete implementation of
114// NetworkBehaviorInterface that is provided on construction.
115class EmulatedNetworkNode : public EmulatedNetworkReceiverInterface {
116 public:
117 // Creates node based on |network_behavior|. The specified |packet_overhead|
118 // is added to the size of each packet in the information provided to
119 // |network_behavior|.
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200120 // |task_queue| is used to process packets and to forward the packets when
121 // they are ready.
Sebastian Jansson62bb47f2019-04-01 18:23:58 +0200122 EmulatedNetworkNode(
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200123 Clock* clock,
124 rtc::TaskQueue* task_queue,
Sebastian Jansson8c8feb92019-01-29 15:59:17 +0100125 std::unique_ptr<NetworkBehaviorInterface> network_behavior);
Artem Titov37d18482019-01-08 15:41:45 +0100126 ~EmulatedNetworkNode() override;
127 RTC_DISALLOW_COPY_AND_ASSIGN(EmulatedNetworkNode);
128
129 void OnPacketReceived(EmulatedIpPacket packet) override;
Sebastian Jansson62bb47f2019-04-01 18:23:58 +0200130
131 LinkEmulation* link() { return &link_; }
132 NetworkRouterNode* router() { return &router_; }
Artem Titov37d18482019-01-08 15:41:45 +0100133
Artem Titov4cd433e2019-04-01 11:01:16 +0200134 // Creates a route for the given receiver_ip over all the given nodes to the
Artem Titov37d18482019-01-08 15:41:45 +0100135 // given receiver.
Artem Titov4cd433e2019-04-01 11:01:16 +0200136 static void CreateRoute(rtc::IPAddress receiver_ip,
Artem Titov37d18482019-01-08 15:41:45 +0100137 std::vector<EmulatedNetworkNode*> nodes,
138 EmulatedNetworkReceiverInterface* receiver);
Artem Titov4cd433e2019-04-01 11:01:16 +0200139 static void ClearRoute(rtc::IPAddress receiver_ip,
Artem Titov37d18482019-01-08 15:41:45 +0100140 std::vector<EmulatedNetworkNode*> nodes);
141
142 private:
Sebastian Jansson62bb47f2019-04-01 18:23:58 +0200143 NetworkRouterNode router_;
144 LinkEmulation link_;
Artem Titov37d18482019-01-08 15:41:45 +0100145};
146
Artem Titov0774bd92019-01-30 15:26:05 +0100147// Represents single network interface on the device.
148// It will be used as sender from socket side to send data to the network and
149// will act as packet receiver from emulated network side to receive packets
150// from other EmulatedNetworkNodes.
Artem Titovaba8dc22019-03-11 10:08:40 +0100151class EmulatedEndpoint : public EmulatedNetworkReceiverInterface {
Artem Titov0774bd92019-01-30 15:26:05 +0100152 public:
Artem Titove5cc85b2019-03-28 12:11:09 +0100153 EmulatedEndpoint(uint64_t id,
Artem Titov612e1792019-04-01 14:43:38 +0200154 const rtc::IPAddress& ip,
Artem Titove5cc85b2019-03-28 12:11:09 +0100155 bool is_enabled,
Artem Titovff393122019-04-05 11:19:52 +0200156 rtc::TaskQueue* task_queue,
Artem Titove5cc85b2019-03-28 12:11:09 +0100157 Clock* clock);
Artem Titovaba8dc22019-03-11 10:08:40 +0100158 ~EmulatedEndpoint() override;
Artem Titov0774bd92019-01-30 15:26:05 +0100159
160 uint64_t GetId() const;
161
Artem Titovff393122019-04-05 11:19:52 +0200162 NetworkRouterNode* router() { return &router_; }
Artem Titov0774bd92019-01-30 15:26:05 +0100163 // Send packet into network.
164 // |from| will be used to set source address for the packet in destination
165 // socket.
166 // |to| will be used for routing verification and picking right socket by port
167 // on destination endpoint.
168 void SendPacket(const rtc::SocketAddress& from,
169 const rtc::SocketAddress& to,
170 rtc::CopyOnWriteBuffer packet);
171
172 // Binds receiver to this endpoint to send and receive data.
173 // |desired_port| is a port that should be used. If it is equal to 0,
174 // endpoint will pick the first available port starting from
175 // |kFirstEphemeralPort|.
176 //
177 // Returns the port, that should be used (it will be equals to desired, if
178 // |desired_port| != 0 and is free or will be the one, selected by endpoint)
179 // or absl::nullopt if desired_port in used. Also fails if there are no more
180 // free ports to bind to.
181 absl::optional<uint16_t> BindReceiver(
182 uint16_t desired_port,
183 EmulatedNetworkReceiverInterface* receiver);
184 void UnbindReceiver(uint16_t port);
185
186 rtc::IPAddress GetPeerLocalAddress() const;
187
188 // Will be called to deliver packet into endpoint from network node.
189 void OnPacketReceived(EmulatedIpPacket packet) override;
190
Artem Titove5cc85b2019-03-28 12:11:09 +0100191 void Enable();
192 void Disable();
193 bool Enabled() const;
194
195 const rtc::Network& network() const { return *network_.get(); }
196
Artem Titov806299e2019-04-12 12:17:19 +0200197 EmulatedNetworkStats stats();
198
Artem Titov0774bd92019-01-30 15:26:05 +0100199 private:
200 static constexpr uint16_t kFirstEphemeralPort = 49152;
201 uint16_t NextPort() RTC_EXCLUSIVE_LOCKS_REQUIRED(receiver_lock_);
Artem Titov806299e2019-04-12 12:17:19 +0200202 void UpdateSendStats(const EmulatedIpPacket& packet);
203 void UpdateReceiveStats(const EmulatedIpPacket& packet);
Artem Titov0774bd92019-01-30 15:26:05 +0100204
205 rtc::CriticalSection receiver_lock_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100206 rtc::ThreadChecker enabled_state_checker_;
Artem Titov0774bd92019-01-30 15:26:05 +0100207
208 uint64_t id_;
209 // Peer's local IP address for this endpoint network interface.
210 const rtc::IPAddress peer_local_addr_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100211 bool is_enabled_ RTC_GUARDED_BY(enabled_state_checker_);
Artem Titov0774bd92019-01-30 15:26:05 +0100212 Clock* const clock_;
Artem Titovff393122019-04-05 11:19:52 +0200213 rtc::TaskQueue* const task_queue_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100214 std::unique_ptr<rtc::Network> network_;
Artem Titovff393122019-04-05 11:19:52 +0200215 NetworkRouterNode router_;
Artem Titov0774bd92019-01-30 15:26:05 +0100216
217 uint16_t next_port_ RTC_GUARDED_BY(receiver_lock_);
218 std::map<uint16_t, EmulatedNetworkReceiverInterface*> port_to_receiver_
219 RTC_GUARDED_BY(receiver_lock_);
Artem Titov806299e2019-04-12 12:17:19 +0200220
221 EmulatedNetworkStats stats_ RTC_GUARDED_BY(task_queue_);
Artem Titov0774bd92019-01-30 15:26:05 +0100222};
223
Artem Titovfc6ab002019-03-12 13:48:32 +0100224class EmulatedRoute {
225 public:
226 EmulatedRoute(EmulatedEndpoint* from,
227 std::vector<EmulatedNetworkNode*> via_nodes,
228 EmulatedEndpoint* to)
229 : from(from), via_nodes(std::move(via_nodes)), to(to), active(true) {}
230
231 EmulatedEndpoint* from;
232 std::vector<EmulatedNetworkNode*> via_nodes;
233 EmulatedEndpoint* to;
234 bool active;
235};
236
Artem Titove5cc85b2019-03-28 12:11:09 +0100237class EndpointsContainer {
238 public:
239 EndpointsContainer(const std::vector<EmulatedEndpoint*>& endpoints);
240
241 EmulatedEndpoint* LookupByLocalAddress(const rtc::IPAddress& local_ip) const;
242 bool HasEndpoint(EmulatedEndpoint* endpoint) const;
243 // Returns list of networks for enabled endpoints. Caller takes ownership of
244 // returned rtc::Network objects.
245 std::vector<std::unique_ptr<rtc::Network>> GetEnabledNetworks() const;
Artem Titov806299e2019-04-12 12:17:19 +0200246 EmulatedNetworkStats GetStats() const;
Artem Titove5cc85b2019-03-28 12:11:09 +0100247
248 private:
249 const std::vector<EmulatedEndpoint*> endpoints_;
250};
251
Artem Titov40f51152019-01-04 15:45:01 +0100252} // namespace webrtc
253
254#endif // TEST_SCENARIO_NETWORK_NETWORK_EMULATION_H_