blob: dbfc8a9336e5f4261d8ae947b07d642f87d231a9 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tillerc506e242016-01-04 15:59:29 -08003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
Craig Tilleracf0f072015-06-29 08:24:16 -070034#include <grpc/support/port_platform.h>
35
Craig Tiller178edfa2016-02-17 20:54:46 -080036#include <limits.h>
Hongwei Wanga3780a82015-07-17 15:27:18 -070037#include <memory.h>
38
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <grpc/grpc.h>
Hongwei Wanga3780a82015-07-17 15:27:18 -070040#include <grpc/support/alloc.h>
Craig Tillerf3756c12015-07-01 17:21:01 -070041#include <grpc/support/time.h>
Craig Tiller178edfa2016-02-17 20:54:46 -080042/* TODO(ctiller): find another way? - better not to include census here */
Craig Tilleradcb92d2016-03-28 10:14:05 -070043#include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
Craig Tiller9533d042016-03-25 17:11:06 -070044#include "src/core/lib/census/grpc_plugin.h"
45#include "src/core/lib/channel/channel_stack.h"
46#include "src/core/lib/channel/client_channel.h"
47#include "src/core/lib/channel/compress_filter.h"
48#include "src/core/lib/channel/connected_channel.h"
49#include "src/core/lib/channel/http_client_filter.h"
50#include "src/core/lib/channel/http_server_filter.h"
51#include "src/core/lib/client_config/lb_policies/pick_first.h"
52#include "src/core/lib/client_config/lb_policies/round_robin.h"
53#include "src/core/lib/client_config/lb_policy_registry.h"
54#include "src/core/lib/client_config/resolver_registry.h"
55#include "src/core/lib/client_config/resolvers/dns_resolver.h"
56#include "src/core/lib/client_config/resolvers/sockaddr_resolver.h"
57#include "src/core/lib/client_config/subchannel.h"
58#include "src/core/lib/client_config/subchannel_index.h"
59#include "src/core/lib/debug/trace.h"
60#include "src/core/lib/iomgr/executor.h"
61#include "src/core/lib/iomgr/iomgr.h"
62#include "src/core/lib/profiling/timers.h"
63#include "src/core/lib/surface/api_trace.h"
64#include "src/core/lib/surface/call.h"
65#include "src/core/lib/surface/channel_init.h"
66#include "src/core/lib/surface/completion_queue.h"
67#include "src/core/lib/surface/init.h"
68#include "src/core/lib/surface/lame_client.h"
69#include "src/core/lib/surface/server.h"
70#include "src/core/lib/surface/surface_trace.h"
Craig Tiller9533d042016-03-25 17:11:06 -070071#include "src/core/lib/transport/connectivity_state.h"
72#include "src/core/lib/transport/transport_impl.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073
Craig Tiller6d1882b2015-12-03 13:28:10 -080074#ifndef GRPC_DEFAULT_NAME_PREFIX
75#define GRPC_DEFAULT_NAME_PREFIX "dns:///"
76#endif
77
Hongwei Wang85ad6852015-08-13 16:13:10 -070078#define MAX_PLUGINS 128
79
Craig Tiller8ace0bc2015-03-05 17:16:09 -080080static gpr_once g_basic_init = GPR_ONCE_INIT;
Craig Tiller35108f62015-02-17 11:24:15 -080081static gpr_mu g_init_mu;
82static int g_initializations;
83
Craig Tiller8ace0bc2015-03-05 17:16:09 -080084static void do_basic_init(void) {
Craig Tiller35108f62015-02-17 11:24:15 -080085 gpr_mu_init(&g_init_mu);
Craig Tiller178edfa2016-02-17 20:54:46 -080086 /* TODO(ctiller): ideally remove this strict linkage */
87 grpc_register_plugin(census_grpc_plugin_init, census_grpc_plugin_destroy);
Craig Tiller35108f62015-02-17 11:24:15 -080088 g_initializations = 0;
89}
90
Craig Tiller178edfa2016-02-17 20:54:46 -080091static bool append_filter(grpc_channel_stack_builder *builder, void *arg) {
vjpaie7077b52016-03-21 20:58:44 -070092 return grpc_channel_stack_builder_append_filter(
93 builder, (const grpc_channel_filter *)arg, NULL, NULL);
Craig Tiller178edfa2016-02-17 20:54:46 -080094}
95
96static bool prepend_filter(grpc_channel_stack_builder *builder, void *arg) {
vjpaie7077b52016-03-21 20:58:44 -070097 return grpc_channel_stack_builder_prepend_filter(
98 builder, (const grpc_channel_filter *)arg, NULL, NULL);
Craig Tiller178edfa2016-02-17 20:54:46 -080099}
100
101static bool maybe_add_http_filter(grpc_channel_stack_builder *builder,
102 void *arg) {
103 grpc_transport *t = grpc_channel_stack_builder_get_transport(builder);
104 if (t && strstr(t->vtable->name, "http")) {
vjpaie7077b52016-03-21 20:58:44 -0700105 return grpc_channel_stack_builder_prepend_filter(
106 builder, (const grpc_channel_filter *)arg, NULL, NULL);
Craig Tiller178edfa2016-02-17 20:54:46 -0800107 }
108 return true;
109}
110
111static void register_builtin_channel_init() {
112 grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX, prepend_filter,
113 (void *)&grpc_compress_filter);
Craig Tillerd78d1642016-02-21 22:18:51 -0800114 grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL, INT_MAX,
115 prepend_filter,
Craig Tillerde676262016-02-19 12:28:34 -0800116 (void *)&grpc_compress_filter);
Craig Tiller178edfa2016-02-17 20:54:46 -0800117 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX, prepend_filter,
118 (void *)&grpc_compress_filter);
119 grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL, INT_MAX,
120 maybe_add_http_filter,
121 (void *)&grpc_http_client_filter);
122 grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL, INT_MAX,
123 grpc_add_connected_filter, NULL);
Craig Tillerde676262016-02-19 12:28:34 -0800124 grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL, INT_MAX,
125 maybe_add_http_filter,
126 (void *)&grpc_http_client_filter);
127 grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL, INT_MAX,
128 grpc_add_connected_filter, NULL);
Craig Tiller178edfa2016-02-17 20:54:46 -0800129 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX,
130 maybe_add_http_filter,
131 (void *)&grpc_http_server_filter);
132 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX,
133 grpc_add_connected_filter, NULL);
134 grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX, append_filter,
135 (void *)&grpc_client_channel_filter);
Craig Tiller178edfa2016-02-17 20:54:46 -0800136 grpc_channel_init_register_stage(GRPC_CLIENT_LAME_CHANNEL, INT_MAX,
137 append_filter, (void *)&grpc_lame_filter);
138 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX, prepend_filter,
139 (void *)&grpc_server_top_filter);
140}
141
Hongwei Wanga3780a82015-07-17 15:27:18 -0700142typedef struct grpc_plugin {
143 void (*init)();
Hongwei Wang85ad6852015-08-13 16:13:10 -0700144 void (*destroy)();
Hongwei Wanga3780a82015-07-17 15:27:18 -0700145} grpc_plugin;
146
Hongwei Wang85ad6852015-08-13 16:13:10 -0700147static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
148static int g_number_of_plugins = 0;
Hongwei Wanga3780a82015-07-17 15:27:18 -0700149
Hongwei Wang85ad6852015-08-13 16:13:10 -0700150void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
Jan Tattermusch1b34aeb2015-12-17 10:37:10 -0800151 GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2,
Craig Tiller178edfa2016-02-17 20:54:46 -0800152 ((void *)(intptr_t)init, (void *)(intptr_t)destroy));
Hongwei Wang85ad6852015-08-13 16:13:10 -0700153 GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
154 g_all_of_the_plugins[g_number_of_plugins].init = init;
155 g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
156 g_number_of_plugins++;
Hongwei Wangff6097a2015-08-12 17:48:56 -0700157}
158
Craig Tiller32946d32015-01-15 11:37:30 -0800159void grpc_init(void) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700160 int i;
Craig Tiller8ace0bc2015-03-05 17:16:09 -0800161 gpr_once_init(&g_basic_init, do_basic_init);
Craig Tiller35108f62015-02-17 11:24:15 -0800162
163 gpr_mu_lock(&g_init_mu);
164 if (++g_initializations == 1) {
Craig Tillerf3756c12015-07-01 17:21:01 -0700165 gpr_time_init();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800166 grpc_mdctx_global_init();
Craig Tiller178edfa2016-02-17 20:54:46 -0800167 grpc_channel_init_init();
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700168 grpc_lb_policy_registry_init(grpc_pick_first_lb_factory_create());
169 grpc_register_lb_policy(grpc_pick_first_lb_factory_create());
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700170 grpc_register_lb_policy(grpc_round_robin_lb_factory_create());
Craig Tiller6d1882b2015-12-03 13:28:10 -0800171 grpc_resolver_registry_init(GRPC_DEFAULT_NAME_PREFIX);
Craig Tillerbc85be12015-08-24 10:36:39 -0700172 grpc_register_resolver_type(grpc_dns_resolver_factory_create());
173 grpc_register_resolver_type(grpc_ipv4_resolver_factory_create());
174 grpc_register_resolver_type(grpc_ipv6_resolver_factory_create());
Craig Tilleracf0f072015-06-29 08:24:16 -0700175#ifdef GPR_POSIX_SOCKET
Craig Tillerbc85be12015-08-24 10:36:39 -0700176 grpc_register_resolver_type(grpc_unix_resolver_factory_create());
Craig Tilleracf0f072015-06-29 08:24:16 -0700177#endif
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700178 grpc_register_tracer("api", &grpc_api_trace);
Craig Tillerfaa84802015-03-01 21:56:38 -0800179 grpc_register_tracer("channel", &grpc_trace_channel);
Craig Tillerfaa84802015-03-01 21:56:38 -0800180 grpc_register_tracer("http", &grpc_http_trace);
Craig Tiller6a8c0382015-04-28 10:01:22 -0700181 grpc_register_tracer("flowctl", &grpc_flowctl_trace);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700182 grpc_register_tracer("connectivity_state", &grpc_connectivity_state_trace);
Craig Tillerd78d1642016-02-21 22:18:51 -0800183 grpc_register_tracer("channel_stack_builder",
184 &grpc_trace_channel_stack_builder);
Craig Tillerfaa84802015-03-01 21:56:38 -0800185 grpc_security_pre_init();
Craig Tiller35108f62015-02-17 11:24:15 -0800186 grpc_iomgr_init();
David Garcia Quintas4bc34632015-10-07 16:12:35 -0700187 grpc_executor_init();
Craig Tiller9aadeb82015-04-15 11:01:08 -0700188 grpc_tracer_init("GRPC_TRACE");
Craig Tiller0ba432d2015-10-09 16:57:11 -0700189 gpr_timers_global_init();
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800190 grpc_cq_global_init();
Craig Tiller8cdba662016-01-22 20:01:55 -0800191 grpc_subchannel_index_init();
Hongwei Wang85ad6852015-08-13 16:13:10 -0700192 for (i = 0; i < g_number_of_plugins; i++) {
193 if (g_all_of_the_plugins[i].init != NULL) {
194 g_all_of_the_plugins[i].init();
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700195 }
Hongwei Wanga3780a82015-07-17 15:27:18 -0700196 }
Craig Tiller178edfa2016-02-17 20:54:46 -0800197 /* register channel finalization AFTER all plugins, to ensure that it's run
198 * at the appropriate time */
199 grpc_register_security_filters();
200 register_builtin_channel_init();
201 /* no more changes to channel init pipelines */
202 grpc_channel_init_finalize();
Craig Tiller35108f62015-02-17 11:24:15 -0800203 }
204 gpr_mu_unlock(&g_init_mu);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700205 GRPC_API_TRACE("grpc_init(void)", 0, ());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206}
207
Craig Tiller32946d32015-01-15 11:37:30 -0800208void grpc_shutdown(void) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700209 int i;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700210 GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
Craig Tiller35108f62015-02-17 11:24:15 -0800211 gpr_mu_lock(&g_init_mu);
212 if (--g_initializations == 0) {
David Garcia Quintas4bc34632015-10-07 16:12:35 -0700213 grpc_executor_shutdown();
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800214 grpc_cq_global_shutdown();
215 grpc_iomgr_shutdown();
Craig Tiller8cdba662016-01-22 20:01:55 -0800216 grpc_subchannel_index_shutdown();
Craig Tiller0ba432d2015-10-09 16:57:11 -0700217 gpr_timers_global_destroy();
Craig Tiller2f300e22015-06-04 08:28:43 -0700218 grpc_tracer_shutdown();
Craig Tillerf1c61912015-07-01 07:19:22 -0700219 grpc_resolver_registry_shutdown();
Craig Tillerdf62b972015-12-09 15:50:19 -0800220 grpc_lb_policy_registry_shutdown();
Hongwei Wang85ad6852015-08-13 16:13:10 -0700221 for (i = 0; i < g_number_of_plugins; i++) {
222 if (g_all_of_the_plugins[i].destroy != NULL) {
223 g_all_of_the_plugins[i].destroy();
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700224 }
Hongwei Wanga3780a82015-07-17 15:27:18 -0700225 }
Craig Tiller178edfa2016-02-17 20:54:46 -0800226 grpc_channel_init_shutdown();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800227 grpc_mdctx_global_shutdown();
Craig Tiller35108f62015-02-17 11:24:15 -0800228 }
229 gpr_mu_unlock(&g_init_mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800230}
Craig Tiller60fd3612015-03-05 16:24:22 -0800231
232int grpc_is_initialized(void) {
233 int r;
Craig Tiller8ace0bc2015-03-05 17:16:09 -0800234 gpr_once_init(&g_basic_init, do_basic_init);
Craig Tiller60fd3612015-03-05 16:24:22 -0800235 gpr_mu_lock(&g_init_mu);
236 r = g_initializations > 0;
237 gpr_mu_unlock(&g_init_mu);
238 return r;
239}