Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Dan Born | 725ee28 | 2016-01-13 13:14:56 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, 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> |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 55 | #ifdef GPR_HAVE_UNIX_SOCKET |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 56 | #include <sys/un.h> |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 57 | #endif |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | #include <unistd.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 60 | #include "src/core/iomgr/pollset_posix.h" |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 61 | #include "src/core/iomgr/resolve_address.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 62 | #include "src/core/iomgr/sockaddr_utils.h" |
| 63 | #include "src/core/iomgr/socket_utils_posix.h" |
| 64 | #include "src/core/iomgr/tcp_posix.h" |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 65 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | #include <grpc/support/alloc.h> |
| 67 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 68 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | #include <grpc/support/sync.h> |
| 70 | #include <grpc/support/time.h> |
| 71 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | #define MIN_SAFE_ACCEPT_QUEUE_SIZE 100 |
| 73 | |
| 74 | static gpr_once s_init_max_accept_queue_size; |
| 75 | static int s_max_accept_queue_size; |
| 76 | |
| 77 | /* one listening port */ |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 78 | typedef struct grpc_tcp_listener grpc_tcp_listener; |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 79 | struct grpc_tcp_listener { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | int fd; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 81 | grpc_fd *emfd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | grpc_tcp_server *server; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 83 | union { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 84 | uint8_t untyped[GRPC_MAX_SOCKADDR_SIZE]; |
Craig Tiller | 772c97b | 2015-02-17 08:07:34 -0800 | [diff] [blame] | 85 | struct sockaddr sockaddr; |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 86 | #ifdef GPR_HAVE_UNIX_SOCKET |
Craig Tiller | 772c97b | 2015-02-17 08:07:34 -0800 | [diff] [blame] | 87 | struct sockaddr_un un; |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 88 | #endif |
Craig Tiller | 772c97b | 2015-02-17 08:07:34 -0800 | [diff] [blame] | 89 | } addr; |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame] | 90 | size_t addr_len; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 91 | int port; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 92 | unsigned port_index; |
| 93 | unsigned fd_index; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 94 | grpc_closure read_closure; |
| 95 | grpc_closure destroyed_closure; |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 96 | struct grpc_tcp_listener *next; |
Nicolas "Pixel" Noble | d86115e | 2015-11-20 05:56:25 +0100 | [diff] [blame] | 97 | /* When we add a listener, more than one can be created, mainly because of |
| 98 | IPv6. A sibling will still be in the normal list, but will be flagged |
| 99 | as such. Any action, such as ref or unref, will affect all of the |
| 100 | siblings in the list. */ |
| 101 | struct grpc_tcp_listener *sibling; |
| 102 | int is_sibling; |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 103 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 104 | |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 105 | #ifdef GPR_HAVE_UNIX_SOCKET |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 106 | static void unlink_if_unix_domain_socket(const struct sockaddr_un *un) { |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 107 | struct stat st; |
| 108 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 109 | if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) { |
| 110 | unlink(un->sun_path); |
| 111 | } |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 112 | } |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 113 | #endif |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 114 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 115 | /* the overall server */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 116 | struct grpc_tcp_server { |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 117 | gpr_refcount refs; |
Robbie Shade | 3cd2d18 | 2015-08-28 14:30:35 -0400 | [diff] [blame] | 118 | /* Called whenever accept() succeeds on a server port. */ |
| 119 | grpc_tcp_server_cb on_accept_cb; |
| 120 | void *on_accept_cb_arg; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 121 | |
| 122 | gpr_mu mu; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 123 | |
| 124 | /* active port count: how many ports are actually still listening */ |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 125 | size_t active_ports; |
| 126 | /* destroyed port count: how many ports are completely destroyed */ |
| 127 | size_t destroyed_ports; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 128 | |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 129 | /* is this server shutting down? (boolean) */ |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 130 | int shutdown; |
| 131 | |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 132 | /* linked list of server ports */ |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 133 | grpc_tcp_listener *head; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 134 | grpc_tcp_listener *tail; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 135 | unsigned nports; |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 136 | |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 137 | /* List of closures passed to shutdown_starting_add(). */ |
| 138 | grpc_closure_list shutdown_starting; |
| 139 | |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 140 | /* shutdown callback */ |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 141 | grpc_closure *shutdown_complete; |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 142 | |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 143 | /* all pollsets interested in new connections */ |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 144 | grpc_pollset **pollsets; |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 145 | /* number of pollsets in the pollsets array */ |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 146 | size_t pollset_count; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 147 | }; |
| 148 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 149 | grpc_tcp_server *grpc_tcp_server_create(grpc_closure *shutdown_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 150 | grpc_tcp_server *s = gpr_malloc(sizeof(grpc_tcp_server)); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 151 | gpr_ref_init(&s->refs, 1); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 152 | gpr_mu_init(&s->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 153 | s->active_ports = 0; |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 154 | s->destroyed_ports = 0; |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 155 | s->shutdown = 0; |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 156 | s->shutdown_starting.head = NULL; |
| 157 | s->shutdown_starting.tail = NULL; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 158 | s->shutdown_complete = shutdown_complete; |
Robbie Shade | 3cd2d18 | 2015-08-28 14:30:35 -0400 | [diff] [blame] | 159 | s->on_accept_cb = NULL; |
| 160 | s->on_accept_cb_arg = NULL; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 161 | s->head = NULL; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 162 | s->tail = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 163 | s->nports = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 164 | return s; |
| 165 | } |
| 166 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 167 | static void finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) { |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 168 | if (s->shutdown_complete != NULL) { |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 169 | grpc_exec_ctx_enqueue(exec_ctx, s->shutdown_complete, true, NULL); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 170 | } |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 171 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 172 | gpr_mu_destroy(&s->mu); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 173 | |
Nicolas "Pixel" Noble | c6a7c6e | 2015-11-19 21:55:44 +0100 | [diff] [blame] | 174 | while (s->head) { |
| 175 | grpc_tcp_listener *sp = s->head; |
| 176 | s->head = sp->next; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 177 | gpr_free(sp); |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 180 | gpr_free(s); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 183 | static void destroyed_port(grpc_exec_ctx *exec_ctx, void *server, |
| 184 | bool success) { |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 185 | grpc_tcp_server *s = server; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | gpr_mu_lock(&s->mu); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 187 | s->destroyed_ports++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 188 | if (s->destroyed_ports == s->nports) { |
| 189 | gpr_mu_unlock(&s->mu); |
| 190 | finish_shutdown(exec_ctx, s); |
| 191 | } else { |
| 192 | GPR_ASSERT(s->destroyed_ports < s->nports); |
| 193 | gpr_mu_unlock(&s->mu); |
| 194 | } |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Craig Tiller | 6174b9a | 2015-06-18 08:13:05 -0700 | [diff] [blame] | 197 | /* called when all listening endpoints have been shutdown, so no further |
| 198 | events will be received on them - at this point it's safe to destroy |
| 199 | things */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 200 | 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] | 201 | /* delete ALL the things */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 202 | gpr_mu_lock(&s->mu); |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 203 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 204 | if (!s->shutdown) { |
| 205 | gpr_mu_unlock(&s->mu); |
| 206 | return; |
| 207 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 208 | |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 209 | if (s->head) { |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 210 | grpc_tcp_listener *sp; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 211 | for (sp = s->head; sp; sp = sp->next) { |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 212 | #ifdef GPR_HAVE_UNIX_SOCKET |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 213 | if (sp->addr.sockaddr.sa_family == AF_UNIX) { |
| 214 | unlink_if_unix_domain_socket(&sp->addr.un); |
| 215 | } |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 216 | #endif |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 217 | sp->destroyed_closure.cb = destroyed_port; |
| 218 | sp->destroyed_closure.cb_arg = s; |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 219 | grpc_fd_orphan(exec_ctx, sp->emfd, &sp->destroyed_closure, NULL, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 220 | "tcp_listener_shutdown"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 221 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 222 | gpr_mu_unlock(&s->mu); |
| 223 | } else { |
| 224 | gpr_mu_unlock(&s->mu); |
| 225 | finish_shutdown(exec_ctx, s); |
| 226 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 229 | 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] | 230 | gpr_mu_lock(&s->mu); |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 231 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 232 | GPR_ASSERT(!s->shutdown); |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 233 | s->shutdown = 1; |
| 234 | |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 235 | /* shutdown all fd's */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | if (s->active_ports) { |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 237 | grpc_tcp_listener *sp; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 238 | for (sp = s->head; sp; sp = sp->next) { |
| 239 | grpc_fd_shutdown(exec_ctx, sp->emfd); |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 240 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 241 | gpr_mu_unlock(&s->mu); |
| 242 | } else { |
| 243 | gpr_mu_unlock(&s->mu); |
| 244 | deactivated_all_ports(exec_ctx, s); |
| 245 | } |
Craig Tiller | 6f05140 | 2015-05-13 11:42:42 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 248 | /* get max listen queue size on linux */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 249 | static void init_max_accept_queue_size(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 250 | int n = SOMAXCONN; |
| 251 | char buf[64]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 252 | FILE *fp = fopen("/proc/sys/net/core/somaxconn", "r"); |
| 253 | if (fp == NULL) { |
| 254 | /* 2.4 kernel. */ |
| 255 | s_max_accept_queue_size = SOMAXCONN; |
| 256 | return; |
| 257 | } |
| 258 | if (fgets(buf, sizeof buf, fp)) { |
| 259 | char *end; |
| 260 | long i = strtol(buf, &end, 10); |
| 261 | if (i > 0 && i <= INT_MAX && end && *end == 0) { |
| 262 | n = (int)i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 263 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 264 | } |
| 265 | fclose(fp); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 266 | s_max_accept_queue_size = n; |
| 267 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 268 | if (s_max_accept_queue_size < MIN_SAFE_ACCEPT_QUEUE_SIZE) { |
| 269 | gpr_log(GPR_INFO, |
| 270 | "Suspiciously small accept queue (%d) will probably lead to " |
| 271 | "connection drops", |
| 272 | s_max_accept_queue_size); |
| 273 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 276 | static int get_max_accept_queue_size(void) { |
| 277 | 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] | 278 | return s_max_accept_queue_size; |
| 279 | } |
| 280 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 281 | /* Prepare a recently-created socket for listening. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 282 | static int prepare_socket(int fd, const struct sockaddr *addr, |
| 283 | size_t addr_len) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 284 | struct sockaddr_storage sockname_temp; |
| 285 | socklen_t sockname_len; |
| 286 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 287 | if (fd < 0) { |
| 288 | goto error; |
| 289 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 290 | |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 291 | #ifdef GPR_HAVE_UNIX_SOCKET |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 292 | if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) || |
| 293 | (addr->sa_family != AF_UNIX && (!grpc_set_socket_low_latency(fd, 1) || |
| 294 | !grpc_set_socket_reuse_addr(fd, 1))) || |
| 295 | !grpc_set_socket_no_sigpipe_if_possible(fd)) { |
| 296 | gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd, |
| 297 | strerror(errno)); |
| 298 | goto error; |
| 299 | } |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 300 | #else |
| 301 | if (!grpc_set_socket_nonblocking(fd, 1) || !grpc_set_socket_cloexec(fd, 1) || |
| 302 | !grpc_set_socket_low_latency(fd, 1) || |
| 303 | !grpc_set_socket_reuse_addr(fd, 1) || |
| 304 | !grpc_set_socket_no_sigpipe_if_possible(fd)) { |
| 305 | gpr_log(GPR_ERROR, "Unable to configure socket %d: %s", fd, |
| 306 | strerror(errno)); |
| 307 | goto error; |
| 308 | } |
| 309 | #endif |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 310 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 311 | GPR_ASSERT(addr_len < ~(socklen_t)0); |
| 312 | if (bind(fd, addr, (socklen_t)addr_len) < 0) { |
| 313 | char *addr_str; |
| 314 | grpc_sockaddr_to_string(&addr_str, addr, 0); |
| 315 | gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno)); |
| 316 | gpr_free(addr_str); |
| 317 | goto error; |
| 318 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 319 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 320 | if (listen(fd, get_max_accept_queue_size()) < 0) { |
| 321 | gpr_log(GPR_ERROR, "listen: %s", strerror(errno)); |
| 322 | goto error; |
| 323 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 324 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 325 | sockname_len = sizeof(sockname_temp); |
| 326 | if (getsockname(fd, (struct sockaddr *)&sockname_temp, &sockname_len) < 0) { |
| 327 | goto error; |
| 328 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 329 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 330 | return grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 331 | |
| 332 | error: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 333 | if (fd >= 0) { |
| 334 | close(fd); |
| 335 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 336 | return -1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* event manager callback when reads are ready */ |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 340 | static void on_read(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 341 | grpc_tcp_listener *sp = arg; |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 342 | grpc_tcp_server_acceptor acceptor = {sp->server, sp->port_index, |
| 343 | sp->fd_index}; |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 344 | grpc_fd *fdobj; |
| 345 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 346 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 347 | if (!success) { |
| 348 | goto error; |
| 349 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 350 | |
| 351 | /* loop until accept4 returns EAGAIN, and then re-arm notification */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 352 | for (;;) { |
| 353 | struct sockaddr_storage addr; |
| 354 | socklen_t addrlen = sizeof(addr); |
| 355 | char *addr_str; |
| 356 | char *name; |
| 357 | /* Note: If we ever decide to return this address to the user, remember to |
| 358 | strip off the ::ffff:0.0.0.0/96 prefix first. */ |
| 359 | int fd = grpc_accept4(sp->fd, (struct sockaddr *)&addr, &addrlen, 1, 1); |
| 360 | if (fd < 0) { |
| 361 | switch (errno) { |
| 362 | case EINTR: |
| 363 | continue; |
| 364 | case EAGAIN: |
| 365 | grpc_fd_notify_on_read(exec_ctx, sp->emfd, &sp->read_closure); |
| 366 | return; |
| 367 | default: |
| 368 | gpr_log(GPR_ERROR, "Failed accept4: %s", strerror(errno)); |
| 369 | goto error; |
| 370 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 373 | grpc_set_socket_no_sigpipe_if_possible(fd); |
| 374 | |
| 375 | addr_str = grpc_sockaddr_to_uri((struct sockaddr *)&addr); |
| 376 | gpr_asprintf(&name, "tcp-server-connection:%s", addr_str); |
| 377 | |
| 378 | if (grpc_tcp_trace) { |
| 379 | gpr_log(GPR_DEBUG, "SERVER_CONNECT: incoming connection: %s", addr_str); |
| 380 | } |
| 381 | |
| 382 | fdobj = grpc_fd_create(fd, name); |
| 383 | /* TODO(ctiller): revise this when we have server-side sharding |
| 384 | of channels -- we certainly should not be automatically adding every |
| 385 | incoming channel to every pollset owned by the server */ |
| 386 | for (i = 0; i < sp->server->pollset_count; i++) { |
| 387 | grpc_pollset_add_fd(exec_ctx, sp->server->pollsets[i], fdobj); |
| 388 | } |
| 389 | sp->server->on_accept_cb( |
| 390 | exec_ctx, sp->server->on_accept_cb_arg, |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 391 | grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str), |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 392 | &acceptor); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 393 | |
| 394 | gpr_free(name); |
| 395 | gpr_free(addr_str); |
| 396 | } |
| 397 | |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 398 | GPR_UNREACHABLE_CODE(return ); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 399 | |
| 400 | error: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 401 | gpr_mu_lock(&sp->server->mu); |
| 402 | if (0 == --sp->server->active_ports) { |
| 403 | gpr_mu_unlock(&sp->server->mu); |
| 404 | deactivated_all_ports(exec_ctx, sp->server); |
| 405 | } else { |
| 406 | gpr_mu_unlock(&sp->server->mu); |
| 407 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 410 | static grpc_tcp_listener *add_socket_to_server(grpc_tcp_server *s, int fd, |
yang-g | 3ff9727 | 2015-11-20 17:01:57 -0800 | [diff] [blame] | 411 | const struct sockaddr *addr, |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 412 | size_t addr_len, |
| 413 | unsigned port_index, |
| 414 | unsigned fd_index) { |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 415 | grpc_tcp_listener *sp = NULL; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 416 | int port; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 417 | char *addr_str; |
| 418 | char *name; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 419 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 420 | port = prepare_socket(fd, addr, addr_len); |
| 421 | if (port >= 0) { |
| 422 | grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1); |
| 423 | gpr_asprintf(&name, "tcp-server-listener:%s", addr_str); |
| 424 | gpr_mu_lock(&s->mu); |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 425 | s->nports++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 426 | GPR_ASSERT(!s->on_accept_cb && "must add ports before starting server"); |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 427 | sp = gpr_malloc(sizeof(grpc_tcp_listener)); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 428 | sp->next = NULL; |
| 429 | if (s->head == NULL) { |
| 430 | s->head = sp; |
| 431 | } else { |
| 432 | s->tail->next = sp; |
| 433 | } |
| 434 | s->tail = sp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 435 | sp->server = s; |
| 436 | sp->fd = fd; |
| 437 | sp->emfd = grpc_fd_create(fd, name); |
| 438 | memcpy(sp->addr.untyped, addr, addr_len); |
| 439 | sp->addr_len = addr_len; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 440 | sp->port = port; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 441 | sp->port_index = port_index; |
| 442 | sp->fd_index = fd_index; |
Nicolas "Pixel" Noble | d86115e | 2015-11-20 05:56:25 +0100 | [diff] [blame] | 443 | sp->is_sibling = 0; |
| 444 | sp->sibling = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 445 | GPR_ASSERT(sp->emfd); |
| 446 | gpr_mu_unlock(&s->mu); |
| 447 | gpr_free(addr_str); |
| 448 | gpr_free(name); |
| 449 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 450 | |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 451 | return sp; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 454 | int grpc_tcp_server_add_port(grpc_tcp_server *s, const void *addr, |
| 455 | size_t addr_len) { |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 456 | grpc_tcp_listener *sp; |
| 457 | grpc_tcp_listener *sp2 = NULL; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 458 | int fd; |
| 459 | grpc_dualstack_mode dsmode; |
| 460 | struct sockaddr_in6 addr6_v4mapped; |
| 461 | struct sockaddr_in wild4; |
| 462 | struct sockaddr_in6 wild6; |
| 463 | struct sockaddr_in addr4_copy; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 464 | struct sockaddr *allocated_addr = NULL; |
| 465 | struct sockaddr_storage sockname_temp; |
| 466 | socklen_t sockname_len; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 467 | int port; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 468 | unsigned port_index = 0; |
| 469 | unsigned fd_index = 0; |
| 470 | if (s->tail != NULL) { |
| 471 | port_index = s->tail->port_index + 1; |
| 472 | } |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 473 | #ifdef GPR_HAVE_UNIX_SOCKET |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 474 | if (((struct sockaddr *)addr)->sa_family == AF_UNIX) { |
| 475 | unlink_if_unix_domain_socket(addr); |
| 476 | } |
ahedberg | 803931d | 2016-03-11 17:24:12 -0500 | [diff] [blame^] | 477 | #endif |
Craig Tiller | aa31da4 | 2015-02-17 16:33:35 -0800 | [diff] [blame] | 478 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 479 | /* Check if this is a wildcard port, and if so, try to keep the port the same |
| 480 | as some previously created listener. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 481 | if (grpc_sockaddr_get_port(addr) == 0) { |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 482 | for (sp = s->head; sp; sp = sp->next) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 483 | sockname_len = sizeof(sockname_temp); |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 484 | if (0 == getsockname(sp->fd, (struct sockaddr *)&sockname_temp, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 485 | &sockname_len)) { |
| 486 | port = grpc_sockaddr_get_port((struct sockaddr *)&sockname_temp); |
| 487 | if (port > 0) { |
| 488 | allocated_addr = malloc(addr_len); |
| 489 | memcpy(allocated_addr, addr, addr_len); |
| 490 | grpc_sockaddr_set_port(allocated_addr, port); |
| 491 | addr = allocated_addr; |
| 492 | break; |
| 493 | } |
| 494 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 495 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 496 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 497 | |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 498 | sp = NULL; |
| 499 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 500 | if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) { |
| 501 | addr = (const struct sockaddr *)&addr6_v4mapped; |
| 502 | addr_len = sizeof(addr6_v4mapped); |
| 503 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 504 | |
| 505 | /* Treat :: or 0.0.0.0 as a family-agnostic wildcard. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 506 | if (grpc_sockaddr_is_wildcard(addr, &port)) { |
| 507 | grpc_sockaddr_make_wildcards(port, &wild4, &wild6); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 508 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 509 | /* Try listening on IPv6 first. */ |
| 510 | addr = (struct sockaddr *)&wild6; |
| 511 | addr_len = sizeof(wild6); |
| 512 | fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 513 | sp = add_socket_to_server(s, fd, addr, addr_len, port_index, fd_index); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 514 | if (fd >= 0 && dsmode == GRPC_DSMODE_DUALSTACK) { |
| 515 | goto done; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 516 | } |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 517 | if (sp != NULL) { |
| 518 | ++fd_index; |
| 519 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 520 | /* If we didn't get a dualstack socket, also listen on 0.0.0.0. */ |
Craig Tiller | e57ba13 | 2015-12-09 15:00:28 -0800 | [diff] [blame] | 521 | if (port == 0 && sp != NULL) { |
| 522 | grpc_sockaddr_set_port((struct sockaddr *)&wild4, sp->port); |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 523 | sp2 = sp; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 524 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 525 | addr = (struct sockaddr *)&wild4; |
| 526 | addr_len = sizeof(wild4); |
| 527 | } |
| 528 | |
| 529 | fd = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode); |
| 530 | if (fd < 0) { |
| 531 | gpr_log(GPR_ERROR, "Unable to create socket: %s", strerror(errno)); |
| 532 | } |
| 533 | if (dsmode == GRPC_DSMODE_IPV4 && |
| 534 | grpc_sockaddr_is_v4mapped(addr, &addr4_copy)) { |
| 535 | addr = (struct sockaddr *)&addr4_copy; |
| 536 | addr_len = sizeof(addr4_copy); |
| 537 | } |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 538 | sp = add_socket_to_server(s, fd, addr, addr_len, port_index, fd_index); |
Dan Born | 03c8a9b | 2016-01-12 21:14:29 -0800 | [diff] [blame] | 539 | if (sp2 != NULL && sp != NULL) { |
| 540 | sp2->sibling = sp; |
| 541 | sp->is_sibling = 1; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 542 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 543 | |
| 544 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 545 | gpr_free(allocated_addr); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 546 | if (sp != NULL) { |
| 547 | return sp->port; |
| 548 | } else { |
| 549 | return -1; |
| 550 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 553 | unsigned grpc_tcp_server_port_fd_count(grpc_tcp_server *s, |
| 554 | unsigned port_index) { |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 555 | unsigned num_fds = 0; |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 556 | grpc_tcp_listener *sp; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 557 | for (sp = s->head; sp && port_index != 0; sp = sp->next) { |
| 558 | if (!sp->is_sibling) { |
| 559 | --port_index; |
| 560 | } |
| 561 | } |
| 562 | for (; sp; sp = sp->sibling, ++num_fds) |
yang-g | 3ff9727 | 2015-11-20 17:01:57 -0800 | [diff] [blame] | 563 | ; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 564 | return num_fds; |
| 565 | } |
| 566 | |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 567 | int grpc_tcp_server_port_fd(grpc_tcp_server *s, unsigned port_index, |
| 568 | unsigned fd_index) { |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 569 | grpc_tcp_listener *sp; |
| 570 | for (sp = s->head; sp && port_index != 0; sp = sp->next) { |
| 571 | if (!sp->is_sibling) { |
| 572 | --port_index; |
| 573 | } |
| 574 | } |
| 575 | for (; sp && fd_index != 0; sp = sp->sibling, --fd_index) |
| 576 | ; |
| 577 | if (sp) { |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 578 | return sp->fd; |
| 579 | } else { |
| 580 | return -1; |
| 581 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 584 | void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s, |
| 585 | grpc_pollset **pollsets, size_t pollset_count, |
| 586 | grpc_tcp_server_cb on_accept_cb, |
| 587 | void *on_accept_cb_arg) { |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 588 | size_t i; |
Nicolas Noble | 8f71462 | 2015-11-19 11:16:54 -0800 | [diff] [blame] | 589 | grpc_tcp_listener *sp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 590 | GPR_ASSERT(on_accept_cb); |
| 591 | gpr_mu_lock(&s->mu); |
| 592 | GPR_ASSERT(!s->on_accept_cb); |
| 593 | GPR_ASSERT(s->active_ports == 0); |
Robbie Shade | 3cd2d18 | 2015-08-28 14:30:35 -0400 | [diff] [blame] | 594 | s->on_accept_cb = on_accept_cb; |
| 595 | s->on_accept_cb_arg = on_accept_cb_arg; |
Craig Tiller | 0189ac0 | 2015-05-11 14:59:19 -0700 | [diff] [blame] | 596 | s->pollsets = pollsets; |
| 597 | s->pollset_count = pollset_count; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 598 | for (sp = s->head; sp; sp = sp->next) { |
| 599 | for (i = 0; i < pollset_count; i++) { |
| 600 | grpc_pollset_add_fd(exec_ctx, pollsets[i], sp->emfd); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 601 | } |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 602 | sp->read_closure.cb = on_read; |
| 603 | sp->read_closure.cb_arg = sp; |
yang-g | 3ff9727 | 2015-11-20 17:01:57 -0800 | [diff] [blame] | 604 | grpc_fd_notify_on_read(exec_ctx, sp->emfd, &sp->read_closure); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 605 | s->active_ports++; |
| 606 | } |
| 607 | gpr_mu_unlock(&s->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 608 | } |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 609 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 610 | grpc_tcp_server *grpc_tcp_server_ref(grpc_tcp_server *s) { |
| 611 | gpr_ref(&s->refs); |
| 612 | return s; |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 615 | void grpc_tcp_server_shutdown_starting_add(grpc_tcp_server *s, |
| 616 | grpc_closure *shutdown_starting) { |
| 617 | gpr_mu_lock(&s->mu); |
| 618 | grpc_closure_list_add(&s->shutdown_starting, shutdown_starting, 1); |
| 619 | gpr_mu_unlock(&s->mu); |
| 620 | } |
| 621 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 622 | void grpc_tcp_server_unref(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s) { |
| 623 | if (gpr_unref(&s->refs)) { |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 624 | /* Complete shutdown_starting work before destroying. */ |
| 625 | grpc_exec_ctx local_exec_ctx = GRPC_EXEC_CTX_INIT; |
| 626 | gpr_mu_lock(&s->mu); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 627 | grpc_exec_ctx_enqueue_list(&local_exec_ctx, &s->shutdown_starting, NULL); |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 628 | gpr_mu_unlock(&s->mu); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 629 | if (exec_ctx == NULL) { |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 630 | grpc_exec_ctx_flush(&local_exec_ctx); |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 631 | tcp_server_destroy(&local_exec_ctx, s); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 632 | grpc_exec_ctx_finish(&local_exec_ctx); |
| 633 | } else { |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 634 | grpc_exec_ctx_finish(&local_exec_ctx); |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 635 | tcp_server_destroy(exec_ctx, s); |
Nicolas "Pixel" Noble | d86115e | 2015-11-20 05:56:25 +0100 | [diff] [blame] | 636 | } |
Nicolas Noble | 5eb4e1c | 2015-11-18 17:19:45 -0800 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 640 | #endif |