Roll src/third_party/WebKit 1e14c28:9b3210f (svn 194535:194542)
[chromium-blink-merge.git] / net / socket / tcp_socket_libevent.cc
blob56c19b203a48613963a15a19e68d8ed0672bb592
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.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
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 } // namespace
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(),
135 FROM_HERE,
136 base::Bind(SystemSupportsTCPFastOpen),
137 base::Bind(RegisterTCPFastOpenIntentAndSupport, user_enabled));
138 #endif
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);
155 Close();
158 int TCPSocketLibevent::Open(AddressFamily family) {
159 DCHECK(!socket_);
160 socket_.reset(new SocketLibevent);
161 int rv = socket_->Open(ConvertAddressFamily(family));
162 if (rv != OK)
163 socket_.reset();
164 return rv;
167 int TCPSocketLibevent::AdoptConnectedSocket(int socket_fd,
168 const IPEndPoint& peer_address) {
169 DCHECK(!socket_);
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);
180 if (rv != OK)
181 socket_.reset();
182 return rv;
185 int TCPSocketLibevent::Bind(const IPEndPoint& address) {
186 DCHECK(socket_);
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) {
196 DCHECK(socket_);
197 return socket_->Listen(backlog);
200 int TCPSocketLibevent::Accept(scoped_ptr<TCPSocketLibevent>* tcp_socket,
201 IPEndPoint* address,
202 const CompletionCallback& callback) {
203 DCHECK(tcp_socket);
204 DCHECK(!callback.is_null());
205 DCHECK(socket_);
206 DCHECK(!accept_socket_);
208 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT);
210 int rv = socket_->Accept(
211 &accept_socket_,
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);
216 return rv;
219 int TCPSocketLibevent::Connect(const IPEndPoint& address,
220 const CompletionCallback& callback) {
221 DCHECK(socket_);
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);
237 return OK;
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);
245 return rv;
248 bool TCPSocketLibevent::IsConnected() const {
249 if (!socket_)
250 return false;
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_.
256 return true;
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,
269 int buf_len,
270 const CompletionCallback& callback) {
271 DCHECK(socket_);
272 DCHECK(!callback.is_null());
274 int rv = socket_->Read(
275 buf, buf_len,
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);
283 return rv;
286 int TCPSocketLibevent::Write(IOBuffer* buf,
287 int buf_len,
288 const CompletionCallback& callback) {
289 DCHECK(socket_);
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);
298 int rv;
300 if (use_tcp_fastopen_ && !tcp_fastopen_write_attempted_) {
301 rv = TcpFastOpenWrite(buf, buf_len, write_callback);
302 } else {
303 rv = socket_->Write(buf, buf_len, write_callback);
306 if (rv != ERR_IO_PENDING)
307 rv = HandleWriteCompleted(buf, rv);
308 return rv;
311 int TCPSocketLibevent::GetLocalAddress(IPEndPoint* address) const {
312 DCHECK(address);
314 if (!socket_)
315 return ERR_SOCKET_NOT_CONNECTED;
317 SockaddrStorage storage;
318 int rv = socket_->GetLocalAddress(&storage);
319 if (rv != OK)
320 return rv;
322 if (!address->FromSockAddr(storage.addr, storage.addr_len))
323 return ERR_ADDRESS_INVALID;
325 return OK;
328 int TCPSocketLibevent::GetPeerAddress(IPEndPoint* address) const {
329 DCHECK(address);
331 if (!IsConnected())
332 return ERR_SOCKET_NOT_CONNECTED;
334 SockaddrStorage storage;
335 int rv = socket_->GetPeerAddress(&storage);
336 if (rv != OK)
337 return rv;
339 if (!address->FromSockAddr(storage.addr, storage.addr_len))
340 return ERR_ADDRESS_INVALID;
342 return OK;
345 int TCPSocketLibevent::SetDefaultOptionsForServer() {
346 DCHECK(socket_);
347 return SetAddressReuse(true);
350 void TCPSocketLibevent::SetDefaultOptionsForClient() {
351 DCHECK(socket_);
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);
373 #endif
376 int TCPSocketLibevent::SetAddressReuse(bool allow) {
377 DCHECK(socket_);
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
384 // behavior.
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
389 // to 3.9.
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));
395 if (rv < 0)
396 return MapSystemError(errno);
397 return OK;
400 int TCPSocketLibevent::SetReceiveBufferSize(int32 size) {
401 DCHECK(socket_);
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) {
408 DCHECK(socket_);
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) {
415 DCHECK(socket_);
416 return SetTCPKeepAlive(socket_->socket_fd(), enable, delay);
419 bool TCPSocketLibevent::SetNoDelay(bool no_delay) {
420 DCHECK(socket_);
421 return SetTCPNoDelay(socket_->socket_fd(), no_delay);
424 void TCPSocketLibevent::Close() {
425 socket_.reset();
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())
445 return;
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;
453 else
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);
466 } else {
467 NOTREACHED();
471 void TCPSocketLibevent::EndLoggingMultipleConnectAttempts(int net_error) {
472 if (logging_multiple_connect_attempts_) {
473 LogConnectEnd(net_error);
474 logging_multiple_connect_attempts_ = false;
475 } else {
476 NOTREACHED();
480 void TCPSocketLibevent::AcceptCompleted(
481 scoped_ptr<TCPSocketLibevent>* tcp_socket,
482 IPEndPoint* address,
483 const CompletionCallback& callback,
484 int rv) {
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,
491 IPEndPoint* address,
492 int rv) {
493 if (rv == OK)
494 rv = BuildTcpSocketLibevent(tcp_socket, address);
496 if (rv == OK) {
497 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT,
498 CreateNetLogIPEndPointCallback(address));
499 } else {
500 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, rv);
503 return 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(),
519 net_log_.source()));
520 (*tcp_socket)->socket_.reset(accept_socket_.release());
521 return OK;
524 void TCPSocketLibevent::ConnectCompleted(const CompletionCallback& callback,
525 int rv) const {
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).
532 if (rv != OK) {
533 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT,
534 NetLog::IntegerCallback("os_error", errno));
535 } else {
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_)
544 LogConnectEnd(rv);
546 return rv;
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);
557 return;
560 UpdateConnectionTypeHistograms(CONNECTION_ANY);
562 SockaddrStorage storage;
563 int rv = socket_->GetLocalAddress(&storage);
564 if (rv != OK) {
565 PLOG(ERROR) << "GetLocalAddress() [rv: " << rv << "] error: ";
566 NOTREACHED();
567 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, rv);
568 return;
571 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT,
572 CreateNetLogSourceAddressCallback(storage.addr,
573 storage.addr_len));
576 void TCPSocketLibevent::ReadCompleted(const scoped_refptr<IOBuffer>& buf,
577 const CompletionCallback& callback,
578 int rv) {
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.
594 if (rv >= 0)
595 tcp_fastopen_connected_ = true;
596 else
597 g_tcp_fastopen_has_failed = true;
598 UpdateTCPFastOpenStatusAfterRead();
601 if (rv < 0) {
602 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR,
603 CreateNetLogSocketErrorCallback(rv, errno));
604 return rv;
606 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv,
607 buf->data());
608 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv);
610 return rv;
613 void TCPSocketLibevent::WriteCompleted(const scoped_refptr<IOBuffer>& buf,
614 const CompletionCallback& callback,
615 int rv) {
616 DCHECK_NE(ERR_IO_PENDING, rv);
617 callback.Run(HandleWriteCompleted(buf.get(), rv));
620 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) {
621 if (rv < 0) {
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));
635 return rv;
637 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv,
638 buf->data());
639 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(rv);
640 return rv;
643 int TCPSocketLibevent::TcpFastOpenWrite(
644 IOBuffer* buf,
645 int buf_len,
646 const CompletionCallback& callback) {
647 SockaddrStorage storage;
648 int rv = socket_->GetPeerAddress(&storage);
649 if (rv != OK)
650 return rv;
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(),
662 buf->data(),
663 buf_len,
664 flags,
665 storage.addr,
666 storage.addr_len));
667 tcp_fastopen_write_attempted_ = true;
669 if (rv >= 0) {
670 tcp_fastopen_status_ = TCP_FASTOPEN_FAST_CONNECT_RETURN;
671 return rv;
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
680 // kernel space.
681 if (errno == EINPROGRESS) {
682 rv = ERR_IO_PENDING;
683 } else {
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;
698 return rv;
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);
715 return;
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.
722 tcp_info info;
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);
729 #endif
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 } // namespace net