blob: ff827527b3ec4137085e2aff670ce7d655bc6420 [file] [log] [blame]
Mark D. Roth74e03a22016-07-13 10:12:17 -07001/*
2 *
3 * Copyright 2016, 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
Mark D. Roth7c2a58c2016-07-14 11:53:47 -070034#include <string.h>
35
David Garcia Quintas6b114622016-07-27 14:49:53 -070036#include <grpc/support/alloc.h>
David Garcia Quintasc79b0652016-07-27 21:11:58 -070037#include <grpc/support/log.h>
Mark D. Roth7c2a58c2016-07-14 11:53:47 -070038
Mark D. Roth45015dc2016-07-15 08:48:25 -070039#include "src/core/lib/channel/channel_args.h"
Mark D. Roth74e03a22016-07-13 10:12:17 -070040#include "src/core/lib/channel/handshaker.h"
Mark D. Rothb16c1e32016-11-14 12:08:13 -080041#include "src/core/lib/iomgr/timer.h"
Mark D. Roth74e03a22016-07-13 10:12:17 -070042
Mark D. Rothc05539f2016-07-14 09:12:51 -070043//
44// grpc_handshaker
45//
46
Mark D. Roth27588bb2016-11-11 15:03:39 -080047void grpc_handshaker_init(const grpc_handshaker_vtable* vtable,
Mark D. Roth7c2a58c2016-07-14 11:53:47 -070048 grpc_handshaker* handshaker) {
Mark D. Roth74e03a22016-07-13 10:12:17 -070049 handshaker->vtable = vtable;
50}
51
Mark D. Roth887e2182016-12-07 08:19:34 -080052void grpc_handshaker_destroy(grpc_exec_ctx* exec_ctx,
53 grpc_handshaker* handshaker) {
Mark D. Roth74e03a22016-07-13 10:12:17 -070054 handshaker->vtable->destroy(exec_ctx, handshaker);
55}
56
Mark D. Roth887e2182016-12-07 08:19:34 -080057void grpc_handshaker_shutdown(grpc_exec_ctx* exec_ctx,
58 grpc_handshaker* handshaker) {
Mark D. Rothc05539f2016-07-14 09:12:51 -070059 handshaker->vtable->shutdown(exec_ctx, handshaker);
60}
61
Mark D. Roth887e2182016-12-07 08:19:34 -080062void grpc_handshaker_do_handshake(grpc_exec_ctx* exec_ctx,
63 grpc_handshaker* handshaker,
64 grpc_tcp_server_acceptor* acceptor,
65 grpc_closure* on_handshake_done,
66 grpc_handshaker_args* args) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -080067 handshaker->vtable->do_handshake(exec_ctx, handshaker, acceptor,
Mark D. Roth27588bb2016-11-11 15:03:39 -080068 on_handshake_done, args);
Mark D. Rothc05539f2016-07-14 09:12:51 -070069}
70
71//
72// grpc_handshake_manager
73//
74
Mark D. Rothb16c1e32016-11-14 12:08:13 -080075struct grpc_handshake_manager {
76 gpr_mu mu;
77 gpr_refcount refs;
Mark D. Rothf6a046e2016-12-02 13:15:42 -080078 bool shutdown;
Mark D. Rothb16c1e32016-11-14 12:08:13 -080079 // An array of handshakers added via grpc_handshake_manager_add().
80 size_t count;
81 grpc_handshaker** handshakers;
82 // The index of the handshaker to invoke next and closure to invoke it.
Mark D. Rothc05539f2016-07-14 09:12:51 -070083 size_t index;
Mark D. Roth4b5cdb72016-11-14 11:37:36 -080084 grpc_closure call_next_handshaker;
Mark D. Roth5682a522016-07-20 09:54:41 -070085 // The acceptor to call the handshakers with.
Mark D. Rothac8df652016-07-21 07:25:24 -070086 grpc_tcp_server_acceptor* acceptor;
Mark D. Rothb16c1e32016-11-14 12:08:13 -080087 // Deadline timer across all handshakers.
88 grpc_timer deadline_timer;
Mark D. Roth45015dc2016-07-15 08:48:25 -070089 // The final callback and user_data to invoke after the last handshaker.
Mark D. Roth4b5cdb72016-11-14 11:37:36 -080090 grpc_closure on_handshake_done;
Mark D. Roth27588bb2016-11-11 15:03:39 -080091 void* user_data;
Mark D. Rothf9b56b92016-11-16 14:58:05 -080092 // Handshaker args.
93 grpc_handshaker_args args;
Mark D. Rothc05539f2016-07-14 09:12:51 -070094};
95
Mark D. Rothc05539f2016-07-14 09:12:51 -070096grpc_handshake_manager* grpc_handshake_manager_create() {
97 grpc_handshake_manager* mgr = gpr_malloc(sizeof(grpc_handshake_manager));
98 memset(mgr, 0, sizeof(*mgr));
Mark D. Rothb16c1e32016-11-14 12:08:13 -080099 gpr_mu_init(&mgr->mu);
100 gpr_ref_init(&mgr->refs, 1);
Mark D. Rothc05539f2016-07-14 09:12:51 -0700101 return mgr;
102}
103
Mark D. Rothac8df652016-07-21 07:25:24 -0700104static bool is_power_of_2(size_t n) { return (n & (n - 1)) == 0; }
Mark D. Roth0b84add2016-07-20 10:08:48 -0700105
Mark D. Roth39377e92016-07-25 11:21:09 -0700106void grpc_handshake_manager_add(grpc_handshake_manager* mgr,
107 grpc_handshaker* handshaker) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800108 gpr_mu_lock(&mgr->mu);
Mark D. Roth0b84add2016-07-20 10:08:48 -0700109 // To avoid allocating memory for each handshaker we add, we double
110 // the number of elements every time we need more.
111 size_t realloc_count = 0;
112 if (mgr->count == 0) {
113 realloc_count = 2;
114 } else if (mgr->count >= 2 && is_power_of_2(mgr->count)) {
115 realloc_count = mgr->count * 2;
116 }
117 if (realloc_count > 0) {
Mark D. Rothac8df652016-07-21 07:25:24 -0700118 mgr->handshakers =
119 gpr_realloc(mgr->handshakers, realloc_count * sizeof(grpc_handshaker*));
Mark D. Roth0b84add2016-07-20 10:08:48 -0700120 }
Mark D. Rothc05539f2016-07-14 09:12:51 -0700121 mgr->handshakers[mgr->count++] = handshaker;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800122 gpr_mu_unlock(&mgr->mu);
123}
124
125static void grpc_handshake_manager_unref(grpc_exec_ctx* exec_ctx,
126 grpc_handshake_manager* mgr) {
127 if (gpr_unref(&mgr->refs)) {
128 for (size_t i = 0; i < mgr->count; ++i) {
129 grpc_handshaker_destroy(exec_ctx, mgr->handshakers[i]);
130 }
131 gpr_free(mgr->handshakers);
132 gpr_mu_destroy(&mgr->mu);
133 gpr_free(mgr);
134 }
Mark D. Rothc05539f2016-07-14 09:12:51 -0700135}
136
Mark D. Roth1d3b2cc2016-07-14 09:18:12 -0700137void grpc_handshake_manager_destroy(grpc_exec_ctx* exec_ctx,
138 grpc_handshake_manager* mgr) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800139 grpc_handshake_manager_unref(exec_ctx, mgr);
Mark D. Rothc05539f2016-07-14 09:12:51 -0700140}
141
Mark D. Roth1d3b2cc2016-07-14 09:18:12 -0700142void grpc_handshake_manager_shutdown(grpc_exec_ctx* exec_ctx,
143 grpc_handshake_manager* mgr) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800144 gpr_mu_lock(&mgr->mu);
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800145 // Shutdown the handshaker that's currently in progress, if any.
Mark D. Rothf6a046e2016-12-02 13:15:42 -0800146 if (!mgr->shutdown && mgr->index > 0) {
147 mgr->shutdown = true;
Mark D. Roth30f698f2016-11-29 14:02:32 -0800148 grpc_handshaker_shutdown(exec_ctx, mgr->handshakers[mgr->index - 1]);
Mark D. Rothc05539f2016-07-14 09:12:51 -0700149 }
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800150 gpr_mu_unlock(&mgr->mu);
151}
152
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800153// Helper function to call either the next handshaker or the
154// on_handshake_done callback.
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800155// Returns true if we've scheduled the on_handshake_done callback.
156static bool call_next_handshaker_locked(grpc_exec_ctx* exec_ctx,
Mark D. Roth2ea37e92016-11-14 12:31:22 -0800157 grpc_handshake_manager* mgr,
Mark D. Roth2ea37e92016-11-14 12:31:22 -0800158 grpc_error* error) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800159 GPR_ASSERT(mgr->index <= mgr->count);
Mark D. Rothcc527cf2016-12-06 10:43:12 -0800160 // If we got an error or we've been shut down or we're exiting early or
161 // we've finished the last handshaker, invoke the on_handshake_done
162 // callback. Otherwise, call the next handshaker.
163 if (error != GRPC_ERROR_NONE || mgr->shutdown || mgr->args.exit_early ||
164 mgr->index == mgr->count) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800165 // Cancel deadline timer, since we're invoking the on_handshake_done
166 // callback now.
167 grpc_timer_cancel(exec_ctx, &mgr->deadline_timer);
Craig Tiller91031da2016-12-28 15:44:25 -0800168 grpc_closure_sched(exec_ctx, &mgr->on_handshake_done, error);
Mark D. Rothf6a046e2016-12-02 13:15:42 -0800169 mgr->shutdown = true;
170 } else {
171 grpc_handshaker_do_handshake(exec_ctx, mgr->handshakers[mgr->index],
172 mgr->acceptor, &mgr->call_next_handshaker,
173 &mgr->args);
Mark D. Rothc05539f2016-07-14 09:12:51 -0700174 }
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800175 ++mgr->index;
Mark D. Rothf6a046e2016-12-02 13:15:42 -0800176 return mgr->shutdown;
Mark D. Rothc05539f2016-07-14 09:12:51 -0700177}
178
179// A function used as the handshaker-done callback when chaining
180// handshakers together.
Mark D. Roth27588bb2016-11-11 15:03:39 -0800181static void call_next_handshaker(grpc_exec_ctx* exec_ctx, void* arg,
182 grpc_error* error) {
Mark D. Roth44756942016-11-29 12:43:55 -0800183 grpc_handshake_manager* mgr = arg;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800184 gpr_mu_lock(&mgr->mu);
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800185 bool done = call_next_handshaker_locked(exec_ctx, mgr, GRPC_ERROR_REF(error));
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800186 gpr_mu_unlock(&mgr->mu);
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800187 // If we're invoked the final callback, we won't be coming back
188 // to this function, so we can release our reference to the
189 // handshake manager.
190 if (done) {
191 grpc_handshake_manager_unref(exec_ctx, mgr);
192 }
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800193}
194
195// Callback invoked when deadline is exceeded.
196static void on_timeout(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) {
197 grpc_handshake_manager* mgr = arg;
198 if (error == GRPC_ERROR_NONE) { // Timer fired, rather than being cancelled.
199 grpc_handshake_manager_shutdown(exec_ctx, mgr);
Mark D. Rotha228e5f2016-07-22 09:02:15 -0700200 }
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800201 grpc_handshake_manager_unref(exec_ctx, mgr);
Mark D. Rothc05539f2016-07-14 09:12:51 -0700202}
203
Mark D. Rothb3ce1782016-07-20 09:25:25 -0700204void grpc_handshake_manager_do_handshake(
205 grpc_exec_ctx* exec_ctx, grpc_handshake_manager* mgr,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800206 grpc_endpoint* endpoint, const grpc_channel_args* channel_args,
Mark D. Rothac8df652016-07-21 07:25:24 -0700207 gpr_timespec deadline, grpc_tcp_server_acceptor* acceptor,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800208 grpc_iomgr_cb_func on_handshake_done, void* user_data) {
Mark D. Roth30f698f2016-11-29 14:02:32 -0800209 gpr_mu_lock(&mgr->mu);
210 GPR_ASSERT(mgr->index == 0);
Mark D. Rothf6a046e2016-12-02 13:15:42 -0800211 GPR_ASSERT(!mgr->shutdown);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800212 // Construct handshaker args. These will be passed through all
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800213 // handshakers and eventually be freed by the on_handshake_done callback.
Mark D. Rothf9b56b92016-11-16 14:58:05 -0800214 mgr->args.endpoint = endpoint;
215 mgr->args.args = grpc_channel_args_copy(channel_args);
Mark D. Roth44756942016-11-29 12:43:55 -0800216 mgr->args.user_data = user_data;
Mark D. Rothf9b56b92016-11-16 14:58:05 -0800217 mgr->args.read_buffer = gpr_malloc(sizeof(*mgr->args.read_buffer));
218 grpc_slice_buffer_init(mgr->args.read_buffer);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800219 // Initialize state needed for calling handshakers.
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800220 mgr->acceptor = acceptor;
Craig Tiller91031da2016-12-28 15:44:25 -0800221 grpc_closure_init(&mgr->call_next_handshaker, call_next_handshaker, mgr,
222 grpc_schedule_on_exec_ctx);
223 grpc_closure_init(&mgr->on_handshake_done, on_handshake_done, &mgr->args,
224 grpc_schedule_on_exec_ctx);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800225 // Start deadline timer, which owns a ref.
226 gpr_ref(&mgr->refs);
227 grpc_timer_init(exec_ctx, &mgr->deadline_timer,
228 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
229 on_timeout, mgr, gpr_now(GPR_CLOCK_MONOTONIC));
230 // Start first handshaker, which also owns a ref.
231 gpr_ref(&mgr->refs);
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800232 bool done = call_next_handshaker_locked(exec_ctx, mgr, GRPC_ERROR_NONE);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800233 gpr_mu_unlock(&mgr->mu);
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800234 if (done) {
235 grpc_handshake_manager_unref(exec_ctx, mgr);
236 }
Mark D. Roth74e03a22016-07-13 10:12:17 -0700237}