| 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 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "absl/types/optional.h" |
| 22 |
|
| 23 |
#include "src/core/lib/iomgr/port.h" |
| 24 |
|
| 25 |
#ifdef GRPC_POSIX_SOCKETUTILS |
| 26 |
#include <fcntl.h> |
| 27 |
#include <sys/socket.h> |
| 28 |
#include <unistd.h> |
| 29 |
|
| 30 |
#include <grpc/impl/codegen/grpc_types.h> |
| 31 |
#include <grpc/support/log.h> |
| 32 |
|
| 33 |
#include "src/core/lib/iomgr/sockaddr.h" |
| 34 |
#include "src/core/lib/iomgr/socket_utils_posix.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
#ifdef GRPC_POSIX_SOCKET_TCP |
| 38 |
|
| 39 |
#include "src/core/lib/event_engine/channel_args_endpoint_config.h" |
| 40 |
#include "src/core/lib/gprpp/strerror.h" |
| 41 |
#include "src/core/lib/iomgr/socket_utils_posix.h" |
| 42 |
|
| 43 |
using ::grpc_event_engine::experimental::EndpointConfig; |
| 44 |
|
| 45 |
using ::grpc_core::PosixTcpOptions; |
| 46 |
|
| 47 |
namespace { |
| 48 |
|
| 49 |
int AdjustValue(int default_value, int min_value, int max_value, |
| 50 |
absl::optional<int> actual_value) { |
| 51 |
if (!actual_value.has_value() || *actual_value < min_value || |
| 52 |
*actual_value > max_value) { |
| 53 |
return default_value; |
| 54 |
} |
| 55 |
return *actual_value; |
| 56 |
} |
| 57 |
} // namespace |
| 58 |
|
| 59 |
PosixTcpOptions TcpOptionsFromEndpointConfig(const EndpointConfig& config) { |
| 60 |
void* value; |
| 61 |
PosixTcpOptions options; |
| 62 |
options.tcp_read_chunk_size = AdjustValue( |
| 63 |
PosixTcpOptions::kDefaultReadChunkSize, 1, PosixTcpOptions::kMaxChunkSize, |
| 64 |
config.GetInt(GRPC_ARG_TCP_READ_CHUNK_SIZE)); |
| 65 |
options.tcp_min_read_chunk_size = |
| 66 |
AdjustValue(PosixTcpOptions::kDefaultMinReadChunksize, 1, |
| 67 |
PosixTcpOptions::kMaxChunkSize, |
| 68 |
config.GetInt(GRPC_ARG_TCP_MIN_READ_CHUNK_SIZE)); |
| 69 |
options.tcp_max_read_chunk_size = |
| 70 |
AdjustValue(PosixTcpOptions::kDefaultMaxReadChunksize, 1, |
| 71 |
PosixTcpOptions::kMaxChunkSize, |
| 72 |
config.GetInt(GRPC_ARG_TCP_MAX_READ_CHUNK_SIZE)); |
| 73 |
options.tcp_tx_zerocopy_send_bytes_threshold = |
| 74 |
AdjustValue(PosixTcpOptions::kDefaultSendBytesThreshold, 0, INT_MAX, |
| 75 |
config.GetInt(GRPC_ARG_TCP_TX_ZEROCOPY_SEND_BYTES_THRESHOLD)); |
| 76 |
options.tcp_tx_zerocopy_max_simultaneous_sends = |
| 77 |
AdjustValue(PosixTcpOptions::kDefaultMaxSends, 0, INT_MAX, |
| 78 |
config.GetInt(GRPC_ARG_TCP_TX_ZEROCOPY_MAX_SIMULT_SENDS)); |
| 79 |
options.tcp_tx_zero_copy_enabled = |
| 80 |
(AdjustValue(PosixTcpOptions::kZerocpTxEnabledDefault, 0, 1, |
| 81 |
config.GetInt(GRPC_ARG_TCP_TX_ZEROCOPY_ENABLED)) != 0); |
| 82 |
options.keep_alive_time_ms = |
| 83 |
AdjustValue(0, 1, INT_MAX, config.GetInt(GRPC_ARG_KEEPALIVE_TIME_MS)); |
| 84 |
options.keep_alive_timeout_ms = |
| 85 |
AdjustValue(0, 1, INT_MAX, config.GetInt(GRPC_ARG_KEEPALIVE_TIMEOUT_MS)); |
| 86 |
options.expand_wildcard_addrs = |
| 87 |
(AdjustValue(0, 1, INT_MAX, |
| 88 |
config.GetInt(GRPC_ARG_EXPAND_WILDCARD_ADDRS)) != 0); |
| 89 |
options.allow_reuse_port = |
| 90 |
(AdjustValue(0, 1, INT_MAX, config.GetInt(GRPC_ARG_ALLOW_REUSEPORT)) != |
| 91 |
0); |
| 92 |
|
| 93 |
if (options.tcp_min_read_chunk_size > options.tcp_max_read_chunk_size) { |
| 94 |
options.tcp_min_read_chunk_size = options.tcp_max_read_chunk_size; |
| 95 |
} |
| 96 |
options.tcp_read_chunk_size = grpc_core::Clamp( |
| 97 |
options.tcp_read_chunk_size, options.tcp_min_read_chunk_size, |
| 98 |
options.tcp_max_read_chunk_size); |
| 99 |
|
| 100 |
value = config.GetVoidPointer(GRPC_ARG_RESOURCE_QUOTA); |
| 101 |
if (value != nullptr) { |
| 102 |
options.resource_quota = |
| 103 |
reinterpret_cast<grpc_core::ResourceQuota*>(value)->Ref(); |
| 104 |
} |
| 105 |
value = config.GetVoidPointer(GRPC_ARG_SOCKET_MUTATOR); |
| 106 |
if (value != nullptr) { |
| 107 |
options.socket_mutator = |
| 108 |
grpc_socket_mutator_ref(static_cast<grpc_socket_mutator*>(value)); |
| 109 |
} |
| 110 |
return options; |
| 111 |
} |
| 112 |
|
| 113 |
#endif /* GRPC_POSIX_SOCKET_TCP */ |
| 114 |
|
| 115 |
#ifdef GRPC_POSIX_SOCKETUTILS |
| 116 |
|
| 117 |
int grpc_accept4(int sockfd, grpc_resolved_address* resolved_addr, int nonblock, |
| 118 |
int cloexec) { |
| 119 |
int fd, flags; |
| 120 |
fd = accept(sockfd, reinterpret_cast<grpc_sockaddr*>(resolved_addr->addr), |
| 121 |
&resolved_addr->len); |
| 122 |
if (fd >= 0) { |
| 123 |
if (nonblock) { |
| 124 |
flags = fcntl(fd, F_GETFL, 0); |
| 125 |
if (flags < 0) goto close_and_error; |
| 126 |
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0) goto close_and_error; |
| 127 |
} |
| 128 |
if (cloexec) { |
| 129 |
flags = fcntl(fd, F_GETFD, 0); |
| 130 |
if (flags < 0) goto close_and_error; |
| 131 |
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != 0) goto close_and_error; |
| 132 |
} |
| 133 |
} |
| 134 |
return fd; |
| 135 |
|
| 136 |
close_and_error: |
| 137 |
close(fd); |
| 138 |
return -1; |
| 139 |
} |
| 140 |
|
| 141 |
#endif /* GRPC_POSIX_SOCKETUTILS */ |