Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
David Klempner | 78b7992 | 2015-02-04 10:18:59 -0800 | [diff] [blame] | 34 | /* FIXME: "posix" files shouldn't be depending on _GNU_SOURCE */ |
| 35 | #ifndef _GNU_SOURCE |
| 36 | #define _GNU_SOURCE |
| 37 | #endif |
| 38 | |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 39 | #include <grpc/support/port_platform.h> |
| 40 | |
Craig Tiller | a172ad7 | 2015-01-21 15:51:47 -0800 | [diff] [blame] | 41 | #ifdef GPR_POSIX_SOCKET |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 42 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 43 | #include "src/core/iomgr/tcp_server.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 45 | #include <errno.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include <fcntl.h> |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 47 | #include <limits.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | #include <netinet/in.h> |
| 49 | #include <netinet/tcp.h> |
| 50 | #include <stdio.h> |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 51 | #include <string.h> |
| 52 | #include <sys/socket.h> |
| 53 | #include <sys/stat.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | #include <sys/types.h> |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 55 | #include <sys/un.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | #include <unistd.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 58 | #include "src/core/iomgr/pollset_posix.h" |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 59 | #include "src/core/iomgr/resolve_address.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 60 | #include "src/core/iomgr/sockaddr_utils.h" |
| 61 | #include "src/core/iomgr/socket_utils_posix.h" |
| 62 | #include "src/core/iomgr/tcp_posix.h" |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 63 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | #include <grpc/support/alloc.h> |
| 65 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 66 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | #include <grpc/support/sync.h> |
| 68 | #include <grpc/support/time.h> |
| 69 | |
| 70 | #define INIT_PORT_CAP 2 |
| 71 | #define MIN_SAFE_ACCEPT_QUEUE_SIZE 100 |
| 72 | |
| 73 | static gpr_once s_init_max_accept_queue_size; |
| 74 | static int s_max_accept_queue_size; |
| 75 | |
| 76 | /* one listening port */ |
| 77 | typedef struct { |
| 78 | int fd; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 79 | grpc_fd *emfd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | grpc_tcp_server *server; |
Craig Tiller | 772c97b | 2015-02-17 08:07:34 -0800 | [diff] [blame] | 81 | union { |
| 82 | gpr_uint8 untyped[GRPC_MAX_SOCKADDR_SIZE]; |
| 83 | struct sockaddr sockaddr; |
| 84 | struct sockaddr_un un; |
| 85 | } addr; |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 86 | int addr_len; |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 87 | grpc_iomgr_closure read_closure; |
Craig Tiller | 0317b3d | 2015-06-01 21:57:03 -0700 | [diff] [blame] | 88 | grpc_iomgr_closure destroyed_closure; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 89 | } server_port; |
| 90 | |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 91 | static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) { |
| 92 | struct stat st; |
| 93 | |
| 94 | if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) { |
| 95 | unlink(un->sun_path); |
| 96 | } |
| 97 | } |
| 98 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | /* the overall server */ |
| 100 | struct grpc_tcp_server { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | grpc_tcp_server_cb cb; |
| 102 | void *cb_arg; |
| 103 | |
| 104 | gpr_mu mu; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 105 | |
| 106 | /* active port count: how many ports are actually still listening */ |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 107 | size_t active_ports; |
| 108 | /* destroyed port count: how many ports are completely destroyed */ |
| 109 | size_t destroyed_ports; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 111 | /* is this server shutting down? (boolean) */ |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 112 | int shutdown; |
| 113 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | /* all listening ports */ |
| 115 | server_port *ports; |
| 116 | size_t nports; |
| 117 | size_t port_capacity; |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 118 | |
| 119 | /* shutdown callback */ |
| 120 | void (*shutdown_complete)(void *); |
| 121 | void *shutdown_complete_arg; |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 122 | |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 123 | /* all pollsets interested in new connections */ |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 124 | grpc_pollset **pollsets; |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 125 | /* number of pollsets in the pollsets array */ |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 126 | size_t pollset_count; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 129 | grpc_tcp_server *grpc_tcp_server_create(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 130 | grpc_tcp_server *s = gpr_malloc(sizeof(grpc_tcp_server)); |
| 131 | gpr_mu_init(&s->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 132 | s->active_ports = 0; |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 133 | s->destroyed_ports = 0; |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 134 | s->shutdown = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 135 | s->cb = NULL; |
| 136 | s->cb_arg = NULL; |
| 137 | s->ports = gpr_malloc(sizeof(server_port) * INIT_PORT_CAP); |
| 138 | s->nports = 0; |
| 139 | s->port_capacity = INIT_PORT_CAP; |
| 140 | return s; |
| 141 | } |
| 142 | |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 143 | static void finish_shutdown(grpc_tcp_server *s) { |
| 144 | s->shutdown_complete(s->shutdown_complete_arg); |
| 145 | |
| 146 | gpr_mu_destroy(&s->mu); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 147 | |
| 148 | gpr_free(s->ports); |
| 149 | gpr_free(s); |
| 150 | } |
| 151 | |
| 152 | static void destroyed_port(void *server, int success) { |
| 153 | grpc_tcp_server *s = server; |
| 154 | gpr_mu_lock(&s->mu); |
| 155 | s->destroyed_ports++; |
| 156 | if (s->destroyed_ports == s->nports) { |
| 157 | gpr_mu_unlock(&s->mu); |
| 158 | finish_shutdown(s); |
| 159 | } else { |
| 160 | gpr_mu_unlock(&s->mu); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | static void dont_care_about_shutdown_completion(void *ignored) {} |
| 165 | |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 166 | /* called when all listening endpoints have been shutdown, so no further |
| 167 | events will be received on them - at this point it's safe to destroy |
| 168 | things */ |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 169 | static void deactivated_all_ports(grpc_tcp_server *s) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 171 | |
| 172 | /* delete ALL the things */ |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 173 | gpr_mu_lock(&s->mu); |
| 174 | |
| 175 | if (!s->shutdown) { |
| 176 | gpr_mu_unlock(&s->mu); |
| 177 | return; |
| 178 | } |
| 179 | |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 180 | if (s->nports) { |
| 181 | for (i = 0; i < s->nports; i++) { |
| 182 | server_port *sp = &s->ports[i]; |
| 183 | if (sp->addr.sockaddr.sa_family == AF_UNIX) { |
| 184 | unlink_if_unix_domain_socket(&sp->addr.un); |
| 185 | } |
Craig Tiller | 0317b3d | 2015-06-01 21:57:03 -0700 | [diff] [blame] | 186 | sp->destroyed_closure.cb = destroyed_port; |
| 187 | sp->destroyed_closure.cb_arg = s; |
Craig Tiller | 4b678bd | 2015-06-02 16:12:24 -0700 | [diff] [blame] | 188 | grpc_fd_orphan(sp->emfd, &sp->destroyed_closure, "tcp_listener_shutdown"); |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 189 | } |
Craig Tiller | 954d7a2 | 2015-04-13 13:01:50 -0700 | [diff] [blame] | 190 | gpr_mu_unlock(&s->mu); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 191 | } else { |
Craig Tiller | 954d7a2 | 2015-04-13 13:01:50 -0700 | [diff] [blame] | 192 | gpr_mu_unlock(&s->mu); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 193 | finish_shutdown(s); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 194 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 197 | void grpc_tcp_server_destroy( |
| 198 | grpc_tcp_server *s, void (*shutdown_complete)(void *shutdown_complete_arg), |
| 199 | void *shutdown_complete_arg) { |
| 200 | size_t i; |
| 201 | gpr_mu_lock(&s->mu); |
| 202 | |
| 203 | GPR_ASSERT(!s->shutdown); |
| 204 | s->shutdown = 1; |
| 205 | |
| 206 | s->shutdown_complete = shutdown_complete |
| 207 | ? shutdown_complete |
| 208 | : dont_care_about_shutdown_completion; |
| 209 | s->shutdown_complete_arg = shutdown_complete_arg; |
| 210 | |
| 211 | /* shutdown all fd's */ |
| 212 | if (s->active_ports) { |
| 213 | for (i = 0; i < s->nports; i++) { |
| 214 | grpc_fd_shutdown(s->ports[i].emfd); |
| 215 | } |
| 216 | gpr_mu_unlock(&s->mu); |
| 217 | } else { |
| 218 | gpr_mu_unlock(&s->mu); |
| 219 | deactivated_all_ports(s); |
| 220 | } |
| 221 | } |
| 222 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 223 | /* get max listen queue size on linux */ |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 224 | static void init_max_accept_queue_size(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 225 | int n = SOMAXCONN; |
| 226 | char buf[64]; |
| 227 | FILE *fp = fopen("/proc/sys/net/core/somaxconn", "r"); |
| 228 | if (fp == NULL) { |
| 229 | /* 2.4 kernel. */ |
| 230 | s_max_accept_queue_size = SOMAXCONN; |
| 231 | return; |
| 232 | } |
| 233 | if (fgets(buf, sizeof buf, fp)) { |
| 234 | char *end; |
| 235 | long i = strtol(buf, &end, 10); |
| 236 | if (i > 0 && i <= INT_MAX && end && *end == 0) { |
| 237 | n = i; |
| 238 | } |
| 239 | } |
| 240 | fclose(fp); |
| 241 | s_max_accept_queue_size = n; |
| 242 | |
| 243 | if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE) { |
| 244 | gpr_log(GPR_INFO, |
| 245 | "Suspiciously small accept queue (%d) will probably lead to " |
| 246 | "connection drops", |
| 247 | s_max_accept_queue_size); |
| 248 | } |
| 249 | } |
| 250 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 251 | static int get_max_accept_queue_size(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 252 | gpr_once_init(&s_init_max_accept_queue_size, init_max_accept_queue_size); |
| 253 | return s_max_accept_queue_size; |
| 254 | } |
| 255 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 256 | /* Prepare a recently-created socket for listening. */ |
| 257 | static int prepare_socket(int fd, const struct sockaddr *addr, int addr_len) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 258 | struct sockaddr_storage sockname_temp; |
| 259 | socklen_t sockname_len; |
| 260 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 261 | if (fd < 0) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 262 | goto error; |
| 263 | } |
| 264 | |
| 265 | if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) || |
Craig Tiller | d209ed0 | 2015-02-13 23:18:15 -0800 | [diff] [blame] | 266 | (addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency(fd, 1) || |
Craig Tiller | 2da0296 | 2015-05-06 16:14:25 -0700 | [diff] [blame] | 267 | !grpc_set_socket_reuse_addr(fd, 1))) || |
| 268 | !grpc_set_socket_no_sigpipe_if_possible(fd)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 269 | gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd, |
| 270 | strerror(errno)); |
| 271 | goto error; |
| 272 | } |
| 273 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 274 | if (bind(fd, addr, addr_len) < 0) { |
| 275 | char *addr_str; |
| 276 | grpc_sockaddr_to_string(&addr_str, addr, 0); |
| 277 | gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno)); |
| 278 | gpr_free(addr_str); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 279 | goto error; |
| 280 | } |
| 281 | |
| 282 | if (listen(fd, get_max_accept_queue_size()) < 0) { |
| 283 | gpr_log(GPR_ERROR, "listen: %s", strerror(errno)); |
| 284 | goto error; |
| 285 | } |
| 286 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 287 | sockname_len = sizeof(sockname_temp); |
| 288 | if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) { |
| 289 | goto error; |
| 290 | } |
| 291 | |
| 292 | return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 293 | |
| 294 | error: |
| 295 | if (fd >= 0) { |
| 296 | close(fd); |
| 297 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 298 | return -1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* event manager callback when reads are ready */ |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 302 | static void on_read(void *arg, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 303 | server_port *sp = arg; |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 304 | grpc_fd *fdobj; |
| 305 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 306 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 307 | if (!success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 308 | goto error; |
| 309 | } |
| 310 | |
| 311 | /* loop until accept4 returns EAGAIN, and then re-arm notification */ |
| 312 | for (;;) { |
| 313 | struct sockaddr_storage addr; |
| 314 | socklen_t addrlen = sizeof(addr); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 315 | char *addr_str; |
| 316 | char *name; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 317 | /* Note: If we ever decide to return this address to the user, remember to |
| 318 | strip off the ::ffff:0.0.0.0/96 prefix first. */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 319 | int fd = grpc_accept4(sp->fd, (struct sockaddr *)&addr, &addrlen, 1, 1); |
| 320 | if (fd < 0) { |
| 321 | switch (errno) { |
| 322 | case EINTR: |
| 323 | continue; |
| 324 | case EAGAIN: |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 325 | grpc_fd_notify_on_read(sp->emfd, &sp->read_closure); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 326 | return; |
| 327 | default: |
| 328 | gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno)); |
| 329 | goto error; |
| 330 | } |
| 331 | } |
| 332 | |
Craig Tiller | 2da0296 | 2015-05-06 16:14:25 -0700 | [diff] [blame] | 333 | grpc_set_socket_no_sigpipe_if_possible(fd); |
| 334 | |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 335 | grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1); |
| 336 | gpr_asprintf(&name, "tcp-server-connection:%s", addr_str); |
| 337 | |
Craig Tiller | 8e0b08a | 2015-06-01 17:04:17 -0700 | [diff] [blame] | 338 | fdobj = grpc_fd_create(fd, name); |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 339 | /* TODO(ctiller): revise this when we have server-side sharding |
| 340 | of channels -- we certainly should not be automatically adding every |
| 341 | incoming channel to every pollset owned by the server */ |
| 342 | for (i = 0; i < sp->server->pollset_count; i++) { |
| 343 | grpc_pollset_add_fd(sp->server->pollsets[i], fdobj); |
| 344 | } |
Craig Tiller | 8674cb1 | 2015-06-05 07:09:25 -0700 | [diff] [blame] | 345 | sp->server->cb(sp->server->cb_arg, |
| 346 | grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE)); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 347 | |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 348 | gpr_free(name); |
Craig Tiller | 8e0b08a | 2015-06-01 17:04:17 -0700 | [diff] [blame] | 349 | gpr_free(addr_str); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | abort(); |
| 353 | |
| 354 | error: |
| 355 | gpr_mu_lock(&sp->server->mu); |
| 356 | if (0 == --sp->server->active_ports) { |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 357 | gpr_mu_unlock(&sp->server->mu); |
| 358 | deactivated_all_ports(sp->server); |
| 359 | } else { |
| 360 | gpr_mu_unlock(&sp->server->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 361 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 362 | } |
| 363 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 364 | static int add_socket_to_server(grpc_tcp_server *s, int fd, |
| 365 | const struct sockaddr *addr, int addr_len) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 366 | server_port *sp; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 367 | int port; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 368 | char *addr_str; |
| 369 | char *name; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 370 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 371 | port = prepare_socket(fd, addr, addr_len); |
| 372 | if (port >= 0) { |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 373 | grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1); |
| 374 | gpr_asprintf(&name, "tcp-server-listener:%s", addr_str); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 375 | gpr_mu_lock(&s->mu); |
| 376 | GPR_ASSERT(!s->cb && "must add ports before starting server"); |
| 377 | /* append it to the list under a lock */ |
| 378 | if (s->nports == s->port_capacity) { |
| 379 | s->port_capacity *= 2; |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 380 | s->ports = gpr_realloc(s->ports, sizeof(server_port) * s->port_capacity); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 381 | } |
| 382 | sp = &s->ports[s->nports++]; |
| 383 | sp->server = s; |
| 384 | sp->fd = fd; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 385 | sp->emfd = grpc_fd_create(fd, name); |
Craig Tiller | 772c97b | 2015-02-17 08:07:34 -0800 | [diff] [blame] | 386 | memcpy(sp->addr.untyped, addr, addr_len); |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 387 | sp->addr_len = addr_len; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 388 | GPR_ASSERT(sp->emfd); |
| 389 | gpr_mu_unlock(&s->mu); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 390 | gpr_free(addr_str); |
| 391 | gpr_free(name); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 392 | } |
| 393 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 394 | return port; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Craig Tiller | a172ad7 | 2015-01-21 15:51:47 -0800 | [diff] [blame] | 397 | int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 398 | int addr_len) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 399 | int allocated_port1 = -1; |
| 400 | int allocated_port2 = -1; |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 401 | unsigned i; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 402 | int fd; |
| 403 | grpc_dualstack_mode dsmode; |
| 404 | struct sockaddr_in6 addr6_v4mapped; |
| 405 | struct sockaddr_in wild4; |
| 406 | struct sockaddr_in6 wild6; |
| 407 | struct sockaddr_in addr4_copy; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 408 | struct sockaddr *allocated_addr = NULL; |
| 409 | struct sockaddr_storage sockname_temp; |
| 410 | socklen_t sockname_len; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 411 | int port; |
| 412 | |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 413 | if (((struct sockaddr *)addr)->sa_family == AF_UNIX) { |
| 414 | unlink_if_unix_domain_socket(addr); |
| 415 | } |
| 416 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 417 | /* Check if this is a wildcard port, and if so, try to keep the port the same |
| 418 | as some previously created listener. */ |
| 419 | if (grpc_sockaddr_get_port(addr) == 0) { |
| 420 | for (i = 0; i < s->nports; i++) { |
| 421 | sockname_len = sizeof(sockname_temp); |
| 422 | if (0 == getsockname(s->ports[i].fd, (struct sockaddr *)&sockname_temp, |
| 423 | &sockname_len)) { |
| 424 | port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); |
| 425 | if (port > 0) { |
| 426 | allocated_addr = malloc(addr_len); |
| 427 | memcpy(allocated_addr, addr, addr_len); |
| 428 | grpc_sockaddr_set_port(allocated_addr, port); |
| 429 | addr = allocated_addr; |
| 430 | break; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 436 | if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) { |
| 437 | addr = (const struct sockaddr *)&addr6_v4mapped; |
| 438 | addr_len = sizeof(addr6_v4mapped); |
| 439 | } |
| 440 | |
| 441 | /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */ |
| 442 | if (grpc_sockaddr_is_wildcard(addr, &port)) { |
| 443 | grpc_sockaddr_make_wildcards(port, &wild4, &wild6); |
| 444 | |
| 445 | /* Try listening on IPv6 first. */ |
| 446 | addr = (struct sockaddr *)&wild6; |
| 447 | addr_len = sizeof(wild6); |
| 448 | fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 449 | allocated_port1 = add_socket_to_server(s, fd, addr, addr_len); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 450 | if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 451 | goto done; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* 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] | 455 | if (port == 0 && allocated_port1 > 0) { |
| 456 | grpc_sockaddr_set_port((struct sockaddr *)&wild4, allocated_port1); |
| 457 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 458 | addr = (struct sockaddr *)&wild4; |
| 459 | addr_len = sizeof(wild4); |
| 460 | } |
| 461 | |
| 462 | fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode); |
| 463 | if (fd < 0) { |
| 464 | gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno)); |
| 465 | } |
| 466 | if (dsmode == GRPC_DSMODE_IPV4 && |
| 467 | grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) { |
| 468 | addr = (struct sockaddr *)&addr4_copy; |
| 469 | addr_len = sizeof(addr4_copy); |
| 470 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 471 | allocated_port2 = add_socket_to_server(s, fd, addr, addr_len); |
| 472 | |
| 473 | done: |
| 474 | gpr_free(allocated_addr); |
| 475 | return allocated_port1 >= 0 ? allocated_port1 : allocated_port2; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 478 | int grpc_tcp_server_get_fd(grpc_tcp_server *s, unsigned index) { |
| 479 | return (index < s->nports) ? s->ports[index].fd : -1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 482 | void grpc_tcp_server_start(grpc_tcp_server *s, grpc_pollset **pollsets, |
| 483 | size_t pollset_count, grpc_tcp_server_cb cb, |
| 484 | void *cb_arg) { |
| 485 | size_t i, j; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 486 | GPR_ASSERT(cb); |
| 487 | gpr_mu_lock(&s->mu); |
| 488 | GPR_ASSERT(!s->cb); |
| 489 | GPR_ASSERT(s->active_ports == 0); |
| 490 | s->cb = cb; |
| 491 | s->cb_arg = cb_arg; |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 492 | s->pollsets = pollsets; |
| 493 | s->pollset_count = pollset_count; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 494 | for (i = 0; i < s->nports; i++) { |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 495 | for (j = 0; j < pollset_count; j++) { |
| 496 | grpc_pollset_add_fd(pollsets[j], s->ports[i].emfd); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 497 | } |
Craig Tiller | 0fcd53c | 2015-02-18 15:10:53 -0800 | [diff] [blame] | 498 | s->ports[i].read_closure.cb = on_read; |
| 499 | s->ports[i].read_closure.cb_arg = &s->ports[i]; |
| 500 | grpc_fd_notify_on_read(s->ports[i].emfd, &s->ports[i].read_closure); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 501 | s->active_ports++; |
| 502 | } |
| 503 | gpr_mu_unlock(&s->mu); |
| 504 | } |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 505 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 506 | #endif |