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
3 // found in the LICENSE file.
5 #include "net/socket/tcp_socket.h"
8 #include <netinet/tcp.h>
9 #include <sys/socket.h>
11 #include "base/bind.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/logging.h"
15 #include "base/metrics/histogram_macros.h"
16 #include "base/posix/eintr_wrapper.h"
17 #include "base/task_runner_util.h"
18 #include "base/threading/worker_pool.h"
19 #include "net/base/address_list.h"
20 #include "net/base/connection_type_histograms.h"
21 #include "net/base/io_buffer.h"
22 #include "net/base/ip_endpoint.h"
23 #include "net/base/net_errors.h"
24 #include "net/base/net_util.h"
25 #include "net/base/network_activity_monitor.h"
26 #include "net/base/network_change_notifier.h"
27 #include "net/socket/socket_libevent.h"
28 #include "net/socket/socket_net_log_params.h"
30 // If we don't have a definition for TCPI_OPT_SYN_DATA, create one.
31 #ifndef TCPI_OPT_SYN_DATA
32 #define TCPI_OPT_SYN_DATA 32
39 // True if OS supports TCP FastOpen.
40 bool g_tcp_fastopen_supported
= false;
41 // True if TCP FastOpen is user-enabled for all connections.
42 // TODO(jri): Change global variable to param in HttpNetworkSession::Params.
43 bool g_tcp_fastopen_user_enabled
= false;
44 // True if TCP FastOpen connect-with-write has failed at least once.
45 bool g_tcp_fastopen_has_failed
= false;
47 // SetTCPNoDelay turns on/off buffering in the kernel. By default, TCP sockets
48 // will wait up to 200ms for more data to complete a packet before transmitting.
49 // After calling this function, the kernel will not wait. See TCP_NODELAY in
51 bool SetTCPNoDelay(int fd
, bool no_delay
) {
52 int on
= no_delay
? 1 : 0;
53 int error
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
57 // SetTCPKeepAlive sets SO_KEEPALIVE.
58 bool SetTCPKeepAlive(int fd
, bool enable
, int delay
) {
59 // Enabling TCP keepalives is the same on all platforms.
60 int on
= enable
? 1 : 0;
61 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, &on
, sizeof(on
))) {
62 PLOG(ERROR
) << "Failed to set SO_KEEPALIVE on fd: " << fd
;
66 // If we disabled TCP keep alive, our work is done here.
70 #if defined(OS_LINUX) || defined(OS_ANDROID)
71 // Setting the keepalive interval varies by platform.
73 // Set seconds until first TCP keep alive.
74 if (setsockopt(fd
, SOL_TCP
, TCP_KEEPIDLE
, &delay
, sizeof(delay
))) {
75 PLOG(ERROR
) << "Failed to set TCP_KEEPIDLE on fd: " << fd
;
78 // Set seconds between TCP keep alives.
79 if (setsockopt(fd
, SOL_TCP
, TCP_KEEPINTVL
, &delay
, sizeof(delay
))) {
80 PLOG(ERROR
) << "Failed to set TCP_KEEPINTVL on fd: " << fd
;
83 #elif defined(OS_MACOSX) || defined(OS_IOS)
84 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPALIVE
, &delay
, sizeof(delay
))) {
85 PLOG(ERROR
) << "Failed to set TCP_KEEPALIVE on fd: " << fd
;
92 #if defined(OS_LINUX) || defined(OS_ANDROID)
93 // Checks if the kernel supports TCP FastOpen.
94 bool SystemSupportsTCPFastOpen() {
95 const base::FilePath::CharType kTCPFastOpenProcFilePath
[] =
96 "/proc/sys/net/ipv4/tcp_fastopen";
97 std::string system_supports_tcp_fastopen
;
98 if (!base::ReadFileToString(base::FilePath(kTCPFastOpenProcFilePath
),
99 &system_supports_tcp_fastopen
)) {
102 // The read from /proc should return '1' if TCP FastOpen is enabled in the OS.
103 if (system_supports_tcp_fastopen
.empty() ||
104 (system_supports_tcp_fastopen
[0] != '1')) {
110 void RegisterTCPFastOpenIntentAndSupport(bool user_enabled
,
111 bool system_supported
) {
112 g_tcp_fastopen_supported
= system_supported
;
113 g_tcp_fastopen_user_enabled
= user_enabled
;
117 #if defined(TCP_INFO)
118 bool GetTcpInfo(SocketDescriptor fd
, tcp_info
* info
) {
119 socklen_t info_len
= sizeof(tcp_info
);
120 return getsockopt(fd
, IPPROTO_TCP
, TCP_INFO
, info
, &info_len
) == 0 &&
121 info_len
== sizeof(tcp_info
);
123 #endif // defined(TCP_INFO)
127 //-----------------------------------------------------------------------------
129 bool IsTCPFastOpenSupported() {
130 return g_tcp_fastopen_supported
;
133 bool IsTCPFastOpenUserEnabled() {
134 return g_tcp_fastopen_user_enabled
;
137 // This is asynchronous because it needs to do file IO, and it isn't allowed to
138 // do that on the IO thread.
139 void CheckSupportAndMaybeEnableTCPFastOpen(bool user_enabled
) {
140 #if defined(OS_LINUX) || defined(OS_ANDROID)
141 base::PostTaskAndReplyWithResult(
142 base::WorkerPool::GetTaskRunner(/*task_is_slow=*/false).get(),
144 base::Bind(SystemSupportsTCPFastOpen
),
145 base::Bind(RegisterTCPFastOpenIntentAndSupport
, user_enabled
));
149 TCPSocketLibevent::TCPSocketLibevent(NetLog
* net_log
,
150 const NetLog::Source
& source
)
151 : use_tcp_fastopen_(false),
152 tcp_fastopen_write_attempted_(false),
153 tcp_fastopen_connected_(false),
154 tcp_fastopen_status_(TCP_FASTOPEN_STATUS_UNKNOWN
),
155 logging_multiple_connect_attempts_(false),
156 net_log_(BoundNetLog::Make(net_log
, NetLog::SOURCE_SOCKET
)) {
157 net_log_
.BeginEvent(NetLog::TYPE_SOCKET_ALIVE
,
158 source
.ToEventParametersCallback());
161 TCPSocketLibevent::~TCPSocketLibevent() {
162 net_log_
.EndEvent(NetLog::TYPE_SOCKET_ALIVE
);
166 int TCPSocketLibevent::Open(AddressFamily family
) {
168 socket_
.reset(new SocketLibevent
);
169 int rv
= socket_
->Open(ConvertAddressFamily(family
));
175 int TCPSocketLibevent::AdoptConnectedSocket(int socket_fd
,
176 const IPEndPoint
& peer_address
) {
179 SockaddrStorage storage
;
180 if (!peer_address
.ToSockAddr(storage
.addr
, &storage
.addr_len
) &&
181 // For backward compatibility, allows the empty address.
182 !(peer_address
== IPEndPoint())) {
183 return ERR_ADDRESS_INVALID
;
186 socket_
.reset(new SocketLibevent
);
187 int rv
= socket_
->AdoptConnectedSocket(socket_fd
, storage
);
193 int TCPSocketLibevent::Bind(const IPEndPoint
& address
) {
196 SockaddrStorage storage
;
197 if (!address
.ToSockAddr(storage
.addr
, &storage
.addr_len
))
198 return ERR_ADDRESS_INVALID
;
200 return socket_
->Bind(storage
);
203 int TCPSocketLibevent::Listen(int backlog
) {
205 return socket_
->Listen(backlog
);
208 int TCPSocketLibevent::Accept(scoped_ptr
<TCPSocketLibevent
>* tcp_socket
,
210 const CompletionCallback
& callback
) {
212 DCHECK(!callback
.is_null());
214 DCHECK(!accept_socket_
);
216 net_log_
.BeginEvent(NetLog::TYPE_TCP_ACCEPT
);
218 int rv
= socket_
->Accept(
220 base::Bind(&TCPSocketLibevent::AcceptCompleted
,
221 base::Unretained(this), tcp_socket
, address
, callback
));
222 if (rv
!= ERR_IO_PENDING
)
223 rv
= HandleAcceptCompleted(tcp_socket
, address
, rv
);
227 int TCPSocketLibevent::Connect(const IPEndPoint
& address
,
228 const CompletionCallback
& callback
) {
231 if (!logging_multiple_connect_attempts_
)
232 LogConnectBegin(AddressList(address
));
234 net_log_
.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
,
235 CreateNetLogIPEndPointCallback(&address
));
237 SockaddrStorage storage
;
238 if (!address
.ToSockAddr(storage
.addr
, &storage
.addr_len
))
239 return ERR_ADDRESS_INVALID
;
241 if (use_tcp_fastopen_
) {
242 // With TCP FastOpen, we pretend that the socket is connected.
243 DCHECK(!tcp_fastopen_write_attempted_
);
244 socket_
->SetPeerAddress(storage
);
248 int rv
= socket_
->Connect(storage
,
249 base::Bind(&TCPSocketLibevent::ConnectCompleted
,
250 base::Unretained(this), callback
));
251 if (rv
!= ERR_IO_PENDING
)
252 rv
= HandleConnectCompleted(rv
);
256 bool TCPSocketLibevent::IsConnected() const {
260 if (use_tcp_fastopen_
&& !tcp_fastopen_write_attempted_
&&
261 socket_
->HasPeerAddress()) {
262 // With TCP FastOpen, we pretend that the socket is connected.
263 // This allows GetPeerAddress() to return peer_address_.
267 return socket_
->IsConnected();
270 bool TCPSocketLibevent::IsConnectedAndIdle() const {
271 // TODO(wtc): should we also handle the TCP FastOpen case here,
272 // as we do in IsConnected()?
273 return socket_
&& socket_
->IsConnectedAndIdle();
276 int TCPSocketLibevent::Read(IOBuffer
* buf
,
278 const CompletionCallback
& callback
) {
280 DCHECK(!callback
.is_null());
282 int rv
= socket_
->Read(
284 base::Bind(&TCPSocketLibevent::ReadCompleted
,
285 // Grab a reference to |buf| so that ReadCompleted() can still
286 // use it when Read() completes, as otherwise, this transfers
287 // ownership of buf to socket.
288 base::Unretained(this), make_scoped_refptr(buf
), callback
));
289 if (rv
!= ERR_IO_PENDING
)
290 rv
= HandleReadCompleted(buf
, rv
);
294 int TCPSocketLibevent::Write(IOBuffer
* buf
,
296 const CompletionCallback
& callback
) {
298 DCHECK(!callback
.is_null());
300 CompletionCallback write_callback
=
301 base::Bind(&TCPSocketLibevent::WriteCompleted
,
302 // Grab a reference to |buf| so that WriteCompleted() can still
303 // use it when Write() completes, as otherwise, this transfers
304 // ownership of buf to socket.
305 base::Unretained(this), make_scoped_refptr(buf
), callback
);
308 if (use_tcp_fastopen_
&& !tcp_fastopen_write_attempted_
) {
309 rv
= TcpFastOpenWrite(buf
, buf_len
, write_callback
);
311 rv
= socket_
->Write(buf
, buf_len
, write_callback
);
314 if (rv
!= ERR_IO_PENDING
)
315 rv
= HandleWriteCompleted(buf
, rv
);
319 int TCPSocketLibevent::GetLocalAddress(IPEndPoint
* address
) const {
323 return ERR_SOCKET_NOT_CONNECTED
;
325 SockaddrStorage storage
;
326 int rv
= socket_
->GetLocalAddress(&storage
);
330 if (!address
->FromSockAddr(storage
.addr
, storage
.addr_len
))
331 return ERR_ADDRESS_INVALID
;
336 int TCPSocketLibevent::GetPeerAddress(IPEndPoint
* address
) const {
340 return ERR_SOCKET_NOT_CONNECTED
;
342 SockaddrStorage storage
;
343 int rv
= socket_
->GetPeerAddress(&storage
);
347 if (!address
->FromSockAddr(storage
.addr
, storage
.addr_len
))
348 return ERR_ADDRESS_INVALID
;
353 int TCPSocketLibevent::SetDefaultOptionsForServer() {
355 return SetAddressReuse(true);
358 void TCPSocketLibevent::SetDefaultOptionsForClient() {
361 // This mirrors the behaviour on Windows. See the comment in
362 // tcp_socket_win.cc after searching for "NODELAY".
363 // If SetTCPNoDelay fails, we don't care.
364 SetTCPNoDelay(socket_
->socket_fd(), true);
366 // TCP keep alive wakes up the radio, which is expensive on mobile. Do not
367 // enable it there. It's useful to prevent TCP middleboxes from timing out
368 // connection mappings. Packets for timed out connection mappings at
369 // middleboxes will either lead to:
370 // a) Middleboxes sending TCP RSTs. It's up to higher layers to check for this
371 // and retry. The HTTP network transaction code does this.
372 // b) Middleboxes just drop the unrecognized TCP packet. This leads to the TCP
373 // stack retransmitting packets per TCP stack retransmission timeouts, which
374 // are very high (on the order of seconds). Given the number of
375 // retransmissions required before killing the connection, this can lead to
376 // tens of seconds or even minutes of delay, depending on OS.
377 #if !defined(OS_ANDROID) && !defined(OS_IOS)
378 const int kTCPKeepAliveSeconds
= 45;
380 SetTCPKeepAlive(socket_
->socket_fd(), true, kTCPKeepAliveSeconds
);
384 int TCPSocketLibevent::SetAddressReuse(bool allow
) {
387 // SO_REUSEADDR is useful for server sockets to bind to a recently unbound
388 // port. When a socket is closed, the end point changes its state to TIME_WAIT
389 // and wait for 2 MSL (maximum segment lifetime) to ensure the remote peer
390 // acknowledges its closure. For server sockets, it is usually safe to
391 // bind to a TIME_WAIT end point immediately, which is a widely adopted
394 // Note that on *nix, SO_REUSEADDR does not enable the TCP socket to bind to
395 // an end point that is already bound by another socket. To do that one must
396 // set SO_REUSEPORT instead. This option is not provided on Linux prior
399 // SO_REUSEPORT is provided in MacOS X and iOS.
400 int boolean_value
= allow
? 1 : 0;
401 int rv
= setsockopt(socket_
->socket_fd(), SOL_SOCKET
, SO_REUSEADDR
,
402 &boolean_value
, sizeof(boolean_value
));
404 return MapSystemError(errno
);
408 int TCPSocketLibevent::SetReceiveBufferSize(int32 size
) {
410 int rv
= setsockopt(socket_
->socket_fd(), SOL_SOCKET
, SO_RCVBUF
,
411 reinterpret_cast<const char*>(&size
), sizeof(size
));
412 return (rv
== 0) ? OK
: MapSystemError(errno
);
415 int TCPSocketLibevent::SetSendBufferSize(int32 size
) {
417 int rv
= setsockopt(socket_
->socket_fd(), SOL_SOCKET
, SO_SNDBUF
,
418 reinterpret_cast<const char*>(&size
), sizeof(size
));
419 return (rv
== 0) ? OK
: MapSystemError(errno
);
422 bool TCPSocketLibevent::SetKeepAlive(bool enable
, int delay
) {
424 return SetTCPKeepAlive(socket_
->socket_fd(), enable
, delay
);
427 bool TCPSocketLibevent::SetNoDelay(bool no_delay
) {
429 return SetTCPNoDelay(socket_
->socket_fd(), no_delay
);
432 void TCPSocketLibevent::Close() {
435 // Record and reset TCP FastOpen state.
436 if (tcp_fastopen_write_attempted_
||
437 tcp_fastopen_status_
== TCP_FASTOPEN_PREVIOUSLY_FAILED
) {
438 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection",
439 tcp_fastopen_status_
, TCP_FASTOPEN_MAX_VALUE
);
441 use_tcp_fastopen_
= false;
442 tcp_fastopen_connected_
= false;
443 tcp_fastopen_write_attempted_
= false;
444 tcp_fastopen_status_
= TCP_FASTOPEN_STATUS_UNKNOWN
;
447 bool TCPSocketLibevent::UsingTCPFastOpen() const {
448 return use_tcp_fastopen_
;
451 void TCPSocketLibevent::EnableTCPFastOpenIfSupported() {
452 if (!IsTCPFastOpenSupported())
455 // Do not enable TCP FastOpen if it had previously failed.
456 // This check conservatively avoids middleboxes that may blackhole
457 // TCP FastOpen SYN+Data packets; on such a failure, subsequent sockets
458 // should not use TCP FastOpen.
459 if(!g_tcp_fastopen_has_failed
)
460 use_tcp_fastopen_
= true;
462 tcp_fastopen_status_
= TCP_FASTOPEN_PREVIOUSLY_FAILED
;
465 bool TCPSocketLibevent::IsValid() const {
466 return socket_
!= NULL
&& socket_
->socket_fd() != kInvalidSocket
;
469 void TCPSocketLibevent::StartLoggingMultipleConnectAttempts(
470 const AddressList
& addresses
) {
471 if (!logging_multiple_connect_attempts_
) {
472 logging_multiple_connect_attempts_
= true;
473 LogConnectBegin(addresses
);
479 void TCPSocketLibevent::EndLoggingMultipleConnectAttempts(int net_error
) {
480 if (logging_multiple_connect_attempts_
) {
481 LogConnectEnd(net_error
);
482 logging_multiple_connect_attempts_
= false;
488 void TCPSocketLibevent::AcceptCompleted(
489 scoped_ptr
<TCPSocketLibevent
>* tcp_socket
,
491 const CompletionCallback
& callback
,
493 DCHECK_NE(ERR_IO_PENDING
, rv
);
494 callback
.Run(HandleAcceptCompleted(tcp_socket
, address
, rv
));
497 int TCPSocketLibevent::HandleAcceptCompleted(
498 scoped_ptr
<TCPSocketLibevent
>* tcp_socket
,
502 rv
= BuildTcpSocketLibevent(tcp_socket
, address
);
505 net_log_
.EndEvent(NetLog::TYPE_TCP_ACCEPT
,
506 CreateNetLogIPEndPointCallback(address
));
508 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT
, rv
);
514 int TCPSocketLibevent::BuildTcpSocketLibevent(
515 scoped_ptr
<TCPSocketLibevent
>* tcp_socket
,
516 IPEndPoint
* address
) {
517 DCHECK(accept_socket_
);
519 SockaddrStorage storage
;
520 if (accept_socket_
->GetPeerAddress(&storage
) != OK
||
521 !address
->FromSockAddr(storage
.addr
, storage
.addr_len
)) {
522 accept_socket_
.reset();
523 return ERR_ADDRESS_INVALID
;
526 tcp_socket
->reset(new TCPSocketLibevent(net_log_
.net_log(),
528 (*tcp_socket
)->socket_
.reset(accept_socket_
.release());
532 void TCPSocketLibevent::ConnectCompleted(const CompletionCallback
& callback
,
534 DCHECK_NE(ERR_IO_PENDING
, rv
);
535 callback
.Run(HandleConnectCompleted(rv
));
538 int TCPSocketLibevent::HandleConnectCompleted(int rv
) const {
539 // Log the end of this attempt (and any OS error it threw).
541 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
,
542 NetLog::IntegerCallback("os_error", errno
));
544 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
);
547 // Give a more specific error when the user is offline.
548 if (rv
== ERR_ADDRESS_UNREACHABLE
&& NetworkChangeNotifier::IsOffline())
549 rv
= ERR_INTERNET_DISCONNECTED
;
551 if (!logging_multiple_connect_attempts_
)
557 void TCPSocketLibevent::LogConnectBegin(const AddressList
& addresses
) const {
558 net_log_
.BeginEvent(NetLog::TYPE_TCP_CONNECT
,
559 addresses
.CreateNetLogCallback());
562 void TCPSocketLibevent::LogConnectEnd(int net_error
) const {
563 if (net_error
!= OK
) {
564 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT
, net_error
);
568 UpdateConnectionTypeHistograms(CONNECTION_ANY
);
570 SockaddrStorage storage
;
571 int rv
= socket_
->GetLocalAddress(&storage
);
573 PLOG(ERROR
) << "GetLocalAddress() [rv: " << rv
<< "] error: ";
575 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT
, rv
);
579 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT
,
580 CreateNetLogSourceAddressCallback(storage
.addr
,
584 void TCPSocketLibevent::ReadCompleted(const scoped_refptr
<IOBuffer
>& buf
,
585 const CompletionCallback
& callback
,
587 DCHECK_NE(ERR_IO_PENDING
, rv
);
588 callback
.Run(HandleReadCompleted(buf
.get(), rv
));
591 int TCPSocketLibevent::HandleReadCompleted(IOBuffer
* buf
, int rv
) {
592 if (tcp_fastopen_write_attempted_
&& !tcp_fastopen_connected_
) {
593 // A TCP FastOpen connect-with-write was attempted. This read was a
594 // subsequent read, which either succeeded or failed. If the read
595 // succeeded, the socket is considered connected via TCP FastOpen.
596 // If the read failed, TCP FastOpen is (conservatively) turned off for all
597 // subsequent connections. TCP FastOpen status is recorded in both cases.
598 // TODO (jri): This currently results in conservative behavior, where TCP
599 // FastOpen is turned off on _any_ error. Implement optimizations,
600 // such as turning off TCP FastOpen on more specific errors, and
601 // re-attempting TCP FastOpen after a certain amount of time has passed.
603 tcp_fastopen_connected_
= true;
605 g_tcp_fastopen_has_failed
= true;
606 UpdateTCPFastOpenStatusAfterRead();
610 net_log_
.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR
,
611 CreateNetLogSocketErrorCallback(rv
, errno
));
614 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED
, rv
,
616 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv
);
621 void TCPSocketLibevent::WriteCompleted(const scoped_refptr
<IOBuffer
>& buf
,
622 const CompletionCallback
& callback
,
624 DCHECK_NE(ERR_IO_PENDING
, rv
);
625 callback
.Run(HandleWriteCompleted(buf
.get(), rv
));
628 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer
* buf
, int rv
) {
630 if (tcp_fastopen_write_attempted_
&& !tcp_fastopen_connected_
) {
631 // TCP FastOpen connect-with-write was attempted, and the write failed
632 // for unknown reasons. Record status and (conservatively) turn off
633 // TCP FastOpen for all subsequent connections.
634 // TODO (jri): This currently results in conservative behavior, where TCP
635 // FastOpen is turned off on _any_ error. Implement optimizations,
636 // such as turning off TCP FastOpen on more specific errors, and
637 // re-attempting TCP FastOpen after a certain amount of time has passed.
638 tcp_fastopen_status_
= TCP_FASTOPEN_ERROR
;
639 g_tcp_fastopen_has_failed
= true;
641 net_log_
.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR
,
642 CreateNetLogSocketErrorCallback(rv
, errno
));
645 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT
, rv
,
647 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(rv
);
651 int TCPSocketLibevent::TcpFastOpenWrite(
654 const CompletionCallback
& callback
) {
655 SockaddrStorage storage
;
656 int rv
= socket_
->GetPeerAddress(&storage
);
660 int flags
= 0x20000000; // Magic flag to enable TCP_FASTOPEN.
661 #if defined(OS_LINUX) || defined(OS_ANDROID)
662 // sendto() will fail with EPIPE when the system doesn't implement TCP
663 // FastOpen, and with EOPNOTSUPP when the system implements TCP FastOpen
664 // but it is disabled. Theoretically these shouldn't happen
665 // since the caller should check for system support on startup, but
666 // users may dynamically disable TCP FastOpen via sysctl.
667 flags
|= MSG_NOSIGNAL
;
668 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
669 rv
= HANDLE_EINTR(sendto(socket_
->socket_fd(),
675 tcp_fastopen_write_attempted_
= true;
678 tcp_fastopen_status_
= TCP_FASTOPEN_FAST_CONNECT_RETURN
;
682 DCHECK_NE(EPIPE
, errno
);
684 // If errno == EINPROGRESS, that means the kernel didn't have a cookie
685 // and would block. The kernel is internally doing a connect() though.
686 // Remap EINPROGRESS to EAGAIN so we treat this the same as our other
687 // asynchronous cases. Note that the user buffer has not been copied to
689 if (errno
== EINPROGRESS
) {
692 rv
= MapSystemError(errno
);
695 if (rv
!= ERR_IO_PENDING
) {
696 // TCP FastOpen connect-with-write was attempted, and the write failed
697 // since TCP FastOpen was not implemented or disabled in the OS.
698 // Record status and turn off TCP FastOpen for all subsequent connections.
699 // TODO (jri): This is almost certainly too conservative, since it blanket
700 // turns off TCP FastOpen on any write error. Two things need to be done
701 // here: (i) record a histogram of write errors; in particular, record
702 // occurrences of EOPNOTSUPP and EPIPE, and (ii) afterwards, consider
703 // turning off TCP FastOpen on more specific errors.
704 tcp_fastopen_status_
= TCP_FASTOPEN_ERROR
;
705 g_tcp_fastopen_has_failed
= true;
709 tcp_fastopen_status_
= TCP_FASTOPEN_SLOW_CONNECT_RETURN
;
710 return socket_
->WaitForWrite(buf
, buf_len
, callback
);
713 void TCPSocketLibevent::UpdateTCPFastOpenStatusAfterRead() {
714 DCHECK(tcp_fastopen_status_
== TCP_FASTOPEN_FAST_CONNECT_RETURN
||
715 tcp_fastopen_status_
== TCP_FASTOPEN_SLOW_CONNECT_RETURN
);
717 if (tcp_fastopen_write_attempted_
&& !tcp_fastopen_connected_
) {
718 // TCP FastOpen connect-with-write was attempted, and failed.
719 tcp_fastopen_status_
=
720 (tcp_fastopen_status_
== TCP_FASTOPEN_FAST_CONNECT_RETURN
?
721 TCP_FASTOPEN_FAST_CONNECT_READ_FAILED
:
722 TCP_FASTOPEN_SLOW_CONNECT_READ_FAILED
);
726 bool getsockopt_success
= false;
727 bool server_acked_data
= false;
728 #if defined(TCP_INFO)
729 // Probe to see the if the socket used TCP FastOpen.
731 getsockopt_success
= GetTcpInfo(socket_
->socket_fd(), &info
);
733 getsockopt_success
&& (info
.tcpi_options
& TCPI_OPT_SYN_DATA
);
734 #endif // defined(TCP_INFO)
736 if (getsockopt_success
) {
737 if (tcp_fastopen_status_
== TCP_FASTOPEN_FAST_CONNECT_RETURN
) {
738 tcp_fastopen_status_
= (server_acked_data
?
739 TCP_FASTOPEN_SYN_DATA_ACK
:
740 TCP_FASTOPEN_SYN_DATA_NACK
);
742 tcp_fastopen_status_
= (server_acked_data
?
743 TCP_FASTOPEN_NO_SYN_DATA_ACK
:
744 TCP_FASTOPEN_NO_SYN_DATA_NACK
);
747 tcp_fastopen_status_
=
748 (tcp_fastopen_status_
== TCP_FASTOPEN_FAST_CONNECT_RETURN
?
749 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED
:
750 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED
);
754 bool TCPSocketLibevent::GetEstimatedRoundTripTime(
755 base::TimeDelta
* out_rtt
) const {
760 #if defined(TCP_INFO)
762 if (GetTcpInfo(socket_
->socket_fd(), &info
)) {
763 // tcpi_rtt is zero when the kernel doesn't have an RTT estimate,
764 // and possibly in other cases such as connections to localhost.
765 if (info
.tcpi_rtt
> 0) {
766 *out_rtt
= base::TimeDelta::FromMicroseconds(info
.tcpi_rtt
);
770 #endif // defined(TCP_INFO)