blob: 9486a806eaa54965f1b0f63181126f9f9018d8ed [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"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/socket_address.h"
Artem Titov40f51152019-01-04 15:45:01 +010028#include "rtc_base/thread.h"
Artem Titov0774bd92019-01-30 15:26:05 +010029#include "system_wrappers/include/clock.h"
Artem Titov40f51152019-01-04 15:45:01 +010030
31namespace webrtc {
Artem Titov37d18482019-01-08 15:41:45 +010032namespace test {
Artem Titov40f51152019-01-04 15:45:01 +010033
34struct EmulatedIpPacket {
35 public:
36 EmulatedIpPacket(const rtc::SocketAddress& from,
37 const rtc::SocketAddress& to,
38 uint64_t dest_endpoint_id,
39 rtc::CopyOnWriteBuffer data,
40 Timestamp arrival_time);
Artem Titov40f51152019-01-04 15:45:01 +010041 ~EmulatedIpPacket();
42 // This object is not copyable or assignable.
43 EmulatedIpPacket(const EmulatedIpPacket&) = delete;
44 EmulatedIpPacket& operator=(const EmulatedIpPacket&) = delete;
45 // This object is only moveable.
46 EmulatedIpPacket(EmulatedIpPacket&&);
47 EmulatedIpPacket& operator=(EmulatedIpPacket&&);
48
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;
54 uint64_t dest_endpoint_id;
55 rtc::CopyOnWriteBuffer data;
56 Timestamp arrival_time;
57};
58
59class EmulatedNetworkReceiverInterface {
60 public:
61 virtual ~EmulatedNetworkReceiverInterface() = default;
62
63 virtual void OnPacketReceived(EmulatedIpPacket packet) = 0;
64};
65
Artem Titov37d18482019-01-08 15:41:45 +010066// Represents node in the emulated network. Nodes can be connected with each
67// other to form different networks with different behavior. The behavior of
68// the node itself is determined by a concrete implementation of
69// NetworkBehaviorInterface that is provided on construction.
70class EmulatedNetworkNode : public EmulatedNetworkReceiverInterface {
71 public:
72 // Creates node based on |network_behavior|. The specified |packet_overhead|
73 // is added to the size of each packet in the information provided to
74 // |network_behavior|.
Sebastian Jansson8c8feb92019-01-29 15:59:17 +010075 explicit EmulatedNetworkNode(
76 std::unique_ptr<NetworkBehaviorInterface> network_behavior);
Artem Titov37d18482019-01-08 15:41:45 +010077 ~EmulatedNetworkNode() override;
78 RTC_DISALLOW_COPY_AND_ASSIGN(EmulatedNetworkNode);
79
80 void OnPacketReceived(EmulatedIpPacket packet) override;
81 void Process(Timestamp at_time);
82 void SetReceiver(uint64_t dest_endpoint_id,
83 EmulatedNetworkReceiverInterface* receiver);
84 void RemoveReceiver(uint64_t dest_endpoint_id);
85
86 // Creates a route for the given receiver_id over all the given nodes to the
87 // given receiver.
88 static void CreateRoute(uint64_t receiver_id,
89 std::vector<EmulatedNetworkNode*> nodes,
90 EmulatedNetworkReceiverInterface* receiver);
91 static void ClearRoute(uint64_t receiver_id,
92 std::vector<EmulatedNetworkNode*> nodes);
93
94 private:
95 struct StoredPacket {
96 uint64_t id;
97 EmulatedIpPacket packet;
98 bool removed;
99 };
100
101 rtc::CriticalSection lock_;
102 std::map<uint64_t, EmulatedNetworkReceiverInterface*> routing_
103 RTC_GUARDED_BY(lock_);
104 const std::unique_ptr<NetworkBehaviorInterface> network_behavior_
105 RTC_GUARDED_BY(lock_);
Artem Titov37d18482019-01-08 15:41:45 +0100106 std::deque<StoredPacket> packets_ RTC_GUARDED_BY(lock_);
107
108 uint64_t next_packet_id_ RTC_GUARDED_BY(lock_) = 1;
109};
110
Artem Titov0774bd92019-01-30 15:26:05 +0100111// Represents single network interface on the device.
112// It will be used as sender from socket side to send data to the network and
113// will act as packet receiver from emulated network side to receive packets
114// from other EmulatedNetworkNodes.
Artem Titovaba8dc22019-03-11 10:08:40 +0100115class EmulatedEndpoint : public EmulatedNetworkReceiverInterface {
Artem Titov0774bd92019-01-30 15:26:05 +0100116 public:
Artem Titovaba8dc22019-03-11 10:08:40 +0100117 EmulatedEndpoint(uint64_t id, rtc::IPAddress, Clock* clock);
118 ~EmulatedEndpoint() override;
Artem Titov0774bd92019-01-30 15:26:05 +0100119
120 uint64_t GetId() const;
121
122 // Set network node, that will be used to send packets to the network.
123 void SetSendNode(EmulatedNetworkNode* send_node);
124 // Send packet into network.
125 // |from| will be used to set source address for the packet in destination
126 // socket.
127 // |to| will be used for routing verification and picking right socket by port
128 // on destination endpoint.
129 void SendPacket(const rtc::SocketAddress& from,
130 const rtc::SocketAddress& to,
131 rtc::CopyOnWriteBuffer packet);
132
133 // Binds receiver to this endpoint to send and receive data.
134 // |desired_port| is a port that should be used. If it is equal to 0,
135 // endpoint will pick the first available port starting from
136 // |kFirstEphemeralPort|.
137 //
138 // Returns the port, that should be used (it will be equals to desired, if
139 // |desired_port| != 0 and is free or will be the one, selected by endpoint)
140 // or absl::nullopt if desired_port in used. Also fails if there are no more
141 // free ports to bind to.
142 absl::optional<uint16_t> BindReceiver(
143 uint16_t desired_port,
144 EmulatedNetworkReceiverInterface* receiver);
145 void UnbindReceiver(uint16_t port);
146
147 rtc::IPAddress GetPeerLocalAddress() const;
148
149 // Will be called to deliver packet into endpoint from network node.
150 void OnPacketReceived(EmulatedIpPacket packet) override;
151
152 protected:
153 friend class NetworkEmulationManager;
154
155 EmulatedNetworkNode* GetSendNode() const;
156 void SetConnectedEndpointId(uint64_t endpoint_id);
157
158 private:
159 static constexpr uint16_t kFirstEphemeralPort = 49152;
160 uint16_t NextPort() RTC_EXCLUSIVE_LOCKS_REQUIRED(receiver_lock_);
161
162 rtc::CriticalSection receiver_lock_;
163
164 uint64_t id_;
165 // Peer's local IP address for this endpoint network interface.
166 const rtc::IPAddress peer_local_addr_;
167 EmulatedNetworkNode* send_node_;
168 Clock* const clock_;
169
170 uint16_t next_port_ RTC_GUARDED_BY(receiver_lock_);
171 std::map<uint16_t, EmulatedNetworkReceiverInterface*> port_to_receiver_
172 RTC_GUARDED_BY(receiver_lock_);
173
174 absl::optional<uint64_t> connected_endpoint_id_;
175};
176
Artem Titov37d18482019-01-08 15:41:45 +0100177} // namespace test
Artem Titov40f51152019-01-04 15:45:01 +0100178} // namespace webrtc
179
180#endif // TEST_SCENARIO_NETWORK_NETWORK_EMULATION_H_