3 * Copyright 2015 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 #ifndef GRPC_CORE_LIB_TRANSPORT_TRANSPORT_H
20 #define GRPC_CORE_LIB_TRANSPORT_TRANSPORT_H
22 #include <grpc/support/port_platform.h>
26 #include "src/core/lib/channel/context.h"
27 #include "src/core/lib/gpr/arena.h"
28 #include "src/core/lib/iomgr/call_combiner.h"
29 #include "src/core/lib/iomgr/endpoint.h"
30 #include "src/core/lib/iomgr/polling_entity.h"
31 #include "src/core/lib/iomgr/pollset.h"
32 #include "src/core/lib/iomgr/pollset_set.h"
33 #include "src/core/lib/transport/byte_stream.h"
34 #include "src/core/lib/transport/metadata_batch.h"
36 /* Minimum and maximum protocol accepted versions. */
37 #define GRPC_PROTOCOL_VERSION_MAX_MAJOR 2
38 #define GRPC_PROTOCOL_VERSION_MAX_MINOR 1
39 #define GRPC_PROTOCOL_VERSION_MIN_MAJOR 2
40 #define GRPC_PROTOCOL_VERSION_MIN_MINOR 1
42 /* forward declarations */
44 typedef struct grpc_transport grpc_transport;
46 /* grpc_stream doesn't actually exist. It's used as a typesafe
47 opaque pointer for whatever data the transport wants to track
49 typedef struct grpc_stream grpc_stream;
51 extern grpc_core::DebugOnlyTraceFlag grpc_trace_stream_refcount;
53 typedef struct grpc_stream_refcount {
57 const char* object_type;
59 grpc_slice_refcount slice_refcount;
60 } grpc_stream_refcount;
63 void grpc_stream_ref_init(grpc_stream_refcount* refcount, int initial_refs,
64 grpc_iomgr_cb_func cb, void* cb_arg,
65 const char* object_type);
66 void grpc_stream_ref(grpc_stream_refcount* refcount, const char* reason);
67 void grpc_stream_unref(grpc_stream_refcount* refcount, const char* reason);
68 #define GRPC_STREAM_REF_INIT(rc, ir, cb, cb_arg, objtype) \
69 grpc_stream_ref_init(rc, ir, cb, cb_arg, objtype)
71 void grpc_stream_ref_init(grpc_stream_refcount* refcount, int initial_refs,
72 grpc_iomgr_cb_func cb, void* cb_arg);
73 void grpc_stream_ref(grpc_stream_refcount* refcount);
74 void grpc_stream_unref(grpc_stream_refcount* refcount);
75 #define GRPC_STREAM_REF_INIT(rc, ir, cb, cb_arg, objtype) \
76 grpc_stream_ref_init(rc, ir, cb, cb_arg)
79 /* Wrap a buffer that is owned by some stream object into a slice that shares
81 grpc_slice grpc_slice_from_stream_owned_buffer(grpc_stream_refcount* refcount,
82 void* buffer, size_t length);
84 struct grpc_transport_one_way_stats {
85 uint64_t framing_bytes = 0;
86 uint64_t data_bytes = 0;
87 uint64_t header_bytes = 0;
90 struct grpc_transport_stream_stats {
91 grpc_transport_one_way_stats incoming;
92 grpc_transport_one_way_stats outgoing;
95 void grpc_transport_move_one_way_stats(grpc_transport_one_way_stats* from,
96 grpc_transport_one_way_stats* to);
98 void grpc_transport_move_stats(grpc_transport_stream_stats* from,
99 grpc_transport_stream_stats* to);
101 // This struct (which is present in both grpc_transport_stream_op_batch
102 // and grpc_transport_op_batch) is a convenience to allow filters or
103 // transports to schedule a closure related to a particular batch without
104 // having to allocate memory. The general pattern is to initialize the
105 // closure with the callback arg set to the batch and extra_arg set to
106 // whatever state is associated with the handler (e.g., the call element
107 // or the transport stream object).
109 // Note that this can only be used by the current handler of a given
110 // batch on the way down the stack (i.e., whichever filter or transport is
111 // currently handling the batch). Once a filter or transport passes control
112 // of the batch to the next handler, it cannot depend on the contents of
113 // this struct anymore, because the next handler may reuse it.
114 struct grpc_handler_private_op_data {
115 void* extra_arg = nullptr;
116 grpc_closure closure;
117 grpc_handler_private_op_data() { memset(&closure, 0, sizeof(closure)); }
120 typedef struct grpc_transport_stream_op_batch_payload
121 grpc_transport_stream_op_batch_payload;
123 /* Transport stream op: a set of operations to perform on a transport
124 against a single stream */
125 struct grpc_transport_stream_op_batch {
126 grpc_transport_stream_op_batch()
127 : send_initial_metadata(false),
128 send_trailing_metadata(false),
130 recv_initial_metadata(false),
132 recv_trailing_metadata(false),
133 cancel_stream(false),
136 /** Should be scheduled when all of the non-recv operations in the batch
139 The recv ops (recv_initial_metadata, recv_message, and
140 recv_trailing_metadata) each have their own callbacks. If a batch
141 contains both recv ops and non-recv ops, on_complete should be
142 scheduled as soon as the non-recv ops are complete, regardless of
143 whether or not the recv ops are complete. If a batch contains
144 only recv ops, on_complete can be null. */
145 grpc_closure* on_complete = nullptr;
147 /** Values for the stream op (fields set are determined by flags above) */
148 grpc_transport_stream_op_batch_payload* payload = nullptr;
150 /** Send initial metadata to the peer, from the provided metadata batch. */
151 bool send_initial_metadata : 1;
153 /** Send trailing metadata to the peer, from the provided metadata batch. */
154 bool send_trailing_metadata : 1;
156 /** Send message data to the peer, from the provided byte stream. */
157 bool send_message : 1;
159 /** Receive initial metadata from the stream, into provided metadata batch. */
160 bool recv_initial_metadata : 1;
162 /** Receive message data from the stream, into provided byte stream. */
163 bool recv_message : 1;
165 /** Receive trailing metadata from the stream, into provided metadata batch.
167 bool recv_trailing_metadata : 1;
169 /** Cancel this stream with the provided error */
170 bool cancel_stream : 1;
172 /** Is this stream traced */
175 /***************************************************************************
176 * remaining fields are initialized and used at the discretion of the
177 * current handler of the op */
179 grpc_handler_private_op_data handler_private;
182 struct grpc_transport_stream_op_batch_payload {
183 explicit grpc_transport_stream_op_batch_payload(
184 grpc_call_context_element* context)
185 : context(context) {}
186 ~grpc_transport_stream_op_batch_payload() {
187 // We don't really own `send_message`, so release ownership and let the
188 // owner clean the data.
189 send_message.send_message.release();
193 grpc_metadata_batch* send_initial_metadata = nullptr;
194 /** Iff send_initial_metadata != NULL, flags associated with
195 send_initial_metadata: a bitfield of GRPC_INITIAL_METADATA_xxx */
196 uint32_t send_initial_metadata_flags = 0;
197 // If non-NULL, will be set by the transport to the peer string (a char*).
198 // The transport retains ownership of the string.
199 // Note: This pointer may be used by the transport after the
200 // send_initial_metadata op is completed. It must remain valid
201 // until the call is destroyed.
202 gpr_atm* peer_string = nullptr;
203 } send_initial_metadata;
206 grpc_metadata_batch* send_trailing_metadata = nullptr;
207 } send_trailing_metadata;
210 // The transport (or a filter that decides to return a failure before
211 // the op gets down to the transport) takes ownership.
212 // The batch's on_complete will not be called until after the byte
213 // stream is orphaned.
214 grpc_core::OrphanablePtr<grpc_core::ByteStream> send_message;
218 grpc_metadata_batch* recv_initial_metadata = nullptr;
219 // Flags are used only on the server side. If non-null, will be set to
220 // a bitfield of the GRPC_INITIAL_METADATA_xxx macros (e.g., to
221 // indicate if the call is idempotent).
222 uint32_t* recv_flags = nullptr;
223 /** Should be enqueued when initial metadata is ready to be processed. */
224 grpc_closure* recv_initial_metadata_ready = nullptr;
225 // If not NULL, will be set to true if trailing metadata is
226 // immediately available. This may be a signal that we received a
227 // Trailers-Only response.
228 bool* trailing_metadata_available = nullptr;
229 // If non-NULL, will be set by the transport to the peer string (a char*).
230 // The transport retains ownership of the string.
231 // Note: This pointer may be used by the transport after the
232 // recv_initial_metadata op is completed. It must remain valid
233 // until the call is destroyed.
234 gpr_atm* peer_string = nullptr;
235 } recv_initial_metadata;
238 // Will be set by the transport to point to the byte stream
239 // containing a received message.
240 // Will be NULL if trailing metadata is received instead of a message.
241 grpc_core::OrphanablePtr<grpc_core::ByteStream>* recv_message = nullptr;
242 /** Should be enqueued when one message is ready to be processed. */
243 grpc_closure* recv_message_ready = nullptr;
247 grpc_metadata_batch* recv_trailing_metadata = nullptr;
248 grpc_transport_stream_stats* collect_stats = nullptr;
249 /** Should be enqueued when initial metadata is ready to be processed. */
250 grpc_closure* recv_trailing_metadata_ready = nullptr;
251 } recv_trailing_metadata;
253 /** Forcefully close this stream.
254 The HTTP2 semantics should be:
255 - server side: if cancel_error has GRPC_ERROR_INT_GRPC_STATUS, and
256 trailing metadata has not been sent, send trailing metadata with status
257 and message from cancel_error (use grpc_error_get_status) followed by
258 a RST_STREAM with error=GRPC_CHTTP2_NO_ERROR to force a full close
259 - at all other times: use grpc_error_get_status to get a status code, and
260 convert to a HTTP2 error code using
261 grpc_chttp2_grpc_status_to_http2_error. Send a RST_STREAM with this
264 // Error contract: the transport that gets this op must cause cancel_error
265 // to be unref'ed after processing it
266 grpc_error* cancel_error = GRPC_ERROR_NONE;
269 /* Indexes correspond to grpc_context_index enum values */
270 grpc_call_context_element* context;
273 /** Transport op: a set of operations to perform on a transport as a whole */
274 typedef struct grpc_transport_op {
275 /** Called when processing of this op is done. */
276 grpc_closure* on_consumed = nullptr;
277 /** connectivity monitoring - set connectivity_state to NULL to unsubscribe */
278 grpc_closure* on_connectivity_state_change = nullptr;
279 grpc_connectivity_state* connectivity_state = nullptr;
280 /** should the transport be disconnected
281 * Error contract: the transport that gets this op must cause
282 * disconnect_with_error to be unref'ed after processing it */
283 grpc_error* disconnect_with_error = nullptr;
284 /** what should the goaway contain?
285 * Error contract: the transport that gets this op must cause
286 * goaway_error to be unref'ed after processing it */
287 grpc_error* goaway_error = nullptr;
288 /** set the callback for accepting new streams;
289 this is a permanent callback, unlike the other one-shot closures.
290 If true, the callback is set to set_accept_stream_fn, with its
291 user_data argument set to set_accept_stream_user_data */
292 bool set_accept_stream = false;
293 void (*set_accept_stream_fn)(void* user_data, grpc_transport* transport,
294 const void* server_data) = nullptr;
295 void* set_accept_stream_user_data = nullptr;
296 /** add this transport to a pollset */
297 grpc_pollset* bind_pollset = nullptr;
298 /** add this transport to a pollset_set */
299 grpc_pollset_set* bind_pollset_set = nullptr;
300 /** send a ping, if either on_initiate or on_ack is not NULL */
302 /** Ping may be delayed by the transport, on_initiate callback will be
303 called when the ping is actually being sent. */
304 grpc_closure* on_initiate = nullptr;
305 /** Called when the ping ack is received */
306 grpc_closure* on_ack = nullptr;
308 // If true, will reset the channel's connection backoff.
309 bool reset_connect_backoff = false;
311 /***************************************************************************
312 * remaining fields are initialized and used at the discretion of the
313 * transport implementation */
315 grpc_handler_private_op_data handler_private;
318 /* Returns the amount of memory required to store a grpc_stream for this
320 size_t grpc_transport_stream_size(grpc_transport* transport);
322 /* Initialize transport data for a stream.
324 Returns 0 on success, any other (transport-defined) value for failure.
325 May assume that stream contains all-zeros.
328 transport - the transport on which to create this stream
329 stream - a pointer to uninitialized memory to initialize
330 server_data - either NULL for a client initiated stream, or a pointer
331 supplied from the accept_stream callback function */
332 int grpc_transport_init_stream(grpc_transport* transport, grpc_stream* stream,
333 grpc_stream_refcount* refcount,
334 const void* server_data, gpr_arena* arena);
336 void grpc_transport_set_pops(grpc_transport* transport, grpc_stream* stream,
337 grpc_polling_entity* pollent);
339 /* Destroy transport data for a stream.
341 Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been
342 received by the up-layer. Must not be called in the same call stack as
346 transport - the transport on which to create this stream
347 stream - the grpc_stream to destroy (memory is still owned by the
348 caller, but any child memory must be cleaned up) */
349 void grpc_transport_destroy_stream(grpc_transport* transport,
351 grpc_closure* then_schedule_closure);
353 void grpc_transport_stream_op_batch_finish_with_failure(
354 grpc_transport_stream_op_batch* op, grpc_error* error,
355 grpc_call_combiner* call_combiner);
357 char* grpc_transport_stream_op_batch_string(grpc_transport_stream_op_batch* op);
358 char* grpc_transport_op_string(grpc_transport_op* op);
360 /* Send a batch of operations on a transport
362 Takes ownership of any objects contained in ops.
365 transport - the transport on which to initiate the stream
366 stream - the stream on which to send the operations. This must be
367 non-NULL and previously initialized by the same transport.
368 op - a grpc_transport_stream_op_batch specifying the op to perform
370 void grpc_transport_perform_stream_op(grpc_transport* transport,
372 grpc_transport_stream_op_batch* op);
374 void grpc_transport_perform_op(grpc_transport* transport,
375 grpc_transport_op* op);
377 /* Send a ping on a transport
379 Calls cb with user data when a response is received. */
380 void grpc_transport_ping(grpc_transport* transport, grpc_closure* cb);
382 /* Advise peer of pending connection termination. */
383 void grpc_transport_goaway(grpc_transport* transport, grpc_status_code status,
384 grpc_slice debug_data);
386 /* Destroy the transport */
387 void grpc_transport_destroy(grpc_transport* transport);
389 /* Get the endpoint used by \a transport */
390 grpc_endpoint* grpc_transport_get_endpoint(grpc_transport* transport);
392 /* Allocate a grpc_transport_op, and preconfigure the on_consumed closure to
393 \a on_consumed and then delete the returned transport op */
394 grpc_transport_op* grpc_make_transport_op(grpc_closure* on_consumed);
395 /* Allocate a grpc_transport_stream_op_batch, and preconfigure the on_consumed
397 to \a on_consumed and then delete the returned transport op */
398 grpc_transport_stream_op_batch* grpc_make_transport_stream_op(
399 grpc_closure* on_consumed);
401 #endif /* GRPC_CORE_LIB_TRANSPORT_TRANSPORT_H */