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/test/network_emulation_manager.h" 21 #include "api/test/simulated_network.h" 22 #include "api/test/time_controller.h" 23 #include "api/units/time_delta.h" 24 #include "api/units/timestamp.h" 25 #include "rtc_base/logging.h" 26 #include "rtc_base/network.h" 27 #include "rtc_base/task_queue_for_test.h" 28 #include "rtc_base/task_utils/repeating_task.h" 29 #include "rtc_base/thread.h" 30 #include "system_wrappers/include/clock.h" 31 #include "test/network/cross_traffic.h" 32 #include "test/network/emulated_network_manager.h" 33 #include "test/network/fake_network_socket_server.h" 34 #include "test/network/network_emulation.h" 35 #include "test/network/traffic_route.h" 36 37 namespace webrtc { 38 namespace test { 39 40 class NetworkEmulationManagerImpl : public NetworkEmulationManager { 41 public: 42 explicit NetworkEmulationManagerImpl(TimeMode mode); 43 ~NetworkEmulationManagerImpl(); 44 45 EmulatedNetworkNode* CreateEmulatedNode( 46 BuiltInNetworkBehaviorConfig config) override; 47 EmulatedNetworkNode* CreateEmulatedNode( 48 std::unique_ptr<NetworkBehaviorInterface> network_behavior) override; 49 50 SimulatedNetworkNode::Builder NodeBuilder() override; 51 52 EmulatedEndpoint* CreateEndpoint(EmulatedEndpointConfig config) override; 53 void EnableEndpoint(EmulatedEndpoint* endpoint) override; 54 void DisableEndpoint(EmulatedEndpoint* endpoint) override; 55 56 EmulatedRoute* CreateRoute(EmulatedEndpoint* from, 57 const std::vector<EmulatedNetworkNode*>& via_nodes, 58 EmulatedEndpoint* to) override; 59 60 EmulatedRoute* CreateRoute( 61 const std::vector<EmulatedNetworkNode*>& via_nodes) override; 62 63 void ClearRoute(EmulatedRoute* route) override; 64 65 TrafficRoute* CreateTrafficRoute( 66 const std::vector<EmulatedNetworkNode*>& via_nodes); 67 RandomWalkCrossTraffic* CreateRandomWalkCrossTraffic( 68 TrafficRoute* traffic_route, 69 RandomWalkConfig config); 70 PulsedPeaksCrossTraffic* CreatePulsedPeaksCrossTraffic( 71 TrafficRoute* traffic_route, 72 PulsedPeaksConfig config); 73 FakeTcpCrossTraffic* StartFakeTcpCrossTraffic( 74 std::vector<EmulatedNetworkNode*> send_link, 75 std::vector<EmulatedNetworkNode*> ret_link, 76 FakeTcpConfig config); 77 78 TcpMessageRoute* CreateTcpRoute(EmulatedRoute* send_route, 79 EmulatedRoute* ret_route) override; 80 81 void StopCrossTraffic(FakeTcpCrossTraffic* traffic); 82 83 EmulatedNetworkManagerInterface* CreateEmulatedNetworkManagerInterface( 84 const std::vector<EmulatedEndpoint*>& endpoints) override; 85 time_controller()86 TimeController* time_controller() override { return time_controller_.get(); } 87 88 Timestamp Now() const; 89 90 private: 91 absl::optional<rtc::IPAddress> GetNextIPv4Address(); 92 const std::unique_ptr<TimeController> time_controller_; 93 Clock* const clock_; 94 int next_node_id_; 95 96 RepeatingTaskHandle process_task_handle_; 97 98 uint32_t next_ip4_address_; 99 std::set<rtc::IPAddress> used_ip_addresses_; 100 101 // All objects can be added to the manager only when it is idle. 102 std::vector<std::unique_ptr<EmulatedEndpoint>> endpoints_; 103 std::vector<std::unique_ptr<EmulatedNetworkNode>> network_nodes_; 104 std::vector<std::unique_ptr<EmulatedRoute>> routes_; 105 std::vector<std::unique_ptr<TrafficRoute>> traffic_routes_; 106 std::vector<std::unique_ptr<RandomWalkCrossTraffic>> random_cross_traffics_; 107 std::vector<std::unique_ptr<PulsedPeaksCrossTraffic>> pulsed_cross_traffics_; 108 std::list<std::unique_ptr<FakeTcpCrossTraffic>> tcp_cross_traffics_; 109 std::list<std::unique_ptr<TcpMessageRouteImpl>> tcp_message_routes_; 110 std::vector<std::unique_ptr<EndpointsContainer>> endpoints_containers_; 111 std::vector<std::unique_ptr<EmulatedNetworkManager>> network_managers_; 112 113 std::map<EmulatedEndpoint*, EmulatedNetworkManager*> 114 endpoint_to_network_manager_; 115 116 // Must be the last field, so it will be deleted first, because tasks 117 // in the TaskQueue can access other fields of the instance of this class. 118 TaskQueueForTest task_queue_; 119 }; 120 121 } // namespace test 122 } // namespace webrtc 123 124 #endif // TEST_NETWORK_NETWORK_EMULATION_MANAGER_H_ 125