1 // 2 // 3 // Copyright 2015 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 // 18 19 #ifndef GRPC_SRC_CORE_LIB_IOMGR_SOCKET_UTILS_POSIX_H 20 #define GRPC_SRC_CORE_LIB_IOMGR_SOCKET_UTILS_POSIX_H 21 22 #include <grpc/event_engine/endpoint_config.h> 23 #include <grpc/impl/grpc_types.h> 24 #include <grpc/support/port_platform.h> 25 26 #include "src/core/lib/iomgr/error.h" 27 #include "src/core/lib/iomgr/resolve_address.h" 28 #include "src/core/lib/iomgr/socket_factory_posix.h" 29 #include "src/core/lib/iomgr/socket_mutator.h" 30 #include "src/core/lib/resource_quota/resource_quota.h" 31 32 #ifdef GRPC_LINUX_ERRQUEUE 33 #ifndef SO_ZEROCOPY 34 #define SO_ZEROCOPY 60 35 #endif 36 #ifndef SO_EE_ORIGIN_ZEROCOPY 37 #define SO_EE_ORIGIN_ZEROCOPY 5 38 #endif 39 #endif // ifdef GRPC_LINUX_ERRQUEUE 40 41 namespace grpc_core { 42 43 struct PosixTcpOptions { 44 static constexpr int kDefaultReadChunkSize = 8192; 45 static constexpr int kDefaultMinReadChunksize = 256; 46 static constexpr int kDefaultMaxReadChunksize = 4 * 1024 * 1024; 47 static constexpr int kZerocpTxEnabledDefault = 0; 48 static constexpr int kMaxChunkSize = 32 * 1024 * 1024; 49 static constexpr int kDefaultMaxSends = 4; 50 static constexpr size_t kDefaultSendBytesThreshold = 16 * 1024; 51 // Let the system decide the proper buffer size. 52 static constexpr int kReadBufferSizeUnset = -1; 53 static constexpr int kDscpNotSet = -1; 54 int tcp_read_chunk_size = kDefaultReadChunkSize; 55 int tcp_min_read_chunk_size = kDefaultMinReadChunksize; 56 int tcp_max_read_chunk_size = kDefaultMaxReadChunksize; 57 int tcp_tx_zerocopy_send_bytes_threshold = kDefaultSendBytesThreshold; 58 int tcp_tx_zerocopy_max_simultaneous_sends = kDefaultMaxSends; 59 int tcp_receive_buffer_size = kReadBufferSizeUnset; 60 bool tcp_tx_zero_copy_enabled = kZerocpTxEnabledDefault; 61 int keep_alive_time_ms = 0; 62 int keep_alive_timeout_ms = 0; 63 int dscp = kDscpNotSet; 64 bool expand_wildcard_addrs = false; 65 bool allow_reuse_port = false; 66 RefCountedPtr<ResourceQuota> resource_quota; 67 struct grpc_socket_mutator* socket_mutator = nullptr; 68 PosixTcpOptions() = default; 69 // Move ctor PosixTcpOptionsPosixTcpOptions70 PosixTcpOptions(PosixTcpOptions&& other) noexcept { 71 socket_mutator = std::exchange(other.socket_mutator, nullptr); 72 resource_quota = std::move(other.resource_quota); 73 CopyIntegerOptions(other); 74 } 75 // Move assignment 76 PosixTcpOptions& operator=(PosixTcpOptions&& other) noexcept { 77 if (socket_mutator != nullptr) { 78 grpc_socket_mutator_unref(socket_mutator); 79 } 80 socket_mutator = std::exchange(other.socket_mutator, nullptr); 81 resource_quota = std::move(other.resource_quota); 82 CopyIntegerOptions(other); 83 return *this; 84 } 85 // Copy ctor PosixTcpOptionsPosixTcpOptions86 PosixTcpOptions(const PosixTcpOptions& other) { 87 if (other.socket_mutator != nullptr) { 88 socket_mutator = grpc_socket_mutator_ref(other.socket_mutator); 89 } 90 resource_quota = other.resource_quota; 91 CopyIntegerOptions(other); 92 } 93 // Copy assignment 94 PosixTcpOptions& operator=(const PosixTcpOptions& other) { 95 if (&other == this) { 96 return *this; 97 } 98 if (socket_mutator != nullptr) { 99 grpc_socket_mutator_unref(socket_mutator); 100 socket_mutator = nullptr; 101 } 102 if (other.socket_mutator != nullptr) { 103 socket_mutator = grpc_socket_mutator_ref(other.socket_mutator); 104 } 105 resource_quota = other.resource_quota; 106 CopyIntegerOptions(other); 107 return *this; 108 } 109 // Destructor. ~PosixTcpOptionsPosixTcpOptions110 ~PosixTcpOptions() { 111 if (socket_mutator != nullptr) { 112 grpc_socket_mutator_unref(socket_mutator); 113 } 114 } 115 116 private: CopyIntegerOptionsPosixTcpOptions117 void CopyIntegerOptions(const PosixTcpOptions& other) { 118 tcp_read_chunk_size = other.tcp_read_chunk_size; 119 tcp_min_read_chunk_size = other.tcp_min_read_chunk_size; 120 tcp_max_read_chunk_size = other.tcp_max_read_chunk_size; 121 tcp_tx_zerocopy_send_bytes_threshold = 122 other.tcp_tx_zerocopy_send_bytes_threshold; 123 tcp_tx_zerocopy_max_simultaneous_sends = 124 other.tcp_tx_zerocopy_max_simultaneous_sends; 125 tcp_tx_zero_copy_enabled = other.tcp_tx_zero_copy_enabled; 126 keep_alive_time_ms = other.keep_alive_time_ms; 127 keep_alive_timeout_ms = other.keep_alive_timeout_ms; 128 expand_wildcard_addrs = other.expand_wildcard_addrs; 129 allow_reuse_port = other.allow_reuse_port; 130 dscp = other.dscp; 131 } 132 }; 133 134 } // namespace grpc_core 135 136 grpc_core::PosixTcpOptions TcpOptionsFromEndpointConfig( 137 const grpc_event_engine::experimental::EndpointConfig& config); 138 139 // a wrapper for accept or accept4 140 int grpc_accept4(int sockfd, grpc_resolved_address* resolved_addr, int nonblock, 141 int cloexec); 142 143 // set a socket to use zerocopy 144 grpc_error_handle grpc_set_socket_zerocopy(int fd); 145 146 // set a socket to non blocking mode 147 grpc_error_handle grpc_set_socket_nonblocking(int fd, int non_blocking); 148 149 // set a socket to close on exec 150 grpc_error_handle grpc_set_socket_cloexec(int fd, int close_on_exec); 151 152 // set a socket to reuse old addresses 153 grpc_error_handle grpc_set_socket_reuse_addr(int fd, int reuse); 154 155 // return true if SO_REUSEPORT is supported 156 bool grpc_is_socket_reuse_port_supported(); 157 158 // disable nagle 159 grpc_error_handle grpc_set_socket_low_latency(int fd, int low_latency); 160 161 // set SO_REUSEPORT 162 grpc_error_handle grpc_set_socket_reuse_port(int fd, int reuse); 163 164 /* Set Differentiated Services Code Point (DSCP) */ 165 grpc_error_handle grpc_set_socket_dscp(int fd, int dscp); 166 167 // Configure the default values for TCP_USER_TIMEOUT 168 void config_default_tcp_user_timeout(bool enable, int timeout, bool is_client); 169 170 // Set TCP_USER_TIMEOUT 171 grpc_error_handle grpc_set_socket_tcp_user_timeout( 172 int fd, const grpc_core::PosixTcpOptions& options, bool is_client); 173 174 // Returns true if this system can create AF_INET6 sockets bound to ::1. 175 // The value is probed once, and cached for the life of the process. 176 177 // This is more restrictive than checking for socket(AF_INET6) to succeed, 178 // because Linux with "net.ipv6.conf.all.disable_ipv6 = 1" is able to create 179 // and bind IPv6 sockets, but cannot connect to a getsockname() of [::]:port 180 // without a valid loopback interface. Rather than expose this half-broken 181 // state to library users, we turn off IPv6 sockets. 182 int grpc_ipv6_loopback_available(void); 183 184 // Tries to set SO_NOSIGPIPE if available on this platform. 185 // If SO_NO_SIGPIPE is not available, returns 1. 186 grpc_error_handle grpc_set_socket_no_sigpipe_if_possible(int fd); 187 188 // Tries to set IP_PKTINFO if available on this platform. 189 // If IP_PKTINFO is not available, returns 1. 190 grpc_error_handle grpc_set_socket_ip_pktinfo_if_possible(int fd); 191 192 // Tries to set IPV6_RECVPKTINFO if available on this platform. 193 // If IPV6_RECVPKTINFO is not available, returns 1. 194 grpc_error_handle grpc_set_socket_ipv6_recvpktinfo_if_possible(int fd); 195 196 // Tries to set the socket's send buffer to given size. 197 grpc_error_handle grpc_set_socket_sndbuf(int fd, int buffer_size_bytes); 198 199 // Tries to set the socket's receive buffer to given size. 200 grpc_error_handle grpc_set_socket_rcvbuf(int fd, int buffer_size_bytes); 201 202 // Tries to set the socket using a grpc_socket_mutator 203 grpc_error_handle grpc_set_socket_with_mutator(int fd, grpc_fd_usage usage, 204 grpc_socket_mutator* mutator); 205 206 // Extracts the first socket mutator from config if any and applies on the fd. 207 // 208 grpc_error_handle grpc_apply_socket_mutator_in_args( 209 int fd, grpc_fd_usage usage, const grpc_core::PosixTcpOptions& options); 210 211 // An enum to keep track of IPv4/IPv6 socket modes. 212 213 // Currently, this information is only used when a socket is first created, but 214 // in the future we may wish to store it alongside the fd. This would let calls 215 // like sendto() know which family to use without asking the kernel first. 216 typedef enum grpc_dualstack_mode { 217 // Uninitialized, or a non-IP socket. 218 GRPC_DSMODE_NONE, 219 // AF_INET only. 220 GRPC_DSMODE_IPV4, 221 // AF_INET6 only, because IPV6_V6ONLY could not be cleared. 222 GRPC_DSMODE_IPV6, 223 // AF_INET6, which also supports ::ffff-mapped IPv4 addresses. 224 GRPC_DSMODE_DUALSTACK 225 } grpc_dualstack_mode; 226 227 // Only tests should use this flag. 228 extern int grpc_forbid_dualstack_sockets_for_testing; 229 230 // Tries to set the socket to dualstack. Returns 1 on success. 231 int grpc_set_socket_dualstack(int fd); 232 233 // Creates a new socket for connecting to (or listening on) an address. 234 235 // If addr is AF_INET6, this creates an IPv6 socket first. If that fails, 236 // and addr is within ::ffff:0.0.0.0/96, then it automatically falls back to 237 // an IPv4 socket. 238 239 // If addr is AF_INET, AF_UNIX, or anything else, then this is similar to 240 // calling socket() directly. 241 242 // Returns an fd on success, otherwise returns -1 with errno set to the result 243 // of a failed socket() call. 244 245 // The *dsmode output indicates which address family was actually created. 246 // The recommended way to use this is: 247 // - First convert to IPv6 using grpc_sockaddr_to_v4mapped(). 248 // - Create the socket. 249 // - If *dsmode is IPV4, use grpc_sockaddr_is_v4mapped() to convert back to 250 // IPv4, so that bind() or connect() see the correct family. 251 // Also, it's important to distinguish between DUALSTACK and IPV6 when 252 // listening on the [::] wildcard address. 253 grpc_error_handle grpc_create_dualstack_socket( 254 const grpc_resolved_address* addr, int type, int protocol, 255 grpc_dualstack_mode* dsmode, int* newfd); 256 257 // Same as grpc_create_dualstack_socket(), but use the given socket factory (if 258 // non-null) to create the socket, rather than calling socket() directly. 259 grpc_error_handle grpc_create_dualstack_socket_using_factory( 260 grpc_socket_factory* factory, const grpc_resolved_address* addr, int type, 261 int protocol, grpc_dualstack_mode* dsmode, int* newfd); 262 263 #endif // GRPC_SRC_CORE_LIB_IOMGR_SOCKET_UTILS_POSIX_H 264