Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / net / socket / tcp_socket_posix.cc
blob653eb82445d658160067f0aea06bca0b75a9fc7c
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"
7 #include <errno.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_net_log_params.h"
28 #include "net/socket/socket_posix.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
33 #endif
35 namespace net {
37 namespace {
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
50 // `man 7 tcp`.
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));
54 return error == 0;
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;
63 return false;
66 // If we disabled TCP keep alive, our work is done here.
67 if (!enable)
68 return true;
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;
76 return false;
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;
81 return false;
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;
86 return false;
88 #endif
89 return true;
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)) {
100 return false;
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')) {
105 return false;
107 return true;
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;
115 #endif
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)
125 } // namespace
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(),
143 FROM_HERE,
144 base::Bind(SystemSupportsTCPFastOpen),
145 base::Bind(RegisterTCPFastOpenIntentAndSupport, user_enabled));
146 #endif
149 TCPSocketPosix::TCPSocketPosix(NetLog* net_log, const NetLog::Source& source)
150 : use_tcp_fastopen_(false),
151 tcp_fastopen_write_attempted_(false),
152 tcp_fastopen_connected_(false),
153 tcp_fastopen_status_(TCP_FASTOPEN_STATUS_UNKNOWN),
154 logging_multiple_connect_attempts_(false),
155 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {
156 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE,
157 source.ToEventParametersCallback());
160 TCPSocketPosix::~TCPSocketPosix() {
161 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE);
162 Close();
165 int TCPSocketPosix::Open(AddressFamily family) {
166 DCHECK(!socket_);
167 socket_.reset(new SocketPosix);
168 int rv = socket_->Open(ConvertAddressFamily(family));
169 if (rv != OK)
170 socket_.reset();
171 return rv;
174 int TCPSocketPosix::AdoptConnectedSocket(int socket_fd,
175 const IPEndPoint& peer_address) {
176 DCHECK(!socket_);
178 SockaddrStorage storage;
179 if (!peer_address.ToSockAddr(storage.addr, &storage.addr_len) &&
180 // For backward compatibility, allows the empty address.
181 !(peer_address == IPEndPoint())) {
182 return ERR_ADDRESS_INVALID;
185 socket_.reset(new SocketPosix);
186 int rv = socket_->AdoptConnectedSocket(socket_fd, storage);
187 if (rv != OK)
188 socket_.reset();
189 return rv;
192 int TCPSocketPosix::Bind(const IPEndPoint& address) {
193 DCHECK(socket_);
195 SockaddrStorage storage;
196 if (!address.ToSockAddr(storage.addr, &storage.addr_len))
197 return ERR_ADDRESS_INVALID;
199 return socket_->Bind(storage);
202 int TCPSocketPosix::Listen(int backlog) {
203 DCHECK(socket_);
204 return socket_->Listen(backlog);
207 int TCPSocketPosix::Accept(scoped_ptr<TCPSocketPosix>* tcp_socket,
208 IPEndPoint* address,
209 const CompletionCallback& callback) {
210 DCHECK(tcp_socket);
211 DCHECK(!callback.is_null());
212 DCHECK(socket_);
213 DCHECK(!accept_socket_);
215 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT);
217 int rv = socket_->Accept(
218 &accept_socket_,
219 base::Bind(&TCPSocketPosix::AcceptCompleted, base::Unretained(this),
220 tcp_socket, address, callback));
221 if (rv != ERR_IO_PENDING)
222 rv = HandleAcceptCompleted(tcp_socket, address, rv);
223 return rv;
226 int TCPSocketPosix::Connect(const IPEndPoint& address,
227 const CompletionCallback& callback) {
228 DCHECK(socket_);
230 if (!logging_multiple_connect_attempts_)
231 LogConnectBegin(AddressList(address));
233 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT,
234 CreateNetLogIPEndPointCallback(&address));
236 SockaddrStorage storage;
237 if (!address.ToSockAddr(storage.addr, &storage.addr_len))
238 return ERR_ADDRESS_INVALID;
240 if (use_tcp_fastopen_) {
241 // With TCP FastOpen, we pretend that the socket is connected.
242 DCHECK(!tcp_fastopen_write_attempted_);
243 socket_->SetPeerAddress(storage);
244 return OK;
247 int rv =
248 socket_->Connect(storage, base::Bind(&TCPSocketPosix::ConnectCompleted,
249 base::Unretained(this), callback));
250 if (rv != ERR_IO_PENDING)
251 rv = HandleConnectCompleted(rv);
252 return rv;
255 bool TCPSocketPosix::IsConnected() const {
256 if (!socket_)
257 return false;
259 if (use_tcp_fastopen_ && !tcp_fastopen_write_attempted_ &&
260 socket_->HasPeerAddress()) {
261 // With TCP FastOpen, we pretend that the socket is connected.
262 // This allows GetPeerAddress() to return peer_address_.
263 return true;
266 return socket_->IsConnected();
269 bool TCPSocketPosix::IsConnectedAndIdle() const {
270 // TODO(wtc): should we also handle the TCP FastOpen case here,
271 // as we do in IsConnected()?
272 return socket_ && socket_->IsConnectedAndIdle();
275 int TCPSocketPosix::Read(IOBuffer* buf,
276 int buf_len,
277 const CompletionCallback& callback) {
278 DCHECK(socket_);
279 DCHECK(!callback.is_null());
281 int rv = socket_->Read(
282 buf, buf_len,
283 base::Bind(&TCPSocketPosix::ReadCompleted,
284 // Grab a reference to |buf| so that ReadCompleted() can still
285 // use it when Read() completes, as otherwise, this transfers
286 // ownership of buf to socket.
287 base::Unretained(this), make_scoped_refptr(buf), callback));
288 if (rv != ERR_IO_PENDING)
289 rv = HandleReadCompleted(buf, rv);
290 return rv;
293 int TCPSocketPosix::Write(IOBuffer* buf,
294 int buf_len,
295 const CompletionCallback& callback) {
296 DCHECK(socket_);
297 DCHECK(!callback.is_null());
299 CompletionCallback write_callback =
300 base::Bind(&TCPSocketPosix::WriteCompleted,
301 // Grab a reference to |buf| so that WriteCompleted() can still
302 // use it when Write() completes, as otherwise, this transfers
303 // ownership of buf to socket.
304 base::Unretained(this), make_scoped_refptr(buf), callback);
305 int rv;
307 if (use_tcp_fastopen_ && !tcp_fastopen_write_attempted_) {
308 rv = TcpFastOpenWrite(buf, buf_len, write_callback);
309 } else {
310 rv = socket_->Write(buf, buf_len, write_callback);
313 if (rv != ERR_IO_PENDING)
314 rv = HandleWriteCompleted(buf, rv);
315 return rv;
318 int TCPSocketPosix::GetLocalAddress(IPEndPoint* address) const {
319 DCHECK(address);
321 if (!socket_)
322 return ERR_SOCKET_NOT_CONNECTED;
324 SockaddrStorage storage;
325 int rv = socket_->GetLocalAddress(&storage);
326 if (rv != OK)
327 return rv;
329 if (!address->FromSockAddr(storage.addr, storage.addr_len))
330 return ERR_ADDRESS_INVALID;
332 return OK;
335 int TCPSocketPosix::GetPeerAddress(IPEndPoint* address) const {
336 DCHECK(address);
338 if (!IsConnected())
339 return ERR_SOCKET_NOT_CONNECTED;
341 SockaddrStorage storage;
342 int rv = socket_->GetPeerAddress(&storage);
343 if (rv != OK)
344 return rv;
346 if (!address->FromSockAddr(storage.addr, storage.addr_len))
347 return ERR_ADDRESS_INVALID;
349 return OK;
352 int TCPSocketPosix::SetDefaultOptionsForServer() {
353 DCHECK(socket_);
354 return SetAddressReuse(true);
357 void TCPSocketPosix::SetDefaultOptionsForClient() {
358 DCHECK(socket_);
360 // This mirrors the behaviour on Windows. See the comment in
361 // tcp_socket_win.cc after searching for "NODELAY".
362 // If SetTCPNoDelay fails, we don't care.
363 SetTCPNoDelay(socket_->socket_fd(), true);
365 // TCP keep alive wakes up the radio, which is expensive on mobile. Do not
366 // enable it there. It's useful to prevent TCP middleboxes from timing out
367 // connection mappings. Packets for timed out connection mappings at
368 // middleboxes will either lead to:
369 // a) Middleboxes sending TCP RSTs. It's up to higher layers to check for this
370 // and retry. The HTTP network transaction code does this.
371 // b) Middleboxes just drop the unrecognized TCP packet. This leads to the TCP
372 // stack retransmitting packets per TCP stack retransmission timeouts, which
373 // are very high (on the order of seconds). Given the number of
374 // retransmissions required before killing the connection, this can lead to
375 // tens of seconds or even minutes of delay, depending on OS.
376 #if !defined(OS_ANDROID) && !defined(OS_IOS)
377 const int kTCPKeepAliveSeconds = 45;
379 SetTCPKeepAlive(socket_->socket_fd(), true, kTCPKeepAliveSeconds);
380 #endif
383 int TCPSocketPosix::SetAddressReuse(bool allow) {
384 DCHECK(socket_);
386 // SO_REUSEADDR is useful for server sockets to bind to a recently unbound
387 // port. When a socket is closed, the end point changes its state to TIME_WAIT
388 // and wait for 2 MSL (maximum segment lifetime) to ensure the remote peer
389 // acknowledges its closure. For server sockets, it is usually safe to
390 // bind to a TIME_WAIT end point immediately, which is a widely adopted
391 // behavior.
393 // Note that on *nix, SO_REUSEADDR does not enable the TCP socket to bind to
394 // an end point that is already bound by another socket. To do that one must
395 // set SO_REUSEPORT instead. This option is not provided on Linux prior
396 // to 3.9.
398 // SO_REUSEPORT is provided in MacOS X and iOS.
399 int boolean_value = allow ? 1 : 0;
400 int rv = setsockopt(socket_->socket_fd(), SOL_SOCKET, SO_REUSEADDR,
401 &boolean_value, sizeof(boolean_value));
402 if (rv < 0)
403 return MapSystemError(errno);
404 return OK;
407 int TCPSocketPosix::SetReceiveBufferSize(int32 size) {
408 DCHECK(socket_);
409 int rv = setsockopt(socket_->socket_fd(), SOL_SOCKET, SO_RCVBUF,
410 reinterpret_cast<const char*>(&size), sizeof(size));
411 return (rv == 0) ? OK : MapSystemError(errno);
414 int TCPSocketPosix::SetSendBufferSize(int32 size) {
415 DCHECK(socket_);
416 int rv = setsockopt(socket_->socket_fd(), SOL_SOCKET, SO_SNDBUF,
417 reinterpret_cast<const char*>(&size), sizeof(size));
418 return (rv == 0) ? OK : MapSystemError(errno);
421 bool TCPSocketPosix::SetKeepAlive(bool enable, int delay) {
422 DCHECK(socket_);
423 return SetTCPKeepAlive(socket_->socket_fd(), enable, delay);
426 bool TCPSocketPosix::SetNoDelay(bool no_delay) {
427 DCHECK(socket_);
428 return SetTCPNoDelay(socket_->socket_fd(), no_delay);
431 void TCPSocketPosix::Close() {
432 socket_.reset();
434 // Record and reset TCP FastOpen state.
435 if (tcp_fastopen_write_attempted_ ||
436 tcp_fastopen_status_ == TCP_FASTOPEN_PREVIOUSLY_FAILED) {
437 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection",
438 tcp_fastopen_status_, TCP_FASTOPEN_MAX_VALUE);
440 use_tcp_fastopen_ = false;
441 tcp_fastopen_connected_ = false;
442 tcp_fastopen_write_attempted_ = false;
443 tcp_fastopen_status_ = TCP_FASTOPEN_STATUS_UNKNOWN;
446 bool TCPSocketPosix::UsingTCPFastOpen() const {
447 return use_tcp_fastopen_;
450 void TCPSocketPosix::EnableTCPFastOpenIfSupported() {
451 if (!IsTCPFastOpenSupported())
452 return;
454 // Do not enable TCP FastOpen if it had previously failed.
455 // This check conservatively avoids middleboxes that may blackhole
456 // TCP FastOpen SYN+Data packets; on such a failure, subsequent sockets
457 // should not use TCP FastOpen.
458 if(!g_tcp_fastopen_has_failed)
459 use_tcp_fastopen_ = true;
460 else
461 tcp_fastopen_status_ = TCP_FASTOPEN_PREVIOUSLY_FAILED;
464 bool TCPSocketPosix::IsValid() const {
465 return socket_ != NULL && socket_->socket_fd() != kInvalidSocket;
468 void TCPSocketPosix::StartLoggingMultipleConnectAttempts(
469 const AddressList& addresses) {
470 if (!logging_multiple_connect_attempts_) {
471 logging_multiple_connect_attempts_ = true;
472 LogConnectBegin(addresses);
473 } else {
474 NOTREACHED();
478 void TCPSocketPosix::EndLoggingMultipleConnectAttempts(int net_error) {
479 if (logging_multiple_connect_attempts_) {
480 LogConnectEnd(net_error);
481 logging_multiple_connect_attempts_ = false;
482 } else {
483 NOTREACHED();
487 void TCPSocketPosix::AcceptCompleted(scoped_ptr<TCPSocketPosix>* tcp_socket,
488 IPEndPoint* address,
489 const CompletionCallback& callback,
490 int rv) {
491 DCHECK_NE(ERR_IO_PENDING, rv);
492 callback.Run(HandleAcceptCompleted(tcp_socket, address, rv));
495 int TCPSocketPosix::HandleAcceptCompleted(
496 scoped_ptr<TCPSocketPosix>* tcp_socket,
497 IPEndPoint* address,
498 int rv) {
499 if (rv == OK)
500 rv = BuildTcpSocketPosix(tcp_socket, address);
502 if (rv == OK) {
503 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT,
504 CreateNetLogIPEndPointCallback(address));
505 } else {
506 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, rv);
509 return rv;
512 int TCPSocketPosix::BuildTcpSocketPosix(scoped_ptr<TCPSocketPosix>* tcp_socket,
513 IPEndPoint* address) {
514 DCHECK(accept_socket_);
516 SockaddrStorage storage;
517 if (accept_socket_->GetPeerAddress(&storage) != OK ||
518 !address->FromSockAddr(storage.addr, storage.addr_len)) {
519 accept_socket_.reset();
520 return ERR_ADDRESS_INVALID;
523 tcp_socket->reset(new TCPSocketPosix(net_log_.net_log(), net_log_.source()));
524 (*tcp_socket)->socket_.reset(accept_socket_.release());
525 return OK;
528 void TCPSocketPosix::ConnectCompleted(const CompletionCallback& callback,
529 int rv) const {
530 DCHECK_NE(ERR_IO_PENDING, rv);
531 callback.Run(HandleConnectCompleted(rv));
534 int TCPSocketPosix::HandleConnectCompleted(int rv) const {
535 // Log the end of this attempt (and any OS error it threw).
536 if (rv != OK) {
537 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT,
538 NetLog::IntegerCallback("os_error", errno));
539 } else {
540 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT);
543 // Give a more specific error when the user is offline.
544 if (rv == ERR_ADDRESS_UNREACHABLE && NetworkChangeNotifier::IsOffline())
545 rv = ERR_INTERNET_DISCONNECTED;
547 if (!logging_multiple_connect_attempts_)
548 LogConnectEnd(rv);
550 return rv;
553 void TCPSocketPosix::LogConnectBegin(const AddressList& addresses) const {
554 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT,
555 addresses.CreateNetLogCallback());
558 void TCPSocketPosix::LogConnectEnd(int net_error) const {
559 if (net_error != OK) {
560 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, net_error);
561 return;
564 UpdateConnectionTypeHistograms(CONNECTION_ANY);
566 SockaddrStorage storage;
567 int rv = socket_->GetLocalAddress(&storage);
568 if (rv != OK) {
569 PLOG(ERROR) << "GetLocalAddress() [rv: " << rv << "] error: ";
570 NOTREACHED();
571 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, rv);
572 return;
575 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT,
576 CreateNetLogSourceAddressCallback(storage.addr,
577 storage.addr_len));
580 void TCPSocketPosix::ReadCompleted(const scoped_refptr<IOBuffer>& buf,
581 const CompletionCallback& callback,
582 int rv) {
583 DCHECK_NE(ERR_IO_PENDING, rv);
584 callback.Run(HandleReadCompleted(buf.get(), rv));
587 int TCPSocketPosix::HandleReadCompleted(IOBuffer* buf, int rv) {
588 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) {
589 // A TCP FastOpen connect-with-write was attempted. This read was a
590 // subsequent read, which either succeeded or failed. If the read
591 // succeeded, the socket is considered connected via TCP FastOpen.
592 // If the read failed, TCP FastOpen is (conservatively) turned off for all
593 // subsequent connections. TCP FastOpen status is recorded in both cases.
594 // TODO (jri): This currently results in conservative behavior, where TCP
595 // FastOpen is turned off on _any_ error. Implement optimizations,
596 // such as turning off TCP FastOpen on more specific errors, and
597 // re-attempting TCP FastOpen after a certain amount of time has passed.
598 if (rv >= 0)
599 tcp_fastopen_connected_ = true;
600 else
601 g_tcp_fastopen_has_failed = true;
602 UpdateTCPFastOpenStatusAfterRead();
605 if (rv < 0) {
606 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR,
607 CreateNetLogSocketErrorCallback(rv, errno));
608 return rv;
610 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv,
611 buf->data());
612 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv);
614 return rv;
617 void TCPSocketPosix::WriteCompleted(const scoped_refptr<IOBuffer>& buf,
618 const CompletionCallback& callback,
619 int rv) {
620 DCHECK_NE(ERR_IO_PENDING, rv);
621 callback.Run(HandleWriteCompleted(buf.get(), rv));
624 int TCPSocketPosix::HandleWriteCompleted(IOBuffer* buf, int rv) {
625 if (rv < 0) {
626 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) {
627 // TCP FastOpen connect-with-write was attempted, and the write failed
628 // for unknown reasons. Record status and (conservatively) turn off
629 // TCP FastOpen for all subsequent connections.
630 // TODO (jri): This currently results in conservative behavior, where TCP
631 // FastOpen is turned off on _any_ error. Implement optimizations,
632 // such as turning off TCP FastOpen on more specific errors, and
633 // re-attempting TCP FastOpen after a certain amount of time has passed.
634 tcp_fastopen_status_ = TCP_FASTOPEN_ERROR;
635 g_tcp_fastopen_has_failed = true;
637 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR,
638 CreateNetLogSocketErrorCallback(rv, errno));
639 return rv;
641 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv,
642 buf->data());
643 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(rv);
644 return rv;
647 int TCPSocketPosix::TcpFastOpenWrite(IOBuffer* buf,
648 int buf_len,
649 const CompletionCallback& callback) {
650 SockaddrStorage storage;
651 int rv = socket_->GetPeerAddress(&storage);
652 if (rv != OK)
653 return rv;
655 int flags = 0x20000000; // Magic flag to enable TCP_FASTOPEN.
656 #if defined(OS_LINUX) || defined(OS_ANDROID)
657 // sendto() will fail with EPIPE when the system doesn't implement TCP
658 // FastOpen, and with EOPNOTSUPP when the system implements TCP FastOpen
659 // but it is disabled. Theoretically these shouldn't happen
660 // since the caller should check for system support on startup, but
661 // users may dynamically disable TCP FastOpen via sysctl.
662 flags |= MSG_NOSIGNAL;
663 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
664 rv = HANDLE_EINTR(sendto(socket_->socket_fd(),
665 buf->data(),
666 buf_len,
667 flags,
668 storage.addr,
669 storage.addr_len));
670 tcp_fastopen_write_attempted_ = true;
672 if (rv >= 0) {
673 tcp_fastopen_status_ = TCP_FASTOPEN_FAST_CONNECT_RETURN;
674 return rv;
677 DCHECK_NE(EPIPE, errno);
679 // If errno == EINPROGRESS, that means the kernel didn't have a cookie
680 // and would block. The kernel is internally doing a connect() though.
681 // Remap EINPROGRESS to EAGAIN so we treat this the same as our other
682 // asynchronous cases. Note that the user buffer has not been copied to
683 // kernel space.
684 if (errno == EINPROGRESS) {
685 rv = ERR_IO_PENDING;
686 } else {
687 rv = MapSystemError(errno);
690 if (rv != ERR_IO_PENDING) {
691 // TCP FastOpen connect-with-write was attempted, and the write failed
692 // since TCP FastOpen was not implemented or disabled in the OS.
693 // Record status and turn off TCP FastOpen for all subsequent connections.
694 // TODO (jri): This is almost certainly too conservative, since it blanket
695 // turns off TCP FastOpen on any write error. Two things need to be done
696 // here: (i) record a histogram of write errors; in particular, record
697 // occurrences of EOPNOTSUPP and EPIPE, and (ii) afterwards, consider
698 // turning off TCP FastOpen on more specific errors.
699 tcp_fastopen_status_ = TCP_FASTOPEN_ERROR;
700 g_tcp_fastopen_has_failed = true;
701 return rv;
704 tcp_fastopen_status_ = TCP_FASTOPEN_SLOW_CONNECT_RETURN;
705 return socket_->WaitForWrite(buf, buf_len, callback);
708 void TCPSocketPosix::UpdateTCPFastOpenStatusAfterRead() {
709 DCHECK(tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ||
710 tcp_fastopen_status_ == TCP_FASTOPEN_SLOW_CONNECT_RETURN);
712 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) {
713 // TCP FastOpen connect-with-write was attempted, and failed.
714 tcp_fastopen_status_ =
715 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ?
716 TCP_FASTOPEN_FAST_CONNECT_READ_FAILED :
717 TCP_FASTOPEN_SLOW_CONNECT_READ_FAILED);
718 return;
721 bool getsockopt_success = false;
722 bool server_acked_data = false;
723 #if defined(TCP_INFO)
724 // Probe to see the if the socket used TCP FastOpen.
725 tcp_info info;
726 getsockopt_success = GetTcpInfo(socket_->socket_fd(), &info);
727 server_acked_data =
728 getsockopt_success && (info.tcpi_options & TCPI_OPT_SYN_DATA);
729 #endif // defined(TCP_INFO)
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);
736 } else {
737 tcp_fastopen_status_ = (server_acked_data ?
738 TCP_FASTOPEN_NO_SYN_DATA_ACK :
739 TCP_FASTOPEN_NO_SYN_DATA_NACK);
741 } else {
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);
749 bool TCPSocketPosix::GetEstimatedRoundTripTime(base::TimeDelta* out_rtt) const {
750 DCHECK(out_rtt);
751 if (!socket_)
752 return false;
754 #if defined(TCP_INFO)
755 tcp_info info;
756 if (GetTcpInfo(socket_->socket_fd(), &info)) {
757 // tcpi_rtt is zero when the kernel doesn't have an RTT estimate,
758 // and possibly in other cases such as connections to localhost.
759 if (info.tcpi_rtt > 0) {
760 *out_rtt = base::TimeDelta::FromMicroseconds(info.tcpi_rtt);
761 return true;
764 #endif // defined(TCP_INFO)
765 return false;
768 } // namespace net