network_emulation_manager.h 4.8 KB

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