blob: ec39dfeeab5e1bc5773c115a51e41519ab32111b [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,
Artem Titov40f51152019-01-04 15:45:01 +010046 rtc::CopyOnWriteBuffer data,
47 Timestamp arrival_time);
Artem Titov40f51152019-01-04 15:45:01 +010048 ~EmulatedIpPacket();
49 // This object is not copyable or assignable.
50 EmulatedIpPacket(const EmulatedIpPacket&) = delete;
51 EmulatedIpPacket& operator=(const EmulatedIpPacket&) = delete;
52 // This object is only moveable.
53 EmulatedIpPacket(EmulatedIpPacket&&);
54 EmulatedIpPacket& operator=(EmulatedIpPacket&&);
55
56 size_t size() const { return data.size(); }
57 const uint8_t* cdata() const { return data.cdata(); }
58
59 rtc::SocketAddress from;
60 rtc::SocketAddress to;
Artem Titov40f51152019-01-04 15:45:01 +010061 rtc::CopyOnWriteBuffer data;
62 Timestamp arrival_time;
63};
64
65class EmulatedNetworkReceiverInterface {
66 public:
67 virtual ~EmulatedNetworkReceiverInterface() = default;
68
69 virtual void OnPacketReceived(EmulatedIpPacket packet) = 0;
70};
71
Artem Titov37d18482019-01-08 15:41:45 +010072// Represents node in the emulated network. Nodes can be connected with each
73// other to form different networks with different behavior. The behavior of
74// the node itself is determined by a concrete implementation of
75// NetworkBehaviorInterface that is provided on construction.
76class EmulatedNetworkNode : public EmulatedNetworkReceiverInterface {
77 public:
78 // Creates node based on |network_behavior|. The specified |packet_overhead|
79 // is added to the size of each packet in the information provided to
80 // |network_behavior|.
Sebastian Jansson8c8feb92019-01-29 15:59:17 +010081 explicit EmulatedNetworkNode(
82 std::unique_ptr<NetworkBehaviorInterface> network_behavior);
Artem Titov37d18482019-01-08 15:41:45 +010083 ~EmulatedNetworkNode() override;
84 RTC_DISALLOW_COPY_AND_ASSIGN(EmulatedNetworkNode);
85
86 void OnPacketReceived(EmulatedIpPacket packet) override;
87 void Process(Timestamp at_time);
Artem Titov4cd433e2019-04-01 11:01:16 +020088 void SetReceiver(rtc::IPAddress dest_ip,
Artem Titov37d18482019-01-08 15:41:45 +010089 EmulatedNetworkReceiverInterface* receiver);
Artem Titov4cd433e2019-04-01 11:01:16 +020090 void RemoveReceiver(rtc::IPAddress dest_ip);
Artem Titov37d18482019-01-08 15:41:45 +010091
Artem Titov4cd433e2019-04-01 11:01:16 +020092 // Creates a route for the given receiver_ip over all the given nodes to the
Artem Titov37d18482019-01-08 15:41:45 +010093 // given receiver.
Artem Titov4cd433e2019-04-01 11:01:16 +020094 static void CreateRoute(rtc::IPAddress receiver_ip,
Artem Titov37d18482019-01-08 15:41:45 +010095 std::vector<EmulatedNetworkNode*> nodes,
96 EmulatedNetworkReceiverInterface* receiver);
Artem Titov4cd433e2019-04-01 11:01:16 +020097 static void ClearRoute(rtc::IPAddress receiver_ip,
Artem Titov37d18482019-01-08 15:41:45 +010098 std::vector<EmulatedNetworkNode*> nodes);
99
100 private:
101 struct StoredPacket {
102 uint64_t id;
103 EmulatedIpPacket packet;
104 bool removed;
105 };
106
107 rtc::CriticalSection lock_;
Artem Titov4cd433e2019-04-01 11:01:16 +0200108 std::map<rtc::IPAddress, EmulatedNetworkReceiverInterface*> routing_
Artem Titov37d18482019-01-08 15:41:45 +0100109 RTC_GUARDED_BY(lock_);
110 const std::unique_ptr<NetworkBehaviorInterface> network_behavior_
111 RTC_GUARDED_BY(lock_);
Artem Titov37d18482019-01-08 15:41:45 +0100112 std::deque<StoredPacket> packets_ RTC_GUARDED_BY(lock_);
113
114 uint64_t next_packet_id_ RTC_GUARDED_BY(lock_) = 1;
115};
116
Artem Titov0774bd92019-01-30 15:26:05 +0100117// Represents single network interface on the device.
118// It will be used as sender from socket side to send data to the network and
119// will act as packet receiver from emulated network side to receive packets
120// from other EmulatedNetworkNodes.
Artem Titovaba8dc22019-03-11 10:08:40 +0100121class EmulatedEndpoint : public EmulatedNetworkReceiverInterface {
Artem Titov0774bd92019-01-30 15:26:05 +0100122 public:
Artem Titove5cc85b2019-03-28 12:11:09 +0100123 EmulatedEndpoint(uint64_t id,
124 rtc::IPAddress ip,
125 bool is_enabled,
126 Clock* clock);
Artem Titovaba8dc22019-03-11 10:08:40 +0100127 ~EmulatedEndpoint() override;
Artem Titov0774bd92019-01-30 15:26:05 +0100128
129 uint64_t GetId() const;
130
131 // Set network node, that will be used to send packets to the network.
132 void SetSendNode(EmulatedNetworkNode* send_node);
133 // Send packet into network.
134 // |from| will be used to set source address for the packet in destination
135 // socket.
136 // |to| will be used for routing verification and picking right socket by port
137 // on destination endpoint.
138 void SendPacket(const rtc::SocketAddress& from,
139 const rtc::SocketAddress& to,
140 rtc::CopyOnWriteBuffer packet);
141
142 // Binds receiver to this endpoint to send and receive data.
143 // |desired_port| is a port that should be used. If it is equal to 0,
144 // endpoint will pick the first available port starting from
145 // |kFirstEphemeralPort|.
146 //
147 // Returns the port, that should be used (it will be equals to desired, if
148 // |desired_port| != 0 and is free or will be the one, selected by endpoint)
149 // or absl::nullopt if desired_port in used. Also fails if there are no more
150 // free ports to bind to.
151 absl::optional<uint16_t> BindReceiver(
152 uint16_t desired_port,
153 EmulatedNetworkReceiverInterface* receiver);
154 void UnbindReceiver(uint16_t port);
155
156 rtc::IPAddress GetPeerLocalAddress() const;
157
158 // Will be called to deliver packet into endpoint from network node.
159 void OnPacketReceived(EmulatedIpPacket packet) override;
160
Artem Titove5cc85b2019-03-28 12:11:09 +0100161 void Enable();
162 void Disable();
163 bool Enabled() const;
164
165 const rtc::Network& network() const { return *network_.get(); }
166
Artem Titov0774bd92019-01-30 15:26:05 +0100167 protected:
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100168 friend class test::NetworkEmulationManagerImpl;
Artem Titov0774bd92019-01-30 15:26:05 +0100169
170 EmulatedNetworkNode* GetSendNode() const;
Artem Titov0774bd92019-01-30 15:26:05 +0100171
172 private:
173 static constexpr uint16_t kFirstEphemeralPort = 49152;
174 uint16_t NextPort() RTC_EXCLUSIVE_LOCKS_REQUIRED(receiver_lock_);
175
176 rtc::CriticalSection receiver_lock_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100177 rtc::ThreadChecker enabled_state_checker_;
Artem Titov0774bd92019-01-30 15:26:05 +0100178
179 uint64_t id_;
180 // Peer's local IP address for this endpoint network interface.
181 const rtc::IPAddress peer_local_addr_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100182 bool is_enabled_ RTC_GUARDED_BY(enabled_state_checker_);
Artem Titov0774bd92019-01-30 15:26:05 +0100183 EmulatedNetworkNode* send_node_;
184 Clock* const clock_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100185 std::unique_ptr<rtc::Network> network_;
Artem Titov0774bd92019-01-30 15:26:05 +0100186
187 uint16_t next_port_ RTC_GUARDED_BY(receiver_lock_);
188 std::map<uint16_t, EmulatedNetworkReceiverInterface*> port_to_receiver_
189 RTC_GUARDED_BY(receiver_lock_);
Artem Titov0774bd92019-01-30 15:26:05 +0100190};
191
Artem Titovfc6ab002019-03-12 13:48:32 +0100192class EmulatedRoute {
193 public:
194 EmulatedRoute(EmulatedEndpoint* from,
195 std::vector<EmulatedNetworkNode*> via_nodes,
196 EmulatedEndpoint* to)
197 : from(from), via_nodes(std::move(via_nodes)), to(to), active(true) {}
198
199 EmulatedEndpoint* from;
200 std::vector<EmulatedNetworkNode*> via_nodes;
201 EmulatedEndpoint* to;
202 bool active;
203};
204
Artem Titove5cc85b2019-03-28 12:11:09 +0100205class EndpointsContainer {
206 public:
207 EndpointsContainer(const std::vector<EmulatedEndpoint*>& endpoints);
208
209 EmulatedEndpoint* LookupByLocalAddress(const rtc::IPAddress& local_ip) const;
210 bool HasEndpoint(EmulatedEndpoint* endpoint) const;
211 // Returns list of networks for enabled endpoints. Caller takes ownership of
212 // returned rtc::Network objects.
213 std::vector<std::unique_ptr<rtc::Network>> GetEnabledNetworks() const;
214
215 private:
216 const std::vector<EmulatedEndpoint*> endpoints_;
217};
218
Artem Titov40f51152019-01-04 15:45:01 +0100219} // namespace webrtc
220
221#endif // TEST_SCENARIO_NETWORK_NETWORK_EMULATION_H_