| 1 |
// |
| 2 |
// Copyright 2022 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include "src/core/ext/xds/xds_transport_grpc.h" |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <functional> |
| 24 |
#include <memory> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
#include "absl/strings/str_cat.h" |
| 28 |
|
| 29 |
#include <grpc/byte_buffer.h> |
| 30 |
#include <grpc/byte_buffer_reader.h> |
| 31 |
#include <grpc/grpc.h> |
| 32 |
#include <grpc/impl/codegen/connectivity_state.h> |
| 33 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 34 |
#include <grpc/slice.h> |
| 35 |
#include <grpc/support/log.h> |
| 36 |
|
| 37 |
#include "src/core/ext/filters/client_channel/client_channel.h" |
| 38 |
#include "src/core/ext/xds/xds_bootstrap.h" |
| 39 |
#include "src/core/ext/xds/xds_bootstrap_grpc.h" |
| 40 |
#include "src/core/lib/channel/channel_args.h" |
| 41 |
#include "src/core/lib/channel/channel_fwd.h" |
| 42 |
#include "src/core/lib/channel/channel_stack.h" |
| 43 |
#include "src/core/lib/config/core_configuration.h" |
| 44 |
#include "src/core/lib/gprpp/debug_location.h" |
| 45 |
#include "src/core/lib/gprpp/orphanable.h" |
| 46 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 47 |
#include "src/core/lib/gprpp/time.h" |
| 48 |
#include "src/core/lib/iomgr/closure.h" |
| 49 |
#include "src/core/lib/iomgr/pollset_set.h" |
| 50 |
#include "src/core/lib/security/credentials/channel_creds_registry.h" |
| 51 |
#include "src/core/lib/security/credentials/credentials.h" |
| 52 |
#include "src/core/lib/slice/slice.h" |
| 53 |
#include "src/core/lib/slice/slice_internal.h" |
| 54 |
#include "src/core/lib/surface/call.h" |
| 55 |
#include "src/core/lib/surface/channel.h" |
| 56 |
#include "src/core/lib/surface/init_internally.h" |
| 57 |
#include "src/core/lib/surface/lame_client.h" |
| 58 |
#include "src/core/lib/transport/connectivity_state.h" |
| 59 |
|
| 60 |
namespace grpc_core { |
| 61 |
|
| 62 |
// |
| 63 |
// GrpcXdsTransportFactory::GrpcXdsTransport::GrpcStreamingCall |
| 64 |
// |
| 65 |
|
| 66 |
GrpcXdsTransportFactory::GrpcXdsTransport::GrpcStreamingCall::GrpcStreamingCall( |
| 67 |
RefCountedPtr<GrpcXdsTransportFactory> factory, grpc_channel* channel, |
| 68 |
const char* method, |
| 69 |
std::unique_ptr<StreamingCall::EventHandler> event_handler) |
| 70 |
: factory_(std::move(factory)), event_handler_(std::move(event_handler)) { |
| 71 |
// Create call. |
| 72 |
call_ = grpc_channel_create_pollset_set_call( |
| 73 |
channel, nullptr, GRPC_PROPAGATE_DEFAULTS, factory_->interested_parties(), |
| 74 |
StaticSlice::FromStaticString(method).c_slice(), nullptr, |
| 75 |
Timestamp::InfFuture(), nullptr); |
| 76 |
GPR_ASSERT(call_ != nullptr); |
| 77 |
// Init data associated with the call. |
| 78 |
grpc_metadata_array_init(&initial_metadata_recv_); |
| 79 |
grpc_metadata_array_init(&trailing_metadata_recv_); |
| 80 |
// Initialize closure to be used for sending messages. |
| 81 |
GRPC_CLOSURE_INIT(&on_request_sent_, OnRequestSent, this, nullptr); |
| 82 |
// Start ops on the call. |
| 83 |
grpc_call_error call_error; |
| 84 |
grpc_op ops[3]; |
| 85 |
memset(ops, 0, sizeof(ops)); |
| 86 |
// Send initial metadata. No callback for this, since we don't really |
| 87 |
// care when it finishes. |
| 88 |
grpc_op* op = ops; |
| 89 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 90 |
op->data.send_initial_metadata.count = 0; |
| 91 |
op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY | |
| 92 |
GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET; |
| 93 |
op->reserved = nullptr; |
| 94 |
op++; |
| 95 |
call_error = grpc_call_start_batch_and_execute( |
| 96 |
call_, ops, static_cast<size_t>(op - ops), nullptr); |
| 97 |
GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 98 |
// Start a batch with recv_initial_metadata and recv_message. |
| 99 |
op = ops; |
| 100 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 101 |
op->data.recv_initial_metadata.recv_initial_metadata = |
| 102 |
&initial_metadata_recv_; |
| 103 |
op->flags = 0; |
| 104 |
op->reserved = nullptr; |
| 105 |
op++; |
| 106 |
op->op = GRPC_OP_RECV_MESSAGE; |
| 107 |
op->data.recv_message.recv_message = &recv_message_payload_; |
| 108 |
op->flags = 0; |
| 109 |
op->reserved = nullptr; |
| 110 |
op++; |
| 111 |
Ref(DEBUG_LOCATION, "OnResponseReceived").release(); |
| 112 |
GRPC_CLOSURE_INIT(&on_response_received_, OnResponseReceived, this, nullptr); |
| 113 |
call_error = grpc_call_start_batch_and_execute( |
| 114 |
call_, ops, static_cast<size_t>(op - ops), &on_response_received_); |
| 115 |
GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 116 |
// Start a batch for recv_trailing_metadata. |
| 117 |
op = ops; |
| 118 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 119 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv_; |
| 120 |
op->data.recv_status_on_client.status = &status_code_; |
| 121 |
op->data.recv_status_on_client.status_details = &status_details_; |
| 122 |
op->flags = 0; |
| 123 |
op->reserved = nullptr; |
| 124 |
op++; |
| 125 |
// This callback signals the end of the call, so it relies on the initial |
| 126 |
// ref instead of a new ref. When it's invoked, it's the initial ref that is |
| 127 |
// unreffed. |
| 128 |
GRPC_CLOSURE_INIT(&on_status_received_, OnStatusReceived, this, nullptr); |
| 129 |
call_error = grpc_call_start_batch_and_execute( |
| 130 |
call_, ops, static_cast<size_t>(op - ops), &on_status_received_); |
| 131 |
GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 132 |
} |
| 133 |
|
| 134 |
GrpcXdsTransportFactory::GrpcXdsTransport::GrpcStreamingCall:: |
| 135 |
~GrpcStreamingCall() { |
| 136 |
grpc_metadata_array_destroy(&initial_metadata_recv_); |
| 137 |
grpc_metadata_array_destroy(&trailing_metadata_recv_); |
| 138 |
grpc_byte_buffer_destroy(send_message_payload_); |
| 139 |
grpc_byte_buffer_destroy(recv_message_payload_); |
| 140 |
CSliceUnref(status_details_); |
| 141 |
GPR_ASSERT(call_ != nullptr); |
| 142 |
grpc_call_unref(call_); |
| 143 |
} |
| 144 |
|
| 145 |
void GrpcXdsTransportFactory::GrpcXdsTransport::GrpcStreamingCall::Orphan() { |
| 146 |
GPR_ASSERT(call_ != nullptr); |
| 147 |
// If we are here because xds_client wants to cancel the call, |
| 148 |
// OnStatusReceived() will complete the cancellation and clean up. |
| 149 |
// Otherwise, we are here because xds_client has to orphan a failed call, |
| 150 |
// in which case the following cancellation will be a no-op. |
| 151 |
grpc_call_cancel_internal(call_); |
| 152 |
// Note that the initial ref is held by OnStatusReceived(), so the |
| 153 |
// corresponding unref happens there instead of here. |
| 154 |
} |
| 155 |
|
| 156 |
void GrpcXdsTransportFactory::GrpcXdsTransport::GrpcStreamingCall::SendMessage( |
| 157 |
std::string payload) { |
| 158 |
// Create payload. |
| 159 |
grpc_slice slice = grpc_slice_from_cpp_string(std::move(payload)); |
| 160 |
send_message_payload_ = grpc_raw_byte_buffer_create(&slice, 1); |
| 161 |
CSliceUnref(slice); |
| 162 |
// Send the message. |
| 163 |
grpc_op op; |
| 164 |
memset(&op, 0, sizeof(op)); |
| 165 |
op.op = GRPC_OP_SEND_MESSAGE; |
| 166 |
op.data.send_message.send_message = send_message_payload_; |
| 167 |
Ref(DEBUG_LOCATION, "OnRequestSent").release(); |
| 168 |
grpc_call_error call_error = |
| 169 |
grpc_call_start_batch_and_execute(call_, &op, 1, &on_request_sent_); |
| 170 |
GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 171 |
} |
| 172 |
|
| 173 |
void GrpcXdsTransportFactory::GrpcXdsTransport::GrpcStreamingCall:: |
| 174 |
OnRequestSent(void* arg, grpc_error_handle error) { |
| 175 |
auto* self = static_cast<GrpcStreamingCall*>(arg); |
| 176 |
// Clean up the sent message. |
| 177 |
grpc_byte_buffer_destroy(self->send_message_payload_); |
| 178 |
self->send_message_payload_ = nullptr; |
| 179 |
// Invoke request handler. |
| 180 |
self->event_handler_->OnRequestSent(error.ok()); |
| 181 |
// Drop the ref. |
| 182 |
self->Unref(DEBUG_LOCATION, "OnRequestSent"); |
| 183 |
} |
| 184 |
|
| 185 |
void GrpcXdsTransportFactory::GrpcXdsTransport::GrpcStreamingCall:: |
| 186 |
OnResponseReceived(void* arg, grpc_error_handle /*error*/) { |
| 187 |
auto* self = static_cast<GrpcStreamingCall*>(arg); |
| 188 |
// If there was no payload, then we received status before we received |
| 189 |
// another message, so we stop reading. |
| 190 |
if (self->recv_message_payload_ == nullptr) { |
| 191 |
self->Unref(DEBUG_LOCATION, "OnResponseReceived"); |
| 192 |
return; |
| 193 |
} |
| 194 |
// Process the response. |
| 195 |
grpc_byte_buffer_reader bbr; |
| 196 |
grpc_byte_buffer_reader_init(&bbr, self->recv_message_payload_); |
| 197 |
grpc_slice response_slice = grpc_byte_buffer_reader_readall(&bbr); |
| 198 |
grpc_byte_buffer_reader_destroy(&bbr); |
| 199 |
grpc_byte_buffer_destroy(self->recv_message_payload_); |
| 200 |
self->recv_message_payload_ = nullptr; |
| 201 |
self->event_handler_->OnRecvMessage(StringViewFromSlice(response_slice)); |
| 202 |
CSliceUnref(response_slice); |
| 203 |
// Keep reading. |
| 204 |
grpc_op op; |
| 205 |
memset(&op, 0, sizeof(op)); |
| 206 |
op.op = GRPC_OP_RECV_MESSAGE; |
| 207 |
op.data.recv_message.recv_message = &self->recv_message_payload_; |
| 208 |
GPR_ASSERT(self->call_ != nullptr); |
| 209 |
// Reuses the "OnResponseReceived" ref taken in ctor. |
| 210 |
const grpc_call_error call_error = grpc_call_start_batch_and_execute( |
| 211 |
self->call_, &op, 1, &self->on_response_received_); |
| 212 |
GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 213 |
} |
| 214 |
|
| 215 |
void GrpcXdsTransportFactory::GrpcXdsTransport::GrpcStreamingCall:: |
| 216 |
OnStatusReceived(void* arg, grpc_error_handle /*error*/) { |
| 217 |
auto* self = static_cast<GrpcStreamingCall*>(arg); |
| 218 |
self->event_handler_->OnStatusReceived( |
| 219 |
absl::Status(static_cast<absl::StatusCode>(self->status_code_), |
| 220 |
StringViewFromSlice(self->status_details_))); |
| 221 |
self->Unref(DEBUG_LOCATION, "OnStatusReceived"); |
| 222 |
} |
| 223 |
|
| 224 |
// |
| 225 |
// GrpcXdsTransportFactory::GrpcXdsTransport::StateWatcher |
| 226 |
// |
| 227 |
|
| 228 |
class GrpcXdsTransportFactory::GrpcXdsTransport::StateWatcher |
| 229 |
: public AsyncConnectivityStateWatcherInterface { |
| 230 |
public: |
| 231 |
explicit StateWatcher( |
| 232 |
std::function<void(absl::Status)> on_connectivity_failure) |
| 233 |
: on_connectivity_failure_(std::move(on_connectivity_failure)) {} |
| 234 |
|
| 235 |
private: |
| 236 |
void OnConnectivityStateChange(grpc_connectivity_state new_state, |
| 237 |
const absl::Status& status) override { |
| 238 |
if (new_state == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 239 |
on_connectivity_failure_(absl::Status( |
| 240 |
status.code(), |
| 241 |
absl::StrCat("channel in TRANSIENT_FAILURE: ", status.message()))); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
std::function<void(absl::Status)> on_connectivity_failure_; |
| 246 |
}; |
| 247 |
|
| 248 |
// |
| 249 |
// GrpcXdsClient::GrpcXdsTransport |
| 250 |
// |
| 251 |
|
| 252 |
namespace { |
| 253 |
|
| 254 |
grpc_channel* CreateXdsChannel(const ChannelArgs& args, |
| 255 |
const GrpcXdsBootstrap::GrpcXdsServer& server) { |
| 256 |
RefCountedPtr<grpc_channel_credentials> channel_creds = |
| 257 |
CoreConfiguration::Get().channel_creds_registry().CreateChannelCreds( |
| 258 |
server.channel_creds_type(), server.channel_creds_config()); |
| 259 |
return grpc_channel_create(server.server_uri().c_str(), channel_creds.get(), |
| 260 |
args.ToC().get()); |
| 261 |
} |
| 262 |
|
| 263 |
bool IsLameChannel(grpc_channel* channel) { |
| 264 |
grpc_channel_element* elem = |
| 265 |
grpc_channel_stack_last_element(grpc_channel_get_channel_stack(channel)); |
| 266 |
return elem->filter == &LameClientFilter::kFilter; |
| 267 |
} |
| 268 |
|
| 269 |
} // namespace |
| 270 |
|
| 271 |
GrpcXdsTransportFactory::GrpcXdsTransport::GrpcXdsTransport( |
| 272 |
GrpcXdsTransportFactory* factory, const XdsBootstrap::XdsServer& server, |
| 273 |
std::function<void(absl::Status)> on_connectivity_failure, |
| 274 |
absl::Status* status) |
| 275 |
: factory_(factory) { |
| 276 |
channel_ = CreateXdsChannel( |
| 277 |
factory->args_, |
| 278 |
static_cast<const GrpcXdsBootstrap::GrpcXdsServer&>(server)); |
| 279 |
GPR_ASSERT(channel_ != nullptr); |
| 280 |
if (IsLameChannel(channel_)) { |
| 281 |
*status = absl::UnavailableError("xds client has a lame channel"); |
| 282 |
} else { |
| 283 |
ClientChannel* client_channel = |
| 284 |
ClientChannel::GetFromChannel(Channel::FromC(channel_)); |
| 285 |
GPR_ASSERT(client_channel != nullptr); |
| 286 |
watcher_ = new StateWatcher(std::move(on_connectivity_failure)); |
| 287 |
client_channel->AddConnectivityWatcher( |
| 288 |
GRPC_CHANNEL_IDLE, |
| 289 |
OrphanablePtr<AsyncConnectivityStateWatcherInterface>(watcher_)); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
GrpcXdsTransportFactory::GrpcXdsTransport::~GrpcXdsTransport() { |
| 294 |
grpc_channel_destroy(channel_); |
| 295 |
} |
| 296 |
|
| 297 |
void GrpcXdsTransportFactory::GrpcXdsTransport::Orphan() { |
| 298 |
if (!IsLameChannel(channel_)) { |
| 299 |
ClientChannel* client_channel = |
| 300 |
ClientChannel::GetFromChannel(Channel::FromC(channel_)); |
| 301 |
GPR_ASSERT(client_channel != nullptr); |
| 302 |
client_channel->RemoveConnectivityWatcher(watcher_); |
| 303 |
} |
| 304 |
Unref(); |
| 305 |
} |
| 306 |
|
| 307 |
OrphanablePtr<XdsTransportFactory::XdsTransport::StreamingCall> |
| 308 |
GrpcXdsTransportFactory::GrpcXdsTransport::CreateStreamingCall( |
| 309 |
const char* method, |
| 310 |
std::unique_ptr<StreamingCall::EventHandler> event_handler) { |
| 311 |
return MakeOrphanable<GrpcStreamingCall>( |
| 312 |
factory_->Ref(DEBUG_LOCATION, "StreamingCall"), channel_, method, |
| 313 |
std::move(event_handler)); |
| 314 |
} |
| 315 |
|
| 316 |
void GrpcXdsTransportFactory::GrpcXdsTransport::ResetBackoff() { |
| 317 |
grpc_channel_reset_connect_backoff(channel_); |
| 318 |
} |
| 319 |
|
| 320 |
// |
| 321 |
// GrpcXdsTransportFactory |
| 322 |
// |
| 323 |
|
| 324 |
namespace { |
| 325 |
|
| 326 |
ChannelArgs ModifyChannelArgs(const ChannelArgs& args) { |
| 327 |
return args.Set(GRPC_ARG_KEEPALIVE_TIME_MS, Duration::Minutes(5).millis()); |
| 328 |
} |
| 329 |
|
| 330 |
} // namespace |
| 331 |
|
| 332 |
GrpcXdsTransportFactory::GrpcXdsTransportFactory(const ChannelArgs& args) |
| 333 |
: args_(ModifyChannelArgs(args)), |
| 334 |
interested_parties_(grpc_pollset_set_create()) { |
| 335 |
// Calling grpc_init to ensure gRPC does not shut down until the XdsClient is |
| 336 |
// destroyed. |
| 337 |
InitInternally(); |
| 338 |
} |
| 339 |
|
| 340 |
GrpcXdsTransportFactory::~GrpcXdsTransportFactory() { |
| 341 |
grpc_pollset_set_destroy(interested_parties_); |
| 342 |
// Calling grpc_shutdown to ensure gRPC does not shut down until the XdsClient |
| 343 |
// is destroyed. |
| 344 |
ShutdownInternally(); |
| 345 |
} |
| 346 |
|
| 347 |
OrphanablePtr<XdsTransportFactory::XdsTransport> |
| 348 |
GrpcXdsTransportFactory::Create( |
| 349 |
const XdsBootstrap::XdsServer& server, |
| 350 |
std::function<void(absl::Status)> on_connectivity_failure, |
| 351 |
absl::Status* status) { |
| 352 |
return MakeOrphanable<GrpcXdsTransport>( |
| 353 |
this, server, std::move(on_connectivity_failure), status); |
| 354 |
} |
| 355 |
|
| 356 |
} // namespace grpc_core |