channel.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 "src/core/lib/surface/channel.h"
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <grpc/compression.h>
  37. #include <grpc/support/alloc.h>
  38. #include <grpc/support/log.h>
  39. #include <grpc/support/string_util.h>
  40. #include "src/core/lib/channel/channel_args.h"
  41. #include "src/core/lib/iomgr/iomgr.h"
  42. #include "src/core/lib/slice/slice_internal.h"
  43. #include "src/core/lib/support/string.h"
  44. #include "src/core/lib/surface/api_trace.h"
  45. #include "src/core/lib/surface/call.h"
  46. #include "src/core/lib/surface/channel_init.h"
  47. #include "src/core/lib/transport/static_metadata.h"
  48. /** Cache grpc-status: X mdelems for X = 0..NUM_CACHED_STATUS_ELEMS.
  49. * Avoids needing to take a metadata context lock for sending status
  50. * if the status code is <= NUM_CACHED_STATUS_ELEMS.
  51. * Sized to allow the most commonly used codes to fit in
  52. * (OK, Cancelled, Unknown). */
  53. #define NUM_CACHED_STATUS_ELEMS 3
  54. typedef struct registered_call {
  55. grpc_mdelem path;
  56. grpc_mdelem authority;
  57. struct registered_call *next;
  58. } registered_call;
  59. struct grpc_channel {
  60. int is_client;
  61. grpc_compression_options compression_options;
  62. grpc_mdelem default_authority;
  63. gpr_mu registered_call_mu;
  64. registered_call *registered_calls;
  65. char *target;
  66. };
  67. #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1))
  68. #define CHANNEL_FROM_CHANNEL_STACK(channel_stack) \
  69. (((grpc_channel *)(channel_stack)) - 1)
  70. #define CHANNEL_FROM_TOP_ELEM(top_elem) \
  71. CHANNEL_FROM_CHANNEL_STACK(grpc_channel_stack_from_top_element(top_elem))
  72. static void destroy_channel(grpc_exec_ctx *exec_ctx, void *arg,
  73. grpc_error *error);
  74. grpc_channel *grpc_channel_create_with_builder(
  75. grpc_exec_ctx *exec_ctx, grpc_channel_stack_builder *builder,
  76. grpc_channel_stack_type channel_stack_type) {
  77. char *target = gpr_strdup(grpc_channel_stack_builder_get_target(builder));
  78. grpc_channel_args *args = grpc_channel_args_copy(
  79. grpc_channel_stack_builder_get_channel_arguments(builder));
  80. grpc_channel *channel;
  81. grpc_error *error = grpc_channel_stack_builder_finish(
  82. exec_ctx, builder, sizeof(grpc_channel), 1, destroy_channel, NULL,
  83. (void **)&channel);
  84. if (error != GRPC_ERROR_NONE) {
  85. gpr_log(GPR_ERROR, "channel stack builder failed: %s",
  86. grpc_error_string(error));
  87. GRPC_ERROR_UNREF(error);
  88. gpr_free(target);
  89. goto done;
  90. }
  91. memset(channel, 0, sizeof(*channel));
  92. channel->target = target;
  93. channel->is_client = grpc_channel_stack_type_is_client(channel_stack_type);
  94. gpr_mu_init(&channel->registered_call_mu);
  95. channel->registered_calls = NULL;
  96. grpc_compression_options_init(&channel->compression_options);
  97. for (size_t i = 0; i < args->num_args; i++) {
  98. if (0 == strcmp(args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY)) {
  99. if (args->args[i].type != GRPC_ARG_STRING) {
  100. gpr_log(GPR_ERROR, "%s ignored: it must be a string",
  101. GRPC_ARG_DEFAULT_AUTHORITY);
  102. } else {
  103. if (!GRPC_MDISNULL(channel->default_authority)) {
  104. /* setting this takes precedence over anything else */
  105. GRPC_MDELEM_UNREF(exec_ctx, channel->default_authority);
  106. }
  107. channel->default_authority = grpc_mdelem_from_slices(
  108. exec_ctx, GRPC_MDSTR_AUTHORITY,
  109. grpc_slice_intern(
  110. grpc_slice_from_static_string(args->args[i].value.string)));
  111. }
  112. } else if (0 ==
  113. strcmp(args->args[i].key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG)) {
  114. if (args->args[i].type != GRPC_ARG_STRING) {
  115. gpr_log(GPR_ERROR, "%s ignored: it must be a string",
  116. GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
  117. } else {
  118. if (!GRPC_MDISNULL(channel->default_authority)) {
  119. /* other ways of setting this (notably ssl) take precedence */
  120. gpr_log(GPR_ERROR,
  121. "%s ignored: default host already set some other way",
  122. GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
  123. } else {
  124. channel->default_authority = grpc_mdelem_from_slices(
  125. exec_ctx, GRPC_MDSTR_AUTHORITY,
  126. grpc_slice_intern(
  127. grpc_slice_from_static_string(args->args[i].value.string)));
  128. }
  129. }
  130. } else if (0 == strcmp(args->args[i].key,
  131. GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL)) {
  132. channel->compression_options.default_level.is_set = true;
  133. GPR_ASSERT(args->args[i].value.integer >= 0 &&
  134. args->args[i].value.integer < GRPC_COMPRESS_LEVEL_COUNT);
  135. channel->compression_options.default_level.level =
  136. (grpc_compression_level)args->args[i].value.integer;
  137. } else if (0 == strcmp(args->args[i].key,
  138. GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM)) {
  139. channel->compression_options.default_algorithm.is_set = true;
  140. GPR_ASSERT(args->args[i].value.integer >= 0 &&
  141. args->args[i].value.integer < GRPC_COMPRESS_ALGORITHMS_COUNT);
  142. channel->compression_options.default_algorithm.algorithm =
  143. (grpc_compression_algorithm)args->args[i].value.integer;
  144. } else if (0 ==
  145. strcmp(args->args[i].key,
  146. GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET)) {
  147. channel->compression_options.enabled_algorithms_bitset =
  148. (uint32_t)args->args[i].value.integer |
  149. 0x1; /* always support no compression */
  150. }
  151. }
  152. done:
  153. grpc_channel_args_destroy(exec_ctx, args);
  154. return channel;
  155. }
  156. grpc_channel *grpc_channel_create(grpc_exec_ctx *exec_ctx, const char *target,
  157. const grpc_channel_args *input_args,
  158. grpc_channel_stack_type channel_stack_type,
  159. grpc_transport *optional_transport) {
  160. grpc_channel_stack_builder *builder = grpc_channel_stack_builder_create();
  161. grpc_channel_stack_builder_set_channel_arguments(exec_ctx, builder,
  162. input_args);
  163. grpc_channel_stack_builder_set_target(builder, target);
  164. grpc_channel_stack_builder_set_transport(builder, optional_transport);
  165. if (!grpc_channel_init_create_stack(exec_ctx, builder, channel_stack_type)) {
  166. grpc_channel_stack_builder_destroy(exec_ctx, builder);
  167. return NULL;
  168. }
  169. return grpc_channel_create_with_builder(exec_ctx, builder,
  170. channel_stack_type);
  171. }
  172. char *grpc_channel_get_target(grpc_channel *channel) {
  173. GRPC_API_TRACE("grpc_channel_get_target(channel=%p)", 1, (channel));
  174. return gpr_strdup(channel->target);
  175. }
  176. void grpc_channel_get_info(grpc_channel *channel,
  177. const grpc_channel_info *channel_info) {
  178. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  179. grpc_channel_element *elem =
  180. grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0);
  181. elem->filter->get_channel_info(&exec_ctx, elem, channel_info);
  182. grpc_exec_ctx_finish(&exec_ctx);
  183. }
  184. static grpc_call *grpc_channel_create_call_internal(
  185. grpc_exec_ctx *exec_ctx, grpc_channel *channel, grpc_call *parent_call,
  186. uint32_t propagation_mask, grpc_completion_queue *cq,
  187. grpc_pollset_set *pollset_set_alternative, grpc_mdelem path_mdelem,
  188. grpc_mdelem authority_mdelem, gpr_timespec deadline) {
  189. grpc_mdelem send_metadata[2];
  190. size_t num_metadata = 0;
  191. GPR_ASSERT(channel->is_client);
  192. GPR_ASSERT(!(cq != NULL && pollset_set_alternative != NULL));
  193. send_metadata[num_metadata++] = path_mdelem;
  194. if (!GRPC_MDISNULL(authority_mdelem)) {
  195. send_metadata[num_metadata++] = authority_mdelem;
  196. } else if (!GRPC_MDISNULL(channel->default_authority)) {
  197. send_metadata[num_metadata++] = GRPC_MDELEM_REF(channel->default_authority);
  198. }
  199. grpc_call_create_args args;
  200. memset(&args, 0, sizeof(args));
  201. args.channel = channel;
  202. args.parent_call = parent_call;
  203. args.propagation_mask = propagation_mask;
  204. args.cq = cq;
  205. args.pollset_set_alternative = pollset_set_alternative;
  206. args.server_transport_data = NULL;
  207. args.add_initial_metadata = send_metadata;
  208. args.add_initial_metadata_count = num_metadata;
  209. args.send_deadline = deadline;
  210. grpc_call *call;
  211. GRPC_LOG_IF_ERROR("call_create", grpc_call_create(exec_ctx, &args, &call));
  212. return call;
  213. }
  214. grpc_call *grpc_channel_create_call(grpc_channel *channel,
  215. grpc_call *parent_call,
  216. uint32_t propagation_mask,
  217. grpc_completion_queue *cq,
  218. grpc_slice method, const grpc_slice *host,
  219. gpr_timespec deadline, void *reserved) {
  220. GPR_ASSERT(!reserved);
  221. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  222. grpc_call *call = grpc_channel_create_call_internal(
  223. &exec_ctx, channel, parent_call, propagation_mask, cq, NULL,
  224. grpc_mdelem_from_slices(&exec_ctx, GRPC_MDSTR_PATH,
  225. grpc_slice_ref_internal(method)),
  226. host != NULL ? grpc_mdelem_from_slices(&exec_ctx, GRPC_MDSTR_AUTHORITY,
  227. grpc_slice_ref_internal(*host))
  228. : GRPC_MDNULL,
  229. deadline);
  230. grpc_exec_ctx_finish(&exec_ctx);
  231. return call;
  232. }
  233. grpc_call *grpc_channel_create_pollset_set_call(
  234. grpc_exec_ctx *exec_ctx, grpc_channel *channel, grpc_call *parent_call,
  235. uint32_t propagation_mask, grpc_pollset_set *pollset_set, grpc_slice method,
  236. const grpc_slice *host, gpr_timespec deadline, void *reserved) {
  237. GPR_ASSERT(!reserved);
  238. return grpc_channel_create_call_internal(
  239. exec_ctx, channel, parent_call, propagation_mask, NULL, pollset_set,
  240. grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_PATH,
  241. grpc_slice_ref_internal(method)),
  242. host != NULL ? grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_AUTHORITY,
  243. grpc_slice_ref_internal(*host))
  244. : GRPC_MDNULL,
  245. deadline);
  246. }
  247. void *grpc_channel_register_call(grpc_channel *channel, const char *method,
  248. const char *host, void *reserved) {
  249. registered_call *rc = gpr_malloc(sizeof(registered_call));
  250. GRPC_API_TRACE(
  251. "grpc_channel_register_call(channel=%p, method=%s, host=%s, reserved=%p)",
  252. 4, (channel, method, host, reserved));
  253. GPR_ASSERT(!reserved);
  254. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  255. rc->path = grpc_mdelem_from_slices(
  256. &exec_ctx, GRPC_MDSTR_PATH,
  257. grpc_slice_intern(grpc_slice_from_static_string(method)));
  258. rc->authority =
  259. host ? grpc_mdelem_from_slices(
  260. &exec_ctx, GRPC_MDSTR_AUTHORITY,
  261. grpc_slice_intern(grpc_slice_from_static_string(host)))
  262. : GRPC_MDNULL;
  263. gpr_mu_lock(&channel->registered_call_mu);
  264. rc->next = channel->registered_calls;
  265. channel->registered_calls = rc;
  266. gpr_mu_unlock(&channel->registered_call_mu);
  267. grpc_exec_ctx_finish(&exec_ctx);
  268. return rc;
  269. }
  270. grpc_call *grpc_channel_create_registered_call(
  271. grpc_channel *channel, grpc_call *parent_call, uint32_t propagation_mask,
  272. grpc_completion_queue *completion_queue, void *registered_call_handle,
  273. gpr_timespec deadline, void *reserved) {
  274. registered_call *rc = registered_call_handle;
  275. GRPC_API_TRACE(
  276. "grpc_channel_create_registered_call("
  277. "channel=%p, parent_call=%p, propagation_mask=%x, completion_queue=%p, "
  278. "registered_call_handle=%p, "
  279. "deadline=gpr_timespec { tv_sec: %" PRId64
  280. ", tv_nsec: %d, clock_type: %d }, "
  281. "reserved=%p)",
  282. 9, (channel, parent_call, (unsigned)propagation_mask, completion_queue,
  283. registered_call_handle, deadline.tv_sec, deadline.tv_nsec,
  284. (int)deadline.clock_type, reserved));
  285. GPR_ASSERT(!reserved);
  286. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  287. grpc_call *call = grpc_channel_create_call_internal(
  288. &exec_ctx, channel, parent_call, propagation_mask, completion_queue, NULL,
  289. GRPC_MDELEM_REF(rc->path), GRPC_MDELEM_REF(rc->authority), deadline);
  290. grpc_exec_ctx_finish(&exec_ctx);
  291. return call;
  292. }
  293. #ifdef GRPC_STREAM_REFCOUNT_DEBUG
  294. #define REF_REASON reason
  295. #define REF_ARG , const char *reason
  296. #else
  297. #define REF_REASON ""
  298. #define REF_ARG
  299. #endif
  300. void grpc_channel_internal_ref(grpc_channel *c REF_ARG) {
  301. GRPC_CHANNEL_STACK_REF(CHANNEL_STACK_FROM_CHANNEL(c), REF_REASON);
  302. }
  303. void grpc_channel_internal_unref(grpc_exec_ctx *exec_ctx,
  304. grpc_channel *c REF_ARG) {
  305. GRPC_CHANNEL_STACK_UNREF(exec_ctx, CHANNEL_STACK_FROM_CHANNEL(c), REF_REASON);
  306. }
  307. static void destroy_channel(grpc_exec_ctx *exec_ctx, void *arg,
  308. grpc_error *error) {
  309. grpc_channel *channel = arg;
  310. grpc_channel_stack_destroy(exec_ctx, CHANNEL_STACK_FROM_CHANNEL(channel));
  311. while (channel->registered_calls) {
  312. registered_call *rc = channel->registered_calls;
  313. channel->registered_calls = rc->next;
  314. GRPC_MDELEM_UNREF(exec_ctx, rc->path);
  315. GRPC_MDELEM_UNREF(exec_ctx, rc->authority);
  316. gpr_free(rc);
  317. }
  318. GRPC_MDELEM_UNREF(exec_ctx, channel->default_authority);
  319. gpr_mu_destroy(&channel->registered_call_mu);
  320. gpr_free(channel->target);
  321. gpr_free(channel);
  322. }
  323. void grpc_channel_destroy(grpc_channel *channel) {
  324. grpc_transport_op *op = grpc_make_transport_op(NULL);
  325. grpc_channel_element *elem;
  326. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  327. GRPC_API_TRACE("grpc_channel_destroy(channel=%p)", 1, (channel));
  328. op->disconnect_with_error = GRPC_ERROR_CREATE("Channel Destroyed");
  329. elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0);
  330. elem->filter->start_transport_op(&exec_ctx, elem, op);
  331. GRPC_CHANNEL_INTERNAL_UNREF(&exec_ctx, channel, "channel");
  332. grpc_exec_ctx_finish(&exec_ctx);
  333. }
  334. grpc_channel_stack *grpc_channel_get_channel_stack(grpc_channel *channel) {
  335. return CHANNEL_STACK_FROM_CHANNEL(channel);
  336. }
  337. grpc_compression_options grpc_channel_compression_options(
  338. const grpc_channel *channel) {
  339. return channel->compression_options;
  340. }
  341. grpc_mdelem grpc_channel_get_reffed_status_elem(grpc_exec_ctx *exec_ctx,
  342. grpc_channel *channel, int i) {
  343. char tmp[GPR_LTOA_MIN_BUFSIZE];
  344. switch (i) {
  345. case 0:
  346. return GRPC_MDELEM_GRPC_STATUS_0;
  347. case 1:
  348. return GRPC_MDELEM_GRPC_STATUS_1;
  349. case 2:
  350. return GRPC_MDELEM_GRPC_STATUS_2;
  351. }
  352. gpr_ltoa(i, tmp);
  353. return grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_GRPC_STATUS,
  354. grpc_slice_from_copied_string(tmp));
  355. }