Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include "src/core/surface/server.h" |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include "src/core/channel/census_filter.h" |
| 40 | #include "src/core/channel/channel_args.h" |
| 41 | #include "src/core/channel/connected_channel.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 42 | #include "src/core/iomgr/iomgr.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 43 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | #include "src/core/surface/call.h" |
| 45 | #include "src/core/surface/channel.h" |
| 46 | #include "src/core/surface/completion_queue.h" |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 47 | #include "src/core/transport/metadata.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | #include <grpc/support/alloc.h> |
| 49 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | #include <grpc/support/useful.h> |
| 51 | |
| 52 | typedef enum { PENDING_START, ALL_CALLS, CALL_LIST_COUNT } call_list; |
| 53 | |
| 54 | typedef struct listener { |
| 55 | void *arg; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 56 | void (*start)(grpc_server *server, void *arg, grpc_pollset *pollset); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | void (*destroy)(grpc_server *server, void *arg); |
| 58 | struct listener *next; |
| 59 | } listener; |
| 60 | |
| 61 | typedef struct call_data call_data; |
| 62 | typedef struct channel_data channel_data; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 63 | typedef struct registered_method registered_method; |
| 64 | |
| 65 | typedef struct { |
| 66 | call_data *next; |
| 67 | call_data *prev; |
| 68 | } call_link; |
| 69 | |
| 70 | typedef enum { LEGACY_CALL, BATCH_CALL, REGISTERED_CALL } requested_call_type; |
| 71 | |
| 72 | typedef struct { |
| 73 | requested_call_type type; |
| 74 | void *tag; |
| 75 | union { |
| 76 | struct { |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 77 | grpc_completion_queue *cq_new; |
| 78 | grpc_completion_queue *cq_bind; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 79 | grpc_call **call; |
| 80 | grpc_call_details *details; |
| 81 | grpc_metadata_array *initial_metadata; |
| 82 | } batch; |
| 83 | struct { |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 84 | grpc_completion_queue *cq_new; |
| 85 | grpc_completion_queue *cq_bind; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 86 | grpc_call **call; |
| 87 | registered_method *registered_method; |
| 88 | gpr_timespec *deadline; |
| 89 | grpc_metadata_array *initial_metadata; |
| 90 | grpc_byte_buffer **optional_payload; |
| 91 | } registered; |
| 92 | } data; |
| 93 | } requested_call; |
| 94 | |
| 95 | typedef struct { |
| 96 | requested_call *calls; |
| 97 | size_t count; |
| 98 | size_t capacity; |
| 99 | } requested_call_array; |
| 100 | |
| 101 | struct registered_method { |
| 102 | char *method; |
| 103 | char *host; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 104 | call_data *pending; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 105 | requested_call_array requested; |
| 106 | registered_method *next; |
| 107 | }; |
| 108 | |
| 109 | typedef struct channel_registered_method { |
| 110 | registered_method *server_registered_method; |
| 111 | grpc_mdstr *method; |
| 112 | grpc_mdstr *host; |
| 113 | } channel_registered_method; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | |
| 115 | struct channel_data { |
| 116 | grpc_server *server; |
| 117 | grpc_channel *channel; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 118 | grpc_mdstr *path_key; |
| 119 | grpc_mdstr *authority_key; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | /* linked list of all channels on a server */ |
| 121 | channel_data *next; |
| 122 | channel_data *prev; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 123 | channel_registered_method *registered_methods; |
| 124 | gpr_uint32 registered_method_slots; |
| 125 | gpr_uint32 registered_method_max_probes; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | struct grpc_server { |
| 129 | size_t channel_filter_count; |
| 130 | const grpc_channel_filter **channel_filters; |
| 131 | grpc_channel_args *channel_args; |
| 132 | grpc_completion_queue *cq; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 133 | |
| 134 | gpr_mu mu; |
| 135 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 136 | registered_method *registered_methods; |
| 137 | requested_call_array requested_calls; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 138 | |
| 139 | gpr_uint8 shutdown; |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 140 | gpr_uint8 have_shutdown_tag; |
| 141 | void *shutdown_tag; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | |
| 143 | call_data *lists[CALL_LIST_COUNT]; |
| 144 | channel_data root_channel_data; |
| 145 | |
| 146 | listener *listeners; |
| 147 | gpr_refcount internal_refcount; |
| 148 | }; |
| 149 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 150 | typedef enum { |
| 151 | /* waiting for metadata */ |
| 152 | NOT_STARTED, |
| 153 | /* inital metadata read, not flow controlled in yet */ |
| 154 | PENDING, |
| 155 | /* flow controlled in, on completion queue */ |
| 156 | ACTIVATED, |
| 157 | /* cancelled before being queued */ |
| 158 | ZOMBIED |
| 159 | } call_state; |
| 160 | |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 161 | typedef struct legacy_data { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 162 | grpc_metadata_array initial_metadata; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 163 | } legacy_data; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 164 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 165 | struct call_data { |
| 166 | grpc_call *call; |
| 167 | |
| 168 | call_state state; |
| 169 | gpr_timespec deadline; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 170 | grpc_mdstr *path; |
| 171 | grpc_mdstr *host; |
| 172 | |
| 173 | legacy_data *legacy; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 174 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 175 | call_data **root[CALL_LIST_COUNT]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 176 | call_link links[CALL_LIST_COUNT]; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 177 | |
| 178 | grpc_completion_queue *cq_new; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | #define SERVER_FROM_CALL_ELEM(elem) \ |
| 182 | (((channel_data *)(elem)->channel_data)->server) |
| 183 | |
| 184 | static void do_nothing(void *unused, grpc_op_error ignored) {} |
| 185 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 186 | static void begin_call(grpc_server *server, call_data *calld, |
| 187 | requested_call *rc); |
| 188 | static void fail_call(grpc_server *server, requested_call *rc); |
| 189 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 190 | static int call_list_join(call_data **root, call_data *call, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 191 | call_list list) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 192 | GPR_ASSERT(!call->root[list]); |
| 193 | call->root[list] = root; |
| 194 | if (!*root) { |
| 195 | *root = call; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 196 | call->links[list].next = call->links[list].prev = call; |
| 197 | } else { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 198 | call->links[list].next = *root; |
| 199 | call->links[list].prev = (*root)->links[list].prev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 200 | call->links[list].next->links[list].prev = |
| 201 | call->links[list].prev->links[list].next = call; |
| 202 | } |
| 203 | return 1; |
| 204 | } |
| 205 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 206 | static call_data *call_list_remove_head(call_data **root, call_list list) { |
| 207 | call_data *out = *root; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 208 | if (out) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 209 | out->root[list] = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 210 | if (out->links[list].next == out) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 211 | *root = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | } else { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 213 | *root = out->links[list].next; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 214 | out->links[list].next->links[list].prev = out->links[list].prev; |
| 215 | out->links[list].prev->links[list].next = out->links[list].next; |
| 216 | } |
| 217 | } |
| 218 | return out; |
| 219 | } |
| 220 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 221 | static int call_list_remove(call_data *call, call_list list) { |
| 222 | call_data **root = call->root[list]; |
| 223 | if (root == NULL) return 0; |
| 224 | call->root[list] = NULL; |
| 225 | if (*root == call) { |
| 226 | *root = call->links[list].next; |
| 227 | if (*root == call) { |
| 228 | *root = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 229 | return 1; |
| 230 | } |
| 231 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 232 | GPR_ASSERT(*root != call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 233 | call->links[list].next->links[list].prev = call->links[list].prev; |
| 234 | call->links[list].prev->links[list].next = call->links[list].next; |
| 235 | return 1; |
| 236 | } |
| 237 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 238 | static void requested_call_array_destroy(requested_call_array *array) { |
| 239 | gpr_free(array->calls); |
| 240 | } |
| 241 | |
| 242 | static requested_call *requested_call_array_add(requested_call_array *array) { |
| 243 | requested_call *rc; |
| 244 | if (array->count == array->capacity) { |
| 245 | array->capacity = GPR_MAX(array->capacity + 8, array->capacity * 2); |
| 246 | array->calls = |
| 247 | gpr_realloc(array->calls, sizeof(requested_call) * array->capacity); |
| 248 | } |
| 249 | rc = &array->calls[array->count++]; |
| 250 | memset(rc, 0, sizeof(*rc)); |
| 251 | return rc; |
| 252 | } |
| 253 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 254 | static void server_ref(grpc_server *server) { |
| 255 | gpr_ref(&server->internal_refcount); |
| 256 | } |
| 257 | |
| 258 | static void server_unref(grpc_server *server) { |
| 259 | if (gpr_unref(&server->internal_refcount)) { |
| 260 | grpc_channel_args_destroy(server->channel_args); |
| 261 | gpr_mu_destroy(&server->mu); |
| 262 | gpr_free(server->channel_filters); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 263 | requested_call_array_destroy(&server->requested_calls); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 264 | gpr_free(server); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static int is_channel_orphaned(channel_data *chand) { |
| 269 | return chand->next == chand; |
| 270 | } |
| 271 | |
| 272 | static void orphan_channel(channel_data *chand) { |
| 273 | chand->next->prev = chand->prev; |
| 274 | chand->prev->next = chand->next; |
| 275 | chand->next = chand->prev = chand; |
| 276 | } |
| 277 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 278 | static void finish_destroy_channel(void *cd, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 279 | channel_data *chand = cd; |
| 280 | grpc_server *server = chand->server; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 281 | grpc_channel_destroy(chand->channel); |
| 282 | server_unref(server); |
| 283 | } |
| 284 | |
| 285 | static void destroy_channel(channel_data *chand) { |
| 286 | if (is_channel_orphaned(chand)) return; |
| 287 | GPR_ASSERT(chand->server != NULL); |
| 288 | orphan_channel(chand); |
| 289 | server_ref(chand->server); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 290 | grpc_iomgr_add_callback(finish_destroy_channel, chand); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 293 | static void finish_start_new_rpc_and_unlock(grpc_server *server, grpc_call_element *elem, call_data **pending_root, requested_call_array *array) { |
| 294 | requested_call rc; |
| 295 | call_data *calld = elem->call_data; |
| 296 | if (array->count == 0) { |
| 297 | calld->state = PENDING; |
| 298 | call_list_join(pending_root, calld, PENDING_START); |
| 299 | gpr_mu_unlock(&server->mu); |
| 300 | } else { |
| 301 | rc = server->requested_calls.calls[--server->requested_calls.count]; |
| 302 | calld->state = ACTIVATED; |
| 303 | gpr_mu_unlock(&server->mu); |
| 304 | begin_call(server, calld, &rc); |
| 305 | } |
| 306 | } |
| 307 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 308 | static void start_new_rpc(grpc_call_element *elem) { |
| 309 | channel_data *chand = elem->channel_data; |
| 310 | call_data *calld = elem->call_data; |
| 311 | grpc_server *server = chand->server; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 312 | gpr_uint32 i; |
| 313 | gpr_uint32 hash; |
| 314 | channel_registered_method *rm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 315 | |
| 316 | gpr_mu_lock(&server->mu); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 317 | if (chand->registered_methods && calld->path && calld->host) { |
| 318 | /* check for an exact match with host */ |
| 319 | hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash); |
| 320 | for (i = 0; i < chand->registered_method_max_probes; i++) { |
| 321 | rm = &chand->registered_methods[(hash + i) % chand->registered_method_slots]; |
| 322 | if (!rm) break; |
| 323 | if (rm->host != calld->host) continue; |
| 324 | if (rm->method != calld->path) continue; |
| 325 | finish_start_new_rpc_and_unlock(server, elem, &rm->server_registered_method->pending, &rm->server_registered_method->requested); |
| 326 | return; |
| 327 | } |
| 328 | /* check for a wildcard method definition (no host set) */ |
| 329 | hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash); |
| 330 | for (i = 0; i < chand->registered_method_max_probes; i++) { |
| 331 | rm = &chand->registered_methods[(hash + i) % chand->registered_method_slots]; |
| 332 | if (!rm) break; |
| 333 | if (rm->host != NULL) continue; |
| 334 | if (rm->method != calld->path) continue; |
| 335 | finish_start_new_rpc_and_unlock(server, elem, &rm->server_registered_method->pending, &rm->server_registered_method->requested); |
| 336 | return; |
| 337 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 338 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 339 | finish_start_new_rpc_and_unlock(server, elem, &server->lists[PENDING_START], &server->requested_calls); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 340 | } |
| 341 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 342 | static void kill_zombie(void *elem, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 343 | grpc_call_destroy(grpc_call_from_top_element(elem)); |
| 344 | } |
| 345 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 346 | static void stream_closed(grpc_call_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 347 | call_data *calld = elem->call_data; |
| 348 | channel_data *chand = elem->channel_data; |
| 349 | gpr_mu_lock(&chand->server->mu); |
| 350 | switch (calld->state) { |
| 351 | case ACTIVATED: |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 352 | break; |
| 353 | case PENDING: |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 354 | call_list_remove(calld, PENDING_START); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 355 | /* fallthrough intended */ |
| 356 | case NOT_STARTED: |
| 357 | calld->state = ZOMBIED; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 358 | grpc_iomgr_add_callback(kill_zombie, elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 359 | break; |
| 360 | case ZOMBIED: |
| 361 | break; |
| 362 | } |
| 363 | gpr_mu_unlock(&chand->server->mu); |
Craig Tiller | 0a927bf | 2015-02-05 10:52:53 -0800 | [diff] [blame] | 364 | grpc_call_stream_closed(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 367 | static void read_closed(grpc_call_element *elem) { |
| 368 | call_data *calld = elem->call_data; |
| 369 | channel_data *chand = elem->channel_data; |
| 370 | gpr_mu_lock(&chand->server->mu); |
| 371 | switch (calld->state) { |
| 372 | case ACTIVATED: |
| 373 | case PENDING: |
| 374 | grpc_call_read_closed(elem); |
| 375 | break; |
| 376 | case NOT_STARTED: |
| 377 | calld->state = ZOMBIED; |
| 378 | grpc_iomgr_add_callback(kill_zombie, elem); |
| 379 | break; |
| 380 | case ZOMBIED: |
| 381 | break; |
| 382 | } |
| 383 | gpr_mu_unlock(&chand->server->mu); |
| 384 | } |
| 385 | |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 386 | static void call_op(grpc_call_element *elem, grpc_call_element *from_elemn, |
| 387 | grpc_call_op *op) { |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 388 | channel_data *chand = elem->channel_data; |
| 389 | call_data *calld = elem->call_data; |
| 390 | grpc_mdelem *md; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 391 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 392 | switch (op->type) { |
| 393 | case GRPC_RECV_METADATA: |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 394 | md = op->data.metadata; |
| 395 | if (md->key == chand->path_key) { |
| 396 | calld->path = grpc_mdstr_ref(md->value); |
| 397 | grpc_mdelem_unref(md); |
| 398 | } else if (md->key == chand->authority_key) { |
| 399 | calld->host = grpc_mdstr_ref(md->value); |
| 400 | grpc_mdelem_unref(md); |
| 401 | } else { |
| 402 | grpc_call_recv_metadata(elem, md); |
| 403 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 404 | break; |
| 405 | case GRPC_RECV_END_OF_INITIAL_METADATA: |
| 406 | start_new_rpc(elem); |
Craig Tiller | 4069b68 | 2015-01-29 14:01:19 -0800 | [diff] [blame] | 407 | grpc_call_initial_metadata_complete(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 408 | break; |
| 409 | case GRPC_RECV_MESSAGE: |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 410 | grpc_call_recv_message(elem, op->data.message); |
| 411 | op->done_cb(op->user_data, GRPC_OP_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 412 | break; |
| 413 | case GRPC_RECV_HALF_CLOSE: |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 414 | read_closed(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 415 | break; |
| 416 | case GRPC_RECV_FINISH: |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 417 | stream_closed(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 418 | break; |
| 419 | case GRPC_RECV_DEADLINE: |
| 420 | grpc_call_set_deadline(elem, op->data.deadline); |
| 421 | ((call_data *)elem->call_data)->deadline = op->data.deadline; |
| 422 | break; |
| 423 | default: |
| 424 | GPR_ASSERT(op->dir == GRPC_CALL_DOWN); |
| 425 | grpc_call_next_op(elem, op); |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 430 | static void channel_op(grpc_channel_element *elem, |
| 431 | grpc_channel_element *from_elem, grpc_channel_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 432 | channel_data *chand = elem->channel_data; |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 433 | grpc_server *server = chand->server; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 434 | |
| 435 | switch (op->type) { |
| 436 | case GRPC_ACCEPT_CALL: |
| 437 | /* create a call */ |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 438 | grpc_call_create(chand->channel, NULL, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 439 | op->data.accept_call.transport_server_data); |
| 440 | break; |
| 441 | case GRPC_TRANSPORT_CLOSED: |
| 442 | /* if the transport is closed for a server channel, we destroy the |
| 443 | channel */ |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 444 | gpr_mu_lock(&server->mu); |
| 445 | server_ref(server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 446 | destroy_channel(chand); |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 447 | gpr_mu_unlock(&server->mu); |
| 448 | server_unref(server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 449 | break; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 450 | case GRPC_TRANSPORT_GOAWAY: |
| 451 | gpr_slice_unref(op->data.goaway.message); |
| 452 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 453 | default: |
| 454 | GPR_ASSERT(op->dir == GRPC_CALL_DOWN); |
| 455 | grpc_channel_next_op(elem, op); |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 460 | static void finish_shutdown_channel(void *cd, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 461 | channel_data *chand = cd; |
| 462 | grpc_channel_op op; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 463 | op.type = GRPC_CHANNEL_DISCONNECT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 464 | op.dir = GRPC_CALL_DOWN; |
| 465 | channel_op(grpc_channel_stack_element( |
| 466 | grpc_channel_get_channel_stack(chand->channel), 0), |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 467 | NULL, &op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 468 | grpc_channel_internal_unref(chand->channel); |
| 469 | } |
| 470 | |
| 471 | static void shutdown_channel(channel_data *chand) { |
| 472 | grpc_channel_internal_ref(chand->channel); |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 473 | grpc_iomgr_add_callback(finish_shutdown_channel, chand); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static void init_call_elem(grpc_call_element *elem, |
| 477 | const void *server_transport_data) { |
| 478 | call_data *calld = elem->call_data; |
| 479 | channel_data *chand = elem->channel_data; |
| 480 | memset(calld, 0, sizeof(call_data)); |
| 481 | calld->deadline = gpr_inf_future; |
| 482 | calld->call = grpc_call_from_top_element(elem); |
| 483 | |
| 484 | gpr_mu_lock(&chand->server->mu); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 485 | call_list_join(&chand->server->lists[ALL_CALLS], calld, ALL_CALLS); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 486 | gpr_mu_unlock(&chand->server->mu); |
| 487 | |
| 488 | server_ref(chand->server); |
| 489 | } |
| 490 | |
| 491 | static void destroy_call_elem(grpc_call_element *elem) { |
| 492 | channel_data *chand = elem->channel_data; |
Craig Tiller | db7db99 | 2015-01-29 11:19:01 -0800 | [diff] [blame] | 493 | call_data *calld = elem->call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 494 | int i; |
| 495 | |
| 496 | gpr_mu_lock(&chand->server->mu); |
| 497 | for (i = 0; i < CALL_LIST_COUNT; i++) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 498 | call_list_remove(elem->call_data, i); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 499 | } |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 500 | if (chand->server->shutdown && chand->server->have_shutdown_tag && |
| 501 | chand->server->lists[ALL_CALLS] == NULL) { |
| 502 | grpc_cq_end_server_shutdown(chand->server->cq, chand->server->shutdown_tag); |
| 503 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 504 | gpr_mu_unlock(&chand->server->mu); |
| 505 | |
Craig Tiller | 4df31a6 | 2015-01-30 09:44:31 -0800 | [diff] [blame] | 506 | if (calld->host) { |
| 507 | grpc_mdstr_unref(calld->host); |
| 508 | } |
| 509 | if (calld->path) { |
| 510 | grpc_mdstr_unref(calld->path); |
| 511 | } |
| 512 | |
Craig Tiller | db7db99 | 2015-01-29 11:19:01 -0800 | [diff] [blame] | 513 | if (calld->legacy) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 514 | gpr_free(calld->legacy->initial_metadata.metadata); |
Craig Tiller | db7db99 | 2015-01-29 11:19:01 -0800 | [diff] [blame] | 515 | gpr_free(calld->legacy); |
| 516 | } |
| 517 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 518 | server_unref(chand->server); |
| 519 | } |
| 520 | |
| 521 | static void init_channel_elem(grpc_channel_element *elem, |
| 522 | const grpc_channel_args *args, |
| 523 | grpc_mdctx *metadata_context, int is_first, |
| 524 | int is_last) { |
| 525 | channel_data *chand = elem->channel_data; |
| 526 | GPR_ASSERT(is_first); |
| 527 | GPR_ASSERT(!is_last); |
| 528 | chand->server = NULL; |
| 529 | chand->channel = NULL; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 530 | chand->path_key = grpc_mdstr_from_string(metadata_context, ":path"); |
| 531 | chand->authority_key = grpc_mdstr_from_string(metadata_context, ":authority"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 532 | chand->next = chand->prev = chand; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 533 | chand->registered_methods = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static void destroy_channel_elem(grpc_channel_element *elem) { |
| 537 | channel_data *chand = elem->channel_data; |
| 538 | if (chand->server) { |
| 539 | gpr_mu_lock(&chand->server->mu); |
| 540 | chand->next->prev = chand->prev; |
| 541 | chand->prev->next = chand->next; |
| 542 | chand->next = chand->prev = chand; |
| 543 | gpr_mu_unlock(&chand->server->mu); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 544 | grpc_mdstr_unref(chand->path_key); |
| 545 | grpc_mdstr_unref(chand->authority_key); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 546 | server_unref(chand->server); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | static const grpc_channel_filter server_surface_filter = { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 551 | call_op, channel_op, sizeof(call_data), init_call_elem, destroy_call_elem, |
| 552 | sizeof(channel_data), init_channel_elem, destroy_channel_elem, "server", |
Craig Tiller | 9f28ac2 | 2015-01-27 17:01:29 -0800 | [diff] [blame] | 553 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 554 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 555 | grpc_server *grpc_server_create_from_filters(grpc_completion_queue *cq, |
| 556 | grpc_channel_filter **filters, |
| 557 | size_t filter_count, |
| 558 | const grpc_channel_args *args) { |
| 559 | size_t i; |
| 560 | int census_enabled = grpc_channel_args_is_census_enabled(args); |
| 561 | |
| 562 | grpc_server *server = gpr_malloc(sizeof(grpc_server)); |
| 563 | memset(server, 0, sizeof(grpc_server)); |
| 564 | |
| 565 | gpr_mu_init(&server->mu); |
| 566 | |
| 567 | server->cq = cq; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 568 | /* decremented by grpc_server_destroy */ |
| 569 | gpr_ref_init(&server->internal_refcount, 1); |
| 570 | server->root_channel_data.next = server->root_channel_data.prev = |
| 571 | &server->root_channel_data; |
| 572 | |
| 573 | /* Server filter stack is: |
| 574 | |
| 575 | server_surface_filter - for making surface API calls |
| 576 | grpc_server_census_filter (optional) - for stats collection and tracing |
| 577 | {passed in filter stack} |
| 578 | grpc_connected_channel_filter - for interfacing with transports */ |
| 579 | server->channel_filter_count = filter_count + 1 + census_enabled; |
| 580 | server->channel_filters = |
| 581 | gpr_malloc(server->channel_filter_count * sizeof(grpc_channel_filter *)); |
| 582 | server->channel_filters[0] = &server_surface_filter; |
| 583 | if (census_enabled) { |
| 584 | server->channel_filters[1] = &grpc_server_census_filter; |
| 585 | } |
| 586 | for (i = 0; i < filter_count; i++) { |
| 587 | server->channel_filters[i + 1 + census_enabled] = filters[i]; |
| 588 | } |
| 589 | |
| 590 | server->channel_args = grpc_channel_args_copy(args); |
| 591 | |
| 592 | return server; |
| 593 | } |
| 594 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 595 | static int streq(const char *a, const char *b) { |
| 596 | if (a == NULL && b == NULL) return 1; |
| 597 | if (a == NULL) return 0; |
| 598 | if (b == NULL) return 0; |
| 599 | return 0 == strcmp(a, b); |
| 600 | } |
| 601 | |
| 602 | void *grpc_server_register_method(grpc_server *server, const char *method, |
| 603 | const char *host) { |
| 604 | registered_method *m; |
| 605 | if (!method) { |
| 606 | gpr_log(GPR_ERROR, "%s method string cannot be NULL", __FUNCTION__); |
| 607 | return NULL; |
| 608 | } |
| 609 | for (m = server->registered_methods; m; m = m->next) { |
| 610 | if (streq(m->method, method) && streq(m->host, host)) { |
| 611 | gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method, |
| 612 | host ? host : "*"); |
| 613 | return NULL; |
| 614 | } |
| 615 | } |
| 616 | m = gpr_malloc(sizeof(registered_method)); |
| 617 | memset(m, 0, sizeof(*m)); |
| 618 | m->method = gpr_strdup(method); |
| 619 | m->host = gpr_strdup(host); |
| 620 | m->next = server->registered_methods; |
| 621 | server->registered_methods = m; |
| 622 | return m; |
| 623 | } |
| 624 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 625 | void grpc_server_start(grpc_server *server) { |
| 626 | listener *l; |
| 627 | |
| 628 | for (l = server->listeners; l; l = l->next) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 629 | l->start(server, l->arg, grpc_cq_pollset(server->cq)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
| 633 | grpc_transport_setup_result grpc_server_setup_transport( |
| 634 | grpc_server *s, grpc_transport *transport, |
| 635 | grpc_channel_filter const **extra_filters, size_t num_extra_filters, |
| 636 | grpc_mdctx *mdctx) { |
| 637 | size_t num_filters = s->channel_filter_count + num_extra_filters + 1; |
| 638 | grpc_channel_filter const **filters = |
| 639 | gpr_malloc(sizeof(grpc_channel_filter *) * num_filters); |
| 640 | size_t i; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 641 | size_t num_registered_methods; |
| 642 | size_t alloc; |
| 643 | registered_method *rm; |
| 644 | channel_registered_method *crm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 645 | grpc_channel *channel; |
| 646 | channel_data *chand; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 647 | grpc_mdstr *host; |
| 648 | grpc_mdstr *method; |
| 649 | gpr_uint32 hash; |
| 650 | gpr_uint32 slots; |
| 651 | gpr_uint32 probes; |
| 652 | gpr_uint32 max_probes = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 653 | |
| 654 | for (i = 0; i < s->channel_filter_count; i++) { |
| 655 | filters[i] = s->channel_filters[i]; |
| 656 | } |
| 657 | for (; i < s->channel_filter_count + num_extra_filters; i++) { |
| 658 | filters[i] = extra_filters[i - s->channel_filter_count]; |
| 659 | } |
| 660 | filters[i] = &grpc_connected_channel_filter; |
| 661 | |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 662 | grpc_transport_add_to_pollset(transport, grpc_cq_pollset(s->cq)); |
| 663 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 664 | channel = grpc_channel_create_from_filters(filters, num_filters, |
| 665 | s->channel_args, mdctx, 0); |
| 666 | chand = (channel_data *)grpc_channel_stack_element( |
| 667 | grpc_channel_get_channel_stack(channel), 0)->channel_data; |
| 668 | chand->server = s; |
| 669 | server_ref(s); |
| 670 | chand->channel = channel; |
| 671 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 672 | num_registered_methods = 0; |
| 673 | for (rm = s->registered_methods; rm; rm = rm->next) { |
| 674 | num_registered_methods++; |
| 675 | } |
| 676 | /* build a lookup table phrased in terms of mdstr's in this channels context |
| 677 | to quickly find registered methods */ |
| 678 | if (num_registered_methods > 0) { |
| 679 | slots = 2 * num_registered_methods; |
| 680 | alloc = sizeof(channel_registered_method) * slots; |
| 681 | chand->registered_methods = gpr_malloc(alloc); |
| 682 | memset(chand->registered_methods, 0, alloc); |
| 683 | for (rm = s->registered_methods; rm; rm = rm->next) { |
| 684 | host = rm->host ? grpc_mdstr_from_string(mdctx, rm->host) : NULL; |
| 685 | method = grpc_mdstr_from_string(mdctx, rm->host); |
| 686 | hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash); |
| 687 | for (probes = 0; chand->registered_methods[(hash + probes) % slots].server_registered_method != NULL; probes++); |
| 688 | if (probes > max_probes) max_probes = probes; |
| 689 | crm = &chand->registered_methods[(hash + probes) % slots]; |
| 690 | crm->server_registered_method = rm; |
| 691 | crm->host = host; |
| 692 | crm->method = method; |
| 693 | } |
| 694 | chand->registered_method_slots = slots; |
| 695 | chand->registered_method_max_probes = max_probes; |
| 696 | } |
| 697 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 698 | gpr_mu_lock(&s->mu); |
| 699 | chand->next = &s->root_channel_data; |
| 700 | chand->prev = chand->next->prev; |
| 701 | chand->next->prev = chand->prev->next = chand; |
| 702 | gpr_mu_unlock(&s->mu); |
| 703 | |
| 704 | gpr_free(filters); |
| 705 | |
| 706 | return grpc_connected_channel_bind_transport( |
| 707 | grpc_channel_get_channel_stack(channel), transport); |
| 708 | } |
| 709 | |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 710 | void shutdown_internal(grpc_server *server, gpr_uint8 have_shutdown_tag, |
| 711 | void *shutdown_tag) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 712 | listener *l; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 713 | requested_call_array requested_calls; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 714 | channel_data **channels; |
| 715 | channel_data *c; |
| 716 | size_t nchannels; |
| 717 | size_t i; |
| 718 | grpc_channel_op op; |
| 719 | grpc_channel_element *elem; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 720 | |
| 721 | /* lock, and gather up some stuff to do */ |
| 722 | gpr_mu_lock(&server->mu); |
| 723 | if (server->shutdown) { |
| 724 | gpr_mu_unlock(&server->mu); |
| 725 | return; |
| 726 | } |
| 727 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 728 | nchannels = 0; |
| 729 | for (c = server->root_channel_data.next; c != &server->root_channel_data; |
| 730 | c = c->next) { |
| 731 | nchannels++; |
| 732 | } |
| 733 | channels = gpr_malloc(sizeof(channel_data *) * nchannels); |
| 734 | i = 0; |
| 735 | for (c = server->root_channel_data.next; c != &server->root_channel_data; |
| 736 | c = c->next) { |
| 737 | grpc_channel_internal_ref(c->channel); |
| 738 | channels[i] = c; |
| 739 | i++; |
| 740 | } |
| 741 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 742 | requested_calls = server->requested_calls; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 743 | memset(&server->requested_calls, 0, sizeof(server->requested_calls)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 744 | |
| 745 | server->shutdown = 1; |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 746 | server->have_shutdown_tag = have_shutdown_tag; |
| 747 | server->shutdown_tag = shutdown_tag; |
| 748 | if (have_shutdown_tag) { |
| 749 | grpc_cq_begin_op(server->cq, NULL, GRPC_SERVER_SHUTDOWN); |
| 750 | if (server->lists[ALL_CALLS] == NULL) { |
| 751 | grpc_cq_end_server_shutdown(server->cq, shutdown_tag); |
| 752 | } |
| 753 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 754 | gpr_mu_unlock(&server->mu); |
| 755 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 756 | for (i = 0; i < nchannels; i++) { |
| 757 | c = channels[i]; |
| 758 | elem = grpc_channel_stack_element( |
| 759 | grpc_channel_get_channel_stack(c->channel), 0); |
| 760 | |
| 761 | op.type = GRPC_CHANNEL_GOAWAY; |
| 762 | op.dir = GRPC_CALL_DOWN; |
| 763 | op.data.goaway.status = GRPC_STATUS_OK; |
| 764 | op.data.goaway.message = gpr_slice_from_copied_string("Server shutdown"); |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 765 | elem->filter->channel_op(elem, NULL, &op); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 766 | |
| 767 | grpc_channel_internal_unref(c->channel); |
| 768 | } |
| 769 | gpr_free(channels); |
| 770 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 771 | /* terminate all the requested calls */ |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 772 | for (i = 0; i < requested_calls.count; i++) { |
| 773 | fail_call(server, &requested_calls.calls[i]); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 774 | } |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 775 | gpr_free(requested_calls.calls); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 776 | |
| 777 | /* Shutdown listeners */ |
| 778 | for (l = server->listeners; l; l = l->next) { |
| 779 | l->destroy(server, l->arg); |
| 780 | } |
| 781 | while (server->listeners) { |
| 782 | l = server->listeners; |
| 783 | server->listeners = l->next; |
| 784 | gpr_free(l); |
| 785 | } |
| 786 | } |
| 787 | |
Craig Tiller | 4ffdcd5 | 2015-01-16 11:34:55 -0800 | [diff] [blame] | 788 | void grpc_server_shutdown(grpc_server *server) { |
| 789 | shutdown_internal(server, 0, NULL); |
| 790 | } |
| 791 | |
| 792 | void grpc_server_shutdown_and_notify(grpc_server *server, void *tag) { |
| 793 | shutdown_internal(server, 1, tag); |
| 794 | } |
| 795 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 796 | void grpc_server_destroy(grpc_server *server) { |
| 797 | channel_data *c; |
| 798 | gpr_mu_lock(&server->mu); |
| 799 | for (c = server->root_channel_data.next; c != &server->root_channel_data; |
| 800 | c = c->next) { |
| 801 | shutdown_channel(c); |
| 802 | } |
| 803 | gpr_mu_unlock(&server->mu); |
| 804 | |
| 805 | server_unref(server); |
| 806 | } |
| 807 | |
| 808 | void grpc_server_add_listener(grpc_server *server, void *arg, |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 809 | void (*start)(grpc_server *server, void *arg, |
| 810 | grpc_pollset *pollset), |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 811 | void (*destroy)(grpc_server *server, void *arg)) { |
| 812 | listener *l = gpr_malloc(sizeof(listener)); |
| 813 | l->arg = arg; |
| 814 | l->start = start; |
| 815 | l->destroy = destroy; |
| 816 | l->next = server->listeners; |
| 817 | server->listeners = l; |
| 818 | } |
| 819 | |
Craig Tiller | 9f28ac2 | 2015-01-27 17:01:29 -0800 | [diff] [blame] | 820 | static grpc_call_error queue_call_request(grpc_server *server, |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 821 | requested_call *rc) { |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame^] | 822 | call_data *calld = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 823 | gpr_mu_lock(&server->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 824 | if (server->shutdown) { |
| 825 | gpr_mu_unlock(&server->mu); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 826 | fail_call(server, rc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 827 | return GRPC_CALL_OK; |
| 828 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 829 | switch (rc->type) { |
| 830 | case LEGACY_CALL: |
| 831 | case BATCH_CALL: |
| 832 | calld = call_list_remove_head(&server->lists[PENDING_START], PENDING_START); |
| 833 | break; |
| 834 | case REGISTERED_CALL: |
| 835 | calld = call_list_remove_head(&rc->data.registered.registered_method->pending, PENDING_START); |
| 836 | break; |
| 837 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 838 | if (calld) { |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 839 | GPR_ASSERT(calld->state == PENDING); |
Craig Tiller | bb3f22f | 2015-01-29 16:40:56 -0800 | [diff] [blame] | 840 | calld->state = ACTIVATED; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 841 | gpr_mu_unlock(&server->mu); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 842 | begin_call(server, calld, rc); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 843 | return GRPC_CALL_OK; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 844 | } else { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 845 | *requested_call_array_add(&server->requested_calls) = *rc; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 846 | gpr_mu_unlock(&server->mu); |
| 847 | return GRPC_CALL_OK; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 848 | } |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 849 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 850 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 851 | grpc_call_error grpc_server_request_call(grpc_server *server, grpc_call **call, |
| 852 | grpc_call_details *details, |
| 853 | grpc_metadata_array *initial_metadata, |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 854 | grpc_completion_queue *cq_new, |
| 855 | grpc_completion_queue *cq_bind, void *tag) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 856 | requested_call rc; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 857 | grpc_cq_begin_op(cq_new, NULL, GRPC_OP_COMPLETE); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 858 | rc.type = BATCH_CALL; |
| 859 | rc.tag = tag; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 860 | rc.data.batch.cq_new = cq_new; |
| 861 | rc.data.batch.cq_bind = cq_bind; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 862 | rc.data.batch.call = call; |
| 863 | rc.data.batch.details = details; |
| 864 | rc.data.batch.initial_metadata = initial_metadata; |
| 865 | return queue_call_request(server, &rc); |
| 866 | } |
| 867 | |
| 868 | grpc_call_error grpc_server_request_registered_call( |
| 869 | grpc_server *server, void *registered_method, grpc_call **call, |
| 870 | gpr_timespec *deadline, grpc_metadata_array *initial_metadata, |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 871 | grpc_byte_buffer **optional_payload, grpc_completion_queue *cq_new, grpc_completion_queue *cq_bind, |
| 872 | void *tag) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 873 | requested_call rc; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 874 | grpc_cq_begin_op(cq_new, NULL, GRPC_OP_COMPLETE); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 875 | rc.type = REGISTERED_CALL; |
| 876 | rc.tag = tag; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 877 | rc.data.registered.cq_new = cq_new; |
| 878 | rc.data.registered.cq_bind = cq_bind; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 879 | rc.data.registered.call = call; |
| 880 | rc.data.registered.registered_method = registered_method; |
| 881 | rc.data.registered.deadline = deadline; |
| 882 | rc.data.registered.initial_metadata = initial_metadata; |
| 883 | rc.data.registered.optional_payload = optional_payload; |
| 884 | return queue_call_request(server, &rc); |
| 885 | } |
| 886 | |
| 887 | grpc_call_error grpc_server_request_call_old(grpc_server *server, |
| 888 | void *tag_new) { |
| 889 | requested_call rc; |
| 890 | grpc_cq_begin_op(server->cq, NULL, GRPC_SERVER_RPC_NEW); |
| 891 | rc.type = LEGACY_CALL; |
| 892 | rc.tag = tag_new; |
| 893 | return queue_call_request(server, &rc); |
| 894 | } |
| 895 | |
| 896 | static void publish_legacy(grpc_call *call, grpc_op_error status, void *tag); |
| 897 | static void publish_registered_or_batch(grpc_call *call, grpc_op_error status, |
| 898 | void *tag); |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame^] | 899 | static void publish_was_not_set(grpc_call *call, grpc_op_error status, void *tag) { |
| 900 | abort(); |
| 901 | } |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 902 | |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 903 | static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) { |
| 904 | gpr_slice slice = value->slice; |
| 905 | size_t len = GPR_SLICE_LENGTH(slice); |
| 906 | |
| 907 | if (len + 1 > *capacity) { |
| 908 | *capacity = GPR_MAX(len + 1, *capacity * 2); |
| 909 | *dest = gpr_realloc(*dest, *capacity); |
| 910 | } |
| 911 | memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1); |
| 912 | } |
| 913 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 914 | static void begin_call(grpc_server *server, call_data *calld, |
| 915 | requested_call *rc) { |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame^] | 916 | grpc_ioreq_completion_func publish = publish_was_not_set; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 917 | grpc_ioreq req[2]; |
| 918 | grpc_ioreq *r = req; |
| 919 | |
| 920 | /* called once initial metadata has been read by the call, but BEFORE |
| 921 | the ioreq to fetch it out of the call has been executed. |
| 922 | This means metadata related fields can be relied on in calld, but to |
| 923 | fill in the metadata array passed by the client, we need to perform |
| 924 | an ioreq op, that should complete immediately. */ |
| 925 | |
| 926 | switch (rc->type) { |
| 927 | case LEGACY_CALL: |
| 928 | calld->legacy = gpr_malloc(sizeof(legacy_data)); |
| 929 | memset(calld->legacy, 0, sizeof(legacy_data)); |
| 930 | r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; |
| 931 | r->data.recv_metadata = &calld->legacy->initial_metadata; |
| 932 | r++; |
| 933 | publish = publish_legacy; |
| 934 | break; |
| 935 | case BATCH_CALL: |
| 936 | cpstr(&rc->data.batch.details->host, |
| 937 | &rc->data.batch.details->host_capacity, calld->host); |
| 938 | cpstr(&rc->data.batch.details->method, |
| 939 | &rc->data.batch.details->method_capacity, calld->path); |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 940 | grpc_call_set_completion_queue(calld->call, rc->data.batch.cq_bind); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 941 | *rc->data.batch.call = calld->call; |
| 942 | r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; |
| 943 | r->data.recv_metadata = rc->data.batch.initial_metadata; |
| 944 | r++; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 945 | calld->cq_new = rc->data.batch.cq_new; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 946 | publish = publish_registered_or_batch; |
| 947 | break; |
| 948 | case REGISTERED_CALL: |
| 949 | *rc->data.registered.deadline = calld->deadline; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 950 | grpc_call_set_completion_queue(calld->call, rc->data.registered.cq_bind); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 951 | *rc->data.registered.call = calld->call; |
| 952 | r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; |
| 953 | r->data.recv_metadata = rc->data.registered.initial_metadata; |
| 954 | r++; |
| 955 | if (rc->data.registered.optional_payload) { |
| 956 | r->op = GRPC_IOREQ_RECV_MESSAGE; |
| 957 | r->data.recv_message = rc->data.registered.optional_payload; |
| 958 | r++; |
| 959 | } |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 960 | calld->cq_new = rc->data.registered.cq_new; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 961 | publish = publish_registered_or_batch; |
| 962 | break; |
| 963 | } |
| 964 | |
| 965 | grpc_call_internal_ref(calld->call); |
| 966 | grpc_call_start_ioreq_and_call_back(calld->call, req, r - req, publish, |
| 967 | rc->tag); |
| 968 | } |
| 969 | |
| 970 | static void fail_call(grpc_server *server, requested_call *rc) { |
| 971 | switch (rc->type) { |
| 972 | case LEGACY_CALL: |
| 973 | grpc_cq_end_new_rpc(server->cq, rc->tag, NULL, do_nothing, NULL, NULL, |
| 974 | NULL, gpr_inf_past, 0, NULL); |
| 975 | break; |
| 976 | case BATCH_CALL: |
| 977 | *rc->data.batch.call = NULL; |
| 978 | rc->data.batch.initial_metadata->count = 0; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 979 | grpc_cq_end_op_complete(rc->data.batch.cq_new, rc->tag, NULL, do_nothing, |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 980 | NULL, GRPC_OP_ERROR); |
| 981 | break; |
| 982 | case REGISTERED_CALL: |
| 983 | *rc->data.registered.call = NULL; |
| 984 | rc->data.registered.initial_metadata->count = 0; |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 985 | grpc_cq_end_op_complete(rc->data.registered.cq_new, rc->tag, NULL, do_nothing, |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 986 | NULL, GRPC_OP_ERROR); |
| 987 | break; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | static void publish_legacy(grpc_call *call, grpc_op_error status, void *tag) { |
| 992 | grpc_call_element *elem = |
| 993 | grpc_call_stack_element(grpc_call_get_call_stack(call), 0); |
| 994 | call_data *calld = elem->call_data; |
| 995 | channel_data *chand = elem->channel_data; |
| 996 | grpc_server *server = chand->server; |
| 997 | |
| 998 | if (status == GRPC_OP_OK) { |
| 999 | grpc_cq_end_new_rpc(server->cq, tag, call, do_nothing, NULL, |
| 1000 | grpc_mdstr_as_c_string(calld->path), |
| 1001 | grpc_mdstr_as_c_string(calld->host), calld->deadline, |
| 1002 | calld->legacy->initial_metadata.count, |
| 1003 | calld->legacy->initial_metadata.metadata); |
| 1004 | } else { |
| 1005 | gpr_log(GPR_ERROR, "should never reach here"); |
| 1006 | abort(); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | static void publish_registered_or_batch(grpc_call *call, grpc_op_error status, |
| 1011 | void *tag) { |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 1012 | grpc_call_element *elem = |
| 1013 | grpc_call_stack_element(grpc_call_get_call_stack(call), 0); |
| 1014 | call_data *calld = elem->call_data; |
| 1015 | grpc_cq_end_op_complete(calld->cq_new, tag, call, |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1016 | do_nothing, NULL, status); |
| 1017 | } |
| 1018 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1019 | const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) { |
| 1020 | return server->channel_args; |
| 1021 | } |