blob: 119f5e82ab2fae7846f30b9f6552f1896735fa30 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070033#include <assert.h>
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080034#include <limits.h>
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070035#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38
39#include <grpc/compression.h>
murgatroid99c3910ca2016-01-06 13:14:23 -080040#include <grpc/grpc.h>
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070041#include <grpc/support/alloc.h>
42#include <grpc/support/log.h>
David Garcia Quintas73dcbda2016-04-23 00:17:05 -070043#include <grpc/support/slice.h>
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070044#include <grpc/support/string_util.h>
David Garcia Quintase091af82015-07-15 21:37:02 -070045#include <grpc/support/useful.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
Craig Tiller9533d042016-03-25 17:11:06 -070047#include "src/core/lib/channel/channel_stack.h"
48#include "src/core/lib/compression/algorithm_metadata.h"
49#include "src/core/lib/iomgr/timer.h"
50#include "src/core/lib/profiling/timers.h"
51#include "src/core/lib/support/string.h"
52#include "src/core/lib/surface/api_trace.h"
53#include "src/core/lib/surface/call.h"
54#include "src/core/lib/surface/channel.h"
55#include "src/core/lib/surface/completion_queue.h"
David Garcia Quintas73dcbda2016-04-23 00:17:05 -070056#include "src/core/lib/transport/metadata.h"
Craig Tiller9533d042016-03-25 17:11:06 -070057#include "src/core/lib/transport/static_metadata.h"
David Garcia Quintas73dcbda2016-04-23 00:17:05 -070058#include "src/core/lib/transport/transport.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080060/** The maximum number of concurrent batches possible.
Craig Tiller1b011672015-07-10 10:41:44 -070061 Based upon the maximum number of individually queueable ops in the batch
62 api:
63 - initial metadata send
64 - message send
65 - status/close send (depending on client/server)
66 - initial metadata recv
67 - message recv
68 - status/close recv (depending on client/server) */
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080069#define MAX_CONCURRENT_BATCHES 6
Craig Tiller1b011672015-07-10 10:41:44 -070070
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080071#define MAX_SEND_EXTRA_METADATA_COUNT 3
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072
Craig Tillerdaceea82015-02-02 16:15:53 -080073/* Status data for a request can come from several sources; this
74 enumerates them all, and acts as a priority sorting for which
75 status to return to the application - earlier entries override
76 later ones */
Craig Tillera82950e2015-09-22 12:33:20 -070077typedef enum {
Craig Tillerdaceea82015-02-02 16:15:53 -080078 /* Status came from the application layer overriding whatever
79 the wire says */
Craig Tiller68752722015-01-29 14:59:54 -080080 STATUS_FROM_API_OVERRIDE = 0,
Craig Tillerdaceea82015-02-02 16:15:53 -080081 /* Status came from 'the wire' - or somewhere below the surface
82 layer */
Craig Tiller68752722015-01-29 14:59:54 -080083 STATUS_FROM_WIRE,
Craig Tiller2aa03df2016-03-16 08:24:55 -070084 /* Status was created by some internal channel stack operation */
85 STATUS_FROM_CORE,
Craig Tilleraea081f2015-06-11 14:19:33 -070086 /* Status came from the server sending status */
87 STATUS_FROM_SERVER_STATUS,
Craig Tiller68752722015-01-29 14:59:54 -080088 STATUS_SOURCE_COUNT
89} status_source;
90
Craig Tillera82950e2015-09-22 12:33:20 -070091typedef struct {
Craig Tiller7536af02015-12-22 13:49:30 -080092 uint8_t is_set;
Craig Tiller68752722015-01-29 14:59:54 -080093 grpc_status_code code;
94 grpc_mdstr *details;
95} received_status;
96
Craig Tillerc7e1a2a2015-11-02 14:17:32 -080097typedef struct batch_control {
98 grpc_call *call;
99 grpc_cq_completion cq_completion;
100 grpc_closure finish_batch;
101 void *notify_tag;
102 gpr_refcount steps_to_complete;
Craig Tillerc027e772016-05-03 16:27:00 -0700103 grpc_error *error;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800104
Craig Tiller7536af02015-12-22 13:49:30 -0800105 uint8_t send_initial_metadata;
106 uint8_t send_message;
107 uint8_t send_final_op;
108 uint8_t recv_initial_metadata;
109 uint8_t recv_message;
110 uint8_t recv_final_op;
111 uint8_t is_notify_tag_closure;
Craig Tiller6e7b45e2016-07-08 17:25:49 -0700112
113 /* TODO(ctiller): now that this is inlined, figure out how much of the above
114 state can be eliminated */
115 grpc_transport_stream_op op;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800116} batch_control;
117
Craig Tillera82950e2015-09-22 12:33:20 -0700118struct grpc_call {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119 grpc_completion_queue *cq;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700120 grpc_polling_entity pollent;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121 grpc_channel *channel;
Craig Tiller3e7c6a72015-07-31 16:17:04 -0700122 grpc_call *parent;
Craig Tillerc7df0df2015-08-03 08:06:50 -0700123 grpc_call *first_child;
Craig Tillercce17ac2015-01-20 09:29:28 -0800124 /* TODO(ctiller): share with cq if possible? */
125 gpr_mu mu;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126
Craig Tillere5d683c2015-02-03 16:37:36 -0800127 /* client or server call */
Craig Tiller1cbf5762016-04-22 16:02:55 -0700128 bool is_client;
Craig Tillere5d683c2015-02-03 16:37:36 -0800129 /* is the alarm set */
Craig Tiller1cbf5762016-04-22 16:02:55 -0700130 bool have_alarm;
Craig Tillerf3fba742015-06-11 09:36:33 -0700131 /** has grpc_call_destroy been called */
Craig Tiller1cbf5762016-04-22 16:02:55 -0700132 bool destroy_called;
Craig Tillerc7df0df2015-08-03 08:06:50 -0700133 /** flag indicating that cancellation is inherited */
Craig Tiller1cbf5762016-04-22 16:02:55 -0700134 bool cancellation_is_inherited;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800135 /** bitmask of live batches */
Craig Tiller7536af02015-12-22 13:49:30 -0800136 uint8_t used_batches;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800137 /** which ops are in-flight */
Craig Tiller1cbf5762016-04-22 16:02:55 -0700138 bool sent_initial_metadata;
139 bool sending_message;
140 bool sent_final_op;
141 bool received_initial_metadata;
142 bool receiving_message;
143 bool requested_final_op;
144 bool received_final_op;
yang-g0b6ad7d2015-06-25 14:39:01 -0700145
Craig Tillera44cbfc2016-02-03 16:02:49 -0800146 /* have we received initial metadata */
147 bool has_initial_md_been_received;
148
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800149 batch_control active_batches[MAX_CONCURRENT_BATCHES];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800150
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800151 /* first idx: is_receiving, second idx: is_trailing */
152 grpc_metadata_batch metadata_batch[2][2];
Craig Tillerebf94bf2015-02-05 08:48:46 -0800153
Craig Tillere5d683c2015-02-03 16:37:36 -0800154 /* Buffered read metadata waiting to be returned to the application.
155 Element 0 is initial metadata, element 1 is trailing metadata. */
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800156 grpc_metadata_array *buffered_metadata[2];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800157
Craig Tillere5d683c2015-02-03 16:37:36 -0800158 /* Received call statuses from various sources */
Craig Tiller68752722015-01-29 14:59:54 -0800159 received_status status[STATUS_SOURCE_COUNT];
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160
David Garcia Quintas01c4d992016-07-07 20:11:27 -0700161 /* Call data useful used for reporting. Only valid after the call has
162 * completed */
163 grpc_call_final_info final_info;
Craig Tiller466129e2016-03-09 14:43:18 -0800164
David Garcia Quintas749367f2016-05-17 19:15:24 -0700165 /* Compression algorithm for *incoming* data */
166 grpc_compression_algorithm incoming_compression_algorithm;
David Garcia Quintase091af82015-07-15 21:37:02 -0700167 /* Supported encodings (compression algorithms), a bitset */
Craig Tiller7536af02015-12-22 13:49:30 -0800168 uint32_t encodings_accepted_by_peer;
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700169
Julien Boeufc6f8d0a2015-05-11 22:40:02 -0700170 /* Contexts for various subsystems (security, tracing, ...). */
Julien Boeuf83b02972015-05-20 22:50:34 -0700171 grpc_call_context_element context[GRPC_CONTEXT_COUNT];
Craig Tiller935cf422015-05-01 14:10:46 -0700172
Craig Tillere5d683c2015-02-03 16:37:36 -0800173 /* Deadline alarm - if have_alarm is non-zero */
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700174 grpc_timer alarm;
Craig Tillercce17ac2015-01-20 09:29:28 -0800175
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800176 /* for the client, extra metadata is initial metadata; for the
177 server, it's trailing metadata */
178 grpc_linked_mdelem send_extra_metadata[MAX_SEND_EXTRA_METADATA_COUNT];
179 int send_extra_metadata_count;
Craig Tiller6902ad22015-04-16 08:01:49 -0700180 gpr_timespec send_deadline;
181
Craig Tillerd6c98df2015-08-18 09:33:44 -0700182 /** siblings: children of the same parent form a list, and this list is
183 protected under
Craig Tillerc7df0df2015-08-03 08:06:50 -0700184 parent->mu */
185 grpc_call *sibling_next;
186 grpc_call *sibling_prev;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800187
188 grpc_slice_buffer_stream sending_stream;
189 grpc_byte_stream *receiving_stream;
190 grpc_byte_buffer **receiving_buffer;
191 gpr_slice receiving_slice;
192 grpc_closure receiving_slice_ready;
193 grpc_closure receiving_stream_ready;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800194 grpc_closure receiving_initial_metadata_ready;
Craig Tiller7536af02015-12-22 13:49:30 -0800195 uint32_t test_only_last_message_flags;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800196
197 union {
198 struct {
199 grpc_status_code *status;
200 char **status_details;
201 size_t *status_details_capacity;
202 } client;
203 struct {
204 int *cancelled;
205 } server;
206 } final_op;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800207
Craig Tiller8a677802016-04-22 15:07:53 -0700208 void *saved_receiving_stream_ready_bctlp;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209};
210
Craig Tiller87d5b192015-04-16 14:37:57 -0700211#define CALL_STACK_FROM_CALL(call) ((grpc_call_stack *)((call) + 1))
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800212#define CALL_FROM_CALL_STACK(call_stack) (((grpc_call *)(call_stack)) - 1)
213#define CALL_ELEM_FROM_CALL(call, idx) \
214 grpc_call_stack_element(CALL_STACK_FROM_CALL(call), idx)
215#define CALL_FROM_TOP_ELEM(top_elem) \
216 CALL_FROM_CALL_STACK(grpc_call_stack_from_top_element(top_elem))
217
Craig Tillera82950e2015-09-22 12:33:20 -0700218static void set_deadline_alarm(grpc_exec_ctx *exec_ctx, grpc_call *call,
219 gpr_timespec deadline);
Craig Tillera82950e2015-09-22 12:33:20 -0700220static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call,
221 grpc_transport_stream_op *op);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800222static grpc_call_error cancel_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c,
223 grpc_status_code status,
Craig Tillera82950e2015-09-22 12:33:20 -0700224 const char *description);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700225static grpc_call_error close_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c,
226 grpc_status_code status,
227 const char *description);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800228static void destroy_call(grpc_exec_ctx *exec_ctx, void *call_stack,
Craig Tillerc027e772016-05-03 16:27:00 -0700229 grpc_error *error);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800230static void receiving_slice_ready(grpc_exec_ctx *exec_ctx, void *bctlp,
Craig Tillerc027e772016-05-03 16:27:00 -0700231 grpc_error *error);
Craig Tillerbac41422015-05-29 16:32:28 -0700232
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700233grpc_call *grpc_call_create(
234 grpc_channel *channel, grpc_call *parent_call, uint32_t propagation_mask,
David Garcia Quintas879b3b92016-04-25 11:23:38 -0700235 grpc_completion_queue *cq, grpc_pollset_set *pollset_set_alternative,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700236 const void *server_transport_data, grpc_mdelem **add_initial_metadata,
237 size_t add_initial_metadata_count, gpr_timespec send_deadline) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800238 size_t i, j;
Craig Tillera82950e2015-09-22 12:33:20 -0700239 grpc_channel_stack *channel_stack = grpc_channel_get_channel_stack(channel);
Craig Tillerf5768a62015-09-22 10:54:34 -0700240 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller1f41b6b2015-10-09 15:07:02 -0700241 grpc_call *call;
Craig Tiller0ba432d2015-10-09 16:57:11 -0700242 GPR_TIMER_BEGIN("grpc_call_create", 0);
Craig Tiller1f41b6b2015-10-09 15:07:02 -0700243 call = gpr_malloc(sizeof(grpc_call) + channel_stack->call_stack_size);
Craig Tillera82950e2015-09-22 12:33:20 -0700244 memset(call, 0, sizeof(grpc_call));
245 gpr_mu_init(&call->mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800246 call->channel = channel;
Craig Tillerfb189f82015-02-03 12:07:07 -0800247 call->cq = cq;
Craig Tiller3e7c6a72015-07-31 16:17:04 -0700248 call->parent = parent_call;
David Garcia Quintas46123372016-05-09 15:28:42 -0700249 /* Always support no compression */
250 GPR_BITSET(&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE);
Craig Tillercce17ac2015-01-20 09:29:28 -0800251 call->is_client = server_transport_data == NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700252 if (call->is_client) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800253 GPR_ASSERT(add_initial_metadata_count < MAX_SEND_EXTRA_METADATA_COUNT);
254 for (i = 0; i < add_initial_metadata_count; i++) {
255 call->send_extra_metadata[i].md = add_initial_metadata[i];
256 }
257 call->send_extra_metadata_count = (int)add_initial_metadata_count;
258 } else {
259 GPR_ASSERT(add_initial_metadata_count == 0);
260 call->send_extra_metadata_count = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700261 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800262 for (i = 0; i < 2; i++) {
263 for (j = 0; j < 2; j++) {
264 call->metadata_batch[i][j].deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
265 }
Craig Tillera82950e2015-09-22 12:33:20 -0700266 }
Craig Tiller33ab1822016-07-08 15:19:06 -0700267 call->send_deadline =
268 gpr_convert_clock_type(send_deadline, GPR_CLOCK_MONOTONIC);
Craig Tillera82950e2015-09-22 12:33:20 -0700269 GRPC_CHANNEL_INTERNAL_REF(channel, "call");
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800270 /* initial refcount dropped by grpc_call_destroy */
Mark D. Roth76d24422016-06-23 13:22:10 -0700271 grpc_error *error = grpc_call_stack_init(
272 &exec_ctx, channel_stack, 1, destroy_call, call, call->context,
273 server_transport_data, CALL_STACK_FROM_CALL(call));
Mark D. Roth9f97cca2016-06-23 10:47:05 -0700274 if (error != GRPC_ERROR_NONE) {
275 intptr_t status;
276 if (!grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, &status))
277 status = GRPC_STATUS_UNKNOWN;
Mark D. Roth76d24422016-06-23 13:22:10 -0700278 const char *error_str =
279 grpc_error_get_str(error, GRPC_ERROR_STR_DESCRIPTION);
Mark D. Rothacfb3432016-06-29 13:02:56 -0700280 close_with_status(&exec_ctx, call, (grpc_status_code)status,
Mark D. Roth76d24422016-06-23 13:22:10 -0700281 error_str == NULL ? "unknown error" : error_str);
Mark D. Roth05d73af2016-07-27 15:52:46 +0000282 GRPC_ERROR_UNREF(error);
Mark D. Roth9f97cca2016-06-23 10:47:05 -0700283 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800284 if (cq != NULL) {
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700285 GPR_ASSERT(
286 pollset_set_alternative == NULL &&
287 "Only one of 'cq' and 'pollset_set_alternative' should be non-NULL.");
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800288 GRPC_CQ_INTERNAL_REF(cq, "bind");
David Garcia Quintas69ff63d2016-06-06 16:39:47 -0700289 call->pollent =
290 grpc_polling_entity_create_from_pollset(grpc_cq_pollset(cq));
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700291 }
David Garcia Quintas879b3b92016-04-25 11:23:38 -0700292 if (pollset_set_alternative != NULL) {
David Garcia Quintas69ff63d2016-06-06 16:39:47 -0700293 call->pollent =
294 grpc_polling_entity_create_from_pollset_set(pollset_set_alternative);
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700295 }
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700296 if (!grpc_polling_entity_is_empty(&call->pollent)) {
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700297 grpc_call_stack_set_pollset_or_pollset_set(
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700298 &exec_ctx, CALL_STACK_FROM_CALL(call), &call->pollent);
Craig Tillera82950e2015-09-22 12:33:20 -0700299 }
Craig Tillera82950e2015-09-22 12:33:20 -0700300 if (parent_call != NULL) {
301 GRPC_CALL_INTERNAL_REF(parent_call, "child");
302 GPR_ASSERT(call->is_client);
303 GPR_ASSERT(!parent_call->is_client);
304
305 gpr_mu_lock(&parent_call->mu);
306
307 if (propagation_mask & GRPC_PROPAGATE_DEADLINE) {
308 send_deadline = gpr_time_min(
309 gpr_convert_clock_type(send_deadline,
310 parent_call->send_deadline.clock_type),
311 parent_call->send_deadline);
Craig Tillerc7df0df2015-08-03 08:06:50 -0700312 }
Craig Tillera82950e2015-09-22 12:33:20 -0700313 /* for now GRPC_PROPAGATE_TRACING_CONTEXT *MUST* be passed with
314 * GRPC_PROPAGATE_STATS_CONTEXT */
315 /* TODO(ctiller): This should change to use the appropriate census start_op
316 * call. */
317 if (propagation_mask & GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT) {
318 GPR_ASSERT(propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT);
319 grpc_call_context_set(call, GRPC_CONTEXT_TRACING,
320 parent_call->context[GRPC_CONTEXT_TRACING].value,
321 NULL);
322 } else {
323 GPR_ASSERT(propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT);
Craig Tiller45724b32015-09-22 10:42:19 -0700324 }
Craig Tillera82950e2015-09-22 12:33:20 -0700325 if (propagation_mask & GRPC_PROPAGATE_CANCELLATION) {
326 call->cancellation_is_inherited = 1;
Craig Tiller45724b32015-09-22 10:42:19 -0700327 }
Craig Tillera82950e2015-09-22 12:33:20 -0700328
329 if (parent_call->first_child == NULL) {
330 parent_call->first_child = call;
331 call->sibling_next = call->sibling_prev = call;
332 } else {
333 call->sibling_next = parent_call->first_child;
334 call->sibling_prev = parent_call->first_child->sibling_prev;
335 call->sibling_next->sibling_prev = call->sibling_prev->sibling_next =
336 call;
337 }
338
339 gpr_mu_unlock(&parent_call->mu);
340 }
341 if (gpr_time_cmp(send_deadline, gpr_inf_future(send_deadline.clock_type)) !=
342 0) {
343 set_deadline_alarm(&exec_ctx, call, send_deadline);
344 }
345 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller0ba432d2015-10-09 16:57:11 -0700346 GPR_TIMER_END("grpc_call_create", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800347 return call;
348}
349
Craig Tillera82950e2015-09-22 12:33:20 -0700350void grpc_call_set_completion_queue(grpc_exec_ctx *exec_ctx, grpc_call *call,
351 grpc_completion_queue *cq) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800352 GPR_ASSERT(cq);
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700353
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700354 if (grpc_polling_entity_pollset_set(&call->pollent) != NULL) {
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700355 gpr_log(GPR_ERROR, "A pollset_set is already registered for this call.");
356 abort();
357 }
Craig Tiller166e2502015-02-03 20:14:41 -0800358 call->cq = cq;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800359 GRPC_CQ_INTERNAL_REF(cq, "bind");
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700360 call->pollent = grpc_polling_entity_create_from_pollset(grpc_cq_pollset(cq));
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700361 grpc_call_stack_set_pollset_or_pollset_set(
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700362 exec_ctx, CALL_STACK_FROM_CALL(call), &call->pollent);
Craig Tiller166e2502015-02-03 20:14:41 -0800363}
364
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800365#ifdef GRPC_STREAM_REFCOUNT_DEBUG
Craig Tiller7b435612015-11-24 08:15:05 -0800366#define REF_REASON reason
367#define REF_ARG , const char *reason
Craig Tiller4df412b2015-04-28 07:57:54 -0700368#else
Craig Tiller7b435612015-11-24 08:15:05 -0800369#define REF_REASON ""
370#define REF_ARG
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800371#endif
Craig Tiller7b435612015-11-24 08:15:05 -0800372void grpc_call_internal_ref(grpc_call *c REF_ARG) {
373 GRPC_CALL_STACK_REF(CALL_STACK_FROM_CALL(c), REF_REASON);
374}
375void grpc_call_internal_unref(grpc_exec_ctx *exec_ctx, grpc_call *c REF_ARG) {
376 GRPC_CALL_STACK_UNREF(exec_ctx, CALL_STACK_FROM_CALL(c), REF_REASON);
377}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800378
David Garcia Quintas01c4d992016-07-07 20:11:27 -0700379static void get_final_status(grpc_call *call,
380 void (*set_value)(grpc_status_code code,
381 void *user_data),
382 void *set_value_user_data) {
383 int i;
384 for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
385 if (call->status[i].is_set) {
386 set_value(call->status[i].code, set_value_user_data);
387 return;
388 }
389 }
390 if (call->is_client) {
391 set_value(GRPC_STATUS_UNKNOWN, set_value_user_data);
392 } else {
393 set_value(GRPC_STATUS_OK, set_value_user_data);
394 }
395}
396
397static void set_status_value_directly(grpc_status_code status, void *dest);
Craig Tillerc027e772016-05-03 16:27:00 -0700398static void destroy_call(grpc_exec_ctx *exec_ctx, void *call,
399 grpc_error *error) {
Craig Tiller566316f2015-02-02 15:25:32 -0800400 size_t i;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800401 int ii;
Craig Tilleraef25da2015-01-29 17:19:45 -0800402 grpc_call *c = call;
Craig Tiller0ba432d2015-10-09 16:57:11 -0700403 GPR_TIMER_BEGIN("destroy_call", 0);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800404 for (i = 0; i < 2; i++) {
405 grpc_metadata_batch_destroy(
406 &c->metadata_batch[1 /* is_receiving */][i /* is_initial */]);
407 }
408 if (c->receiving_stream != NULL) {
Craig Tiller3b66ab92015-12-09 19:42:22 -0800409 grpc_byte_stream_destroy(exec_ctx, c->receiving_stream);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800410 }
Craig Tillera82950e2015-09-22 12:33:20 -0700411 gpr_mu_destroy(&c->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700412 for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
413 if (c->status[i].details) {
414 GRPC_MDSTR_UNREF(c->status[i].details);
Craig Tiller68752722015-01-29 14:59:54 -0800415 }
Craig Tillera82950e2015-09-22 12:33:20 -0700416 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800417 for (ii = 0; ii < c->send_extra_metadata_count; ii++) {
418 GRPC_MDELEM_UNREF(c->send_extra_metadata[ii].md);
Craig Tillera82950e2015-09-22 12:33:20 -0700419 }
420 for (i = 0; i < GRPC_CONTEXT_COUNT; i++) {
421 if (c->context[i].destroy) {
422 c->context[i].destroy(c->context[i].value);
Craig Tiller935cf422015-05-01 14:10:46 -0700423 }
Craig Tillera82950e2015-09-22 12:33:20 -0700424 }
Craig Tillera82950e2015-09-22 12:33:20 -0700425 if (c->cq) {
426 GRPC_CQ_INTERNAL_UNREF(c->cq, "bind");
427 }
Craig Tiller9859d8d2016-04-26 21:07:53 -0700428 grpc_channel *channel = c->channel;
David Garcia Quintas01c4d992016-07-07 20:11:27 -0700429
430 get_final_status(call, set_status_value_directly,
431 &c->final_info.final_status);
432
433 grpc_call_stack_destroy(exec_ctx, CALL_STACK_FROM_CALL(c), &c->final_info, c);
Craig Tiller9859d8d2016-04-26 21:07:53 -0700434 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, channel, "call");
Craig Tiller0ba432d2015-10-09 16:57:11 -0700435 GPR_TIMER_END("destroy_call", 0);
Craig Tillera4541102015-01-29 11:46:11 -0800436}
437
Craig Tillera82950e2015-09-22 12:33:20 -0700438static void set_status_code(grpc_call *call, status_source source,
Craig Tiller7536af02015-12-22 13:49:30 -0800439 uint32_t status) {
Craig Tillera82950e2015-09-22 12:33:20 -0700440 if (call->status[source].is_set) return;
Craig Tillerb8d3a312015-06-19 17:27:53 -0700441
Craig Tillerdaceea82015-02-02 16:15:53 -0800442 call->status[source].is_set = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700443 call->status[source].code = (grpc_status_code)status;
Craig Tillerf0f70a82016-06-23 13:55:06 -0700444}
Craig Tiller30547562015-02-05 17:04:51 -0800445
Craig Tillerf0f70a82016-06-23 13:55:06 -0700446static void set_status_details(grpc_call *call, status_source source,
447 grpc_mdstr *status) {
448 if (call->status[source].details != NULL) {
Craig Tillerbe1b9a72016-06-24 13:22:11 -0700449 GRPC_MDSTR_UNREF(status);
450 } else {
451 call->status[source].details = status;
Craig Tillerf0f70a82016-06-23 13:55:06 -0700452 }
Craig Tillerf0f70a82016-06-23 13:55:06 -0700453}
454
Craig Tillerf0f70a82016-06-23 13:55:06 -0700455static void set_status_from_error(grpc_call *call, status_source source,
456 grpc_error *error) {
457 intptr_t status;
458 if (grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, &status)) {
459 set_status_code(call, source, (uint32_t)status);
460 } else {
461 set_status_code(call, source, GRPC_STATUS_INTERNAL);
462 }
463 const char *msg = grpc_error_get_str(error, GRPC_ERROR_STR_GRPC_MESSAGE);
464 bool free_msg = false;
465 if (msg == NULL) {
466 free_msg = true;
467 msg = grpc_error_string(error);
468 }
469 set_status_details(call, source, grpc_mdstr_from_string(msg));
470 if (free_msg) grpc_error_free_string(msg);
Craig Tiller68752722015-01-29 14:59:54 -0800471}
472
David Garcia Quintasac094472016-05-18 20:25:57 -0700473static void set_incoming_compression_algorithm(
474 grpc_call *call, grpc_compression_algorithm algo) {
David Garcia Quintas303d3082016-05-05 18:25:34 -0700475 GPR_ASSERT(algo < GRPC_COMPRESS_ALGORITHMS_COUNT);
David Garcia Quintas749367f2016-05-17 19:15:24 -0700476 call->incoming_compression_algorithm = algo;
David Garcia Quintasdb94b272015-06-15 18:37:01 -0700477}
478
David Garcia Quintas0c331882015-10-08 14:51:54 -0700479grpc_compression_algorithm grpc_call_test_only_get_compression_algorithm(
David Garcia Quintas64824be2015-10-06 19:45:36 -0700480 grpc_call *call) {
481 grpc_compression_algorithm algorithm;
482 gpr_mu_lock(&call->mu);
David Garcia Quintas749367f2016-05-17 19:15:24 -0700483 algorithm = call->incoming_compression_algorithm;
David Garcia Quintas64824be2015-10-06 19:45:36 -0700484 gpr_mu_unlock(&call->mu);
485 return algorithm;
David Garcia Quintas7c0d9142015-07-23 04:58:20 -0700486}
487
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700488static grpc_compression_algorithm compression_algorithm_for_level_locked(
489 grpc_call *call, grpc_compression_level level) {
David Garcia Quintasac094472016-05-18 20:25:57 -0700490 return grpc_compression_algorithm_for_level(level,
491 call->encodings_accepted_by_peer);
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700492}
493
Craig Tiller7536af02015-12-22 13:49:30 -0800494uint32_t grpc_call_test_only_get_message_flags(grpc_call *call) {
495 uint32_t flags;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800496 gpr_mu_lock(&call->mu);
497 flags = call->test_only_last_message_flags;
498 gpr_mu_unlock(&call->mu);
499 return flags;
500}
501
Craig Tiller3ff27542015-10-09 15:39:44 -0700502static void destroy_encodings_accepted_by_peer(void *p) { return; }
503
504static void set_encodings_accepted_by_peer(grpc_call *call, grpc_mdelem *mdel) {
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700505 size_t i;
506 grpc_compression_algorithm algorithm;
507 gpr_slice_buffer accept_encoding_parts;
Craig Tiller3ff27542015-10-09 15:39:44 -0700508 gpr_slice accept_encoding_slice;
509 void *accepted_user_data;
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700510
Craig Tiller3ff27542015-10-09 15:39:44 -0700511 accepted_user_data =
512 grpc_mdelem_get_user_data(mdel, destroy_encodings_accepted_by_peer);
513 if (accepted_user_data != NULL) {
514 call->encodings_accepted_by_peer =
Craig Tiller7536af02015-12-22 13:49:30 -0800515 (uint32_t)(((uintptr_t)accepted_user_data) - 1);
Craig Tiller3ff27542015-10-09 15:39:44 -0700516 return;
517 }
518
519 accept_encoding_slice = mdel->value->slice;
Craig Tillera82950e2015-09-22 12:33:20 -0700520 gpr_slice_buffer_init(&accept_encoding_parts);
521 gpr_slice_split(accept_encoding_slice, ",", &accept_encoding_parts);
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700522
David Garcia Quintase091af82015-07-15 21:37:02 -0700523 /* No need to zero call->encodings_accepted_by_peer: grpc_call_create already
524 * zeroes the whole grpc_call */
David Garcia Quintasb1866bd2015-07-08 22:37:01 -0700525 /* Always support no compression */
Craig Tillera82950e2015-09-22 12:33:20 -0700526 GPR_BITSET(&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE);
527 for (i = 0; i < accept_encoding_parts.count; i++) {
528 const gpr_slice *accept_encoding_entry_slice =
529 &accept_encoding_parts.slices[i];
530 if (grpc_compression_algorithm_parse(
531 (const char *)GPR_SLICE_START_PTR(*accept_encoding_entry_slice),
532 GPR_SLICE_LENGTH(*accept_encoding_entry_slice), &algorithm)) {
533 GPR_BITSET(&call->encodings_accepted_by_peer, algorithm);
534 } else {
535 char *accept_encoding_entry_str =
536 gpr_dump_slice(*accept_encoding_entry_slice, GPR_DUMP_ASCII);
537 gpr_log(GPR_ERROR,
538 "Invalid entry in accept encoding metadata: '%s'. Ignoring.",
539 accept_encoding_entry_str);
540 gpr_free(accept_encoding_entry_str);
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700541 }
Craig Tillera82950e2015-09-22 12:33:20 -0700542 }
Craig Tiller3ff27542015-10-09 15:39:44 -0700543
544 gpr_slice_buffer_destroy(&accept_encoding_parts);
545
546 grpc_mdelem_set_user_data(
547 mdel, destroy_encodings_accepted_by_peer,
Craig Tiller7536af02015-12-22 13:49:30 -0800548 (void *)(((uintptr_t)call->encodings_accepted_by_peer) + 1));
David Garcia Quintasb8edf7e2015-07-08 20:18:57 -0700549}
550
Craig Tiller7536af02015-12-22 13:49:30 -0800551uint32_t grpc_call_test_only_get_encodings_accepted_by_peer(grpc_call *call) {
552 uint32_t encodings_accepted_by_peer;
David Garcia Quintas0c331882015-10-08 14:51:54 -0700553 gpr_mu_lock(&call->mu);
554 encodings_accepted_by_peer = call->encodings_accepted_by_peer;
555 gpr_mu_unlock(&call->mu);
556 return encodings_accepted_by_peer;
Craig Tiller68752722015-01-29 14:59:54 -0800557}
558
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800559static void get_final_details(grpc_call *call, char **out_details,
560 size_t *out_details_capacity) {
Craig Tillerfb189f82015-02-03 12:07:07 -0800561 int i;
Craig Tillera82950e2015-09-22 12:33:20 -0700562 for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
563 if (call->status[i].is_set) {
564 if (call->status[i].details) {
565 gpr_slice details = call->status[i].details->slice;
566 size_t len = GPR_SLICE_LENGTH(details);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800567 if (len + 1 > *out_details_capacity) {
568 *out_details_capacity =
569 GPR_MAX(len + 1, *out_details_capacity * 3 / 2);
570 *out_details = gpr_realloc(*out_details, *out_details_capacity);
Craig Tillera82950e2015-09-22 12:33:20 -0700571 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800572 memcpy(*out_details, GPR_SLICE_START_PTR(details), len);
573 (*out_details)[len] = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700574 } else {
575 goto no_details;
576 }
577 return;
Craig Tiller68752722015-01-29 14:59:54 -0800578 }
Craig Tillera82950e2015-09-22 12:33:20 -0700579 }
Craig Tiller1e0d4c42015-01-30 16:17:29 -0800580
581no_details:
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800582 if (0 == *out_details_capacity) {
583 *out_details_capacity = 8;
584 *out_details = gpr_malloc(*out_details_capacity);
Craig Tillera82950e2015-09-22 12:33:20 -0700585 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800586 **out_details = 0;
Craig Tiller68752722015-01-29 14:59:54 -0800587}
588
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800589static grpc_linked_mdelem *linked_from_md(grpc_metadata *md) {
590 return (grpc_linked_mdelem *)&md->internal_data;
Craig Tillerc12fee62015-02-03 11:55:50 -0800591}
592
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700593static grpc_metadata *get_md_elem(grpc_metadata *metadata,
594 grpc_metadata *additional_metadata, int i,
595 int count) {
596 grpc_metadata *res =
597 i < count ? &metadata[i] : &additional_metadata[i - count];
598 GPR_ASSERT(res);
599 return res;
600}
601
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800602static int prepare_application_metadata(grpc_call *call, int count,
603 grpc_metadata *metadata,
604 int is_trailing,
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700605 int prepend_extra_metadata,
606 grpc_metadata *additional_metadata,
607 int additional_metadata_count) {
608 int total_count = count + additional_metadata_count;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800609 int i;
610 grpc_metadata_batch *batch =
611 &call->metadata_batch[0 /* is_receiving */][is_trailing];
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700612 for (i = 0; i < total_count; i++) {
613 const grpc_metadata *md =
614 get_md_elem(metadata, additional_metadata, i, count);
Craig Tillerb42445c2016-04-22 13:11:44 -0700615 grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data;
616 GPR_ASSERT(sizeof(grpc_linked_mdelem) == sizeof(md->internal_data));
617 l->md = grpc_mdelem_from_string_and_buffer(
618 md->key, (const uint8_t *)md->value, md->value_length);
619 if (!grpc_header_key_is_legal(grpc_mdstr_as_c_string(l->md->key),
620 GRPC_MDSTR_LENGTH(l->md->key))) {
621 gpr_log(GPR_ERROR, "attempt to send invalid metadata key: %s",
622 grpc_mdstr_as_c_string(l->md->key));
623 break;
624 } else if (!grpc_is_binary_header(grpc_mdstr_as_c_string(l->md->key),
625 GRPC_MDSTR_LENGTH(l->md->key)) &&
626 !grpc_header_nonbin_value_is_legal(
627 grpc_mdstr_as_c_string(l->md->value),
628 GRPC_MDSTR_LENGTH(l->md->value))) {
629 gpr_log(GPR_ERROR, "attempt to send invalid metadata value");
630 break;
631 }
632 }
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700633 if (i != total_count) {
Craig Tillerb42445c2016-04-22 13:11:44 -0700634 for (int j = 0; j <= i; j++) {
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700635 const grpc_metadata *md =
636 get_md_elem(metadata, additional_metadata, j, count);
Craig Tillerb42445c2016-04-22 13:11:44 -0700637 grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data;
638 GRPC_MDELEM_UNREF(l->md);
639 }
640 return 0;
641 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800642 if (prepend_extra_metadata) {
643 if (call->send_extra_metadata_count == 0) {
644 prepend_extra_metadata = 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700645 } else {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800646 for (i = 0; i < call->send_extra_metadata_count; i++) {
647 GRPC_MDELEM_REF(call->send_extra_metadata[i].md);
648 }
649 for (i = 1; i < call->send_extra_metadata_count; i++) {
650 call->send_extra_metadata[i].prev = &call->send_extra_metadata[i - 1];
651 }
652 for (i = 0; i < call->send_extra_metadata_count - 1; i++) {
653 call->send_extra_metadata[i].next = &call->send_extra_metadata[i + 1];
Craig Tillera82950e2015-09-22 12:33:20 -0700654 }
Craig Tiller629b0ed2015-04-22 11:14:26 -0700655 }
Craig Tillera82950e2015-09-22 12:33:20 -0700656 }
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700657 for (i = 1; i < total_count; i++) {
658 grpc_metadata *md = get_md_elem(metadata, additional_metadata, i, count);
659 grpc_metadata *prev_md =
660 get_md_elem(metadata, additional_metadata, i - 1, count);
661 linked_from_md(md)->prev = linked_from_md(prev_md);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800662 }
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700663 for (i = 0; i < total_count - 1; i++) {
664 grpc_metadata *md = get_md_elem(metadata, additional_metadata, i, count);
665 grpc_metadata *next_md =
666 get_md_elem(metadata, additional_metadata, i + 1, count);
667 linked_from_md(md)->next = linked_from_md(next_md);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800668 }
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700669
670 switch (prepend_extra_metadata * 2 + (total_count != 0)) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800671 case 0:
672 /* no prepend, no metadata => nothing to do */
673 batch->list.head = batch->list.tail = NULL;
674 break;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700675 case 1: {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800676 /* metadata, but no prepend */
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700677 grpc_metadata *first_md =
678 get_md_elem(metadata, additional_metadata, 0, count);
679 grpc_metadata *last_md =
680 get_md_elem(metadata, additional_metadata, total_count - 1, count);
681 batch->list.head = linked_from_md(first_md);
682 batch->list.tail = linked_from_md(last_md);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800683 batch->list.head->prev = NULL;
684 batch->list.tail->next = NULL;
685 break;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700686 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800687 case 2:
688 /* prepend, but no md */
689 batch->list.head = &call->send_extra_metadata[0];
690 batch->list.tail =
691 &call->send_extra_metadata[call->send_extra_metadata_count - 1];
692 batch->list.head->prev = NULL;
693 batch->list.tail->next = NULL;
694 break;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700695 case 3: {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800696 /* prepend AND md */
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700697 grpc_metadata *first_md =
698 get_md_elem(metadata, additional_metadata, 0, count);
699 grpc_metadata *last_md =
700 get_md_elem(metadata, additional_metadata, total_count - 1, count);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800701 batch->list.head = &call->send_extra_metadata[0];
702 call->send_extra_metadata[call->send_extra_metadata_count - 1].next =
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700703 linked_from_md(first_md);
704 linked_from_md(first_md)->prev =
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800705 &call->send_extra_metadata[call->send_extra_metadata_count - 1];
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700706 batch->list.tail = linked_from_md(last_md);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800707 batch->list.head->prev = NULL;
708 batch->list.tail->next = NULL;
709 break;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700710 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800711 default:
712 GPR_UNREACHABLE_CODE(return 0);
713 }
714
Craig Tillerb96d0012015-05-06 15:33:23 -0700715 return 1;
716}
717
Craig Tillera82950e2015-09-22 12:33:20 -0700718void grpc_call_destroy(grpc_call *c) {
ctillerc6d61c42014-12-15 14:52:08 -0800719 int cancel;
Craig Tiller2e95e4a2015-08-07 10:40:33 -0700720 grpc_call *parent = c->parent;
Craig Tillerf5768a62015-09-22 10:54:34 -0700721 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller2e95e4a2015-08-07 10:40:33 -0700722
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800723 GPR_TIMER_BEGIN("grpc_call_destroy", 0);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700724 GRPC_API_TRACE("grpc_call_destroy(c=%p)", 1, (c));
725
Craig Tillera82950e2015-09-22 12:33:20 -0700726 if (parent) {
727 gpr_mu_lock(&parent->mu);
728 if (c == parent->first_child) {
729 parent->first_child = c->sibling_next;
730 if (c == parent->first_child) {
731 parent->first_child = NULL;
732 }
733 c->sibling_prev->sibling_next = c->sibling_next;
734 c->sibling_next->sibling_prev = c->sibling_prev;
Craig Tiller2e95e4a2015-08-07 10:40:33 -0700735 }
Craig Tillera82950e2015-09-22 12:33:20 -0700736 gpr_mu_unlock(&parent->mu);
737 GRPC_CALL_INTERNAL_UNREF(&exec_ctx, parent, "child");
738 }
Craig Tiller2e95e4a2015-08-07 10:40:33 -0700739
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800740 gpr_mu_lock(&c->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700741 GPR_ASSERT(!c->destroy_called);
Craig Tillerf3fba742015-06-11 09:36:33 -0700742 c->destroy_called = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700743 if (c->have_alarm) {
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700744 grpc_timer_cancel(&exec_ctx, &c->alarm);
Craig Tillera82950e2015-09-22 12:33:20 -0700745 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800746 cancel = !c->received_final_op;
747 gpr_mu_unlock(&c->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700748 if (cancel) grpc_call_cancel(c, NULL);
749 GRPC_CALL_INTERNAL_UNREF(&exec_ctx, c, "destroy");
750 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800751 GPR_TIMER_END("grpc_call_destroy", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800752}
753
Craig Tillera82950e2015-09-22 12:33:20 -0700754grpc_call_error grpc_call_cancel(grpc_call *call, void *reserved) {
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700755 GRPC_API_TRACE("grpc_call_cancel(call=%p, reserved=%p)", 2, (call, reserved));
Craig Tillera82950e2015-09-22 12:33:20 -0700756 GPR_ASSERT(!reserved);
757 return grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED, "Cancelled",
758 NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800759}
760
Craig Tillera82950e2015-09-22 12:33:20 -0700761grpc_call_error grpc_call_cancel_with_status(grpc_call *c,
762 grpc_status_code status,
763 const char *description,
764 void *reserved) {
Craig Tiller5dde66e2015-06-02 09:05:23 -0700765 grpc_call_error r;
Craig Tillerf5768a62015-09-22 10:54:34 -0700766 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700767 GRPC_API_TRACE(
768 "grpc_call_cancel_with_status("
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700769 "c=%p, status=%d, description=%s, reserved=%p)",
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700770 4, (c, (int)status, description, reserved));
Craig Tillera82950e2015-09-22 12:33:20 -0700771 GPR_ASSERT(reserved == NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800772 gpr_mu_lock(&c->mu);
773 r = cancel_with_status(&exec_ctx, c, status, description);
774 gpr_mu_unlock(&c->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700775 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller5dde66e2015-06-02 09:05:23 -0700776 return r;
Yang Gaoff30f8e2015-05-04 00:13:39 -0700777}
778
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700779typedef struct termination_closure {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800780 grpc_closure closure;
781 grpc_call *call;
Craig Tillerf0f70a82016-06-23 13:55:06 -0700782 grpc_error *error;
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700783 grpc_closure *op_closure;
784 enum { TC_CANCEL, TC_CLOSE } type;
Craig Tiller6e7b45e2016-07-08 17:25:49 -0700785 grpc_transport_stream_op op;
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700786} termination_closure;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800787
Craig Tiller0ca68b72016-06-09 07:50:50 -0700788static void done_termination(grpc_exec_ctx *exec_ctx, void *tcp,
789 grpc_error *error) {
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700790 termination_closure *tc = tcp;
Craig Tiller0ca68b72016-06-09 07:50:50 -0700791 switch (tc->type) {
792 case TC_CANCEL:
793 GRPC_CALL_INTERNAL_UNREF(exec_ctx, tc->call, "cancel");
794 break;
795 case TC_CLOSE:
796 GRPC_CALL_INTERNAL_UNREF(exec_ctx, tc->call, "close");
797 break;
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700798 }
Craig Tillerf0f70a82016-06-23 13:55:06 -0700799 GRPC_ERROR_UNREF(tc->error);
Craig Tiller0ca68b72016-06-09 07:50:50 -0700800 grpc_exec_ctx_sched(exec_ctx, tc->op_closure, GRPC_ERROR_NONE, NULL);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700801 gpr_free(tc);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800802}
803
Craig Tiller0ca68b72016-06-09 07:50:50 -0700804static void send_cancel(grpc_exec_ctx *exec_ctx, void *tcp, grpc_error *error) {
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700805 termination_closure *tc = tcp;
Craig Tiller6e7b45e2016-07-08 17:25:49 -0700806 memset(&tc->op, 0, sizeof(tc->op));
807 tc->op.cancel_error = tc->error;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800808 /* reuse closure to catch completion */
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700809 grpc_closure_init(&tc->closure, done_termination, tc);
Craig Tiller6e7b45e2016-07-08 17:25:49 -0700810 tc->op.on_complete = &tc->closure;
811 execute_op(exec_ctx, tc->call, &tc->op);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700812}
813
Craig Tiller0ca68b72016-06-09 07:50:50 -0700814static void send_close(grpc_exec_ctx *exec_ctx, void *tcp, grpc_error *error) {
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700815 termination_closure *tc = tcp;
Craig Tiller6e7b45e2016-07-08 17:25:49 -0700816 memset(&tc->op, 0, sizeof(tc->op));
817 tc->op.close_error = tc->error;
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700818 /* reuse closure to catch completion */
819 grpc_closure_init(&tc->closure, done_termination, tc);
Craig Tiller6e7b45e2016-07-08 17:25:49 -0700820 tc->op_closure = tc->op.on_complete;
821 tc->op.on_complete = &tc->closure;
822 execute_op(exec_ctx, tc->call, &tc->op);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700823}
824
825static grpc_call_error terminate_with_status(grpc_exec_ctx *exec_ctx,
826 termination_closure *tc) {
Craig Tillerf0f70a82016-06-23 13:55:06 -0700827 set_status_from_error(tc->call, STATUS_FROM_API_OVERRIDE, tc->error);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700828
829 if (tc->type == TC_CANCEL) {
830 grpc_closure_init(&tc->closure, send_cancel, tc);
831 GRPC_CALL_INTERNAL_REF(tc->call, "cancel");
832 } else if (tc->type == TC_CLOSE) {
833 grpc_closure_init(&tc->closure, send_close, tc);
834 GRPC_CALL_INTERNAL_REF(tc->call, "close");
835 }
Craig Tiller0ca68b72016-06-09 07:50:50 -0700836 grpc_exec_ctx_sched(exec_ctx, &tc->closure, GRPC_ERROR_NONE, NULL);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700837 return GRPC_CALL_OK;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800838}
839
840static grpc_call_error cancel_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c,
841 grpc_status_code status,
Craig Tillera82950e2015-09-22 12:33:20 -0700842 const char *description) {
Craig Tillerf0f70a82016-06-23 13:55:06 -0700843 GPR_ASSERT(status != GRPC_STATUS_OK);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700844 termination_closure *tc = gpr_malloc(sizeof(*tc));
845 memset(tc, 0, sizeof(termination_closure));
846 tc->type = TC_CANCEL;
847 tc->call = c;
Craig Tillerf0f70a82016-06-23 13:55:06 -0700848 tc->error = grpc_error_set_int(
849 grpc_error_set_str(GRPC_ERROR_CREATE(description),
850 GRPC_ERROR_STR_GRPC_MESSAGE, description),
851 GRPC_ERROR_INT_GRPC_STATUS, status);
Craig Tiller5dde66e2015-06-02 09:05:23 -0700852
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700853 return terminate_with_status(exec_ctx, tc);
854}
Craig Tiller48b9fde2015-04-24 08:04:59 -0700855
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700856static grpc_call_error close_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c,
857 grpc_status_code status,
858 const char *description) {
Craig Tillerf0f70a82016-06-23 13:55:06 -0700859 GPR_ASSERT(status != GRPC_STATUS_OK);
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700860 termination_closure *tc = gpr_malloc(sizeof(*tc));
861 memset(tc, 0, sizeof(termination_closure));
862 tc->type = TC_CLOSE;
863 tc->call = c;
Craig Tillerf0f70a82016-06-23 13:55:06 -0700864 tc->error = grpc_error_set_int(
865 grpc_error_set_str(GRPC_ERROR_CREATE(description),
866 GRPC_ERROR_STR_GRPC_MESSAGE, description),
867 GRPC_ERROR_INT_GRPC_STATUS, status);
Craig Tiller48b9fde2015-04-24 08:04:59 -0700868
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700869 return terminate_with_status(exec_ctx, tc);
Craig Tillerd248c242015-01-14 11:49:12 -0800870}
871
Craig Tillera82950e2015-09-22 12:33:20 -0700872static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call,
873 grpc_transport_stream_op *op) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800874 grpc_call_element *elem;
Craig Tiller5dde66e2015-06-02 09:05:23 -0700875
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800876 GPR_TIMER_BEGIN("execute_op", 0);
Craig Tillera82950e2015-09-22 12:33:20 -0700877 elem = CALL_ELEM_FROM_CALL(call, 0);
Craig Tiller935cf422015-05-01 14:10:46 -0700878 op->context = call->context;
Craig Tillera82950e2015-09-22 12:33:20 -0700879 elem->filter->start_transport_stream_op(exec_ctx, elem, op);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800880 GPR_TIMER_END("execute_op", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800881}
882
Craig Tillera82950e2015-09-22 12:33:20 -0700883char *grpc_call_get_peer(grpc_call *call) {
884 grpc_call_element *elem = CALL_ELEM_FROM_CALL(call, 0);
Craig Tillerf5768a62015-09-22 10:54:34 -0700885 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller27e5aa42015-11-24 16:28:54 -0800886 char *result;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700887 GRPC_API_TRACE("grpc_call_get_peer(%p)", 1, (call));
Craig Tiller27e5aa42015-11-24 16:28:54 -0800888 result = elem->filter->get_peer(&exec_ctx, elem);
889 if (result == NULL) {
890 result = grpc_channel_get_target(call->channel);
891 }
892 if (result == NULL) {
893 result = gpr_strdup("unknown");
894 }
Craig Tillera82950e2015-09-22 12:33:20 -0700895 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller3ffd8222015-09-21 08:21:57 -0700896 return result;
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700897}
898
Craig Tillera82950e2015-09-22 12:33:20 -0700899grpc_call *grpc_call_from_top_element(grpc_call_element *elem) {
900 return CALL_FROM_TOP_ELEM(elem);
Craig Tiller566316f2015-02-02 15:25:32 -0800901}
902
Craig Tillerc027e772016-05-03 16:27:00 -0700903static void call_alarm(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
Craig Tiller566316f2015-02-02 15:25:32 -0800904 grpc_call *call = arg;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800905 gpr_mu_lock(&call->mu);
Craig Tiller77f04612015-07-01 13:39:45 -0700906 call->have_alarm = 0;
Craig Tiller1c51edc2016-05-07 16:18:43 -0700907 if (error != GRPC_ERROR_CANCELLED) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800908 cancel_with_status(exec_ctx, call, GRPC_STATUS_DEADLINE_EXCEEDED,
Craig Tillera82950e2015-09-22 12:33:20 -0700909 "Deadline Exceeded");
910 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800911 gpr_mu_unlock(&call->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700912 GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "alarm");
Craig Tiller566316f2015-02-02 15:25:32 -0800913}
914
Craig Tillera82950e2015-09-22 12:33:20 -0700915static void set_deadline_alarm(grpc_exec_ctx *exec_ctx, grpc_call *call,
916 gpr_timespec deadline) {
917 if (call->have_alarm) {
918 gpr_log(GPR_ERROR, "Attempt to set deadline alarm twice");
919 assert(0);
920 return;
921 }
922 GRPC_CALL_INTERNAL_REF(call, "alarm");
Craig Tiller566316f2015-02-02 15:25:32 -0800923 call->have_alarm = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700924 call->send_deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
David Garcia Quintasf747bbc2015-10-04 23:09:47 -0700925 grpc_timer_init(exec_ctx, &call->alarm, call->send_deadline, call_alarm, call,
Craig Tillera82950e2015-09-22 12:33:20 -0700926 gpr_now(GPR_CLOCK_MONOTONIC));
Craig Tiller566316f2015-02-02 15:25:32 -0800927}
928
Craig Tiller566316f2015-02-02 15:25:32 -0800929/* we offset status by a small amount when storing it into transport metadata
930 as metadata cannot store a 0 value (which is used as OK for grpc_status_codes
931 */
932#define STATUS_OFFSET 1
Craig Tillera82950e2015-09-22 12:33:20 -0700933static void destroy_status(void *ignored) {}
Craig Tiller566316f2015-02-02 15:25:32 -0800934
Craig Tiller7536af02015-12-22 13:49:30 -0800935static uint32_t decode_status(grpc_mdelem *md) {
936 uint32_t status;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800937 void *user_data;
938 if (md == GRPC_MDELEM_GRPC_STATUS_0) return 0;
939 if (md == GRPC_MDELEM_GRPC_STATUS_1) return 1;
940 if (md == GRPC_MDELEM_GRPC_STATUS_2) return 2;
941 user_data = grpc_mdelem_get_user_data(md, destroy_status);
942 if (user_data != NULL) {
Craig Tiller7536af02015-12-22 13:49:30 -0800943 status = ((uint32_t)(intptr_t)user_data) - STATUS_OFFSET;
Craig Tillera82950e2015-09-22 12:33:20 -0700944 } else {
945 if (!gpr_parse_bytes_to_uint32(grpc_mdstr_as_c_string(md->value),
946 GPR_SLICE_LENGTH(md->value->slice),
947 &status)) {
948 status = GRPC_STATUS_UNKNOWN; /* could not parse status code */
Craig Tiller566316f2015-02-02 15:25:32 -0800949 }
Craig Tillera82950e2015-09-22 12:33:20 -0700950 grpc_mdelem_set_user_data(md, destroy_status,
Craig Tiller7536af02015-12-22 13:49:30 -0800951 (void *)(intptr_t)(status + STATUS_OFFSET));
Craig Tillera82950e2015-09-22 12:33:20 -0700952 }
Craig Tiller566316f2015-02-02 15:25:32 -0800953 return status;
954}
955
David Garcia Quintas303d3082016-05-05 18:25:34 -0700956static grpc_compression_algorithm decode_compression(grpc_mdelem *md) {
Craig Tillerebdef9d2015-11-19 17:09:49 -0800957 grpc_compression_algorithm algorithm =
958 grpc_compression_algorithm_from_mdstr(md->value);
959 if (algorithm == GRPC_COMPRESS_ALGORITHMS_COUNT) {
Craig Tillera82950e2015-09-22 12:33:20 -0700960 const char *md_c_str = grpc_mdstr_as_c_string(md->value);
David Garcia Quintas303d3082016-05-05 18:25:34 -0700961 gpr_log(GPR_ERROR,
962 "Invalid incoming compression algorithm: '%s'. Interpreting "
963 "incoming data as uncompressed.",
964 md_c_str);
965 return GRPC_COMPRESS_NONE;
Craig Tillera82950e2015-09-22 12:33:20 -0700966 }
David Garcia Quintasfc0fa332015-06-25 18:11:07 -0700967 return algorithm;
David Garcia Quintasdb94b272015-06-15 18:37:01 -0700968}
969
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800970static grpc_mdelem *recv_common_filter(grpc_call *call, grpc_mdelem *elem) {
Craig Tillerebdef9d2015-11-19 17:09:49 -0800971 if (elem->key == GRPC_MDSTR_GRPC_STATUS) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800972 GPR_TIMER_BEGIN("status", 0);
973 set_status_code(call, STATUS_FROM_WIRE, decode_status(elem));
974 GPR_TIMER_END("status", 0);
975 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -0800976 } else if (elem->key == GRPC_MDSTR_GRPC_MESSAGE) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800977 GPR_TIMER_BEGIN("status-details", 0);
978 set_status_details(call, STATUS_FROM_WIRE, GRPC_MDSTR_REF(elem->value));
979 GPR_TIMER_END("status-details", 0);
980 return NULL;
981 }
982 return elem;
983}
984
985static grpc_mdelem *publish_app_metadata(grpc_call *call, grpc_mdelem *elem,
986 int is_trailing) {
Craig Tiller566316f2015-02-02 15:25:32 -0800987 grpc_metadata_array *dest;
988 grpc_metadata *mdusr;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800989 GPR_TIMER_BEGIN("publish_app_metadata", 0);
990 dest = call->buffered_metadata[is_trailing];
991 if (dest->count == dest->capacity) {
992 dest->capacity = GPR_MAX(dest->capacity + 8, dest->capacity * 2);
993 dest->metadata =
994 gpr_realloc(dest->metadata, sizeof(grpc_metadata) * dest->capacity);
995 }
996 mdusr = &dest->metadata[dest->count++];
997 mdusr->key = grpc_mdstr_as_c_string(elem->key);
998 mdusr->value = grpc_mdstr_as_c_string(elem->value);
999 mdusr->value_length = GPR_SLICE_LENGTH(elem->value->slice);
1000 GPR_TIMER_END("publish_app_metadata", 0);
1001 return elem;
1002}
Craig Tiller566316f2015-02-02 15:25:32 -08001003
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001004static grpc_mdelem *recv_initial_filter(void *callp, grpc_mdelem *elem) {
1005 grpc_call *call = callp;
1006 elem = recv_common_filter(call, elem);
1007 if (elem == NULL) {
1008 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -08001009 } else if (elem->key == GRPC_MDSTR_GRPC_ENCODING) {
David Garcia Quintas749367f2016-05-17 19:15:24 -07001010 GPR_TIMER_BEGIN("incoming_compression_algorithm", 0);
David Garcia Quintasac094472016-05-18 20:25:57 -07001011 set_incoming_compression_algorithm(call, decode_compression(elem));
David Garcia Quintas749367f2016-05-17 19:15:24 -07001012 GPR_TIMER_END("incoming_compression_algorithm", 0);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001013 return NULL;
Craig Tillerebdef9d2015-11-19 17:09:49 -08001014 } else if (elem->key == GRPC_MDSTR_GRPC_ACCEPT_ENCODING) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001015 GPR_TIMER_BEGIN("encodings_accepted_by_peer", 0);
1016 set_encodings_accepted_by_peer(call, elem);
1017 GPR_TIMER_END("encodings_accepted_by_peer", 0);
1018 return NULL;
1019 } else {
1020 return publish_app_metadata(call, elem, 0);
Craig Tillera82950e2015-09-22 12:33:20 -07001021 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001022}
Craig Tiller6902ad22015-04-16 08:01:49 -07001023
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001024static grpc_mdelem *recv_trailing_filter(void *callp, grpc_mdelem *elem) {
1025 grpc_call *call = callp;
1026 elem = recv_common_filter(call, elem);
1027 if (elem == NULL) {
1028 return NULL;
1029 } else {
1030 return publish_app_metadata(call, elem, 1);
Craig Tillera82950e2015-09-22 12:33:20 -07001031 }
Craig Tiller629b0ed2015-04-22 11:14:26 -07001032}
Craig Tiller8b282cb2015-04-17 14:57:44 -07001033
Craig Tillera82950e2015-09-22 12:33:20 -07001034grpc_call_stack *grpc_call_get_call_stack(grpc_call *call) {
1035 return CALL_STACK_FROM_CALL(call);
Craig Tiller566316f2015-02-02 15:25:32 -08001036}
1037
1038/*
Craig Tillerfb189f82015-02-03 12:07:07 -08001039 * BATCH API IMPLEMENTATION
1040 */
1041
Craig Tillera82950e2015-09-22 12:33:20 -07001042static void set_status_value_directly(grpc_status_code status, void *dest) {
1043 *(grpc_status_code *)dest = status;
Craig Tillerfb189f82015-02-03 12:07:07 -08001044}
1045
Craig Tillera82950e2015-09-22 12:33:20 -07001046static void set_cancelled_value(grpc_status_code status, void *dest) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001047 *(int *)dest = (status != GRPC_STATUS_OK);
Craig Tiller166e2502015-02-03 20:14:41 -08001048}
Craig Tillerfb189f82015-02-03 12:07:07 -08001049
Craig Tillerc6549762016-03-09 17:10:43 -08001050static bool are_write_flags_valid(uint32_t flags) {
David Garcia Quintas1d5aca52015-06-14 14:42:04 -07001051 /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */
Craig Tiller7536af02015-12-22 13:49:30 -08001052 const uint32_t allowed_write_positions =
Craig Tillera82950e2015-09-22 12:33:20 -07001053 (GRPC_WRITE_USED_MASK | GRPC_WRITE_INTERNAL_USED_MASK);
Craig Tiller7536af02015-12-22 13:49:30 -08001054 const uint32_t invalid_positions = ~allowed_write_positions;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -07001055 return !(flags & invalid_positions);
1056}
1057
Craig Tillerc6549762016-03-09 17:10:43 -08001058static bool are_initial_metadata_flags_valid(uint32_t flags, bool is_client) {
1059 /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */
1060 uint32_t invalid_positions = ~GRPC_INITIAL_METADATA_USED_MASK;
1061 if (!is_client) {
1062 invalid_positions |= GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST;
1063 }
1064 return !(flags & invalid_positions);
1065}
1066
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001067static batch_control *allocate_batch_control(grpc_call *call) {
1068 size_t i;
1069 for (i = 0; i < MAX_CONCURRENT_BATCHES; i++) {
1070 if ((call->used_batches & (1 << i)) == 0) {
Craig Tiller7536af02015-12-22 13:49:30 -08001071 call->used_batches = (uint8_t)(call->used_batches | (uint8_t)(1 << i));
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001072 return &call->active_batches[i];
1073 }
1074 }
yang-g49209762015-12-02 11:28:19 -08001075 return NULL;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001076}
1077
1078static void finish_batch_completion(grpc_exec_ctx *exec_ctx, void *user_data,
1079 grpc_cq_completion *storage) {
1080 batch_control *bctl = user_data;
1081 grpc_call *call = bctl->call;
1082 gpr_mu_lock(&call->mu);
Craig Tiller7536af02015-12-22 13:49:30 -08001083 call->used_batches = (uint8_t)(
1084 call->used_batches & ~(uint8_t)(1 << (bctl - call->active_batches)));
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001085 gpr_mu_unlock(&call->mu);
1086 GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "completion");
1087}
1088
1089static void post_batch_completion(grpc_exec_ctx *exec_ctx,
1090 batch_control *bctl) {
1091 grpc_call *call = bctl->call;
1092 if (bctl->is_notify_tag_closure) {
Craig Tillerb08fa492016-05-10 14:56:05 -07001093 /* unrefs bctl->error */
Craig Tiller332f1b32016-05-24 13:21:21 -07001094 grpc_exec_ctx_sched(exec_ctx, bctl->notify_tag, bctl->error, NULL);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001095 gpr_mu_lock(&call->mu);
1096 bctl->call->used_batches =
Craig Tiller7536af02015-12-22 13:49:30 -08001097 (uint8_t)(bctl->call->used_batches &
1098 ~(uint8_t)(1 << (bctl - bctl->call->active_batches)));
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001099 gpr_mu_unlock(&call->mu);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001100 GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "completion");
1101 } else {
Craig Tillerb08fa492016-05-10 14:56:05 -07001102 /* unrefs bctl->error */
Craig Tillerc027e772016-05-03 16:27:00 -07001103 grpc_cq_end_op(exec_ctx, bctl->call->cq, bctl->notify_tag, bctl->error,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001104 finish_batch_completion, bctl, &bctl->cq_completion);
1105 }
1106}
1107
1108static void continue_receiving_slices(grpc_exec_ctx *exec_ctx,
1109 batch_control *bctl) {
1110 grpc_call *call = bctl->call;
1111 for (;;) {
1112 size_t remaining = call->receiving_stream->length -
1113 (*call->receiving_buffer)->data.raw.slice_buffer.length;
1114 if (remaining == 0) {
1115 call->receiving_message = 0;
Craig Tiller3b66ab92015-12-09 19:42:22 -08001116 grpc_byte_stream_destroy(exec_ctx, call->receiving_stream);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001117 call->receiving_stream = NULL;
1118 if (gpr_unref(&bctl->steps_to_complete)) {
1119 post_batch_completion(exec_ctx, bctl);
1120 }
1121 return;
1122 }
1123 if (grpc_byte_stream_next(exec_ctx, call->receiving_stream,
1124 &call->receiving_slice, remaining,
1125 &call->receiving_slice_ready)) {
1126 gpr_slice_buffer_add(&(*call->receiving_buffer)->data.raw.slice_buffer,
1127 call->receiving_slice);
1128 } else {
1129 return;
1130 }
1131 }
1132}
1133
1134static void receiving_slice_ready(grpc_exec_ctx *exec_ctx, void *bctlp,
Craig Tillerc027e772016-05-03 16:27:00 -07001135 grpc_error *error) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001136 batch_control *bctl = bctlp;
1137 grpc_call *call = bctl->call;
1138
Craig Tillerc027e772016-05-03 16:27:00 -07001139 if (error == GRPC_ERROR_NONE) {
Craig Tiller38edec62015-12-14 15:01:29 -08001140 gpr_slice_buffer_add(&(*call->receiving_buffer)->data.raw.slice_buffer,
1141 call->receiving_slice);
1142 continue_receiving_slices(exec_ctx, bctl);
1143 } else {
Craig Tillera286b042016-06-13 15:20:39 +00001144 if (grpc_trace_operation_failures) {
1145 GRPC_LOG_IF_ERROR("receiving_slice_ready", GRPC_ERROR_REF(error));
1146 }
Craig Tillere1b8c2b2015-12-16 19:27:52 -08001147 grpc_byte_stream_destroy(exec_ctx, call->receiving_stream);
Craig Tiller38edec62015-12-14 15:01:29 -08001148 call->receiving_stream = NULL;
1149 grpc_byte_buffer_destroy(*call->receiving_buffer);
1150 *call->receiving_buffer = NULL;
1151 if (gpr_unref(&bctl->steps_to_complete)) {
1152 post_batch_completion(exec_ctx, bctl);
1153 }
1154 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001155}
1156
Craig Tillera44cbfc2016-02-03 16:02:49 -08001157static void process_data_after_md(grpc_exec_ctx *exec_ctx, batch_control *bctl,
1158 bool success) {
1159 grpc_call *call = bctl->call;
1160 if (call->receiving_stream == NULL) {
1161 *call->receiving_buffer = NULL;
1162 call->receiving_message = 0;
1163 if (gpr_unref(&bctl->steps_to_complete)) {
1164 post_batch_completion(exec_ctx, bctl);
1165 }
1166 } else if (call->receiving_stream->length >
1167 grpc_channel_get_max_message_length(call->channel)) {
1168 cancel_with_status(exec_ctx, call, GRPC_STATUS_INTERNAL,
1169 "Max message size exceeded");
1170 grpc_byte_stream_destroy(exec_ctx, call->receiving_stream);
1171 call->receiving_stream = NULL;
1172 *call->receiving_buffer = NULL;
1173 call->receiving_message = 0;
1174 if (gpr_unref(&bctl->steps_to_complete)) {
1175 post_batch_completion(exec_ctx, bctl);
1176 }
1177 } else {
1178 call->test_only_last_message_flags = call->receiving_stream->flags;
1179 if ((call->receiving_stream->flags & GRPC_WRITE_INTERNAL_COMPRESS) &&
David Garcia Quintas749367f2016-05-17 19:15:24 -07001180 (call->incoming_compression_algorithm > GRPC_COMPRESS_NONE)) {
Craig Tillera44cbfc2016-02-03 16:02:49 -08001181 *call->receiving_buffer = grpc_raw_compressed_byte_buffer_create(
David Garcia Quintas749367f2016-05-17 19:15:24 -07001182 NULL, 0, call->incoming_compression_algorithm);
Craig Tillera44cbfc2016-02-03 16:02:49 -08001183 } else {
1184 *call->receiving_buffer = grpc_raw_byte_buffer_create(NULL, 0);
1185 }
1186 grpc_closure_init(&call->receiving_slice_ready, receiving_slice_ready,
1187 bctl);
1188 continue_receiving_slices(exec_ctx, bctl);
1189 /* early out */
1190 return;
1191 }
1192}
1193
1194static void receiving_stream_ready(grpc_exec_ctx *exec_ctx, void *bctlp,
Craig Tillerc027e772016-05-03 16:27:00 -07001195 grpc_error *error) {
Craig Tillera44cbfc2016-02-03 16:02:49 -08001196 batch_control *bctl = bctlp;
1197 grpc_call *call = bctl->call;
1198
1199 gpr_mu_lock(&bctl->call->mu);
Craig Tillerc027e772016-05-03 16:27:00 -07001200 if (bctl->call->has_initial_md_been_received || error != GRPC_ERROR_NONE ||
Craig Tiller52cf8712016-04-23 22:54:21 -07001201 call->receiving_stream == NULL) {
Craig Tillera44cbfc2016-02-03 16:02:49 -08001202 gpr_mu_unlock(&bctl->call->mu);
Craig Tillerc027e772016-05-03 16:27:00 -07001203 process_data_after_md(exec_ctx, bctlp, error);
Craig Tillera44cbfc2016-02-03 16:02:49 -08001204 } else {
Craig Tiller8a677802016-04-22 15:07:53 -07001205 call->saved_receiving_stream_ready_bctlp = bctlp;
Craig Tillera44cbfc2016-02-03 16:02:49 -08001206 gpr_mu_unlock(&bctl->call->mu);
1207 }
1208}
1209
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001210static void validate_filtered_metadata(grpc_exec_ctx *exec_ctx,
1211 batch_control *bctl) {
1212 grpc_call *call = bctl->call;
David Garcia Quintasac094472016-05-18 20:25:57 -07001213 /* validate call->incoming_compression_algorithm */
1214 if (call->incoming_compression_algorithm != GRPC_COMPRESS_NONE) {
1215 const grpc_compression_algorithm algo =
1216 call->incoming_compression_algorithm;
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001217 char *error_msg = NULL;
1218 const grpc_compression_options compression_options =
David Garcia Quintasac094472016-05-18 20:25:57 -07001219 grpc_channel_compression_options(call->channel);
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001220 /* check if algorithm is known */
1221 if (algo >= GRPC_COMPRESS_ALGORITHMS_COUNT) {
1222 gpr_asprintf(&error_msg, "Invalid compression algorithm value '%d'.",
1223 algo);
Yuchen Zeng64c0e8d2016-06-10 11:19:51 -07001224 gpr_log(GPR_ERROR, "%s", error_msg);
David Garcia Quintas824f8372016-05-18 17:52:46 -07001225 close_with_status(exec_ctx, call, GRPC_STATUS_UNIMPLEMENTED, error_msg);
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001226 } else if (grpc_compression_options_is_algorithm_enabled(
1227 &compression_options, algo) == 0) {
1228 /* check if algorithm is supported by current channel config */
David Garcia Quintas1ff168a2016-06-30 10:25:04 -07001229 char *algo_name = NULL;
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001230 grpc_compression_algorithm_name(algo, &algo_name);
1231 gpr_asprintf(&error_msg, "Compression algorithm '%s' is disabled.",
1232 algo_name);
Yuchen Zeng64c0e8d2016-06-10 11:19:51 -07001233 gpr_log(GPR_ERROR, "%s", error_msg);
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001234 close_with_status(exec_ctx, call, GRPC_STATUS_UNIMPLEMENTED, error_msg);
1235 } else {
David Garcia Quintasac094472016-05-18 20:25:57 -07001236 call->incoming_compression_algorithm = algo;
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001237 }
1238 gpr_free(error_msg);
1239 }
David Garcia Quintasf1945f22016-05-18 10:53:14 -07001240
1241 /* make sure the received grpc-encoding is amongst the ones listed in
1242 * grpc-accept-encoding */
1243 GPR_ASSERT(call->encodings_accepted_by_peer != 0);
1244 if (!GPR_BITGET(call->encodings_accepted_by_peer,
David Garcia Quintasac094472016-05-18 20:25:57 -07001245 call->incoming_compression_algorithm)) {
David Garcia Quintasf1945f22016-05-18 10:53:14 -07001246 extern int grpc_compression_trace;
1247 if (grpc_compression_trace) {
David Garcia Quintas1ff168a2016-06-30 10:25:04 -07001248 char *algo_name = NULL;
David Garcia Quintasac094472016-05-18 20:25:57 -07001249 grpc_compression_algorithm_name(call->incoming_compression_algorithm,
1250 &algo_name);
David Garcia Quintasf1945f22016-05-18 10:53:14 -07001251 gpr_log(GPR_ERROR,
1252 "Compression algorithm (grpc-encoding = '%s') not present in "
1253 "the bitset of accepted encodings (grpc-accept-encodings: "
1254 "'0x%x')",
1255 algo_name, call->encodings_accepted_by_peer);
1256 }
1257 }
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001258}
1259
Craig Tillera44cbfc2016-02-03 16:02:49 -08001260static void receiving_initial_metadata_ready(grpc_exec_ctx *exec_ctx,
Craig Tillerc027e772016-05-03 16:27:00 -07001261 void *bctlp, grpc_error *error) {
Craig Tillera44cbfc2016-02-03 16:02:49 -08001262 batch_control *bctl = bctlp;
1263 grpc_call *call = bctl->call;
1264
1265 gpr_mu_lock(&call->mu);
1266
Craig Tillerc027e772016-05-03 16:27:00 -07001267 if (error != GRPC_ERROR_NONE) {
Craig Tillerf707d622016-05-06 14:26:12 -07001268 bctl->error = GRPC_ERROR_REF(error);
Craig Tillerc48ca712016-04-04 13:42:04 -07001269 } else {
1270 grpc_metadata_batch *md =
1271 &call->metadata_batch[1 /* is_receiving */][0 /* is_trailing */];
1272 grpc_metadata_batch_filter(md, recv_initial_filter, call);
Craig Tillera44cbfc2016-02-03 16:02:49 -08001273
David Garcia Quintas3e71f772016-05-18 10:14:32 -07001274 GPR_TIMER_BEGIN("validate_filtered_metadata", 0);
1275 validate_filtered_metadata(exec_ctx, bctl);
1276 GPR_TIMER_END("validate_filtered_metadata", 0);
David Garcia Quintas46123372016-05-09 15:28:42 -07001277
Craig Tillerc48ca712016-04-04 13:42:04 -07001278 if (gpr_time_cmp(md->deadline, gpr_inf_future(md->deadline.clock_type)) !=
1279 0 &&
1280 !call->is_client) {
1281 GPR_TIMER_BEGIN("set_deadline_alarm", 0);
1282 set_deadline_alarm(exec_ctx, call, md->deadline);
1283 GPR_TIMER_END("set_deadline_alarm", 0);
1284 }
Craig Tillera44cbfc2016-02-03 16:02:49 -08001285 }
1286
Craig Tillerc48ca712016-04-04 13:42:04 -07001287 call->has_initial_md_been_received = true;
Craig Tiller8a677802016-04-22 15:07:53 -07001288 if (call->saved_receiving_stream_ready_bctlp != NULL) {
Craig Tillera44cbfc2016-02-03 16:02:49 -08001289 grpc_closure *saved_rsr_closure = grpc_closure_create(
Craig Tiller8a677802016-04-22 15:07:53 -07001290 receiving_stream_ready, call->saved_receiving_stream_ready_bctlp);
1291 call->saved_receiving_stream_ready_bctlp = NULL;
Craig Tiller332f1b32016-05-24 13:21:21 -07001292 grpc_exec_ctx_sched(exec_ctx, saved_rsr_closure, error, NULL);
Craig Tillera44cbfc2016-02-03 16:02:49 -08001293 }
1294
1295 gpr_mu_unlock(&call->mu);
1296
1297 if (gpr_unref(&bctl->steps_to_complete)) {
1298 post_batch_completion(exec_ctx, bctl);
1299 }
1300}
1301
Craig Tillerc027e772016-05-03 16:27:00 -07001302static void finish_batch(grpc_exec_ctx *exec_ctx, void *bctlp,
1303 grpc_error *error) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001304 batch_control *bctl = bctlp;
1305 grpc_call *call = bctl->call;
1306 grpc_call *child_call;
1307 grpc_call *next_child_call;
1308
Craig Tiller9ccf5f12016-05-07 21:41:01 -07001309 GRPC_ERROR_REF(error);
1310
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001311 gpr_mu_lock(&call->mu);
1312 if (bctl->send_initial_metadata) {
Craig Tillerc027e772016-05-03 16:27:00 -07001313 if (error != GRPC_ERROR_NONE) {
Craig Tiller5b1e6592016-03-11 15:05:30 -08001314 set_status_code(call, STATUS_FROM_CORE, GRPC_STATUS_UNAVAILABLE);
1315 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001316 grpc_metadata_batch_destroy(
1317 &call->metadata_batch[0 /* is_receiving */][0 /* is_trailing */]);
1318 }
1319 if (bctl->send_message) {
1320 call->sending_message = 0;
1321 }
1322 if (bctl->send_final_op) {
1323 grpc_metadata_batch_destroy(
1324 &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */]);
1325 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001326 if (bctl->recv_final_op) {
1327 grpc_metadata_batch *md =
1328 &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */];
1329 grpc_metadata_batch_filter(md, recv_trailing_filter, call);
1330
Craig Tiller1cbf5762016-04-22 16:02:55 -07001331 call->received_final_op = true;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001332 if (call->have_alarm) {
1333 grpc_timer_cancel(exec_ctx, &call->alarm);
1334 }
1335 /* propagate cancellation to any interested children */
1336 child_call = call->first_child;
1337 if (child_call != NULL) {
1338 do {
1339 next_child_call = child_call->sibling_next;
1340 if (child_call->cancellation_is_inherited) {
1341 GRPC_CALL_INTERNAL_REF(child_call, "propagate_cancel");
1342 grpc_call_cancel(child_call, NULL);
1343 GRPC_CALL_INTERNAL_UNREF(exec_ctx, child_call, "propagate_cancel");
1344 }
1345 child_call = next_child_call;
1346 } while (child_call != call->first_child);
1347 }
1348
1349 if (call->is_client) {
1350 get_final_status(call, set_status_value_directly,
1351 call->final_op.client.status);
1352 get_final_details(call, call->final_op.client.status_details,
1353 call->final_op.client.status_details_capacity);
1354 } else {
1355 get_final_status(call, set_cancelled_value,
1356 call->final_op.server.cancelled);
1357 }
1358
Craig Tillerf707d622016-05-06 14:26:12 -07001359 GRPC_ERROR_UNREF(error);
Craig Tillerc027e772016-05-03 16:27:00 -07001360 error = GRPC_ERROR_NONE;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001361 }
Craig Tiller10dd6f22016-05-10 13:06:15 -07001362 GRPC_ERROR_UNREF(bctl->error);
Craig Tillerb08fa492016-05-10 14:56:05 -07001363 bctl->error = GRPC_ERROR_REF(error);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001364 gpr_mu_unlock(&call->mu);
1365 if (gpr_unref(&bctl->steps_to_complete)) {
1366 post_batch_completion(exec_ctx, bctl);
1367 }
Craig Tiller10dd6f22016-05-10 13:06:15 -07001368
1369 GRPC_ERROR_UNREF(error);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001370}
1371
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001372static grpc_call_error call_start_batch(grpc_exec_ctx *exec_ctx,
1373 grpc_call *call, const grpc_op *ops,
1374 size_t nops, void *notify_tag,
1375 int is_notify_tag_closure) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001376 size_t i;
Craig Tillerfb189f82015-02-03 12:07:07 -08001377 const grpc_op *op;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001378 batch_control *bctl;
1379 int num_completion_callbacks_needed = 1;
1380 grpc_call_error error = GRPC_CALL_OK;
Craig Tiller9928d392015-08-18 09:40:24 -07001381
Vitaly Bukae60003d2016-08-01 19:34:51 -07001382 // sent_initial_metadata guards against variable reuse.
1383 grpc_metadata compression_md;
1384
Craig Tiller0ba432d2015-10-09 16:57:11 -07001385 GPR_TIMER_BEGIN("grpc_call_start_batch", 0);
Craig Tiller86253ca2015-10-08 13:31:02 -07001386
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001387 GRPC_CALL_LOG_BATCH(GPR_INFO, call, ops, nops, notify_tag);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -07001388
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001389 /* TODO(ctiller): this feels like it could be made lock-free */
1390 gpr_mu_lock(&call->mu);
1391 bctl = allocate_batch_control(call);
1392 memset(bctl, 0, sizeof(*bctl));
1393 bctl->call = call;
1394 bctl->notify_tag = notify_tag;
Craig Tiller7536af02015-12-22 13:49:30 -08001395 bctl->is_notify_tag_closure = (uint8_t)(is_notify_tag_closure != 0);
Craig Tillera82950e2015-09-22 12:33:20 -07001396
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001397 grpc_transport_stream_op *stream_op = &bctl->op;
1398 memset(stream_op, 0, sizeof(*stream_op));
1399
Craig Tillera82950e2015-09-22 12:33:20 -07001400 if (nops == 0) {
Craig Tillera82950e2015-09-22 12:33:20 -07001401 GRPC_CALL_INTERNAL_REF(call, "completion");
Craig Tillerc027e772016-05-03 16:27:00 -07001402 bctl->error = GRPC_ERROR_NONE;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001403 if (!is_notify_tag_closure) {
Craig Tiller4bf29282015-12-14 11:25:48 -08001404 grpc_cq_begin_op(call->cq, notify_tag);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001405 }
1406 gpr_mu_unlock(&call->mu);
1407 post_batch_completion(exec_ctx, bctl);
Craig Tillerea50b902015-12-15 07:05:25 -08001408 error = GRPC_CALL_OK;
1409 goto done;
Craig Tillera82950e2015-09-22 12:33:20 -07001410 }
1411
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001412 /* rewrite batch ops into a transport op */
1413 for (i = 0; i < nops; i++) {
1414 op = &ops[i];
Craig Tillera82950e2015-09-22 12:33:20 -07001415 if (op->reserved != NULL) {
Craig Tiller3ffd8222015-09-21 08:21:57 -07001416 error = GRPC_CALL_ERROR;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001417 goto done_with_error;
Craig Tiller3ffd8222015-09-21 08:21:57 -07001418 }
Craig Tillera82950e2015-09-22 12:33:20 -07001419 switch (op->op) {
1420 case GRPC_OP_SEND_INITIAL_METADATA:
1421 /* Flag validation: currently allow no flags */
Craig Tillerc6549762016-03-09 17:10:43 -08001422 if (!are_initial_metadata_flags_valid(op->flags, call->is_client)) {
Craig Tillera82950e2015-09-22 12:33:20 -07001423 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001424 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001425 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001426 if (call->sent_initial_metadata) {
1427 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1428 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001429 }
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001430 /* process compression level */
Vitaly Bukae60003d2016-08-01 19:34:51 -07001431 memset(&compression_md, 0, sizeof(compression_md));
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001432 size_t additional_metadata_count = 0;
David Garcia Quintas749367f2016-05-17 19:15:24 -07001433 grpc_compression_level effective_compression_level;
1434 bool level_set = false;
1435 if (op->data.send_initial_metadata.maybe_compression_level.is_set) {
David Garcia Quintas749367f2016-05-17 19:15:24 -07001436 effective_compression_level =
David Garcia Quintas8ba42be2016-06-07 17:30:20 -07001437 op->data.send_initial_metadata.maybe_compression_level.level;
David Garcia Quintas749367f2016-05-17 19:15:24 -07001438 level_set = true;
1439 } else {
David Garcia Quintasac094472016-05-18 20:25:57 -07001440 const grpc_compression_options copts =
1441 grpc_channel_compression_options(call->channel);
1442 level_set = copts.default_level.is_set;
1443 if (level_set) {
1444 effective_compression_level = copts.default_level.level;
1445 }
David Garcia Quintas749367f2016-05-17 19:15:24 -07001446 }
David Garcia Quintas3e4f49f2016-05-18 23:59:02 -07001447 if (level_set && !call->is_client) {
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001448 const grpc_compression_algorithm calgo =
1449 compression_algorithm_for_level_locked(
David Garcia Quintas749367f2016-05-17 19:15:24 -07001450 call, effective_compression_level);
David Garcia Quintas1ff168a2016-06-30 10:25:04 -07001451 char *calgo_name = NULL;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001452 grpc_compression_algorithm_name(calgo, &calgo_name);
David Garcia Quintas3e4f49f2016-05-18 23:59:02 -07001453 // the following will be picked up by the compress filter and used as
1454 // the call's compression algorithm.
David Garcia Quintasf54171c2016-05-17 17:19:33 -07001455 compression_md.key = GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY;
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001456 compression_md.value = calgo_name;
1457 compression_md.value_length = strlen(calgo_name);
1458 additional_metadata_count++;
1459 }
1460
1461 if (op->data.send_initial_metadata.count + additional_metadata_count >
1462 INT_MAX) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001463 error = GRPC_CALL_ERROR_INVALID_METADATA;
1464 goto done_with_error;
1465 }
1466 bctl->send_initial_metadata = 1;
1467 call->sent_initial_metadata = 1;
1468 if (!prepare_application_metadata(
1469 call, (int)op->data.send_initial_metadata.count,
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001470 op->data.send_initial_metadata.metadata, 0, call->is_client,
1471 &compression_md, (int)additional_metadata_count)) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001472 error = GRPC_CALL_ERROR_INVALID_METADATA;
1473 goto done_with_error;
1474 }
1475 /* TODO(ctiller): just make these the same variable? */
1476 call->metadata_batch[0][0].deadline = call->send_deadline;
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001477 stream_op->send_initial_metadata =
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001478 &call->metadata_batch[0 /* is_receiving */][0 /* is_trailing */];
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001479 stream_op->send_initial_metadata_flags = op->flags;
Craig Tillera82950e2015-09-22 12:33:20 -07001480 break;
1481 case GRPC_OP_SEND_MESSAGE:
1482 if (!are_write_flags_valid(op->flags)) {
1483 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001484 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001485 }
1486 if (op->data.send_message == NULL) {
1487 error = GRPC_CALL_ERROR_INVALID_MESSAGE;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001488 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001489 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001490 if (call->sending_message) {
1491 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1492 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001493 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001494 bctl->send_message = 1;
1495 call->sending_message = 1;
1496 grpc_slice_buffer_stream_init(
1497 &call->sending_stream,
1498 &op->data.send_message->data.raw.slice_buffer, op->flags);
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001499 stream_op->send_message = &call->sending_stream.base;
Craig Tillera82950e2015-09-22 12:33:20 -07001500 break;
1501 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
1502 /* Flag validation: currently allow no flags */
1503 if (op->flags != 0) {
1504 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001505 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001506 }
1507 if (!call->is_client) {
1508 error = GRPC_CALL_ERROR_NOT_ON_SERVER;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001509 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001510 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001511 if (call->sent_final_op) {
1512 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1513 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001514 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001515 bctl->send_final_op = 1;
1516 call->sent_final_op = 1;
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001517 stream_op->send_trailing_metadata =
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001518 &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */];
Craig Tillera82950e2015-09-22 12:33:20 -07001519 break;
1520 case GRPC_OP_SEND_STATUS_FROM_SERVER:
1521 /* Flag validation: currently allow no flags */
1522 if (op->flags != 0) {
1523 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001524 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001525 }
1526 if (call->is_client) {
1527 error = GRPC_CALL_ERROR_NOT_ON_CLIENT;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001528 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001529 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001530 if (call->sent_final_op) {
1531 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1532 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001533 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001534 if (op->data.send_status_from_server.trailing_metadata_count >
1535 INT_MAX) {
1536 error = GRPC_CALL_ERROR_INVALID_METADATA;
1537 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001538 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001539 bctl->send_final_op = 1;
1540 call->sent_final_op = 1;
1541 call->send_extra_metadata_count = 1;
1542 call->send_extra_metadata[0].md = grpc_channel_get_reffed_status_elem(
1543 call->channel, op->data.send_status_from_server.status);
1544 if (op->data.send_status_from_server.status_details != NULL) {
1545 call->send_extra_metadata[1].md = grpc_mdelem_from_metadata_strings(
Craig Tillerb2b42612015-11-20 12:02:17 -08001546 GRPC_MDSTR_GRPC_MESSAGE,
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001547 grpc_mdstr_from_string(
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001548 op->data.send_status_from_server.status_details));
1549 call->send_extra_metadata_count++;
1550 set_status_details(
1551 call, STATUS_FROM_API_OVERRIDE,
1552 GRPC_MDSTR_REF(call->send_extra_metadata[1].md->value));
Craig Tillera82950e2015-09-22 12:33:20 -07001553 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001554 set_status_code(call, STATUS_FROM_API_OVERRIDE,
Craig Tiller7536af02015-12-22 13:49:30 -08001555 (uint32_t)op->data.send_status_from_server.status);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001556 if (!prepare_application_metadata(
1557 call,
1558 (int)op->data.send_status_from_server.trailing_metadata_count,
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001559 op->data.send_status_from_server.trailing_metadata, 1, 1, NULL,
1560 0)) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001561 error = GRPC_CALL_ERROR_INVALID_METADATA;
1562 goto done_with_error;
1563 }
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001564 stream_op->send_trailing_metadata =
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001565 &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */];
Craig Tillera82950e2015-09-22 12:33:20 -07001566 break;
1567 case GRPC_OP_RECV_INITIAL_METADATA:
1568 /* Flag validation: currently allow no flags */
1569 if (op->flags != 0) {
1570 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001571 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001572 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001573 if (call->received_initial_metadata) {
1574 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1575 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001576 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001577 call->received_initial_metadata = 1;
1578 call->buffered_metadata[0] = op->data.recv_initial_metadata;
Craig Tillera44cbfc2016-02-03 16:02:49 -08001579 grpc_closure_init(&call->receiving_initial_metadata_ready,
1580 receiving_initial_metadata_ready, bctl);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001581 bctl->recv_initial_metadata = 1;
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001582 stream_op->recv_initial_metadata =
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001583 &call->metadata_batch[1 /* is_receiving */][0 /* is_trailing */];
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001584 stream_op->recv_initial_metadata_ready =
Craig Tillera44cbfc2016-02-03 16:02:49 -08001585 &call->receiving_initial_metadata_ready;
1586 num_completion_callbacks_needed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001587 break;
1588 case GRPC_OP_RECV_MESSAGE:
1589 /* Flag validation: currently allow no flags */
1590 if (op->flags != 0) {
1591 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001592 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001593 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001594 if (call->receiving_message) {
1595 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
yang-g48f3a712015-12-07 11:23:50 -08001596 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001597 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001598 call->receiving_message = 1;
1599 bctl->recv_message = 1;
1600 call->receiving_buffer = op->data.recv_message;
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001601 stream_op->recv_message = &call->receiving_stream;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001602 grpc_closure_init(&call->receiving_stream_ready, receiving_stream_ready,
1603 bctl);
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001604 stream_op->recv_message_ready = &call->receiving_stream_ready;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001605 num_completion_callbacks_needed++;
Craig Tillera82950e2015-09-22 12:33:20 -07001606 break;
1607 case GRPC_OP_RECV_STATUS_ON_CLIENT:
1608 /* Flag validation: currently allow no flags */
1609 if (op->flags != 0) {
1610 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001611 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001612 }
1613 if (!call->is_client) {
1614 error = GRPC_CALL_ERROR_NOT_ON_SERVER;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001615 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001616 }
Craig Tiller1cbf5762016-04-22 16:02:55 -07001617 if (call->requested_final_op) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001618 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1619 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001620 }
Craig Tiller1cbf5762016-04-22 16:02:55 -07001621 call->requested_final_op = 1;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001622 call->buffered_metadata[1] =
Craig Tillera82950e2015-09-22 12:33:20 -07001623 op->data.recv_status_on_client.trailing_metadata;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001624 call->final_op.client.status = op->data.recv_status_on_client.status;
1625 call->final_op.client.status_details =
1626 op->data.recv_status_on_client.status_details;
1627 call->final_op.client.status_details_capacity =
1628 op->data.recv_status_on_client.status_details_capacity;
1629 bctl->recv_final_op = 1;
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001630 stream_op->recv_trailing_metadata =
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001631 &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */];
Craig Tiller106df112016-08-04 09:22:34 -07001632 stream_op->collect_stats =
David Garcia Quintas5dde14c2016-07-28 17:29:27 -07001633 &call->final_info.stats.transport_stream_stats;
Craig Tillera82950e2015-09-22 12:33:20 -07001634 break;
1635 case GRPC_OP_RECV_CLOSE_ON_SERVER:
1636 /* Flag validation: currently allow no flags */
1637 if (op->flags != 0) {
1638 error = GRPC_CALL_ERROR_INVALID_FLAGS;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001639 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001640 }
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001641 if (call->is_client) {
1642 error = GRPC_CALL_ERROR_NOT_ON_CLIENT;
1643 goto done_with_error;
Craig Tillera82950e2015-09-22 12:33:20 -07001644 }
Craig Tiller1cbf5762016-04-22 16:02:55 -07001645 if (call->requested_final_op) {
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001646 error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
1647 goto done_with_error;
1648 }
Craig Tiller1cbf5762016-04-22 16:02:55 -07001649 call->requested_final_op = 1;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001650 call->final_op.server.cancelled =
Craig Tillera82950e2015-09-22 12:33:20 -07001651 op->data.recv_close_on_server.cancelled;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001652 bctl->recv_final_op = 1;
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001653 stream_op->recv_trailing_metadata =
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001654 &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */];
Craig Tiller106df112016-08-04 09:22:34 -07001655 stream_op->collect_stats =
David Garcia Quintas5dde14c2016-07-28 17:29:27 -07001656 &call->final_info.stats.transport_stream_stats;
Craig Tillera82950e2015-09-22 12:33:20 -07001657 break;
Craig Tillerfb189f82015-02-03 12:07:07 -08001658 }
Craig Tillera82950e2015-09-22 12:33:20 -07001659 }
Craig Tillerfb189f82015-02-03 12:07:07 -08001660
Craig Tillera82950e2015-09-22 12:33:20 -07001661 GRPC_CALL_INTERNAL_REF(call, "completion");
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001662 if (!is_notify_tag_closure) {
Craig Tiller4bf29282015-12-14 11:25:48 -08001663 grpc_cq_begin_op(call->cq, notify_tag);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001664 }
1665 gpr_ref_init(&bctl->steps_to_complete, num_completion_callbacks_needed);
Craig Tillerfb189f82015-02-03 12:07:07 -08001666
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001667 stream_op->context = call->context;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001668 grpc_closure_init(&bctl->finish_batch, finish_batch, bctl);
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001669 stream_op->on_complete = &bctl->finish_batch;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001670 gpr_mu_unlock(&call->mu);
1671
Craig Tiller6e7b45e2016-07-08 17:25:49 -07001672 execute_op(exec_ctx, call, stream_op);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001673
Craig Tiller3ffd8222015-09-21 08:21:57 -07001674done:
Craig Tiller0ba432d2015-10-09 16:57:11 -07001675 GPR_TIMER_END("grpc_call_start_batch", 0);
Craig Tiller3ffd8222015-09-21 08:21:57 -07001676 return error;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001677
1678done_with_error:
1679 /* reverse any mutations that occured */
1680 if (bctl->send_initial_metadata) {
1681 call->sent_initial_metadata = 0;
1682 grpc_metadata_batch_clear(&call->metadata_batch[0][0]);
1683 }
1684 if (bctl->send_message) {
1685 call->sending_message = 0;
Craig Tiller3b66ab92015-12-09 19:42:22 -08001686 grpc_byte_stream_destroy(exec_ctx, &call->sending_stream.base);
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001687 }
1688 if (bctl->send_final_op) {
1689 call->sent_final_op = 0;
1690 grpc_metadata_batch_clear(&call->metadata_batch[0][1]);
1691 }
1692 if (bctl->recv_initial_metadata) {
1693 call->received_initial_metadata = 0;
1694 }
1695 if (bctl->recv_message) {
1696 call->receiving_message = 0;
1697 }
1698 if (bctl->recv_final_op) {
Craig Tiller1cbf5762016-04-22 16:02:55 -07001699 call->requested_final_op = 0;
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001700 }
1701 gpr_mu_unlock(&call->mu);
1702 goto done;
1703}
1704
1705grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
1706 size_t nops, void *tag, void *reserved) {
1707 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
1708 grpc_call_error err;
1709
1710 GRPC_API_TRACE(
David Garcia Quintas46123372016-05-09 15:28:42 -07001711 "grpc_call_start_batch(call=%p, ops=%p, nops=%lu, tag=%p, "
1712 "reserved=%p)",
Craig Tillerc7e1a2a2015-11-02 14:17:32 -08001713 5, (call, ops, (unsigned long)nops, tag, reserved));
1714
1715 if (reserved != NULL) {
1716 err = GRPC_CALL_ERROR;
1717 } else {
1718 err = call_start_batch(&exec_ctx, call, ops, nops, tag, 0);
1719 }
1720
1721 grpc_exec_ctx_finish(&exec_ctx);
1722 return err;
1723}
1724
1725grpc_call_error grpc_call_start_batch_and_execute(grpc_exec_ctx *exec_ctx,
1726 grpc_call *call,
1727 const grpc_op *ops,
1728 size_t nops,
1729 grpc_closure *closure) {
1730 return call_start_batch(exec_ctx, call, ops, nops, closure, 1);
Craig Tillerfb189f82015-02-03 12:07:07 -08001731}
Craig Tiller935cf422015-05-01 14:10:46 -07001732
Craig Tillera82950e2015-09-22 12:33:20 -07001733void grpc_call_context_set(grpc_call *call, grpc_context_index elem,
1734 void *value, void (*destroy)(void *value)) {
1735 if (call->context[elem].destroy) {
1736 call->context[elem].destroy(call->context[elem].value);
1737 }
Julien Boeuf83b02972015-05-20 22:50:34 -07001738 call->context[elem].value = value;
1739 call->context[elem].destroy = destroy;
Craig Tiller935cf422015-05-01 14:10:46 -07001740}
1741
Craig Tillera82950e2015-09-22 12:33:20 -07001742void *grpc_call_context_get(grpc_call *call, grpc_context_index elem) {
Julien Boeuf83b02972015-05-20 22:50:34 -07001743 return call->context[elem].value;
Craig Tiller935cf422015-05-01 14:10:46 -07001744}
Julien Boeuf9f218dd2015-04-23 10:24:02 -07001745
Craig Tiller7536af02015-12-22 13:49:30 -08001746uint8_t grpc_call_is_client(grpc_call *call) { return call->is_client; }
David Garcia Quintas13c2f6e2016-03-17 22:51:52 -07001747
1748grpc_compression_algorithm grpc_call_compression_for_level(
1749 grpc_call *call, grpc_compression_level level) {
David Garcia Quintas7d2c7332016-03-18 11:37:14 -07001750 gpr_mu_lock(&call->mu);
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001751 grpc_compression_algorithm algo =
1752 compression_algorithm_for_level_locked(call, level);
David Garcia Quintas7d2c7332016-03-18 11:37:14 -07001753 gpr_mu_unlock(&call->mu);
David Garcia Quintasa301eaa2016-05-06 16:59:03 -07001754 return algo;
David Garcia Quintas13c2f6e2016-03-17 22:51:52 -07001755}
Yuchen Zeng2e7d9572016-04-15 17:29:57 -07001756
1757const char *grpc_call_error_to_string(grpc_call_error error) {
1758 switch (error) {
1759 case GRPC_CALL_ERROR:
1760 return "GRPC_CALL_ERROR";
1761 case GRPC_CALL_ERROR_ALREADY_ACCEPTED:
1762 return "GRPC_CALL_ERROR_ALREADY_ACCEPTED";
1763 case GRPC_CALL_ERROR_ALREADY_FINISHED:
1764 return "GRPC_CALL_ERROR_ALREADY_FINISHED";
1765 case GRPC_CALL_ERROR_ALREADY_INVOKED:
1766 return "GRPC_CALL_ERROR_ALREADY_INVOKED";
1767 case GRPC_CALL_ERROR_BATCH_TOO_BIG:
1768 return "GRPC_CALL_ERROR_BATCH_TOO_BIG";
1769 case GRPC_CALL_ERROR_INVALID_FLAGS:
1770 return "GRPC_CALL_ERROR_INVALID_FLAGS";
1771 case GRPC_CALL_ERROR_INVALID_MESSAGE:
1772 return "GRPC_CALL_ERROR_INVALID_MESSAGE";
1773 case GRPC_CALL_ERROR_INVALID_METADATA:
1774 return "GRPC_CALL_ERROR_INVALID_METADATA";
1775 case GRPC_CALL_ERROR_NOT_INVOKED:
1776 return "GRPC_CALL_ERROR_NOT_INVOKED";
1777 case GRPC_CALL_ERROR_NOT_ON_CLIENT:
1778 return "GRPC_CALL_ERROR_NOT_ON_CLIENT";
1779 case GRPC_CALL_ERROR_NOT_ON_SERVER:
1780 return "GRPC_CALL_ERROR_NOT_ON_SERVER";
1781 case GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE:
1782 return "GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE";
1783 case GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH:
1784 return "GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH";
1785 case GRPC_CALL_ERROR_TOO_MANY_OPERATIONS:
1786 return "GRPC_CALL_ERROR_TOO_MANY_OPERATIONS";
1787 case GRPC_CALL_OK:
1788 return "GRPC_CALL_OK";
Yuchen Zeng2e7d9572016-04-15 17:29:57 -07001789 }
Yuchen Zengf02bada2016-04-19 14:12:27 -07001790 GPR_UNREACHABLE_CODE(return "GRPC_CALL_ERROR_UNKNOW");
Yuchen Zeng2e7d9572016-04-15 17:29:57 -07001791}