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