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 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 // `man 7 tcp`. | 50 // `man 7 tcp`. |
| 51 bool SetTCPNoDelay(int fd, bool no_delay) { | 51 bool SetTCPNoDelay(int fd, bool no_delay) { |
| 52 int on = no_delay ? 1 : 0; | 52 int on = no_delay ? 1 : 0; |
| 53 int error = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); | 53 int error = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 54 return error == 0; | 54 return error == 0; |
| 55 } | 55 } |
| 56 | 56 |
| 57 // SetTCPKeepAlive sets SO_KEEPALIVE. | 57 // SetTCPKeepAlive sets SO_KEEPALIVE. |
| 58 bool SetTCPKeepAlive(int fd, bool enable, int delay) { | 58 bool SetTCPKeepAlive(int fd, bool enable, int delay) { |
| 59 int on = enable ? 1 : 0; | 59 int on = enable ? 1 : 0; |
| 60 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on))) { | 60 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on))) { |
|
Jana
2015/04/06 23:20:00
Nit: Perhaps add a comment here to say that this s
mmenke
2015/04/07 15:28:06
Done.
| |
| 61 PLOG(ERROR) << "Failed to set SO_KEEPALIVE on fd: " << fd; | 61 PLOG(ERROR) << "Failed to set SO_KEEPALIVE on fd: " << fd; |
| 62 return false; | 62 return false; |
| 63 } | 63 } |
| 64 | 64 |
| 65 // If we disabled TCP keep alive, our work is done here. | 65 // If we disabled TCP keep alive, our work is done here. |
| 66 if (!enable) | 66 if (!enable) |
| 67 return true; | 67 return true; |
| 68 | 68 |
| 69 #if defined(OS_LINUX) || defined(OS_ANDROID) | 69 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 70 // Set seconds until first TCP keep alive. | 70 // Set seconds until first TCP keep alive. |
| 71 if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &delay, sizeof(delay))) { | 71 if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &delay, sizeof(delay))) { |
| 72 PLOG(ERROR) << "Failed to set TCP_KEEPIDLE on fd: " << fd; | 72 PLOG(ERROR) << "Failed to set TCP_KEEPIDLE on fd: " << fd; |
| 73 return false; | 73 return false; |
| 74 } | 74 } |
| 75 // Set seconds between TCP keep alives. | 75 // Set seconds between TCP keep alives. |
| 76 if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &delay, sizeof(delay))) { | 76 if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &delay, sizeof(delay))) { |
| 77 PLOG(ERROR) << "Failed to set TCP_KEEPINTVL on fd: " << fd; | 77 PLOG(ERROR) << "Failed to set TCP_KEEPINTVL on fd: " << fd; |
| 78 return false; | 78 return false; |
| 79 } | 79 } |
| 80 #elif defined(OS_MACOSX) || defined(OS_IOS) | |
| 81 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &delay, sizeof(delay))) { | |
| 82 PLOG(ERROR) << "Failed to set TCP_KEEPALIVE on fd: " << fd; | |
| 83 return false; | |
| 84 } | |
| 80 #endif | 85 #endif |
|
Jana
2015/04/06 23:20:00
Should there be a #else? What are the platforms th
mmenke
2015/04/07 15:28:06
There's also ChromeOS, but I believe that has OS_L
Jana
2015/04/07 19:47:19
I think that the FBSD code is close to the MacOS c
| |
| 81 return true; | 86 return true; |
| 82 } | 87 } |
| 83 | 88 |
| 84 #if defined(OS_LINUX) || defined(OS_ANDROID) | 89 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 85 // Checks if the kernel supports TCP FastOpen. | 90 // Checks if the kernel supports TCP FastOpen. |
| 86 bool SystemSupportsTCPFastOpen() { | 91 bool SystemSupportsTCPFastOpen() { |
| 87 const base::FilePath::CharType kTCPFastOpenProcFilePath[] = | 92 const base::FilePath::CharType kTCPFastOpenProcFilePath[] = |
| 88 "/proc/sys/net/ipv4/tcp_fastopen"; | 93 "/proc/sys/net/ipv4/tcp_fastopen"; |
| 89 std::string system_supports_tcp_fastopen; | 94 std::string system_supports_tcp_fastopen; |
| 90 if (!base::ReadFileToString(base::FilePath(kTCPFastOpenProcFilePath), | 95 if (!base::ReadFileToString(base::FilePath(kTCPFastOpenProcFilePath), |
| (...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 732 } | 737 } |
| 733 } else { | 738 } else { |
| 734 tcp_fastopen_status_ = | 739 tcp_fastopen_status_ = |
| 735 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? | 740 (tcp_fastopen_status_ == TCP_FASTOPEN_FAST_CONNECT_RETURN ? |
| 736 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED : | 741 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED : |
| 737 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED); | 742 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED); |
| 738 } | 743 } |
| 739 } | 744 } |
| 740 | 745 |
| 741 } // namespace net | 746 } // namespace net |
| OLD | NEW |