Debian Codegraph @

src/core/lib/iomgr/tcp_server_utils_posix_common.cc

raw
1 /*
2 *
3 * Copyright 2017 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 "src/core/lib/iomgr/port.h"
22
23 #ifdef GRPC_POSIX_SOCKET_TCP_SERVER_UTILS_COMMON
24
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/socket.h>
30
31 #include <string>
32
33 #include "absl/strings/str_cat.h"
34
35 #include <grpc/support/alloc.h>
36 #include <grpc/support/log.h>
37 #include <grpc/support/sync.h>
38
39 #include "src/core/lib/address_utils/sockaddr_utils.h"
40 #include "src/core/lib/iomgr/error.h"
41 #include "src/core/lib/iomgr/sockaddr.h"
42 #include "src/core/lib/iomgr/tcp_server_utils_posix.h"
43 #include "src/core/lib/iomgr/unix_sockets_posix.h"
44
45 #define MIN_SAFE_ACCEPT_QUEUE_SIZE 100
46
47 static gpr_once s_init_max_accept_queue_size = GPR_ONCE_INIT;
48 static int s_max_accept_queue_size;
49
50 /* get max listen queue size on linux */
51 static void init_max_accept_queue_size(void) {
52 int n = SOMAXCONN;
53 char buf[64];
54 FILE* fp = fopen("/proc/sys/net/core/somaxconn", "r");
55 if (fp == nullptr) {
56 /* 2.4 kernel. */
57 s_max_accept_queue_size = SOMAXCONN;
58 return;
59 }
60 if (fgets(buf, sizeof buf, fp)) {
61 char* end;
62 long i = strtol(buf, &end, 10);
63 if (i > 0 && i <= INT_MAX && end && *end == '\n') {
64 n = static_cast<int>(i);
65 }
66 }
67 fclose(fp);
68 s_max_accept_queue_size = n;
69
70 if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE) {
71 gpr_log(GPR_INFO,
72 "Suspiciously small accept queue (%d) will probably lead to "
73 "connection drops",
74 s_max_accept_queue_size);
75 }
76 }
77
78 static int get_max_accept_queue_size(void) {
79 gpr_once_init(&s_init_max_accept_queue_size, init_max_accept_queue_size);
80 return s_max_accept_queue_size;
81 }
82
83 static grpc_error_handle add_socket_to_server(grpc_tcp_server* s, int fd,
84 const grpc_resolved_address* addr,
85 unsigned port_index,
86 unsigned fd_index,
87 grpc_tcp_listener** listener) {
88 *listener = nullptr;
89 int port = -1;
90
91 grpc_error_handle err =
92 grpc_tcp_server_prepare_socket(s, fd, addr, s->so_reuseport, &port);
93 if (!err.ok()) return err;
94 GPR_ASSERT(port > 0);
95 absl::StatusOr<std::string> addr_str = grpc_sockaddr_to_string(addr, true);
96 if (!addr_str.ok()) {
97 return GRPC_ERROR_CREATE(addr_str.status().ToString());
98 }
99 std::string name = absl::StrCat("tcp-server-listener:", addr_str.value());
100 gpr_mu_lock(&s->mu);
101 s->nports++;
102 GPR_ASSERT(!s->on_accept_cb && "must add ports before starting server");
103 grpc_tcp_listener* sp =
104 static_cast<grpc_tcp_listener*>(gpr_malloc(sizeof(grpc_tcp_listener)));
105 sp->next = nullptr;
106 if (s->head == nullptr) {
107 s->head = sp;
108 } else {
109 s->tail->next = sp;
110 }
111 s->tail = sp;
112 sp->server = s;
113 sp->fd = fd;
114 sp->emfd = grpc_fd_create(fd, name.c_str(), true);
115 memcpy(&sp->addr, addr, sizeof(grpc_resolved_address));
116 sp->port = port;
117 sp->port_index = port_index;
118 sp->fd_index = fd_index;
119 sp->is_sibling = 0;
120 sp->sibling = nullptr;
121 GPR_ASSERT(sp->emfd);
122 gpr_mu_unlock(&s->mu);
123
124 *listener = sp;
125 return err;
126 }
127
128 /* If successful, add a listener to s for addr, set *dsmode for the socket, and
129 return the *listener. */
130 grpc_error_handle grpc_tcp_server_add_addr(grpc_tcp_server* s,
131 const grpc_resolved_address* addr,
132 unsigned port_index,
133 unsigned fd_index,
134 grpc_dualstack_mode* dsmode,
135 grpc_tcp_listener** listener) {
136 grpc_resolved_address addr4_copy;
137 int fd;
138 grpc_error_handle err =
139 grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, dsmode, &fd);
140 if (!err.ok()) {
141 return err;
142 }
143 if (*dsmode == GRPC_DSMODE_IPV4 &&
144 grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) {
145 addr = &addr4_copy;
146 }
147 return add_socket_to_server(s, fd, addr, port_index, fd_index, listener);
148 }
149
150 /* Prepare a recently-created socket for listening. */
151 grpc_error_handle grpc_tcp_server_prepare_socket(
152 grpc_tcp_server* s, int fd, const grpc_resolved_address* addr,
153 bool so_reuseport, int* port) {
154 grpc_resolved_address sockname_temp;
155 grpc_error_handle err;
156
157 GPR_ASSERT(fd >= 0);
158
159 if (so_reuseport && !grpc_is_unix_socket(addr)) {
160 err = grpc_set_socket_reuse_port(fd, 1);
161 if (!err.ok()) goto error;
162 }
163
164 #ifdef GRPC_LINUX_ERRQUEUE
165 err = grpc_set_socket_zerocopy(fd);
166 if (!err.ok()) {
167 /* it's not fatal, so just log it. */
168 gpr_log(GPR_DEBUG, "Node does not support SO_ZEROCOPY, continuing.");
169 }
170 #endif
171 err = grpc_set_socket_nonblocking(fd, 1);
172 if (!err.ok()) goto error;
173 err = grpc_set_socket_cloexec(fd, 1);
174 if (!err.ok()) goto error;
175 if (!grpc_is_unix_socket(addr)) {
176 err = grpc_set_socket_low_latency(fd, 1);
177 if (!err.ok()) goto error;
178 err = grpc_set_socket_reuse_addr(fd, 1);
179 if (!err.ok()) goto error;
180 err =
181 grpc_set_socket_tcp_user_timeout(fd, s->options, false /* is_client */);
182 if (!err.ok()) goto error;
183 }
184 err = grpc_set_socket_no_sigpipe_if_possible(fd);
185 if (!err.ok()) goto error;
186
187 err = grpc_apply_socket_mutator_in_args(fd, GRPC_FD_SERVER_LISTENER_USAGE,
188 s->options);
189 if (!err.ok()) goto error;
190
191 if (bind(fd, reinterpret_cast<grpc_sockaddr*>(const_cast<char*>(addr->addr)),
192 addr->len) < 0) {
193 err = GRPC_OS_ERROR(errno, "bind");
194 goto error;
195 }
196
197 if (listen(fd, get_max_accept_queue_size()) < 0) {
198 err = GRPC_OS_ERROR(errno, "listen");
199 goto error;
200 }
201
202 sockname_temp.len = static_cast<socklen_t>(sizeof(struct sockaddr_storage));
203
204 if (getsockname(fd, reinterpret_cast<grpc_sockaddr*>(sockname_temp.addr),
205 &sockname_temp.len) < 0) {
206 err = GRPC_OS_ERROR(errno, "getsockname");
207 goto error;
208 }
209
210 *port = grpc_sockaddr_get_port(&sockname_temp);
211 return absl::OkStatus();
212
213 error:
214 GPR_ASSERT(!err.ok());
215 if (fd >= 0) {
216 close(fd);
217 }
218 grpc_error_handle ret = grpc_error_set_int(
219 GRPC_ERROR_CREATE_REFERENCING("Unable to configure socket", &err, 1),
220 grpc_core::StatusIntProperty::kFd, fd);
221 return ret;
222 }
223
224 #endif /* GRPC_POSIX_SOCKET_TCP_SERVER_UTILS_COMMON */