Delete unused downloads page asset.
[chromium-blink-merge.git] / net / socket / tcp_socket_libevent.cc
blob0eef1f5fbefa4a828be2bf8df6934bb076047732
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_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 #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 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);
163 Close();
166 int TCPSocketLibevent::Open(AddressFamily family) {
167 DCHECK(!socket_);
168 socket_.reset(new SocketLibevent);
169 int rv = socket_->Open(ConvertAddressFamily(family));
170 if (rv != OK)
171 socket_.reset();
172 return rv;
175 int TCPSocketLibevent::AdoptConnectedSocket(int socket_fd,
176 const IPEndPoint& peer_address) {
177 DCHECK(!socket_);
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);
188 if (rv != OK)
189 socket_.reset();
190 return rv;
193 int TCPSocketLibevent::Bind(const IPEndPoint& address) {
194 DCHECK(socket_);
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) {
204 DCHECK(socket_);
205 return socket_->Listen(backlog);
208 int TCPSocketLibevent::Accept(scoped_ptr<TCPSocketLibevent>* tcp_socket,
209 IPEndPoint* address,
210 const CompletionCallback& callback) {
211 DCHECK(tcp_socket);
212 DCHECK(!callback.is_null());
213 DCHECK(socket_);
214 DCHECK(!accept_socket_);
216 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT);
218 int rv = socket_->Accept(
219 &accept_socket_,
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);
224 return rv;
227 int TCPSocketLibevent::Connect(const IPEndPoint& address,
228 const CompletionCallback& callback) {
229 DCHECK(socket_);
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);
245 return OK;
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);
253 return rv;
256 bool TCPSocketLibevent::IsConnected() const {
257 if (!socket_)
258 return false;
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_.
264 return true;
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,
277 int buf_len,
278 const CompletionCallback& callback) {
279 DCHECK(socket_);
280 DCHECK(!callback.is_null());
282 int rv = socket_->Read(
283 buf, buf_len,
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);
291 return rv;
294 int TCPSocketLibevent::Write(IOBuffer* buf,
295 int buf_len,
296 const CompletionCallback& callback) {
297 DCHECK(socket_);
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);
306 int rv;
308 if (use_tcp_fastopen_ && !tcp_fastopen_write_attempted_) {
309 rv = TcpFastOpenWrite(buf, buf_len, write_callback);
310 } else {
311 rv = socket_->Write(buf, buf_len, write_callback);
314 if (rv != ERR_IO_PENDING)
315 rv = HandleWriteCompleted(buf, rv);
316 return rv;
319 int TCPSocketLibevent::GetLocalAddress(IPEndPoint* address) const {
320 DCHECK(address);
322 if (!socket_)
323 return ERR_SOCKET_NOT_CONNECTED;
325 SockaddrStorage storage;
326 int rv = socket_->GetLocalAddress(&storage);
327 if (rv != OK)
328 return rv;
330 if (!address->FromSockAddr(storage.addr, storage.addr_len))
331 return ERR_ADDRESS_INVALID;
333 return OK;
336 int TCPSocketLibevent::GetPeerAddress(IPEndPoint* address) const {
337 DCHECK(address);
339 if (!IsConnected())
340 return ERR_SOCKET_NOT_CONNECTED;
342 SockaddrStorage storage;
343 int rv = socket_->GetPeerAddress(&storage);
344 if (rv != OK)
345 return rv;
347 if (!address->FromSockAddr(storage.addr, storage.addr_len))
348 return ERR_ADDRESS_INVALID;
350 return OK;
353 int TCPSocketLibevent::SetDefaultOptionsForServer() {
354 DCHECK(socket_);
355 return SetAddressReuse(true);
358 void TCPSocketLibevent::SetDefaultOptionsForClient() {
359 DCHECK(socket_);
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);
381 #endif
384 int TCPSocketLibevent::SetAddressReuse(bool allow) {
385 DCHECK(socket_);
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
392 // behavior.
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
397 // to 3.9.
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));
403 if (rv < 0)
404 return MapSystemError(errno);
405 return OK;
408 int TCPSocketLibevent::SetReceiveBufferSize(int32 size) {
409 DCHECK(socket_);
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) {
416 DCHECK(socket_);
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) {
423 DCHECK(socket_);
424 return SetTCPKeepAlive(socket_->socket_fd(), enable, delay);
427 bool TCPSocketLibevent::SetNoDelay(bool no_delay) {
428 DCHECK(socket_);
429 return SetTCPNoDelay(socket_->socket_fd(), no_delay);
432 void TCPSocketLibevent::Close() {
433 socket_.reset();
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())
453 return;
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;
461 else
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);
474 } else {
475 NOTREACHED();
479 void TCPSocketLibevent::EndLoggingMultipleConnectAttempts(int net_error) {
480 if (logging_multiple_connect_attempts_) {
481 LogConnectEnd(net_error);
482 logging_multiple_connect_attempts_ = false;
483 } else {
484 NOTREACHED();
488 void TCPSocketLibevent::AcceptCompleted(
489 scoped_ptr<TCPSocketLibevent>* tcp_socket,
490 IPEndPoint* address,
491 const CompletionCallback& callback,
492 int rv) {
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,
499 IPEndPoint* address,
500 int rv) {
501 if (rv == OK)
502 rv = BuildTcpSocketLibevent(tcp_socket, address);
504 if (rv == OK) {
505 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT,
506 CreateNetLogIPEndPointCallback(address));
507 } else {
508 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, rv);
511 return 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(),
527 net_log_.source()));
528 (*tcp_socket)->socket_.reset(accept_socket_.release());
529 return OK;
532 void TCPSocketLibevent::ConnectCompleted(const CompletionCallback& callback,
533 int rv) const {
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).
540 if (rv != OK) {
541 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT,
542 NetLog::IntegerCallback("os_error", errno));
543 } else {
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_)
552 LogConnectEnd(rv);
554 return rv;
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);
565 return;
568 UpdateConnectionTypeHistograms(CONNECTION_ANY);
570 SockaddrStorage storage;
571 int rv = socket_->GetLocalAddress(&storage);
572 if (rv != OK) {
573 PLOG(ERROR) << "GetLocalAddress() [rv: " << rv << "] error: ";
574 NOTREACHED();
575 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, rv);
576 return;
579 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT,
580 CreateNetLogSourceAddressCallback(storage.addr,
581 storage.addr_len));
584 void TCPSocketLibevent::ReadCompleted(const scoped_refptr<IOBuffer>& buf,
585 const CompletionCallback& callback,
586 int rv) {
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.
602 if (rv >= 0)
603 tcp_fastopen_connected_ = true;
604 else
605 g_tcp_fastopen_has_failed = true;
606 UpdateTCPFastOpenStatusAfterRead();
609 if (rv < 0) {
610 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR,
611 CreateNetLogSocketErrorCallback(rv, errno));
612 return rv;
614 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv,
615 buf->data());
616 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv);
618 return rv;
621 void TCPSocketLibevent::WriteCompleted(const scoped_refptr<IOBuffer>& buf,
622 const CompletionCallback& callback,
623 int rv) {
624 DCHECK_NE(ERR_IO_PENDING, rv);
625 callback.Run(HandleWriteCompleted(buf.get(), rv));
628 int TCPSocketLibevent::HandleWriteCompleted(IOBuffer* buf, int rv) {
629 if (rv < 0) {
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));
643 return rv;
645 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv,
646 buf->data());
647 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(rv);
648 return rv;
651 int TCPSocketLibevent::TcpFastOpenWrite(
652 IOBuffer* buf,
653 int buf_len,
654 const CompletionCallback& callback) {
655 SockaddrStorage storage;
656 int rv = socket_->GetPeerAddress(&storage);
657 if (rv != OK)
658 return rv;
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(),
670 buf->data(),
671 buf_len,
672 flags,
673 storage.addr,
674 storage.addr_len));
675 tcp_fastopen_write_attempted_ = true;
677 if (rv >= 0) {
678 tcp_fastopen_status_ = TCP_FASTOPEN_FAST_CONNECT_RETURN;
679 return rv;
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
688 // kernel space.
689 if (errno == EINPROGRESS) {
690 rv = ERR_IO_PENDING;
691 } else {
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;
706 return rv;
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);
723 return;
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.
730 tcp_info info;
731 getsockopt_success = GetTcpInfo(socket_->socket_fd(), &info);
732 server_acked_data =
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);
741 } else {
742 tcp_fastopen_status_ = (server_acked_data ?
743 TCP_FASTOPEN_NO_SYN_DATA_ACK :
744 TCP_FASTOPEN_NO_SYN_DATA_NACK);
746 } else {
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 {
756 DCHECK(out_rtt);
757 if (!socket_)
758 return false;
760 #if defined(TCP_INFO)
761 tcp_info 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);
767 return true;
770 #endif // defined(TCP_INFO)
771 return false;
774 } // namespace net