Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 43 | #include "src/core/lib/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> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | #include <unistd.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 57 | #include <grpc/support/alloc.h> |
| 58 | #include <grpc/support/log.h> |
| 59 | #include <grpc/support/string_util.h> |
| 60 | #include <grpc/support/sync.h> |
| 61 | #include <grpc/support/time.h> |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 62 | #include <grpc/support/useful.h> |
| 63 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 64 | #include "src/core/lib/iomgr/resolve_address.h" |
| 65 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
| 66 | #include "src/core/lib/iomgr/socket_utils_posix.h" |
| 67 | #include "src/core/lib/iomgr/tcp_posix.h" |
| 68 | #include "src/core/lib/iomgr/unix_sockets_posix.h" |
| 69 | #include "src/core/lib/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 */ |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 77 | typedef struct grpc_tcp_listener grpc_tcp_listener; |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 78 | struct grpc_tcp_listener { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 79 | int fd; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 80 | grpc_fd *emfd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | grpc_tcp_server *server; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | union { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 83 | uint8_t untyped[GRPC_MAX_SOCKADDR_SIZE]; |
Craig Tiller | 772c97b | 2015-02-17 08:07:34 -0800 | [diff] [blame] | 84 | struct sockaddr sockaddr; |
Craig Tiller | 772c97b | 2015-02-17 08:07:34 -0800 | [diff] [blame] | 85 | } addr; |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame] | 86 | size_t addr_len; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 87 | int port; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 88 | unsigned port_index; |
| 89 | unsigned fd_index; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 90 | grpc_closure read_closure; |
| 91 | grpc_closure destroyed_closure; |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 92 | struct grpc_tcp_listener *next; |
Dan Born | ad1f31f | 2016-07-08 13:24:49 -0700 | [diff] [blame] | 93 | /* sibling is a linked list of all listeners for a given port. add_port and |
| 94 | clone_port place all new listeners in the same sibling list. A member of |
| 95 | the 'sibling' list is also a member of the 'next' list. The head of each |
| 96 | sibling list has is_sibling==0, and subsequent members of sibling lists |
| 97 | have is_sibling==1. is_sibling allows separate sibling lists to be |
| 98 | identified while iterating through 'next'. */ |
Nicolas "Pixel" Noble | d86115e | 2015-11-20 05:56:25 +0100 | [diff] [blame] | 99 | struct grpc_tcp_listener *sibling; |
| 100 | int is_sibling; |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 101 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 102 | |
| 103 | /* the overall server */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 104 | struct grpc_tcp_server { |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 105 | gpr_refcount refs; |
Robbie Shade | 3cd2d18 | 2015-08-28 14:30:35 -0400 | [diff] [blame] | 106 | /* Called whenever accept() succeeds on a server port. */ |
| 107 | grpc_tcp_server_cb on_accept_cb; |
| 108 | void *on_accept_cb_arg; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 109 | |
| 110 | gpr_mu mu; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 111 | |
| 112 | /* active port count: how many ports are actually still listening */ |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 113 | size_t active_ports; |
| 114 | /* destroyed port count: how many ports are completely destroyed */ |
| 115 | size_t destroyed_ports; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 116 | |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 117 | /* is this server shutting down? */ |
| 118 | bool shutdown; |
| 119 | /* use SO_REUSEPORT */ |
| 120 | bool so_reuseport; |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 121 | |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 122 | /* linked list of server ports */ |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 123 | grpc_tcp_listener *head; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 124 | grpc_tcp_listener *tail; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 125 | unsigned nports; |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 126 | |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 127 | /* List of closures passed to shutdown_starting_add(). */ |
| 128 | grpc_closure_list shutdown_starting; |
| 129 | |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 130 | /* shutdown callback */ |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 131 | grpc_closure *shutdown_complete; |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 132 | |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 133 | /* all pollsets interested in new connections */ |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 134 | grpc_pollset **pollsets; |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 135 | /* number of pollsets in the pollsets array */ |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 136 | size_t pollset_count; |
Craig Tiller | 53dd6b9 | 2016-05-24 13:49:50 -0700 | [diff] [blame] | 137 | |
| 138 | /* next pollset to assign a channel to */ |
Craig Tiller | b76471d | 2016-07-01 10:24:56 -0700 | [diff] [blame] | 139 | gpr_atm next_pollset_to_assign; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 142 | static gpr_once check_init = GPR_ONCE_INIT; |
Nicolas "Pixel" Noble | baa35fc | 2016-08-19 02:20:11 +0200 | [diff] [blame] | 143 | static bool has_so_reuseport = false; |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 144 | |
| 145 | static void init(void) { |
Nicolas "Pixel" Noble | baa35fc | 2016-08-19 02:20:11 +0200 | [diff] [blame] | 146 | #ifndef GPR_MANYLINUX1 |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 147 | int s = socket(AF_INET, SOCK_STREAM, 0); |
| 148 | if (s >= 0) { |
| 149 | has_so_reuseport = GRPC_LOG_IF_ERROR("check for SO_REUSEPORT", |
| 150 | grpc_set_socket_reuse_port(s, 1)); |
| 151 | close(s); |
| 152 | } |
Nicolas "Pixel" Noble | baa35fc | 2016-08-19 02:20:11 +0200 | [diff] [blame] | 153 | #endif |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Craig Tiller | 0b5857f | 2016-05-04 10:58:06 -0700 | [diff] [blame] | 156 | grpc_error *grpc_tcp_server_create(grpc_closure *shutdown_complete, |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 157 | const grpc_channel_args *args, |
Craig Tiller | 0b5857f | 2016-05-04 10:58:06 -0700 | [diff] [blame] | 158 | grpc_tcp_server **server) { |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 159 | gpr_once_init(&check_init, init); |
| 160 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 161 | grpc_tcp_server *s = gpr_malloc(sizeof(grpc_tcp_server)); |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 162 | s->so_reuseport = has_so_reuseport; |
| 163 | for (size_t i = 0; i < (args == NULL ? 0 : args->num_args); i++) { |
| 164 | if (0 == strcmp(GRPC_ARG_ALLOW_REUSEPORT, args->args[i].key)) { |
| 165 | if (args->args[i].type == GRPC_ARG_INTEGER) { |
| 166 | s->so_reuseport = |
| 167 | has_so_reuseport && (args->args[i].value.integer != 0); |
| 168 | } else { |
| 169 | gpr_free(s); |
| 170 | return GRPC_ERROR_CREATE(GRPC_ARG_ALLOW_REUSEPORT |
| 171 | " must be an integer"); |
| 172 | } |
| 173 | } |
| 174 | } |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 175 | gpr_ref_init(&s->refs, 1); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 176 | gpr_mu_init(&s->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 177 | s->active_ports = 0; |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 178 | s->destroyed_ports = 0; |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 179 | s->shutdown = false; |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 180 | s->shutdown_starting.head = NULL; |
| 181 | s->shutdown_starting.tail = NULL; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 182 | s->shutdown_complete = shutdown_complete; |
Robbie Shade | 3cd2d18 | 2015-08-28 14:30:35 -0400 | [diff] [blame] | 183 | s->on_accept_cb = NULL; |
| 184 | s->on_accept_cb_arg = NULL; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 185 | s->head = NULL; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 186 | s->tail = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 187 | s->nports = 0; |
Craig Tiller | b76471d | 2016-07-01 10:24:56 -0700 | [diff] [blame] | 188 | gpr_atm_no_barrier_store(&s->next_pollset_to_assign, 0); |
Craig Tiller | 0b5857f | 2016-05-04 10:58:06 -0700 | [diff] [blame] | 189 | *server = s; |
| 190 | return GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) { |
Dan Born | 930c298 | 2016-09-08 19:12:18 -0700 | [diff] [blame] | 194 | gpr_mu_lock(&s->mu); |
| 195 | GPR_ASSERT(s->shutdown); |
| 196 | gpr_mu_unlock(&s->mu); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 197 | if (s->shutdown_complete != NULL) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 198 | grpc_exec_ctx_sched(exec_ctx, s->shutdown_complete, GRPC_ERROR_NONE, NULL); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 199 | } |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 200 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 201 | gpr_mu_destroy(&s->mu); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 202 | |
Nicolas "Pixel" Noble | c6a7c6e | 2015-11-19 21:55:44 +0100 | [diff] [blame] | 203 | while (s->head) { |
| 204 | grpc_tcp_listener *sp = s->head; |
| 205 | s->head = sp->next; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 206 | gpr_free(sp); |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 209 | gpr_free(s); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 212 | static void destroyed_port(grpc_exec_ctx *exec_ctx, void *server, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 213 | grpc_error *error) { |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 214 | grpc_tcp_server *s = server; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 215 | gpr_mu_lock(&s->mu); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 216 | s->destroyed_ports++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 217 | if (s->destroyed_ports == s->nports) { |
| 218 | gpr_mu_unlock(&s->mu); |
| 219 | finish_shutdown(exec_ctx, s); |
| 220 | } else { |
| 221 | GPR_ASSERT(s->destroyed_ports < s->nports); |
| 222 | gpr_mu_unlock(&s->mu); |
| 223 | } |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 226 | /* called when all listening endpoints have been shutdown, so no further |
| 227 | events will be received on them - at this point it's safe to destroy |
| 228 | things */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 229 | static void deactivated_all_ports(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 230 | /* delete ALL the things */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 231 | gpr_mu_lock(&s->mu); |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 232 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 233 | if (!s->shutdown) { |
| 234 | gpr_mu_unlock(&s->mu); |
| 235 | return; |
| 236 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 237 | |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 238 | if (s->head) { |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 239 | grpc_tcp_listener *sp; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 240 | for (sp = s->head; sp; sp = sp->next) { |
ahedberg | fcbcbff | 2016-03-22 13:23:47 -0400 | [diff] [blame] | 241 | grpc_unlink_if_unix_domain_socket(&sp->addr.sockaddr); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 242 | sp->destroyed_closure.cb = destroyed_port; |
| 243 | sp->destroyed_closure.cb_arg = s; |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 244 | grpc_fd_orphan(exec_ctx, sp->emfd, &sp->destroyed_closure, NULL, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 245 | "tcp_listener_shutdown"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 246 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 247 | gpr_mu_unlock(&s->mu); |
| 248 | } else { |
| 249 | gpr_mu_unlock(&s->mu); |
| 250 | finish_shutdown(exec_ctx, s); |
| 251 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 254 | static void tcp_server_destroy(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 255 | gpr_mu_lock(&s->mu); |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 256 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 257 | GPR_ASSERT(!s->shutdown); |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 258 | s->shutdown = true; |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 259 | |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 260 | /* shutdown all fd's */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 261 | if (s->active_ports) { |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 262 | grpc_tcp_listener *sp; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 263 | for (sp = s->head; sp; sp = sp->next) { |
| 264 | grpc_fd_shutdown(exec_ctx, sp->emfd); |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 265 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 266 | gpr_mu_unlock(&s->mu); |
| 267 | } else { |
| 268 | gpr_mu_unlock(&s->mu); |
| 269 | deactivated_all_ports(exec_ctx, s); |
| 270 | } |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 273 | /* get max listen queue size on linux */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 274 | static void init_max_accept_queue_size(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 275 | int n = SOMAXCONN; |
| 276 | char buf[64]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 277 | FILE *fp = fopen("/proc/sys/net/core/somaxconn", "r"); |
| 278 | if (fp == NULL) { |
| 279 | /* 2.4 kernel. */ |
| 280 | s_max_accept_queue_size = SOMAXCONN; |
| 281 | return; |
| 282 | } |
| 283 | if (fgets(buf, sizeof buf, fp)) { |
| 284 | char *end; |
| 285 | long i = strtol(buf, &end, 10); |
| 286 | if (i > 0 && i <= INT_MAX && end && *end == 0) { |
| 287 | n = (int)i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 288 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 289 | } |
| 290 | fclose(fp); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 291 | s_max_accept_queue_size = n; |
| 292 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 293 | if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE) { |
| 294 | gpr_log(GPR_INFO, |
| 295 | "Suspiciously small accept queue (%d) will probably lead to " |
| 296 | "connection drops", |
| 297 | s_max_accept_queue_size); |
| 298 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 301 | static int get_max_accept_queue_size(void) { |
| 302 | gpr_once_init(&s_init_max_accept_queue_size, init_max_accept_queue_size); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 303 | return s_max_accept_queue_size; |
| 304 | } |
| 305 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 306 | /* Prepare a recently-created socket for listening. */ |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 307 | static grpc_error *prepare_socket(int fd, const struct sockaddr *addr, |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 308 | size_t addr_len, bool so_reuseport, |
| 309 | int *port) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 310 | struct sockaddr_storage sockname_temp; |
| 311 | socklen_t sockname_len; |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 312 | grpc_error *err = GRPC_ERROR_NONE; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 313 | |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 314 | GPR_ASSERT(fd >= 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 315 | |
Yuchen Zeng | 2414bbb | 2016-08-08 21:22:54 -0700 | [diff] [blame] | 316 | if (so_reuseport && !grpc_is_unix_socket(addr)) { |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 317 | err = grpc_set_socket_reuse_port(fd, 1); |
| 318 | if (err != GRPC_ERROR_NONE) goto error; |
| 319 | } |
| 320 | |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 321 | err = grpc_set_socket_nonblocking(fd, 1); |
| 322 | if (err != GRPC_ERROR_NONE) goto error; |
| 323 | err = grpc_set_socket_cloexec(fd, 1); |
| 324 | if (err != GRPC_ERROR_NONE) goto error; |
| 325 | if (!grpc_is_unix_socket(addr)) { |
| 326 | err = grpc_set_socket_low_latency(fd, 1); |
| 327 | if (err != GRPC_ERROR_NONE) goto error; |
| 328 | err = grpc_set_socket_reuse_addr(fd, 1); |
| 329 | if (err != GRPC_ERROR_NONE) goto error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 330 | } |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 331 | err = grpc_set_socket_no_sigpipe_if_possible(fd); |
| 332 | if (err != GRPC_ERROR_NONE) goto error; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 333 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 334 | GPR_ASSERT(addr_len < ~(socklen_t)0); |
| 335 | if (bind(fd, addr, (socklen_t)addr_len) < 0) { |
David Garcia Quintas | 03291c4 | 2016-05-10 18:28:56 -0700 | [diff] [blame] | 336 | err = GRPC_OS_ERROR(errno, "bind"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 337 | goto error; |
| 338 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 339 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 340 | if (listen(fd, get_max_accept_queue_size()) < 0) { |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 341 | err = GRPC_OS_ERROR(errno, "listen"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 342 | goto error; |
| 343 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 344 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 345 | sockname_len = sizeof(sockname_temp); |
| 346 | if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) { |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 347 | err = GRPC_OS_ERROR(errno, "getsockname"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 348 | goto error; |
| 349 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 350 | |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 351 | *port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); |
| 352 | return GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 353 | |
| 354 | error: |
Craig Tiller | 94f8453 | 2016-05-06 21:03:29 -0700 | [diff] [blame] | 355 | GPR_ASSERT(err != GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 356 | if (fd >= 0) { |
| 357 | close(fd); |
| 358 | } |
David Garcia Quintas | 32ec133 | 2016-05-11 12:22:53 -0700 | [diff] [blame] | 359 | grpc_error *ret = grpc_error_set_int( |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 360 | GRPC_ERROR_CREATE_REFERENCING("Unable to configure socket", &err, 1), |
| 361 | GRPC_ERROR_INT_FD, fd); |
David Garcia Quintas | 32ec133 | 2016-05-11 12:22:53 -0700 | [diff] [blame] | 362 | GRPC_ERROR_UNREF(err); |
| 363 | return ret; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* event manager callback when reads are ready */ |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 367 | static void on_read(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *err) { |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 368 | grpc_tcp_listener *sp = arg; |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 369 | grpc_tcp_server_acceptor acceptor = {sp->server, sp->port_index, |
| 370 | sp->fd_index}; |
Sree Kuchibhotla | 42b004a | 2016-04-08 14:41:49 -0700 | [diff] [blame] | 371 | grpc_pollset *read_notifier_pollset = NULL; |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 372 | grpc_fd *fdobj; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 373 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 374 | if (err != GRPC_ERROR_NONE) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 375 | goto error; |
| 376 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 377 | |
Craig Tiller | 53dd6b9 | 2016-05-24 13:49:50 -0700 | [diff] [blame] | 378 | read_notifier_pollset = |
Craig Tiller | b76471d | 2016-07-01 10:24:56 -0700 | [diff] [blame] | 379 | sp->server->pollsets[(size_t)gpr_atm_no_barrier_fetch_add( |
| 380 | &sp->server->next_pollset_to_assign, 1) % |
Craig Tiller | 53dd6b9 | 2016-05-24 13:49:50 -0700 | [diff] [blame] | 381 | sp->server->pollset_count]; |
Sree Kuchibhotla | 42b004a | 2016-04-08 14:41:49 -0700 | [diff] [blame] | 382 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 383 | /* loop until accept4 returns EAGAIN, and then re-arm notification */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 384 | for (;;) { |
| 385 | struct sockaddr_storage addr; |
| 386 | socklen_t addrlen = sizeof(addr); |
| 387 | char *addr_str; |
| 388 | char *name; |
| 389 | /* Note: If we ever decide to return this address to the user, remember to |
| 390 | strip off the ::ffff:0.0.0.0/96 prefix first. */ |
| 391 | int fd = grpc_accept4(sp->fd, (struct sockaddr *)&addr, &addrlen, 1, 1); |
| 392 | if (fd < 0) { |
| 393 | switch (errno) { |
| 394 | case EINTR: |
| 395 | continue; |
| 396 | case EAGAIN: |
| 397 | grpc_fd_notify_on_read(exec_ctx, sp->emfd, &sp->read_closure); |
| 398 | return; |
| 399 | default: |
| 400 | gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno)); |
| 401 | goto error; |
| 402 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 405 | grpc_set_socket_no_sigpipe_if_possible(fd); |
| 406 | |
| 407 | addr_str = grpc_sockaddr_to_uri((struct sockaddr *)&addr); |
| 408 | gpr_asprintf(&name, "tcp-server-connection:%s", addr_str); |
| 409 | |
| 410 | if (grpc_tcp_trace) { |
| 411 | gpr_log(GPR_DEBUG, "SERVER_CONNECT: incoming connection: %s", addr_str); |
| 412 | } |
| 413 | |
| 414 | fdobj = grpc_fd_create(fd, name); |
Sree Kuchibhotla | 42b004a | 2016-04-08 14:41:49 -0700 | [diff] [blame] | 415 | |
| 416 | if (read_notifier_pollset == NULL) { |
Sree Kuchibhotla | 42b004a | 2016-04-08 14:41:49 -0700 | [diff] [blame] | 417 | gpr_log(GPR_ERROR, "Read notifier pollset is not set on the fd"); |
| 418 | goto error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 419 | } |
Sree Kuchibhotla | 42b004a | 2016-04-08 14:41:49 -0700 | [diff] [blame] | 420 | |
Sree Kuchibhotla | 42b004a | 2016-04-08 14:41:49 -0700 | [diff] [blame] | 421 | grpc_pollset_add_fd(exec_ctx, read_notifier_pollset, fdobj); |
| 422 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 423 | sp->server->on_accept_cb( |
| 424 | exec_ctx, sp->server->on_accept_cb_arg, |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 425 | grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str), |
Craig Tiller | 418a821 | 2016-05-16 16:27:51 -0700 | [diff] [blame] | 426 | read_notifier_pollset, &acceptor); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 427 | |
| 428 | gpr_free(name); |
| 429 | gpr_free(addr_str); |
| 430 | } |
| 431 | |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 432 | GPR_UNREACHABLE_CODE(return ); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 433 | |
| 434 | error: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 435 | gpr_mu_lock(&sp->server->mu); |
| 436 | if (0 == --sp->server->active_ports) { |
| 437 | gpr_mu_unlock(&sp->server->mu); |
| 438 | deactivated_all_ports(exec_ctx, sp->server); |
| 439 | } else { |
| 440 | gpr_mu_unlock(&sp->server->mu); |
| 441 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 444 | static grpc_error *add_socket_to_server(grpc_tcp_server *s, int fd, |
| 445 | const struct sockaddr *addr, |
| 446 | size_t addr_len, unsigned port_index, |
| 447 | unsigned fd_index, |
| 448 | grpc_tcp_listener **listener) { |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 449 | grpc_tcp_listener *sp = NULL; |
Craig Tiller | 9147296 | 2016-05-09 11:01:15 -0700 | [diff] [blame] | 450 | int port = -1; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 451 | char *addr_str; |
| 452 | char *name; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 453 | |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 454 | grpc_error *err = prepare_socket(fd, addr, addr_len, s->so_reuseport, &port); |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 455 | if (err == GRPC_ERROR_NONE) { |
Craig Tiller | 9147296 | 2016-05-09 11:01:15 -0700 | [diff] [blame] | 456 | GPR_ASSERT(port > 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 457 | grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1); |
| 458 | gpr_asprintf(&name, "tcp-server-listener:%s", addr_str); |
| 459 | gpr_mu_lock(&s->mu); |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 460 | s->nports++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 461 | GPR_ASSERT(!s->on_accept_cb && "must add ports before starting server"); |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 462 | sp = gpr_malloc(sizeof(grpc_tcp_listener)); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 463 | sp->next = NULL; |
| 464 | if (s->head == NULL) { |
| 465 | s->head = sp; |
| 466 | } else { |
| 467 | s->tail->next = sp; |
| 468 | } |
| 469 | s->tail = sp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 470 | sp->server = s; |
| 471 | sp->fd = fd; |
| 472 | sp->emfd = grpc_fd_create(fd, name); |
| 473 | memcpy(sp->addr.untyped, addr, addr_len); |
| 474 | sp->addr_len = addr_len; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 475 | sp->port = port; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 476 | sp->port_index = port_index; |
| 477 | sp->fd_index = fd_index; |
Nicolas "Pixel" Noble | d86115e | 2015-11-20 05:56:25 +0100 | [diff] [blame] | 478 | sp->is_sibling = 0; |
| 479 | sp->sibling = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 480 | GPR_ASSERT(sp->emfd); |
| 481 | gpr_mu_unlock(&s->mu); |
| 482 | gpr_free(addr_str); |
| 483 | gpr_free(name); |
| 484 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 485 | |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 486 | *listener = sp; |
| 487 | return err; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Dan Born | ad1f31f | 2016-07-08 13:24:49 -0700 | [diff] [blame] | 490 | /* Insert count new listeners after listener. Every new listener will have the |
| 491 | same listen address as listener (SO_REUSEPORT must be enabled). Every new |
| 492 | listener is a sibling of listener. */ |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 493 | static grpc_error *clone_port(grpc_tcp_listener *listener, unsigned count) { |
| 494 | grpc_tcp_listener *sp = NULL; |
| 495 | char *addr_str; |
| 496 | char *name; |
| 497 | grpc_error *err; |
| 498 | |
| 499 | for (grpc_tcp_listener *l = listener->next; l && l->is_sibling; l = l->next) { |
| 500 | l->fd_index += count; |
| 501 | } |
| 502 | |
| 503 | for (unsigned i = 0; i < count; i++) { |
Craig Tiller | 136698c | 2016-05-19 11:49:44 -0700 | [diff] [blame] | 504 | int fd = -1; |
| 505 | int port = -1; |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 506 | grpc_dualstack_mode dsmode; |
| 507 | err = grpc_create_dualstack_socket(&listener->addr.sockaddr, SOCK_STREAM, 0, |
| 508 | &dsmode, &fd); |
| 509 | if (err != GRPC_ERROR_NONE) return err; |
| 510 | err = prepare_socket(fd, &listener->addr.sockaddr, listener->addr_len, true, |
| 511 | &port); |
| 512 | if (err != GRPC_ERROR_NONE) return err; |
Craig Tiller | 644da98 | 2016-05-19 09:19:28 -0700 | [diff] [blame] | 513 | listener->server->nports++; |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 514 | grpc_sockaddr_to_string(&addr_str, &listener->addr.sockaddr, 1); |
| 515 | gpr_asprintf(&name, "tcp-server-listener:%s/clone-%d", addr_str, i); |
| 516 | sp = gpr_malloc(sizeof(grpc_tcp_listener)); |
| 517 | sp->next = listener->next; |
| 518 | listener->next = sp; |
Dan Born | ad1f31f | 2016-07-08 13:24:49 -0700 | [diff] [blame] | 519 | /* sp (the new listener) is a sibling of 'listener' (the original |
| 520 | listener). */ |
| 521 | sp->is_sibling = 1; |
| 522 | sp->sibling = listener->sibling; |
| 523 | listener->sibling = sp; |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 524 | sp->server = listener->server; |
| 525 | sp->fd = fd; |
| 526 | sp->emfd = grpc_fd_create(fd, name); |
| 527 | memcpy(sp->addr.untyped, listener->addr.untyped, listener->addr_len); |
| 528 | sp->addr_len = listener->addr_len; |
| 529 | sp->port = port; |
| 530 | sp->port_index = listener->port_index; |
| 531 | sp->fd_index = listener->fd_index + count - i; |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 532 | GPR_ASSERT(sp->emfd); |
| 533 | while (listener->server->tail->next != NULL) { |
| 534 | listener->server->tail = listener->server->tail->next; |
| 535 | } |
| 536 | gpr_free(addr_str); |
| 537 | gpr_free(name); |
| 538 | } |
| 539 | |
| 540 | return GRPC_ERROR_NONE; |
| 541 | } |
| 542 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 543 | grpc_error *grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, |
| 544 | size_t addr_len, int *out_port) { |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 545 | grpc_tcp_listener *sp; |
| 546 | grpc_tcp_listener *sp2 = NULL; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 547 | int fd; |
| 548 | grpc_dualstack_mode dsmode; |
| 549 | struct sockaddr_in6 addr6_v4mapped; |
| 550 | struct sockaddr_in wild4; |
| 551 | struct sockaddr_in6 wild6; |
| 552 | struct sockaddr_in addr4_copy; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 553 | struct sockaddr *allocated_addr = NULL; |
| 554 | struct sockaddr_storage sockname_temp; |
| 555 | socklen_t sockname_len; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 556 | int port; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 557 | unsigned port_index = 0; |
| 558 | unsigned fd_index = 0; |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 559 | grpc_error *errs[2] = {GRPC_ERROR_NONE, GRPC_ERROR_NONE}; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 560 | if (s->tail != NULL) { |
| 561 | port_index = s->tail->port_index + 1; |
| 562 | } |
ahedberg | fcbcbff | 2016-03-22 13:23:47 -0400 | [diff] [blame] | 563 | grpc_unlink_if_unix_domain_socket((struct sockaddr *)addr); |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 564 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 565 | /* Check if this is a wildcard port, and if so, try to keep the port the same |
| 566 | as some previously created listener. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 567 | if (grpc_sockaddr_get_port(addr) == 0) { |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 568 | for (sp = s->head; sp; sp = sp->next) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 569 | sockname_len = sizeof(sockname_temp); |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 570 | if (0 == getsockname(sp->fd, (struct sockaddr *)&sockname_temp, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 571 | &sockname_len)) { |
| 572 | port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); |
| 573 | if (port > 0) { |
Nicolas "Pixel" Noble | 7c9a154 | 2016-03-26 01:33:34 +0100 | [diff] [blame] | 574 | allocated_addr = gpr_malloc(addr_len); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 575 | memcpy(allocated_addr, addr, addr_len); |
| 576 | grpc_sockaddr_set_port(allocated_addr, port); |
| 577 | addr = allocated_addr; |
| 578 | break; |
| 579 | } |
| 580 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 581 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 582 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 583 | |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 584 | sp = NULL; |
| 585 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 586 | if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) { |
| 587 | addr = (const struct sockaddr *)&addr6_v4mapped; |
| 588 | addr_len = sizeof(addr6_v4mapped); |
| 589 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 590 | |
| 591 | /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 592 | if (grpc_sockaddr_is_wildcard(addr, &port)) { |
| 593 | grpc_sockaddr_make_wildcards(port, &wild4, &wild6); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 594 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 595 | /* Try listening on IPv6 first. */ |
| 596 | addr = (struct sockaddr *)&wild6; |
| 597 | addr_len = sizeof(wild6); |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 598 | errs[0] = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode, &fd); |
| 599 | if (errs[0] == GRPC_ERROR_NONE) { |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 600 | errs[0] = add_socket_to_server(s, fd, addr, addr_len, port_index, |
| 601 | fd_index, &sp); |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 602 | if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) { |
| 603 | goto done; |
| 604 | } |
| 605 | if (sp != NULL) { |
| 606 | ++fd_index; |
| 607 | } |
| 608 | /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */ |
| 609 | if (port == 0 && sp != NULL) { |
| 610 | grpc_sockaddr_set_port((struct sockaddr *)&wild4, sp->port); |
| 611 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 612 | } |
Craig Tiller | 161f763 | 2016-06-09 15:46:53 -0700 | [diff] [blame] | 613 | addr = (struct sockaddr *)&wild4; |
| 614 | addr_len = sizeof(wild4); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 617 | errs[1] = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode, &fd); |
| 618 | if (errs[1] == GRPC_ERROR_NONE) { |
yang-g | 6346041 | 2016-03-22 14:58:15 -0700 | [diff] [blame] | 619 | if (dsmode == GRPC_DSMODE_IPV4 && |
| 620 | grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) { |
| 621 | addr = (struct sockaddr *)&addr4_copy; |
| 622 | addr_len = sizeof(addr4_copy); |
| 623 | } |
yang-g | b7921c7 | 2016-03-23 10:16:45 -0700 | [diff] [blame] | 624 | sp2 = sp; |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 625 | errs[1] = |
| 626 | add_socket_to_server(s, fd, addr, addr_len, port_index, fd_index, &sp); |
yang-g | 6346041 | 2016-03-22 14:58:15 -0700 | [diff] [blame] | 627 | if (sp2 != NULL && sp != NULL) { |
| 628 | sp2->sibling = sp; |
| 629 | sp->is_sibling = 1; |
| 630 | } |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 631 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 632 | |
| 633 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 634 | gpr_free(allocated_addr); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 635 | if (sp != NULL) { |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 636 | *out_port = sp->port; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 637 | GRPC_ERROR_UNREF(errs[0]); |
| 638 | GRPC_ERROR_UNREF(errs[1]); |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 639 | return GRPC_ERROR_NONE; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 640 | } else { |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 641 | *out_port = -1; |
| 642 | char *addr_str = grpc_sockaddr_to_uri(addr); |
| 643 | grpc_error *err = grpc_error_set_str( |
| 644 | GRPC_ERROR_CREATE_REFERENCING("Failed to add port to server", errs, |
| 645 | GPR_ARRAY_SIZE(errs)), |
| 646 | GRPC_ERROR_STR_TARGET_ADDRESS, addr_str); |
David Garcia Quintas | 32ec133 | 2016-05-11 12:22:53 -0700 | [diff] [blame] | 647 | GRPC_ERROR_UNREF(errs[0]); |
| 648 | GRPC_ERROR_UNREF(errs[1]); |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 649 | gpr_free(addr_str); |
| 650 | return err; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 651 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 654 | unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server *s, |
| 655 | unsigned port_index) { |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 656 | unsigned num_fds = 0; |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 657 | grpc_tcp_listener *sp; |
Dan Born | 930c298 | 2016-09-08 19:12:18 -0700 | [diff] [blame] | 658 | gpr_mu_lock(&s->mu); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 659 | for (sp = s->head; sp && port_index != 0; sp = sp->next) { |
| 660 | if (!sp->is_sibling) { |
| 661 | --port_index; |
| 662 | } |
| 663 | } |
| 664 | for (; sp; sp = sp->sibling, ++num_fds) |
yang-g | 3ff9727 | 2015-11-20 17:01:57 -0800 | [diff] [blame] | 665 | ; |
Dan Born | 930c298 | 2016-09-08 19:12:18 -0700 | [diff] [blame] | 666 | gpr_mu_unlock(&s->mu); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 667 | return num_fds; |
| 668 | } |
| 669 | |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 670 | int grpc_tcp_server_port_fd(grpc_tcp_server *s, unsigned port_index, |
| 671 | unsigned fd_index) { |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 672 | grpc_tcp_listener *sp; |
Dan Born | 930c298 | 2016-09-08 19:12:18 -0700 | [diff] [blame] | 673 | int fd; |
| 674 | gpr_mu_lock(&s->mu); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 675 | for (sp = s->head; sp && port_index != 0; sp = sp->next) { |
| 676 | if (!sp->is_sibling) { |
| 677 | --port_index; |
| 678 | } |
| 679 | } |
| 680 | for (; sp && fd_index != 0; sp = sp->sibling, --fd_index) |
| 681 | ; |
| 682 | if (sp) { |
Dan Born | 930c298 | 2016-09-08 19:12:18 -0700 | [diff] [blame] | 683 | fd = sp->fd; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 684 | } else { |
Dan Born | 930c298 | 2016-09-08 19:12:18 -0700 | [diff] [blame] | 685 | fd = -1; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 686 | } |
Dan Born | 930c298 | 2016-09-08 19:12:18 -0700 | [diff] [blame] | 687 | gpr_mu_unlock(&s->mu); |
| 688 | return fd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 691 | void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s, |
| 692 | grpc_pollset **pollsets, size_t pollset_count, |
| 693 | grpc_tcp_server_cb on_accept_cb, |
| 694 | void *on_accept_cb_arg) { |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 695 | size_t i; |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 696 | grpc_tcp_listener *sp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 697 | GPR_ASSERT(on_accept_cb); |
| 698 | gpr_mu_lock(&s->mu); |
| 699 | GPR_ASSERT(!s->on_accept_cb); |
| 700 | GPR_ASSERT(s->active_ports == 0); |
Robbie Shade | 3cd2d18 | 2015-08-28 14:30:35 -0400 | [diff] [blame] | 701 | s->on_accept_cb = on_accept_cb; |
| 702 | s->on_accept_cb_arg = on_accept_cb_arg; |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 703 | s->pollsets = pollsets; |
| 704 | s->pollset_count = pollset_count; |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 705 | sp = s->head; |
| 706 | while (sp != NULL) { |
Yuchen Zeng | d745a6f | 2016-08-08 20:44:36 -0700 | [diff] [blame] | 707 | if (s->so_reuseport && !grpc_is_unix_socket(&sp->addr.sockaddr) && |
| 708 | pollset_count > 1) { |
Craig Tiller | ef96264 | 2016-05-18 22:57:17 -0700 | [diff] [blame] | 709 | GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 710 | "clone_port", clone_port(sp, (unsigned)(pollset_count - 1)))); |
| 711 | for (i = 0; i < pollset_count; i++) { |
| 712 | grpc_pollset_add_fd(exec_ctx, pollsets[i], sp->emfd); |
| 713 | sp->read_closure.cb = on_read; |
| 714 | sp->read_closure.cb_arg = sp; |
| 715 | grpc_fd_notify_on_read(exec_ctx, sp->emfd, &sp->read_closure); |
| 716 | s->active_ports++; |
| 717 | sp = sp->next; |
| 718 | } |
| 719 | } else { |
| 720 | for (i = 0; i < pollset_count; i++) { |
| 721 | grpc_pollset_add_fd(exec_ctx, pollsets[i], sp->emfd); |
| 722 | } |
| 723 | sp->read_closure.cb = on_read; |
| 724 | sp->read_closure.cb_arg = sp; |
| 725 | grpc_fd_notify_on_read(exec_ctx, sp->emfd, &sp->read_closure); |
| 726 | s->active_ports++; |
| 727 | sp = sp->next; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 728 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 729 | } |
| 730 | gpr_mu_unlock(&s->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 731 | } |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 732 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 733 | grpc_tcp_server *grpc_tcp_server_ref(grpc_tcp_server *s) { |
Dan Born | 930c298 | 2016-09-08 19:12:18 -0700 | [diff] [blame] | 734 | gpr_ref_non_zero(&s->refs); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 735 | return s; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 736 | } |
| 737 | |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 738 | void grpc_tcp_server_shutdown_starting_add(grpc_tcp_server *s, |
| 739 | grpc_closure *shutdown_starting) { |
| 740 | gpr_mu_lock(&s->mu); |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 741 | grpc_closure_list_append(&s->shutdown_starting, shutdown_starting, |
| 742 | GRPC_ERROR_NONE); |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 743 | gpr_mu_unlock(&s->mu); |
| 744 | } |
| 745 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 746 | void grpc_tcp_server_unref(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) { |
| 747 | if (gpr_unref(&s->refs)) { |
Dan Born | 930c298 | 2016-09-08 19:12:18 -0700 | [diff] [blame] | 748 | grpc_tcp_server_shutdown_listeners(exec_ctx, s); |
| 749 | gpr_mu_lock(&s->mu); |
| 750 | grpc_exec_ctx_enqueue_list(exec_ctx, &s->shutdown_starting, NULL); |
| 751 | gpr_mu_unlock(&s->mu); |
| 752 | tcp_server_destroy(exec_ctx, s); |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
yang-g | 9275d40 | 2016-07-11 16:51:39 -0700 | [diff] [blame] | 756 | void grpc_tcp_server_shutdown_listeners(grpc_exec_ctx *exec_ctx, |
| 757 | grpc_tcp_server *s) { |
| 758 | gpr_mu_lock(&s->mu); |
| 759 | /* shutdown all fd's */ |
| 760 | if (s->active_ports) { |
| 761 | grpc_tcp_listener *sp; |
| 762 | for (sp = s->head; sp; sp = sp->next) { |
| 763 | grpc_fd_shutdown(exec_ctx, sp->emfd); |
| 764 | } |
| 765 | } |
| 766 | gpr_mu_unlock(&s->mu); |
| 767 | } |
| 768 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 769 | #endif |