cross_traffic.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_CROSS_TRAFFIC_H_
  11. #define TEST_NETWORK_CROSS_TRAFFIC_H_
  12. #include <algorithm>
  13. #include <map>
  14. #include <memory>
  15. #include "api/units/data_rate.h"
  16. #include "api/units/data_size.h"
  17. #include "api/units/time_delta.h"
  18. #include "api/units/timestamp.h"
  19. #include "rtc_base/random.h"
  20. #include "rtc_base/synchronization/sequence_checker.h"
  21. #include "test/network/traffic_route.h"
  22. #include "test/scenario/column_printer.h"
  23. namespace webrtc {
  24. namespace test {
  25. struct RandomWalkConfig {
  26. int random_seed = 1;
  27. DataRate peak_rate = DataRate::KilobitsPerSec(100);
  28. DataSize min_packet_size = DataSize::Bytes(200);
  29. TimeDelta min_packet_interval = TimeDelta::Millis(1);
  30. TimeDelta update_interval = TimeDelta::Millis(200);
  31. double variance = 0.6;
  32. double bias = -0.1;
  33. };
  34. class RandomWalkCrossTraffic {
  35. public:
  36. RandomWalkCrossTraffic(RandomWalkConfig config, TrafficRoute* traffic_route);
  37. ~RandomWalkCrossTraffic();
  38. void Process(Timestamp at_time);
  39. DataRate TrafficRate() const;
  40. ColumnPrinter StatsPrinter();
  41. private:
  42. SequenceChecker sequence_checker_;
  43. const RandomWalkConfig config_;
  44. TrafficRoute* const traffic_route_ RTC_PT_GUARDED_BY(sequence_checker_);
  45. webrtc::Random random_ RTC_GUARDED_BY(sequence_checker_);
  46. Timestamp last_process_time_ RTC_GUARDED_BY(sequence_checker_) =
  47. Timestamp::MinusInfinity();
  48. Timestamp last_update_time_ RTC_GUARDED_BY(sequence_checker_) =
  49. Timestamp::MinusInfinity();
  50. Timestamp last_send_time_ RTC_GUARDED_BY(sequence_checker_) =
  51. Timestamp::MinusInfinity();
  52. double intensity_ RTC_GUARDED_BY(sequence_checker_) = 0;
  53. DataSize pending_size_ RTC_GUARDED_BY(sequence_checker_) = DataSize::Zero();
  54. };
  55. struct PulsedPeaksConfig {
  56. DataRate peak_rate = DataRate::KilobitsPerSec(100);
  57. DataSize min_packet_size = DataSize::Bytes(200);
  58. TimeDelta min_packet_interval = TimeDelta::Millis(1);
  59. TimeDelta send_duration = TimeDelta::Millis(100);
  60. TimeDelta hold_duration = TimeDelta::Millis(2000);
  61. };
  62. class PulsedPeaksCrossTraffic {
  63. public:
  64. PulsedPeaksCrossTraffic(PulsedPeaksConfig config,
  65. TrafficRoute* traffic_route);
  66. ~PulsedPeaksCrossTraffic();
  67. void Process(Timestamp at_time);
  68. DataRate TrafficRate() const;
  69. ColumnPrinter StatsPrinter();
  70. private:
  71. SequenceChecker sequence_checker_;
  72. const PulsedPeaksConfig config_;
  73. TrafficRoute* const traffic_route_ RTC_PT_GUARDED_BY(sequence_checker_);
  74. Timestamp last_update_time_ RTC_GUARDED_BY(sequence_checker_) =
  75. Timestamp::MinusInfinity();
  76. Timestamp last_send_time_ RTC_GUARDED_BY(sequence_checker_) =
  77. Timestamp::MinusInfinity();
  78. bool sending_ RTC_GUARDED_BY(sequence_checker_) = false;
  79. };
  80. class TcpMessageRouteImpl final : public TcpMessageRoute {
  81. public:
  82. TcpMessageRouteImpl(Clock* clock,
  83. TaskQueueBase* task_queue,
  84. EmulatedRoute* send_route,
  85. EmulatedRoute* ret_route);
  86. // Sends a TCP message of the given |size| over the route, |on_received| is
  87. // called when the message has been delivered. Note that the connection
  88. // parameters are reset iff there's no currently pending message on the route.
  89. void SendMessage(size_t size, std::function<void()> on_received) override;
  90. private:
  91. // Represents a message sent over the route. When all fragments has been
  92. // delivered, the message is considered delivered and the handler is
  93. // triggered. This only happen once.
  94. struct Message {
  95. std::function<void()> handler;
  96. std::set<int> pending_fragment_ids;
  97. };
  98. // Represents a piece of a message that fit into a TCP packet.
  99. struct MessageFragment {
  100. int fragment_id;
  101. size_t size;
  102. };
  103. // Represents a packet sent on the wire.
  104. struct TcpPacket {
  105. int sequence_number;
  106. Timestamp send_time = Timestamp::MinusInfinity();
  107. MessageFragment fragment;
  108. };
  109. void OnRequest(TcpPacket packet_info);
  110. void OnResponse(TcpPacket packet_info, Timestamp at_time);
  111. void HandleLoss(Timestamp at_time);
  112. void SendPackets(Timestamp at_time);
  113. void HandlePacketTimeout(int seq_num, Timestamp at_time);
  114. Clock* const clock_;
  115. TaskQueueBase* const task_queue_;
  116. FakePacketRoute<TcpPacket> request_route_;
  117. FakePacketRoute<TcpPacket> response_route_;
  118. std::deque<MessageFragment> pending_;
  119. std::map<int, TcpPacket> in_flight_;
  120. std::list<Message> messages_;
  121. double cwnd_;
  122. double ssthresh_;
  123. int last_acked_seq_num_ = 0;
  124. int next_sequence_number_ = 0;
  125. int next_fragment_id_ = 0;
  126. Timestamp last_reduction_time_ = Timestamp::MinusInfinity();
  127. TimeDelta last_rtt_ = TimeDelta::Zero();
  128. };
  129. struct FakeTcpConfig {
  130. DataSize packet_size = DataSize::Bytes(1200);
  131. DataSize send_limit = DataSize::PlusInfinity();
  132. TimeDelta process_interval = TimeDelta::Millis(200);
  133. TimeDelta packet_timeout = TimeDelta::Seconds(1);
  134. };
  135. class FakeTcpCrossTraffic
  136. : public TwoWayFakeTrafficRoute<int, int>::TrafficHandlerInterface {
  137. public:
  138. FakeTcpCrossTraffic(Clock* clock,
  139. FakeTcpConfig config,
  140. EmulatedRoute* send_route,
  141. EmulatedRoute* ret_route);
  142. void Start(TaskQueueBase* task_queue);
  143. void Stop();
  144. void Process(Timestamp at_time);
  145. void OnRequest(int sequence_number, Timestamp at_time) override;
  146. void OnResponse(int sequence_number, Timestamp at_time) override;
  147. void HandleLoss(Timestamp at_time);
  148. void SendPackets(Timestamp at_time);
  149. private:
  150. Clock* const clock_;
  151. const FakeTcpConfig conf_;
  152. TwoWayFakeTrafficRoute<int, int> route_;
  153. std::map<int, Timestamp> in_flight_;
  154. double cwnd_ = 10;
  155. double ssthresh_ = INFINITY;
  156. bool ack_received_ = false;
  157. int last_acked_seq_num_ = 0;
  158. int next_sequence_number_ = 0;
  159. Timestamp last_reduction_time_ = Timestamp::MinusInfinity();
  160. TimeDelta last_rtt_ = TimeDelta::Zero();
  161. DataSize total_sent_ = DataSize::Zero();
  162. RepeatingTaskHandle repeating_task_handle_;
  163. };
  164. } // namespace test
  165. } // namespace webrtc
  166. #endif // TEST_NETWORK_CROSS_TRAFFIC_H_