blob: e7e45dcc6d8c3ef8e832f01c163ed9aed2e3046f [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,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700238 requested_call *rc, grpc_error *error);
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++) {
Craig Tiller71f96652016-05-11 23:17:45 -0700302 send_shutdown(exec_ctx, cb->channels[i], send_goaway,
303 GRPC_ERROR_REF(force_disconnect));
Craig Tillera82950e2015-09-22 12:33:20 -0700304 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, cb->channels[i], "broadcast");
305 }
306 gpr_free(cb->channels);
Craig Tiller71f96652016-05-11 23:17:45 -0700307 GRPC_ERROR_UNREF(force_disconnect);
Craig Tillerff3ae682015-06-29 17:44:04 -0700308}
309
Craig Tiller729b35a2015-07-13 12:36:47 -0700310/*
311 * request_matcher
312 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700313
Craig Tiller88512692016-04-04 09:32:52 -0700314static void request_matcher_init(request_matcher *rm, size_t entries,
315 grpc_server *server) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700316 memset(rm, 0, sizeof(*rm));
Craig Tiller88512692016-04-04 09:32:52 -0700317 rm->server = server;
Craig Tillerb9d35962015-09-11 13:31:16 -0700318 rm->requests = gpr_stack_lockfree_create(entries);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800319}
320
Craig Tillerb9d35962015-09-11 13:31:16 -0700321static void request_matcher_destroy(request_matcher *rm) {
322 GPR_ASSERT(gpr_stack_lockfree_pop(rm->requests) == -1);
323 gpr_stack_lockfree_destroy(rm->requests);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800324}
325
Craig Tillerf51457b2016-05-03 17:06:32 -0700326static void kill_zombie(grpc_exec_ctx *exec_ctx, void *elem,
327 grpc_error *error) {
Craig Tillera82950e2015-09-22 12:33:20 -0700328 grpc_call_destroy(grpc_call_from_top_element(elem));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800329}
330
Craig Tiller8dc09712015-09-24 13:58:16 -0700331static void request_matcher_zombify_all_pending_calls(grpc_exec_ctx *exec_ctx,
332 request_matcher *rm) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700333 while (rm->pending_head) {
334 call_data *calld = rm->pending_head;
335 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -0700336 gpr_mu_lock(&calld->mu_state);
337 calld->state = ZOMBIED;
338 gpr_mu_unlock(&calld->mu_state);
339 grpc_closure_init(
340 &calld->kill_zombie_closure, kill_zombie,
341 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tillerf51457b2016-05-03 17:06:32 -0700342 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
343 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700344 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800345}
346
Craig Tillera82950e2015-09-22 12:33:20 -0700347static void request_matcher_kill_requests(grpc_exec_ctx *exec_ctx,
348 grpc_server *server,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700349 request_matcher *rm,
350 grpc_error *error) {
Craig Tiller1191e212015-07-30 14:49:02 -0700351 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -0700352 while ((request_id = gpr_stack_lockfree_pop(rm->requests)) != -1) {
Craig Tillercae4b1b2016-05-10 09:11:09 -0700353 fail_call(exec_ctx, server, &server->requested_calls[request_id],
354 GRPC_ERROR_REF(error));
Craig Tillera82950e2015-09-22 12:33:20 -0700355 }
Craig Tillercae4b1b2016-05-10 09:11:09 -0700356 GRPC_ERROR_UNREF(error);
Craig Tiller1191e212015-07-30 14:49:02 -0700357}
358
Craig Tiller729b35a2015-07-13 12:36:47 -0700359/*
360 * server proper
361 */
362
Craig Tillera82950e2015-09-22 12:33:20 -0700363static void server_ref(grpc_server *server) {
364 gpr_ref(&server->internal_refcount);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800365}
366
Craig Tillera82950e2015-09-22 12:33:20 -0700367static void server_delete(grpc_exec_ctx *exec_ctx, grpc_server *server) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800368 registered_method *rm;
Craig Tiller89504612015-04-27 11:48:46 -0700369 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700370 grpc_channel_args_destroy(server->channel_args);
371 gpr_mu_destroy(&server->mu_global);
372 gpr_mu_destroy(&server->mu_call);
Craig Tillera82950e2015-09-22 12:33:20 -0700373 while ((rm = server->registered_methods) != NULL) {
374 server->registered_methods = rm->next;
375 request_matcher_destroy(&rm->request_matcher);
376 gpr_free(rm->method);
377 gpr_free(rm->host);
378 gpr_free(rm);
379 }
380 for (i = 0; i < server->cq_count; i++) {
381 GRPC_CQ_INTERNAL_UNREF(server->cqs[i], "server");
382 }
383 request_matcher_destroy(&server->unregistered_request_matcher);
384 gpr_stack_lockfree_destroy(server->request_freelist);
385 gpr_free(server->cqs);
386 gpr_free(server->pollsets);
387 gpr_free(server->shutdown_tags);
388 gpr_free(server->requested_calls);
389 gpr_free(server);
Craig Tilleree945e82015-05-26 16:15:34 -0700390}
391
Craig Tillera82950e2015-09-22 12:33:20 -0700392static void server_unref(grpc_exec_ctx *exec_ctx, grpc_server *server) {
393 if (gpr_unref(&server->internal_refcount)) {
394 server_delete(exec_ctx, server);
395 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800396}
397
Craig Tillera82950e2015-09-22 12:33:20 -0700398static int is_channel_orphaned(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800399 return chand->next == chand;
400}
401
Craig Tillera82950e2015-09-22 12:33:20 -0700402static void orphan_channel(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800403 chand->next->prev = chand->prev;
404 chand->prev->next = chand->next;
405 chand->next = chand->prev = chand;
406}
407
Craig Tillera82950e2015-09-22 12:33:20 -0700408static void finish_destroy_channel(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700409 grpc_error *error) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800410 channel_data *chand = cd;
411 grpc_server *server = chand->server;
Craig Tillera82950e2015-09-22 12:33:20 -0700412 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "server");
413 server_unref(exec_ctx, server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800414}
415
Craig Tillera82950e2015-09-22 12:33:20 -0700416static void destroy_channel(grpc_exec_ctx *exec_ctx, channel_data *chand) {
417 if (is_channel_orphaned(chand)) return;
418 GPR_ASSERT(chand->server != NULL);
419 orphan_channel(chand);
420 server_ref(chand->server);
421 maybe_finish_shutdown(exec_ctx, chand->server);
David Garcia Quintas284488b2015-05-28 16:27:39 -0700422 chand->finish_destroy_channel_closure.cb = finish_destroy_channel;
423 chand->finish_destroy_channel_closure.cb_arg = chand;
Craig Tillerd7f12e32016-03-03 10:08:31 -0800424
425 grpc_transport_op op;
426 memset(&op, 0, sizeof(op));
427 op.set_accept_stream = true;
428 op.on_consumed = &chand->finish_destroy_channel_closure;
429 grpc_channel_next_op(exec_ctx,
430 grpc_channel_stack_element(
431 grpc_channel_get_channel_stack(chand->channel), 0),
432 &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800433}
434
Craig Tiller06cb1a92016-04-04 08:10:47 -0700435static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) {
436 gpr_slice slice = value->slice;
437 size_t len = GPR_SLICE_LENGTH(slice);
Craig Tiller6a006ce2015-07-13 16:25:40 -0700438
Craig Tiller06cb1a92016-04-04 08:10:47 -0700439 if (len + 1 > *capacity) {
440 *capacity = GPR_MAX(len + 1, *capacity * 2);
441 *dest = gpr_realloc(*dest, *capacity);
442 }
443 memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1);
444}
445
Craig Tiller88512692016-04-04 09:32:52 -0700446static void done_request_event(grpc_exec_ctx *exec_ctx, void *req,
447 grpc_cq_completion *c) {
448 requested_call *rc = req;
449 grpc_server *server = rc->server;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700450
Craig Tiller88512692016-04-04 09:32:52 -0700451 if (rc >= server->requested_calls &&
452 rc < server->requested_calls + server->max_requested_calls) {
453 GPR_ASSERT(rc - server->requested_calls <= INT_MAX);
454 gpr_stack_lockfree_push(server->request_freelist,
455 (int)(rc - server->requested_calls));
456 } else {
457 gpr_free(req);
458 }
Craig Tiller06cb1a92016-04-04 08:10:47 -0700459
Craig Tiller88512692016-04-04 09:32:52 -0700460 server_unref(exec_ctx, server);
461}
Craig Tiller06cb1a92016-04-04 08:10:47 -0700462
Craig Tiller88512692016-04-04 09:32:52 -0700463static void publish_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
464 call_data *calld, requested_call *rc) {
Craig Tiller06cb1a92016-04-04 08:10:47 -0700465 grpc_call_set_completion_queue(exec_ctx, calld->call, rc->cq_bound_to_call);
Craig Tiller88512692016-04-04 09:32:52 -0700466 grpc_call *call = calld->call;
467 *rc->call = call;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700468 calld->cq_new = rc->cq_for_notification;
469 GPR_SWAP(grpc_metadata_array, *rc->initial_metadata, calld->initial_metadata);
470 switch (rc->type) {
471 case BATCH_CALL:
472 GPR_ASSERT(calld->host != NULL);
473 GPR_ASSERT(calld->path != NULL);
474 cpstr(&rc->data.batch.details->host,
475 &rc->data.batch.details->host_capacity, calld->host);
476 cpstr(&rc->data.batch.details->method,
477 &rc->data.batch.details->method_capacity, calld->path);
478 rc->data.batch.details->deadline = calld->deadline;
479 rc->data.batch.details->flags =
480 0 | (calld->recv_idempotent_request
481 ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
482 : 0);
483 break;
484 case REGISTERED_CALL:
485 *rc->data.registered.deadline = calld->deadline;
486 if (rc->data.registered.optional_payload) {
Craig Tiller88512692016-04-04 09:32:52 -0700487 *rc->data.registered.optional_payload = calld->payload;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700488 }
489 break;
490 default:
491 GPR_UNREACHABLE_CODE(return );
492 }
493
Craig Tiller88512692016-04-04 09:32:52 -0700494 grpc_call_element *elem =
495 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
496 channel_data *chand = elem->channel_data;
497 server_ref(chand->server);
Craig Tillerf51457b2016-05-03 17:06:32 -0700498 grpc_cq_end_op(exec_ctx, calld->cq_new, rc->tag, GRPC_ERROR_NONE,
499 done_request_event, rc, &rc->completion);
Craig Tiller06cb1a92016-04-04 08:10:47 -0700500}
501
Craig Tillerf51457b2016-05-03 17:06:32 -0700502static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *arg,
503 grpc_error *error) {
Craig Tiller88512692016-04-04 09:32:52 -0700504 call_data *calld = arg;
505 request_matcher *rm = calld->request_matcher;
506 grpc_server *server = rm->server;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700507
Craig Tillerf51457b2016-05-03 17:06:32 -0700508 if (error != GRPC_ERROR_NONE || gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tillera82950e2015-09-22 12:33:20 -0700509 gpr_mu_lock(&calld->mu_state);
510 calld->state = ZOMBIED;
511 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -0700512 grpc_closure_init(
513 &calld->kill_zombie_closure, kill_zombie,
514 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tillerf51457b2016-05-03 17:06:32 -0700515 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, error, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700516 return;
517 }
Craig Tiller45724b32015-09-22 10:42:19 -0700518
Craig Tiller88512692016-04-04 09:32:52 -0700519 int request_id = gpr_stack_lockfree_pop(rm->requests);
Craig Tillera82950e2015-09-22 12:33:20 -0700520 if (request_id == -1) {
521 gpr_mu_lock(&server->mu_call);
522 gpr_mu_lock(&calld->mu_state);
523 calld->state = PENDING;
524 gpr_mu_unlock(&calld->mu_state);
Craig Tillerb9d35962015-09-11 13:31:16 -0700525 if (rm->pending_head == NULL) {
526 rm->pending_tail = rm->pending_head = calld;
Craig Tillera82950e2015-09-22 12:33:20 -0700527 } else {
Craig Tillerb9d35962015-09-11 13:31:16 -0700528 rm->pending_tail->pending_next = calld;
529 rm->pending_tail = calld;
Craig Tiller45724b32015-09-22 10:42:19 -0700530 }
Craig Tillera82950e2015-09-22 12:33:20 -0700531 calld->pending_next = NULL;
532 gpr_mu_unlock(&server->mu_call);
533 } else {
534 gpr_mu_lock(&calld->mu_state);
535 calld->state = ACTIVATED;
536 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -0700537 publish_call(exec_ctx, server, calld, &server->requested_calls[request_id]);
538 }
539}
540
541static void finish_start_new_rpc(
542 grpc_exec_ctx *exec_ctx, grpc_server *server, grpc_call_element *elem,
543 request_matcher *rm,
544 grpc_server_register_method_payload_handling payload_handling) {
545 call_data *calld = elem->call_data;
546
547 if (gpr_atm_acq_load(&server->shutdown_flag)) {
548 gpr_mu_lock(&calld->mu_state);
549 calld->state = ZOMBIED;
550 gpr_mu_unlock(&calld->mu_state);
551 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
Craig Tillerf51457b2016-05-03 17:06:32 -0700552 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
553 NULL);
Craig Tiller88512692016-04-04 09:32:52 -0700554 return;
555 }
556
557 calld->request_matcher = rm;
558
559 switch (payload_handling) {
560 case GRPC_SRM_PAYLOAD_NONE:
Craig Tillerf51457b2016-05-03 17:06:32 -0700561 publish_new_rpc(exec_ctx, calld, GRPC_ERROR_NONE);
Craig Tiller88512692016-04-04 09:32:52 -0700562 break;
563 case GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER: {
564 grpc_op op;
565 memset(&op, 0, sizeof(op));
566 op.op = GRPC_OP_RECV_MESSAGE;
567 op.data.recv_message = &calld->payload;
568 grpc_closure_init(&calld->publish, publish_new_rpc, calld);
569 grpc_call_start_batch_and_execute(exec_ctx, calld->call, &op, 1,
570 &calld->publish);
571 break;
572 }
Craig Tillera82950e2015-09-22 12:33:20 -0700573 }
Craig Tiller04cc8be2015-02-10 16:11:22 -0800574}
575
Craig Tillera82950e2015-09-22 12:33:20 -0700576static void start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800577 channel_data *chand = elem->channel_data;
578 call_data *calld = elem->call_data;
579 grpc_server *server = chand->server;
Craig Tiller7536af02015-12-22 13:49:30 -0800580 uint32_t i;
581 uint32_t hash;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800582 channel_registered_method *rm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800583
Craig Tillera82950e2015-09-22 12:33:20 -0700584 if (chand->registered_methods && calld->path && calld->host) {
585 /* TODO(ctiller): unify these two searches */
586 /* check for an exact match with host */
587 hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash);
588 for (i = 0; i <= chand->registered_method_max_probes; i++) {
589 rm = &chand->registered_methods[(hash + i) %
590 chand->registered_method_slots];
591 if (!rm) break;
592 if (rm->host != calld->host) continue;
593 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800594 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
595 !calld->recv_idempotent_request)
596 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700597 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700598 &rm->server_registered_method->request_matcher,
599 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700600 return;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800601 }
Craig Tillera82950e2015-09-22 12:33:20 -0700602 /* check for a wildcard method definition (no host set) */
603 hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash);
604 for (i = 0; i <= chand->registered_method_max_probes; i++) {
605 rm = &chand->registered_methods[(hash + i) %
606 chand->registered_method_slots];
607 if (!rm) break;
608 if (rm->host != NULL) continue;
609 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800610 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
611 !calld->recv_idempotent_request)
612 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700613 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700614 &rm->server_registered_method->request_matcher,
615 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700616 return;
617 }
618 }
619 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700620 &server->unregistered_request_matcher,
621 GRPC_SRM_PAYLOAD_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800622}
623
Craig Tillera82950e2015-09-22 12:33:20 -0700624static int num_listeners(grpc_server *server) {
Craig Tilleree945e82015-05-26 16:15:34 -0700625 listener *l;
626 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700627 for (l = server->listeners; l; l = l->next) {
628 n++;
629 }
Craig Tilleree945e82015-05-26 16:15:34 -0700630 return n;
631}
632
Craig Tillera82950e2015-09-22 12:33:20 -0700633static void done_shutdown_event(grpc_exec_ctx *exec_ctx, void *server,
634 grpc_cq_completion *completion) {
635 server_unref(exec_ctx, server);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700636}
637
Craig Tillera82950e2015-09-22 12:33:20 -0700638static int num_channels(grpc_server *server) {
Craig Tillerab54f792015-07-08 08:34:20 -0700639 channel_data *chand;
640 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700641 for (chand = server->root_channel_data.next;
642 chand != &server->root_channel_data; chand = chand->next) {
643 n++;
644 }
Craig Tillerab54f792015-07-08 08:34:20 -0700645 return n;
646}
647
Craig Tillera82950e2015-09-22 12:33:20 -0700648static void kill_pending_work_locked(grpc_exec_ctx *exec_ctx,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700649 grpc_server *server, grpc_error *error) {
Craig Tiller1191e212015-07-30 14:49:02 -0700650 registered_method *rm;
Craig Tillera82950e2015-09-22 12:33:20 -0700651 request_matcher_kill_requests(exec_ctx, server,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700652 &server->unregistered_request_matcher,
653 GRPC_ERROR_REF(error));
Craig Tillera82950e2015-09-22 12:33:20 -0700654 request_matcher_zombify_all_pending_calls(
655 exec_ctx, &server->unregistered_request_matcher);
656 for (rm = server->registered_methods; rm; rm = rm->next) {
Craig Tillercae4b1b2016-05-10 09:11:09 -0700657 request_matcher_kill_requests(exec_ctx, server, &rm->request_matcher,
658 GRPC_ERROR_REF(error));
Craig Tillera82950e2015-09-22 12:33:20 -0700659 request_matcher_zombify_all_pending_calls(exec_ctx, &rm->request_matcher);
660 }
Craig Tillercae4b1b2016-05-10 09:11:09 -0700661 GRPC_ERROR_UNREF(error);
Craig Tillerdc627722015-05-26 15:27:02 -0700662}
663
Craig Tillera82950e2015-09-22 12:33:20 -0700664static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx,
665 grpc_server *server) {
Craig Tiller45724b32015-09-22 10:42:19 -0700666 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700667 if (!gpr_atm_acq_load(&server->shutdown_flag) || server->shutdown_published) {
668 return;
669 }
Craig Tiller45724b32015-09-22 10:42:19 -0700670
Craig Tillercae4b1b2016-05-10 09:11:09 -0700671 kill_pending_work_locked(exec_ctx, server,
672 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tiller45724b32015-09-22 10:42:19 -0700673
Craig Tillera82950e2015-09-22 12:33:20 -0700674 if (server->root_channel_data.next != &server->root_channel_data ||
675 server->listeners_destroyed < num_listeners(server)) {
676 if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME),
677 server->last_shutdown_message_time),
678 gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
679 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
680 gpr_log(GPR_DEBUG,
681 "Waiting for %d channels and %d/%d listeners to be destroyed"
682 " before shutting down server",
683 num_channels(server),
684 num_listeners(server) - server->listeners_destroyed,
685 num_listeners(server));
Craig Tiller45724b32015-09-22 10:42:19 -0700686 }
Craig Tillera82950e2015-09-22 12:33:20 -0700687 return;
688 }
Craig Tiller45724b32015-09-22 10:42:19 -0700689 server->shutdown_published = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700690 for (i = 0; i < server->num_shutdown_tags; i++) {
691 server_ref(server);
692 grpc_cq_end_op(exec_ctx, server->shutdown_tags[i].cq,
Craig Tillerf51457b2016-05-03 17:06:32 -0700693 server->shutdown_tags[i].tag, GRPC_ERROR_NONE,
694 done_shutdown_event, server,
Craig Tillera82950e2015-09-22 12:33:20 -0700695 &server->shutdown_tags[i].completion);
696 }
Craig Tiller45724b32015-09-22 10:42:19 -0700697}
698
Craig Tillera82950e2015-09-22 12:33:20 -0700699static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700700 grpc_call_element *elem = user_data;
Craig Tillercce17ac2015-01-20 09:29:28 -0800701 call_data *calld = elem->call_data;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800702 if (md->key == GRPC_MDSTR_PATH) {
Craig Tiller9cac2a12016-04-04 20:56:56 -0700703 if (calld->path == NULL) {
704 calld->path = GRPC_MDSTR_REF(md->value);
705 }
Craig Tillera82950e2015-09-22 12:33:20 -0700706 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800707 } else if (md->key == GRPC_MDSTR_AUTHORITY) {
Craig Tiller9cac2a12016-04-04 20:56:56 -0700708 if (calld->host == NULL) {
709 calld->host = GRPC_MDSTR_REF(md->value);
710 }
Craig Tillera82950e2015-09-22 12:33:20 -0700711 return NULL;
712 }
Craig Tiller6902ad22015-04-16 08:01:49 -0700713 return md;
714}
715
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800716static void server_on_recv_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700717 grpc_error *error) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700718 grpc_call_element *elem = ptr;
Craig Tiller6902ad22015-04-16 08:01:49 -0700719 call_data *calld = elem->call_data;
Craig Tiller94329d02015-07-23 09:52:11 -0700720 gpr_timespec op_deadline;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700721
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800722 grpc_metadata_batch_filter(calld->recv_initial_metadata, server_filter, elem);
723 op_deadline = calld->recv_initial_metadata->deadline;
724 if (0 != gpr_time_cmp(op_deadline, gpr_inf_future(op_deadline.clock_type))) {
725 calld->deadline = op_deadline;
726 }
727 if (calld->host && calld->path) {
728 /* do nothing */
729 } else {
Craig Tillerf51457b2016-05-03 17:06:32 -0700730 error =
731 GRPC_ERROR_CREATE_REFERENCING("Missing :authority or :path", &error, 1);
Craig Tillera82950e2015-09-22 12:33:20 -0700732 }
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700733
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800734 calld->on_done_recv_initial_metadata->cb(
Craig Tillerf51457b2016-05-03 17:06:32 -0700735 exec_ctx, calld->on_done_recv_initial_metadata->cb_arg, error);
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700736}
737
Craig Tillera82950e2015-09-22 12:33:20 -0700738static void server_mutate_op(grpc_call_element *elem,
739 grpc_transport_stream_op *op) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700740 call_data *calld = elem->call_data;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700741
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800742 if (op->recv_initial_metadata != NULL) {
Craig Tiller4d40ba32016-03-09 17:48:40 -0800743 GPR_ASSERT(op->recv_idempotent_request == NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800744 calld->recv_initial_metadata = op->recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800745 calld->on_done_recv_initial_metadata = op->recv_initial_metadata_ready;
746 op->recv_initial_metadata_ready = &calld->server_on_recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800747 op->recv_idempotent_request = &calld->recv_idempotent_request;
Craig Tillera82950e2015-09-22 12:33:20 -0700748 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700749}
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700750
Craig Tillera82950e2015-09-22 12:33:20 -0700751static void server_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
752 grpc_call_element *elem,
753 grpc_transport_stream_op *op) {
754 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
755 server_mutate_op(elem, op);
756 grpc_call_next_op(exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800757}
758
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800759static void got_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700760 grpc_error *error) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800761 grpc_call_element *elem = ptr;
762 call_data *calld = elem->call_data;
Craig Tillerf51457b2016-05-03 17:06:32 -0700763 if (error == GRPC_ERROR_NONE) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800764 start_new_rpc(exec_ctx, elem);
765 } else {
766 gpr_mu_lock(&calld->mu_state);
767 if (calld->state == NOT_STARTED) {
768 calld->state = ZOMBIED;
769 gpr_mu_unlock(&calld->mu_state);
770 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
Craig Tillerf51457b2016-05-03 17:06:32 -0700771 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
772 NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800773 } else if (calld->state == PENDING) {
774 calld->state = ZOMBIED;
775 gpr_mu_unlock(&calld->mu_state);
776 /* zombied call will be destroyed when it's removed from the pending
777 queue... later */
778 } else {
779 gpr_mu_unlock(&calld->mu_state);
780 }
781 }
782}
783
784static void accept_stream(grpc_exec_ctx *exec_ctx, void *cd,
785 grpc_transport *transport,
Craig Tillera82950e2015-09-22 12:33:20 -0700786 const void *transport_server_data) {
Craig Tillere039f032015-06-25 12:54:23 -0700787 channel_data *chand = cd;
788 /* create a call */
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800789 grpc_call *call =
790 grpc_call_create(chand->channel, NULL, 0, NULL, transport_server_data,
791 NULL, 0, gpr_inf_future(GPR_CLOCK_MONOTONIC));
792 grpc_call_element *elem =
793 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
794 call_data *calld = elem->call_data;
795 grpc_op op;
796 memset(&op, 0, sizeof(op));
797 op.op = GRPC_OP_RECV_INITIAL_METADATA;
798 op.data.recv_initial_metadata = &calld->initial_metadata;
799 grpc_closure_init(&calld->got_initial_metadata, got_initial_metadata, elem);
800 grpc_call_start_batch_and_execute(exec_ctx, call, &op, 1,
801 &calld->got_initial_metadata);
Craig Tillere039f032015-06-25 12:54:23 -0700802}
803
Craig Tillera82950e2015-09-22 12:33:20 -0700804static void channel_connectivity_changed(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700805 grpc_error *error) {
Craig Tillere039f032015-06-25 12:54:23 -0700806 channel_data *chand = cd;
807 grpc_server *server = chand->server;
Craig Tillera82950e2015-09-22 12:33:20 -0700808 if (chand->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE) {
809 grpc_transport_op op;
810 memset(&op, 0, sizeof(op));
811 op.on_connectivity_state_change = &chand->channel_connectivity_changed,
812 op.connectivity_state = &chand->connectivity_state;
813 grpc_channel_next_op(exec_ctx,
814 grpc_channel_stack_element(
815 grpc_channel_get_channel_stack(chand->channel), 0),
816 &op);
817 } else {
818 gpr_mu_lock(&server->mu_global);
819 destroy_channel(exec_ctx, chand);
820 gpr_mu_unlock(&server->mu_global);
821 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "connectivity");
822 }
Craig Tillere039f032015-06-25 12:54:23 -0700823}
824
Craig Tillera82950e2015-09-22 12:33:20 -0700825static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800826 grpc_call_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800827 call_data *calld = elem->call_data;
828 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700829 memset(calld, 0, sizeof(call_data));
830 calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
831 calld->call = grpc_call_from_top_element(elem);
832 gpr_mu_init(&calld->mu_state);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800833
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800834 grpc_closure_init(&calld->server_on_recv_initial_metadata,
835 server_on_recv_initial_metadata, elem);
Craig Tiller1e6facb2015-06-11 22:47:11 -0700836
Craig Tillera82950e2015-09-22 12:33:20 -0700837 server_ref(chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800838}
839
Craig Tiller2c8063c2016-03-22 22:12:15 -0700840static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
841 void *ignored) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800842 channel_data *chand = elem->channel_data;
Craig Tillerdb7db992015-01-29 11:19:01 -0800843 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800844
Craig Tillera82950e2015-09-22 12:33:20 -0700845 GPR_ASSERT(calld->state != PENDING);
Craig Tiller092d8d12015-07-04 22:35:00 -0700846
Craig Tillera82950e2015-09-22 12:33:20 -0700847 if (calld->host) {
848 GRPC_MDSTR_UNREF(calld->host);
849 }
850 if (calld->path) {
851 GRPC_MDSTR_UNREF(calld->path);
852 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800853 grpc_metadata_array_destroy(&calld->initial_metadata);
Craig Tiller4df31a62015-01-30 09:44:31 -0800854
Craig Tillera82950e2015-09-22 12:33:20 -0700855 gpr_mu_destroy(&calld->mu_state);
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700856
Craig Tillera82950e2015-09-22 12:33:20 -0700857 server_unref(exec_ctx, chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800858}
859
Craig Tillera82950e2015-09-22 12:33:20 -0700860static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800861 grpc_channel_element *elem,
862 grpc_channel_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800863 channel_data *chand = elem->channel_data;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800864 GPR_ASSERT(args->is_first);
865 GPR_ASSERT(!args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800866 chand->server = NULL;
867 chand->channel = NULL;
868 chand->next = chand->prev = chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800869 chand->registered_methods = NULL;
Craig Tillere039f032015-06-25 12:54:23 -0700870 chand->connectivity_state = GRPC_CHANNEL_IDLE;
Craig Tillera82950e2015-09-22 12:33:20 -0700871 grpc_closure_init(&chand->channel_connectivity_changed,
872 channel_connectivity_changed, chand);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800873}
874
Craig Tillera82950e2015-09-22 12:33:20 -0700875static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
876 grpc_channel_element *elem) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800877 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800878 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700879 if (chand->registered_methods) {
880 for (i = 0; i < chand->registered_method_slots; i++) {
881 if (chand->registered_methods[i].method) {
882 GRPC_MDSTR_UNREF(chand->registered_methods[i].method);
883 }
884 if (chand->registered_methods[i].host) {
885 GRPC_MDSTR_UNREF(chand->registered_methods[i].host);
886 }
Craig Tillerec3257c2015-02-12 15:59:43 -0800887 }
Craig Tillera82950e2015-09-22 12:33:20 -0700888 gpr_free(chand->registered_methods);
889 }
890 if (chand->server) {
891 gpr_mu_lock(&chand->server->mu_global);
892 chand->next->prev = chand->prev;
893 chand->prev->next = chand->next;
894 chand->next = chand->prev = chand;
895 maybe_finish_shutdown(exec_ctx, chand->server);
896 gpr_mu_unlock(&chand->server->mu_global);
Craig Tillera82950e2015-09-22 12:33:20 -0700897 server_unref(exec_ctx, chand->server);
898 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800899}
900
Craig Tiller178edfa2016-02-17 20:54:46 -0800901const grpc_channel_filter grpc_server_top_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700902 server_start_transport_stream_op,
903 grpc_channel_next_op,
904 sizeof(call_data),
905 init_call_elem,
906 grpc_call_stack_ignore_set_pollset,
907 destroy_call_elem,
908 sizeof(channel_data),
909 init_channel_elem,
910 destroy_channel_elem,
911 grpc_call_next_get_peer,
912 "server",
Craig Tiller9f28ac22015-01-27 17:01:29 -0800913};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800914
Craig Tillera82950e2015-09-22 12:33:20 -0700915void grpc_server_register_completion_queue(grpc_server *server,
916 grpc_completion_queue *cq,
917 void *reserved) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800918 size_t i, n;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700919 GRPC_API_TRACE(
920 "grpc_server_register_completion_queue(server=%p, cq=%p, reserved=%p)", 3,
921 (server, cq, reserved));
Craig Tillera82950e2015-09-22 12:33:20 -0700922 GPR_ASSERT(!reserved);
923 for (i = 0; i < server->cq_count; i++) {
924 if (server->cqs[i] == cq) return;
925 }
926 GRPC_CQ_INTERNAL_REF(cq, "server");
927 grpc_cq_mark_server_cq(cq);
Craig Tiller20bc56d2015-02-12 09:02:56 -0800928 n = server->cq_count++;
Craig Tillera82950e2015-09-22 12:33:20 -0700929 server->cqs = gpr_realloc(server->cqs,
930 server->cq_count * sizeof(grpc_completion_queue *));
Craig Tiller20bc56d2015-02-12 09:02:56 -0800931 server->cqs[n] = cq;
932}
933
Craig Tiller178edfa2016-02-17 20:54:46 -0800934grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800935 size_t i;
Craig Tiller178edfa2016-02-17 20:54:46 -0800936
937 GRPC_API_TRACE("grpc_server_create(%p, %p)", 2, (args, reserved));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800938
Craig Tillera82950e2015-09-22 12:33:20 -0700939 grpc_server *server = gpr_malloc(sizeof(grpc_server));
Craig Tiller60fd3612015-03-05 16:24:22 -0800940
Craig Tillera82950e2015-09-22 12:33:20 -0700941 GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
Craig Tiller60fd3612015-03-05 16:24:22 -0800942
Craig Tillera82950e2015-09-22 12:33:20 -0700943 memset(server, 0, sizeof(grpc_server));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800944
Craig Tillera82950e2015-09-22 12:33:20 -0700945 gpr_mu_init(&server->mu_global);
946 gpr_mu_init(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800947
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800948 /* decremented by grpc_server_destroy */
Craig Tillera82950e2015-09-22 12:33:20 -0700949 gpr_ref_init(&server->internal_refcount, 1);
950 server->root_channel_data.next = server->root_channel_data.prev =
951 &server->root_channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800952
Craig Tiller6a006ce2015-07-13 16:25:40 -0700953 /* TODO(ctiller): expose a channel_arg for this */
954 server->max_requested_calls = 32768;
Craig Tillera82950e2015-09-22 12:33:20 -0700955 server->request_freelist =
956 gpr_stack_lockfree_create(server->max_requested_calls);
957 for (i = 0; i < (size_t)server->max_requested_calls; i++) {
958 gpr_stack_lockfree_push(server->request_freelist, (int)i);
959 }
960 request_matcher_init(&server->unregistered_request_matcher,
Craig Tiller88512692016-04-04 09:32:52 -0700961 server->max_requested_calls, server);
Craig Tillera82950e2015-09-22 12:33:20 -0700962 server->requested_calls = gpr_malloc(server->max_requested_calls *
963 sizeof(*server->requested_calls));
Craig Tiller729b35a2015-07-13 12:36:47 -0700964
Craig Tillera82950e2015-09-22 12:33:20 -0700965 server->channel_args = grpc_channel_args_copy(args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800966
967 return server;
968}
969
Craig Tillera82950e2015-09-22 12:33:20 -0700970static int streq(const char *a, const char *b) {
971 if (a == NULL && b == NULL) return 1;
972 if (a == NULL) return 0;
973 if (b == NULL) return 0;
974 return 0 == strcmp(a, b);
Craig Tiller24be0f72015-02-10 14:04:22 -0800975}
976
Craig Tiller06cb1a92016-04-04 08:10:47 -0700977void *grpc_server_register_method(
978 grpc_server *server, const char *method, const char *host,
979 grpc_server_register_method_payload_handling payload_handling,
980 uint32_t flags) {
Craig Tiller24be0f72015-02-10 14:04:22 -0800981 registered_method *m;
Craig Tillerb2906862016-03-10 06:50:07 -0800982 GRPC_API_TRACE(
983 "grpc_server_register_method(server=%p, method=%s, host=%s, "
984 "flags=0x%08x)",
985 4, (server, method, host, flags));
Craig Tillera82950e2015-09-22 12:33:20 -0700986 if (!method) {
987 gpr_log(GPR_ERROR,
988 "grpc_server_register_method method string cannot be NULL");
989 return NULL;
990 }
991 for (m = server->registered_methods; m; m = m->next) {
992 if (streq(m->method, method) && streq(m->host, host)) {
993 gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method,
994 host ? host : "*");
Craig Tiller24be0f72015-02-10 14:04:22 -0800995 return NULL;
996 }
Craig Tillera82950e2015-09-22 12:33:20 -0700997 }
Craig Tillerb2906862016-03-10 06:50:07 -0800998 if ((flags & ~GRPC_INITIAL_METADATA_USED_MASK) != 0) {
999 gpr_log(GPR_ERROR, "grpc_server_register_method invalid flags 0x%08x",
1000 flags);
1001 return NULL;
1002 }
Craig Tillera82950e2015-09-22 12:33:20 -07001003 m = gpr_malloc(sizeof(registered_method));
1004 memset(m, 0, sizeof(*m));
Craig Tiller88512692016-04-04 09:32:52 -07001005 request_matcher_init(&m->request_matcher, server->max_requested_calls,
1006 server);
Craig Tillera82950e2015-09-22 12:33:20 -07001007 m->method = gpr_strdup(method);
1008 m->host = gpr_strdup(host);
Craig Tiller24be0f72015-02-10 14:04:22 -08001009 m->next = server->registered_methods;
Craig Tiller5ada3d22016-04-04 08:30:59 -07001010 m->payload_handling = payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -08001011 m->flags = flags;
Craig Tiller24be0f72015-02-10 14:04:22 -08001012 server->registered_methods = m;
1013 return m;
1014}
1015
Craig Tillera82950e2015-09-22 12:33:20 -07001016void grpc_server_start(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001017 listener *l;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001018 size_t i;
Craig Tillerf5768a62015-09-22 10:54:34 -07001019 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001020
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001021 GRPC_API_TRACE("grpc_server_start(server=%p)", 1, (server));
1022
Craig Tillera82950e2015-09-22 12:33:20 -07001023 server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count);
1024 for (i = 0; i < server->cq_count; i++) {
1025 server->pollsets[i] = grpc_cq_pollset(server->cqs[i]);
1026 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001027
Craig Tillera82950e2015-09-22 12:33:20 -07001028 for (l = server->listeners; l; l = l->next) {
1029 l->start(&exec_ctx, server, l->arg, server->pollsets, server->cq_count);
1030 }
Craig Tillerdfff1b82015-09-21 14:39:57 -07001031
Craig Tillera82950e2015-09-22 12:33:20 -07001032 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001033}
1034
Craig Tillera82950e2015-09-22 12:33:20 -07001035void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *s,
1036 grpc_transport *transport,
Craig Tillera82950e2015-09-22 12:33:20 -07001037 const grpc_channel_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001038 size_t i;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001039 size_t num_registered_methods;
1040 size_t alloc;
1041 registered_method *rm;
1042 channel_registered_method *crm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001043 grpc_channel *channel;
1044 channel_data *chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001045 grpc_mdstr *host;
1046 grpc_mdstr *method;
Craig Tiller7536af02015-12-22 13:49:30 -08001047 uint32_t hash;
Craig Tillerf96dfc32015-09-10 14:43:18 -07001048 size_t slots;
Craig Tiller7536af02015-12-22 13:49:30 -08001049 uint32_t probes;
1050 uint32_t max_probes = 0;
Craig Tillere039f032015-06-25 12:54:23 -07001051 grpc_transport_op op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001052
Craig Tillera82950e2015-09-22 12:33:20 -07001053 for (i = 0; i < s->cq_count; i++) {
1054 memset(&op, 0, sizeof(op));
1055 op.bind_pollset = grpc_cq_pollset(s->cqs[i]);
1056 grpc_transport_perform_op(exec_ctx, transport, &op);
1057 }
ctillerd79b4862014-12-17 16:36:59 -08001058
Craig Tiller178edfa2016-02-17 20:54:46 -08001059 channel =
1060 grpc_channel_create(exec_ctx, NULL, args, GRPC_SERVER_CHANNEL, transport);
Craig Tillera82950e2015-09-22 12:33:20 -07001061 chand = (channel_data *)grpc_channel_stack_element(
Craig Tillerf40df232016-03-25 13:38:14 -07001062 grpc_channel_get_channel_stack(channel), 0)
1063 ->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001064 chand->server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001065 server_ref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001066 chand->channel = channel;
1067
Craig Tiller04cc8be2015-02-10 16:11:22 -08001068 num_registered_methods = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001069 for (rm = s->registered_methods; rm; rm = rm->next) {
1070 num_registered_methods++;
1071 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001072 /* build a lookup table phrased in terms of mdstr's in this channels context
1073 to quickly find registered methods */
Craig Tillera82950e2015-09-22 12:33:20 -07001074 if (num_registered_methods > 0) {
1075 slots = 2 * num_registered_methods;
1076 alloc = sizeof(channel_registered_method) * slots;
1077 chand->registered_methods = gpr_malloc(alloc);
1078 memset(chand->registered_methods, 0, alloc);
1079 for (rm = s->registered_methods; rm; rm = rm->next) {
Craig Tillerb2b42612015-11-20 12:02:17 -08001080 host = rm->host ? grpc_mdstr_from_string(rm->host) : NULL;
1081 method = grpc_mdstr_from_string(rm->method);
Craig Tillera82950e2015-09-22 12:33:20 -07001082 hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash);
1083 for (probes = 0; chand->registered_methods[(hash + probes) % slots]
Craig Tillerf40df232016-03-25 13:38:14 -07001084 .server_registered_method != NULL;
Craig Tillera82950e2015-09-22 12:33:20 -07001085 probes++)
1086 ;
1087 if (probes > max_probes) max_probes = probes;
1088 crm = &chand->registered_methods[(hash + probes) % slots];
1089 crm->server_registered_method = rm;
Craig Tillerb2906862016-03-10 06:50:07 -08001090 crm->flags = rm->flags;
Craig Tillera82950e2015-09-22 12:33:20 -07001091 crm->host = host;
1092 crm->method = method;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001093 }
Craig Tiller7536af02015-12-22 13:49:30 -08001094 GPR_ASSERT(slots <= UINT32_MAX);
1095 chand->registered_method_slots = (uint32_t)slots;
Craig Tillera82950e2015-09-22 12:33:20 -07001096 chand->registered_method_max_probes = max_probes;
1097 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001098
Craig Tillera82950e2015-09-22 12:33:20 -07001099 gpr_mu_lock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001100 chand->next = &s->root_channel_data;
1101 chand->prev = chand->next->prev;
1102 chand->next->prev = chand->prev->next = chand;
Craig Tillera82950e2015-09-22 12:33:20 -07001103 gpr_mu_unlock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001104
Craig Tillera82950e2015-09-22 12:33:20 -07001105 GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity");
1106 memset(&op, 0, sizeof(op));
Craig Tillerd7f12e32016-03-03 10:08:31 -08001107 op.set_accept_stream = true;
1108 op.set_accept_stream_fn = accept_stream;
Craig Tiller4b804102015-06-26 16:16:12 -07001109 op.set_accept_stream_user_data = chand;
1110 op.on_connectivity_state_change = &chand->channel_connectivity_changed;
1111 op.connectivity_state = &chand->connectivity_state;
Craig Tiller804ff712016-05-05 16:25:40 -07001112 if (gpr_atm_acq_load(&s->shutdown_flag) != 0) {
1113 op.disconnect_with_error = GRPC_ERROR_CREATE("Server shutdown");
1114 }
Craig Tillera82950e2015-09-22 12:33:20 -07001115 grpc_transport_perform_op(exec_ctx, transport, &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001116}
1117
Craig Tillera82950e2015-09-22 12:33:20 -07001118void done_published_shutdown(grpc_exec_ctx *exec_ctx, void *done_arg,
1119 grpc_cq_completion *storage) {
1120 (void)done_arg;
1121 gpr_free(storage);
murgatroid9900a3dab2015-08-19 11:15:38 -07001122}
1123
Craig Tillera82950e2015-09-22 12:33:20 -07001124static void listener_destroy_done(grpc_exec_ctx *exec_ctx, void *s,
Craig Tillerf51457b2016-05-03 17:06:32 -07001125 grpc_error *error) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001126 grpc_server *server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001127 gpr_mu_lock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001128 server->listeners_destroyed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001129 maybe_finish_shutdown(exec_ctx, server);
1130 gpr_mu_unlock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001131}
1132
Craig Tillera82950e2015-09-22 12:33:20 -07001133void grpc_server_shutdown_and_notify(grpc_server *server,
1134 grpc_completion_queue *cq, void *tag) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001135 listener *l;
Craig Tillerbce999f2015-05-27 09:55:51 -07001136 shutdown_tag *sdt;
Craig Tillerff3ae682015-06-29 17:44:04 -07001137 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001138 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001139
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001140 GRPC_API_TRACE("grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", 3,
1141 (server, cq, tag));
1142
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001143 /* lock, and gather up some stuff to do */
Craig Tillera82950e2015-09-22 12:33:20 -07001144 gpr_mu_lock(&server->mu_global);
Craig Tiller4bf29282015-12-14 11:25:48 -08001145 grpc_cq_begin_op(cq, tag);
Craig Tillera82950e2015-09-22 12:33:20 -07001146 if (server->shutdown_published) {
Craig Tillerf51457b2016-05-03 17:06:32 -07001147 grpc_cq_end_op(&exec_ctx, cq, tag, GRPC_ERROR_NONE, done_published_shutdown,
1148 NULL, gpr_malloc(sizeof(grpc_cq_completion)));
Craig Tillera82950e2015-09-22 12:33:20 -07001149 gpr_mu_unlock(&server->mu_global);
1150 goto done;
1151 }
1152 server->shutdown_tags =
1153 gpr_realloc(server->shutdown_tags,
1154 sizeof(shutdown_tag) * (server->num_shutdown_tags + 1));
Craig Tillerbce999f2015-05-27 09:55:51 -07001155 sdt = &server->shutdown_tags[server->num_shutdown_tags++];
1156 sdt->tag = tag;
1157 sdt->cq = cq;
Craig Tillera82950e2015-09-22 12:33:20 -07001158 if (gpr_atm_acq_load(&server->shutdown_flag)) {
1159 gpr_mu_unlock(&server->mu_global);
1160 goto done;
1161 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001162
Craig Tillera82950e2015-09-22 12:33:20 -07001163 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
Craig Tillerab54f792015-07-08 08:34:20 -07001164
Craig Tillera82950e2015-09-22 12:33:20 -07001165 channel_broadcaster_init(server, &broadcaster);
nnoble0c475f02014-12-05 15:37:39 -08001166
Craig Tillerfc193e12015-09-24 15:29:03 -07001167 gpr_atm_rel_store(&server->shutdown_flag, 1);
1168
Craig Tillerbd217572015-02-11 18:10:56 -08001169 /* collect all unregistered then registered calls */
Craig Tillera82950e2015-09-22 12:33:20 -07001170 gpr_mu_lock(&server->mu_call);
Craig Tillercae4b1b2016-05-10 09:11:09 -07001171 kill_pending_work_locked(&exec_ctx, server,
1172 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001173 gpr_mu_unlock(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001174
Craig Tillera82950e2015-09-22 12:33:20 -07001175 maybe_finish_shutdown(&exec_ctx, server);
1176 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001177
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001178 /* Shutdown listeners */
Craig Tillera82950e2015-09-22 12:33:20 -07001179 for (l = server->listeners; l; l = l->next) {
1180 grpc_closure_init(&l->destroy_done, listener_destroy_done, server);
1181 l->destroy(&exec_ctx, server, l->arg, &l->destroy_done);
1182 }
Craig Tillerff3ae682015-06-29 17:44:04 -07001183
Craig Tillera82950e2015-09-22 12:33:20 -07001184 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 1, 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001185
Craig Tillerdfff1b82015-09-21 14:39:57 -07001186done:
Craig Tillera82950e2015-09-22 12:33:20 -07001187 grpc_exec_ctx_finish(&exec_ctx);
Craig Tilleraec96aa2015-04-07 14:32:15 -07001188}
1189
Craig Tillera82950e2015-09-22 12:33:20 -07001190void grpc_server_cancel_all_calls(grpc_server *server) {
Craig Tiller092d8d12015-07-04 22:35:00 -07001191 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001192 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerafa2d632015-05-26 16:39:13 -07001193
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001194 GRPC_API_TRACE("grpc_server_cancel_all_calls(server=%p)", 1, (server));
1195
Craig Tillera82950e2015-09-22 12:33:20 -07001196 gpr_mu_lock(&server->mu_global);
1197 channel_broadcaster_init(server, &broadcaster);
1198 gpr_mu_unlock(&server->mu_global);
Craig Tillerafa2d632015-05-26 16:39:13 -07001199
Craig Tiller804ff712016-05-05 16:25:40 -07001200 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 0,
1201 GRPC_ERROR_CREATE("Cancelling all calls"));
Craig Tillera82950e2015-09-22 12:33:20 -07001202 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerafa2d632015-05-26 16:39:13 -07001203}
1204
Craig Tillera82950e2015-09-22 12:33:20 -07001205void grpc_server_destroy(grpc_server *server) {
Craig Tilleraec96aa2015-04-07 14:32:15 -07001206 listener *l;
Craig Tillerf5768a62015-09-22 10:54:34 -07001207 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller872af022015-04-24 15:57:52 -07001208
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001209 GRPC_API_TRACE("grpc_server_destroy(server=%p)", 1, (server));
1210
Craig Tillera82950e2015-09-22 12:33:20 -07001211 gpr_mu_lock(&server->mu_global);
1212 GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners);
1213 GPR_ASSERT(server->listeners_destroyed == num_listeners(server));
Craig Tilleraec96aa2015-04-07 14:32:15 -07001214
Craig Tillera82950e2015-09-22 12:33:20 -07001215 while (server->listeners) {
1216 l = server->listeners;
1217 server->listeners = l->next;
1218 gpr_free(l);
1219 }
Craig Tilleraec96aa2015-04-07 14:32:15 -07001220
Craig Tillera82950e2015-09-22 12:33:20 -07001221 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001222
Craig Tillera82950e2015-09-22 12:33:20 -07001223 server_unref(&exec_ctx, server);
1224 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001225}
1226
Craig Tillera82950e2015-09-22 12:33:20 -07001227void grpc_server_add_listener(
1228 grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1229 void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1230 grpc_pollset **pollsets, size_t pollset_count),
1231 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1232 grpc_closure *on_done)) {
1233 listener *l = gpr_malloc(sizeof(listener));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001234 l->arg = arg;
1235 l->start = start;
1236 l->destroy = destroy;
1237 l->next = server->listeners;
1238 server->listeners = l;
1239}
1240
Craig Tillera82950e2015-09-22 12:33:20 -07001241static grpc_call_error queue_call_request(grpc_exec_ctx *exec_ctx,
1242 grpc_server *server,
1243 requested_call *rc) {
Yang Gaoeb8e7cd2015-02-11 11:43:40 -08001244 call_data *calld = NULL;
Craig Tillerb9d35962015-09-11 13:31:16 -07001245 request_matcher *rm = NULL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001246 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -07001247 if (gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tillercae4b1b2016-05-10 09:11:09 -07001248 fail_call(exec_ctx, server, rc, GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001249 return GRPC_CALL_OK;
1250 }
1251 request_id = gpr_stack_lockfree_pop(server->request_freelist);
1252 if (request_id == -1) {
1253 /* out of request ids: just fail this one */
Craig Tillercae4b1b2016-05-10 09:11:09 -07001254 fail_call(exec_ctx, server, rc, GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001255 return GRPC_CALL_OK;
1256 }
1257 switch (rc->type) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001258 case BATCH_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001259 rm = &server->unregistered_request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001260 break;
1261 case REGISTERED_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001262 rm = &rc->data.registered.registered_method->request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001263 break;
Craig Tillera82950e2015-09-22 12:33:20 -07001264 }
Craig Tiller45724b32015-09-22 10:42:19 -07001265 server->requested_calls[request_id] = *rc;
Craig Tillera82950e2015-09-22 12:33:20 -07001266 gpr_free(rc);
Craig Tillerb9d35962015-09-11 13:31:16 -07001267 if (gpr_stack_lockfree_push(rm->requests, request_id)) {
Craig Tillera82950e2015-09-22 12:33:20 -07001268 /* this was the first queued request: we need to lock and start
1269 matching calls */
1270 gpr_mu_lock(&server->mu_call);
Craig Tillerb9d35962015-09-11 13:31:16 -07001271 while ((calld = rm->pending_head) != NULL) {
1272 request_id = gpr_stack_lockfree_pop(rm->requests);
Craig Tillera82950e2015-09-22 12:33:20 -07001273 if (request_id == -1) break;
Craig Tillerb9d35962015-09-11 13:31:16 -07001274 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -07001275 gpr_mu_unlock(&server->mu_call);
1276 gpr_mu_lock(&calld->mu_state);
1277 if (calld->state == ZOMBIED) {
1278 gpr_mu_unlock(&calld->mu_state);
1279 grpc_closure_init(
1280 &calld->kill_zombie_closure, kill_zombie,
1281 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tillerf51457b2016-05-03 17:06:32 -07001282 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure,
1283 GRPC_ERROR_NONE, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001284 } else {
1285 GPR_ASSERT(calld->state == PENDING);
1286 calld->state = ACTIVATED;
1287 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -07001288 publish_call(exec_ctx, server, calld,
1289 &server->requested_calls[request_id]);
Craig Tillera82950e2015-09-22 12:33:20 -07001290 }
1291 gpr_mu_lock(&server->mu_call);
Craig Tiller45724b32015-09-22 10:42:19 -07001292 }
Craig Tillera82950e2015-09-22 12:33:20 -07001293 gpr_mu_unlock(&server->mu_call);
1294 }
Craig Tiller6a006ce2015-07-13 16:25:40 -07001295 return GRPC_CALL_OK;
Craig Tillercce17ac2015-01-20 09:29:28 -08001296}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001297
Craig Tillera82950e2015-09-22 12:33:20 -07001298grpc_call_error grpc_server_request_call(
1299 grpc_server *server, grpc_call **call, grpc_call_details *details,
1300 grpc_metadata_array *initial_metadata,
1301 grpc_completion_queue *cq_bound_to_call,
1302 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001303 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001304 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001305 requested_call *rc = gpr_malloc(sizeof(*rc));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001306 GRPC_API_TRACE(
1307 "grpc_server_request_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001308 "server=%p, call=%p, details=%p, initial_metadata=%p, "
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001309 "cq_bound_to_call=%p, cq_for_notification=%p, tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001310 7, (server, call, details, initial_metadata, cq_bound_to_call,
1311 cq_for_notification, tag));
Craig Tillera82950e2015-09-22 12:33:20 -07001312 if (!grpc_cq_is_server_cq(cq_for_notification)) {
1313 gpr_free(rc);
1314 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1315 goto done;
1316 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001317 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller9928d392015-08-18 09:40:24 -07001318 details->reserved = NULL;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001319 rc->type = BATCH_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001320 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001321 rc->tag = tag;
1322 rc->cq_bound_to_call = cq_bound_to_call;
1323 rc->cq_for_notification = cq_for_notification;
1324 rc->call = call;
1325 rc->data.batch.details = details;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001326 rc->initial_metadata = initial_metadata;
Craig Tillera82950e2015-09-22 12:33:20 -07001327 error = queue_call_request(&exec_ctx, server, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001328done:
Craig Tillera82950e2015-09-22 12:33:20 -07001329 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001330 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001331}
1332
Craig Tillera82950e2015-09-22 12:33:20 -07001333grpc_call_error grpc_server_request_registered_call(
Craig Tillerb9d35962015-09-11 13:31:16 -07001334 grpc_server *server, void *rmp, grpc_call **call, gpr_timespec *deadline,
Craig Tillera82950e2015-09-22 12:33:20 -07001335 grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload,
1336 grpc_completion_queue *cq_bound_to_call,
1337 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001338 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001339 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001340 requested_call *rc = gpr_malloc(sizeof(*rc));
Craig Tillerb9d35962015-09-11 13:31:16 -07001341 registered_method *rm = rmp;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001342 GRPC_API_TRACE(
1343 "grpc_server_request_registered_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001344 "server=%p, rmp=%p, call=%p, deadline=%p, initial_metadata=%p, "
1345 "optional_payload=%p, cq_bound_to_call=%p, cq_for_notification=%p, "
1346 "tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001347 9, (server, rmp, call, deadline, initial_metadata, optional_payload,
1348 cq_bound_to_call, cq_for_notification, tag));
Craig Tillera82950e2015-09-22 12:33:20 -07001349 if (!grpc_cq_is_server_cq(cq_for_notification)) {
1350 gpr_free(rc);
1351 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1352 goto done;
1353 }
Craig Tiller06cb1a92016-04-04 08:10:47 -07001354 if ((optional_payload == NULL) !=
1355 (rm->payload_handling == GRPC_SRM_PAYLOAD_NONE)) {
1356 gpr_free(rc);
1357 error = GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH;
1358 goto done;
1359 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001360 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller97fc6a32015-07-08 15:31:35 -07001361 rc->type = REGISTERED_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001362 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001363 rc->tag = tag;
1364 rc->cq_bound_to_call = cq_bound_to_call;
1365 rc->cq_for_notification = cq_for_notification;
1366 rc->call = call;
Craig Tillerb9d35962015-09-11 13:31:16 -07001367 rc->data.registered.registered_method = rm;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001368 rc->data.registered.deadline = deadline;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001369 rc->initial_metadata = initial_metadata;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001370 rc->data.registered.optional_payload = optional_payload;
Craig Tillera82950e2015-09-22 12:33:20 -07001371 error = queue_call_request(&exec_ctx, server, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001372done:
Craig Tillera82950e2015-09-22 12:33:20 -07001373 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001374 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001375}
1376
Craig Tillera82950e2015-09-22 12:33:20 -07001377static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tillercae4b1b2016-05-10 09:11:09 -07001378 requested_call *rc, grpc_error *error) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -07001379 *rc->call = NULL;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001380 rc->initial_metadata->count = 0;
Craig Tillercae4b1b2016-05-10 09:11:09 -07001381 GPR_ASSERT(error != GRPC_ERROR_NONE);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001382
Craig Tillera82950e2015-09-22 12:33:20 -07001383 server_ref(server);
Craig Tillercae4b1b2016-05-10 09:11:09 -07001384 grpc_cq_end_op(exec_ctx, rc->cq_for_notification, rc->tag, error,
Craig Tillera82950e2015-09-22 12:33:20 -07001385 done_request_event, rc, &rc->completion);
Craig Tiller24be0f72015-02-10 14:04:22 -08001386}
1387
Craig Tillera82950e2015-09-22 12:33:20 -07001388const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001389 return server->channel_args;
Craig Tiller190d3602015-02-18 09:23:38 -08001390}
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001391
Craig Tillera82950e2015-09-22 12:33:20 -07001392int grpc_server_has_open_connections(grpc_server *server) {
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001393 int r;
Craig Tillera82950e2015-09-22 12:33:20 -07001394 gpr_mu_lock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001395 r = server->root_channel_data.next != &server->root_channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -07001396 gpr_mu_unlock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001397 return r;
1398}