blob: 7210c69fb0d2d3586dde250ff308ef26fc3d7ae8 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Sree Kuchibhotla01907122016-04-21 15:09:13 -07003 * Copyright 2015-2016, 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"
Craig Tillera59c16c2016-10-31 07:25:01 -070048#include "src/core/lib/slice/slice_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070049#include "src/core/lib/support/stack_lockfree.h"
50#include "src/core/lib/support/string.h"
51#include "src/core/lib/surface/api_trace.h"
52#include "src/core/lib/surface/call.h"
53#include "src/core/lib/surface/channel.h"
54#include "src/core/lib/surface/completion_queue.h"
55#include "src/core/lib/surface/init.h"
56#include "src/core/lib/transport/metadata.h"
57#include "src/core/lib/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058
Craig Tillera82950e2015-09-22 12:33:20 -070059typedef struct listener {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060 void *arg;
Craig Tillera82950e2015-09-22 12:33:20 -070061 void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
62 grpc_pollset **pollsets, size_t pollset_count);
63 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
64 grpc_closure *closure);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065 struct listener *next;
Craig Tillerdfff1b82015-09-21 14:39:57 -070066 grpc_closure destroy_done;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067} listener;
68
69typedef struct call_data call_data;
70typedef struct channel_data channel_data;
Craig Tiller24be0f72015-02-10 14:04:22 -080071typedef struct registered_method registered_method;
72
Craig Tillera82950e2015-09-22 12:33:20 -070073typedef enum { BATCH_CALL, REGISTERED_CALL } requested_call_type;
Craig Tiller24be0f72015-02-10 14:04:22 -080074
Craig Tiller687a0e62016-09-21 10:48:59 -070075int grpc_server_channel_trace = 0;
76
Craig Tillera82950e2015-09-22 12:33:20 -070077typedef struct requested_call {
Craig Tiller24be0f72015-02-10 14:04:22 -080078 requested_call_type type;
Craig Tillerb19dbea2016-07-12 15:33:11 -070079 size_t cq_idx;
Craig Tiller24be0f72015-02-10 14:04:22 -080080 void *tag;
Craig Tiller6a006ce2015-07-13 16:25:40 -070081 grpc_server *server;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070082 grpc_completion_queue *cq_bound_to_call;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070083 grpc_call **call;
Craig Tiller97fc6a32015-07-08 15:31:35 -070084 grpc_cq_completion completion;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080085 grpc_metadata_array *initial_metadata;
Craig Tillera82950e2015-09-22 12:33:20 -070086 union {
87 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080088 grpc_call_details *details;
Craig Tiller24be0f72015-02-10 14:04:22 -080089 } batch;
Craig Tillera82950e2015-09-22 12:33:20 -070090 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080091 registered_method *registered_method;
92 gpr_timespec *deadline;
Craig Tiller24be0f72015-02-10 14:04:22 -080093 grpc_byte_buffer **optional_payload;
94 } registered;
95 } data;
96} requested_call;
97
Craig Tillera82950e2015-09-22 12:33:20 -070098typedef struct channel_registered_method {
Craig Tiller24be0f72015-02-10 14:04:22 -080099 registered_method *server_registered_method;
Craig Tillerb2906862016-03-10 06:50:07 -0800100 uint32_t flags;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800101 bool has_host;
102 grpc_slice method;
103 grpc_slice host;
Craig Tiller24be0f72015-02-10 14:04:22 -0800104} channel_registered_method;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105
Craig Tillera82950e2015-09-22 12:33:20 -0700106struct channel_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800107 grpc_server *server;
Craig Tillere039f032015-06-25 12:54:23 -0700108 grpc_connectivity_state connectivity_state;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109 grpc_channel *channel;
Craig Tiller418a8212016-05-16 16:27:51 -0700110 size_t cq_idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111 /* 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 Tiller7c70b6c2017-01-23 07:48:42 -0800148 bool path_set;
149 bool host_set;
150 grpc_slice path;
151 grpc_slice host;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700152 gpr_timespec deadline;
Craig Tillercce17ac2015-01-20 09:29:28 -0800153
Craig Tiller20bc56d2015-02-12 09:02:56 -0800154 grpc_completion_queue *cq_new;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800155
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800156 grpc_metadata_batch *recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800157 bool recv_idempotent_request;
Makarand Dharmapurikard322d4b2016-08-29 12:54:19 -0700158 bool recv_cacheable_request;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800159 grpc_metadata_array initial_metadata;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700160
Craig Tiller88512692016-04-04 09:32:52 -0700161 request_matcher *request_matcher;
162 grpc_byte_buffer *payload;
163
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800164 grpc_closure got_initial_metadata;
165 grpc_closure server_on_recv_initial_metadata;
Craig Tiller33825112015-09-18 07:44:19 -0700166 grpc_closure kill_zombie_closure;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800167 grpc_closure *on_done_recv_initial_metadata;
David Garcia Quintas284488b2015-05-28 16:27:39 -0700168
Craig Tiller88512692016-04-04 09:32:52 -0700169 grpc_closure publish;
170
Craig Tiller729b35a2015-07-13 12:36:47 -0700171 call_data *pending_next;
172};
173
Craig Tillera82950e2015-09-22 12:33:20 -0700174struct request_matcher {
Craig Tiller88512692016-04-04 09:32:52 -0700175 grpc_server *server;
Craig Tiller729b35a2015-07-13 12:36:47 -0700176 call_data *pending_head;
177 call_data *pending_tail;
Craig Tillerdb7c3562016-05-19 11:02:52 -0700178 gpr_stack_lockfree **requests_per_cq;
Craig Tiller729b35a2015-07-13 12:36:47 -0700179};
180
Craig Tillera82950e2015-09-22 12:33:20 -0700181struct registered_method {
Craig Tiller729b35a2015-07-13 12:36:47 -0700182 char *method;
183 char *host;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700184 grpc_server_register_method_payload_handling payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -0800185 uint32_t flags;
Craig Tillerfa96d862016-05-21 12:39:56 -0700186 /* one request matcher per method */
Craig Tiller729b35a2015-07-13 12:36:47 -0700187 request_matcher request_matcher;
188 registered_method *next;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800189};
190
Craig Tillera82950e2015-09-22 12:33:20 -0700191typedef struct {
Craig Tillerff3ae682015-06-29 17:44:04 -0700192 grpc_channel **channels;
193 size_t num_channels;
194} channel_broadcaster;
195
Craig Tillera82950e2015-09-22 12:33:20 -0700196struct grpc_server {
Craig Tiller729b35a2015-07-13 12:36:47 -0700197 grpc_channel_args *channel_args;
198
199 grpc_completion_queue **cqs;
200 grpc_pollset **pollsets;
201 size_t cq_count;
Craig Tiller1d0fce92016-10-26 08:26:22 -0700202 size_t pollset_count;
Craig Tiller88ef00e2016-05-17 09:31:49 -0700203 bool started;
Craig Tiller729b35a2015-07-13 12:36:47 -0700204
205 /* The two following mutexes control access to server-state
206 mu_global controls access to non-call-related state (e.g., channel state)
207 mu_call controls access to call-related state (e.g., the call lists)
208
209 If they are ever required to be nested, you must lock mu_global
210 before mu_call. This is currently used in shutdown processing
211 (grpc_server_shutdown_and_notify and maybe_finish_shutdown) */
Craig Tillera82950e2015-09-22 12:33:20 -0700212 gpr_mu mu_global; /* mutex for server and channel state */
213 gpr_mu mu_call; /* mutex for call-specific state */
Craig Tiller729b35a2015-07-13 12:36:47 -0700214
215 registered_method *registered_methods;
Craig Tillerfa96d862016-05-21 12:39:56 -0700216 /** one request matcher for unregistered methods */
Craig Tiller729b35a2015-07-13 12:36:47 -0700217 request_matcher unregistered_request_matcher;
Craig Tiller0a06cd72016-07-14 13:21:24 -0700218 /** free list of available requested_calls_per_cq indices */
Craig Tillerb19dbea2016-07-12 15:33:11 -0700219 gpr_stack_lockfree **request_freelist_per_cq;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700220 /** requested call backing data */
Craig Tillerb19dbea2016-07-12 15:33:11 -0700221 requested_call **requested_calls_per_cq;
222 int max_requested_calls_per_cq;
Craig Tiller729b35a2015-07-13 12:36:47 -0700223
Craig Tiller6a006ce2015-07-13 16:25:40 -0700224 gpr_atm shutdown_flag;
Craig Tiller7536af02015-12-22 13:49:30 -0800225 uint8_t shutdown_published;
Craig Tiller729b35a2015-07-13 12:36:47 -0700226 size_t num_shutdown_tags;
227 shutdown_tag *shutdown_tags;
228
229 channel_data root_channel_data;
230
231 listener *listeners;
232 int listeners_destroyed;
233 gpr_refcount internal_refcount;
234
235 /** when did we print the last shutdown progress message */
236 gpr_timespec last_shutdown_message_time;
237};
238
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800239#define SERVER_FROM_CALL_ELEM(elem) \
240 (((channel_data *)(elem)->channel_data)->server)
241
Craig Tillerf51457b2016-05-03 17:06:32 -0700242static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *calld,
243 grpc_error *error);
Craig Tillera82950e2015-09-22 12:33:20 -0700244static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tiller48abdde2016-05-24 06:55:28 -0700245 size_t cq_idx, requested_call *rc, grpc_error *error);
Vijay Pai8931cdd2015-06-17 12:42:17 -0700246/* Before calling maybe_finish_shutdown, we must hold mu_global and not
247 hold mu_call */
Craig Tillera82950e2015-09-22 12:33:20 -0700248static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_server *server);
Craig Tiller24be0f72015-02-10 14:04:22 -0800249
Craig Tiller729b35a2015-07-13 12:36:47 -0700250/*
251 * channel broadcaster
252 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700253
254/* assumes server locked */
Craig Tillera82950e2015-09-22 12:33:20 -0700255static void channel_broadcaster_init(grpc_server *s, channel_broadcaster *cb) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700256 channel_data *c;
257 size_t count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700258 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
259 count++;
260 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700261 cb->num_channels = count;
Craig Tillera82950e2015-09-22 12:33:20 -0700262 cb->channels = gpr_malloc(sizeof(*cb->channels) * cb->num_channels);
Craig Tillerff3ae682015-06-29 17:44:04 -0700263 count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700264 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
265 cb->channels[count++] = c->channel;
266 GRPC_CHANNEL_INTERNAL_REF(c->channel, "broadcast");
267 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700268}
269
Craig Tillera82950e2015-09-22 12:33:20 -0700270struct shutdown_cleanup_args {
Craig Tiller33825112015-09-18 07:44:19 -0700271 grpc_closure closure;
Craig Tillerd41a4a72016-10-26 16:16:06 -0700272 grpc_slice slice;
Craig Tillerff3ae682015-06-29 17:44:04 -0700273};
274
Craig Tillera82950e2015-09-22 12:33:20 -0700275static void shutdown_cleanup(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tillerf51457b2016-05-03 17:06:32 -0700276 grpc_error *error) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700277 struct shutdown_cleanup_args *a = arg;
Craig Tillera59c16c2016-10-31 07:25:01 -0700278 grpc_slice_unref_internal(exec_ctx, a->slice);
Craig Tillera82950e2015-09-22 12:33:20 -0700279 gpr_free(a);
Craig Tillerff3ae682015-06-29 17:44:04 -0700280}
281
Craig Tillera82950e2015-09-22 12:33:20 -0700282static void send_shutdown(grpc_exec_ctx *exec_ctx, grpc_channel *channel,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800283 bool send_goaway, grpc_error *send_disconnect) {
Craig Tillere0221ff2016-07-11 15:56:08 -0700284 struct shutdown_cleanup_args *sc = gpr_malloc(sizeof(*sc));
Craig Tiller91031da2016-12-28 15:44:25 -0800285 grpc_closure_init(&sc->closure, shutdown_cleanup, sc,
286 grpc_schedule_on_exec_ctx);
Craig Tillere0221ff2016-07-11 15:56:08 -0700287 grpc_transport_op *op = grpc_make_transport_op(&sc->closure);
Craig Tillerff3ae682015-06-29 17:44:04 -0700288 grpc_channel_element *elem;
289
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800290 op->goaway_error =
291 send_goaway
292 ? grpc_error_set_int(GRPC_ERROR_CREATE("Server shutdown"),
293 GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_OK)
294 : GRPC_ERROR_NONE;
Craig Tillere194ff02016-08-29 15:48:41 -0700295 op->set_accept_stream = true;
Craig Tillerd41a4a72016-10-26 16:16:06 -0700296 sc->slice = grpc_slice_from_copied_string("Server shutdown");
Craig Tillere0221ff2016-07-11 15:56:08 -0700297 op->disconnect_with_error = send_disconnect;
Craig Tillerff3ae682015-06-29 17:44:04 -0700298
Craig Tillera82950e2015-09-22 12:33:20 -0700299 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
Craig Tillere0221ff2016-07-11 15:56:08 -0700300 elem->filter->start_transport_op(exec_ctx, elem, op);
Craig Tillerff3ae682015-06-29 17:44:04 -0700301}
302
Craig Tillera82950e2015-09-22 12:33:20 -0700303static void channel_broadcaster_shutdown(grpc_exec_ctx *exec_ctx,
304 channel_broadcaster *cb,
Mark D. Roth7f8db252016-06-24 08:24:15 -0700305 bool send_goaway,
Craig Tiller804ff712016-05-05 16:25:40 -0700306 grpc_error *force_disconnect) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700307 size_t i;
308
Craig Tillera82950e2015-09-22 12:33:20 -0700309 for (i = 0; i < cb->num_channels; i++) {
Craig Tiller71f96652016-05-11 23:17:45 -0700310 send_shutdown(exec_ctx, cb->channels[i], send_goaway,
311 GRPC_ERROR_REF(force_disconnect));
Craig Tillera82950e2015-09-22 12:33:20 -0700312 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, cb->channels[i], "broadcast");
313 }
314 gpr_free(cb->channels);
Craig Tiller71f96652016-05-11 23:17:45 -0700315 GRPC_ERROR_UNREF(force_disconnect);
Craig Tillerff3ae682015-06-29 17:44:04 -0700316}
317
Craig Tiller729b35a2015-07-13 12:36:47 -0700318/*
319 * request_matcher
320 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700321
Craig Tiller88512692016-04-04 09:32:52 -0700322static void request_matcher_init(request_matcher *rm, size_t entries,
323 grpc_server *server) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700324 memset(rm, 0, sizeof(*rm));
Craig Tiller88512692016-04-04 09:32:52 -0700325 rm->server = server;
Craig Tillerdb7c3562016-05-19 11:02:52 -0700326 rm->requests_per_cq =
327 gpr_malloc(sizeof(*rm->requests_per_cq) * server->cq_count);
328 for (size_t i = 0; i < server->cq_count; i++) {
329 rm->requests_per_cq[i] = gpr_stack_lockfree_create(entries);
330 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800331}
332
Craig Tillerb9d35962015-09-11 13:31:16 -0700333static void request_matcher_destroy(request_matcher *rm) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700334 for (size_t i = 0; i < rm->server->cq_count; i++) {
335 GPR_ASSERT(gpr_stack_lockfree_pop(rm->requests_per_cq[i]) == -1);
336 gpr_stack_lockfree_destroy(rm->requests_per_cq[i]);
337 }
338 gpr_free(rm->requests_per_cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800339}
340
Craig Tillerf51457b2016-05-03 17:06:32 -0700341static void kill_zombie(grpc_exec_ctx *exec_ctx, void *elem,
342 grpc_error *error) {
Craig Tillera82950e2015-09-22 12:33:20 -0700343 grpc_call_destroy(grpc_call_from_top_element(elem));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800344}
345
Craig Tiller8dc09712015-09-24 13:58:16 -0700346static void request_matcher_zombify_all_pending_calls(grpc_exec_ctx *exec_ctx,
347 request_matcher *rm) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700348 while (rm->pending_head) {
349 call_data *calld = rm->pending_head;
350 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -0700351 gpr_mu_lock(&calld->mu_state);
352 calld->state = ZOMBIED;
353 gpr_mu_unlock(&calld->mu_state);
354 grpc_closure_init(
355 &calld->kill_zombie_closure, kill_zombie,
Craig Tiller91031da2016-12-28 15:44:25 -0800356 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0),
357 grpc_schedule_on_exec_ctx);
358 grpc_closure_sched(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -0700359 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800360}
361
Craig Tillera82950e2015-09-22 12:33:20 -0700362static void request_matcher_kill_requests(grpc_exec_ctx *exec_ctx,
363 grpc_server *server,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700364 request_matcher *rm,
365 grpc_error *error) {
Craig Tiller1191e212015-07-30 14:49:02 -0700366 int request_id;
Craig Tillerdb7c3562016-05-19 11:02:52 -0700367 for (size_t i = 0; i < server->cq_count; i++) {
368 while ((request_id = gpr_stack_lockfree_pop(rm->requests_per_cq[i])) !=
369 -1) {
Craig Tillerb19dbea2016-07-12 15:33:11 -0700370 fail_call(exec_ctx, server, i,
371 &server->requested_calls_per_cq[i][request_id],
Craig Tiller48abdde2016-05-24 06:55:28 -0700372 GRPC_ERROR_REF(error));
Craig Tillerdb7c3562016-05-19 11:02:52 -0700373 }
Craig Tillera82950e2015-09-22 12:33:20 -0700374 }
Craig Tillercae4b1b2016-05-10 09:11:09 -0700375 GRPC_ERROR_UNREF(error);
Craig Tiller1191e212015-07-30 14:49:02 -0700376}
377
Craig Tiller729b35a2015-07-13 12:36:47 -0700378/*
379 * server proper
380 */
381
Craig Tillera82950e2015-09-22 12:33:20 -0700382static void server_ref(grpc_server *server) {
383 gpr_ref(&server->internal_refcount);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800384}
385
Craig Tillera82950e2015-09-22 12:33:20 -0700386static void server_delete(grpc_exec_ctx *exec_ctx, grpc_server *server) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800387 registered_method *rm;
Craig Tiller89504612015-04-27 11:48:46 -0700388 size_t i;
Craig Tillera59c16c2016-10-31 07:25:01 -0700389 grpc_channel_args_destroy(exec_ctx, server->channel_args);
Craig Tillera82950e2015-09-22 12:33:20 -0700390 gpr_mu_destroy(&server->mu_global);
391 gpr_mu_destroy(&server->mu_call);
Craig Tillera82950e2015-09-22 12:33:20 -0700392 while ((rm = server->registered_methods) != NULL) {
393 server->registered_methods = rm->next;
Craig Tiller88ef00e2016-05-17 09:31:49 -0700394 if (server->started) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700395 request_matcher_destroy(&rm->request_matcher);
Craig Tiller418a8212016-05-16 16:27:51 -0700396 }
Craig Tillera82950e2015-09-22 12:33:20 -0700397 gpr_free(rm->method);
398 gpr_free(rm->host);
399 gpr_free(rm);
400 }
Craig Tillerdb7c3562016-05-19 11:02:52 -0700401 if (server->started) {
402 request_matcher_destroy(&server->unregistered_request_matcher);
403 }
Craig Tillera82950e2015-09-22 12:33:20 -0700404 for (i = 0; i < server->cq_count; i++) {
405 GRPC_CQ_INTERNAL_UNREF(server->cqs[i], "server");
Craig Tillerb19dbea2016-07-12 15:33:11 -0700406 if (server->started) {
407 gpr_stack_lockfree_destroy(server->request_freelist_per_cq[i]);
408 gpr_free(server->requested_calls_per_cq[i]);
409 }
Craig Tillera82950e2015-09-22 12:33:20 -0700410 }
Craig Tillerb19dbea2016-07-12 15:33:11 -0700411 gpr_free(server->request_freelist_per_cq);
412 gpr_free(server->requested_calls_per_cq);
Craig Tillera82950e2015-09-22 12:33:20 -0700413 gpr_free(server->cqs);
414 gpr_free(server->pollsets);
415 gpr_free(server->shutdown_tags);
Craig Tillera82950e2015-09-22 12:33:20 -0700416 gpr_free(server);
Craig Tilleree945e82015-05-26 16:15:34 -0700417}
418
Craig Tillera82950e2015-09-22 12:33:20 -0700419static void server_unref(grpc_exec_ctx *exec_ctx, grpc_server *server) {
420 if (gpr_unref(&server->internal_refcount)) {
421 server_delete(exec_ctx, server);
422 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800423}
424
Craig Tillera82950e2015-09-22 12:33:20 -0700425static int is_channel_orphaned(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800426 return chand->next == chand;
427}
428
Craig Tillera82950e2015-09-22 12:33:20 -0700429static void orphan_channel(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800430 chand->next->prev = chand->prev;
431 chand->prev->next = chand->next;
432 chand->next = chand->prev = chand;
433}
434
Craig Tillera82950e2015-09-22 12:33:20 -0700435static void finish_destroy_channel(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700436 grpc_error *error) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800437 channel_data *chand = cd;
438 grpc_server *server = chand->server;
Craig Tillera82950e2015-09-22 12:33:20 -0700439 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "server");
440 server_unref(exec_ctx, server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800441}
442
Craig Tiller9d018482016-07-18 08:53:49 -0700443static void destroy_channel(grpc_exec_ctx *exec_ctx, channel_data *chand,
444 grpc_error *error) {
Craig Tillera82950e2015-09-22 12:33:20 -0700445 if (is_channel_orphaned(chand)) return;
446 GPR_ASSERT(chand->server != NULL);
447 orphan_channel(chand);
448 server_ref(chand->server);
449 maybe_finish_shutdown(exec_ctx, chand->server);
Craig Tiller3cb34472016-12-28 16:11:38 -0800450 grpc_closure_init(&chand->finish_destroy_channel_closure,
451 finish_destroy_channel, chand, grpc_schedule_on_exec_ctx);
Craig Tillerd7f12e32016-03-03 10:08:31 -0800452
Craig Tiller687a0e62016-09-21 10:48:59 -0700453 if (grpc_server_channel_trace && error != GRPC_ERROR_NONE) {
Craig Tillere7603b82016-07-18 15:43:42 -0700454 const char *msg = grpc_error_string(error);
455 gpr_log(GPR_INFO, "Disconnected client: %s", msg);
Craig Tillere7603b82016-07-18 15:43:42 -0700456 }
457 GRPC_ERROR_UNREF(error);
Craig Tillerdf1d3da2016-09-01 13:51:42 -0700458
Craig Tiller44b12f92016-09-08 10:06:14 -0700459 grpc_transport_op *op =
460 grpc_make_transport_op(&chand->finish_destroy_channel_closure);
Craig Tillerdf1d3da2016-09-01 13:51:42 -0700461 op->set_accept_stream = true;
462 grpc_channel_next_op(exec_ctx,
463 grpc_channel_stack_element(
464 grpc_channel_get_channel_stack(chand->channel), 0),
465 op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800466}
467
Craig Tiller88512692016-04-04 09:32:52 -0700468static void done_request_event(grpc_exec_ctx *exec_ctx, void *req,
469 grpc_cq_completion *c) {
470 requested_call *rc = req;
471 grpc_server *server = rc->server;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700472
Craig Tillerb19dbea2016-07-12 15:33:11 -0700473 if (rc >= server->requested_calls_per_cq[rc->cq_idx] &&
474 rc < server->requested_calls_per_cq[rc->cq_idx] +
475 server->max_requested_calls_per_cq) {
476 GPR_ASSERT(rc - server->requested_calls_per_cq[rc->cq_idx] <= INT_MAX);
477 gpr_stack_lockfree_push(
478 server->request_freelist_per_cq[rc->cq_idx],
479 (int)(rc - server->requested_calls_per_cq[rc->cq_idx]));
Craig Tiller88512692016-04-04 09:32:52 -0700480 } else {
481 gpr_free(req);
482 }
Craig Tiller06cb1a92016-04-04 08:10:47 -0700483
Craig Tiller88512692016-04-04 09:32:52 -0700484 server_unref(exec_ctx, server);
485}
Craig Tiller06cb1a92016-04-04 08:10:47 -0700486
Craig Tiller88512692016-04-04 09:32:52 -0700487static void publish_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tiller9f9d4222016-05-16 17:02:14 -0700488 call_data *calld, size_t cq_idx, requested_call *rc) {
Craig Tiller06cb1a92016-04-04 08:10:47 -0700489 grpc_call_set_completion_queue(exec_ctx, calld->call, rc->cq_bound_to_call);
Craig Tiller88512692016-04-04 09:32:52 -0700490 grpc_call *call = calld->call;
491 *rc->call = call;
Craig Tiller9f9d4222016-05-16 17:02:14 -0700492 calld->cq_new = server->cqs[cq_idx];
Craig Tiller06cb1a92016-04-04 08:10:47 -0700493 GPR_SWAP(grpc_metadata_array, *rc->initial_metadata, calld->initial_metadata);
494 switch (rc->type) {
495 case BATCH_CALL:
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800496 GPR_ASSERT(calld->host_set);
497 GPR_ASSERT(calld->path_set);
498 rc->data.batch.details->host = grpc_slice_ref_internal(calld->host);
499 rc->data.batch.details->method = grpc_slice_ref_internal(calld->path);
Craig Tiller06cb1a92016-04-04 08:10:47 -0700500 rc->data.batch.details->deadline = calld->deadline;
501 rc->data.batch.details->flags =
Makarand Dharmapurikard322d4b2016-08-29 12:54:19 -0700502 (calld->recv_idempotent_request
503 ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
504 : 0) |
505 (calld->recv_cacheable_request
506 ? GRPC_INITIAL_METADATA_CACHEABLE_REQUEST
507 : 0);
Craig Tiller06cb1a92016-04-04 08:10:47 -0700508 break;
509 case REGISTERED_CALL:
510 *rc->data.registered.deadline = calld->deadline;
511 if (rc->data.registered.optional_payload) {
Craig Tiller88512692016-04-04 09:32:52 -0700512 *rc->data.registered.optional_payload = calld->payload;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700513 }
514 break;
515 default:
516 GPR_UNREACHABLE_CODE(return );
517 }
518
Craig Tiller88512692016-04-04 09:32:52 -0700519 grpc_call_element *elem =
520 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
521 channel_data *chand = elem->channel_data;
522 server_ref(chand->server);
Craig Tillerf51457b2016-05-03 17:06:32 -0700523 grpc_cq_end_op(exec_ctx, calld->cq_new, rc->tag, GRPC_ERROR_NONE,
524 done_request_event, rc, &rc->completion);
Craig Tiller06cb1a92016-04-04 08:10:47 -0700525}
526
Craig Tillerf51457b2016-05-03 17:06:32 -0700527static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *arg,
528 grpc_error *error) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700529 grpc_call_element *call_elem = arg;
530 call_data *calld = call_elem->call_data;
531 channel_data *chand = call_elem->channel_data;
Craig Tiller88512692016-04-04 09:32:52 -0700532 request_matcher *rm = calld->request_matcher;
533 grpc_server *server = rm->server;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700534
Craig Tillerf51457b2016-05-03 17:06:32 -0700535 if (error != GRPC_ERROR_NONE || gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tillera82950e2015-09-22 12:33:20 -0700536 gpr_mu_lock(&calld->mu_state);
537 calld->state = ZOMBIED;
538 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -0700539 grpc_closure_init(
540 &calld->kill_zombie_closure, kill_zombie,
Craig Tiller91031da2016-12-28 15:44:25 -0800541 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0),
542 grpc_schedule_on_exec_ctx);
543 grpc_closure_sched(exec_ctx, &calld->kill_zombie_closure, error);
Craig Tillera82950e2015-09-22 12:33:20 -0700544 return;
545 }
Craig Tiller45724b32015-09-22 10:42:19 -0700546
Craig Tillerdb7c3562016-05-19 11:02:52 -0700547 for (size_t i = 0; i < server->cq_count; i++) {
548 size_t cq_idx = (chand->cq_idx + i) % server->cq_count;
549 int request_id = gpr_stack_lockfree_pop(rm->requests_per_cq[cq_idx]);
550 if (request_id == -1) {
551 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700552 } else {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700553 gpr_mu_lock(&calld->mu_state);
554 calld->state = ACTIVATED;
555 gpr_mu_unlock(&calld->mu_state);
556 publish_call(exec_ctx, server, calld, cq_idx,
Craig Tillerb19dbea2016-07-12 15:33:11 -0700557 &server->requested_calls_per_cq[cq_idx][request_id]);
Craig Tillerdb7c3562016-05-19 11:02:52 -0700558 return; /* early out */
Craig Tiller45724b32015-09-22 10:42:19 -0700559 }
Craig Tiller88512692016-04-04 09:32:52 -0700560 }
Craig Tillerdb7c3562016-05-19 11:02:52 -0700561
562 /* no cq to take the request found: queue it on the slow list */
563 gpr_mu_lock(&server->mu_call);
564 gpr_mu_lock(&calld->mu_state);
565 calld->state = PENDING;
566 gpr_mu_unlock(&calld->mu_state);
567 if (rm->pending_head == NULL) {
568 rm->pending_tail = rm->pending_head = calld;
569 } else {
570 rm->pending_tail->pending_next = calld;
571 rm->pending_tail = calld;
572 }
573 calld->pending_next = NULL;
574 gpr_mu_unlock(&server->mu_call);
Craig Tiller88512692016-04-04 09:32:52 -0700575}
576
577static void finish_start_new_rpc(
578 grpc_exec_ctx *exec_ctx, grpc_server *server, grpc_call_element *elem,
579 request_matcher *rm,
580 grpc_server_register_method_payload_handling payload_handling) {
581 call_data *calld = elem->call_data;
582
583 if (gpr_atm_acq_load(&server->shutdown_flag)) {
584 gpr_mu_lock(&calld->mu_state);
585 calld->state = ZOMBIED;
586 gpr_mu_unlock(&calld->mu_state);
Craig Tiller91031da2016-12-28 15:44:25 -0800587 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem,
588 grpc_schedule_on_exec_ctx);
589 grpc_closure_sched(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE);
Craig Tiller88512692016-04-04 09:32:52 -0700590 return;
591 }
592
593 calld->request_matcher = rm;
594
595 switch (payload_handling) {
596 case GRPC_SRM_PAYLOAD_NONE:
Craig Tiller48abdde2016-05-24 06:55:28 -0700597 publish_new_rpc(exec_ctx, elem, GRPC_ERROR_NONE);
Craig Tiller88512692016-04-04 09:32:52 -0700598 break;
599 case GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER: {
600 grpc_op op;
601 memset(&op, 0, sizeof(op));
602 op.op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth448c1f02017-01-25 10:44:30 -0800603 op.data.recv_message.recv_message = &calld->payload;
Craig Tiller91031da2016-12-28 15:44:25 -0800604 grpc_closure_init(&calld->publish, publish_new_rpc, elem,
605 grpc_schedule_on_exec_ctx);
Craig Tiller88512692016-04-04 09:32:52 -0700606 grpc_call_start_batch_and_execute(exec_ctx, calld->call, &op, 1,
607 &calld->publish);
608 break;
609 }
Craig Tillera82950e2015-09-22 12:33:20 -0700610 }
Craig Tiller04cc8be2015-02-10 16:11:22 -0800611}
612
Craig Tillera82950e2015-09-22 12:33:20 -0700613static void start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800614 channel_data *chand = elem->channel_data;
615 call_data *calld = elem->call_data;
616 grpc_server *server = chand->server;
Craig Tiller7536af02015-12-22 13:49:30 -0800617 uint32_t i;
618 uint32_t hash;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800619 channel_registered_method *rm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800620
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800621 if (chand->registered_methods && calld->path_set && calld->host_set) {
Craig Tillera82950e2015-09-22 12:33:20 -0700622 /* TODO(ctiller): unify these two searches */
623 /* check for an exact match with host */
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800624 hash = GRPC_MDSTR_KV_HASH(grpc_slice_hash(calld->host),
625 grpc_slice_hash(calld->path));
Craig Tillera82950e2015-09-22 12:33:20 -0700626 for (i = 0; i <= chand->registered_method_max_probes; i++) {
627 rm = &chand->registered_methods[(hash + i) %
628 chand->registered_method_slots];
629 if (!rm) break;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800630 if (!rm->has_host) continue;
631 if (!grpc_slice_eq(rm->host, calld->host)) continue;
632 if (!grpc_slice_eq(rm->method, calld->path)) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800633 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800634 !calld->recv_idempotent_request) {
Craig Tillerb2906862016-03-10 06:50:07 -0800635 continue;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800636 }
Craig Tillera82950e2015-09-22 12:33:20 -0700637 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700638 &rm->server_registered_method->request_matcher,
639 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700640 return;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800641 }
Craig Tillera82950e2015-09-22 12:33:20 -0700642 /* check for a wildcard method definition (no host set) */
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800643 hash = GRPC_MDSTR_KV_HASH(0, grpc_slice_hash(calld->path));
Craig Tillera82950e2015-09-22 12:33:20 -0700644 for (i = 0; i <= chand->registered_method_max_probes; i++) {
645 rm = &chand->registered_methods[(hash + i) %
646 chand->registered_method_slots];
647 if (!rm) break;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800648 if (rm->has_host) continue;
649 if (!grpc_slice_eq(rm->method, calld->path)) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800650 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800651 !calld->recv_idempotent_request) {
Craig Tillerb2906862016-03-10 06:50:07 -0800652 continue;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800653 }
Craig Tillera82950e2015-09-22 12:33:20 -0700654 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700655 &rm->server_registered_method->request_matcher,
656 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700657 return;
658 }
659 }
660 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700661 &server->unregistered_request_matcher,
662 GRPC_SRM_PAYLOAD_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800663}
664
Craig Tillera82950e2015-09-22 12:33:20 -0700665static int num_listeners(grpc_server *server) {
Craig Tilleree945e82015-05-26 16:15:34 -0700666 listener *l;
667 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700668 for (l = server->listeners; l; l = l->next) {
669 n++;
670 }
Craig Tilleree945e82015-05-26 16:15:34 -0700671 return n;
672}
673
Craig Tillera82950e2015-09-22 12:33:20 -0700674static void done_shutdown_event(grpc_exec_ctx *exec_ctx, void *server,
675 grpc_cq_completion *completion) {
676 server_unref(exec_ctx, server);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700677}
678
Craig Tillera82950e2015-09-22 12:33:20 -0700679static int num_channels(grpc_server *server) {
Craig Tillerab54f792015-07-08 08:34:20 -0700680 channel_data *chand;
681 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700682 for (chand = server->root_channel_data.next;
683 chand != &server->root_channel_data; chand = chand->next) {
684 n++;
685 }
Craig Tillerab54f792015-07-08 08:34:20 -0700686 return n;
687}
688
Craig Tillera82950e2015-09-22 12:33:20 -0700689static void kill_pending_work_locked(grpc_exec_ctx *exec_ctx,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700690 grpc_server *server, grpc_error *error) {
Craig Tiller88ef00e2016-05-17 09:31:49 -0700691 if (server->started) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700692 request_matcher_kill_requests(exec_ctx, server,
Craig Tiller48abdde2016-05-24 06:55:28 -0700693 &server->unregistered_request_matcher,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700694 GRPC_ERROR_REF(error));
Craig Tillerdb7c3562016-05-19 11:02:52 -0700695 request_matcher_zombify_all_pending_calls(
696 exec_ctx, &server->unregistered_request_matcher);
697 for (registered_method *rm = server->registered_methods; rm;
698 rm = rm->next) {
Craig Tiller48abdde2016-05-24 06:55:28 -0700699 request_matcher_kill_requests(exec_ctx, server, &rm->request_matcher,
700 GRPC_ERROR_REF(error));
Craig Tillerdb7c3562016-05-19 11:02:52 -0700701 request_matcher_zombify_all_pending_calls(exec_ctx, &rm->request_matcher);
Craig Tiller418a8212016-05-16 16:27:51 -0700702 }
Craig Tillera82950e2015-09-22 12:33:20 -0700703 }
Craig Tillercae4b1b2016-05-10 09:11:09 -0700704 GRPC_ERROR_UNREF(error);
Craig Tillerdc627722015-05-26 15:27:02 -0700705}
706
Craig Tillera82950e2015-09-22 12:33:20 -0700707static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx,
708 grpc_server *server) {
Craig Tiller45724b32015-09-22 10:42:19 -0700709 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700710 if (!gpr_atm_acq_load(&server->shutdown_flag) || server->shutdown_published) {
711 return;
712 }
Craig Tiller45724b32015-09-22 10:42:19 -0700713
Craig Tillercae4b1b2016-05-10 09:11:09 -0700714 kill_pending_work_locked(exec_ctx, server,
715 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tiller45724b32015-09-22 10:42:19 -0700716
Craig Tillera82950e2015-09-22 12:33:20 -0700717 if (server->root_channel_data.next != &server->root_channel_data ||
718 server->listeners_destroyed < num_listeners(server)) {
719 if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME),
720 server->last_shutdown_message_time),
721 gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
722 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
723 gpr_log(GPR_DEBUG,
724 "Waiting for %d channels and %d/%d listeners to be destroyed"
725 " before shutting down server",
726 num_channels(server),
727 num_listeners(server) - server->listeners_destroyed,
728 num_listeners(server));
Craig Tiller45724b32015-09-22 10:42:19 -0700729 }
Craig Tillera82950e2015-09-22 12:33:20 -0700730 return;
731 }
Craig Tiller45724b32015-09-22 10:42:19 -0700732 server->shutdown_published = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700733 for (i = 0; i < server->num_shutdown_tags; i++) {
734 server_ref(server);
735 grpc_cq_end_op(exec_ctx, server->shutdown_tags[i].cq,
Craig Tillerf51457b2016-05-03 17:06:32 -0700736 server->shutdown_tags[i].tag, GRPC_ERROR_NONE,
737 done_shutdown_event, server,
Craig Tillera82950e2015-09-22 12:33:20 -0700738 &server->shutdown_tags[i].completion);
739 }
Craig Tiller45724b32015-09-22 10:42:19 -0700740}
741
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800742static void server_on_recv_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700743 grpc_error *error) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700744 grpc_call_element *elem = ptr;
Craig Tiller6902ad22015-04-16 08:01:49 -0700745 call_data *calld = elem->call_data;
Craig Tiller94329d02015-07-23 09:52:11 -0700746 gpr_timespec op_deadline;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700747
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800748 if (error == GRPC_ERROR_NONE) {
749 GPR_ASSERT(calld->recv_initial_metadata->idx.named.path != NULL);
750 GPR_ASSERT(calld->recv_initial_metadata->idx.named.authority != NULL);
751 calld->path = grpc_slice_ref_internal(
752 GRPC_MDVALUE(calld->recv_initial_metadata->idx.named.path->md));
753 calld->host = grpc_slice_ref_internal(
754 GRPC_MDVALUE(calld->recv_initial_metadata->idx.named.authority->md));
755 calld->path_set = true;
756 calld->host_set = true;
757 grpc_metadata_batch_remove(exec_ctx, calld->recv_initial_metadata,
758 calld->recv_initial_metadata->idx.named.path);
759 grpc_metadata_batch_remove(
760 exec_ctx, calld->recv_initial_metadata,
761 calld->recv_initial_metadata->idx.named.authority);
762 } else {
763 GRPC_ERROR_REF(error);
764 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800765 op_deadline = calld->recv_initial_metadata->deadline;
766 if (0 != gpr_time_cmp(op_deadline, gpr_inf_future(op_deadline.clock_type))) {
767 calld->deadline = op_deadline;
768 }
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800769 if (calld->host_set && calld->path_set) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800770 /* do nothing */
771 } else {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800772 grpc_error *src_error = error;
Craig Tillerf51457b2016-05-03 17:06:32 -0700773 error =
774 GRPC_ERROR_CREATE_REFERENCING("Missing :authority or :path", &error, 1);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800775 GRPC_ERROR_UNREF(src_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700776 }
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700777
Craig Tillerde2c41c2016-09-01 15:08:08 -0700778 grpc_closure_run(exec_ctx, calld->on_done_recv_initial_metadata, error);
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700779}
780
Craig Tillera82950e2015-09-22 12:33:20 -0700781static void server_mutate_op(grpc_call_element *elem,
782 grpc_transport_stream_op *op) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700783 call_data *calld = elem->call_data;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700784
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800785 if (op->recv_initial_metadata != NULL) {
Craig Tiller4d40ba32016-03-09 17:48:40 -0800786 GPR_ASSERT(op->recv_idempotent_request == NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800787 calld->recv_initial_metadata = op->recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800788 calld->on_done_recv_initial_metadata = op->recv_initial_metadata_ready;
789 op->recv_initial_metadata_ready = &calld->server_on_recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800790 op->recv_idempotent_request = &calld->recv_idempotent_request;
Makarand Dharmapurikard322d4b2016-08-29 12:54:19 -0700791 op->recv_cacheable_request = &calld->recv_cacheable_request;
Craig Tillera82950e2015-09-22 12:33:20 -0700792 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700793}
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700794
Craig Tillera82950e2015-09-22 12:33:20 -0700795static void server_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
796 grpc_call_element *elem,
797 grpc_transport_stream_op *op) {
798 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
799 server_mutate_op(elem, op);
800 grpc_call_next_op(exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800801}
802
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800803static void got_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700804 grpc_error *error) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800805 grpc_call_element *elem = ptr;
806 call_data *calld = elem->call_data;
Craig Tillerf51457b2016-05-03 17:06:32 -0700807 if (error == GRPC_ERROR_NONE) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800808 start_new_rpc(exec_ctx, elem);
809 } else {
810 gpr_mu_lock(&calld->mu_state);
811 if (calld->state == NOT_STARTED) {
812 calld->state = ZOMBIED;
813 gpr_mu_unlock(&calld->mu_state);
Craig Tiller91031da2016-12-28 15:44:25 -0800814 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem,
815 grpc_schedule_on_exec_ctx);
816 grpc_closure_sched(exec_ctx, &calld->kill_zombie_closure,
817 GRPC_ERROR_NONE);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800818 } else if (calld->state == PENDING) {
819 calld->state = ZOMBIED;
820 gpr_mu_unlock(&calld->mu_state);
821 /* zombied call will be destroyed when it's removed from the pending
822 queue... later */
823 } else {
824 gpr_mu_unlock(&calld->mu_state);
825 }
826 }
827}
828
829static void accept_stream(grpc_exec_ctx *exec_ctx, void *cd,
830 grpc_transport *transport,
Craig Tillera82950e2015-09-22 12:33:20 -0700831 const void *transport_server_data) {
Craig Tillere039f032015-06-25 12:54:23 -0700832 channel_data *chand = cd;
833 /* create a call */
Craig Tiller8e214652016-08-19 09:54:31 -0700834 grpc_call_create_args args;
835 memset(&args, 0, sizeof(args));
836 args.channel = chand->channel;
837 args.server_transport_data = transport_server_data;
838 args.send_deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
839 grpc_call *call;
Craig Tillera59c16c2016-10-31 07:25:01 -0700840 grpc_error *error = grpc_call_create(exec_ctx, &args, &call);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800841 grpc_call_element *elem =
842 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
Craig Tiller8e214652016-08-19 09:54:31 -0700843 if (error != GRPC_ERROR_NONE) {
844 got_initial_metadata(exec_ctx, elem, error);
845 GRPC_ERROR_UNREF(error);
846 return;
847 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800848 call_data *calld = elem->call_data;
849 grpc_op op;
850 memset(&op, 0, sizeof(op));
851 op.op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth448c1f02017-01-25 10:44:30 -0800852 op.data.recv_initial_metadata.recv_initial_metadata =
853 &calld->initial_metadata;
Craig Tiller91031da2016-12-28 15:44:25 -0800854 grpc_closure_init(&calld->got_initial_metadata, got_initial_metadata, elem,
855 grpc_schedule_on_exec_ctx);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800856 grpc_call_start_batch_and_execute(exec_ctx, call, &op, 1,
857 &calld->got_initial_metadata);
Craig Tillere039f032015-06-25 12:54:23 -0700858}
859
Craig Tillera82950e2015-09-22 12:33:20 -0700860static void channel_connectivity_changed(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700861 grpc_error *error) {
Craig Tillere039f032015-06-25 12:54:23 -0700862 channel_data *chand = cd;
863 grpc_server *server = chand->server;
Craig Tiller48ed92e2016-06-02 11:07:12 -0700864 if (chand->connectivity_state != GRPC_CHANNEL_SHUTDOWN) {
Craig Tillere0221ff2016-07-11 15:56:08 -0700865 grpc_transport_op *op = grpc_make_transport_op(NULL);
866 op->on_connectivity_state_change = &chand->channel_connectivity_changed,
867 op->connectivity_state = &chand->connectivity_state;
Craig Tillera82950e2015-09-22 12:33:20 -0700868 grpc_channel_next_op(exec_ctx,
869 grpc_channel_stack_element(
870 grpc_channel_get_channel_stack(chand->channel), 0),
Craig Tillere0221ff2016-07-11 15:56:08 -0700871 op);
Craig Tillera82950e2015-09-22 12:33:20 -0700872 } else {
873 gpr_mu_lock(&server->mu_global);
Craig Tiller9d018482016-07-18 08:53:49 -0700874 destroy_channel(exec_ctx, chand, GRPC_ERROR_REF(error));
Craig Tillera82950e2015-09-22 12:33:20 -0700875 gpr_mu_unlock(&server->mu_global);
876 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "connectivity");
877 }
Craig Tillere039f032015-06-25 12:54:23 -0700878}
879
Mark D. Roth76d24422016-06-23 13:22:10 -0700880static grpc_error *init_call_elem(grpc_exec_ctx *exec_ctx,
Mark D. Roth0badbe82016-06-23 10:15:12 -0700881 grpc_call_element *elem,
Craig Tillerc52ba3a2017-02-15 22:57:43 -0800882 const grpc_call_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800883 call_data *calld = elem->call_data;
884 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700885 memset(calld, 0, sizeof(call_data));
886 calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
887 calld->call = grpc_call_from_top_element(elem);
888 gpr_mu_init(&calld->mu_state);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800889
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800890 grpc_closure_init(&calld->server_on_recv_initial_metadata,
Craig Tiller91031da2016-12-28 15:44:25 -0800891 server_on_recv_initial_metadata, elem,
892 grpc_schedule_on_exec_ctx);
Craig Tiller1e6facb2015-06-11 22:47:11 -0700893
Craig Tillera82950e2015-09-22 12:33:20 -0700894 server_ref(chand->server);
Mark D. Roth0badbe82016-06-23 10:15:12 -0700895 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800896}
897
Craig Tiller2c8063c2016-03-22 22:12:15 -0700898static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
David Garcia Quintas5dde14c2016-07-28 17:29:27 -0700899 const grpc_call_final_info *final_info,
900 void *ignored) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800901 channel_data *chand = elem->channel_data;
Craig Tillerdb7db992015-01-29 11:19:01 -0800902 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800903
Craig Tillera82950e2015-09-22 12:33:20 -0700904 GPR_ASSERT(calld->state != PENDING);
Craig Tiller092d8d12015-07-04 22:35:00 -0700905
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800906 if (calld->host_set) {
907 grpc_slice_unref_internal(exec_ctx, calld->host);
Craig Tillera82950e2015-09-22 12:33:20 -0700908 }
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800909 if (calld->path_set) {
910 grpc_slice_unref_internal(exec_ctx, calld->path);
Craig Tillera82950e2015-09-22 12:33:20 -0700911 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800912 grpc_metadata_array_destroy(&calld->initial_metadata);
Craig Tiller4df31a62015-01-30 09:44:31 -0800913
Craig Tillera82950e2015-09-22 12:33:20 -0700914 gpr_mu_destroy(&calld->mu_state);
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700915
Craig Tillera82950e2015-09-22 12:33:20 -0700916 server_unref(exec_ctx, chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800917}
918
Mark D. Rothc1087882016-11-18 10:54:45 -0800919static grpc_error *init_channel_elem(grpc_exec_ctx *exec_ctx,
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800920 grpc_channel_element *elem,
921 grpc_channel_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800922 channel_data *chand = elem->channel_data;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800923 GPR_ASSERT(args->is_first);
924 GPR_ASSERT(!args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800925 chand->server = NULL;
926 chand->channel = NULL;
927 chand->next = chand->prev = chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800928 chand->registered_methods = NULL;
Craig Tillere039f032015-06-25 12:54:23 -0700929 chand->connectivity_state = GRPC_CHANNEL_IDLE;
Craig Tillera82950e2015-09-22 12:33:20 -0700930 grpc_closure_init(&chand->channel_connectivity_changed,
Craig Tiller91031da2016-12-28 15:44:25 -0800931 channel_connectivity_changed, chand,
932 grpc_schedule_on_exec_ctx);
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800933 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800934}
935
Craig Tillera82950e2015-09-22 12:33:20 -0700936static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
937 grpc_channel_element *elem) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800938 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800939 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700940 if (chand->registered_methods) {
941 for (i = 0; i < chand->registered_method_slots; i++) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800942 grpc_slice_unref_internal(exec_ctx, chand->registered_methods[i].method);
943 if (chand->registered_methods[i].has_host) {
944 grpc_slice_unref_internal(exec_ctx, chand->registered_methods[i].host);
Craig Tillera82950e2015-09-22 12:33:20 -0700945 }
Craig Tillerec3257c2015-02-12 15:59:43 -0800946 }
Craig Tillera82950e2015-09-22 12:33:20 -0700947 gpr_free(chand->registered_methods);
948 }
949 if (chand->server) {
950 gpr_mu_lock(&chand->server->mu_global);
951 chand->next->prev = chand->prev;
952 chand->prev->next = chand->next;
953 chand->next = chand->prev = chand;
954 maybe_finish_shutdown(exec_ctx, chand->server);
955 gpr_mu_unlock(&chand->server->mu_global);
Craig Tillera82950e2015-09-22 12:33:20 -0700956 server_unref(exec_ctx, chand->server);
957 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800958}
959
Craig Tiller178edfa2016-02-17 20:54:46 -0800960const grpc_channel_filter grpc_server_top_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700961 server_start_transport_stream_op,
962 grpc_channel_next_op,
963 sizeof(call_data),
964 init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700965 grpc_call_stack_ignore_set_pollset_or_pollset_set,
Craig Tillerf40df232016-03-25 13:38:14 -0700966 destroy_call_elem,
967 sizeof(channel_data),
968 init_channel_elem,
969 destroy_channel_elem,
970 grpc_call_next_get_peer,
Mark D. Rothb2d24882016-10-27 15:44:07 -0700971 grpc_channel_next_get_info,
Craig Tillerf40df232016-03-25 13:38:14 -0700972 "server",
Craig Tiller9f28ac22015-01-27 17:01:29 -0800973};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800974
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700975static void register_completion_queue(grpc_server *server,
976 grpc_completion_queue *cq,
977 bool is_non_listening, void *reserved) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800978 size_t i, n;
Craig Tillera82950e2015-09-22 12:33:20 -0700979 GPR_ASSERT(!reserved);
980 for (i = 0; i < server->cq_count; i++) {
981 if (server->cqs[i] == cq) return;
982 }
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700983
Craig Tillera82950e2015-09-22 12:33:20 -0700984 grpc_cq_mark_server_cq(cq);
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700985
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700986 if (is_non_listening) {
987 grpc_cq_mark_non_listening_server_cq(cq);
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700988 }
Craig Tiller509b30e2016-05-21 12:32:39 -0700989
990 GRPC_CQ_INTERNAL_REF(cq, "server");
Craig Tiller20bc56d2015-02-12 09:02:56 -0800991 n = server->cq_count++;
Craig Tillera82950e2015-09-22 12:33:20 -0700992 server->cqs = gpr_realloc(server->cqs,
993 server->cq_count * sizeof(grpc_completion_queue *));
Craig Tiller20bc56d2015-02-12 09:02:56 -0800994 server->cqs[n] = cq;
995}
996
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700997void grpc_server_register_completion_queue(grpc_server *server,
998 grpc_completion_queue *cq,
999 void *reserved) {
1000 GRPC_API_TRACE(
1001 "grpc_server_register_completion_queue(server=%p, cq=%p, reserved=%p)", 3,
1002 (server, cq, reserved));
1003 register_completion_queue(server, cq, false, reserved);
1004}
1005
1006void grpc_server_register_non_listening_completion_queue(
1007 grpc_server *server, grpc_completion_queue *cq, void *reserved) {
1008 GRPC_API_TRACE(
1009 "grpc_server_register_non_listening_completion_queue(server=%p, cq=%p, "
1010 "reserved=%p)",
1011 3, (server, cq, reserved));
1012 register_completion_queue(server, cq, true, reserved);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001013}
1014
Craig Tiller178edfa2016-02-17 20:54:46 -08001015grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved) {
Craig Tiller178edfa2016-02-17 20:54:46 -08001016 GRPC_API_TRACE("grpc_server_create(%p, %p)", 2, (args, reserved));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001017
Craig Tillera82950e2015-09-22 12:33:20 -07001018 grpc_server *server = gpr_malloc(sizeof(grpc_server));
Craig Tiller60fd3612015-03-05 16:24:22 -08001019
Craig Tillera82950e2015-09-22 12:33:20 -07001020 GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
Craig Tiller60fd3612015-03-05 16:24:22 -08001021
Craig Tillera82950e2015-09-22 12:33:20 -07001022 memset(server, 0, sizeof(grpc_server));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001023
Craig Tillera82950e2015-09-22 12:33:20 -07001024 gpr_mu_init(&server->mu_global);
1025 gpr_mu_init(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001026
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001027 /* decremented by grpc_server_destroy */
Craig Tillera82950e2015-09-22 12:33:20 -07001028 gpr_ref_init(&server->internal_refcount, 1);
1029 server->root_channel_data.next = server->root_channel_data.prev =
1030 &server->root_channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001031
Craig Tiller6a006ce2015-07-13 16:25:40 -07001032 /* TODO(ctiller): expose a channel_arg for this */
Craig Tillerb19dbea2016-07-12 15:33:11 -07001033 server->max_requested_calls_per_cq = 32768;
Craig Tillera82950e2015-09-22 12:33:20 -07001034 server->channel_args = grpc_channel_args_copy(args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001035
1036 return server;
1037}
1038
Craig Tillera82950e2015-09-22 12:33:20 -07001039static int streq(const char *a, const char *b) {
1040 if (a == NULL && b == NULL) return 1;
1041 if (a == NULL) return 0;
1042 if (b == NULL) return 0;
1043 return 0 == strcmp(a, b);
Craig Tiller24be0f72015-02-10 14:04:22 -08001044}
1045
Craig Tiller06cb1a92016-04-04 08:10:47 -07001046void *grpc_server_register_method(
1047 grpc_server *server, const char *method, const char *host,
1048 grpc_server_register_method_payload_handling payload_handling,
1049 uint32_t flags) {
Craig Tiller24be0f72015-02-10 14:04:22 -08001050 registered_method *m;
Craig Tillerb2906862016-03-10 06:50:07 -08001051 GRPC_API_TRACE(
1052 "grpc_server_register_method(server=%p, method=%s, host=%s, "
1053 "flags=0x%08x)",
1054 4, (server, method, host, flags));
Craig Tillera82950e2015-09-22 12:33:20 -07001055 if (!method) {
1056 gpr_log(GPR_ERROR,
1057 "grpc_server_register_method method string cannot be NULL");
1058 return NULL;
1059 }
1060 for (m = server->registered_methods; m; m = m->next) {
1061 if (streq(m->method, method) && streq(m->host, host)) {
1062 gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method,
1063 host ? host : "*");
Craig Tiller24be0f72015-02-10 14:04:22 -08001064 return NULL;
1065 }
Craig Tillera82950e2015-09-22 12:33:20 -07001066 }
Craig Tillerb2906862016-03-10 06:50:07 -08001067 if ((flags & ~GRPC_INITIAL_METADATA_USED_MASK) != 0) {
1068 gpr_log(GPR_ERROR, "grpc_server_register_method invalid flags 0x%08x",
1069 flags);
1070 return NULL;
1071 }
Craig Tillera82950e2015-09-22 12:33:20 -07001072 m = gpr_malloc(sizeof(registered_method));
1073 memset(m, 0, sizeof(*m));
Craig Tillera82950e2015-09-22 12:33:20 -07001074 m->method = gpr_strdup(method);
1075 m->host = gpr_strdup(host);
Craig Tiller24be0f72015-02-10 14:04:22 -08001076 m->next = server->registered_methods;
Craig Tiller5ada3d22016-04-04 08:30:59 -07001077 m->payload_handling = payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -08001078 m->flags = flags;
Craig Tiller24be0f72015-02-10 14:04:22 -08001079 server->registered_methods = m;
1080 return m;
1081}
1082
Craig Tillera82950e2015-09-22 12:33:20 -07001083void grpc_server_start(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001084 listener *l;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001085 size_t i;
Craig Tillerf5768a62015-09-22 10:54:34 -07001086 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001087
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001088 GRPC_API_TRACE("grpc_server_start(server=%p)", 1, (server));
1089
Craig Tiller88ef00e2016-05-17 09:31:49 -07001090 server->started = true;
Craig Tiller1d0fce92016-10-26 08:26:22 -07001091 server->pollset_count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001092 server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count);
Craig Tillerb19dbea2016-07-12 15:33:11 -07001093 server->request_freelist_per_cq =
1094 gpr_malloc(sizeof(*server->request_freelist_per_cq) * server->cq_count);
1095 server->requested_calls_per_cq =
1096 gpr_malloc(sizeof(*server->requested_calls_per_cq) * server->cq_count);
Craig Tillera82950e2015-09-22 12:33:20 -07001097 for (i = 0; i < server->cq_count; i++) {
Craig Tiller509b30e2016-05-21 12:32:39 -07001098 if (!grpc_cq_is_non_listening_server_cq(server->cqs[i])) {
Craig Tiller1d0fce92016-10-26 08:26:22 -07001099 server->pollsets[server->pollset_count++] =
1100 grpc_cq_pollset(server->cqs[i]);
Craig Tiller509b30e2016-05-21 12:32:39 -07001101 }
Craig Tillerb19dbea2016-07-12 15:33:11 -07001102 server->request_freelist_per_cq[i] =
1103 gpr_stack_lockfree_create((size_t)server->max_requested_calls_per_cq);
1104 for (int j = 0; j < server->max_requested_calls_per_cq; j++) {
1105 gpr_stack_lockfree_push(server->request_freelist_per_cq[i], j);
1106 }
1107 server->requested_calls_per_cq[i] =
1108 gpr_malloc((size_t)server->max_requested_calls_per_cq *
1109 sizeof(*server->requested_calls_per_cq[i]));
Craig Tillerdb7c3562016-05-19 11:02:52 -07001110 }
1111 request_matcher_init(&server->unregistered_request_matcher,
Craig Tillerb19dbea2016-07-12 15:33:11 -07001112 (size_t)server->max_requested_calls_per_cq, server);
Craig Tillerdb7c3562016-05-19 11:02:52 -07001113 for (registered_method *rm = server->registered_methods; rm; rm = rm->next) {
Craig Tillerb19dbea2016-07-12 15:33:11 -07001114 request_matcher_init(&rm->request_matcher,
1115 (size_t)server->max_requested_calls_per_cq, server);
Craig Tillera82950e2015-09-22 12:33:20 -07001116 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001117
Craig Tillera82950e2015-09-22 12:33:20 -07001118 for (l = server->listeners; l; l = l->next) {
Craig Tiller1d0fce92016-10-26 08:26:22 -07001119 l->start(&exec_ctx, server, l->arg, server->pollsets,
1120 server->pollset_count);
Craig Tillera82950e2015-09-22 12:33:20 -07001121 }
Craig Tillerdfff1b82015-09-21 14:39:57 -07001122
Craig Tillera82950e2015-09-22 12:33:20 -07001123 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001124}
1125
Sree Kuchibhotlacc357bf2016-08-22 16:08:50 -07001126void grpc_server_get_pollsets(grpc_server *server, grpc_pollset ***pollsets,
1127 size_t *pollset_count) {
Craig Tiller1d0fce92016-10-26 08:26:22 -07001128 *pollset_count = server->pollset_count;
Sree Kuchibhotlacc357bf2016-08-22 16:08:50 -07001129 *pollsets = server->pollsets;
1130}
1131
Craig Tillera82950e2015-09-22 12:33:20 -07001132void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *s,
1133 grpc_transport *transport,
Craig Tiller418a8212016-05-16 16:27:51 -07001134 grpc_pollset *accepting_pollset,
Craig Tillera82950e2015-09-22 12:33:20 -07001135 const grpc_channel_args *args) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001136 size_t num_registered_methods;
1137 size_t alloc;
1138 registered_method *rm;
1139 channel_registered_method *crm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001140 grpc_channel *channel;
1141 channel_data *chand;
Craig Tiller7536af02015-12-22 13:49:30 -08001142 uint32_t hash;
Craig Tillerf96dfc32015-09-10 14:43:18 -07001143 size_t slots;
Craig Tiller7536af02015-12-22 13:49:30 -08001144 uint32_t probes;
1145 uint32_t max_probes = 0;
Craig Tillere0221ff2016-07-11 15:56:08 -07001146 grpc_transport_op *op = NULL;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001147
Craig Tiller178edfa2016-02-17 20:54:46 -08001148 channel =
1149 grpc_channel_create(exec_ctx, NULL, args, GRPC_SERVER_CHANNEL, transport);
Craig Tillera82950e2015-09-22 12:33:20 -07001150 chand = (channel_data *)grpc_channel_stack_element(
Craig Tillerf40df232016-03-25 13:38:14 -07001151 grpc_channel_get_channel_stack(channel), 0)
1152 ->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001153 chand->server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001154 server_ref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001155 chand->channel = channel;
1156
Craig Tiller9f9d4222016-05-16 17:02:14 -07001157 size_t cq_idx;
1158 grpc_completion_queue *accepting_cq = grpc_cq_from_pollset(accepting_pollset);
1159 for (cq_idx = 0; cq_idx < s->cq_count; cq_idx++) {
1160 if (s->cqs[cq_idx] == accepting_cq) break;
1161 }
1162 if (cq_idx == s->cq_count) {
1163 /* completion queue not found: pick a random one to publish new calls to */
1164 cq_idx = (size_t)rand() % s->cq_count;
1165 }
1166 chand->cq_idx = cq_idx;
1167
Craig Tiller04cc8be2015-02-10 16:11:22 -08001168 num_registered_methods = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001169 for (rm = s->registered_methods; rm; rm = rm->next) {
1170 num_registered_methods++;
1171 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001172 /* build a lookup table phrased in terms of mdstr's in this channels context
1173 to quickly find registered methods */
Craig Tillera82950e2015-09-22 12:33:20 -07001174 if (num_registered_methods > 0) {
1175 slots = 2 * num_registered_methods;
1176 alloc = sizeof(channel_registered_method) * slots;
1177 chand->registered_methods = gpr_malloc(alloc);
1178 memset(chand->registered_methods, 0, alloc);
1179 for (rm = s->registered_methods; rm; rm = rm->next) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -08001180 grpc_slice host;
1181 bool has_host;
1182 grpc_slice method;
1183 if (rm->host != NULL) {
1184 host = grpc_slice_intern(grpc_slice_from_static_string(rm->host));
1185 has_host = true;
1186 } else {
1187 has_host = false;
1188 }
1189 method = grpc_slice_intern(grpc_slice_from_static_string(rm->method));
1190 hash = GRPC_MDSTR_KV_HASH(has_host ? grpc_slice_hash(host) : 0,
1191 grpc_slice_hash(method));
Craig Tillera82950e2015-09-22 12:33:20 -07001192 for (probes = 0; chand->registered_methods[(hash + probes) % slots]
Craig Tillerf40df232016-03-25 13:38:14 -07001193 .server_registered_method != NULL;
Craig Tillera82950e2015-09-22 12:33:20 -07001194 probes++)
1195 ;
1196 if (probes > max_probes) max_probes = probes;
1197 crm = &chand->registered_methods[(hash + probes) % slots];
1198 crm->server_registered_method = rm;
Craig Tillerb2906862016-03-10 06:50:07 -08001199 crm->flags = rm->flags;
Craig Tiller7c70b6c2017-01-23 07:48:42 -08001200 crm->has_host = has_host;
Eduardo Caceresa2b0e562017-02-21 20:33:40 +01001201 if (has_host) {
Eduardo Cáceres de la Calleca468792017-02-20 18:42:17 +01001202 crm->host = host;
1203 }
Craig Tillera82950e2015-09-22 12:33:20 -07001204 crm->method = method;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001205 }
Craig Tiller7536af02015-12-22 13:49:30 -08001206 GPR_ASSERT(slots <= UINT32_MAX);
1207 chand->registered_method_slots = (uint32_t)slots;
Craig Tillera82950e2015-09-22 12:33:20 -07001208 chand->registered_method_max_probes = max_probes;
1209 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001210
Craig Tillera82950e2015-09-22 12:33:20 -07001211 gpr_mu_lock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001212 chand->next = &s->root_channel_data;
1213 chand->prev = chand->next->prev;
1214 chand->next->prev = chand->prev->next = chand;
Craig Tillera82950e2015-09-22 12:33:20 -07001215 gpr_mu_unlock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001216
Craig Tillera82950e2015-09-22 12:33:20 -07001217 GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity");
Craig Tillere0221ff2016-07-11 15:56:08 -07001218 op = grpc_make_transport_op(NULL);
1219 op->set_accept_stream = true;
1220 op->set_accept_stream_fn = accept_stream;
1221 op->set_accept_stream_user_data = chand;
1222 op->on_connectivity_state_change = &chand->channel_connectivity_changed;
1223 op->connectivity_state = &chand->connectivity_state;
Craig Tiller804ff712016-05-05 16:25:40 -07001224 if (gpr_atm_acq_load(&s->shutdown_flag) != 0) {
Craig Tillere0221ff2016-07-11 15:56:08 -07001225 op->disconnect_with_error = GRPC_ERROR_CREATE("Server shutdown");
Craig Tiller804ff712016-05-05 16:25:40 -07001226 }
Craig Tillere0221ff2016-07-11 15:56:08 -07001227 grpc_transport_perform_op(exec_ctx, transport, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001228}
1229
Craig Tillera82950e2015-09-22 12:33:20 -07001230void done_published_shutdown(grpc_exec_ctx *exec_ctx, void *done_arg,
1231 grpc_cq_completion *storage) {
1232 (void)done_arg;
1233 gpr_free(storage);
murgatroid9900a3dab2015-08-19 11:15:38 -07001234}
1235
Craig Tillera82950e2015-09-22 12:33:20 -07001236static void listener_destroy_done(grpc_exec_ctx *exec_ctx, void *s,
Craig Tillerf51457b2016-05-03 17:06:32 -07001237 grpc_error *error) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001238 grpc_server *server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001239 gpr_mu_lock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001240 server->listeners_destroyed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001241 maybe_finish_shutdown(exec_ctx, server);
1242 gpr_mu_unlock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001243}
1244
Craig Tillera82950e2015-09-22 12:33:20 -07001245void grpc_server_shutdown_and_notify(grpc_server *server,
1246 grpc_completion_queue *cq, void *tag) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001247 listener *l;
Craig Tillerbce999f2015-05-27 09:55:51 -07001248 shutdown_tag *sdt;
Craig Tillerff3ae682015-06-29 17:44:04 -07001249 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001250 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001251
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001252 GRPC_API_TRACE("grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", 3,
1253 (server, cq, tag));
1254
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001255 /* lock, and gather up some stuff to do */
Craig Tillera82950e2015-09-22 12:33:20 -07001256 gpr_mu_lock(&server->mu_global);
Craig Tiller4bf29282015-12-14 11:25:48 -08001257 grpc_cq_begin_op(cq, tag);
Craig Tillera82950e2015-09-22 12:33:20 -07001258 if (server->shutdown_published) {
Craig Tillerf51457b2016-05-03 17:06:32 -07001259 grpc_cq_end_op(&exec_ctx, cq, tag, GRPC_ERROR_NONE, done_published_shutdown,
1260 NULL, gpr_malloc(sizeof(grpc_cq_completion)));
Craig Tillera82950e2015-09-22 12:33:20 -07001261 gpr_mu_unlock(&server->mu_global);
1262 goto done;
1263 }
1264 server->shutdown_tags =
1265 gpr_realloc(server->shutdown_tags,
1266 sizeof(shutdown_tag) * (server->num_shutdown_tags + 1));
Craig Tillerbce999f2015-05-27 09:55:51 -07001267 sdt = &server->shutdown_tags[server->num_shutdown_tags++];
1268 sdt->tag = tag;
1269 sdt->cq = cq;
Craig Tillera82950e2015-09-22 12:33:20 -07001270 if (gpr_atm_acq_load(&server->shutdown_flag)) {
1271 gpr_mu_unlock(&server->mu_global);
1272 goto done;
1273 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001274
Craig Tillera82950e2015-09-22 12:33:20 -07001275 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
Craig Tillerab54f792015-07-08 08:34:20 -07001276
Craig Tillera82950e2015-09-22 12:33:20 -07001277 channel_broadcaster_init(server, &broadcaster);
nnoble0c475f02014-12-05 15:37:39 -08001278
Craig Tillerfc193e12015-09-24 15:29:03 -07001279 gpr_atm_rel_store(&server->shutdown_flag, 1);
1280
Craig Tillerbd217572015-02-11 18:10:56 -08001281 /* collect all unregistered then registered calls */
Craig Tillera82950e2015-09-22 12:33:20 -07001282 gpr_mu_lock(&server->mu_call);
Craig Tillercae4b1b2016-05-10 09:11:09 -07001283 kill_pending_work_locked(&exec_ctx, server,
1284 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001285 gpr_mu_unlock(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001286
Craig Tillera82950e2015-09-22 12:33:20 -07001287 maybe_finish_shutdown(&exec_ctx, server);
1288 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001289
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001290 /* Shutdown listeners */
Craig Tillera82950e2015-09-22 12:33:20 -07001291 for (l = server->listeners; l; l = l->next) {
Craig Tiller91031da2016-12-28 15:44:25 -08001292 grpc_closure_init(&l->destroy_done, listener_destroy_done, server,
1293 grpc_schedule_on_exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -07001294 l->destroy(&exec_ctx, server, l->arg, &l->destroy_done);
1295 }
Craig Tillerff3ae682015-06-29 17:44:04 -07001296
Mark D. Roth7f8db252016-06-24 08:24:15 -07001297 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, true /* send_goaway */,
Mark D. Roth89ebe552016-06-29 07:50:18 -07001298 GRPC_ERROR_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001299
Craig Tillerdfff1b82015-09-21 14:39:57 -07001300done:
Craig Tillera82950e2015-09-22 12:33:20 -07001301 grpc_exec_ctx_finish(&exec_ctx);
Craig Tilleraec96aa2015-04-07 14:32:15 -07001302}
1303
Craig Tillera82950e2015-09-22 12:33:20 -07001304void grpc_server_cancel_all_calls(grpc_server *server) {
Craig Tiller092d8d12015-07-04 22:35:00 -07001305 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001306 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerafa2d632015-05-26 16:39:13 -07001307
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001308 GRPC_API_TRACE("grpc_server_cancel_all_calls(server=%p)", 1, (server));
1309
Craig Tillera82950e2015-09-22 12:33:20 -07001310 gpr_mu_lock(&server->mu_global);
1311 channel_broadcaster_init(server, &broadcaster);
1312 gpr_mu_unlock(&server->mu_global);
Craig Tillerafa2d632015-05-26 16:39:13 -07001313
Mark D. Roth7f8db252016-06-24 08:24:15 -07001314 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, false /* send_goaway */,
Craig Tiller804ff712016-05-05 16:25:40 -07001315 GRPC_ERROR_CREATE("Cancelling all calls"));
Craig Tillera82950e2015-09-22 12:33:20 -07001316 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerafa2d632015-05-26 16:39:13 -07001317}
1318
Craig Tillera82950e2015-09-22 12:33:20 -07001319void grpc_server_destroy(grpc_server *server) {
Craig Tilleraec96aa2015-04-07 14:32:15 -07001320 listener *l;
Craig Tillerf5768a62015-09-22 10:54:34 -07001321 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller872af022015-04-24 15:57:52 -07001322
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001323 GRPC_API_TRACE("grpc_server_destroy(server=%p)", 1, (server));
1324
Craig Tillera82950e2015-09-22 12:33:20 -07001325 gpr_mu_lock(&server->mu_global);
1326 GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners);
1327 GPR_ASSERT(server->listeners_destroyed == num_listeners(server));
Craig Tilleraec96aa2015-04-07 14:32:15 -07001328
Craig Tillera82950e2015-09-22 12:33:20 -07001329 while (server->listeners) {
1330 l = server->listeners;
1331 server->listeners = l->next;
1332 gpr_free(l);
1333 }
Craig Tilleraec96aa2015-04-07 14:32:15 -07001334
Craig Tillera82950e2015-09-22 12:33:20 -07001335 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001336
Craig Tillera82950e2015-09-22 12:33:20 -07001337 server_unref(&exec_ctx, server);
1338 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001339}
1340
Craig Tillera82950e2015-09-22 12:33:20 -07001341void grpc_server_add_listener(
1342 grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1343 void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1344 grpc_pollset **pollsets, size_t pollset_count),
1345 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1346 grpc_closure *on_done)) {
1347 listener *l = gpr_malloc(sizeof(listener));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001348 l->arg = arg;
1349 l->start = start;
1350 l->destroy = destroy;
1351 l->next = server->listeners;
1352 server->listeners = l;
1353}
1354
Craig Tillera82950e2015-09-22 12:33:20 -07001355static grpc_call_error queue_call_request(grpc_exec_ctx *exec_ctx,
Craig Tiller9f9d4222016-05-16 17:02:14 -07001356 grpc_server *server, size_t cq_idx,
Craig Tillera82950e2015-09-22 12:33:20 -07001357 requested_call *rc) {
Yang Gaoeb8e7cd2015-02-11 11:43:40 -08001358 call_data *calld = NULL;
Craig Tillerb9d35962015-09-11 13:31:16 -07001359 request_matcher *rm = NULL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001360 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -07001361 if (gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tiller48abdde2016-05-24 06:55:28 -07001362 fail_call(exec_ctx, server, cq_idx, rc,
1363 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001364 return GRPC_CALL_OK;
1365 }
Craig Tillerb19dbea2016-07-12 15:33:11 -07001366 request_id = gpr_stack_lockfree_pop(server->request_freelist_per_cq[cq_idx]);
Craig Tillera82950e2015-09-22 12:33:20 -07001367 if (request_id == -1) {
1368 /* out of request ids: just fail this one */
Craig Tiller48abdde2016-05-24 06:55:28 -07001369 fail_call(exec_ctx, server, cq_idx, rc,
Craig Tillerbdfa39c2016-07-12 16:08:13 -07001370 grpc_error_set_int(GRPC_ERROR_CREATE("Out of request ids"),
1371 GRPC_ERROR_INT_LIMIT,
1372 server->max_requested_calls_per_cq));
Craig Tillera82950e2015-09-22 12:33:20 -07001373 return GRPC_CALL_OK;
1374 }
1375 switch (rc->type) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001376 case BATCH_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001377 rm = &server->unregistered_request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001378 break;
1379 case REGISTERED_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001380 rm = &rc->data.registered.registered_method->request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001381 break;
Craig Tillera82950e2015-09-22 12:33:20 -07001382 }
Craig Tillerb19dbea2016-07-12 15:33:11 -07001383 server->requested_calls_per_cq[cq_idx][request_id] = *rc;
Craig Tillera82950e2015-09-22 12:33:20 -07001384 gpr_free(rc);
Craig Tillerdb7c3562016-05-19 11:02:52 -07001385 if (gpr_stack_lockfree_push(rm->requests_per_cq[cq_idx], request_id)) {
Craig Tillera82950e2015-09-22 12:33:20 -07001386 /* this was the first queued request: we need to lock and start
1387 matching calls */
1388 gpr_mu_lock(&server->mu_call);
Craig Tillerb9d35962015-09-11 13:31:16 -07001389 while ((calld = rm->pending_head) != NULL) {
Craig Tillerdb7c3562016-05-19 11:02:52 -07001390 request_id = gpr_stack_lockfree_pop(rm->requests_per_cq[cq_idx]);
Craig Tillera82950e2015-09-22 12:33:20 -07001391 if (request_id == -1) break;
Craig Tillerb9d35962015-09-11 13:31:16 -07001392 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -07001393 gpr_mu_unlock(&server->mu_call);
1394 gpr_mu_lock(&calld->mu_state);
1395 if (calld->state == ZOMBIED) {
1396 gpr_mu_unlock(&calld->mu_state);
1397 grpc_closure_init(
1398 &calld->kill_zombie_closure, kill_zombie,
Craig Tiller91031da2016-12-28 15:44:25 -08001399 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0),
1400 grpc_schedule_on_exec_ctx);
1401 grpc_closure_sched(exec_ctx, &calld->kill_zombie_closure,
1402 GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -07001403 } else {
1404 GPR_ASSERT(calld->state == PENDING);
1405 calld->state = ACTIVATED;
1406 gpr_mu_unlock(&calld->mu_state);
Craig Tiller9f9d4222016-05-16 17:02:14 -07001407 publish_call(exec_ctx, server, calld, cq_idx,
Craig Tillerb19dbea2016-07-12 15:33:11 -07001408 &server->requested_calls_per_cq[cq_idx][request_id]);
Craig Tillera82950e2015-09-22 12:33:20 -07001409 }
1410 gpr_mu_lock(&server->mu_call);
Craig Tiller45724b32015-09-22 10:42:19 -07001411 }
Craig Tillera82950e2015-09-22 12:33:20 -07001412 gpr_mu_unlock(&server->mu_call);
1413 }
Craig Tiller6a006ce2015-07-13 16:25:40 -07001414 return GRPC_CALL_OK;
Craig Tillercce17ac2015-01-20 09:29:28 -08001415}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001416
Craig Tillera82950e2015-09-22 12:33:20 -07001417grpc_call_error grpc_server_request_call(
1418 grpc_server *server, grpc_call **call, grpc_call_details *details,
1419 grpc_metadata_array *initial_metadata,
1420 grpc_completion_queue *cq_bound_to_call,
1421 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001422 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001423 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001424 requested_call *rc = gpr_malloc(sizeof(*rc));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001425 GRPC_API_TRACE(
1426 "grpc_server_request_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001427 "server=%p, call=%p, details=%p, initial_metadata=%p, "
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001428 "cq_bound_to_call=%p, cq_for_notification=%p, tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001429 7, (server, call, details, initial_metadata, cq_bound_to_call,
1430 cq_for_notification, tag));
Craig Tiller9f9d4222016-05-16 17:02:14 -07001431 size_t cq_idx;
1432 for (cq_idx = 0; cq_idx < server->cq_count; cq_idx++) {
1433 if (server->cqs[cq_idx] == cq_for_notification) {
1434 break;
1435 }
1436 }
1437 if (cq_idx == server->cq_count) {
Craig Tillera82950e2015-09-22 12:33:20 -07001438 gpr_free(rc);
1439 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1440 goto done;
1441 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001442 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller9928d392015-08-18 09:40:24 -07001443 details->reserved = NULL;
Craig Tillerb19dbea2016-07-12 15:33:11 -07001444 rc->cq_idx = cq_idx;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001445 rc->type = BATCH_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001446 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001447 rc->tag = tag;
1448 rc->cq_bound_to_call = cq_bound_to_call;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001449 rc->call = call;
1450 rc->data.batch.details = details;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001451 rc->initial_metadata = initial_metadata;
Craig Tiller9f9d4222016-05-16 17:02:14 -07001452 error = queue_call_request(&exec_ctx, server, cq_idx, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001453done:
Craig Tillera82950e2015-09-22 12:33:20 -07001454 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001455 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001456}
1457
Craig Tillera82950e2015-09-22 12:33:20 -07001458grpc_call_error grpc_server_request_registered_call(
Craig Tillerb9d35962015-09-11 13:31:16 -07001459 grpc_server *server, void *rmp, grpc_call **call, gpr_timespec *deadline,
Craig Tillera82950e2015-09-22 12:33:20 -07001460 grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload,
1461 grpc_completion_queue *cq_bound_to_call,
1462 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001463 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001464 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001465 requested_call *rc = gpr_malloc(sizeof(*rc));
Craig Tillerb9d35962015-09-11 13:31:16 -07001466 registered_method *rm = rmp;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001467 GRPC_API_TRACE(
1468 "grpc_server_request_registered_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001469 "server=%p, rmp=%p, call=%p, deadline=%p, initial_metadata=%p, "
1470 "optional_payload=%p, cq_bound_to_call=%p, cq_for_notification=%p, "
1471 "tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001472 9, (server, rmp, call, deadline, initial_metadata, optional_payload,
1473 cq_bound_to_call, cq_for_notification, tag));
Craig Tiller9f9d4222016-05-16 17:02:14 -07001474
1475 size_t cq_idx;
1476 for (cq_idx = 0; cq_idx < server->cq_count; cq_idx++) {
1477 if (server->cqs[cq_idx] == cq_for_notification) {
1478 break;
1479 }
1480 }
1481 if (cq_idx == server->cq_count) {
Craig Tillera82950e2015-09-22 12:33:20 -07001482 gpr_free(rc);
1483 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1484 goto done;
1485 }
Craig Tiller06cb1a92016-04-04 08:10:47 -07001486 if ((optional_payload == NULL) !=
1487 (rm->payload_handling == GRPC_SRM_PAYLOAD_NONE)) {
1488 gpr_free(rc);
1489 error = GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH;
1490 goto done;
1491 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001492 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tillerb19dbea2016-07-12 15:33:11 -07001493 rc->cq_idx = cq_idx;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001494 rc->type = REGISTERED_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001495 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001496 rc->tag = tag;
1497 rc->cq_bound_to_call = cq_bound_to_call;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001498 rc->call = call;
Craig Tillerb9d35962015-09-11 13:31:16 -07001499 rc->data.registered.registered_method = rm;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001500 rc->data.registered.deadline = deadline;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001501 rc->initial_metadata = initial_metadata;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001502 rc->data.registered.optional_payload = optional_payload;
Craig Tiller9f9d4222016-05-16 17:02:14 -07001503 error = queue_call_request(&exec_ctx, server, cq_idx, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001504done:
Craig Tillera82950e2015-09-22 12:33:20 -07001505 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001506 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001507}
1508
Craig Tillera82950e2015-09-22 12:33:20 -07001509static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tiller48abdde2016-05-24 06:55:28 -07001510 size_t cq_idx, requested_call *rc, grpc_error *error) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -07001511 *rc->call = NULL;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001512 rc->initial_metadata->count = 0;
Craig Tillercae4b1b2016-05-10 09:11:09 -07001513 GPR_ASSERT(error != GRPC_ERROR_NONE);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001514
Craig Tillera82950e2015-09-22 12:33:20 -07001515 server_ref(server);
Craig Tiller48abdde2016-05-24 06:55:28 -07001516 grpc_cq_end_op(exec_ctx, server->cqs[cq_idx], rc->tag, error,
Craig Tillera82950e2015-09-22 12:33:20 -07001517 done_request_event, rc, &rc->completion);
Craig Tiller24be0f72015-02-10 14:04:22 -08001518}
1519
Craig Tillera82950e2015-09-22 12:33:20 -07001520const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001521 return server->channel_args;
Craig Tiller190d3602015-02-18 09:23:38 -08001522}
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001523
Craig Tillera82950e2015-09-22 12:33:20 -07001524int grpc_server_has_open_connections(grpc_server *server) {
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001525 int r;
Craig Tillera82950e2015-09-22 12:33:20 -07001526 gpr_mu_lock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001527 r = server->root_channel_data.next != &server->root_channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -07001528 gpr_mu_unlock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001529 return r;
1530}