blob: a482ba43d8a4e91007598633a79ceae49657e482 [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"
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 enum { BATCH_CALL, REGISTERED_CALL } requested_call_type;
Craig Tiller24be0f72015-02-10 14:04:22 -080073
Craig Tillera82950e2015-09-22 12:33:20 -070074typedef struct requested_call {
Craig Tiller24be0f72015-02-10 14:04:22 -080075 requested_call_type type;
Craig Tillerb19dbea2016-07-12 15:33:11 -070076 size_t cq_idx;
Craig Tiller24be0f72015-02-10 14:04:22 -080077 void *tag;
Craig Tiller6a006ce2015-07-13 16:25:40 -070078 grpc_server *server;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070079 grpc_completion_queue *cq_bound_to_call;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070080 grpc_call **call;
Craig Tiller97fc6a32015-07-08 15:31:35 -070081 grpc_cq_completion completion;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080082 grpc_metadata_array *initial_metadata;
Craig Tillera82950e2015-09-22 12:33:20 -070083 union {
84 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080085 grpc_call_details *details;
Craig Tiller24be0f72015-02-10 14:04:22 -080086 } batch;
Craig Tillera82950e2015-09-22 12:33:20 -070087 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080088 registered_method *registered_method;
89 gpr_timespec *deadline;
Craig Tiller24be0f72015-02-10 14:04:22 -080090 grpc_byte_buffer **optional_payload;
91 } registered;
92 } data;
93} requested_call;
94
Craig Tillera82950e2015-09-22 12:33:20 -070095typedef struct channel_registered_method {
Craig Tiller24be0f72015-02-10 14:04:22 -080096 registered_method *server_registered_method;
Craig Tillerb2906862016-03-10 06:50:07 -080097 uint32_t flags;
Craig Tiller24be0f72015-02-10 14:04:22 -080098 grpc_mdstr *method;
99 grpc_mdstr *host;
100} channel_registered_method;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101
Craig Tillera82950e2015-09-22 12:33:20 -0700102struct channel_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103 grpc_server *server;
Craig Tillere039f032015-06-25 12:54:23 -0700104 grpc_connectivity_state connectivity_state;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105 grpc_channel *channel;
Craig Tiller418a8212016-05-16 16:27:51 -0700106 size_t cq_idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800107 /* linked list of all channels on a server */
108 channel_data *next;
109 channel_data *prev;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800110 channel_registered_method *registered_methods;
Craig Tiller7536af02015-12-22 13:49:30 -0800111 uint32_t registered_method_slots;
112 uint32_t registered_method_max_probes;
Craig Tiller33825112015-09-18 07:44:19 -0700113 grpc_closure finish_destroy_channel_closure;
114 grpc_closure channel_connectivity_changed;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115};
116
Craig Tillera82950e2015-09-22 12:33:20 -0700117typedef struct shutdown_tag {
Craig Tillerbce999f2015-05-27 09:55:51 -0700118 void *tag;
119 grpc_completion_queue *cq;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700120 grpc_cq_completion completion;
Craig Tillerbce999f2015-05-27 09:55:51 -0700121} shutdown_tag;
122
Craig Tillera82950e2015-09-22 12:33:20 -0700123typedef enum {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800124 /* waiting for metadata */
125 NOT_STARTED,
126 /* inital metadata read, not flow controlled in yet */
127 PENDING,
128 /* flow controlled in, on completion queue */
129 ACTIVATED,
130 /* cancelled before being queued */
131 ZOMBIED
132} call_state;
133
Craig Tiller729b35a2015-07-13 12:36:47 -0700134typedef struct request_matcher request_matcher;
135
Craig Tillera82950e2015-09-22 12:33:20 -0700136struct call_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800137 grpc_call *call;
138
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700139 /** protects state */
140 gpr_mu mu_state;
141 /** the current state of a call - see call_state */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142 call_state state;
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700143
Craig Tillercce17ac2015-01-20 09:29:28 -0800144 grpc_mdstr *path;
145 grpc_mdstr *host;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700146 gpr_timespec deadline;
Craig Tillercce17ac2015-01-20 09:29:28 -0800147
Craig Tiller20bc56d2015-02-12 09:02:56 -0800148 grpc_completion_queue *cq_new;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800149
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800150 grpc_metadata_batch *recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800151 bool recv_idempotent_request;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800152 grpc_metadata_array initial_metadata;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700153
Craig Tiller88512692016-04-04 09:32:52 -0700154 request_matcher *request_matcher;
155 grpc_byte_buffer *payload;
156
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800157 grpc_closure got_initial_metadata;
158 grpc_closure server_on_recv_initial_metadata;
Craig Tiller33825112015-09-18 07:44:19 -0700159 grpc_closure kill_zombie_closure;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800160 grpc_closure *on_done_recv_initial_metadata;
David Garcia Quintas284488b2015-05-28 16:27:39 -0700161
Craig Tiller88512692016-04-04 09:32:52 -0700162 grpc_closure publish;
163
Craig Tiller729b35a2015-07-13 12:36:47 -0700164 call_data *pending_next;
165};
166
Craig Tillera82950e2015-09-22 12:33:20 -0700167struct request_matcher {
Craig Tiller88512692016-04-04 09:32:52 -0700168 grpc_server *server;
Craig Tiller729b35a2015-07-13 12:36:47 -0700169 call_data *pending_head;
170 call_data *pending_tail;
Craig Tillerdb7c3562016-05-19 11:02:52 -0700171 gpr_stack_lockfree **requests_per_cq;
Craig Tiller729b35a2015-07-13 12:36:47 -0700172};
173
Craig Tillera82950e2015-09-22 12:33:20 -0700174struct registered_method {
Craig Tiller729b35a2015-07-13 12:36:47 -0700175 char *method;
176 char *host;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700177 grpc_server_register_method_payload_handling payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -0800178 uint32_t flags;
Craig Tillerfa96d862016-05-21 12:39:56 -0700179 /* one request matcher per method */
Craig Tiller729b35a2015-07-13 12:36:47 -0700180 request_matcher request_matcher;
181 registered_method *next;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800182};
183
Craig Tillera82950e2015-09-22 12:33:20 -0700184typedef struct {
Craig Tillerff3ae682015-06-29 17:44:04 -0700185 grpc_channel **channels;
186 size_t num_channels;
187} channel_broadcaster;
188
Craig Tillera82950e2015-09-22 12:33:20 -0700189struct grpc_server {
Craig Tiller729b35a2015-07-13 12:36:47 -0700190 grpc_channel_args *channel_args;
191
192 grpc_completion_queue **cqs;
193 grpc_pollset **pollsets;
194 size_t cq_count;
Craig Tiller88ef00e2016-05-17 09:31:49 -0700195 bool started;
Craig Tiller729b35a2015-07-13 12:36:47 -0700196
197 /* The two following mutexes control access to server-state
198 mu_global controls access to non-call-related state (e.g., channel state)
199 mu_call controls access to call-related state (e.g., the call lists)
200
201 If they are ever required to be nested, you must lock mu_global
202 before mu_call. This is currently used in shutdown processing
203 (grpc_server_shutdown_and_notify and maybe_finish_shutdown) */
Craig Tillera82950e2015-09-22 12:33:20 -0700204 gpr_mu mu_global; /* mutex for server and channel state */
205 gpr_mu mu_call; /* mutex for call-specific state */
Craig Tiller729b35a2015-07-13 12:36:47 -0700206
207 registered_method *registered_methods;
Craig Tillerfa96d862016-05-21 12:39:56 -0700208 /** one request matcher for unregistered methods */
Craig Tiller729b35a2015-07-13 12:36:47 -0700209 request_matcher unregistered_request_matcher;
Craig Tiller0a06cd72016-07-14 13:21:24 -0700210 /** free list of available requested_calls_per_cq indices */
Craig Tillerb19dbea2016-07-12 15:33:11 -0700211 gpr_stack_lockfree **request_freelist_per_cq;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700212 /** requested call backing data */
Craig Tillerb19dbea2016-07-12 15:33:11 -0700213 requested_call **requested_calls_per_cq;
214 int max_requested_calls_per_cq;
Craig Tiller729b35a2015-07-13 12:36:47 -0700215
Craig Tiller6a006ce2015-07-13 16:25:40 -0700216 gpr_atm shutdown_flag;
Craig Tiller7536af02015-12-22 13:49:30 -0800217 uint8_t shutdown_published;
Craig Tiller729b35a2015-07-13 12:36:47 -0700218 size_t num_shutdown_tags;
219 shutdown_tag *shutdown_tags;
220
221 channel_data root_channel_data;
222
223 listener *listeners;
224 int listeners_destroyed;
225 gpr_refcount internal_refcount;
226
227 /** when did we print the last shutdown progress message */
228 gpr_timespec last_shutdown_message_time;
229};
230
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800231#define SERVER_FROM_CALL_ELEM(elem) \
232 (((channel_data *)(elem)->channel_data)->server)
233
Craig Tillerf51457b2016-05-03 17:06:32 -0700234static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *calld,
235 grpc_error *error);
Craig Tillera82950e2015-09-22 12:33:20 -0700236static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tiller48abdde2016-05-24 06:55:28 -0700237 size_t cq_idx, requested_call *rc, grpc_error *error);
Vijay Pai8931cdd2015-06-17 12:42:17 -0700238/* Before calling maybe_finish_shutdown, we must hold mu_global and not
239 hold mu_call */
Craig Tillera82950e2015-09-22 12:33:20 -0700240static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_server *server);
Craig Tiller24be0f72015-02-10 14:04:22 -0800241
Craig Tiller729b35a2015-07-13 12:36:47 -0700242/*
243 * channel broadcaster
244 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700245
246/* assumes server locked */
Craig Tillera82950e2015-09-22 12:33:20 -0700247static void channel_broadcaster_init(grpc_server *s, channel_broadcaster *cb) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700248 channel_data *c;
249 size_t count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700250 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
251 count++;
252 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700253 cb->num_channels = count;
Craig Tillera82950e2015-09-22 12:33:20 -0700254 cb->channels = gpr_malloc(sizeof(*cb->channels) * cb->num_channels);
Craig Tillerff3ae682015-06-29 17:44:04 -0700255 count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700256 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
257 cb->channels[count++] = c->channel;
258 GRPC_CHANNEL_INTERNAL_REF(c->channel, "broadcast");
259 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700260}
261
Craig Tillera82950e2015-09-22 12:33:20 -0700262struct shutdown_cleanup_args {
Craig Tiller33825112015-09-18 07:44:19 -0700263 grpc_closure closure;
Craig Tillerff3ae682015-06-29 17:44:04 -0700264 gpr_slice slice;
265};
266
Craig Tillera82950e2015-09-22 12:33:20 -0700267static void shutdown_cleanup(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tillerf51457b2016-05-03 17:06:32 -0700268 grpc_error *error) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700269 struct shutdown_cleanup_args *a = arg;
Craig Tillera82950e2015-09-22 12:33:20 -0700270 gpr_slice_unref(a->slice);
271 gpr_free(a);
Craig Tillerff3ae682015-06-29 17:44:04 -0700272}
273
Craig Tillera82950e2015-09-22 12:33:20 -0700274static void send_shutdown(grpc_exec_ctx *exec_ctx, grpc_channel *channel,
Craig Tiller804ff712016-05-05 16:25:40 -0700275 int send_goaway, grpc_error *send_disconnect) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700276 grpc_transport_op op;
277 struct shutdown_cleanup_args *sc;
278 grpc_channel_element *elem;
279
Craig Tillera82950e2015-09-22 12:33:20 -0700280 memset(&op, 0, sizeof(op));
Craig Tillerff3ae682015-06-29 17:44:04 -0700281 op.send_goaway = send_goaway;
Craig Tillera82950e2015-09-22 12:33:20 -0700282 sc = gpr_malloc(sizeof(*sc));
283 sc->slice = gpr_slice_from_copied_string("Server shutdown");
Craig Tillerff3ae682015-06-29 17:44:04 -0700284 op.goaway_message = &sc->slice;
285 op.goaway_status = GRPC_STATUS_OK;
Craig Tiller804ff712016-05-05 16:25:40 -0700286 op.disconnect_with_error = send_disconnect;
Craig Tillera82950e2015-09-22 12:33:20 -0700287 grpc_closure_init(&sc->closure, shutdown_cleanup, sc);
Craig Tillerff3ae682015-06-29 17:44:04 -0700288 op.on_consumed = &sc->closure;
289
Craig Tillera82950e2015-09-22 12:33:20 -0700290 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
291 elem->filter->start_transport_op(exec_ctx, elem, &op);
Craig Tillerff3ae682015-06-29 17:44:04 -0700292}
293
Craig Tillera82950e2015-09-22 12:33:20 -0700294static void channel_broadcaster_shutdown(grpc_exec_ctx *exec_ctx,
295 channel_broadcaster *cb,
296 int send_goaway,
Craig Tiller804ff712016-05-05 16:25:40 -0700297 grpc_error *force_disconnect) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700298 size_t i;
299
Craig Tillera82950e2015-09-22 12:33:20 -0700300 for (i = 0; i < cb->num_channels; i++) {
Craig Tiller71f96652016-05-11 23:17:45 -0700301 send_shutdown(exec_ctx, cb->channels[i], send_goaway,
302 GRPC_ERROR_REF(force_disconnect));
Craig Tillera82950e2015-09-22 12:33:20 -0700303 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, cb->channels[i], "broadcast");
304 }
305 gpr_free(cb->channels);
Craig Tiller71f96652016-05-11 23:17:45 -0700306 GRPC_ERROR_UNREF(force_disconnect);
Craig Tillerff3ae682015-06-29 17:44:04 -0700307}
308
Craig Tiller729b35a2015-07-13 12:36:47 -0700309/*
310 * request_matcher
311 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700312
Craig Tiller88512692016-04-04 09:32:52 -0700313static void request_matcher_init(request_matcher *rm, size_t entries,
314 grpc_server *server) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700315 memset(rm, 0, sizeof(*rm));
Craig Tiller88512692016-04-04 09:32:52 -0700316 rm->server = server;
Craig Tillerdb7c3562016-05-19 11:02:52 -0700317 rm->requests_per_cq =
318 gpr_malloc(sizeof(*rm->requests_per_cq) * server->cq_count);
319 for (size_t i = 0; i < server->cq_count; i++) {
320 rm->requests_per_cq[i] = gpr_stack_lockfree_create(entries);
321 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800322}
323
Craig Tillerb9d35962015-09-11 13:31:16 -0700324static void request_matcher_destroy(request_matcher *rm) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700325 for (size_t i = 0; i < rm->server->cq_count; i++) {
326 GPR_ASSERT(gpr_stack_lockfree_pop(rm->requests_per_cq[i]) == -1);
327 gpr_stack_lockfree_destroy(rm->requests_per_cq[i]);
328 }
329 gpr_free(rm->requests_per_cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800330}
331
Craig Tillerf51457b2016-05-03 17:06:32 -0700332static void kill_zombie(grpc_exec_ctx *exec_ctx, void *elem,
333 grpc_error *error) {
Craig Tillera82950e2015-09-22 12:33:20 -0700334 grpc_call_destroy(grpc_call_from_top_element(elem));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800335}
336
Craig Tiller8dc09712015-09-24 13:58:16 -0700337static void request_matcher_zombify_all_pending_calls(grpc_exec_ctx *exec_ctx,
338 request_matcher *rm) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700339 while (rm->pending_head) {
340 call_data *calld = rm->pending_head;
341 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -0700342 gpr_mu_lock(&calld->mu_state);
343 calld->state = ZOMBIED;
344 gpr_mu_unlock(&calld->mu_state);
345 grpc_closure_init(
346 &calld->kill_zombie_closure, kill_zombie,
347 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tiller332f1b32016-05-24 13:21:21 -0700348 grpc_exec_ctx_sched(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
349 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700350 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800351}
352
Craig Tillera82950e2015-09-22 12:33:20 -0700353static void request_matcher_kill_requests(grpc_exec_ctx *exec_ctx,
354 grpc_server *server,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700355 request_matcher *rm,
356 grpc_error *error) {
Craig Tiller1191e212015-07-30 14:49:02 -0700357 int request_id;
Craig Tillerdb7c3562016-05-19 11:02:52 -0700358 for (size_t i = 0; i < server->cq_count; i++) {
359 while ((request_id = gpr_stack_lockfree_pop(rm->requests_per_cq[i])) !=
360 -1) {
Craig Tillerb19dbea2016-07-12 15:33:11 -0700361 fail_call(exec_ctx, server, i,
362 &server->requested_calls_per_cq[i][request_id],
Craig Tiller48abdde2016-05-24 06:55:28 -0700363 GRPC_ERROR_REF(error));
Craig Tillerdb7c3562016-05-19 11:02:52 -0700364 }
Craig Tillera82950e2015-09-22 12:33:20 -0700365 }
Craig Tillercae4b1b2016-05-10 09:11:09 -0700366 GRPC_ERROR_UNREF(error);
Craig Tiller1191e212015-07-30 14:49:02 -0700367}
368
Craig Tiller729b35a2015-07-13 12:36:47 -0700369/*
370 * server proper
371 */
372
Craig Tillera82950e2015-09-22 12:33:20 -0700373static void server_ref(grpc_server *server) {
374 gpr_ref(&server->internal_refcount);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800375}
376
Craig Tillera82950e2015-09-22 12:33:20 -0700377static void server_delete(grpc_exec_ctx *exec_ctx, grpc_server *server) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800378 registered_method *rm;
Craig Tiller89504612015-04-27 11:48:46 -0700379 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700380 grpc_channel_args_destroy(server->channel_args);
381 gpr_mu_destroy(&server->mu_global);
382 gpr_mu_destroy(&server->mu_call);
Craig Tillera82950e2015-09-22 12:33:20 -0700383 while ((rm = server->registered_methods) != NULL) {
384 server->registered_methods = rm->next;
Craig Tiller88ef00e2016-05-17 09:31:49 -0700385 if (server->started) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700386 request_matcher_destroy(&rm->request_matcher);
Craig Tiller418a8212016-05-16 16:27:51 -0700387 }
Craig Tillera82950e2015-09-22 12:33:20 -0700388 gpr_free(rm->method);
389 gpr_free(rm->host);
390 gpr_free(rm);
391 }
Craig Tillerdb7c3562016-05-19 11:02:52 -0700392 if (server->started) {
393 request_matcher_destroy(&server->unregistered_request_matcher);
394 }
Craig Tillera82950e2015-09-22 12:33:20 -0700395 for (i = 0; i < server->cq_count; i++) {
396 GRPC_CQ_INTERNAL_UNREF(server->cqs[i], "server");
Craig Tillerb19dbea2016-07-12 15:33:11 -0700397 if (server->started) {
398 gpr_stack_lockfree_destroy(server->request_freelist_per_cq[i]);
399 gpr_free(server->requested_calls_per_cq[i]);
400 }
Craig Tillera82950e2015-09-22 12:33:20 -0700401 }
Craig Tillerb19dbea2016-07-12 15:33:11 -0700402 gpr_free(server->request_freelist_per_cq);
403 gpr_free(server->requested_calls_per_cq);
Craig Tillera82950e2015-09-22 12:33:20 -0700404 gpr_free(server->cqs);
405 gpr_free(server->pollsets);
406 gpr_free(server->shutdown_tags);
Craig Tillera82950e2015-09-22 12:33:20 -0700407 gpr_free(server);
Craig Tilleree945e82015-05-26 16:15:34 -0700408}
409
Craig Tillera82950e2015-09-22 12:33:20 -0700410static void server_unref(grpc_exec_ctx *exec_ctx, grpc_server *server) {
411 if (gpr_unref(&server->internal_refcount)) {
412 server_delete(exec_ctx, server);
413 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800414}
415
Craig Tillera82950e2015-09-22 12:33:20 -0700416static int is_channel_orphaned(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800417 return chand->next == chand;
418}
419
Craig Tillera82950e2015-09-22 12:33:20 -0700420static void orphan_channel(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800421 chand->next->prev = chand->prev;
422 chand->prev->next = chand->next;
423 chand->next = chand->prev = chand;
424}
425
Craig Tillera82950e2015-09-22 12:33:20 -0700426static void finish_destroy_channel(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700427 grpc_error *error) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800428 channel_data *chand = cd;
429 grpc_server *server = chand->server;
Craig Tillera82950e2015-09-22 12:33:20 -0700430 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "server");
431 server_unref(exec_ctx, server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800432}
433
Craig Tillera82950e2015-09-22 12:33:20 -0700434static void destroy_channel(grpc_exec_ctx *exec_ctx, channel_data *chand) {
435 if (is_channel_orphaned(chand)) return;
436 GPR_ASSERT(chand->server != NULL);
437 orphan_channel(chand);
438 server_ref(chand->server);
439 maybe_finish_shutdown(exec_ctx, chand->server);
David Garcia Quintas284488b2015-05-28 16:27:39 -0700440 chand->finish_destroy_channel_closure.cb = finish_destroy_channel;
441 chand->finish_destroy_channel_closure.cb_arg = chand;
Craig Tillerd7f12e32016-03-03 10:08:31 -0800442
443 grpc_transport_op op;
444 memset(&op, 0, sizeof(op));
445 op.set_accept_stream = true;
446 op.on_consumed = &chand->finish_destroy_channel_closure;
447 grpc_channel_next_op(exec_ctx,
448 grpc_channel_stack_element(
449 grpc_channel_get_channel_stack(chand->channel), 0),
450 &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800451}
452
Craig Tiller06cb1a92016-04-04 08:10:47 -0700453static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) {
454 gpr_slice slice = value->slice;
455 size_t len = GPR_SLICE_LENGTH(slice);
Craig Tiller6a006ce2015-07-13 16:25:40 -0700456
Craig Tiller06cb1a92016-04-04 08:10:47 -0700457 if (len + 1 > *capacity) {
458 *capacity = GPR_MAX(len + 1, *capacity * 2);
459 *dest = gpr_realloc(*dest, *capacity);
460 }
461 memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1);
462}
463
Craig Tiller88512692016-04-04 09:32:52 -0700464static void done_request_event(grpc_exec_ctx *exec_ctx, void *req,
465 grpc_cq_completion *c) {
466 requested_call *rc = req;
467 grpc_server *server = rc->server;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700468
Craig Tillerb19dbea2016-07-12 15:33:11 -0700469 if (rc >= server->requested_calls_per_cq[rc->cq_idx] &&
470 rc < server->requested_calls_per_cq[rc->cq_idx] +
471 server->max_requested_calls_per_cq) {
472 GPR_ASSERT(rc - server->requested_calls_per_cq[rc->cq_idx] <= INT_MAX);
473 gpr_stack_lockfree_push(
474 server->request_freelist_per_cq[rc->cq_idx],
475 (int)(rc - server->requested_calls_per_cq[rc->cq_idx]));
Craig Tiller88512692016-04-04 09:32:52 -0700476 } else {
477 gpr_free(req);
478 }
Craig Tiller06cb1a92016-04-04 08:10:47 -0700479
Craig Tiller88512692016-04-04 09:32:52 -0700480 server_unref(exec_ctx, server);
481}
Craig Tiller06cb1a92016-04-04 08:10:47 -0700482
Craig Tiller88512692016-04-04 09:32:52 -0700483static void publish_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tiller9f9d4222016-05-16 17:02:14 -0700484 call_data *calld, size_t cq_idx, requested_call *rc) {
Craig Tiller06cb1a92016-04-04 08:10:47 -0700485 grpc_call_set_completion_queue(exec_ctx, calld->call, rc->cq_bound_to_call);
Craig Tiller88512692016-04-04 09:32:52 -0700486 grpc_call *call = calld->call;
487 *rc->call = call;
Craig Tiller9f9d4222016-05-16 17:02:14 -0700488 calld->cq_new = server->cqs[cq_idx];
Craig Tiller06cb1a92016-04-04 08:10:47 -0700489 GPR_SWAP(grpc_metadata_array, *rc->initial_metadata, calld->initial_metadata);
490 switch (rc->type) {
491 case BATCH_CALL:
492 GPR_ASSERT(calld->host != NULL);
493 GPR_ASSERT(calld->path != NULL);
494 cpstr(&rc->data.batch.details->host,
495 &rc->data.batch.details->host_capacity, calld->host);
496 cpstr(&rc->data.batch.details->method,
497 &rc->data.batch.details->method_capacity, calld->path);
498 rc->data.batch.details->deadline = calld->deadline;
499 rc->data.batch.details->flags =
500 0 | (calld->recv_idempotent_request
501 ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
502 : 0);
503 break;
504 case REGISTERED_CALL:
505 *rc->data.registered.deadline = calld->deadline;
506 if (rc->data.registered.optional_payload) {
Craig Tiller88512692016-04-04 09:32:52 -0700507 *rc->data.registered.optional_payload = calld->payload;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700508 }
509 break;
510 default:
511 GPR_UNREACHABLE_CODE(return );
512 }
513
Craig Tiller88512692016-04-04 09:32:52 -0700514 grpc_call_element *elem =
515 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
516 channel_data *chand = elem->channel_data;
517 server_ref(chand->server);
Craig Tillerf51457b2016-05-03 17:06:32 -0700518 grpc_cq_end_op(exec_ctx, calld->cq_new, rc->tag, GRPC_ERROR_NONE,
519 done_request_event, rc, &rc->completion);
Craig Tiller06cb1a92016-04-04 08:10:47 -0700520}
521
Craig Tillerf51457b2016-05-03 17:06:32 -0700522static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *arg,
523 grpc_error *error) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700524 grpc_call_element *call_elem = arg;
525 call_data *calld = call_elem->call_data;
526 channel_data *chand = call_elem->channel_data;
Craig Tiller88512692016-04-04 09:32:52 -0700527 request_matcher *rm = calld->request_matcher;
528 grpc_server *server = rm->server;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700529
Craig Tillerf51457b2016-05-03 17:06:32 -0700530 if (error != GRPC_ERROR_NONE || gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tillera82950e2015-09-22 12:33:20 -0700531 gpr_mu_lock(&calld->mu_state);
532 calld->state = ZOMBIED;
533 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -0700534 grpc_closure_init(
535 &calld->kill_zombie_closure, kill_zombie,
536 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tiller332f1b32016-05-24 13:21:21 -0700537 grpc_exec_ctx_sched(exec_ctx, &calld->kill_zombie_closure, error, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700538 return;
539 }
Craig Tiller45724b32015-09-22 10:42:19 -0700540
Craig Tillerdb7c3562016-05-19 11:02:52 -0700541 for (size_t i = 0; i < server->cq_count; i++) {
542 size_t cq_idx = (chand->cq_idx + i) % server->cq_count;
543 int request_id = gpr_stack_lockfree_pop(rm->requests_per_cq[cq_idx]);
544 if (request_id == -1) {
545 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700546 } else {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700547 gpr_mu_lock(&calld->mu_state);
548 calld->state = ACTIVATED;
549 gpr_mu_unlock(&calld->mu_state);
550 publish_call(exec_ctx, server, calld, cq_idx,
Craig Tillerb19dbea2016-07-12 15:33:11 -0700551 &server->requested_calls_per_cq[cq_idx][request_id]);
Craig Tillerdb7c3562016-05-19 11:02:52 -0700552 return; /* early out */
Craig Tiller45724b32015-09-22 10:42:19 -0700553 }
Craig Tiller88512692016-04-04 09:32:52 -0700554 }
Craig Tillerdb7c3562016-05-19 11:02:52 -0700555
556 /* no cq to take the request found: queue it on the slow list */
557 gpr_mu_lock(&server->mu_call);
558 gpr_mu_lock(&calld->mu_state);
559 calld->state = PENDING;
560 gpr_mu_unlock(&calld->mu_state);
561 if (rm->pending_head == NULL) {
562 rm->pending_tail = rm->pending_head = calld;
563 } else {
564 rm->pending_tail->pending_next = calld;
565 rm->pending_tail = calld;
566 }
567 calld->pending_next = NULL;
568 gpr_mu_unlock(&server->mu_call);
Craig Tiller88512692016-04-04 09:32:52 -0700569}
570
571static void finish_start_new_rpc(
572 grpc_exec_ctx *exec_ctx, grpc_server *server, grpc_call_element *elem,
573 request_matcher *rm,
574 grpc_server_register_method_payload_handling payload_handling) {
575 call_data *calld = elem->call_data;
576
577 if (gpr_atm_acq_load(&server->shutdown_flag)) {
578 gpr_mu_lock(&calld->mu_state);
579 calld->state = ZOMBIED;
580 gpr_mu_unlock(&calld->mu_state);
581 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
Craig Tiller332f1b32016-05-24 13:21:21 -0700582 grpc_exec_ctx_sched(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
583 NULL);
Craig Tiller88512692016-04-04 09:32:52 -0700584 return;
585 }
586
587 calld->request_matcher = rm;
588
589 switch (payload_handling) {
590 case GRPC_SRM_PAYLOAD_NONE:
Craig Tiller48abdde2016-05-24 06:55:28 -0700591 publish_new_rpc(exec_ctx, elem, GRPC_ERROR_NONE);
Craig Tiller88512692016-04-04 09:32:52 -0700592 break;
593 case GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER: {
594 grpc_op op;
595 memset(&op, 0, sizeof(op));
596 op.op = GRPC_OP_RECV_MESSAGE;
597 op.data.recv_message = &calld->payload;
Craig Tillerdb7c3562016-05-19 11:02:52 -0700598 grpc_closure_init(&calld->publish, publish_new_rpc, elem);
Craig Tiller88512692016-04-04 09:32:52 -0700599 grpc_call_start_batch_and_execute(exec_ctx, calld->call, &op, 1,
600 &calld->publish);
601 break;
602 }
Craig Tillera82950e2015-09-22 12:33:20 -0700603 }
Craig Tiller04cc8be2015-02-10 16:11:22 -0800604}
605
Craig Tillera82950e2015-09-22 12:33:20 -0700606static void start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800607 channel_data *chand = elem->channel_data;
608 call_data *calld = elem->call_data;
609 grpc_server *server = chand->server;
Craig Tiller7536af02015-12-22 13:49:30 -0800610 uint32_t i;
611 uint32_t hash;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800612 channel_registered_method *rm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800613
Craig Tillera82950e2015-09-22 12:33:20 -0700614 if (chand->registered_methods && calld->path && calld->host) {
615 /* TODO(ctiller): unify these two searches */
616 /* check for an exact match with host */
617 hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash);
618 for (i = 0; i <= chand->registered_method_max_probes; i++) {
619 rm = &chand->registered_methods[(hash + i) %
620 chand->registered_method_slots];
621 if (!rm) break;
622 if (rm->host != calld->host) continue;
623 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800624 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
625 !calld->recv_idempotent_request)
626 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700627 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700628 &rm->server_registered_method->request_matcher,
629 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700630 return;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800631 }
Craig Tillera82950e2015-09-22 12:33:20 -0700632 /* check for a wildcard method definition (no host set) */
633 hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash);
634 for (i = 0; i <= chand->registered_method_max_probes; i++) {
635 rm = &chand->registered_methods[(hash + i) %
636 chand->registered_method_slots];
637 if (!rm) break;
638 if (rm->host != NULL) continue;
639 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800640 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
641 !calld->recv_idempotent_request)
642 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700643 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700644 &rm->server_registered_method->request_matcher,
645 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700646 return;
647 }
648 }
649 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700650 &server->unregistered_request_matcher,
651 GRPC_SRM_PAYLOAD_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800652}
653
Craig Tillera82950e2015-09-22 12:33:20 -0700654static int num_listeners(grpc_server *server) {
Craig Tilleree945e82015-05-26 16:15:34 -0700655 listener *l;
656 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700657 for (l = server->listeners; l; l = l->next) {
658 n++;
659 }
Craig Tilleree945e82015-05-26 16:15:34 -0700660 return n;
661}
662
Craig Tillera82950e2015-09-22 12:33:20 -0700663static void done_shutdown_event(grpc_exec_ctx *exec_ctx, void *server,
664 grpc_cq_completion *completion) {
665 server_unref(exec_ctx, server);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700666}
667
Craig Tillera82950e2015-09-22 12:33:20 -0700668static int num_channels(grpc_server *server) {
Craig Tillerab54f792015-07-08 08:34:20 -0700669 channel_data *chand;
670 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700671 for (chand = server->root_channel_data.next;
672 chand != &server->root_channel_data; chand = chand->next) {
673 n++;
674 }
Craig Tillerab54f792015-07-08 08:34:20 -0700675 return n;
676}
677
Craig Tillera82950e2015-09-22 12:33:20 -0700678static void kill_pending_work_locked(grpc_exec_ctx *exec_ctx,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700679 grpc_server *server, grpc_error *error) {
Craig Tiller88ef00e2016-05-17 09:31:49 -0700680 if (server->started) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700681 request_matcher_kill_requests(exec_ctx, server,
Craig Tiller48abdde2016-05-24 06:55:28 -0700682 &server->unregistered_request_matcher,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700683 GRPC_ERROR_REF(error));
Craig Tillerdb7c3562016-05-19 11:02:52 -0700684 request_matcher_zombify_all_pending_calls(
685 exec_ctx, &server->unregistered_request_matcher);
686 for (registered_method *rm = server->registered_methods; rm;
687 rm = rm->next) {
Craig Tiller48abdde2016-05-24 06:55:28 -0700688 request_matcher_kill_requests(exec_ctx, server, &rm->request_matcher,
689 GRPC_ERROR_REF(error));
Craig Tillerdb7c3562016-05-19 11:02:52 -0700690 request_matcher_zombify_all_pending_calls(exec_ctx, &rm->request_matcher);
Craig Tiller418a8212016-05-16 16:27:51 -0700691 }
Craig Tillera82950e2015-09-22 12:33:20 -0700692 }
Craig Tillercae4b1b2016-05-10 09:11:09 -0700693 GRPC_ERROR_UNREF(error);
Craig Tillerdc627722015-05-26 15:27:02 -0700694}
695
Craig Tillera82950e2015-09-22 12:33:20 -0700696static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx,
697 grpc_server *server) {
Craig Tiller45724b32015-09-22 10:42:19 -0700698 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700699 if (!gpr_atm_acq_load(&server->shutdown_flag) || server->shutdown_published) {
700 return;
701 }
Craig Tiller45724b32015-09-22 10:42:19 -0700702
Craig Tillercae4b1b2016-05-10 09:11:09 -0700703 kill_pending_work_locked(exec_ctx, server,
704 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tiller45724b32015-09-22 10:42:19 -0700705
Craig Tillera82950e2015-09-22 12:33:20 -0700706 if (server->root_channel_data.next != &server->root_channel_data ||
707 server->listeners_destroyed < num_listeners(server)) {
708 if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME),
709 server->last_shutdown_message_time),
710 gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
711 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
712 gpr_log(GPR_DEBUG,
713 "Waiting for %d channels and %d/%d listeners to be destroyed"
714 " before shutting down server",
715 num_channels(server),
716 num_listeners(server) - server->listeners_destroyed,
717 num_listeners(server));
Craig Tiller45724b32015-09-22 10:42:19 -0700718 }
Craig Tillera82950e2015-09-22 12:33:20 -0700719 return;
720 }
Craig Tiller45724b32015-09-22 10:42:19 -0700721 server->shutdown_published = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700722 for (i = 0; i < server->num_shutdown_tags; i++) {
723 server_ref(server);
724 grpc_cq_end_op(exec_ctx, server->shutdown_tags[i].cq,
Craig Tillerf51457b2016-05-03 17:06:32 -0700725 server->shutdown_tags[i].tag, GRPC_ERROR_NONE,
726 done_shutdown_event, server,
Craig Tillera82950e2015-09-22 12:33:20 -0700727 &server->shutdown_tags[i].completion);
728 }
Craig Tiller45724b32015-09-22 10:42:19 -0700729}
730
Craig Tillera82950e2015-09-22 12:33:20 -0700731static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700732 grpc_call_element *elem = user_data;
Craig Tillercce17ac2015-01-20 09:29:28 -0800733 call_data *calld = elem->call_data;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800734 if (md->key == GRPC_MDSTR_PATH) {
Craig Tiller9cac2a12016-04-04 20:56:56 -0700735 if (calld->path == NULL) {
736 calld->path = GRPC_MDSTR_REF(md->value);
737 }
Craig Tillera82950e2015-09-22 12:33:20 -0700738 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800739 } else if (md->key == GRPC_MDSTR_AUTHORITY) {
Craig Tiller9cac2a12016-04-04 20:56:56 -0700740 if (calld->host == NULL) {
741 calld->host = GRPC_MDSTR_REF(md->value);
742 }
Craig Tillera82950e2015-09-22 12:33:20 -0700743 return NULL;
744 }
Craig Tiller6902ad22015-04-16 08:01:49 -0700745 return md;
746}
747
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800748static void server_on_recv_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700749 grpc_error *error) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700750 grpc_call_element *elem = ptr;
Craig Tiller6902ad22015-04-16 08:01:49 -0700751 call_data *calld = elem->call_data;
Craig Tiller94329d02015-07-23 09:52:11 -0700752 gpr_timespec op_deadline;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700753
Craig Tiller8e5c9342016-05-12 15:34:59 -0700754 GRPC_ERROR_REF(error);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800755 grpc_metadata_batch_filter(calld->recv_initial_metadata, server_filter, elem);
756 op_deadline = calld->recv_initial_metadata->deadline;
757 if (0 != gpr_time_cmp(op_deadline, gpr_inf_future(op_deadline.clock_type))) {
758 calld->deadline = op_deadline;
759 }
760 if (calld->host && calld->path) {
761 /* do nothing */
762 } else {
Craig Tiller8e5c9342016-05-12 15:34:59 -0700763 GRPC_ERROR_UNREF(error);
Craig Tillerf51457b2016-05-03 17:06:32 -0700764 error =
765 GRPC_ERROR_CREATE_REFERENCING("Missing :authority or :path", &error, 1);
Craig Tillera82950e2015-09-22 12:33:20 -0700766 }
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700767
Craig Tiller332f1b32016-05-24 13:21:21 -0700768 grpc_exec_ctx_sched(exec_ctx, calld->on_done_recv_initial_metadata, error,
769 NULL);
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700770}
771
Craig Tillera82950e2015-09-22 12:33:20 -0700772static void server_mutate_op(grpc_call_element *elem,
773 grpc_transport_stream_op *op) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700774 call_data *calld = elem->call_data;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700775
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800776 if (op->recv_initial_metadata != NULL) {
Craig Tiller4d40ba32016-03-09 17:48:40 -0800777 GPR_ASSERT(op->recv_idempotent_request == NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800778 calld->recv_initial_metadata = op->recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800779 calld->on_done_recv_initial_metadata = op->recv_initial_metadata_ready;
780 op->recv_initial_metadata_ready = &calld->server_on_recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800781 op->recv_idempotent_request = &calld->recv_idempotent_request;
Craig Tillera82950e2015-09-22 12:33:20 -0700782 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700783}
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700784
Craig Tillera82950e2015-09-22 12:33:20 -0700785static void server_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
786 grpc_call_element *elem,
787 grpc_transport_stream_op *op) {
788 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
789 server_mutate_op(elem, op);
790 grpc_call_next_op(exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800791}
792
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800793static void got_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700794 grpc_error *error) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800795 grpc_call_element *elem = ptr;
796 call_data *calld = elem->call_data;
Craig Tillerf51457b2016-05-03 17:06:32 -0700797 if (error == GRPC_ERROR_NONE) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800798 start_new_rpc(exec_ctx, elem);
799 } else {
800 gpr_mu_lock(&calld->mu_state);
801 if (calld->state == NOT_STARTED) {
802 calld->state = ZOMBIED;
803 gpr_mu_unlock(&calld->mu_state);
804 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
Craig Tiller332f1b32016-05-24 13:21:21 -0700805 grpc_exec_ctx_sched(exec_ctx, &calld->kill_zombie_closure,
806 GRPC_ERROR_NONE, NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800807 } else if (calld->state == PENDING) {
808 calld->state = ZOMBIED;
809 gpr_mu_unlock(&calld->mu_state);
810 /* zombied call will be destroyed when it's removed from the pending
811 queue... later */
812 } else {
813 gpr_mu_unlock(&calld->mu_state);
814 }
815 }
816}
817
818static void accept_stream(grpc_exec_ctx *exec_ctx, void *cd,
819 grpc_transport *transport,
Craig Tillera82950e2015-09-22 12:33:20 -0700820 const void *transport_server_data) {
Craig Tillere039f032015-06-25 12:54:23 -0700821 channel_data *chand = cd;
822 /* create a call */
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700823 grpc_call *call = grpc_call_create(chand->channel, NULL, 0, NULL, NULL,
824 transport_server_data, NULL, 0,
825 gpr_inf_future(GPR_CLOCK_MONOTONIC));
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800826 grpc_call_element *elem =
827 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
828 call_data *calld = elem->call_data;
829 grpc_op op;
830 memset(&op, 0, sizeof(op));
831 op.op = GRPC_OP_RECV_INITIAL_METADATA;
832 op.data.recv_initial_metadata = &calld->initial_metadata;
833 grpc_closure_init(&calld->got_initial_metadata, got_initial_metadata, elem);
834 grpc_call_start_batch_and_execute(exec_ctx, call, &op, 1,
835 &calld->got_initial_metadata);
Craig Tillere039f032015-06-25 12:54:23 -0700836}
837
Craig Tillera82950e2015-09-22 12:33:20 -0700838static void channel_connectivity_changed(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700839 grpc_error *error) {
Craig Tillere039f032015-06-25 12:54:23 -0700840 channel_data *chand = cd;
841 grpc_server *server = chand->server;
Craig Tiller48ed92e2016-06-02 11:07:12 -0700842 if (chand->connectivity_state != GRPC_CHANNEL_SHUTDOWN) {
Craig Tillera82950e2015-09-22 12:33:20 -0700843 grpc_transport_op op;
844 memset(&op, 0, sizeof(op));
845 op.on_connectivity_state_change = &chand->channel_connectivity_changed,
846 op.connectivity_state = &chand->connectivity_state;
847 grpc_channel_next_op(exec_ctx,
848 grpc_channel_stack_element(
849 grpc_channel_get_channel_stack(chand->channel), 0),
850 &op);
851 } else {
852 gpr_mu_lock(&server->mu_global);
853 destroy_channel(exec_ctx, chand);
854 gpr_mu_unlock(&server->mu_global);
855 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "connectivity");
856 }
Craig Tillere039f032015-06-25 12:54:23 -0700857}
858
Craig Tillera82950e2015-09-22 12:33:20 -0700859static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800860 grpc_call_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800861 call_data *calld = elem->call_data;
862 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700863 memset(calld, 0, sizeof(call_data));
864 calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
865 calld->call = grpc_call_from_top_element(elem);
866 gpr_mu_init(&calld->mu_state);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800867
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800868 grpc_closure_init(&calld->server_on_recv_initial_metadata,
869 server_on_recv_initial_metadata, elem);
Craig Tiller1e6facb2015-06-11 22:47:11 -0700870
Craig Tillera82950e2015-09-22 12:33:20 -0700871 server_ref(chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800872}
873
Craig Tiller2c8063c2016-03-22 22:12:15 -0700874static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
David Garcia Quintas5dde14c2016-07-28 17:29:27 -0700875 const grpc_call_final_info *final_info,
876 void *ignored) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800877 channel_data *chand = elem->channel_data;
Craig Tillerdb7db992015-01-29 11:19:01 -0800878 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800879
Craig Tillera82950e2015-09-22 12:33:20 -0700880 GPR_ASSERT(calld->state != PENDING);
Craig Tiller092d8d12015-07-04 22:35:00 -0700881
Craig Tillera82950e2015-09-22 12:33:20 -0700882 if (calld->host) {
883 GRPC_MDSTR_UNREF(calld->host);
884 }
885 if (calld->path) {
886 GRPC_MDSTR_UNREF(calld->path);
887 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800888 grpc_metadata_array_destroy(&calld->initial_metadata);
Craig Tiller4df31a62015-01-30 09:44:31 -0800889
Craig Tillera82950e2015-09-22 12:33:20 -0700890 gpr_mu_destroy(&calld->mu_state);
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700891
Craig Tillera82950e2015-09-22 12:33:20 -0700892 server_unref(exec_ctx, chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800893}
894
Craig Tillera82950e2015-09-22 12:33:20 -0700895static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800896 grpc_channel_element *elem,
897 grpc_channel_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800898 channel_data *chand = elem->channel_data;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800899 GPR_ASSERT(args->is_first);
900 GPR_ASSERT(!args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800901 chand->server = NULL;
902 chand->channel = NULL;
903 chand->next = chand->prev = chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800904 chand->registered_methods = NULL;
Craig Tillere039f032015-06-25 12:54:23 -0700905 chand->connectivity_state = GRPC_CHANNEL_IDLE;
Craig Tillera82950e2015-09-22 12:33:20 -0700906 grpc_closure_init(&chand->channel_connectivity_changed,
907 channel_connectivity_changed, chand);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800908}
909
Craig Tillera82950e2015-09-22 12:33:20 -0700910static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
911 grpc_channel_element *elem) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800912 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800913 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700914 if (chand->registered_methods) {
915 for (i = 0; i < chand->registered_method_slots; i++) {
916 if (chand->registered_methods[i].method) {
917 GRPC_MDSTR_UNREF(chand->registered_methods[i].method);
918 }
919 if (chand->registered_methods[i].host) {
920 GRPC_MDSTR_UNREF(chand->registered_methods[i].host);
921 }
Craig Tillerec3257c2015-02-12 15:59:43 -0800922 }
Craig Tillera82950e2015-09-22 12:33:20 -0700923 gpr_free(chand->registered_methods);
924 }
925 if (chand->server) {
926 gpr_mu_lock(&chand->server->mu_global);
927 chand->next->prev = chand->prev;
928 chand->prev->next = chand->next;
929 chand->next = chand->prev = chand;
930 maybe_finish_shutdown(exec_ctx, chand->server);
931 gpr_mu_unlock(&chand->server->mu_global);
Craig Tillera82950e2015-09-22 12:33:20 -0700932 server_unref(exec_ctx, chand->server);
933 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800934}
935
Craig Tiller178edfa2016-02-17 20:54:46 -0800936const grpc_channel_filter grpc_server_top_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700937 server_start_transport_stream_op,
938 grpc_channel_next_op,
939 sizeof(call_data),
940 init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700941 grpc_call_stack_ignore_set_pollset_or_pollset_set,
Craig Tillerf40df232016-03-25 13:38:14 -0700942 destroy_call_elem,
943 sizeof(channel_data),
944 init_channel_elem,
945 destroy_channel_elem,
946 grpc_call_next_get_peer,
947 "server",
Craig Tiller9f28ac22015-01-27 17:01:29 -0800948};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800949
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700950static void register_completion_queue(grpc_server *server,
951 grpc_completion_queue *cq,
952 bool is_non_listening, void *reserved) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800953 size_t i, n;
Craig Tillera82950e2015-09-22 12:33:20 -0700954 GPR_ASSERT(!reserved);
955 for (i = 0; i < server->cq_count; i++) {
956 if (server->cqs[i] == cq) return;
957 }
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700958
Craig Tillera82950e2015-09-22 12:33:20 -0700959 grpc_cq_mark_server_cq(cq);
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700960
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700961 if (is_non_listening) {
962 grpc_cq_mark_non_listening_server_cq(cq);
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700963 }
Craig Tiller509b30e2016-05-21 12:32:39 -0700964
965 GRPC_CQ_INTERNAL_REF(cq, "server");
Craig Tiller20bc56d2015-02-12 09:02:56 -0800966 n = server->cq_count++;
Craig Tillera82950e2015-09-22 12:33:20 -0700967 server->cqs = gpr_realloc(server->cqs,
968 server->cq_count * sizeof(grpc_completion_queue *));
Craig Tiller20bc56d2015-02-12 09:02:56 -0800969 server->cqs[n] = cq;
970}
971
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700972void grpc_server_register_completion_queue(grpc_server *server,
973 grpc_completion_queue *cq,
974 void *reserved) {
975 GRPC_API_TRACE(
976 "grpc_server_register_completion_queue(server=%p, cq=%p, reserved=%p)", 3,
977 (server, cq, reserved));
978 register_completion_queue(server, cq, false, reserved);
979}
980
981void grpc_server_register_non_listening_completion_queue(
982 grpc_server *server, grpc_completion_queue *cq, void *reserved) {
983 GRPC_API_TRACE(
984 "grpc_server_register_non_listening_completion_queue(server=%p, cq=%p, "
985 "reserved=%p)",
986 3, (server, cq, reserved));
987 register_completion_queue(server, cq, true, reserved);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800988}
989
Craig Tiller178edfa2016-02-17 20:54:46 -0800990grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved) {
Craig Tiller178edfa2016-02-17 20:54:46 -0800991 GRPC_API_TRACE("grpc_server_create(%p, %p)", 2, (args, reserved));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800992
Craig Tillera82950e2015-09-22 12:33:20 -0700993 grpc_server *server = gpr_malloc(sizeof(grpc_server));
Craig Tiller60fd3612015-03-05 16:24:22 -0800994
Craig Tillera82950e2015-09-22 12:33:20 -0700995 GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
Craig Tiller60fd3612015-03-05 16:24:22 -0800996
Craig Tillera82950e2015-09-22 12:33:20 -0700997 memset(server, 0, sizeof(grpc_server));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800998
Craig Tillera82950e2015-09-22 12:33:20 -0700999 gpr_mu_init(&server->mu_global);
1000 gpr_mu_init(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001001
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001002 /* decremented by grpc_server_destroy */
Craig Tillera82950e2015-09-22 12:33:20 -07001003 gpr_ref_init(&server->internal_refcount, 1);
1004 server->root_channel_data.next = server->root_channel_data.prev =
1005 &server->root_channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001006
Craig Tiller6a006ce2015-07-13 16:25:40 -07001007 /* TODO(ctiller): expose a channel_arg for this */
Craig Tillerb19dbea2016-07-12 15:33:11 -07001008 server->max_requested_calls_per_cq = 32768;
Craig Tillera82950e2015-09-22 12:33:20 -07001009 server->channel_args = grpc_channel_args_copy(args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001010
1011 return server;
1012}
1013
Craig Tillera82950e2015-09-22 12:33:20 -07001014static int streq(const char *a, const char *b) {
1015 if (a == NULL && b == NULL) return 1;
1016 if (a == NULL) return 0;
1017 if (b == NULL) return 0;
1018 return 0 == strcmp(a, b);
Craig Tiller24be0f72015-02-10 14:04:22 -08001019}
1020
Craig Tiller06cb1a92016-04-04 08:10:47 -07001021void *grpc_server_register_method(
1022 grpc_server *server, const char *method, const char *host,
1023 grpc_server_register_method_payload_handling payload_handling,
1024 uint32_t flags) {
Craig Tiller24be0f72015-02-10 14:04:22 -08001025 registered_method *m;
Craig Tillerb2906862016-03-10 06:50:07 -08001026 GRPC_API_TRACE(
1027 "grpc_server_register_method(server=%p, method=%s, host=%s, "
1028 "flags=0x%08x)",
1029 4, (server, method, host, flags));
Craig Tillera82950e2015-09-22 12:33:20 -07001030 if (!method) {
1031 gpr_log(GPR_ERROR,
1032 "grpc_server_register_method method string cannot be NULL");
1033 return NULL;
1034 }
1035 for (m = server->registered_methods; m; m = m->next) {
1036 if (streq(m->method, method) && streq(m->host, host)) {
1037 gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method,
1038 host ? host : "*");
Craig Tiller24be0f72015-02-10 14:04:22 -08001039 return NULL;
1040 }
Craig Tillera82950e2015-09-22 12:33:20 -07001041 }
Craig Tillerb2906862016-03-10 06:50:07 -08001042 if ((flags & ~GRPC_INITIAL_METADATA_USED_MASK) != 0) {
1043 gpr_log(GPR_ERROR, "grpc_server_register_method invalid flags 0x%08x",
1044 flags);
1045 return NULL;
1046 }
Craig Tillera82950e2015-09-22 12:33:20 -07001047 m = gpr_malloc(sizeof(registered_method));
1048 memset(m, 0, sizeof(*m));
Craig Tillera82950e2015-09-22 12:33:20 -07001049 m->method = gpr_strdup(method);
1050 m->host = gpr_strdup(host);
Craig Tiller24be0f72015-02-10 14:04:22 -08001051 m->next = server->registered_methods;
Craig Tiller5ada3d22016-04-04 08:30:59 -07001052 m->payload_handling = payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -08001053 m->flags = flags;
Craig Tiller24be0f72015-02-10 14:04:22 -08001054 server->registered_methods = m;
1055 return m;
1056}
1057
Craig Tillera82950e2015-09-22 12:33:20 -07001058void grpc_server_start(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001059 listener *l;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001060 size_t i;
Craig Tillerf5768a62015-09-22 10:54:34 -07001061 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001062
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001063 GRPC_API_TRACE("grpc_server_start(server=%p)", 1, (server));
1064
Craig Tiller88ef00e2016-05-17 09:31:49 -07001065 server->started = true;
Craig Tiller509b30e2016-05-21 12:32:39 -07001066 size_t pollset_count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001067 server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count);
Craig Tillerb19dbea2016-07-12 15:33:11 -07001068 server->request_freelist_per_cq =
1069 gpr_malloc(sizeof(*server->request_freelist_per_cq) * server->cq_count);
1070 server->requested_calls_per_cq =
1071 gpr_malloc(sizeof(*server->requested_calls_per_cq) * server->cq_count);
Craig Tillera82950e2015-09-22 12:33:20 -07001072 for (i = 0; i < server->cq_count; i++) {
Craig Tiller509b30e2016-05-21 12:32:39 -07001073 if (!grpc_cq_is_non_listening_server_cq(server->cqs[i])) {
1074 server->pollsets[pollset_count++] = grpc_cq_pollset(server->cqs[i]);
1075 }
Craig Tillerb19dbea2016-07-12 15:33:11 -07001076 server->request_freelist_per_cq[i] =
1077 gpr_stack_lockfree_create((size_t)server->max_requested_calls_per_cq);
1078 for (int j = 0; j < server->max_requested_calls_per_cq; j++) {
1079 gpr_stack_lockfree_push(server->request_freelist_per_cq[i], j);
1080 }
1081 server->requested_calls_per_cq[i] =
1082 gpr_malloc((size_t)server->max_requested_calls_per_cq *
1083 sizeof(*server->requested_calls_per_cq[i]));
Craig Tillerdb7c3562016-05-19 11:02:52 -07001084 }
1085 request_matcher_init(&server->unregistered_request_matcher,
Craig Tillerb19dbea2016-07-12 15:33:11 -07001086 (size_t)server->max_requested_calls_per_cq, server);
Craig Tillerdb7c3562016-05-19 11:02:52 -07001087 for (registered_method *rm = server->registered_methods; rm; rm = rm->next) {
Craig Tillerb19dbea2016-07-12 15:33:11 -07001088 request_matcher_init(&rm->request_matcher,
1089 (size_t)server->max_requested_calls_per_cq, server);
Craig Tillera82950e2015-09-22 12:33:20 -07001090 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001091
Craig Tillera82950e2015-09-22 12:33:20 -07001092 for (l = server->listeners; l; l = l->next) {
Craig Tiller509b30e2016-05-21 12:32:39 -07001093 l->start(&exec_ctx, server, l->arg, server->pollsets, pollset_count);
Craig Tillera82950e2015-09-22 12:33:20 -07001094 }
Craig Tillerdfff1b82015-09-21 14:39:57 -07001095
Craig Tillera82950e2015-09-22 12:33:20 -07001096 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001097}
1098
Craig Tillera82950e2015-09-22 12:33:20 -07001099void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *s,
1100 grpc_transport *transport,
Craig Tiller418a8212016-05-16 16:27:51 -07001101 grpc_pollset *accepting_pollset,
Craig Tillera82950e2015-09-22 12:33:20 -07001102 const grpc_channel_args *args) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001103 size_t num_registered_methods;
1104 size_t alloc;
1105 registered_method *rm;
1106 channel_registered_method *crm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001107 grpc_channel *channel;
1108 channel_data *chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001109 grpc_mdstr *host;
1110 grpc_mdstr *method;
Craig Tiller7536af02015-12-22 13:49:30 -08001111 uint32_t hash;
Craig Tillerf96dfc32015-09-10 14:43:18 -07001112 size_t slots;
Craig Tiller7536af02015-12-22 13:49:30 -08001113 uint32_t probes;
1114 uint32_t max_probes = 0;
Craig Tillere039f032015-06-25 12:54:23 -07001115 grpc_transport_op op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001116
Craig Tiller178edfa2016-02-17 20:54:46 -08001117 channel =
1118 grpc_channel_create(exec_ctx, NULL, args, GRPC_SERVER_CHANNEL, transport);
Craig Tillera82950e2015-09-22 12:33:20 -07001119 chand = (channel_data *)grpc_channel_stack_element(
Craig Tillerf40df232016-03-25 13:38:14 -07001120 grpc_channel_get_channel_stack(channel), 0)
1121 ->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001122 chand->server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001123 server_ref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001124 chand->channel = channel;
1125
Craig Tiller9f9d4222016-05-16 17:02:14 -07001126 size_t cq_idx;
1127 grpc_completion_queue *accepting_cq = grpc_cq_from_pollset(accepting_pollset);
1128 for (cq_idx = 0; cq_idx < s->cq_count; cq_idx++) {
1129 if (s->cqs[cq_idx] == accepting_cq) break;
1130 }
1131 if (cq_idx == s->cq_count) {
1132 /* completion queue not found: pick a random one to publish new calls to */
1133 cq_idx = (size_t)rand() % s->cq_count;
1134 }
1135 chand->cq_idx = cq_idx;
1136
Craig Tiller04cc8be2015-02-10 16:11:22 -08001137 num_registered_methods = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001138 for (rm = s->registered_methods; rm; rm = rm->next) {
1139 num_registered_methods++;
1140 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001141 /* build a lookup table phrased in terms of mdstr's in this channels context
1142 to quickly find registered methods */
Craig Tillera82950e2015-09-22 12:33:20 -07001143 if (num_registered_methods > 0) {
1144 slots = 2 * num_registered_methods;
1145 alloc = sizeof(channel_registered_method) * slots;
1146 chand->registered_methods = gpr_malloc(alloc);
1147 memset(chand->registered_methods, 0, alloc);
1148 for (rm = s->registered_methods; rm; rm = rm->next) {
Craig Tillerb2b42612015-11-20 12:02:17 -08001149 host = rm->host ? grpc_mdstr_from_string(rm->host) : NULL;
1150 method = grpc_mdstr_from_string(rm->method);
Craig Tillera82950e2015-09-22 12:33:20 -07001151 hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash);
1152 for (probes = 0; chand->registered_methods[(hash + probes) % slots]
Craig Tillerf40df232016-03-25 13:38:14 -07001153 .server_registered_method != NULL;
Craig Tillera82950e2015-09-22 12:33:20 -07001154 probes++)
1155 ;
1156 if (probes > max_probes) max_probes = probes;
1157 crm = &chand->registered_methods[(hash + probes) % slots];
1158 crm->server_registered_method = rm;
Craig Tillerb2906862016-03-10 06:50:07 -08001159 crm->flags = rm->flags;
Craig Tillera82950e2015-09-22 12:33:20 -07001160 crm->host = host;
1161 crm->method = method;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001162 }
Craig Tiller7536af02015-12-22 13:49:30 -08001163 GPR_ASSERT(slots <= UINT32_MAX);
1164 chand->registered_method_slots = (uint32_t)slots;
Craig Tillera82950e2015-09-22 12:33:20 -07001165 chand->registered_method_max_probes = max_probes;
1166 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001167
Craig Tillera82950e2015-09-22 12:33:20 -07001168 gpr_mu_lock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001169 chand->next = &s->root_channel_data;
1170 chand->prev = chand->next->prev;
1171 chand->next->prev = chand->prev->next = chand;
Craig Tillera82950e2015-09-22 12:33:20 -07001172 gpr_mu_unlock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001173
Craig Tillera82950e2015-09-22 12:33:20 -07001174 GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity");
1175 memset(&op, 0, sizeof(op));
Craig Tillerd7f12e32016-03-03 10:08:31 -08001176 op.set_accept_stream = true;
1177 op.set_accept_stream_fn = accept_stream;
Craig Tiller4b804102015-06-26 16:16:12 -07001178 op.set_accept_stream_user_data = chand;
1179 op.on_connectivity_state_change = &chand->channel_connectivity_changed;
1180 op.connectivity_state = &chand->connectivity_state;
Craig Tiller804ff712016-05-05 16:25:40 -07001181 if (gpr_atm_acq_load(&s->shutdown_flag) != 0) {
1182 op.disconnect_with_error = GRPC_ERROR_CREATE("Server shutdown");
1183 }
Craig Tillera82950e2015-09-22 12:33:20 -07001184 grpc_transport_perform_op(exec_ctx, transport, &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001185}
1186
Craig Tillera82950e2015-09-22 12:33:20 -07001187void done_published_shutdown(grpc_exec_ctx *exec_ctx, void *done_arg,
1188 grpc_cq_completion *storage) {
1189 (void)done_arg;
1190 gpr_free(storage);
murgatroid9900a3dab2015-08-19 11:15:38 -07001191}
1192
Craig Tillera82950e2015-09-22 12:33:20 -07001193static void listener_destroy_done(grpc_exec_ctx *exec_ctx, void *s,
Craig Tillerf51457b2016-05-03 17:06:32 -07001194 grpc_error *error) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001195 grpc_server *server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001196 gpr_mu_lock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001197 server->listeners_destroyed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001198 maybe_finish_shutdown(exec_ctx, server);
1199 gpr_mu_unlock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001200}
1201
Craig Tillera82950e2015-09-22 12:33:20 -07001202void grpc_server_shutdown_and_notify(grpc_server *server,
1203 grpc_completion_queue *cq, void *tag) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001204 listener *l;
Craig Tillerbce999f2015-05-27 09:55:51 -07001205 shutdown_tag *sdt;
Craig Tillerff3ae682015-06-29 17:44:04 -07001206 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001207 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001208
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001209 GRPC_API_TRACE("grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", 3,
1210 (server, cq, tag));
1211
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001212 /* lock, and gather up some stuff to do */
Craig Tillera82950e2015-09-22 12:33:20 -07001213 gpr_mu_lock(&server->mu_global);
Craig Tiller4bf29282015-12-14 11:25:48 -08001214 grpc_cq_begin_op(cq, tag);
Craig Tillera82950e2015-09-22 12:33:20 -07001215 if (server->shutdown_published) {
Craig Tillerf51457b2016-05-03 17:06:32 -07001216 grpc_cq_end_op(&exec_ctx, cq, tag, GRPC_ERROR_NONE, done_published_shutdown,
1217 NULL, gpr_malloc(sizeof(grpc_cq_completion)));
Craig Tillera82950e2015-09-22 12:33:20 -07001218 gpr_mu_unlock(&server->mu_global);
1219 goto done;
1220 }
1221 server->shutdown_tags =
1222 gpr_realloc(server->shutdown_tags,
1223 sizeof(shutdown_tag) * (server->num_shutdown_tags + 1));
Craig Tillerbce999f2015-05-27 09:55:51 -07001224 sdt = &server->shutdown_tags[server->num_shutdown_tags++];
1225 sdt->tag = tag;
1226 sdt->cq = cq;
Craig Tillera82950e2015-09-22 12:33:20 -07001227 if (gpr_atm_acq_load(&server->shutdown_flag)) {
1228 gpr_mu_unlock(&server->mu_global);
1229 goto done;
1230 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001231
Craig Tillera82950e2015-09-22 12:33:20 -07001232 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
Craig Tillerab54f792015-07-08 08:34:20 -07001233
Craig Tillera82950e2015-09-22 12:33:20 -07001234 channel_broadcaster_init(server, &broadcaster);
nnoble0c475f02014-12-05 15:37:39 -08001235
Craig Tillerfc193e12015-09-24 15:29:03 -07001236 gpr_atm_rel_store(&server->shutdown_flag, 1);
1237
Craig Tillerbd217572015-02-11 18:10:56 -08001238 /* collect all unregistered then registered calls */
Craig Tillera82950e2015-09-22 12:33:20 -07001239 gpr_mu_lock(&server->mu_call);
Craig Tillercae4b1b2016-05-10 09:11:09 -07001240 kill_pending_work_locked(&exec_ctx, server,
1241 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001242 gpr_mu_unlock(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001243
Craig Tillera82950e2015-09-22 12:33:20 -07001244 maybe_finish_shutdown(&exec_ctx, server);
1245 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001246
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001247 /* Shutdown listeners */
Craig Tillera82950e2015-09-22 12:33:20 -07001248 for (l = server->listeners; l; l = l->next) {
1249 grpc_closure_init(&l->destroy_done, listener_destroy_done, server);
1250 l->destroy(&exec_ctx, server, l->arg, &l->destroy_done);
1251 }
Craig Tillerff3ae682015-06-29 17:44:04 -07001252
Craig Tillera82950e2015-09-22 12:33:20 -07001253 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 1, 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001254
Craig Tillerdfff1b82015-09-21 14:39:57 -07001255done:
Craig Tillera82950e2015-09-22 12:33:20 -07001256 grpc_exec_ctx_finish(&exec_ctx);
Craig Tilleraec96aa2015-04-07 14:32:15 -07001257}
1258
Craig Tillera82950e2015-09-22 12:33:20 -07001259void grpc_server_cancel_all_calls(grpc_server *server) {
Craig Tiller092d8d12015-07-04 22:35:00 -07001260 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001261 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerafa2d632015-05-26 16:39:13 -07001262
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001263 GRPC_API_TRACE("grpc_server_cancel_all_calls(server=%p)", 1, (server));
1264
Craig Tillera82950e2015-09-22 12:33:20 -07001265 gpr_mu_lock(&server->mu_global);
1266 channel_broadcaster_init(server, &broadcaster);
1267 gpr_mu_unlock(&server->mu_global);
Craig Tillerafa2d632015-05-26 16:39:13 -07001268
Craig Tiller804ff712016-05-05 16:25:40 -07001269 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 0,
1270 GRPC_ERROR_CREATE("Cancelling all calls"));
Craig Tillera82950e2015-09-22 12:33:20 -07001271 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerafa2d632015-05-26 16:39:13 -07001272}
1273
Craig Tillera82950e2015-09-22 12:33:20 -07001274void grpc_server_destroy(grpc_server *server) {
Craig Tilleraec96aa2015-04-07 14:32:15 -07001275 listener *l;
Craig Tillerf5768a62015-09-22 10:54:34 -07001276 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller872af022015-04-24 15:57:52 -07001277
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001278 GRPC_API_TRACE("grpc_server_destroy(server=%p)", 1, (server));
1279
Craig Tillera82950e2015-09-22 12:33:20 -07001280 gpr_mu_lock(&server->mu_global);
1281 GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners);
1282 GPR_ASSERT(server->listeners_destroyed == num_listeners(server));
Craig Tilleraec96aa2015-04-07 14:32:15 -07001283
Craig Tillera82950e2015-09-22 12:33:20 -07001284 while (server->listeners) {
1285 l = server->listeners;
1286 server->listeners = l->next;
1287 gpr_free(l);
1288 }
Craig Tilleraec96aa2015-04-07 14:32:15 -07001289
Craig Tillera82950e2015-09-22 12:33:20 -07001290 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001291
Craig Tillera82950e2015-09-22 12:33:20 -07001292 server_unref(&exec_ctx, server);
1293 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001294}
1295
Craig Tillera82950e2015-09-22 12:33:20 -07001296void grpc_server_add_listener(
1297 grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1298 void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1299 grpc_pollset **pollsets, size_t pollset_count),
1300 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1301 grpc_closure *on_done)) {
1302 listener *l = gpr_malloc(sizeof(listener));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001303 l->arg = arg;
1304 l->start = start;
1305 l->destroy = destroy;
1306 l->next = server->listeners;
1307 server->listeners = l;
1308}
1309
Craig Tillera82950e2015-09-22 12:33:20 -07001310static grpc_call_error queue_call_request(grpc_exec_ctx *exec_ctx,
Craig Tiller9f9d4222016-05-16 17:02:14 -07001311 grpc_server *server, size_t cq_idx,
Craig Tillera82950e2015-09-22 12:33:20 -07001312 requested_call *rc) {
Yang Gaoeb8e7cd2015-02-11 11:43:40 -08001313 call_data *calld = NULL;
Craig Tillerb9d35962015-09-11 13:31:16 -07001314 request_matcher *rm = NULL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001315 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -07001316 if (gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tiller48abdde2016-05-24 06:55:28 -07001317 fail_call(exec_ctx, server, cq_idx, rc,
1318 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001319 return GRPC_CALL_OK;
1320 }
Craig Tillerb19dbea2016-07-12 15:33:11 -07001321 request_id = gpr_stack_lockfree_pop(server->request_freelist_per_cq[cq_idx]);
Craig Tillera82950e2015-09-22 12:33:20 -07001322 if (request_id == -1) {
1323 /* out of request ids: just fail this one */
Craig Tiller48abdde2016-05-24 06:55:28 -07001324 fail_call(exec_ctx, server, cq_idx, rc,
Craig Tillerbdfa39c2016-07-12 16:08:13 -07001325 grpc_error_set_int(GRPC_ERROR_CREATE("Out of request ids"),
1326 GRPC_ERROR_INT_LIMIT,
1327 server->max_requested_calls_per_cq));
Craig Tillera82950e2015-09-22 12:33:20 -07001328 return GRPC_CALL_OK;
1329 }
1330 switch (rc->type) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001331 case BATCH_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001332 rm = &server->unregistered_request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001333 break;
1334 case REGISTERED_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001335 rm = &rc->data.registered.registered_method->request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001336 break;
Craig Tillera82950e2015-09-22 12:33:20 -07001337 }
Craig Tillerb19dbea2016-07-12 15:33:11 -07001338 server->requested_calls_per_cq[cq_idx][request_id] = *rc;
Craig Tillera82950e2015-09-22 12:33:20 -07001339 gpr_free(rc);
Craig Tillerdb7c3562016-05-19 11:02:52 -07001340 if (gpr_stack_lockfree_push(rm->requests_per_cq[cq_idx], request_id)) {
Craig Tillera82950e2015-09-22 12:33:20 -07001341 /* this was the first queued request: we need to lock and start
1342 matching calls */
1343 gpr_mu_lock(&server->mu_call);
Craig Tillerb9d35962015-09-11 13:31:16 -07001344 while ((calld = rm->pending_head) != NULL) {
Craig Tillerdb7c3562016-05-19 11:02:52 -07001345 request_id = gpr_stack_lockfree_pop(rm->requests_per_cq[cq_idx]);
Craig Tillera82950e2015-09-22 12:33:20 -07001346 if (request_id == -1) break;
Craig Tillerb9d35962015-09-11 13:31:16 -07001347 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -07001348 gpr_mu_unlock(&server->mu_call);
1349 gpr_mu_lock(&calld->mu_state);
1350 if (calld->state == ZOMBIED) {
1351 gpr_mu_unlock(&calld->mu_state);
1352 grpc_closure_init(
1353 &calld->kill_zombie_closure, kill_zombie,
1354 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tiller332f1b32016-05-24 13:21:21 -07001355 grpc_exec_ctx_sched(exec_ctx, &calld->kill_zombie_closure,
1356 GRPC_ERROR_NONE, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001357 } else {
1358 GPR_ASSERT(calld->state == PENDING);
1359 calld->state = ACTIVATED;
1360 gpr_mu_unlock(&calld->mu_state);
Craig Tiller9f9d4222016-05-16 17:02:14 -07001361 publish_call(exec_ctx, server, calld, cq_idx,
Craig Tillerb19dbea2016-07-12 15:33:11 -07001362 &server->requested_calls_per_cq[cq_idx][request_id]);
Craig Tillera82950e2015-09-22 12:33:20 -07001363 }
1364 gpr_mu_lock(&server->mu_call);
Craig Tiller45724b32015-09-22 10:42:19 -07001365 }
Craig Tillera82950e2015-09-22 12:33:20 -07001366 gpr_mu_unlock(&server->mu_call);
1367 }
Craig Tiller6a006ce2015-07-13 16:25:40 -07001368 return GRPC_CALL_OK;
Craig Tillercce17ac2015-01-20 09:29:28 -08001369}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001370
Craig Tillera82950e2015-09-22 12:33:20 -07001371grpc_call_error grpc_server_request_call(
1372 grpc_server *server, grpc_call **call, grpc_call_details *details,
1373 grpc_metadata_array *initial_metadata,
1374 grpc_completion_queue *cq_bound_to_call,
1375 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001376 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001377 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001378 requested_call *rc = gpr_malloc(sizeof(*rc));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001379 GRPC_API_TRACE(
1380 "grpc_server_request_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001381 "server=%p, call=%p, details=%p, initial_metadata=%p, "
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001382 "cq_bound_to_call=%p, cq_for_notification=%p, tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001383 7, (server, call, details, initial_metadata, cq_bound_to_call,
1384 cq_for_notification, tag));
Craig Tiller9f9d4222016-05-16 17:02:14 -07001385 size_t cq_idx;
1386 for (cq_idx = 0; cq_idx < server->cq_count; cq_idx++) {
1387 if (server->cqs[cq_idx] == cq_for_notification) {
1388 break;
1389 }
1390 }
1391 if (cq_idx == server->cq_count) {
Craig Tillera82950e2015-09-22 12:33:20 -07001392 gpr_free(rc);
1393 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1394 goto done;
1395 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001396 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller9928d392015-08-18 09:40:24 -07001397 details->reserved = NULL;
Craig Tillerb19dbea2016-07-12 15:33:11 -07001398 rc->cq_idx = cq_idx;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001399 rc->type = BATCH_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001400 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001401 rc->tag = tag;
1402 rc->cq_bound_to_call = cq_bound_to_call;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001403 rc->call = call;
1404 rc->data.batch.details = details;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001405 rc->initial_metadata = initial_metadata;
Craig Tiller9f9d4222016-05-16 17:02:14 -07001406 error = queue_call_request(&exec_ctx, server, cq_idx, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001407done:
Craig Tillera82950e2015-09-22 12:33:20 -07001408 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001409 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001410}
1411
Craig Tillera82950e2015-09-22 12:33:20 -07001412grpc_call_error grpc_server_request_registered_call(
Craig Tillerb9d35962015-09-11 13:31:16 -07001413 grpc_server *server, void *rmp, grpc_call **call, gpr_timespec *deadline,
Craig Tillera82950e2015-09-22 12:33:20 -07001414 grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload,
1415 grpc_completion_queue *cq_bound_to_call,
1416 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001417 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001418 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001419 requested_call *rc = gpr_malloc(sizeof(*rc));
Craig Tillerb9d35962015-09-11 13:31:16 -07001420 registered_method *rm = rmp;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001421 GRPC_API_TRACE(
1422 "grpc_server_request_registered_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001423 "server=%p, rmp=%p, call=%p, deadline=%p, initial_metadata=%p, "
1424 "optional_payload=%p, cq_bound_to_call=%p, cq_for_notification=%p, "
1425 "tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001426 9, (server, rmp, call, deadline, initial_metadata, optional_payload,
1427 cq_bound_to_call, cq_for_notification, tag));
Craig Tiller9f9d4222016-05-16 17:02:14 -07001428
1429 size_t cq_idx;
1430 for (cq_idx = 0; cq_idx < server->cq_count; cq_idx++) {
1431 if (server->cqs[cq_idx] == cq_for_notification) {
1432 break;
1433 }
1434 }
1435 if (cq_idx == server->cq_count) {
Craig Tillera82950e2015-09-22 12:33:20 -07001436 gpr_free(rc);
1437 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1438 goto done;
1439 }
Craig Tiller06cb1a92016-04-04 08:10:47 -07001440 if ((optional_payload == NULL) !=
1441 (rm->payload_handling == GRPC_SRM_PAYLOAD_NONE)) {
1442 gpr_free(rc);
1443 error = GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH;
1444 goto done;
1445 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001446 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tillerb19dbea2016-07-12 15:33:11 -07001447 rc->cq_idx = cq_idx;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001448 rc->type = REGISTERED_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001449 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001450 rc->tag = tag;
1451 rc->cq_bound_to_call = cq_bound_to_call;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001452 rc->call = call;
Craig Tillerb9d35962015-09-11 13:31:16 -07001453 rc->data.registered.registered_method = rm;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001454 rc->data.registered.deadline = deadline;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001455 rc->initial_metadata = initial_metadata;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001456 rc->data.registered.optional_payload = optional_payload;
Craig Tiller9f9d4222016-05-16 17:02:14 -07001457 error = queue_call_request(&exec_ctx, server, cq_idx, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001458done:
Craig Tillera82950e2015-09-22 12:33:20 -07001459 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001460 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001461}
1462
Craig Tillera82950e2015-09-22 12:33:20 -07001463static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tiller48abdde2016-05-24 06:55:28 -07001464 size_t cq_idx, requested_call *rc, grpc_error *error) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -07001465 *rc->call = NULL;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001466 rc->initial_metadata->count = 0;
Craig Tillercae4b1b2016-05-10 09:11:09 -07001467 GPR_ASSERT(error != GRPC_ERROR_NONE);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001468
Craig Tillera82950e2015-09-22 12:33:20 -07001469 server_ref(server);
Craig Tiller48abdde2016-05-24 06:55:28 -07001470 grpc_cq_end_op(exec_ctx, server->cqs[cq_idx], rc->tag, error,
Craig Tillera82950e2015-09-22 12:33:20 -07001471 done_request_event, rc, &rc->completion);
Craig Tiller24be0f72015-02-10 14:04:22 -08001472}
1473
Craig Tillera82950e2015-09-22 12:33:20 -07001474const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001475 return server->channel_args;
Craig Tiller190d3602015-02-18 09:23:38 -08001476}
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001477
Craig Tillera82950e2015-09-22 12:33:20 -07001478int grpc_server_has_open_connections(grpc_server *server) {
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001479 int r;
Craig Tillera82950e2015-09-22 12:33:20 -07001480 gpr_mu_lock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001481 r = server->root_channel_data.next != &server->root_channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -07001482 gpr_mu_unlock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001483 return r;
1484}