xref: /aosp_15_r20/external/webrtc/test/network/network_emulation_manager.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #ifndef TEST_NETWORK_NETWORK_EMULATION_MANAGER_H_
12 #define TEST_NETWORK_NETWORK_EMULATION_MANAGER_H_
13 
14 #include <map>
15 #include <memory>
16 #include <set>
17 #include <utility>
18 #include <vector>
19 
20 #include "api/array_view.h"
21 #include "api/test/network_emulation_manager.h"
22 #include "api/test/simulated_network.h"
23 #include "api/test/time_controller.h"
24 #include "api/units/time_delta.h"
25 #include "api/units/timestamp.h"
26 #include "rtc_base/task_queue_for_test.h"
27 #include "rtc_base/task_utils/repeating_task.h"
28 #include "system_wrappers/include/clock.h"
29 #include "test/network/cross_traffic.h"
30 #include "test/network/emulated_network_manager.h"
31 #include "test/network/emulated_turn_server.h"
32 #include "test/network/network_emulation.h"
33 
34 namespace webrtc {
35 namespace test {
36 
37 class NetworkEmulationManagerImpl : public NetworkEmulationManager {
38  public:
39   NetworkEmulationManagerImpl(
40       TimeMode mode,
41       EmulatedNetworkStatsGatheringMode stats_gathering_mode);
42   ~NetworkEmulationManagerImpl();
43 
44   EmulatedNetworkNode* CreateEmulatedNode(BuiltInNetworkBehaviorConfig config,
45                                           uint64_t random_seed = 1) override;
46   EmulatedNetworkNode* CreateEmulatedNode(
47       std::unique_ptr<NetworkBehaviorInterface> network_behavior) override;
48 
49   SimulatedNetworkNode::Builder NodeBuilder() override;
50 
51   EmulatedEndpointImpl* CreateEndpoint(EmulatedEndpointConfig config) override;
52   void EnableEndpoint(EmulatedEndpoint* endpoint) override;
53   void DisableEndpoint(EmulatedEndpoint* endpoint) override;
54 
55   EmulatedRoute* CreateRoute(EmulatedEndpoint* from,
56                              const std::vector<EmulatedNetworkNode*>& via_nodes,
57                              EmulatedEndpoint* to) override;
58 
59   EmulatedRoute* CreateRoute(
60       const std::vector<EmulatedNetworkNode*>& via_nodes) override;
61 
62   EmulatedRoute* CreateDefaultRoute(
63       EmulatedEndpoint* from,
64       const std::vector<EmulatedNetworkNode*>& via_nodes,
65       EmulatedEndpoint* to) override;
66 
67   void ClearRoute(EmulatedRoute* route) override;
68 
69   TcpMessageRoute* CreateTcpRoute(EmulatedRoute* send_route,
70                                   EmulatedRoute* ret_route) override;
71 
72   CrossTrafficRoute* CreateCrossTrafficRoute(
73       const std::vector<EmulatedNetworkNode*>& via_nodes) override;
74 
75   CrossTrafficGenerator* StartCrossTraffic(
76       std::unique_ptr<CrossTrafficGenerator> generator) override;
77   void StopCrossTraffic(CrossTrafficGenerator* generator) override;
78 
79   EmulatedNetworkManagerInterface* CreateEmulatedNetworkManagerInterface(
80       const std::vector<EmulatedEndpoint*>& endpoints) override;
81 
82   void GetStats(
83       rtc::ArrayView<EmulatedEndpoint* const> endpoints,
84       std::function<void(EmulatedNetworkStats)> stats_callback) override;
85 
86   void GetStats(
87       rtc::ArrayView<EmulatedNetworkNode* const> nodes,
88       std::function<void(EmulatedNetworkNodeStats)> stats_callback) override;
89 
time_controller()90   TimeController* time_controller() override { return time_controller_.get(); }
91 
time_mode()92   TimeMode time_mode() const override { return time_mode_; }
93 
94   Timestamp Now() const;
95 
96   EmulatedTURNServerInterface* CreateTURNServer(
97       EmulatedTURNServerConfig config) override;
98 
99  private:
100   using CrossTrafficSource =
101       std::pair<std::unique_ptr<CrossTrafficGenerator>, RepeatingTaskHandle>;
102 
103   absl::optional<rtc::IPAddress> GetNextIPv4Address();
104 
105   const TimeMode time_mode_;
106   const EmulatedNetworkStatsGatheringMode stats_gathering_mode_;
107   const std::unique_ptr<TimeController> time_controller_;
108   Clock* const clock_;
109   int next_node_id_;
110 
111   RepeatingTaskHandle process_task_handle_;
112 
113   uint32_t next_ip4_address_;
114   std::set<rtc::IPAddress> used_ip_addresses_;
115 
116   // All objects can be added to the manager only when it is idle.
117   std::vector<std::unique_ptr<EmulatedEndpoint>> endpoints_;
118   std::vector<std::unique_ptr<EmulatedNetworkNode>> network_nodes_;
119   std::vector<std::unique_ptr<EmulatedRoute>> routes_;
120   std::vector<std::unique_ptr<CrossTrafficRoute>> traffic_routes_;
121   std::vector<CrossTrafficSource> cross_traffics_;
122   std::list<std::unique_ptr<TcpMessageRouteImpl>> tcp_message_routes_;
123   std::vector<std::unique_ptr<EndpointsContainer>> endpoints_containers_;
124   std::vector<std::unique_ptr<EmulatedNetworkManager>> network_managers_;
125   std::vector<std::unique_ptr<EmulatedTURNServer>> turn_servers_;
126 
127   std::map<EmulatedEndpoint*, EmulatedNetworkManager*>
128       endpoint_to_network_manager_;
129 
130   // Must be the last field, so it will be deleted first, because tasks
131   // in the TaskQueue can access other fields of the instance of this class.
132   TaskQueueForTest task_queue_;
133 };
134 
135 }  // namespace test
136 }  // namespace webrtc
137 
138 #endif  // TEST_NETWORK_NETWORK_EMULATION_MANAGER_H_
139