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 |
| 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 } |
| (...skipping 70 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 if (tcp_fastopen_write_attempted_) { |
| 143 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection", | 147 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection", |
| 144 fast_open_status_, FAST_OPEN_MAX_VALUE); | 148 tcp_fastopen_status_, TCP_FASTOPEN_MAX_VALUE); |
| 145 } | 149 } |
| 146 } | 150 } |
| 147 | 151 |
| 148 int TCPSocketLibevent::Open(AddressFamily family) { | 152 int TCPSocketLibevent::Open(AddressFamily family) { |
| 149 DCHECK(!socket_); | 153 DCHECK(!socket_); |
| 150 socket_.reset(new SocketLibevent); | 154 socket_.reset(new SocketLibevent); |
| 151 int rv = socket_->Open(ConvertAddressFamily(family)); | 155 int rv = socket_->Open(ConvertAddressFamily(family)); |
| 152 if (rv != OK) | 156 if (rv != OK) |
| 153 socket_.reset(); | 157 socket_.reset(); |
| 154 return rv; | 158 return rv; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 | 219 |
| 216 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, | 220 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, |
| 217 CreateNetLogIPEndPointCallback(&address)); | 221 CreateNetLogIPEndPointCallback(&address)); |
| 218 | 222 |
| 219 SockaddrStorage storage; | 223 SockaddrStorage storage; |
| 220 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 224 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
| 221 return ERR_ADDRESS_INVALID; | 225 return ERR_ADDRESS_INVALID; |
| 222 | 226 |
| 223 if (use_tcp_fastopen_) { | 227 if (use_tcp_fastopen_) { |
| 224 // With TCP FastOpen, we pretend that the socket is connected. | 228 // With TCP FastOpen, we pretend that the socket is connected. |
| 225 DCHECK(!tcp_fastopen_connected_); | 229 DCHECK(!tcp_fastopen_write_attempted_); |
| 226 socket_->SetPeerAddress(storage); | 230 socket_->SetPeerAddress(storage); |
| 227 return OK; | 231 return OK; |
| 228 } | 232 } |
| 229 | 233 |
| 230 int rv = socket_->Connect(storage, | 234 int rv = socket_->Connect(storage, |
| 231 base::Bind(&TCPSocketLibevent::ConnectCompleted, | 235 base::Bind(&TCPSocketLibevent::ConnectCompleted, |
| 232 base::Unretained(this), callback)); | 236 base::Unretained(this), callback)); |
| 233 if (rv != ERR_IO_PENDING) | 237 if (rv != ERR_IO_PENDING) |
| 234 rv = HandleConnectCompleted(rv); | 238 rv = HandleConnectCompleted(rv); |
| 235 return rv; | 239 return rv; |
| 236 } | 240 } |
| 237 | 241 |
| 238 bool TCPSocketLibevent::IsConnected() const { | 242 bool TCPSocketLibevent::IsConnected() const { |
| 239 if (!socket_) | 243 if (!socket_) |
| 240 return false; | 244 return false; |
| 241 | 245 |
| 242 if (use_tcp_fastopen_ && !tcp_fastopen_connected_ && | 246 if (use_tcp_fastopen_ && !tcp_fastopen_write_attempted_ && |
| 243 socket_->HasPeerAddress()) { | 247 socket_->HasPeerAddress()) { |
| 244 // With TCP FastOpen, we pretend that the socket is connected. | 248 // With TCP FastOpen, we pretend that the socket is connected. |
| 245 // This allows GetPeerAddress() to return peer_address_. | 249 // This allows GetPeerAddress() to return peer_address_. |
| 246 return true; | 250 return true; |
| 247 } | 251 } |
| 248 | 252 |
| 249 return socket_->IsConnected(); | 253 return socket_->IsConnected(); |
| 250 } | 254 } |
| 251 | 255 |
| 252 bool TCPSocketLibevent::IsConnectedAndIdle() const { | 256 bool TCPSocketLibevent::IsConnectedAndIdle() const { |
| 253 // TODO(wtc): should we also handle the TCP FastOpen case here, | 257 // TODO(wtc): should we also handle the TCP FastOpen case here, |
| 254 // as we do in IsConnected()? | 258 // as we do in IsConnected()? |
| 255 return socket_ && socket_->IsConnectedAndIdle(); | 259 return socket_ && socket_->IsConnectedAndIdle(); |
| 256 } | 260 } |
| 257 | 261 |
| 258 int TCPSocketLibevent::Read(IOBuffer* buf, | 262 int TCPSocketLibevent::Read(IOBuffer* buf, |
| 259 int buf_len, | 263 int buf_len, |
| 260 const CompletionCallback& callback) { | 264 const CompletionCallback& callback) { |
| 261 DCHECK(socket_); | 265 DCHECK(socket_); |
| 262 DCHECK(!callback.is_null()); | 266 DCHECK(!callback.is_null()); |
| 263 | 267 |
| 264 int rv = socket_->Read( | 268 int rv = socket_->Read( |
| 265 buf, buf_len, | 269 buf, buf_len, |
| 266 base::Bind(&TCPSocketLibevent::ReadCompleted, | 270 base::Bind(&TCPSocketLibevent::ReadCompleted, |
| 267 // Grab a reference to |buf| so that ReadCompleted() can still | 271 // Grab a reference to |buf| so that ReadCompleted() can still |
| 268 // use it when Read() completes, as otherwise, this transfers | 272 // use it when Read() completes, as otherwise, this transfers |
| 269 // ownership of buf to socket. | 273 // ownership of buf to socket. |
| 270 base::Unretained(this), make_scoped_refptr(buf), callback)); | 274 base::Unretained(this), make_scoped_refptr(buf), callback)); |
| 271 if (rv >= 0) | |
| 272 RecordFastOpenStatus(); | |
| 273 if (rv != ERR_IO_PENDING) | 275 if (rv != ERR_IO_PENDING) |
| 274 rv = HandleReadCompleted(buf, rv); | 276 rv = HandleReadCompleted(buf, rv); |
| 275 return rv; | 277 return rv; |
| 276 } | 278 } |
| 277 | 279 |
| 278 int TCPSocketLibevent::Write(IOBuffer* buf, | 280 int TCPSocketLibevent::Write(IOBuffer* buf, |
| 279 int buf_len, | 281 int buf_len, |
| 280 const CompletionCallback& callback) { | 282 const CompletionCallback& callback) { |
| 281 DCHECK(socket_); | 283 DCHECK(socket_); |
| 282 DCHECK(!callback.is_null()); | 284 DCHECK(!callback.is_null()); |
| 283 | 285 |
| 284 CompletionCallback write_callback = | 286 CompletionCallback write_callback = |
| 285 base::Bind(&TCPSocketLibevent::WriteCompleted, | 287 base::Bind(&TCPSocketLibevent::WriteCompleted, |
| 286 // Grab a reference to |buf| so that WriteCompleted() can still | 288 // Grab a reference to |buf| so that WriteCompleted() can still |
| 287 // use it when Write() completes, as otherwise, this transfers | 289 // use it when Write() completes, as otherwise, this transfers |
| 288 // ownership of buf to socket. | 290 // ownership of buf to socket. |
| 289 base::Unretained(this), make_scoped_refptr(buf), callback); | 291 base::Unretained(this), make_scoped_refptr(buf), callback); |
| 290 int rv; | 292 int rv; |
| 291 if (use_tcp_fastopen_ && !tcp_fastopen_connected_) { | 293 |
| 294 if (use_tcp_fastopen_ && !tcp_fastopen_write_attempted_) { | |
| 295 DCHECK(!g_tcp_fastopen_has_failed); | |
| 292 rv = TcpFastOpenWrite(buf, buf_len, write_callback); | 296 rv = TcpFastOpenWrite(buf, buf_len, write_callback); |
| 293 } else { | 297 } else { |
| 294 rv = socket_->Write(buf, buf_len, write_callback); | 298 rv = socket_->Write(buf, buf_len, write_callback); |
| 295 } | 299 } |
| 296 | 300 |
| 297 if (rv != ERR_IO_PENDING) | 301 if (rv != ERR_IO_PENDING) |
| 298 rv = HandleWriteCompleted(buf, rv); | 302 rv = HandleWriteCompleted(buf, rv); |
| 299 return rv; | 303 return rv; |
| 300 } | 304 } |
| 301 | 305 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 return SetTCPKeepAlive(socket_->socket_fd(), enable, delay); | 411 return SetTCPKeepAlive(socket_->socket_fd(), enable, delay); |
| 408 } | 412 } |
| 409 | 413 |
| 410 bool TCPSocketLibevent::SetNoDelay(bool no_delay) { | 414 bool TCPSocketLibevent::SetNoDelay(bool no_delay) { |
| 411 DCHECK(socket_); | 415 DCHECK(socket_); |
| 412 return SetTCPNoDelay(socket_->socket_fd(), no_delay); | 416 return SetTCPNoDelay(socket_->socket_fd(), no_delay); |
| 413 } | 417 } |
| 414 | 418 |
| 415 void TCPSocketLibevent::Close() { | 419 void TCPSocketLibevent::Close() { |
| 416 socket_.reset(); | 420 socket_.reset(); |
| 421 | |
| 422 // Reset TCP FastOpen state. | |
|
mmenke
2014/09/24 14:46:11
Should we have:
if (tcp_fastopen_write_attempted_
Jana
2014/09/25 16:10:16
Good suggestion -- Done.
| |
| 423 use_tcp_fastopen_ = false; | |
| 417 tcp_fastopen_connected_ = false; | 424 tcp_fastopen_connected_ = false; |
| 418 fast_open_status_ = FAST_OPEN_STATUS_UNKNOWN; | 425 tcp_fastopen_write_attempted_ = false; |
| 426 tcp_fastopen_status_ = TCP_FASTOPEN_STATUS_UNKNOWN; | |
| 419 } | 427 } |
| 420 | 428 |
| 421 bool TCPSocketLibevent::UsingTCPFastOpen() const { | 429 bool TCPSocketLibevent::UsingTCPFastOpen() const { |
| 422 return use_tcp_fastopen_; | 430 return use_tcp_fastopen_; |
| 423 } | 431 } |
| 424 | 432 |
| 425 void TCPSocketLibevent::EnableTCPFastOpenIfSupported() { | 433 void TCPSocketLibevent::EnableTCPFastOpenIfSupported() { |
| 426 if (IsTCPFastOpenSupported()) | 434 // Do not enable TCP FastOpen if it had previously failed. |
| 435 // This check conservatively avoids middleboxes that may blackhole | |
| 436 // TCP FastOpen SYN+Data packets; on such a failure, subsequent sockets | |
| 437 // should not use TCP FastOpen. | |
| 438 if (IsTCPFastOpenSupported() && !g_tcp_fastopen_has_failed) | |
| 427 use_tcp_fastopen_ = true; | 439 use_tcp_fastopen_ = true; |
| 428 } | 440 } |
| 429 | 441 |
| 430 bool TCPSocketLibevent::IsValid() const { | 442 bool TCPSocketLibevent::IsValid() const { |
| 431 return socket_ != NULL && socket_->socket_fd() != kInvalidSocket; | 443 return socket_ != NULL && socket_->socket_fd() != kInvalidSocket; |
| 432 } | 444 } |
| 433 | 445 |
| 434 void TCPSocketLibevent::StartLoggingMultipleConnectAttempts( | 446 void TCPSocketLibevent::StartLoggingMultipleConnectAttempts( |
| 435 const AddressList& addresses) { | 447 const AddressList& addresses) { |
| 436 if (!logging_multiple_connect_attempts_) { | 448 if (!logging_multiple_connect_attempts_) { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 546 | 558 |
| 547 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, | 559 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, |
| 548 CreateNetLogSourceAddressCallback(storage.addr, | 560 CreateNetLogSourceAddressCallback(storage.addr, |
| 549 storage.addr_len)); | 561 storage.addr_len)); |
| 550 } | 562 } |
| 551 | 563 |
| 552 void TCPSocketLibevent::ReadCompleted(const scoped_refptr<IOBuffer>& buf, | 564 void TCPSocketLibevent::ReadCompleted(const scoped_refptr<IOBuffer>& buf, |
| 553 const CompletionCallback& callback, | 565 const CompletionCallback& callback, |
| 554 int rv) { | 566 int rv) { |
| 555 DCHECK_NE(ERR_IO_PENDING, rv); | 567 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)); | 568 callback.Run(HandleReadCompleted(buf.get(), rv)); |
| 561 } | 569 } |
| 562 | 570 |
| 563 int TCPSocketLibevent::HandleReadCompleted(IOBuffer* buf, int rv) { | 571 int TCPSocketLibevent::HandleReadCompleted(IOBuffer* buf, int rv) { |
| 572 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_ && | |
| 573 !g_tcp_fastopen_has_failed) { | |
|
mmenke
2014/09/24 14:46:11
The !g_tcp_fastopen_has_failed still seems kinda w
Jana
2014/09/25 16:10:16
Hmm -- good point. I had the g_tcp_fastopen_has_fa
| |
| 574 // A TCP FastOpen connect-with-write was attempted. This read was a | |
| 575 // subsequent read, which either succeeded or failed. If the read | |
| 576 // succeeded, the socket is considered connected via TCP FastOpen. | |
| 577 // If the read failed, TCP FastOpen is (conservatively) turned off for all | |
| 578 // subsequent connections. TCP FastOpen status is recorded in both cases. | |
| 579 // TODO(rdsmith,jri): Change histogram name to indicate it could be called | |
| 580 // on error. | |
| 581 if (rv >= 0) | |
| 582 tcp_fastopen_connected_ = true; | |
| 583 else | |
| 584 g_tcp_fastopen_has_failed = true; | |
| 585 RecordTCPFastOpenStatus(); | |
| 586 } | |
| 587 | |
| 564 if (rv < 0) { | 588 if (rv < 0) { |
| 565 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, | 589 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, |
| 566 CreateNetLogSocketErrorCallback(rv, errno)); | 590 CreateNetLogSocketErrorCallback(rv, errno)); |
| 567 return rv; | 591 return rv; |
| 568 } | 592 } |
| 569 | 593 |
| 570 base::StatsCounter read_bytes("tcp.read_bytes"); | 594 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 571 read_bytes.Add(rv); | 595 read_bytes.Add(rv); |
| 572 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, | 596 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, |
| 573 buf->data()); | 597 buf->data()); |
| 574 return rv; | 598 return rv; |
| 575 } | 599 } |
| 576 | 600 |
| 577 void TCPSocketLibevent::WriteCompleted(const scoped_refptr<IOBuffer>& buf, | 601 void TCPSocketLibevent::WriteCompleted(const scoped_refptr<IOBuffer>& buf, |
| 578 const CompletionCallback& callback, | 602 const CompletionCallback& callback, |
| 579 int rv) const { | 603 int rv) { |
| 580 DCHECK_NE(ERR_IO_PENDING, rv); | 604 DCHECK_NE(ERR_IO_PENDING, rv); |
| 581 callback.Run(HandleWriteCompleted(buf.get(), rv)); | 605 callback.Run(HandleWriteCompleted(buf.get(), rv)); |
| 582 } | 606 } |
| 583 | 607 |
| 584 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) const { | 608 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) { |
| 585 if (rv < 0) { | 609 if (rv < 0) { |
| 610 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { | |
| 611 // TCP FastOpen connect-with-write was attempted, and the write failed | |
| 612 // for unknown reasons. Record status and (conservatively) turn off | |
| 613 // TCP FastOpen for all subsequent connections. | |
| 614 tcp_fastopen_status_ = TCP_FASTOPEN_ERROR; | |
| 615 g_tcp_fastopen_has_failed = true; | |
| 616 } | |
| 586 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, | 617 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, |
| 587 CreateNetLogSocketErrorCallback(rv, errno)); | 618 CreateNetLogSocketErrorCallback(rv, errno)); |
| 588 return rv; | 619 return rv; |
| 589 } | 620 } |
| 590 | 621 |
| 591 base::StatsCounter write_bytes("tcp.write_bytes"); | 622 base::StatsCounter write_bytes("tcp.write_bytes"); |
| 592 write_bytes.Add(rv); | 623 write_bytes.Add(rv); |
| 593 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, | 624 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, |
| 594 buf->data()); | 625 buf->data()); |
| 595 return rv; | 626 return rv; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 611 // for system support on startup, but users may dynamically disable TCP Fast | 642 // for system support on startup, but users may dynamically disable TCP Fast |
| 612 // Open via sysctl. | 643 // Open via sysctl. |
| 613 flags |= MSG_NOSIGNAL; | 644 flags |= MSG_NOSIGNAL; |
| 614 #endif // defined(OS_LINUX) | 645 #endif // defined(OS_LINUX) |
| 615 rv = HANDLE_EINTR(sendto(socket_->socket_fd(), | 646 rv = HANDLE_EINTR(sendto(socket_->socket_fd(), |
| 616 buf->data(), | 647 buf->data(), |
| 617 buf_len, | 648 buf_len, |
| 618 flags, | 649 flags, |
| 619 storage.addr, | 650 storage.addr, |
| 620 storage.addr_len)); | 651 storage.addr_len)); |
| 621 tcp_fastopen_connected_ = true; | 652 tcp_fastopen_write_attempted_ = true; |
| 622 | 653 |
| 623 if (rv >= 0) { | 654 if (rv >= 0) { |
| 624 fast_open_status_ = FAST_OPEN_FAST_CONNECT_RETURN; | 655 tcp_fastopen_status_ = TCP_FASTOPEN_FAST_CONNECT_RETURN; |
| 625 return rv; | 656 return rv; |
| 626 } | 657 } |
| 627 | 658 |
| 628 DCHECK_NE(EPIPE, errno); | 659 DCHECK_NE(EPIPE, errno); |
| 629 | 660 |
| 630 // If errno == EINPROGRESS, that means the kernel didn't have a cookie | 661 // If errno == EINPROGRESS, that means the kernel didn't have a cookie |
| 631 // and would block. The kernel is internally doing a connect() though. | 662 // 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 | 663 // 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 | 664 // asynchronous cases. Note that the user buffer has not been copied to |
| 634 // kernel space. | 665 // kernel space. |
| 635 if (errno == EINPROGRESS) { | 666 if (errno == EINPROGRESS) { |
| 636 rv = ERR_IO_PENDING; | 667 rv = ERR_IO_PENDING; |
| 637 } else { | 668 } else { |
| 638 rv = MapSystemError(errno); | 669 rv = MapSystemError(errno); |
| 639 } | 670 } |
| 640 | 671 |
| 641 if (rv != ERR_IO_PENDING) { | 672 if (rv != ERR_IO_PENDING) { |
| 642 fast_open_status_ = FAST_OPEN_ERROR; | 673 // TCP FastOpen connect-with-write was attempted, and the write failed for |
| 674 // unknown reasons. Record status and (conservatively) turn off | |
| 675 // TCP FastOpen for all subsequent connections. | |
| 676 tcp_fastopen_status_ = TCP_FASTOPEN_ERROR; | |
| 677 g_tcp_fastopen_has_failed = true; | |
| 643 return rv; | 678 return rv; |
| 644 } | 679 } |
| 645 | 680 |
| 646 fast_open_status_ = FAST_OPEN_SLOW_CONNECT_RETURN; | 681 tcp_fastopen_status_ = TCP_FASTOPEN_SLOW_CONNECT_RETURN; |
| 647 return socket_->WaitForWrite(buf, buf_len, callback); | 682 return socket_->WaitForWrite(buf, buf_len, callback); |
| 648 } | 683 } |
| 649 | 684 |
| 650 void TCPSocketLibevent::RecordFastOpenStatus() { | 685 void TCPSocketLibevent::RecordTCPFastOpenStatus() { |
|
mmenke
2014/09/24 14:46:11
Maybe UpdateTCPFastOpenStatusAfterRead?
Jana
2014/09/25 16:10:16
Done.
| |
| 651 if (use_tcp_fastopen_ && | 686 if (!use_tcp_fastopen_ || |
| 652 (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN || | 687 (tcp_fastopen_status_ != TCP_FASTOPEN_FAST_CONNECT_RETURN && |
| 653 fast_open_status_ == FAST_OPEN_SLOW_CONNECT_RETURN)) { | 688 tcp_fastopen_status_ != TCP_FASTOPEN_SLOW_CONNECT_RETURN)) { |
| 654 DCHECK_NE(FAST_OPEN_STATUS_UNKNOWN, fast_open_status_); | 689 // Not using TCP FastOpen, or status has been finalized. |
| 655 bool getsockopt_success(false); | 690 return; |
| 656 bool server_acked_data(false); | 691 } |
| 692 DCHECK_NE(TCP_FASTOPEN_STATUS_UNKNOWN, tcp_fastopen_status_); | |
| 693 | |
| 694 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { | |
| 695 // TCP FastOpen connect-with-write was attempted, and failed. | |
| 696 tcp_fastopen_status_ = | |
| 697 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? | |
| 698 TCP_FASTOPEN_FAST_CONNECT_FAILED : TCP_FASTOPEN_SLOW_CONNECT_FAILED); | |
|
mmenke
2014/09/24 14:46:11
nit: 4 space indent.
Jana
2014/09/25 16:10:16
Done.
| |
| 699 return; | |
| 700 } | |
| 701 | |
| 702 bool getsockopt_success(false); | |
| 703 bool server_acked_data(false); | |
| 657 #if defined(TCP_INFO) | 704 #if defined(TCP_INFO) |
| 658 // Probe to see the if the socket used TCP FastOpen. | 705 // Probe to see the if the socket used TCP FastOpen. |
| 659 tcp_info info; | 706 tcp_info info; |
| 660 socklen_t info_len = sizeof(tcp_info); | 707 socklen_t info_len = sizeof(tcp_info); |
| 661 getsockopt_success = | 708 getsockopt_success = getsockopt(socket_->socket_fd(), IPPROTO_TCP, TCP_INFO, |
| 662 getsockopt(socket_->socket_fd(), IPPROTO_TCP, TCP_INFO, | 709 &info, &info_len) == 0 && |
| 663 &info, &info_len) == 0 && | 710 info_len == sizeof(tcp_info); |
| 664 info_len == sizeof(tcp_info); | 711 server_acked_data = getsockopt_success && |
| 665 server_acked_data = getsockopt_success && | 712 (info.tcpi_options & TCPI_OPT_SYN_DATA); |
| 666 (info.tcpi_options & TCPI_OPT_SYN_DATA); | |
| 667 #endif | 713 #endif |
| 668 if (getsockopt_success) { | 714 |
| 669 if (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN) { | 715 if (getsockopt_success) { |
| 670 fast_open_status_ = (server_acked_data ? FAST_OPEN_SYN_DATA_ACK : | 716 if (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN) { |
| 671 FAST_OPEN_SYN_DATA_NACK); | 717 tcp_fastopen_status_ = (server_acked_data ? |
| 672 } else { | 718 TCP_FASTOPEN_SYN_DATA_ACK : |
| 673 fast_open_status_ = (server_acked_data ? FAST_OPEN_NO_SYN_DATA_ACK : | 719 TCP_FASTOPEN_SYN_DATA_NACK); |
| 674 FAST_OPEN_NO_SYN_DATA_NACK); | |
| 675 } | |
| 676 } else { | 720 } else { |
| 677 fast_open_status_ = (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN ? | 721 tcp_fastopen_status_ = (server_acked_data ? |
| 678 FAST_OPEN_SYN_DATA_FAILED : | 722 TCP_FASTOPEN_NO_SYN_DATA_ACK : |
| 679 FAST_OPEN_NO_SYN_DATA_FAILED); | 723 TCP_FASTOPEN_NO_SYN_DATA_NACK); |
| 680 } | 724 } |
| 725 } else { | |
| 726 tcp_fastopen_status_ = | |
| 727 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? | |
| 728 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED : | |
| 729 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED); | |
| 681 } | 730 } |
| 682 } | 731 } |
| 683 | 732 |
| 684 } // namespace net | 733 } // namespace net |
| OLD | NEW |