channel.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "src/core/lib/surface/channel.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <grpc/compression.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include <grpc/support/string_util.h>
  25. #include "src/core/lib/channel/channel_args.h"
  26. #include "src/core/lib/iomgr/iomgr.h"
  27. #include "src/core/lib/slice/slice_internal.h"
  28. #include "src/core/lib/support/string.h"
  29. #include "src/core/lib/surface/api_trace.h"
  30. #include "src/core/lib/surface/call.h"
  31. #include "src/core/lib/surface/channel_init.h"
  32. #include "src/core/lib/transport/static_metadata.h"
  33. /** Cache grpc-status: X mdelems for X = 0..NUM_CACHED_STATUS_ELEMS.
  34. * Avoids needing to take a metadata context lock for sending status
  35. * if the status code is <= NUM_CACHED_STATUS_ELEMS.
  36. * Sized to allow the most commonly used codes to fit in
  37. * (OK, Cancelled, Unknown). */
  38. #define NUM_CACHED_STATUS_ELEMS 3
  39. typedef struct registered_call {
  40. grpc_mdelem path;
  41. grpc_mdelem authority;
  42. struct registered_call *next;
  43. } registered_call;
  44. struct grpc_channel {
  45. int is_client;
  46. grpc_compression_options compression_options;
  47. grpc_mdelem default_authority;
  48. gpr_atm call_size_estimate;
  49. gpr_mu registered_call_mu;
  50. registered_call *registered_calls;
  51. char *target;
  52. };
  53. #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1))
  54. #define CHANNEL_FROM_CHANNEL_STACK(channel_stack) \
  55. (((grpc_channel *)(channel_stack)) - 1)
  56. #define CHANNEL_FROM_TOP_ELEM(top_elem) \
  57. CHANNEL_FROM_CHANNEL_STACK(grpc_channel_stack_from_top_element(top_elem))
  58. static void destroy_channel(grpc_exec_ctx *exec_ctx, void *arg,
  59. grpc_error *error);
  60. grpc_channel *grpc_channel_create_with_builder(
  61. grpc_exec_ctx *exec_ctx, grpc_channel_stack_builder *builder,
  62. grpc_channel_stack_type channel_stack_type) {
  63. char *target = gpr_strdup(grpc_channel_stack_builder_get_target(builder));
  64. grpc_channel_args *args = grpc_channel_args_copy(
  65. grpc_channel_stack_builder_get_channel_arguments(builder));
  66. grpc_channel *channel;
  67. grpc_error *error = grpc_channel_stack_builder_finish(
  68. exec_ctx, builder, sizeof(grpc_channel), 1, destroy_channel, NULL,
  69. (void **)&channel);
  70. if (error != GRPC_ERROR_NONE) {
  71. gpr_log(GPR_ERROR, "channel stack builder failed: %s",
  72. grpc_error_string(error));
  73. GRPC_ERROR_UNREF(error);
  74. gpr_free(target);
  75. goto done;
  76. }
  77. memset(channel, 0, sizeof(*channel));
  78. channel->target = target;
  79. channel->is_client = grpc_channel_stack_type_is_client(channel_stack_type);
  80. gpr_mu_init(&channel->registered_call_mu);
  81. channel->registered_calls = NULL;
  82. gpr_atm_no_barrier_store(
  83. &channel->call_size_estimate,
  84. (gpr_atm)CHANNEL_STACK_FROM_CHANNEL(channel)->call_stack_size);
  85. grpc_compression_options_init(&channel->compression_options);
  86. for (size_t i = 0; i < args->num_args; i++) {
  87. if (0 == strcmp(args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY)) {
  88. if (args->args[i].type != GRPC_ARG_STRING) {
  89. gpr_log(GPR_ERROR, "%s ignored: it must be a string",
  90. GRPC_ARG_DEFAULT_AUTHORITY);
  91. } else {
  92. if (!GRPC_MDISNULL(channel->default_authority)) {
  93. /* setting this takes precedence over anything else */
  94. GRPC_MDELEM_UNREF(exec_ctx, channel->default_authority);
  95. }
  96. channel->default_authority = grpc_mdelem_from_slices(
  97. exec_ctx, GRPC_MDSTR_AUTHORITY,
  98. grpc_slice_intern(
  99. grpc_slice_from_static_string(args->args[i].value.string)));
  100. }
  101. } else if (0 ==
  102. strcmp(args->args[i].key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG)) {
  103. if (args->args[i].type != GRPC_ARG_STRING) {
  104. gpr_log(GPR_ERROR, "%s ignored: it must be a string",
  105. GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
  106. } else {
  107. if (!GRPC_MDISNULL(channel->default_authority)) {
  108. /* other ways of setting this (notably ssl) take precedence */
  109. gpr_log(GPR_ERROR,
  110. "%s ignored: default host already set some other way",
  111. GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
  112. } else {
  113. channel->default_authority = grpc_mdelem_from_slices(
  114. exec_ctx, GRPC_MDSTR_AUTHORITY,
  115. grpc_slice_intern(
  116. grpc_slice_from_static_string(args->args[i].value.string)));
  117. }
  118. }
  119. } else if (0 == strcmp(args->args[i].key,
  120. GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL)) {
  121. channel->compression_options.default_level.is_set = true;
  122. channel->compression_options.default_level.level =
  123. (grpc_compression_level)grpc_channel_arg_get_integer(
  124. &args->args[i],
  125. (grpc_integer_options){GRPC_COMPRESS_LEVEL_NONE,
  126. GRPC_COMPRESS_LEVEL_NONE,
  127. GRPC_COMPRESS_LEVEL_COUNT - 1});
  128. } else if (0 == strcmp(args->args[i].key,
  129. GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM)) {
  130. channel->compression_options.default_algorithm.is_set = true;
  131. channel->compression_options.default_algorithm.algorithm =
  132. (grpc_compression_algorithm)grpc_channel_arg_get_integer(
  133. &args->args[i],
  134. (grpc_integer_options){GRPC_COMPRESS_NONE, GRPC_COMPRESS_NONE,
  135. GRPC_COMPRESS_ALGORITHMS_COUNT - 1});
  136. } else if (0 ==
  137. strcmp(args->args[i].key,
  138. GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET)) {
  139. channel->compression_options.enabled_algorithms_bitset =
  140. (uint32_t)args->args[i].value.integer |
  141. 0x1; /* always support no compression */
  142. }
  143. }
  144. done:
  145. grpc_channel_args_destroy(exec_ctx, args);
  146. return channel;
  147. }
  148. grpc_channel *grpc_channel_create(grpc_exec_ctx *exec_ctx, const char *target,
  149. const grpc_channel_args *input_args,
  150. grpc_channel_stack_type channel_stack_type,
  151. grpc_transport *optional_transport) {
  152. grpc_channel_stack_builder *builder = grpc_channel_stack_builder_create();
  153. grpc_channel_stack_builder_set_channel_arguments(exec_ctx, builder,
  154. input_args);
  155. grpc_channel_stack_builder_set_target(builder, target);
  156. grpc_channel_stack_builder_set_transport(builder, optional_transport);
  157. if (!grpc_channel_init_create_stack(exec_ctx, builder, channel_stack_type)) {
  158. grpc_channel_stack_builder_destroy(exec_ctx, builder);
  159. return NULL;
  160. }
  161. return grpc_channel_create_with_builder(exec_ctx, builder,
  162. channel_stack_type);
  163. }
  164. size_t grpc_channel_get_call_size_estimate(grpc_channel *channel) {
  165. #define ROUND_UP_SIZE 256
  166. /* We round up our current estimate to the NEXT value of ROUND_UP_SIZE.
  167. This ensures:
  168. 1. a consistent size allocation when our estimate is drifting slowly
  169. (which is common) - which tends to help most allocators reuse memory
  170. 2. a small amount of allowed growth over the estimate without hitting
  171. the arena size doubling case, reducing overall memory usage */
  172. return ((size_t)gpr_atm_no_barrier_load(&channel->call_size_estimate) +
  173. 2 * ROUND_UP_SIZE) &
  174. ~(size_t)(ROUND_UP_SIZE - 1);
  175. }
  176. void grpc_channel_update_call_size_estimate(grpc_channel *channel,
  177. size_t size) {
  178. size_t cur = (size_t)gpr_atm_no_barrier_load(&channel->call_size_estimate);
  179. if (cur < size) {
  180. /* size grew: update estimate */
  181. gpr_atm_no_barrier_cas(&channel->call_size_estimate, (gpr_atm)cur,
  182. (gpr_atm)size);
  183. /* if we lose: never mind, something else will likely update soon enough */
  184. } else if (cur == size) {
  185. /* no change: holding pattern */
  186. } else if (cur > 0) {
  187. /* size shrank: decrease estimate */
  188. gpr_atm_no_barrier_cas(
  189. &channel->call_size_estimate, (gpr_atm)cur,
  190. (gpr_atm)(GPR_MIN(cur - 1, (255 * cur + size) / 256)));
  191. /* if we lose: never mind, something else will likely update soon enough */
  192. }
  193. }
  194. char *grpc_channel_get_target(grpc_channel *channel) {
  195. GRPC_API_TRACE("grpc_channel_get_target(channel=%p)", 1, (channel));
  196. return gpr_strdup(channel->target);
  197. }
  198. void grpc_channel_get_info(grpc_channel *channel,
  199. const grpc_channel_info *channel_info) {
  200. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  201. grpc_channel_element *elem =
  202. grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0);
  203. elem->filter->get_channel_info(&exec_ctx, elem, channel_info);
  204. grpc_exec_ctx_finish(&exec_ctx);
  205. }
  206. static grpc_call *grpc_channel_create_call_internal(
  207. grpc_exec_ctx *exec_ctx, grpc_channel *channel, grpc_call *parent_call,
  208. uint32_t propagation_mask, grpc_completion_queue *cq,
  209. grpc_pollset_set *pollset_set_alternative, grpc_mdelem path_mdelem,
  210. grpc_mdelem authority_mdelem, gpr_timespec deadline) {
  211. grpc_mdelem send_metadata[2];
  212. size_t num_metadata = 0;
  213. GPR_ASSERT(channel->is_client);
  214. GPR_ASSERT(!(cq != NULL && pollset_set_alternative != NULL));
  215. send_metadata[num_metadata++] = path_mdelem;
  216. if (!GRPC_MDISNULL(authority_mdelem)) {
  217. send_metadata[num_metadata++] = authority_mdelem;
  218. } else if (!GRPC_MDISNULL(channel->default_authority)) {
  219. send_metadata[num_metadata++] = GRPC_MDELEM_REF(channel->default_authority);
  220. }
  221. grpc_call_create_args args;
  222. memset(&args, 0, sizeof(args));
  223. args.channel = channel;
  224. args.parent_call = parent_call;
  225. args.propagation_mask = propagation_mask;
  226. args.cq = cq;
  227. args.pollset_set_alternative = pollset_set_alternative;
  228. args.server_transport_data = NULL;
  229. args.add_initial_metadata = send_metadata;
  230. args.add_initial_metadata_count = num_metadata;
  231. args.send_deadline = deadline;
  232. grpc_call *call;
  233. GRPC_LOG_IF_ERROR("call_create", grpc_call_create(exec_ctx, &args, &call));
  234. return call;
  235. }
  236. grpc_call *grpc_channel_create_call(grpc_channel *channel,
  237. grpc_call *parent_call,
  238. uint32_t propagation_mask,
  239. grpc_completion_queue *cq,
  240. grpc_slice method, const grpc_slice *host,
  241. gpr_timespec deadline, void *reserved) {
  242. GPR_ASSERT(!reserved);
  243. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  244. grpc_call *call = grpc_channel_create_call_internal(
  245. &exec_ctx, channel, parent_call, propagation_mask, cq, NULL,
  246. grpc_mdelem_from_slices(&exec_ctx, GRPC_MDSTR_PATH,
  247. grpc_slice_ref_internal(method)),
  248. host != NULL ? grpc_mdelem_from_slices(&exec_ctx, GRPC_MDSTR_AUTHORITY,
  249. grpc_slice_ref_internal(*host))
  250. : GRPC_MDNULL,
  251. deadline);
  252. grpc_exec_ctx_finish(&exec_ctx);
  253. return call;
  254. }
  255. grpc_call *grpc_channel_create_pollset_set_call(
  256. grpc_exec_ctx *exec_ctx, grpc_channel *channel, grpc_call *parent_call,
  257. uint32_t propagation_mask, grpc_pollset_set *pollset_set, grpc_slice method,
  258. const grpc_slice *host, gpr_timespec deadline, void *reserved) {
  259. GPR_ASSERT(!reserved);
  260. return grpc_channel_create_call_internal(
  261. exec_ctx, channel, parent_call, propagation_mask, NULL, pollset_set,
  262. grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_PATH,
  263. grpc_slice_ref_internal(method)),
  264. host != NULL ? grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_AUTHORITY,
  265. grpc_slice_ref_internal(*host))
  266. : GRPC_MDNULL,
  267. deadline);
  268. }
  269. void *grpc_channel_register_call(grpc_channel *channel, const char *method,
  270. const char *host, void *reserved) {
  271. registered_call *rc = gpr_malloc(sizeof(registered_call));
  272. GRPC_API_TRACE(
  273. "grpc_channel_register_call(channel=%p, method=%s, host=%s, reserved=%p)",
  274. 4, (channel, method, host, reserved));
  275. GPR_ASSERT(!reserved);
  276. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  277. rc->path = grpc_mdelem_from_slices(
  278. &exec_ctx, GRPC_MDSTR_PATH,
  279. grpc_slice_intern(grpc_slice_from_static_string(method)));
  280. rc->authority =
  281. host ? grpc_mdelem_from_slices(
  282. &exec_ctx, GRPC_MDSTR_AUTHORITY,
  283. grpc_slice_intern(grpc_slice_from_static_string(host)))
  284. : GRPC_MDNULL;
  285. gpr_mu_lock(&channel->registered_call_mu);
  286. rc->next = channel->registered_calls;
  287. channel->registered_calls = rc;
  288. gpr_mu_unlock(&channel->registered_call_mu);
  289. grpc_exec_ctx_finish(&exec_ctx);
  290. return rc;
  291. }
  292. grpc_call *grpc_channel_create_registered_call(
  293. grpc_channel *channel, grpc_call *parent_call, uint32_t propagation_mask,
  294. grpc_completion_queue *completion_queue, void *registered_call_handle,
  295. gpr_timespec deadline, void *reserved) {
  296. registered_call *rc = registered_call_handle;
  297. GRPC_API_TRACE(
  298. "grpc_channel_create_registered_call("
  299. "channel=%p, parent_call=%p, propagation_mask=%x, completion_queue=%p, "
  300. "registered_call_handle=%p, "
  301. "deadline=gpr_timespec { tv_sec: %" PRId64
  302. ", tv_nsec: %d, clock_type: %d }, "
  303. "reserved=%p)",
  304. 9, (channel, parent_call, (unsigned)propagation_mask, completion_queue,
  305. registered_call_handle, deadline.tv_sec, deadline.tv_nsec,
  306. (int)deadline.clock_type, reserved));
  307. GPR_ASSERT(!reserved);
  308. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  309. grpc_call *call = grpc_channel_create_call_internal(
  310. &exec_ctx, channel, parent_call, propagation_mask, completion_queue, NULL,
  311. GRPC_MDELEM_REF(rc->path), GRPC_MDELEM_REF(rc->authority), deadline);
  312. grpc_exec_ctx_finish(&exec_ctx);
  313. return call;
  314. }
  315. #ifndef NDEBUG
  316. #define REF_REASON reason
  317. #define REF_ARG , const char *reason
  318. #else
  319. #define REF_REASON ""
  320. #define REF_ARG
  321. #endif
  322. void grpc_channel_internal_ref(grpc_channel *c REF_ARG) {
  323. GRPC_CHANNEL_STACK_REF(CHANNEL_STACK_FROM_CHANNEL(c), REF_REASON);
  324. }
  325. void grpc_channel_internal_unref(grpc_exec_ctx *exec_ctx,
  326. grpc_channel *c REF_ARG) {
  327. GRPC_CHANNEL_STACK_UNREF(exec_ctx, CHANNEL_STACK_FROM_CHANNEL(c), REF_REASON);
  328. }
  329. static void destroy_channel(grpc_exec_ctx *exec_ctx, void *arg,
  330. grpc_error *error) {
  331. grpc_channel *channel = arg;
  332. grpc_channel_stack_destroy(exec_ctx, CHANNEL_STACK_FROM_CHANNEL(channel));
  333. while (channel->registered_calls) {
  334. registered_call *rc = channel->registered_calls;
  335. channel->registered_calls = rc->next;
  336. GRPC_MDELEM_UNREF(exec_ctx, rc->path);
  337. GRPC_MDELEM_UNREF(exec_ctx, rc->authority);
  338. gpr_free(rc);
  339. }
  340. GRPC_MDELEM_UNREF(exec_ctx, channel->default_authority);
  341. gpr_mu_destroy(&channel->registered_call_mu);
  342. gpr_free(channel->target);
  343. gpr_free(channel);
  344. }
  345. void grpc_channel_destroy(grpc_channel *channel) {
  346. grpc_transport_op *op = grpc_make_transport_op(NULL);
  347. grpc_channel_element *elem;
  348. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  349. GRPC_API_TRACE("grpc_channel_destroy(channel=%p)", 1, (channel));
  350. op->disconnect_with_error =
  351. GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel Destroyed");
  352. elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0);
  353. elem->filter->start_transport_op(&exec_ctx, elem, op);
  354. GRPC_CHANNEL_INTERNAL_UNREF(&exec_ctx, channel, "channel");
  355. grpc_exec_ctx_finish(&exec_ctx);
  356. }
  357. grpc_channel_stack *grpc_channel_get_channel_stack(grpc_channel *channel) {
  358. return CHANNEL_STACK_FROM_CHANNEL(channel);
  359. }
  360. grpc_compression_options grpc_channel_compression_options(
  361. const grpc_channel *channel) {
  362. return channel->compression_options;
  363. }
  364. grpc_mdelem grpc_channel_get_reffed_status_elem(grpc_exec_ctx *exec_ctx,
  365. grpc_channel *channel, int i) {
  366. char tmp[GPR_LTOA_MIN_BUFSIZE];
  367. switch (i) {
  368. case 0:
  369. return GRPC_MDELEM_GRPC_STATUS_0;
  370. case 1:
  371. return GRPC_MDELEM_GRPC_STATUS_1;
  372. case 2:
  373. return GRPC_MDELEM_GRPC_STATUS_2;
  374. }
  375. gpr_ltoa(i, tmp);
  376. return grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_GRPC_STATUS,
  377. grpc_slice_from_copied_string(tmp));
  378. }