blob: ad4d0b54811154f1a535c40a25e5795896a8ed2b [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/surface/server.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
Craig Tillerf96dfc32015-09-10 14:43:18 -070036#include <limits.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#include <stdlib.h>
38#include <string.h>
39
Craig Tiller6a006ce2015-07-13 16:25:40 -070040#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
42#include <grpc/support/string_util.h>
43#include <grpc/support/useful.h>
44
Craig Tiller9533d042016-03-25 17:11:06 -070045#include "src/core/lib/channel/channel_args.h"
46#include "src/core/lib/channel/connected_channel.h"
47#include "src/core/lib/iomgr/iomgr.h"
48#include "src/core/lib/support/stack_lockfree.h"
49#include "src/core/lib/support/string.h"
50#include "src/core/lib/surface/api_trace.h"
51#include "src/core/lib/surface/call.h"
52#include "src/core/lib/surface/channel.h"
53#include "src/core/lib/surface/completion_queue.h"
54#include "src/core/lib/surface/init.h"
55#include "src/core/lib/transport/metadata.h"
56#include "src/core/lib/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057
Craig Tillera82950e2015-09-22 12:33:20 -070058typedef struct listener {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059 void *arg;
Craig Tillera82950e2015-09-22 12:33:20 -070060 void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
61 grpc_pollset **pollsets, size_t pollset_count);
62 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
63 grpc_closure *closure);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064 struct listener *next;
Craig Tillerdfff1b82015-09-21 14:39:57 -070065 grpc_closure destroy_done;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066} listener;
67
68typedef struct call_data call_data;
69typedef struct channel_data channel_data;
Craig Tiller24be0f72015-02-10 14:04:22 -080070typedef struct registered_method registered_method;
71
Craig Tillera82950e2015-09-22 12:33:20 -070072typedef struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080073 call_data *next;
74 call_data *prev;
75} call_link;
76
Craig Tillera82950e2015-09-22 12:33:20 -070077typedef enum { BATCH_CALL, REGISTERED_CALL } requested_call_type;
Craig Tiller24be0f72015-02-10 14:04:22 -080078
Craig Tillera82950e2015-09-22 12:33:20 -070079typedef struct requested_call {
Craig Tiller24be0f72015-02-10 14:04:22 -080080 requested_call_type type;
81 void *tag;
Craig Tiller6a006ce2015-07-13 16:25:40 -070082 grpc_server *server;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070083 grpc_completion_queue *cq_bound_to_call;
84 grpc_completion_queue *cq_for_notification;
85 grpc_call **call;
Craig Tiller97fc6a32015-07-08 15:31:35 -070086 grpc_cq_completion completion;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080087 grpc_metadata_array *initial_metadata;
Craig Tillera82950e2015-09-22 12:33:20 -070088 union {
89 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080090 grpc_call_details *details;
Craig Tiller24be0f72015-02-10 14:04:22 -080091 } batch;
Craig Tillera82950e2015-09-22 12:33:20 -070092 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080093 registered_method *registered_method;
94 gpr_timespec *deadline;
Craig Tiller24be0f72015-02-10 14:04:22 -080095 grpc_byte_buffer **optional_payload;
96 } registered;
97 } data;
98} requested_call;
99
Craig Tillera82950e2015-09-22 12:33:20 -0700100typedef struct channel_registered_method {
Craig Tiller24be0f72015-02-10 14:04:22 -0800101 registered_method *server_registered_method;
Craig Tillerb2906862016-03-10 06:50:07 -0800102 uint32_t flags;
Craig Tiller24be0f72015-02-10 14:04:22 -0800103 grpc_mdstr *method;
104 grpc_mdstr *host;
105} channel_registered_method;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106
Craig Tillera82950e2015-09-22 12:33:20 -0700107struct channel_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800108 grpc_server *server;
Craig Tillere039f032015-06-25 12:54:23 -0700109 grpc_connectivity_state connectivity_state;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 grpc_channel *channel;
111 /* linked list of all channels on a server */
112 channel_data *next;
113 channel_data *prev;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800114 channel_registered_method *registered_methods;
Craig Tiller7536af02015-12-22 13:49:30 -0800115 uint32_t registered_method_slots;
116 uint32_t registered_method_max_probes;
Craig Tiller33825112015-09-18 07:44:19 -0700117 grpc_closure finish_destroy_channel_closure;
118 grpc_closure channel_connectivity_changed;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119};
120
Craig Tillera82950e2015-09-22 12:33:20 -0700121typedef struct shutdown_tag {
Craig Tillerbce999f2015-05-27 09:55:51 -0700122 void *tag;
123 grpc_completion_queue *cq;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700124 grpc_cq_completion completion;
Craig Tillerbce999f2015-05-27 09:55:51 -0700125} shutdown_tag;
126
Craig Tillera82950e2015-09-22 12:33:20 -0700127typedef enum {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128 /* waiting for metadata */
129 NOT_STARTED,
130 /* inital metadata read, not flow controlled in yet */
131 PENDING,
132 /* flow controlled in, on completion queue */
133 ACTIVATED,
134 /* cancelled before being queued */
135 ZOMBIED
136} call_state;
137
Craig Tiller729b35a2015-07-13 12:36:47 -0700138typedef struct request_matcher request_matcher;
139
Craig Tillera82950e2015-09-22 12:33:20 -0700140struct call_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141 grpc_call *call;
142
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700143 /** protects state */
144 gpr_mu mu_state;
145 /** the current state of a call - see call_state */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146 call_state state;
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700147
Craig Tillercce17ac2015-01-20 09:29:28 -0800148 grpc_mdstr *path;
149 grpc_mdstr *host;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700150 gpr_timespec deadline;
Craig Tillercce17ac2015-01-20 09:29:28 -0800151
Craig Tiller20bc56d2015-02-12 09:02:56 -0800152 grpc_completion_queue *cq_new;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800153
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800154 grpc_metadata_batch *recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800155 bool recv_idempotent_request;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800156 grpc_metadata_array initial_metadata;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700157
Craig Tiller88512692016-04-04 09:32:52 -0700158 request_matcher *request_matcher;
159 grpc_byte_buffer *payload;
160
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800161 grpc_closure got_initial_metadata;
162 grpc_closure server_on_recv_initial_metadata;
Craig Tiller33825112015-09-18 07:44:19 -0700163 grpc_closure kill_zombie_closure;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800164 grpc_closure *on_done_recv_initial_metadata;
David Garcia Quintas284488b2015-05-28 16:27:39 -0700165
Craig Tiller88512692016-04-04 09:32:52 -0700166 grpc_closure publish;
167
Craig Tiller729b35a2015-07-13 12:36:47 -0700168 call_data *pending_next;
169};
170
Craig Tillera82950e2015-09-22 12:33:20 -0700171struct request_matcher {
Craig Tiller88512692016-04-04 09:32:52 -0700172 grpc_server *server;
Craig Tiller729b35a2015-07-13 12:36:47 -0700173 call_data *pending_head;
174 call_data *pending_tail;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700175 gpr_stack_lockfree *requests;
Craig Tiller729b35a2015-07-13 12:36:47 -0700176};
177
Craig Tillera82950e2015-09-22 12:33:20 -0700178struct registered_method {
Craig Tiller729b35a2015-07-13 12:36:47 -0700179 char *method;
180 char *host;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700181 grpc_server_register_method_payload_handling payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -0800182 uint32_t flags;
Craig Tiller729b35a2015-07-13 12:36:47 -0700183 request_matcher request_matcher;
184 registered_method *next;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185};
186
Craig Tillera82950e2015-09-22 12:33:20 -0700187typedef struct {
Craig Tillerff3ae682015-06-29 17:44:04 -0700188 grpc_channel **channels;
189 size_t num_channels;
190} channel_broadcaster;
191
Craig Tillera82950e2015-09-22 12:33:20 -0700192struct grpc_server {
Craig Tiller729b35a2015-07-13 12:36:47 -0700193 grpc_channel_args *channel_args;
194
195 grpc_completion_queue **cqs;
196 grpc_pollset **pollsets;
197 size_t cq_count;
198
199 /* The two following mutexes control access to server-state
200 mu_global controls access to non-call-related state (e.g., channel state)
201 mu_call controls access to call-related state (e.g., the call lists)
202
203 If they are ever required to be nested, you must lock mu_global
204 before mu_call. This is currently used in shutdown processing
205 (grpc_server_shutdown_and_notify and maybe_finish_shutdown) */
Craig Tillera82950e2015-09-22 12:33:20 -0700206 gpr_mu mu_global; /* mutex for server and channel state */
207 gpr_mu mu_call; /* mutex for call-specific state */
Craig Tiller729b35a2015-07-13 12:36:47 -0700208
209 registered_method *registered_methods;
210 request_matcher unregistered_request_matcher;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700211 /** free list of available requested_calls indices */
212 gpr_stack_lockfree *request_freelist;
213 /** requested call backing data */
214 requested_call *requested_calls;
Craig Tiller32ca48c2015-09-10 11:47:15 -0700215 size_t max_requested_calls;
Craig Tiller729b35a2015-07-13 12:36:47 -0700216
Craig Tiller6a006ce2015-07-13 16:25:40 -0700217 gpr_atm shutdown_flag;
Craig Tiller7536af02015-12-22 13:49:30 -0800218 uint8_t shutdown_published;
Craig Tiller729b35a2015-07-13 12:36:47 -0700219 size_t num_shutdown_tags;
220 shutdown_tag *shutdown_tags;
221
222 channel_data root_channel_data;
223
224 listener *listeners;
225 int listeners_destroyed;
226 gpr_refcount internal_refcount;
227
228 /** when did we print the last shutdown progress message */
229 gpr_timespec last_shutdown_message_time;
230};
231
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800232#define SERVER_FROM_CALL_ELEM(elem) \
233 (((channel_data *)(elem)->channel_data)->server)
234
Craig Tillerf51457b2016-05-03 17:06:32 -0700235static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *calld,
236 grpc_error *error);
Craig Tillera82950e2015-09-22 12:33:20 -0700237static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
238 requested_call *rc);
Vijay Pai8931cdd2015-06-17 12:42:17 -0700239/* Before calling maybe_finish_shutdown, we must hold mu_global and not
240 hold mu_call */
Craig Tillera82950e2015-09-22 12:33:20 -0700241static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_server *server);
Craig Tiller24be0f72015-02-10 14:04:22 -0800242
Craig Tiller729b35a2015-07-13 12:36:47 -0700243/*
244 * channel broadcaster
245 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700246
247/* assumes server locked */
Craig Tillera82950e2015-09-22 12:33:20 -0700248static void channel_broadcaster_init(grpc_server *s, channel_broadcaster *cb) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700249 channel_data *c;
250 size_t count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700251 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
252 count++;
253 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700254 cb->num_channels = count;
Craig Tillera82950e2015-09-22 12:33:20 -0700255 cb->channels = gpr_malloc(sizeof(*cb->channels) * cb->num_channels);
Craig Tillerff3ae682015-06-29 17:44:04 -0700256 count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700257 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
258 cb->channels[count++] = c->channel;
259 GRPC_CHANNEL_INTERNAL_REF(c->channel, "broadcast");
260 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700261}
262
Craig Tillera82950e2015-09-22 12:33:20 -0700263struct shutdown_cleanup_args {
Craig Tiller33825112015-09-18 07:44:19 -0700264 grpc_closure closure;
Craig Tillerff3ae682015-06-29 17:44:04 -0700265 gpr_slice slice;
266};
267
Craig Tillera82950e2015-09-22 12:33:20 -0700268static void shutdown_cleanup(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tillerf51457b2016-05-03 17:06:32 -0700269 grpc_error *error) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700270 struct shutdown_cleanup_args *a = arg;
Craig Tillera82950e2015-09-22 12:33:20 -0700271 gpr_slice_unref(a->slice);
272 gpr_free(a);
Craig Tillerff3ae682015-06-29 17:44:04 -0700273}
274
Craig Tillera82950e2015-09-22 12:33:20 -0700275static void send_shutdown(grpc_exec_ctx *exec_ctx, grpc_channel *channel,
Craig Tiller804ff712016-05-05 16:25:40 -0700276 int send_goaway, grpc_error *send_disconnect) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700277 grpc_transport_op op;
278 struct shutdown_cleanup_args *sc;
279 grpc_channel_element *elem;
280
Craig Tillera82950e2015-09-22 12:33:20 -0700281 memset(&op, 0, sizeof(op));
Craig Tillerff3ae682015-06-29 17:44:04 -0700282 op.send_goaway = send_goaway;
Craig Tillera82950e2015-09-22 12:33:20 -0700283 sc = gpr_malloc(sizeof(*sc));
284 sc->slice = gpr_slice_from_copied_string("Server shutdown");
Craig Tillerff3ae682015-06-29 17:44:04 -0700285 op.goaway_message = &sc->slice;
286 op.goaway_status = GRPC_STATUS_OK;
Craig Tiller804ff712016-05-05 16:25:40 -0700287 op.disconnect_with_error = send_disconnect;
Craig Tillera82950e2015-09-22 12:33:20 -0700288 grpc_closure_init(&sc->closure, shutdown_cleanup, sc);
Craig Tillerff3ae682015-06-29 17:44:04 -0700289 op.on_consumed = &sc->closure;
290
Craig Tillera82950e2015-09-22 12:33:20 -0700291 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
292 elem->filter->start_transport_op(exec_ctx, elem, &op);
Craig Tillerff3ae682015-06-29 17:44:04 -0700293}
294
Craig Tillera82950e2015-09-22 12:33:20 -0700295static void channel_broadcaster_shutdown(grpc_exec_ctx *exec_ctx,
296 channel_broadcaster *cb,
297 int send_goaway,
Craig Tiller804ff712016-05-05 16:25:40 -0700298 grpc_error *force_disconnect) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700299 size_t i;
300
Craig Tillera82950e2015-09-22 12:33:20 -0700301 for (i = 0; i < cb->num_channels; i++) {
302 send_shutdown(exec_ctx, cb->channels[i], send_goaway, force_disconnect);
303 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, cb->channels[i], "broadcast");
304 }
305 gpr_free(cb->channels);
Craig Tillerff3ae682015-06-29 17:44:04 -0700306}
307
Craig Tiller729b35a2015-07-13 12:36:47 -0700308/*
309 * request_matcher
310 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700311
Craig Tiller88512692016-04-04 09:32:52 -0700312static void request_matcher_init(request_matcher *rm, size_t entries,
313 grpc_server *server) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700314 memset(rm, 0, sizeof(*rm));
Craig Tiller88512692016-04-04 09:32:52 -0700315 rm->server = server;
Craig Tillerb9d35962015-09-11 13:31:16 -0700316 rm->requests = gpr_stack_lockfree_create(entries);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800317}
318
Craig Tillerb9d35962015-09-11 13:31:16 -0700319static void request_matcher_destroy(request_matcher *rm) {
320 GPR_ASSERT(gpr_stack_lockfree_pop(rm->requests) == -1);
321 gpr_stack_lockfree_destroy(rm->requests);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800322}
323
Craig Tillerf51457b2016-05-03 17:06:32 -0700324static void kill_zombie(grpc_exec_ctx *exec_ctx, void *elem,
325 grpc_error *error) {
Craig Tillera82950e2015-09-22 12:33:20 -0700326 grpc_call_destroy(grpc_call_from_top_element(elem));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800327}
328
Craig Tiller8dc09712015-09-24 13:58:16 -0700329static void request_matcher_zombify_all_pending_calls(grpc_exec_ctx *exec_ctx,
330 request_matcher *rm) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700331 while (rm->pending_head) {
332 call_data *calld = rm->pending_head;
333 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -0700334 gpr_mu_lock(&calld->mu_state);
335 calld->state = ZOMBIED;
336 gpr_mu_unlock(&calld->mu_state);
337 grpc_closure_init(
338 &calld->kill_zombie_closure, kill_zombie,
339 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tillerf51457b2016-05-03 17:06:32 -0700340 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
341 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700342 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800343}
344
Craig Tillera82950e2015-09-22 12:33:20 -0700345static void request_matcher_kill_requests(grpc_exec_ctx *exec_ctx,
346 grpc_server *server,
347 request_matcher *rm) {
Craig Tiller1191e212015-07-30 14:49:02 -0700348 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -0700349 while ((request_id = gpr_stack_lockfree_pop(rm->requests)) != -1) {
350 fail_call(exec_ctx, server, &server->requested_calls[request_id]);
351 }
Craig Tiller1191e212015-07-30 14:49:02 -0700352}
353
Craig Tiller729b35a2015-07-13 12:36:47 -0700354/*
355 * server proper
356 */
357
Craig Tillera82950e2015-09-22 12:33:20 -0700358static void server_ref(grpc_server *server) {
359 gpr_ref(&server->internal_refcount);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800360}
361
Craig Tillera82950e2015-09-22 12:33:20 -0700362static void server_delete(grpc_exec_ctx *exec_ctx, grpc_server *server) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800363 registered_method *rm;
Craig Tiller89504612015-04-27 11:48:46 -0700364 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700365 grpc_channel_args_destroy(server->channel_args);
366 gpr_mu_destroy(&server->mu_global);
367 gpr_mu_destroy(&server->mu_call);
Craig Tillera82950e2015-09-22 12:33:20 -0700368 while ((rm = server->registered_methods) != NULL) {
369 server->registered_methods = rm->next;
370 request_matcher_destroy(&rm->request_matcher);
371 gpr_free(rm->method);
372 gpr_free(rm->host);
373 gpr_free(rm);
374 }
375 for (i = 0; i < server->cq_count; i++) {
376 GRPC_CQ_INTERNAL_UNREF(server->cqs[i], "server");
377 }
378 request_matcher_destroy(&server->unregistered_request_matcher);
379 gpr_stack_lockfree_destroy(server->request_freelist);
380 gpr_free(server->cqs);
381 gpr_free(server->pollsets);
382 gpr_free(server->shutdown_tags);
383 gpr_free(server->requested_calls);
384 gpr_free(server);
Craig Tilleree945e82015-05-26 16:15:34 -0700385}
386
Craig Tillera82950e2015-09-22 12:33:20 -0700387static void server_unref(grpc_exec_ctx *exec_ctx, grpc_server *server) {
388 if (gpr_unref(&server->internal_refcount)) {
389 server_delete(exec_ctx, server);
390 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800391}
392
Craig Tillera82950e2015-09-22 12:33:20 -0700393static int is_channel_orphaned(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800394 return chand->next == chand;
395}
396
Craig Tillera82950e2015-09-22 12:33:20 -0700397static void orphan_channel(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800398 chand->next->prev = chand->prev;
399 chand->prev->next = chand->next;
400 chand->next = chand->prev = chand;
401}
402
Craig Tillera82950e2015-09-22 12:33:20 -0700403static void finish_destroy_channel(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700404 grpc_error *error) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800405 channel_data *chand = cd;
406 grpc_server *server = chand->server;
Craig Tillera82950e2015-09-22 12:33:20 -0700407 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "server");
408 server_unref(exec_ctx, server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800409}
410
Craig Tillera82950e2015-09-22 12:33:20 -0700411static void destroy_channel(grpc_exec_ctx *exec_ctx, channel_data *chand) {
412 if (is_channel_orphaned(chand)) return;
413 GPR_ASSERT(chand->server != NULL);
414 orphan_channel(chand);
415 server_ref(chand->server);
416 maybe_finish_shutdown(exec_ctx, chand->server);
David Garcia Quintas284488b2015-05-28 16:27:39 -0700417 chand->finish_destroy_channel_closure.cb = finish_destroy_channel;
418 chand->finish_destroy_channel_closure.cb_arg = chand;
Craig Tillerd7f12e32016-03-03 10:08:31 -0800419
420 grpc_transport_op op;
421 memset(&op, 0, sizeof(op));
422 op.set_accept_stream = true;
423 op.on_consumed = &chand->finish_destroy_channel_closure;
424 grpc_channel_next_op(exec_ctx,
425 grpc_channel_stack_element(
426 grpc_channel_get_channel_stack(chand->channel), 0),
427 &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800428}
429
Craig Tiller06cb1a92016-04-04 08:10:47 -0700430static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) {
431 gpr_slice slice = value->slice;
432 size_t len = GPR_SLICE_LENGTH(slice);
Craig Tiller6a006ce2015-07-13 16:25:40 -0700433
Craig Tiller06cb1a92016-04-04 08:10:47 -0700434 if (len + 1 > *capacity) {
435 *capacity = GPR_MAX(len + 1, *capacity * 2);
436 *dest = gpr_realloc(*dest, *capacity);
437 }
438 memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1);
439}
440
Craig Tiller88512692016-04-04 09:32:52 -0700441static void done_request_event(grpc_exec_ctx *exec_ctx, void *req,
442 grpc_cq_completion *c) {
443 requested_call *rc = req;
444 grpc_server *server = rc->server;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700445
Craig Tiller88512692016-04-04 09:32:52 -0700446 if (rc >= server->requested_calls &&
447 rc < server->requested_calls + server->max_requested_calls) {
448 GPR_ASSERT(rc - server->requested_calls <= INT_MAX);
449 gpr_stack_lockfree_push(server->request_freelist,
450 (int)(rc - server->requested_calls));
451 } else {
452 gpr_free(req);
453 }
Craig Tiller06cb1a92016-04-04 08:10:47 -0700454
Craig Tiller88512692016-04-04 09:32:52 -0700455 server_unref(exec_ctx, server);
456}
Craig Tiller06cb1a92016-04-04 08:10:47 -0700457
Craig Tiller88512692016-04-04 09:32:52 -0700458static void publish_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
459 call_data *calld, requested_call *rc) {
Craig Tiller06cb1a92016-04-04 08:10:47 -0700460 grpc_call_set_completion_queue(exec_ctx, calld->call, rc->cq_bound_to_call);
Craig Tiller88512692016-04-04 09:32:52 -0700461 grpc_call *call = calld->call;
462 *rc->call = call;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700463 calld->cq_new = rc->cq_for_notification;
464 GPR_SWAP(grpc_metadata_array, *rc->initial_metadata, calld->initial_metadata);
465 switch (rc->type) {
466 case BATCH_CALL:
467 GPR_ASSERT(calld->host != NULL);
468 GPR_ASSERT(calld->path != NULL);
469 cpstr(&rc->data.batch.details->host,
470 &rc->data.batch.details->host_capacity, calld->host);
471 cpstr(&rc->data.batch.details->method,
472 &rc->data.batch.details->method_capacity, calld->path);
473 rc->data.batch.details->deadline = calld->deadline;
474 rc->data.batch.details->flags =
475 0 | (calld->recv_idempotent_request
476 ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
477 : 0);
478 break;
479 case REGISTERED_CALL:
480 *rc->data.registered.deadline = calld->deadline;
481 if (rc->data.registered.optional_payload) {
Craig Tiller88512692016-04-04 09:32:52 -0700482 *rc->data.registered.optional_payload = calld->payload;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700483 }
484 break;
485 default:
486 GPR_UNREACHABLE_CODE(return );
487 }
488
Craig Tiller88512692016-04-04 09:32:52 -0700489 grpc_call_element *elem =
490 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
491 channel_data *chand = elem->channel_data;
492 server_ref(chand->server);
Craig Tillerf51457b2016-05-03 17:06:32 -0700493 grpc_cq_end_op(exec_ctx, calld->cq_new, rc->tag, GRPC_ERROR_NONE,
494 done_request_event, rc, &rc->completion);
Craig Tiller06cb1a92016-04-04 08:10:47 -0700495}
496
Craig Tillerf51457b2016-05-03 17:06:32 -0700497static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *arg,
498 grpc_error *error) {
Craig Tiller88512692016-04-04 09:32:52 -0700499 call_data *calld = arg;
500 request_matcher *rm = calld->request_matcher;
501 grpc_server *server = rm->server;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700502
Craig Tillerf51457b2016-05-03 17:06:32 -0700503 if (error != GRPC_ERROR_NONE || gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tillera82950e2015-09-22 12:33:20 -0700504 gpr_mu_lock(&calld->mu_state);
505 calld->state = ZOMBIED;
506 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -0700507 grpc_closure_init(
508 &calld->kill_zombie_closure, kill_zombie,
509 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tillerf51457b2016-05-03 17:06:32 -0700510 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, error, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700511 return;
512 }
Craig Tiller45724b32015-09-22 10:42:19 -0700513
Craig Tiller88512692016-04-04 09:32:52 -0700514 int request_id = gpr_stack_lockfree_pop(rm->requests);
Craig Tillera82950e2015-09-22 12:33:20 -0700515 if (request_id == -1) {
516 gpr_mu_lock(&server->mu_call);
517 gpr_mu_lock(&calld->mu_state);
518 calld->state = PENDING;
519 gpr_mu_unlock(&calld->mu_state);
Craig Tillerb9d35962015-09-11 13:31:16 -0700520 if (rm->pending_head == NULL) {
521 rm->pending_tail = rm->pending_head = calld;
Craig Tillera82950e2015-09-22 12:33:20 -0700522 } else {
Craig Tillerb9d35962015-09-11 13:31:16 -0700523 rm->pending_tail->pending_next = calld;
524 rm->pending_tail = calld;
Craig Tiller45724b32015-09-22 10:42:19 -0700525 }
Craig Tillera82950e2015-09-22 12:33:20 -0700526 calld->pending_next = NULL;
527 gpr_mu_unlock(&server->mu_call);
528 } else {
529 gpr_mu_lock(&calld->mu_state);
530 calld->state = ACTIVATED;
531 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -0700532 publish_call(exec_ctx, server, calld, &server->requested_calls[request_id]);
533 }
534}
535
536static void finish_start_new_rpc(
537 grpc_exec_ctx *exec_ctx, grpc_server *server, grpc_call_element *elem,
538 request_matcher *rm,
539 grpc_server_register_method_payload_handling payload_handling) {
540 call_data *calld = elem->call_data;
541
542 if (gpr_atm_acq_load(&server->shutdown_flag)) {
543 gpr_mu_lock(&calld->mu_state);
544 calld->state = ZOMBIED;
545 gpr_mu_unlock(&calld->mu_state);
546 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
Craig Tillerf51457b2016-05-03 17:06:32 -0700547 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
548 NULL);
Craig Tiller88512692016-04-04 09:32:52 -0700549 return;
550 }
551
552 calld->request_matcher = rm;
553
554 switch (payload_handling) {
555 case GRPC_SRM_PAYLOAD_NONE:
Craig Tillerf51457b2016-05-03 17:06:32 -0700556 publish_new_rpc(exec_ctx, calld, GRPC_ERROR_NONE);
Craig Tiller88512692016-04-04 09:32:52 -0700557 break;
558 case GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER: {
559 grpc_op op;
560 memset(&op, 0, sizeof(op));
561 op.op = GRPC_OP_RECV_MESSAGE;
562 op.data.recv_message = &calld->payload;
563 grpc_closure_init(&calld->publish, publish_new_rpc, calld);
564 grpc_call_start_batch_and_execute(exec_ctx, calld->call, &op, 1,
565 &calld->publish);
566 break;
567 }
Craig Tillera82950e2015-09-22 12:33:20 -0700568 }
Craig Tiller04cc8be2015-02-10 16:11:22 -0800569}
570
Craig Tillera82950e2015-09-22 12:33:20 -0700571static void start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800572 channel_data *chand = elem->channel_data;
573 call_data *calld = elem->call_data;
574 grpc_server *server = chand->server;
Craig Tiller7536af02015-12-22 13:49:30 -0800575 uint32_t i;
576 uint32_t hash;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800577 channel_registered_method *rm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800578
Craig Tillera82950e2015-09-22 12:33:20 -0700579 if (chand->registered_methods && calld->path && calld->host) {
580 /* TODO(ctiller): unify these two searches */
581 /* check for an exact match with host */
582 hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash);
583 for (i = 0; i <= chand->registered_method_max_probes; i++) {
584 rm = &chand->registered_methods[(hash + i) %
585 chand->registered_method_slots];
586 if (!rm) break;
587 if (rm->host != calld->host) continue;
588 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800589 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
590 !calld->recv_idempotent_request)
591 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700592 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700593 &rm->server_registered_method->request_matcher,
594 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700595 return;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800596 }
Craig Tillera82950e2015-09-22 12:33:20 -0700597 /* check for a wildcard method definition (no host set) */
598 hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash);
599 for (i = 0; i <= chand->registered_method_max_probes; i++) {
600 rm = &chand->registered_methods[(hash + i) %
601 chand->registered_method_slots];
602 if (!rm) break;
603 if (rm->host != NULL) continue;
604 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800605 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
606 !calld->recv_idempotent_request)
607 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700608 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700609 &rm->server_registered_method->request_matcher,
610 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700611 return;
612 }
613 }
614 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700615 &server->unregistered_request_matcher,
616 GRPC_SRM_PAYLOAD_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800617}
618
Craig Tillera82950e2015-09-22 12:33:20 -0700619static int num_listeners(grpc_server *server) {
Craig Tilleree945e82015-05-26 16:15:34 -0700620 listener *l;
621 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700622 for (l = server->listeners; l; l = l->next) {
623 n++;
624 }
Craig Tilleree945e82015-05-26 16:15:34 -0700625 return n;
626}
627
Craig Tillera82950e2015-09-22 12:33:20 -0700628static void done_shutdown_event(grpc_exec_ctx *exec_ctx, void *server,
629 grpc_cq_completion *completion) {
630 server_unref(exec_ctx, server);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700631}
632
Craig Tillera82950e2015-09-22 12:33:20 -0700633static int num_channels(grpc_server *server) {
Craig Tillerab54f792015-07-08 08:34:20 -0700634 channel_data *chand;
635 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700636 for (chand = server->root_channel_data.next;
637 chand != &server->root_channel_data; chand = chand->next) {
638 n++;
639 }
Craig Tillerab54f792015-07-08 08:34:20 -0700640 return n;
641}
642
Craig Tillera82950e2015-09-22 12:33:20 -0700643static void kill_pending_work_locked(grpc_exec_ctx *exec_ctx,
644 grpc_server *server) {
Craig Tiller1191e212015-07-30 14:49:02 -0700645 registered_method *rm;
Craig Tillera82950e2015-09-22 12:33:20 -0700646 request_matcher_kill_requests(exec_ctx, server,
647 &server->unregistered_request_matcher);
648 request_matcher_zombify_all_pending_calls(
649 exec_ctx, &server->unregistered_request_matcher);
650 for (rm = server->registered_methods; rm; rm = rm->next) {
651 request_matcher_kill_requests(exec_ctx, server, &rm->request_matcher);
652 request_matcher_zombify_all_pending_calls(exec_ctx, &rm->request_matcher);
653 }
Craig Tillerdc627722015-05-26 15:27:02 -0700654}
655
Craig Tillera82950e2015-09-22 12:33:20 -0700656static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx,
657 grpc_server *server) {
Craig Tiller45724b32015-09-22 10:42:19 -0700658 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700659 if (!gpr_atm_acq_load(&server->shutdown_flag) || server->shutdown_published) {
660 return;
661 }
Craig Tiller45724b32015-09-22 10:42:19 -0700662
Craig Tillera82950e2015-09-22 12:33:20 -0700663 kill_pending_work_locked(exec_ctx, server);
Craig Tiller45724b32015-09-22 10:42:19 -0700664
Craig Tillera82950e2015-09-22 12:33:20 -0700665 if (server->root_channel_data.next != &server->root_channel_data ||
666 server->listeners_destroyed < num_listeners(server)) {
667 if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME),
668 server->last_shutdown_message_time),
669 gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
670 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
671 gpr_log(GPR_DEBUG,
672 "Waiting for %d channels and %d/%d listeners to be destroyed"
673 " before shutting down server",
674 num_channels(server),
675 num_listeners(server) - server->listeners_destroyed,
676 num_listeners(server));
Craig Tiller45724b32015-09-22 10:42:19 -0700677 }
Craig Tillera82950e2015-09-22 12:33:20 -0700678 return;
679 }
Craig Tiller45724b32015-09-22 10:42:19 -0700680 server->shutdown_published = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700681 for (i = 0; i < server->num_shutdown_tags; i++) {
682 server_ref(server);
683 grpc_cq_end_op(exec_ctx, server->shutdown_tags[i].cq,
Craig Tillerf51457b2016-05-03 17:06:32 -0700684 server->shutdown_tags[i].tag, GRPC_ERROR_NONE,
685 done_shutdown_event, server,
Craig Tillera82950e2015-09-22 12:33:20 -0700686 &server->shutdown_tags[i].completion);
687 }
Craig Tiller45724b32015-09-22 10:42:19 -0700688}
689
Craig Tillera82950e2015-09-22 12:33:20 -0700690static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700691 grpc_call_element *elem = user_data;
Craig Tillercce17ac2015-01-20 09:29:28 -0800692 call_data *calld = elem->call_data;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800693 if (md->key == GRPC_MDSTR_PATH) {
Craig Tiller9cac2a12016-04-04 20:56:56 -0700694 if (calld->path == NULL) {
695 calld->path = GRPC_MDSTR_REF(md->value);
696 }
Craig Tillera82950e2015-09-22 12:33:20 -0700697 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800698 } else if (md->key == GRPC_MDSTR_AUTHORITY) {
Craig Tiller9cac2a12016-04-04 20:56:56 -0700699 if (calld->host == NULL) {
700 calld->host = GRPC_MDSTR_REF(md->value);
701 }
Craig Tillera82950e2015-09-22 12:33:20 -0700702 return NULL;
703 }
Craig Tiller6902ad22015-04-16 08:01:49 -0700704 return md;
705}
706
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800707static void server_on_recv_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700708 grpc_error *error) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700709 grpc_call_element *elem = ptr;
Craig Tiller6902ad22015-04-16 08:01:49 -0700710 call_data *calld = elem->call_data;
Craig Tiller94329d02015-07-23 09:52:11 -0700711 gpr_timespec op_deadline;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700712
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800713 grpc_metadata_batch_filter(calld->recv_initial_metadata, server_filter, elem);
714 op_deadline = calld->recv_initial_metadata->deadline;
715 if (0 != gpr_time_cmp(op_deadline, gpr_inf_future(op_deadline.clock_type))) {
716 calld->deadline = op_deadline;
717 }
718 if (calld->host && calld->path) {
719 /* do nothing */
720 } else {
Craig Tillerf51457b2016-05-03 17:06:32 -0700721 error =
722 GRPC_ERROR_CREATE_REFERENCING("Missing :authority or :path", &error, 1);
Craig Tillera82950e2015-09-22 12:33:20 -0700723 }
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700724
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800725 calld->on_done_recv_initial_metadata->cb(
Craig Tillerf51457b2016-05-03 17:06:32 -0700726 exec_ctx, calld->on_done_recv_initial_metadata->cb_arg, error);
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700727}
728
Craig Tillera82950e2015-09-22 12:33:20 -0700729static void server_mutate_op(grpc_call_element *elem,
730 grpc_transport_stream_op *op) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700731 call_data *calld = elem->call_data;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700732
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800733 if (op->recv_initial_metadata != NULL) {
Craig Tiller4d40ba32016-03-09 17:48:40 -0800734 GPR_ASSERT(op->recv_idempotent_request == NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800735 calld->recv_initial_metadata = op->recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800736 calld->on_done_recv_initial_metadata = op->recv_initial_metadata_ready;
737 op->recv_initial_metadata_ready = &calld->server_on_recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800738 op->recv_idempotent_request = &calld->recv_idempotent_request;
Craig Tillera82950e2015-09-22 12:33:20 -0700739 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700740}
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700741
Craig Tillera82950e2015-09-22 12:33:20 -0700742static void server_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
743 grpc_call_element *elem,
744 grpc_transport_stream_op *op) {
745 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
746 server_mutate_op(elem, op);
747 grpc_call_next_op(exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800748}
749
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800750static void got_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700751 grpc_error *error) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800752 grpc_call_element *elem = ptr;
753 call_data *calld = elem->call_data;
Craig Tillerf51457b2016-05-03 17:06:32 -0700754 if (error == GRPC_ERROR_NONE) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800755 start_new_rpc(exec_ctx, elem);
756 } else {
757 gpr_mu_lock(&calld->mu_state);
758 if (calld->state == NOT_STARTED) {
759 calld->state = ZOMBIED;
760 gpr_mu_unlock(&calld->mu_state);
761 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
Craig Tillerf51457b2016-05-03 17:06:32 -0700762 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
763 NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800764 } else if (calld->state == PENDING) {
765 calld->state = ZOMBIED;
766 gpr_mu_unlock(&calld->mu_state);
767 /* zombied call will be destroyed when it's removed from the pending
768 queue... later */
769 } else {
770 gpr_mu_unlock(&calld->mu_state);
771 }
772 }
773}
774
775static void accept_stream(grpc_exec_ctx *exec_ctx, void *cd,
776 grpc_transport *transport,
Craig Tillera82950e2015-09-22 12:33:20 -0700777 const void *transport_server_data) {
Craig Tillere039f032015-06-25 12:54:23 -0700778 channel_data *chand = cd;
779 /* create a call */
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800780 grpc_call *call =
781 grpc_call_create(chand->channel, NULL, 0, NULL, transport_server_data,
782 NULL, 0, gpr_inf_future(GPR_CLOCK_MONOTONIC));
783 grpc_call_element *elem =
784 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
785 call_data *calld = elem->call_data;
786 grpc_op op;
787 memset(&op, 0, sizeof(op));
788 op.op = GRPC_OP_RECV_INITIAL_METADATA;
789 op.data.recv_initial_metadata = &calld->initial_metadata;
790 grpc_closure_init(&calld->got_initial_metadata, got_initial_metadata, elem);
791 grpc_call_start_batch_and_execute(exec_ctx, call, &op, 1,
792 &calld->got_initial_metadata);
Craig Tillere039f032015-06-25 12:54:23 -0700793}
794
Craig Tillera82950e2015-09-22 12:33:20 -0700795static void channel_connectivity_changed(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700796 grpc_error *error) {
Craig Tillere039f032015-06-25 12:54:23 -0700797 channel_data *chand = cd;
798 grpc_server *server = chand->server;
Craig Tillera82950e2015-09-22 12:33:20 -0700799 if (chand->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE) {
800 grpc_transport_op op;
801 memset(&op, 0, sizeof(op));
802 op.on_connectivity_state_change = &chand->channel_connectivity_changed,
803 op.connectivity_state = &chand->connectivity_state;
804 grpc_channel_next_op(exec_ctx,
805 grpc_channel_stack_element(
806 grpc_channel_get_channel_stack(chand->channel), 0),
807 &op);
808 } else {
809 gpr_mu_lock(&server->mu_global);
810 destroy_channel(exec_ctx, chand);
811 gpr_mu_unlock(&server->mu_global);
812 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "connectivity");
813 }
Craig Tillere039f032015-06-25 12:54:23 -0700814}
815
Craig Tillera82950e2015-09-22 12:33:20 -0700816static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800817 grpc_call_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800818 call_data *calld = elem->call_data;
819 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700820 memset(calld, 0, sizeof(call_data));
821 calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
822 calld->call = grpc_call_from_top_element(elem);
823 gpr_mu_init(&calld->mu_state);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800824
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800825 grpc_closure_init(&calld->server_on_recv_initial_metadata,
826 server_on_recv_initial_metadata, elem);
Craig Tiller1e6facb2015-06-11 22:47:11 -0700827
Craig Tillera82950e2015-09-22 12:33:20 -0700828 server_ref(chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800829}
830
Craig Tiller2c8063c2016-03-22 22:12:15 -0700831static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
832 void *ignored) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800833 channel_data *chand = elem->channel_data;
Craig Tillerdb7db992015-01-29 11:19:01 -0800834 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800835
Craig Tillera82950e2015-09-22 12:33:20 -0700836 GPR_ASSERT(calld->state != PENDING);
Craig Tiller092d8d12015-07-04 22:35:00 -0700837
Craig Tillera82950e2015-09-22 12:33:20 -0700838 if (calld->host) {
839 GRPC_MDSTR_UNREF(calld->host);
840 }
841 if (calld->path) {
842 GRPC_MDSTR_UNREF(calld->path);
843 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800844 grpc_metadata_array_destroy(&calld->initial_metadata);
Craig Tiller4df31a62015-01-30 09:44:31 -0800845
Craig Tillera82950e2015-09-22 12:33:20 -0700846 gpr_mu_destroy(&calld->mu_state);
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700847
Craig Tillera82950e2015-09-22 12:33:20 -0700848 server_unref(exec_ctx, chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800849}
850
Craig Tillera82950e2015-09-22 12:33:20 -0700851static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800852 grpc_channel_element *elem,
853 grpc_channel_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800854 channel_data *chand = elem->channel_data;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800855 GPR_ASSERT(args->is_first);
856 GPR_ASSERT(!args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800857 chand->server = NULL;
858 chand->channel = NULL;
859 chand->next = chand->prev = chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800860 chand->registered_methods = NULL;
Craig Tillere039f032015-06-25 12:54:23 -0700861 chand->connectivity_state = GRPC_CHANNEL_IDLE;
Craig Tillera82950e2015-09-22 12:33:20 -0700862 grpc_closure_init(&chand->channel_connectivity_changed,
863 channel_connectivity_changed, chand);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800864}
865
Craig Tillera82950e2015-09-22 12:33:20 -0700866static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
867 grpc_channel_element *elem) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800868 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800869 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700870 if (chand->registered_methods) {
871 for (i = 0; i < chand->registered_method_slots; i++) {
872 if (chand->registered_methods[i].method) {
873 GRPC_MDSTR_UNREF(chand->registered_methods[i].method);
874 }
875 if (chand->registered_methods[i].host) {
876 GRPC_MDSTR_UNREF(chand->registered_methods[i].host);
877 }
Craig Tillerec3257c2015-02-12 15:59:43 -0800878 }
Craig Tillera82950e2015-09-22 12:33:20 -0700879 gpr_free(chand->registered_methods);
880 }
881 if (chand->server) {
882 gpr_mu_lock(&chand->server->mu_global);
883 chand->next->prev = chand->prev;
884 chand->prev->next = chand->next;
885 chand->next = chand->prev = chand;
886 maybe_finish_shutdown(exec_ctx, chand->server);
887 gpr_mu_unlock(&chand->server->mu_global);
Craig Tillera82950e2015-09-22 12:33:20 -0700888 server_unref(exec_ctx, chand->server);
889 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800890}
891
Craig Tiller178edfa2016-02-17 20:54:46 -0800892const grpc_channel_filter grpc_server_top_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700893 server_start_transport_stream_op,
894 grpc_channel_next_op,
895 sizeof(call_data),
896 init_call_elem,
897 grpc_call_stack_ignore_set_pollset,
898 destroy_call_elem,
899 sizeof(channel_data),
900 init_channel_elem,
901 destroy_channel_elem,
902 grpc_call_next_get_peer,
903 "server",
Craig Tiller9f28ac22015-01-27 17:01:29 -0800904};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800905
Craig Tillera82950e2015-09-22 12:33:20 -0700906void grpc_server_register_completion_queue(grpc_server *server,
907 grpc_completion_queue *cq,
908 void *reserved) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800909 size_t i, n;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700910 GRPC_API_TRACE(
911 "grpc_server_register_completion_queue(server=%p, cq=%p, reserved=%p)", 3,
912 (server, cq, reserved));
Craig Tillera82950e2015-09-22 12:33:20 -0700913 GPR_ASSERT(!reserved);
914 for (i = 0; i < server->cq_count; i++) {
915 if (server->cqs[i] == cq) return;
916 }
917 GRPC_CQ_INTERNAL_REF(cq, "server");
918 grpc_cq_mark_server_cq(cq);
Craig Tiller20bc56d2015-02-12 09:02:56 -0800919 n = server->cq_count++;
Craig Tillera82950e2015-09-22 12:33:20 -0700920 server->cqs = gpr_realloc(server->cqs,
921 server->cq_count * sizeof(grpc_completion_queue *));
Craig Tiller20bc56d2015-02-12 09:02:56 -0800922 server->cqs[n] = cq;
923}
924
Craig Tiller178edfa2016-02-17 20:54:46 -0800925grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800926 size_t i;
Craig Tiller178edfa2016-02-17 20:54:46 -0800927
928 GRPC_API_TRACE("grpc_server_create(%p, %p)", 2, (args, reserved));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800929
Craig Tillera82950e2015-09-22 12:33:20 -0700930 grpc_server *server = gpr_malloc(sizeof(grpc_server));
Craig Tiller60fd3612015-03-05 16:24:22 -0800931
Craig Tillera82950e2015-09-22 12:33:20 -0700932 GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
Craig Tiller60fd3612015-03-05 16:24:22 -0800933
Craig Tillera82950e2015-09-22 12:33:20 -0700934 memset(server, 0, sizeof(grpc_server));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800935
Craig Tillera82950e2015-09-22 12:33:20 -0700936 gpr_mu_init(&server->mu_global);
937 gpr_mu_init(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800938
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800939 /* decremented by grpc_server_destroy */
Craig Tillera82950e2015-09-22 12:33:20 -0700940 gpr_ref_init(&server->internal_refcount, 1);
941 server->root_channel_data.next = server->root_channel_data.prev =
942 &server->root_channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800943
Craig Tiller6a006ce2015-07-13 16:25:40 -0700944 /* TODO(ctiller): expose a channel_arg for this */
945 server->max_requested_calls = 32768;
Craig Tillera82950e2015-09-22 12:33:20 -0700946 server->request_freelist =
947 gpr_stack_lockfree_create(server->max_requested_calls);
948 for (i = 0; i < (size_t)server->max_requested_calls; i++) {
949 gpr_stack_lockfree_push(server->request_freelist, (int)i);
950 }
951 request_matcher_init(&server->unregistered_request_matcher,
Craig Tiller88512692016-04-04 09:32:52 -0700952 server->max_requested_calls, server);
Craig Tillera82950e2015-09-22 12:33:20 -0700953 server->requested_calls = gpr_malloc(server->max_requested_calls *
954 sizeof(*server->requested_calls));
Craig Tiller729b35a2015-07-13 12:36:47 -0700955
Craig Tillera82950e2015-09-22 12:33:20 -0700956 server->channel_args = grpc_channel_args_copy(args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800957
958 return server;
959}
960
Craig Tillera82950e2015-09-22 12:33:20 -0700961static int streq(const char *a, const char *b) {
962 if (a == NULL && b == NULL) return 1;
963 if (a == NULL) return 0;
964 if (b == NULL) return 0;
965 return 0 == strcmp(a, b);
Craig Tiller24be0f72015-02-10 14:04:22 -0800966}
967
Craig Tiller06cb1a92016-04-04 08:10:47 -0700968void *grpc_server_register_method(
969 grpc_server *server, const char *method, const char *host,
970 grpc_server_register_method_payload_handling payload_handling,
971 uint32_t flags) {
Craig Tiller24be0f72015-02-10 14:04:22 -0800972 registered_method *m;
Craig Tillerb2906862016-03-10 06:50:07 -0800973 GRPC_API_TRACE(
974 "grpc_server_register_method(server=%p, method=%s, host=%s, "
975 "flags=0x%08x)",
976 4, (server, method, host, flags));
Craig Tillera82950e2015-09-22 12:33:20 -0700977 if (!method) {
978 gpr_log(GPR_ERROR,
979 "grpc_server_register_method method string cannot be NULL");
980 return NULL;
981 }
982 for (m = server->registered_methods; m; m = m->next) {
983 if (streq(m->method, method) && streq(m->host, host)) {
984 gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method,
985 host ? host : "*");
Craig Tiller24be0f72015-02-10 14:04:22 -0800986 return NULL;
987 }
Craig Tillera82950e2015-09-22 12:33:20 -0700988 }
Craig Tillerb2906862016-03-10 06:50:07 -0800989 if ((flags & ~GRPC_INITIAL_METADATA_USED_MASK) != 0) {
990 gpr_log(GPR_ERROR, "grpc_server_register_method invalid flags 0x%08x",
991 flags);
992 return NULL;
993 }
Craig Tillera82950e2015-09-22 12:33:20 -0700994 m = gpr_malloc(sizeof(registered_method));
995 memset(m, 0, sizeof(*m));
Craig Tiller88512692016-04-04 09:32:52 -0700996 request_matcher_init(&m->request_matcher, server->max_requested_calls,
997 server);
Craig Tillera82950e2015-09-22 12:33:20 -0700998 m->method = gpr_strdup(method);
999 m->host = gpr_strdup(host);
Craig Tiller24be0f72015-02-10 14:04:22 -08001000 m->next = server->registered_methods;
Craig Tiller5ada3d22016-04-04 08:30:59 -07001001 m->payload_handling = payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -08001002 m->flags = flags;
Craig Tiller24be0f72015-02-10 14:04:22 -08001003 server->registered_methods = m;
1004 return m;
1005}
1006
Craig Tillera82950e2015-09-22 12:33:20 -07001007void grpc_server_start(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001008 listener *l;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001009 size_t i;
Craig Tillerf5768a62015-09-22 10:54:34 -07001010 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001011
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001012 GRPC_API_TRACE("grpc_server_start(server=%p)", 1, (server));
1013
Craig Tillera82950e2015-09-22 12:33:20 -07001014 server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count);
1015 for (i = 0; i < server->cq_count; i++) {
1016 server->pollsets[i] = grpc_cq_pollset(server->cqs[i]);
1017 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001018
Craig Tillera82950e2015-09-22 12:33:20 -07001019 for (l = server->listeners; l; l = l->next) {
1020 l->start(&exec_ctx, server, l->arg, server->pollsets, server->cq_count);
1021 }
Craig Tillerdfff1b82015-09-21 14:39:57 -07001022
Craig Tillera82950e2015-09-22 12:33:20 -07001023 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001024}
1025
Craig Tillera82950e2015-09-22 12:33:20 -07001026void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *s,
1027 grpc_transport *transport,
Craig Tillera82950e2015-09-22 12:33:20 -07001028 const grpc_channel_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001029 size_t i;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001030 size_t num_registered_methods;
1031 size_t alloc;
1032 registered_method *rm;
1033 channel_registered_method *crm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001034 grpc_channel *channel;
1035 channel_data *chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001036 grpc_mdstr *host;
1037 grpc_mdstr *method;
Craig Tiller7536af02015-12-22 13:49:30 -08001038 uint32_t hash;
Craig Tillerf96dfc32015-09-10 14:43:18 -07001039 size_t slots;
Craig Tiller7536af02015-12-22 13:49:30 -08001040 uint32_t probes;
1041 uint32_t max_probes = 0;
Craig Tillere039f032015-06-25 12:54:23 -07001042 grpc_transport_op op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001043
Craig Tillera82950e2015-09-22 12:33:20 -07001044 for (i = 0; i < s->cq_count; i++) {
1045 memset(&op, 0, sizeof(op));
1046 op.bind_pollset = grpc_cq_pollset(s->cqs[i]);
1047 grpc_transport_perform_op(exec_ctx, transport, &op);
1048 }
ctillerd79b4862014-12-17 16:36:59 -08001049
Craig Tiller178edfa2016-02-17 20:54:46 -08001050 channel =
1051 grpc_channel_create(exec_ctx, NULL, args, GRPC_SERVER_CHANNEL, transport);
Craig Tillera82950e2015-09-22 12:33:20 -07001052 chand = (channel_data *)grpc_channel_stack_element(
Craig Tillerf40df232016-03-25 13:38:14 -07001053 grpc_channel_get_channel_stack(channel), 0)
1054 ->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001055 chand->server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001056 server_ref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001057 chand->channel = channel;
1058
Craig Tiller04cc8be2015-02-10 16:11:22 -08001059 num_registered_methods = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001060 for (rm = s->registered_methods; rm; rm = rm->next) {
1061 num_registered_methods++;
1062 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001063 /* build a lookup table phrased in terms of mdstr's in this channels context
1064 to quickly find registered methods */
Craig Tillera82950e2015-09-22 12:33:20 -07001065 if (num_registered_methods > 0) {
1066 slots = 2 * num_registered_methods;
1067 alloc = sizeof(channel_registered_method) * slots;
1068 chand->registered_methods = gpr_malloc(alloc);
1069 memset(chand->registered_methods, 0, alloc);
1070 for (rm = s->registered_methods; rm; rm = rm->next) {
Craig Tillerb2b42612015-11-20 12:02:17 -08001071 host = rm->host ? grpc_mdstr_from_string(rm->host) : NULL;
1072 method = grpc_mdstr_from_string(rm->method);
Craig Tillera82950e2015-09-22 12:33:20 -07001073 hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash);
1074 for (probes = 0; chand->registered_methods[(hash + probes) % slots]
Craig Tillerf40df232016-03-25 13:38:14 -07001075 .server_registered_method != NULL;
Craig Tillera82950e2015-09-22 12:33:20 -07001076 probes++)
1077 ;
1078 if (probes > max_probes) max_probes = probes;
1079 crm = &chand->registered_methods[(hash + probes) % slots];
1080 crm->server_registered_method = rm;
Craig Tillerb2906862016-03-10 06:50:07 -08001081 crm->flags = rm->flags;
Craig Tillera82950e2015-09-22 12:33:20 -07001082 crm->host = host;
1083 crm->method = method;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001084 }
Craig Tiller7536af02015-12-22 13:49:30 -08001085 GPR_ASSERT(slots <= UINT32_MAX);
1086 chand->registered_method_slots = (uint32_t)slots;
Craig Tillera82950e2015-09-22 12:33:20 -07001087 chand->registered_method_max_probes = max_probes;
1088 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001089
Craig Tillera82950e2015-09-22 12:33:20 -07001090 gpr_mu_lock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001091 chand->next = &s->root_channel_data;
1092 chand->prev = chand->next->prev;
1093 chand->next->prev = chand->prev->next = chand;
Craig Tillera82950e2015-09-22 12:33:20 -07001094 gpr_mu_unlock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001095
Craig Tillera82950e2015-09-22 12:33:20 -07001096 GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity");
1097 memset(&op, 0, sizeof(op));
Craig Tillerd7f12e32016-03-03 10:08:31 -08001098 op.set_accept_stream = true;
1099 op.set_accept_stream_fn = accept_stream;
Craig Tiller4b804102015-06-26 16:16:12 -07001100 op.set_accept_stream_user_data = chand;
1101 op.on_connectivity_state_change = &chand->channel_connectivity_changed;
1102 op.connectivity_state = &chand->connectivity_state;
Craig Tiller804ff712016-05-05 16:25:40 -07001103 if (gpr_atm_acq_load(&s->shutdown_flag) != 0) {
1104 op.disconnect_with_error = GRPC_ERROR_CREATE("Server shutdown");
1105 }
Craig Tillera82950e2015-09-22 12:33:20 -07001106 grpc_transport_perform_op(exec_ctx, transport, &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001107}
1108
Craig Tillera82950e2015-09-22 12:33:20 -07001109void done_published_shutdown(grpc_exec_ctx *exec_ctx, void *done_arg,
1110 grpc_cq_completion *storage) {
1111 (void)done_arg;
1112 gpr_free(storage);
murgatroid9900a3dab2015-08-19 11:15:38 -07001113}
1114
Craig Tillera82950e2015-09-22 12:33:20 -07001115static void listener_destroy_done(grpc_exec_ctx *exec_ctx, void *s,
Craig Tillerf51457b2016-05-03 17:06:32 -07001116 grpc_error *error) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001117 grpc_server *server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001118 gpr_mu_lock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001119 server->listeners_destroyed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001120 maybe_finish_shutdown(exec_ctx, server);
1121 gpr_mu_unlock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001122}
1123
Craig Tillera82950e2015-09-22 12:33:20 -07001124void grpc_server_shutdown_and_notify(grpc_server *server,
1125 grpc_completion_queue *cq, void *tag) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001126 listener *l;
Craig Tillerbce999f2015-05-27 09:55:51 -07001127 shutdown_tag *sdt;
Craig Tillerff3ae682015-06-29 17:44:04 -07001128 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001129 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001130
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001131 GRPC_API_TRACE("grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", 3,
1132 (server, cq, tag));
1133
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001134 /* lock, and gather up some stuff to do */
Craig Tillera82950e2015-09-22 12:33:20 -07001135 gpr_mu_lock(&server->mu_global);
Craig Tiller4bf29282015-12-14 11:25:48 -08001136 grpc_cq_begin_op(cq, tag);
Craig Tillera82950e2015-09-22 12:33:20 -07001137 if (server->shutdown_published) {
Craig Tillerf51457b2016-05-03 17:06:32 -07001138 grpc_cq_end_op(&exec_ctx, cq, tag, GRPC_ERROR_NONE, done_published_shutdown,
1139 NULL, gpr_malloc(sizeof(grpc_cq_completion)));
Craig Tillera82950e2015-09-22 12:33:20 -07001140 gpr_mu_unlock(&server->mu_global);
1141 goto done;
1142 }
1143 server->shutdown_tags =
1144 gpr_realloc(server->shutdown_tags,
1145 sizeof(shutdown_tag) * (server->num_shutdown_tags + 1));
Craig Tillerbce999f2015-05-27 09:55:51 -07001146 sdt = &server->shutdown_tags[server->num_shutdown_tags++];
1147 sdt->tag = tag;
1148 sdt->cq = cq;
Craig Tillera82950e2015-09-22 12:33:20 -07001149 if (gpr_atm_acq_load(&server->shutdown_flag)) {
1150 gpr_mu_unlock(&server->mu_global);
1151 goto done;
1152 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001153
Craig Tillera82950e2015-09-22 12:33:20 -07001154 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
Craig Tillerab54f792015-07-08 08:34:20 -07001155
Craig Tillera82950e2015-09-22 12:33:20 -07001156 channel_broadcaster_init(server, &broadcaster);
nnoble0c475f02014-12-05 15:37:39 -08001157
Craig Tillerfc193e12015-09-24 15:29:03 -07001158 gpr_atm_rel_store(&server->shutdown_flag, 1);
1159
Craig Tillerbd217572015-02-11 18:10:56 -08001160 /* collect all unregistered then registered calls */
Craig Tillera82950e2015-09-22 12:33:20 -07001161 gpr_mu_lock(&server->mu_call);
1162 kill_pending_work_locked(&exec_ctx, server);
1163 gpr_mu_unlock(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001164
Craig Tillera82950e2015-09-22 12:33:20 -07001165 maybe_finish_shutdown(&exec_ctx, server);
1166 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001167
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001168 /* Shutdown listeners */
Craig Tillera82950e2015-09-22 12:33:20 -07001169 for (l = server->listeners; l; l = l->next) {
1170 grpc_closure_init(&l->destroy_done, listener_destroy_done, server);
1171 l->destroy(&exec_ctx, server, l->arg, &l->destroy_done);
1172 }
Craig Tillerff3ae682015-06-29 17:44:04 -07001173
Craig Tillera82950e2015-09-22 12:33:20 -07001174 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 1, 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001175
Craig Tillerdfff1b82015-09-21 14:39:57 -07001176done:
Craig Tillera82950e2015-09-22 12:33:20 -07001177 grpc_exec_ctx_finish(&exec_ctx);
Craig Tilleraec96aa2015-04-07 14:32:15 -07001178}
1179
Craig Tillera82950e2015-09-22 12:33:20 -07001180void grpc_server_cancel_all_calls(grpc_server *server) {
Craig Tiller092d8d12015-07-04 22:35:00 -07001181 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001182 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerafa2d632015-05-26 16:39:13 -07001183
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001184 GRPC_API_TRACE("grpc_server_cancel_all_calls(server=%p)", 1, (server));
1185
Craig Tillera82950e2015-09-22 12:33:20 -07001186 gpr_mu_lock(&server->mu_global);
1187 channel_broadcaster_init(server, &broadcaster);
1188 gpr_mu_unlock(&server->mu_global);
Craig Tillerafa2d632015-05-26 16:39:13 -07001189
Craig Tiller804ff712016-05-05 16:25:40 -07001190 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 0,
1191 GRPC_ERROR_CREATE("Cancelling all calls"));
Craig Tillera82950e2015-09-22 12:33:20 -07001192 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerafa2d632015-05-26 16:39:13 -07001193}
1194
Craig Tillera82950e2015-09-22 12:33:20 -07001195void grpc_server_destroy(grpc_server *server) {
Craig Tilleraec96aa2015-04-07 14:32:15 -07001196 listener *l;
Craig Tillerf5768a62015-09-22 10:54:34 -07001197 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller872af022015-04-24 15:57:52 -07001198
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001199 GRPC_API_TRACE("grpc_server_destroy(server=%p)", 1, (server));
1200
Craig Tillera82950e2015-09-22 12:33:20 -07001201 gpr_mu_lock(&server->mu_global);
1202 GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners);
1203 GPR_ASSERT(server->listeners_destroyed == num_listeners(server));
Craig Tilleraec96aa2015-04-07 14:32:15 -07001204
Craig Tillera82950e2015-09-22 12:33:20 -07001205 while (server->listeners) {
1206 l = server->listeners;
1207 server->listeners = l->next;
1208 gpr_free(l);
1209 }
Craig Tilleraec96aa2015-04-07 14:32:15 -07001210
Craig Tillera82950e2015-09-22 12:33:20 -07001211 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001212
Craig Tillera82950e2015-09-22 12:33:20 -07001213 server_unref(&exec_ctx, server);
1214 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001215}
1216
Craig Tillera82950e2015-09-22 12:33:20 -07001217void grpc_server_add_listener(
1218 grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1219 void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1220 grpc_pollset **pollsets, size_t pollset_count),
1221 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1222 grpc_closure *on_done)) {
1223 listener *l = gpr_malloc(sizeof(listener));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001224 l->arg = arg;
1225 l->start = start;
1226 l->destroy = destroy;
1227 l->next = server->listeners;
1228 server->listeners = l;
1229}
1230
Craig Tillera82950e2015-09-22 12:33:20 -07001231static grpc_call_error queue_call_request(grpc_exec_ctx *exec_ctx,
1232 grpc_server *server,
1233 requested_call *rc) {
Yang Gaoeb8e7cd2015-02-11 11:43:40 -08001234 call_data *calld = NULL;
Craig Tillerb9d35962015-09-11 13:31:16 -07001235 request_matcher *rm = NULL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001236 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -07001237 if (gpr_atm_acq_load(&server->shutdown_flag)) {
1238 fail_call(exec_ctx, server, rc);
1239 return GRPC_CALL_OK;
1240 }
1241 request_id = gpr_stack_lockfree_pop(server->request_freelist);
1242 if (request_id == -1) {
1243 /* out of request ids: just fail this one */
1244 fail_call(exec_ctx, server, rc);
1245 return GRPC_CALL_OK;
1246 }
1247 switch (rc->type) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001248 case BATCH_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001249 rm = &server->unregistered_request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001250 break;
1251 case REGISTERED_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001252 rm = &rc->data.registered.registered_method->request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001253 break;
Craig Tillera82950e2015-09-22 12:33:20 -07001254 }
Craig Tiller45724b32015-09-22 10:42:19 -07001255 server->requested_calls[request_id] = *rc;
Craig Tillera82950e2015-09-22 12:33:20 -07001256 gpr_free(rc);
Craig Tillerb9d35962015-09-11 13:31:16 -07001257 if (gpr_stack_lockfree_push(rm->requests, request_id)) {
Craig Tillera82950e2015-09-22 12:33:20 -07001258 /* this was the first queued request: we need to lock and start
1259 matching calls */
1260 gpr_mu_lock(&server->mu_call);
Craig Tillerb9d35962015-09-11 13:31:16 -07001261 while ((calld = rm->pending_head) != NULL) {
1262 request_id = gpr_stack_lockfree_pop(rm->requests);
Craig Tillera82950e2015-09-22 12:33:20 -07001263 if (request_id == -1) break;
Craig Tillerb9d35962015-09-11 13:31:16 -07001264 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -07001265 gpr_mu_unlock(&server->mu_call);
1266 gpr_mu_lock(&calld->mu_state);
1267 if (calld->state == ZOMBIED) {
1268 gpr_mu_unlock(&calld->mu_state);
1269 grpc_closure_init(
1270 &calld->kill_zombie_closure, kill_zombie,
1271 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tillerf51457b2016-05-03 17:06:32 -07001272 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure,
1273 GRPC_ERROR_NONE, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001274 } else {
1275 GPR_ASSERT(calld->state == PENDING);
1276 calld->state = ACTIVATED;
1277 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -07001278 publish_call(exec_ctx, server, calld,
1279 &server->requested_calls[request_id]);
Craig Tillera82950e2015-09-22 12:33:20 -07001280 }
1281 gpr_mu_lock(&server->mu_call);
Craig Tiller45724b32015-09-22 10:42:19 -07001282 }
Craig Tillera82950e2015-09-22 12:33:20 -07001283 gpr_mu_unlock(&server->mu_call);
1284 }
Craig Tiller6a006ce2015-07-13 16:25:40 -07001285 return GRPC_CALL_OK;
Craig Tillercce17ac2015-01-20 09:29:28 -08001286}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001287
Craig Tillera82950e2015-09-22 12:33:20 -07001288grpc_call_error grpc_server_request_call(
1289 grpc_server *server, grpc_call **call, grpc_call_details *details,
1290 grpc_metadata_array *initial_metadata,
1291 grpc_completion_queue *cq_bound_to_call,
1292 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001293 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001294 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001295 requested_call *rc = gpr_malloc(sizeof(*rc));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001296 GRPC_API_TRACE(
1297 "grpc_server_request_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001298 "server=%p, call=%p, details=%p, initial_metadata=%p, "
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001299 "cq_bound_to_call=%p, cq_for_notification=%p, tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001300 7, (server, call, details, initial_metadata, cq_bound_to_call,
1301 cq_for_notification, tag));
Craig Tillera82950e2015-09-22 12:33:20 -07001302 if (!grpc_cq_is_server_cq(cq_for_notification)) {
1303 gpr_free(rc);
1304 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1305 goto done;
1306 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001307 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller9928d392015-08-18 09:40:24 -07001308 details->reserved = NULL;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001309 rc->type = BATCH_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001310 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001311 rc->tag = tag;
1312 rc->cq_bound_to_call = cq_bound_to_call;
1313 rc->cq_for_notification = cq_for_notification;
1314 rc->call = call;
1315 rc->data.batch.details = details;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001316 rc->initial_metadata = initial_metadata;
Craig Tillera82950e2015-09-22 12:33:20 -07001317 error = queue_call_request(&exec_ctx, server, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001318done:
Craig Tillera82950e2015-09-22 12:33:20 -07001319 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001320 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001321}
1322
Craig Tillera82950e2015-09-22 12:33:20 -07001323grpc_call_error grpc_server_request_registered_call(
Craig Tillerb9d35962015-09-11 13:31:16 -07001324 grpc_server *server, void *rmp, grpc_call **call, gpr_timespec *deadline,
Craig Tillera82950e2015-09-22 12:33:20 -07001325 grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload,
1326 grpc_completion_queue *cq_bound_to_call,
1327 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001328 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001329 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001330 requested_call *rc = gpr_malloc(sizeof(*rc));
Craig Tillerb9d35962015-09-11 13:31:16 -07001331 registered_method *rm = rmp;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001332 GRPC_API_TRACE(
1333 "grpc_server_request_registered_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001334 "server=%p, rmp=%p, call=%p, deadline=%p, initial_metadata=%p, "
1335 "optional_payload=%p, cq_bound_to_call=%p, cq_for_notification=%p, "
1336 "tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001337 9, (server, rmp, call, deadline, initial_metadata, optional_payload,
1338 cq_bound_to_call, cq_for_notification, tag));
Craig Tillera82950e2015-09-22 12:33:20 -07001339 if (!grpc_cq_is_server_cq(cq_for_notification)) {
1340 gpr_free(rc);
1341 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1342 goto done;
1343 }
Craig Tiller06cb1a92016-04-04 08:10:47 -07001344 if ((optional_payload == NULL) !=
1345 (rm->payload_handling == GRPC_SRM_PAYLOAD_NONE)) {
1346 gpr_free(rc);
1347 error = GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH;
1348 goto done;
1349 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001350 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller97fc6a32015-07-08 15:31:35 -07001351 rc->type = REGISTERED_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001352 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001353 rc->tag = tag;
1354 rc->cq_bound_to_call = cq_bound_to_call;
1355 rc->cq_for_notification = cq_for_notification;
1356 rc->call = call;
Craig Tillerb9d35962015-09-11 13:31:16 -07001357 rc->data.registered.registered_method = rm;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001358 rc->data.registered.deadline = deadline;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001359 rc->initial_metadata = initial_metadata;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001360 rc->data.registered.optional_payload = optional_payload;
Craig Tillera82950e2015-09-22 12:33:20 -07001361 error = queue_call_request(&exec_ctx, server, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001362done:
Craig Tillera82950e2015-09-22 12:33:20 -07001363 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001364 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001365}
1366
Craig Tillera82950e2015-09-22 12:33:20 -07001367static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
1368 requested_call *rc) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -07001369 *rc->call = NULL;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001370 rc->initial_metadata->count = 0;
1371
Craig Tillera82950e2015-09-22 12:33:20 -07001372 server_ref(server);
1373 grpc_cq_end_op(exec_ctx, rc->cq_for_notification, rc->tag, 0,
1374 done_request_event, rc, &rc->completion);
Craig Tiller24be0f72015-02-10 14:04:22 -08001375}
1376
Craig Tillera82950e2015-09-22 12:33:20 -07001377const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001378 return server->channel_args;
Craig Tiller190d3602015-02-18 09:23:38 -08001379}
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001380
Craig Tillera82950e2015-09-22 12:33:20 -07001381int grpc_server_has_open_connections(grpc_server *server) {
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001382 int r;
Craig Tillera82950e2015-09-22 12:33:20 -07001383 gpr_mu_lock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001384 r = server->root_channel_data.next != &server->root_channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -07001385 gpr_mu_unlock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001386 return r;
1387}