call.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  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. */
  33. #include <assert.h>
  34. #include <limits.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <grpc/compression.h>
  39. #include <grpc/grpc.h>
  40. #include <grpc/support/alloc.h>
  41. #include <grpc/support/log.h>
  42. #include <grpc/support/string_util.h>
  43. #include <grpc/support/useful.h>
  44. #include "src/core/lib/channel/channel_stack.h"
  45. #include "src/core/lib/compression/algorithm_metadata.h"
  46. #include "src/core/lib/iomgr/timer.h"
  47. #include "src/core/lib/profiling/timers.h"
  48. #include "src/core/lib/support/string.h"
  49. #include "src/core/lib/surface/api_trace.h"
  50. #include "src/core/lib/surface/call.h"
  51. #include "src/core/lib/surface/channel.h"
  52. #include "src/core/lib/surface/completion_queue.h"
  53. #include "src/core/lib/transport/static_metadata.h"
  54. /** The maximum number of concurrent batches possible.
  55. Based upon the maximum number of individually queueable ops in the batch
  56. api:
  57. - initial metadata send
  58. - message send
  59. - status/close send (depending on client/server)
  60. - initial metadata recv
  61. - message recv
  62. - status/close recv (depending on client/server) */
  63. #define MAX_CONCURRENT_BATCHES 6
  64. typedef struct {
  65. grpc_ioreq_completion_func on_complete;
  66. void *user_data;
  67. int success;
  68. } completed_request;
  69. #define MAX_SEND_EXTRA_METADATA_COUNT 3
  70. /* Status data for a request can come from several sources; this
  71. enumerates them all, and acts as a priority sorting for which
  72. status to return to the application - earlier entries override
  73. later ones */
  74. typedef enum {
  75. /* Status came from the application layer overriding whatever
  76. the wire says */
  77. STATUS_FROM_API_OVERRIDE = 0,
  78. /* Status came from 'the wire' - or somewhere below the surface
  79. layer */
  80. STATUS_FROM_WIRE,
  81. /* Status was created by some internal channel stack operation */
  82. STATUS_FROM_CORE,
  83. /* Status came from the server sending status */
  84. STATUS_FROM_SERVER_STATUS,
  85. STATUS_SOURCE_COUNT
  86. } status_source;
  87. typedef struct {
  88. uint8_t is_set;
  89. grpc_status_code code;
  90. grpc_mdstr *details;
  91. } received_status;
  92. /* How far through the GRPC stream have we read? */
  93. typedef enum {
  94. /* We are still waiting for initial metadata to complete */
  95. READ_STATE_INITIAL = 0,
  96. /* We have gotten initial metadata, and are reading either
  97. messages or trailing metadata */
  98. READ_STATE_GOT_INITIAL_METADATA,
  99. /* The stream is closed for reading */
  100. READ_STATE_READ_CLOSED,
  101. /* The stream is closed for reading & writing */
  102. READ_STATE_STREAM_CLOSED
  103. } read_state;
  104. typedef enum {
  105. WRITE_STATE_INITIAL = 0,
  106. WRITE_STATE_STARTED,
  107. WRITE_STATE_WRITE_CLOSED
  108. } write_state;
  109. typedef struct batch_control {
  110. grpc_call *call;
  111. grpc_cq_completion cq_completion;
  112. grpc_closure finish_batch;
  113. void *notify_tag;
  114. gpr_refcount steps_to_complete;
  115. uint8_t send_initial_metadata;
  116. uint8_t send_message;
  117. uint8_t send_final_op;
  118. uint8_t recv_initial_metadata;
  119. uint8_t recv_message;
  120. uint8_t recv_final_op;
  121. uint8_t is_notify_tag_closure;
  122. uint8_t success;
  123. } batch_control;
  124. struct grpc_call {
  125. grpc_completion_queue *cq;
  126. grpc_channel *channel;
  127. grpc_call *parent;
  128. grpc_call *first_child;
  129. /* TODO(ctiller): share with cq if possible? */
  130. gpr_mu mu;
  131. /* client or server call */
  132. uint8_t is_client;
  133. /* is the alarm set */
  134. uint8_t have_alarm;
  135. /** has grpc_call_destroy been called */
  136. uint8_t destroy_called;
  137. /** flag indicating that cancellation is inherited */
  138. uint8_t cancellation_is_inherited;
  139. /** bitmask of live batches */
  140. uint8_t used_batches;
  141. /** which ops are in-flight */
  142. uint8_t sent_initial_metadata;
  143. uint8_t sending_message;
  144. uint8_t sent_final_op;
  145. uint8_t received_initial_metadata;
  146. uint8_t receiving_message;
  147. uint8_t received_final_op;
  148. /* have we received initial metadata */
  149. bool has_initial_md_been_received;
  150. batch_control active_batches[MAX_CONCURRENT_BATCHES];
  151. /* first idx: is_receiving, second idx: is_trailing */
  152. grpc_metadata_batch metadata_batch[2][2];
  153. /* Buffered read metadata waiting to be returned to the application.
  154. Element 0 is initial metadata, element 1 is trailing metadata. */
  155. grpc_metadata_array *buffered_metadata[2];
  156. /* Received call statuses from various sources */
  157. received_status status[STATUS_SOURCE_COUNT];
  158. /* Call stats: only valid after trailing metadata received */
  159. grpc_transport_stream_stats stats;
  160. /* Compression algorithm for the call */
  161. grpc_compression_algorithm compression_algorithm;
  162. /* Supported encodings (compression algorithms), a bitset */
  163. uint32_t encodings_accepted_by_peer;
  164. /* Contexts for various subsystems (security, tracing, ...). */
  165. grpc_call_context_element context[GRPC_CONTEXT_COUNT];
  166. /* Deadline alarm - if have_alarm is non-zero */
  167. grpc_timer alarm;
  168. /* for the client, extra metadata is initial metadata; for the
  169. server, it's trailing metadata */
  170. grpc_linked_mdelem send_extra_metadata[MAX_SEND_EXTRA_METADATA_COUNT];
  171. int send_extra_metadata_count;
  172. gpr_timespec send_deadline;
  173. /** siblings: children of the same parent form a list, and this list is
  174. protected under
  175. parent->mu */
  176. grpc_call *sibling_next;
  177. grpc_call *sibling_prev;
  178. grpc_slice_buffer_stream sending_stream;
  179. grpc_byte_stream *receiving_stream;
  180. grpc_byte_buffer **receiving_buffer;
  181. gpr_slice receiving_slice;
  182. grpc_closure receiving_slice_ready;
  183. grpc_closure receiving_stream_ready;
  184. grpc_closure receiving_initial_metadata_ready;
  185. uint32_t test_only_last_message_flags;
  186. union {
  187. struct {
  188. grpc_status_code *status;
  189. char **status_details;
  190. size_t *status_details_capacity;
  191. } client;
  192. struct {
  193. int *cancelled;
  194. } server;
  195. } final_op;
  196. struct {
  197. void *bctlp;
  198. bool success;
  199. } saved_receiving_stream_ready_ctx;
  200. };
  201. #define CALL_STACK_FROM_CALL(call) ((grpc_call_stack *)((call) + 1))
  202. #define CALL_FROM_CALL_STACK(call_stack) (((grpc_call *)(call_stack)) - 1)
  203. #define CALL_ELEM_FROM_CALL(call, idx) \
  204. grpc_call_stack_element(CALL_STACK_FROM_CALL(call), idx)
  205. #define CALL_FROM_TOP_ELEM(top_elem) \
  206. CALL_FROM_CALL_STACK(grpc_call_stack_from_top_element(top_elem))
  207. static void set_deadline_alarm(grpc_exec_ctx *exec_ctx, grpc_call *call,
  208. gpr_timespec deadline);
  209. static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call,
  210. grpc_transport_stream_op *op);
  211. static grpc_call_error cancel_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c,
  212. grpc_status_code status,
  213. const char *description);
  214. static void destroy_call(grpc_exec_ctx *exec_ctx, void *call_stack,
  215. bool success);
  216. static void receiving_slice_ready(grpc_exec_ctx *exec_ctx, void *bctlp,
  217. bool success);
  218. grpc_call *grpc_call_create(grpc_channel *channel, grpc_call *parent_call,
  219. uint32_t propagation_mask,
  220. grpc_completion_queue *cq,
  221. const void *server_transport_data,
  222. grpc_mdelem **add_initial_metadata,
  223. size_t add_initial_metadata_count,
  224. gpr_timespec send_deadline) {
  225. size_t i, j;
  226. grpc_channel_stack *channel_stack = grpc_channel_get_channel_stack(channel);
  227. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  228. grpc_call *call;
  229. GPR_TIMER_BEGIN("grpc_call_create", 0);
  230. call = gpr_malloc(sizeof(grpc_call) + channel_stack->call_stack_size);
  231. memset(call, 0, sizeof(grpc_call));
  232. gpr_mu_init(&call->mu);
  233. call->channel = channel;
  234. call->cq = cq;
  235. call->parent = parent_call;
  236. call->is_client = server_transport_data == NULL;
  237. if (call->is_client) {
  238. GPR_ASSERT(add_initial_metadata_count < MAX_SEND_EXTRA_METADATA_COUNT);
  239. for (i = 0; i < add_initial_metadata_count; i++) {
  240. call->send_extra_metadata[i].md = add_initial_metadata[i];
  241. }
  242. call->send_extra_metadata_count = (int)add_initial_metadata_count;
  243. } else {
  244. GPR_ASSERT(add_initial_metadata_count == 0);
  245. call->send_extra_metadata_count = 0;
  246. }
  247. for (i = 0; i < 2; i++) {
  248. for (j = 0; j < 2; j++) {
  249. call->metadata_batch[i][j].deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  250. }
  251. }
  252. call->send_deadline = send_deadline;
  253. GRPC_CHANNEL_INTERNAL_REF(channel, "call");
  254. /* initial refcount dropped by grpc_call_destroy */
  255. grpc_call_stack_init(&exec_ctx, channel_stack, 1, destroy_call, call,
  256. call->context, server_transport_data,
  257. CALL_STACK_FROM_CALL(call));
  258. if (cq != NULL) {
  259. GRPC_CQ_INTERNAL_REF(cq, "bind");
  260. grpc_call_stack_set_pollset(&exec_ctx, CALL_STACK_FROM_CALL(call),
  261. grpc_cq_pollset(cq));
  262. }
  263. if (parent_call != NULL) {
  264. GRPC_CALL_INTERNAL_REF(parent_call, "child");
  265. GPR_ASSERT(call->is_client);
  266. GPR_ASSERT(!parent_call->is_client);
  267. gpr_mu_lock(&parent_call->mu);
  268. if (propagation_mask & GRPC_PROPAGATE_DEADLINE) {
  269. send_deadline = gpr_time_min(
  270. gpr_convert_clock_type(send_deadline,
  271. parent_call->send_deadline.clock_type),
  272. parent_call->send_deadline);
  273. }
  274. /* for now GRPC_PROPAGATE_TRACING_CONTEXT *MUST* be passed with
  275. * GRPC_PROPAGATE_STATS_CONTEXT */
  276. /* TODO(ctiller): This should change to use the appropriate census start_op
  277. * call. */
  278. if (propagation_mask & GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT) {
  279. GPR_ASSERT(propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT);
  280. grpc_call_context_set(call, GRPC_CONTEXT_TRACING,
  281. parent_call->context[GRPC_CONTEXT_TRACING].value,
  282. NULL);
  283. } else {
  284. GPR_ASSERT(propagation_mask & GRPC_PROPAGATE_CENSUS_STATS_CONTEXT);
  285. }
  286. if (propagation_mask & GRPC_PROPAGATE_CANCELLATION) {
  287. call->cancellation_is_inherited = 1;
  288. }
  289. if (parent_call->first_child == NULL) {
  290. parent_call->first_child = call;
  291. call->sibling_next = call->sibling_prev = call;
  292. } else {
  293. call->sibling_next = parent_call->first_child;
  294. call->sibling_prev = parent_call->first_child->sibling_prev;
  295. call->sibling_next->sibling_prev = call->sibling_prev->sibling_next =
  296. call;
  297. }
  298. gpr_mu_unlock(&parent_call->mu);
  299. }
  300. if (gpr_time_cmp(send_deadline, gpr_inf_future(send_deadline.clock_type)) !=
  301. 0) {
  302. set_deadline_alarm(&exec_ctx, call, send_deadline);
  303. }
  304. grpc_exec_ctx_finish(&exec_ctx);
  305. GPR_TIMER_END("grpc_call_create", 0);
  306. return call;
  307. }
  308. void grpc_call_set_completion_queue(grpc_exec_ctx *exec_ctx, grpc_call *call,
  309. grpc_completion_queue *cq) {
  310. GPR_ASSERT(cq);
  311. call->cq = cq;
  312. GRPC_CQ_INTERNAL_REF(cq, "bind");
  313. grpc_call_stack_set_pollset(exec_ctx, CALL_STACK_FROM_CALL(call),
  314. grpc_cq_pollset(cq));
  315. }
  316. #ifdef GRPC_STREAM_REFCOUNT_DEBUG
  317. #define REF_REASON reason
  318. #define REF_ARG , const char *reason
  319. #else
  320. #define REF_REASON ""
  321. #define REF_ARG
  322. #endif
  323. void grpc_call_internal_ref(grpc_call *c REF_ARG) {
  324. GRPC_CALL_STACK_REF(CALL_STACK_FROM_CALL(c), REF_REASON);
  325. }
  326. void grpc_call_internal_unref(grpc_exec_ctx *exec_ctx, grpc_call *c REF_ARG) {
  327. GRPC_CALL_STACK_UNREF(exec_ctx, CALL_STACK_FROM_CALL(c), REF_REASON);
  328. }
  329. static void destroy_call(grpc_exec_ctx *exec_ctx, void *call, bool success) {
  330. size_t i;
  331. int ii;
  332. grpc_call *c = call;
  333. GPR_TIMER_BEGIN("destroy_call", 0);
  334. for (i = 0; i < 2; i++) {
  335. grpc_metadata_batch_destroy(
  336. &c->metadata_batch[1 /* is_receiving */][i /* is_initial */]);
  337. }
  338. if (c->receiving_stream != NULL) {
  339. grpc_byte_stream_destroy(exec_ctx, c->receiving_stream);
  340. }
  341. grpc_call_stack_destroy(exec_ctx, CALL_STACK_FROM_CALL(c));
  342. GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, c->channel, "call");
  343. gpr_mu_destroy(&c->mu);
  344. for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
  345. if (c->status[i].details) {
  346. GRPC_MDSTR_UNREF(c->status[i].details);
  347. }
  348. }
  349. for (ii = 0; ii < c->send_extra_metadata_count; ii++) {
  350. GRPC_MDELEM_UNREF(c->send_extra_metadata[ii].md);
  351. }
  352. for (i = 0; i < GRPC_CONTEXT_COUNT; i++) {
  353. if (c->context[i].destroy) {
  354. c->context[i].destroy(c->context[i].value);
  355. }
  356. }
  357. if (c->cq) {
  358. GRPC_CQ_INTERNAL_UNREF(c->cq, "bind");
  359. }
  360. gpr_free(c);
  361. GPR_TIMER_END("destroy_call", 0);
  362. }
  363. static void set_status_code(grpc_call *call, status_source source,
  364. uint32_t status) {
  365. if (call->status[source].is_set) return;
  366. call->status[source].is_set = 1;
  367. call->status[source].code = (grpc_status_code)status;
  368. /* TODO(ctiller): what to do about the flush that was previously here */
  369. }
  370. static void set_compression_algorithm(grpc_call *call,
  371. grpc_compression_algorithm algo) {
  372. call->compression_algorithm = algo;
  373. }
  374. grpc_compression_algorithm grpc_call_test_only_get_compression_algorithm(
  375. grpc_call *call) {
  376. grpc_compression_algorithm algorithm;
  377. gpr_mu_lock(&call->mu);
  378. algorithm = call->compression_algorithm;
  379. gpr_mu_unlock(&call->mu);
  380. return algorithm;
  381. }
  382. uint32_t grpc_call_test_only_get_message_flags(grpc_call *call) {
  383. uint32_t flags;
  384. gpr_mu_lock(&call->mu);
  385. flags = call->test_only_last_message_flags;
  386. gpr_mu_unlock(&call->mu);
  387. return flags;
  388. }
  389. static void destroy_encodings_accepted_by_peer(void *p) { return; }
  390. static void set_encodings_accepted_by_peer(grpc_call *call, grpc_mdelem *mdel) {
  391. size_t i;
  392. grpc_compression_algorithm algorithm;
  393. gpr_slice_buffer accept_encoding_parts;
  394. gpr_slice accept_encoding_slice;
  395. void *accepted_user_data;
  396. accepted_user_data =
  397. grpc_mdelem_get_user_data(mdel, destroy_encodings_accepted_by_peer);
  398. if (accepted_user_data != NULL) {
  399. call->encodings_accepted_by_peer =
  400. (uint32_t)(((uintptr_t)accepted_user_data) - 1);
  401. return;
  402. }
  403. accept_encoding_slice = mdel->value->slice;
  404. gpr_slice_buffer_init(&accept_encoding_parts);
  405. gpr_slice_split(accept_encoding_slice, ",", &accept_encoding_parts);
  406. /* No need to zero call->encodings_accepted_by_peer: grpc_call_create already
  407. * zeroes the whole grpc_call */
  408. /* Always support no compression */
  409. GPR_BITSET(&call->encodings_accepted_by_peer, GRPC_COMPRESS_NONE);
  410. for (i = 0; i < accept_encoding_parts.count; i++) {
  411. const gpr_slice *accept_encoding_entry_slice =
  412. &accept_encoding_parts.slices[i];
  413. if (grpc_compression_algorithm_parse(
  414. (const char *)GPR_SLICE_START_PTR(*accept_encoding_entry_slice),
  415. GPR_SLICE_LENGTH(*accept_encoding_entry_slice), &algorithm)) {
  416. GPR_BITSET(&call->encodings_accepted_by_peer, algorithm);
  417. } else {
  418. char *accept_encoding_entry_str =
  419. gpr_dump_slice(*accept_encoding_entry_slice, GPR_DUMP_ASCII);
  420. gpr_log(GPR_ERROR,
  421. "Invalid entry in accept encoding metadata: '%s'. Ignoring.",
  422. accept_encoding_entry_str);
  423. gpr_free(accept_encoding_entry_str);
  424. }
  425. }
  426. gpr_slice_buffer_destroy(&accept_encoding_parts);
  427. grpc_mdelem_set_user_data(
  428. mdel, destroy_encodings_accepted_by_peer,
  429. (void *)(((uintptr_t)call->encodings_accepted_by_peer) + 1));
  430. }
  431. uint32_t grpc_call_test_only_get_encodings_accepted_by_peer(grpc_call *call) {
  432. uint32_t encodings_accepted_by_peer;
  433. gpr_mu_lock(&call->mu);
  434. encodings_accepted_by_peer = call->encodings_accepted_by_peer;
  435. gpr_mu_unlock(&call->mu);
  436. return encodings_accepted_by_peer;
  437. }
  438. static void set_status_details(grpc_call *call, status_source source,
  439. grpc_mdstr *status) {
  440. if (call->status[source].details != NULL) {
  441. GRPC_MDSTR_UNREF(call->status[source].details);
  442. }
  443. call->status[source].details = status;
  444. }
  445. static void get_final_status(grpc_call *call,
  446. void (*set_value)(grpc_status_code code,
  447. void *user_data),
  448. void *set_value_user_data) {
  449. int i;
  450. for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
  451. if (call->status[i].is_set) {
  452. set_value(call->status[i].code, set_value_user_data);
  453. return;
  454. }
  455. }
  456. if (call->is_client) {
  457. set_value(GRPC_STATUS_UNKNOWN, set_value_user_data);
  458. } else {
  459. set_value(GRPC_STATUS_OK, set_value_user_data);
  460. }
  461. }
  462. static void get_final_details(grpc_call *call, char **out_details,
  463. size_t *out_details_capacity) {
  464. int i;
  465. for (i = 0; i < STATUS_SOURCE_COUNT; i++) {
  466. if (call->status[i].is_set) {
  467. if (call->status[i].details) {
  468. gpr_slice details = call->status[i].details->slice;
  469. size_t len = GPR_SLICE_LENGTH(details);
  470. if (len + 1 > *out_details_capacity) {
  471. *out_details_capacity =
  472. GPR_MAX(len + 1, *out_details_capacity * 3 / 2);
  473. *out_details = gpr_realloc(*out_details, *out_details_capacity);
  474. }
  475. memcpy(*out_details, GPR_SLICE_START_PTR(details), len);
  476. (*out_details)[len] = 0;
  477. } else {
  478. goto no_details;
  479. }
  480. return;
  481. }
  482. }
  483. no_details:
  484. if (0 == *out_details_capacity) {
  485. *out_details_capacity = 8;
  486. *out_details = gpr_malloc(*out_details_capacity);
  487. }
  488. **out_details = 0;
  489. }
  490. static grpc_linked_mdelem *linked_from_md(grpc_metadata *md) {
  491. return (grpc_linked_mdelem *)&md->internal_data;
  492. }
  493. static int prepare_application_metadata(grpc_call *call, int count,
  494. grpc_metadata *metadata,
  495. int is_trailing,
  496. int prepend_extra_metadata) {
  497. int i;
  498. grpc_metadata_batch *batch =
  499. &call->metadata_batch[0 /* is_receiving */][is_trailing];
  500. for (i = 0; i < count; i++) {
  501. grpc_metadata *md = &metadata[i];
  502. grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data;
  503. GPR_ASSERT(sizeof(grpc_linked_mdelem) == sizeof(md->internal_data));
  504. l->md = grpc_mdelem_from_string_and_buffer(
  505. md->key, (const uint8_t *)md->value, md->value_length);
  506. if (!grpc_header_key_is_legal(grpc_mdstr_as_c_string(l->md->key),
  507. GRPC_MDSTR_LENGTH(l->md->key))) {
  508. gpr_log(GPR_ERROR, "attempt to send invalid metadata key: %s",
  509. grpc_mdstr_as_c_string(l->md->key));
  510. break;
  511. } else if (!grpc_is_binary_header(grpc_mdstr_as_c_string(l->md->key),
  512. GRPC_MDSTR_LENGTH(l->md->key)) &&
  513. !grpc_header_nonbin_value_is_legal(
  514. grpc_mdstr_as_c_string(l->md->value),
  515. GRPC_MDSTR_LENGTH(l->md->value))) {
  516. gpr_log(GPR_ERROR, "attempt to send invalid metadata value");
  517. break;
  518. }
  519. }
  520. if (i != count) {
  521. for (int j = 0; j <= i; j++) {
  522. grpc_metadata *md = &metadata[i];
  523. grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data;
  524. GRPC_MDELEM_UNREF(l->md);
  525. }
  526. return 0;
  527. }
  528. if (prepend_extra_metadata) {
  529. if (call->send_extra_metadata_count == 0) {
  530. prepend_extra_metadata = 0;
  531. } else {
  532. for (i = 0; i < call->send_extra_metadata_count; i++) {
  533. GRPC_MDELEM_REF(call->send_extra_metadata[i].md);
  534. }
  535. for (i = 1; i < call->send_extra_metadata_count; i++) {
  536. call->send_extra_metadata[i].prev = &call->send_extra_metadata[i - 1];
  537. }
  538. for (i = 0; i < call->send_extra_metadata_count - 1; i++) {
  539. call->send_extra_metadata[i].next = &call->send_extra_metadata[i + 1];
  540. }
  541. }
  542. }
  543. for (i = 1; i < count; i++) {
  544. linked_from_md(&metadata[i])->prev = linked_from_md(&metadata[i - 1]);
  545. }
  546. for (i = 0; i < count - 1; i++) {
  547. linked_from_md(&metadata[i])->next = linked_from_md(&metadata[i + 1]);
  548. }
  549. switch (prepend_extra_metadata * 2 + (count != 0)) {
  550. case 0:
  551. /* no prepend, no metadata => nothing to do */
  552. batch->list.head = batch->list.tail = NULL;
  553. break;
  554. case 1:
  555. /* metadata, but no prepend */
  556. batch->list.head = linked_from_md(&metadata[0]);
  557. batch->list.tail = linked_from_md(&metadata[count - 1]);
  558. batch->list.head->prev = NULL;
  559. batch->list.tail->next = NULL;
  560. break;
  561. case 2:
  562. /* prepend, but no md */
  563. batch->list.head = &call->send_extra_metadata[0];
  564. batch->list.tail =
  565. &call->send_extra_metadata[call->send_extra_metadata_count - 1];
  566. batch->list.head->prev = NULL;
  567. batch->list.tail->next = NULL;
  568. break;
  569. case 3:
  570. /* prepend AND md */
  571. batch->list.head = &call->send_extra_metadata[0];
  572. call->send_extra_metadata[call->send_extra_metadata_count - 1].next =
  573. linked_from_md(&metadata[0]);
  574. linked_from_md(&metadata[0])->prev =
  575. &call->send_extra_metadata[call->send_extra_metadata_count - 1];
  576. batch->list.tail = linked_from_md(&metadata[count - 1]);
  577. batch->list.head->prev = NULL;
  578. batch->list.tail->next = NULL;
  579. break;
  580. default:
  581. GPR_UNREACHABLE_CODE(return 0);
  582. }
  583. return 1;
  584. }
  585. void grpc_call_destroy(grpc_call *c) {
  586. int cancel;
  587. grpc_call *parent = c->parent;
  588. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  589. GPR_TIMER_BEGIN("grpc_call_destroy", 0);
  590. GRPC_API_TRACE("grpc_call_destroy(c=%p)", 1, (c));
  591. if (parent) {
  592. gpr_mu_lock(&parent->mu);
  593. if (c == parent->first_child) {
  594. parent->first_child = c->sibling_next;
  595. if (c == parent->first_child) {
  596. parent->first_child = NULL;
  597. }
  598. c->sibling_prev->sibling_next = c->sibling_next;
  599. c->sibling_next->sibling_prev = c->sibling_prev;
  600. }
  601. gpr_mu_unlock(&parent->mu);
  602. GRPC_CALL_INTERNAL_UNREF(&exec_ctx, parent, "child");
  603. }
  604. gpr_mu_lock(&c->mu);
  605. GPR_ASSERT(!c->destroy_called);
  606. c->destroy_called = 1;
  607. if (c->have_alarm) {
  608. grpc_timer_cancel(&exec_ctx, &c->alarm);
  609. }
  610. cancel = !c->received_final_op;
  611. gpr_mu_unlock(&c->mu);
  612. if (cancel) grpc_call_cancel(c, NULL);
  613. GRPC_CALL_INTERNAL_UNREF(&exec_ctx, c, "destroy");
  614. grpc_exec_ctx_finish(&exec_ctx);
  615. GPR_TIMER_END("grpc_call_destroy", 0);
  616. }
  617. grpc_call_error grpc_call_cancel(grpc_call *call, void *reserved) {
  618. GRPC_API_TRACE("grpc_call_cancel(call=%p, reserved=%p)", 2, (call, reserved));
  619. GPR_ASSERT(!reserved);
  620. return grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED, "Cancelled",
  621. NULL);
  622. }
  623. grpc_call_error grpc_call_cancel_with_status(grpc_call *c,
  624. grpc_status_code status,
  625. const char *description,
  626. void *reserved) {
  627. grpc_call_error r;
  628. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  629. GRPC_API_TRACE(
  630. "grpc_call_cancel_with_status("
  631. "c=%p, status=%d, description=%s, reserved=%p)",
  632. 4, (c, (int)status, description, reserved));
  633. GPR_ASSERT(reserved == NULL);
  634. gpr_mu_lock(&c->mu);
  635. r = cancel_with_status(&exec_ctx, c, status, description);
  636. gpr_mu_unlock(&c->mu);
  637. grpc_exec_ctx_finish(&exec_ctx);
  638. return r;
  639. }
  640. typedef struct cancel_closure {
  641. grpc_closure closure;
  642. grpc_call *call;
  643. grpc_status_code status;
  644. } cancel_closure;
  645. static void done_cancel(grpc_exec_ctx *exec_ctx, void *ccp, bool success) {
  646. cancel_closure *cc = ccp;
  647. GRPC_CALL_INTERNAL_UNREF(exec_ctx, cc->call, "cancel");
  648. gpr_free(cc);
  649. }
  650. static void send_cancel(grpc_exec_ctx *exec_ctx, void *ccp, bool success) {
  651. grpc_transport_stream_op op;
  652. cancel_closure *cc = ccp;
  653. memset(&op, 0, sizeof(op));
  654. op.cancel_with_status = cc->status;
  655. /* reuse closure to catch completion */
  656. grpc_closure_init(&cc->closure, done_cancel, cc);
  657. op.on_complete = &cc->closure;
  658. execute_op(exec_ctx, cc->call, &op);
  659. }
  660. static grpc_call_error cancel_with_status(grpc_exec_ctx *exec_ctx, grpc_call *c,
  661. grpc_status_code status,
  662. const char *description) {
  663. grpc_mdstr *details =
  664. description ? grpc_mdstr_from_string(description) : NULL;
  665. cancel_closure *cc = gpr_malloc(sizeof(*cc));
  666. GPR_ASSERT(status != GRPC_STATUS_OK);
  667. set_status_code(c, STATUS_FROM_API_OVERRIDE, (uint32_t)status);
  668. set_status_details(c, STATUS_FROM_API_OVERRIDE, details);
  669. grpc_closure_init(&cc->closure, send_cancel, cc);
  670. cc->call = c;
  671. cc->status = status;
  672. GRPC_CALL_INTERNAL_REF(c, "cancel");
  673. grpc_exec_ctx_enqueue(exec_ctx, &cc->closure, true, NULL);
  674. return GRPC_CALL_OK;
  675. }
  676. static void execute_op(grpc_exec_ctx *exec_ctx, grpc_call *call,
  677. grpc_transport_stream_op *op) {
  678. grpc_call_element *elem;
  679. GPR_TIMER_BEGIN("execute_op", 0);
  680. elem = CALL_ELEM_FROM_CALL(call, 0);
  681. op->context = call->context;
  682. elem->filter->start_transport_stream_op(exec_ctx, elem, op);
  683. GPR_TIMER_END("execute_op", 0);
  684. }
  685. char *grpc_call_get_peer(grpc_call *call) {
  686. grpc_call_element *elem = CALL_ELEM_FROM_CALL(call, 0);
  687. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  688. char *result;
  689. GRPC_API_TRACE("grpc_call_get_peer(%p)", 1, (call));
  690. result = elem->filter->get_peer(&exec_ctx, elem);
  691. if (result == NULL) {
  692. result = grpc_channel_get_target(call->channel);
  693. }
  694. if (result == NULL) {
  695. result = gpr_strdup("unknown");
  696. }
  697. grpc_exec_ctx_finish(&exec_ctx);
  698. return result;
  699. }
  700. grpc_call *grpc_call_from_top_element(grpc_call_element *elem) {
  701. return CALL_FROM_TOP_ELEM(elem);
  702. }
  703. static void call_alarm(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
  704. grpc_call *call = arg;
  705. gpr_mu_lock(&call->mu);
  706. call->have_alarm = 0;
  707. if (success) {
  708. cancel_with_status(exec_ctx, call, GRPC_STATUS_DEADLINE_EXCEEDED,
  709. "Deadline Exceeded");
  710. }
  711. gpr_mu_unlock(&call->mu);
  712. GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "alarm");
  713. }
  714. static void set_deadline_alarm(grpc_exec_ctx *exec_ctx, grpc_call *call,
  715. gpr_timespec deadline) {
  716. if (call->have_alarm) {
  717. gpr_log(GPR_ERROR, "Attempt to set deadline alarm twice");
  718. assert(0);
  719. return;
  720. }
  721. GRPC_CALL_INTERNAL_REF(call, "alarm");
  722. call->have_alarm = 1;
  723. call->send_deadline = gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC);
  724. grpc_timer_init(exec_ctx, &call->alarm, call->send_deadline, call_alarm, call,
  725. gpr_now(GPR_CLOCK_MONOTONIC));
  726. }
  727. /* we offset status by a small amount when storing it into transport metadata
  728. as metadata cannot store a 0 value (which is used as OK for grpc_status_codes
  729. */
  730. #define STATUS_OFFSET 1
  731. static void destroy_status(void *ignored) {}
  732. static uint32_t decode_status(grpc_mdelem *md) {
  733. uint32_t status;
  734. void *user_data;
  735. if (md == GRPC_MDELEM_GRPC_STATUS_0) return 0;
  736. if (md == GRPC_MDELEM_GRPC_STATUS_1) return 1;
  737. if (md == GRPC_MDELEM_GRPC_STATUS_2) return 2;
  738. user_data = grpc_mdelem_get_user_data(md, destroy_status);
  739. if (user_data != NULL) {
  740. status = ((uint32_t)(intptr_t)user_data) - STATUS_OFFSET;
  741. } else {
  742. if (!gpr_parse_bytes_to_uint32(grpc_mdstr_as_c_string(md->value),
  743. GPR_SLICE_LENGTH(md->value->slice),
  744. &status)) {
  745. status = GRPC_STATUS_UNKNOWN; /* could not parse status code */
  746. }
  747. grpc_mdelem_set_user_data(md, destroy_status,
  748. (void *)(intptr_t)(status + STATUS_OFFSET));
  749. }
  750. return status;
  751. }
  752. static uint32_t decode_compression(grpc_mdelem *md) {
  753. grpc_compression_algorithm algorithm =
  754. grpc_compression_algorithm_from_mdstr(md->value);
  755. if (algorithm == GRPC_COMPRESS_ALGORITHMS_COUNT) {
  756. const char *md_c_str = grpc_mdstr_as_c_string(md->value);
  757. gpr_log(GPR_ERROR, "Invalid compression algorithm: '%s'", md_c_str);
  758. }
  759. return algorithm;
  760. }
  761. static grpc_mdelem *recv_common_filter(grpc_call *call, grpc_mdelem *elem) {
  762. if (elem->key == GRPC_MDSTR_GRPC_STATUS) {
  763. GPR_TIMER_BEGIN("status", 0);
  764. set_status_code(call, STATUS_FROM_WIRE, decode_status(elem));
  765. GPR_TIMER_END("status", 0);
  766. return NULL;
  767. } else if (elem->key == GRPC_MDSTR_GRPC_MESSAGE) {
  768. GPR_TIMER_BEGIN("status-details", 0);
  769. set_status_details(call, STATUS_FROM_WIRE, GRPC_MDSTR_REF(elem->value));
  770. GPR_TIMER_END("status-details", 0);
  771. return NULL;
  772. }
  773. return elem;
  774. }
  775. static grpc_mdelem *publish_app_metadata(grpc_call *call, grpc_mdelem *elem,
  776. int is_trailing) {
  777. grpc_metadata_array *dest;
  778. grpc_metadata *mdusr;
  779. GPR_TIMER_BEGIN("publish_app_metadata", 0);
  780. dest = call->buffered_metadata[is_trailing];
  781. if (dest->count == dest->capacity) {
  782. dest->capacity = GPR_MAX(dest->capacity + 8, dest->capacity * 2);
  783. dest->metadata =
  784. gpr_realloc(dest->metadata, sizeof(grpc_metadata) * dest->capacity);
  785. }
  786. mdusr = &dest->metadata[dest->count++];
  787. mdusr->key = grpc_mdstr_as_c_string(elem->key);
  788. mdusr->value = grpc_mdstr_as_c_string(elem->value);
  789. mdusr->value_length = GPR_SLICE_LENGTH(elem->value->slice);
  790. GPR_TIMER_END("publish_app_metadata", 0);
  791. return elem;
  792. }
  793. static grpc_mdelem *recv_initial_filter(void *callp, grpc_mdelem *elem) {
  794. grpc_call *call = callp;
  795. elem = recv_common_filter(call, elem);
  796. if (elem == NULL) {
  797. return NULL;
  798. } else if (elem->key == GRPC_MDSTR_GRPC_ENCODING) {
  799. GPR_TIMER_BEGIN("compression_algorithm", 0);
  800. set_compression_algorithm(call, decode_compression(elem));
  801. GPR_TIMER_END("compression_algorithm", 0);
  802. return NULL;
  803. } else if (elem->key == GRPC_MDSTR_GRPC_ACCEPT_ENCODING) {
  804. GPR_TIMER_BEGIN("encodings_accepted_by_peer", 0);
  805. set_encodings_accepted_by_peer(call, elem);
  806. GPR_TIMER_END("encodings_accepted_by_peer", 0);
  807. return NULL;
  808. } else {
  809. return publish_app_metadata(call, elem, 0);
  810. }
  811. }
  812. static grpc_mdelem *recv_trailing_filter(void *callp, grpc_mdelem *elem) {
  813. grpc_call *call = callp;
  814. elem = recv_common_filter(call, elem);
  815. if (elem == NULL) {
  816. return NULL;
  817. } else {
  818. return publish_app_metadata(call, elem, 1);
  819. }
  820. }
  821. grpc_call_stack *grpc_call_get_call_stack(grpc_call *call) {
  822. return CALL_STACK_FROM_CALL(call);
  823. }
  824. /*
  825. * BATCH API IMPLEMENTATION
  826. */
  827. static void set_status_value_directly(grpc_status_code status, void *dest) {
  828. *(grpc_status_code *)dest = status;
  829. }
  830. static void set_cancelled_value(grpc_status_code status, void *dest) {
  831. *(int *)dest = (status != GRPC_STATUS_OK);
  832. }
  833. static bool are_write_flags_valid(uint32_t flags) {
  834. /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */
  835. const uint32_t allowed_write_positions =
  836. (GRPC_WRITE_USED_MASK | GRPC_WRITE_INTERNAL_USED_MASK);
  837. const uint32_t invalid_positions = ~allowed_write_positions;
  838. return !(flags & invalid_positions);
  839. }
  840. static bool are_initial_metadata_flags_valid(uint32_t flags, bool is_client) {
  841. /* check that only bits in GRPC_WRITE_(INTERNAL?)_USED_MASK are set */
  842. uint32_t invalid_positions = ~GRPC_INITIAL_METADATA_USED_MASK;
  843. if (!is_client) {
  844. invalid_positions |= GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST;
  845. }
  846. return !(flags & invalid_positions);
  847. }
  848. static batch_control *allocate_batch_control(grpc_call *call) {
  849. size_t i;
  850. for (i = 0; i < MAX_CONCURRENT_BATCHES; i++) {
  851. if ((call->used_batches & (1 << i)) == 0) {
  852. call->used_batches = (uint8_t)(call->used_batches | (uint8_t)(1 << i));
  853. return &call->active_batches[i];
  854. }
  855. }
  856. return NULL;
  857. }
  858. static void finish_batch_completion(grpc_exec_ctx *exec_ctx, void *user_data,
  859. grpc_cq_completion *storage) {
  860. batch_control *bctl = user_data;
  861. grpc_call *call = bctl->call;
  862. gpr_mu_lock(&call->mu);
  863. call->used_batches = (uint8_t)(
  864. call->used_batches & ~(uint8_t)(1 << (bctl - call->active_batches)));
  865. gpr_mu_unlock(&call->mu);
  866. GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "completion");
  867. }
  868. static void post_batch_completion(grpc_exec_ctx *exec_ctx,
  869. batch_control *bctl) {
  870. grpc_call *call = bctl->call;
  871. if (bctl->is_notify_tag_closure) {
  872. grpc_exec_ctx_enqueue(exec_ctx, bctl->notify_tag, bctl->success, NULL);
  873. gpr_mu_lock(&call->mu);
  874. bctl->call->used_batches =
  875. (uint8_t)(bctl->call->used_batches &
  876. ~(uint8_t)(1 << (bctl - bctl->call->active_batches)));
  877. gpr_mu_unlock(&call->mu);
  878. GRPC_CALL_INTERNAL_UNREF(exec_ctx, call, "completion");
  879. } else {
  880. grpc_cq_end_op(exec_ctx, bctl->call->cq, bctl->notify_tag, bctl->success,
  881. finish_batch_completion, bctl, &bctl->cq_completion);
  882. }
  883. }
  884. static void continue_receiving_slices(grpc_exec_ctx *exec_ctx,
  885. batch_control *bctl) {
  886. grpc_call *call = bctl->call;
  887. for (;;) {
  888. size_t remaining = call->receiving_stream->length -
  889. (*call->receiving_buffer)->data.raw.slice_buffer.length;
  890. if (remaining == 0) {
  891. call->receiving_message = 0;
  892. grpc_byte_stream_destroy(exec_ctx, call->receiving_stream);
  893. call->receiving_stream = NULL;
  894. if (gpr_unref(&bctl->steps_to_complete)) {
  895. post_batch_completion(exec_ctx, bctl);
  896. }
  897. return;
  898. }
  899. if (grpc_byte_stream_next(exec_ctx, call->receiving_stream,
  900. &call->receiving_slice, remaining,
  901. &call->receiving_slice_ready)) {
  902. gpr_slice_buffer_add(&(*call->receiving_buffer)->data.raw.slice_buffer,
  903. call->receiving_slice);
  904. } else {
  905. return;
  906. }
  907. }
  908. }
  909. static void receiving_slice_ready(grpc_exec_ctx *exec_ctx, void *bctlp,
  910. bool success) {
  911. batch_control *bctl = bctlp;
  912. grpc_call *call = bctl->call;
  913. if (success) {
  914. gpr_slice_buffer_add(&(*call->receiving_buffer)->data.raw.slice_buffer,
  915. call->receiving_slice);
  916. continue_receiving_slices(exec_ctx, bctl);
  917. } else {
  918. grpc_byte_stream_destroy(exec_ctx, call->receiving_stream);
  919. call->receiving_stream = NULL;
  920. grpc_byte_buffer_destroy(*call->receiving_buffer);
  921. *call->receiving_buffer = NULL;
  922. if (gpr_unref(&bctl->steps_to_complete)) {
  923. post_batch_completion(exec_ctx, bctl);
  924. }
  925. }
  926. }
  927. static void process_data_after_md(grpc_exec_ctx *exec_ctx, batch_control *bctl,
  928. bool success) {
  929. grpc_call *call = bctl->call;
  930. if (call->receiving_stream == NULL) {
  931. *call->receiving_buffer = NULL;
  932. call->receiving_message = 0;
  933. if (gpr_unref(&bctl->steps_to_complete)) {
  934. post_batch_completion(exec_ctx, bctl);
  935. }
  936. } else if (call->receiving_stream->length >
  937. grpc_channel_get_max_message_length(call->channel)) {
  938. cancel_with_status(exec_ctx, call, GRPC_STATUS_INTERNAL,
  939. "Max message size exceeded");
  940. grpc_byte_stream_destroy(exec_ctx, call->receiving_stream);
  941. call->receiving_stream = NULL;
  942. *call->receiving_buffer = NULL;
  943. call->receiving_message = 0;
  944. if (gpr_unref(&bctl->steps_to_complete)) {
  945. post_batch_completion(exec_ctx, bctl);
  946. }
  947. } else {
  948. call->test_only_last_message_flags = call->receiving_stream->flags;
  949. if ((call->receiving_stream->flags & GRPC_WRITE_INTERNAL_COMPRESS) &&
  950. (call->compression_algorithm > GRPC_COMPRESS_NONE)) {
  951. *call->receiving_buffer = grpc_raw_compressed_byte_buffer_create(
  952. NULL, 0, call->compression_algorithm);
  953. } else {
  954. *call->receiving_buffer = grpc_raw_byte_buffer_create(NULL, 0);
  955. }
  956. grpc_closure_init(&call->receiving_slice_ready, receiving_slice_ready,
  957. bctl);
  958. continue_receiving_slices(exec_ctx, bctl);
  959. /* early out */
  960. return;
  961. }
  962. }
  963. static void receiving_stream_ready(grpc_exec_ctx *exec_ctx, void *bctlp,
  964. bool success) {
  965. batch_control *bctl = bctlp;
  966. grpc_call *call = bctl->call;
  967. gpr_mu_lock(&bctl->call->mu);
  968. if (bctl->call->has_initial_md_been_received) {
  969. gpr_mu_unlock(&bctl->call->mu);
  970. process_data_after_md(exec_ctx, bctlp, success);
  971. } else {
  972. call->saved_receiving_stream_ready_ctx.bctlp = bctlp;
  973. call->saved_receiving_stream_ready_ctx.success = success;
  974. gpr_mu_unlock(&bctl->call->mu);
  975. }
  976. }
  977. static void receiving_initial_metadata_ready(grpc_exec_ctx *exec_ctx,
  978. void *bctlp, bool success) {
  979. batch_control *bctl = bctlp;
  980. grpc_call *call = bctl->call;
  981. gpr_mu_lock(&call->mu);
  982. if (!success) {
  983. bctl->success = false;
  984. } else {
  985. grpc_metadata_batch *md =
  986. &call->metadata_batch[1 /* is_receiving */][0 /* is_trailing */];
  987. grpc_metadata_batch_filter(md, recv_initial_filter, call);
  988. if (gpr_time_cmp(md->deadline, gpr_inf_future(md->deadline.clock_type)) !=
  989. 0 &&
  990. !call->is_client) {
  991. GPR_TIMER_BEGIN("set_deadline_alarm", 0);
  992. set_deadline_alarm(exec_ctx, call, md->deadline);
  993. GPR_TIMER_END("set_deadline_alarm", 0);
  994. }
  995. }
  996. call->has_initial_md_been_received = true;
  997. if (call->saved_receiving_stream_ready_ctx.bctlp != NULL) {
  998. grpc_closure *saved_rsr_closure = grpc_closure_create(
  999. receiving_stream_ready, call->saved_receiving_stream_ready_ctx.bctlp);
  1000. grpc_exec_ctx_enqueue(
  1001. exec_ctx, saved_rsr_closure,
  1002. call->saved_receiving_stream_ready_ctx.success && success, NULL);
  1003. call->saved_receiving_stream_ready_ctx.bctlp = NULL;
  1004. }
  1005. gpr_mu_unlock(&call->mu);
  1006. if (gpr_unref(&bctl->steps_to_complete)) {
  1007. post_batch_completion(exec_ctx, bctl);
  1008. }
  1009. }
  1010. static void finish_batch(grpc_exec_ctx *exec_ctx, void *bctlp, bool success) {
  1011. batch_control *bctl = bctlp;
  1012. grpc_call *call = bctl->call;
  1013. grpc_call *child_call;
  1014. grpc_call *next_child_call;
  1015. gpr_mu_lock(&call->mu);
  1016. if (bctl->send_initial_metadata) {
  1017. if (!success) {
  1018. set_status_code(call, STATUS_FROM_CORE, GRPC_STATUS_UNAVAILABLE);
  1019. }
  1020. grpc_metadata_batch_destroy(
  1021. &call->metadata_batch[0 /* is_receiving */][0 /* is_trailing */]);
  1022. }
  1023. if (bctl->send_message) {
  1024. call->sending_message = 0;
  1025. }
  1026. if (bctl->send_final_op) {
  1027. grpc_metadata_batch_destroy(
  1028. &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */]);
  1029. }
  1030. if (bctl->recv_final_op) {
  1031. grpc_metadata_batch *md =
  1032. &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */];
  1033. grpc_metadata_batch_filter(md, recv_trailing_filter, call);
  1034. if (call->have_alarm) {
  1035. grpc_timer_cancel(exec_ctx, &call->alarm);
  1036. }
  1037. /* propagate cancellation to any interested children */
  1038. child_call = call->first_child;
  1039. if (child_call != NULL) {
  1040. do {
  1041. next_child_call = child_call->sibling_next;
  1042. if (child_call->cancellation_is_inherited) {
  1043. GRPC_CALL_INTERNAL_REF(child_call, "propagate_cancel");
  1044. grpc_call_cancel(child_call, NULL);
  1045. GRPC_CALL_INTERNAL_UNREF(exec_ctx, child_call, "propagate_cancel");
  1046. }
  1047. child_call = next_child_call;
  1048. } while (child_call != call->first_child);
  1049. }
  1050. if (call->is_client) {
  1051. get_final_status(call, set_status_value_directly,
  1052. call->final_op.client.status);
  1053. get_final_details(call, call->final_op.client.status_details,
  1054. call->final_op.client.status_details_capacity);
  1055. } else {
  1056. get_final_status(call, set_cancelled_value,
  1057. call->final_op.server.cancelled);
  1058. }
  1059. success = 1;
  1060. }
  1061. bctl->success = success != 0;
  1062. gpr_mu_unlock(&call->mu);
  1063. if (gpr_unref(&bctl->steps_to_complete)) {
  1064. post_batch_completion(exec_ctx, bctl);
  1065. }
  1066. }
  1067. static grpc_call_error call_start_batch(grpc_exec_ctx *exec_ctx,
  1068. grpc_call *call, const grpc_op *ops,
  1069. size_t nops, void *notify_tag,
  1070. int is_notify_tag_closure) {
  1071. grpc_transport_stream_op stream_op;
  1072. size_t i;
  1073. const grpc_op *op;
  1074. batch_control *bctl;
  1075. int num_completion_callbacks_needed = 1;
  1076. grpc_call_error error = GRPC_CALL_OK;
  1077. GPR_TIMER_BEGIN("grpc_call_start_batch", 0);
  1078. GRPC_CALL_LOG_BATCH(GPR_INFO, call, ops, nops, notify_tag);
  1079. memset(&stream_op, 0, sizeof(stream_op));
  1080. /* TODO(ctiller): this feels like it could be made lock-free */
  1081. gpr_mu_lock(&call->mu);
  1082. bctl = allocate_batch_control(call);
  1083. memset(bctl, 0, sizeof(*bctl));
  1084. bctl->call = call;
  1085. bctl->notify_tag = notify_tag;
  1086. bctl->is_notify_tag_closure = (uint8_t)(is_notify_tag_closure != 0);
  1087. if (nops == 0) {
  1088. GRPC_CALL_INTERNAL_REF(call, "completion");
  1089. bctl->success = 1;
  1090. if (!is_notify_tag_closure) {
  1091. grpc_cq_begin_op(call->cq, notify_tag);
  1092. }
  1093. gpr_mu_unlock(&call->mu);
  1094. post_batch_completion(exec_ctx, bctl);
  1095. error = GRPC_CALL_OK;
  1096. goto done;
  1097. }
  1098. /* rewrite batch ops into a transport op */
  1099. for (i = 0; i < nops; i++) {
  1100. op = &ops[i];
  1101. if (op->reserved != NULL) {
  1102. error = GRPC_CALL_ERROR;
  1103. goto done_with_error;
  1104. }
  1105. switch (op->op) {
  1106. case GRPC_OP_SEND_INITIAL_METADATA:
  1107. /* Flag validation: currently allow no flags */
  1108. if (!are_initial_metadata_flags_valid(op->flags, call->is_client)) {
  1109. error = GRPC_CALL_ERROR_INVALID_FLAGS;
  1110. goto done_with_error;
  1111. }
  1112. if (call->sent_initial_metadata) {
  1113. error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
  1114. goto done_with_error;
  1115. }
  1116. if (op->data.send_initial_metadata.count > INT_MAX) {
  1117. error = GRPC_CALL_ERROR_INVALID_METADATA;
  1118. goto done_with_error;
  1119. }
  1120. bctl->send_initial_metadata = 1;
  1121. call->sent_initial_metadata = 1;
  1122. if (!prepare_application_metadata(
  1123. call, (int)op->data.send_initial_metadata.count,
  1124. op->data.send_initial_metadata.metadata, 0, call->is_client)) {
  1125. error = GRPC_CALL_ERROR_INVALID_METADATA;
  1126. goto done_with_error;
  1127. }
  1128. /* TODO(ctiller): just make these the same variable? */
  1129. call->metadata_batch[0][0].deadline = call->send_deadline;
  1130. stream_op.send_initial_metadata =
  1131. &call->metadata_batch[0 /* is_receiving */][0 /* is_trailing */];
  1132. stream_op.send_initial_metadata_flags = op->flags;
  1133. break;
  1134. case GRPC_OP_SEND_MESSAGE:
  1135. if (!are_write_flags_valid(op->flags)) {
  1136. error = GRPC_CALL_ERROR_INVALID_FLAGS;
  1137. goto done_with_error;
  1138. }
  1139. if (op->data.send_message == NULL) {
  1140. error = GRPC_CALL_ERROR_INVALID_MESSAGE;
  1141. goto done_with_error;
  1142. }
  1143. if (call->sending_message) {
  1144. error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
  1145. goto done_with_error;
  1146. }
  1147. bctl->send_message = 1;
  1148. call->sending_message = 1;
  1149. grpc_slice_buffer_stream_init(
  1150. &call->sending_stream,
  1151. &op->data.send_message->data.raw.slice_buffer, op->flags);
  1152. stream_op.send_message = &call->sending_stream.base;
  1153. break;
  1154. case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
  1155. /* Flag validation: currently allow no flags */
  1156. if (op->flags != 0) {
  1157. error = GRPC_CALL_ERROR_INVALID_FLAGS;
  1158. goto done_with_error;
  1159. }
  1160. if (!call->is_client) {
  1161. error = GRPC_CALL_ERROR_NOT_ON_SERVER;
  1162. goto done_with_error;
  1163. }
  1164. if (call->sent_final_op) {
  1165. error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
  1166. goto done_with_error;
  1167. }
  1168. bctl->send_final_op = 1;
  1169. call->sent_final_op = 1;
  1170. stream_op.send_trailing_metadata =
  1171. &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */];
  1172. break;
  1173. case GRPC_OP_SEND_STATUS_FROM_SERVER:
  1174. /* Flag validation: currently allow no flags */
  1175. if (op->flags != 0) {
  1176. error = GRPC_CALL_ERROR_INVALID_FLAGS;
  1177. goto done_with_error;
  1178. }
  1179. if (call->is_client) {
  1180. error = GRPC_CALL_ERROR_NOT_ON_CLIENT;
  1181. goto done_with_error;
  1182. }
  1183. if (call->sent_final_op) {
  1184. error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
  1185. goto done_with_error;
  1186. }
  1187. if (op->data.send_status_from_server.trailing_metadata_count >
  1188. INT_MAX) {
  1189. error = GRPC_CALL_ERROR_INVALID_METADATA;
  1190. goto done_with_error;
  1191. }
  1192. bctl->send_final_op = 1;
  1193. call->sent_final_op = 1;
  1194. call->send_extra_metadata_count = 1;
  1195. call->send_extra_metadata[0].md = grpc_channel_get_reffed_status_elem(
  1196. call->channel, op->data.send_status_from_server.status);
  1197. if (op->data.send_status_from_server.status_details != NULL) {
  1198. call->send_extra_metadata[1].md = grpc_mdelem_from_metadata_strings(
  1199. GRPC_MDSTR_GRPC_MESSAGE,
  1200. grpc_mdstr_from_string(
  1201. op->data.send_status_from_server.status_details));
  1202. call->send_extra_metadata_count++;
  1203. set_status_details(
  1204. call, STATUS_FROM_API_OVERRIDE,
  1205. GRPC_MDSTR_REF(call->send_extra_metadata[1].md->value));
  1206. }
  1207. set_status_code(call, STATUS_FROM_API_OVERRIDE,
  1208. (uint32_t)op->data.send_status_from_server.status);
  1209. if (!prepare_application_metadata(
  1210. call,
  1211. (int)op->data.send_status_from_server.trailing_metadata_count,
  1212. op->data.send_status_from_server.trailing_metadata, 1, 1)) {
  1213. error = GRPC_CALL_ERROR_INVALID_METADATA;
  1214. goto done_with_error;
  1215. }
  1216. stream_op.send_trailing_metadata =
  1217. &call->metadata_batch[0 /* is_receiving */][1 /* is_trailing */];
  1218. break;
  1219. case GRPC_OP_RECV_INITIAL_METADATA:
  1220. /* Flag validation: currently allow no flags */
  1221. if (op->flags != 0) {
  1222. error = GRPC_CALL_ERROR_INVALID_FLAGS;
  1223. goto done_with_error;
  1224. }
  1225. if (call->received_initial_metadata) {
  1226. error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
  1227. goto done_with_error;
  1228. }
  1229. call->received_initial_metadata = 1;
  1230. call->buffered_metadata[0] = op->data.recv_initial_metadata;
  1231. grpc_closure_init(&call->receiving_initial_metadata_ready,
  1232. receiving_initial_metadata_ready, bctl);
  1233. bctl->recv_initial_metadata = 1;
  1234. stream_op.recv_initial_metadata =
  1235. &call->metadata_batch[1 /* is_receiving */][0 /* is_trailing */];
  1236. stream_op.recv_initial_metadata_ready =
  1237. &call->receiving_initial_metadata_ready;
  1238. num_completion_callbacks_needed++;
  1239. break;
  1240. case GRPC_OP_RECV_MESSAGE:
  1241. /* Flag validation: currently allow no flags */
  1242. if (op->flags != 0) {
  1243. error = GRPC_CALL_ERROR_INVALID_FLAGS;
  1244. goto done_with_error;
  1245. }
  1246. if (call->receiving_message) {
  1247. error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
  1248. goto done_with_error;
  1249. }
  1250. call->receiving_message = 1;
  1251. bctl->recv_message = 1;
  1252. call->receiving_buffer = op->data.recv_message;
  1253. stream_op.recv_message = &call->receiving_stream;
  1254. grpc_closure_init(&call->receiving_stream_ready, receiving_stream_ready,
  1255. bctl);
  1256. stream_op.recv_message_ready = &call->receiving_stream_ready;
  1257. num_completion_callbacks_needed++;
  1258. break;
  1259. case GRPC_OP_RECV_STATUS_ON_CLIENT:
  1260. /* Flag validation: currently allow no flags */
  1261. if (op->flags != 0) {
  1262. error = GRPC_CALL_ERROR_INVALID_FLAGS;
  1263. goto done_with_error;
  1264. }
  1265. if (!call->is_client) {
  1266. error = GRPC_CALL_ERROR_NOT_ON_SERVER;
  1267. goto done_with_error;
  1268. }
  1269. if (call->received_final_op) {
  1270. error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
  1271. goto done_with_error;
  1272. }
  1273. call->received_final_op = 1;
  1274. call->buffered_metadata[1] =
  1275. op->data.recv_status_on_client.trailing_metadata;
  1276. call->final_op.client.status = op->data.recv_status_on_client.status;
  1277. call->final_op.client.status_details =
  1278. op->data.recv_status_on_client.status_details;
  1279. call->final_op.client.status_details_capacity =
  1280. op->data.recv_status_on_client.status_details_capacity;
  1281. bctl->recv_final_op = 1;
  1282. stream_op.recv_trailing_metadata =
  1283. &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */];
  1284. stream_op.collect_stats = &call->stats;
  1285. break;
  1286. case GRPC_OP_RECV_CLOSE_ON_SERVER:
  1287. /* Flag validation: currently allow no flags */
  1288. if (op->flags != 0) {
  1289. error = GRPC_CALL_ERROR_INVALID_FLAGS;
  1290. goto done_with_error;
  1291. }
  1292. if (call->is_client) {
  1293. error = GRPC_CALL_ERROR_NOT_ON_CLIENT;
  1294. goto done_with_error;
  1295. }
  1296. if (call->received_final_op) {
  1297. error = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS;
  1298. goto done_with_error;
  1299. }
  1300. call->received_final_op = 1;
  1301. call->final_op.server.cancelled =
  1302. op->data.recv_close_on_server.cancelled;
  1303. bctl->recv_final_op = 1;
  1304. stream_op.recv_trailing_metadata =
  1305. &call->metadata_batch[1 /* is_receiving */][1 /* is_trailing */];
  1306. stream_op.collect_stats = &call->stats;
  1307. break;
  1308. }
  1309. }
  1310. GRPC_CALL_INTERNAL_REF(call, "completion");
  1311. if (!is_notify_tag_closure) {
  1312. grpc_cq_begin_op(call->cq, notify_tag);
  1313. }
  1314. gpr_ref_init(&bctl->steps_to_complete, num_completion_callbacks_needed);
  1315. stream_op.context = call->context;
  1316. grpc_closure_init(&bctl->finish_batch, finish_batch, bctl);
  1317. stream_op.on_complete = &bctl->finish_batch;
  1318. gpr_mu_unlock(&call->mu);
  1319. execute_op(exec_ctx, call, &stream_op);
  1320. done:
  1321. GPR_TIMER_END("grpc_call_start_batch", 0);
  1322. return error;
  1323. done_with_error:
  1324. /* reverse any mutations that occured */
  1325. if (bctl->send_initial_metadata) {
  1326. call->sent_initial_metadata = 0;
  1327. grpc_metadata_batch_clear(&call->metadata_batch[0][0]);
  1328. }
  1329. if (bctl->send_message) {
  1330. call->sending_message = 0;
  1331. grpc_byte_stream_destroy(exec_ctx, &call->sending_stream.base);
  1332. }
  1333. if (bctl->send_final_op) {
  1334. call->sent_final_op = 0;
  1335. grpc_metadata_batch_clear(&call->metadata_batch[0][1]);
  1336. }
  1337. if (bctl->recv_initial_metadata) {
  1338. call->received_initial_metadata = 0;
  1339. }
  1340. if (bctl->recv_message) {
  1341. call->receiving_message = 0;
  1342. }
  1343. if (bctl->recv_final_op) {
  1344. call->received_final_op = 0;
  1345. }
  1346. gpr_mu_unlock(&call->mu);
  1347. goto done;
  1348. }
  1349. grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
  1350. size_t nops, void *tag, void *reserved) {
  1351. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  1352. grpc_call_error err;
  1353. GRPC_API_TRACE(
  1354. "grpc_call_start_batch(call=%p, ops=%p, nops=%lu, tag=%p, reserved=%p)",
  1355. 5, (call, ops, (unsigned long)nops, tag, reserved));
  1356. if (reserved != NULL) {
  1357. err = GRPC_CALL_ERROR;
  1358. } else {
  1359. err = call_start_batch(&exec_ctx, call, ops, nops, tag, 0);
  1360. }
  1361. grpc_exec_ctx_finish(&exec_ctx);
  1362. return err;
  1363. }
  1364. grpc_call_error grpc_call_start_batch_and_execute(grpc_exec_ctx *exec_ctx,
  1365. grpc_call *call,
  1366. const grpc_op *ops,
  1367. size_t nops,
  1368. grpc_closure *closure) {
  1369. return call_start_batch(exec_ctx, call, ops, nops, closure, 1);
  1370. }
  1371. void grpc_call_context_set(grpc_call *call, grpc_context_index elem,
  1372. void *value, void (*destroy)(void *value)) {
  1373. if (call->context[elem].destroy) {
  1374. call->context[elem].destroy(call->context[elem].value);
  1375. }
  1376. call->context[elem].value = value;
  1377. call->context[elem].destroy = destroy;
  1378. }
  1379. void *grpc_call_context_get(grpc_call *call, grpc_context_index elem) {
  1380. return call->context[elem].value;
  1381. }
  1382. uint8_t grpc_call_is_client(grpc_call *call) { return call->is_client; }
  1383. grpc_compression_algorithm grpc_call_compression_for_level(
  1384. grpc_call *call, grpc_compression_level level) {
  1385. gpr_mu_lock(&call->mu);
  1386. const uint32_t accepted_encodings = call->encodings_accepted_by_peer;
  1387. gpr_mu_unlock(&call->mu);
  1388. return grpc_compression_algorithm_for_level(level, accepted_encodings);
  1389. }