blob: 165d8aa647a75159fabab78e8fc54e32a89eddf8 [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;
76 void *tag;
Craig Tiller6a006ce2015-07-13 16:25:40 -070077 grpc_server *server;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070078 grpc_completion_queue *cq_bound_to_call;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070079 grpc_call **call;
Craig Tiller97fc6a32015-07-08 15:31:35 -070080 grpc_cq_completion completion;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080081 grpc_metadata_array *initial_metadata;
Craig Tillera82950e2015-09-22 12:33:20 -070082 union {
83 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080084 grpc_call_details *details;
Craig Tiller24be0f72015-02-10 14:04:22 -080085 } batch;
Craig Tillera82950e2015-09-22 12:33:20 -070086 struct {
Craig Tiller24be0f72015-02-10 14:04:22 -080087 registered_method *registered_method;
88 gpr_timespec *deadline;
Craig Tiller24be0f72015-02-10 14:04:22 -080089 grpc_byte_buffer **optional_payload;
90 } registered;
91 } data;
92} requested_call;
93
Craig Tillera82950e2015-09-22 12:33:20 -070094typedef struct channel_registered_method {
Craig Tiller24be0f72015-02-10 14:04:22 -080095 registered_method *server_registered_method;
Craig Tillerb2906862016-03-10 06:50:07 -080096 uint32_t flags;
Craig Tiller24be0f72015-02-10 14:04:22 -080097 grpc_mdstr *method;
98 grpc_mdstr *host;
99} channel_registered_method;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800100
Craig Tillera82950e2015-09-22 12:33:20 -0700101struct channel_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800102 grpc_server *server;
Craig Tillere039f032015-06-25 12:54:23 -0700103 grpc_connectivity_state connectivity_state;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800104 grpc_channel *channel;
Craig Tiller418a8212016-05-16 16:27:51 -0700105 size_t cq_idx;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 /* linked list of all channels on a server */
107 channel_data *next;
108 channel_data *prev;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800109 channel_registered_method *registered_methods;
Craig Tiller7536af02015-12-22 13:49:30 -0800110 uint32_t registered_method_slots;
111 uint32_t registered_method_max_probes;
Craig Tiller33825112015-09-18 07:44:19 -0700112 grpc_closure finish_destroy_channel_closure;
113 grpc_closure channel_connectivity_changed;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800114};
115
Craig Tillera82950e2015-09-22 12:33:20 -0700116typedef struct shutdown_tag {
Craig Tillerbce999f2015-05-27 09:55:51 -0700117 void *tag;
118 grpc_completion_queue *cq;
Craig Tiller97fc6a32015-07-08 15:31:35 -0700119 grpc_cq_completion completion;
Craig Tillerbce999f2015-05-27 09:55:51 -0700120} shutdown_tag;
121
Craig Tillera82950e2015-09-22 12:33:20 -0700122typedef enum {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800123 /* waiting for metadata */
124 NOT_STARTED,
125 /* inital metadata read, not flow controlled in yet */
126 PENDING,
127 /* flow controlled in, on completion queue */
128 ACTIVATED,
129 /* cancelled before being queued */
130 ZOMBIED
131} call_state;
132
Craig Tiller729b35a2015-07-13 12:36:47 -0700133typedef struct request_matcher request_matcher;
134
Craig Tillera82950e2015-09-22 12:33:20 -0700135struct call_data {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800136 grpc_call *call;
137
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700138 /** protects state */
139 gpr_mu mu_state;
140 /** the current state of a call - see call_state */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141 call_state state;
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700142
Craig Tillercce17ac2015-01-20 09:29:28 -0800143 grpc_mdstr *path;
144 grpc_mdstr *host;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700145 gpr_timespec deadline;
Craig Tillercce17ac2015-01-20 09:29:28 -0800146
Craig Tiller20bc56d2015-02-12 09:02:56 -0800147 grpc_completion_queue *cq_new;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800148
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800149 grpc_metadata_batch *recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800150 bool recv_idempotent_request;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800151 grpc_metadata_array initial_metadata;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700152
Craig Tiller88512692016-04-04 09:32:52 -0700153 request_matcher *request_matcher;
154 grpc_byte_buffer *payload;
155
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800156 grpc_closure got_initial_metadata;
157 grpc_closure server_on_recv_initial_metadata;
Craig Tiller33825112015-09-18 07:44:19 -0700158 grpc_closure kill_zombie_closure;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800159 grpc_closure *on_done_recv_initial_metadata;
David Garcia Quintas284488b2015-05-28 16:27:39 -0700160
Craig Tiller88512692016-04-04 09:32:52 -0700161 grpc_closure publish;
162
Craig Tiller729b35a2015-07-13 12:36:47 -0700163 call_data *pending_next;
164};
165
Craig Tillera82950e2015-09-22 12:33:20 -0700166struct request_matcher {
Craig Tiller88512692016-04-04 09:32:52 -0700167 grpc_server *server;
Craig Tiller729b35a2015-07-13 12:36:47 -0700168 call_data *pending_head;
169 call_data *pending_tail;
Craig Tillerdb7c3562016-05-19 11:02:52 -0700170 gpr_stack_lockfree **requests_per_cq;
Craig Tiller729b35a2015-07-13 12:36:47 -0700171};
172
Craig Tillera82950e2015-09-22 12:33:20 -0700173struct registered_method {
Craig Tiller729b35a2015-07-13 12:36:47 -0700174 char *method;
175 char *host;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700176 grpc_server_register_method_payload_handling payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -0800177 uint32_t flags;
Craig Tillerfa96d862016-05-21 12:39:56 -0700178 /* one request matcher per method */
Craig Tiller729b35a2015-07-13 12:36:47 -0700179 request_matcher request_matcher;
180 registered_method *next;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800181};
182
Craig Tillera82950e2015-09-22 12:33:20 -0700183typedef struct {
Craig Tillerff3ae682015-06-29 17:44:04 -0700184 grpc_channel **channels;
185 size_t num_channels;
186} channel_broadcaster;
187
Craig Tillera82950e2015-09-22 12:33:20 -0700188struct grpc_server {
Craig Tiller729b35a2015-07-13 12:36:47 -0700189 grpc_channel_args *channel_args;
190
191 grpc_completion_queue **cqs;
192 grpc_pollset **pollsets;
193 size_t cq_count;
Craig Tiller88ef00e2016-05-17 09:31:49 -0700194 bool started;
Craig Tiller729b35a2015-07-13 12:36:47 -0700195
196 /* The two following mutexes control access to server-state
197 mu_global controls access to non-call-related state (e.g., channel state)
198 mu_call controls access to call-related state (e.g., the call lists)
199
200 If they are ever required to be nested, you must lock mu_global
201 before mu_call. This is currently used in shutdown processing
202 (grpc_server_shutdown_and_notify and maybe_finish_shutdown) */
Craig Tillera82950e2015-09-22 12:33:20 -0700203 gpr_mu mu_global; /* mutex for server and channel state */
204 gpr_mu mu_call; /* mutex for call-specific state */
Craig Tiller729b35a2015-07-13 12:36:47 -0700205
206 registered_method *registered_methods;
Craig Tillerfa96d862016-05-21 12:39:56 -0700207 /** one request matcher for unregistered methods */
Craig Tiller729b35a2015-07-13 12:36:47 -0700208 request_matcher unregistered_request_matcher;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700209 /** free list of available requested_calls indices */
210 gpr_stack_lockfree *request_freelist;
211 /** requested call backing data */
212 requested_call *requested_calls;
Craig Tiller32ca48c2015-09-10 11:47:15 -0700213 size_t max_requested_calls;
Craig Tiller729b35a2015-07-13 12:36:47 -0700214
Craig Tiller6a006ce2015-07-13 16:25:40 -0700215 gpr_atm shutdown_flag;
Craig Tiller7536af02015-12-22 13:49:30 -0800216 uint8_t shutdown_published;
Craig Tiller729b35a2015-07-13 12:36:47 -0700217 size_t num_shutdown_tags;
218 shutdown_tag *shutdown_tags;
219
220 channel_data root_channel_data;
221
222 listener *listeners;
223 int listeners_destroyed;
224 gpr_refcount internal_refcount;
225
226 /** when did we print the last shutdown progress message */
227 gpr_timespec last_shutdown_message_time;
228};
229
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800230#define SERVER_FROM_CALL_ELEM(elem) \
231 (((channel_data *)(elem)->channel_data)->server)
232
Craig Tillerf51457b2016-05-03 17:06:32 -0700233static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *calld,
234 grpc_error *error);
Craig Tillera82950e2015-09-22 12:33:20 -0700235static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tiller48abdde2016-05-24 06:55:28 -0700236 size_t cq_idx, requested_call *rc, grpc_error *error);
Vijay Pai8931cdd2015-06-17 12:42:17 -0700237/* Before calling maybe_finish_shutdown, we must hold mu_global and not
238 hold mu_call */
Craig Tillera82950e2015-09-22 12:33:20 -0700239static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx, grpc_server *server);
Craig Tiller24be0f72015-02-10 14:04:22 -0800240
Craig Tiller729b35a2015-07-13 12:36:47 -0700241/*
242 * channel broadcaster
243 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700244
245/* assumes server locked */
Craig Tillera82950e2015-09-22 12:33:20 -0700246static void channel_broadcaster_init(grpc_server *s, channel_broadcaster *cb) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700247 channel_data *c;
248 size_t count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700249 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
250 count++;
251 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700252 cb->num_channels = count;
Craig Tillera82950e2015-09-22 12:33:20 -0700253 cb->channels = gpr_malloc(sizeof(*cb->channels) * cb->num_channels);
Craig Tillerff3ae682015-06-29 17:44:04 -0700254 count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700255 for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) {
256 cb->channels[count++] = c->channel;
257 GRPC_CHANNEL_INTERNAL_REF(c->channel, "broadcast");
258 }
Craig Tillerff3ae682015-06-29 17:44:04 -0700259}
260
Craig Tillera82950e2015-09-22 12:33:20 -0700261struct shutdown_cleanup_args {
Craig Tiller33825112015-09-18 07:44:19 -0700262 grpc_closure closure;
Craig Tillerff3ae682015-06-29 17:44:04 -0700263 gpr_slice slice;
264};
265
Craig Tillera82950e2015-09-22 12:33:20 -0700266static void shutdown_cleanup(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tillerf51457b2016-05-03 17:06:32 -0700267 grpc_error *error) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700268 struct shutdown_cleanup_args *a = arg;
Craig Tillera82950e2015-09-22 12:33:20 -0700269 gpr_slice_unref(a->slice);
270 gpr_free(a);
Craig Tillerff3ae682015-06-29 17:44:04 -0700271}
272
Craig Tillera82950e2015-09-22 12:33:20 -0700273static void send_shutdown(grpc_exec_ctx *exec_ctx, grpc_channel *channel,
Craig Tiller804ff712016-05-05 16:25:40 -0700274 int send_goaway, grpc_error *send_disconnect) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700275 grpc_transport_op op;
276 struct shutdown_cleanup_args *sc;
277 grpc_channel_element *elem;
278
Craig Tillera82950e2015-09-22 12:33:20 -0700279 memset(&op, 0, sizeof(op));
Craig Tillerff3ae682015-06-29 17:44:04 -0700280 op.send_goaway = send_goaway;
Craig Tillera82950e2015-09-22 12:33:20 -0700281 sc = gpr_malloc(sizeof(*sc));
282 sc->slice = gpr_slice_from_copied_string("Server shutdown");
Craig Tillerff3ae682015-06-29 17:44:04 -0700283 op.goaway_message = &sc->slice;
284 op.goaway_status = GRPC_STATUS_OK;
Craig Tiller804ff712016-05-05 16:25:40 -0700285 op.disconnect_with_error = send_disconnect;
Craig Tillera82950e2015-09-22 12:33:20 -0700286 grpc_closure_init(&sc->closure, shutdown_cleanup, sc);
Craig Tillerff3ae682015-06-29 17:44:04 -0700287 op.on_consumed = &sc->closure;
288
Craig Tillera82950e2015-09-22 12:33:20 -0700289 elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
290 elem->filter->start_transport_op(exec_ctx, elem, &op);
Craig Tillerff3ae682015-06-29 17:44:04 -0700291}
292
Craig Tillera82950e2015-09-22 12:33:20 -0700293static void channel_broadcaster_shutdown(grpc_exec_ctx *exec_ctx,
294 channel_broadcaster *cb,
295 int send_goaway,
Craig Tiller804ff712016-05-05 16:25:40 -0700296 grpc_error *force_disconnect) {
Craig Tillerff3ae682015-06-29 17:44:04 -0700297 size_t i;
298
Craig Tillera82950e2015-09-22 12:33:20 -0700299 for (i = 0; i < cb->num_channels; i++) {
Craig Tiller71f96652016-05-11 23:17:45 -0700300 send_shutdown(exec_ctx, cb->channels[i], send_goaway,
301 GRPC_ERROR_REF(force_disconnect));
Craig Tillera82950e2015-09-22 12:33:20 -0700302 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, cb->channels[i], "broadcast");
303 }
304 gpr_free(cb->channels);
Craig Tiller71f96652016-05-11 23:17:45 -0700305 GRPC_ERROR_UNREF(force_disconnect);
Craig Tillerff3ae682015-06-29 17:44:04 -0700306}
307
Craig Tiller729b35a2015-07-13 12:36:47 -0700308/*
309 * request_matcher
310 */
Craig Tillerff3ae682015-06-29 17:44:04 -0700311
Craig Tiller88512692016-04-04 09:32:52 -0700312static void request_matcher_init(request_matcher *rm, size_t entries,
313 grpc_server *server) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700314 memset(rm, 0, sizeof(*rm));
Craig Tiller88512692016-04-04 09:32:52 -0700315 rm->server = server;
Craig Tillerdb7c3562016-05-19 11:02:52 -0700316 rm->requests_per_cq =
317 gpr_malloc(sizeof(*rm->requests_per_cq) * server->cq_count);
318 for (size_t i = 0; i < server->cq_count; i++) {
319 rm->requests_per_cq[i] = gpr_stack_lockfree_create(entries);
320 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800321}
322
Craig Tillerb9d35962015-09-11 13:31:16 -0700323static void request_matcher_destroy(request_matcher *rm) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700324 for (size_t i = 0; i < rm->server->cq_count; i++) {
325 GPR_ASSERT(gpr_stack_lockfree_pop(rm->requests_per_cq[i]) == -1);
326 gpr_stack_lockfree_destroy(rm->requests_per_cq[i]);
327 }
328 gpr_free(rm->requests_per_cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800329}
330
Craig Tillerf51457b2016-05-03 17:06:32 -0700331static void kill_zombie(grpc_exec_ctx *exec_ctx, void *elem,
332 grpc_error *error) {
Craig Tillera82950e2015-09-22 12:33:20 -0700333 grpc_call_destroy(grpc_call_from_top_element(elem));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800334}
335
Craig Tiller8dc09712015-09-24 13:58:16 -0700336static void request_matcher_zombify_all_pending_calls(grpc_exec_ctx *exec_ctx,
337 request_matcher *rm) {
Craig Tillerb9d35962015-09-11 13:31:16 -0700338 while (rm->pending_head) {
339 call_data *calld = rm->pending_head;
340 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -0700341 gpr_mu_lock(&calld->mu_state);
342 calld->state = ZOMBIED;
343 gpr_mu_unlock(&calld->mu_state);
344 grpc_closure_init(
345 &calld->kill_zombie_closure, kill_zombie,
346 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tiller332f1b32016-05-24 13:21:21 -0700347 grpc_exec_ctx_sched(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
348 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700349 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800350}
351
Craig Tillera82950e2015-09-22 12:33:20 -0700352static void request_matcher_kill_requests(grpc_exec_ctx *exec_ctx,
353 grpc_server *server,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700354 request_matcher *rm,
355 grpc_error *error) {
Craig Tiller1191e212015-07-30 14:49:02 -0700356 int request_id;
Craig Tillerdb7c3562016-05-19 11:02:52 -0700357 for (size_t i = 0; i < server->cq_count; i++) {
358 while ((request_id = gpr_stack_lockfree_pop(rm->requests_per_cq[i])) !=
359 -1) {
Craig Tiller48abdde2016-05-24 06:55:28 -0700360 fail_call(exec_ctx, server, i, &server->requested_calls[request_id],
361 GRPC_ERROR_REF(error));
Craig Tillerdb7c3562016-05-19 11:02:52 -0700362 }
Craig Tillera82950e2015-09-22 12:33:20 -0700363 }
Craig Tillercae4b1b2016-05-10 09:11:09 -0700364 GRPC_ERROR_UNREF(error);
Craig Tiller1191e212015-07-30 14:49:02 -0700365}
366
Craig Tiller729b35a2015-07-13 12:36:47 -0700367/*
368 * server proper
369 */
370
Craig Tillera82950e2015-09-22 12:33:20 -0700371static void server_ref(grpc_server *server) {
372 gpr_ref(&server->internal_refcount);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800373}
374
Craig Tillera82950e2015-09-22 12:33:20 -0700375static void server_delete(grpc_exec_ctx *exec_ctx, grpc_server *server) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800376 registered_method *rm;
Craig Tiller89504612015-04-27 11:48:46 -0700377 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700378 grpc_channel_args_destroy(server->channel_args);
379 gpr_mu_destroy(&server->mu_global);
380 gpr_mu_destroy(&server->mu_call);
Craig Tillera82950e2015-09-22 12:33:20 -0700381 while ((rm = server->registered_methods) != NULL) {
382 server->registered_methods = rm->next;
Craig Tiller88ef00e2016-05-17 09:31:49 -0700383 if (server->started) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700384 request_matcher_destroy(&rm->request_matcher);
Craig Tiller418a8212016-05-16 16:27:51 -0700385 }
Craig Tillera82950e2015-09-22 12:33:20 -0700386 gpr_free(rm->method);
387 gpr_free(rm->host);
388 gpr_free(rm);
389 }
Craig Tillerdb7c3562016-05-19 11:02:52 -0700390 if (server->started) {
391 request_matcher_destroy(&server->unregistered_request_matcher);
392 }
Craig Tillera82950e2015-09-22 12:33:20 -0700393 for (i = 0; i < server->cq_count; i++) {
394 GRPC_CQ_INTERNAL_UNREF(server->cqs[i], "server");
395 }
Craig Tillera82950e2015-09-22 12:33:20 -0700396 gpr_stack_lockfree_destroy(server->request_freelist);
397 gpr_free(server->cqs);
398 gpr_free(server->pollsets);
399 gpr_free(server->shutdown_tags);
400 gpr_free(server->requested_calls);
401 gpr_free(server);
Craig Tilleree945e82015-05-26 16:15:34 -0700402}
403
Craig Tillera82950e2015-09-22 12:33:20 -0700404static void server_unref(grpc_exec_ctx *exec_ctx, grpc_server *server) {
405 if (gpr_unref(&server->internal_refcount)) {
406 server_delete(exec_ctx, server);
407 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800408}
409
Craig Tillera82950e2015-09-22 12:33:20 -0700410static int is_channel_orphaned(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800411 return chand->next == chand;
412}
413
Craig Tillera82950e2015-09-22 12:33:20 -0700414static void orphan_channel(channel_data *chand) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800415 chand->next->prev = chand->prev;
416 chand->prev->next = chand->next;
417 chand->next = chand->prev = chand;
418}
419
Craig Tillera82950e2015-09-22 12:33:20 -0700420static void finish_destroy_channel(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700421 grpc_error *error) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800422 channel_data *chand = cd;
423 grpc_server *server = chand->server;
Craig Tillera82950e2015-09-22 12:33:20 -0700424 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "server");
425 server_unref(exec_ctx, server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800426}
427
Craig Tillera82950e2015-09-22 12:33:20 -0700428static void destroy_channel(grpc_exec_ctx *exec_ctx, channel_data *chand) {
429 if (is_channel_orphaned(chand)) return;
430 GPR_ASSERT(chand->server != NULL);
431 orphan_channel(chand);
432 server_ref(chand->server);
433 maybe_finish_shutdown(exec_ctx, chand->server);
David Garcia Quintas284488b2015-05-28 16:27:39 -0700434 chand->finish_destroy_channel_closure.cb = finish_destroy_channel;
435 chand->finish_destroy_channel_closure.cb_arg = chand;
Craig Tillerd7f12e32016-03-03 10:08:31 -0800436
437 grpc_transport_op op;
438 memset(&op, 0, sizeof(op));
439 op.set_accept_stream = true;
440 op.on_consumed = &chand->finish_destroy_channel_closure;
441 grpc_channel_next_op(exec_ctx,
442 grpc_channel_stack_element(
443 grpc_channel_get_channel_stack(chand->channel), 0),
444 &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800445}
446
Craig Tiller06cb1a92016-04-04 08:10:47 -0700447static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) {
448 gpr_slice slice = value->slice;
449 size_t len = GPR_SLICE_LENGTH(slice);
Craig Tiller6a006ce2015-07-13 16:25:40 -0700450
Craig Tiller06cb1a92016-04-04 08:10:47 -0700451 if (len + 1 > *capacity) {
452 *capacity = GPR_MAX(len + 1, *capacity * 2);
453 *dest = gpr_realloc(*dest, *capacity);
454 }
455 memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1);
456}
457
Craig Tiller88512692016-04-04 09:32:52 -0700458static void done_request_event(grpc_exec_ctx *exec_ctx, void *req,
459 grpc_cq_completion *c) {
460 requested_call *rc = req;
461 grpc_server *server = rc->server;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700462
Craig Tiller88512692016-04-04 09:32:52 -0700463 if (rc >= server->requested_calls &&
464 rc < server->requested_calls + server->max_requested_calls) {
465 GPR_ASSERT(rc - server->requested_calls <= INT_MAX);
466 gpr_stack_lockfree_push(server->request_freelist,
467 (int)(rc - server->requested_calls));
468 } else {
469 gpr_free(req);
470 }
Craig Tiller06cb1a92016-04-04 08:10:47 -0700471
Craig Tiller88512692016-04-04 09:32:52 -0700472 server_unref(exec_ctx, server);
473}
Craig Tiller06cb1a92016-04-04 08:10:47 -0700474
Craig Tiller88512692016-04-04 09:32:52 -0700475static void publish_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tiller9f9d4222016-05-16 17:02:14 -0700476 call_data *calld, size_t cq_idx, requested_call *rc) {
Craig Tiller06cb1a92016-04-04 08:10:47 -0700477 grpc_call_set_completion_queue(exec_ctx, calld->call, rc->cq_bound_to_call);
Craig Tiller88512692016-04-04 09:32:52 -0700478 grpc_call *call = calld->call;
479 *rc->call = call;
Craig Tiller9f9d4222016-05-16 17:02:14 -0700480 calld->cq_new = server->cqs[cq_idx];
Craig Tiller06cb1a92016-04-04 08:10:47 -0700481 GPR_SWAP(grpc_metadata_array, *rc->initial_metadata, calld->initial_metadata);
482 switch (rc->type) {
483 case BATCH_CALL:
484 GPR_ASSERT(calld->host != NULL);
485 GPR_ASSERT(calld->path != NULL);
486 cpstr(&rc->data.batch.details->host,
487 &rc->data.batch.details->host_capacity, calld->host);
488 cpstr(&rc->data.batch.details->method,
489 &rc->data.batch.details->method_capacity, calld->path);
490 rc->data.batch.details->deadline = calld->deadline;
491 rc->data.batch.details->flags =
492 0 | (calld->recv_idempotent_request
493 ? GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST
494 : 0);
495 break;
496 case REGISTERED_CALL:
497 *rc->data.registered.deadline = calld->deadline;
498 if (rc->data.registered.optional_payload) {
Craig Tiller88512692016-04-04 09:32:52 -0700499 *rc->data.registered.optional_payload = calld->payload;
Craig Tiller06cb1a92016-04-04 08:10:47 -0700500 }
501 break;
502 default:
503 GPR_UNREACHABLE_CODE(return );
504 }
505
Craig Tiller88512692016-04-04 09:32:52 -0700506 grpc_call_element *elem =
507 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
508 channel_data *chand = elem->channel_data;
509 server_ref(chand->server);
Craig Tillerf51457b2016-05-03 17:06:32 -0700510 grpc_cq_end_op(exec_ctx, calld->cq_new, rc->tag, GRPC_ERROR_NONE,
511 done_request_event, rc, &rc->completion);
Craig Tiller06cb1a92016-04-04 08:10:47 -0700512}
513
Craig Tillerf51457b2016-05-03 17:06:32 -0700514static void publish_new_rpc(grpc_exec_ctx *exec_ctx, void *arg,
515 grpc_error *error) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700516 grpc_call_element *call_elem = arg;
517 call_data *calld = call_elem->call_data;
518 channel_data *chand = call_elem->channel_data;
Craig Tiller88512692016-04-04 09:32:52 -0700519 request_matcher *rm = calld->request_matcher;
520 grpc_server *server = rm->server;
Craig Tiller6a006ce2015-07-13 16:25:40 -0700521
Craig Tillerf51457b2016-05-03 17:06:32 -0700522 if (error != GRPC_ERROR_NONE || gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tillera82950e2015-09-22 12:33:20 -0700523 gpr_mu_lock(&calld->mu_state);
524 calld->state = ZOMBIED;
525 gpr_mu_unlock(&calld->mu_state);
Craig Tiller88512692016-04-04 09:32:52 -0700526 grpc_closure_init(
527 &calld->kill_zombie_closure, kill_zombie,
528 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tiller332f1b32016-05-24 13:21:21 -0700529 grpc_exec_ctx_sched(exec_ctx, &calld->kill_zombie_closure, error, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700530 return;
531 }
Craig Tiller45724b32015-09-22 10:42:19 -0700532
Craig Tillerdb7c3562016-05-19 11:02:52 -0700533 for (size_t i = 0; i < server->cq_count; i++) {
534 size_t cq_idx = (chand->cq_idx + i) % server->cq_count;
535 int request_id = gpr_stack_lockfree_pop(rm->requests_per_cq[cq_idx]);
536 if (request_id == -1) {
537 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700538 } else {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700539 gpr_mu_lock(&calld->mu_state);
540 calld->state = ACTIVATED;
541 gpr_mu_unlock(&calld->mu_state);
542 publish_call(exec_ctx, server, calld, cq_idx,
543 &server->requested_calls[request_id]);
544 return; /* early out */
Craig Tiller45724b32015-09-22 10:42:19 -0700545 }
Craig Tiller88512692016-04-04 09:32:52 -0700546 }
Craig Tillerdb7c3562016-05-19 11:02:52 -0700547
548 /* no cq to take the request found: queue it on the slow list */
549 gpr_mu_lock(&server->mu_call);
550 gpr_mu_lock(&calld->mu_state);
551 calld->state = PENDING;
552 gpr_mu_unlock(&calld->mu_state);
553 if (rm->pending_head == NULL) {
554 rm->pending_tail = rm->pending_head = calld;
555 } else {
556 rm->pending_tail->pending_next = calld;
557 rm->pending_tail = calld;
558 }
559 calld->pending_next = NULL;
560 gpr_mu_unlock(&server->mu_call);
Craig Tiller88512692016-04-04 09:32:52 -0700561}
562
563static void finish_start_new_rpc(
564 grpc_exec_ctx *exec_ctx, grpc_server *server, grpc_call_element *elem,
565 request_matcher *rm,
566 grpc_server_register_method_payload_handling payload_handling) {
567 call_data *calld = elem->call_data;
568
569 if (gpr_atm_acq_load(&server->shutdown_flag)) {
570 gpr_mu_lock(&calld->mu_state);
571 calld->state = ZOMBIED;
572 gpr_mu_unlock(&calld->mu_state);
573 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
Craig Tiller332f1b32016-05-24 13:21:21 -0700574 grpc_exec_ctx_sched(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
575 NULL);
Craig Tiller88512692016-04-04 09:32:52 -0700576 return;
577 }
578
579 calld->request_matcher = rm;
580
581 switch (payload_handling) {
582 case GRPC_SRM_PAYLOAD_NONE:
Craig Tiller48abdde2016-05-24 06:55:28 -0700583 publish_new_rpc(exec_ctx, elem, GRPC_ERROR_NONE);
Craig Tiller88512692016-04-04 09:32:52 -0700584 break;
585 case GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER: {
586 grpc_op op;
587 memset(&op, 0, sizeof(op));
588 op.op = GRPC_OP_RECV_MESSAGE;
589 op.data.recv_message = &calld->payload;
Craig Tillerdb7c3562016-05-19 11:02:52 -0700590 grpc_closure_init(&calld->publish, publish_new_rpc, elem);
Craig Tiller88512692016-04-04 09:32:52 -0700591 grpc_call_start_batch_and_execute(exec_ctx, calld->call, &op, 1,
592 &calld->publish);
593 break;
594 }
Craig Tillera82950e2015-09-22 12:33:20 -0700595 }
Craig Tiller04cc8be2015-02-10 16:11:22 -0800596}
597
Craig Tillera82950e2015-09-22 12:33:20 -0700598static void start_new_rpc(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800599 channel_data *chand = elem->channel_data;
600 call_data *calld = elem->call_data;
601 grpc_server *server = chand->server;
Craig Tiller7536af02015-12-22 13:49:30 -0800602 uint32_t i;
603 uint32_t hash;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800604 channel_registered_method *rm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800605
Craig Tillera82950e2015-09-22 12:33:20 -0700606 if (chand->registered_methods && calld->path && calld->host) {
607 /* TODO(ctiller): unify these two searches */
608 /* check for an exact match with host */
609 hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash);
610 for (i = 0; i <= chand->registered_method_max_probes; i++) {
611 rm = &chand->registered_methods[(hash + i) %
612 chand->registered_method_slots];
613 if (!rm) break;
614 if (rm->host != calld->host) continue;
615 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800616 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
617 !calld->recv_idempotent_request)
618 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700619 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700620 &rm->server_registered_method->request_matcher,
621 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700622 return;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800623 }
Craig Tillera82950e2015-09-22 12:33:20 -0700624 /* check for a wildcard method definition (no host set) */
625 hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash);
626 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;
630 if (rm->host != NULL) continue;
631 if (rm->method != calld->path) continue;
Craig Tillerb2906862016-03-10 06:50:07 -0800632 if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
633 !calld->recv_idempotent_request)
634 continue;
Craig Tillera82950e2015-09-22 12:33:20 -0700635 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700636 &rm->server_registered_method->request_matcher,
637 rm->server_registered_method->payload_handling);
Craig Tillera82950e2015-09-22 12:33:20 -0700638 return;
639 }
640 }
641 finish_start_new_rpc(exec_ctx, server, elem,
Craig Tiller88512692016-04-04 09:32:52 -0700642 &server->unregistered_request_matcher,
643 GRPC_SRM_PAYLOAD_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800644}
645
Craig Tillera82950e2015-09-22 12:33:20 -0700646static int num_listeners(grpc_server *server) {
Craig Tilleree945e82015-05-26 16:15:34 -0700647 listener *l;
648 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700649 for (l = server->listeners; l; l = l->next) {
650 n++;
651 }
Craig Tilleree945e82015-05-26 16:15:34 -0700652 return n;
653}
654
Craig Tillera82950e2015-09-22 12:33:20 -0700655static void done_shutdown_event(grpc_exec_ctx *exec_ctx, void *server,
656 grpc_cq_completion *completion) {
657 server_unref(exec_ctx, server);
Craig Tiller97fc6a32015-07-08 15:31:35 -0700658}
659
Craig Tillera82950e2015-09-22 12:33:20 -0700660static int num_channels(grpc_server *server) {
Craig Tillerab54f792015-07-08 08:34:20 -0700661 channel_data *chand;
662 int n = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700663 for (chand = server->root_channel_data.next;
664 chand != &server->root_channel_data; chand = chand->next) {
665 n++;
666 }
Craig Tillerab54f792015-07-08 08:34:20 -0700667 return n;
668}
669
Craig Tillera82950e2015-09-22 12:33:20 -0700670static void kill_pending_work_locked(grpc_exec_ctx *exec_ctx,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700671 grpc_server *server, grpc_error *error) {
Craig Tiller88ef00e2016-05-17 09:31:49 -0700672 if (server->started) {
Craig Tillerdb7c3562016-05-19 11:02:52 -0700673 request_matcher_kill_requests(exec_ctx, server,
Craig Tiller48abdde2016-05-24 06:55:28 -0700674 &server->unregistered_request_matcher,
Craig Tillercae4b1b2016-05-10 09:11:09 -0700675 GRPC_ERROR_REF(error));
Craig Tillerdb7c3562016-05-19 11:02:52 -0700676 request_matcher_zombify_all_pending_calls(
677 exec_ctx, &server->unregistered_request_matcher);
678 for (registered_method *rm = server->registered_methods; rm;
679 rm = rm->next) {
Craig Tiller48abdde2016-05-24 06:55:28 -0700680 request_matcher_kill_requests(exec_ctx, server, &rm->request_matcher,
681 GRPC_ERROR_REF(error));
Craig Tillerdb7c3562016-05-19 11:02:52 -0700682 request_matcher_zombify_all_pending_calls(exec_ctx, &rm->request_matcher);
Craig Tiller418a8212016-05-16 16:27:51 -0700683 }
Craig Tillera82950e2015-09-22 12:33:20 -0700684 }
Craig Tillercae4b1b2016-05-10 09:11:09 -0700685 GRPC_ERROR_UNREF(error);
Craig Tillerdc627722015-05-26 15:27:02 -0700686}
687
Craig Tillera82950e2015-09-22 12:33:20 -0700688static void maybe_finish_shutdown(grpc_exec_ctx *exec_ctx,
689 grpc_server *server) {
Craig Tiller45724b32015-09-22 10:42:19 -0700690 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700691 if (!gpr_atm_acq_load(&server->shutdown_flag) || server->shutdown_published) {
692 return;
693 }
Craig Tiller45724b32015-09-22 10:42:19 -0700694
Craig Tillercae4b1b2016-05-10 09:11:09 -0700695 kill_pending_work_locked(exec_ctx, server,
696 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tiller45724b32015-09-22 10:42:19 -0700697
Craig Tillera82950e2015-09-22 12:33:20 -0700698 if (server->root_channel_data.next != &server->root_channel_data ||
699 server->listeners_destroyed < num_listeners(server)) {
700 if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME),
701 server->last_shutdown_message_time),
702 gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
703 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
704 gpr_log(GPR_DEBUG,
705 "Waiting for %d channels and %d/%d listeners to be destroyed"
706 " before shutting down server",
707 num_channels(server),
708 num_listeners(server) - server->listeners_destroyed,
709 num_listeners(server));
Craig Tiller45724b32015-09-22 10:42:19 -0700710 }
Craig Tillera82950e2015-09-22 12:33:20 -0700711 return;
712 }
Craig Tiller45724b32015-09-22 10:42:19 -0700713 server->shutdown_published = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700714 for (i = 0; i < server->num_shutdown_tags; i++) {
715 server_ref(server);
716 grpc_cq_end_op(exec_ctx, server->shutdown_tags[i].cq,
Craig Tillerf51457b2016-05-03 17:06:32 -0700717 server->shutdown_tags[i].tag, GRPC_ERROR_NONE,
718 done_shutdown_event, server,
Craig Tillera82950e2015-09-22 12:33:20 -0700719 &server->shutdown_tags[i].completion);
720 }
Craig Tiller45724b32015-09-22 10:42:19 -0700721}
722
Craig Tillera82950e2015-09-22 12:33:20 -0700723static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) {
Craig Tiller6902ad22015-04-16 08:01:49 -0700724 grpc_call_element *elem = user_data;
Craig Tillercce17ac2015-01-20 09:29:28 -0800725 call_data *calld = elem->call_data;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800726 if (md->key == GRPC_MDSTR_PATH) {
Craig Tiller9cac2a12016-04-04 20:56:56 -0700727 if (calld->path == NULL) {
728 calld->path = GRPC_MDSTR_REF(md->value);
729 }
Craig Tillera82950e2015-09-22 12:33:20 -0700730 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800731 } else if (md->key == GRPC_MDSTR_AUTHORITY) {
Craig Tiller9cac2a12016-04-04 20:56:56 -0700732 if (calld->host == NULL) {
733 calld->host = GRPC_MDSTR_REF(md->value);
734 }
Craig Tillera82950e2015-09-22 12:33:20 -0700735 return NULL;
736 }
Craig Tiller6902ad22015-04-16 08:01:49 -0700737 return md;
738}
739
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800740static void server_on_recv_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700741 grpc_error *error) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700742 grpc_call_element *elem = ptr;
Craig Tiller6902ad22015-04-16 08:01:49 -0700743 call_data *calld = elem->call_data;
Craig Tiller94329d02015-07-23 09:52:11 -0700744 gpr_timespec op_deadline;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700745
Craig Tiller8e5c9342016-05-12 15:34:59 -0700746 GRPC_ERROR_REF(error);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800747 grpc_metadata_batch_filter(calld->recv_initial_metadata, server_filter, elem);
748 op_deadline = calld->recv_initial_metadata->deadline;
749 if (0 != gpr_time_cmp(op_deadline, gpr_inf_future(op_deadline.clock_type))) {
750 calld->deadline = op_deadline;
751 }
752 if (calld->host && calld->path) {
753 /* do nothing */
754 } else {
Craig Tiller8e5c9342016-05-12 15:34:59 -0700755 GRPC_ERROR_UNREF(error);
Craig Tillerf51457b2016-05-03 17:06:32 -0700756 error =
757 GRPC_ERROR_CREATE_REFERENCING("Missing :authority or :path", &error, 1);
Craig Tillera82950e2015-09-22 12:33:20 -0700758 }
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700759
Craig Tiller332f1b32016-05-24 13:21:21 -0700760 grpc_exec_ctx_sched(exec_ctx, calld->on_done_recv_initial_metadata, error,
761 NULL);
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700762}
763
Craig Tillera82950e2015-09-22 12:33:20 -0700764static void server_mutate_op(grpc_call_element *elem,
765 grpc_transport_stream_op *op) {
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700766 call_data *calld = elem->call_data;
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700767
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800768 if (op->recv_initial_metadata != NULL) {
Craig Tiller4d40ba32016-03-09 17:48:40 -0800769 GPR_ASSERT(op->recv_idempotent_request == NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800770 calld->recv_initial_metadata = op->recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800771 calld->on_done_recv_initial_metadata = op->recv_initial_metadata_ready;
772 op->recv_initial_metadata_ready = &calld->server_on_recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800773 op->recv_idempotent_request = &calld->recv_idempotent_request;
Craig Tillera82950e2015-09-22 12:33:20 -0700774 }
Craig Tiller50d9db52015-04-23 10:52:14 -0700775}
Craig Tillerbe18b8d2015-04-22 14:00:47 -0700776
Craig Tillera82950e2015-09-22 12:33:20 -0700777static void server_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
778 grpc_call_element *elem,
779 grpc_transport_stream_op *op) {
780 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
781 server_mutate_op(elem, op);
782 grpc_call_next_op(exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800783}
784
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800785static void got_initial_metadata(grpc_exec_ctx *exec_ctx, void *ptr,
Craig Tillerf51457b2016-05-03 17:06:32 -0700786 grpc_error *error) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800787 grpc_call_element *elem = ptr;
788 call_data *calld = elem->call_data;
Craig Tillerf51457b2016-05-03 17:06:32 -0700789 if (error == GRPC_ERROR_NONE) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800790 start_new_rpc(exec_ctx, elem);
791 } else {
792 gpr_mu_lock(&calld->mu_state);
793 if (calld->state == NOT_STARTED) {
794 calld->state = ZOMBIED;
795 gpr_mu_unlock(&calld->mu_state);
796 grpc_closure_init(&calld->kill_zombie_closure, kill_zombie, elem);
Craig Tiller332f1b32016-05-24 13:21:21 -0700797 grpc_exec_ctx_sched(exec_ctx, &calld->kill_zombie_closure,
798 GRPC_ERROR_NONE, NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800799 } else if (calld->state == PENDING) {
800 calld->state = ZOMBIED;
801 gpr_mu_unlock(&calld->mu_state);
802 /* zombied call will be destroyed when it's removed from the pending
803 queue... later */
804 } else {
805 gpr_mu_unlock(&calld->mu_state);
806 }
807 }
808}
809
810static void accept_stream(grpc_exec_ctx *exec_ctx, void *cd,
811 grpc_transport *transport,
Craig Tillera82950e2015-09-22 12:33:20 -0700812 const void *transport_server_data) {
Craig Tillere039f032015-06-25 12:54:23 -0700813 channel_data *chand = cd;
814 /* create a call */
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700815 grpc_call *call = grpc_call_create(chand->channel, NULL, 0, NULL, NULL,
816 transport_server_data, NULL, 0,
817 gpr_inf_future(GPR_CLOCK_MONOTONIC));
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800818 grpc_call_element *elem =
819 grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
820 call_data *calld = elem->call_data;
821 grpc_op op;
822 memset(&op, 0, sizeof(op));
823 op.op = GRPC_OP_RECV_INITIAL_METADATA;
824 op.data.recv_initial_metadata = &calld->initial_metadata;
825 grpc_closure_init(&calld->got_initial_metadata, got_initial_metadata, elem);
826 grpc_call_start_batch_and_execute(exec_ctx, call, &op, 1,
827 &calld->got_initial_metadata);
Craig Tillere039f032015-06-25 12:54:23 -0700828}
829
Craig Tillera82950e2015-09-22 12:33:20 -0700830static void channel_connectivity_changed(grpc_exec_ctx *exec_ctx, void *cd,
Craig Tillerf51457b2016-05-03 17:06:32 -0700831 grpc_error *error) {
Craig Tillere039f032015-06-25 12:54:23 -0700832 channel_data *chand = cd;
833 grpc_server *server = chand->server;
Craig Tiller48ed92e2016-06-02 11:07:12 -0700834 if (chand->connectivity_state != GRPC_CHANNEL_SHUTDOWN) {
Craig Tillera82950e2015-09-22 12:33:20 -0700835 grpc_transport_op op;
836 memset(&op, 0, sizeof(op));
837 op.on_connectivity_state_change = &chand->channel_connectivity_changed,
838 op.connectivity_state = &chand->connectivity_state;
839 grpc_channel_next_op(exec_ctx,
840 grpc_channel_stack_element(
841 grpc_channel_get_channel_stack(chand->channel), 0),
842 &op);
843 } else {
844 gpr_mu_lock(&server->mu_global);
845 destroy_channel(exec_ctx, chand);
846 gpr_mu_unlock(&server->mu_global);
847 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->channel, "connectivity");
848 }
Craig Tillere039f032015-06-25 12:54:23 -0700849}
850
Mark D. Roth76d24422016-06-23 13:22:10 -0700851static grpc_error *init_call_elem(grpc_exec_ctx *exec_ctx,
Mark D. Roth0badbe82016-06-23 10:15:12 -0700852 grpc_call_element *elem,
853 grpc_call_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800854 call_data *calld = elem->call_data;
855 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700856 memset(calld, 0, sizeof(call_data));
857 calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
858 calld->call = grpc_call_from_top_element(elem);
859 gpr_mu_init(&calld->mu_state);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800860
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800861 grpc_closure_init(&calld->server_on_recv_initial_metadata,
862 server_on_recv_initial_metadata, elem);
Craig Tiller1e6facb2015-06-11 22:47:11 -0700863
Craig Tillera82950e2015-09-22 12:33:20 -0700864 server_ref(chand->server);
Mark D. Roth0badbe82016-06-23 10:15:12 -0700865 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800866}
867
Craig Tiller2c8063c2016-03-22 22:12:15 -0700868static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
David Garcia Quintase293b642016-05-02 10:29:51 -0700869 const grpc_call_stats *stats, void *ignored) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800870 channel_data *chand = elem->channel_data;
Craig Tillerdb7db992015-01-29 11:19:01 -0800871 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800872
Craig Tillera82950e2015-09-22 12:33:20 -0700873 GPR_ASSERT(calld->state != PENDING);
Craig Tiller092d8d12015-07-04 22:35:00 -0700874
Craig Tillera82950e2015-09-22 12:33:20 -0700875 if (calld->host) {
876 GRPC_MDSTR_UNREF(calld->host);
877 }
878 if (calld->path) {
879 GRPC_MDSTR_UNREF(calld->path);
880 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800881 grpc_metadata_array_destroy(&calld->initial_metadata);
Craig Tiller4df31a62015-01-30 09:44:31 -0800882
Craig Tillera82950e2015-09-22 12:33:20 -0700883 gpr_mu_destroy(&calld->mu_state);
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700884
Craig Tillera82950e2015-09-22 12:33:20 -0700885 server_unref(exec_ctx, chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800886}
887
Craig Tillera82950e2015-09-22 12:33:20 -0700888static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800889 grpc_channel_element *elem,
890 grpc_channel_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800891 channel_data *chand = elem->channel_data;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800892 GPR_ASSERT(args->is_first);
893 GPR_ASSERT(!args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800894 chand->server = NULL;
895 chand->channel = NULL;
896 chand->next = chand->prev = chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800897 chand->registered_methods = NULL;
Craig Tillere039f032015-06-25 12:54:23 -0700898 chand->connectivity_state = GRPC_CHANNEL_IDLE;
Craig Tillera82950e2015-09-22 12:33:20 -0700899 grpc_closure_init(&chand->channel_connectivity_changed,
900 channel_connectivity_changed, chand);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800901}
902
Craig Tillera82950e2015-09-22 12:33:20 -0700903static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
904 grpc_channel_element *elem) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800905 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800906 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700907 if (chand->registered_methods) {
908 for (i = 0; i < chand->registered_method_slots; i++) {
909 if (chand->registered_methods[i].method) {
910 GRPC_MDSTR_UNREF(chand->registered_methods[i].method);
911 }
912 if (chand->registered_methods[i].host) {
913 GRPC_MDSTR_UNREF(chand->registered_methods[i].host);
914 }
Craig Tillerec3257c2015-02-12 15:59:43 -0800915 }
Craig Tillera82950e2015-09-22 12:33:20 -0700916 gpr_free(chand->registered_methods);
917 }
918 if (chand->server) {
919 gpr_mu_lock(&chand->server->mu_global);
920 chand->next->prev = chand->prev;
921 chand->prev->next = chand->next;
922 chand->next = chand->prev = chand;
923 maybe_finish_shutdown(exec_ctx, chand->server);
924 gpr_mu_unlock(&chand->server->mu_global);
Craig Tillera82950e2015-09-22 12:33:20 -0700925 server_unref(exec_ctx, chand->server);
926 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800927}
928
Craig Tiller178edfa2016-02-17 20:54:46 -0800929const grpc_channel_filter grpc_server_top_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700930 server_start_transport_stream_op,
931 grpc_channel_next_op,
932 sizeof(call_data),
933 init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700934 grpc_call_stack_ignore_set_pollset_or_pollset_set,
Craig Tillerf40df232016-03-25 13:38:14 -0700935 destroy_call_elem,
936 sizeof(channel_data),
937 init_channel_elem,
938 destroy_channel_elem,
939 grpc_call_next_get_peer,
940 "server",
Craig Tiller9f28ac22015-01-27 17:01:29 -0800941};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800942
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700943static void register_completion_queue(grpc_server *server,
944 grpc_completion_queue *cq,
945 bool is_non_listening, void *reserved) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800946 size_t i, n;
Craig Tillera82950e2015-09-22 12:33:20 -0700947 GPR_ASSERT(!reserved);
948 for (i = 0; i < server->cq_count; i++) {
949 if (server->cqs[i] == cq) return;
950 }
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700951
Craig Tillera82950e2015-09-22 12:33:20 -0700952 grpc_cq_mark_server_cq(cq);
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700953
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700954 if (is_non_listening) {
955 grpc_cq_mark_non_listening_server_cq(cq);
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700956 }
Craig Tiller509b30e2016-05-21 12:32:39 -0700957
958 GRPC_CQ_INTERNAL_REF(cq, "server");
Craig Tiller20bc56d2015-02-12 09:02:56 -0800959 n = server->cq_count++;
Craig Tillera82950e2015-09-22 12:33:20 -0700960 server->cqs = gpr_realloc(server->cqs,
961 server->cq_count * sizeof(grpc_completion_queue *));
Craig Tiller20bc56d2015-02-12 09:02:56 -0800962 server->cqs[n] = cq;
963}
964
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700965void grpc_server_register_completion_queue(grpc_server *server,
966 grpc_completion_queue *cq,
967 void *reserved) {
968 GRPC_API_TRACE(
969 "grpc_server_register_completion_queue(server=%p, cq=%p, reserved=%p)", 3,
970 (server, cq, reserved));
971 register_completion_queue(server, cq, false, reserved);
972}
973
974void grpc_server_register_non_listening_completion_queue(
975 grpc_server *server, grpc_completion_queue *cq, void *reserved) {
976 GRPC_API_TRACE(
977 "grpc_server_register_non_listening_completion_queue(server=%p, cq=%p, "
978 "reserved=%p)",
979 3, (server, cq, reserved));
980 register_completion_queue(server, cq, true, reserved);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800981}
982
Craig Tiller178edfa2016-02-17 20:54:46 -0800983grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800984 size_t i;
Craig Tiller178edfa2016-02-17 20:54:46 -0800985
986 GRPC_API_TRACE("grpc_server_create(%p, %p)", 2, (args, reserved));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800987
Craig Tillera82950e2015-09-22 12:33:20 -0700988 grpc_server *server = gpr_malloc(sizeof(grpc_server));
Craig Tiller60fd3612015-03-05 16:24:22 -0800989
Craig Tillera82950e2015-09-22 12:33:20 -0700990 GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
Craig Tiller60fd3612015-03-05 16:24:22 -0800991
Craig Tillera82950e2015-09-22 12:33:20 -0700992 memset(server, 0, sizeof(grpc_server));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800993
Craig Tillera82950e2015-09-22 12:33:20 -0700994 gpr_mu_init(&server->mu_global);
995 gpr_mu_init(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800996
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800997 /* decremented by grpc_server_destroy */
Craig Tillera82950e2015-09-22 12:33:20 -0700998 gpr_ref_init(&server->internal_refcount, 1);
999 server->root_channel_data.next = server->root_channel_data.prev =
1000 &server->root_channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001001
Craig Tiller6a006ce2015-07-13 16:25:40 -07001002 /* TODO(ctiller): expose a channel_arg for this */
1003 server->max_requested_calls = 32768;
Craig Tillera82950e2015-09-22 12:33:20 -07001004 server->request_freelist =
1005 gpr_stack_lockfree_create(server->max_requested_calls);
1006 for (i = 0; i < (size_t)server->max_requested_calls; i++) {
1007 gpr_stack_lockfree_push(server->request_freelist, (int)i);
1008 }
Craig Tillera82950e2015-09-22 12:33:20 -07001009 server->requested_calls = gpr_malloc(server->max_requested_calls *
1010 sizeof(*server->requested_calls));
Craig Tiller729b35a2015-07-13 12:36:47 -07001011
Craig Tillera82950e2015-09-22 12:33:20 -07001012 server->channel_args = grpc_channel_args_copy(args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001013
1014 return server;
1015}
1016
Craig Tillera82950e2015-09-22 12:33:20 -07001017static int streq(const char *a, const char *b) {
1018 if (a == NULL && b == NULL) return 1;
1019 if (a == NULL) return 0;
1020 if (b == NULL) return 0;
1021 return 0 == strcmp(a, b);
Craig Tiller24be0f72015-02-10 14:04:22 -08001022}
1023
Craig Tiller06cb1a92016-04-04 08:10:47 -07001024void *grpc_server_register_method(
1025 grpc_server *server, const char *method, const char *host,
1026 grpc_server_register_method_payload_handling payload_handling,
1027 uint32_t flags) {
Craig Tiller24be0f72015-02-10 14:04:22 -08001028 registered_method *m;
Craig Tillerb2906862016-03-10 06:50:07 -08001029 GRPC_API_TRACE(
1030 "grpc_server_register_method(server=%p, method=%s, host=%s, "
1031 "flags=0x%08x)",
1032 4, (server, method, host, flags));
Craig Tillera82950e2015-09-22 12:33:20 -07001033 if (!method) {
1034 gpr_log(GPR_ERROR,
1035 "grpc_server_register_method method string cannot be NULL");
1036 return NULL;
1037 }
1038 for (m = server->registered_methods; m; m = m->next) {
1039 if (streq(m->method, method) && streq(m->host, host)) {
1040 gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method,
1041 host ? host : "*");
Craig Tiller24be0f72015-02-10 14:04:22 -08001042 return NULL;
1043 }
Craig Tillera82950e2015-09-22 12:33:20 -07001044 }
Craig Tillerb2906862016-03-10 06:50:07 -08001045 if ((flags & ~GRPC_INITIAL_METADATA_USED_MASK) != 0) {
1046 gpr_log(GPR_ERROR, "grpc_server_register_method invalid flags 0x%08x",
1047 flags);
1048 return NULL;
1049 }
Craig Tillera82950e2015-09-22 12:33:20 -07001050 m = gpr_malloc(sizeof(registered_method));
1051 memset(m, 0, sizeof(*m));
Craig Tillera82950e2015-09-22 12:33:20 -07001052 m->method = gpr_strdup(method);
1053 m->host = gpr_strdup(host);
Craig Tiller24be0f72015-02-10 14:04:22 -08001054 m->next = server->registered_methods;
Craig Tiller5ada3d22016-04-04 08:30:59 -07001055 m->payload_handling = payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -08001056 m->flags = flags;
Craig Tiller24be0f72015-02-10 14:04:22 -08001057 server->registered_methods = m;
1058 return m;
1059}
1060
Craig Tillera82950e2015-09-22 12:33:20 -07001061void grpc_server_start(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001062 listener *l;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001063 size_t i;
Craig Tillerf5768a62015-09-22 10:54:34 -07001064 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001065
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001066 GRPC_API_TRACE("grpc_server_start(server=%p)", 1, (server));
1067
Craig Tiller88ef00e2016-05-17 09:31:49 -07001068 server->started = true;
Craig Tiller509b30e2016-05-21 12:32:39 -07001069 size_t pollset_count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001070 server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count);
1071 for (i = 0; i < server->cq_count; i++) {
Craig Tiller509b30e2016-05-21 12:32:39 -07001072 if (!grpc_cq_is_non_listening_server_cq(server->cqs[i])) {
1073 server->pollsets[pollset_count++] = grpc_cq_pollset(server->cqs[i]);
1074 }
Craig Tillerdb7c3562016-05-19 11:02:52 -07001075 }
1076 request_matcher_init(&server->unregistered_request_matcher,
1077 server->max_requested_calls, server);
1078 for (registered_method *rm = server->registered_methods; rm; rm = rm->next) {
1079 request_matcher_init(&rm->request_matcher, server->max_requested_calls,
1080 server);
Craig Tillera82950e2015-09-22 12:33:20 -07001081 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001082
Craig Tillera82950e2015-09-22 12:33:20 -07001083 for (l = server->listeners; l; l = l->next) {
Craig Tiller509b30e2016-05-21 12:32:39 -07001084 l->start(&exec_ctx, server, l->arg, server->pollsets, pollset_count);
Craig Tillera82950e2015-09-22 12:33:20 -07001085 }
Craig Tillerdfff1b82015-09-21 14:39:57 -07001086
Craig Tillera82950e2015-09-22 12:33:20 -07001087 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001088}
1089
Craig Tillera82950e2015-09-22 12:33:20 -07001090void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *s,
1091 grpc_transport *transport,
Craig Tiller418a8212016-05-16 16:27:51 -07001092 grpc_pollset *accepting_pollset,
Craig Tillera82950e2015-09-22 12:33:20 -07001093 const grpc_channel_args *args) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001094 size_t num_registered_methods;
1095 size_t alloc;
1096 registered_method *rm;
1097 channel_registered_method *crm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001098 grpc_channel *channel;
1099 channel_data *chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001100 grpc_mdstr *host;
1101 grpc_mdstr *method;
Craig Tiller7536af02015-12-22 13:49:30 -08001102 uint32_t hash;
Craig Tillerf96dfc32015-09-10 14:43:18 -07001103 size_t slots;
Craig Tiller7536af02015-12-22 13:49:30 -08001104 uint32_t probes;
1105 uint32_t max_probes = 0;
Craig Tillere039f032015-06-25 12:54:23 -07001106 grpc_transport_op op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001107
Craig Tiller178edfa2016-02-17 20:54:46 -08001108 channel =
1109 grpc_channel_create(exec_ctx, NULL, args, GRPC_SERVER_CHANNEL, transport);
Craig Tillera82950e2015-09-22 12:33:20 -07001110 chand = (channel_data *)grpc_channel_stack_element(
Craig Tillerf40df232016-03-25 13:38:14 -07001111 grpc_channel_get_channel_stack(channel), 0)
1112 ->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001113 chand->server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001114 server_ref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001115 chand->channel = channel;
1116
Craig Tiller9f9d4222016-05-16 17:02:14 -07001117 size_t cq_idx;
1118 grpc_completion_queue *accepting_cq = grpc_cq_from_pollset(accepting_pollset);
1119 for (cq_idx = 0; cq_idx < s->cq_count; cq_idx++) {
1120 if (s->cqs[cq_idx] == accepting_cq) break;
1121 }
1122 if (cq_idx == s->cq_count) {
1123 /* completion queue not found: pick a random one to publish new calls to */
1124 cq_idx = (size_t)rand() % s->cq_count;
1125 }
1126 chand->cq_idx = cq_idx;
1127
Craig Tiller04cc8be2015-02-10 16:11:22 -08001128 num_registered_methods = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001129 for (rm = s->registered_methods; rm; rm = rm->next) {
1130 num_registered_methods++;
1131 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001132 /* build a lookup table phrased in terms of mdstr's in this channels context
1133 to quickly find registered methods */
Craig Tillera82950e2015-09-22 12:33:20 -07001134 if (num_registered_methods > 0) {
1135 slots = 2 * num_registered_methods;
1136 alloc = sizeof(channel_registered_method) * slots;
1137 chand->registered_methods = gpr_malloc(alloc);
1138 memset(chand->registered_methods, 0, alloc);
1139 for (rm = s->registered_methods; rm; rm = rm->next) {
Craig Tillerb2b42612015-11-20 12:02:17 -08001140 host = rm->host ? grpc_mdstr_from_string(rm->host) : NULL;
1141 method = grpc_mdstr_from_string(rm->method);
Craig Tillera82950e2015-09-22 12:33:20 -07001142 hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash);
1143 for (probes = 0; chand->registered_methods[(hash + probes) % slots]
Craig Tillerf40df232016-03-25 13:38:14 -07001144 .server_registered_method != NULL;
Craig Tillera82950e2015-09-22 12:33:20 -07001145 probes++)
1146 ;
1147 if (probes > max_probes) max_probes = probes;
1148 crm = &chand->registered_methods[(hash + probes) % slots];
1149 crm->server_registered_method = rm;
Craig Tillerb2906862016-03-10 06:50:07 -08001150 crm->flags = rm->flags;
Craig Tillera82950e2015-09-22 12:33:20 -07001151 crm->host = host;
1152 crm->method = method;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001153 }
Craig Tiller7536af02015-12-22 13:49:30 -08001154 GPR_ASSERT(slots <= UINT32_MAX);
1155 chand->registered_method_slots = (uint32_t)slots;
Craig Tillera82950e2015-09-22 12:33:20 -07001156 chand->registered_method_max_probes = max_probes;
1157 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001158
Craig Tillera82950e2015-09-22 12:33:20 -07001159 gpr_mu_lock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001160 chand->next = &s->root_channel_data;
1161 chand->prev = chand->next->prev;
1162 chand->next->prev = chand->prev->next = chand;
Craig Tillera82950e2015-09-22 12:33:20 -07001163 gpr_mu_unlock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001164
Craig Tillera82950e2015-09-22 12:33:20 -07001165 GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity");
1166 memset(&op, 0, sizeof(op));
Craig Tillerd7f12e32016-03-03 10:08:31 -08001167 op.set_accept_stream = true;
1168 op.set_accept_stream_fn = accept_stream;
Craig Tiller4b804102015-06-26 16:16:12 -07001169 op.set_accept_stream_user_data = chand;
1170 op.on_connectivity_state_change = &chand->channel_connectivity_changed;
1171 op.connectivity_state = &chand->connectivity_state;
Craig Tiller804ff712016-05-05 16:25:40 -07001172 if (gpr_atm_acq_load(&s->shutdown_flag) != 0) {
1173 op.disconnect_with_error = GRPC_ERROR_CREATE("Server shutdown");
1174 }
Craig Tillera82950e2015-09-22 12:33:20 -07001175 grpc_transport_perform_op(exec_ctx, transport, &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001176}
1177
Craig Tillera82950e2015-09-22 12:33:20 -07001178void done_published_shutdown(grpc_exec_ctx *exec_ctx, void *done_arg,
1179 grpc_cq_completion *storage) {
1180 (void)done_arg;
1181 gpr_free(storage);
murgatroid9900a3dab2015-08-19 11:15:38 -07001182}
1183
Craig Tillera82950e2015-09-22 12:33:20 -07001184static void listener_destroy_done(grpc_exec_ctx *exec_ctx, void *s,
Craig Tillerf51457b2016-05-03 17:06:32 -07001185 grpc_error *error) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001186 grpc_server *server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001187 gpr_mu_lock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001188 server->listeners_destroyed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001189 maybe_finish_shutdown(exec_ctx, server);
1190 gpr_mu_unlock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001191}
1192
Craig Tillera82950e2015-09-22 12:33:20 -07001193void grpc_server_shutdown_and_notify(grpc_server *server,
1194 grpc_completion_queue *cq, void *tag) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001195 listener *l;
Craig Tillerbce999f2015-05-27 09:55:51 -07001196 shutdown_tag *sdt;
Craig Tillerff3ae682015-06-29 17:44:04 -07001197 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001198 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001199
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001200 GRPC_API_TRACE("grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", 3,
1201 (server, cq, tag));
1202
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001203 /* lock, and gather up some stuff to do */
Craig Tillera82950e2015-09-22 12:33:20 -07001204 gpr_mu_lock(&server->mu_global);
Craig Tiller4bf29282015-12-14 11:25:48 -08001205 grpc_cq_begin_op(cq, tag);
Craig Tillera82950e2015-09-22 12:33:20 -07001206 if (server->shutdown_published) {
Craig Tillerf51457b2016-05-03 17:06:32 -07001207 grpc_cq_end_op(&exec_ctx, cq, tag, GRPC_ERROR_NONE, done_published_shutdown,
1208 NULL, gpr_malloc(sizeof(grpc_cq_completion)));
Craig Tillera82950e2015-09-22 12:33:20 -07001209 gpr_mu_unlock(&server->mu_global);
1210 goto done;
1211 }
1212 server->shutdown_tags =
1213 gpr_realloc(server->shutdown_tags,
1214 sizeof(shutdown_tag) * (server->num_shutdown_tags + 1));
Craig Tillerbce999f2015-05-27 09:55:51 -07001215 sdt = &server->shutdown_tags[server->num_shutdown_tags++];
1216 sdt->tag = tag;
1217 sdt->cq = cq;
Craig Tillera82950e2015-09-22 12:33:20 -07001218 if (gpr_atm_acq_load(&server->shutdown_flag)) {
1219 gpr_mu_unlock(&server->mu_global);
1220 goto done;
1221 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001222
Craig Tillera82950e2015-09-22 12:33:20 -07001223 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
Craig Tillerab54f792015-07-08 08:34:20 -07001224
Craig Tillera82950e2015-09-22 12:33:20 -07001225 channel_broadcaster_init(server, &broadcaster);
nnoble0c475f02014-12-05 15:37:39 -08001226
Craig Tillerfc193e12015-09-24 15:29:03 -07001227 gpr_atm_rel_store(&server->shutdown_flag, 1);
1228
Craig Tillerbd217572015-02-11 18:10:56 -08001229 /* collect all unregistered then registered calls */
Craig Tillera82950e2015-09-22 12:33:20 -07001230 gpr_mu_lock(&server->mu_call);
Craig Tillercae4b1b2016-05-10 09:11:09 -07001231 kill_pending_work_locked(&exec_ctx, server,
1232 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001233 gpr_mu_unlock(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001234
Craig Tillera82950e2015-09-22 12:33:20 -07001235 maybe_finish_shutdown(&exec_ctx, server);
1236 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001237
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001238 /* Shutdown listeners */
Craig Tillera82950e2015-09-22 12:33:20 -07001239 for (l = server->listeners; l; l = l->next) {
1240 grpc_closure_init(&l->destroy_done, listener_destroy_done, server);
1241 l->destroy(&exec_ctx, server, l->arg, &l->destroy_done);
1242 }
Craig Tillerff3ae682015-06-29 17:44:04 -07001243
Craig Tillera82950e2015-09-22 12:33:20 -07001244 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 1, 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001245
Craig Tillerdfff1b82015-09-21 14:39:57 -07001246done:
Craig Tillera82950e2015-09-22 12:33:20 -07001247 grpc_exec_ctx_finish(&exec_ctx);
Craig Tilleraec96aa2015-04-07 14:32:15 -07001248}
1249
Craig Tillera82950e2015-09-22 12:33:20 -07001250void grpc_server_cancel_all_calls(grpc_server *server) {
Craig Tiller092d8d12015-07-04 22:35:00 -07001251 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001252 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerafa2d632015-05-26 16:39:13 -07001253
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001254 GRPC_API_TRACE("grpc_server_cancel_all_calls(server=%p)", 1, (server));
1255
Craig Tillera82950e2015-09-22 12:33:20 -07001256 gpr_mu_lock(&server->mu_global);
1257 channel_broadcaster_init(server, &broadcaster);
1258 gpr_mu_unlock(&server->mu_global);
Craig Tillerafa2d632015-05-26 16:39:13 -07001259
Craig Tiller804ff712016-05-05 16:25:40 -07001260 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 0,
1261 GRPC_ERROR_CREATE("Cancelling all calls"));
Craig Tillera82950e2015-09-22 12:33:20 -07001262 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerafa2d632015-05-26 16:39:13 -07001263}
1264
Craig Tillera82950e2015-09-22 12:33:20 -07001265void grpc_server_destroy(grpc_server *server) {
Craig Tilleraec96aa2015-04-07 14:32:15 -07001266 listener *l;
Craig Tillerf5768a62015-09-22 10:54:34 -07001267 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller872af022015-04-24 15:57:52 -07001268
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001269 GRPC_API_TRACE("grpc_server_destroy(server=%p)", 1, (server));
1270
Craig Tillera82950e2015-09-22 12:33:20 -07001271 gpr_mu_lock(&server->mu_global);
1272 GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners);
1273 GPR_ASSERT(server->listeners_destroyed == num_listeners(server));
Craig Tilleraec96aa2015-04-07 14:32:15 -07001274
Craig Tillera82950e2015-09-22 12:33:20 -07001275 while (server->listeners) {
1276 l = server->listeners;
1277 server->listeners = l->next;
1278 gpr_free(l);
1279 }
Craig Tilleraec96aa2015-04-07 14:32:15 -07001280
Craig Tillera82950e2015-09-22 12:33:20 -07001281 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001282
Craig Tillera82950e2015-09-22 12:33:20 -07001283 server_unref(&exec_ctx, server);
1284 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001285}
1286
Craig Tillera82950e2015-09-22 12:33:20 -07001287void grpc_server_add_listener(
1288 grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1289 void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1290 grpc_pollset **pollsets, size_t pollset_count),
1291 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1292 grpc_closure *on_done)) {
1293 listener *l = gpr_malloc(sizeof(listener));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001294 l->arg = arg;
1295 l->start = start;
1296 l->destroy = destroy;
1297 l->next = server->listeners;
1298 server->listeners = l;
1299}
1300
Craig Tillera82950e2015-09-22 12:33:20 -07001301static grpc_call_error queue_call_request(grpc_exec_ctx *exec_ctx,
Craig Tiller9f9d4222016-05-16 17:02:14 -07001302 grpc_server *server, size_t cq_idx,
Craig Tillera82950e2015-09-22 12:33:20 -07001303 requested_call *rc) {
Yang Gaoeb8e7cd2015-02-11 11:43:40 -08001304 call_data *calld = NULL;
Craig Tillerb9d35962015-09-11 13:31:16 -07001305 request_matcher *rm = NULL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001306 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -07001307 if (gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tiller48abdde2016-05-24 06:55:28 -07001308 fail_call(exec_ctx, server, cq_idx, rc,
1309 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001310 return GRPC_CALL_OK;
1311 }
1312 request_id = gpr_stack_lockfree_pop(server->request_freelist);
1313 if (request_id == -1) {
1314 /* out of request ids: just fail this one */
Craig Tiller48abdde2016-05-24 06:55:28 -07001315 fail_call(exec_ctx, server, cq_idx, rc,
1316 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001317 return GRPC_CALL_OK;
1318 }
1319 switch (rc->type) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001320 case BATCH_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001321 rm = &server->unregistered_request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001322 break;
1323 case REGISTERED_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001324 rm = &rc->data.registered.registered_method->request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001325 break;
Craig Tillera82950e2015-09-22 12:33:20 -07001326 }
Craig Tiller45724b32015-09-22 10:42:19 -07001327 server->requested_calls[request_id] = *rc;
Craig Tillera82950e2015-09-22 12:33:20 -07001328 gpr_free(rc);
Craig Tillerdb7c3562016-05-19 11:02:52 -07001329 if (gpr_stack_lockfree_push(rm->requests_per_cq[cq_idx], request_id)) {
Craig Tillera82950e2015-09-22 12:33:20 -07001330 /* this was the first queued request: we need to lock and start
1331 matching calls */
1332 gpr_mu_lock(&server->mu_call);
Craig Tillerb9d35962015-09-11 13:31:16 -07001333 while ((calld = rm->pending_head) != NULL) {
Craig Tillerdb7c3562016-05-19 11:02:52 -07001334 request_id = gpr_stack_lockfree_pop(rm->requests_per_cq[cq_idx]);
Craig Tillera82950e2015-09-22 12:33:20 -07001335 if (request_id == -1) break;
Craig Tillerb9d35962015-09-11 13:31:16 -07001336 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -07001337 gpr_mu_unlock(&server->mu_call);
1338 gpr_mu_lock(&calld->mu_state);
1339 if (calld->state == ZOMBIED) {
1340 gpr_mu_unlock(&calld->mu_state);
1341 grpc_closure_init(
1342 &calld->kill_zombie_closure, kill_zombie,
1343 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tiller332f1b32016-05-24 13:21:21 -07001344 grpc_exec_ctx_sched(exec_ctx, &calld->kill_zombie_closure,
1345 GRPC_ERROR_NONE, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001346 } else {
1347 GPR_ASSERT(calld->state == PENDING);
1348 calld->state = ACTIVATED;
1349 gpr_mu_unlock(&calld->mu_state);
Craig Tiller9f9d4222016-05-16 17:02:14 -07001350 publish_call(exec_ctx, server, calld, cq_idx,
Craig Tiller88512692016-04-04 09:32:52 -07001351 &server->requested_calls[request_id]);
Craig Tillera82950e2015-09-22 12:33:20 -07001352 }
1353 gpr_mu_lock(&server->mu_call);
Craig Tiller45724b32015-09-22 10:42:19 -07001354 }
Craig Tillera82950e2015-09-22 12:33:20 -07001355 gpr_mu_unlock(&server->mu_call);
1356 }
Craig Tiller6a006ce2015-07-13 16:25:40 -07001357 return GRPC_CALL_OK;
Craig Tillercce17ac2015-01-20 09:29:28 -08001358}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001359
Craig Tillera82950e2015-09-22 12:33:20 -07001360grpc_call_error grpc_server_request_call(
1361 grpc_server *server, grpc_call **call, grpc_call_details *details,
1362 grpc_metadata_array *initial_metadata,
1363 grpc_completion_queue *cq_bound_to_call,
1364 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001365 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001366 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001367 requested_call *rc = gpr_malloc(sizeof(*rc));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001368 GRPC_API_TRACE(
1369 "grpc_server_request_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001370 "server=%p, call=%p, details=%p, initial_metadata=%p, "
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001371 "cq_bound_to_call=%p, cq_for_notification=%p, tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001372 7, (server, call, details, initial_metadata, cq_bound_to_call,
1373 cq_for_notification, tag));
Craig Tiller9f9d4222016-05-16 17:02:14 -07001374 size_t cq_idx;
1375 for (cq_idx = 0; cq_idx < server->cq_count; cq_idx++) {
1376 if (server->cqs[cq_idx] == cq_for_notification) {
1377 break;
1378 }
1379 }
1380 if (cq_idx == server->cq_count) {
Craig Tillera82950e2015-09-22 12:33:20 -07001381 gpr_free(rc);
1382 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1383 goto done;
1384 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001385 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller9928d392015-08-18 09:40:24 -07001386 details->reserved = NULL;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001387 rc->type = BATCH_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001388 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001389 rc->tag = tag;
1390 rc->cq_bound_to_call = cq_bound_to_call;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001391 rc->call = call;
1392 rc->data.batch.details = details;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001393 rc->initial_metadata = initial_metadata;
Craig Tiller9f9d4222016-05-16 17:02:14 -07001394 error = queue_call_request(&exec_ctx, server, cq_idx, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001395done:
Craig Tillera82950e2015-09-22 12:33:20 -07001396 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001397 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001398}
1399
Craig Tillera82950e2015-09-22 12:33:20 -07001400grpc_call_error grpc_server_request_registered_call(
Craig Tillerb9d35962015-09-11 13:31:16 -07001401 grpc_server *server, void *rmp, grpc_call **call, gpr_timespec *deadline,
Craig Tillera82950e2015-09-22 12:33:20 -07001402 grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload,
1403 grpc_completion_queue *cq_bound_to_call,
1404 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001405 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001406 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001407 requested_call *rc = gpr_malloc(sizeof(*rc));
Craig Tillerb9d35962015-09-11 13:31:16 -07001408 registered_method *rm = rmp;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001409 GRPC_API_TRACE(
1410 "grpc_server_request_registered_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001411 "server=%p, rmp=%p, call=%p, deadline=%p, initial_metadata=%p, "
1412 "optional_payload=%p, cq_bound_to_call=%p, cq_for_notification=%p, "
1413 "tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001414 9, (server, rmp, call, deadline, initial_metadata, optional_payload,
1415 cq_bound_to_call, cq_for_notification, tag));
Craig Tiller9f9d4222016-05-16 17:02:14 -07001416
1417 size_t cq_idx;
1418 for (cq_idx = 0; cq_idx < server->cq_count; cq_idx++) {
1419 if (server->cqs[cq_idx] == cq_for_notification) {
1420 break;
1421 }
1422 }
1423 if (cq_idx == server->cq_count) {
Craig Tillera82950e2015-09-22 12:33:20 -07001424 gpr_free(rc);
1425 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1426 goto done;
1427 }
Craig Tiller06cb1a92016-04-04 08:10:47 -07001428 if ((optional_payload == NULL) !=
1429 (rm->payload_handling == GRPC_SRM_PAYLOAD_NONE)) {
1430 gpr_free(rc);
1431 error = GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH;
1432 goto done;
1433 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001434 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller97fc6a32015-07-08 15:31:35 -07001435 rc->type = REGISTERED_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001436 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001437 rc->tag = tag;
1438 rc->cq_bound_to_call = cq_bound_to_call;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001439 rc->call = call;
Craig Tillerb9d35962015-09-11 13:31:16 -07001440 rc->data.registered.registered_method = rm;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001441 rc->data.registered.deadline = deadline;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001442 rc->initial_metadata = initial_metadata;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001443 rc->data.registered.optional_payload = optional_payload;
Craig Tiller9f9d4222016-05-16 17:02:14 -07001444 error = queue_call_request(&exec_ctx, server, cq_idx, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001445done:
Craig Tillera82950e2015-09-22 12:33:20 -07001446 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001447 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001448}
1449
Craig Tillera82950e2015-09-22 12:33:20 -07001450static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tiller48abdde2016-05-24 06:55:28 -07001451 size_t cq_idx, requested_call *rc, grpc_error *error) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -07001452 *rc->call = NULL;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001453 rc->initial_metadata->count = 0;
Craig Tillercae4b1b2016-05-10 09:11:09 -07001454 GPR_ASSERT(error != GRPC_ERROR_NONE);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001455
Craig Tillera82950e2015-09-22 12:33:20 -07001456 server_ref(server);
Craig Tiller48abdde2016-05-24 06:55:28 -07001457 grpc_cq_end_op(exec_ctx, server->cqs[cq_idx], rc->tag, error,
Craig Tillera82950e2015-09-22 12:33:20 -07001458 done_request_event, rc, &rc->completion);
Craig Tiller24be0f72015-02-10 14:04:22 -08001459}
1460
Craig Tillera82950e2015-09-22 12:33:20 -07001461const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001462 return server->channel_args;
Craig Tiller190d3602015-02-18 09:23:38 -08001463}
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001464
Craig Tillera82950e2015-09-22 12:33:20 -07001465int grpc_server_has_open_connections(grpc_server *server) {
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001466 int r;
Craig Tillera82950e2015-09-22 12:33:20 -07001467 gpr_mu_lock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001468 r = server->root_channel_data.next != &server->root_channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -07001469 gpr_mu_unlock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001470 return r;
1471}