blob: 674e39e10afd2f9401ec70f2c46dd1faef92d2cb [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
Artem Titov37d18482019-01-08 15:41:45 +010071// 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.
75class 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 Jansson4124dab2019-04-01 14:33:53 +020080 // |task_queue| is used to process packets and to forward the packets when
81 // they are ready.
Sebastian Jansson8c8feb92019-01-29 15:59:17 +010082 explicit EmulatedNetworkNode(
Sebastian Jansson4124dab2019-04-01 14:33:53 +020083 Clock* clock,
84 rtc::TaskQueue* task_queue,
Sebastian Jansson8c8feb92019-01-29 15:59:17 +010085 std::unique_ptr<NetworkBehaviorInterface> network_behavior);
Artem Titov37d18482019-01-08 15:41:45 +010086 ~EmulatedNetworkNode() override;
87 RTC_DISALLOW_COPY_AND_ASSIGN(EmulatedNetworkNode);
88
89 void OnPacketReceived(EmulatedIpPacket packet) override;
Artem Titov4cd433e2019-04-01 11:01:16 +020090 void SetReceiver(rtc::IPAddress dest_ip,
Artem Titov37d18482019-01-08 15:41:45 +010091 EmulatedNetworkReceiverInterface* receiver);
Artem Titov4cd433e2019-04-01 11:01:16 +020092 void RemoveReceiver(rtc::IPAddress dest_ip);
Artem Titov37d18482019-01-08 15:41:45 +010093
Artem Titov4cd433e2019-04-01 11:01:16 +020094 // Creates a route for the given receiver_ip over all the given nodes to the
Artem Titov37d18482019-01-08 15:41:45 +010095 // given receiver.
Artem Titov4cd433e2019-04-01 11:01:16 +020096 static void CreateRoute(rtc::IPAddress receiver_ip,
Artem Titov37d18482019-01-08 15:41:45 +010097 std::vector<EmulatedNetworkNode*> nodes,
98 EmulatedNetworkReceiverInterface* receiver);
Artem Titov4cd433e2019-04-01 11:01:16 +020099 static void ClearRoute(rtc::IPAddress receiver_ip,
Artem Titov37d18482019-01-08 15:41:45 +0100100 std::vector<EmulatedNetworkNode*> nodes);
101
102 private:
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200103 void Process(Timestamp at_time) RTC_RUN_ON(task_queue_);
104 void HandlePacketReceived(EmulatedIpPacket packet) RTC_RUN_ON(task_queue_);
Artem Titov37d18482019-01-08 15:41:45 +0100105 struct StoredPacket {
106 uint64_t id;
107 EmulatedIpPacket packet;
108 bool removed;
109 };
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200110 Clock* const clock_;
111 rtc::TaskQueue* const task_queue_;
112 RepeatingTaskHandle process_task_;
Artem Titov4cd433e2019-04-01 11:01:16 +0200113 std::map<rtc::IPAddress, EmulatedNetworkReceiverInterface*> routing_
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200114 RTC_GUARDED_BY(task_queue_);
Artem Titov37d18482019-01-08 15:41:45 +0100115 const std::unique_ptr<NetworkBehaviorInterface> network_behavior_
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200116 RTC_GUARDED_BY(task_queue_);
117 std::deque<StoredPacket> packets_ RTC_GUARDED_BY(task_queue_);
Artem Titov37d18482019-01-08 15:41:45 +0100118
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200119 uint64_t next_packet_id_ RTC_GUARDED_BY(task_queue_) = 1;
Artem Titov37d18482019-01-08 15:41:45 +0100120};
121
Artem Titov0774bd92019-01-30 15:26:05 +0100122// 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 Titovaba8dc22019-03-11 10:08:40 +0100126class EmulatedEndpoint : public EmulatedNetworkReceiverInterface {
Artem Titov0774bd92019-01-30 15:26:05 +0100127 public:
Artem Titove5cc85b2019-03-28 12:11:09 +0100128 EmulatedEndpoint(uint64_t id,
Artem Titov612e1792019-04-01 14:43:38 +0200129 const rtc::IPAddress& ip,
Artem Titove5cc85b2019-03-28 12:11:09 +0100130 bool is_enabled,
131 Clock* clock);
Artem Titovaba8dc22019-03-11 10:08:40 +0100132 ~EmulatedEndpoint() override;
Artem Titov0774bd92019-01-30 15:26:05 +0100133
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 Titove5cc85b2019-03-28 12:11:09 +0100166 void Enable();
167 void Disable();
168 bool Enabled() const;
169
170 const rtc::Network& network() const { return *network_.get(); }
171
Artem Titov0774bd92019-01-30 15:26:05 +0100172 protected:
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100173 friend class test::NetworkEmulationManagerImpl;
Artem Titov0774bd92019-01-30 15:26:05 +0100174
175 EmulatedNetworkNode* GetSendNode() const;
Artem Titov0774bd92019-01-30 15:26:05 +0100176
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 Titove5cc85b2019-03-28 12:11:09 +0100182 rtc::ThreadChecker enabled_state_checker_;
Artem Titov0774bd92019-01-30 15:26:05 +0100183
184 uint64_t id_;
185 // Peer's local IP address for this endpoint network interface.
186 const rtc::IPAddress peer_local_addr_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100187 bool is_enabled_ RTC_GUARDED_BY(enabled_state_checker_);
Artem Titov0774bd92019-01-30 15:26:05 +0100188 EmulatedNetworkNode* send_node_;
189 Clock* const clock_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100190 std::unique_ptr<rtc::Network> network_;
Artem Titov0774bd92019-01-30 15:26:05 +0100191
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 Titov0774bd92019-01-30 15:26:05 +0100195};
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_