3 * Copyright 2016 gRPC authors.
5 * 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
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * 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.
19 #include <grpc/support/port_platform.h>
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/string_util.h>
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/channel/handshaker.h"
29 #include "src/core/lib/debug/trace.h"
30 #include "src/core/lib/iomgr/timer.h"
31 #include "src/core/lib/slice/slice_internal.h"
35 TraceFlag grpc_handshaker_trace(false, "handshaker");
39 char* HandshakerArgsString(HandshakerArgs* args) {
40 char* args_str = grpc_channel_args_string(args->args);
41 size_t num_args = args->args != nullptr ? args->args->num_args : 0;
42 size_t read_buffer_length =
43 args->read_buffer != nullptr ? args->read_buffer->length : 0;
46 "{endpoint=%p, args=%p {size=%" PRIuPTR
47 ": %s}, read_buffer=%p (length=%" PRIuPTR "), exit_early=%d}",
48 args->endpoint, args->args, num_args, args_str,
49 args->read_buffer, read_buffer_length, args->exit_early);
56 HandshakeManager::HandshakeManager() { gpr_mu_init(&mu_); }
58 /// Add \a mgr to the server side list of all pending handshake managers, the
59 /// list starts with \a *head.
60 // Not thread-safe. Caller needs to synchronize.
61 void HandshakeManager::AddToPendingMgrList(HandshakeManager** head) {
62 GPR_ASSERT(prev_ == nullptr);
63 GPR_ASSERT(next_ == nullptr);
66 (*head)->prev_ = this;
71 /// Remove \a mgr from the server side list of all pending handshake managers.
72 // Not thread-safe. Caller needs to synchronize.
73 void HandshakeManager::RemoveFromPendingMgrList(HandshakeManager** head) {
74 if (next_ != nullptr) {
77 if (prev_ != nullptr) {
80 GPR_ASSERT(*head == this);
85 /// Shutdown all pending handshake managers starting at head on the server
86 /// side. Not thread-safe. Caller needs to synchronize.
87 void HandshakeManager::ShutdownAllPending(grpc_error* why) {
89 while (head != nullptr) {
90 head->Shutdown(GRPC_ERROR_REF(why));
93 GRPC_ERROR_UNREF(why);
96 void HandshakeManager::Add(RefCountedPtr<Handshaker> handshaker) {
97 if (GRPC_TRACE_FLAG_ENABLED(grpc_handshaker_trace)) {
100 "handshake_manager %p: adding handshaker %s [%p] at index %" PRIuPTR,
101 this, handshaker->name(), handshaker.get(), handshakers_.size());
103 MutexLock lock(&mu_);
104 handshakers_.push_back(std::move(handshaker));
107 HandshakeManager::~HandshakeManager() {
108 handshakers_.clear();
109 gpr_mu_destroy(&mu_);
112 void HandshakeManager::Shutdown(grpc_error* why) {
114 MutexLock lock(&mu_);
115 // Shutdown the handshaker that's currently in progress, if any.
116 if (!is_shutdown_ && index_ > 0) {
118 handshakers_[index_ - 1]->Shutdown(GRPC_ERROR_REF(why));
121 GRPC_ERROR_UNREF(why);
124 // Helper function to call either the next handshaker or the
125 // on_handshake_done callback.
126 // Returns true if we've scheduled the on_handshake_done callback.
127 bool HandshakeManager::CallNextHandshakerLocked(grpc_error* error) {
128 if (GRPC_TRACE_FLAG_ENABLED(grpc_handshaker_trace)) {
129 char* args_str = HandshakerArgsString(&args_);
131 "handshake_manager %p: error=%s shutdown=%d index=%" PRIuPTR
133 this, grpc_error_string(error), is_shutdown_, index_, args_str);
136 GPR_ASSERT(index_ <= handshakers_.size());
137 // If we got an error or we've been shut down or we're exiting early or
138 // we've finished the last handshaker, invoke the on_handshake_done
139 // callback. Otherwise, call the next handshaker.
140 if (error != GRPC_ERROR_NONE || is_shutdown_ || args_.exit_early ||
141 index_ == handshakers_.size()) {
142 if (error == GRPC_ERROR_NONE && is_shutdown_) {
143 error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("handshaker shutdown");
144 // It is possible that the endpoint has already been destroyed by
145 // a shutdown call while this callback was sitting on the ExecCtx
147 if (args_.endpoint != nullptr) {
148 // TODO(roth): It is currently necessary to shutdown endpoints
149 // before destroying then, even when we know that there are no
150 // pending read/write callbacks. This should be fixed, at which
151 // point this can be removed.
152 grpc_endpoint_shutdown(args_.endpoint, GRPC_ERROR_REF(error));
153 grpc_endpoint_destroy(args_.endpoint);
154 args_.endpoint = nullptr;
155 grpc_channel_args_destroy(args_.args);
156 args_.args = nullptr;
157 grpc_slice_buffer_destroy_internal(args_.read_buffer);
158 gpr_free(args_.read_buffer);
159 args_.read_buffer = nullptr;
162 if (GRPC_TRACE_FLAG_ENABLED(grpc_handshaker_trace)) {
164 "handshake_manager %p: handshaking complete -- scheduling "
165 "on_handshake_done with error=%s",
166 this, grpc_error_string(error));
168 // Cancel deadline timer, since we're invoking the on_handshake_done
170 grpc_timer_cancel(&deadline_timer_);
171 GRPC_CLOSURE_SCHED(&on_handshake_done_, error);
174 auto handshaker = handshakers_[index_];
175 if (GRPC_TRACE_FLAG_ENABLED(grpc_handshaker_trace)) {
178 "handshake_manager %p: calling handshaker %s [%p] at index %" PRIuPTR,
179 this, handshaker->name(), handshaker.get(), index_);
181 handshaker->DoHandshake(acceptor_, &call_next_handshaker_, &args_);
187 void HandshakeManager::CallNextHandshakerFn(void* arg, grpc_error* error) {
188 auto* mgr = static_cast<HandshakeManager*>(arg);
191 MutexLock lock(&mgr->mu_);
192 done = mgr->CallNextHandshakerLocked(GRPC_ERROR_REF(error));
194 // If we're invoked the final callback, we won't be coming back
195 // to this function, so we can release our reference to the
196 // handshake manager.
202 void HandshakeManager::OnTimeoutFn(void* arg, grpc_error* error) {
203 auto* mgr = static_cast<HandshakeManager*>(arg);
204 if (error == GRPC_ERROR_NONE) { // Timer fired, rather than being cancelled
205 mgr->Shutdown(GRPC_ERROR_CREATE_FROM_STATIC_STRING("Handshake timed out"));
210 void HandshakeManager::DoHandshake(grpc_endpoint* endpoint,
211 const grpc_channel_args* channel_args,
212 grpc_millis deadline,
213 grpc_tcp_server_acceptor* acceptor,
214 grpc_iomgr_cb_func on_handshake_done,
218 MutexLock lock(&mu_);
219 GPR_ASSERT(index_ == 0);
220 GPR_ASSERT(!is_shutdown_);
221 // Construct handshaker args. These will be passed through all
222 // handshakers and eventually be freed by the on_handshake_done callback.
223 args_.endpoint = endpoint;
224 args_.args = grpc_channel_args_copy(channel_args);
225 args_.user_data = user_data;
227 static_cast<grpc_slice_buffer*>(gpr_malloc(sizeof(*args_.read_buffer)));
228 grpc_slice_buffer_init(args_.read_buffer);
229 // Initialize state needed for calling handshakers.
230 acceptor_ = acceptor;
231 GRPC_CLOSURE_INIT(&call_next_handshaker_,
232 &HandshakeManager::CallNextHandshakerFn, this,
233 grpc_schedule_on_exec_ctx);
234 GRPC_CLOSURE_INIT(&on_handshake_done_, on_handshake_done, &args_,
235 grpc_schedule_on_exec_ctx);
236 // Start deadline timer, which owns a ref.
238 GRPC_CLOSURE_INIT(&on_timeout_, &HandshakeManager::OnTimeoutFn, this,
239 grpc_schedule_on_exec_ctx);
240 grpc_timer_init(&deadline_timer_, deadline, &on_timeout_);
241 // Start first handshaker, which also owns a ref.
243 done = CallNextHandshakerLocked(GRPC_ERROR_NONE);
250 } // namespace grpc_core
252 void grpc_handshake_manager_add(grpc_handshake_manager* mgr,
253 grpc_handshaker* handshaker) {
254 // This is a transition method to aid the API change for handshakers.
255 using namespace grpc_core;
256 RefCountedPtr<Handshaker> refd_hs(static_cast<Handshaker*>(handshaker));