Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/tcp_socket.h" | 5 #include "net/socket/tcp_socket.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <netinet/tcp.h> | 8 #include <netinet/tcp.h> |
| 9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/posix/eintr_wrapper.h" | 16 #include "base/posix/eintr_wrapper.h" |
| 17 #include "base/task_scheduler/post_task.h" | 17 #include "base/task_scheduler/post_task.h" |
| 18 #include "base/time/default_tick_clock.h" | 18 #include "base/time/time.h" |
| 19 #include "net/base/address_list.h" | 19 #include "net/base/address_list.h" |
| 20 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
| 21 #include "net/base/ip_endpoint.h" | 21 #include "net/base/ip_endpoint.h" |
| 22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 23 #include "net/base/network_activity_monitor.h" | 23 #include "net/base/network_activity_monitor.h" |
| 24 #include "net/base/network_change_notifier.h" | 24 #include "net/base/network_change_notifier.h" |
| 25 #include "net/base/sockaddr_storage.h" | 25 #include "net/base/sockaddr_storage.h" |
| 26 #include "net/log/net_log.h" | 26 #include "net/log/net_log.h" |
| 27 #include "net/log/net_log_event_type.h" | 27 #include "net/log/net_log_event_type.h" |
| 28 #include "net/log/net_log_source.h" | 28 #include "net/log/net_log_source.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 base::Bind(SystemSupportsTCPFastOpen), | 137 base::Bind(SystemSupportsTCPFastOpen), |
| 138 base::Bind(RegisterTCPFastOpenIntentAndSupport, user_enabled)); | 138 base::Bind(RegisterTCPFastOpenIntentAndSupport, user_enabled)); |
| 139 #endif | 139 #endif |
| 140 } | 140 } |
| 141 | 141 |
| 142 TCPSocketPosix::TCPSocketPosix( | 142 TCPSocketPosix::TCPSocketPosix( |
| 143 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, | 143 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, |
| 144 NetLog* net_log, | 144 NetLog* net_log, |
| 145 const NetLogSource& source) | 145 const NetLogSource& source) |
| 146 : socket_performance_watcher_(std::move(socket_performance_watcher)), | 146 : socket_performance_watcher_(std::move(socket_performance_watcher)), |
| 147 tick_clock_(new base::DefaultTickClock()), | |
| 148 rtt_notifications_minimum_interval_(base::TimeDelta::FromSeconds(1)), | |
| 149 use_tcp_fastopen_(false), | 147 use_tcp_fastopen_(false), |
| 150 tcp_fastopen_write_attempted_(false), | 148 tcp_fastopen_write_attempted_(false), |
| 151 tcp_fastopen_connected_(false), | 149 tcp_fastopen_connected_(false), |
| 152 tcp_fastopen_status_(TCP_FASTOPEN_STATUS_UNKNOWN), | 150 tcp_fastopen_status_(TCP_FASTOPEN_STATUS_UNKNOWN), |
| 153 logging_multiple_connect_attempts_(false), | 151 logging_multiple_connect_attempts_(false), |
| 154 net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)) { | 152 net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)) { |
| 155 net_log_.BeginEvent(NetLogEventType::SOCKET_ALIVE, | 153 net_log_.BeginEvent(NetLogEventType::SOCKET_ALIVE, |
| 156 source.ToEventParametersCallback()); | 154 source.ToEventParametersCallback()); |
| 157 } | 155 } |
| 158 | 156 |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 476 | 474 |
| 477 void TCPSocketPosix::EndLoggingMultipleConnectAttempts(int net_error) { | 475 void TCPSocketPosix::EndLoggingMultipleConnectAttempts(int net_error) { |
| 478 if (logging_multiple_connect_attempts_) { | 476 if (logging_multiple_connect_attempts_) { |
| 479 LogConnectEnd(net_error); | 477 LogConnectEnd(net_error); |
| 480 logging_multiple_connect_attempts_ = false; | 478 logging_multiple_connect_attempts_ = false; |
| 481 } else { | 479 } else { |
| 482 NOTREACHED(); | 480 NOTREACHED(); |
| 483 } | 481 } |
| 484 } | 482 } |
| 485 | 483 |
| 486 void TCPSocketPosix::SetTickClockForTesting( | |
| 487 std::unique_ptr<base::TickClock> tick_clock) { | |
| 488 tick_clock_ = std::move(tick_clock); | |
| 489 } | |
| 490 | |
| 491 void TCPSocketPosix::AcceptCompleted( | 484 void TCPSocketPosix::AcceptCompleted( |
| 492 std::unique_ptr<TCPSocketPosix>* tcp_socket, | 485 std::unique_ptr<TCPSocketPosix>* tcp_socket, |
| 493 IPEndPoint* address, | 486 IPEndPoint* address, |
| 494 const CompletionCallback& callback, | 487 const CompletionCallback& callback, |
| 495 int rv) { | 488 int rv) { |
| 496 DCHECK_NE(ERR_IO_PENDING, rv); | 489 DCHECK_NE(ERR_IO_PENDING, rv); |
| 497 callback.Run(HandleAcceptCompleted(tcp_socket, address, rv)); | 490 callback.Run(HandleAcceptCompleted(tcp_socket, address, rv)); |
| 498 } | 491 } |
| 499 | 492 |
| 500 int TCPSocketPosix::HandleAcceptCompleted( | 493 int TCPSocketPosix::HandleAcceptCompleted( |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 716 g_tcp_fastopen_has_failed = true; | 709 g_tcp_fastopen_has_failed = true; |
| 717 return rv; | 710 return rv; |
| 718 } | 711 } |
| 719 | 712 |
| 720 tcp_fastopen_status_ = TCP_FASTOPEN_SLOW_CONNECT_RETURN; | 713 tcp_fastopen_status_ = TCP_FASTOPEN_SLOW_CONNECT_RETURN; |
| 721 return socket_->WaitForWrite(buf, buf_len, callback); | 714 return socket_->WaitForWrite(buf, buf_len, callback); |
| 722 } | 715 } |
| 723 | 716 |
| 724 void TCPSocketPosix::NotifySocketPerformanceWatcher() { | 717 void TCPSocketPosix::NotifySocketPerformanceWatcher() { |
| 725 #if defined(TCP_INFO) | 718 #if defined(TCP_INFO) |
| 726 const base::TimeTicks now_ticks = tick_clock_->NowTicks(); | |
| 727 // Do not notify |socket_performance_watcher_| if the last notification was | |
| 728 // recent than |rtt_notifications_minimum_interval_| ago. This helps in | |
| 729 // reducing the overall overhead of the tcp_info syscalls. | |
| 730 if (now_ticks - last_rtt_notification_ < rtt_notifications_minimum_interval_) | |
| 731 return; | |
| 732 | |
|
Ryan Hamilton
2017/02/15 23:08:17
This is a nice cleanup.
Should we do something si
tbansal1
2017/02/15 23:16:12
TCPSocketWin currently does not push any RTT readi
Ryan Hamilton
2017/02/16 00:01:42
Since windows is huge part of Chrome's user base,
tbansal1
2017/02/16 00:10:58
Filed https://bugs.chromium.org/p/chromium/issues/
| |
| 733 // Check if |socket_performance_watcher_| is interested in receiving a RTT | 719 // Check if |socket_performance_watcher_| is interested in receiving a RTT |
| 734 // update notification. | 720 // update notification. |
| 735 if (!socket_performance_watcher_ || | 721 if (!socket_performance_watcher_ || |
| 736 !socket_performance_watcher_->ShouldNotifyUpdatedRTT()) { | 722 !socket_performance_watcher_->ShouldNotifyUpdatedRTT()) { |
|
Ryan Hamilton
2017/02/15 23:08:17
Ah! I think I understand your API choice. Getting
tbansal1
2017/02/15 23:16:12
Right. (see the other comment)
| |
| 737 return; | 723 return; |
| 738 } | 724 } |
| 739 | 725 |
| 740 tcp_info info; | 726 tcp_info info; |
| 741 if (!GetTcpInfo(socket_->socket_fd(), &info)) | 727 if (!GetTcpInfo(socket_->socket_fd(), &info)) |
| 742 return; | 728 return; |
| 743 | 729 |
| 744 // Only notify the |socket_performance_watcher_| if the RTT in |tcp_info| | 730 // Only notify the |socket_performance_watcher_| if the RTT in |tcp_info| |
| 745 // struct was populated. A value of 0 may be valid in certain cases | 731 // struct was populated. A value of 0 may be valid in certain cases |
| 746 // (on very fast networks), but it is discarded. This means that | 732 // (on very fast networks), but it is discarded. This means that |
| 747 // some of the RTT values may be missed, but the values that are kept are | 733 // some of the RTT values may be missed, but the values that are kept are |
| 748 // guaranteed to be correct. | 734 // guaranteed to be correct. |
| 749 if (info.tcpi_rtt == 0 && info.tcpi_rttvar == 0) | 735 if (info.tcpi_rtt == 0 && info.tcpi_rttvar == 0) |
| 750 return; | 736 return; |
| 751 | 737 |
| 752 socket_performance_watcher_->OnUpdatedRTTAvailable( | 738 socket_performance_watcher_->OnUpdatedRTTAvailable( |
| 753 base::TimeDelta::FromMicroseconds(info.tcpi_rtt)); | 739 base::TimeDelta::FromMicroseconds(info.tcpi_rtt)); |
| 754 last_rtt_notification_ = now_ticks; | |
| 755 #endif // defined(TCP_INFO) | 740 #endif // defined(TCP_INFO) |
| 756 } | 741 } |
| 757 | 742 |
| 758 void TCPSocketPosix::UpdateTCPFastOpenStatusAfterRead() { | 743 void TCPSocketPosix::UpdateTCPFastOpenStatusAfterRead() { |
| 759 DCHECK(tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN || | 744 DCHECK(tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN || |
| 760 tcp_fastopen_status_ == TCP_FASTOPEN_SLOW_CONNECT_RETURN); | 745 tcp_fastopen_status_ == TCP_FASTOPEN_SLOW_CONNECT_RETURN); |
| 761 | 746 |
| 762 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { | 747 if (tcp_fastopen_write_attempted_ && !tcp_fastopen_connected_) { |
| 763 // TCP FastOpen connect-with-write was attempted, and failed. | 748 // TCP FastOpen connect-with-write was attempted, and failed. |
| 764 tcp_fastopen_status_ = | 749 tcp_fastopen_status_ = |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 809 if (info.tcpi_rtt > 0) { | 794 if (info.tcpi_rtt > 0) { |
| 810 *out_rtt = base::TimeDelta::FromMicroseconds(info.tcpi_rtt); | 795 *out_rtt = base::TimeDelta::FromMicroseconds(info.tcpi_rtt); |
| 811 return true; | 796 return true; |
| 812 } | 797 } |
| 813 } | 798 } |
| 814 #endif // defined(TCP_INFO) | 799 #endif // defined(TCP_INFO) |
| 815 return false; | 800 return false; |
| 816 } | 801 } |
| 817 | 802 |
| 818 } // namespace net | 803 } // namespace net |
| OLD | NEW |