OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 // SignalWriteEvent if successful. | 521 // SignalWriteEvent if successful. |
522 server_->SendTcp(this); | 522 server_->SendTcp(this); |
523 } | 523 } |
524 } | 524 } |
525 | 525 |
526 VirtualSocketServer::VirtualSocketServer(SocketServer* ss) | 526 VirtualSocketServer::VirtualSocketServer(SocketServer* ss) |
527 : server_(ss), | 527 : server_(ss), |
528 server_owned_(false), | 528 server_owned_(false), |
529 msg_queue_(NULL), | 529 msg_queue_(NULL), |
530 stop_on_idle_(false), | 530 stop_on_idle_(false), |
531 network_delay_(0), | |
532 next_ipv4_(kInitialNextIPv4), | 531 next_ipv4_(kInitialNextIPv4), |
533 next_ipv6_(kInitialNextIPv6), | 532 next_ipv6_(kInitialNextIPv6), |
534 next_port_(kFirstEphemeralPort), | 533 next_port_(kFirstEphemeralPort), |
535 bindings_(new AddressMap()), | 534 bindings_(new AddressMap()), |
536 connections_(new ConnectionMap()), | 535 connections_(new ConnectionMap()), |
537 bandwidth_(0), | 536 bandwidth_(0), |
538 network_capacity_(kDefaultNetworkCapacity), | 537 network_capacity_(kDefaultNetworkCapacity), |
539 send_buffer_capacity_(kDefaultTcpBufferSize), | 538 send_buffer_capacity_(kDefaultTcpBufferSize), |
540 recv_buffer_capacity_(kDefaultTcpBufferSize), | 539 recv_buffer_capacity_(kDefaultTcpBufferSize), |
541 delay_mean_(0), | 540 delay_mean_(0), |
542 delay_stddev_(0), | 541 delay_stddev_(0), |
543 delay_samples_(NUM_SAMPLES), | 542 delay_samples_(NUM_SAMPLES), |
544 delay_dist_(NULL), | |
545 drop_prob_(0.0) { | 543 drop_prob_(0.0) { |
546 if (!server_) { | 544 if (!server_) { |
547 server_ = new PhysicalSocketServer(); | 545 server_ = new PhysicalSocketServer(); |
548 server_owned_ = true; | 546 server_owned_ = true; |
549 } | 547 } |
550 UpdateDelayDistribution(); | 548 UpdateDelayDistribution(); |
551 } | 549 } |
552 | 550 |
553 VirtualSocketServer::~VirtualSocketServer() { | 551 VirtualSocketServer::~VirtualSocketServer() { |
554 delete bindings_; | 552 delete bindings_; |
555 delete connections_; | 553 delete connections_; |
556 delete delay_dist_; | |
557 if (server_owned_) { | 554 if (server_owned_) { |
558 delete server_; | 555 delete server_; |
559 } | 556 } |
560 } | 557 } |
561 | 558 |
562 IPAddress VirtualSocketServer::GetNextIP(int family) { | 559 IPAddress VirtualSocketServer::GetNextIP(int family) { |
563 if (family == AF_INET) { | 560 if (family == AF_INET) { |
564 IPAddress next_ip(next_ipv4_); | 561 IPAddress next_ip(next_ipv4_); |
565 next_ipv4_.s_addr = | 562 next_ipv4_.s_addr = |
566 HostToNetwork32(NetworkToHost32(next_ipv4_.s_addr) + 1); | 563 HostToNetwork32(NetworkToHost32(next_ipv4_.s_addr) + 1); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 connections_->erase(address_pair); | 771 connections_->erase(address_pair); |
775 } | 772 } |
776 | 773 |
777 static double Random() { | 774 static double Random() { |
778 return static_cast<double>(rand()) / RAND_MAX; | 775 return static_cast<double>(rand()) / RAND_MAX; |
779 } | 776 } |
780 | 777 |
781 int VirtualSocketServer::Connect(VirtualSocket* socket, | 778 int VirtualSocketServer::Connect(VirtualSocket* socket, |
782 const SocketAddress& remote_addr, | 779 const SocketAddress& remote_addr, |
783 bool use_delay) { | 780 bool use_delay) { |
784 uint32_t delay = use_delay ? GetRandomTransitDelay() : 0; | 781 uint32_t delay = use_delay ? GetTransitDelay(socket) : 0; |
785 VirtualSocket* remote = LookupBinding(remote_addr); | 782 VirtualSocket* remote = LookupBinding(remote_addr); |
786 if (!CanInteractWith(socket, remote)) { | 783 if (!CanInteractWith(socket, remote)) { |
787 LOG(LS_INFO) << "Address family mismatch between " | 784 LOG(LS_INFO) << "Address family mismatch between " |
788 << socket->GetLocalAddress() << " and " << remote_addr; | 785 << socket->GetLocalAddress() << " and " << remote_addr; |
789 return -1; | 786 return -1; |
790 } | 787 } |
791 if (remote != NULL) { | 788 if (remote != NULL) { |
792 SocketAddress addr = socket->GetLocalAddress(); | 789 SocketAddress addr = socket->GetLocalAddress(); |
793 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, remote, MSG_ID_CONNECT, | 790 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, remote, MSG_ID_CONNECT, |
794 new MessageAddress(addr)); | 791 new MessageAddress(addr)); |
795 } else { | 792 } else { |
796 LOG(LS_INFO) << "No one listening at " << remote_addr; | 793 LOG(LS_INFO) << "No one listening at " << remote_addr; |
797 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); | 794 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
798 } | 795 } |
799 return 0; | 796 return 0; |
800 } | 797 } |
801 | 798 |
802 bool VirtualSocketServer::Disconnect(VirtualSocket* socket) { | 799 bool VirtualSocketServer::Disconnect(VirtualSocket* socket) { |
803 if (socket) { | 800 if (socket) { |
804 // If we simulate packets being delayed, we should simulate the | 801 // If we simulate packets being delayed, we should simulate the |
805 // equivalent of a FIN being delayed as well. | 802 // equivalent of a FIN being delayed as well. |
806 uint32_t delay = GetRandomTransitDelay(); | 803 uint32_t delay = GetTransitDelay(socket); |
807 // Remove the mapping. | 804 // Remove the mapping. |
808 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); | 805 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
809 return true; | 806 return true; |
810 } | 807 } |
811 return false; | 808 return false; |
812 } | 809 } |
813 | 810 |
814 int VirtualSocketServer::SendUdp(VirtualSocket* socket, | 811 int VirtualSocketServer::SendUdp(VirtualSocket* socket, |
815 const char* data, size_t data_size, | 812 const char* data, size_t data_size, |
816 const SocketAddress& remote_addr) { | 813 const SocketAddress& remote_addr) { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
940 bool ordered) { | 937 bool ordered) { |
941 VirtualSocket::NetworkEntry entry; | 938 VirtualSocket::NetworkEntry entry; |
942 entry.size = data_size + header_size; | 939 entry.size = data_size + header_size; |
943 | 940 |
944 sender->network_size_ += entry.size; | 941 sender->network_size_ += entry.size; |
945 uint32_t send_delay = SendDelay(static_cast<uint32_t>(sender->network_size_)); | 942 uint32_t send_delay = SendDelay(static_cast<uint32_t>(sender->network_size_)); |
946 entry.done_time = cur_time + send_delay; | 943 entry.done_time = cur_time + send_delay; |
947 sender->network_.push_back(entry); | 944 sender->network_.push_back(entry); |
948 | 945 |
949 // Find the delay for crossing the many virtual hops of the network. | 946 // Find the delay for crossing the many virtual hops of the network. |
950 uint32_t transit_delay = GetRandomTransitDelay(); | 947 uint32_t transit_delay = GetTransitDelay(sender); |
951 | 948 |
952 // When the incoming packet is from a binding of the any address, translate it | 949 // When the incoming packet is from a binding of the any address, translate it |
953 // to the default route here such that the recipient will see the default | 950 // to the default route here such that the recipient will see the default |
954 // route. | 951 // route. |
955 SocketAddress sender_addr = sender->local_addr_; | 952 SocketAddress sender_addr = sender->local_addr_; |
956 IPAddress default_ip = GetDefaultRoute(sender_addr.ipaddr().family()); | 953 IPAddress default_ip = GetDefaultRoute(sender_addr.ipaddr().family()); |
957 if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) { | 954 if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) { |
958 sender_addr.SetIP(default_ip); | 955 sender_addr.SetIP(default_ip); |
959 } | 956 } |
960 | 957 |
961 // Post the packet as a message to be delivered (on our own thread) | 958 // Post the packet as a message to be delivered (on our own thread) |
962 Packet* p = new Packet(data, data_size, sender_addr); | 959 Packet* p = new Packet(data, data_size, sender_addr); |
963 | 960 |
964 int64_t ts = TimeAfter(send_delay + transit_delay); | 961 int64_t ts = TimeAfter(send_delay + transit_delay); |
965 if (ordered) { | 962 if (ordered) { |
966 // Ensure that new packets arrive after previous ones | 963 // Ensure that new packets arrive after previous ones |
967 // TODO: consider ordering on a per-socket basis, since this | 964 ts = std::max(ts, sender->last_delivery_time_); |
968 // introduces artificial delay. | 965 // A socket should not have both ordered and unordered delivery, so its last |
969 ts = std::max(ts, network_delay_); | 966 // delivery time only needs to be updated when it has ordered delivery. |
| 967 sender->last_delivery_time_ = ts; |
970 } | 968 } |
971 msg_queue_->PostAt(RTC_FROM_HERE, ts, recipient, MSG_ID_PACKET, p); | 969 msg_queue_->PostAt(RTC_FROM_HERE, ts, recipient, MSG_ID_PACKET, p); |
972 network_delay_ = std::max(ts, network_delay_); | |
973 } | 970 } |
974 | 971 |
975 void VirtualSocketServer::PurgeNetworkPackets(VirtualSocket* socket, | 972 void VirtualSocketServer::PurgeNetworkPackets(VirtualSocket* socket, |
976 int64_t cur_time) { | 973 int64_t cur_time) { |
977 while (!socket->network_.empty() && | 974 while (!socket->network_.empty() && |
978 (socket->network_.front().done_time <= cur_time)) { | 975 (socket->network_.front().done_time <= cur_time)) { |
979 RTC_DCHECK(socket->network_size_ >= socket->network_.front().size); | 976 RTC_DCHECK(socket->network_size_ >= socket->network_.front().size); |
980 socket->network_size_ -= socket->network_.front().size; | 977 socket->network_size_ -= socket->network_.front().size; |
981 socket->network_.pop_front(); | 978 socket->network_.pop_front(); |
982 } | 979 } |
(...skipping 26 matching lines...) Expand all Loading... |
1009 } | 1006 } |
1010 } | 1007 } |
1011 #endif // <unused> | 1008 #endif // <unused> |
1012 | 1009 |
1013 void VirtualSocketServer::UpdateDelayDistribution() { | 1010 void VirtualSocketServer::UpdateDelayDistribution() { |
1014 Function* dist = CreateDistribution(delay_mean_, delay_stddev_, | 1011 Function* dist = CreateDistribution(delay_mean_, delay_stddev_, |
1015 delay_samples_); | 1012 delay_samples_); |
1016 // We take a lock just to make sure we don't leak memory. | 1013 // We take a lock just to make sure we don't leak memory. |
1017 { | 1014 { |
1018 CritScope cs(&delay_crit_); | 1015 CritScope cs(&delay_crit_); |
1019 delete delay_dist_; | 1016 delay_dist_.reset(dist); |
1020 delay_dist_ = dist; | |
1021 } | 1017 } |
1022 } | 1018 } |
1023 | 1019 |
1024 static double PI = 4 * atan(1.0); | 1020 static double PI = 4 * atan(1.0); |
1025 | 1021 |
1026 static double Normal(double x, double mean, double stddev) { | 1022 static double Normal(double x, double mean, double stddev) { |
1027 double a = (x - mean) * (x - mean) / (2 * stddev * stddev); | 1023 double a = (x - mean) * (x - mean) / (2 * stddev * stddev); |
1028 return exp(-a) / (stddev * sqrt(2 * PI)); | 1024 return exp(-a) / (stddev * sqrt(2 * PI)); |
1029 } | 1025 } |
1030 | 1026 |
(...skipping 22 matching lines...) Expand all Loading... |
1053 | 1049 |
1054 for (uint32_t i = 0; i < samples; i++) { | 1050 for (uint32_t i = 0; i < samples; i++) { |
1055 double x = start + (end - start) * i / (samples - 1); | 1051 double x = start + (end - start) * i / (samples - 1); |
1056 double y = Normal(x, mean, stddev); | 1052 double y = Normal(x, mean, stddev); |
1057 f->push_back(Point(x, y)); | 1053 f->push_back(Point(x, y)); |
1058 } | 1054 } |
1059 } | 1055 } |
1060 return Resample(Invert(Accumulate(f)), 0, 1, samples); | 1056 return Resample(Invert(Accumulate(f)), 0, 1, samples); |
1061 } | 1057 } |
1062 | 1058 |
1063 uint32_t VirtualSocketServer::GetRandomTransitDelay() { | 1059 uint32_t VirtualSocketServer::GetTransitDelay(Socket* socket) { |
| 1060 // Use the delay based on the address if it is set. |
| 1061 auto iter = delay_by_ip_.find(socket->GetLocalAddress().ipaddr()); |
| 1062 if (iter != delay_by_ip_.end()) { |
| 1063 return static_cast<uint32_t>(iter->second); |
| 1064 } |
| 1065 // Otherwise, use the delay from the distribution distribution. |
1064 size_t index = rand() % delay_dist_->size(); | 1066 size_t index = rand() % delay_dist_->size(); |
1065 double delay = (*delay_dist_)[index].second; | 1067 double delay = (*delay_dist_)[index].second; |
1066 //LOG_F(LS_INFO) << "random[" << index << "] = " << delay; | 1068 // LOG_F(LS_INFO) << "random[" << index << "] = " << delay; |
1067 return static_cast<uint32_t>(delay); | 1069 return static_cast<uint32_t>(delay); |
1068 } | 1070 } |
1069 | 1071 |
1070 struct FunctionDomainCmp { | 1072 struct FunctionDomainCmp { |
1071 bool operator()(const VirtualSocketServer::Point& p1, | 1073 bool operator()(const VirtualSocketServer::Point& p1, |
1072 const VirtualSocketServer::Point& p2) { | 1074 const VirtualSocketServer::Point& p2) { |
1073 return p1.first < p2.first; | 1075 return p1.first < p2.first; |
1074 } | 1076 } |
1075 bool operator()(double v1, const VirtualSocketServer::Point& p2) { | 1077 bool operator()(double v1, const VirtualSocketServer::Point& p2) { |
1076 return v1 < p2.first; | 1078 return v1 < p2.first; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1189 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { | 1191 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { |
1190 RTC_DCHECK(!IPIsAny(from_addr)); | 1192 RTC_DCHECK(!IPIsAny(from_addr)); |
1191 if (from_addr.family() == AF_INET) { | 1193 if (from_addr.family() == AF_INET) { |
1192 default_route_v4_ = from_addr; | 1194 default_route_v4_ = from_addr; |
1193 } else if (from_addr.family() == AF_INET6) { | 1195 } else if (from_addr.family() == AF_INET6) { |
1194 default_route_v6_ = from_addr; | 1196 default_route_v6_ = from_addr; |
1195 } | 1197 } |
1196 } | 1198 } |
1197 | 1199 |
1198 } // namespace rtc | 1200 } // namespace rtc |
OLD | NEW |