blob: 1be734cdb7841c91019cabff2ea89660aed6c406 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
Craig Tiller9533d042016-03-25 17:11:06 -070019#include "src/core/lib/surface/channel.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080020
Yash Tibrewalfcd26bc2017-09-25 15:08:28 -070021#include <inttypes.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080022#include <stdlib.h>
23#include <string.h>
24
David Garcia Quintas73dcbda2016-04-23 00:17:05 -070025#include <grpc/compression.h>
Craig Tiller1b22b9d2015-07-20 13:42:22 -070026#include <grpc/support/alloc.h>
27#include <grpc/support/log.h>
28#include <grpc/support/string_util.h>
29
David Garcia Quintas73dcbda2016-04-23 00:17:05 -070030#include "src/core/lib/channel/channel_args.h"
Craig Tillerf1dc9c32017-09-13 14:21:27 -070031#include "src/core/lib/debug/stats.h"
Craig Tiller9533d042016-03-25 17:11:06 -070032#include "src/core/lib/iomgr/iomgr.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080033#include "src/core/lib/slice/slice_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/support/string.h"
35#include "src/core/lib/surface/api_trace.h"
36#include "src/core/lib/surface/call.h"
37#include "src/core/lib/surface/channel_init.h"
Craig Tiller9533d042016-03-25 17:11:06 -070038#include "src/core/lib/transport/static_metadata.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
Craig Tiller5d5bd222015-06-08 17:01:49 -070040/** Cache grpc-status: X mdelems for X = 0..NUM_CACHED_STATUS_ELEMS.
41 * Avoids needing to take a metadata context lock for sending status
42 * if the status code is <= NUM_CACHED_STATUS_ELEMS.
43 * Sized to allow the most commonly used codes to fit in
44 * (OK, Cancelled, Unknown). */
Craig Tiller3fc8e822015-06-08 16:31:28 -070045#define NUM_CACHED_STATUS_ELEMS 3
46
Craig Tillera82950e2015-09-22 12:33:20 -070047typedef struct registered_call {
Craig Tiller7c70b6c2017-01-23 07:48:42 -080048 grpc_mdelem path;
49 grpc_mdelem authority;
Craig Tillerbaa14a92017-11-03 09:09:36 -070050 struct registered_call* next;
Craig Tiller08453372015-04-10 16:05:38 -070051} registered_call;
52
Craig Tillera82950e2015-09-22 12:33:20 -070053struct grpc_channel {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054 int is_client;
David Garcia Quintas73dcbda2016-04-23 00:17:05 -070055 grpc_compression_options compression_options;
Craig Tiller7c70b6c2017-01-23 07:48:42 -080056 grpc_mdelem default_authority;
Craig Tiller08453372015-04-10 16:05:38 -070057
Craig Tillera6bec8f2017-03-14 08:26:04 -070058 gpr_atm call_size_estimate;
59
Craig Tiller08453372015-04-10 16:05:38 -070060 gpr_mu registered_call_mu;
Craig Tillerbaa14a92017-11-03 09:09:36 -070061 registered_call* registered_calls;
David Garcia Quintasb0dd2532016-05-17 19:14:51 -070062
Craig Tillerbaa14a92017-11-03 09:09:36 -070063 char* target;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064};
65
Craig Tillerbaa14a92017-11-03 09:09:36 -070066#define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack*)((c) + 1))
Craig Tillerb20111c2015-04-10 23:27:11 +000067#define CHANNEL_FROM_CHANNEL_STACK(channel_stack) \
Craig Tillerbaa14a92017-11-03 09:09:36 -070068 (((grpc_channel*)(channel_stack)) - 1)
Craig Tillerda669372015-02-05 10:10:15 -080069#define CHANNEL_FROM_TOP_ELEM(top_elem) \
70 CHANNEL_FROM_CHANNEL_STACK(grpc_channel_stack_from_top_element(top_elem))
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071
Craig Tillerbaa14a92017-11-03 09:09:36 -070072static void destroy_channel(grpc_exec_ctx* exec_ctx, void* arg,
73 grpc_error* error);
Craig Tiller7b435612015-11-24 08:15:05 -080074
Craig Tillerbaa14a92017-11-03 09:09:36 -070075grpc_channel* grpc_channel_create_with_builder(
76 grpc_exec_ctx* exec_ctx, grpc_channel_stack_builder* builder,
Craig Tillerb7f35a62017-03-16 13:29:05 -070077 grpc_channel_stack_type channel_stack_type) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070078 char* target = gpr_strdup(grpc_channel_stack_builder_get_target(builder));
79 grpc_channel_args* args = grpc_channel_args_copy(
Mark D. Roth5e2566e2016-11-18 10:53:13 -080080 grpc_channel_stack_builder_get_channel_arguments(builder));
Craig Tillerbaa14a92017-11-03 09:09:36 -070081 grpc_channel* channel;
Craig Tillerf1dc9c32017-09-13 14:21:27 -070082 if (channel_stack_type == GRPC_SERVER_CHANNEL) {
83 GRPC_STATS_INC_SERVER_CHANNELS_CREATED(exec_ctx);
84 } else {
85 GRPC_STATS_INC_CLIENT_CHANNELS_CREATED(exec_ctx);
86 }
Craig Tillerbaa14a92017-11-03 09:09:36 -070087 grpc_error* error = grpc_channel_stack_builder_finish(
Craig Tiller4782d922017-11-10 09:53:21 -080088 exec_ctx, builder, sizeof(grpc_channel), 1, destroy_channel, nullptr,
Craig Tillerbaa14a92017-11-03 09:09:36 -070089 (void**)&channel);
Mark D. Roth5e2566e2016-11-18 10:53:13 -080090 if (error != GRPC_ERROR_NONE) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -080091 gpr_log(GPR_ERROR, "channel stack builder failed: %s",
92 grpc_error_string(error));
Mark D. Rothe62605f2016-11-29 16:31:36 +000093 GRPC_ERROR_UNREF(error);
Craig Tillerb7f35a62017-03-16 13:29:05 -070094 gpr_free(target);
Mark D. Rothe62605f2016-11-29 16:31:36 +000095 goto done;
Craig Tiller839bebe2016-04-06 08:07:11 -070096 }
Craig Tiller178edfa2016-02-17 20:54:46 -080097
Craig Tillera82950e2015-09-22 12:33:20 -070098 memset(channel, 0, sizeof(*channel));
Craig Tillerb7f35a62017-03-16 13:29:05 -070099 channel->target = target;
Mark D. Rothe62605f2016-11-29 16:31:36 +0000100 channel->is_client = grpc_channel_stack_type_is_client(channel_stack_type);
Craig Tillera82950e2015-09-22 12:33:20 -0700101 gpr_mu_init(&channel->registered_call_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800102 channel->registered_calls = nullptr;
Craig Tiller629b0ed2015-04-22 11:14:26 -0700103
Craig Tillera6bec8f2017-03-14 08:26:04 -0700104 gpr_atm_no_barrier_store(
105 &channel->call_size_estimate,
Craig Tiller2ccd5022017-03-16 09:57:46 -0700106 (gpr_atm)CHANNEL_STACK_FROM_CHANNEL(channel)->call_stack_size);
Craig Tillera6bec8f2017-03-14 08:26:04 -0700107
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700108 grpc_compression_options_init(&channel->compression_options);
Mark D. Rothe62605f2016-11-29 16:31:36 +0000109 for (size_t i = 0; i < args->num_args; i++) {
110 if (0 == strcmp(args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY)) {
111 if (args->args[i].type != GRPC_ARG_STRING) {
112 gpr_log(GPR_ERROR, "%s ignored: it must be a string",
113 GRPC_ARG_DEFAULT_AUTHORITY);
114 } else {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800115 if (!GRPC_MDISNULL(channel->default_authority)) {
Mark D. Rothe62605f2016-11-29 16:31:36 +0000116 /* setting this takes precedence over anything else */
Craig Tiller4cc1c352016-12-27 08:48:01 -0800117 GRPC_MDELEM_UNREF(exec_ctx, channel->default_authority);
Craig Tillera82950e2015-09-22 12:33:20 -0700118 }
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800119 channel->default_authority = grpc_mdelem_from_slices(
120 exec_ctx, GRPC_MDSTR_AUTHORITY,
Craig Tiller5f4264f2017-02-08 14:53:04 -0800121 grpc_slice_intern(
122 grpc_slice_from_static_string(args->args[i].value.string)));
Mark D. Rothe62605f2016-11-29 16:31:36 +0000123 }
124 } else if (0 ==
125 strcmp(args->args[i].key, GRPC_SSL_TARGET_NAME_OVERRIDE_ARG)) {
126 if (args->args[i].type != GRPC_ARG_STRING) {
127 gpr_log(GPR_ERROR, "%s ignored: it must be a string",
128 GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
129 } else {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800130 if (!GRPC_MDISNULL(channel->default_authority)) {
Mark D. Rothe62605f2016-11-29 16:31:36 +0000131 /* other ways of setting this (notably ssl) take precedence */
132 gpr_log(GPR_ERROR,
133 "%s ignored: default host already set some other way",
Craig Tillera82950e2015-09-22 12:33:20 -0700134 GRPC_SSL_TARGET_NAME_OVERRIDE_ARG);
135 } else {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800136 channel->default_authority = grpc_mdelem_from_slices(
137 exec_ctx, GRPC_MDSTR_AUTHORITY,
Craig Tiller5f4264f2017-02-08 14:53:04 -0800138 grpc_slice_intern(
139 grpc_slice_from_static_string(args->args[i].value.string)));
Craig Tillera82950e2015-09-22 12:33:20 -0700140 }
141 }
Mark D. Rothe62605f2016-11-29 16:31:36 +0000142 } else if (0 == strcmp(args->args[i].key,
143 GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL)) {
144 channel->compression_options.default_level.is_set = true;
Mark D. Rothe62605f2016-11-29 16:31:36 +0000145 channel->compression_options.default_level.level =
David Garcia Quintas228a5142017-03-30 19:43:00 -0700146 (grpc_compression_level)grpc_channel_arg_get_integer(
147 &args->args[i],
Yash Tibrewal37fdb732017-09-25 16:45:02 -0700148 {GRPC_COMPRESS_LEVEL_NONE, GRPC_COMPRESS_LEVEL_NONE,
149 GRPC_COMPRESS_LEVEL_COUNT - 1});
Mark D. Rothe62605f2016-11-29 16:31:36 +0000150 } else if (0 == strcmp(args->args[i].key,
Muxi Yand37411e2017-08-17 18:44:12 -0700151 GRPC_STREAM_COMPRESSION_CHANNEL_DEFAULT_LEVEL)) {
152 channel->compression_options.default_stream_compression_level.is_set =
153 true;
154 channel->compression_options.default_stream_compression_level.level =
Muxi Yanda4f0872017-08-18 17:05:27 -0700155 (grpc_stream_compression_level)grpc_channel_arg_get_integer(
Muxi Yand37411e2017-08-17 18:44:12 -0700156 &args->args[i],
Yash Tibrewal37fdb732017-09-25 16:45:02 -0700157 {GRPC_STREAM_COMPRESS_LEVEL_NONE, GRPC_STREAM_COMPRESS_LEVEL_NONE,
158 GRPC_STREAM_COMPRESS_LEVEL_COUNT - 1});
Muxi Yand37411e2017-08-17 18:44:12 -0700159 } else if (0 == strcmp(args->args[i].key,
Mark D. Rothe62605f2016-11-29 16:31:36 +0000160 GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM)) {
161 channel->compression_options.default_algorithm.is_set = true;
Mark D. Rothe62605f2016-11-29 16:31:36 +0000162 channel->compression_options.default_algorithm.algorithm =
David Garcia Quintas228a5142017-03-30 19:43:00 -0700163 (grpc_compression_algorithm)grpc_channel_arg_get_integer(
Yash Tibrewal37fdb732017-09-25 16:45:02 -0700164 &args->args[i], {GRPC_COMPRESS_NONE, GRPC_COMPRESS_NONE,
165 GRPC_COMPRESS_ALGORITHMS_COUNT - 1});
Muxi Yan68a0fd52017-07-21 09:26:04 -0700166 } else if (0 == strcmp(args->args[i].key,
167 GRPC_STREAM_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM)) {
168 channel->compression_options.default_stream_compression_algorithm.is_set =
169 true;
170 channel->compression_options.default_stream_compression_algorithm
171 .algorithm =
172 (grpc_stream_compression_algorithm)grpc_channel_arg_get_integer(
173 &args->args[i],
Yash Tibrewal37fdb732017-09-25 16:45:02 -0700174 {GRPC_STREAM_COMPRESS_NONE, GRPC_STREAM_COMPRESS_NONE,
175 GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT - 1});
Mark D. Rothe62605f2016-11-29 16:31:36 +0000176 } else if (0 ==
177 strcmp(args->args[i].key,
178 GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET)) {
179 channel->compression_options.enabled_algorithms_bitset =
180 (uint32_t)args->args[i].value.integer |
181 0x1; /* always support no compression */
Muxi Yan68a0fd52017-07-21 09:26:04 -0700182 } else if (0 ==
183 strcmp(
184 args->args[i].key,
185 GRPC_STREAM_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET)) {
186 channel->compression_options
187 .enabled_stream_compression_algorithms_bitset =
188 (uint32_t)args->args[i].value.integer |
189 0x1; /* always support no compression */
Craig Tiller629b0ed2015-04-22 11:14:26 -0700190 }
Craig Tillera82950e2015-09-22 12:33:20 -0700191 }
Craig Tiller629b0ed2015-04-22 11:14:26 -0700192
Mark D. Rothe62605f2016-11-29 16:31:36 +0000193done:
Craig Tiller4cc1c352016-12-27 08:48:01 -0800194 grpc_channel_args_destroy(exec_ctx, args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800195 return channel;
196}
197
Craig Tillerbaa14a92017-11-03 09:09:36 -0700198grpc_channel* grpc_channel_create(grpc_exec_ctx* exec_ctx, const char* target,
199 const grpc_channel_args* input_args,
Craig Tillerb7f35a62017-03-16 13:29:05 -0700200 grpc_channel_stack_type channel_stack_type,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700201 grpc_transport* optional_transport) {
202 grpc_channel_stack_builder* builder = grpc_channel_stack_builder_create();
Craig Tillerb7f35a62017-03-16 13:29:05 -0700203 grpc_channel_stack_builder_set_channel_arguments(exec_ctx, builder,
204 input_args);
205 grpc_channel_stack_builder_set_target(builder, target);
206 grpc_channel_stack_builder_set_transport(builder, optional_transport);
207 if (!grpc_channel_init_create_stack(exec_ctx, builder, channel_stack_type)) {
208 grpc_channel_stack_builder_destroy(exec_ctx, builder);
Craig Tiller4782d922017-11-10 09:53:21 -0800209 return nullptr;
Craig Tillerb7f35a62017-03-16 13:29:05 -0700210 }
211 return grpc_channel_create_with_builder(exec_ctx, builder,
212 channel_stack_type);
213}
214
Craig Tillerbaa14a92017-11-03 09:09:36 -0700215size_t grpc_channel_get_call_size_estimate(grpc_channel* channel) {
Craig Tillera6bec8f2017-03-14 08:26:04 -0700216#define ROUND_UP_SIZE 256
Craig Tiller878a7c22017-03-23 15:14:48 -0700217 /* We round up our current estimate to the NEXT value of ROUND_UP_SIZE.
218 This ensures:
219 1. a consistent size allocation when our estimate is drifting slowly
220 (which is common) - which tends to help most allocators reuse memory
221 2. a small amount of allowed growth over the estimate without hitting
222 the arena size doubling case, reducing overall memory usage */
Craig Tillerb64d6522017-03-23 15:17:18 -0700223 return ((size_t)gpr_atm_no_barrier_load(&channel->call_size_estimate) +
224 2 * ROUND_UP_SIZE) &
225 ~(size_t)(ROUND_UP_SIZE - 1);
Craig Tillera6bec8f2017-03-14 08:26:04 -0700226}
227
Craig Tillerbaa14a92017-11-03 09:09:36 -0700228void grpc_channel_update_call_size_estimate(grpc_channel* channel,
Craig Tillera6bec8f2017-03-14 08:26:04 -0700229 size_t size) {
230 size_t cur = (size_t)gpr_atm_no_barrier_load(&channel->call_size_estimate);
231 if (cur < size) {
232 /* size grew: update estimate */
233 gpr_atm_no_barrier_cas(&channel->call_size_estimate, (gpr_atm)cur,
234 (gpr_atm)size);
235 /* if we lose: never mind, something else will likely update soon enough */
236 } else if (cur == size) {
237 /* no change: holding pattern */
238 } else if (cur > 0) {
239 /* size shrank: decrease estimate */
240 gpr_atm_no_barrier_cas(
241 &channel->call_size_estimate, (gpr_atm)cur,
242 (gpr_atm)(GPR_MIN(cur - 1, (255 * cur + size) / 256)));
243 /* if we lose: never mind, something else will likely update soon enough */
244 }
245}
246
Craig Tillerbaa14a92017-11-03 09:09:36 -0700247char* grpc_channel_get_target(grpc_channel* channel) {
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700248 GRPC_API_TRACE("grpc_channel_get_target(channel=%p)", 1, (channel));
Craig Tillera82950e2015-09-22 12:33:20 -0700249 return gpr_strdup(channel->target);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700250}
251
Craig Tillerbaa14a92017-11-03 09:09:36 -0700252void grpc_channel_get_info(grpc_channel* channel,
253 const grpc_channel_info* channel_info) {
Mark D. Rothb2d24882016-10-27 15:44:07 -0700254 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700255 grpc_channel_element* elem =
Mark D. Rothb2d24882016-10-27 15:44:07 -0700256 grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0);
257 elem->filter->get_channel_info(&exec_ctx, elem, channel_info);
258 grpc_exec_ctx_finish(&exec_ctx);
259}
260
Craig Tillerbaa14a92017-11-03 09:09:36 -0700261static grpc_call* grpc_channel_create_call_internal(
262 grpc_exec_ctx* exec_ctx, grpc_channel* channel, grpc_call* parent_call,
263 uint32_t propagation_mask, grpc_completion_queue* cq,
264 grpc_pollset_set* pollset_set_alternative, grpc_mdelem path_mdelem,
Craig Tiller89c14282017-07-19 15:32:27 -0700265 grpc_mdelem authority_mdelem, grpc_millis deadline) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800266 grpc_mdelem send_metadata[2];
Craig Tiller32ca48c2015-09-10 11:47:15 -0700267 size_t num_metadata = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800268
Craig Tillera82950e2015-09-22 12:33:20 -0700269 GPR_ASSERT(channel->is_client);
Craig Tiller4782d922017-11-10 09:53:21 -0800270 GPR_ASSERT(!(cq != nullptr && pollset_set_alternative != nullptr));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800271
Craig Tillerc4b56b62015-07-23 17:44:11 -0700272 send_metadata[num_metadata++] = path_mdelem;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800273 if (!GRPC_MDISNULL(authority_mdelem)) {
Craig Tillera82950e2015-09-22 12:33:20 -0700274 send_metadata[num_metadata++] = authority_mdelem;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800275 } else if (!GRPC_MDISNULL(channel->default_authority)) {
Craig Tillera82950e2015-09-22 12:33:20 -0700276 send_metadata[num_metadata++] = GRPC_MDELEM_REF(channel->default_authority);
277 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800278
Craig Tiller8e214652016-08-19 09:54:31 -0700279 grpc_call_create_args args;
280 memset(&args, 0, sizeof(args));
281 args.channel = channel;
Yash Tibrewalb2a54ac2017-09-13 10:18:07 -0700282 args.parent = parent_call;
Craig Tiller8e214652016-08-19 09:54:31 -0700283 args.propagation_mask = propagation_mask;
284 args.cq = cq;
285 args.pollset_set_alternative = pollset_set_alternative;
Craig Tiller4782d922017-11-10 09:53:21 -0800286 args.server_transport_data = nullptr;
Craig Tiller8e214652016-08-19 09:54:31 -0700287 args.add_initial_metadata = send_metadata;
288 args.add_initial_metadata_count = num_metadata;
289 args.send_deadline = deadline;
290
Craig Tillerbaa14a92017-11-03 09:09:36 -0700291 grpc_call* call;
Craig Tillera59c16c2016-10-31 07:25:01 -0700292 GRPC_LOG_IF_ERROR("call_create", grpc_call_create(exec_ctx, &args, &call));
Craig Tiller8e214652016-08-19 09:54:31 -0700293 return call;
Craig Tiller6902ad22015-04-16 08:01:49 -0700294}
klempnerc463f742014-12-19 13:03:35 -0800295
Craig Tillerbaa14a92017-11-03 09:09:36 -0700296grpc_call* grpc_channel_create_call(grpc_channel* channel,
297 grpc_call* parent_call,
Craig Tiller7536af02015-12-22 13:49:30 -0800298 uint32_t propagation_mask,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700299 grpc_completion_queue* cq,
300 grpc_slice method, const grpc_slice* host,
301 gpr_timespec deadline, void* reserved) {
Craig Tillera82950e2015-09-22 12:33:20 -0700302 GPR_ASSERT(!reserved);
Craig Tillera59c16c2016-10-31 07:25:01 -0700303 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700304 grpc_call* call = grpc_channel_create_call_internal(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800305 &exec_ctx, channel, parent_call, propagation_mask, cq, nullptr,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800306 grpc_mdelem_from_slices(&exec_ctx, GRPC_MDSTR_PATH,
307 grpc_slice_ref_internal(method)),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800308 host != nullptr ? grpc_mdelem_from_slices(&exec_ctx, GRPC_MDSTR_AUTHORITY,
Craig Tillerbe98d242017-11-10 15:26:57 -0800309 grpc_slice_ref_internal(*host))
310 : GRPC_MDNULL,
Craig Tiller9a8c3f32017-07-21 13:14:14 -0700311 grpc_timespec_to_millis_round_up(deadline));
Craig Tillera59c16c2016-10-31 07:25:01 -0700312 grpc_exec_ctx_finish(&exec_ctx);
313 return call;
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700314}
315
Craig Tillerbaa14a92017-11-03 09:09:36 -0700316grpc_call* grpc_channel_create_pollset_set_call(
317 grpc_exec_ctx* exec_ctx, grpc_channel* channel, grpc_call* parent_call,
318 uint32_t propagation_mask, grpc_pollset_set* pollset_set, grpc_slice method,
319 const grpc_slice* host, grpc_millis deadline, void* reserved) {
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700320 GPR_ASSERT(!reserved);
321 return grpc_channel_create_call_internal(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800322 exec_ctx, channel, parent_call, propagation_mask, nullptr, pollset_set,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800323 grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_PATH,
324 grpc_slice_ref_internal(method)),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800325 host != nullptr ? grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_AUTHORITY,
Craig Tillerbe98d242017-11-10 15:26:57 -0800326 grpc_slice_ref_internal(*host))
327 : GRPC_MDNULL,
Craig Tillera82950e2015-09-22 12:33:20 -0700328 deadline);
Craig Tiller08453372015-04-10 16:05:38 -0700329}
330
Craig Tillerbaa14a92017-11-03 09:09:36 -0700331void* grpc_channel_register_call(grpc_channel* channel, const char* method,
332 const char* host, void* reserved) {
333 registered_call* rc = (registered_call*)gpr_malloc(sizeof(registered_call));
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700334 GRPC_API_TRACE(
335 "grpc_channel_register_call(channel=%p, method=%s, host=%s, reserved=%p)",
336 4, (channel, method, host, reserved));
Craig Tillera82950e2015-09-22 12:33:20 -0700337 GPR_ASSERT(!reserved);
Craig Tillera59c16c2016-10-31 07:25:01 -0700338 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800339
340 rc->path = grpc_mdelem_from_slices(
341 &exec_ctx, GRPC_MDSTR_PATH,
342 grpc_slice_intern(grpc_slice_from_static_string(method)));
Craig Tillera59c16c2016-10-31 07:25:01 -0700343 rc->authority =
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800344 host ? grpc_mdelem_from_slices(
345 &exec_ctx, GRPC_MDSTR_AUTHORITY,
346 grpc_slice_intern(grpc_slice_from_static_string(host)))
347 : GRPC_MDNULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700348 gpr_mu_lock(&channel->registered_call_mu);
Craig Tiller08453372015-04-10 16:05:38 -0700349 rc->next = channel->registered_calls;
350 channel->registered_calls = rc;
Craig Tillera82950e2015-09-22 12:33:20 -0700351 gpr_mu_unlock(&channel->registered_call_mu);
Craig Tillera59c16c2016-10-31 07:25:01 -0700352 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller08453372015-04-10 16:05:38 -0700353 return rc;
354}
355
Craig Tillerbaa14a92017-11-03 09:09:36 -0700356grpc_call* grpc_channel_create_registered_call(
357 grpc_channel* channel, grpc_call* parent_call, uint32_t propagation_mask,
358 grpc_completion_queue* completion_queue, void* registered_call_handle,
359 gpr_timespec deadline, void* reserved) {
360 registered_call* rc = (registered_call*)registered_call_handle;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700361 GRPC_API_TRACE(
362 "grpc_channel_create_registered_call("
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700363 "channel=%p, parent_call=%p, propagation_mask=%x, completion_queue=%p, "
364 "registered_call_handle=%p, "
Ken Payson62a6c922016-06-24 11:53:54 -0700365 "deadline=gpr_timespec { tv_sec: %" PRId64
366 ", tv_nsec: %d, clock_type: %d }, "
Craig Tiller4de3e4f2015-10-05 08:55:50 -0700367 "reserved=%p)",
Craig Tillerbaa14a92017-11-03 09:09:36 -0700368 9,
369 (channel, parent_call, (unsigned)propagation_mask, completion_queue,
370 registered_call_handle, deadline.tv_sec, deadline.tv_nsec,
371 (int)deadline.clock_type, reserved));
Craig Tillera82950e2015-09-22 12:33:20 -0700372 GPR_ASSERT(!reserved);
Craig Tillera59c16c2016-10-31 07:25:01 -0700373 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700374 grpc_call* call = grpc_channel_create_call_internal(
Craig Tillerbe98d242017-11-10 15:26:57 -0800375 &exec_ctx, channel, parent_call, propagation_mask, completion_queue,
376 nullptr, GRPC_MDELEM_REF(rc->path), GRPC_MDELEM_REF(rc->authority),
Craig Tiller9a8c3f32017-07-21 13:14:14 -0700377 grpc_timespec_to_millis_round_up(deadline));
Craig Tillera59c16c2016-10-31 07:25:01 -0700378 grpc_exec_ctx_finish(&exec_ctx);
379 return call;
Craig Tiller08453372015-04-10 16:05:38 -0700380}
381
ncteisen9c43fc02017-06-08 16:06:23 -0700382#ifndef NDEBUG
Craig Tiller7b435612015-11-24 08:15:05 -0800383#define REF_REASON reason
Craig Tillerbaa14a92017-11-03 09:09:36 -0700384#define REF_ARG , const char* reason
Craig Tiller9ec2a522015-05-29 22:46:54 -0700385#else
Craig Tiller7b435612015-11-24 08:15:05 -0800386#define REF_REASON ""
387#define REF_ARG
Craig Tiller9ec2a522015-05-29 22:46:54 -0700388#endif
Craig Tillerbaa14a92017-11-03 09:09:36 -0700389void grpc_channel_internal_ref(grpc_channel* c REF_ARG) {
Craig Tiller7b435612015-11-24 08:15:05 -0800390 GRPC_CHANNEL_STACK_REF(CHANNEL_STACK_FROM_CHANNEL(c), REF_REASON);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800391}
392
Craig Tillerbaa14a92017-11-03 09:09:36 -0700393void grpc_channel_internal_unref(grpc_exec_ctx* exec_ctx,
394 grpc_channel* c REF_ARG) {
Craig Tiller7b435612015-11-24 08:15:05 -0800395 GRPC_CHANNEL_STACK_UNREF(exec_ctx, CHANNEL_STACK_FROM_CHANNEL(c), REF_REASON);
396}
397
Craig Tillerbaa14a92017-11-03 09:09:36 -0700398static void destroy_channel(grpc_exec_ctx* exec_ctx, void* arg,
399 grpc_error* error) {
400 grpc_channel* channel = (grpc_channel*)arg;
Craig Tillera82950e2015-09-22 12:33:20 -0700401 grpc_channel_stack_destroy(exec_ctx, CHANNEL_STACK_FROM_CHANNEL(channel));
Craig Tillera82950e2015-09-22 12:33:20 -0700402 while (channel->registered_calls) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700403 registered_call* rc = channel->registered_calls;
Craig Tillera82950e2015-09-22 12:33:20 -0700404 channel->registered_calls = rc->next;
Craig Tillera59c16c2016-10-31 07:25:01 -0700405 GRPC_MDELEM_UNREF(exec_ctx, rc->path);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800406 GRPC_MDELEM_UNREF(exec_ctx, rc->authority);
Craig Tillera82950e2015-09-22 12:33:20 -0700407 gpr_free(rc);
408 }
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800409 GRPC_MDELEM_UNREF(exec_ctx, channel->default_authority);
Craig Tillera82950e2015-09-22 12:33:20 -0700410 gpr_mu_destroy(&channel->registered_call_mu);
411 gpr_free(channel->target);
412 gpr_free(channel);
Craig Tiller7bd5ab12015-02-17 22:29:04 -0800413}
414
Craig Tillerbaa14a92017-11-03 09:09:36 -0700415void grpc_channel_destroy(grpc_channel* channel) {
Craig Tiller4782d922017-11-10 09:53:21 -0800416 grpc_transport_op* op = grpc_make_transport_op(nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700417 grpc_channel_element* elem;
Craig Tillerf5768a62015-09-22 10:54:34 -0700418 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700419 GRPC_API_TRACE("grpc_channel_destroy(channel=%p)", 1, (channel));
ncteisen4b36a3d2017-03-13 19:08:06 -0700420 op->disconnect_with_error =
421 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel Destroyed");
Craig Tillera82950e2015-09-22 12:33:20 -0700422 elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CHANNEL(channel), 0);
Craig Tillere0221ff2016-07-11 15:56:08 -0700423 elem->filter->start_transport_op(&exec_ctx, elem, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800424
Craig Tillera82950e2015-09-22 12:33:20 -0700425 GRPC_CHANNEL_INTERNAL_UNREF(&exec_ctx, channel, "channel");
Craig Tillerdfff1b82015-09-21 14:39:57 -0700426
Craig Tillera82950e2015-09-22 12:33:20 -0700427 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800428}
429
Craig Tillerbaa14a92017-11-03 09:09:36 -0700430grpc_channel_stack* grpc_channel_get_channel_stack(grpc_channel* channel) {
Craig Tillera82950e2015-09-22 12:33:20 -0700431 return CHANNEL_STACK_FROM_CHANNEL(channel);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800432}
433
David Garcia Quintasac094472016-05-18 20:25:57 -0700434grpc_compression_options grpc_channel_compression_options(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700435 const grpc_channel* channel) {
David Garcia Quintas73dcbda2016-04-23 00:17:05 -0700436 return channel->compression_options;
437}
438
Craig Tillerbaa14a92017-11-03 09:09:36 -0700439grpc_mdelem grpc_channel_get_reffed_status_elem(grpc_exec_ctx* exec_ctx,
440 grpc_channel* channel, int i) {
Craig Tillerebdef9d2015-11-19 17:09:49 -0800441 char tmp[GPR_LTOA_MIN_BUFSIZE];
442 switch (i) {
443 case 0:
444 return GRPC_MDELEM_GRPC_STATUS_0;
445 case 1:
446 return GRPC_MDELEM_GRPC_STATUS_1;
447 case 2:
448 return GRPC_MDELEM_GRPC_STATUS_2;
Craig Tillera82950e2015-09-22 12:33:20 -0700449 }
Craig Tillerebdef9d2015-11-19 17:09:49 -0800450 gpr_ltoa(i, tmp);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800451 return grpc_mdelem_from_slices(exec_ctx, GRPC_MDSTR_GRPC_STATUS,
452 grpc_slice_from_copied_string(tmp));
Craig Tiller190d3602015-02-18 09:23:38 -0800453}