Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 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 |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 35 | #include "src/core/iomgr/tcp_server.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| 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 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 48 | #include "src/core/iomgr/pollset_posix.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 49 | #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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | #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 | |
| 60 | static gpr_once s_init_max_accept_queue_size; |
| 61 | static int s_max_accept_queue_size; |
| 62 | |
| 63 | /* one listening port */ |
| 64 | typedef struct { |
| 65 | int fd; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 66 | grpc_fd *emfd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | grpc_tcp_server *server; |
| 68 | } server_port; |
| 69 | |
| 70 | /* the overall server */ |
| 71 | struct grpc_tcp_server { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | 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 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 87 | grpc_tcp_server *grpc_tcp_server_create() { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 92 | 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 | |
| 100 | void 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++) { |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 105 | grpc_fd_shutdown(s->ports[i].emfd); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | } |
| 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]; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 116 | grpc_fd_orphan(sp->emfd, NULL, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | } |
| 118 | gpr_free(s->ports); |
| 119 | gpr_free(s); |
| 120 | } |
| 121 | |
| 122 | /* get max listen queue size on linux */ |
| 123 | static 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 | |
| 150 | static 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 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 155 | /* Prepare a recently-created socket for listening. */ |
| 156 | static int prepare_socket(int fd, const struct sockaddr *addr, int addr_len) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 157 | struct sockaddr_storage sockname_temp; |
| 158 | socklen_t sockname_len; |
| 159 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 160 | if (fd < 0) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 161 | 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 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 172 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 177 | 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 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 185 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 191 | |
| 192 | error: |
| 193 | if (fd >= 0) { |
| 194 | close(fd); |
| 195 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 196 | return -1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* event manager callback when reads are ready */ |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 200 | static void on_read(void *arg, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 201 | server_port *sp = arg; |
| 202 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 203 | if (!success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 204 | 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); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 211 | /* 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 213 | 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: |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 219 | grpc_fd_notify_on_read(sp->emfd, on_read, sp); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 220 | return; |
| 221 | default: |
| 222 | gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno)); |
| 223 | goto error; |
| 224 | } |
| 225 | } |
| 226 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 227 | sp->server->cb( |
| 228 | sp->server->cb_arg, |
| 229 | grpc_tcp_create(grpc_fd_create(fd), GRPC_TCP_DEFAULT_READ_SLICE_SIZE)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | abort(); |
| 233 | |
| 234 | error: |
| 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 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 242 | static int add_socket_to_server(grpc_tcp_server *s, int fd, |
| 243 | const struct sockaddr *addr, int addr_len) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 244 | server_port *sp; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 245 | int port; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 246 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 247 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 263 | } |
| 264 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 265 | return port; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int grpc_tcp_server_add_port(grpc_tcp_server *s, const struct sockaddr *addr, |
| 269 | int addr_len) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 270 | int allocated_port1 = -1; |
| 271 | int allocated_port2 = -1; |
| 272 | int i; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 273 | 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; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 279 | struct sockaddr *allocated_addr = NULL; |
| 280 | struct sockaddr_storage sockname_temp; |
| 281 | socklen_t sockname_len; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 282 | int port; |
| 283 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 284 | /* 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 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 303 | 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); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 316 | allocated_port1 = add_socket_to_server(s, fd, addr, addr_len); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 317 | if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 318 | goto done; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */ |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 322 | if (port == 0 && allocated_port1 > 0) { |
| 323 | grpc_sockaddr_set_port((struct sockaddr *)&wild4, allocated_port1); |
| 324 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 325 | 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 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame^] | 338 | allocated_port2 = add_socket_to_server(s, fd, addr, addr_len); |
| 339 | |
| 340 | done: |
| 341 | gpr_free(allocated_addr); |
| 342 | return allocated_port1 >= 0 ? allocated_port1 : allocated_port2; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | int grpc_tcp_server_get_fd(grpc_tcp_server *s, int index) { |
| 346 | return (0 <= index && index < s->nports) ? s->ports[index].fd : -1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 347 | } |
| 348 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 349 | void grpc_tcp_server_start(grpc_tcp_server *s, grpc_pollset *pollset, |
| 350 | grpc_tcp_server_cb cb, void *cb_arg) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 351 | 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++) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 359 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 363 | s->active_ports++; |
| 364 | } |
| 365 | gpr_mu_unlock(&s->mu); |
| 366 | } |