| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 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/transport.h" |
| 22 |
|
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include <memory> |
| 26 |
#include <new> |
| 27 |
|
| 28 |
#include "absl/status/status.h" |
| 29 |
|
| 30 |
#include <grpc/event_engine/event_engine.h> |
| 31 |
|
| 32 |
#include "src/core/lib/event_engine/default_event_engine.h" |
| 33 |
#include "src/core/lib/gpr/alloc.h" |
| 34 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 35 |
#include "src/core/lib/promise/context.h" |
| 36 |
#include "src/core/lib/slice/slice.h" |
| 37 |
#include "src/core/lib/transport/transport_impl.h" |
| 38 |
|
| 39 |
grpc_core::DebugOnlyTraceFlag grpc_trace_stream_refcount(false, |
| 40 |
"stream_refcount"); |
| 41 |
|
| 42 |
void grpc_stream_destroy(grpc_stream_refcount* refcount) { |
| 43 |
if ((grpc_core::ExecCtx::Get()->flags() & |
| 44 |
GRPC_EXEC_CTX_FLAG_THREAD_RESOURCE_LOOP)) { |
| 45 |
/* Ick. |
| 46 |
The thread we're running on MAY be owned (indirectly) by a call-stack. |
| 47 |
If that's the case, destroying the call-stack MAY try to destroy the |
| 48 |
thread, which is a tangled mess that we just don't want to ever have to |
| 49 |
cope with. |
| 50 |
Throw this over to the executor (on a core-owned thread) and process it |
| 51 |
there. */ |
| 52 |
grpc_event_engine::experimental::GetDefaultEventEngine()->Run([refcount] { |
| 53 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 54 |
grpc_core::ExecCtx exec_ctx; |
| 55 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, &refcount->destroy, |
| 56 |
absl::OkStatus()); |
| 57 |
}); |
| 58 |
} else { |
| 59 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, &refcount->destroy, |
| 60 |
absl::OkStatus()); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
void slice_stream_destroy(void* arg) { |
| 65 |
grpc_stream_destroy(static_cast<grpc_stream_refcount*>(arg)); |
| 66 |
} |
| 67 |
|
| 68 |
#ifndef NDEBUG |
| 69 |
void grpc_stream_ref_init(grpc_stream_refcount* refcount, int /*initial_refs*/, |
| 70 |
grpc_iomgr_cb_func cb, void* cb_arg, |
| 71 |
const char* object_type) { |
| 72 |
refcount->object_type = object_type; |
| 73 |
#else |
| 74 |
void grpc_stream_ref_init(grpc_stream_refcount* refcount, int /*initial_refs*/, |
| 75 |
grpc_iomgr_cb_func cb, void* cb_arg) { |
| 76 |
#endif |
| 77 |
GRPC_CLOSURE_INIT(&refcount->destroy, cb, cb_arg, grpc_schedule_on_exec_ctx); |
| 78 |
|
| 79 |
new (&refcount->refs) grpc_core::RefCount( |
| 80 |
1, GRPC_TRACE_FLAG_ENABLED(grpc_trace_stream_refcount) ? "stream_refcount" |
| 81 |
: nullptr); |
| 82 |
} |
| 83 |
|
| 84 |
static void move64bits(uint64_t* from, uint64_t* to) { |
| 85 |
*to += *from; |
| 86 |
*from = 0; |
| 87 |
} |
| 88 |
|
| 89 |
void grpc_transport_move_one_way_stats(grpc_transport_one_way_stats* from, |
| 90 |
grpc_transport_one_way_stats* to) { |
| 91 |
move64bits(&from->framing_bytes, &to->framing_bytes); |
| 92 |
move64bits(&from->data_bytes, &to->data_bytes); |
| 93 |
move64bits(&from->header_bytes, &to->header_bytes); |
| 94 |
} |
| 95 |
|
| 96 |
void grpc_transport_move_stats(grpc_transport_stream_stats* from, |
| 97 |
grpc_transport_stream_stats* to) { |
| 98 |
grpc_transport_move_one_way_stats(&from->incoming, &to->incoming); |
| 99 |
grpc_transport_move_one_way_stats(&from->outgoing, &to->outgoing); |
| 100 |
} |
| 101 |
|
| 102 |
size_t grpc_transport_stream_size(grpc_transport* transport) { |
| 103 |
return GPR_ROUND_UP_TO_ALIGNMENT_SIZE(transport->vtable->sizeof_stream); |
| 104 |
} |
| 105 |
|
| 106 |
void grpc_transport_destroy(grpc_transport* transport) { |
| 107 |
transport->vtable->destroy(transport); |
| 108 |
} |
| 109 |
|
| 110 |
int grpc_transport_init_stream(grpc_transport* transport, grpc_stream* stream, |
| 111 |
grpc_stream_refcount* refcount, |
| 112 |
const void* server_data, |
| 113 |
grpc_core::Arena* arena) { |
| 114 |
return transport->vtable->init_stream(transport, stream, refcount, |
| 115 |
server_data, arena); |
| 116 |
} |
| 117 |
|
| 118 |
void grpc_transport_perform_stream_op(grpc_transport* transport, |
| 119 |
grpc_stream* stream, |
| 120 |
grpc_transport_stream_op_batch* op) { |
| 121 |
transport->vtable->perform_stream_op(transport, stream, op); |
| 122 |
} |
| 123 |
|
| 124 |
void grpc_transport_perform_op(grpc_transport* transport, |
| 125 |
grpc_transport_op* op) { |
| 126 |
transport->vtable->perform_op(transport, op); |
| 127 |
} |
| 128 |
|
| 129 |
void grpc_transport_set_pops(grpc_transport* transport, grpc_stream* stream, |
| 130 |
grpc_polling_entity* pollent) { |
| 131 |
grpc_pollset* pollset; |
| 132 |
grpc_pollset_set* pollset_set; |
| 133 |
if ((pollset = grpc_polling_entity_pollset(pollent)) != nullptr) { |
| 134 |
transport->vtable->set_pollset(transport, stream, pollset); |
| 135 |
} else if ((pollset_set = grpc_polling_entity_pollset_set(pollent)) != |
| 136 |
nullptr) { |
| 137 |
transport->vtable->set_pollset_set(transport, stream, pollset_set); |
| 138 |
} else { |
| 139 |
// No-op for empty pollset. Empty pollset is possible when using |
| 140 |
// non-fd-based event engines such as CFStream. |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void grpc_transport_destroy_stream(grpc_transport* transport, |
| 145 |
grpc_stream* stream, |
| 146 |
grpc_closure* then_schedule_closure) { |
| 147 |
transport->vtable->destroy_stream(transport, stream, then_schedule_closure); |
| 148 |
} |
| 149 |
|
| 150 |
grpc_endpoint* grpc_transport_get_endpoint(grpc_transport* transport) { |
| 151 |
return transport->vtable->get_endpoint(transport); |
| 152 |
} |
| 153 |
|
| 154 |
// This comment should be sung to the tune of |
| 155 |
// "Supercalifragilisticexpialidocious": |
| 156 |
// |
| 157 |
// grpc_transport_stream_op_batch_finish_with_failure |
| 158 |
// is a function that must always unref cancel_error |
| 159 |
// though it lives in lib, it handles transport stream ops sure |
| 160 |
// it's grpc_transport_stream_op_batch_finish_with_failure |
| 161 |
void grpc_transport_stream_op_batch_finish_with_failure( |
| 162 |
grpc_transport_stream_op_batch* batch, grpc_error_handle error, |
| 163 |
grpc_core::CallCombiner* call_combiner) { |
| 164 |
grpc_core::CallCombinerClosureList closures; |
| 165 |
grpc_transport_stream_op_batch_queue_finish_with_failure(batch, error, |
| 166 |
&closures); |
| 167 |
// Execute closures. |
| 168 |
closures.RunClosures(call_combiner); |
| 169 |
} |
| 170 |
|
| 171 |
void grpc_transport_stream_op_batch_queue_finish_with_failure( |
| 172 |
grpc_transport_stream_op_batch* batch, grpc_error_handle error, |
| 173 |
grpc_core::CallCombinerClosureList* closures) { |
| 174 |
if (batch->cancel_stream) { |
| 175 |
} |
| 176 |
// Construct a list of closures to execute. |
| 177 |
if (batch->recv_initial_metadata) { |
| 178 |
closures->Add( |
| 179 |
batch->payload->recv_initial_metadata.recv_initial_metadata_ready, |
| 180 |
error, "failing recv_initial_metadata_ready"); |
| 181 |
} |
| 182 |
if (batch->recv_message) { |
| 183 |
closures->Add(batch->payload->recv_message.recv_message_ready, error, |
| 184 |
"failing recv_message_ready"); |
| 185 |
} |
| 186 |
if (batch->recv_trailing_metadata) { |
| 187 |
closures->Add( |
| 188 |
batch->payload->recv_trailing_metadata.recv_trailing_metadata_ready, |
| 189 |
error, "failing recv_trailing_metadata_ready"); |
| 190 |
} |
| 191 |
if (batch->on_complete != nullptr) { |
| 192 |
closures->Add(batch->on_complete, error, "failing on_complete"); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
void grpc_transport_stream_op_batch_finish_with_failure_from_transport( |
| 197 |
grpc_transport_stream_op_batch* batch, grpc_error_handle error) { |
| 198 |
if (batch->cancel_stream) { |
| 199 |
} |
| 200 |
// Construct a list of closures to execute. |
| 201 |
if (batch->recv_initial_metadata) { |
| 202 |
grpc_core::ExecCtx::Run( |
| 203 |
DEBUG_LOCATION, |
| 204 |
batch->payload->recv_initial_metadata.recv_initial_metadata_ready, |
| 205 |
error); |
| 206 |
} |
| 207 |
if (batch->recv_message) { |
| 208 |
grpc_core::ExecCtx::Run( |
| 209 |
DEBUG_LOCATION, batch->payload->recv_message.recv_message_ready, error); |
| 210 |
} |
| 211 |
if (batch->recv_trailing_metadata) { |
| 212 |
grpc_core::ExecCtx::Run( |
| 213 |
DEBUG_LOCATION, |
| 214 |
batch->payload->recv_trailing_metadata.recv_trailing_metadata_ready, |
| 215 |
error); |
| 216 |
} |
| 217 |
if (batch->on_complete != nullptr) { |
| 218 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, batch->on_complete, error); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
struct made_transport_op { |
| 223 |
grpc_closure outer_on_complete; |
| 224 |
grpc_closure* inner_on_complete = nullptr; |
| 225 |
grpc_transport_op op; |
| 226 |
made_transport_op() { |
| 227 |
memset(&outer_on_complete, 0, sizeof(outer_on_complete)); |
| 228 |
} |
| 229 |
}; |
| 230 |
|
| 231 |
static void destroy_made_transport_op(void* arg, grpc_error_handle error) { |
| 232 |
made_transport_op* op = static_cast<made_transport_op*>(arg); |
| 233 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, op->inner_on_complete, error); |
| 234 |
delete op; |
| 235 |
} |
| 236 |
|
| 237 |
grpc_transport_op* grpc_make_transport_op(grpc_closure* on_complete) { |
| 238 |
made_transport_op* op = new made_transport_op(); |
| 239 |
GRPC_CLOSURE_INIT(&op->outer_on_complete, destroy_made_transport_op, op, |
| 240 |
grpc_schedule_on_exec_ctx); |
| 241 |
op->inner_on_complete = on_complete; |
| 242 |
op->op.on_consumed = &op->outer_on_complete; |
| 243 |
return &op->op; |
| 244 |
} |
| 245 |
|
| 246 |
struct made_transport_stream_op { |
| 247 |
grpc_closure outer_on_complete; |
| 248 |
grpc_closure* inner_on_complete = nullptr; |
| 249 |
grpc_transport_stream_op_batch op; |
| 250 |
grpc_transport_stream_op_batch_payload payload{nullptr}; |
| 251 |
}; |
| 252 |
static void destroy_made_transport_stream_op(void* arg, |
| 253 |
grpc_error_handle error) { |
| 254 |
made_transport_stream_op* op = static_cast<made_transport_stream_op*>(arg); |
| 255 |
grpc_closure* c = op->inner_on_complete; |
| 256 |
delete op; |
| 257 |
if (c != nullptr) { |
| 258 |
grpc_core::Closure::Run(DEBUG_LOCATION, c, error); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
grpc_transport_stream_op_batch* grpc_make_transport_stream_op( |
| 263 |
grpc_closure* on_complete) { |
| 264 |
made_transport_stream_op* op = new made_transport_stream_op(); |
| 265 |
op->op.payload = &op->payload; |
| 266 |
GRPC_CLOSURE_INIT(&op->outer_on_complete, destroy_made_transport_stream_op, |
| 267 |
op, grpc_schedule_on_exec_ctx); |
| 268 |
op->inner_on_complete = on_complete; |
| 269 |
op->op.on_complete = &op->outer_on_complete; |
| 270 |
return &op->op; |
| 271 |
} |
| 272 |
|
| 273 |
namespace grpc_core { |
| 274 |
|
| 275 |
ServerMetadataHandle ServerMetadataFromStatus(const absl::Status& status) { |
| 276 |
auto hdl = |
| 277 |
GetContext<Arena>()->MakePooled<ServerMetadata>(GetContext<Arena>()); |
| 278 |
hdl->Set(GrpcStatusMetadata(), static_cast<grpc_status_code>(status.code())); |
| 279 |
if (!status.ok()) { |
| 280 |
hdl->Set(GrpcMessageMetadata(), Slice::FromCopiedString(status.message())); |
| 281 |
} |
| 282 |
return hdl; |
| 283 |
} |
| 284 |
|
| 285 |
} // namespace grpc_core |