blob: 82c361c7ef69700aac7f155d7af31b5ba868eca7 [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,
Craig Tillercda759d2017-01-27 11:37:37 -080058 grpc_handshaker* handshaker, grpc_error* why) {
59 handshaker->vtable->shutdown(exec_ctx, handshaker, why);
Mark D. Rothc05539f2016-07-14 09:12:51 -070060}
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;
Masood Malekghassemib5b43722017-01-05 15:07:26 -080089 grpc_closure on_timeout;
Mark D. Roth45015dc2016-07-15 08:48:25 -070090 // The final callback and user_data to invoke after the last handshaker.
Mark D. Roth4b5cdb72016-11-14 11:37:36 -080091 grpc_closure on_handshake_done;
Mark D. Roth27588bb2016-11-11 15:03:39 -080092 void* user_data;
Mark D. Rothf9b56b92016-11-16 14:58:05 -080093 // Handshaker args.
94 grpc_handshaker_args args;
yang-g6da1e852017-02-16 10:34:06 -080095 // Links to the previous and next managers in a list of all pending handshakes
96 // Used at server side only.
97 grpc_handshake_manager* prev;
98 grpc_handshake_manager* next;
Mark D. Rothc05539f2016-07-14 09:12:51 -070099};
100
Mark D. Rothc05539f2016-07-14 09:12:51 -0700101grpc_handshake_manager* grpc_handshake_manager_create() {
102 grpc_handshake_manager* mgr = gpr_malloc(sizeof(grpc_handshake_manager));
103 memset(mgr, 0, sizeof(*mgr));
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800104 gpr_mu_init(&mgr->mu);
105 gpr_ref_init(&mgr->refs, 1);
Mark D. Rothc05539f2016-07-14 09:12:51 -0700106 return mgr;
107}
108
yang-g6da1e852017-02-16 10:34:06 -0800109void grpc_handshake_manager_pending_list_add(grpc_handshake_manager** head,
110 grpc_handshake_manager* mgr) {
111 GPR_ASSERT(mgr->prev == NULL);
112 GPR_ASSERT(mgr->next == NULL);
113 mgr->next = *head;
114 if (*head) {
115 (*head)->prev = mgr;
116 }
117 *head = mgr;
118}
119
120void grpc_handshake_manager_pending_list_remove(grpc_handshake_manager** head,
121 grpc_handshake_manager* mgr) {
122 if (mgr->next != NULL) {
123 mgr->next->prev = mgr->prev;
124 }
125 if (mgr->prev != NULL) {
126 mgr->prev->next = mgr->next;
127 } else {
128 GPR_ASSERT(*head == mgr);
129 *head = mgr->next;
130 }
131}
132
133void grpc_handshake_manager_pending_list_shutdown_all(
134 grpc_exec_ctx* exec_ctx, grpc_handshake_manager* head, grpc_error* why) {
135 while (head != NULL) {
136 grpc_handshake_manager_shutdown(exec_ctx, head, GRPC_ERROR_REF(why));
137 head = head->next;
138 }
139 GRPC_ERROR_UNREF(why);
140}
141
Mark D. Rothac8df652016-07-21 07:25:24 -0700142static bool is_power_of_2(size_t n) { return (n & (n - 1)) == 0; }
Mark D. Roth0b84add2016-07-20 10:08:48 -0700143
Mark D. Roth39377e92016-07-25 11:21:09 -0700144void grpc_handshake_manager_add(grpc_handshake_manager* mgr,
145 grpc_handshaker* handshaker) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800146 gpr_mu_lock(&mgr->mu);
Mark D. Roth0b84add2016-07-20 10:08:48 -0700147 // To avoid allocating memory for each handshaker we add, we double
148 // the number of elements every time we need more.
149 size_t realloc_count = 0;
150 if (mgr->count == 0) {
151 realloc_count = 2;
152 } else if (mgr->count >= 2 && is_power_of_2(mgr->count)) {
153 realloc_count = mgr->count * 2;
154 }
155 if (realloc_count > 0) {
Mark D. Rothac8df652016-07-21 07:25:24 -0700156 mgr->handshakers =
157 gpr_realloc(mgr->handshakers, realloc_count * sizeof(grpc_handshaker*));
Mark D. Roth0b84add2016-07-20 10:08:48 -0700158 }
Mark D. Rothc05539f2016-07-14 09:12:51 -0700159 mgr->handshakers[mgr->count++] = handshaker;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800160 gpr_mu_unlock(&mgr->mu);
161}
162
163static void grpc_handshake_manager_unref(grpc_exec_ctx* exec_ctx,
164 grpc_handshake_manager* mgr) {
165 if (gpr_unref(&mgr->refs)) {
166 for (size_t i = 0; i < mgr->count; ++i) {
167 grpc_handshaker_destroy(exec_ctx, mgr->handshakers[i]);
168 }
169 gpr_free(mgr->handshakers);
170 gpr_mu_destroy(&mgr->mu);
171 gpr_free(mgr);
172 }
Mark D. Rothc05539f2016-07-14 09:12:51 -0700173}
174
Mark D. Roth1d3b2cc2016-07-14 09:18:12 -0700175void grpc_handshake_manager_destroy(grpc_exec_ctx* exec_ctx,
176 grpc_handshake_manager* mgr) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800177 grpc_handshake_manager_unref(exec_ctx, mgr);
Mark D. Rothc05539f2016-07-14 09:12:51 -0700178}
179
Mark D. Roth1d3b2cc2016-07-14 09:18:12 -0700180void grpc_handshake_manager_shutdown(grpc_exec_ctx* exec_ctx,
Craig Tillercda759d2017-01-27 11:37:37 -0800181 grpc_handshake_manager* mgr,
182 grpc_error* why) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800183 gpr_mu_lock(&mgr->mu);
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800184 // Shutdown the handshaker that's currently in progress, if any.
Mark D. Rothf6a046e2016-12-02 13:15:42 -0800185 if (!mgr->shutdown && mgr->index > 0) {
186 mgr->shutdown = true;
Craig Tillercda759d2017-01-27 11:37:37 -0800187 grpc_handshaker_shutdown(exec_ctx, mgr->handshakers[mgr->index - 1],
188 GRPC_ERROR_REF(why));
Mark D. Rothc05539f2016-07-14 09:12:51 -0700189 }
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800190 gpr_mu_unlock(&mgr->mu);
Craig Tillercda759d2017-01-27 11:37:37 -0800191 GRPC_ERROR_UNREF(why);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800192}
193
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800194// Helper function to call either the next handshaker or the
195// on_handshake_done callback.
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800196// Returns true if we've scheduled the on_handshake_done callback.
197static bool call_next_handshaker_locked(grpc_exec_ctx* exec_ctx,
Mark D. Roth2ea37e92016-11-14 12:31:22 -0800198 grpc_handshake_manager* mgr,
Mark D. Roth2ea37e92016-11-14 12:31:22 -0800199 grpc_error* error) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800200 GPR_ASSERT(mgr->index <= mgr->count);
Mark D. Rothcc527cf2016-12-06 10:43:12 -0800201 // If we got an error or we've been shut down or we're exiting early or
202 // we've finished the last handshaker, invoke the on_handshake_done
203 // callback. Otherwise, call the next handshaker.
204 if (error != GRPC_ERROR_NONE || mgr->shutdown || mgr->args.exit_early ||
205 mgr->index == mgr->count) {
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800206 // Cancel deadline timer, since we're invoking the on_handshake_done
207 // callback now.
208 grpc_timer_cancel(exec_ctx, &mgr->deadline_timer);
Craig Tiller91031da2016-12-28 15:44:25 -0800209 grpc_closure_sched(exec_ctx, &mgr->on_handshake_done, error);
Mark D. Rothf6a046e2016-12-02 13:15:42 -0800210 mgr->shutdown = true;
211 } else {
212 grpc_handshaker_do_handshake(exec_ctx, mgr->handshakers[mgr->index],
213 mgr->acceptor, &mgr->call_next_handshaker,
214 &mgr->args);
Mark D. Rothc05539f2016-07-14 09:12:51 -0700215 }
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800216 ++mgr->index;
Mark D. Rothf6a046e2016-12-02 13:15:42 -0800217 return mgr->shutdown;
Mark D. Rothc05539f2016-07-14 09:12:51 -0700218}
219
220// A function used as the handshaker-done callback when chaining
221// handshakers together.
Mark D. Roth27588bb2016-11-11 15:03:39 -0800222static void call_next_handshaker(grpc_exec_ctx* exec_ctx, void* arg,
223 grpc_error* error) {
Mark D. Roth44756942016-11-29 12:43:55 -0800224 grpc_handshake_manager* mgr = arg;
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800225 gpr_mu_lock(&mgr->mu);
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800226 bool done = call_next_handshaker_locked(exec_ctx, mgr, GRPC_ERROR_REF(error));
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800227 gpr_mu_unlock(&mgr->mu);
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800228 // If we're invoked the final callback, we won't be coming back
229 // to this function, so we can release our reference to the
230 // handshake manager.
231 if (done) {
232 grpc_handshake_manager_unref(exec_ctx, mgr);
233 }
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800234}
235
236// Callback invoked when deadline is exceeded.
237static void on_timeout(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) {
238 grpc_handshake_manager* mgr = arg;
239 if (error == GRPC_ERROR_NONE) { // Timer fired, rather than being cancelled.
Craig Tillercda759d2017-01-27 11:37:37 -0800240 grpc_handshake_manager_shutdown(exec_ctx, mgr,
241 GRPC_ERROR_CREATE("Handshake timed out"));
Mark D. Rotha228e5f2016-07-22 09:02:15 -0700242 }
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800243 grpc_handshake_manager_unref(exec_ctx, mgr);
Mark D. Rothc05539f2016-07-14 09:12:51 -0700244}
245
Mark D. Rothb3ce1782016-07-20 09:25:25 -0700246void grpc_handshake_manager_do_handshake(
247 grpc_exec_ctx* exec_ctx, grpc_handshake_manager* mgr,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800248 grpc_endpoint* endpoint, const grpc_channel_args* channel_args,
Mark D. Rothac8df652016-07-21 07:25:24 -0700249 gpr_timespec deadline, grpc_tcp_server_acceptor* acceptor,
Mark D. Roth27588bb2016-11-11 15:03:39 -0800250 grpc_iomgr_cb_func on_handshake_done, void* user_data) {
Mark D. Roth30f698f2016-11-29 14:02:32 -0800251 gpr_mu_lock(&mgr->mu);
252 GPR_ASSERT(mgr->index == 0);
Mark D. Rothf6a046e2016-12-02 13:15:42 -0800253 GPR_ASSERT(!mgr->shutdown);
Mark D. Roth27588bb2016-11-11 15:03:39 -0800254 // Construct handshaker args. These will be passed through all
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800255 // handshakers and eventually be freed by the on_handshake_done callback.
Mark D. Rothf9b56b92016-11-16 14:58:05 -0800256 mgr->args.endpoint = endpoint;
257 mgr->args.args = grpc_channel_args_copy(channel_args);
Mark D. Roth44756942016-11-29 12:43:55 -0800258 mgr->args.user_data = user_data;
Mark D. Rothf9b56b92016-11-16 14:58:05 -0800259 mgr->args.read_buffer = gpr_malloc(sizeof(*mgr->args.read_buffer));
260 grpc_slice_buffer_init(mgr->args.read_buffer);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800261 // Initialize state needed for calling handshakers.
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800262 mgr->acceptor = acceptor;
Craig Tiller91031da2016-12-28 15:44:25 -0800263 grpc_closure_init(&mgr->call_next_handshaker, call_next_handshaker, mgr,
264 grpc_schedule_on_exec_ctx);
265 grpc_closure_init(&mgr->on_handshake_done, on_handshake_done, &mgr->args,
266 grpc_schedule_on_exec_ctx);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800267 // Start deadline timer, which owns a ref.
268 gpr_ref(&mgr->refs);
Masood Malekghassemib5b43722017-01-05 15:07:26 -0800269 grpc_closure_init(&mgr->on_timeout, on_timeout, mgr,
270 grpc_schedule_on_exec_ctx);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800271 grpc_timer_init(exec_ctx, &mgr->deadline_timer,
272 gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC),
Masood Malekghassemib5b43722017-01-05 15:07:26 -0800273 &mgr->on_timeout, gpr_now(GPR_CLOCK_MONOTONIC));
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800274 // Start first handshaker, which also owns a ref.
275 gpr_ref(&mgr->refs);
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800276 bool done = call_next_handshaker_locked(exec_ctx, mgr, GRPC_ERROR_NONE);
Mark D. Rothb16c1e32016-11-14 12:08:13 -0800277 gpr_mu_unlock(&mgr->mu);
Mark D. Rotha9bd9432016-11-30 09:38:55 -0800278 if (done) {
279 grpc_handshake_manager_unref(exec_ctx, mgr);
280 }
Mark D. Roth74e03a22016-07-13 10:12:17 -0700281}