Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 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 "test/scenario/network_node.h" |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <vector> |
| 14 | |
Steve Anton | 40d5533 | 2019-01-07 10:21:47 -0800 | [diff] [blame] | 15 | #include "absl/memory/memory.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 16 | #include "rtc_base/numerics/safe_minmax.h" |
| 17 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | namespace test { |
| 20 | namespace { |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame^] | 21 | constexpr char kDummyTransportName[] = "dummy"; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 22 | SimulatedNetwork::Config CreateSimulationConfig(NetworkNodeConfig config) { |
| 23 | SimulatedNetwork::Config sim_config; |
| 24 | sim_config.link_capacity_kbps = config.simulation.bandwidth.kbps_or(0); |
| 25 | sim_config.loss_percent = config.simulation.loss_rate * 100; |
| 26 | sim_config.queue_delay_ms = config.simulation.delay.ms(); |
| 27 | sim_config.delay_standard_deviation_ms = config.simulation.delay_std_dev.ms(); |
Sebastian Jansson | 8c8feb9 | 2019-01-29 15:59:17 +0100 | [diff] [blame] | 28 | sim_config.packet_overhead = config.packet_overhead.bytes<int>(); |
Sebastian Jansson | 2b08e31 | 2019-02-25 10:24:46 +0100 | [diff] [blame] | 29 | sim_config.codel_active_queue_management = |
| 30 | config.simulation.codel_active_queue_management; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 31 | return sim_config; |
| 32 | } |
| 33 | } // namespace |
| 34 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 35 | void NullReceiver::OnPacketReceived(EmulatedIpPacket packet) {} |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 36 | |
| 37 | ActionReceiver::ActionReceiver(std::function<void()> action) |
| 38 | : action_(action) {} |
| 39 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 40 | void ActionReceiver::OnPacketReceived(EmulatedIpPacket packet) { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 41 | action_(); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 44 | std::unique_ptr<SimulationNode> SimulationNode::Create( |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame^] | 45 | Clock* clock, |
| 46 | rtc::TaskQueue* task_queue, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 47 | NetworkNodeConfig config) { |
| 48 | RTC_DCHECK(config.mode == NetworkNodeConfig::TrafficMode::kSimulation); |
| 49 | SimulatedNetwork::Config sim_config = CreateSimulationConfig(config); |
| 50 | auto network = absl::make_unique<SimulatedNetwork>(sim_config); |
| 51 | SimulatedNetwork* simulation_ptr = network.get(); |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame^] | 52 | return std::unique_ptr<SimulationNode>(new SimulationNode( |
| 53 | clock, task_queue, config, std::move(network), simulation_ptr)); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void SimulationNode::UpdateConfig( |
| 57 | std::function<void(NetworkNodeConfig*)> modifier) { |
| 58 | modifier(&config_); |
| 59 | SimulatedNetwork::Config sim_config = CreateSimulationConfig(config_); |
| 60 | simulated_network_->SetConfig(sim_config); |
| 61 | } |
| 62 | |
| 63 | void SimulationNode::PauseTransmissionUntil(Timestamp until) { |
| 64 | simulated_network_->PauseTransmissionUntil(until.us()); |
| 65 | } |
| 66 | |
| 67 | ColumnPrinter SimulationNode::ConfigPrinter() const { |
| 68 | return ColumnPrinter::Lambda("propagation_delay capacity loss_rate", |
| 69 | [this](rtc::SimpleStringBuilder& sb) { |
| 70 | sb.AppendFormat( |
| 71 | "%.3lf %.0lf %.2lf", |
| 72 | config_.simulation.delay.seconds<double>(), |
| 73 | config_.simulation.bandwidth.bps() / 8.0, |
| 74 | config_.simulation.loss_rate); |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | SimulationNode::SimulationNode( |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame^] | 79 | Clock* clock, |
| 80 | rtc::TaskQueue* task_queue, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 81 | NetworkNodeConfig config, |
Artem Titov | 8ea1e9d | 2018-10-04 14:46:31 +0200 | [diff] [blame] | 82 | std::unique_ptr<NetworkBehaviorInterface> behavior, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 83 | SimulatedNetwork* simulation) |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame^] | 84 | : EmulatedNetworkNode(clock, task_queue, std::move(behavior)), |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 85 | simulated_network_(simulation), |
| 86 | config_(config) {} |
| 87 | |
Sebastian Jansson | aa01f27 | 2019-01-30 11:28:59 +0100 | [diff] [blame] | 88 | NetworkNodeTransport::NetworkNodeTransport(Clock* sender_clock, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 89 | Call* sender_call) |
| 90 | : sender_clock_(sender_clock), sender_call_(sender_call) {} |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 91 | |
| 92 | NetworkNodeTransport::~NetworkNodeTransport() = default; |
| 93 | |
| 94 | bool NetworkNodeTransport::SendRtp(const uint8_t* packet, |
| 95 | size_t length, |
| 96 | const PacketOptions& options) { |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 97 | int64_t send_time_ms = sender_clock_->TimeInMilliseconds(); |
Sebastian Jansson | 156d11d | 2018-09-28 17:21:34 +0200 | [diff] [blame] | 98 | rtc::SentPacket sent_packet; |
| 99 | sent_packet.packet_id = options.packet_id; |
Sebastian Jansson | 0378997 | 2018-10-09 18:27:57 +0200 | [diff] [blame] | 100 | sent_packet.info.included_in_feedback = options.included_in_feedback; |
| 101 | sent_packet.info.included_in_allocation = options.included_in_allocation; |
Sebastian Jansson | 156d11d | 2018-09-28 17:21:34 +0200 | [diff] [blame] | 102 | sent_packet.send_time_ms = send_time_ms; |
| 103 | sent_packet.info.packet_size_bytes = length; |
| 104 | sent_packet.info.packet_type = rtc::PacketType::kData; |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 105 | sender_call_->OnSentPacket(sent_packet); |
Sebastian Jansson | 156d11d | 2018-09-28 17:21:34 +0200 | [diff] [blame] | 106 | |
| 107 | Timestamp send_time = Timestamp::ms(send_time_ms); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 108 | rtc::CritScope crit(&crit_sect_); |
| 109 | if (!send_net_) |
| 110 | return false; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 111 | rtc::CopyOnWriteBuffer buffer(packet, length, |
| 112 | length + packet_overhead_.bytes()); |
| 113 | buffer.SetSize(length + packet_overhead_.bytes()); |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 114 | send_net_->OnPacketReceived( |
| 115 | EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time)); |
Sebastian Jansson | f65309c | 2018-12-20 10:26:00 +0100 | [diff] [blame] | 116 | return true; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) { |
| 120 | rtc::CopyOnWriteBuffer buffer(packet, length); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 121 | Timestamp send_time = Timestamp::ms(sender_clock_->TimeInMilliseconds()); |
| 122 | rtc::CritScope crit(&crit_sect_); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 123 | buffer.SetSize(length + packet_overhead_.bytes()); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 124 | if (!send_net_) |
| 125 | return false; |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 126 | send_net_->OnPacketReceived( |
| 127 | EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time)); |
Sebastian Jansson | f65309c | 2018-12-20 10:26:00 +0100 | [diff] [blame] | 128 | return true; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 131 | void NetworkNodeTransport::Connect(EmulatedNetworkNode* send_node, |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 132 | rtc::IPAddress receiver_ip, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 133 | DataSize packet_overhead) { |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 134 | rtc::NetworkRoute route; |
| 135 | route.connected = true; |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 136 | route.local_network_id = |
| 137 | static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger()); |
| 138 | route.remote_network_id = |
| 139 | static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger()); |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame^] | 140 | { |
| 141 | // Only IPv4 address is supported. We don't use full range of IPs in |
| 142 | // scenario framework and also we need a simple way to convert IP into |
| 143 | // network_id to signal network route. |
| 144 | RTC_CHECK_EQ(receiver_ip.family(), AF_INET); |
| 145 | RTC_CHECK_LE(receiver_ip.v4AddressAsHostOrderInteger(), |
| 146 | std::numeric_limits<uint16_t>::max()); |
| 147 | rtc::CritScope crit(&crit_sect_); |
| 148 | send_net_ = send_node; |
| 149 | receiver_address_ = rtc::SocketAddress(receiver_ip, 0); |
| 150 | packet_overhead_ = packet_overhead; |
| 151 | current_network_route_ = route; |
| 152 | } |
| 153 | |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 154 | sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged( |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame^] | 155 | kDummyTransportName, route); |
| 156 | } |
| 157 | |
| 158 | void NetworkNodeTransport::Disconnect() { |
| 159 | rtc::CritScope crit(&crit_sect_); |
| 160 | current_network_route_.connected = false; |
| 161 | sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged( |
| 162 | kDummyTransportName, current_network_route_); |
| 163 | current_network_route_ = {}; |
| 164 | send_net_ = nullptr; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 167 | CrossTrafficSource::CrossTrafficSource(EmulatedNetworkReceiverInterface* target, |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 168 | rtc::IPAddress receiver_ip, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 169 | CrossTrafficConfig config) |
| 170 | : target_(target), |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 171 | receiver_address_(receiver_ip, 0), |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 172 | config_(config), |
| 173 | random_(config.random_seed) {} |
| 174 | |
| 175 | CrossTrafficSource::~CrossTrafficSource() = default; |
| 176 | |
| 177 | DataRate CrossTrafficSource::TrafficRate() const { |
| 178 | return config_.peak_rate * intensity_; |
| 179 | } |
| 180 | |
| 181 | void CrossTrafficSource::Process(Timestamp at_time, TimeDelta delta) { |
| 182 | time_since_update_ += delta; |
| 183 | if (config_.mode == CrossTrafficConfig::Mode::kRandomWalk) { |
| 184 | if (time_since_update_ >= config_.random_walk.update_interval) { |
| 185 | intensity_ += random_.Gaussian(config_.random_walk.bias, |
| 186 | config_.random_walk.variance) * |
| 187 | time_since_update_.seconds<double>(); |
| 188 | intensity_ = rtc::SafeClamp(intensity_, 0.0, 1.0); |
| 189 | time_since_update_ = TimeDelta::Zero(); |
| 190 | } |
| 191 | } else if (config_.mode == CrossTrafficConfig::Mode::kPulsedPeaks) { |
| 192 | if (intensity_ == 0 && time_since_update_ >= config_.pulsed.hold_duration) { |
| 193 | intensity_ = 1; |
| 194 | time_since_update_ = TimeDelta::Zero(); |
| 195 | } else if (intensity_ == 1 && |
| 196 | time_since_update_ >= config_.pulsed.send_duration) { |
| 197 | intensity_ = 0; |
| 198 | time_since_update_ = TimeDelta::Zero(); |
| 199 | } |
| 200 | } |
| 201 | pending_size_ += TrafficRate() * delta; |
| 202 | if (pending_size_ > config_.min_packet_size) { |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 203 | target_->OnPacketReceived(EmulatedIpPacket( |
Artem Titov | 4cd433e | 2019-04-01 11:01:16 +0200 | [diff] [blame] | 204 | rtc::SocketAddress() /*from*/, receiver_address_, |
| 205 | rtc::CopyOnWriteBuffer(pending_size_.bytes()), at_time)); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 206 | pending_size_ = DataSize::Zero(); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | ColumnPrinter CrossTrafficSource::StatsPrinter() { |
| 211 | return ColumnPrinter::Lambda("cross_traffic_rate", |
| 212 | [this](rtc::SimpleStringBuilder& sb) { |
| 213 | sb.AppendFormat("%.0lf", |
| 214 | TrafficRate().bps() / 8.0); |
| 215 | }, |
| 216 | 32); |
| 217 | } |
| 218 | |
| 219 | } // namespace test |
| 220 | } // namespace webrtc |