blob: 449441a3c12f72d5fe51442a0f58a5f0183414c5 [file] [log] [blame]
Artem Titov0774bd92019-01-30 15:26:05 +01001/*
2 * Copyright (c) 2019 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
Artem Titov386802e2019-07-05 10:48:17 +020011#ifndef TEST_NETWORK_NETWORK_EMULATION_MANAGER_H_
12#define TEST_NETWORK_NETWORK_EMULATION_MANAGER_H_
Artem Titov0774bd92019-01-30 15:26:05 +010013
Artem Titov386802e2019-07-05 10:48:17 +020014#include <map>
Artem Titov0774bd92019-01-30 15:26:05 +010015#include <memory>
Artem Titov386802e2019-07-05 10:48:17 +020016#include <set>
Artem Titov0774bd92019-01-30 15:26:05 +010017#include <utility>
18#include <vector>
19
Artem Titovcf781282020-07-28 13:45:16 +020020#include "api/array_view.h"
Artem Titov7bf8c7f2019-03-15 15:00:37 +010021#include "api/test/network_emulation_manager.h"
Artem Titov0774bd92019-01-30 15:26:05 +010022#include "api/test/simulated_network.h"
Bjorn A Mellemc4f86542019-11-21 10:37:18 -080023#include "api/test/time_controller.h"
Artem Titov0774bd92019-01-30 15:26:05 +010024#include "api/units/time_delta.h"
25#include "api/units/timestamp.h"
26#include "rtc_base/logging.h"
Artem Titov20863472019-03-13 10:30:51 +010027#include "rtc_base/network.h"
Danil Chapovalov982b5762019-03-21 09:58:30 +010028#include "rtc_base/task_queue_for_test.h"
Artem Titov0774bd92019-01-30 15:26:05 +010029#include "rtc_base/task_utils/repeating_task.h"
30#include "rtc_base/thread.h"
Sebastian Janssonb00eb192019-02-11 09:13:01 +010031#include "system_wrappers/include/clock.h"
Artem Titov386802e2019-07-05 10:48:17 +020032#include "test/network/cross_traffic.h"
33#include "test/network/emulated_network_manager.h"
Jonas Oreland97050112020-11-17 21:30:33 +010034#include "test/network/emulated_turn_server.h"
Artem Titov386802e2019-07-05 10:48:17 +020035#include "test/network/fake_network_socket_server.h"
36#include "test/network/network_emulation.h"
Artem Titov0774bd92019-01-30 15:26:05 +010037
38namespace webrtc {
39namespace test {
40
Artem Titov7bf8c7f2019-03-15 15:00:37 +010041class NetworkEmulationManagerImpl : public NetworkEmulationManager {
Artem Titov0774bd92019-01-30 15:26:05 +010042 public:
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010043 explicit NetworkEmulationManagerImpl(TimeMode mode);
Artem Titov7bf8c7f2019-03-15 15:00:37 +010044 ~NetworkEmulationManagerImpl();
Artem Titov0774bd92019-01-30 15:26:05 +010045
Artem Titovec9b2812021-01-07 15:49:31 +010046 EmulatedNetworkNode* CreateEmulatedNode(BuiltInNetworkBehaviorConfig config,
47 uint64_t random_seed = 1) override;
Artem Titov48b1b182019-07-05 13:09:48 +020048 EmulatedNetworkNode* CreateEmulatedNode(
Artem Titov7bf8c7f2019-03-15 15:00:37 +010049 std::unique_ptr<NetworkBehaviorInterface> network_behavior) override;
Artem Titov0774bd92019-01-30 15:26:05 +010050
Sebastian Janssoncec24332019-12-04 14:26:50 +010051 SimulatedNetworkNode::Builder NodeBuilder() override;
Sebastian Jansson8d3e4bd2019-07-31 18:33:17 +020052
Niels Möller376cf382021-02-26 09:24:51 +010053 EmulatedEndpointImpl* CreateEndpoint(EmulatedEndpointConfig config) override;
Artem Titove5cc85b2019-03-28 12:11:09 +010054 void EnableEndpoint(EmulatedEndpoint* endpoint) override;
55 void DisableEndpoint(EmulatedEndpoint* endpoint) override;
Artem Titov0774bd92019-01-30 15:26:05 +010056
Artem Titovfc6ab002019-03-12 13:48:32 +010057 EmulatedRoute* CreateRoute(EmulatedEndpoint* from,
Artem Titov7bf8c7f2019-03-15 15:00:37 +010058 const std::vector<EmulatedNetworkNode*>& via_nodes,
59 EmulatedEndpoint* to) override;
Sebastian Janssonb13ccc52019-06-07 13:06:00 +020060
61 EmulatedRoute* CreateRoute(
Sebastian Janssoncec24332019-12-04 14:26:50 +010062 const std::vector<EmulatedNetworkNode*>& via_nodes) override;
Sebastian Janssonb13ccc52019-06-07 13:06:00 +020063
Artem Titov3d37e062021-02-19 20:26:32 +010064 EmulatedRoute* CreateDefaultRoute(
65 EmulatedEndpoint* from,
66 const std::vector<EmulatedNetworkNode*>& via_nodes,
67 EmulatedEndpoint* to) override;
68
Artem Titov7bf8c7f2019-03-15 15:00:37 +010069 void ClearRoute(EmulatedRoute* route) override;
Artem Titov0774bd92019-01-30 15:26:05 +010070
Sebastian Jansson50f86862019-11-13 14:10:07 +010071 TcpMessageRoute* CreateTcpRoute(EmulatedRoute* send_route,
Sebastian Janssoncec24332019-12-04 14:26:50 +010072 EmulatedRoute* ret_route) override;
Sebastian Janssondcc910a2019-11-12 16:36:34 +010073
Andrey Logvinf9ee0e02021-01-14 09:50:32 +000074 CrossTrafficRoute* CreateCrossTrafficRoute(
75 const std::vector<EmulatedNetworkNode*>& via_nodes) override;
76
77 CrossTrafficGenerator* StartCrossTraffic(
78 std::unique_ptr<CrossTrafficGenerator> generator) override;
79 void StopCrossTraffic(CrossTrafficGenerator* generator) override;
Artem Titovd3666b22019-02-11 14:40:17 +010080
Artem Titove5cc85b2019-03-28 12:11:09 +010081 EmulatedNetworkManagerInterface* CreateEmulatedNetworkManagerInterface(
Artem Titov7bf8c7f2019-03-15 15:00:37 +010082 const std::vector<EmulatedEndpoint*>& endpoints) override;
Artem Titov0774bd92019-01-30 15:26:05 +010083
Per Kjellander410c9982021-02-15 11:24:37 +010084 void GetStats(rtc::ArrayView<EmulatedEndpoint* const> endpoints,
Artem Titovcf781282020-07-28 13:45:16 +020085 std::function<void(std::unique_ptr<EmulatedNetworkStats>)>
86 stats_callback) override;
87
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010088 TimeController* time_controller() override { return time_controller_.get(); }
89
Artem Titovcc8a1f82021-01-26 13:58:23 +010090 TimeMode time_mode() const override { return time_mode_; }
91
Artem Titov0774bd92019-01-30 15:26:05 +010092 Timestamp Now() const;
93
Jonas Oreland97050112020-11-17 21:30:33 +010094 EmulatedTURNServerInterface* CreateTURNServer(
95 EmulatedTURNServerConfig config) override;
96
Sebastian Jansson6ce033a2020-01-22 10:12:56 +010097 private:
Andrey Logvinf9ee0e02021-01-14 09:50:32 +000098 using CrossTrafficSource =
99 std::pair<std::unique_ptr<CrossTrafficGenerator>, RepeatingTaskHandle>;
100
Sebastian Jansson6ce033a2020-01-22 10:12:56 +0100101 absl::optional<rtc::IPAddress> GetNextIPv4Address();
Artem Titovcc8a1f82021-01-26 13:58:23 +0100102
103 const TimeMode time_mode_;
Sebastian Jansson6ce033a2020-01-22 10:12:56 +0100104 const std::unique_ptr<TimeController> time_controller_;
Artem Titov0774bd92019-01-30 15:26:05 +0100105 Clock* const clock_;
106 int next_node_id_;
107
108 RepeatingTaskHandle process_task_handle_;
109
Artem Titov0bf4c292019-02-26 10:00:07 +0100110 uint32_t next_ip4_address_;
111 std::set<rtc::IPAddress> used_ip_addresses_;
112
Artem Titov0774bd92019-01-30 15:26:05 +0100113 // All objects can be added to the manager only when it is idle.
Artem Titovaba8dc22019-03-11 10:08:40 +0100114 std::vector<std::unique_ptr<EmulatedEndpoint>> endpoints_;
Artem Titov0774bd92019-01-30 15:26:05 +0100115 std::vector<std::unique_ptr<EmulatedNetworkNode>> network_nodes_;
Artem Titovfc6ab002019-03-12 13:48:32 +0100116 std::vector<std::unique_ptr<EmulatedRoute>> routes_;
Andrey Logvinf9ee0e02021-01-14 09:50:32 +0000117 std::vector<std::unique_ptr<CrossTrafficRoute>> traffic_routes_;
118 std::vector<CrossTrafficSource> cross_traffics_;
Sebastian Janssoncec24332019-12-04 14:26:50 +0100119 std::list<std::unique_ptr<TcpMessageRouteImpl>> tcp_message_routes_;
Artem Titov0d32a732019-04-05 14:49:59 +0200120 std::vector<std::unique_ptr<EndpointsContainer>> endpoints_containers_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100121 std::vector<std::unique_ptr<EmulatedNetworkManager>> network_managers_;
Jonas Oreland97050112020-11-17 21:30:33 +0100122 std::vector<std::unique_ptr<EmulatedTURNServer>> turn_servers_;
Artem Titove5cc85b2019-03-28 12:11:09 +0100123
124 std::map<EmulatedEndpoint*, EmulatedNetworkManager*>
125 endpoint_to_network_manager_;
Artem Titov0774bd92019-01-30 15:26:05 +0100126
127 // Must be the last field, so it will be deleted first, because tasks
128 // in the TaskQueue can access other fields of the instance of this class.
Danil Chapovalov982b5762019-03-21 09:58:30 +0100129 TaskQueueForTest task_queue_;
Artem Titov0774bd92019-01-30 15:26:05 +0100130};
131
132} // namespace test
133} // namespace webrtc
134
Artem Titov386802e2019-07-05 10:48:17 +0200135#endif // TEST_NETWORK_NETWORK_EMULATION_MANAGER_H_