You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "call/simulated_network.h"
  11. #include <algorithm>
  12. #include <cmath>
  13. #include <utility>
  14. #include "api/units/data_rate.h"
  15. #include "api/units/data_size.h"
  16. #include "api/units/time_delta.h"
  17. #include "rtc_base/checks.h"
  18. namespace webrtc {
  19. namespace {
  20. constexpr TimeDelta kDefaultProcessDelay = TimeDelta::Millis<5>();
  21. } // namespace
  22. CoDelSimulation::CoDelSimulation() = default;
  23. CoDelSimulation::~CoDelSimulation() = default;
  24. bool CoDelSimulation::DropDequeuedPacket(Timestamp now,
  25. Timestamp enqueing_time,
  26. DataSize packet_size,
  27. DataSize queue_size) {
  28. constexpr TimeDelta kWindow = TimeDelta::Millis<100>();
  29. constexpr TimeDelta kDelayThreshold = TimeDelta::Millis<5>();
  30. constexpr TimeDelta kDropCountMemory = TimeDelta::Millis<1600>();
  31. constexpr DataSize kMaxPacketSize = DataSize::Bytes<1500>();
  32. // Compensates for process interval in simulation; not part of standard CoDel.
  33. TimeDelta queuing_time = now - enqueing_time - kDefaultProcessDelay;
  34. if (queue_size < kMaxPacketSize || queuing_time < kDelayThreshold) {
  35. enter_drop_state_at_ = Timestamp::PlusInfinity();
  36. state_ = kNormal;
  37. return false;
  38. }
  39. switch (state_) {
  40. case kNormal:
  41. enter_drop_state_at_ = now + kWindow;
  42. state_ = kPending;
  43. return false;
  44. case kPending:
  45. if (now >= enter_drop_state_at_) {
  46. state_ = kDropping;
  47. // Starting the drop counter with the drops made during the most recent
  48. // drop state period.
  49. drop_count_ = drop_count_ - previous_drop_count_;
  50. if (now >= last_drop_at_ + kDropCountMemory)
  51. drop_count_ = 0;
  52. previous_drop_count_ = drop_count_;
  53. last_drop_at_ = now;
  54. ++drop_count_;
  55. return true;
  56. }
  57. return false;
  58. case kDropping:
  59. TimeDelta drop_delay = kWindow / sqrt(static_cast<double>(drop_count_));
  60. Timestamp next_drop_at = last_drop_at_ + drop_delay;
  61. if (now >= next_drop_at) {
  62. if (queue_size - packet_size < kMaxPacketSize)
  63. state_ = kPending;
  64. last_drop_at_ = next_drop_at;
  65. ++drop_count_;
  66. return true;
  67. }
  68. return false;
  69. }
  70. }
  71. SimulatedNetwork::SimulatedNetwork(SimulatedNetwork::Config config,
  72. uint64_t random_seed)
  73. : random_(random_seed), bursting_(false) {
  74. SetConfig(config);
  75. }
  76. SimulatedNetwork::~SimulatedNetwork() = default;
  77. void SimulatedNetwork::SetConfig(const SimulatedNetwork::Config& config) {
  78. rtc::CritScope crit(&config_lock_);
  79. config_state_.config = config; // Shallow copy of the struct.
  80. double prob_loss = config.loss_percent / 100.0;
  81. if (config_state_.config.avg_burst_loss_length == -1) {
  82. // Uniform loss
  83. config_state_.prob_loss_bursting = prob_loss;
  84. config_state_.prob_start_bursting = prob_loss;
  85. } else {
  86. // Lose packets according to a gilbert-elliot model.
  87. int avg_burst_loss_length = config.avg_burst_loss_length;
  88. int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss));
  89. RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length)
  90. << "For a total packet loss of " << config.loss_percent << "%% then"
  91. << " avg_burst_loss_length must be " << min_avg_burst_loss_length + 1
  92. << " or higher.";
  93. config_state_.prob_loss_bursting = (1.0 - 1.0 / avg_burst_loss_length);
  94. config_state_.prob_start_bursting =
  95. prob_loss / (1 - prob_loss) / avg_burst_loss_length;
  96. }
  97. }
  98. void SimulatedNetwork::PauseTransmissionUntil(int64_t until_us) {
  99. rtc::CritScope crit(&config_lock_);
  100. config_state_.pause_transmission_until_us = until_us;
  101. }
  102. bool SimulatedNetwork::EnqueuePacket(PacketInFlightInfo packet) {
  103. RTC_DCHECK_RUNS_SERIALIZED(&process_checker_);
  104. ConfigState state = GetConfigState();
  105. UpdateCapacityQueue(state, packet.send_time_us);
  106. packet.size += state.config.packet_overhead;
  107. if (state.config.queue_length_packets > 0 &&
  108. capacity_link_.size() >= state.config.queue_length_packets) {
  109. // Too many packet on the link, drop this one.
  110. return false;
  111. }
  112. // Set arrival time = send time for now; actual arrival time will be
  113. // calculated in UpdateCapacityQueue.
  114. queue_size_bytes_ += packet.size;
  115. capacity_link_.push({packet, packet.send_time_us});
  116. if (!next_process_time_us_) {
  117. next_process_time_us_ = packet.send_time_us + kDefaultProcessDelay.us();
  118. }
  119. return true;
  120. }
  121. absl::optional<int64_t> SimulatedNetwork::NextDeliveryTimeUs() const {
  122. RTC_DCHECK_RUNS_SERIALIZED(&process_checker_);
  123. return next_process_time_us_;
  124. }
  125. void SimulatedNetwork::UpdateCapacityQueue(ConfigState state,
  126. int64_t time_now_us) {
  127. bool needs_sort = false;
  128. // Catch for thread races.
  129. if (time_now_us < last_capacity_link_visit_us_.value_or(time_now_us))
  130. return;
  131. int64_t time_us = last_capacity_link_visit_us_.value_or(time_now_us);
  132. // Check the capacity link first.
  133. while (!capacity_link_.empty()) {
  134. int64_t time_until_front_exits_us = 0;
  135. if (state.config.link_capacity_kbps > 0) {
  136. int64_t remaining_bits =
  137. capacity_link_.front().packet.size * 8 - pending_drain_bits_;
  138. RTC_DCHECK(remaining_bits > 0);
  139. // Division rounded up - packet not delivered until its last bit is.
  140. time_until_front_exits_us =
  141. (1000 * remaining_bits + state.config.link_capacity_kbps - 1) /
  142. state.config.link_capacity_kbps;
  143. }
  144. if (time_us + time_until_front_exits_us > time_now_us) {
  145. // Packet at front will not exit yet. Will not enter here on infinite
  146. // capacity(=0) so no special handling needed.
  147. pending_drain_bits_ +=
  148. ((time_now_us - time_us) * state.config.link_capacity_kbps) / 1000;
  149. break;
  150. }
  151. if (state.config.link_capacity_kbps > 0) {
  152. pending_drain_bits_ +=
  153. (time_until_front_exits_us * state.config.link_capacity_kbps) / 1000;
  154. } else {
  155. // Enough to drain the whole queue.
  156. pending_drain_bits_ = queue_size_bytes_ * 8;
  157. }
  158. // Time to get this packet.
  159. PacketInfo packet = capacity_link_.front();
  160. capacity_link_.pop();
  161. time_us += time_until_front_exits_us;
  162. if (state.config.codel_active_queue_management) {
  163. while (!capacity_link_.empty() &&
  164. codel_controller_.DropDequeuedPacket(
  165. Timestamp::us(time_us),
  166. Timestamp::us(capacity_link_.front().packet.send_time_us),
  167. DataSize::bytes(capacity_link_.front().packet.size),
  168. DataSize::bytes(queue_size_bytes_))) {
  169. PacketInfo dropped = capacity_link_.front();
  170. capacity_link_.pop();
  171. queue_size_bytes_ -= dropped.packet.size;
  172. dropped.arrival_time_us = PacketDeliveryInfo::kNotReceived;
  173. delay_link_.emplace_back(dropped);
  174. }
  175. }
  176. RTC_DCHECK(time_us >= packet.packet.send_time_us);
  177. packet.arrival_time_us =
  178. std::max(state.pause_transmission_until_us, time_us);
  179. queue_size_bytes_ -= packet.packet.size;
  180. pending_drain_bits_ -= packet.packet.size * 8;
  181. RTC_DCHECK(pending_drain_bits_ >= 0);
  182. // Drop packets at an average rate of |state.config.loss_percent| with
  183. // and average loss burst length of |state.config.avg_burst_loss_length|.
  184. if ((bursting_ && random_.Rand<double>() < state.prob_loss_bursting) ||
  185. (!bursting_ && random_.Rand<double>() < state.prob_start_bursting)) {
  186. bursting_ = true;
  187. packet.arrival_time_us = PacketDeliveryInfo::kNotReceived;
  188. } else {
  189. bursting_ = false;
  190. int64_t arrival_time_jitter_us = std::max(
  191. random_.Gaussian(state.config.queue_delay_ms * 1000,
  192. state.config.delay_standard_deviation_ms * 1000),
  193. 0.0);
  194. // If reordering is not allowed then adjust arrival_time_jitter
  195. // to make sure all packets are sent in order.
  196. int64_t last_arrival_time_us =
  197. delay_link_.empty() ? -1 : delay_link_.back().arrival_time_us;
  198. if (!state.config.allow_reordering && !delay_link_.empty() &&
  199. packet.arrival_time_us + arrival_time_jitter_us <
  200. last_arrival_time_us) {
  201. arrival_time_jitter_us = last_arrival_time_us - packet.arrival_time_us;
  202. }
  203. packet.arrival_time_us += arrival_time_jitter_us;
  204. if (packet.arrival_time_us >= last_arrival_time_us) {
  205. last_arrival_time_us = packet.arrival_time_us;
  206. } else {
  207. needs_sort = true;
  208. }
  209. }
  210. delay_link_.emplace_back(packet);
  211. }
  212. last_capacity_link_visit_us_ = time_now_us;
  213. // Cannot save unused capacity for later.
  214. pending_drain_bits_ = std::min(pending_drain_bits_, queue_size_bytes_ * 8);
  215. if (needs_sort) {
  216. // Packet(s) arrived out of order, make sure list is sorted.
  217. std::sort(delay_link_.begin(), delay_link_.end(),
  218. [](const PacketInfo& p1, const PacketInfo& p2) {
  219. return p1.arrival_time_us < p2.arrival_time_us;
  220. });
  221. }
  222. }
  223. SimulatedNetwork::ConfigState SimulatedNetwork::GetConfigState() const {
  224. rtc::CritScope crit(&config_lock_);
  225. return config_state_;
  226. }
  227. std::vector<PacketDeliveryInfo> SimulatedNetwork::DequeueDeliverablePackets(
  228. int64_t receive_time_us) {
  229. RTC_DCHECK_RUNS_SERIALIZED(&process_checker_);
  230. UpdateCapacityQueue(GetConfigState(), receive_time_us);
  231. std::vector<PacketDeliveryInfo> packets_to_deliver;
  232. // Check the extra delay queue.
  233. while (!delay_link_.empty() &&
  234. receive_time_us >= delay_link_.front().arrival_time_us) {
  235. PacketInfo packet_info = delay_link_.front();
  236. packets_to_deliver.emplace_back(
  237. PacketDeliveryInfo(packet_info.packet, packet_info.arrival_time_us));
  238. delay_link_.pop_front();
  239. }
  240. if (!delay_link_.empty()) {
  241. next_process_time_us_ = delay_link_.front().arrival_time_us;
  242. } else if (!capacity_link_.empty()) {
  243. next_process_time_us_ = receive_time_us + kDefaultProcessDelay.us();
  244. } else {
  245. next_process_time_us_.reset();
  246. }
  247. return packets_to_deliver;
  248. }
  249. } // namespace webrtc