blob: 03f379aba889ba3d66583f93dd22388605bdca16 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, 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>
Yuchen Zeng95e4c482016-04-15 10:38:24 -070041#include <grpc/support/log.h>
Craig Tillerf3756c12015-07-01 17:21:01 -070042#include <grpc/support/time.h>
Craig Tiller9533d042016-03-25 17:11:06 -070043#include "src/core/lib/channel/channel_stack.h"
Craig Tiller9533d042016-03-25 17:11:06 -070044#include "src/core/lib/channel/compress_filter.h"
45#include "src/core/lib/channel/connected_channel.h"
46#include "src/core/lib/channel/http_client_filter.h"
47#include "src/core/lib/channel/http_server_filter.h"
Craig Tiller9533d042016-03-25 17:11:06 -070048#include "src/core/lib/debug/trace.h"
Craig Tiller3ab2fe02016-04-11 20:11:18 -070049#include "src/core/lib/http/parser.h"
Craig Tiller9533d042016-03-25 17:11:06 -070050#include "src/core/lib/iomgr/executor.h"
51#include "src/core/lib/iomgr/iomgr.h"
52#include "src/core/lib/profiling/timers.h"
53#include "src/core/lib/surface/api_trace.h"
54#include "src/core/lib/surface/call.h"
55#include "src/core/lib/surface/channel_init.h"
56#include "src/core/lib/surface/completion_queue.h"
57#include "src/core/lib/surface/init.h"
58#include "src/core/lib/surface/lame_client.h"
59#include "src/core/lib/surface/server.h"
60#include "src/core/lib/surface/surface_trace.h"
Craig Tiller9533d042016-03-25 17:11:06 -070061#include "src/core/lib/transport/connectivity_state.h"
62#include "src/core/lib/transport/transport_impl.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063
Craig Tiller3113ef42016-03-29 09:03:14 -070064/* (generated) built in registry of plugins */
65extern void grpc_register_built_in_plugins(void);
66
Hongwei Wang85ad6852015-08-13 16:13:10 -070067#define MAX_PLUGINS 128
68
Craig Tiller8ace0bc2015-03-05 17:16:09 -080069static gpr_once g_basic_init = GPR_ONCE_INIT;
Craig Tiller35108f62015-02-17 11:24:15 -080070static gpr_mu g_init_mu;
71static int g_initializations;
72
Craig Tiller8ace0bc2015-03-05 17:16:09 -080073static void do_basic_init(void) {
Yuchen Zeng95e4c482016-04-15 10:38:24 -070074 gpr_log_verbosity_init();
Craig Tiller35108f62015-02-17 11:24:15 -080075 gpr_mu_init(&g_init_mu);
Craig Tiller3113ef42016-03-29 09:03:14 -070076 grpc_register_built_in_plugins();
Craig Tiller35108f62015-02-17 11:24:15 -080077 g_initializations = 0;
78}
79
Craig Tiller178edfa2016-02-17 20:54:46 -080080static bool append_filter(grpc_channel_stack_builder *builder, void *arg) {
vjpaie7077b52016-03-21 20:58:44 -070081 return grpc_channel_stack_builder_append_filter(
82 builder, (const grpc_channel_filter *)arg, NULL, NULL);
Craig Tiller178edfa2016-02-17 20:54:46 -080083}
84
85static bool prepend_filter(grpc_channel_stack_builder *builder, void *arg) {
vjpaie7077b52016-03-21 20:58:44 -070086 return grpc_channel_stack_builder_prepend_filter(
87 builder, (const grpc_channel_filter *)arg, NULL, NULL);
Craig Tiller178edfa2016-02-17 20:54:46 -080088}
89
90static bool maybe_add_http_filter(grpc_channel_stack_builder *builder,
91 void *arg) {
92 grpc_transport *t = grpc_channel_stack_builder_get_transport(builder);
93 if (t && strstr(t->vtable->name, "http")) {
vjpaie7077b52016-03-21 20:58:44 -070094 return grpc_channel_stack_builder_prepend_filter(
95 builder, (const grpc_channel_filter *)arg, NULL, NULL);
Craig Tiller178edfa2016-02-17 20:54:46 -080096 }
97 return true;
98}
99
100static void register_builtin_channel_init() {
Craig Tillerf82ddc42016-04-05 17:15:07 -0700101 grpc_channel_init_register_stage(
102 GRPC_CLIENT_CHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY, prepend_filter,
103 (void *)&grpc_compress_filter);
104 grpc_channel_init_register_stage(
105 GRPC_CLIENT_DIRECT_CHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
106 prepend_filter, (void *)&grpc_compress_filter);
107 grpc_channel_init_register_stage(
108 GRPC_SERVER_CHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY, prepend_filter,
109 (void *)&grpc_compress_filter);
110 grpc_channel_init_register_stage(
111 GRPC_CLIENT_SUBCHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
112 maybe_add_http_filter, (void *)&grpc_http_client_filter);
113 grpc_channel_init_register_stage(GRPC_CLIENT_SUBCHANNEL,
114 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller178edfa2016-02-17 20:54:46 -0800115 grpc_add_connected_filter, NULL);
Craig Tillerf82ddc42016-04-05 17:15:07 -0700116 grpc_channel_init_register_stage(
117 GRPC_CLIENT_DIRECT_CHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
118 maybe_add_http_filter, (void *)&grpc_http_client_filter);
119 grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL,
120 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tillerde676262016-02-19 12:28:34 -0800121 grpc_add_connected_filter, NULL);
Craig Tillerf82ddc42016-04-05 17:15:07 -0700122 grpc_channel_init_register_stage(
123 GRPC_SERVER_CHANNEL, GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
124 maybe_add_http_filter, (void *)&grpc_http_server_filter);
125 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL,
126 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller178edfa2016-02-17 20:54:46 -0800127 grpc_add_connected_filter, NULL);
Craig Tillerf82ddc42016-04-05 17:15:07 -0700128 grpc_channel_init_register_stage(GRPC_CLIENT_LAME_CHANNEL,
129 GRPC_CHANNEL_INIT_BUILTIN_PRIORITY,
Craig Tiller178edfa2016-02-17 20:54:46 -0800130 append_filter, (void *)&grpc_lame_filter);
Craig Tiller81667322016-04-06 07:47:01 -0700131 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX, prepend_filter,
132 (void *)&grpc_server_top_filter);
Craig Tiller178edfa2016-02-17 20:54:46 -0800133}
134
Hongwei Wanga3780a82015-07-17 15:27:18 -0700135typedef struct grpc_plugin {
136 void (*init)();
Hongwei Wang85ad6852015-08-13 16:13:10 -0700137 void (*destroy)();
Hongwei Wanga3780a82015-07-17 15:27:18 -0700138} grpc_plugin;
139
Hongwei Wang85ad6852015-08-13 16:13:10 -0700140static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS];
141static int g_number_of_plugins = 0;
Hongwei Wanga3780a82015-07-17 15:27:18 -0700142
Hongwei Wang85ad6852015-08-13 16:13:10 -0700143void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
Jan Tattermusch1b34aeb2015-12-17 10:37:10 -0800144 GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2,
Craig Tiller178edfa2016-02-17 20:54:46 -0800145 ((void *)(intptr_t)init, (void *)(intptr_t)destroy));
Hongwei Wang85ad6852015-08-13 16:13:10 -0700146 GPR_ASSERT(g_number_of_plugins != MAX_PLUGINS);
147 g_all_of_the_plugins[g_number_of_plugins].init = init;
148 g_all_of_the_plugins[g_number_of_plugins].destroy = destroy;
149 g_number_of_plugins++;
Hongwei Wangff6097a2015-08-12 17:48:56 -0700150}
151
Craig Tiller32946d32015-01-15 11:37:30 -0800152void grpc_init(void) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700153 int i;
Craig Tiller8ace0bc2015-03-05 17:16:09 -0800154 gpr_once_init(&g_basic_init, do_basic_init);
Craig Tiller35108f62015-02-17 11:24:15 -0800155
156 gpr_mu_lock(&g_init_mu);
157 if (++g_initializations == 1) {
Craig Tillerf3756c12015-07-01 17:21:01 -0700158 gpr_time_init();
Craig Tiller0e72ede2015-11-19 07:48:53 -0800159 grpc_mdctx_global_init();
Craig Tiller178edfa2016-02-17 20:54:46 -0800160 grpc_channel_init_init();
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700161 grpc_register_tracer("api", &grpc_api_trace);
Craig Tillerfaa84802015-03-01 21:56:38 -0800162 grpc_register_tracer("channel", &grpc_trace_channel);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700163 grpc_register_tracer("connectivity_state", &grpc_connectivity_state_trace);
Craig Tillerd78d1642016-02-21 22:18:51 -0800164 grpc_register_tracer("channel_stack_builder",
165 &grpc_trace_channel_stack_builder);
Craig Tiller3ab2fe02016-04-11 20:11:18 -0700166 grpc_register_tracer("http1", &grpc_http1_trace);
Craig Tillerfaa84802015-03-01 21:56:38 -0800167 grpc_security_pre_init();
Craig Tiller35108f62015-02-17 11:24:15 -0800168 grpc_iomgr_init();
David Garcia Quintas4bc34632015-10-07 16:12:35 -0700169 grpc_executor_init();
Craig Tiller9aadeb82015-04-15 11:01:08 -0700170 grpc_tracer_init("GRPC_TRACE");
Craig Tiller0ba432d2015-10-09 16:57:11 -0700171 gpr_timers_global_init();
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800172 grpc_cq_global_init();
Hongwei Wang85ad6852015-08-13 16:13:10 -0700173 for (i = 0; i < g_number_of_plugins; i++) {
174 if (g_all_of_the_plugins[i].init != NULL) {
175 g_all_of_the_plugins[i].init();
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700176 }
Hongwei Wanga3780a82015-07-17 15:27:18 -0700177 }
Craig Tiller178edfa2016-02-17 20:54:46 -0800178 /* register channel finalization AFTER all plugins, to ensure that it's run
179 * at the appropriate time */
180 grpc_register_security_filters();
181 register_builtin_channel_init();
182 /* no more changes to channel init pipelines */
183 grpc_channel_init_finalize();
Craig Tiller35108f62015-02-17 11:24:15 -0800184 }
185 gpr_mu_unlock(&g_init_mu);
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700186 GRPC_API_TRACE("grpc_init(void)", 0, ());
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187}
188
Craig Tiller32946d32015-01-15 11:37:30 -0800189void grpc_shutdown(void) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700190 int i;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -0700191 GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
Craig Tiller35108f62015-02-17 11:24:15 -0800192 gpr_mu_lock(&g_init_mu);
193 if (--g_initializations == 0) {
David Garcia Quintas4bc34632015-10-07 16:12:35 -0700194 grpc_executor_shutdown();
Craig Tillerc7e1a2a2015-11-02 14:17:32 -0800195 grpc_cq_global_shutdown();
196 grpc_iomgr_shutdown();
Craig Tiller0ba432d2015-10-09 16:57:11 -0700197 gpr_timers_global_destroy();
Craig Tiller2f300e22015-06-04 08:28:43 -0700198 grpc_tracer_shutdown();
Craig Tillerf82ddc42016-04-05 17:15:07 -0700199 for (i = g_number_of_plugins; i >= 0; i--) {
Hongwei Wang85ad6852015-08-13 16:13:10 -0700200 if (g_all_of_the_plugins[i].destroy != NULL) {
201 g_all_of_the_plugins[i].destroy();
Hongwei Wang35d5a0f2015-07-24 11:14:23 -0700202 }
Hongwei Wanga3780a82015-07-17 15:27:18 -0700203 }
Craig Tiller0e72ede2015-11-19 07:48:53 -0800204 grpc_mdctx_global_shutdown();
Craig Tiller35108f62015-02-17 11:24:15 -0800205 }
206 gpr_mu_unlock(&g_init_mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800207}
Craig Tiller60fd3612015-03-05 16:24:22 -0800208
209int grpc_is_initialized(void) {
210 int r;
Craig Tiller8ace0bc2015-03-05 17:16:09 -0800211 gpr_once_init(&g_basic_init, do_basic_init);
Craig Tiller60fd3612015-03-05 16:24:22 -0800212 gpr_mu_lock(&g_init_mu);
213 r = g_initializations > 0;
214 gpr_mu_unlock(&g_init_mu);
215 return r;
216}