Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 | */ |
murgatroid99 | 9030c81 | 2016-09-16 13:25:08 -0700 | [diff] [blame] | 33 | |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 34 | #include <assert.h> |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 35 | #include <limits.h> |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include <grpc/compression.h> |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 41 | #include <grpc/grpc.h> |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 42 | #include <grpc/slice.h> |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 43 | #include <grpc/support/alloc.h> |
| 44 | #include <grpc/support/log.h> |
| 45 | #include <grpc/support/string_util.h> |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 46 | #include <grpc/support/useful.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 48 | #include "src/core/lib/channel/channel_stack.h" |
| 49 | #include "src/core/lib/compression/algorithm_metadata.h" |
| 50 | #include "src/core/lib/iomgr/timer.h" |
| 51 | #include "src/core/lib/profiling/timers.h" |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 52 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 53 | #include "src/core/lib/slice/slice_string_helpers.h" |
Craig Tiller | e7a1702 | 2017-03-13 10:20:38 -0700 | [diff] [blame] | 54 | #include "src/core/lib/support/arena.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 55 | #include "src/core/lib/support/string.h" |
| 56 | #include "src/core/lib/surface/api_trace.h" |
| 57 | #include "src/core/lib/surface/call.h" |
| 58 | #include "src/core/lib/surface/channel.h" |
| 59 | #include "src/core/lib/surface/completion_queue.h" |
Craig Tiller | f2b5b7e | 2017-01-10 08:28:59 -0800 | [diff] [blame] | 60 | #include "src/core/lib/surface/validate_metadata.h" |
Craig Tiller | 732351f | 2016-12-13 16:40:38 -0800 | [diff] [blame] | 61 | #include "src/core/lib/transport/error_utils.h" |
David Garcia Quintas | 73dcbda | 2016-04-23 00:17:05 -0700 | [diff] [blame] | 62 | #include "src/core/lib/transport/metadata.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 63 | #include "src/core/lib/transport/static_metadata.h" |
David Garcia Quintas | 73dcbda | 2016-04-23 00:17:05 -0700 | [diff] [blame] | 64 | #include "src/core/lib/transport/transport.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 66 | /** The maximum number of concurrent batches possible. |
Craig Tiller | 1b01167 | 2015-07-10 10:41:44 -0700 | [diff] [blame] | 67 | Based upon the maximum number of individually queueable ops in the batch |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 68 | api: |
Craig Tiller | 1b01167 | 2015-07-10 10:41:44 -0700 | [diff] [blame] | 69 | - initial metadata send |
| 70 | - message send |
| 71 | - status/close send (depending on client/server) |
| 72 | - initial metadata recv |
| 73 | - message recv |
| 74 | - status/close recv (depending on client/server) */ |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 75 | #define MAX_CONCURRENT_BATCHES 6 |
Craig Tiller | 1b01167 | 2015-07-10 10:41:44 -0700 | [diff] [blame] | 76 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 77 | #define MAX_SEND_EXTRA_METADATA_COUNT 3 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 79 | /* Status data for a request can come from several sources; this |
| 80 | enumerates them all, and acts as a priority sorting for which |
| 81 | status to return to the application - earlier entries override |
| 82 | later ones */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 83 | typedef enum { |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 84 | /* Status came from the application layer overriding whatever |
| 85 | the wire says */ |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 86 | STATUS_FROM_API_OVERRIDE = 0, |
Craig Tiller | daceea8 | 2015-02-02 16:15:53 -0800 | [diff] [blame] | 87 | /* Status came from 'the wire' - or somewhere below the surface |
| 88 | layer */ |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 89 | STATUS_FROM_WIRE, |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 90 | /* Status was created by some internal channel stack operation: must come via |
| 91 | add_batch_error */ |
Craig Tiller | 2aa03df | 2016-03-16 08:24:55 -0700 | [diff] [blame] | 92 | STATUS_FROM_CORE, |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 93 | /* Status was created by some surface error */ |
| 94 | STATUS_FROM_SURFACE, |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 95 | /* Status came from the server sending status */ |
| 96 | STATUS_FROM_SERVER_STATUS, |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 97 | STATUS_SOURCE_COUNT |
| 98 | } status_source; |
| 99 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | typedef struct { |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 101 | bool is_set; |
| 102 | grpc_error *error; |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 103 | } received_status; |
| 104 | |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 105 | static gpr_atm pack_received_status(received_status r) { |
| 106 | return r.is_set ? (1 | (gpr_atm)r.error) : 0; |
| 107 | } |
| 108 | |
| 109 | static received_status unpack_received_status(gpr_atm atm) { |
| 110 | return (atm & 1) == 0 |
| 111 | ? (received_status){.is_set = false, .error = GRPC_ERROR_NONE} |
| 112 | : (received_status){.is_set = true, |
| 113 | .error = (grpc_error *)(atm & ~(gpr_atm)1)}; |
| 114 | } |
| 115 | |
yang-g | 23f777d | 2017-02-22 23:32:26 -0800 | [diff] [blame] | 116 | #define MAX_ERRORS_PER_BATCH 4 |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 117 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 118 | typedef struct batch_control { |
| 119 | grpc_call *call; |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 120 | /* Share memory for cq_completion and notify_tag as they are never needed |
| 121 | simultaneously. Each byte used in this data structure count as six bytes |
Craig Tiller | 7a8232d | 2017-04-03 10:59:42 -0700 | [diff] [blame] | 122 | per call, so any savings we can make are worthwhile, |
| 123 | |
| 124 | We use notify_tag to determine whether or not to send notification to the |
| 125 | completion queue. Once we've made that determination, we can reuse the |
| 126 | memory for cq_completion. */ |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 127 | union { |
| 128 | grpc_cq_completion cq_completion; |
| 129 | struct { |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 130 | /* Any given op indicates completion by either (a) calling a closure or |
| 131 | (b) sending a notification on the call's completion queue. If |
| 132 | \a is_closure is true, \a tag indicates a closure to be invoked; |
| 133 | otherwise, \a tag indicates the tag to be used in the notification to |
| 134 | be sent to the completion queue. */ |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 135 | void *tag; |
| 136 | bool is_closure; |
| 137 | } notify_tag; |
| 138 | } completion_data; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 139 | grpc_closure finish_batch; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 140 | gpr_refcount steps_to_complete; |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 141 | |
| 142 | grpc_error *errors[MAX_ERRORS_PER_BATCH]; |
| 143 | gpr_atm num_errors; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 144 | |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 145 | grpc_transport_stream_op_batch op; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 146 | } batch_control; |
| 147 | |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 148 | typedef struct { |
| 149 | gpr_mu child_list_mu; |
| 150 | grpc_call *first_child; |
| 151 | } parent_call; |
| 152 | |
| 153 | typedef struct { |
| 154 | grpc_call *parent; |
| 155 | /** siblings: children of the same parent form a list, and this list is |
| 156 | protected under |
| 157 | parent->mu */ |
| 158 | grpc_call *sibling_next; |
| 159 | grpc_call *sibling_prev; |
| 160 | } child_call; |
| 161 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | struct grpc_call { |
Craig Tiller | e7a1702 | 2017-03-13 10:20:38 -0700 | [diff] [blame] | 163 | gpr_arena *arena; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 164 | grpc_completion_queue *cq; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 165 | grpc_polling_entity pollent; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | grpc_channel *channel; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 167 | gpr_timespec start_time; |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 168 | /* parent_call* */ gpr_atm parent_call_atm; |
| 169 | child_call *child_call; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 170 | |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 171 | /* client or server call */ |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 172 | bool is_client; |
Craig Tiller | f3fba74 | 2015-06-11 09:36:33 -0700 | [diff] [blame] | 173 | /** has grpc_call_destroy been called */ |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 174 | bool destroy_called; |
Craig Tiller | c7df0df | 2015-08-03 08:06:50 -0700 | [diff] [blame] | 175 | /** flag indicating that cancellation is inherited */ |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 176 | bool cancellation_is_inherited; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 177 | /** which ops are in-flight */ |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 178 | bool sent_initial_metadata; |
| 179 | bool sending_message; |
| 180 | bool sent_final_op; |
| 181 | bool received_initial_metadata; |
| 182 | bool receiving_message; |
| 183 | bool requested_final_op; |
Craig Tiller | b597dcf | 2017-03-09 07:02:11 -0800 | [diff] [blame] | 184 | gpr_atm any_ops_sent_atm; |
| 185 | gpr_atm received_final_op_atm; |
yang-g | 0b6ad7d | 2015-06-25 14:39:01 -0700 | [diff] [blame] | 186 | |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 187 | /* have we received initial metadata */ |
| 188 | bool has_initial_md_been_received; |
| 189 | |
Craig Tiller | b58de72 | 2017-03-29 14:15:12 -0700 | [diff] [blame] | 190 | batch_control *active_batches[MAX_CONCURRENT_BATCHES]; |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 191 | grpc_transport_stream_op_batch_payload stream_op_payload; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 192 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 193 | /* first idx: is_receiving, second idx: is_trailing */ |
| 194 | grpc_metadata_batch metadata_batch[2][2]; |
Craig Tiller | ebf94bf | 2015-02-05 08:48:46 -0800 | [diff] [blame] | 195 | |
Craig Tiller | e5d683c | 2015-02-03 16:37:36 -0800 | [diff] [blame] | 196 | /* Buffered read metadata waiting to be returned to the application. |
| 197 | Element 0 is initial metadata, element 1 is trailing metadata. */ |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 198 | grpc_metadata_array *buffered_metadata[2]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 199 | |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 200 | /* Packed received call statuses from various sources */ |
| 201 | gpr_atm status[STATUS_SOURCE_COUNT]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 202 | |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 203 | /* Call data useful used for reporting. Only valid after the call has |
| 204 | * completed */ |
| 205 | grpc_call_final_info final_info; |
Craig Tiller | 466129e | 2016-03-09 14:43:18 -0800 | [diff] [blame] | 206 | |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 207 | /* Compression algorithm for *incoming* data */ |
| 208 | grpc_compression_algorithm incoming_compression_algorithm; |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 209 | /* Supported encodings (compression algorithms), a bitset */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 210 | uint32_t encodings_accepted_by_peer; |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 211 | |
Julien Boeuf | c6f8d0a | 2015-05-11 22:40:02 -0700 | [diff] [blame] | 212 | /* Contexts for various subsystems (security, tracing, ...). */ |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 213 | grpc_call_context_element context[GRPC_CONTEXT_COUNT]; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 214 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 215 | /* for the client, extra metadata is initial metadata; for the |
| 216 | server, it's trailing metadata */ |
| 217 | grpc_linked_mdelem send_extra_metadata[MAX_SEND_EXTRA_METADATA_COUNT]; |
| 218 | int send_extra_metadata_count; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 219 | gpr_timespec send_deadline; |
| 220 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 221 | grpc_slice_buffer_stream sending_stream; |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 222 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 223 | grpc_byte_stream *receiving_stream; |
| 224 | grpc_byte_buffer **receiving_buffer; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 225 | grpc_slice receiving_slice; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 226 | grpc_closure receiving_slice_ready; |
| 227 | grpc_closure receiving_stream_ready; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 228 | grpc_closure receiving_initial_metadata_ready; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 229 | uint32_t test_only_last_message_flags; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 230 | |
Craig Tiller | e7a1702 | 2017-03-13 10:20:38 -0700 | [diff] [blame] | 231 | grpc_closure release_call; |
| 232 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 233 | union { |
| 234 | struct { |
| 235 | grpc_status_code *status; |
Craig Tiller | 68208fe | 2016-11-14 14:35:02 -0800 | [diff] [blame] | 236 | grpc_slice *status_details; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 237 | } client; |
| 238 | struct { |
| 239 | int *cancelled; |
| 240 | } server; |
| 241 | } final_op; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 242 | |
Craig Tiller | 8a67780 | 2016-04-22 15:07:53 -0700 | [diff] [blame] | 243 | void *saved_receiving_stream_ready_bctlp; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 244 | }; |
| 245 | |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 246 | int grpc_call_error_trace = 0; |
| 247 | |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 248 | #define CALL_STACK_FROM_CALL(call) ((grpc_call_stack *)((call) + 1)) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 249 | #define CALL_FROM_CALL_STACK(call_stack) (((grpc_call *)(call_stack)) - 1) |
| 250 | #define CALL_ELEM_FROM_CALL(call, idx) \ |
| 251 | grpc_call_stack_element(CALL_STACK_FROM_CALL(call), idx) |
| 252 | #define CALL_FROM_TOP_ELEM(top_elem) \ |
| 253 | CALL_FROM_CALL_STACK(grpc_call_stack_from_top_element(top_elem)) |
| 254 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 255 | static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call, |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 256 | grpc_transport_stream_op_batch *op); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 257 | static void cancel_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c, |
| 258 | status_source source, grpc_status_code status, |
| 259 | const char *description); |
Craig Tiller | 255edaa | 2016-12-13 09:04:55 -0800 | [diff] [blame] | 260 | static void cancel_with_error(grpc_exec_ctx *exec_ctx, grpc_call *c, |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 261 | status_source source, grpc_error *error); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 262 | static void destroy_call(grpc_exec_ctx *exec_ctx, void *call_stack, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 263 | grpc_error *error); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 264 | static void receiving_slice_ready(grpc_exec_ctx *exec_ctx, void *bctlp, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 265 | grpc_error *error); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 266 | static void get_final_status(grpc_call *call, |
| 267 | void (*set_value)(grpc_status_code code, |
| 268 | void *user_data), |
| 269 | void *set_value_user_data, grpc_slice *details); |
| 270 | static void set_status_value_directly(grpc_status_code status, void *dest); |
| 271 | static void set_status_from_error(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 272 | status_source source, grpc_error *error); |
| 273 | static void process_data_after_md(grpc_exec_ctx *exec_ctx, batch_control *bctl); |
| 274 | static void post_batch_completion(grpc_exec_ctx *exec_ctx, batch_control *bctl); |
| 275 | static void add_batch_error(grpc_exec_ctx *exec_ctx, batch_control *bctl, |
yang-g | 23f777d | 2017-02-22 23:32:26 -0800 | [diff] [blame] | 276 | grpc_error *error, bool has_cancelled); |
Craig Tiller | bac4142 | 2015-05-29 16:32:28 -0700 | [diff] [blame] | 277 | |
Craig Tiller | f4484cd | 2017-02-01 08:28:40 -0800 | [diff] [blame] | 278 | static void add_init_error(grpc_error **composite, grpc_error *new) { |
| 279 | if (new == GRPC_ERROR_NONE) return; |
| 280 | if (*composite == GRPC_ERROR_NONE) |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 281 | *composite = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Call creation failed"); |
Craig Tiller | f4484cd | 2017-02-01 08:28:40 -0800 | [diff] [blame] | 282 | *composite = grpc_error_add_child(*composite, new); |
| 283 | } |
| 284 | |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 285 | static parent_call *get_or_create_parent_call(grpc_call *call) { |
| 286 | parent_call *p = (parent_call *)gpr_atm_acq_load(&call->parent_call_atm); |
| 287 | if (p == NULL) { |
| 288 | p = gpr_arena_alloc(call->arena, sizeof(*p)); |
| 289 | gpr_mu_init(&p->child_list_mu); |
| 290 | if (!gpr_atm_rel_cas(&call->parent_call_atm, (gpr_atm)NULL, (gpr_atm)p)) { |
| 291 | gpr_mu_destroy(&p->child_list_mu); |
| 292 | p = (parent_call *)gpr_atm_acq_load(&call->parent_call_atm); |
| 293 | } |
| 294 | } |
| 295 | return p; |
| 296 | } |
| 297 | |
| 298 | static parent_call *get_parent_call(grpc_call *call) { |
| 299 | return (parent_call *)gpr_atm_acq_load(&call->parent_call_atm); |
| 300 | } |
| 301 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 302 | grpc_error *grpc_call_create(grpc_exec_ctx *exec_ctx, |
| 303 | const grpc_call_create_args *args, |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 304 | grpc_call **out_call) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 305 | size_t i, j; |
Craig Tiller | f4484cd | 2017-02-01 08:28:40 -0800 | [diff] [blame] | 306 | grpc_error *error = GRPC_ERROR_NONE; |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 307 | grpc_channel_stack *channel_stack = |
| 308 | grpc_channel_get_channel_stack(args->channel); |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 309 | grpc_call *call; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 310 | GPR_TIMER_BEGIN("grpc_call_create", 0); |
Craig Tiller | a6bec8f | 2017-03-14 08:26:04 -0700 | [diff] [blame] | 311 | gpr_arena *arena = |
| 312 | gpr_arena_create(grpc_channel_get_call_size_estimate(args->channel)); |
Craig Tiller | e7a1702 | 2017-03-13 10:20:38 -0700 | [diff] [blame] | 313 | call = gpr_arena_alloc(arena, |
| 314 | sizeof(grpc_call) + channel_stack->call_stack_size); |
| 315 | call->arena = arena; |
Craig Tiller | 0eaed72 | 2016-09-21 10:44:18 -0700 | [diff] [blame] | 316 | *out_call = call; |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 317 | call->channel = args->channel; |
| 318 | call->cq = args->cq; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 319 | call->start_time = gpr_now(GPR_CLOCK_MONOTONIC); |
David Garcia Quintas | 4612337 | 2016-05-09 15:28:42 -0700 | [diff] [blame] | 320 | /* Always support no compression */ |
| 321 | GPR_BITSET(&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE); |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 322 | call->is_client = args->server_transport_data == NULL; |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 323 | call->stream_op_payload.context = call->context; |
Craig Tiller | 4eecdde | 2016-11-14 08:21:17 -0800 | [diff] [blame] | 324 | grpc_slice path = grpc_empty_slice(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 325 | if (call->is_client) { |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 326 | GPR_ASSERT(args->add_initial_metadata_count < |
| 327 | MAX_SEND_EXTRA_METADATA_COUNT); |
| 328 | for (i = 0; i < args->add_initial_metadata_count; i++) { |
| 329 | call->send_extra_metadata[i].md = args->add_initial_metadata[i]; |
Craig Tiller | 3b05e1d | 2016-11-21 13:46:31 -0800 | [diff] [blame] | 330 | if (grpc_slice_eq(GRPC_MDKEY(args->add_initial_metadata[i]), |
| 331 | GRPC_MDSTR_PATH)) { |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 332 | path = grpc_slice_ref_internal( |
| 333 | GRPC_MDVALUE(args->add_initial_metadata[i])); |
Mark D. Roth | aa850a7 | 2016-09-26 13:38:02 -0700 | [diff] [blame] | 334 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 335 | } |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 336 | call->send_extra_metadata_count = (int)args->add_initial_metadata_count; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 337 | } else { |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 338 | GPR_ASSERT(args->add_initial_metadata_count == 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 339 | call->send_extra_metadata_count = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 340 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 341 | for (i = 0; i < 2; i++) { |
| 342 | for (j = 0; j < 2; j++) { |
| 343 | call->metadata_batch[i][j].deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC); |
| 344 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 345 | } |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 346 | gpr_timespec send_deadline = |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 347 | gpr_convert_clock_type(args->send_deadline, GPR_CLOCK_MONOTONIC); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 348 | |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 349 | if (args->parent_call != NULL) { |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 350 | child_call *cc = call->child_call = |
| 351 | gpr_arena_alloc(arena, sizeof(child_call)); |
| 352 | call->child_call->parent = args->parent_call; |
| 353 | |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 354 | GRPC_CALL_INTERNAL_REF(args->parent_call, "child"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 355 | GPR_ASSERT(call->is_client); |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 356 | GPR_ASSERT(!args->parent_call->is_client); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 357 | |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 358 | parent_call *pc = get_or_create_parent_call(args->parent_call); |
| 359 | |
| 360 | gpr_mu_lock(&pc->child_list_mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 361 | |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 362 | if (args->propagation_mask & GRPC_PROPAGATE_DEADLINE) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 363 | send_deadline = gpr_time_min( |
| 364 | gpr_convert_clock_type(send_deadline, |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 365 | args->parent_call->send_deadline.clock_type), |
| 366 | args->parent_call->send_deadline); |
Craig Tiller | c7df0df | 2015-08-03 08:06:50 -0700 | [diff] [blame] | 367 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 368 | /* for now GRPC_PROPAGATE_TRACING_CONTEXT *MUST* be passed with |
| 369 | * GRPC_PROPAGATE_STATS_CONTEXT */ |
| 370 | /* TODO(ctiller): This should change to use the appropriate census start_op |
| 371 | * call. */ |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 372 | if (args->propagation_mask & GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT) { |
Craig Tiller | 239af8b | 2017-02-01 10:21:42 -0800 | [diff] [blame] | 373 | if (0 == (args->propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT)) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 374 | add_init_error(&error, GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 375 | "Census tracing propagation requested " |
| 376 | "without Census context propagation")); |
Craig Tiller | 239af8b | 2017-02-01 10:21:42 -0800 | [diff] [blame] | 377 | } |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 378 | grpc_call_context_set( |
| 379 | call, GRPC_CONTEXT_TRACING, |
| 380 | args->parent_call->context[GRPC_CONTEXT_TRACING].value, NULL); |
Craig Tiller | f20d307 | 2017-02-01 10:39:26 -0800 | [diff] [blame] | 381 | } else if (args->propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 382 | add_init_error(&error, GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 383 | "Census context propagation requested " |
| 384 | "without Census tracing propagation")); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 385 | } |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 386 | if (args->propagation_mask & GRPC_PROPAGATE_CANCELLATION) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 387 | call->cancellation_is_inherited = 1; |
Craig Tiller | 123c72b | 2017-03-10 07:33:27 -0800 | [diff] [blame] | 388 | if (gpr_atm_acq_load(&args->parent_call->received_final_op_atm)) { |
| 389 | cancel_with_error(exec_ctx, call, STATUS_FROM_API_OVERRIDE, |
| 390 | GRPC_ERROR_CANCELLED); |
| 391 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 392 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 393 | |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 394 | if (pc->first_child == NULL) { |
| 395 | pc->first_child = call; |
| 396 | cc->sibling_next = cc->sibling_prev = call; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 397 | } else { |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 398 | cc->sibling_next = pc->first_child; |
| 399 | cc->sibling_prev = pc->first_child->child_call->sibling_prev; |
| 400 | cc->sibling_next->child_call->sibling_prev = |
| 401 | cc->sibling_prev->child_call->sibling_next = call; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 404 | gpr_mu_unlock(&pc->child_list_mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 405 | } |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 406 | |
Mark D. Roth | 14c072c | 2016-08-26 08:31:34 -0700 | [diff] [blame] | 407 | call->send_deadline = send_deadline; |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 408 | |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 409 | GRPC_CHANNEL_INTERNAL_REF(args->channel, "call"); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 410 | /* initial refcount dropped by grpc_call_destroy */ |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame] | 411 | grpc_call_element_args call_args = { |
| 412 | .call_stack = CALL_STACK_FROM_CALL(call), |
| 413 | .server_transport_data = args->server_transport_data, |
| 414 | .context = call->context, |
| 415 | .path = path, |
| 416 | .start_time = call->start_time, |
| 417 | .deadline = send_deadline, |
| 418 | .arena = call->arena}; |
Craig Tiller | f4484cd | 2017-02-01 08:28:40 -0800 | [diff] [blame] | 419 | add_init_error(&error, grpc_call_stack_init(exec_ctx, channel_stack, 1, |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame] | 420 | destroy_call, call, &call_args)); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 421 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 422 | cancel_with_error(exec_ctx, call, STATUS_FROM_SURFACE, |
| 423 | GRPC_ERROR_REF(error)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 424 | } |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 425 | if (args->cq != NULL) { |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 426 | GPR_ASSERT( |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 427 | args->pollset_set_alternative == NULL && |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 428 | "Only one of 'cq' and 'pollset_set_alternative' should be non-NULL."); |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 429 | GRPC_CQ_INTERNAL_REF(args->cq, "bind"); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 430 | call->pollent = |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 431 | grpc_polling_entity_create_from_pollset(grpc_cq_pollset(args->cq)); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 432 | } |
Craig Tiller | ca3451d | 2016-09-29 10:27:44 -0700 | [diff] [blame] | 433 | if (args->pollset_set_alternative != NULL) { |
| 434 | call->pollent = grpc_polling_entity_create_from_pollset_set( |
| 435 | args->pollset_set_alternative); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 436 | } |
| 437 | if (!grpc_polling_entity_is_empty(&call->pollent)) { |
| 438 | grpc_call_stack_set_pollset_or_pollset_set( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 439 | exec_ctx, CALL_STACK_FROM_CALL(call), &call->pollent); |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Craig Tiller | 4eecdde | 2016-11-14 08:21:17 -0800 | [diff] [blame] | 442 | grpc_slice_unref_internal(exec_ctx, path); |
Mark D. Roth | aa850a7 | 2016-09-26 13:38:02 -0700 | [diff] [blame] | 443 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 444 | GPR_TIMER_END("grpc_call_create", 0); |
Craig Tiller | 8e21465 | 2016-08-19 09:54:31 -0700 | [diff] [blame] | 445 | return error; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 448 | void grpc_call_set_completion_queue(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 449 | grpc_completion_queue *cq) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 450 | GPR_ASSERT(cq); |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 451 | |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 452 | if (grpc_polling_entity_pollset_set(&call->pollent) != NULL) { |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 453 | gpr_log(GPR_ERROR, "A pollset_set is already registered for this call."); |
| 454 | abort(); |
| 455 | } |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 456 | call->cq = cq; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 457 | GRPC_CQ_INTERNAL_REF(cq, "bind"); |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 458 | call->pollent = grpc_polling_entity_create_from_pollset(grpc_cq_pollset(cq)); |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 459 | grpc_call_stack_set_pollset_or_pollset_set( |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 460 | exec_ctx, CALL_STACK_FROM_CALL(call), &call->pollent); |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 463 | #ifdef GRPC_STREAM_REFCOUNT_DEBUG |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 464 | #define REF_REASON reason |
| 465 | #define REF_ARG , const char *reason |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 466 | #else |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 467 | #define REF_REASON "" |
| 468 | #define REF_ARG |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 469 | #endif |
Craig Tiller | 7b43561 | 2015-11-24 08:15:05 -0800 | [diff] [blame] | 470 | void grpc_call_internal_ref(grpc_call *c REF_ARG) { |
| 471 | GRPC_CALL_STACK_REF(CALL_STACK_FROM_CALL(c), REF_REASON); |
| 472 | } |
| 473 | void grpc_call_internal_unref(grpc_exec_ctx *exec_ctx, grpc_call *c REF_ARG) { |
| 474 | GRPC_CALL_STACK_UNREF(exec_ctx, CALL_STACK_FROM_CALL(c), REF_REASON); |
| 475 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 476 | |
Craig Tiller | e7a1702 | 2017-03-13 10:20:38 -0700 | [diff] [blame] | 477 | static void release_call(grpc_exec_ctx *exec_ctx, void *call, |
| 478 | grpc_error *error) { |
| 479 | grpc_call *c = call; |
Craig Tiller | 51006fe | 2017-03-15 08:07:02 -0700 | [diff] [blame] | 480 | grpc_channel *channel = c->channel; |
Craig Tiller | dbad370 | 2017-03-15 08:21:19 -0700 | [diff] [blame] | 481 | grpc_channel_update_call_size_estimate(channel, gpr_arena_destroy(c->arena)); |
Craig Tiller | 51006fe | 2017-03-15 08:07:02 -0700 | [diff] [blame] | 482 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, channel, "call"); |
Craig Tiller | e7a1702 | 2017-03-13 10:20:38 -0700 | [diff] [blame] | 483 | } |
| 484 | |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 485 | static void set_status_value_directly(grpc_status_code status, void *dest); |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 486 | static void destroy_call(grpc_exec_ctx *exec_ctx, void *call, |
| 487 | grpc_error *error) { |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 488 | size_t i; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 489 | int ii; |
Craig Tiller | aef25da | 2015-01-29 17:19:45 -0800 | [diff] [blame] | 490 | grpc_call *c = call; |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 491 | GPR_TIMER_BEGIN("destroy_call", 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 492 | for (i = 0; i < 2; i++) { |
| 493 | grpc_metadata_batch_destroy( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 494 | exec_ctx, &c->metadata_batch[1 /* is_receiving */][i /* is_initial */]); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 495 | } |
| 496 | if (c->receiving_stream != NULL) { |
Craig Tiller | 3b66ab9 | 2015-12-09 19:42:22 -0800 | [diff] [blame] | 497 | grpc_byte_stream_destroy(exec_ctx, c->receiving_stream); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 498 | } |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 499 | parent_call *pc = get_parent_call(c); |
| 500 | if (pc != NULL) { |
| 501 | gpr_mu_destroy(&pc->child_list_mu); |
| 502 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 503 | for (ii = 0; ii < c->send_extra_metadata_count; ii++) { |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 504 | GRPC_MDELEM_UNREF(exec_ctx, c->send_extra_metadata[ii].md); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 505 | } |
| 506 | for (i = 0; i < GRPC_CONTEXT_COUNT; i++) { |
| 507 | if (c->context[i].destroy) { |
| 508 | c->context[i].destroy(c->context[i].value); |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 509 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 510 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 511 | if (c->cq) { |
| 512 | GRPC_CQ_INTERNAL_UNREF(c->cq, "bind"); |
| 513 | } |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 514 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 515 | get_final_status(call, set_status_value_directly, &c->final_info.final_status, |
| 516 | NULL); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 517 | c->final_info.stats.latency = |
| 518 | gpr_time_sub(gpr_now(GPR_CLOCK_MONOTONIC), c->start_time); |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 519 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 520 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 521 | GRPC_ERROR_UNREF( |
Craig Tiller | b597dcf | 2017-03-09 07:02:11 -0800 | [diff] [blame] | 522 | unpack_received_status(gpr_atm_acq_load(&c->status[i])).error); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Craig Tiller | e7a1702 | 2017-03-13 10:20:38 -0700 | [diff] [blame] | 525 | grpc_call_stack_destroy(exec_ctx, CALL_STACK_FROM_CALL(c), &c->final_info, |
| 526 | grpc_closure_init(&c->release_call, release_call, c, |
| 527 | grpc_schedule_on_exec_ctx)); |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 528 | GPR_TIMER_END("destroy_call", 0); |
Craig Tiller | a454110 | 2015-01-29 11:46:11 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 531 | void grpc_call_destroy(grpc_call *c) { |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 532 | child_call *cc = c->child_call; |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 533 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | b8d3a31 | 2015-06-19 17:27:53 -0700 | [diff] [blame] | 534 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 535 | GPR_TIMER_BEGIN("grpc_call_destroy", 0); |
| 536 | GRPC_API_TRACE("grpc_call_destroy(c=%p)", 1, (c)); |
| 537 | |
Craig Tiller | 9fd9a44 | 2017-04-05 16:52:34 -0700 | [diff] [blame^] | 538 | if (cc) { |
| 539 | parent_call *pc = get_parent_call(cc->parent); |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 540 | gpr_mu_lock(&pc->child_list_mu); |
| 541 | if (c == pc->first_child) { |
| 542 | pc->first_child = cc->sibling_next; |
| 543 | if (c == pc->first_child) { |
| 544 | pc->first_child = NULL; |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 545 | } |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 546 | } |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 547 | cc->sibling_prev->child_call->sibling_next = cc->sibling_next; |
| 548 | cc->sibling_next->child_call->sibling_prev = cc->sibling_prev; |
| 549 | gpr_mu_unlock(&pc->child_list_mu); |
| 550 | GRPC_CALL_INTERNAL_UNREF(&exec_ctx, cc->parent, "child"); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 553 | GPR_ASSERT(!c->destroy_called); |
| 554 | c->destroy_called = 1; |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 555 | bool cancel = gpr_atm_acq_load(&c->any_ops_sent_atm) != 0 && |
| 556 | gpr_atm_acq_load(&c->received_final_op_atm) == 0; |
Craig Tiller | 37cbc3f | 2017-02-16 14:54:55 -0800 | [diff] [blame] | 557 | if (cancel) { |
| 558 | cancel_with_error(&exec_ctx, c, STATUS_FROM_API_OVERRIDE, |
| 559 | GRPC_ERROR_CANCELLED); |
| 560 | } |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 561 | GRPC_CALL_INTERNAL_UNREF(&exec_ctx, c, "destroy"); |
| 562 | grpc_exec_ctx_finish(&exec_ctx); |
| 563 | GPR_TIMER_END("grpc_call_destroy", 0); |
Craig Tiller | f0f70a8 | 2016-06-23 13:55:06 -0700 | [diff] [blame] | 564 | } |
Craig Tiller | 3054756 | 2015-02-05 17:04:51 -0800 | [diff] [blame] | 565 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 566 | grpc_call_error grpc_call_cancel(grpc_call *call, void *reserved) { |
| 567 | GRPC_API_TRACE("grpc_call_cancel(call=%p, reserved=%p)", 2, (call, reserved)); |
| 568 | GPR_ASSERT(!reserved); |
Craig Tiller | 37cbc3f | 2017-02-16 14:54:55 -0800 | [diff] [blame] | 569 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 570 | cancel_with_error(&exec_ctx, call, STATUS_FROM_API_OVERRIDE, |
| 571 | GRPC_ERROR_CANCELLED); |
| 572 | grpc_exec_ctx_finish(&exec_ctx); |
| 573 | return GRPC_CALL_OK; |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call, |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 577 | grpc_transport_stream_op_batch *op) { |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 578 | grpc_call_element *elem; |
| 579 | |
| 580 | GPR_TIMER_BEGIN("execute_op", 0); |
| 581 | elem = CALL_ELEM_FROM_CALL(call, 0); |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 582 | elem->filter->start_transport_stream_op_batch(exec_ctx, elem, op); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 583 | GPR_TIMER_END("execute_op", 0); |
| 584 | } |
| 585 | |
| 586 | char *grpc_call_get_peer(grpc_call *call) { |
| 587 | grpc_call_element *elem = CALL_ELEM_FROM_CALL(call, 0); |
| 588 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 589 | char *result; |
| 590 | GRPC_API_TRACE("grpc_call_get_peer(%p)", 1, (call)); |
| 591 | result = elem->filter->get_peer(&exec_ctx, elem); |
| 592 | if (result == NULL) { |
| 593 | result = grpc_channel_get_target(call->channel); |
| 594 | } |
| 595 | if (result == NULL) { |
| 596 | result = gpr_strdup("unknown"); |
| 597 | } |
| 598 | grpc_exec_ctx_finish(&exec_ctx); |
| 599 | return result; |
| 600 | } |
| 601 | |
| 602 | grpc_call *grpc_call_from_top_element(grpc_call_element *elem) { |
| 603 | return CALL_FROM_TOP_ELEM(elem); |
| 604 | } |
| 605 | |
| 606 | /******************************************************************************* |
| 607 | * CANCELLATION |
| 608 | */ |
| 609 | |
| 610 | grpc_call_error grpc_call_cancel_with_status(grpc_call *c, |
| 611 | grpc_status_code status, |
| 612 | const char *description, |
| 613 | void *reserved) { |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 614 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 615 | GRPC_API_TRACE( |
| 616 | "grpc_call_cancel_with_status(" |
| 617 | "c=%p, status=%d, description=%s, reserved=%p)", |
| 618 | 4, (c, (int)status, description, reserved)); |
| 619 | GPR_ASSERT(reserved == NULL); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 620 | cancel_with_status(&exec_ctx, c, STATUS_FROM_API_OVERRIDE, status, |
| 621 | description); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 622 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 623 | return GRPC_CALL_OK; |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Craig Tiller | c5b90df | 2017-03-10 16:11:08 -0800 | [diff] [blame] | 626 | static void done_termination(grpc_exec_ctx *exec_ctx, void *call, |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 627 | grpc_error *error) { |
Craig Tiller | c5b90df | 2017-03-10 16:11:08 -0800 | [diff] [blame] | 628 | GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "termination"); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Craig Tiller | 255edaa | 2016-12-13 09:04:55 -0800 | [diff] [blame] | 631 | static void cancel_with_error(grpc_exec_ctx *exec_ctx, grpc_call *c, |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 632 | status_source source, grpc_error *error) { |
Craig Tiller | c5b90df | 2017-03-10 16:11:08 -0800 | [diff] [blame] | 633 | GRPC_CALL_INTERNAL_REF(c, "termination"); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 634 | set_status_from_error(exec_ctx, c, source, GRPC_ERROR_REF(error)); |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 635 | grpc_transport_stream_op_batch *op = grpc_make_transport_stream_op( |
Craig Tiller | c5b90df | 2017-03-10 16:11:08 -0800 | [diff] [blame] | 636 | grpc_closure_create(done_termination, c, grpc_schedule_on_exec_ctx)); |
Craig Tiller | 22b182b | 2017-03-16 10:56:58 -0700 | [diff] [blame] | 637 | op->cancel_stream = true; |
| 638 | op->payload->cancel_stream.cancel_error = error; |
Craig Tiller | c5b90df | 2017-03-10 16:11:08 -0800 | [diff] [blame] | 639 | execute_op(exec_ctx, c, op); |
Craig Tiller | 255edaa | 2016-12-13 09:04:55 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 642 | static grpc_error *error_from_status(grpc_status_code status, |
| 643 | const char *description) { |
| 644 | return grpc_error_set_int( |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 645 | grpc_error_set_str(GRPC_ERROR_CREATE_FROM_COPIED_STRING(description), |
| 646 | GRPC_ERROR_STR_GRPC_MESSAGE, |
| 647 | grpc_slice_from_copied_string(description)), |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 648 | GRPC_ERROR_INT_GRPC_STATUS, status); |
| 649 | } |
| 650 | |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 651 | static void cancel_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c, |
| 652 | status_source source, grpc_status_code status, |
| 653 | const char *description) { |
| 654 | cancel_with_error(exec_ctx, c, source, |
| 655 | error_from_status(status, description)); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /******************************************************************************* |
| 659 | * FINAL STATUS CODE MANIPULATION |
| 660 | */ |
| 661 | |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 662 | static bool get_final_status_from( |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 663 | grpc_call *call, grpc_error *error, bool allow_ok_status, |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 664 | void (*set_value)(grpc_status_code code, void *user_data), |
| 665 | void *set_value_user_data, grpc_slice *details) { |
Craig Tiller | 737b625 | 2017-01-09 15:25:15 -0800 | [diff] [blame] | 666 | grpc_status_code code; |
ncteisen | eb2b115 | 2017-03-28 15:27:27 -0700 | [diff] [blame] | 667 | grpc_slice slice = grpc_empty_slice(); |
ncteisen | bbb3801 | 2017-03-10 14:58:43 -0800 | [diff] [blame] | 668 | grpc_error_get_status(error, call->send_deadline, &code, &slice, NULL); |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 669 | if (code == GRPC_STATUS_OK && !allow_ok_status) { |
| 670 | return false; |
| 671 | } |
Craig Tiller | 737b625 | 2017-01-09 15:25:15 -0800 | [diff] [blame] | 672 | |
| 673 | set_value(code, set_value_user_data); |
| 674 | if (details != NULL) { |
ncteisen | bbb3801 | 2017-03-10 14:58:43 -0800 | [diff] [blame] | 675 | *details = grpc_slice_ref_internal(slice); |
Craig Tiller | 737b625 | 2017-01-09 15:25:15 -0800 | [diff] [blame] | 676 | } |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 677 | return true; |
Craig Tiller | 737b625 | 2017-01-09 15:25:15 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 680 | static void get_final_status(grpc_call *call, |
| 681 | void (*set_value)(grpc_status_code code, |
| 682 | void *user_data), |
| 683 | void *set_value_user_data, grpc_slice *details) { |
| 684 | int i; |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 685 | received_status status[STATUS_SOURCE_COUNT]; |
| 686 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
| 687 | status[i] = unpack_received_status(gpr_atm_acq_load(&call->status[i])); |
| 688 | } |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 689 | if (grpc_call_error_trace) { |
| 690 | gpr_log(GPR_DEBUG, "get_final_status %s", call->is_client ? "CLI" : "SVR"); |
| 691 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 692 | if (status[i].is_set) { |
| 693 | gpr_log(GPR_DEBUG, " %d: %s", i, grpc_error_string(status[i].error)); |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 694 | } |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 695 | } |
| 696 | } |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 697 | /* first search through ignoring "OK" statuses: if something went wrong, |
| 698 | * ensure we report it */ |
| 699 | for (int allow_ok_status = 0; allow_ok_status < 2; allow_ok_status++) { |
| 700 | /* search for the best status we can present: ideally the error we use has a |
| 701 | clearly defined grpc-status, and we'll prefer that. */ |
| 702 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 703 | if (status[i].is_set && |
| 704 | grpc_error_has_clear_grpc_status(status[i].error)) { |
| 705 | if (get_final_status_from(call, status[i].error, allow_ok_status != 0, |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 706 | set_value, set_value_user_data, details)) { |
| 707 | return; |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | /* If no clearly defined status exists, search for 'anything' */ |
| 712 | for (i = 0; i < STATUS_SOURCE_COUNT; i++) { |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 713 | if (status[i].is_set) { |
| 714 | if (get_final_status_from(call, status[i].error, allow_ok_status != 0, |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 715 | set_value, set_value_user_data, details)) { |
| 716 | return; |
| 717 | } |
| 718 | } |
Craig Tiller | 737b625 | 2017-01-09 15:25:15 -0800 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | /* If nothing exists, set some default */ |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 722 | if (call->is_client) { |
| 723 | set_value(GRPC_STATUS_UNKNOWN, set_value_user_data); |
Craig Tiller | be1b9a7 | 2016-06-24 13:22:11 -0700 | [diff] [blame] | 724 | } else { |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 725 | set_value(GRPC_STATUS_OK, set_value_user_data); |
Craig Tiller | f0f70a8 | 2016-06-23 13:55:06 -0700 | [diff] [blame] | 726 | } |
Craig Tiller | f0f70a8 | 2016-06-23 13:55:06 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 729 | static void set_status_from_error(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 730 | status_source source, grpc_error *error) { |
Craig Tiller | 4bab946 | 2017-02-22 08:56:02 -0800 | [diff] [blame] | 731 | if (!gpr_atm_rel_cas(&call->status[source], |
| 732 | pack_received_status((received_status){ |
| 733 | .is_set = false, .error = GRPC_ERROR_NONE}), |
| 734 | pack_received_status((received_status){ |
| 735 | .is_set = true, .error = error}))) { |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 736 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 737 | } |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 738 | } |
| 739 | |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 740 | /******************************************************************************* |
| 741 | * COMPRESSION |
| 742 | */ |
| 743 | |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 744 | static void set_incoming_compression_algorithm( |
| 745 | grpc_call *call, grpc_compression_algorithm algo) { |
David Garcia Quintas | 303d308 | 2016-05-05 18:25:34 -0700 | [diff] [blame] | 746 | GPR_ASSERT(algo < GRPC_COMPRESS_ALGORITHMS_COUNT); |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 747 | call->incoming_compression_algorithm = algo; |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 748 | } |
| 749 | |
David Garcia Quintas | 0c33188 | 2015-10-08 14:51:54 -0700 | [diff] [blame] | 750 | grpc_compression_algorithm grpc_call_test_only_get_compression_algorithm( |
David Garcia Quintas | 64824be | 2015-10-06 19:45:36 -0700 | [diff] [blame] | 751 | grpc_call *call) { |
| 752 | grpc_compression_algorithm algorithm; |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 753 | algorithm = call->incoming_compression_algorithm; |
David Garcia Quintas | 64824be | 2015-10-06 19:45:36 -0700 | [diff] [blame] | 754 | return algorithm; |
David Garcia Quintas | 7c0d914 | 2015-07-23 04:58:20 -0700 | [diff] [blame] | 755 | } |
| 756 | |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 757 | static grpc_compression_algorithm compression_algorithm_for_level_locked( |
| 758 | grpc_call *call, grpc_compression_level level) { |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 759 | return grpc_compression_algorithm_for_level(level, |
| 760 | call->encodings_accepted_by_peer); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 763 | uint32_t grpc_call_test_only_get_message_flags(grpc_call *call) { |
| 764 | uint32_t flags; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 765 | flags = call->test_only_last_message_flags; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 766 | return flags; |
| 767 | } |
| 768 | |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 769 | static void destroy_encodings_accepted_by_peer(void *p) { return; } |
| 770 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 771 | static void set_encodings_accepted_by_peer(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 772 | grpc_call *call, grpc_mdelem mdel) { |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 773 | size_t i; |
| 774 | grpc_compression_algorithm algorithm; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 775 | grpc_slice_buffer accept_encoding_parts; |
| 776 | grpc_slice accept_encoding_slice; |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 777 | void *accepted_user_data; |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 778 | |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 779 | accepted_user_data = |
| 780 | grpc_mdelem_get_user_data(mdel, destroy_encodings_accepted_by_peer); |
| 781 | if (accepted_user_data != NULL) { |
| 782 | call->encodings_accepted_by_peer = |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 783 | (uint32_t)(((uintptr_t)accepted_user_data) - 1); |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 784 | return; |
| 785 | } |
| 786 | |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 787 | accept_encoding_slice = GRPC_MDVALUE(mdel); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 788 | grpc_slice_buffer_init(&accept_encoding_parts); |
| 789 | grpc_slice_split(accept_encoding_slice, ",", &accept_encoding_parts); |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 790 | |
David Garcia Quintas | e091af8 | 2015-07-15 21:37:02 -0700 | [diff] [blame] | 791 | /* No need to zero call->encodings_accepted_by_peer: grpc_call_create already |
| 792 | * zeroes the whole grpc_call */ |
David Garcia Quintas | b1866bd | 2015-07-08 22:37:01 -0700 | [diff] [blame] | 793 | /* Always support no compression */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 794 | GPR_BITSET(&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE); |
| 795 | for (i = 0; i < accept_encoding_parts.count; i++) { |
Craig Tiller | 68208fe | 2016-11-14 14:35:02 -0800 | [diff] [blame] | 796 | grpc_slice accept_encoding_entry_slice = accept_encoding_parts.slices[i]; |
| 797 | if (grpc_compression_algorithm_parse(accept_encoding_entry_slice, |
| 798 | &algorithm)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 799 | GPR_BITSET(&call->encodings_accepted_by_peer, algorithm); |
| 800 | } else { |
| 801 | char *accept_encoding_entry_str = |
Craig Tiller | b4aa70e | 2016-12-09 09:40:11 -0800 | [diff] [blame] | 802 | grpc_slice_to_c_string(accept_encoding_entry_slice); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 803 | gpr_log(GPR_ERROR, |
| 804 | "Invalid entry in accept encoding metadata: '%s'. Ignoring.", |
| 805 | accept_encoding_entry_str); |
| 806 | gpr_free(accept_encoding_entry_str); |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 807 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 808 | } |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 809 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 810 | grpc_slice_buffer_destroy_internal(exec_ctx, &accept_encoding_parts); |
Craig Tiller | 3ff2754 | 2015-10-09 15:39:44 -0700 | [diff] [blame] | 811 | |
| 812 | grpc_mdelem_set_user_data( |
| 813 | mdel, destroy_encodings_accepted_by_peer, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 814 | (void *)(((uintptr_t)call->encodings_accepted_by_peer) + 1)); |
David Garcia Quintas | b8edf7e | 2015-07-08 20:18:57 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 817 | uint32_t grpc_call_test_only_get_encodings_accepted_by_peer(grpc_call *call) { |
| 818 | uint32_t encodings_accepted_by_peer; |
David Garcia Quintas | 0c33188 | 2015-10-08 14:51:54 -0700 | [diff] [blame] | 819 | encodings_accepted_by_peer = call->encodings_accepted_by_peer; |
David Garcia Quintas | 0c33188 | 2015-10-08 14:51:54 -0700 | [diff] [blame] | 820 | return encodings_accepted_by_peer; |
Craig Tiller | 6875272 | 2015-01-29 14:59:54 -0800 | [diff] [blame] | 821 | } |
| 822 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 823 | static grpc_linked_mdelem *linked_from_md(grpc_metadata *md) { |
| 824 | return (grpc_linked_mdelem *)&md->internal_data; |
Craig Tiller | c12fee6 | 2015-02-03 11:55:50 -0800 | [diff] [blame] | 825 | } |
| 826 | |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 827 | static grpc_metadata *get_md_elem(grpc_metadata *metadata, |
| 828 | grpc_metadata *additional_metadata, int i, |
| 829 | int count) { |
| 830 | grpc_metadata *res = |
| 831 | i < count ? &metadata[i] : &additional_metadata[i - count]; |
| 832 | GPR_ASSERT(res); |
| 833 | return res; |
| 834 | } |
| 835 | |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 836 | static int prepare_application_metadata( |
| 837 | grpc_exec_ctx *exec_ctx, grpc_call *call, int count, |
| 838 | grpc_metadata *metadata, int is_trailing, int prepend_extra_metadata, |
| 839 | grpc_metadata *additional_metadata, int additional_metadata_count) { |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 840 | int total_count = count + additional_metadata_count; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 841 | int i; |
| 842 | grpc_metadata_batch *batch = |
| 843 | &call->metadata_batch[0 /* is_receiving */][is_trailing]; |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 844 | for (i = 0; i < total_count; i++) { |
| 845 | const grpc_metadata *md = |
| 846 | get_md_elem(metadata, additional_metadata, i, count); |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 847 | grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data; |
| 848 | GPR_ASSERT(sizeof(grpc_linked_mdelem) == sizeof(md->internal_data)); |
Craig Tiller | f2b5b7e | 2017-01-10 08:28:59 -0800 | [diff] [blame] | 849 | if (!GRPC_LOG_IF_ERROR("validate_metadata", |
| 850 | grpc_validate_header_key_is_legal(md->key))) { |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 851 | break; |
Craig Tiller | df2d922 | 2016-11-18 16:38:57 -0800 | [diff] [blame] | 852 | } else if (!grpc_is_binary_header(md->key) && |
Craig Tiller | f2b5b7e | 2017-01-10 08:28:59 -0800 | [diff] [blame] | 853 | !GRPC_LOG_IF_ERROR( |
| 854 | "validate_metadata", |
| 855 | grpc_validate_header_nonbin_value_is_legal(md->value))) { |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 856 | break; |
| 857 | } |
Craig Tiller | 1282a67 | 2016-11-18 14:57:53 -0800 | [diff] [blame] | 858 | l->md = grpc_mdelem_from_grpc_metadata(exec_ctx, (grpc_metadata *)md); |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 859 | } |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 860 | if (i != total_count) { |
Craig Tiller | 5ae3ffb | 2016-11-18 14:58:32 -0800 | [diff] [blame] | 861 | for (int j = 0; j < i; j++) { |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 862 | const grpc_metadata *md = |
| 863 | get_md_elem(metadata, additional_metadata, j, count); |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 864 | grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 865 | GRPC_MDELEM_UNREF(exec_ctx, l->md); |
Craig Tiller | b42445c | 2016-04-22 13:11:44 -0700 | [diff] [blame] | 866 | } |
| 867 | return 0; |
| 868 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 869 | if (prepend_extra_metadata) { |
| 870 | if (call->send_extra_metadata_count == 0) { |
| 871 | prepend_extra_metadata = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 872 | } else { |
Craig Tiller | 0960818 | 2016-11-22 15:43:56 -0800 | [diff] [blame] | 873 | for (i = 0; i < call->send_extra_metadata_count; i++) { |
| 874 | GRPC_LOG_IF_ERROR("prepare_application_metadata", |
| 875 | grpc_metadata_batch_link_tail( |
Craig Tiller | 9277aa7 | 2017-01-11 14:15:38 -0800 | [diff] [blame] | 876 | exec_ctx, batch, &call->send_extra_metadata[i])); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 877 | } |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 878 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 879 | } |
Craig Tiller | 0960818 | 2016-11-22 15:43:56 -0800 | [diff] [blame] | 880 | for (i = 0; i < total_count; i++) { |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 881 | grpc_metadata *md = get_md_elem(metadata, additional_metadata, i, count); |
Craig Tiller | 9277aa7 | 2017-01-11 14:15:38 -0800 | [diff] [blame] | 882 | GRPC_LOG_IF_ERROR( |
| 883 | "prepare_application_metadata", |
| 884 | grpc_metadata_batch_link_tail(exec_ctx, batch, linked_from_md(md))); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 885 | } |
Craig Tiller | 0960818 | 2016-11-22 15:43:56 -0800 | [diff] [blame] | 886 | call->send_extra_metadata_count = 0; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 887 | |
Craig Tiller | b96d001 | 2015-05-06 15:33:23 -0700 | [diff] [blame] | 888 | return 1; |
| 889 | } |
| 890 | |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 891 | /* we offset status by a small amount when storing it into transport metadata |
| 892 | as metadata cannot store a 0 value (which is used as OK for grpc_status_codes |
| 893 | */ |
| 894 | #define STATUS_OFFSET 1 |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 895 | static void destroy_status(void *ignored) {} |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 896 | |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 897 | static uint32_t decode_status(grpc_mdelem md) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 898 | uint32_t status; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 899 | void *user_data; |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 900 | if (grpc_mdelem_eq(md, GRPC_MDELEM_GRPC_STATUS_0)) return 0; |
| 901 | if (grpc_mdelem_eq(md, GRPC_MDELEM_GRPC_STATUS_1)) return 1; |
| 902 | if (grpc_mdelem_eq(md, GRPC_MDELEM_GRPC_STATUS_2)) return 2; |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 903 | user_data = grpc_mdelem_get_user_data(md, destroy_status); |
| 904 | if (user_data != NULL) { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 905 | status = ((uint32_t)(intptr_t)user_data) - STATUS_OFFSET; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 906 | } else { |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 907 | if (!grpc_parse_slice_to_uint32(GRPC_MDVALUE(md), &status)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 908 | status = GRPC_STATUS_UNKNOWN; /* could not parse status code */ |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 909 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 910 | grpc_mdelem_set_user_data(md, destroy_status, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 911 | (void *)(intptr_t)(status + STATUS_OFFSET)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 912 | } |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 913 | return status; |
| 914 | } |
| 915 | |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 916 | static grpc_compression_algorithm decode_compression(grpc_mdelem md) { |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 917 | grpc_compression_algorithm algorithm = |
Craig Tiller | 0160de9 | 2016-11-18 08:46:46 -0800 | [diff] [blame] | 918 | grpc_compression_algorithm_from_slice(GRPC_MDVALUE(md)); |
Craig Tiller | ebdef9d | 2015-11-19 17:09:49 -0800 | [diff] [blame] | 919 | if (algorithm == GRPC_COMPRESS_ALGORITHMS_COUNT) { |
Craig Tiller | b4aa70e | 2016-12-09 09:40:11 -0800 | [diff] [blame] | 920 | char *md_c_str = grpc_slice_to_c_string(GRPC_MDVALUE(md)); |
David Garcia Quintas | 303d308 | 2016-05-05 18:25:34 -0700 | [diff] [blame] | 921 | gpr_log(GPR_ERROR, |
| 922 | "Invalid incoming compression algorithm: '%s'. Interpreting " |
| 923 | "incoming data as uncompressed.", |
| 924 | md_c_str); |
Craig Tiller | 68208fe | 2016-11-14 14:35:02 -0800 | [diff] [blame] | 925 | gpr_free(md_c_str); |
David Garcia Quintas | 303d308 | 2016-05-05 18:25:34 -0700 | [diff] [blame] | 926 | return GRPC_COMPRESS_NONE; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 927 | } |
David Garcia Quintas | fc0fa33 | 2015-06-25 18:11:07 -0700 | [diff] [blame] | 928 | return algorithm; |
David Garcia Quintas | db94b27 | 2015-06-15 18:37:01 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 931 | static void recv_common_filter(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 932 | grpc_metadata_batch *b) { |
Craig Tiller | de2508b | 2017-01-06 15:23:23 -0800 | [diff] [blame] | 933 | if (b->idx.named.grpc_status != NULL) { |
| 934 | uint32_t status_code = decode_status(b->idx.named.grpc_status->md); |
| 935 | grpc_error *error = |
| 936 | status_code == GRPC_STATUS_OK |
| 937 | ? GRPC_ERROR_NONE |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 938 | : grpc_error_set_int(GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 939 | "Error received from peer"), |
Craig Tiller | cae37f3 | 2017-01-09 15:50:03 -0800 | [diff] [blame] | 940 | GRPC_ERROR_INT_GRPC_STATUS, |
| 941 | (intptr_t)status_code); |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 942 | |
Craig Tiller | de2508b | 2017-01-06 15:23:23 -0800 | [diff] [blame] | 943 | if (b->idx.named.grpc_message != NULL) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 944 | error = grpc_error_set_str( |
| 945 | error, GRPC_ERROR_STR_GRPC_MESSAGE, |
| 946 | grpc_slice_ref_internal(GRPC_MDVALUE(b->idx.named.grpc_message->md))); |
Craig Tiller | de2508b | 2017-01-06 15:23:23 -0800 | [diff] [blame] | 947 | grpc_metadata_batch_remove(exec_ctx, b, b->idx.named.grpc_message); |
Craig Tiller | 8c58a48 | 2017-02-07 14:52:59 -0800 | [diff] [blame] | 948 | } else if (error != GRPC_ERROR_NONE) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 949 | error = grpc_error_set_str(error, GRPC_ERROR_STR_GRPC_MESSAGE, |
| 950 | grpc_empty_slice()); |
Craig Tiller | de2508b | 2017-01-06 15:23:23 -0800 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | set_status_from_error(exec_ctx, call, STATUS_FROM_WIRE, error); |
| 954 | grpc_metadata_batch_remove(exec_ctx, b, b->idx.named.grpc_status); |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 955 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 956 | } |
| 957 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 958 | static void publish_app_metadata(grpc_call *call, grpc_metadata_batch *b, |
| 959 | int is_trailing) { |
Craig Tiller | 0960818 | 2016-11-22 15:43:56 -0800 | [diff] [blame] | 960 | if (b->list.count == 0) return; |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 961 | GPR_TIMER_BEGIN("publish_app_metadata", 0); |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 962 | grpc_metadata_array *dest; |
| 963 | grpc_metadata *mdusr; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 964 | dest = call->buffered_metadata[is_trailing]; |
Craig Tiller | b0f3bca | 2016-11-22 14:54:10 -0800 | [diff] [blame] | 965 | if (dest->count + b->list.count > dest->capacity) { |
| 966 | dest->capacity = |
| 967 | GPR_MAX(dest->capacity + b->list.count, dest->capacity * 3 / 2); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 968 | dest->metadata = |
| 969 | gpr_realloc(dest->metadata, sizeof(grpc_metadata) * dest->capacity); |
| 970 | } |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 971 | for (grpc_linked_mdelem *l = b->list.head; l != NULL; l = l->next) { |
| 972 | mdusr = &dest->metadata[dest->count++]; |
Craig Tiller | cf0a202 | 2016-11-23 11:36:21 -0800 | [diff] [blame] | 973 | /* we pass back borrowed slices that are valid whilst the call is valid */ |
| 974 | mdusr->key = GRPC_MDKEY(l->md); |
| 975 | mdusr->value = GRPC_MDVALUE(l->md); |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 976 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 977 | GPR_TIMER_END("publish_app_metadata", 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 978 | } |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 979 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 980 | static void recv_initial_filter(grpc_exec_ctx *exec_ctx, grpc_call *call, |
| 981 | grpc_metadata_batch *b) { |
| 982 | recv_common_filter(exec_ctx, call, b); |
| 983 | |
| 984 | if (b->idx.named.grpc_encoding != NULL) { |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 985 | GPR_TIMER_BEGIN("incoming_compression_algorithm", 0); |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 986 | set_incoming_compression_algorithm( |
| 987 | call, decode_compression(b->idx.named.grpc_encoding->md)); |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 988 | GPR_TIMER_END("incoming_compression_algorithm", 0); |
Craig Tiller | de7b467 | 2016-11-23 11:13:46 -0800 | [diff] [blame] | 989 | grpc_metadata_batch_remove(exec_ctx, b, b->idx.named.grpc_encoding); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 990 | } |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 991 | |
| 992 | if (b->idx.named.grpc_accept_encoding != NULL) { |
| 993 | GPR_TIMER_BEGIN("encodings_accepted_by_peer", 0); |
| 994 | set_encodings_accepted_by_peer(exec_ctx, call, |
| 995 | b->idx.named.grpc_accept_encoding->md); |
Craig Tiller | de7b467 | 2016-11-23 11:13:46 -0800 | [diff] [blame] | 996 | grpc_metadata_batch_remove(exec_ctx, b, b->idx.named.grpc_accept_encoding); |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 997 | GPR_TIMER_END("encodings_accepted_by_peer", 0); |
| 998 | } |
| 999 | |
| 1000 | publish_app_metadata(call, b, false); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1001 | } |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 1002 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 1003 | static void recv_trailing_filter(grpc_exec_ctx *exec_ctx, void *args, |
| 1004 | grpc_metadata_batch *b) { |
Craig Tiller | c586666 | 2016-11-16 15:25:00 -0800 | [diff] [blame] | 1005 | grpc_call *call = args; |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 1006 | recv_common_filter(exec_ctx, call, b); |
| 1007 | publish_app_metadata(call, b, true); |
Craig Tiller | 629b0ed | 2015-04-22 11:14:26 -0700 | [diff] [blame] | 1008 | } |
Craig Tiller | 8b282cb | 2015-04-17 14:57:44 -0700 | [diff] [blame] | 1009 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1010 | grpc_call_stack *grpc_call_get_call_stack(grpc_call *call) { |
| 1011 | return CALL_STACK_FROM_CALL(call); |
Craig Tiller | 566316f | 2015-02-02 15:25:32 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
Craig Tiller | 255edaa | 2016-12-13 09:04:55 -0800 | [diff] [blame] | 1014 | /******************************************************************************* |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1015 | * BATCH API IMPLEMENTATION |
| 1016 | */ |
| 1017 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1018 | static void set_status_value_directly(grpc_status_code status, void *dest) { |
| 1019 | *(grpc_status_code *)dest = status; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1022 | static void set_cancelled_value(grpc_status_code status, void *dest) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1023 | *(int *)dest = (status != GRPC_STATUS_OK); |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 1024 | } |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1025 | |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 1026 | static bool are_write_flags_valid(uint32_t flags) { |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1027 | /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1028 | const uint32_t allowed_write_positions = |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1029 | (GRPC_WRITE_USED_MASK | GRPC_WRITE_INTERNAL_USED_MASK); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1030 | const uint32_t invalid_positions = ~allowed_write_positions; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 1031 | return !(flags & invalid_positions); |
| 1032 | } |
| 1033 | |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 1034 | static bool are_initial_metadata_flags_valid(uint32_t flags, bool is_client) { |
| 1035 | /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */ |
| 1036 | uint32_t invalid_positions = ~GRPC_INITIAL_METADATA_USED_MASK; |
| 1037 | if (!is_client) { |
| 1038 | invalid_positions |= GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST; |
| 1039 | } |
| 1040 | return !(flags & invalid_positions); |
| 1041 | } |
| 1042 | |
Craig Tiller | 2a11ad1 | 2017-02-08 17:09:02 -0800 | [diff] [blame] | 1043 | static int batch_slot_for_op(grpc_op_type type) { |
| 1044 | switch (type) { |
| 1045 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 1046 | return 0; |
| 1047 | case GRPC_OP_SEND_MESSAGE: |
| 1048 | return 1; |
| 1049 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 1050 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 1051 | return 2; |
| 1052 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 1053 | return 3; |
| 1054 | case GRPC_OP_RECV_MESSAGE: |
| 1055 | return 4; |
| 1056 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 1057 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 1058 | return 5; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1059 | } |
Craig Tiller | c869da0 | 2017-02-08 17:11:17 -0800 | [diff] [blame] | 1060 | GPR_UNREACHABLE_CODE(return 123456789); |
Craig Tiller | 2a11ad1 | 2017-02-08 17:09:02 -0800 | [diff] [blame] | 1061 | } |
Craig Tiller | 89d3379 | 2017-02-08 16:39:16 -0800 | [diff] [blame] | 1062 | |
| 1063 | static batch_control *allocate_batch_control(grpc_call *call, |
| 1064 | const grpc_op *ops, |
| 1065 | size_t num_ops) { |
| 1066 | int slot = batch_slot_for_op(ops[0].op); |
Craig Tiller | b58de72 | 2017-03-29 14:15:12 -0700 | [diff] [blame] | 1067 | batch_control **pslot = &call->active_batches[slot]; |
| 1068 | if (*pslot == NULL) { |
| 1069 | *pslot = gpr_arena_alloc(call->arena, sizeof(batch_control)); |
| 1070 | } |
| 1071 | batch_control *bctl = *pslot; |
Craig Tiller | 5e5ef30 | 2017-02-09 08:46:49 -0800 | [diff] [blame] | 1072 | if (bctl->call != NULL) { |
| 1073 | return NULL; |
| 1074 | } |
| 1075 | memset(bctl, 0, sizeof(*bctl)); |
| 1076 | bctl->call = call; |
Craig Tiller | 3fddcb4 | 2017-03-10 11:01:48 -0800 | [diff] [blame] | 1077 | bctl->op.payload = &call->stream_op_payload; |
Craig Tiller | 5e5ef30 | 2017-02-09 08:46:49 -0800 | [diff] [blame] | 1078 | return bctl; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static void finish_batch_completion(grpc_exec_ctx *exec_ctx, void *user_data, |
| 1082 | grpc_cq_completion *storage) { |
| 1083 | batch_control *bctl = user_data; |
| 1084 | grpc_call *call = bctl->call; |
Craig Tiller | 5e5ef30 | 2017-02-09 08:46:49 -0800 | [diff] [blame] | 1085 | bctl->call = NULL; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1086 | GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "completion"); |
| 1087 | } |
| 1088 | |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1089 | static grpc_error *consolidate_batch_errors(batch_control *bctl) { |
Craig Tiller | b597dcf | 2017-03-09 07:02:11 -0800 | [diff] [blame] | 1090 | size_t n = (size_t)gpr_atm_acq_load(&bctl->num_errors); |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1091 | if (n == 0) { |
| 1092 | return GRPC_ERROR_NONE; |
| 1093 | } else if (n == 1) { |
Craig Tiller | a78da60 | 2017-01-27 08:16:23 -0800 | [diff] [blame] | 1094 | /* Skip creating a composite error in the case that only one error was |
| 1095 | logged */ |
Craig Tiller | ad980e3 | 2017-01-23 07:46:25 -0800 | [diff] [blame] | 1096 | grpc_error *e = bctl->errors[0]; |
| 1097 | bctl->errors[0] = NULL; |
| 1098 | return e; |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1099 | } else { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 1100 | grpc_error *error = GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 1101 | "Call batch failed", bctl->errors, n); |
Craig Tiller | 1c4775c | 2017-01-06 16:07:45 -0800 | [diff] [blame] | 1102 | for (size_t i = 0; i < n; i++) { |
| 1103 | GRPC_ERROR_UNREF(bctl->errors[i]); |
Craig Tiller | ad980e3 | 2017-01-23 07:46:25 -0800 | [diff] [blame] | 1104 | bctl->errors[i] = NULL; |
Craig Tiller | 1c4775c | 2017-01-06 16:07:45 -0800 | [diff] [blame] | 1105 | } |
| 1106 | return error; |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1110 | static void post_batch_completion(grpc_exec_ctx *exec_ctx, |
| 1111 | batch_control *bctl) { |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1112 | grpc_call *next_child_call; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1113 | grpc_call *call = bctl->call; |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1114 | grpc_error *error = consolidate_batch_errors(bctl); |
| 1115 | |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 1116 | if (bctl->op.send_initial_metadata) { |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1117 | grpc_metadata_batch_destroy( |
| 1118 | exec_ctx, |
| 1119 | &call->metadata_batch[0 /* is_receiving */][0 /* is_trailing */]); |
| 1120 | } |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 1121 | if (bctl->op.send_message) { |
Craig Tiller | f927ad1 | 2017-01-06 15:27:31 -0800 | [diff] [blame] | 1122 | call->sending_message = false; |
| 1123 | } |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 1124 | if (bctl->op.send_trailing_metadata) { |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1125 | grpc_metadata_batch_destroy( |
| 1126 | exec_ctx, |
| 1127 | &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */]); |
| 1128 | } |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 1129 | if (bctl->op.recv_trailing_metadata) { |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1130 | grpc_metadata_batch *md = |
| 1131 | &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */]; |
| 1132 | recv_trailing_filter(exec_ctx, call, md); |
| 1133 | |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1134 | /* propagate cancellation to any interested children */ |
Craig Tiller | b18c8ba | 2017-03-13 15:51:37 -0700 | [diff] [blame] | 1135 | gpr_atm_rel_store(&call->received_final_op_atm, 1); |
Craig Tiller | 1c10a7b | 2017-03-29 14:35:16 -0700 | [diff] [blame] | 1136 | parent_call *pc = get_parent_call(call); |
| 1137 | if (pc != NULL) { |
| 1138 | grpc_call *child; |
| 1139 | gpr_mu_lock(&pc->child_list_mu); |
| 1140 | child = pc->first_child; |
| 1141 | if (child != NULL) { |
| 1142 | do { |
| 1143 | next_child_call = child->child_call->sibling_next; |
| 1144 | if (child->cancellation_is_inherited) { |
| 1145 | GRPC_CALL_INTERNAL_REF(child, "propagate_cancel"); |
| 1146 | cancel_with_error(exec_ctx, child, STATUS_FROM_API_OVERRIDE, |
| 1147 | GRPC_ERROR_CANCELLED); |
| 1148 | GRPC_CALL_INTERNAL_UNREF(exec_ctx, child, "propagate_cancel"); |
| 1149 | } |
| 1150 | child = next_child_call; |
| 1151 | } while (child != pc->first_child); |
| 1152 | } |
| 1153 | gpr_mu_unlock(&pc->child_list_mu); |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | if (call->is_client) { |
| 1157 | get_final_status(call, set_status_value_directly, |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 1158 | call->final_op.client.status, |
| 1159 | call->final_op.client.status_details); |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1160 | } else { |
| 1161 | get_final_status(call, set_cancelled_value, |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 1162 | call->final_op.server.cancelled, NULL); |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Craig Tiller | 02b87cd | 2016-09-02 09:50:08 -0700 | [diff] [blame] | 1165 | GRPC_ERROR_UNREF(error); |
| 1166 | error = GRPC_ERROR_NONE; |
| 1167 | } |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1168 | |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 1169 | if (bctl->completion_data.notify_tag.is_closure) { |
Craig Tiller | b08fa49 | 2016-05-10 14:56:05 -0700 | [diff] [blame] | 1170 | /* unrefs bctl->error */ |
Craig Tiller | 2db5bda | 2017-02-09 10:30:55 -0800 | [diff] [blame] | 1171 | bctl->call = NULL; |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 1172 | grpc_closure_run(exec_ctx, bctl->completion_data.notify_tag.tag, error); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1173 | GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "completion"); |
| 1174 | } else { |
Craig Tiller | b08fa49 | 2016-05-10 14:56:05 -0700 | [diff] [blame] | 1175 | /* unrefs bctl->error */ |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1176 | grpc_cq_end_op( |
| 1177 | exec_ctx, bctl->call->cq, bctl->completion_data.notify_tag.tag, error, |
| 1178 | finish_batch_completion, bctl, &bctl->completion_data.cq_completion); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1182 | static void finish_batch_step(grpc_exec_ctx *exec_ctx, batch_control *bctl) { |
Craig Tiller | 065b139 | 2017-01-09 14:05:07 -0800 | [diff] [blame] | 1183 | if (gpr_unref(&bctl->steps_to_complete)) { |
| 1184 | post_batch_completion(exec_ctx, bctl); |
| 1185 | } |
| 1186 | } |
| 1187 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1188 | static void continue_receiving_slices(grpc_exec_ctx *exec_ctx, |
| 1189 | batch_control *bctl) { |
| 1190 | grpc_call *call = bctl->call; |
| 1191 | for (;;) { |
| 1192 | size_t remaining = call->receiving_stream->length - |
| 1193 | (*call->receiving_buffer)->data.raw.slice_buffer.length; |
| 1194 | if (remaining == 0) { |
| 1195 | call->receiving_message = 0; |
Craig Tiller | 3b66ab9 | 2015-12-09 19:42:22 -0800 | [diff] [blame] | 1196 | grpc_byte_stream_destroy(exec_ctx, call->receiving_stream); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1197 | call->receiving_stream = NULL; |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1198 | finish_batch_step(exec_ctx, bctl); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1199 | return; |
| 1200 | } |
| 1201 | if (grpc_byte_stream_next(exec_ctx, call->receiving_stream, |
| 1202 | &call->receiving_slice, remaining, |
| 1203 | &call->receiving_slice_ready)) { |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 1204 | grpc_slice_buffer_add(&(*call->receiving_buffer)->data.raw.slice_buffer, |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 1205 | call->receiving_slice); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1206 | } else { |
| 1207 | return; |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | static void receiving_slice_ready(grpc_exec_ctx *exec_ctx, void *bctlp, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 1213 | grpc_error *error) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1214 | batch_control *bctl = bctlp; |
| 1215 | grpc_call *call = bctl->call; |
| 1216 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 1217 | if (error == GRPC_ERROR_NONE) { |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 1218 | grpc_slice_buffer_add(&(*call->receiving_buffer)->data.raw.slice_buffer, |
Craig Tiller | 0f31080 | 2016-10-26 16:25:56 -0700 | [diff] [blame] | 1219 | call->receiving_slice); |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 1220 | continue_receiving_slices(exec_ctx, bctl); |
| 1221 | } else { |
Craig Tiller | a286b04 | 2016-06-13 15:20:39 +0000 | [diff] [blame] | 1222 | if (grpc_trace_operation_failures) { |
| 1223 | GRPC_LOG_IF_ERROR("receiving_slice_ready", GRPC_ERROR_REF(error)); |
| 1224 | } |
Craig Tiller | e1b8c2b | 2015-12-16 19:27:52 -0800 | [diff] [blame] | 1225 | grpc_byte_stream_destroy(exec_ctx, call->receiving_stream); |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 1226 | call->receiving_stream = NULL; |
| 1227 | grpc_byte_buffer_destroy(*call->receiving_buffer); |
| 1228 | *call->receiving_buffer = NULL; |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1229 | finish_batch_step(exec_ctx, bctl); |
Craig Tiller | 38edec6 | 2015-12-14 15:01:29 -0800 | [diff] [blame] | 1230 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1231 | } |
| 1232 | |
Mark D. Roth | 274c8ed | 2016-10-04 09:21:42 -0700 | [diff] [blame] | 1233 | static void process_data_after_md(grpc_exec_ctx *exec_ctx, |
| 1234 | batch_control *bctl) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1235 | grpc_call *call = bctl->call; |
| 1236 | if (call->receiving_stream == NULL) { |
| 1237 | *call->receiving_buffer = NULL; |
| 1238 | call->receiving_message = 0; |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1239 | finish_batch_step(exec_ctx, bctl); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1240 | } else { |
| 1241 | call->test_only_last_message_flags = call->receiving_stream->flags; |
| 1242 | if ((call->receiving_stream->flags & GRPC_WRITE_INTERNAL_COMPRESS) && |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1243 | (call->incoming_compression_algorithm > GRPC_COMPRESS_NONE)) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1244 | *call->receiving_buffer = grpc_raw_compressed_byte_buffer_create( |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1245 | NULL, 0, call->incoming_compression_algorithm); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1246 | } else { |
| 1247 | *call->receiving_buffer = grpc_raw_byte_buffer_create(NULL, 0); |
| 1248 | } |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 1249 | grpc_closure_init(&call->receiving_slice_ready, receiving_slice_ready, bctl, |
| 1250 | grpc_schedule_on_exec_ctx); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1251 | continue_receiving_slices(exec_ctx, bctl); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | static void receiving_stream_ready(grpc_exec_ctx *exec_ctx, void *bctlp, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 1256 | grpc_error *error) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1257 | batch_control *bctl = bctlp; |
| 1258 | grpc_call *call = bctl->call; |
Mark D. Roth | 274c8ed | 2016-10-04 09:21:42 -0700 | [diff] [blame] | 1259 | if (error != GRPC_ERROR_NONE) { |
yang-g | 23f777d | 2017-02-22 23:32:26 -0800 | [diff] [blame] | 1260 | if (call->receiving_stream != NULL) { |
| 1261 | grpc_byte_stream_destroy(exec_ctx, call->receiving_stream); |
| 1262 | call->receiving_stream = NULL; |
| 1263 | } |
| 1264 | add_batch_error(exec_ctx, bctl, GRPC_ERROR_REF(error), true); |
Craig Tiller | 58b30cd | 2017-01-31 17:07:36 -0800 | [diff] [blame] | 1265 | cancel_with_error(exec_ctx, call, STATUS_FROM_SURFACE, |
| 1266 | GRPC_ERROR_REF(error)); |
Mark D. Roth | 274c8ed | 2016-10-04 09:21:42 -0700 | [diff] [blame] | 1267 | } |
Craig Tiller | 065b139 | 2017-01-09 14:05:07 -0800 | [diff] [blame] | 1268 | if (call->has_initial_md_been_received || error != GRPC_ERROR_NONE || |
Craig Tiller | 52cf871 | 2016-04-23 22:54:21 -0700 | [diff] [blame] | 1269 | call->receiving_stream == NULL) { |
Mark D. Roth | 274c8ed | 2016-10-04 09:21:42 -0700 | [diff] [blame] | 1270 | process_data_after_md(exec_ctx, bctlp); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1271 | } else { |
Craig Tiller | 8a67780 | 2016-04-22 15:07:53 -0700 | [diff] [blame] | 1272 | call->saved_receiving_stream_ready_bctlp = bctlp; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1276 | static void validate_filtered_metadata(grpc_exec_ctx *exec_ctx, |
| 1277 | batch_control *bctl) { |
| 1278 | grpc_call *call = bctl->call; |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1279 | /* validate call->incoming_compression_algorithm */ |
| 1280 | if (call->incoming_compression_algorithm != GRPC_COMPRESS_NONE) { |
| 1281 | const grpc_compression_algorithm algo = |
| 1282 | call->incoming_compression_algorithm; |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1283 | char *error_msg = NULL; |
| 1284 | const grpc_compression_options compression_options = |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1285 | grpc_channel_compression_options(call->channel); |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1286 | /* check if algorithm is known */ |
| 1287 | if (algo >= GRPC_COMPRESS_ALGORITHMS_COUNT) { |
| 1288 | gpr_asprintf(&error_msg, "Invalid compression algorithm value '%d'.", |
| 1289 | algo); |
Yuchen Zeng | 64c0e8d | 2016-06-10 11:19:51 -0700 | [diff] [blame] | 1290 | gpr_log(GPR_ERROR, "%s", error_msg); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 1291 | cancel_with_status(exec_ctx, call, STATUS_FROM_SURFACE, |
| 1292 | GRPC_STATUS_UNIMPLEMENTED, error_msg); |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1293 | } else if (grpc_compression_options_is_algorithm_enabled( |
| 1294 | &compression_options, algo) == 0) { |
| 1295 | /* check if algorithm is supported by current channel config */ |
David Garcia Quintas | 1ff168a | 2016-06-30 10:25:04 -0700 | [diff] [blame] | 1296 | char *algo_name = NULL; |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1297 | grpc_compression_algorithm_name(algo, &algo_name); |
| 1298 | gpr_asprintf(&error_msg, "Compression algorithm '%s' is disabled.", |
| 1299 | algo_name); |
Yuchen Zeng | 64c0e8d | 2016-06-10 11:19:51 -0700 | [diff] [blame] | 1300 | gpr_log(GPR_ERROR, "%s", error_msg); |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 1301 | cancel_with_status(exec_ctx, call, STATUS_FROM_SURFACE, |
| 1302 | GRPC_STATUS_UNIMPLEMENTED, error_msg); |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1303 | } else { |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1304 | call->incoming_compression_algorithm = algo; |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1305 | } |
| 1306 | gpr_free(error_msg); |
| 1307 | } |
David Garcia Quintas | f1945f2 | 2016-05-18 10:53:14 -0700 | [diff] [blame] | 1308 | |
| 1309 | /* make sure the received grpc-encoding is amongst the ones listed in |
| 1310 | * grpc-accept-encoding */ |
| 1311 | GPR_ASSERT(call->encodings_accepted_by_peer != 0); |
| 1312 | if (!GPR_BITGET(call->encodings_accepted_by_peer, |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1313 | call->incoming_compression_algorithm)) { |
David Garcia Quintas | f1945f2 | 2016-05-18 10:53:14 -0700 | [diff] [blame] | 1314 | extern int grpc_compression_trace; |
| 1315 | if (grpc_compression_trace) { |
David Garcia Quintas | 1ff168a | 2016-06-30 10:25:04 -0700 | [diff] [blame] | 1316 | char *algo_name = NULL; |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1317 | grpc_compression_algorithm_name(call->incoming_compression_algorithm, |
| 1318 | &algo_name); |
David Garcia Quintas | f1945f2 | 2016-05-18 10:53:14 -0700 | [diff] [blame] | 1319 | gpr_log(GPR_ERROR, |
| 1320 | "Compression algorithm (grpc-encoding = '%s') not present in " |
| 1321 | "the bitset of accepted encodings (grpc-accept-encodings: " |
| 1322 | "'0x%x')", |
| 1323 | algo_name, call->encodings_accepted_by_peer); |
| 1324 | } |
| 1325 | } |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Craig Tiller | 3ba16e4 | 2016-12-08 16:46:18 -0800 | [diff] [blame] | 1328 | static void add_batch_error(grpc_exec_ctx *exec_ctx, batch_control *bctl, |
yang-g | 23f777d | 2017-02-22 23:32:26 -0800 | [diff] [blame] | 1329 | grpc_error *error, bool has_cancelled) { |
Craig Tiller | 452422e | 2016-09-01 15:54:56 -0700 | [diff] [blame] | 1330 | if (error == GRPC_ERROR_NONE) return; |
Craig Tiller | b597dcf | 2017-03-09 07:02:11 -0800 | [diff] [blame] | 1331 | int idx = (int)gpr_atm_full_fetch_add(&bctl->num_errors, 1); |
yang-g | 23f777d | 2017-02-22 23:32:26 -0800 | [diff] [blame] | 1332 | if (idx == 0 && !has_cancelled) { |
Craig Tiller | 2dc32ea | 2017-01-31 15:32:34 -0800 | [diff] [blame] | 1333 | cancel_with_error(exec_ctx, bctl->call, STATUS_FROM_CORE, |
| 1334 | GRPC_ERROR_REF(error)); |
| 1335 | } |
Craig Tiller | 9490389 | 2016-10-11 15:43:35 -0700 | [diff] [blame] | 1336 | bctl->errors[idx] = error; |
Craig Tiller | 452422e | 2016-09-01 15:54:56 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1339 | static void receiving_initial_metadata_ready(grpc_exec_ctx *exec_ctx, |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 1340 | void *bctlp, grpc_error *error) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1341 | batch_control *bctl = bctlp; |
| 1342 | grpc_call *call = bctl->call; |
| 1343 | |
yang-g | 23f777d | 2017-02-22 23:32:26 -0800 | [diff] [blame] | 1344 | add_batch_error(exec_ctx, bctl, GRPC_ERROR_REF(error), false); |
Craig Tiller | 452422e | 2016-09-01 15:54:56 -0700 | [diff] [blame] | 1345 | if (error == GRPC_ERROR_NONE) { |
Craig Tiller | c48ca71 | 2016-04-04 13:42:04 -0700 | [diff] [blame] | 1346 | grpc_metadata_batch *md = |
| 1347 | &call->metadata_batch[1 /* is_receiving */][0 /* is_trailing */]; |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 1348 | recv_initial_filter(exec_ctx, call, md); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1349 | |
Craig Tiller | a7d37a3 | 2016-11-22 14:37:16 -0800 | [diff] [blame] | 1350 | /* TODO(ctiller): this could be moved into recv_initial_filter now */ |
David Garcia Quintas | 3e71f77 | 2016-05-18 10:14:32 -0700 | [diff] [blame] | 1351 | GPR_TIMER_BEGIN("validate_filtered_metadata", 0); |
| 1352 | validate_filtered_metadata(exec_ctx, bctl); |
| 1353 | GPR_TIMER_END("validate_filtered_metadata", 0); |
David Garcia Quintas | 4612337 | 2016-05-09 15:28:42 -0700 | [diff] [blame] | 1354 | |
Craig Tiller | c48ca71 | 2016-04-04 13:42:04 -0700 | [diff] [blame] | 1355 | if (gpr_time_cmp(md->deadline, gpr_inf_future(md->deadline.clock_type)) != |
| 1356 | 0 && |
| 1357 | !call->is_client) { |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 1358 | call->send_deadline = |
| 1359 | gpr_convert_clock_type(md->deadline, GPR_CLOCK_MONOTONIC); |
Craig Tiller | c48ca71 | 2016-04-04 13:42:04 -0700 | [diff] [blame] | 1360 | } |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1361 | } |
| 1362 | |
Craig Tiller | c48ca71 | 2016-04-04 13:42:04 -0700 | [diff] [blame] | 1363 | call->has_initial_md_been_received = true; |
Craig Tiller | 8a67780 | 2016-04-22 15:07:53 -0700 | [diff] [blame] | 1364 | if (call->saved_receiving_stream_ready_bctlp != NULL) { |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1365 | grpc_closure *saved_rsr_closure = grpc_closure_create( |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 1366 | receiving_stream_ready, call->saved_receiving_stream_ready_bctlp, |
| 1367 | grpc_schedule_on_exec_ctx); |
Craig Tiller | 8a67780 | 2016-04-22 15:07:53 -0700 | [diff] [blame] | 1368 | call->saved_receiving_stream_ready_bctlp = NULL; |
Craig Tiller | e034179 | 2017-03-08 15:59:16 -0800 | [diff] [blame] | 1369 | grpc_closure_run(exec_ctx, saved_rsr_closure, GRPC_ERROR_REF(error)); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1370 | } |
| 1371 | |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1372 | finish_batch_step(exec_ctx, bctl); |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1373 | } |
| 1374 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 1375 | static void finish_batch(grpc_exec_ctx *exec_ctx, void *bctlp, |
| 1376 | grpc_error *error) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1377 | batch_control *bctl = bctlp; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1378 | |
yang-g | 23f777d | 2017-02-22 23:32:26 -0800 | [diff] [blame] | 1379 | add_batch_error(exec_ctx, bctl, GRPC_ERROR_REF(error), false); |
Craig Tiller | d04dc4d | 2017-01-09 14:40:28 -0800 | [diff] [blame] | 1380 | finish_batch_step(exec_ctx, bctl); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
Craig Tiller | 89d3379 | 2017-02-08 16:39:16 -0800 | [diff] [blame] | 1383 | static void free_no_op_completion(grpc_exec_ctx *exec_ctx, void *p, |
| 1384 | grpc_cq_completion *completion) { |
| 1385 | gpr_free(completion); |
| 1386 | } |
| 1387 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1388 | static grpc_call_error call_start_batch(grpc_exec_ctx *exec_ctx, |
| 1389 | grpc_call *call, const grpc_op *ops, |
| 1390 | size_t nops, void *notify_tag, |
| 1391 | int is_notify_tag_closure) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1392 | size_t i; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1393 | const grpc_op *op; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1394 | batch_control *bctl; |
| 1395 | int num_completion_callbacks_needed = 1; |
| 1396 | grpc_call_error error = GRPC_CALL_OK; |
Craig Tiller | 9928d39 | 2015-08-18 09:40:24 -0700 | [diff] [blame] | 1397 | |
Vitaly Buka | e60003d | 2016-08-01 19:34:51 -0700 | [diff] [blame] | 1398 | // sent_initial_metadata guards against variable reuse. |
| 1399 | grpc_metadata compression_md; |
| 1400 | |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 1401 | GPR_TIMER_BEGIN("grpc_call_start_batch", 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1402 | GRPC_CALL_LOG_BATCH(GPR_INFO, call, ops, nops, notify_tag); |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 1403 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1404 | if (nops == 0) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1405 | if (!is_notify_tag_closure) { |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 1406 | grpc_cq_begin_op(call->cq, notify_tag); |
Craig Tiller | 89d3379 | 2017-02-08 16:39:16 -0800 | [diff] [blame] | 1407 | grpc_cq_end_op(exec_ctx, call->cq, notify_tag, GRPC_ERROR_NONE, |
| 1408 | free_no_op_completion, NULL, |
| 1409 | gpr_malloc(sizeof(grpc_cq_completion))); |
Craig Tiller | 2db5bda | 2017-02-09 10:30:55 -0800 | [diff] [blame] | 1410 | } else { |
| 1411 | grpc_closure_sched(exec_ctx, notify_tag, GRPC_ERROR_NONE); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1412 | } |
Craig Tiller | ea50b90 | 2015-12-15 07:05:25 -0800 | [diff] [blame] | 1413 | error = GRPC_CALL_OK; |
| 1414 | goto done; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Craig Tiller | 89d3379 | 2017-02-08 16:39:16 -0800 | [diff] [blame] | 1417 | bctl = allocate_batch_control(call, ops, nops); |
Craig Tiller | 5e5ef30 | 2017-02-09 08:46:49 -0800 | [diff] [blame] | 1418 | if (bctl == NULL) { |
| 1419 | return GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1420 | } |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 1421 | bctl->completion_data.notify_tag.tag = notify_tag; |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1422 | bctl->completion_data.notify_tag.is_closure = |
| 1423 | (uint8_t)(is_notify_tag_closure != 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1424 | |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 1425 | grpc_transport_stream_op_batch *stream_op = &bctl->op; |
| 1426 | grpc_transport_stream_op_batch_payload *stream_op_payload = |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1427 | &call->stream_op_payload; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1428 | stream_op->covered_by_poller = true; |
| 1429 | |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1430 | /* rewrite batch ops into a transport op */ |
| 1431 | for (i = 0; i < nops; i++) { |
| 1432 | op = &ops[i]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1433 | if (op->reserved != NULL) { |
Craig Tiller | 3ffd822 | 2015-09-21 08:21:57 -0700 | [diff] [blame] | 1434 | error = GRPC_CALL_ERROR; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1435 | goto done_with_error; |
Craig Tiller | 3ffd822 | 2015-09-21 08:21:57 -0700 | [diff] [blame] | 1436 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1437 | switch (op->op) { |
| 1438 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 1439 | /* Flag validation: currently allow no flags */ |
Craig Tiller | c654976 | 2016-03-09 17:10:43 -0800 | [diff] [blame] | 1440 | if (!are_initial_metadata_flags_valid(op->flags, call->is_client)) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1441 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1442 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1443 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1444 | if (call->sent_initial_metadata) { |
| 1445 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1446 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1447 | } |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1448 | /* process compression level */ |
Vitaly Buka | e60003d | 2016-08-01 19:34:51 -0700 | [diff] [blame] | 1449 | memset(&compression_md, 0, sizeof(compression_md)); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1450 | size_t additional_metadata_count = 0; |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1451 | grpc_compression_level effective_compression_level; |
| 1452 | bool level_set = false; |
| 1453 | if (op->data.send_initial_metadata.maybe_compression_level.is_set) { |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1454 | effective_compression_level = |
David Garcia Quintas | 8ba42be | 2016-06-07 17:30:20 -0700 | [diff] [blame] | 1455 | op->data.send_initial_metadata.maybe_compression_level.level; |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1456 | level_set = true; |
| 1457 | } else { |
David Garcia Quintas | ac09447 | 2016-05-18 20:25:57 -0700 | [diff] [blame] | 1458 | const grpc_compression_options copts = |
| 1459 | grpc_channel_compression_options(call->channel); |
| 1460 | level_set = copts.default_level.is_set; |
| 1461 | if (level_set) { |
| 1462 | effective_compression_level = copts.default_level.level; |
| 1463 | } |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1464 | } |
David Garcia Quintas | 3e4f49f | 2016-05-18 23:59:02 -0700 | [diff] [blame] | 1465 | if (level_set && !call->is_client) { |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1466 | const grpc_compression_algorithm calgo = |
| 1467 | compression_algorithm_for_level_locked( |
David Garcia Quintas | 749367f | 2016-05-17 19:15:24 -0700 | [diff] [blame] | 1468 | call, effective_compression_level); |
David Garcia Quintas | 3e4f49f | 2016-05-18 23:59:02 -0700 | [diff] [blame] | 1469 | // the following will be picked up by the compress filter and used as |
| 1470 | // the call's compression algorithm. |
Craig Tiller | 68208fe | 2016-11-14 14:35:02 -0800 | [diff] [blame] | 1471 | compression_md.key = GRPC_MDSTR_GRPC_INTERNAL_ENCODING_REQUEST; |
| 1472 | compression_md.value = grpc_compression_algorithm_slice(calgo); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1473 | additional_metadata_count++; |
| 1474 | } |
| 1475 | |
| 1476 | if (op->data.send_initial_metadata.count + additional_metadata_count > |
| 1477 | INT_MAX) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1478 | error = GRPC_CALL_ERROR_INVALID_METADATA; |
| 1479 | goto done_with_error; |
| 1480 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1481 | stream_op->send_initial_metadata = true; |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 1482 | call->sent_initial_metadata = true; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1483 | if (!prepare_application_metadata( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 1484 | exec_ctx, call, (int)op->data.send_initial_metadata.count, |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1485 | op->data.send_initial_metadata.metadata, 0, call->is_client, |
| 1486 | &compression_md, (int)additional_metadata_count)) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1487 | error = GRPC_CALL_ERROR_INVALID_METADATA; |
| 1488 | goto done_with_error; |
| 1489 | } |
| 1490 | /* TODO(ctiller): just make these the same variable? */ |
| 1491 | call->metadata_batch[0][0].deadline = call->send_deadline; |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1492 | stream_op_payload->send_initial_metadata.send_initial_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1493 | &call->metadata_batch[0 /* is_receiving */][0 /* is_trailing */]; |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1494 | stream_op_payload->send_initial_metadata.send_initial_metadata_flags = |
| 1495 | op->flags; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1496 | break; |
| 1497 | case GRPC_OP_SEND_MESSAGE: |
| 1498 | if (!are_write_flags_valid(op->flags)) { |
| 1499 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1500 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1501 | } |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1502 | if (op->data.send_message.send_message == NULL) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1503 | error = GRPC_CALL_ERROR_INVALID_MESSAGE; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1504 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1505 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1506 | if (call->sending_message) { |
| 1507 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1508 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1509 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1510 | stream_op->send_message = true; |
Craig Tiller | ea54b8c | 2017-03-01 16:58:28 -0800 | [diff] [blame] | 1511 | call->sending_message = true; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1512 | grpc_slice_buffer_stream_init( |
| 1513 | &call->sending_stream, |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1514 | &op->data.send_message.send_message->data.raw.slice_buffer, |
| 1515 | op->flags); |
Lizan Zhou | 61f0973 | 2016-10-26 14:09:52 -0700 | [diff] [blame] | 1516 | /* If the outgoing buffer is already compressed, mark it as so in the |
| 1517 | flags. These will be picked up by the compression filter and further |
| 1518 | (wasteful) attempts at compression skipped. */ |
Mark D. Roth | 9d76dbe | 2017-01-25 15:02:56 -0800 | [diff] [blame] | 1519 | if (op->data.send_message.send_message->data.raw.compression > |
| 1520 | GRPC_COMPRESS_NONE) { |
Lizan Zhou | 61f0973 | 2016-10-26 14:09:52 -0700 | [diff] [blame] | 1521 | call->sending_stream.base.flags |= GRPC_WRITE_INTERNAL_COMPRESS; |
| 1522 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1523 | stream_op_payload->send_message.send_message = |
| 1524 | &call->sending_stream.base; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1525 | break; |
| 1526 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 1527 | /* Flag validation: currently allow no flags */ |
| 1528 | if (op->flags != 0) { |
| 1529 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1530 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1531 | } |
| 1532 | if (!call->is_client) { |
| 1533 | error = GRPC_CALL_ERROR_NOT_ON_SERVER; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1534 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1535 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1536 | if (call->sent_final_op) { |
| 1537 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1538 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1539 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1540 | stream_op->send_trailing_metadata = true; |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 1541 | call->sent_final_op = true; |
Craig Tiller | 2d43fbf | 2017-03-13 16:10:05 -0700 | [diff] [blame] | 1542 | stream_op_payload->send_trailing_metadata.send_trailing_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1543 | &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1544 | break; |
| 1545 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 1546 | /* Flag validation: currently allow no flags */ |
| 1547 | if (op->flags != 0) { |
| 1548 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1549 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1550 | } |
| 1551 | if (call->is_client) { |
| 1552 | error = GRPC_CALL_ERROR_NOT_ON_CLIENT; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1553 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1554 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1555 | if (call->sent_final_op) { |
| 1556 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1557 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1558 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1559 | if (op->data.send_status_from_server.trailing_metadata_count > |
| 1560 | INT_MAX) { |
| 1561 | error = GRPC_CALL_ERROR_INVALID_METADATA; |
| 1562 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1563 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1564 | stream_op->send_trailing_metadata = true; |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 1565 | call->sent_final_op = true; |
Craig Tiller | 93727aa | 2017-02-06 13:05:39 -0800 | [diff] [blame] | 1566 | GPR_ASSERT(call->send_extra_metadata_count == 0); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1567 | call->send_extra_metadata_count = 1; |
| 1568 | call->send_extra_metadata[0].md = grpc_channel_get_reffed_status_elem( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 1569 | exec_ctx, call->channel, op->data.send_status_from_server.status); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 1570 | { |
| 1571 | grpc_error *override_error = GRPC_ERROR_NONE; |
| 1572 | if (op->data.send_status_from_server.status != GRPC_STATUS_OK) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 1573 | override_error = GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 1574 | "Error from server send status"); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 1575 | } |
| 1576 | if (op->data.send_status_from_server.status_details != NULL) { |
| 1577 | call->send_extra_metadata[1].md = grpc_mdelem_from_slices( |
| 1578 | exec_ctx, GRPC_MDSTR_GRPC_MESSAGE, |
| 1579 | grpc_slice_ref_internal( |
| 1580 | *op->data.send_status_from_server.status_details)); |
| 1581 | call->send_extra_metadata_count++; |
| 1582 | char *msg = grpc_slice_to_c_string( |
| 1583 | GRPC_MDVALUE(call->send_extra_metadata[1].md)); |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 1584 | override_error = |
| 1585 | grpc_error_set_str(override_error, GRPC_ERROR_STR_GRPC_MESSAGE, |
| 1586 | grpc_slice_from_copied_string(msg)); |
Craig Tiller | 841a99d | 2016-12-12 16:58:57 -0800 | [diff] [blame] | 1587 | gpr_free(msg); |
| 1588 | } |
| 1589 | set_status_from_error(exec_ctx, call, STATUS_FROM_API_OVERRIDE, |
| 1590 | override_error); |
Craig Tiller | 69a1f66 | 2016-09-28 10:24:21 -0700 | [diff] [blame] | 1591 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1592 | if (!prepare_application_metadata( |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 1593 | exec_ctx, call, |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1594 | (int)op->data.send_status_from_server.trailing_metadata_count, |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1595 | op->data.send_status_from_server.trailing_metadata, 1, 1, NULL, |
| 1596 | 0)) { |
Craig Tiller | 93727aa | 2017-02-06 13:05:39 -0800 | [diff] [blame] | 1597 | for (int n = 0; n < call->send_extra_metadata_count; n++) { |
| 1598 | GRPC_MDELEM_UNREF(exec_ctx, call->send_extra_metadata[n].md); |
| 1599 | } |
| 1600 | call->send_extra_metadata_count = 0; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1601 | error = GRPC_CALL_ERROR_INVALID_METADATA; |
| 1602 | goto done_with_error; |
| 1603 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1604 | stream_op_payload->send_trailing_metadata.send_trailing_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1605 | &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1606 | break; |
| 1607 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 1608 | /* Flag validation: currently allow no flags */ |
| 1609 | if (op->flags != 0) { |
| 1610 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1611 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1612 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1613 | if (call->received_initial_metadata) { |
| 1614 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1615 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1616 | } |
Craig Tiller | 9c5318a | 2016-12-05 15:07:04 -0800 | [diff] [blame] | 1617 | /* IF this is a server, then GRPC_OP_RECV_INITIAL_METADATA *must* come |
| 1618 | from server.c. In that case, it's coming from accept_stream, and in |
| 1619 | that case we're not necessarily covered by a poller. */ |
| 1620 | stream_op->covered_by_poller = call->is_client; |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 1621 | call->received_initial_metadata = true; |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1622 | call->buffered_metadata[0] = |
| 1623 | op->data.recv_initial_metadata.recv_initial_metadata; |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1624 | grpc_closure_init(&call->receiving_initial_metadata_ready, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 1625 | receiving_initial_metadata_ready, bctl, |
| 1626 | grpc_schedule_on_exec_ctx); |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1627 | stream_op->recv_initial_metadata = true; |
| 1628 | stream_op_payload->recv_initial_metadata.recv_initial_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1629 | &call->metadata_batch[1 /* is_receiving */][0 /* is_trailing */]; |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1630 | stream_op_payload->recv_initial_metadata.recv_initial_metadata_ready = |
Craig Tiller | a44cbfc | 2016-02-03 16:02:49 -0800 | [diff] [blame] | 1631 | &call->receiving_initial_metadata_ready; |
| 1632 | num_completion_callbacks_needed++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1633 | break; |
| 1634 | case GRPC_OP_RECV_MESSAGE: |
| 1635 | /* Flag validation: currently allow no flags */ |
| 1636 | if (op->flags != 0) { |
| 1637 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1638 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1639 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1640 | if (call->receiving_message) { |
| 1641 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
yang-g | 48f3a71 | 2015-12-07 11:23:50 -0800 | [diff] [blame] | 1642 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1643 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1644 | call->receiving_message = true; |
| 1645 | stream_op->recv_message = true; |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 1646 | call->receiving_buffer = op->data.recv_message.recv_message; |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1647 | stream_op_payload->recv_message.recv_message = &call->receiving_stream; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1648 | grpc_closure_init(&call->receiving_stream_ready, receiving_stream_ready, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 1649 | bctl, grpc_schedule_on_exec_ctx); |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1650 | stream_op_payload->recv_message.recv_message_ready = |
| 1651 | &call->receiving_stream_ready; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1652 | num_completion_callbacks_needed++; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1653 | break; |
| 1654 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 1655 | /* Flag validation: currently allow no flags */ |
| 1656 | if (op->flags != 0) { |
| 1657 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1658 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1659 | } |
| 1660 | if (!call->is_client) { |
| 1661 | error = GRPC_CALL_ERROR_NOT_ON_SERVER; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1662 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1663 | } |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 1664 | if (call->requested_final_op) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1665 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1666 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1667 | } |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 1668 | call->requested_final_op = true; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1669 | call->buffered_metadata[1] = |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1670 | op->data.recv_status_on_client.trailing_metadata; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1671 | call->final_op.client.status = op->data.recv_status_on_client.status; |
| 1672 | call->final_op.client.status_details = |
| 1673 | op->data.recv_status_on_client.status_details; |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1674 | stream_op->recv_trailing_metadata = true; |
| 1675 | stream_op->collect_stats = true; |
| 1676 | stream_op_payload->recv_trailing_metadata.recv_trailing_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1677 | &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */]; |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1678 | stream_op_payload->collect_stats.collect_stats = |
David Garcia Quintas | 5dde14c | 2016-07-28 17:29:27 -0700 | [diff] [blame] | 1679 | &call->final_info.stats.transport_stream_stats; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1680 | break; |
| 1681 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 1682 | /* Flag validation: currently allow no flags */ |
| 1683 | if (op->flags != 0) { |
| 1684 | error = GRPC_CALL_ERROR_INVALID_FLAGS; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1685 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1686 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1687 | if (call->is_client) { |
| 1688 | error = GRPC_CALL_ERROR_NOT_ON_CLIENT; |
| 1689 | goto done_with_error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1690 | } |
Craig Tiller | 1cbf576 | 2016-04-22 16:02:55 -0700 | [diff] [blame] | 1691 | if (call->requested_final_op) { |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1692 | error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS; |
| 1693 | goto done_with_error; |
| 1694 | } |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 1695 | call->requested_final_op = true; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1696 | call->final_op.server.cancelled = |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1697 | op->data.recv_close_on_server.cancelled; |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1698 | stream_op->recv_trailing_metadata = true; |
| 1699 | stream_op->collect_stats = true; |
| 1700 | stream_op_payload->recv_trailing_metadata.recv_trailing_metadata = |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1701 | &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */]; |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1702 | stream_op_payload->collect_stats.collect_stats = |
David Garcia Quintas | 5dde14c | 2016-07-28 17:29:27 -0700 | [diff] [blame] | 1703 | &call->final_info.stats.transport_stream_stats; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1704 | break; |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1705 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1706 | } |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1707 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1708 | GRPC_CALL_INTERNAL_REF(call, "completion"); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1709 | if (!is_notify_tag_closure) { |
Craig Tiller | 4bf2928 | 2015-12-14 11:25:48 -0800 | [diff] [blame] | 1710 | grpc_cq_begin_op(call->cq, notify_tag); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1711 | } |
| 1712 | gpr_ref_init(&bctl->steps_to_complete, num_completion_callbacks_needed); |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1713 | |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 1714 | grpc_closure_init(&bctl->finish_batch, finish_batch, bctl, |
| 1715 | grpc_schedule_on_exec_ctx); |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1716 | stream_op->on_complete = &bctl->finish_batch; |
Craig Tiller | b597dcf | 2017-03-09 07:02:11 -0800 | [diff] [blame] | 1717 | gpr_atm_rel_store(&call->any_ops_sent_atm, 1); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1718 | |
Craig Tiller | 6e7b45e | 2016-07-08 17:25:49 -0700 | [diff] [blame] | 1719 | execute_op(exec_ctx, call, stream_op); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1720 | |
Craig Tiller | 3ffd822 | 2015-09-21 08:21:57 -0700 | [diff] [blame] | 1721 | done: |
Craig Tiller | 0ba432d | 2015-10-09 16:57:11 -0700 | [diff] [blame] | 1722 | GPR_TIMER_END("grpc_call_start_batch", 0); |
Craig Tiller | 3ffd822 | 2015-09-21 08:21:57 -0700 | [diff] [blame] | 1723 | return error; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1724 | |
| 1725 | done_with_error: |
| 1726 | /* reverse any mutations that occured */ |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1727 | if (stream_op->send_initial_metadata) { |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 1728 | call->sent_initial_metadata = false; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 1729 | grpc_metadata_batch_clear(exec_ctx, &call->metadata_batch[0][0]); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1730 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1731 | if (stream_op->send_message) { |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 1732 | call->sending_message = false; |
Craig Tiller | 3b66ab9 | 2015-12-09 19:42:22 -0800 | [diff] [blame] | 1733 | grpc_byte_stream_destroy(exec_ctx, &call->sending_stream.base); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1734 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1735 | if (stream_op->send_trailing_metadata) { |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 1736 | call->sent_final_op = false; |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 1737 | grpc_metadata_batch_clear(exec_ctx, &call->metadata_batch[0][1]); |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1738 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1739 | if (stream_op->recv_initial_metadata) { |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 1740 | call->received_initial_metadata = false; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1741 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1742 | if (stream_op->recv_message) { |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 1743 | call->receiving_message = false; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1744 | } |
Craig Tiller | 9c1ec54 | 2017-03-02 08:42:54 -0800 | [diff] [blame] | 1745 | if (stream_op->recv_trailing_metadata) { |
Craig Tiller | e198b71 | 2017-03-31 15:29:33 -0700 | [diff] [blame] | 1746 | call->requested_final_op = false; |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1747 | } |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1748 | goto done; |
| 1749 | } |
| 1750 | |
| 1751 | grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, |
| 1752 | size_t nops, void *tag, void *reserved) { |
| 1753 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 1754 | grpc_call_error err; |
| 1755 | |
| 1756 | GRPC_API_TRACE( |
David Garcia Quintas | 4612337 | 2016-05-09 15:28:42 -0700 | [diff] [blame] | 1757 | "grpc_call_start_batch(call=%p, ops=%p, nops=%lu, tag=%p, " |
| 1758 | "reserved=%p)", |
Craig Tiller | c7e1a2a | 2015-11-02 14:17:32 -0800 | [diff] [blame] | 1759 | 5, (call, ops, (unsigned long)nops, tag, reserved)); |
| 1760 | |
| 1761 | if (reserved != NULL) { |
| 1762 | err = GRPC_CALL_ERROR; |
| 1763 | } else { |
| 1764 | err = call_start_batch(&exec_ctx, call, ops, nops, tag, 0); |
| 1765 | } |
| 1766 | |
| 1767 | grpc_exec_ctx_finish(&exec_ctx); |
| 1768 | return err; |
| 1769 | } |
| 1770 | |
| 1771 | grpc_call_error grpc_call_start_batch_and_execute(grpc_exec_ctx *exec_ctx, |
| 1772 | grpc_call *call, |
| 1773 | const grpc_op *ops, |
| 1774 | size_t nops, |
| 1775 | grpc_closure *closure) { |
| 1776 | return call_start_batch(exec_ctx, call, ops, nops, closure, 1); |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 1777 | } |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 1778 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1779 | void grpc_call_context_set(grpc_call *call, grpc_context_index elem, |
| 1780 | void *value, void (*destroy)(void *value)) { |
| 1781 | if (call->context[elem].destroy) { |
| 1782 | call->context[elem].destroy(call->context[elem].value); |
| 1783 | } |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 1784 | call->context[elem].value = value; |
| 1785 | call->context[elem].destroy = destroy; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 1786 | } |
| 1787 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1788 | void *grpc_call_context_get(grpc_call *call, grpc_context_index elem) { |
Julien Boeuf | 83b0297 | 2015-05-20 22:50:34 -0700 | [diff] [blame] | 1789 | return call->context[elem].value; |
Craig Tiller | 935cf42 | 2015-05-01 14:10:46 -0700 | [diff] [blame] | 1790 | } |
Julien Boeuf | 9f218dd | 2015-04-23 10:24:02 -0700 | [diff] [blame] | 1791 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 1792 | uint8_t grpc_call_is_client(grpc_call *call) { return call->is_client; } |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 1793 | |
| 1794 | grpc_compression_algorithm grpc_call_compression_for_level( |
| 1795 | grpc_call *call, grpc_compression_level level) { |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1796 | grpc_compression_algorithm algo = |
| 1797 | compression_algorithm_for_level_locked(call, level); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 1798 | return algo; |
David Garcia Quintas | 13c2f6e | 2016-03-17 22:51:52 -0700 | [diff] [blame] | 1799 | } |
Yuchen Zeng | 2e7d957 | 2016-04-15 17:29:57 -0700 | [diff] [blame] | 1800 | |
| 1801 | const char *grpc_call_error_to_string(grpc_call_error error) { |
| 1802 | switch (error) { |
| 1803 | case GRPC_CALL_ERROR: |
| 1804 | return "GRPC_CALL_ERROR"; |
| 1805 | case GRPC_CALL_ERROR_ALREADY_ACCEPTED: |
| 1806 | return "GRPC_CALL_ERROR_ALREADY_ACCEPTED"; |
| 1807 | case GRPC_CALL_ERROR_ALREADY_FINISHED: |
| 1808 | return "GRPC_CALL_ERROR_ALREADY_FINISHED"; |
| 1809 | case GRPC_CALL_ERROR_ALREADY_INVOKED: |
| 1810 | return "GRPC_CALL_ERROR_ALREADY_INVOKED"; |
| 1811 | case GRPC_CALL_ERROR_BATCH_TOO_BIG: |
| 1812 | return "GRPC_CALL_ERROR_BATCH_TOO_BIG"; |
| 1813 | case GRPC_CALL_ERROR_INVALID_FLAGS: |
| 1814 | return "GRPC_CALL_ERROR_INVALID_FLAGS"; |
| 1815 | case GRPC_CALL_ERROR_INVALID_MESSAGE: |
| 1816 | return "GRPC_CALL_ERROR_INVALID_MESSAGE"; |
| 1817 | case GRPC_CALL_ERROR_INVALID_METADATA: |
| 1818 | return "GRPC_CALL_ERROR_INVALID_METADATA"; |
| 1819 | case GRPC_CALL_ERROR_NOT_INVOKED: |
| 1820 | return "GRPC_CALL_ERROR_NOT_INVOKED"; |
| 1821 | case GRPC_CALL_ERROR_NOT_ON_CLIENT: |
| 1822 | return "GRPC_CALL_ERROR_NOT_ON_CLIENT"; |
| 1823 | case GRPC_CALL_ERROR_NOT_ON_SERVER: |
| 1824 | return "GRPC_CALL_ERROR_NOT_ON_SERVER"; |
| 1825 | case GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE: |
| 1826 | return "GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE"; |
| 1827 | case GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH: |
| 1828 | return "GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH"; |
| 1829 | case GRPC_CALL_ERROR_TOO_MANY_OPERATIONS: |
| 1830 | return "GRPC_CALL_ERROR_TOO_MANY_OPERATIONS"; |
| 1831 | case GRPC_CALL_OK: |
| 1832 | return "GRPC_CALL_OK"; |
Yuchen Zeng | 2e7d957 | 2016-04-15 17:29:57 -0700 | [diff] [blame] | 1833 | } |
Yuchen Zeng | f02bada | 2016-04-19 14:12:27 -0700 | [diff] [blame] | 1834 | GPR_UNREACHABLE_CODE(return "GRPC_CALL_ERROR_UNKNOW"); |
Yuchen Zeng | 2e7d957 | 2016-04-15 17:29:57 -0700 | [diff] [blame] | 1835 | } |