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 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 15 | #include <memory> |
Sebastian Jansson | f52d3ed | 2020-02-28 16:22:09 +0100 | [diff] [blame] | 16 | #include "rtc_base/net_helper.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 17 | #include "rtc_base/numerics/safe_minmax.h" |
| 18 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | namespace test { |
| 21 | namespace { |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 22 | constexpr char kDummyTransportName[] = "dummy"; |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 23 | SimulatedNetwork::Config CreateSimulationConfig( |
| 24 | NetworkSimulationConfig config) { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 25 | SimulatedNetwork::Config sim_config; |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 26 | sim_config.link_capacity_kbps = config.bandwidth.kbps_or(0); |
| 27 | sim_config.loss_percent = config.loss_rate * 100; |
| 28 | sim_config.queue_delay_ms = config.delay.ms(); |
| 29 | sim_config.delay_standard_deviation_ms = config.delay_std_dev.ms(); |
Sebastian Jansson | 8c8feb9 | 2019-01-29 15:59:17 +0100 | [diff] [blame] | 30 | sim_config.packet_overhead = config.packet_overhead.bytes<int>(); |
Sebastian Jansson | 2b08e31 | 2019-02-25 10:24:46 +0100 | [diff] [blame] | 31 | sim_config.codel_active_queue_management = |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 32 | config.codel_active_queue_management; |
Sebastian Jansson | 24c678f | 2019-10-14 15:07:58 +0200 | [diff] [blame] | 33 | sim_config.queue_length_packets = |
| 34 | config.packet_queue_length_limit.value_or(0); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 35 | return sim_config; |
| 36 | } |
| 37 | } // namespace |
| 38 | |
Sebastian Jansson | a4c22b9 | 2019-04-15 21:10:00 +0200 | [diff] [blame] | 39 | SimulationNode::SimulationNode(NetworkSimulationConfig config, |
| 40 | SimulatedNetwork* behavior, |
| 41 | EmulatedNetworkNode* network_node) |
| 42 | : config_(config), simulation_(behavior), network_node_(network_node) {} |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 43 | |
Sebastian Jansson | a4c22b9 | 2019-04-15 21:10:00 +0200 | [diff] [blame] | 44 | std::unique_ptr<SimulatedNetwork> SimulationNode::CreateBehavior( |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 45 | NetworkSimulationConfig config) { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 46 | SimulatedNetwork::Config sim_config = CreateSimulationConfig(config); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 47 | return std::make_unique<SimulatedNetwork>(sim_config); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void SimulationNode::UpdateConfig( |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 51 | std::function<void(NetworkSimulationConfig*)> modifier) { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 52 | modifier(&config_); |
| 53 | SimulatedNetwork::Config sim_config = CreateSimulationConfig(config_); |
Sebastian Jansson | a4c22b9 | 2019-04-15 21:10:00 +0200 | [diff] [blame] | 54 | simulation_->SetConfig(sim_config); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void SimulationNode::PauseTransmissionUntil(Timestamp until) { |
Sebastian Jansson | a4c22b9 | 2019-04-15 21:10:00 +0200 | [diff] [blame] | 58 | simulation_->PauseTransmissionUntil(until.us()); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | ColumnPrinter SimulationNode::ConfigPrinter() const { |
Sebastian Jansson | ef86d14 | 2019-04-15 14:42:42 +0200 | [diff] [blame] | 62 | return ColumnPrinter::Lambda( |
| 63 | "propagation_delay capacity loss_rate", |
| 64 | [this](rtc::SimpleStringBuilder& sb) { |
| 65 | sb.AppendFormat("%.3lf %.0lf %.2lf", config_.delay.seconds<double>(), |
| 66 | config_.bandwidth.bps() / 8.0, config_.loss_rate); |
| 67 | }); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Sebastian Jansson | aa01f27 | 2019-01-30 11:28:59 +0100 | [diff] [blame] | 70 | NetworkNodeTransport::NetworkNodeTransport(Clock* sender_clock, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 71 | Call* sender_call) |
| 72 | : sender_clock_(sender_clock), sender_call_(sender_call) {} |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 73 | |
| 74 | NetworkNodeTransport::~NetworkNodeTransport() = default; |
| 75 | |
| 76 | bool NetworkNodeTransport::SendRtp(const uint8_t* packet, |
| 77 | size_t length, |
| 78 | const PacketOptions& options) { |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 79 | int64_t send_time_ms = sender_clock_->TimeInMilliseconds(); |
Sebastian Jansson | 156d11d | 2018-09-28 17:21:34 +0200 | [diff] [blame] | 80 | rtc::SentPacket sent_packet; |
| 81 | sent_packet.packet_id = options.packet_id; |
Sebastian Jansson | 0378997 | 2018-10-09 18:27:57 +0200 | [diff] [blame] | 82 | sent_packet.info.included_in_feedback = options.included_in_feedback; |
| 83 | sent_packet.info.included_in_allocation = options.included_in_allocation; |
Sebastian Jansson | 156d11d | 2018-09-28 17:21:34 +0200 | [diff] [blame] | 84 | sent_packet.send_time_ms = send_time_ms; |
| 85 | sent_packet.info.packet_size_bytes = length; |
| 86 | sent_packet.info.packet_type = rtc::PacketType::kData; |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 87 | sender_call_->OnSentPacket(sent_packet); |
Sebastian Jansson | 156d11d | 2018-09-28 17:21:34 +0200 | [diff] [blame] | 88 | |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 89 | rtc::CritScope crit(&crit_sect_); |
Sebastian Jansson | 77bd385 | 2020-01-17 13:05:54 +0100 | [diff] [blame] | 90 | if (!endpoint_) |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 91 | return false; |
Sebastian Jansson | c9f42ad | 2020-01-17 11:47:35 +0100 | [diff] [blame] | 92 | rtc::CopyOnWriteBuffer buffer(packet, length); |
Sebastian Jansson | 77bd385 | 2020-01-17 13:05:54 +0100 | [diff] [blame] | 93 | endpoint_->SendPacket(local_address_, remote_address_, buffer, |
| 94 | packet_overhead_.bytes()); |
Sebastian Jansson | f65309c | 2018-12-20 10:26:00 +0100 | [diff] [blame] | 95 | return true; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) { |
| 99 | rtc::CopyOnWriteBuffer buffer(packet, length); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 100 | rtc::CritScope crit(&crit_sect_); |
Sebastian Jansson | 77bd385 | 2020-01-17 13:05:54 +0100 | [diff] [blame] | 101 | if (!endpoint_) |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 102 | return false; |
Sebastian Jansson | 77bd385 | 2020-01-17 13:05:54 +0100 | [diff] [blame] | 103 | endpoint_->SendPacket(local_address_, remote_address_, buffer, |
| 104 | packet_overhead_.bytes()); |
Sebastian Jansson | f65309c | 2018-12-20 10:26:00 +0100 | [diff] [blame] | 105 | return true; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Sebastian Jansson | 77bd385 | 2020-01-17 13:05:54 +0100 | [diff] [blame] | 108 | void NetworkNodeTransport::Connect(EmulatedEndpoint* endpoint, |
| 109 | const rtc::SocketAddress& receiver_address, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 110 | DataSize packet_overhead) { |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 111 | rtc::NetworkRoute route; |
| 112 | route.connected = true; |
Sebastian Jansson | 77bd385 | 2020-01-17 13:05:54 +0100 | [diff] [blame] | 113 | // We assume that the address will be unique in the lower bytes. |
Jonas Oreland | 71fda36 | 2020-03-20 16:11:56 +0100 | [diff] [blame^] | 114 | route.local = rtc::RouteEndpoint::CreateWithNetworkId(static_cast<uint16_t>( |
| 115 | receiver_address.ipaddr().v4AddressAsHostOrderInteger())); |
| 116 | route.remote = rtc::RouteEndpoint::CreateWithNetworkId(static_cast<uint16_t>( |
| 117 | receiver_address.ipaddr().v4AddressAsHostOrderInteger())); |
Sebastian Jansson | f52d3ed | 2020-02-28 16:22:09 +0100 | [diff] [blame] | 118 | route.packet_overhead = packet_overhead.bytes() + |
| 119 | receiver_address.ipaddr().overhead() + |
| 120 | cricket::kUdpHeaderSize; |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 121 | { |
Sebastian Jansson | 77bd385 | 2020-01-17 13:05:54 +0100 | [diff] [blame] | 122 | // Only IPv4 address is supported. |
| 123 | RTC_CHECK_EQ(receiver_address.family(), AF_INET); |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 124 | rtc::CritScope crit(&crit_sect_); |
Sebastian Jansson | 77bd385 | 2020-01-17 13:05:54 +0100 | [diff] [blame] | 125 | endpoint_ = endpoint; |
| 126 | local_address_ = rtc::SocketAddress(endpoint_->GetPeerLocalAddress(), 0); |
| 127 | remote_address_ = receiver_address; |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 128 | packet_overhead_ = packet_overhead; |
| 129 | current_network_route_ = route; |
| 130 | } |
| 131 | |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 132 | sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged( |
Sebastian Jansson | 4124dab | 2019-04-01 14:33:53 +0200 | [diff] [blame] | 133 | kDummyTransportName, route); |
| 134 | } |
| 135 | |
| 136 | void NetworkNodeTransport::Disconnect() { |
| 137 | rtc::CritScope crit(&crit_sect_); |
| 138 | current_network_route_.connected = false; |
| 139 | sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged( |
| 140 | kDummyTransportName, current_network_route_); |
| 141 | current_network_route_ = {}; |
Sebastian Jansson | 77bd385 | 2020-01-17 13:05:54 +0100 | [diff] [blame] | 142 | endpoint_ = nullptr; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 145 | } // namespace test |
| 146 | } // namespace webrtc |