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