Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(357)

Side by Side Diff: webrtc/p2p/base/port.cc

Issue 2163403002: Prepare for ICE-renomination (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: . Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 736 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 } 747 }
748 uint32_t network_info = connection_->port()->Network()->id(); 748 uint32_t network_info = connection_->port()->Network()->id();
749 network_info = (network_info << 16) | connection_->port()->network_cost(); 749 network_info = (network_info << 16) | connection_->port()->network_cost();
750 request->AddAttribute( 750 request->AddAttribute(
751 new StunUInt32Attribute(STUN_ATTR_NETWORK_INFO, network_info)); 751 new StunUInt32Attribute(STUN_ATTR_NETWORK_INFO, network_info));
752 752
753 // Adding ICE_CONTROLLED or ICE_CONTROLLING attribute based on the role. 753 // Adding ICE_CONTROLLED or ICE_CONTROLLING attribute based on the role.
754 if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLING) { 754 if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLING) {
755 request->AddAttribute(new StunUInt64Attribute( 755 request->AddAttribute(new StunUInt64Attribute(
756 STUN_ATTR_ICE_CONTROLLING, connection_->port()->IceTiebreaker())); 756 STUN_ATTR_ICE_CONTROLLING, connection_->port()->IceTiebreaker()));
757 // Since we are trying aggressive nomination, sending USE-CANDIDATE 757 // We should have either USE_CANDIDATE attribute or ICE_NOMINATION_VALUE
758 // attribute in every ping. 758 // attribute but not both. That was enforced in p2ptransportchannel.
759 // If we are dealing with a ice-lite end point, nomination flag
760 // in Connection will be set to false by default. Once the connection
761 // becomes "best connection", nomination flag will be turned on.
762 if (connection_->use_candidate_attr()) { 759 if (connection_->use_candidate_attr()) {
763 request->AddAttribute(new StunByteStringAttribute( 760 request->AddAttribute(new StunByteStringAttribute(
764 STUN_ATTR_USE_CANDIDATE)); 761 STUN_ATTR_USE_CANDIDATE));
765 } 762 }
763 if (connection_->nominating_value() >
764 connection_->acknowledged_nominating_value()) {
pthatcher1 2016/07/28 22:57:26 I think it would be more robust if it were: conne
honghaiz3 2016/08/03 04:46:56 Done.
765 request->AddAttribute(new StunUInt32Attribute(
766 STUN_ATTR_NOMINATION_VALUE, connection_->nominating_value()));
pthatcher1 2016/07/28 22:57:26 I'd prefer STUN_ATTR_NOMINATION.
honghaiz3 2016/08/03 04:46:56 Done.
767 }
766 } else if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLED) { 768 } else if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLED) {
767 request->AddAttribute(new StunUInt64Attribute( 769 request->AddAttribute(new StunUInt64Attribute(
768 STUN_ATTR_ICE_CONTROLLED, connection_->port()->IceTiebreaker())); 770 STUN_ATTR_ICE_CONTROLLED, connection_->port()->IceTiebreaker()));
769 } else { 771 } else {
770 ASSERT(false); 772 ASSERT(false);
771 } 773 }
772 774
773 // Adding PRIORITY Attribute. 775 // Adding PRIORITY Attribute.
774 // Changing the type preference to Peer Reflexive and local preference 776 // Changing the type preference to Peer Reflexive and local preference
775 // and component id information is unchanged from the original priority. 777 // and component id information is unchanged from the original priority.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
818 // 820 //
819 // Connection 821 // Connection
820 // 822 //
821 823
822 Connection::Connection(Port* port, 824 Connection::Connection(Port* port,
823 size_t index, 825 size_t index,
824 const Candidate& remote_candidate) 826 const Candidate& remote_candidate)
825 : port_(port), 827 : port_(port),
826 local_candidate_index_(index), 828 local_candidate_index_(index),
827 remote_candidate_(remote_candidate), 829 remote_candidate_(remote_candidate),
830 recv_rate_tracker_(100, 10u),
831 send_rate_tracker_(100, 10u),
828 write_state_(STATE_WRITE_INIT), 832 write_state_(STATE_WRITE_INIT),
829 receiving_(false), 833 receiving_(false),
830 connected_(true), 834 connected_(true),
831 pruned_(false), 835 pruned_(false),
832 use_candidate_attr_(false), 836 use_candidate_attr_(false),
833 nominated_(false),
834 remote_ice_mode_(ICEMODE_FULL), 837 remote_ice_mode_(ICEMODE_FULL),
835 requests_(port->thread()), 838 requests_(port->thread()),
836 rtt_(DEFAULT_RTT), 839 rtt_(DEFAULT_RTT),
837 last_ping_sent_(0), 840 last_ping_sent_(0),
838 last_ping_received_(0), 841 last_ping_received_(0),
839 last_data_received_(0), 842 last_data_received_(0),
840 last_ping_response_received_(0), 843 last_ping_response_received_(0),
841 recv_rate_tracker_(100, 10u),
842 send_rate_tracker_(100, 10u),
843 reported_(false), 844 reported_(false),
844 state_(STATE_WAITING), 845 state_(STATE_WAITING),
845 receiving_timeout_(WEAK_CONNECTION_RECEIVE_TIMEOUT), 846 receiving_timeout_(WEAK_CONNECTION_RECEIVE_TIMEOUT),
846 time_created_ms_(rtc::TimeMillis()) { 847 time_created_ms_(rtc::TimeMillis()) {
847 // All of our connections start in WAITING state. 848 // All of our connections start in WAITING state.
848 // TODO(mallinath) - Start connections from STATE_FROZEN. 849 // TODO(mallinath) - Start connections from STATE_FROZEN.
849 // Wire up to send stun packets 850 // Wire up to send stun packets
850 requests_.SignalSendPacket.connect(this, &Connection::OnSendStunPacket); 851 requests_.SignalSendPacket.connect(this, &Connection::OnSendStunPacket);
851 LOG_J(LS_INFO, this) << "Connection created"; 852 LOG_J(LS_INFO, this) << "Connection created";
852 } 853 }
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 1035
1035 // This is a validated stun request from remote peer. 1036 // This is a validated stun request from remote peer.
1036 port_->SendBindingResponse(msg, remote_addr); 1037 port_->SendBindingResponse(msg, remote_addr);
1037 1038
1038 // If it timed out on writing check, start up again 1039 // If it timed out on writing check, start up again
1039 if (!pruned_ && write_state_ == STATE_WRITE_TIMEOUT) { 1040 if (!pruned_ && write_state_ == STATE_WRITE_TIMEOUT) {
1040 set_write_state(STATE_WRITE_INIT); 1041 set_write_state(STATE_WRITE_INIT);
1041 } 1042 }
1042 1043
1043 if (port_->GetIceRole() == ICEROLE_CONTROLLED) { 1044 if (port_->GetIceRole() == ICEROLE_CONTROLLED) {
1044 const StunByteStringAttribute* use_candidate_attr = 1045 const StunUInt32Attribute* nomination_value_attr =
1045 msg->GetByteString(STUN_ATTR_USE_CANDIDATE); 1046 msg->GetUInt32(STUN_ATTR_NOMINATION_VALUE);
1046 if (use_candidate_attr) { 1047 int nomination_value = 0;
1047 set_nominated(true); 1048 if (nomination_value_attr) {
1049 nomination_value = static_cast<int>(nomination_value_attr->value());
1050 if (nomination_value < 0) {
1051 LOG(LS_ERROR) << "Negative nomination value: " << nomination_value;
pthatcher1 2016/07/28 22:57:26 Should we use a uint32_t instead of an int? It ca
honghaiz3 2016/08/03 04:46:56 I think the code style says we should not use uint
pthatcher1 2016/08/03 22:13:25 It's not just because it can't be negative. What'
honghaiz3 2016/08/03 23:39:51 Done, although I don't think it should go to the n
1052 }
1053 } else {
1054 const StunByteStringAttribute* use_candidate_attr =
1055 msg->GetByteString(STUN_ATTR_USE_CANDIDATE);
1056 if (use_candidate_attr) {
1057 nomination_value = 1;
1058 }
1059 }
1060 if (nomination_value > 0) {
1061 set_nominated_value(nomination_value);
1048 SignalNominated(this); 1062 SignalNominated(this);
1049 } 1063 }
1050 } 1064 }
1051 // Set the remote cost if the network_info attribute is available. 1065 // Set the remote cost if the network_info attribute is available.
1052 // Note: If packets are re-ordered, we may get incorrect network cost 1066 // Note: If packets are re-ordered, we may get incorrect network cost
1053 // temporarily, but it should get the correct value shortly after that. 1067 // temporarily, but it should get the correct value shortly after that.
1054 const StunUInt32Attribute* network_attr = 1068 const StunUInt32Attribute* network_attr =
1055 msg->GetUInt32(STUN_ATTR_NETWORK_INFO); 1069 msg->GetUInt32(STUN_ATTR_NETWORK_INFO);
1056 if (network_attr) { 1070 if (network_attr) {
1057 uint32_t network_info = network_attr->value(); 1071 uint32_t network_info = network_attr->value();
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1171 // Update the receiving state. 1185 // Update the receiving state.
1172 UpdateReceiving(now); 1186 UpdateReceiving(now);
1173 if (dead(now)) { 1187 if (dead(now)) {
1174 Destroy(); 1188 Destroy();
1175 } 1189 }
1176 } 1190 }
1177 1191
1178 void Connection::Ping(int64_t now) { 1192 void Connection::Ping(int64_t now) {
1179 last_ping_sent_ = now; 1193 last_ping_sent_ = now;
1180 ConnectionRequest *req = new ConnectionRequest(this); 1194 ConnectionRequest *req = new ConnectionRequest(this);
1181 pings_since_last_response_.push_back(SentPing(req->id(), now)); 1195 pings_since_last_response_.push_back(
1196 SentPing(req->id(), now, nominating_value_));
1182 LOG_J(LS_VERBOSE, this) << "Sending STUN ping " 1197 LOG_J(LS_VERBOSE, this) << "Sending STUN ping "
1183 << ", id=" << rtc::hex_encode(req->id()); 1198 << ", id=" << rtc::hex_encode(req->id())
1199 << ", nominating_value=" << nominating_value_;
1184 requests_.Send(req); 1200 requests_.Send(req);
1185 state_ = STATE_INPROGRESS; 1201 state_ = STATE_INPROGRESS;
1186 num_pings_sent_++; 1202 num_pings_sent_++;
1187 } 1203 }
1188 1204
1189 void Connection::ReceivedPing() { 1205 void Connection::ReceivedPing() {
1190 last_ping_received_ = rtc::TimeMillis(); 1206 last_ping_received_ = rtc::TimeMillis();
1191 UpdateReceiving(last_ping_received_); 1207 UpdateReceiving(last_ping_received_);
1192 } 1208 }
1193 1209
1194 void Connection::ReceivedPingResponse(int rtt) { 1210 void Connection::ReceivedPingResponse(int rtt, const std::string& request_id) {
1195 // We've already validated that this is a STUN binding response with 1211 // We've already validated that this is a STUN binding response with
1196 // the correct local and remote username for this connection. 1212 // the correct local and remote username for this connection.
1197 // So if we're not already, become writable. We may be bringing a pruned 1213 // So if we're not already, become writable. We may be bringing a pruned
1198 // connection back to life, but if we don't really want it, we can always 1214 // connection back to life, but if we don't really want it, we can always
1199 // prune it again. 1215 // prune it again.
1216 auto iter = std::find_if(
1217 pings_since_last_response_.begin(), pings_since_last_response_.end(),
1218 [request_id](const SentPing& ping) { return ping.id == request_id; });
1219 if (iter != pings_since_last_response_.end() &&
1220 iter->nomination_value > acknowledged_nominating_value_) {
1221 acknowledged_nominating_value_ = iter->nomination_value;
1222 }
1223
1224 pings_since_last_response_.clear();
1200 last_ping_response_received_ = rtc::TimeMillis(); 1225 last_ping_response_received_ = rtc::TimeMillis();
1201 UpdateReceiving(last_ping_response_received_); 1226 UpdateReceiving(last_ping_response_received_);
1202 set_write_state(STATE_WRITABLE); 1227 set_write_state(STATE_WRITABLE);
1203 set_state(STATE_SUCCEEDED); 1228 set_state(STATE_SUCCEEDED);
1204 pings_since_last_response_.clear();
1205 rtt_samples_++; 1229 rtt_samples_++;
1206 rtt_ = (RTT_RATIO * rtt_ + rtt) / (RTT_RATIO + 1); 1230 rtt_ = (RTT_RATIO * rtt_ + rtt) / (RTT_RATIO + 1);
1207 } 1231 }
1208 1232
1209 bool Connection::dead(int64_t now) const { 1233 bool Connection::dead(int64_t now) const {
1210 if (last_received() > 0) { 1234 if (last_received() > 0) {
1211 // If it has ever received anything, we keep it alive until it hasn't 1235 // If it has ever received anything, we keep it alive until it hasn't
1212 // received anything for DEAD_CONNECTION_RECEIVE_TIMEOUT. This covers the 1236 // received anything for DEAD_CONNECTION_RECEIVE_TIMEOUT. This covers the
1213 // normal case of a successfully used connection that stops working. This 1237 // normal case of a successfully used connection that stops working. This
1214 // also allows a remote peer to continue pinging over a locally inactive 1238 // also allows a remote peer to continue pinging over a locally inactive
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1267 }; 1291 };
1268 const std::string ICESTATE[4] = { 1292 const std::string ICESTATE[4] = {
1269 "W", // STATE_WAITING 1293 "W", // STATE_WAITING
1270 "I", // STATE_INPROGRESS 1294 "I", // STATE_INPROGRESS
1271 "S", // STATE_SUCCEEDED 1295 "S", // STATE_SUCCEEDED
1272 "F" // STATE_FAILED 1296 "F" // STATE_FAILED
1273 }; 1297 };
1274 const Candidate& local = local_candidate(); 1298 const Candidate& local = local_candidate();
1275 const Candidate& remote = remote_candidate(); 1299 const Candidate& remote = remote_candidate();
1276 std::stringstream ss; 1300 std::stringstream ss;
1277 ss << "Conn[" << ToDebugId() 1301 ss << "Conn[" << ToDebugId() << ":" << port_->content_name() << ":"
1278 << ":" << port_->content_name() 1302 << local.id() << ":" << local.component() << ":" << local.generation()
1279 << ":" << local.id() << ":" << local.component() 1303 << ":" << local.type() << ":" << local.protocol() << ":"
1280 << ":" << local.generation() 1304 << local.address().ToSensitiveString() << "->" << remote.id() << ":"
1281 << ":" << local.type() << ":" << local.protocol() 1305 << remote.component() << ":" << remote.priority() << ":" << remote.type()
1282 << ":" << local.address().ToSensitiveString() 1306 << ":" << remote.protocol() << ":" << remote.address().ToSensitiveString()
1283 << "->" << remote.id() << ":" << remote.component() 1307 << "|" << CONNECT_STATE_ABBREV[connected()]
1284 << ":" << remote.priority() 1308 << RECEIVE_STATE_ABBREV[receiving()] << WRITE_STATE_ABBREV[write_state()]
1285 << ":" << remote.type() << ":" 1309 << ICESTATE[state()] << "|" << nominated_value() << "|" << priority()
1286 << remote.protocol() << ":" << remote.address().ToSensitiveString() << "|" 1310 << "|";
1287 << CONNECT_STATE_ABBREV[connected()]
1288 << RECEIVE_STATE_ABBREV[receiving()]
1289 << WRITE_STATE_ABBREV[write_state()]
1290 << ICESTATE[state()] << "|"
1291 << priority() << "|";
1292 if (rtt_ < DEFAULT_RTT) { 1311 if (rtt_ < DEFAULT_RTT) {
1293 ss << rtt_ << "]"; 1312 ss << rtt_ << "]";
1294 } else { 1313 } else {
1295 ss << "-]"; 1314 ss << "-]";
1296 } 1315 }
1297 return ss.str(); 1316 return ss.str();
1298 } 1317 }
1299 1318
1300 std::string Connection::ToSensitiveString() const { 1319 std::string Connection::ToSensitiveString() const {
1301 return ToString(); 1320 return ToString();
1302 } 1321 }
1303 1322
1304 void Connection::OnConnectionRequestResponse(ConnectionRequest* request, 1323 void Connection::OnConnectionRequestResponse(ConnectionRequest* request,
1305 StunMessage* response) { 1324 StunMessage* response) {
1306 // Log at LS_INFO if we receive a ping response on an unwritable 1325 // Log at LS_INFO if we receive a ping response on an unwritable
1307 // connection. 1326 // connection.
1308 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; 1327 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE;
1309 1328
1310 int rtt = request->Elapsed(); 1329 int rtt = request->Elapsed();
1311 1330
1312 ReceivedPingResponse(rtt);
1313
1314 if (LOG_CHECK_LEVEL_V(sev)) { 1331 if (LOG_CHECK_LEVEL_V(sev)) {
1315 bool use_candidate = (
1316 response->GetByteString(STUN_ATTR_USE_CANDIDATE) != nullptr);
1317 std::string pings; 1332 std::string pings;
1318 PrintPingsSinceLastResponse(&pings, 5); 1333 PrintPingsSinceLastResponse(&pings, 5);
1319 LOG_JV(sev, this) << "Received STUN ping response" 1334 LOG_JV(sev, this) << "Received STUN ping response"
1320 << ", id=" << rtc::hex_encode(request->id()) 1335 << ", id=" << rtc::hex_encode(request->id())
1321 << ", code=0" // Makes logging easier to parse. 1336 << ", code=0" // Makes logging easier to parse.
1322 << ", rtt=" << rtt 1337 << ", rtt=" << rtt
1323 << ", use_candidate=" << use_candidate
1324 << ", pings_since_last_response=" << pings; 1338 << ", pings_since_last_response=" << pings;
1325 } 1339 }
1340 ReceivedPingResponse(rtt, request->id());
1326 1341
1327 stats_.recv_ping_responses++; 1342 stats_.recv_ping_responses++;
1328 1343
1329 MaybeAddPrflxCandidate(request, response); 1344 MaybeAddPrflxCandidate(request, response);
1330 } 1345 }
1331 1346
1332 void Connection::OnConnectionRequestErrorResponse(ConnectionRequest* request, 1347 void Connection::OnConnectionRequestErrorResponse(ConnectionRequest* request,
1333 StunMessage* response) { 1348 StunMessage* response) {
1334 const StunErrorCodeAttribute* error_attr = response->GetErrorCode(); 1349 const StunErrorCodeAttribute* error_attr = response->GetErrorCode();
1335 int error_code = STUN_ERROR_GLOBAL_FAILURE; 1350 int error_code = STUN_ERROR_GLOBAL_FAILURE;
(...skipping 26 matching lines...) Expand all
1362 // Log at LS_INFO if we miss a ping on a writable connection. 1377 // Log at LS_INFO if we miss a ping on a writable connection.
1363 rtc::LoggingSeverity sev = writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; 1378 rtc::LoggingSeverity sev = writable() ? rtc::LS_INFO : rtc::LS_VERBOSE;
1364 LOG_JV(sev, this) << "Timing-out STUN ping " 1379 LOG_JV(sev, this) << "Timing-out STUN ping "
1365 << rtc::hex_encode(request->id()) 1380 << rtc::hex_encode(request->id())
1366 << " after " << request->Elapsed() << " ms"; 1381 << " after " << request->Elapsed() << " ms";
1367 } 1382 }
1368 1383
1369 void Connection::OnConnectionRequestSent(ConnectionRequest* request) { 1384 void Connection::OnConnectionRequestSent(ConnectionRequest* request) {
1370 // Log at LS_INFO if we send a ping on an unwritable connection. 1385 // Log at LS_INFO if we send a ping on an unwritable connection.
1371 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; 1386 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE;
1372 bool use_candidate = use_candidate_attr();
1373 LOG_JV(sev, this) << "Sent STUN ping" 1387 LOG_JV(sev, this) << "Sent STUN ping"
1374 << ", id=" << rtc::hex_encode(request->id()) 1388 << ", id=" << rtc::hex_encode(request->id())
1375 << ", use_candidate=" << use_candidate; 1389 << ", use_candidate=" << use_candidate_attr()
1390 << ", nominating value=" << nominating_value();
1376 stats_.sent_ping_requests_total++; 1391 stats_.sent_ping_requests_total++;
1377 if (stats_.recv_ping_responses == 0) { 1392 if (stats_.recv_ping_responses == 0) {
1378 stats_.sent_ping_requests_before_first_response++; 1393 stats_.sent_ping_requests_before_first_response++;
1379 } 1394 }
1380 } 1395 }
1381 1396
1382 void Connection::HandleRoleConflictFromPeer() { 1397 void Connection::HandleRoleConflictFromPeer() {
1383 port_->SignalRoleConflict(port_); 1398 port_->SignalRoleConflict(port_);
1384 } 1399 }
1385 1400
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1530 ASSERT(sent < 0); 1545 ASSERT(sent < 0);
1531 error_ = port_->GetError(); 1546 error_ = port_->GetError();
1532 stats_.sent_discarded_packets++; 1547 stats_.sent_discarded_packets++;
1533 } else { 1548 } else {
1534 send_rate_tracker_.AddSamples(sent); 1549 send_rate_tracker_.AddSamples(sent);
1535 } 1550 }
1536 return sent; 1551 return sent;
1537 } 1552 }
1538 1553
1539 } // namespace cricket 1554 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698