simulated_network.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2018 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 CALL_SIMULATED_NETWORK_H_
  11. #define CALL_SIMULATED_NETWORK_H_
  12. #include <stdint.h>
  13. #include <deque>
  14. #include <queue>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "api/test/simulated_network.h"
  18. #include "api/units/data_size.h"
  19. #include "api/units/timestamp.h"
  20. #include "rtc_base/race_checker.h"
  21. #include "rtc_base/random.h"
  22. #include "rtc_base/synchronization/mutex.h"
  23. #include "rtc_base/thread_annotations.h"
  24. #include "rtc_base/thread_checker.h"
  25. namespace webrtc {
  26. // Implementation of the CoDel active queue management algorithm. Loosely based
  27. // on CoDel pseudocode from ACMQueue. CoDel keeps queuing delays low by dropping
  28. // packets when delay is high. For each packet ready for dequeue, call
  29. // DropDequeuePacket with the packet parameters to update the CoDel state.
  30. class CoDelSimulation {
  31. public:
  32. CoDelSimulation();
  33. ~CoDelSimulation();
  34. // Returns true if packet should be dropped.
  35. bool DropDequeuedPacket(Timestamp now,
  36. Timestamp enqueing_time,
  37. DataSize packet_size,
  38. DataSize queue_size);
  39. private:
  40. enum State { kNormal, kPending, kDropping };
  41. Timestamp enter_drop_state_at_ = Timestamp::PlusInfinity();
  42. Timestamp last_drop_at_ = Timestamp::MinusInfinity();
  43. int drop_count_ = 0;
  44. int previous_drop_count_ = 0;
  45. State state_ = State::kNormal;
  46. };
  47. // Class simulating a network link. This is a simple and naive solution just
  48. // faking capacity and adding an extra transport delay in addition to the
  49. // capacity introduced delay.
  50. class SimulatedNetwork : public SimulatedNetworkInterface {
  51. public:
  52. using Config = BuiltInNetworkBehaviorConfig;
  53. explicit SimulatedNetwork(Config config, uint64_t random_seed = 1);
  54. ~SimulatedNetwork() override;
  55. // Sets a new configuration. This won't affect packets already in the pipe.
  56. void SetConfig(const Config& config) override;
  57. void UpdateConfig(std::function<void(BuiltInNetworkBehaviorConfig*)>
  58. config_modifier) override;
  59. void PauseTransmissionUntil(int64_t until_us) override;
  60. // NetworkBehaviorInterface
  61. bool EnqueuePacket(PacketInFlightInfo packet) override;
  62. std::vector<PacketDeliveryInfo> DequeueDeliverablePackets(
  63. int64_t receive_time_us) override;
  64. absl::optional<int64_t> NextDeliveryTimeUs() const override;
  65. private:
  66. struct PacketInfo {
  67. PacketInFlightInfo packet;
  68. int64_t arrival_time_us;
  69. };
  70. // Contains current configuration state.
  71. struct ConfigState {
  72. // Static link configuration.
  73. Config config;
  74. // The probability to drop the packet if we are currently dropping a
  75. // burst of packet
  76. double prob_loss_bursting;
  77. // The probability to drop a burst of packets.
  78. double prob_start_bursting;
  79. // Used for temporary delay spikes.
  80. int64_t pause_transmission_until_us = 0;
  81. };
  82. // Moves packets from capacity- to delay link.
  83. void UpdateCapacityQueue(ConfigState state, int64_t time_now_us)
  84. RTC_RUN_ON(&process_checker_);
  85. ConfigState GetConfigState() const;
  86. mutable Mutex config_lock_;
  87. // |process_checker_| guards the data structures involved in delay and loss
  88. // processes, such as the packet queues.
  89. rtc::RaceChecker process_checker_;
  90. CoDelSimulation codel_controller_ RTC_GUARDED_BY(process_checker_);
  91. std::queue<PacketInfo> capacity_link_ RTC_GUARDED_BY(process_checker_);
  92. Random random_;
  93. std::deque<PacketInfo> delay_link_ RTC_GUARDED_BY(process_checker_);
  94. ConfigState config_state_ RTC_GUARDED_BY(config_lock_);
  95. // Are we currently dropping a burst of packets?
  96. bool bursting_;
  97. int64_t queue_size_bytes_ RTC_GUARDED_BY(process_checker_) = 0;
  98. int64_t pending_drain_bits_ RTC_GUARDED_BY(process_checker_) = 0;
  99. absl::optional<int64_t> last_capacity_link_visit_us_
  100. RTC_GUARDED_BY(process_checker_);
  101. absl::optional<int64_t> next_process_time_us_
  102. RTC_GUARDED_BY(process_checker_);
  103. };
  104. } // namespace webrtc
  105. #endif // CALL_SIMULATED_NETWORK_H_