Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/tcp_socket.h" | 5 #include "net/socket/tcp_socket.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <netinet/tcp.h> | 8 #include <netinet/tcp.h> |
| 9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 | 32 |
| 33 namespace net { | 33 namespace net { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 // True if OS supports TCP FastOpen. | 37 // True if OS supports TCP FastOpen. |
| 38 bool g_tcp_fastopen_supported = false; | 38 bool g_tcp_fastopen_supported = false; |
| 39 // True if TCP FastOpen is user-enabled for all connections. | 39 // True if TCP FastOpen is user-enabled for all connections. |
| 40 // TODO(jri): Change global variable to param in HttpNetworkSession::Params. | 40 // TODO(jri): Change global variable to param in HttpNetworkSession::Params. |
| 41 bool g_tcp_fastopen_user_enabled = false; | 41 bool g_tcp_fastopen_user_enabled = false; |
| 42 // True if TCP FastOpen connect-with-write has failed at least once. | |
| 43 bool g_tcp_fastopen_has_failed = false; | |
| 44 | |
| 42 | 45 |
|
mmenke
2014/09/29 17:49:34
nit: Remove extra blank line.
Jana
2014/09/29 23:01:46
Done.
| |
| 43 // SetTCPNoDelay turns on/off buffering in the kernel. By default, TCP sockets | 46 // SetTCPNoDelay turns on/off buffering in the kernel. By default, TCP sockets |
| 44 // will wait up to 200ms for more data to complete a packet before transmitting. | 47 // will wait up to 200ms for more data to complete a packet before transmitting. |
| 45 // After calling this function, the kernel will not wait. See TCP_NODELAY in | 48 // After calling this function, the kernel will not wait. See TCP_NODELAY in |
| 46 // `man 7 tcp`. | 49 // `man 7 tcp`. |
| 47 bool SetTCPNoDelay(int fd, bool no_delay) { | 50 bool SetTCPNoDelay(int fd, bool no_delay) { |
| 48 int on = no_delay ? 1 : 0; | 51 int on = no_delay ? 1 : 0; |
| 49 int error = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); | 52 int error = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 50 return error == 0; | 53 return error == 0; |
| 51 } | 54 } |
| 52 | 55 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 base::WorkerPool::GetTaskRunner(/*task_is_slow=*/false).get(), | 125 base::WorkerPool::GetTaskRunner(/*task_is_slow=*/false).get(), |
| 123 FROM_HERE, | 126 FROM_HERE, |
| 124 base::Bind(SystemSupportsTCPFastOpen), | 127 base::Bind(SystemSupportsTCPFastOpen), |
| 125 base::Bind(RegisterTCPFastOpenIntentAndSupport, user_enabled)); | 128 base::Bind(RegisterTCPFastOpenIntentAndSupport, user_enabled)); |
| 126 #endif | 129 #endif |
| 127 } | 130 } |
| 128 | 131 |
| 129 TCPSocketLibevent::TCPSocketLibevent(NetLog* net_log, | 132 TCPSocketLibevent::TCPSocketLibevent(NetLog* net_log, |
| 130 const NetLog::Source& source) | 133 const NetLog::Source& source) |
| 131 : use_tcp_fastopen_(false), | 134 : use_tcp_fastopen_(false), |
| 135 tcp_fastopen_write_attempted_(false), | |
| 132 tcp_fastopen_connected_(false), | 136 tcp_fastopen_connected_(false), |
| 133 fast_open_status_(FAST_OPEN_STATUS_UNKNOWN), | 137 tcp_fastopen_status_(TCP_FASTOPEN_STATUS_UNKNOWN), |
| 134 logging_multiple_connect_attempts_(false), | 138 logging_multiple_connect_attempts_(false), |
| 135 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { | 139 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
| 136 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, | 140 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 137 source.ToEventParametersCallback()); | 141 source.ToEventParametersCallback()); |
| 138 } | 142 } |
| 139 | 143 |
| 140 TCPSocketLibevent::~TCPSocketLibevent() { | 144 TCPSocketLibevent::~TCPSocketLibevent() { |
| 141 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); | 145 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
| 142 if (tcp_fastopen_connected_) { | 146 Close(); |
| 143 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection", | |
| 144 fast_open_status_, FAST_OPEN_MAX_VALUE); | |
| 145 } | |
| 146 } | 147 } |
| 147 | 148 |
| 148 int TCPSocketLibevent::Open(AddressFamily family) { | 149 int TCPSocketLibevent::Open(AddressFamily family) { |
| 149 DCHECK(!socket_); | 150 DCHECK(!socket_); |
| 150 socket_.reset(new SocketLibevent); | 151 socket_.reset(new SocketLibevent); |
| 151 int rv = socket_->Open(ConvertAddressFamily(family)); | 152 int rv = socket_->Open(ConvertAddressFamily(family)); |
| 152 if (rv != OK) | 153 if (rv != OK) |
| 153 socket_.reset(); | 154 socket_.reset(); |
| 154 return rv; | 155 return rv; |
| 155 } | 156 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 | 216 |
| 216 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, | 217 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, |
| 217 CreateNetLogIPEndPointCallback(&address)); | 218 CreateNetLogIPEndPointCallback(&address)); |
| 218 | 219 |
| 219 SockaddrStorage storage; | 220 SockaddrStorage storage; |
| 220 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 221 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
| 221 return ERR_ADDRESS_INVALID; | 222 return ERR_ADDRESS_INVALID; |
| 222 | 223 |
| 223 if (use_tcp_fastopen_) { | 224 if (use_tcp_fastopen_) { |
| 224 // With TCP FastOpen, we pretend that the socket is connected. | 225 // With TCP FastOpen, we pretend that the socket is connected. |
| 225 DCHECK(!tcp_fastopen_connected_); | 226 DCHECK(!tcp_fastopen_write_attempted_); |
| 226 socket_->SetPeerAddress(storage); | 227 socket_->SetPeerAddress(storage); |
| 227 return OK; | 228 return OK; |
| 228 } | 229 } |
| 229 | 230 |
| 230 int rv = socket_->Connect(storage, | 231 int rv = socket_->Connect(storage, |
| 231 base::Bind(&TCPSocketLibevent::ConnectCompleted, | 232 base::Bind(&TCPSocketLibevent::ConnectCompleted, |
| 232 base::Unretained(this), callback)); | 233 base::Unretained(this), callback)); |
| 233 if (rv != ERR_IO_PENDING) | 234 if (rv != ERR_IO_PENDING) |
| 234 rv = HandleConnectCompleted(rv); | 235 rv = HandleConnectCompleted(rv); |
| 235 return rv; | 236 return rv; |
| 236 } | 237 } |
| 237 | 238 |
| 238 bool TCPSocketLibevent::IsConnected() const { | 239 bool TCPSocketLibevent::IsConnected() const { |
| 239 if (!socket_) | 240 if (!socket_) |
| 240 return false; | 241 return false; |
| 241 | 242 |
| 242 if (use_tcp_fastopen_ && !tcp_fastopen_connected_ && | 243 if (use_tcp_fastopen_ && !tcp_fastopen_write_attempted_ && |
| 243 socket_->HasPeerAddress()) { | 244 socket_->HasPeerAddress()) { |
| 244 // With TCP FastOpen, we pretend that the socket is connected. | 245 // With TCP FastOpen, we pretend that the socket is connected. |
| 245 // This allows GetPeerAddress() to return peer_address_. | 246 // This allows GetPeerAddress() to return peer_address_. |
| 246 return true; | 247 return true; |
| 247 } | 248 } |
| 248 | 249 |
| 249 return socket_->IsConnected(); | 250 return socket_->IsConnected(); |
| 250 } | 251 } |
| 251 | 252 |
| 252 bool TCPSocketLibevent::IsConnectedAndIdle() const { | 253 bool TCPSocketLibevent::IsConnectedAndIdle() const { |
| 253 // TODO(wtc): should we also handle the TCP FastOpen case here, | 254 // TODO(wtc): should we also handle the TCP FastOpen case here, |
| 254 // as we do in IsConnected()? | 255 // as we do in IsConnected()? |
| 255 return socket_ && socket_->IsConnectedAndIdle(); | 256 return socket_ && socket_->IsConnectedAndIdle(); |
| 256 } | 257 } |
| 257 | 258 |
| 258 int TCPSocketLibevent::Read(IOBuffer* buf, | 259 int TCPSocketLibevent::Read(IOBuffer* buf, |
| 259 int buf_len, | 260 int buf_len, |
| 260 const CompletionCallback& callback) { | 261 const CompletionCallback& callback) { |
| 261 DCHECK(socket_); | 262 DCHECK(socket_); |
| 262 DCHECK(!callback.is_null()); | 263 DCHECK(!callback.is_null()); |
| 263 | 264 |
| 264 int rv = socket_->Read( | 265 int rv = socket_->Read( |
| 265 buf, buf_len, | 266 buf, buf_len, |
| 266 base::Bind(&TCPSocketLibevent::ReadCompleted, | 267 base::Bind(&TCPSocketLibevent::ReadCompleted, |
| 267 // Grab a reference to |buf| so that ReadCompleted() can still | 268 // Grab a reference to |buf| so that ReadCompleted() can still |
| 268 // use it when Read() completes, as otherwise, this transfers | 269 // use it when Read() completes, as otherwise, this transfers |
| 269 // ownership of buf to socket. | 270 // ownership of buf to socket. |
| 270 base::Unretained(this), make_scoped_refptr(buf), callback)); | 271 base::Unretained(this), make_scoped_refptr(buf), callback)); |
| 271 if (rv >= 0) | |
| 272 RecordFastOpenStatus(); | |
| 273 if (rv != ERR_IO_PENDING) | 272 if (rv != ERR_IO_PENDING) |
| 274 rv = HandleReadCompleted(buf, rv); | 273 rv = HandleReadCompleted(buf, rv); |
|
mmenke
2014/09/29 17:49:34
Previously, we didn't call RecordFastOpenStatus on
Jana
2014/09/29 23:01:46
Yes, I believe so. That's why I moved it to Handle
| |
| 275 return rv; | 274 return rv; |
| 276 } | 275 } |
| 277 | 276 |
| 278 int TCPSocketLibevent::Write(IOBuffer* buf, | 277 int TCPSocketLibevent::Write(IOBuffer* buf, |
| 279 int buf_len, | 278 int buf_len, |
| 280 const CompletionCallback& callback) { | 279 const CompletionCallback& callback) { |
| 281 DCHECK(socket_); | 280 DCHECK(socket_); |
| 282 DCHECK(!callback.is_null()); | 281 DCHECK(!callback.is_null()); |
| 283 | 282 |
| 284 CompletionCallback write_callback = | 283 CompletionCallback write_callback = |
| 285 base::Bind(&TCPSocketLibevent::WriteCompleted, | 284 base::Bind(&TCPSocketLibevent::WriteCompleted, |
| 286 // Grab a reference to |buf| so that WriteCompleted() can still | 285 // Grab a reference to |buf| so that WriteCompleted() can still |
| 287 // use it when Write() completes, as otherwise, this transfers | 286 // use it when Write() completes, as otherwise, this transfers |
| 288 // ownership of buf to socket. | 287 // ownership of buf to socket. |
| 289 base::Unretained(this), make_scoped_refptr(buf), callback); | 288 base::Unretained(this), make_scoped_refptr(buf), callback); |
| 290 int rv; | 289 int rv; |
| 291 if (use_tcp_fastopen_ && !tcp_fastopen_connected_) { | 290 |
| 291 if (use_tcp_fastopen_ && !tcp_fastopen_write_attempted_) { | |
| 292 rv = TcpFastOpenWrite(buf, buf_len, write_callback); | 292 rv = TcpFastOpenWrite(buf, buf_len, write_callback); |
| 293 } else { | 293 } else { |
| 294 rv = socket_->Write(buf, buf_len, write_callback); | 294 rv = socket_->Write(buf, buf_len, write_callback); |
| 295 } | 295 } |
| 296 | 296 |
| 297 if (rv != ERR_IO_PENDING) | 297 if (rv != ERR_IO_PENDING) |
| 298 rv = HandleWriteCompleted(buf, rv); | 298 rv = HandleWriteCompleted(buf, rv); |
| 299 return rv; | 299 return rv; |
| 300 } | 300 } |
| 301 | 301 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 return SetTCPKeepAlive(socket_->socket_fd(), enable, delay); | 407 return SetTCPKeepAlive(socket_->socket_fd(), enable, delay); |
| 408 } | 408 } |
| 409 | 409 |
| 410 bool TCPSocketLibevent::SetNoDelay(bool no_delay) { | 410 bool TCPSocketLibevent::SetNoDelay(bool no_delay) { |
| 411 DCHECK(socket_); | 411 DCHECK(socket_); |
| 412 return SetTCPNoDelay(socket_->socket_fd(), no_delay); | 412 return SetTCPNoDelay(socket_->socket_fd(), no_delay); |
| 413 } | 413 } |
| 414 | 414 |
| 415 void TCPSocketLibevent::Close() { | 415 void TCPSocketLibevent::Close() { |
| 416 socket_.reset(); | 416 socket_.reset(); |
| 417 | |
| 418 // Record and reset TCP FastOpen state. | |
| 419 if (tcp_fastopen_write_attempted_ || | |
| 420 tcp_fastopen_status_ == TCP_FASTOPEN_PREVIOUSLY_FAILED) { | |
| 421 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection", | |
| 422 tcp_fastopen_status_, TCP_FASTOPEN_MAX_VALUE); | |
| 423 } | |
| 424 use_tcp_fastopen_ = false; | |
| 417 tcp_fastopen_connected_ = false; | 425 tcp_fastopen_connected_ = false; |
| 418 fast_open_status_ = FAST_OPEN_STATUS_UNKNOWN; | 426 tcp_fastopen_write_attempted_ = false; |
| 427 tcp_fastopen_status_ = TCP_FASTOPEN_STATUS_UNKNOWN; | |
| 419 } | 428 } |
| 420 | 429 |
| 421 bool TCPSocketLibevent::UsingTCPFastOpen() const { | 430 bool TCPSocketLibevent::UsingTCPFastOpen() const { |
| 422 return use_tcp_fastopen_; | 431 return use_tcp_fastopen_; |
| 423 } | 432 } |
| 424 | 433 |
| 425 void TCPSocketLibevent::EnableTCPFastOpenIfSupported() { | 434 void TCPSocketLibevent::EnableTCPFastOpenIfSupported() { |
| 426 if (IsTCPFastOpenSupported()) | 435 if (!IsTCPFastOpenSupported()) |
| 436 return; | |
| 437 | |
| 438 // Do not enable TCP FastOpen if it had previously failed. | |
| 439 // This check conservatively avoids middleboxes that may blackhole | |
| 440 // TCP FastOpen SYN+Data packets; on such a failure, subsequent sockets | |
| 441 // should not use TCP FastOpen. | |
| 442 if(!g_tcp_fastopen_has_failed) | |
| 427 use_tcp_fastopen_ = true; | 443 use_tcp_fastopen_ = true; |
| 444 else | |
| 445 tcp_fastopen_status_ = TCP_FASTOPEN_PREVIOUSLY_FAILED; | |
|
mmenke
2014/09/29 17:49:34
This doesn't quite match the not-yet-failed case -
Jana
2014/09/29 23:01:47
Yes, you're right on both accounts. I agree that i
| |
| 428 } | 446 } |
| 429 | 447 |
| 430 bool TCPSocketLibevent::IsValid() const { | 448 bool TCPSocketLibevent::IsValid() const { |
| 431 return socket_ != NULL && socket_->socket_fd() != kInvalidSocket; | 449 return socket_ != NULL && socket_->socket_fd() != kInvalidSocket; |
| 432 } | 450 } |
| 433 | 451 |
| 434 void TCPSocketLibevent::StartLoggingMultipleConnectAttempts( | 452 void TCPSocketLibevent::StartLoggingMultipleConnectAttempts( |
| 435 const AddressList& addresses) { | 453 const AddressList& addresses) { |
| 436 if (!logging_multiple_connect_attempts_) { | 454 if (!logging_multiple_connect_attempts_) { |
| 437 logging_multiple_connect_attempts_ = true; | 455 logging_multiple_connect_attempts_ = true; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 546 | 564 |
| 547 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, | 565 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, |
| 548 CreateNetLogSourceAddressCallback(storage.addr, | 566 CreateNetLogSourceAddressCallback(storage.addr, |
| 549 storage.addr_len)); | 567 storage.addr_len)); |
| 550 } | 568 } |
| 551 | 569 |
| 552 void TCPSocketLibevent::ReadCompleted(const scoped_refptr<IOBuffer>& buf, | 570 void TCPSocketLibevent::ReadCompleted(const scoped_refptr<IOBuffer>& buf, |
| 553 const CompletionCallback& callback, | 571 const CompletionCallback& callback, |
| 554 int rv) { | 572 int rv) { |
| 555 DCHECK_NE(ERR_IO_PENDING, rv); | 573 DCHECK_NE(ERR_IO_PENDING, rv); |
| 556 // Records TCP FastOpen status regardless of error in asynchronous case. | |
| 557 // TODO(rdsmith,jri): Change histogram name to indicate it could be called on | |
| 558 // error. | |
| 559 RecordFastOpenStatus(); | |
| 560 callback.Run(HandleReadCompleted(buf.get(), rv)); | 574 callback.Run(HandleReadCompleted(buf.get(), rv)); |
| 561 } | 575 } |
| 562 | 576 |
| 563 int TCPSocketLibevent::HandleReadCompleted(IOBuffer* buf, int rv) { | 577 int TCPSocketLibevent::HandleReadCompleted(IOBuffer* buf, int rv) { |
| 578 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { | |
| 579 // A TCP FastOpen connect-with-write was attempted. This read was a | |
| 580 // subsequent read, which either succeeded or failed. If the read | |
| 581 // succeeded, the socket is considered connected via TCP FastOpen. | |
| 582 // If the read failed, TCP FastOpen is (conservatively) turned off for all | |
| 583 // subsequent connections. TCP FastOpen status is recorded in both cases. | |
| 584 // TODO(rdsmith,jri): Change histogram name to indicate it could be called | |
| 585 // on error. | |
| 586 if (rv >= 0) | |
| 587 tcp_fastopen_connected_ = true; | |
| 588 else | |
| 589 g_tcp_fastopen_has_failed = true; | |
| 590 UpdateTCPFastOpenStatusAfterRead(); | |
|
mmenke
2014/09/29 17:49:34
Not relevant for this CL, but... So if we ever ge
mmenke
2014/09/29 17:51:55
Not relevant meaning I don't want to force a more
Jana
2014/09/29 23:01:46
Yes we are more conservative than we need to be. W
| |
| 591 } | |
| 592 | |
| 564 if (rv < 0) { | 593 if (rv < 0) { |
| 565 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, | 594 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, |
| 566 CreateNetLogSocketErrorCallback(rv, errno)); | 595 CreateNetLogSocketErrorCallback(rv, errno)); |
| 567 return rv; | 596 return rv; |
| 568 } | 597 } |
| 569 | 598 |
| 570 base::StatsCounter read_bytes("tcp.read_bytes"); | 599 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 571 read_bytes.Add(rv); | 600 read_bytes.Add(rv); |
| 572 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, | 601 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, |
| 573 buf->data()); | 602 buf->data()); |
| 574 return rv; | 603 return rv; |
| 575 } | 604 } |
| 576 | 605 |
| 577 void TCPSocketLibevent::WriteCompleted(const scoped_refptr<IOBuffer>& buf, | 606 void TCPSocketLibevent::WriteCompleted(const scoped_refptr<IOBuffer>& buf, |
| 578 const CompletionCallback& callback, | 607 const CompletionCallback& callback, |
| 579 int rv) const { | 608 int rv) { |
| 580 DCHECK_NE(ERR_IO_PENDING, rv); | 609 DCHECK_NE(ERR_IO_PENDING, rv); |
| 581 callback.Run(HandleWriteCompleted(buf.get(), rv)); | 610 callback.Run(HandleWriteCompleted(buf.get(), rv)); |
| 582 } | 611 } |
| 583 | 612 |
| 584 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) const { | 613 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) { |
| 585 if (rv < 0) { | 614 if (rv < 0) { |
| 615 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { | |
| 616 // TCP FastOpen connect-with-write was attempted, and the write failed | |
| 617 // for unknown reasons. Record status and (conservatively) turn off | |
| 618 // TCP FastOpen for all subsequent connections. | |
|
mmenke
2014/09/29 17:49:34
This comment seems to be making the assumption tha
Jana
2014/09/29 23:01:47
Hmm... good point. I am comfortable, especially gi
mmenke
2014/09/30 14:30:34
I should note I was wrong here. UpdateTCPFastOpen
| |
| 619 tcp_fastopen_status_ = TCP_FASTOPEN_ERROR; | |
| 620 g_tcp_fastopen_has_failed = true; | |
| 621 } | |
| 586 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, | 622 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, |
| 587 CreateNetLogSocketErrorCallback(rv, errno)); | 623 CreateNetLogSocketErrorCallback(rv, errno)); |
| 588 return rv; | 624 return rv; |
| 589 } | 625 } |
| 590 | 626 |
| 591 base::StatsCounter write_bytes("tcp.write_bytes"); | 627 base::StatsCounter write_bytes("tcp.write_bytes"); |
| 592 write_bytes.Add(rv); | 628 write_bytes.Add(rv); |
| 593 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, | 629 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, |
| 594 buf->data()); | 630 buf->data()); |
| 595 return rv; | 631 return rv; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 611 // for system support on startup, but users may dynamically disable TCP Fast | 647 // for system support on startup, but users may dynamically disable TCP Fast |
| 612 // Open via sysctl. | 648 // Open via sysctl. |
| 613 flags |= MSG_NOSIGNAL; | 649 flags |= MSG_NOSIGNAL; |
| 614 #endif // defined(OS_LINUX) | 650 #endif // defined(OS_LINUX) |
| 615 rv = HANDLE_EINTR(sendto(socket_->socket_fd(), | 651 rv = HANDLE_EINTR(sendto(socket_->socket_fd(), |
| 616 buf->data(), | 652 buf->data(), |
| 617 buf_len, | 653 buf_len, |
| 618 flags, | 654 flags, |
| 619 storage.addr, | 655 storage.addr, |
| 620 storage.addr_len)); | 656 storage.addr_len)); |
| 621 tcp_fastopen_connected_ = true; | 657 tcp_fastopen_write_attempted_ = true; |
| 622 | 658 |
| 623 if (rv >= 0) { | 659 if (rv >= 0) { |
| 624 fast_open_status_ = FAST_OPEN_FAST_CONNECT_RETURN; | 660 tcp_fastopen_status_ = TCP_FASTOPEN_FAST_CONNECT_RETURN; |
| 625 return rv; | 661 return rv; |
| 626 } | 662 } |
| 627 | 663 |
| 628 DCHECK_NE(EPIPE, errno); | 664 DCHECK_NE(EPIPE, errno); |
| 629 | 665 |
| 630 // If errno == EINPROGRESS, that means the kernel didn't have a cookie | 666 // If errno == EINPROGRESS, that means the kernel didn't have a cookie |
| 631 // and would block. The kernel is internally doing a connect() though. | 667 // and would block. The kernel is internally doing a connect() though. |
| 632 // Remap EINPROGRESS to EAGAIN so we treat this the same as our other | 668 // Remap EINPROGRESS to EAGAIN so we treat this the same as our other |
| 633 // asynchronous cases. Note that the user buffer has not been copied to | 669 // asynchronous cases. Note that the user buffer has not been copied to |
| 634 // kernel space. | 670 // kernel space. |
| 635 if (errno == EINPROGRESS) { | 671 if (errno == EINPROGRESS) { |
| 636 rv = ERR_IO_PENDING; | 672 rv = ERR_IO_PENDING; |
| 637 } else { | 673 } else { |
| 638 rv = MapSystemError(errno); | 674 rv = MapSystemError(errno); |
| 639 } | 675 } |
| 640 | 676 |
| 641 if (rv != ERR_IO_PENDING) { | 677 if (rv != ERR_IO_PENDING) { |
| 642 fast_open_status_ = FAST_OPEN_ERROR; | 678 // TCP FastOpen connect-with-write was attempted, and the write failed for |
| 679 // unknown reasons. Record status and (conservatively) turn off | |
| 680 // TCP FastOpen for all subsequent connections. | |
| 681 tcp_fastopen_status_ = TCP_FASTOPEN_ERROR; | |
| 682 g_tcp_fastopen_has_failed = true; | |
| 643 return rv; | 683 return rv; |
| 644 } | 684 } |
| 645 | 685 |
| 646 fast_open_status_ = FAST_OPEN_SLOW_CONNECT_RETURN; | 686 tcp_fastopen_status_ = TCP_FASTOPEN_SLOW_CONNECT_RETURN; |
| 647 return socket_->WaitForWrite(buf, buf_len, callback); | 687 return socket_->WaitForWrite(buf, buf_len, callback); |
| 648 } | 688 } |
| 649 | 689 |
| 650 void TCPSocketLibevent::RecordFastOpenStatus() { | 690 void TCPSocketLibevent::UpdateTCPFastOpenStatusAfterRead() { |
| 651 if (use_tcp_fastopen_ && | 691 if (!use_tcp_fastopen_ || |
| 652 (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN || | 692 (tcp_fastopen_status_ != TCP_FASTOPEN_FAST_CONNECT_RETURN && |
| 653 fast_open_status_ == FAST_OPEN_SLOW_CONNECT_RETURN)) { | 693 tcp_fastopen_status_ != TCP_FASTOPEN_SLOW_CONNECT_RETURN)) { |
| 654 DCHECK_NE(FAST_OPEN_STATUS_UNKNOWN, fast_open_status_); | 694 // Not using TCP FastOpen, or status has been finalized. |
| 655 bool getsockopt_success(false); | 695 return; |
| 656 bool server_acked_data(false); | 696 } |
| 697 DCHECK_NE(TCP_FASTOPEN_STATUS_UNKNOWN, tcp_fastopen_status_); | |
| 698 | |
| 699 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { | |
| 700 // TCP FastOpen connect-with-write was attempted, and failed. | |
| 701 tcp_fastopen_status_ = | |
| 702 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? | |
| 703 TCP_FASTOPEN_FAST_CONNECT_READ_FAILED : | |
| 704 TCP_FASTOPEN_SLOW_CONNECT_READ_FAILED); | |
| 705 return; | |
| 706 } | |
| 707 | |
| 708 bool getsockopt_success(false); | |
| 709 bool server_acked_data(false); | |
| 657 #if defined(TCP_INFO) | 710 #if defined(TCP_INFO) |
| 658 // Probe to see the if the socket used TCP FastOpen. | 711 // Probe to see the if the socket used TCP FastOpen. |
| 659 tcp_info info; | 712 tcp_info info; |
| 660 socklen_t info_len = sizeof(tcp_info); | 713 socklen_t info_len = sizeof(tcp_info); |
| 661 getsockopt_success = | 714 getsockopt_success = getsockopt(socket_->socket_fd(), IPPROTO_TCP, TCP_INFO, |
| 662 getsockopt(socket_->socket_fd(), IPPROTO_TCP, TCP_INFO, | 715 &info, &info_len) == 0 && |
| 663 &info, &info_len) == 0 && | 716 info_len == sizeof(tcp_info); |
| 664 info_len == sizeof(tcp_info); | 717 server_acked_data = getsockopt_success && |
| 665 server_acked_data = getsockopt_success && | 718 (info.tcpi_options & TCPI_OPT_SYN_DATA); |
| 666 (info.tcpi_options & TCPI_OPT_SYN_DATA); | |
| 667 #endif | 719 #endif |
| 668 if (getsockopt_success) { | 720 |
| 669 if (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN) { | 721 if (getsockopt_success) { |
| 670 fast_open_status_ = (server_acked_data ? FAST_OPEN_SYN_DATA_ACK : | 722 if (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN) { |
| 671 FAST_OPEN_SYN_DATA_NACK); | 723 tcp_fastopen_status_ = (server_acked_data ? |
| 672 } else { | 724 TCP_FASTOPEN_SYN_DATA_ACK : |
| 673 fast_open_status_ = (server_acked_data ? FAST_OPEN_NO_SYN_DATA_ACK : | 725 TCP_FASTOPEN_SYN_DATA_NACK); |
| 674 FAST_OPEN_NO_SYN_DATA_NACK); | |
| 675 } | |
| 676 } else { | 726 } else { |
| 677 fast_open_status_ = (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN ? | 727 tcp_fastopen_status_ = (server_acked_data ? |
| 678 FAST_OPEN_SYN_DATA_FAILED : | 728 TCP_FASTOPEN_NO_SYN_DATA_ACK : |
| 679 FAST_OPEN_NO_SYN_DATA_FAILED); | 729 TCP_FASTOPEN_NO_SYN_DATA_NACK); |
| 680 } | 730 } |
| 731 } else { | |
| 732 tcp_fastopen_status_ = | |
| 733 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? | |
| 734 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED : | |
| 735 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED); | |
| 681 } | 736 } |
| 682 } | 737 } |
| 683 | 738 |
| 684 } // namespace net | 739 } // namespace net |
| OLD | NEW |