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.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
;
119 //-----------------------------------------------------------------------------
121 bool IsTCPFastOpenSupported() {
122 return g_tcp_fastopen_supported
;
125 bool IsTCPFastOpenUserEnabled() {
126 return g_tcp_fastopen_user_enabled
;
129 // This is asynchronous because it needs to do file IO, and it isn't allowed to
130 // do that on the IO thread.
131 void CheckSupportAndMaybeEnableTCPFastOpen(bool user_enabled
) {
132 #if defined(OS_LINUX) || defined(OS_ANDROID)
133 base::PostTaskAndReplyWithResult(
134 base::WorkerPool::GetTaskRunner(/*task_is_slow=*/false).get(),
136 base::Bind(SystemSupportsTCPFastOpen
),
137 base::Bind(RegisterTCPFastOpenIntentAndSupport
, user_enabled
));
141 TCPSocketLibevent::TCPSocketLibevent(NetLog
* net_log
,
142 const NetLog::Source
& source
)
143 : use_tcp_fastopen_(false),
144 tcp_fastopen_write_attempted_(false),
145 tcp_fastopen_connected_(false),
146 tcp_fastopen_status_(TCP_FASTOPEN_STATUS_UNKNOWN
),
147 logging_multiple_connect_attempts_(false),
148 net_log_(BoundNetLog::Make(net_log
, NetLog::SOURCE_SOCKET
)) {
149 net_log_
.BeginEvent(NetLog::TYPE_SOCKET_ALIVE
,
150 source
.ToEventParametersCallback());
153 TCPSocketLibevent::~TCPSocketLibevent() {
154 net_log_
.EndEvent(NetLog::TYPE_SOCKET_ALIVE
);
158 int TCPSocketLibevent::Open(AddressFamily family
) {
160 socket_
.reset(new SocketLibevent
);
161 int rv
= socket_
->Open(ConvertAddressFamily(family
));
167 int TCPSocketLibevent::AdoptConnectedSocket(int socket_fd
,
168 const IPEndPoint
& peer_address
) {
171 SockaddrStorage storage
;
172 if (!peer_address
.ToSockAddr(storage
.addr
, &storage
.addr_len
) &&
173 // For backward compatibility, allows the empty address.
174 !(peer_address
== IPEndPoint())) {
175 return ERR_ADDRESS_INVALID
;
178 socket_
.reset(new SocketLibevent
);
179 int rv
= socket_
->AdoptConnectedSocket(socket_fd
, storage
);
185 int TCPSocketLibevent::Bind(const IPEndPoint
& address
) {
188 SockaddrStorage storage
;
189 if (!address
.ToSockAddr(storage
.addr
, &storage
.addr_len
))
190 return ERR_ADDRESS_INVALID
;
192 return socket_
->Bind(storage
);
195 int TCPSocketLibevent::Listen(int backlog
) {
197 return socket_
->Listen(backlog
);
200 int TCPSocketLibevent::Accept(scoped_ptr
<TCPSocketLibevent
>* tcp_socket
,
202 const CompletionCallback
& callback
) {
204 DCHECK(!callback
.is_null());
206 DCHECK(!accept_socket_
);
208 net_log_
.BeginEvent(NetLog::TYPE_TCP_ACCEPT
);
210 int rv
= socket_
->Accept(
212 base::Bind(&TCPSocketLibevent::AcceptCompleted
,
213 base::Unretained(this), tcp_socket
, address
, callback
));
214 if (rv
!= ERR_IO_PENDING
)
215 rv
= HandleAcceptCompleted(tcp_socket
, address
, rv
);
219 int TCPSocketLibevent::Connect(const IPEndPoint
& address
,
220 const CompletionCallback
& callback
) {
223 if (!logging_multiple_connect_attempts_
)
224 LogConnectBegin(AddressList(address
));
226 net_log_
.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
,
227 CreateNetLogIPEndPointCallback(&address
));
229 SockaddrStorage storage
;
230 if (!address
.ToSockAddr(storage
.addr
, &storage
.addr_len
))
231 return ERR_ADDRESS_INVALID
;
233 if (use_tcp_fastopen_
) {
234 // With TCP FastOpen, we pretend that the socket is connected.
235 DCHECK(!tcp_fastopen_write_attempted_
);
236 socket_
->SetPeerAddress(storage
);
240 int rv
= socket_
->Connect(storage
,
241 base::Bind(&TCPSocketLibevent::ConnectCompleted
,
242 base::Unretained(this), callback
));
243 if (rv
!= ERR_IO_PENDING
)
244 rv
= HandleConnectCompleted(rv
);
248 bool TCPSocketLibevent::IsConnected() const {
252 if (use_tcp_fastopen_
&& !tcp_fastopen_write_attempted_
&&
253 socket_
->HasPeerAddress()) {
254 // With TCP FastOpen, we pretend that the socket is connected.
255 // This allows GetPeerAddress() to return peer_address_.
259 return socket_
->IsConnected();
262 bool TCPSocketLibevent::IsConnectedAndIdle() const {
263 // TODO(wtc): should we also handle the TCP FastOpen case here,
264 // as we do in IsConnected()?
265 return socket_
&& socket_
->IsConnectedAndIdle();
268 int TCPSocketLibevent::Read(IOBuffer
* buf
,
270 const CompletionCallback
& callback
) {
272 DCHECK(!callback
.is_null());
274 int rv
= socket_
->Read(
276 base::Bind(&TCPSocketLibevent::ReadCompleted
,
277 // Grab a reference to |buf| so that ReadCompleted() can still
278 // use it when Read() completes, as otherwise, this transfers
279 // ownership of buf to socket.
280 base::Unretained(this), make_scoped_refptr(buf
), callback
));
281 if (rv
!= ERR_IO_PENDING
)
282 rv
= HandleReadCompleted(buf
, rv
);
286 int TCPSocketLibevent::Write(IOBuffer
* buf
,
288 const CompletionCallback
& callback
) {
290 DCHECK(!callback
.is_null());
292 CompletionCallback write_callback
=
293 base::Bind(&TCPSocketLibevent::WriteCompleted
,
294 // Grab a reference to |buf| so that WriteCompleted() can still
295 // use it when Write() completes, as otherwise, this transfers
296 // ownership of buf to socket.
297 base::Unretained(this), make_scoped_refptr(buf
), callback
);
300 if (use_tcp_fastopen_
&& !tcp_fastopen_write_attempted_
) {
301 rv
= TcpFastOpenWrite(buf
, buf_len
, write_callback
);
303 rv
= socket_
->Write(buf
, buf_len
, write_callback
);
306 if (rv
!= ERR_IO_PENDING
)
307 rv
= HandleWriteCompleted(buf
, rv
);
311 int TCPSocketLibevent::GetLocalAddress(IPEndPoint
* address
) const {
315 return ERR_SOCKET_NOT_CONNECTED
;
317 SockaddrStorage storage
;
318 int rv
= socket_
->GetLocalAddress(&storage
);
322 if (!address
->FromSockAddr(storage
.addr
, storage
.addr_len
))
323 return ERR_ADDRESS_INVALID
;
328 int TCPSocketLibevent::GetPeerAddress(IPEndPoint
* address
) const {
332 return ERR_SOCKET_NOT_CONNECTED
;
334 SockaddrStorage storage
;
335 int rv
= socket_
->GetPeerAddress(&storage
);
339 if (!address
->FromSockAddr(storage
.addr
, storage
.addr_len
))
340 return ERR_ADDRESS_INVALID
;
345 int TCPSocketLibevent::SetDefaultOptionsForServer() {
347 return SetAddressReuse(true);
350 void TCPSocketLibevent::SetDefaultOptionsForClient() {
353 // This mirrors the behaviour on Windows. See the comment in
354 // tcp_socket_win.cc after searching for "NODELAY".
355 // If SetTCPNoDelay fails, we don't care.
356 SetTCPNoDelay(socket_
->socket_fd(), true);
358 // TCP keep alive wakes up the radio, which is expensive on mobile. Do not
359 // enable it there. It's useful to prevent TCP middleboxes from timing out
360 // connection mappings. Packets for timed out connection mappings at
361 // middleboxes will either lead to:
362 // a) Middleboxes sending TCP RSTs. It's up to higher layers to check for this
363 // and retry. The HTTP network transaction code does this.
364 // b) Middleboxes just drop the unrecognized TCP packet. This leads to the TCP
365 // stack retransmitting packets per TCP stack retransmission timeouts, which
366 // are very high (on the order of seconds). Given the number of
367 // retransmissions required before killing the connection, this can lead to
368 // tens of seconds or even minutes of delay, depending on OS.
369 #if !defined(OS_ANDROID) && !defined(OS_IOS)
370 const int kTCPKeepAliveSeconds
= 45;
372 SetTCPKeepAlive(socket_
->socket_fd(), true, kTCPKeepAliveSeconds
);
376 int TCPSocketLibevent::SetAddressReuse(bool allow
) {
379 // SO_REUSEADDR is useful for server sockets to bind to a recently unbound
380 // port. When a socket is closed, the end point changes its state to TIME_WAIT
381 // and wait for 2 MSL (maximum segment lifetime) to ensure the remote peer
382 // acknowledges its closure. For server sockets, it is usually safe to
383 // bind to a TIME_WAIT end point immediately, which is a widely adopted
386 // Note that on *nix, SO_REUSEADDR does not enable the TCP socket to bind to
387 // an end point that is already bound by another socket. To do that one must
388 // set SO_REUSEPORT instead. This option is not provided on Linux prior
391 // SO_REUSEPORT is provided in MacOS X and iOS.
392 int boolean_value
= allow
? 1 : 0;
393 int rv
= setsockopt(socket_
->socket_fd(), SOL_SOCKET
, SO_REUSEADDR
,
394 &boolean_value
, sizeof(boolean_value
));
396 return MapSystemError(errno
);
400 int TCPSocketLibevent::SetReceiveBufferSize(int32 size
) {
402 int rv
= setsockopt(socket_
->socket_fd(), SOL_SOCKET
, SO_RCVBUF
,
403 reinterpret_cast<const char*>(&size
), sizeof(size
));
404 return (rv
== 0) ? OK
: MapSystemError(errno
);
407 int TCPSocketLibevent::SetSendBufferSize(int32 size
) {
409 int rv
= setsockopt(socket_
->socket_fd(), SOL_SOCKET
, SO_SNDBUF
,
410 reinterpret_cast<const char*>(&size
), sizeof(size
));
411 return (rv
== 0) ? OK
: MapSystemError(errno
);
414 bool TCPSocketLibevent::SetKeepAlive(bool enable
, int delay
) {
416 return SetTCPKeepAlive(socket_
->socket_fd(), enable
, delay
);
419 bool TCPSocketLibevent::SetNoDelay(bool no_delay
) {
421 return SetTCPNoDelay(socket_
->socket_fd(), no_delay
);
424 void TCPSocketLibevent::Close() {
427 // Record and reset TCP FastOpen state.
428 if (tcp_fastopen_write_attempted_
||
429 tcp_fastopen_status_
== TCP_FASTOPEN_PREVIOUSLY_FAILED
) {
430 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection",
431 tcp_fastopen_status_
, TCP_FASTOPEN_MAX_VALUE
);
433 use_tcp_fastopen_
= false;
434 tcp_fastopen_connected_
= false;
435 tcp_fastopen_write_attempted_
= false;
436 tcp_fastopen_status_
= TCP_FASTOPEN_STATUS_UNKNOWN
;
439 bool TCPSocketLibevent::UsingTCPFastOpen() const {
440 return use_tcp_fastopen_
;
443 void TCPSocketLibevent::EnableTCPFastOpenIfSupported() {
444 if (!IsTCPFastOpenSupported())
447 // Do not enable TCP FastOpen if it had previously failed.
448 // This check conservatively avoids middleboxes that may blackhole
449 // TCP FastOpen SYN+Data packets; on such a failure, subsequent sockets
450 // should not use TCP FastOpen.
451 if(!g_tcp_fastopen_has_failed
)
452 use_tcp_fastopen_
= true;
454 tcp_fastopen_status_
= TCP_FASTOPEN_PREVIOUSLY_FAILED
;
457 bool TCPSocketLibevent::IsValid() const {
458 return socket_
!= NULL
&& socket_
->socket_fd() != kInvalidSocket
;
461 void TCPSocketLibevent::StartLoggingMultipleConnectAttempts(
462 const AddressList
& addresses
) {
463 if (!logging_multiple_connect_attempts_
) {
464 logging_multiple_connect_attempts_
= true;
465 LogConnectBegin(addresses
);
471 void TCPSocketLibevent::EndLoggingMultipleConnectAttempts(int net_error
) {
472 if (logging_multiple_connect_attempts_
) {
473 LogConnectEnd(net_error
);
474 logging_multiple_connect_attempts_
= false;
480 void TCPSocketLibevent::AcceptCompleted(
481 scoped_ptr
<TCPSocketLibevent
>* tcp_socket
,
483 const CompletionCallback
& callback
,
485 DCHECK_NE(ERR_IO_PENDING
, rv
);
486 callback
.Run(HandleAcceptCompleted(tcp_socket
, address
, rv
));
489 int TCPSocketLibevent::HandleAcceptCompleted(
490 scoped_ptr
<TCPSocketLibevent
>* tcp_socket
,
494 rv
= BuildTcpSocketLibevent(tcp_socket
, address
);
497 net_log_
.EndEvent(NetLog::TYPE_TCP_ACCEPT
,
498 CreateNetLogIPEndPointCallback(address
));
500 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT
, rv
);
506 int TCPSocketLibevent::BuildTcpSocketLibevent(
507 scoped_ptr
<TCPSocketLibevent
>* tcp_socket
,
508 IPEndPoint
* address
) {
509 DCHECK(accept_socket_
);
511 SockaddrStorage storage
;
512 if (accept_socket_
->GetPeerAddress(&storage
) != OK
||
513 !address
->FromSockAddr(storage
.addr
, storage
.addr_len
)) {
514 accept_socket_
.reset();
515 return ERR_ADDRESS_INVALID
;
518 tcp_socket
->reset(new TCPSocketLibevent(net_log_
.net_log(),
520 (*tcp_socket
)->socket_
.reset(accept_socket_
.release());
524 void TCPSocketLibevent::ConnectCompleted(const CompletionCallback
& callback
,
526 DCHECK_NE(ERR_IO_PENDING
, rv
);
527 callback
.Run(HandleConnectCompleted(rv
));
530 int TCPSocketLibevent::HandleConnectCompleted(int rv
) const {
531 // Log the end of this attempt (and any OS error it threw).
533 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
,
534 NetLog::IntegerCallback("os_error", errno
));
536 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
);
539 // Give a more specific error when the user is offline.
540 if (rv
== ERR_ADDRESS_UNREACHABLE
&& NetworkChangeNotifier::IsOffline())
541 rv
= ERR_INTERNET_DISCONNECTED
;
543 if (!logging_multiple_connect_attempts_
)
549 void TCPSocketLibevent::LogConnectBegin(const AddressList
& addresses
) const {
550 net_log_
.BeginEvent(NetLog::TYPE_TCP_CONNECT
,
551 addresses
.CreateNetLogCallback());
554 void TCPSocketLibevent::LogConnectEnd(int net_error
) const {
555 if (net_error
!= OK
) {
556 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT
, net_error
);
560 UpdateConnectionTypeHistograms(CONNECTION_ANY
);
562 SockaddrStorage storage
;
563 int rv
= socket_
->GetLocalAddress(&storage
);
565 PLOG(ERROR
) << "GetLocalAddress() [rv: " << rv
<< "] error: ";
567 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT
, rv
);
571 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT
,
572 CreateNetLogSourceAddressCallback(storage
.addr
,
576 void TCPSocketLibevent::ReadCompleted(const scoped_refptr
<IOBuffer
>& buf
,
577 const CompletionCallback
& callback
,
579 DCHECK_NE(ERR_IO_PENDING
, rv
);
580 callback
.Run(HandleReadCompleted(buf
.get(), rv
));
583 int TCPSocketLibevent::HandleReadCompleted(IOBuffer
* buf
, int rv
) {
584 if (tcp_fastopen_write_attempted_
&& !tcp_fastopen_connected_
) {
585 // A TCP FastOpen connect-with-write was attempted. This read was a
586 // subsequent read, which either succeeded or failed. If the read
587 // succeeded, the socket is considered connected via TCP FastOpen.
588 // If the read failed, TCP FastOpen is (conservatively) turned off for all
589 // subsequent connections. TCP FastOpen status is recorded in both cases.
590 // TODO (jri): This currently results in conservative behavior, where TCP
591 // FastOpen is turned off on _any_ error. Implement optimizations,
592 // such as turning off TCP FastOpen on more specific errors, and
593 // re-attempting TCP FastOpen after a certain amount of time has passed.
595 tcp_fastopen_connected_
= true;
597 g_tcp_fastopen_has_failed
= true;
598 UpdateTCPFastOpenStatusAfterRead();
602 net_log_
.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR
,
603 CreateNetLogSocketErrorCallback(rv
, errno
));
606 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED
, rv
,
608 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv
);
613 void TCPSocketLibevent::WriteCompleted(const scoped_refptr
<IOBuffer
>& buf
,
614 const CompletionCallback
& callback
,
616 DCHECK_NE(ERR_IO_PENDING
, rv
);
617 callback
.Run(HandleWriteCompleted(buf
.get(), rv
));
620 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer
* buf
, int rv
) {
622 if (tcp_fastopen_write_attempted_
&& !tcp_fastopen_connected_
) {
623 // TCP FastOpen connect-with-write was attempted, and the write failed
624 // for unknown reasons. Record status and (conservatively) turn off
625 // TCP FastOpen for all subsequent connections.
626 // TODO (jri): This currently results in conservative behavior, where TCP
627 // FastOpen is turned off on _any_ error. Implement optimizations,
628 // such as turning off TCP FastOpen on more specific errors, and
629 // re-attempting TCP FastOpen after a certain amount of time has passed.
630 tcp_fastopen_status_
= TCP_FASTOPEN_ERROR
;
631 g_tcp_fastopen_has_failed
= true;
633 net_log_
.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR
,
634 CreateNetLogSocketErrorCallback(rv
, errno
));
637 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT
, rv
,
639 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(rv
);
643 int TCPSocketLibevent::TcpFastOpenWrite(
646 const CompletionCallback
& callback
) {
647 SockaddrStorage storage
;
648 int rv
= socket_
->GetPeerAddress(&storage
);
652 int flags
= 0x20000000; // Magic flag to enable TCP_FASTOPEN.
653 #if defined(OS_LINUX) || defined(OS_ANDROID)
654 // sendto() will fail with EPIPE when the system doesn't implement TCP
655 // FastOpen, and with EOPNOTSUPP when the system implements TCP FastOpen
656 // but it is disabled. Theoretically these shouldn't happen
657 // since the caller should check for system support on startup, but
658 // users may dynamically disable TCP FastOpen via sysctl.
659 flags
|= MSG_NOSIGNAL
;
660 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
661 rv
= HANDLE_EINTR(sendto(socket_
->socket_fd(),
667 tcp_fastopen_write_attempted_
= true;
670 tcp_fastopen_status_
= TCP_FASTOPEN_FAST_CONNECT_RETURN
;
674 DCHECK_NE(EPIPE
, errno
);
676 // If errno == EINPROGRESS, that means the kernel didn't have a cookie
677 // and would block. The kernel is internally doing a connect() though.
678 // Remap EINPROGRESS to EAGAIN so we treat this the same as our other
679 // asynchronous cases. Note that the user buffer has not been copied to
681 if (errno
== EINPROGRESS
) {
684 rv
= MapSystemError(errno
);
687 if (rv
!= ERR_IO_PENDING
) {
688 // TCP FastOpen connect-with-write was attempted, and the write failed
689 // since TCP FastOpen was not implemented or disabled in the OS.
690 // Record status and turn off TCP FastOpen for all subsequent connections.
691 // TODO (jri): This is almost certainly too conservative, since it blanket
692 // turns off TCP FastOpen on any write error. Two things need to be done
693 // here: (i) record a histogram of write errors; in particular, record
694 // occurrences of EOPNOTSUPP and EPIPE, and (ii) afterwards, consider
695 // turning off TCP FastOpen on more specific errors.
696 tcp_fastopen_status_
= TCP_FASTOPEN_ERROR
;
697 g_tcp_fastopen_has_failed
= true;
701 tcp_fastopen_status_
= TCP_FASTOPEN_SLOW_CONNECT_RETURN
;
702 return socket_
->WaitForWrite(buf
, buf_len
, callback
);
705 void TCPSocketLibevent::UpdateTCPFastOpenStatusAfterRead() {
706 DCHECK(tcp_fastopen_status_
== TCP_FASTOPEN_FAST_CONNECT_RETURN
||
707 tcp_fastopen_status_
== TCP_FASTOPEN_SLOW_CONNECT_RETURN
);
709 if (tcp_fastopen_write_attempted_
&& !tcp_fastopen_connected_
) {
710 // TCP FastOpen connect-with-write was attempted, and failed.
711 tcp_fastopen_status_
=
712 (tcp_fastopen_status_
== TCP_FASTOPEN_FAST_CONNECT_RETURN
?
713 TCP_FASTOPEN_FAST_CONNECT_READ_FAILED
:
714 TCP_FASTOPEN_SLOW_CONNECT_READ_FAILED
);
718 bool getsockopt_success
= false;
719 bool server_acked_data
= false;
720 #if defined(TCP_INFO)
721 // Probe to see the if the socket used TCP FastOpen.
723 socklen_t info_len
= sizeof(tcp_info
);
724 getsockopt_success
= getsockopt(socket_
->socket_fd(), IPPROTO_TCP
, TCP_INFO
,
725 &info
, &info_len
) == 0 &&
726 info_len
== sizeof(tcp_info
);
727 server_acked_data
= getsockopt_success
&&
728 (info
.tcpi_options
& TCPI_OPT_SYN_DATA
);
731 if (getsockopt_success
) {
732 if (tcp_fastopen_status_
== TCP_FASTOPEN_FAST_CONNECT_RETURN
) {
733 tcp_fastopen_status_
= (server_acked_data
?
734 TCP_FASTOPEN_SYN_DATA_ACK
:
735 TCP_FASTOPEN_SYN_DATA_NACK
);
737 tcp_fastopen_status_
= (server_acked_data
?
738 TCP_FASTOPEN_NO_SYN_DATA_ACK
:
739 TCP_FASTOPEN_NO_SYN_DATA_NACK
);
742 tcp_fastopen_status_
=
743 (tcp_fastopen_status_
== TCP_FASTOPEN_FAST_CONNECT_RETURN
?
744 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED
:
745 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED
);