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 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 next_port_(kFirstEphemeralPort), | 534 next_port_(kFirstEphemeralPort), |
535 bindings_(new AddressMap()), | 535 bindings_(new AddressMap()), |
536 connections_(new ConnectionMap()), | 536 connections_(new ConnectionMap()), |
537 bandwidth_(0), | 537 bandwidth_(0), |
538 network_capacity_(kDefaultNetworkCapacity), | 538 network_capacity_(kDefaultNetworkCapacity), |
539 send_buffer_capacity_(kDefaultTcpBufferSize), | 539 send_buffer_capacity_(kDefaultTcpBufferSize), |
540 recv_buffer_capacity_(kDefaultTcpBufferSize), | 540 recv_buffer_capacity_(kDefaultTcpBufferSize), |
541 delay_mean_(0), | 541 delay_mean_(0), |
542 delay_stddev_(0), | 542 delay_stddev_(0), |
543 delay_samples_(NUM_SAMPLES), | 543 delay_samples_(NUM_SAMPLES), |
544 delay_dist_(NULL), | |
545 drop_prob_(0.0) { | 544 drop_prob_(0.0) { |
546 if (!server_) { | 545 if (!server_) { |
547 server_ = new PhysicalSocketServer(); | 546 server_ = new PhysicalSocketServer(); |
548 server_owned_ = true; | 547 server_owned_ = true; |
549 } | 548 } |
550 UpdateDelayDistribution(); | 549 UpdateDelayDistribution(); |
551 } | 550 } |
552 | 551 |
553 VirtualSocketServer::~VirtualSocketServer() { | 552 VirtualSocketServer::~VirtualSocketServer() { |
554 delete bindings_; | 553 delete bindings_; |
555 delete connections_; | 554 delete connections_; |
556 delete delay_dist_; | |
557 if (server_owned_) { | 555 if (server_owned_) { |
558 delete server_; | 556 delete server_; |
559 } | 557 } |
560 } | 558 } |
561 | 559 |
562 IPAddress VirtualSocketServer::GetNextIP(int family) { | 560 IPAddress VirtualSocketServer::GetNextIP(int family) { |
563 if (family == AF_INET) { | 561 if (family == AF_INET) { |
564 IPAddress next_ip(next_ipv4_); | 562 IPAddress next_ip(next_ipv4_); |
565 next_ipv4_.s_addr = | 563 next_ipv4_.s_addr = |
566 HostToNetwork32(NetworkToHost32(next_ipv4_.s_addr) + 1); | 564 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); | 772 connections_->erase(address_pair); |
775 } | 773 } |
776 | 774 |
777 static double Random() { | 775 static double Random() { |
778 return static_cast<double>(rand()) / RAND_MAX; | 776 return static_cast<double>(rand()) / RAND_MAX; |
779 } | 777 } |
780 | 778 |
781 int VirtualSocketServer::Connect(VirtualSocket* socket, | 779 int VirtualSocketServer::Connect(VirtualSocket* socket, |
782 const SocketAddress& remote_addr, | 780 const SocketAddress& remote_addr, |
783 bool use_delay) { | 781 bool use_delay) { |
784 uint32_t delay = use_delay ? GetRandomTransitDelay() : 0; | 782 uint32_t delay = use_delay ? GetRandomTransitDelay(socket) : 0; |
785 VirtualSocket* remote = LookupBinding(remote_addr); | 783 VirtualSocket* remote = LookupBinding(remote_addr); |
786 if (!CanInteractWith(socket, remote)) { | 784 if (!CanInteractWith(socket, remote)) { |
787 LOG(LS_INFO) << "Address family mismatch between " | 785 LOG(LS_INFO) << "Address family mismatch between " |
788 << socket->GetLocalAddress() << " and " << remote_addr; | 786 << socket->GetLocalAddress() << " and " << remote_addr; |
789 return -1; | 787 return -1; |
790 } | 788 } |
791 if (remote != NULL) { | 789 if (remote != NULL) { |
792 SocketAddress addr = socket->GetLocalAddress(); | 790 SocketAddress addr = socket->GetLocalAddress(); |
793 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, remote, MSG_ID_CONNECT, | 791 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, remote, MSG_ID_CONNECT, |
794 new MessageAddress(addr)); | 792 new MessageAddress(addr)); |
795 } else { | 793 } else { |
796 LOG(LS_INFO) << "No one listening at " << remote_addr; | 794 LOG(LS_INFO) << "No one listening at " << remote_addr; |
797 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); | 795 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
798 } | 796 } |
799 return 0; | 797 return 0; |
800 } | 798 } |
801 | 799 |
802 bool VirtualSocketServer::Disconnect(VirtualSocket* socket) { | 800 bool VirtualSocketServer::Disconnect(VirtualSocket* socket) { |
803 if (socket) { | 801 if (socket) { |
804 // If we simulate packets being delayed, we should simulate the | 802 // If we simulate packets being delayed, we should simulate the |
805 // equivalent of a FIN being delayed as well. | 803 // equivalent of a FIN being delayed as well. |
806 uint32_t delay = GetRandomTransitDelay(); | 804 uint32_t delay = GetRandomTransitDelay(socket); |
807 // Remove the mapping. | 805 // Remove the mapping. |
808 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); | 806 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
809 return true; | 807 return true; |
810 } | 808 } |
811 return false; | 809 return false; |
812 } | 810 } |
813 | 811 |
814 int VirtualSocketServer::SendUdp(VirtualSocket* socket, | 812 int VirtualSocketServer::SendUdp(VirtualSocket* socket, |
815 const char* data, size_t data_size, | 813 const char* data, size_t data_size, |
816 const SocketAddress& remote_addr) { | 814 const SocketAddress& remote_addr) { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
940 bool ordered) { | 938 bool ordered) { |
941 VirtualSocket::NetworkEntry entry; | 939 VirtualSocket::NetworkEntry entry; |
942 entry.size = data_size + header_size; | 940 entry.size = data_size + header_size; |
943 | 941 |
944 sender->network_size_ += entry.size; | 942 sender->network_size_ += entry.size; |
945 uint32_t send_delay = SendDelay(static_cast<uint32_t>(sender->network_size_)); | 943 uint32_t send_delay = SendDelay(static_cast<uint32_t>(sender->network_size_)); |
946 entry.done_time = cur_time + send_delay; | 944 entry.done_time = cur_time + send_delay; |
947 sender->network_.push_back(entry); | 945 sender->network_.push_back(entry); |
948 | 946 |
949 // Find the delay for crossing the many virtual hops of the network. | 947 // Find the delay for crossing the many virtual hops of the network. |
950 uint32_t transit_delay = GetRandomTransitDelay(); | 948 uint32_t transit_delay = GetRandomTransitDelay(sender); |
951 | 949 |
952 // When the incoming packet is from a binding of the any address, translate it | 950 // 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 | 951 // to the default route here such that the recipient will see the default |
954 // route. | 952 // route. |
955 SocketAddress sender_addr = sender->local_addr_; | 953 SocketAddress sender_addr = sender->local_addr_; |
956 IPAddress default_ip = GetDefaultRoute(sender_addr.ipaddr().family()); | 954 IPAddress default_ip = GetDefaultRoute(sender_addr.ipaddr().family()); |
957 if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) { | 955 if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) { |
958 sender_addr.SetIP(default_ip); | 956 sender_addr.SetIP(default_ip); |
959 } | 957 } |
960 | 958 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1009 } | 1007 } |
1010 } | 1008 } |
1011 #endif // <unused> | 1009 #endif // <unused> |
1012 | 1010 |
1013 void VirtualSocketServer::UpdateDelayDistribution() { | 1011 void VirtualSocketServer::UpdateDelayDistribution() { |
1014 Function* dist = CreateDistribution(delay_mean_, delay_stddev_, | 1012 Function* dist = CreateDistribution(delay_mean_, delay_stddev_, |
1015 delay_samples_); | 1013 delay_samples_); |
1016 // We take a lock just to make sure we don't leak memory. | 1014 // We take a lock just to make sure we don't leak memory. |
1017 { | 1015 { |
1018 CritScope cs(&delay_crit_); | 1016 CritScope cs(&delay_crit_); |
1019 delete delay_dist_; | 1017 delay_dist_.reset(dist); |
1020 delay_dist_ = dist; | |
1021 } | 1018 } |
1022 } | 1019 } |
1023 | 1020 |
| 1021 void VirtualSocketServer::UpdateDelayDistributionForIPv6() { |
| 1022 Function* dist = |
| 1023 CreateDistribution(delay_mean_, delay_stddev_, delay_samples_); |
| 1024 // We take a lock just to make sure we don't leak memory. |
| 1025 { |
| 1026 CritScope cs(&delay_crit_); |
| 1027 delay_dist_ipv6_.reset(dist); |
| 1028 } |
| 1029 } |
| 1030 |
1024 static double PI = 4 * atan(1.0); | 1031 static double PI = 4 * atan(1.0); |
1025 | 1032 |
1026 static double Normal(double x, double mean, double stddev) { | 1033 static double Normal(double x, double mean, double stddev) { |
1027 double a = (x - mean) * (x - mean) / (2 * stddev * stddev); | 1034 double a = (x - mean) * (x - mean) / (2 * stddev * stddev); |
1028 return exp(-a) / (stddev * sqrt(2 * PI)); | 1035 return exp(-a) / (stddev * sqrt(2 * PI)); |
1029 } | 1036 } |
1030 | 1037 |
1031 #if 0 // static unused gives a warning | 1038 #if 0 // static unused gives a warning |
1032 static double Pareto(double x, double min, double k) { | 1039 static double Pareto(double x, double min, double k) { |
1033 if (x < min) | 1040 if (x < min) |
(...skipping 19 matching lines...) Expand all Loading... |
1053 | 1060 |
1054 for (uint32_t i = 0; i < samples; i++) { | 1061 for (uint32_t i = 0; i < samples; i++) { |
1055 double x = start + (end - start) * i / (samples - 1); | 1062 double x = start + (end - start) * i / (samples - 1); |
1056 double y = Normal(x, mean, stddev); | 1063 double y = Normal(x, mean, stddev); |
1057 f->push_back(Point(x, y)); | 1064 f->push_back(Point(x, y)); |
1058 } | 1065 } |
1059 } | 1066 } |
1060 return Resample(Invert(Accumulate(f)), 0, 1, samples); | 1067 return Resample(Invert(Accumulate(f)), 0, 1, samples); |
1061 } | 1068 } |
1062 | 1069 |
1063 uint32_t VirtualSocketServer::GetRandomTransitDelay() { | 1070 uint32_t VirtualSocketServer::GetRandomTransitDelay(Socket* socket) { |
1064 size_t index = rand() % delay_dist_->size(); | 1071 Function* delay_dist = delay_dist_.get(); |
1065 double delay = (*delay_dist_)[index].second; | 1072 if (socket->GetLocalAddress().family() == AF_INET6 && delay_dist_ipv6_) { |
1066 //LOG_F(LS_INFO) << "random[" << index << "] = " << delay; | 1073 delay_dist = delay_dist_ipv6_.get(); |
| 1074 } |
| 1075 |
| 1076 size_t index = rand() % delay_dist->size(); |
| 1077 double delay = (*delay_dist)[index].second; |
| 1078 // LOG_F(LS_INFO) << "random[" << index << "] = " << delay; |
1067 return static_cast<uint32_t>(delay); | 1079 return static_cast<uint32_t>(delay); |
1068 } | 1080 } |
1069 | 1081 |
1070 struct FunctionDomainCmp { | 1082 struct FunctionDomainCmp { |
1071 bool operator()(const VirtualSocketServer::Point& p1, | 1083 bool operator()(const VirtualSocketServer::Point& p1, |
1072 const VirtualSocketServer::Point& p2) { | 1084 const VirtualSocketServer::Point& p2) { |
1073 return p1.first < p2.first; | 1085 return p1.first < p2.first; |
1074 } | 1086 } |
1075 bool operator()(double v1, const VirtualSocketServer::Point& p2) { | 1087 bool operator()(double v1, const VirtualSocketServer::Point& p2) { |
1076 return v1 < p2.first; | 1088 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) { | 1201 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { |
1190 RTC_DCHECK(!IPIsAny(from_addr)); | 1202 RTC_DCHECK(!IPIsAny(from_addr)); |
1191 if (from_addr.family() == AF_INET) { | 1203 if (from_addr.family() == AF_INET) { |
1192 default_route_v4_ = from_addr; | 1204 default_route_v4_ = from_addr; |
1193 } else if (from_addr.family() == AF_INET6) { | 1205 } else if (from_addr.family() == AF_INET6) { |
1194 default_route_v6_ = from_addr; | 1206 default_route_v6_ = from_addr; |
1195 } | 1207 } |
1196 } | 1208 } |
1197 | 1209 |
1198 } // namespace rtc | 1210 } // namespace rtc |
OLD | NEW |