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