| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 gRPC authors. |
| 4 |
* |
| 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 |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 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. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/transport/handshaker.h" |
| 22 |
|
| 23 |
#include <inttypes.h> |
| 24 |
|
| 25 |
#include <string> |
| 26 |
#include <utility> |
| 27 |
|
| 28 |
#include "absl/status/status.h" |
| 29 |
#include "absl/strings/str_format.h" |
| 30 |
|
| 31 |
#include <grpc/impl/codegen/grpc_types.h> |
| 32 |
#include <grpc/slice_buffer.h> |
| 33 |
#include <grpc/support/alloc.h> |
| 34 |
#include <grpc/support/log.h> |
| 35 |
|
| 36 |
#include "src/core/lib/channel/channel_args.h" |
| 37 |
#include "src/core/lib/debug/trace.h" |
| 38 |
#include "src/core/lib/gprpp/debug_location.h" |
| 39 |
#include "src/core/lib/gprpp/status_helper.h" |
| 40 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 41 |
#include "src/core/lib/iomgr/timer.h" |
| 42 |
|
| 43 |
namespace grpc_core { |
| 44 |
|
| 45 |
TraceFlag grpc_handshaker_trace(false, "handshaker"); |
| 46 |
|
| 47 |
namespace { |
| 48 |
|
| 49 |
std::string HandshakerArgsString(HandshakerArgs* args) { |
| 50 |
size_t read_buffer_length = |
| 51 |
args->read_buffer != nullptr ? args->read_buffer->length : 0; |
| 52 |
return absl::StrFormat( |
| 53 |
"{endpoint=%p, args=%s, read_buffer=%p (length=%" PRIuPTR |
| 54 |
"), exit_early=%d}", |
| 55 |
args->endpoint, args->args.ToString(), args->read_buffer, |
| 56 |
read_buffer_length, args->exit_early); |
| 57 |
} |
| 58 |
|
| 59 |
} // namespace |
| 60 |
|
| 61 |
HandshakeManager::HandshakeManager() {} |
| 62 |
|
| 63 |
void HandshakeManager::Add(RefCountedPtr<Handshaker> handshaker) { |
| 64 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_handshaker_trace)) { |
| 65 |
gpr_log( |
| 66 |
GPR_INFO, |
| 67 |
"handshake_manager %p: adding handshaker %s [%p] at index %" PRIuPTR, |
| 68 |
this, handshaker->name(), handshaker.get(), handshakers_.size()); |
| 69 |
} |
| 70 |
MutexLock lock(&mu_); |
| 71 |
handshakers_.push_back(std::move(handshaker)); |
| 72 |
} |
| 73 |
|
| 74 |
HandshakeManager::~HandshakeManager() { handshakers_.clear(); } |
| 75 |
|
| 76 |
void HandshakeManager::Shutdown(grpc_error_handle why) { |
| 77 |
{ |
| 78 |
MutexLock lock(&mu_); |
| 79 |
// Shutdown the handshaker that's currently in progress, if any. |
| 80 |
if (!is_shutdown_ && index_ > 0) { |
| 81 |
is_shutdown_ = true; |
| 82 |
handshakers_[index_ - 1]->Shutdown(why); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Helper function to call either the next handshaker or the |
| 88 |
// on_handshake_done callback. |
| 89 |
// Returns true if we've scheduled the on_handshake_done callback. |
| 90 |
bool HandshakeManager::CallNextHandshakerLocked(grpc_error_handle error) { |
| 91 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_handshaker_trace)) { |
| 92 |
gpr_log(GPR_INFO, |
| 93 |
"handshake_manager %p: error=%s shutdown=%d index=%" PRIuPTR |
| 94 |
", args=%s", |
| 95 |
this, StatusToString(error).c_str(), is_shutdown_, index_, |
| 96 |
HandshakerArgsString(&args_).c_str()); |
| 97 |
} |
| 98 |
GPR_ASSERT(index_ <= handshakers_.size()); |
| 99 |
// If we got an error or we've been shut down or we're exiting early or |
| 100 |
// we've finished the last handshaker, invoke the on_handshake_done |
| 101 |
// callback. Otherwise, call the next handshaker. |
| 102 |
if (!error.ok() || is_shutdown_ || args_.exit_early || |
| 103 |
index_ == handshakers_.size()) { |
| 104 |
if (error.ok() && is_shutdown_) { |
| 105 |
error = GRPC_ERROR_CREATE("handshaker shutdown"); |
| 106 |
// It is possible that the endpoint has already been destroyed by |
| 107 |
// a shutdown call while this callback was sitting on the ExecCtx |
| 108 |
// with no error. |
| 109 |
if (args_.endpoint != nullptr) { |
| 110 |
// TODO(roth): It is currently necessary to shutdown endpoints |
| 111 |
// before destroying then, even when we know that there are no |
| 112 |
// pending read/write callbacks. This should be fixed, at which |
| 113 |
// point this can be removed. |
| 114 |
grpc_endpoint_shutdown(args_.endpoint, error); |
| 115 |
grpc_endpoint_destroy(args_.endpoint); |
| 116 |
args_.endpoint = nullptr; |
| 117 |
args_.args = ChannelArgs(); |
| 118 |
grpc_slice_buffer_destroy(args_.read_buffer); |
| 119 |
gpr_free(args_.read_buffer); |
| 120 |
args_.read_buffer = nullptr; |
| 121 |
} |
| 122 |
} |
| 123 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_handshaker_trace)) { |
| 124 |
gpr_log(GPR_INFO, |
| 125 |
"handshake_manager %p: handshaking complete -- scheduling " |
| 126 |
"on_handshake_done with error=%s", |
| 127 |
this, StatusToString(error).c_str()); |
| 128 |
} |
| 129 |
// Cancel deadline timer, since we're invoking the on_handshake_done |
| 130 |
// callback now. |
| 131 |
grpc_timer_cancel(&deadline_timer_); |
| 132 |
ExecCtx::Run(DEBUG_LOCATION, &on_handshake_done_, error); |
| 133 |
is_shutdown_ = true; |
| 134 |
} else { |
| 135 |
auto handshaker = handshakers_[index_]; |
| 136 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_handshaker_trace)) { |
| 137 |
gpr_log( |
| 138 |
GPR_INFO, |
| 139 |
"handshake_manager %p: calling handshaker %s [%p] at index %" PRIuPTR, |
| 140 |
this, handshaker->name(), handshaker.get(), index_); |
| 141 |
} |
| 142 |
handshaker->DoHandshake(acceptor_, &call_next_handshaker_, &args_); |
| 143 |
} |
| 144 |
++index_; |
| 145 |
return is_shutdown_; |
| 146 |
} |
| 147 |
|
| 148 |
void HandshakeManager::CallNextHandshakerFn(void* arg, |
| 149 |
grpc_error_handle error) { |
| 150 |
auto* mgr = static_cast<HandshakeManager*>(arg); |
| 151 |
bool done; |
| 152 |
{ |
| 153 |
MutexLock lock(&mgr->mu_); |
| 154 |
done = mgr->CallNextHandshakerLocked(error); |
| 155 |
} |
| 156 |
// If we're invoked the final callback, we won't be coming back |
| 157 |
// to this function, so we can release our reference to the |
| 158 |
// handshake manager. |
| 159 |
if (done) { |
| 160 |
mgr->Unref(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
void HandshakeManager::OnTimeoutFn(void* arg, grpc_error_handle error) { |
| 165 |
auto* mgr = static_cast<HandshakeManager*>(arg); |
| 166 |
if (error.ok()) { // Timer fired, rather than being cancelled |
| 167 |
mgr->Shutdown(GRPC_ERROR_CREATE("Handshake timed out")); |
| 168 |
} |
| 169 |
mgr->Unref(); |
| 170 |
} |
| 171 |
|
| 172 |
void HandshakeManager::DoHandshake(grpc_endpoint* endpoint, |
| 173 |
const ChannelArgs& channel_args, |
| 174 |
Timestamp deadline, |
| 175 |
grpc_tcp_server_acceptor* acceptor, |
| 176 |
grpc_iomgr_cb_func on_handshake_done, |
| 177 |
void* user_data) { |
| 178 |
bool done; |
| 179 |
{ |
| 180 |
MutexLock lock(&mu_); |
| 181 |
GPR_ASSERT(index_ == 0); |
| 182 |
// Construct handshaker args. These will be passed through all |
| 183 |
// handshakers and eventually be freed by the on_handshake_done callback. |
| 184 |
args_.endpoint = endpoint; |
| 185 |
args_.deadline = deadline; |
| 186 |
args_.args = channel_args; |
| 187 |
args_.user_data = user_data; |
| 188 |
args_.read_buffer = |
| 189 |
static_cast<grpc_slice_buffer*>(gpr_malloc(sizeof(*args_.read_buffer))); |
| 190 |
grpc_slice_buffer_init(args_.read_buffer); |
| 191 |
if (acceptor != nullptr && acceptor->external_connection && |
| 192 |
acceptor->pending_data != nullptr) { |
| 193 |
grpc_slice_buffer_swap(args_.read_buffer, |
| 194 |
&(acceptor->pending_data->data.raw.slice_buffer)); |
| 195 |
} |
| 196 |
// Initialize state needed for calling handshakers. |
| 197 |
acceptor_ = acceptor; |
| 198 |
GRPC_CLOSURE_INIT(&call_next_handshaker_, |
| 199 |
&HandshakeManager::CallNextHandshakerFn, this, |
| 200 |
grpc_schedule_on_exec_ctx); |
| 201 |
GRPC_CLOSURE_INIT(&on_handshake_done_, on_handshake_done, &args_, |
| 202 |
grpc_schedule_on_exec_ctx); |
| 203 |
// Start deadline timer, which owns a ref. |
| 204 |
Ref().release(); |
| 205 |
GRPC_CLOSURE_INIT(&on_timeout_, &HandshakeManager::OnTimeoutFn, this, |
| 206 |
grpc_schedule_on_exec_ctx); |
| 207 |
grpc_timer_init(&deadline_timer_, deadline, &on_timeout_); |
| 208 |
// Start first handshaker, which also owns a ref. |
| 209 |
Ref().release(); |
| 210 |
done = CallNextHandshakerLocked(absl::OkStatus()); |
| 211 |
} |
| 212 |
if (done) { |
| 213 |
Unref(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
} // namespace grpc_core |
| 218 |
|
| 219 |
void grpc_handshake_manager_add(grpc_handshake_manager* mgr, |
| 220 |
grpc_handshaker* handshaker) { |
| 221 |
// This is a transition method to aid the API change for handshakers. |
| 222 |
grpc_core::RefCountedPtr<grpc_core::Handshaker> refd_hs( |
| 223 |
static_cast<grpc_core::Handshaker*>(handshaker)); |
| 224 |
mgr->Add(refd_hs); |
| 225 |
} |