blob: f2613217507c95f0e813e4cdf566bb0a975c5bb0 [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 Tillerf51457b2016-05-03 17:06:32 -0700347 grpc_exec_ctx_push(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 Tillerf51457b2016-05-03 17:06:32 -0700529 grpc_exec_ctx_push(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 Tillerf51457b2016-05-03 17:06:32 -0700574 grpc_exec_ctx_push(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 Tillerbe0d7fe2016-05-12 15:38:59 -0700760 grpc_exec_ctx_push(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 Tillerf51457b2016-05-03 17:06:32 -0700797 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure, GRPC_ERROR_NONE,
798 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 */
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800815 grpc_call *call =
816 grpc_call_create(chand->channel, NULL, 0, NULL, transport_server_data,
817 NULL, 0, gpr_inf_future(GPR_CLOCK_MONOTONIC));
818 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 Tillera82950e2015-09-22 12:33:20 -0700834 if (chand->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE) {
835 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
Craig Tillera82950e2015-09-22 12:33:20 -0700851static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800852 grpc_call_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800853 call_data *calld = elem->call_data;
854 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700855 memset(calld, 0, sizeof(call_data));
856 calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME);
857 calld->call = grpc_call_from_top_element(elem);
858 gpr_mu_init(&calld->mu_state);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800859
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800860 grpc_closure_init(&calld->server_on_recv_initial_metadata,
861 server_on_recv_initial_metadata, elem);
Craig Tiller1e6facb2015-06-11 22:47:11 -0700862
Craig Tillera82950e2015-09-22 12:33:20 -0700863 server_ref(chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800864}
865
Craig Tiller2c8063c2016-03-22 22:12:15 -0700866static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
867 void *ignored) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800868 channel_data *chand = elem->channel_data;
Craig Tillerdb7db992015-01-29 11:19:01 -0800869 call_data *calld = elem->call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800870
Craig Tillera82950e2015-09-22 12:33:20 -0700871 GPR_ASSERT(calld->state != PENDING);
Craig Tiller092d8d12015-07-04 22:35:00 -0700872
Craig Tillera82950e2015-09-22 12:33:20 -0700873 if (calld->host) {
874 GRPC_MDSTR_UNREF(calld->host);
875 }
876 if (calld->path) {
877 GRPC_MDSTR_UNREF(calld->path);
878 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800879 grpc_metadata_array_destroy(&calld->initial_metadata);
Craig Tiller4df31a62015-01-30 09:44:31 -0800880
Craig Tillera82950e2015-09-22 12:33:20 -0700881 gpr_mu_destroy(&calld->mu_state);
Craig Tiller76d2c3b2015-07-07 11:46:01 -0700882
Craig Tillera82950e2015-09-22 12:33:20 -0700883 server_unref(exec_ctx, chand->server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800884}
885
Craig Tillera82950e2015-09-22 12:33:20 -0700886static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800887 grpc_channel_element *elem,
888 grpc_channel_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800889 channel_data *chand = elem->channel_data;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800890 GPR_ASSERT(args->is_first);
891 GPR_ASSERT(!args->is_last);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800892 chand->server = NULL;
893 chand->channel = NULL;
894 chand->next = chand->prev = chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -0800895 chand->registered_methods = NULL;
Craig Tillere039f032015-06-25 12:54:23 -0700896 chand->connectivity_state = GRPC_CHANNEL_IDLE;
Craig Tillera82950e2015-09-22 12:33:20 -0700897 grpc_closure_init(&chand->channel_connectivity_changed,
898 channel_connectivity_changed, chand);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800899}
900
Craig Tillera82950e2015-09-22 12:33:20 -0700901static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
902 grpc_channel_element *elem) {
Craig Tillerec3257c2015-02-12 15:59:43 -0800903 size_t i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800904 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700905 if (chand->registered_methods) {
906 for (i = 0; i < chand->registered_method_slots; i++) {
907 if (chand->registered_methods[i].method) {
908 GRPC_MDSTR_UNREF(chand->registered_methods[i].method);
909 }
910 if (chand->registered_methods[i].host) {
911 GRPC_MDSTR_UNREF(chand->registered_methods[i].host);
912 }
Craig Tillerec3257c2015-02-12 15:59:43 -0800913 }
Craig Tillera82950e2015-09-22 12:33:20 -0700914 gpr_free(chand->registered_methods);
915 }
916 if (chand->server) {
917 gpr_mu_lock(&chand->server->mu_global);
918 chand->next->prev = chand->prev;
919 chand->prev->next = chand->next;
920 chand->next = chand->prev = chand;
921 maybe_finish_shutdown(exec_ctx, chand->server);
922 gpr_mu_unlock(&chand->server->mu_global);
Craig Tillera82950e2015-09-22 12:33:20 -0700923 server_unref(exec_ctx, chand->server);
924 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800925}
926
Craig Tiller178edfa2016-02-17 20:54:46 -0800927const grpc_channel_filter grpc_server_top_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700928 server_start_transport_stream_op,
929 grpc_channel_next_op,
930 sizeof(call_data),
931 init_call_elem,
932 grpc_call_stack_ignore_set_pollset,
933 destroy_call_elem,
934 sizeof(channel_data),
935 init_channel_elem,
936 destroy_channel_elem,
937 grpc_call_next_get_peer,
938 "server",
Craig Tiller9f28ac22015-01-27 17:01:29 -0800939};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800940
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700941static void register_completion_queue(grpc_server *server,
942 grpc_completion_queue *cq,
943 bool is_non_listening, void *reserved) {
Craig Tiller20bc56d2015-02-12 09:02:56 -0800944 size_t i, n;
Craig Tillera82950e2015-09-22 12:33:20 -0700945 GPR_ASSERT(!reserved);
946 for (i = 0; i < server->cq_count; i++) {
947 if (server->cqs[i] == cq) return;
948 }
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700949
Craig Tillera82950e2015-09-22 12:33:20 -0700950 grpc_cq_mark_server_cq(cq);
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700951
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700952 if (is_non_listening) {
953 grpc_cq_mark_non_listening_server_cq(cq);
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700954 }
Craig Tiller509b30e2016-05-21 12:32:39 -0700955
956 GRPC_CQ_INTERNAL_REF(cq, "server");
Craig Tiller20bc56d2015-02-12 09:02:56 -0800957 n = server->cq_count++;
Craig Tillera82950e2015-09-22 12:33:20 -0700958 server->cqs = gpr_realloc(server->cqs,
959 server->cq_count * sizeof(grpc_completion_queue *));
Craig Tiller20bc56d2015-02-12 09:02:56 -0800960 server->cqs[n] = cq;
961}
962
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700963void grpc_server_register_completion_queue(grpc_server *server,
964 grpc_completion_queue *cq,
965 void *reserved) {
966 GRPC_API_TRACE(
967 "grpc_server_register_completion_queue(server=%p, cq=%p, reserved=%p)", 3,
968 (server, cq, reserved));
969 register_completion_queue(server, cq, false, reserved);
970}
971
972void grpc_server_register_non_listening_completion_queue(
973 grpc_server *server, grpc_completion_queue *cq, void *reserved) {
974 GRPC_API_TRACE(
975 "grpc_server_register_non_listening_completion_queue(server=%p, cq=%p, "
976 "reserved=%p)",
977 3, (server, cq, reserved));
978 register_completion_queue(server, cq, true, reserved);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800979}
980
Craig Tiller178edfa2016-02-17 20:54:46 -0800981grpc_server *grpc_server_create(const grpc_channel_args *args, void *reserved) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800982 size_t i;
Craig Tiller178edfa2016-02-17 20:54:46 -0800983
984 GRPC_API_TRACE("grpc_server_create(%p, %p)", 2, (args, reserved));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800985
Craig Tillera82950e2015-09-22 12:33:20 -0700986 grpc_server *server = gpr_malloc(sizeof(grpc_server));
Craig Tiller60fd3612015-03-05 16:24:22 -0800987
Craig Tillera82950e2015-09-22 12:33:20 -0700988 GPR_ASSERT(grpc_is_initialized() && "call grpc_init()");
Craig Tiller60fd3612015-03-05 16:24:22 -0800989
Craig Tillera82950e2015-09-22 12:33:20 -0700990 memset(server, 0, sizeof(grpc_server));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800991
Craig Tillera82950e2015-09-22 12:33:20 -0700992 gpr_mu_init(&server->mu_global);
993 gpr_mu_init(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800994
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800995 /* decremented by grpc_server_destroy */
Craig Tillera82950e2015-09-22 12:33:20 -0700996 gpr_ref_init(&server->internal_refcount, 1);
997 server->root_channel_data.next = server->root_channel_data.prev =
998 &server->root_channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800999
Craig Tiller6a006ce2015-07-13 16:25:40 -07001000 /* TODO(ctiller): expose a channel_arg for this */
1001 server->max_requested_calls = 32768;
Craig Tillera82950e2015-09-22 12:33:20 -07001002 server->request_freelist =
1003 gpr_stack_lockfree_create(server->max_requested_calls);
1004 for (i = 0; i < (size_t)server->max_requested_calls; i++) {
1005 gpr_stack_lockfree_push(server->request_freelist, (int)i);
1006 }
Craig Tillera82950e2015-09-22 12:33:20 -07001007 server->requested_calls = gpr_malloc(server->max_requested_calls *
1008 sizeof(*server->requested_calls));
Craig Tiller729b35a2015-07-13 12:36:47 -07001009
Craig Tillera82950e2015-09-22 12:33:20 -07001010 server->channel_args = grpc_channel_args_copy(args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001011
1012 return server;
1013}
1014
Craig Tillera82950e2015-09-22 12:33:20 -07001015static int streq(const char *a, const char *b) {
1016 if (a == NULL && b == NULL) return 1;
1017 if (a == NULL) return 0;
1018 if (b == NULL) return 0;
1019 return 0 == strcmp(a, b);
Craig Tiller24be0f72015-02-10 14:04:22 -08001020}
1021
Craig Tiller06cb1a92016-04-04 08:10:47 -07001022void *grpc_server_register_method(
1023 grpc_server *server, const char *method, const char *host,
1024 grpc_server_register_method_payload_handling payload_handling,
1025 uint32_t flags) {
Craig Tiller24be0f72015-02-10 14:04:22 -08001026 registered_method *m;
Craig Tillerb2906862016-03-10 06:50:07 -08001027 GRPC_API_TRACE(
1028 "grpc_server_register_method(server=%p, method=%s, host=%s, "
1029 "flags=0x%08x)",
1030 4, (server, method, host, flags));
Craig Tillera82950e2015-09-22 12:33:20 -07001031 if (!method) {
1032 gpr_log(GPR_ERROR,
1033 "grpc_server_register_method method string cannot be NULL");
1034 return NULL;
1035 }
1036 for (m = server->registered_methods; m; m = m->next) {
1037 if (streq(m->method, method) && streq(m->host, host)) {
1038 gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method,
1039 host ? host : "*");
Craig Tiller24be0f72015-02-10 14:04:22 -08001040 return NULL;
1041 }
Craig Tillera82950e2015-09-22 12:33:20 -07001042 }
Craig Tillerb2906862016-03-10 06:50:07 -08001043 if ((flags & ~GRPC_INITIAL_METADATA_USED_MASK) != 0) {
1044 gpr_log(GPR_ERROR, "grpc_server_register_method invalid flags 0x%08x",
1045 flags);
1046 return NULL;
1047 }
Craig Tillera82950e2015-09-22 12:33:20 -07001048 m = gpr_malloc(sizeof(registered_method));
1049 memset(m, 0, sizeof(*m));
Craig Tillera82950e2015-09-22 12:33:20 -07001050 m->method = gpr_strdup(method);
1051 m->host = gpr_strdup(host);
Craig Tiller24be0f72015-02-10 14:04:22 -08001052 m->next = server->registered_methods;
Craig Tiller5ada3d22016-04-04 08:30:59 -07001053 m->payload_handling = payload_handling;
Craig Tillerb2906862016-03-10 06:50:07 -08001054 m->flags = flags;
Craig Tiller24be0f72015-02-10 14:04:22 -08001055 server->registered_methods = m;
1056 return m;
1057}
1058
Craig Tillera82950e2015-09-22 12:33:20 -07001059void grpc_server_start(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001060 listener *l;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001061 size_t i;
Craig Tillerf5768a62015-09-22 10:54:34 -07001062 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller20bc56d2015-02-12 09:02:56 -08001063
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001064 GRPC_API_TRACE("grpc_server_start(server=%p)", 1, (server));
1065
Craig Tiller88ef00e2016-05-17 09:31:49 -07001066 server->started = true;
Craig Tiller509b30e2016-05-21 12:32:39 -07001067 size_t pollset_count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001068 server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count);
1069 for (i = 0; i < server->cq_count; i++) {
Craig Tiller509b30e2016-05-21 12:32:39 -07001070 if (!grpc_cq_is_non_listening_server_cq(server->cqs[i])) {
1071 server->pollsets[pollset_count++] = grpc_cq_pollset(server->cqs[i]);
1072 }
Craig Tillerdb7c3562016-05-19 11:02:52 -07001073 }
1074 request_matcher_init(&server->unregistered_request_matcher,
1075 server->max_requested_calls, server);
1076 for (registered_method *rm = server->registered_methods; rm; rm = rm->next) {
1077 request_matcher_init(&rm->request_matcher, server->max_requested_calls,
1078 server);
Craig Tillera82950e2015-09-22 12:33:20 -07001079 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001080
Craig Tillera82950e2015-09-22 12:33:20 -07001081 for (l = server->listeners; l; l = l->next) {
Craig Tiller509b30e2016-05-21 12:32:39 -07001082 l->start(&exec_ctx, server, l->arg, server->pollsets, pollset_count);
Craig Tillera82950e2015-09-22 12:33:20 -07001083 }
Craig Tillerdfff1b82015-09-21 14:39:57 -07001084
Craig Tillera82950e2015-09-22 12:33:20 -07001085 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001086}
1087
Craig Tillera82950e2015-09-22 12:33:20 -07001088void grpc_server_setup_transport(grpc_exec_ctx *exec_ctx, grpc_server *s,
1089 grpc_transport *transport,
Craig Tiller418a8212016-05-16 16:27:51 -07001090 grpc_pollset *accepting_pollset,
Craig Tillera82950e2015-09-22 12:33:20 -07001091 const grpc_channel_args *args) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001092 size_t num_registered_methods;
1093 size_t alloc;
1094 registered_method *rm;
1095 channel_registered_method *crm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001096 grpc_channel *channel;
1097 channel_data *chand;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001098 grpc_mdstr *host;
1099 grpc_mdstr *method;
Craig Tiller7536af02015-12-22 13:49:30 -08001100 uint32_t hash;
Craig Tillerf96dfc32015-09-10 14:43:18 -07001101 size_t slots;
Craig Tiller7536af02015-12-22 13:49:30 -08001102 uint32_t probes;
1103 uint32_t max_probes = 0;
Craig Tillere039f032015-06-25 12:54:23 -07001104 grpc_transport_op op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001105
Craig Tiller178edfa2016-02-17 20:54:46 -08001106 channel =
1107 grpc_channel_create(exec_ctx, NULL, args, GRPC_SERVER_CHANNEL, transport);
Craig Tillera82950e2015-09-22 12:33:20 -07001108 chand = (channel_data *)grpc_channel_stack_element(
Craig Tillerf40df232016-03-25 13:38:14 -07001109 grpc_channel_get_channel_stack(channel), 0)
1110 ->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001111 chand->server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001112 server_ref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001113 chand->channel = channel;
1114
Craig Tiller9f9d4222016-05-16 17:02:14 -07001115 size_t cq_idx;
1116 grpc_completion_queue *accepting_cq = grpc_cq_from_pollset(accepting_pollset);
1117 for (cq_idx = 0; cq_idx < s->cq_count; cq_idx++) {
1118 if (s->cqs[cq_idx] == accepting_cq) break;
1119 }
1120 if (cq_idx == s->cq_count) {
1121 /* completion queue not found: pick a random one to publish new calls to */
1122 cq_idx = (size_t)rand() % s->cq_count;
1123 }
1124 chand->cq_idx = cq_idx;
1125
Craig Tiller04cc8be2015-02-10 16:11:22 -08001126 num_registered_methods = 0;
Craig Tillera82950e2015-09-22 12:33:20 -07001127 for (rm = s->registered_methods; rm; rm = rm->next) {
1128 num_registered_methods++;
1129 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001130 /* build a lookup table phrased in terms of mdstr's in this channels context
1131 to quickly find registered methods */
Craig Tillera82950e2015-09-22 12:33:20 -07001132 if (num_registered_methods > 0) {
1133 slots = 2 * num_registered_methods;
1134 alloc = sizeof(channel_registered_method) * slots;
1135 chand->registered_methods = gpr_malloc(alloc);
1136 memset(chand->registered_methods, 0, alloc);
1137 for (rm = s->registered_methods; rm; rm = rm->next) {
Craig Tillerb2b42612015-11-20 12:02:17 -08001138 host = rm->host ? grpc_mdstr_from_string(rm->host) : NULL;
1139 method = grpc_mdstr_from_string(rm->method);
Craig Tillera82950e2015-09-22 12:33:20 -07001140 hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash);
1141 for (probes = 0; chand->registered_methods[(hash + probes) % slots]
Craig Tillerf40df232016-03-25 13:38:14 -07001142 .server_registered_method != NULL;
Craig Tillera82950e2015-09-22 12:33:20 -07001143 probes++)
1144 ;
1145 if (probes > max_probes) max_probes = probes;
1146 crm = &chand->registered_methods[(hash + probes) % slots];
1147 crm->server_registered_method = rm;
Craig Tillerb2906862016-03-10 06:50:07 -08001148 crm->flags = rm->flags;
Craig Tillera82950e2015-09-22 12:33:20 -07001149 crm->host = host;
1150 crm->method = method;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001151 }
Craig Tiller7536af02015-12-22 13:49:30 -08001152 GPR_ASSERT(slots <= UINT32_MAX);
1153 chand->registered_method_slots = (uint32_t)slots;
Craig Tillera82950e2015-09-22 12:33:20 -07001154 chand->registered_method_max_probes = max_probes;
1155 }
Craig Tiller04cc8be2015-02-10 16:11:22 -08001156
Craig Tillera82950e2015-09-22 12:33:20 -07001157 gpr_mu_lock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001158 chand->next = &s->root_channel_data;
1159 chand->prev = chand->next->prev;
1160 chand->next->prev = chand->prev->next = chand;
Craig Tillera82950e2015-09-22 12:33:20 -07001161 gpr_mu_unlock(&s->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001162
Craig Tillera82950e2015-09-22 12:33:20 -07001163 GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity");
1164 memset(&op, 0, sizeof(op));
Craig Tillerd7f12e32016-03-03 10:08:31 -08001165 op.set_accept_stream = true;
1166 op.set_accept_stream_fn = accept_stream;
Craig Tiller4b804102015-06-26 16:16:12 -07001167 op.set_accept_stream_user_data = chand;
1168 op.on_connectivity_state_change = &chand->channel_connectivity_changed;
1169 op.connectivity_state = &chand->connectivity_state;
Craig Tiller804ff712016-05-05 16:25:40 -07001170 if (gpr_atm_acq_load(&s->shutdown_flag) != 0) {
1171 op.disconnect_with_error = GRPC_ERROR_CREATE("Server shutdown");
1172 }
Craig Tillera82950e2015-09-22 12:33:20 -07001173 grpc_transport_perform_op(exec_ctx, transport, &op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001174}
1175
Craig Tillera82950e2015-09-22 12:33:20 -07001176void done_published_shutdown(grpc_exec_ctx *exec_ctx, void *done_arg,
1177 grpc_cq_completion *storage) {
1178 (void)done_arg;
1179 gpr_free(storage);
murgatroid9900a3dab2015-08-19 11:15:38 -07001180}
1181
Craig Tillera82950e2015-09-22 12:33:20 -07001182static void listener_destroy_done(grpc_exec_ctx *exec_ctx, void *s,
Craig Tillerf51457b2016-05-03 17:06:32 -07001183 grpc_error *error) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001184 grpc_server *server = s;
Craig Tillera82950e2015-09-22 12:33:20 -07001185 gpr_mu_lock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001186 server->listeners_destroyed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001187 maybe_finish_shutdown(exec_ctx, server);
1188 gpr_mu_unlock(&server->mu_global);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001189}
1190
Craig Tillera82950e2015-09-22 12:33:20 -07001191void grpc_server_shutdown_and_notify(grpc_server *server,
1192 grpc_completion_queue *cq, void *tag) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001193 listener *l;
Craig Tillerbce999f2015-05-27 09:55:51 -07001194 shutdown_tag *sdt;
Craig Tillerff3ae682015-06-29 17:44:04 -07001195 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001196 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001197
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001198 GRPC_API_TRACE("grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", 3,
1199 (server, cq, tag));
1200
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001201 /* lock, and gather up some stuff to do */
Craig Tillera82950e2015-09-22 12:33:20 -07001202 gpr_mu_lock(&server->mu_global);
Craig Tiller4bf29282015-12-14 11:25:48 -08001203 grpc_cq_begin_op(cq, tag);
Craig Tillera82950e2015-09-22 12:33:20 -07001204 if (server->shutdown_published) {
Craig Tillerf51457b2016-05-03 17:06:32 -07001205 grpc_cq_end_op(&exec_ctx, cq, tag, GRPC_ERROR_NONE, done_published_shutdown,
1206 NULL, gpr_malloc(sizeof(grpc_cq_completion)));
Craig Tillera82950e2015-09-22 12:33:20 -07001207 gpr_mu_unlock(&server->mu_global);
1208 goto done;
1209 }
1210 server->shutdown_tags =
1211 gpr_realloc(server->shutdown_tags,
1212 sizeof(shutdown_tag) * (server->num_shutdown_tags + 1));
Craig Tillerbce999f2015-05-27 09:55:51 -07001213 sdt = &server->shutdown_tags[server->num_shutdown_tags++];
1214 sdt->tag = tag;
1215 sdt->cq = cq;
Craig Tillera82950e2015-09-22 12:33:20 -07001216 if (gpr_atm_acq_load(&server->shutdown_flag)) {
1217 gpr_mu_unlock(&server->mu_global);
1218 goto done;
1219 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001220
Craig Tillera82950e2015-09-22 12:33:20 -07001221 server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME);
Craig Tillerab54f792015-07-08 08:34:20 -07001222
Craig Tillera82950e2015-09-22 12:33:20 -07001223 channel_broadcaster_init(server, &broadcaster);
nnoble0c475f02014-12-05 15:37:39 -08001224
Craig Tillerfc193e12015-09-24 15:29:03 -07001225 gpr_atm_rel_store(&server->shutdown_flag, 1);
1226
Craig Tillerbd217572015-02-11 18:10:56 -08001227 /* collect all unregistered then registered calls */
Craig Tillera82950e2015-09-22 12:33:20 -07001228 gpr_mu_lock(&server->mu_call);
Craig Tillercae4b1b2016-05-10 09:11:09 -07001229 kill_pending_work_locked(&exec_ctx, server,
1230 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001231 gpr_mu_unlock(&server->mu_call);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001232
Craig Tillera82950e2015-09-22 12:33:20 -07001233 maybe_finish_shutdown(&exec_ctx, server);
1234 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001235
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001236 /* Shutdown listeners */
Craig Tillera82950e2015-09-22 12:33:20 -07001237 for (l = server->listeners; l; l = l->next) {
1238 grpc_closure_init(&l->destroy_done, listener_destroy_done, server);
1239 l->destroy(&exec_ctx, server, l->arg, &l->destroy_done);
1240 }
Craig Tillerff3ae682015-06-29 17:44:04 -07001241
Craig Tillera82950e2015-09-22 12:33:20 -07001242 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 1, 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001243
Craig Tillerdfff1b82015-09-21 14:39:57 -07001244done:
Craig Tillera82950e2015-09-22 12:33:20 -07001245 grpc_exec_ctx_finish(&exec_ctx);
Craig Tilleraec96aa2015-04-07 14:32:15 -07001246}
1247
Craig Tillera82950e2015-09-22 12:33:20 -07001248void grpc_server_cancel_all_calls(grpc_server *server) {
Craig Tiller092d8d12015-07-04 22:35:00 -07001249 channel_broadcaster broadcaster;
Craig Tillerf5768a62015-09-22 10:54:34 -07001250 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerafa2d632015-05-26 16:39:13 -07001251
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001252 GRPC_API_TRACE("grpc_server_cancel_all_calls(server=%p)", 1, (server));
1253
Craig Tillera82950e2015-09-22 12:33:20 -07001254 gpr_mu_lock(&server->mu_global);
1255 channel_broadcaster_init(server, &broadcaster);
1256 gpr_mu_unlock(&server->mu_global);
Craig Tillerafa2d632015-05-26 16:39:13 -07001257
Craig Tiller804ff712016-05-05 16:25:40 -07001258 channel_broadcaster_shutdown(&exec_ctx, &broadcaster, 0,
1259 GRPC_ERROR_CREATE("Cancelling all calls"));
Craig Tillera82950e2015-09-22 12:33:20 -07001260 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerafa2d632015-05-26 16:39:13 -07001261}
1262
Craig Tillera82950e2015-09-22 12:33:20 -07001263void grpc_server_destroy(grpc_server *server) {
Craig Tilleraec96aa2015-04-07 14:32:15 -07001264 listener *l;
Craig Tillerf5768a62015-09-22 10:54:34 -07001265 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller872af022015-04-24 15:57:52 -07001266
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001267 GRPC_API_TRACE("grpc_server_destroy(server=%p)", 1, (server));
1268
Craig Tillera82950e2015-09-22 12:33:20 -07001269 gpr_mu_lock(&server->mu_global);
1270 GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners);
1271 GPR_ASSERT(server->listeners_destroyed == num_listeners(server));
Craig Tilleraec96aa2015-04-07 14:32:15 -07001272
Craig Tillera82950e2015-09-22 12:33:20 -07001273 while (server->listeners) {
1274 l = server->listeners;
1275 server->listeners = l->next;
1276 gpr_free(l);
1277 }
Craig Tilleraec96aa2015-04-07 14:32:15 -07001278
Craig Tillera82950e2015-09-22 12:33:20 -07001279 gpr_mu_unlock(&server->mu_global);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001280
Craig Tillera82950e2015-09-22 12:33:20 -07001281 server_unref(&exec_ctx, server);
1282 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001283}
1284
Craig Tillera82950e2015-09-22 12:33:20 -07001285void grpc_server_add_listener(
1286 grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1287 void (*start)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1288 grpc_pollset **pollsets, size_t pollset_count),
1289 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_server *server, void *arg,
1290 grpc_closure *on_done)) {
1291 listener *l = gpr_malloc(sizeof(listener));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001292 l->arg = arg;
1293 l->start = start;
1294 l->destroy = destroy;
1295 l->next = server->listeners;
1296 server->listeners = l;
1297}
1298
Craig Tillera82950e2015-09-22 12:33:20 -07001299static grpc_call_error queue_call_request(grpc_exec_ctx *exec_ctx,
Craig Tiller9f9d4222016-05-16 17:02:14 -07001300 grpc_server *server, size_t cq_idx,
Craig Tillera82950e2015-09-22 12:33:20 -07001301 requested_call *rc) {
Yang Gaoeb8e7cd2015-02-11 11:43:40 -08001302 call_data *calld = NULL;
Craig Tillerb9d35962015-09-11 13:31:16 -07001303 request_matcher *rm = NULL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001304 int request_id;
Craig Tillera82950e2015-09-22 12:33:20 -07001305 if (gpr_atm_acq_load(&server->shutdown_flag)) {
Craig Tiller48abdde2016-05-24 06:55:28 -07001306 fail_call(exec_ctx, server, cq_idx, rc,
1307 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001308 return GRPC_CALL_OK;
1309 }
1310 request_id = gpr_stack_lockfree_pop(server->request_freelist);
1311 if (request_id == -1) {
1312 /* out of request ids: just fail this one */
Craig Tiller48abdde2016-05-24 06:55:28 -07001313 fail_call(exec_ctx, server, cq_idx, rc,
1314 GRPC_ERROR_CREATE("Server Shutdown"));
Craig Tillera82950e2015-09-22 12:33:20 -07001315 return GRPC_CALL_OK;
1316 }
1317 switch (rc->type) {
Craig Tiller04cc8be2015-02-10 16:11:22 -08001318 case BATCH_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001319 rm = &server->unregistered_request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001320 break;
1321 case REGISTERED_CALL:
Craig Tillerb9d35962015-09-11 13:31:16 -07001322 rm = &rc->data.registered.registered_method->request_matcher;
Craig Tiller04cc8be2015-02-10 16:11:22 -08001323 break;
Craig Tillera82950e2015-09-22 12:33:20 -07001324 }
Craig Tiller45724b32015-09-22 10:42:19 -07001325 server->requested_calls[request_id] = *rc;
Craig Tillera82950e2015-09-22 12:33:20 -07001326 gpr_free(rc);
Craig Tillerdb7c3562016-05-19 11:02:52 -07001327 if (gpr_stack_lockfree_push(rm->requests_per_cq[cq_idx], request_id)) {
Craig Tillera82950e2015-09-22 12:33:20 -07001328 /* this was the first queued request: we need to lock and start
1329 matching calls */
1330 gpr_mu_lock(&server->mu_call);
Craig Tillerb9d35962015-09-11 13:31:16 -07001331 while ((calld = rm->pending_head) != NULL) {
Craig Tillerdb7c3562016-05-19 11:02:52 -07001332 request_id = gpr_stack_lockfree_pop(rm->requests_per_cq[cq_idx]);
Craig Tillera82950e2015-09-22 12:33:20 -07001333 if (request_id == -1) break;
Craig Tillerb9d35962015-09-11 13:31:16 -07001334 rm->pending_head = calld->pending_next;
Craig Tillera82950e2015-09-22 12:33:20 -07001335 gpr_mu_unlock(&server->mu_call);
1336 gpr_mu_lock(&calld->mu_state);
1337 if (calld->state == ZOMBIED) {
1338 gpr_mu_unlock(&calld->mu_state);
1339 grpc_closure_init(
1340 &calld->kill_zombie_closure, kill_zombie,
1341 grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0));
Craig Tillerf51457b2016-05-03 17:06:32 -07001342 grpc_exec_ctx_push(exec_ctx, &calld->kill_zombie_closure,
1343 GRPC_ERROR_NONE, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001344 } else {
1345 GPR_ASSERT(calld->state == PENDING);
1346 calld->state = ACTIVATED;
1347 gpr_mu_unlock(&calld->mu_state);
Craig Tiller9f9d4222016-05-16 17:02:14 -07001348 publish_call(exec_ctx, server, calld, cq_idx,
Craig Tiller88512692016-04-04 09:32:52 -07001349 &server->requested_calls[request_id]);
Craig Tillera82950e2015-09-22 12:33:20 -07001350 }
1351 gpr_mu_lock(&server->mu_call);
Craig Tiller45724b32015-09-22 10:42:19 -07001352 }
Craig Tillera82950e2015-09-22 12:33:20 -07001353 gpr_mu_unlock(&server->mu_call);
1354 }
Craig Tiller6a006ce2015-07-13 16:25:40 -07001355 return GRPC_CALL_OK;
Craig Tillercce17ac2015-01-20 09:29:28 -08001356}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001357
Craig Tillera82950e2015-09-22 12:33:20 -07001358grpc_call_error grpc_server_request_call(
1359 grpc_server *server, grpc_call **call, grpc_call_details *details,
1360 grpc_metadata_array *initial_metadata,
1361 grpc_completion_queue *cq_bound_to_call,
1362 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001363 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001364 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001365 requested_call *rc = gpr_malloc(sizeof(*rc));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001366 GRPC_API_TRACE(
1367 "grpc_server_request_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001368 "server=%p, call=%p, details=%p, initial_metadata=%p, "
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001369 "cq_bound_to_call=%p, cq_for_notification=%p, tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001370 7, (server, call, details, initial_metadata, cq_bound_to_call,
1371 cq_for_notification, tag));
Craig Tiller9f9d4222016-05-16 17:02:14 -07001372 size_t cq_idx;
1373 for (cq_idx = 0; cq_idx < server->cq_count; cq_idx++) {
1374 if (server->cqs[cq_idx] == cq_for_notification) {
1375 break;
1376 }
1377 }
1378 if (cq_idx == server->cq_count) {
Craig Tillera82950e2015-09-22 12:33:20 -07001379 gpr_free(rc);
1380 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1381 goto done;
1382 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001383 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller9928d392015-08-18 09:40:24 -07001384 details->reserved = NULL;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001385 rc->type = BATCH_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001386 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001387 rc->tag = tag;
1388 rc->cq_bound_to_call = cq_bound_to_call;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001389 rc->call = call;
1390 rc->data.batch.details = details;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001391 rc->initial_metadata = initial_metadata;
Craig Tiller9f9d4222016-05-16 17:02:14 -07001392 error = queue_call_request(&exec_ctx, server, cq_idx, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001393done:
Craig Tillera82950e2015-09-22 12:33:20 -07001394 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001395 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001396}
1397
Craig Tillera82950e2015-09-22 12:33:20 -07001398grpc_call_error grpc_server_request_registered_call(
Craig Tillerb9d35962015-09-11 13:31:16 -07001399 grpc_server *server, void *rmp, grpc_call **call, gpr_timespec *deadline,
Craig Tillera82950e2015-09-22 12:33:20 -07001400 grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload,
1401 grpc_completion_queue *cq_bound_to_call,
1402 grpc_completion_queue *cq_for_notification, void *tag) {
Craig Tillerdfff1b82015-09-21 14:39:57 -07001403 grpc_call_error error;
Craig Tillerf5768a62015-09-22 10:54:34 -07001404 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -07001405 requested_call *rc = gpr_malloc(sizeof(*rc));
Craig Tillerb9d35962015-09-11 13:31:16 -07001406 registered_method *rm = rmp;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001407 GRPC_API_TRACE(
1408 "grpc_server_request_registered_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -07001409 "server=%p, rmp=%p, call=%p, deadline=%p, initial_metadata=%p, "
1410 "optional_payload=%p, cq_bound_to_call=%p, cq_for_notification=%p, "
1411 "tag=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001412 9, (server, rmp, call, deadline, initial_metadata, optional_payload,
1413 cq_bound_to_call, cq_for_notification, tag));
Craig Tiller9f9d4222016-05-16 17:02:14 -07001414
1415 size_t cq_idx;
1416 for (cq_idx = 0; cq_idx < server->cq_count; cq_idx++) {
1417 if (server->cqs[cq_idx] == cq_for_notification) {
1418 break;
1419 }
1420 }
1421 if (cq_idx == server->cq_count) {
Craig Tillera82950e2015-09-22 12:33:20 -07001422 gpr_free(rc);
1423 error = GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
1424 goto done;
1425 }
Craig Tiller06cb1a92016-04-04 08:10:47 -07001426 if ((optional_payload == NULL) !=
1427 (rm->payload_handling == GRPC_SRM_PAYLOAD_NONE)) {
1428 gpr_free(rc);
1429 error = GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH;
1430 goto done;
1431 }
Craig Tiller4bf29282015-12-14 11:25:48 -08001432 grpc_cq_begin_op(cq_for_notification, tag);
Craig Tiller97fc6a32015-07-08 15:31:35 -07001433 rc->type = REGISTERED_CALL;
Craig Tiller6a006ce2015-07-13 16:25:40 -07001434 rc->server = server;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001435 rc->tag = tag;
1436 rc->cq_bound_to_call = cq_bound_to_call;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001437 rc->call = call;
Craig Tillerb9d35962015-09-11 13:31:16 -07001438 rc->data.registered.registered_method = rm;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001439 rc->data.registered.deadline = deadline;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001440 rc->initial_metadata = initial_metadata;
Craig Tiller97fc6a32015-07-08 15:31:35 -07001441 rc->data.registered.optional_payload = optional_payload;
Craig Tiller9f9d4222016-05-16 17:02:14 -07001442 error = queue_call_request(&exec_ctx, server, cq_idx, rc);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001443done:
Craig Tillera82950e2015-09-22 12:33:20 -07001444 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerdfff1b82015-09-21 14:39:57 -07001445 return error;
Craig Tiller24be0f72015-02-10 14:04:22 -08001446}
1447
Craig Tillera82950e2015-09-22 12:33:20 -07001448static void fail_call(grpc_exec_ctx *exec_ctx, grpc_server *server,
Craig Tiller48abdde2016-05-24 06:55:28 -07001449 size_t cq_idx, requested_call *rc, grpc_error *error) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -07001450 *rc->call = NULL;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001451 rc->initial_metadata->count = 0;
Craig Tillercae4b1b2016-05-10 09:11:09 -07001452 GPR_ASSERT(error != GRPC_ERROR_NONE);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001453
Craig Tillera82950e2015-09-22 12:33:20 -07001454 server_ref(server);
Craig Tiller48abdde2016-05-24 06:55:28 -07001455 grpc_cq_end_op(exec_ctx, server->cqs[cq_idx], rc->tag, error,
Craig Tillera82950e2015-09-22 12:33:20 -07001456 done_request_event, rc, &rc->completion);
Craig Tiller24be0f72015-02-10 14:04:22 -08001457}
1458
Craig Tillera82950e2015-09-22 12:33:20 -07001459const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001460 return server->channel_args;
Craig Tiller190d3602015-02-18 09:23:38 -08001461}
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001462
Craig Tillera82950e2015-09-22 12:33:20 -07001463int grpc_server_has_open_connections(grpc_server *server) {
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001464 int r;
Craig Tillera82950e2015-09-22 12:33:20 -07001465 gpr_mu_lock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001466 r = server->root_channel_data.next != &server->root_channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -07001467 gpr_mu_unlock(&server->mu_global);
Craig Tillerba3c3cd2015-05-26 06:28:10 -07001468 return r;
1469}