blob: 753e24c38e5a06ef623eb273aba04d14c509b094 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#define _GNU_SOURCE
ctiller18b49ab2014-12-09 14:39:16 -080035#include "src/core/iomgr/tcp_server.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#include <limits.h>
38#include <fcntl.h>
39#include <netinet/in.h>
40#include <netinet/tcp.h>
41#include <stdio.h>
42#include <sys/types.h>
43#include <sys/socket.h>
44#include <unistd.h>
45#include <string.h>
46#include <errno.h>
47
ctiller58393c22015-01-07 14:03:30 -080048#include "src/core/iomgr/pollset_posix.h"
ctiller18b49ab2014-12-09 14:39:16 -080049#include "src/core/iomgr/sockaddr_utils.h"
50#include "src/core/iomgr/socket_utils_posix.h"
51#include "src/core/iomgr/tcp_posix.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052#include <grpc/support/alloc.h>
53#include <grpc/support/log.h>
54#include <grpc/support/sync.h>
55#include <grpc/support/time.h>
56
57#define INIT_PORT_CAP 2
58#define MIN_SAFE_ACCEPT_QUEUE_SIZE 100
59
60static gpr_once s_init_max_accept_queue_size;
61static int s_max_accept_queue_size;
62
63/* one listening port */
64typedef struct {
65 int fd;
ctiller18b49ab2014-12-09 14:39:16 -080066 grpc_fd *emfd;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067 grpc_tcp_server *server;
68} server_port;
69
70/* the overall server */
71struct grpc_tcp_server {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072 grpc_tcp_server_cb cb;
73 void *cb_arg;
74
75 gpr_mu mu;
76 gpr_cv cv;
77
78 /* active port count: how many ports are actually still listening */
79 int active_ports;
80
81 /* all listening ports */
82 server_port *ports;
83 size_t nports;
84 size_t port_capacity;
85};
86
ctiller18b49ab2014-12-09 14:39:16 -080087grpc_tcp_server *grpc_tcp_server_create() {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088 grpc_tcp_server *s = gpr_malloc(sizeof(grpc_tcp_server));
89 gpr_mu_init(&s->mu);
90 gpr_cv_init(&s->cv);
91 s->active_ports = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092 s->cb = NULL;
93 s->cb_arg = NULL;
94 s->ports = gpr_malloc(sizeof(server_port) * INIT_PORT_CAP);
95 s->nports = 0;
96 s->port_capacity = INIT_PORT_CAP;
97 return s;
98}
99
100void grpc_tcp_server_destroy(grpc_tcp_server *s) {
101 size_t i;
102 gpr_mu_lock(&s->mu);
103 /* shutdown all fd's */
104 for (i = 0; i < s->nports; i++) {
ctiller18b49ab2014-12-09 14:39:16 -0800105 grpc_fd_shutdown(s->ports[i].emfd);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 }
107 /* wait while that happens */
108 while (s->active_ports) {
109 gpr_cv_wait(&s->cv, &s->mu, gpr_inf_future);
110 }
111 gpr_mu_unlock(&s->mu);
112
113 /* delete ALL the things */
114 for (i = 0; i < s->nports; i++) {
115 server_port *sp = &s->ports[i];
ctiller58393c22015-01-07 14:03:30 -0800116 grpc_fd_orphan(sp->emfd, NULL, NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117 }
118 gpr_free(s->ports);
119 gpr_free(s);
120}
121
122/* get max listen queue size on linux */
123static void init_max_accept_queue_size() {
124 int n = SOMAXCONN;
125 char buf[64];
126 FILE *fp = fopen("/proc/sys/net/core/somaxconn", "r");
127 if (fp == NULL) {
128 /* 2.4 kernel. */
129 s_max_accept_queue_size = SOMAXCONN;
130 return;
131 }
132 if (fgets(buf, sizeof buf, fp)) {
133 char *end;
134 long i = strtol(buf, &end, 10);
135 if (i > 0 && i <= INT_MAX && end && *end == 0) {
136 n = i;
137 }
138 }
139 fclose(fp);
140 s_max_accept_queue_size = n;
141
142 if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE) {
143 gpr_log(GPR_INFO,
144 "Suspiciously small accept queue (%d) will probably lead to "
145 "connection drops",
146 s_max_accept_queue_size);
147 }
148}
149
150static int get_max_accept_queue_size() {
151 gpr_once_init(&s_init_max_accept_queue_size, init_max_accept_queue_size);
152 return s_max_accept_queue_size;
153}
154
nnoble0c475f02014-12-05 15:37:39 -0800155/* Prepare a recently-created socket for listening. */
156static int prepare_socket(int fd, const struct sockaddr *addr, int addr_len) {
ctiller570d1f42015-01-12 16:29:52 -0800157 struct sockaddr_storage sockname_temp;
158 socklen_t sockname_len;
159
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160 if (fd < 0) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800161 goto error;
162 }
163
164 if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) ||
165 !grpc_set_socket_low_latency(fd, 1) ||
166 !grpc_set_socket_reuse_addr(fd, 1)) {
167 gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd,
168 strerror(errno));
169 goto error;
170 }
171
nnoble0c475f02014-12-05 15:37:39 -0800172 if (bind(fd, addr, addr_len) < 0) {
173 char *addr_str;
174 grpc_sockaddr_to_string(&addr_str, addr, 0);
175 gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno));
176 gpr_free(addr_str);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177 goto error;
178 }
179
180 if (listen(fd, get_max_accept_queue_size()) < 0) {
181 gpr_log(GPR_ERROR, "listen: %s", strerror(errno));
182 goto error;
183 }
184
ctiller570d1f42015-01-12 16:29:52 -0800185 sockname_len = sizeof(sockname_temp);
186 if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) {
187 goto error;
188 }
189
190 return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800191
192error:
193 if (fd >= 0) {
194 close(fd);
195 }
ctiller570d1f42015-01-12 16:29:52 -0800196 return -1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800197}
198
199/* event manager callback when reads are ready */
ctiller58393c22015-01-07 14:03:30 -0800200static void on_read(void *arg, int success) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201 server_port *sp = arg;
202
ctiller58393c22015-01-07 14:03:30 -0800203 if (!success) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800204 goto error;
205 }
206
207 /* loop until accept4 returns EAGAIN, and then re-arm notification */
208 for (;;) {
209 struct sockaddr_storage addr;
210 socklen_t addrlen = sizeof(addr);
nnoble0c475f02014-12-05 15:37:39 -0800211 /* Note: If we ever decide to return this address to the user, remember to
212 strip off the ::ffff:0.0.0.0/96 prefix first. */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213 int fd = grpc_accept4(sp->fd, (struct sockaddr *)&addr, &addrlen, 1, 1);
214 if (fd < 0) {
215 switch (errno) {
216 case EINTR:
217 continue;
218 case EAGAIN:
ctiller58393c22015-01-07 14:03:30 -0800219 grpc_fd_notify_on_read(sp->emfd, on_read, sp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800220 return;
221 default:
222 gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno));
223 goto error;
224 }
225 }
226
ctiller18b49ab2014-12-09 14:39:16 -0800227 sp->server->cb(
228 sp->server->cb_arg,
229 grpc_tcp_create(grpc_fd_create(fd), GRPC_TCP_DEFAULT_READ_SLICE_SIZE));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800230 }
231
232 abort();
233
234error:
235 gpr_mu_lock(&sp->server->mu);
236 if (0 == --sp->server->active_ports) {
237 gpr_cv_broadcast(&sp->server->cv);
238 }
239 gpr_mu_unlock(&sp->server->mu);
240}
241
nnoble0c475f02014-12-05 15:37:39 -0800242static int add_socket_to_server(grpc_tcp_server *s, int fd,
243 const struct sockaddr *addr, int addr_len) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800244 server_port *sp;
ctiller570d1f42015-01-12 16:29:52 -0800245 int port;
nnoble0c475f02014-12-05 15:37:39 -0800246
ctiller570d1f42015-01-12 16:29:52 -0800247 port = prepare_socket(fd, addr, addr_len);
248 if (port >= 0) {
249 gpr_mu_lock(&s->mu);
250 GPR_ASSERT(!s->cb && "must add ports before starting server");
251 /* append it to the list under a lock */
252 if (s->nports == s->port_capacity) {
253 s->port_capacity *= 2;
254 s->ports =
255 gpr_realloc(s->ports, sizeof(server_port *) * s->port_capacity);
256 }
257 sp = &s->ports[s->nports++];
258 sp->server = s;
259 sp->fd = fd;
260 sp->emfd = grpc_fd_create(fd);
261 GPR_ASSERT(sp->emfd);
262 gpr_mu_unlock(&s->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800263 }
264
ctiller570d1f42015-01-12 16:29:52 -0800265 return port;
nnoble0c475f02014-12-05 15:37:39 -0800266}
267
268int grpc_tcp_server_add_port(grpc_tcp_server *s, const struct sockaddr *addr,
269 int addr_len) {
ctiller570d1f42015-01-12 16:29:52 -0800270 int allocated_port1 = -1;
271 int allocated_port2 = -1;
272 int i;
nnoble0c475f02014-12-05 15:37:39 -0800273 int fd;
274 grpc_dualstack_mode dsmode;
275 struct sockaddr_in6 addr6_v4mapped;
276 struct sockaddr_in wild4;
277 struct sockaddr_in6 wild6;
278 struct sockaddr_in addr4_copy;
ctiller570d1f42015-01-12 16:29:52 -0800279 struct sockaddr *allocated_addr = NULL;
280 struct sockaddr_storage sockname_temp;
281 socklen_t sockname_len;
nnoble0c475f02014-12-05 15:37:39 -0800282 int port;
283
ctiller570d1f42015-01-12 16:29:52 -0800284 /* Check if this is a wildcard port, and if so, try to keep the port the same
285 as some previously created listener. */
286 if (grpc_sockaddr_get_port(addr) == 0) {
287 for (i = 0; i < s->nports; i++) {
288 sockname_len = sizeof(sockname_temp);
289 if (0 == getsockname(s->ports[i].fd, (struct sockaddr *)&sockname_temp,
290 &sockname_len)) {
291 port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp);
292 if (port > 0) {
293 allocated_addr = malloc(addr_len);
294 memcpy(allocated_addr, addr, addr_len);
295 grpc_sockaddr_set_port(allocated_addr, port);
296 addr = allocated_addr;
297 break;
298 }
299 }
300 }
301 }
302
nnoble0c475f02014-12-05 15:37:39 -0800303 if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) {
304 addr = (const struct sockaddr *)&addr6_v4mapped;
305 addr_len = sizeof(addr6_v4mapped);
306 }
307
308 /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */
309 if (grpc_sockaddr_is_wildcard(addr, &port)) {
310 grpc_sockaddr_make_wildcards(port, &wild4, &wild6);
311
312 /* Try listening on IPv6 first. */
313 addr = (struct sockaddr *)&wild6;
314 addr_len = sizeof(wild6);
315 fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
ctiller570d1f42015-01-12 16:29:52 -0800316 allocated_port1 = add_socket_to_server(s, fd, addr, addr_len);
nnoble0c475f02014-12-05 15:37:39 -0800317 if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) {
ctiller570d1f42015-01-12 16:29:52 -0800318 goto done;
nnoble0c475f02014-12-05 15:37:39 -0800319 }
320
321 /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */
ctiller570d1f42015-01-12 16:29:52 -0800322 if (port == 0 && allocated_port1 > 0) {
323 grpc_sockaddr_set_port((struct sockaddr *)&wild4, allocated_port1);
324 }
nnoble0c475f02014-12-05 15:37:39 -0800325 addr = (struct sockaddr *)&wild4;
326 addr_len = sizeof(wild4);
327 }
328
329 fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode);
330 if (fd < 0) {
331 gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno));
332 }
333 if (dsmode == GRPC_DSMODE_IPV4 &&
334 grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) {
335 addr = (struct sockaddr *)&addr4_copy;
336 addr_len = sizeof(addr4_copy);
337 }
ctiller570d1f42015-01-12 16:29:52 -0800338 allocated_port2 = add_socket_to_server(s, fd, addr, addr_len);
339
340done:
341 gpr_free(allocated_addr);
342 return allocated_port1 >= 0 ? allocated_port1 : allocated_port2;
nnoble0c475f02014-12-05 15:37:39 -0800343}
344
345int grpc_tcp_server_get_fd(grpc_tcp_server *s, int index) {
346 return (0 <= index && index < s->nports) ? s->ports[index].fd : -1;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800347}
348
ctiller58393c22015-01-07 14:03:30 -0800349void grpc_tcp_server_start(grpc_tcp_server *s, grpc_pollset *pollset,
350 grpc_tcp_server_cb cb, void *cb_arg) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800351 size_t i;
352 GPR_ASSERT(cb);
353 gpr_mu_lock(&s->mu);
354 GPR_ASSERT(!s->cb);
355 GPR_ASSERT(s->active_ports == 0);
356 s->cb = cb;
357 s->cb_arg = cb_arg;
358 for (i = 0; i < s->nports; i++) {
ctiller58393c22015-01-07 14:03:30 -0800359 if (pollset) {
360 grpc_pollset_add_fd(pollset, s->ports[i].emfd);
361 }
362 grpc_fd_notify_on_read(s->ports[i].emfd, on_read, &s->ports[i]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800363 s->active_ports++;
364 }
365 gpr_mu_unlock(&s->mu);
366}