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