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; | |
| 42 | 44 |
| 43 // SetTCPNoDelay turns on/off buffering in the kernel. By default, TCP sockets | 45 // 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. | 46 // 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 | 47 // After calling this function, the kernel will not wait. See TCP_NODELAY in |
| 46 // `man 7 tcp`. | 48 // `man 7 tcp`. |
| 47 bool SetTCPNoDelay(int fd, bool no_delay) { | 49 bool SetTCPNoDelay(int fd, bool no_delay) { |
| 48 int on = no_delay ? 1 : 0; | 50 int on = no_delay ? 1 : 0; |
| 49 int error = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); | 51 int error = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 50 return error == 0; | 52 return error == 0; |
| 51 } | 53 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 base::WorkerPool::GetTaskRunner(/*task_is_slow=*/false).get(), | 124 base::WorkerPool::GetTaskRunner(/*task_is_slow=*/false).get(), |
| 123 FROM_HERE, | 125 FROM_HERE, |
| 124 base::Bind(SystemSupportsTCPFastOpen), | 126 base::Bind(SystemSupportsTCPFastOpen), |
| 125 base::Bind(RegisterTCPFastOpenIntentAndSupport, user_enabled)); | 127 base::Bind(RegisterTCPFastOpenIntentAndSupport, user_enabled)); |
| 126 #endif | 128 #endif |
| 127 } | 129 } |
| 128 | 130 |
| 129 TCPSocketLibevent::TCPSocketLibevent(NetLog* net_log, | 131 TCPSocketLibevent::TCPSocketLibevent(NetLog* net_log, |
| 130 const NetLog::Source& source) | 132 const NetLog::Source& source) |
| 131 : use_tcp_fastopen_(false), | 133 : use_tcp_fastopen_(false), |
| 134 tcp_fastopen_write_attempted_(false), | |
| 132 tcp_fastopen_connected_(false), | 135 tcp_fastopen_connected_(false), |
| 133 fast_open_status_(FAST_OPEN_STATUS_UNKNOWN), | 136 tcp_fastopen_status_(TCP_FASTOPEN_STATUS_UNKNOWN), |
| 134 logging_multiple_connect_attempts_(false), | 137 logging_multiple_connect_attempts_(false), |
| 135 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { | 138 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
| 136 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, | 139 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 137 source.ToEventParametersCallback()); | 140 source.ToEventParametersCallback()); |
| 138 } | 141 } |
| 139 | 142 |
| 140 TCPSocketLibevent::~TCPSocketLibevent() { | 143 TCPSocketLibevent::~TCPSocketLibevent() { |
| 141 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); | 144 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
| 142 if (tcp_fastopen_connected_) { | 145 Close(); |
| 143 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection", | |
| 144 fast_open_status_, FAST_OPEN_MAX_VALUE); | |
| 145 } | |
| 146 } | 146 } |
| 147 | 147 |
| 148 int TCPSocketLibevent::Open(AddressFamily family) { | 148 int TCPSocketLibevent::Open(AddressFamily family) { |
| 149 DCHECK(!socket_); | 149 DCHECK(!socket_); |
| 150 socket_.reset(new SocketLibevent); | 150 socket_.reset(new SocketLibevent); |
| 151 int rv = socket_->Open(ConvertAddressFamily(family)); | 151 int rv = socket_->Open(ConvertAddressFamily(family)); |
| 152 if (rv != OK) | 152 if (rv != OK) |
| 153 socket_.reset(); | 153 socket_.reset(); |
| 154 return rv; | 154 return rv; |
| 155 } | 155 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 | 215 |
| 216 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, | 216 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT, |
| 217 CreateNetLogIPEndPointCallback(&address)); | 217 CreateNetLogIPEndPointCallback(&address)); |
| 218 | 218 |
| 219 SockaddrStorage storage; | 219 SockaddrStorage storage; |
| 220 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) | 220 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
| 221 return ERR_ADDRESS_INVALID; | 221 return ERR_ADDRESS_INVALID; |
| 222 | 222 |
| 223 if (use_tcp_fastopen_) { | 223 if (use_tcp_fastopen_) { |
| 224 // With TCP FastOpen, we pretend that the socket is connected. | 224 // With TCP FastOpen, we pretend that the socket is connected. |
| 225 DCHECK(!tcp_fastopen_connected_); | 225 DCHECK(!tcp_fastopen_write_attempted_); |
| 226 socket_->SetPeerAddress(storage); | 226 socket_->SetPeerAddress(storage); |
| 227 return OK; | 227 return OK; |
| 228 } | 228 } |
| 229 | 229 |
| 230 int rv = socket_->Connect(storage, | 230 int rv = socket_->Connect(storage, |
| 231 base::Bind(&TCPSocketLibevent::ConnectCompleted, | 231 base::Bind(&TCPSocketLibevent::ConnectCompleted, |
| 232 base::Unretained(this), callback)); | 232 base::Unretained(this), callback)); |
| 233 if (rv != ERR_IO_PENDING) | 233 if (rv != ERR_IO_PENDING) |
| 234 rv = HandleConnectCompleted(rv); | 234 rv = HandleConnectCompleted(rv); |
| 235 return rv; | 235 return rv; |
| 236 } | 236 } |
| 237 | 237 |
| 238 bool TCPSocketLibevent::IsConnected() const { | 238 bool TCPSocketLibevent::IsConnected() const { |
| 239 if (!socket_) | 239 if (!socket_) |
| 240 return false; | 240 return false; |
| 241 | 241 |
| 242 if (use_tcp_fastopen_ && !tcp_fastopen_connected_ && | 242 if (use_tcp_fastopen_ && !tcp_fastopen_write_attempted_ && |
|
mmenke
2014/09/30 14:30:34
One more question: tcp_fastopen_write_attempted_
Jana
2014/10/01 01:54:00
Good question -- it took me some digging to figure
| |
| 243 socket_->HasPeerAddress()) { | 243 socket_->HasPeerAddress()) { |
| 244 // With TCP FastOpen, we pretend that the socket is connected. | 244 // With TCP FastOpen, we pretend that the socket is connected. |
| 245 // This allows GetPeerAddress() to return peer_address_. | 245 // This allows GetPeerAddress() to return peer_address_. |
| 246 return true; | 246 return true; |
| 247 } | 247 } |
| 248 | 248 |
| 249 return socket_->IsConnected(); | 249 return socket_->IsConnected(); |
| 250 } | 250 } |
| 251 | 251 |
| 252 bool TCPSocketLibevent::IsConnectedAndIdle() const { | 252 bool TCPSocketLibevent::IsConnectedAndIdle() const { |
| 253 // TODO(wtc): should we also handle the TCP FastOpen case here, | 253 // TODO(wtc): should we also handle the TCP FastOpen case here, |
| 254 // as we do in IsConnected()? | 254 // as we do in IsConnected()? |
| 255 return socket_ && socket_->IsConnectedAndIdle(); | 255 return socket_ && socket_->IsConnectedAndIdle(); |
| 256 } | 256 } |
| 257 | 257 |
| 258 int TCPSocketLibevent::Read(IOBuffer* buf, | 258 int TCPSocketLibevent::Read(IOBuffer* buf, |
| 259 int buf_len, | 259 int buf_len, |
| 260 const CompletionCallback& callback) { | 260 const CompletionCallback& callback) { |
| 261 DCHECK(socket_); | 261 DCHECK(socket_); |
| 262 DCHECK(!callback.is_null()); | 262 DCHECK(!callback.is_null()); |
| 263 | 263 |
| 264 int rv = socket_->Read( | 264 int rv = socket_->Read( |
| 265 buf, buf_len, | 265 buf, buf_len, |
| 266 base::Bind(&TCPSocketLibevent::ReadCompleted, | 266 base::Bind(&TCPSocketLibevent::ReadCompleted, |
| 267 // Grab a reference to |buf| so that ReadCompleted() can still | 267 // Grab a reference to |buf| so that ReadCompleted() can still |
| 268 // use it when Read() completes, as otherwise, this transfers | 268 // use it when Read() completes, as otherwise, this transfers |
| 269 // ownership of buf to socket. | 269 // ownership of buf to socket. |
| 270 base::Unretained(this), make_scoped_refptr(buf), callback)); | 270 base::Unretained(this), make_scoped_refptr(buf), callback)); |
| 271 if (rv >= 0) | |
| 272 RecordFastOpenStatus(); | |
| 273 if (rv != ERR_IO_PENDING) | 271 if (rv != ERR_IO_PENDING) |
| 274 rv = HandleReadCompleted(buf, rv); | 272 rv = HandleReadCompleted(buf, rv); |
| 275 return rv; | 273 return rv; |
| 276 } | 274 } |
| 277 | 275 |
| 278 int TCPSocketLibevent::Write(IOBuffer* buf, | 276 int TCPSocketLibevent::Write(IOBuffer* buf, |
| 279 int buf_len, | 277 int buf_len, |
| 280 const CompletionCallback& callback) { | 278 const CompletionCallback& callback) { |
| 281 DCHECK(socket_); | 279 DCHECK(socket_); |
| 282 DCHECK(!callback.is_null()); | 280 DCHECK(!callback.is_null()); |
| 283 | 281 |
| 284 CompletionCallback write_callback = | 282 CompletionCallback write_callback = |
| 285 base::Bind(&TCPSocketLibevent::WriteCompleted, | 283 base::Bind(&TCPSocketLibevent::WriteCompleted, |
| 286 // Grab a reference to |buf| so that WriteCompleted() can still | 284 // Grab a reference to |buf| so that WriteCompleted() can still |
| 287 // use it when Write() completes, as otherwise, this transfers | 285 // use it when Write() completes, as otherwise, this transfers |
| 288 // ownership of buf to socket. | 286 // ownership of buf to socket. |
| 289 base::Unretained(this), make_scoped_refptr(buf), callback); | 287 base::Unretained(this), make_scoped_refptr(buf), callback); |
| 290 int rv; | 288 int rv; |
| 291 if (use_tcp_fastopen_ && !tcp_fastopen_connected_) { | 289 |
| 290 if (use_tcp_fastopen_ && !tcp_fastopen_write_attempted_) { | |
| 292 rv = TcpFastOpenWrite(buf, buf_len, write_callback); | 291 rv = TcpFastOpenWrite(buf, buf_len, write_callback); |
| 293 } else { | 292 } else { |
| 294 rv = socket_->Write(buf, buf_len, write_callback); | 293 rv = socket_->Write(buf, buf_len, write_callback); |
| 295 } | 294 } |
| 296 | 295 |
| 297 if (rv != ERR_IO_PENDING) | 296 if (rv != ERR_IO_PENDING) |
| 298 rv = HandleWriteCompleted(buf, rv); | 297 rv = HandleWriteCompleted(buf, rv); |
| 299 return rv; | 298 return rv; |
| 300 } | 299 } |
| 301 | 300 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 return SetTCPKeepAlive(socket_->socket_fd(), enable, delay); | 406 return SetTCPKeepAlive(socket_->socket_fd(), enable, delay); |
| 408 } | 407 } |
| 409 | 408 |
| 410 bool TCPSocketLibevent::SetNoDelay(bool no_delay) { | 409 bool TCPSocketLibevent::SetNoDelay(bool no_delay) { |
| 411 DCHECK(socket_); | 410 DCHECK(socket_); |
| 412 return SetTCPNoDelay(socket_->socket_fd(), no_delay); | 411 return SetTCPNoDelay(socket_->socket_fd(), no_delay); |
| 413 } | 412 } |
| 414 | 413 |
| 415 void TCPSocketLibevent::Close() { | 414 void TCPSocketLibevent::Close() { |
| 416 socket_.reset(); | 415 socket_.reset(); |
| 416 | |
| 417 // Record and reset TCP FastOpen state. | |
| 418 if (tcp_fastopen_write_attempted_ || | |
| 419 tcp_fastopen_status_ == TCP_FASTOPEN_PREVIOUSLY_FAILED) { | |
| 420 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection", | |
| 421 tcp_fastopen_status_, TCP_FASTOPEN_MAX_VALUE); | |
| 422 } | |
| 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 if (!IsTCPFastOpenSupported()) |
| 435 return; | |
| 436 | |
| 437 // Do not enable TCP FastOpen if it had previously failed. | |
| 438 // This check conservatively avoids middleboxes that may blackhole | |
| 439 // TCP FastOpen SYN+Data packets; on such a failure, subsequent sockets | |
| 440 // should not use TCP FastOpen. | |
| 441 if(!g_tcp_fastopen_has_failed) | |
| 427 use_tcp_fastopen_ = true; | 442 use_tcp_fastopen_ = true; |
| 443 else | |
| 444 tcp_fastopen_status_ = TCP_FASTOPEN_PREVIOUSLY_FAILED; | |
| 428 } | 445 } |
| 429 | 446 |
| 430 bool TCPSocketLibevent::IsValid() const { | 447 bool TCPSocketLibevent::IsValid() const { |
| 431 return socket_ != NULL && socket_->socket_fd() != kInvalidSocket; | 448 return socket_ != NULL && socket_->socket_fd() != kInvalidSocket; |
| 432 } | 449 } |
| 433 | 450 |
| 434 void TCPSocketLibevent::StartLoggingMultipleConnectAttempts( | 451 void TCPSocketLibevent::StartLoggingMultipleConnectAttempts( |
| 435 const AddressList& addresses) { | 452 const AddressList& addresses) { |
| 436 if (!logging_multiple_connect_attempts_) { | 453 if (!logging_multiple_connect_attempts_) { |
| 437 logging_multiple_connect_attempts_ = true; | 454 logging_multiple_connect_attempts_ = true; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 546 | 563 |
| 547 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, | 564 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, |
| 548 CreateNetLogSourceAddressCallback(storage.addr, | 565 CreateNetLogSourceAddressCallback(storage.addr, |
| 549 storage.addr_len)); | 566 storage.addr_len)); |
| 550 } | 567 } |
| 551 | 568 |
| 552 void TCPSocketLibevent::ReadCompleted(const scoped_refptr<IOBuffer>& buf, | 569 void TCPSocketLibevent::ReadCompleted(const scoped_refptr<IOBuffer>& buf, |
| 553 const CompletionCallback& callback, | 570 const CompletionCallback& callback, |
| 554 int rv) { | 571 int rv) { |
| 555 DCHECK_NE(ERR_IO_PENDING, rv); | 572 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)); | 573 callback.Run(HandleReadCompleted(buf.get(), rv)); |
| 561 } | 574 } |
| 562 | 575 |
| 563 int TCPSocketLibevent::HandleReadCompleted(IOBuffer* buf, int rv) { | 576 int TCPSocketLibevent::HandleReadCompleted(IOBuffer* buf, int rv) { |
| 577 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { | |
| 578 // A TCP FastOpen connect-with-write was attempted. This read was a | |
| 579 // subsequent read, which either succeeded or failed. If the read | |
| 580 // succeeded, the socket is considered connected via TCP FastOpen. | |
| 581 // If the read failed, TCP FastOpen is (conservatively) turned off for all | |
| 582 // subsequent connections. TCP FastOpen status is recorded in both cases. | |
| 583 // TODO (jri): This currently results in conservative behavior, where TCP | |
| 584 // FastOpen is turned off on _any_ error. Implement optimizations, | |
| 585 // such as turning off TCP FastOpen on more specific errors, and | |
| 586 // re-attempting TCP FastOpen after a certain amount of time has passed. | |
| 587 if (rv >= 0) | |
|
mmenke
2014/09/30 14:30:34
Is 0 a reliable indicator of success here?
Jana
2014/10/01 01:54:00
I think so. 0 indicates a close, not an error, whi
mmenke
2014/10/01 02:11:12
Right, my concern was just that a confused server
| |
| 588 tcp_fastopen_connected_ = true; | |
| 589 else | |
| 590 g_tcp_fastopen_has_failed = true; | |
| 591 UpdateTCPFastOpenStatusAfterRead(); | |
| 592 } | |
| 593 | |
| 564 if (rv < 0) { | 594 if (rv < 0) { |
| 565 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, | 595 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, |
| 566 CreateNetLogSocketErrorCallback(rv, errno)); | 596 CreateNetLogSocketErrorCallback(rv, errno)); |
| 567 return rv; | 597 return rv; |
| 568 } | 598 } |
| 569 | 599 |
| 570 base::StatsCounter read_bytes("tcp.read_bytes"); | 600 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 571 read_bytes.Add(rv); | 601 read_bytes.Add(rv); |
| 572 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, | 602 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv, |
| 573 buf->data()); | 603 buf->data()); |
| 574 return rv; | 604 return rv; |
| 575 } | 605 } |
| 576 | 606 |
| 577 void TCPSocketLibevent::WriteCompleted(const scoped_refptr<IOBuffer>& buf, | 607 void TCPSocketLibevent::WriteCompleted(const scoped_refptr<IOBuffer>& buf, |
| 578 const CompletionCallback& callback, | 608 const CompletionCallback& callback, |
| 579 int rv) const { | 609 int rv) { |
| 580 DCHECK_NE(ERR_IO_PENDING, rv); | 610 DCHECK_NE(ERR_IO_PENDING, rv); |
| 581 callback.Run(HandleWriteCompleted(buf.get(), rv)); | 611 callback.Run(HandleWriteCompleted(buf.get(), rv)); |
| 582 } | 612 } |
| 583 | 613 |
| 584 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) const { | 614 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) { |
| 585 if (rv < 0) { | 615 if (rv < 0) { |
| 616 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { | |
| 617 // TCP FastOpen connect-with-write was attempted, and the write failed | |
| 618 // for unknown reasons. Record status and (conservatively) turn off | |
| 619 // TCP FastOpen for all subsequent connections. | |
| 620 // TODO (jri): This currently results in conservative behavior, where TCP | |
| 621 // FastOpen is turned off on _any_ error. Implement optimizations, | |
| 622 // such as turning off TCP FastOpen on more specific errors, and | |
| 623 // re-attempting TCP FastOpen after a certain amount of time has passed. | |
| 624 tcp_fastopen_status_ = TCP_FASTOPEN_ERROR; | |
| 625 g_tcp_fastopen_has_failed = true; | |
| 626 } | |
| 586 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, | 627 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, |
| 587 CreateNetLogSocketErrorCallback(rv, errno)); | 628 CreateNetLogSocketErrorCallback(rv, errno)); |
| 588 return rv; | 629 return rv; |
| 589 } | 630 } |
| 590 | 631 |
| 591 base::StatsCounter write_bytes("tcp.write_bytes"); | 632 base::StatsCounter write_bytes("tcp.write_bytes"); |
| 592 write_bytes.Add(rv); | 633 write_bytes.Add(rv); |
| 593 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, | 634 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv, |
| 594 buf->data()); | 635 buf->data()); |
| 595 return rv; | 636 return rv; |
| 596 } | 637 } |
| 597 | 638 |
| 598 int TCPSocketLibevent::TcpFastOpenWrite( | 639 int TCPSocketLibevent::TcpFastOpenWrite( |
| 599 IOBuffer* buf, | 640 IOBuffer* buf, |
| 600 int buf_len, | 641 int buf_len, |
| 601 const CompletionCallback& callback) { | 642 const CompletionCallback& callback) { |
| 602 SockaddrStorage storage; | 643 SockaddrStorage storage; |
| 603 int rv = socket_->GetPeerAddress(&storage); | 644 int rv = socket_->GetPeerAddress(&storage); |
| 604 if (rv != OK) | 645 if (rv != OK) |
| 605 return rv; | 646 return rv; |
| 606 | 647 |
| 607 int flags = 0x20000000; // Magic flag to enable TCP_FASTOPEN. | 648 int flags = 0x20000000; // Magic flag to enable TCP_FASTOPEN. |
| 608 #if defined(OS_LINUX) | 649 #if defined(OS_LINUX) |
| 609 // sendto() will fail with EPIPE when the system doesn't support TCP Fast | 650 // sendto() will fail with EPIPE when the system doesn't implement TCP |
| 610 // Open. Theoretically that shouldn't happen since the caller should check | 651 // FastOpen, and with EOPNOTSUPP when the system implements TCP FastOpen |
| 611 // for system support on startup, but users may dynamically disable TCP Fast | 652 // but it is disabled. Theoretically these shouldn't happen |
| 612 // Open via sysctl. | 653 // since the caller should check for system support on startup, but |
| 654 // users may dynamically disable TCP FastOpen via sysctl. | |
| 613 flags |= MSG_NOSIGNAL; | 655 flags |= MSG_NOSIGNAL; |
| 614 #endif // defined(OS_LINUX) | 656 #endif // defined(OS_LINUX) |
| 615 rv = HANDLE_EINTR(sendto(socket_->socket_fd(), | 657 rv = HANDLE_EINTR(sendto(socket_->socket_fd(), |
| 616 buf->data(), | 658 buf->data(), |
| 617 buf_len, | 659 buf_len, |
| 618 flags, | 660 flags, |
| 619 storage.addr, | 661 storage.addr, |
| 620 storage.addr_len)); | 662 storage.addr_len)); |
| 621 tcp_fastopen_connected_ = true; | 663 tcp_fastopen_write_attempted_ = true; |
| 622 | 664 |
| 623 if (rv >= 0) { | 665 if (rv >= 0) { |
| 624 fast_open_status_ = FAST_OPEN_FAST_CONNECT_RETURN; | 666 tcp_fastopen_status_ = TCP_FASTOPEN_FAST_CONNECT_RETURN; |
| 625 return rv; | 667 return rv; |
| 626 } | 668 } |
| 627 | 669 |
| 628 DCHECK_NE(EPIPE, errno); | 670 DCHECK_NE(EPIPE, errno); |
| 629 | 671 |
| 630 // If errno == EINPROGRESS, that means the kernel didn't have a cookie | 672 // If errno == EINPROGRESS, that means the kernel didn't have a cookie |
| 631 // and would block. The kernel is internally doing a connect() though. | 673 // 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 | 674 // 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 | 675 // asynchronous cases. Note that the user buffer has not been copied to |
| 634 // kernel space. | 676 // kernel space. |
| 635 if (errno == EINPROGRESS) { | 677 if (errno == EINPROGRESS) { |
| 636 rv = ERR_IO_PENDING; | 678 rv = ERR_IO_PENDING; |
| 637 } else { | 679 } else { |
| 638 rv = MapSystemError(errno); | 680 rv = MapSystemError(errno); |
| 639 } | 681 } |
| 640 | 682 |
| 641 if (rv != ERR_IO_PENDING) { | 683 if (rv != ERR_IO_PENDING) { |
| 642 fast_open_status_ = FAST_OPEN_ERROR; | 684 // TCP FastOpen connect-with-write was attempted, and the write failed |
| 685 // since TCP FastOpen was not implemented or disabled in the OS. | |
| 686 // Record status and turn off TCP FastOpen for all subsequent connections. | |
| 687 // TODO (jri): This is almost certainly too conservative, since it blanket | |
| 688 // turns off TCP FastOpen on any write error. Two things need to be done | |
| 689 // here: (i) record a histogram of write errors; in particular, record | |
| 690 // occurrences of EOPNOTSUPP and EPIPE, and (ii) afterwards, consider | |
| 691 // turning off TCP FastOpen on more specific errors. | |
| 692 tcp_fastopen_status_ = TCP_FASTOPEN_ERROR; | |
| 693 g_tcp_fastopen_has_failed = true; | |
| 643 return rv; | 694 return rv; |
| 644 } | 695 } |
| 645 | 696 |
| 646 fast_open_status_ = FAST_OPEN_SLOW_CONNECT_RETURN; | 697 tcp_fastopen_status_ = TCP_FASTOPEN_SLOW_CONNECT_RETURN; |
| 647 return socket_->WaitForWrite(buf, buf_len, callback); | 698 return socket_->WaitForWrite(buf, buf_len, callback); |
| 648 } | 699 } |
| 649 | 700 |
| 650 void TCPSocketLibevent::RecordFastOpenStatus() { | 701 void TCPSocketLibevent::UpdateTCPFastOpenStatusAfterRead() { |
| 651 if (use_tcp_fastopen_ && | 702 if (!use_tcp_fastopen_ || |
| 652 (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN || | 703 (tcp_fastopen_status_ != TCP_FASTOPEN_FAST_CONNECT_RETURN && |
| 653 fast_open_status_ == FAST_OPEN_SLOW_CONNECT_RETURN)) { | 704 tcp_fastopen_status_ != TCP_FASTOPEN_SLOW_CONNECT_RETURN)) { |
|
mmenke
2014/09/30 14:30:34
I think it's a little weird that here we check tcp
Jana
2014/10/01 01:54:00
We don't need the fastopen status check here at al
mmenke
2014/10/01 02:11:12
Two read failures in a row could get us here...But
| |
| 654 DCHECK_NE(FAST_OPEN_STATUS_UNKNOWN, fast_open_status_); | 705 // Not using TCP FastOpen, or status has been finalized. |
| 655 bool getsockopt_success(false); | 706 return; |
| 656 bool server_acked_data(false); | 707 } |
| 708 DCHECK_NE(TCP_FASTOPEN_STATUS_UNKNOWN, tcp_fastopen_status_); | |
| 709 | |
| 710 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { | |
| 711 // TCP FastOpen connect-with-write was attempted, and failed. | |
| 712 tcp_fastopen_status_ = | |
| 713 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? | |
| 714 TCP_FASTOPEN_FAST_CONNECT_READ_FAILED : | |
| 715 TCP_FASTOPEN_SLOW_CONNECT_READ_FAILED); | |
| 716 return; | |
| 717 } | |
| 718 | |
| 719 bool getsockopt_success(false); | |
| 720 bool server_acked_data(false); | |
|
mmenke
2014/09/30 14:30:34
nit: While you're here, "=" is almost always used
Jana
2014/10/01 01:54:00
Done.
| |
| 657 #if defined(TCP_INFO) | 721 #if defined(TCP_INFO) |
| 658 // Probe to see the if the socket used TCP FastOpen. | 722 // Probe to see the if the socket used TCP FastOpen. |
| 659 tcp_info info; | 723 tcp_info info; |
| 660 socklen_t info_len = sizeof(tcp_info); | 724 socklen_t info_len = sizeof(tcp_info); |
| 661 getsockopt_success = | 725 getsockopt_success = getsockopt(socket_->socket_fd(), IPPROTO_TCP, TCP_INFO, |
| 662 getsockopt(socket_->socket_fd(), IPPROTO_TCP, TCP_INFO, | 726 &info, &info_len) == 0 && |
| 663 &info, &info_len) == 0 && | 727 info_len == sizeof(tcp_info); |
| 664 info_len == sizeof(tcp_info); | 728 server_acked_data = getsockopt_success && |
| 665 server_acked_data = getsockopt_success && | 729 (info.tcpi_options & TCPI_OPT_SYN_DATA); |
| 666 (info.tcpi_options & TCPI_OPT_SYN_DATA); | |
| 667 #endif | 730 #endif |
| 668 if (getsockopt_success) { | 731 |
| 669 if (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN) { | 732 if (getsockopt_success) { |
| 670 fast_open_status_ = (server_acked_data ? FAST_OPEN_SYN_DATA_ACK : | 733 if (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN) { |
| 671 FAST_OPEN_SYN_DATA_NACK); | 734 tcp_fastopen_status_ = (server_acked_data ? |
| 672 } else { | 735 TCP_FASTOPEN_SYN_DATA_ACK : |
| 673 fast_open_status_ = (server_acked_data ? FAST_OPEN_NO_SYN_DATA_ACK : | 736 TCP_FASTOPEN_SYN_DATA_NACK); |
| 674 FAST_OPEN_NO_SYN_DATA_NACK); | |
| 675 } | |
| 676 } else { | 737 } else { |
| 677 fast_open_status_ = (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN ? | 738 tcp_fastopen_status_ = (server_acked_data ? |
| 678 FAST_OPEN_SYN_DATA_FAILED : | 739 TCP_FASTOPEN_NO_SYN_DATA_ACK : |
| 679 FAST_OPEN_NO_SYN_DATA_FAILED); | 740 TCP_FASTOPEN_NO_SYN_DATA_NACK); |
| 680 } | 741 } |
| 742 } else { | |
| 743 tcp_fastopen_status_ = | |
| 744 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? | |
| 745 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED : | |
| 746 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED); | |
| 681 } | 747 } |
| 682 } | 748 } |
| 683 | 749 |
| 684 } // namespace net | 750 } // namespace net |
| OLD | NEW |