blob: 37cc2bd101dd466c068fa14cffc5a5672cd4e56a [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 Tiller88512692016-04-04 09:32:52 -0700235static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *calld, bool success);
Craig Tillera82950e2015-09-22 12:33:20 -0700236static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
237 requested_call *rc);
Vijay Pai8931cdd2015-06-17 12:42:17 -0700238/* Before calling maybe_finish_shutdown, we must hold mu_global and not
239 hold mu_call */
Craig Tillera82950e2015-09-22 12:33:20 -0700240static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_server *server);
Craig Tiller24be0f72015-02-10 14:04:22 -0800241
Craig Tiller729b35a2015-07-13 12:36:47 -0700242/*
243 * channel broadcaster
244 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700245
246/* assumes server locked */
Craig Tillera82950e2015-09-22 12:33:20 -0700247static void channel_broadcaster_init(grpc_server *s, channel_broadcaster *cb) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700248 channel_data *c;
249 size_t count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700250 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
251 count++;
252 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700253 cb->num_channels = count;
Craig Tillera82950e2015-09-22 12:33:20 -0700254 cb->channels = gpr_malloc(sizeof(*cb->channels) * cb->num_channels);
Craig Tillerff3ae682015-06-29 17:44:04 -0700255 count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700256 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
257 cb->channels[count++] = c->channel;
258 GRPC_CHANNEL_INTERNAL_REF(c->channel, "broadcast");
259 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700260}
261
Craig Tillera82950e2015-09-22 12:33:20 -0700262struct shutdown_cleanup_args {
Craig Tiller33825112015-09-18 07:44:19 -0700263 grpc_closure closure;
Craig Tillerff3ae682015-06-29 17:44:04 -0700264 gpr_slice slice;
265};
266
Craig Tillera82950e2015-09-22 12:33:20 -0700267static void shutdown_cleanup(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller6c396862016-01-28 13:53:40 -0800268 bool iomgr_status_ignored) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700269 struct shutdown_cleanup_args *a = arg;
Craig Tillera82950e2015-09-22 12:33:20 -0700270 gpr_slice_unref(a->slice);
271 gpr_free(a);
Craig Tillerff3ae682015-06-29 17:44:04 -0700272}
273
Craig Tillera82950e2015-09-22 12:33:20 -0700274static void send_shutdown(grpc_exec_ctx *exec_ctx, grpc_channel *channel,
275 int send_goaway, int send_disconnect) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700276 grpc_transport_op op;
277 struct shutdown_cleanup_args *sc;
278 grpc_channel_element *elem;
279
Craig Tillera82950e2015-09-22 12:33:20 -0700280 memset(&op, 0, sizeof(op));
Craig Tillerff3ae682015-06-29 17:44:04 -0700281 op.send_goaway = send_goaway;
Craig Tillera82950e2015-09-22 12:33:20 -0700282 sc = gpr_malloc(sizeof(*sc));
283 sc->slice = gpr_slice_from_copied_string("Server shutdown");
Craig Tillerff3ae682015-06-29 17:44:04 -0700284 op.goaway_message = &sc->slice;
285 op.goaway_status = GRPC_STATUS_OK;
286 op.disconnect = send_disconnect;
Craig Tillera82950e2015-09-22 12:33:20 -0700287 grpc_closure_init(&sc->closure, shutdown_cleanup, sc);
Craig Tillerff3ae682015-06-29 17:44:04 -0700288 op.on_consumed = &sc->closure;
289
Craig Tillera82950e2015-09-22 12:33:20 -0700290 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
291 elem->filter->start_transport_op(exec_ctx, elem, &op);
Craig Tillerff3ae682015-06-29 17:44:04 -0700292}
293
Craig Tillera82950e2015-09-22 12:33:20 -0700294static void channel_broadcaster_shutdown(grpc_exec_ctx *exec_ctx,
295 channel_broadcaster *cb,
296 int send_goaway,
297 int force_disconnect) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700298 size_t i;
299
Craig Tillera82950e2015-09-22 12:33:20 -0700300 for (i = 0; i < cb->num_channels; i++) {
301 send_shutdown(exec_ctx, cb->channels[i], send_goaway, force_disconnect);
302 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, cb->channels[i], "broadcast");
303 }
304 gpr_free(cb->channels);
Craig Tillerff3ae682015-06-29 17:44:04 -0700305}
306
Craig Tiller729b35a2015-07-13 12:36:47 -0700307/*
308 * request_matcher
309 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700310
Craig Tiller88512692016-04-04 09:32:52 -0700311static void request_matcher_init(request_matcher *rm, size_t entries,
312 grpc_server *server) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700313 memset(rm, 0, sizeof(*rm));
Craig Tiller88512692016-04-04 09:32:52 -0700314 rm->server = server;
Craig Tillerb9d35962015-09-11 13:31:16 -0700315 rm->requests = gpr_stack_lockfree_create(entries);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800316}
317
Craig Tillerb9d35962015-09-11 13:31:16 -0700318static void request_matcher_destroy(request_matcher *rm) {
319 GPR_ASSERT(gpr_stack_lockfree_pop(rm->requests) == -1);
320 gpr_stack_lockfree_destroy(rm->requests);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800321}
322
Craig Tiller6c396862016-01-28 13:53:40 -0800323static void kill_zombie(grpc_exec_ctx *exec_ctx, void *elem, bool success) {
Craig Tillera82950e2015-09-22 12:33:20 -0700324 grpc_call_destroy(grpc_call_from_top_element(elem));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800325}
326
Craig Tiller8dc09712015-09-24 13:58:16 -0700327static void request_matcher_zombify_all_pending_calls(grpc_exec_ctx *exec_ctx,
328 request_matcher *rm) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700329 while (rm->pending_head) {
330 call_data *calld = rm->pending_head;
331 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -0700332 gpr_mu_lock(&calld->mu_state);
333 calld->state = ZOMBIED;
334 gpr_mu_unlock(&calld->mu_state);
335 grpc_closure_init(
336 &calld->kill_zombie_closure, kill_zombie,
337 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tiller6c396862016-01-28 13:53:40 -0800338 grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, true, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700339 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800340}
341
Craig Tillera82950e2015-09-22 12:33:20 -0700342static void request_matcher_kill_requests(grpc_exec_ctx *exec_ctx,
343 grpc_server *server,
344 request_matcher *rm) {
Craig Tiller1191e212015-07-30 14:49:02 -0700345 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -0700346 while ((request_id = gpr_stack_lockfree_pop(rm->requests)) != -1) {
347 fail_call(exec_ctx, server, &server->requested_calls[request_id]);
348 }
Craig Tiller1191e212015-07-30 14:49:02 -0700349}
350
Craig Tiller729b35a2015-07-13 12:36:47 -0700351/*
352 * server proper
353 */
354
Craig Tillera82950e2015-09-22 12:33:20 -0700355static void server_ref(grpc_server *server) {
356 gpr_ref(&server->internal_refcount);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800357}
358
Craig Tillera82950e2015-09-22 12:33:20 -0700359static void server_delete(grpc_exec_ctx *exec_ctx, grpc_server *server) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800360 registered_method *rm;
Craig Tiller89504612015-04-27 11:48:46 -0700361 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700362 grpc_channel_args_destroy(server->channel_args);
363 gpr_mu_destroy(&server->mu_global);
364 gpr_mu_destroy(&server->mu_call);
Craig Tillera82950e2015-09-22 12:33:20 -0700365 while ((rm = server->registered_methods) != NULL) {
366 server->registered_methods = rm->next;
367 request_matcher_destroy(&rm->request_matcher);
368 gpr_free(rm->method);
369 gpr_free(rm->host);
370 gpr_free(rm);
371 }
372 for (i = 0; i < server->cq_count; i++) {
373 GRPC_CQ_INTERNAL_UNREF(server->cqs[i], "server");
374 }
375 request_matcher_destroy(&server->unregistered_request_matcher);
376 gpr_stack_lockfree_destroy(server->request_freelist);
377 gpr_free(server->cqs);
378 gpr_free(server->pollsets);
379 gpr_free(server->shutdown_tags);
380 gpr_free(server->requested_calls);
381 gpr_free(server);
Craig Tilleree945e82015-05-26 16:15:34 -0700382}
383
Craig Tillera82950e2015-09-22 12:33:20 -0700384static void server_unref(grpc_exec_ctx *exec_ctx, grpc_server *server) {
385 if (gpr_unref(&server->internal_refcount)) {
386 server_delete(exec_ctx, server);
387 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800388}
389
Craig Tillera82950e2015-09-22 12:33:20 -0700390static int is_channel_orphaned(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800391 return chand->next == chand;
392}
393
Craig Tillera82950e2015-09-22 12:33:20 -0700394static void orphan_channel(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800395 chand->next->prev = chand->prev;
396 chand->prev->next = chand->next;
397 chand->next = chand->prev = chand;
398}
399
Craig Tillera82950e2015-09-22 12:33:20 -0700400static void finish_destroy_channel(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tiller6c396862016-01-28 13:53:40 -0800401 bool success) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800402 channel_data *chand = cd;
403 grpc_server *server = chand->server;
Craig Tillera82950e2015-09-22 12:33:20 -0700404 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "server");
405 server_unref(exec_ctx, server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800406}
407
Craig Tillera82950e2015-09-22 12:33:20 -0700408static void destroy_channel(grpc_exec_ctx *exec_ctx, channel_data *chand) {
409 if (is_channel_orphaned(chand)) return;
410 GPR_ASSERT(chand->server != NULL);
411 orphan_channel(chand);
412 server_ref(chand->server);
413 maybe_finish_shutdown(exec_ctx, chand->server);
David Garcia Quintas284488b2015-05-28 16:27:39 -0700414 chand->finish_destroy_channel_closure.cb = finish_destroy_channel;
415 chand->finish_destroy_channel_closure.cb_arg = chand;
Craig Tillerd7f12e32016-03-03 10:08:31 -0800416
417 grpc_transport_op op;
418 memset(&op, 0, sizeof(op));
419 op.set_accept_stream = true;
420 op.on_consumed = &chand->finish_destroy_channel_closure;
421 grpc_channel_next_op(exec_ctx,
422 grpc_channel_stack_element(
423 grpc_channel_get_channel_stack(chand->channel), 0),
424 &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800425}
426
Craig Tiller06cb1a92016-04-04 08:10:47 -0700427static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) {
428 gpr_slice slice = value->slice;
429 size_t len = GPR_SLICE_LENGTH(slice);
430
431 if (len + 1 > *capacity) {
432 *capacity = GPR_MAX(len + 1, *capacity * 2);
433 *dest = gpr_realloc(*dest, *capacity);
434 }
435 memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1);
436}
437
Craig Tiller88512692016-04-04 09:32:52 -0700438static void done_request_event(grpc_exec_ctx *exec_ctx, void *req,
439 grpc_cq_completion *c) {
440 requested_call *rc = req;
441 grpc_server *server = rc->server;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700442
Craig Tiller88512692016-04-04 09:32:52 -0700443 if (rc >= server->requested_calls &&
444 rc < server->requested_calls + server->max_requested_calls) {
445 GPR_ASSERT(rc - server->requested_calls <= INT_MAX);
446 gpr_stack_lockfree_push(server->request_freelist,
447 (int)(rc - server->requested_calls));
448 } else {
449 gpr_free(req);
450 }
Craig Tiller06cb1a92016-04-04 08:10:47 -0700451
Craig Tiller88512692016-04-04 09:32:52 -0700452 server_unref(exec_ctx, server);
453}
Craig Tiller06cb1a92016-04-04 08:10:47 -0700454
Craig Tiller88512692016-04-04 09:32:52 -0700455static void publish_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
456 call_data *calld, requested_call *rc) {
Craig Tiller06cb1a92016-04-04 08:10:47 -0700457 grpc_call_set_completion_queue(exec_ctx, calld->call, rc->cq_bound_to_call);
Craig Tiller88512692016-04-04 09:32:52 -0700458 grpc_call *call = calld->call;
459 *rc->call = call;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700460 calld->cq_new = rc->cq_for_notification;
461 GPR_SWAP(grpc_metadata_array, *rc->initial_metadata, calld->initial_metadata);
462 switch (rc->type) {
463 case BATCH_CALL:
464 GPR_ASSERT(calld->host != NULL);
465 GPR_ASSERT(calld->path != NULL);
466 cpstr(&rc->data.batch.details->host,
467 &rc->data.batch.details->host_capacity, calld->host);
468 cpstr(&rc->data.batch.details->method,
469 &rc->data.batch.details->method_capacity, calld->path);
470 rc->data.batch.details->deadline = calld->deadline;
471 rc->data.batch.details->flags =
472 0 | (calld->recv_idempotent_request
473 ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
474 : 0);
475 break;
476 case REGISTERED_CALL:
477 *rc->data.registered.deadline = calld->deadline;
478 if (rc->data.registered.optional_payload) {
Craig Tiller88512692016-04-04 09:32:52 -0700479 *rc->data.registered.optional_payload = calld->payload;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700480 }
481 break;
482 default:
483 GPR_UNREACHABLE_CODE(return );
484 }
485
Craig Tiller88512692016-04-04 09:32:52 -0700486 grpc_call_element *elem =
487 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
488 channel_data *chand = elem->channel_data;
489 server_ref(chand->server);
490 grpc_cq_end_op(exec_ctx, calld->cq_new, rc->tag, true, done_request_event, rc,
491 &rc->completion);
Craig Tiller06cb1a92016-04-04 08:10:47 -0700492}
493
Craig Tiller88512692016-04-04 09:32:52 -0700494static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
495 call_data *calld = arg;
496 request_matcher *rm = calld->request_matcher;
497 grpc_server *server = rm->server;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700498
Craig Tiller88512692016-04-04 09:32:52 -0700499 if (!success || gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tillera82950e2015-09-22 12:33:20 -0700500 gpr_mu_lock(&calld->mu_state);
501 calld->state = ZOMBIED;
502 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -0700503 grpc_closure_init(
504 &calld->kill_zombie_closure, kill_zombie,
505 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tiller6c396862016-01-28 13:53:40 -0800506 grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, true, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700507 return;
508 }
Craig Tiller45724b32015-09-22 10:42:19 -0700509
Craig Tiller88512692016-04-04 09:32:52 -0700510 int request_id = gpr_stack_lockfree_pop(rm->requests);
Craig Tillera82950e2015-09-22 12:33:20 -0700511 if (request_id == -1) {
512 gpr_mu_lock(&server->mu_call);
513 gpr_mu_lock(&calld->mu_state);
514 calld->state = PENDING;
515 gpr_mu_unlock(&calld->mu_state);
Craig Tillerb9d35962015-09-11 13:31:16 -0700516 if (rm->pending_head == NULL) {
517 rm->pending_tail = rm->pending_head = calld;
Craig Tillera82950e2015-09-22 12:33:20 -0700518 } else {
Craig Tillerb9d35962015-09-11 13:31:16 -0700519 rm->pending_tail->pending_next = calld;
520 rm->pending_tail = calld;
Craig Tiller45724b32015-09-22 10:42:19 -0700521 }
Craig Tillera82950e2015-09-22 12:33:20 -0700522 calld->pending_next = NULL;
523 gpr_mu_unlock(&server->mu_call);
524 } else {
525 gpr_mu_lock(&calld->mu_state);
526 calld->state = ACTIVATED;
527 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -0700528 publish_call(exec_ctx, server, calld, &server->requested_calls[request_id]);
529 }
530}
531
532static void finish_start_new_rpc(
533 grpc_exec_ctx *exec_ctx, grpc_server *server, grpc_call_element *elem,
534 request_matcher *rm,
535 grpc_server_register_method_payload_handling payload_handling) {
536 call_data *calld = elem->call_data;
537
538 if (gpr_atm_acq_load(&server->shutdown_flag)) {
539 gpr_mu_lock(&calld->mu_state);
540 calld->state = ZOMBIED;
541 gpr_mu_unlock(&calld->mu_state);
542 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
543 grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, true, NULL);
544 return;
545 }
546
547 calld->request_matcher = rm;
548
549 switch (payload_handling) {
550 case GRPC_SRM_PAYLOAD_NONE:
551 publish_new_rpc(exec_ctx, calld, true);
552 break;
553 case GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER: {
554 grpc_op op;
555 memset(&op, 0, sizeof(op));
556 op.op = GRPC_OP_RECV_MESSAGE;
557 op.data.recv_message = &calld->payload;
558 grpc_closure_init(&calld->publish, publish_new_rpc, calld);
559 grpc_call_start_batch_and_execute(exec_ctx, calld->call, &op, 1,
560 &calld->publish);
561 break;
562 }
Craig Tillera82950e2015-09-22 12:33:20 -0700563 }
Craig Tiller04cc8be2015-02-10 16:11:22 -0800564}
565
Craig Tillera82950e2015-09-22 12:33:20 -0700566static void start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800567 channel_data *chand = elem->channel_data;
568 call_data *calld = elem->call_data;
569 grpc_server *server = chand->server;
Craig Tiller7536af02015-12-22 13:49:30 -0800570 uint32_t i;
571 uint32_t hash;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800572 channel_registered_method *rm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800573
Craig Tillera82950e2015-09-22 12:33:20 -0700574 if (chand->registered_methods && calld->path && calld->host) {
575 /* TODO(ctiller): unify these two searches */
576 /* check for an exact match with host */
577 hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash);
578 for (i = 0; i <= chand->registered_method_max_probes; i++) {
579 rm = &chand->registered_methods[(hash + i) %
580 chand->registered_method_slots];
581 if (!rm) break;
582 if (rm->host != calld->host) continue;
583 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800584 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
585 !calld->recv_idempotent_request)
586 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700587 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700588 &rm->server_registered_method->request_matcher,
589 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700590 return;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800591 }
Craig Tillera82950e2015-09-22 12:33:20 -0700592 /* check for a wildcard method definition (no host set) */
593 hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash);
594 for (i = 0; i <= chand->registered_method_max_probes; i++) {
595 rm = &chand->registered_methods[(hash + i) %
596 chand->registered_method_slots];
597 if (!rm) break;
598 if (rm->host != NULL) continue;
599 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800600 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
601 !calld->recv_idempotent_request)
602 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700603 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700604 &rm->server_registered_method->request_matcher,
605 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700606 return;
607 }
608 }
609 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700610 &server->unregistered_request_matcher,
611 GRPC_SRM_PAYLOAD_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800612}
613
Craig Tillera82950e2015-09-22 12:33:20 -0700614static int num_listeners(grpc_server *server) {
Craig Tilleree945e82015-05-26 16:15:34 -0700615 listener *l;
616 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700617 for (l = server->listeners; l; l = l->next) {
618 n++;
619 }
Craig Tilleree945e82015-05-26 16:15:34 -0700620 return n;
621}
622
Craig Tillera82950e2015-09-22 12:33:20 -0700623static void done_shutdown_event(grpc_exec_ctx *exec_ctx, void *server,
624 grpc_cq_completion *completion) {
625 server_unref(exec_ctx, server);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700626}
627
Craig Tillera82950e2015-09-22 12:33:20 -0700628static int num_channels(grpc_server *server) {
Craig Tillerab54f792015-07-08 08:34:20 -0700629 channel_data *chand;
630 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700631 for (chand = server->root_channel_data.next;
632 chand != &server->root_channel_data; chand = chand->next) {
633 n++;
634 }
Craig Tillerab54f792015-07-08 08:34:20 -0700635 return n;
636}
637
Craig Tillera82950e2015-09-22 12:33:20 -0700638static void kill_pending_work_locked(grpc_exec_ctx *exec_ctx,
639 grpc_server *server) {
Craig Tiller1191e212015-07-30 14:49:02 -0700640 registered_method *rm;
Craig Tillera82950e2015-09-22 12:33:20 -0700641 request_matcher_kill_requests(exec_ctx, server,
642 &server->unregistered_request_matcher);
643 request_matcher_zombify_all_pending_calls(
644 exec_ctx, &server->unregistered_request_matcher);
645 for (rm = server->registered_methods; rm; rm = rm->next) {
646 request_matcher_kill_requests(exec_ctx, server, &rm->request_matcher);
647 request_matcher_zombify_all_pending_calls(exec_ctx, &rm->request_matcher);
648 }
Craig Tillerdc627722015-05-26 15:27:02 -0700649}
650
Craig Tillera82950e2015-09-22 12:33:20 -0700651static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx,
652 grpc_server *server) {
Craig Tiller45724b32015-09-22 10:42:19 -0700653 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700654 if (!gpr_atm_acq_load(&server->shutdown_flag) || server->shutdown_published) {
655 return;
656 }
Craig Tiller45724b32015-09-22 10:42:19 -0700657
Craig Tillera82950e2015-09-22 12:33:20 -0700658 kill_pending_work_locked(exec_ctx, server);
Craig Tiller45724b32015-09-22 10:42:19 -0700659
Craig Tillera82950e2015-09-22 12:33:20 -0700660 if (server->root_channel_data.next != &server->root_channel_data ||
661 server->listeners_destroyed < num_listeners(server)) {
662 if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME),
663 server->last_shutdown_message_time),
664 gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
665 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
666 gpr_log(GPR_DEBUG,
667 "Waiting for %d channels and %d/%d listeners to be destroyed"
668 " before shutting down server",
669 num_channels(server),
670 num_listeners(server) - server->listeners_destroyed,
671 num_listeners(server));
Craig Tiller45724b32015-09-22 10:42:19 -0700672 }
Craig Tillera82950e2015-09-22 12:33:20 -0700673 return;
674 }
Craig Tiller45724b32015-09-22 10:42:19 -0700675 server->shutdown_published = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700676 for (i = 0; i < server->num_shutdown_tags; i++) {
677 server_ref(server);
678 grpc_cq_end_op(exec_ctx, server->shutdown_tags[i].cq,
679 server->shutdown_tags[i].tag, 1, done_shutdown_event, server,
680 &server->shutdown_tags[i].completion);
681 }
Craig Tiller45724b32015-09-22 10:42:19 -0700682}
683
Craig Tillera82950e2015-09-22 12:33:20 -0700684static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700685 grpc_call_element *elem = user_data;
Craig Tillercce17ac2015-01-20 09:29:28 -0800686 call_data *calld = elem->call_data;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800687 if (md->key == GRPC_MDSTR_PATH) {
Craig Tillera82950e2015-09-22 12:33:20 -0700688 calld->path = GRPC_MDSTR_REF(md->value);
689 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800690 } else if (md->key == GRPC_MDSTR_AUTHORITY) {
Craig Tillera82950e2015-09-22 12:33:20 -0700691 calld->host = GRPC_MDSTR_REF(md->value);
692 return NULL;
693 }
Craig Tiller6902ad22015-04-16 08:01:49 -0700694 return md;
695}
696
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800697static void server_on_recv_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tiller6c396862016-01-28 13:53:40 -0800698 bool success) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700699 grpc_call_element *elem = ptr;
Craig Tiller6902ad22015-04-16 08:01:49 -0700700 call_data *calld = elem->call_data;
Craig Tiller94329d02015-07-23 09:52:11 -0700701 gpr_timespec op_deadline;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700702
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800703 grpc_metadata_batch_filter(calld->recv_initial_metadata, server_filter, elem);
704 op_deadline = calld->recv_initial_metadata->deadline;
705 if (0 != gpr_time_cmp(op_deadline, gpr_inf_future(op_deadline.clock_type))) {
706 calld->deadline = op_deadline;
707 }
708 if (calld->host && calld->path) {
709 /* do nothing */
710 } else {
711 success = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700712 }
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700713
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800714 calld->on_done_recv_initial_metadata->cb(
715 exec_ctx, calld->on_done_recv_initial_metadata->cb_arg, success);
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700716}
717
Craig Tillera82950e2015-09-22 12:33:20 -0700718static void server_mutate_op(grpc_call_element *elem,
719 grpc_transport_stream_op *op) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700720 call_data *calld = elem->call_data;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700721
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800722 if (op->recv_initial_metadata != NULL) {
Craig Tiller4d40ba32016-03-09 17:48:40 -0800723 GPR_ASSERT(op->recv_idempotent_request == NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800724 calld->recv_initial_metadata = op->recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800725 calld->on_done_recv_initial_metadata = op->recv_initial_metadata_ready;
726 op->recv_initial_metadata_ready = &calld->server_on_recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800727 op->recv_idempotent_request = &calld->recv_idempotent_request;
Craig Tillera82950e2015-09-22 12:33:20 -0700728 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700729}
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700730
Craig Tillera82950e2015-09-22 12:33:20 -0700731static void server_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
732 grpc_call_element *elem,
733 grpc_transport_stream_op *op) {
734 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
735 server_mutate_op(elem, op);
736 grpc_call_next_op(exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800737}
738
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800739static void got_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tiller6c396862016-01-28 13:53:40 -0800740 bool success) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800741 grpc_call_element *elem = ptr;
742 call_data *calld = elem->call_data;
743 if (success) {
744 start_new_rpc(exec_ctx, elem);
745 } else {
746 gpr_mu_lock(&calld->mu_state);
747 if (calld->state == NOT_STARTED) {
748 calld->state = ZOMBIED;
749 gpr_mu_unlock(&calld->mu_state);
750 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
Craig Tiller6c396862016-01-28 13:53:40 -0800751 grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, true, NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800752 } else if (calld->state == PENDING) {
753 calld->state = ZOMBIED;
754 gpr_mu_unlock(&calld->mu_state);
755 /* zombied call will be destroyed when it's removed from the pending
756 queue... later */
757 } else {
758 gpr_mu_unlock(&calld->mu_state);
759 }
760 }
761}
762
763static void accept_stream(grpc_exec_ctx *exec_ctx, void *cd,
764 grpc_transport *transport,
Craig Tillera82950e2015-09-22 12:33:20 -0700765 const void *transport_server_data) {
Craig Tillere039f032015-06-25 12:54:23 -0700766 channel_data *chand = cd;
767 /* create a call */
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800768 grpc_call *call =
769 grpc_call_create(chand->channel, NULL, 0, NULL, transport_server_data,
770 NULL, 0, gpr_inf_future(GPR_CLOCK_MONOTONIC));
771 grpc_call_element *elem =
772 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
773 call_data *calld = elem->call_data;
774 grpc_op op;
775 memset(&op, 0, sizeof(op));
776 op.op = GRPC_OP_RECV_INITIAL_METADATA;
777 op.data.recv_initial_metadata = &calld->initial_metadata;
778 grpc_closure_init(&calld->got_initial_metadata, got_initial_metadata, elem);
779 grpc_call_start_batch_and_execute(exec_ctx, call, &op, 1,
780 &calld->got_initial_metadata);
Craig Tillere039f032015-06-25 12:54:23 -0700781}
782
Craig Tillera82950e2015-09-22 12:33:20 -0700783static void channel_connectivity_changed(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tiller6c396862016-01-28 13:53:40 -0800784 bool iomgr_status_ignored) {
Craig Tillere039f032015-06-25 12:54:23 -0700785 channel_data *chand = cd;
786 grpc_server *server = chand->server;
Craig Tillera82950e2015-09-22 12:33:20 -0700787 if (chand->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE) {
788 grpc_transport_op op;
789 memset(&op, 0, sizeof(op));
790 op.on_connectivity_state_change = &chand->channel_connectivity_changed,
791 op.connectivity_state = &chand->connectivity_state;
792 grpc_channel_next_op(exec_ctx,
793 grpc_channel_stack_element(
794 grpc_channel_get_channel_stack(chand->channel), 0),
795 &op);
796 } else {
797 gpr_mu_lock(&server->mu_global);
798 destroy_channel(exec_ctx, chand);
799 gpr_mu_unlock(&server->mu_global);
800 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "connectivity");
801 }
Craig Tillere039f032015-06-25 12:54:23 -0700802}
803
Craig Tillera82950e2015-09-22 12:33:20 -0700804static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800805 grpc_call_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800806 call_data *calld = elem->call_data;
807 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700808 memset(calld, 0, sizeof(call_data));
809 calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
810 calld->call = grpc_call_from_top_element(elem);
811 gpr_mu_init(&calld->mu_state);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800812
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800813 grpc_closure_init(&calld->server_on_recv_initial_metadata,
814 server_on_recv_initial_metadata, elem);
Craig Tiller1e6facb2015-06-11 22:47:11 -0700815
Craig Tillera82950e2015-09-22 12:33:20 -0700816 server_ref(chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800817}
818
Craig Tillera82950e2015-09-22 12:33:20 -0700819static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
820 grpc_call_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800821 channel_data *chand = elem->channel_data;
Craig Tillerdb7db992015-01-29 11:19:01 -0800822 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800823
Craig Tillera82950e2015-09-22 12:33:20 -0700824 GPR_ASSERT(calld->state != PENDING);
Craig Tiller092d8d12015-07-04 22:35:00 -0700825
Craig Tillera82950e2015-09-22 12:33:20 -0700826 if (calld->host) {
827 GRPC_MDSTR_UNREF(calld->host);
828 }
829 if (calld->path) {
830 GRPC_MDSTR_UNREF(calld->path);
831 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800832 grpc_metadata_array_destroy(&calld->initial_metadata);
Craig Tiller4df31a62015-01-30 09:44:31 -0800833
Craig Tillera82950e2015-09-22 12:33:20 -0700834 gpr_mu_destroy(&calld->mu_state);
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700835
Craig Tillera82950e2015-09-22 12:33:20 -0700836 server_unref(exec_ctx, chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800837}
838
Craig Tillera82950e2015-09-22 12:33:20 -0700839static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800840 grpc_channel_element *elem,
841 grpc_channel_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800842 channel_data *chand = elem->channel_data;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800843 GPR_ASSERT(args->is_first);
844 GPR_ASSERT(!args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800845 chand->server = NULL;
846 chand->channel = NULL;
847 chand->next = chand->prev = chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800848 chand->registered_methods = NULL;
Craig Tillere039f032015-06-25 12:54:23 -0700849 chand->connectivity_state = GRPC_CHANNEL_IDLE;
Craig Tillera82950e2015-09-22 12:33:20 -0700850 grpc_closure_init(&chand->channel_connectivity_changed,
851 channel_connectivity_changed, chand);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800852}
853
Craig Tillera82950e2015-09-22 12:33:20 -0700854static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
855 grpc_channel_element *elem) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800856 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800857 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700858 if (chand->registered_methods) {
859 for (i = 0; i < chand->registered_method_slots; i++) {
860 if (chand->registered_methods[i].method) {
861 GRPC_MDSTR_UNREF(chand->registered_methods[i].method);
862 }
863 if (chand->registered_methods[i].host) {
864 GRPC_MDSTR_UNREF(chand->registered_methods[i].host);
865 }
Craig Tillerec3257c2015-02-12 15:59:43 -0800866 }
Craig Tillera82950e2015-09-22 12:33:20 -0700867 gpr_free(chand->registered_methods);
868 }
869 if (chand->server) {
870 gpr_mu_lock(&chand->server->mu_global);
871 chand->next->prev = chand->prev;
872 chand->prev->next = chand->next;
873 chand->next = chand->prev = chand;
874 maybe_finish_shutdown(exec_ctx, chand->server);
875 gpr_mu_unlock(&chand->server->mu_global);
Craig Tillera82950e2015-09-22 12:33:20 -0700876 server_unref(exec_ctx, chand->server);
877 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800878}
879
Craig Tiller178edfa2016-02-17 20:54:46 -0800880const grpc_channel_filter grpc_server_top_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700881 server_start_transport_stream_op,
882 grpc_channel_next_op,
883 sizeof(call_data),
884 init_call_elem,
885 grpc_call_stack_ignore_set_pollset,
886 destroy_call_elem,
887 sizeof(channel_data),
888 init_channel_elem,
889 destroy_channel_elem,
890 grpc_call_next_get_peer,
891 "server",
Craig Tiller9f28ac22015-01-27 17:01:29 -0800892};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800893
Craig Tillera82950e2015-09-22 12:33:20 -0700894void grpc_server_register_completion_queue(grpc_server *server,
895 grpc_completion_queue *cq,
896 void *reserved) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800897 size_t i, n;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700898 GRPC_API_TRACE(
899 "grpc_server_register_completion_queue(server=%p, cq=%p, reserved=%p)", 3,
900 (server, cq, reserved));
Craig Tillera82950e2015-09-22 12:33:20 -0700901 GPR_ASSERT(!reserved);
902 for (i = 0; i < server->cq_count; i++) {
903 if (server->cqs[i] == cq) return;
904 }
905 GRPC_CQ_INTERNAL_REF(cq, "server");
906 grpc_cq_mark_server_cq(cq);
Craig Tiller20bc56d2015-02-12 09:02:56 -0800907 n = server->cq_count++;
Craig Tillera82950e2015-09-22 12:33:20 -0700908 server->cqs = gpr_realloc(server->cqs,
909 server->cq_count * sizeof(grpc_completion_queue *));
Craig Tiller20bc56d2015-02-12 09:02:56 -0800910 server->cqs[n] = cq;
911}
912
Craig Tiller178edfa2016-02-17 20:54:46 -0800913grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800914 size_t i;
Craig Tiller178edfa2016-02-17 20:54:46 -0800915
916 GRPC_API_TRACE("grpc_server_create(%p, %p)", 2, (args, reserved));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800917
Craig Tillera82950e2015-09-22 12:33:20 -0700918 grpc_server *server = gpr_malloc(sizeof(grpc_server));
Craig Tiller60fd3612015-03-05 16:24:22 -0800919
Craig Tillera82950e2015-09-22 12:33:20 -0700920 GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
Craig Tiller60fd3612015-03-05 16:24:22 -0800921
Craig Tillera82950e2015-09-22 12:33:20 -0700922 memset(server, 0, sizeof(grpc_server));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800923
Craig Tillera82950e2015-09-22 12:33:20 -0700924 gpr_mu_init(&server->mu_global);
925 gpr_mu_init(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800926
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800927 /* decremented by grpc_server_destroy */
Craig Tillera82950e2015-09-22 12:33:20 -0700928 gpr_ref_init(&server->internal_refcount, 1);
929 server->root_channel_data.next = server->root_channel_data.prev =
930 &server->root_channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800931
Craig Tiller6a006ce2015-07-13 16:25:40 -0700932 /* TODO(ctiller): expose a channel_arg for this */
933 server->max_requested_calls = 32768;
Craig Tillera82950e2015-09-22 12:33:20 -0700934 server->request_freelist =
935 gpr_stack_lockfree_create(server->max_requested_calls);
936 for (i = 0; i < (size_t)server->max_requested_calls; i++) {
937 gpr_stack_lockfree_push(server->request_freelist, (int)i);
938 }
939 request_matcher_init(&server->unregistered_request_matcher,
Craig Tiller88512692016-04-04 09:32:52 -0700940 server->max_requested_calls, server);
Craig Tillera82950e2015-09-22 12:33:20 -0700941 server->requested_calls = gpr_malloc(server->max_requested_calls *
942 sizeof(*server->requested_calls));
Craig Tiller729b35a2015-07-13 12:36:47 -0700943
Craig Tillera82950e2015-09-22 12:33:20 -0700944 server->channel_args = grpc_channel_args_copy(args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800945
946 return server;
947}
948
Craig Tillera82950e2015-09-22 12:33:20 -0700949static int streq(const char *a, const char *b) {
950 if (a == NULL && b == NULL) return 1;
951 if (a == NULL) return 0;
952 if (b == NULL) return 0;
953 return 0 == strcmp(a, b);
Craig Tiller24be0f72015-02-10 14:04:22 -0800954}
955
Craig Tiller06cb1a92016-04-04 08:10:47 -0700956void *grpc_server_register_method(
957 grpc_server *server, const char *method, const char *host,
958 grpc_server_register_method_payload_handling payload_handling,
959 uint32_t flags) {
Craig Tiller24be0f72015-02-10 14:04:22 -0800960 registered_method *m;
Craig Tillerb2906862016-03-10 06:50:07 -0800961 GRPC_API_TRACE(
962 "grpc_server_register_method(server=%p, method=%s, host=%s, "
963 "flags=0x%08x)",
964 4, (server, method, host, flags));
Craig Tillera82950e2015-09-22 12:33:20 -0700965 if (!method) {
966 gpr_log(GPR_ERROR,
967 "grpc_server_register_method method string cannot be NULL");
968 return NULL;
969 }
970 for (m = server->registered_methods; m; m = m->next) {
971 if (streq(m->method, method) && streq(m->host, host)) {
972 gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method,
973 host ? host : "*");
Craig Tiller24be0f72015-02-10 14:04:22 -0800974 return NULL;
975 }
Craig Tillera82950e2015-09-22 12:33:20 -0700976 }
Craig Tillerb2906862016-03-10 06:50:07 -0800977 if ((flags & ~GRPC_INITIAL_METADATA_USED_MASK) != 0) {
978 gpr_log(GPR_ERROR, "grpc_server_register_method invalid flags 0x%08x",
979 flags);
980 return NULL;
981 }
Craig Tillera82950e2015-09-22 12:33:20 -0700982 m = gpr_malloc(sizeof(registered_method));
983 memset(m, 0, sizeof(*m));
Craig Tiller88512692016-04-04 09:32:52 -0700984 request_matcher_init(&m->request_matcher, server->max_requested_calls,
985 server);
Craig Tillera82950e2015-09-22 12:33:20 -0700986 m->method = gpr_strdup(method);
987 m->host = gpr_strdup(host);
Craig Tiller24be0f72015-02-10 14:04:22 -0800988 m->next = server->registered_methods;
Craig Tiller5ada3d22016-04-04 08:30:59 -0700989 m->payload_handling = payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -0800990 m->flags = flags;
Craig Tiller24be0f72015-02-10 14:04:22 -0800991 server->registered_methods = m;
992 return m;
993}
994
Craig Tillera82950e2015-09-22 12:33:20 -0700995void grpc_server_start(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800996 listener *l;
Craig Tiller20bc56d2015-02-12 09:02:56 -0800997 size_t i;
Craig Tillerf5768a62015-09-22 10:54:34 -0700998 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller20bc56d2015-02-12 09:02:56 -0800999
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001000 GRPC_API_TRACE("grpc_server_start(server=%p)", 1, (server));
1001
Craig Tillera82950e2015-09-22 12:33:20 -07001002 server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count);
1003 for (i = 0; i < server->cq_count; i++) {
1004 server->pollsets[i] = grpc_cq_pollset(server->cqs[i]);
1005 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001006
Craig Tillera82950e2015-09-22 12:33:20 -07001007 for (l = server->listeners; l; l = l->next) {
1008 l->start(&exec_ctx, server, l->arg, server->pollsets, server->cq_count);
1009 }
Craig Tillerdfff1b82015-09-21 14:39:57 -07001010
Craig Tillera82950e2015-09-22 12:33:20 -07001011 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001012}
1013
Craig Tillera82950e2015-09-22 12:33:20 -07001014void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *s,
1015 grpc_transport *transport,
Craig Tillera82950e2015-09-22 12:33:20 -07001016 const grpc_channel_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001017 size_t i;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001018 size_t num_registered_methods;
1019 size_t alloc;
1020 registered_method *rm;
1021 channel_registered_method *crm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001022 grpc_channel *channel;
1023 channel_data *chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001024 grpc_mdstr *host;
1025 grpc_mdstr *method;
Craig Tiller7536af02015-12-22 13:49:30 -08001026 uint32_t hash;
Craig Tillerf96dfc32015-09-10 14:43:18 -07001027 size_t slots;
Craig Tiller7536af02015-12-22 13:49:30 -08001028 uint32_t probes;
1029 uint32_t max_probes = 0;
Craig Tillere039f032015-06-25 12:54:23 -07001030 grpc_transport_op op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001031
Craig Tillera82950e2015-09-22 12:33:20 -07001032 for (i = 0; i < s->cq_count; i++) {
1033 memset(&op, 0, sizeof(op));
1034 op.bind_pollset = grpc_cq_pollset(s->cqs[i]);
1035 grpc_transport_perform_op(exec_ctx, transport, &op);
1036 }
ctillerd79b4862014-12-17 16:36:59 -08001037
Craig Tiller178edfa2016-02-17 20:54:46 -08001038 channel =
1039 grpc_channel_create(exec_ctx, NULL, args, GRPC_SERVER_CHANNEL, transport);
Craig Tillera82950e2015-09-22 12:33:20 -07001040 chand = (channel_data *)grpc_channel_stack_element(
Craig Tillerf40df232016-03-25 13:38:14 -07001041 grpc_channel_get_channel_stack(channel), 0)
1042 ->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001043 chand->server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001044 server_ref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001045 chand->channel = channel;
1046
Craig Tiller04cc8be2015-02-10 16:11:22 -08001047 num_registered_methods = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001048 for (rm = s->registered_methods; rm; rm = rm->next) {
1049 num_registered_methods++;
1050 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001051 /* build a lookup table phrased in terms of mdstr's in this channels context
1052 to quickly find registered methods */
Craig Tillera82950e2015-09-22 12:33:20 -07001053 if (num_registered_methods > 0) {
1054 slots = 2 * num_registered_methods;
1055 alloc = sizeof(channel_registered_method) * slots;
1056 chand->registered_methods = gpr_malloc(alloc);
1057 memset(chand->registered_methods, 0, alloc);
1058 for (rm = s->registered_methods; rm; rm = rm->next) {
Craig Tillerb2b42612015-11-20 12:02:17 -08001059 host = rm->host ? grpc_mdstr_from_string(rm->host) : NULL;
1060 method = grpc_mdstr_from_string(rm->method);
Craig Tillera82950e2015-09-22 12:33:20 -07001061 hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash);
1062 for (probes = 0; chand->registered_methods[(hash + probes) % slots]
Craig Tillerf40df232016-03-25 13:38:14 -07001063 .server_registered_method != NULL;
Craig Tillera82950e2015-09-22 12:33:20 -07001064 probes++)
1065 ;
1066 if (probes > max_probes) max_probes = probes;
1067 crm = &chand->registered_methods[(hash + probes) % slots];
1068 crm->server_registered_method = rm;
Craig Tillerb2906862016-03-10 06:50:07 -08001069 crm->flags = rm->flags;
Craig Tillera82950e2015-09-22 12:33:20 -07001070 crm->host = host;
1071 crm->method = method;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001072 }
Craig Tiller7536af02015-12-22 13:49:30 -08001073 GPR_ASSERT(slots <= UINT32_MAX);
1074 chand->registered_method_slots = (uint32_t)slots;
Craig Tillera82950e2015-09-22 12:33:20 -07001075 chand->registered_method_max_probes = max_probes;
1076 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001077
Craig Tillera82950e2015-09-22 12:33:20 -07001078 gpr_mu_lock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001079 chand->next = &s->root_channel_data;
1080 chand->prev = chand->next->prev;
1081 chand->next->prev = chand->prev->next = chand;
Craig Tillera82950e2015-09-22 12:33:20 -07001082 gpr_mu_unlock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001083
Craig Tillera82950e2015-09-22 12:33:20 -07001084 GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity");
1085 memset(&op, 0, sizeof(op));
Craig Tillerd7f12e32016-03-03 10:08:31 -08001086 op.set_accept_stream = true;
1087 op.set_accept_stream_fn = accept_stream;
Craig Tiller4b804102015-06-26 16:16:12 -07001088 op.set_accept_stream_user_data = chand;
1089 op.on_connectivity_state_change = &chand->channel_connectivity_changed;
1090 op.connectivity_state = &chand->connectivity_state;
Craig Tillera82950e2015-09-22 12:33:20 -07001091 op.disconnect = gpr_atm_acq_load(&s->shutdown_flag) != 0;
1092 grpc_transport_perform_op(exec_ctx, transport, &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001093}
1094
Craig Tillera82950e2015-09-22 12:33:20 -07001095void done_published_shutdown(grpc_exec_ctx *exec_ctx, void *done_arg,
1096 grpc_cq_completion *storage) {
1097 (void)done_arg;
1098 gpr_free(storage);
murgatroid9900a3dab2015-08-19 11:15:38 -07001099}
1100
Craig Tillera82950e2015-09-22 12:33:20 -07001101static void listener_destroy_done(grpc_exec_ctx *exec_ctx, void *s,
Craig Tiller6c396862016-01-28 13:53:40 -08001102 bool success) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001103 grpc_server *server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001104 gpr_mu_lock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001105 server->listeners_destroyed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001106 maybe_finish_shutdown(exec_ctx, server);
1107 gpr_mu_unlock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001108}
1109
Craig Tillera82950e2015-09-22 12:33:20 -07001110void grpc_server_shutdown_and_notify(grpc_server *server,
1111 grpc_completion_queue *cq, void *tag) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001112 listener *l;
Craig Tillerbce999f2015-05-27 09:55:51 -07001113 shutdown_tag *sdt;
Craig Tillerff3ae682015-06-29 17:44:04 -07001114 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001115 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001116
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001117 GRPC_API_TRACE("grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", 3,
1118 (server, cq, tag));
1119
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001120 /* lock, and gather up some stuff to do */
Craig Tillera82950e2015-09-22 12:33:20 -07001121 gpr_mu_lock(&server->mu_global);
Craig Tiller4bf29282015-12-14 11:25:48 -08001122 grpc_cq_begin_op(cq, tag);
Craig Tillera82950e2015-09-22 12:33:20 -07001123 if (server->shutdown_published) {
1124 grpc_cq_end_op(&exec_ctx, cq, tag, 1, done_published_shutdown, NULL,
1125 gpr_malloc(sizeof(grpc_cq_completion)));
1126 gpr_mu_unlock(&server->mu_global);
1127 goto done;
1128 }
1129 server->shutdown_tags =
1130 gpr_realloc(server->shutdown_tags,
1131 sizeof(shutdown_tag) * (server->num_shutdown_tags + 1));
Craig Tillerbce999f2015-05-27 09:55:51 -07001132 sdt = &server->shutdown_tags[server->num_shutdown_tags++];
1133 sdt->tag = tag;
1134 sdt->cq = cq;
Craig Tillera82950e2015-09-22 12:33:20 -07001135 if (gpr_atm_acq_load(&server->shutdown_flag)) {
1136 gpr_mu_unlock(&server->mu_global);
1137 goto done;
1138 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001139
Craig Tillera82950e2015-09-22 12:33:20 -07001140 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
Craig Tillerab54f792015-07-08 08:34:20 -07001141
Craig Tillera82950e2015-09-22 12:33:20 -07001142 channel_broadcaster_init(server, &broadcaster);
nnoble0c475f02014-12-05 15:37:39 -08001143
Craig Tillerfc193e12015-09-24 15:29:03 -07001144 gpr_atm_rel_store(&server->shutdown_flag, 1);
1145
Craig Tillerbd217572015-02-11 18:10:56 -08001146 /* collect all unregistered then registered calls */
Craig Tillera82950e2015-09-22 12:33:20 -07001147 gpr_mu_lock(&server->mu_call);
1148 kill_pending_work_locked(&exec_ctx, server);
1149 gpr_mu_unlock(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001150
Craig Tillera82950e2015-09-22 12:33:20 -07001151 maybe_finish_shutdown(&exec_ctx, server);
1152 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001153
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001154 /* Shutdown listeners */
Craig Tillera82950e2015-09-22 12:33:20 -07001155 for (l = server->listeners; l; l = l->next) {
1156 grpc_closure_init(&l->destroy_done, listener_destroy_done, server);
1157 l->destroy(&exec_ctx, server, l->arg, &l->destroy_done);
1158 }
Craig Tillerff3ae682015-06-29 17:44:04 -07001159
Craig Tillera82950e2015-09-22 12:33:20 -07001160 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 1, 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001161
Craig Tillerdfff1b82015-09-21 14:39:57 -07001162done:
Craig Tillera82950e2015-09-22 12:33:20 -07001163 grpc_exec_ctx_finish(&exec_ctx);
Craig Tilleraec96aa2015-04-07 14:32:15 -07001164}
1165
Craig Tillera82950e2015-09-22 12:33:20 -07001166void grpc_server_cancel_all_calls(grpc_server *server) {
Craig Tiller092d8d12015-07-04 22:35:00 -07001167 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001168 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerafa2d632015-05-26 16:39:13 -07001169
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001170 GRPC_API_TRACE("grpc_server_cancel_all_calls(server=%p)", 1, (server));
1171
Craig Tillera82950e2015-09-22 12:33:20 -07001172 gpr_mu_lock(&server->mu_global);
1173 channel_broadcaster_init(server, &broadcaster);
1174 gpr_mu_unlock(&server->mu_global);
Craig Tillerafa2d632015-05-26 16:39:13 -07001175
Craig Tillera82950e2015-09-22 12:33:20 -07001176 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 0, 1);
1177 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerafa2d632015-05-26 16:39:13 -07001178}
1179
Craig Tillera82950e2015-09-22 12:33:20 -07001180void grpc_server_destroy(grpc_server *server) {
Craig Tilleraec96aa2015-04-07 14:32:15 -07001181 listener *l;
Craig Tillerf5768a62015-09-22 10:54:34 -07001182 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller872af022015-04-24 15:57:52 -07001183
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001184 GRPC_API_TRACE("grpc_server_destroy(server=%p)", 1, (server));
1185
Craig Tillera82950e2015-09-22 12:33:20 -07001186 gpr_mu_lock(&server->mu_global);
1187 GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners);
1188 GPR_ASSERT(server->listeners_destroyed == num_listeners(server));
Craig Tilleraec96aa2015-04-07 14:32:15 -07001189
Craig Tillera82950e2015-09-22 12:33:20 -07001190 while (server->listeners) {
1191 l = server->listeners;
1192 server->listeners = l->next;
1193 gpr_free(l);
1194 }
Craig Tilleraec96aa2015-04-07 14:32:15 -07001195
Craig Tillera82950e2015-09-22 12:33:20 -07001196 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001197
Craig Tillera82950e2015-09-22 12:33:20 -07001198 server_unref(&exec_ctx, server);
1199 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001200}
1201
Craig Tillera82950e2015-09-22 12:33:20 -07001202void grpc_server_add_listener(
1203 grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1204 void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1205 grpc_pollset **pollsets, size_t pollset_count),
1206 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1207 grpc_closure *on_done)) {
1208 listener *l = gpr_malloc(sizeof(listener));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001209 l->arg = arg;
1210 l->start = start;
1211 l->destroy = destroy;
1212 l->next = server->listeners;
1213 server->listeners = l;
1214}
1215
Craig Tillera82950e2015-09-22 12:33:20 -07001216static grpc_call_error queue_call_request(grpc_exec_ctx *exec_ctx,
1217 grpc_server *server,
1218 requested_call *rc) {
Yang Gaoeb8e7cd2015-02-11 11:43:40 -08001219 call_data *calld = NULL;
Craig Tillerb9d35962015-09-11 13:31:16 -07001220 request_matcher *rm = NULL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001221 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -07001222 if (gpr_atm_acq_load(&server->shutdown_flag)) {
1223 fail_call(exec_ctx, server, rc);
1224 return GRPC_CALL_OK;
1225 }
1226 request_id = gpr_stack_lockfree_pop(server->request_freelist);
1227 if (request_id == -1) {
1228 /* out of request ids: just fail this one */
1229 fail_call(exec_ctx, server, rc);
1230 return GRPC_CALL_OK;
1231 }
1232 switch (rc->type) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001233 case BATCH_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001234 rm = &server->unregistered_request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001235 break;
1236 case REGISTERED_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001237 rm = &rc->data.registered.registered_method->request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001238 break;
Craig Tillera82950e2015-09-22 12:33:20 -07001239 }
Craig Tiller45724b32015-09-22 10:42:19 -07001240 server->requested_calls[request_id] = *rc;
Craig Tillera82950e2015-09-22 12:33:20 -07001241 gpr_free(rc);
Craig Tillerb9d35962015-09-11 13:31:16 -07001242 if (gpr_stack_lockfree_push(rm->requests, request_id)) {
Craig Tillera82950e2015-09-22 12:33:20 -07001243 /* this was the first queued request: we need to lock and start
1244 matching calls */
1245 gpr_mu_lock(&server->mu_call);
Craig Tillerb9d35962015-09-11 13:31:16 -07001246 while ((calld = rm->pending_head) != NULL) {
1247 request_id = gpr_stack_lockfree_pop(rm->requests);
Craig Tillera82950e2015-09-22 12:33:20 -07001248 if (request_id == -1) break;
Craig Tillerb9d35962015-09-11 13:31:16 -07001249 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -07001250 gpr_mu_unlock(&server->mu_call);
1251 gpr_mu_lock(&calld->mu_state);
1252 if (calld->state == ZOMBIED) {
1253 gpr_mu_unlock(&calld->mu_state);
1254 grpc_closure_init(
1255 &calld->kill_zombie_closure, kill_zombie,
1256 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tiller6c396862016-01-28 13:53:40 -08001257 grpc_exec_ctx_enqueue(exec_ctx, &calld->kill_zombie_closure, true,
1258 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001259 } else {
1260 GPR_ASSERT(calld->state == PENDING);
1261 calld->state = ACTIVATED;
1262 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -07001263 publish_call(exec_ctx, server, calld,
1264 &server->requested_calls[request_id]);
Craig Tillera82950e2015-09-22 12:33:20 -07001265 }
1266 gpr_mu_lock(&server->mu_call);
Craig Tiller45724b32015-09-22 10:42:19 -07001267 }
Craig Tillera82950e2015-09-22 12:33:20 -07001268 gpr_mu_unlock(&server->mu_call);
1269 }
Craig Tiller6a006ce2015-07-13 16:25:40 -07001270 return GRPC_CALL_OK;
Craig Tillercce17ac2015-01-20 09:29:28 -08001271}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001272
Craig Tillera82950e2015-09-22 12:33:20 -07001273grpc_call_error grpc_server_request_call(
1274 grpc_server *server, grpc_call **call, grpc_call_details *details,
1275 grpc_metadata_array *initial_metadata,
1276 grpc_completion_queue *cq_bound_to_call,
1277 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001278 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001279 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001280 requested_call *rc = gpr_malloc(sizeof(*rc));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001281 GRPC_API_TRACE(
1282 "grpc_server_request_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001283 "server=%p, call=%p, details=%p, initial_metadata=%p, "
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001284 "cq_bound_to_call=%p, cq_for_notification=%p, tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001285 7, (server, call, details, initial_metadata, cq_bound_to_call,
1286 cq_for_notification, tag));
Craig Tillera82950e2015-09-22 12:33:20 -07001287 if (!grpc_cq_is_server_cq(cq_for_notification)) {
1288 gpr_free(rc);
1289 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1290 goto done;
1291 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001292 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller9928d392015-08-18 09:40:24 -07001293 details->reserved = NULL;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001294 rc->type = BATCH_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001295 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001296 rc->tag = tag;
1297 rc->cq_bound_to_call = cq_bound_to_call;
1298 rc->cq_for_notification = cq_for_notification;
1299 rc->call = call;
1300 rc->data.batch.details = details;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001301 rc->initial_metadata = initial_metadata;
Craig Tillera82950e2015-09-22 12:33:20 -07001302 error = queue_call_request(&exec_ctx, server, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001303done:
Craig Tillera82950e2015-09-22 12:33:20 -07001304 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001305 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001306}
1307
Craig Tillera82950e2015-09-22 12:33:20 -07001308grpc_call_error grpc_server_request_registered_call(
Craig Tillerb9d35962015-09-11 13:31:16 -07001309 grpc_server *server, void *rmp, grpc_call **call, gpr_timespec *deadline,
Craig Tillera82950e2015-09-22 12:33:20 -07001310 grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload,
1311 grpc_completion_queue *cq_bound_to_call,
1312 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001313 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001314 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001315 requested_call *rc = gpr_malloc(sizeof(*rc));
Craig Tillerb9d35962015-09-11 13:31:16 -07001316 registered_method *rm = rmp;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001317 GRPC_API_TRACE(
1318 "grpc_server_request_registered_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001319 "server=%p, rmp=%p, call=%p, deadline=%p, initial_metadata=%p, "
1320 "optional_payload=%p, cq_bound_to_call=%p, cq_for_notification=%p, "
1321 "tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001322 9, (server, rmp, call, deadline, initial_metadata, optional_payload,
1323 cq_bound_to_call, cq_for_notification, tag));
Craig Tillera82950e2015-09-22 12:33:20 -07001324 if (!grpc_cq_is_server_cq(cq_for_notification)) {
1325 gpr_free(rc);
1326 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1327 goto done;
1328 }
Craig Tiller06cb1a92016-04-04 08:10:47 -07001329 if ((optional_payload == NULL) !=
1330 (rm->payload_handling == GRPC_SRM_PAYLOAD_NONE)) {
1331 gpr_free(rc);
1332 error = GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH;
1333 goto done;
1334 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001335 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller97fc6a32015-07-08 15:31:35 -07001336 rc->type = REGISTERED_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001337 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001338 rc->tag = tag;
1339 rc->cq_bound_to_call = cq_bound_to_call;
1340 rc->cq_for_notification = cq_for_notification;
1341 rc->call = call;
Craig Tillerb9d35962015-09-11 13:31:16 -07001342 rc->data.registered.registered_method = rm;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001343 rc->data.registered.deadline = deadline;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001344 rc->initial_metadata = initial_metadata;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001345 rc->data.registered.optional_payload = optional_payload;
Craig Tillera82950e2015-09-22 12:33:20 -07001346 error = queue_call_request(&exec_ctx, server, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001347done:
Craig Tillera82950e2015-09-22 12:33:20 -07001348 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001349 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001350}
1351
Craig Tillera82950e2015-09-22 12:33:20 -07001352static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
1353 requested_call *rc) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -07001354 *rc->call = NULL;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001355 rc->initial_metadata->count = 0;
1356
Craig Tillera82950e2015-09-22 12:33:20 -07001357 server_ref(server);
1358 grpc_cq_end_op(exec_ctx, rc->cq_for_notification, rc->tag, 0,
1359 done_request_event, rc, &rc->completion);
Craig Tiller24be0f72015-02-10 14:04:22 -08001360}
1361
Craig Tillera82950e2015-09-22 12:33:20 -07001362const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001363 return server->channel_args;
Craig Tiller190d3602015-02-18 09:23:38 -08001364}
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001365
Craig Tillera82950e2015-09-22 12:33:20 -07001366int grpc_server_has_open_connections(grpc_server *server) {
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001367 int r;
Craig Tillera82950e2015-09-22 12:33:20 -07001368 gpr_mu_lock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001369 r = server->root_channel_data.next != &server->root_channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -07001370 gpr_mu_unlock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001371 return r;
1372}