blob: e56bf2780abb54e892c0c6f52b58a6906c961878 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_TRANSPORT_TRANSPORT_H
35#define GRPC_CORE_LIB_TRANSPORT_TRANSPORT_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#include <stddef.h>
38
Craig Tiller9533d042016-03-25 17:11:06 -070039#include "src/core/lib/channel/context.h"
Yuchen Zeng5ab4ca52016-10-24 10:49:55 -070040#include "src/core/lib/iomgr/endpoint.h"
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070041#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070042#include "src/core/lib/iomgr/pollset.h"
43#include "src/core/lib/iomgr/pollset_set.h"
44#include "src/core/lib/transport/byte_stream.h"
45#include "src/core/lib/transport/metadata_batch.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
Mark D. Roth035cb3a2016-06-16 14:52:41 -070047#ifdef __cplusplus
48extern "C" {
49#endif
50
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051/* forward declarations */
52typedef struct grpc_transport grpc_transport;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053
54/* grpc_stream doesn't actually exist. It's used as a typesafe
55 opaque pointer for whatever data the transport wants to track
56 for a stream. */
57typedef struct grpc_stream grpc_stream;
58
Craig Tillerf0e11192016-08-19 11:32:28 -070059//#define GRPC_STREAM_REFCOUNT_DEBUG
Craig Tiller48613042015-11-29 14:45:11 -080060
Craig Tiller9d35a1f2015-11-02 14:16:12 -080061typedef struct grpc_stream_refcount {
62 gpr_refcount refs;
63 grpc_closure destroy;
Craig Tiller27e5aa42015-11-24 16:28:54 -080064#ifdef GRPC_STREAM_REFCOUNT_DEBUG
65 const char *object_type;
66#endif
Craig Tiller9d35a1f2015-11-02 14:16:12 -080067} grpc_stream_refcount;
68
Craig Tiller9d35a1f2015-11-02 14:16:12 -080069#ifdef GRPC_STREAM_REFCOUNT_DEBUG
Craig Tiller27e5aa42015-11-24 16:28:54 -080070void grpc_stream_ref_init(grpc_stream_refcount *refcount, int initial_refs,
71 grpc_iomgr_cb_func cb, void *cb_arg,
72 const char *object_type);
Craig Tiller9d35a1f2015-11-02 14:16:12 -080073void grpc_stream_ref(grpc_stream_refcount *refcount, const char *reason);
74void grpc_stream_unref(grpc_exec_ctx *exec_ctx, grpc_stream_refcount *refcount,
75 const char *reason);
Craig Tiller27e5aa42015-11-24 16:28:54 -080076#define GRPC_STREAM_REF_INIT(rc, ir, cb, cb_arg, objtype) \
77 grpc_stream_ref_init(rc, ir, cb, cb_arg, objtype)
Craig Tiller9d35a1f2015-11-02 14:16:12 -080078#else
Craig Tiller27e5aa42015-11-24 16:28:54 -080079void grpc_stream_ref_init(grpc_stream_refcount *refcount, int initial_refs,
80 grpc_iomgr_cb_func cb, void *cb_arg);
Craig Tiller9d35a1f2015-11-02 14:16:12 -080081void grpc_stream_ref(grpc_stream_refcount *refcount);
82void grpc_stream_unref(grpc_exec_ctx *exec_ctx, grpc_stream_refcount *refcount);
Craig Tiller27e5aa42015-11-24 16:28:54 -080083#define GRPC_STREAM_REF_INIT(rc, ir, cb, cb_arg, objtype) \
84 grpc_stream_ref_init(rc, ir, cb, cb_arg)
Craig Tiller9d35a1f2015-11-02 14:16:12 -080085#endif
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086
Craig Tiller466129e2016-03-09 14:43:18 -080087typedef struct {
88 uint64_t framing_bytes;
89 uint64_t data_bytes;
90 uint64_t header_bytes;
91} grpc_transport_one_way_stats;
92
93typedef struct grpc_transport_stream_stats {
94 grpc_transport_one_way_stats incoming;
95 grpc_transport_one_way_stats outgoing;
96} grpc_transport_stream_stats;
97
98void grpc_transport_move_one_way_stats(grpc_transport_one_way_stats *from,
99 grpc_transport_one_way_stats *to);
100
101void grpc_transport_move_stats(grpc_transport_stream_stats *from,
102 grpc_transport_stream_stats *to);
103
Craig Tiller93023e42016-07-11 12:43:23 -0700104typedef struct {
105 grpc_closure closure;
106 void *args[2];
107} grpc_transport_private_op_data;
108
Craig Tiller3f475422015-06-25 10:43:05 -0700109/* Transport stream op: a set of operations to perform on a transport
110 against a single stream */
Craig Tillera82950e2015-09-22 12:33:20 -0700111typedef struct grpc_transport_stream_op {
Craig Tillera67a83e2016-03-22 21:43:59 -0700112 /** Should be enqueued when all requested operations (excluding recv_message
113 and recv_initial_metadata which have their own closures) in a given batch
114 have been completed. */
115 grpc_closure *on_complete;
116
Craig Tillera7cd41c2016-08-31 12:59:24 -0700117 /** Is the completion of this op covered by a poller (if false: the op should
118 complete independently of some pollset being polled) */
119 bool covered_by_poller;
120
Craig Tillerc6549762016-03-09 17:10:43 -0800121 /** Send initial metadata to the peer, from the provided metadata batch.
122 idempotent_request MUST be set if this is non-null */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800123 grpc_metadata_batch *send_initial_metadata;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800124 /** Iff send_initial_metadata != NULL, flags associated with
125 send_initial_metadata: a bitfield of GRPC_INITIAL_METADATA_xxx */
126 uint32_t send_initial_metadata_flags;
Robbie Shade9891e8f2016-01-25 13:19:32 -0500127
128 /** Send trailing metadata to the peer, from the provided metadata batch. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800129 grpc_metadata_batch *send_trailing_metadata;
Craig Tiller5dde66e2015-06-02 09:05:23 -0700130
Robbie Shade9891e8f2016-01-25 13:19:32 -0500131 /** Send message data to the peer, from the provided byte stream. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800132 grpc_byte_stream *send_message;
Craig Tiller3f2c2212015-04-23 07:56:33 -0700133
Robbie Shade9891e8f2016-01-25 13:19:32 -0500134 /** Receive initial metadata from the stream, into provided metadata batch. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800135 grpc_metadata_batch *recv_initial_metadata;
Craig Tiller4d40ba32016-03-09 17:48:40 -0800136 bool *recv_idempotent_request;
Makarand Dharmapurikard322d4b2016-08-29 12:54:19 -0700137 bool *recv_cacheable_request;
Craig Tillera44cbfc2016-02-03 16:02:49 -0800138 /** Should be enqueued when initial metadata is ready to be processed. */
139 grpc_closure *recv_initial_metadata_ready;
Robbie Shade9891e8f2016-01-25 13:19:32 -0500140
141 /** Receive message data from the stream, into provided byte stream. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800142 grpc_byte_stream **recv_message;
Robbie Shade9891e8f2016-01-25 13:19:32 -0500143 /** Should be enqueued when one message is ready to be processed. */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800144 grpc_closure *recv_message_ready;
Robbie Shade9891e8f2016-01-25 13:19:32 -0500145
Robbie Shadecdc182e2016-01-29 09:39:57 -0500146 /** Receive trailing metadata from the stream, into provided metadata batch.
147 */
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800148 grpc_metadata_batch *recv_trailing_metadata;
Craig Tiller3f2c2212015-04-23 07:56:33 -0700149
Craig Tiller466129e2016-03-09 14:43:18 -0800150 /** Collect any stats into provided buffer, zero internal stat counters */
151 grpc_transport_stream_stats *collect_stats;
152
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800153 /** If != GRPC_ERROR_NONE, forcefully close this stream.
154 The HTTP2 semantics should be:
155 - server side: if cancel_error has GRPC_ERROR_INT_GRPC_STATUS, and
156 trailing metadata has not been sent, send trailing metadata with status
157 and message from cancel_error (use grpc_error_get_status) followed by
158 a RST_STREAM with error=GRPC_CHTTP2_NO_ERROR to force a full close
159 - at all other times: use grpc_error_get_status to get a status code, and
160 convert to a HTTP2 error code using
161 grpc_chttp2_grpc_status_to_http2_error. Send a RST_STREAM with this
162 error. */
Craig Tillerf0f70a82016-06-23 13:55:06 -0700163 grpc_error *cancel_error;
Craig Tiller935cf422015-05-01 14:10:46 -0700164
165 /* Indexes correspond to grpc_context_index enum values */
Julien Boeuf83b02972015-05-20 22:50:34 -0700166 grpc_call_context_element *context;
Craig Tiller93023e42016-07-11 12:43:23 -0700167
168 /***************************************************************************
169 * remaining fields are initialized and used at the discretion of the
Craig Tiller4a84bdd2017-02-14 09:48:41 -0800170 * current handler of the op */
Craig Tiller93023e42016-07-11 12:43:23 -0700171
Craig Tiller4a84bdd2017-02-14 09:48:41 -0800172 grpc_transport_private_op_data handler_private;
Craig Tillerb7959a02015-06-25 08:50:54 -0700173} grpc_transport_stream_op;
Craig Tiller3f2c2212015-04-23 07:56:33 -0700174
Craig Tiller3f475422015-06-25 10:43:05 -0700175/** Transport op: a set of operations to perform on a transport as a whole */
Craig Tillera82950e2015-09-22 12:33:20 -0700176typedef struct grpc_transport_op {
Craig Tiller1bd9ea42016-03-03 21:09:31 -0800177 /** Called when processing of this op is done. */
Craig Tiller33825112015-09-18 07:44:19 -0700178 grpc_closure *on_consumed;
Craig Tillerb5585d42015-11-17 07:18:31 -0800179 /** connectivity monitoring - set connectivity_state to NULL to unsubscribe */
Craig Tiller33825112015-09-18 07:44:19 -0700180 grpc_closure *on_connectivity_state_change;
Craig Tiller3f475422015-06-25 10:43:05 -0700181 grpc_connectivity_state *connectivity_state;
182 /** should the transport be disconnected */
Craig Tiller804ff712016-05-05 16:25:40 -0700183 grpc_error *disconnect_with_error;
Craig Tiller3f475422015-06-25 10:43:05 -0700184 /** what should the goaway contain? */
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800185 grpc_error *goaway_error;
Craig Tiller3f475422015-06-25 10:43:05 -0700186 /** set the callback for accepting new streams;
Craig Tiller389297d2016-03-03 21:02:54 -0800187 this is a permanent callback, unlike the other one-shot closures.
188 If true, the callback is set to set_accept_stream_fn, with its
189 user_data argument set to set_accept_stream_user_data */
Craig Tillerd7f12e32016-03-03 10:08:31 -0800190 bool set_accept_stream;
191 void (*set_accept_stream_fn)(grpc_exec_ctx *exec_ctx, void *user_data,
192 grpc_transport *transport,
193 const void *server_data);
Craig Tiller3f475422015-06-25 10:43:05 -0700194 void *set_accept_stream_user_data;
195 /** add this transport to a pollset */
196 grpc_pollset *bind_pollset;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700197 /** add this transport to a pollset_set */
198 grpc_pollset_set *bind_pollset_set;
Craig Tiller3f475422015-06-25 10:43:05 -0700199 /** send a ping, call this back if not NULL */
Craig Tiller33825112015-09-18 07:44:19 -0700200 grpc_closure *send_ping;
Craig Tiller93023e42016-07-11 12:43:23 -0700201
202 /***************************************************************************
203 * remaining fields are initialized and used at the discretion of the
204 * transport implementation */
205
206 grpc_transport_private_op_data transport_private;
Craig Tiller3f475422015-06-25 10:43:05 -0700207} grpc_transport_op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800208
209/* Returns the amount of memory required to store a grpc_stream for this
210 transport */
Craig Tillera82950e2015-09-22 12:33:20 -0700211size_t grpc_transport_stream_size(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800212
213/* Initialize transport data for a stream.
214
215 Returns 0 on success, any other (transport-defined) value for failure.
216
217 Arguments:
218 transport - the transport on which to create this stream
219 stream - a pointer to uninitialized memory to initialize
220 server_data - either NULL for a client initiated stream, or a pointer
221 supplied from the accept_stream callback function */
Craig Tillera82950e2015-09-22 12:33:20 -0700222int grpc_transport_init_stream(grpc_exec_ctx *exec_ctx,
223 grpc_transport *transport, grpc_stream *stream,
Craig Tiller9d35a1f2015-11-02 14:16:12 -0800224 grpc_stream_refcount *refcount,
225 const void *server_data);
226
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700227void grpc_transport_set_pops(grpc_exec_ctx *exec_ctx, grpc_transport *transport,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700228 grpc_stream *stream, grpc_polling_entity *pollent);
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700229
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800230/* Destroy transport data for a stream.
231
232 Requires: a recv_batch with final_state == GRPC_STREAM_CLOSED has been
233 received by the up-layer. Must not be called in the same call stack as
234 recv_frame.
235
236 Arguments:
237 transport - the transport on which to create this stream
238 stream - the grpc_stream to destroy (memory is still owned by the
239 caller, but any child memory must be cleaned up) */
Craig Tillera82950e2015-09-22 12:33:20 -0700240void grpc_transport_destroy_stream(grpc_exec_ctx *exec_ctx,
241 grpc_transport *transport,
Craig Tiller2c8063c2016-03-22 22:12:15 -0700242 grpc_stream *stream, void *and_free_memory);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800243
Craig Tillera82950e2015-09-22 12:33:20 -0700244void grpc_transport_stream_op_finish_with_failure(grpc_exec_ctx *exec_ctx,
Craig Tillerf51457b2016-05-03 17:06:32 -0700245 grpc_transport_stream_op *op,
246 grpc_error *error);
Craig Tiller83f88d92015-04-21 16:02:05 -0700247
Craig Tillera82950e2015-09-22 12:33:20 -0700248char *grpc_transport_stream_op_string(grpc_transport_stream_op *op);
Craig Tillere194ff02016-08-29 15:48:41 -0700249char *grpc_transport_op_string(grpc_transport_op *op);
Craig Tiller83f88d92015-04-21 16:02:05 -0700250
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800251/* Send a batch of operations on a transport
252
253 Takes ownership of any objects contained in ops.
254
255 Arguments:
256 transport - the transport on which to initiate the stream
257 stream - the stream on which to send the operations. This must be
258 non-NULL and previously initialized by the same transport.
Craig Tillerb7959a02015-06-25 08:50:54 -0700259 op - a grpc_transport_stream_op specifying the op to perform */
Craig Tillera82950e2015-09-22 12:33:20 -0700260void grpc_transport_perform_stream_op(grpc_exec_ctx *exec_ctx,
261 grpc_transport *transport,
262 grpc_stream *stream,
263 grpc_transport_stream_op *op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800264
Craig Tillera82950e2015-09-22 12:33:20 -0700265void grpc_transport_perform_op(grpc_exec_ctx *exec_ctx,
266 grpc_transport *transport,
267 grpc_transport_op *op);
Craig Tiller3f475422015-06-25 10:43:05 -0700268
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800269/* Send a ping on a transport
270
Craig Tiller4aa71a12015-06-15 13:00:55 -0700271 Calls cb with user data when a response is received. */
Craig Tillera82950e2015-09-22 12:33:20 -0700272void grpc_transport_ping(grpc_transport *transport, grpc_closure *cb);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800273
nnoble0c475f02014-12-05 15:37:39 -0800274/* Advise peer of pending connection termination. */
Craig Tillera82950e2015-09-22 12:33:20 -0700275void grpc_transport_goaway(grpc_transport *transport, grpc_status_code status,
Craig Tillerd41a4a72016-10-26 16:16:06 -0700276 grpc_slice debug_data);
nnoble0c475f02014-12-05 15:37:39 -0800277
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800278/* Close a transport. Aborts all open streams. */
Craig Tillera82950e2015-09-22 12:33:20 -0700279void grpc_transport_close(grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800280
281/* Destroy the transport */
Craig Tillera82950e2015-09-22 12:33:20 -0700282void grpc_transport_destroy(grpc_exec_ctx *exec_ctx, grpc_transport *transport);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800283
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700284/* Get the transports peer */
Craig Tillera82950e2015-09-22 12:33:20 -0700285char *grpc_transport_get_peer(grpc_exec_ctx *exec_ctx,
286 grpc_transport *transport);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700287
Yuchen Zeng5ab4ca52016-10-24 10:49:55 -0700288/* Get the endpoint used by \a transport */
289grpc_endpoint *grpc_transport_get_endpoint(grpc_exec_ctx *exec_ctx,
290 grpc_transport *transport);
291
Craig Tillere0221ff2016-07-11 15:56:08 -0700292/* Allocate a grpc_transport_op, and preconfigure the on_consumed closure to
293 \a on_consumed and then delete the returned transport op */
294grpc_transport_op *grpc_make_transport_op(grpc_closure *on_consumed);
Craig Tillerdfd3a8f2016-08-24 09:43:45 -0700295/* Allocate a grpc_transport_stream_op, and preconfigure the on_consumed closure
296 to \a on_consumed and then delete the returned transport op */
297grpc_transport_stream_op *grpc_make_transport_stream_op(
298 grpc_closure *on_consumed);
Craig Tillere0221ff2016-07-11 15:56:08 -0700299
Mark D. Roth035cb3a2016-06-16 14:52:41 -0700300#ifdef __cplusplus
301}
302#endif
303
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700304#endif /* GRPC_CORE_LIB_TRANSPORT_TRANSPORT_H */