| 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 |
#ifndef GRPCXX_CHANNEL_FILTER_H |
| 20 |
#define GRPCXX_CHANNEL_FILTER_H |
| 21 |
|
| 22 |
#include <stddef.h> |
| 23 |
|
| 24 |
#include <functional> |
| 25 |
#include <new> |
| 26 |
#include <string> |
| 27 |
#include <utility> |
| 28 |
|
| 29 |
#include "absl/status/status.h" |
| 30 |
#include "absl/types/optional.h" |
| 31 |
|
| 32 |
#include <grpc/grpc.h> |
| 33 |
#include <grpc/impl/codegen/grpc_types.h> |
| 34 |
#include <grpc/support/atm.h> |
| 35 |
#include <grpcpp/support/config.h> |
| 36 |
|
| 37 |
#include "src/core/lib/channel/channel_fwd.h" |
| 38 |
#include "src/core/lib/channel/channel_stack.h" |
| 39 |
#include "src/core/lib/channel/context.h" |
| 40 |
#include "src/core/lib/iomgr/closure.h" |
| 41 |
#include "src/core/lib/iomgr/error.h" |
| 42 |
#include "src/core/lib/iomgr/polling_entity.h" |
| 43 |
#include "src/core/lib/slice/slice_buffer.h" |
| 44 |
#include "src/core/lib/surface/channel_stack_type.h" |
| 45 |
#include "src/core/lib/transport/metadata_batch.h" |
| 46 |
#include "src/core/lib/transport/transport.h" |
| 47 |
|
| 48 |
/// An interface to define filters. |
| 49 |
/// |
| 50 |
/// To define a filter, implement a subclass of each of \c CallData and |
| 51 |
/// \c ChannelData. Then register the filter using something like this: |
| 52 |
/// \code{.cpp} |
| 53 |
/// RegisterChannelFilter<MyChannelDataSubclass, MyCallDataSubclass>( |
| 54 |
/// "name-of-filter", GRPC_SERVER_CHANNEL, INT_MAX, nullptr); |
| 55 |
/// \endcode |
| 56 |
|
| 57 |
namespace grpc { |
| 58 |
|
| 59 |
/// A C++ wrapper for the \c grpc_metadata_batch struct. |
| 60 |
class MetadataBatch { |
| 61 |
public: |
| 62 |
/// Borrows a pointer to \a batch, but does NOT take ownership. |
| 63 |
/// The caller must ensure that \a batch continues to exist for as |
| 64 |
/// long as the MetadataBatch object does. |
| 65 |
explicit MetadataBatch(grpc_metadata_batch* batch) : batch_(batch) {} |
| 66 |
|
| 67 |
grpc_metadata_batch* batch() const { return batch_; } |
| 68 |
|
| 69 |
/// Adds metadata. |
| 70 |
void AddMetadata(const string& key, const string& value); |
| 71 |
|
| 72 |
private: |
| 73 |
grpc_metadata_batch* batch_; // Not owned. |
| 74 |
}; |
| 75 |
|
| 76 |
/// A C++ wrapper for the \c grpc_transport_op struct. |
| 77 |
class TransportOp { |
| 78 |
public: |
| 79 |
/// Borrows a pointer to \a op, but does NOT take ownership. |
| 80 |
/// The caller must ensure that \a op continues to exist for as |
| 81 |
/// long as the TransportOp object does. |
| 82 |
explicit TransportOp(grpc_transport_op* op) : op_(op) {} |
| 83 |
|
| 84 |
grpc_transport_op* op() const { return op_; } |
| 85 |
|
| 86 |
// TODO(roth): Add a C++ wrapper for grpc_error? |
| 87 |
grpc_error_handle disconnect_with_error() const { |
| 88 |
return op_->disconnect_with_error; |
| 89 |
} |
| 90 |
bool send_goaway() const { return !op_->goaway_error.ok(); } |
| 91 |
|
| 92 |
// TODO(roth): Add methods for additional fields as needed. |
| 93 |
|
| 94 |
private: |
| 95 |
grpc_transport_op* op_; // Not owned. |
| 96 |
}; |
| 97 |
|
| 98 |
/// A C++ wrapper for the \c grpc_transport_stream_op_batch struct. |
| 99 |
class TransportStreamOpBatch { |
| 100 |
public: |
| 101 |
/// Borrows a pointer to \a op, but does NOT take ownership. |
| 102 |
/// The caller must ensure that \a op continues to exist for as |
| 103 |
/// long as the TransportStreamOpBatch object does. |
| 104 |
explicit TransportStreamOpBatch(grpc_transport_stream_op_batch* op) |
| 105 |
: op_(op), |
| 106 |
send_initial_metadata_( |
| 107 |
op->send_initial_metadata |
| 108 |
? op->payload->send_initial_metadata.send_initial_metadata |
| 109 |
: nullptr), |
| 110 |
send_trailing_metadata_( |
| 111 |
op->send_trailing_metadata |
| 112 |
? op->payload->send_trailing_metadata.send_trailing_metadata |
| 113 |
: nullptr), |
| 114 |
recv_initial_metadata_( |
| 115 |
op->recv_initial_metadata |
| 116 |
? op->payload->recv_initial_metadata.recv_initial_metadata |
| 117 |
: nullptr), |
| 118 |
recv_trailing_metadata_( |
| 119 |
op->recv_trailing_metadata |
| 120 |
? op->payload->recv_trailing_metadata.recv_trailing_metadata |
| 121 |
: nullptr) {} |
| 122 |
|
| 123 |
grpc_transport_stream_op_batch* op() const { return op_; } |
| 124 |
|
| 125 |
grpc_closure* on_complete() const { return op_->on_complete; } |
| 126 |
void set_on_complete(grpc_closure* closure) { op_->on_complete = closure; } |
| 127 |
|
| 128 |
MetadataBatch* send_initial_metadata() { |
| 129 |
return op_->send_initial_metadata ? &send_initial_metadata_ : nullptr; |
| 130 |
} |
| 131 |
MetadataBatch* send_trailing_metadata() { |
| 132 |
return op_->send_trailing_metadata ? &send_trailing_metadata_ : nullptr; |
| 133 |
} |
| 134 |
MetadataBatch* recv_initial_metadata() { |
| 135 |
return op_->recv_initial_metadata ? &recv_initial_metadata_ : nullptr; |
| 136 |
} |
| 137 |
MetadataBatch* recv_trailing_metadata() { |
| 138 |
return op_->recv_trailing_metadata ? &recv_trailing_metadata_ : nullptr; |
| 139 |
} |
| 140 |
|
| 141 |
grpc_closure* recv_initial_metadata_ready() const { |
| 142 |
return op_->recv_initial_metadata |
| 143 |
? op_->payload->recv_initial_metadata.recv_initial_metadata_ready |
| 144 |
: nullptr; |
| 145 |
} |
| 146 |
void set_recv_initial_metadata_ready(grpc_closure* closure) { |
| 147 |
op_->payload->recv_initial_metadata.recv_initial_metadata_ready = closure; |
| 148 |
} |
| 149 |
|
| 150 |
grpc_core::SliceBuffer* send_message() const { |
| 151 |
return op_->send_message ? op_->payload->send_message.send_message |
| 152 |
: nullptr; |
| 153 |
} |
| 154 |
|
| 155 |
void set_send_message(grpc_core::SliceBuffer* send_message) { |
| 156 |
op_->send_message = true; |
| 157 |
op_->payload->send_message.send_message = send_message; |
| 158 |
} |
| 159 |
|
| 160 |
absl::optional<grpc_core::SliceBuffer>* recv_message() const { |
| 161 |
return op_->recv_message ? op_->payload->recv_message.recv_message |
| 162 |
: nullptr; |
| 163 |
} |
| 164 |
void set_recv_message(absl::optional<grpc_core::SliceBuffer>* recv_message) { |
| 165 |
op_->recv_message = true; |
| 166 |
op_->payload->recv_message.recv_message = recv_message; |
| 167 |
} |
| 168 |
|
| 169 |
census_context* get_census_context() const { |
| 170 |
return static_cast<census_context*>( |
| 171 |
op_->payload->context[GRPC_CONTEXT_TRACING].value); |
| 172 |
} |
| 173 |
|
| 174 |
const gpr_atm* get_peer_string() const { |
| 175 |
if (op_->send_initial_metadata && |
| 176 |
op_->payload->send_initial_metadata.peer_string != nullptr) { |
| 177 |
return op_->payload->send_initial_metadata.peer_string; |
| 178 |
} else if (op_->recv_initial_metadata && |
| 179 |
op_->payload->recv_initial_metadata.peer_string != nullptr) { |
| 180 |
return op_->payload->recv_initial_metadata.peer_string; |
| 181 |
} else { |
| 182 |
return nullptr; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
private: |
| 187 |
grpc_transport_stream_op_batch* op_; // Not owned. |
| 188 |
MetadataBatch send_initial_metadata_; |
| 189 |
MetadataBatch send_trailing_metadata_; |
| 190 |
MetadataBatch recv_initial_metadata_; |
| 191 |
MetadataBatch recv_trailing_metadata_; |
| 192 |
}; |
| 193 |
|
| 194 |
/// Represents channel data. |
| 195 |
class ChannelData { |
| 196 |
public: |
| 197 |
ChannelData() {} |
| 198 |
virtual ~ChannelData() {} |
| 199 |
|
| 200 |
// TODO(roth): Come up with a more C++-like API for the channel element. |
| 201 |
|
| 202 |
/// Initializes the channel data. |
| 203 |
virtual grpc_error_handle Init(grpc_channel_element* /*elem*/, |
| 204 |
grpc_channel_element_args* /*args*/) { |
| 205 |
return absl::OkStatus(); |
| 206 |
} |
| 207 |
|
| 208 |
// Called before destruction. |
| 209 |
virtual void Destroy(grpc_channel_element* /*elem*/) {} |
| 210 |
|
| 211 |
virtual void StartTransportOp(grpc_channel_element* elem, TransportOp* op); |
| 212 |
|
| 213 |
virtual void GetInfo(grpc_channel_element* elem, |
| 214 |
const grpc_channel_info* channel_info); |
| 215 |
}; |
| 216 |
|
| 217 |
/// Represents call data. |
| 218 |
class CallData { |
| 219 |
public: |
| 220 |
CallData() {} |
| 221 |
virtual ~CallData() {} |
| 222 |
|
| 223 |
// TODO(roth): Come up with a more C++-like API for the call element. |
| 224 |
|
| 225 |
/// Initializes the call data. |
| 226 |
virtual grpc_error_handle Init(grpc_call_element* /*elem*/, |
| 227 |
const grpc_call_element_args* /*args*/) { |
| 228 |
return absl::OkStatus(); |
| 229 |
} |
| 230 |
|
| 231 |
// Called before destruction. |
| 232 |
virtual void Destroy(grpc_call_element* /*elem*/, |
| 233 |
const grpc_call_final_info* /*final_info*/, |
| 234 |
grpc_closure* /*then_call_closure*/) {} |
| 235 |
|
| 236 |
/// Starts a new stream operation. |
| 237 |
virtual void StartTransportStreamOpBatch(grpc_call_element* elem, |
| 238 |
TransportStreamOpBatch* op); |
| 239 |
|
| 240 |
/// Sets a pollset or pollset set. |
| 241 |
virtual void SetPollsetOrPollsetSet(grpc_call_element* elem, |
| 242 |
grpc_polling_entity* pollent); |
| 243 |
}; |
| 244 |
|
| 245 |
namespace internal { |
| 246 |
|
| 247 |
// Defines static members for passing to C core. |
| 248 |
// Members of this class correspond to the members of the C |
| 249 |
// grpc_channel_filter struct. |
| 250 |
template <typename ChannelDataType, typename CallDataType> |
| 251 |
class ChannelFilter final { |
| 252 |
public: |
| 253 |
static const size_t channel_data_size = sizeof(ChannelDataType); |
| 254 |
|
| 255 |
static grpc_error_handle InitChannelElement(grpc_channel_element* elem, |
| 256 |
grpc_channel_element_args* args) { |
| 257 |
// Construct the object in the already-allocated memory. |
| 258 |
ChannelDataType* channel_data = new (elem->channel_data) ChannelDataType(); |
| 259 |
return channel_data->Init(elem, args); |
| 260 |
} |
| 261 |
|
| 262 |
static void DestroyChannelElement(grpc_channel_element* elem) { |
| 263 |
ChannelDataType* channel_data = |
| 264 |
static_cast<ChannelDataType*>(elem->channel_data); |
| 265 |
channel_data->Destroy(elem); |
| 266 |
channel_data->~ChannelDataType(); |
| 267 |
} |
| 268 |
|
| 269 |
static void StartTransportOp(grpc_channel_element* elem, |
| 270 |
grpc_transport_op* op) { |
| 271 |
ChannelDataType* channel_data = |
| 272 |
static_cast<ChannelDataType*>(elem->channel_data); |
| 273 |
TransportOp op_wrapper(op); |
| 274 |
channel_data->StartTransportOp(elem, &op_wrapper); |
| 275 |
} |
| 276 |
|
| 277 |
static void GetChannelInfo(grpc_channel_element* elem, |
| 278 |
const grpc_channel_info* channel_info) { |
| 279 |
ChannelDataType* channel_data = |
| 280 |
static_cast<ChannelDataType*>(elem->channel_data); |
| 281 |
channel_data->GetInfo(elem, channel_info); |
| 282 |
} |
| 283 |
|
| 284 |
static const size_t call_data_size = sizeof(CallDataType); |
| 285 |
|
| 286 |
static grpc_error_handle InitCallElement(grpc_call_element* elem, |
| 287 |
const grpc_call_element_args* args) { |
| 288 |
// Construct the object in the already-allocated memory. |
| 289 |
CallDataType* call_data = new (elem->call_data) CallDataType(); |
| 290 |
return call_data->Init(elem, args); |
| 291 |
} |
| 292 |
|
| 293 |
static void DestroyCallElement(grpc_call_element* elem, |
| 294 |
const grpc_call_final_info* final_info, |
| 295 |
grpc_closure* then_call_closure) { |
| 296 |
CallDataType* call_data = static_cast<CallDataType*>(elem->call_data); |
| 297 |
call_data->Destroy(elem, final_info, then_call_closure); |
| 298 |
call_data->~CallDataType(); |
| 299 |
} |
| 300 |
|
| 301 |
static void StartTransportStreamOpBatch(grpc_call_element* elem, |
| 302 |
grpc_transport_stream_op_batch* op) { |
| 303 |
CallDataType* call_data = static_cast<CallDataType*>(elem->call_data); |
| 304 |
TransportStreamOpBatch op_wrapper(op); |
| 305 |
call_data->StartTransportStreamOpBatch(elem, &op_wrapper); |
| 306 |
} |
| 307 |
|
| 308 |
static void SetPollsetOrPollsetSet(grpc_call_element* elem, |
| 309 |
grpc_polling_entity* pollent) { |
| 310 |
CallDataType* call_data = static_cast<CallDataType*>(elem->call_data); |
| 311 |
call_data->SetPollsetOrPollsetSet(elem, pollent); |
| 312 |
} |
| 313 |
}; |
| 314 |
|
| 315 |
void RegisterChannelFilter( |
| 316 |
grpc_channel_stack_type stack_type, int priority, |
| 317 |
std::function<bool(const grpc_channel_args&)> include_filter, |
| 318 |
const grpc_channel_filter* filter); |
| 319 |
|
| 320 |
} // namespace internal |
| 321 |
|
| 322 |
/// Registers a new filter. |
| 323 |
/// Must be called by only one thread at a time. |
| 324 |
/// The \a include_filter argument specifies a function that will be called |
| 325 |
/// to determine at run-time whether or not to add the filter. If the |
| 326 |
/// value is nullptr, the filter will be added unconditionally. |
| 327 |
/// If the channel stack type is GRPC_CLIENT_SUBCHANNEL, the caller should |
| 328 |
/// ensure that subchannels with different filter lists will always have |
| 329 |
/// different channel args. This requires setting a channel arg in case the |
| 330 |
/// registration function relies on some condition other than channel args to |
| 331 |
/// decide whether to add a filter or not. |
| 332 |
template <typename ChannelDataType, typename CallDataType> |
| 333 |
void RegisterChannelFilter( |
| 334 |
const char* name, grpc_channel_stack_type stack_type, int priority, |
| 335 |
std::function<bool(const grpc_channel_args&)> include_filter) { |
| 336 |
using FilterType = internal::ChannelFilter<ChannelDataType, CallDataType>; |
| 337 |
static const grpc_channel_filter filter = { |
| 338 |
FilterType::StartTransportStreamOpBatch, |
| 339 |
nullptr, |
| 340 |
FilterType::StartTransportOp, |
| 341 |
FilterType::call_data_size, |
| 342 |
FilterType::InitCallElement, |
| 343 |
FilterType::SetPollsetOrPollsetSet, |
| 344 |
FilterType::DestroyCallElement, |
| 345 |
FilterType::channel_data_size, |
| 346 |
FilterType::InitChannelElement, |
| 347 |
grpc_channel_stack_no_post_init, |
| 348 |
FilterType::DestroyChannelElement, |
| 349 |
FilterType::GetChannelInfo, |
| 350 |
name}; |
| 351 |
grpc::internal::RegisterChannelFilter(stack_type, priority, |
| 352 |
std::move(include_filter), &filter); |
| 353 |
} |
| 354 |
|
| 355 |
} // namespace grpc |
| 356 |
|
| 357 |
#endif // GRPCXX_CHANNEL_FILTER_H |