blob: 02c94744ee2db716d3d696590d5471f7e3314057 [file] [log] [blame]
Craig Tiller640d3bd2015-06-12 07:51:51 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * 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 Tiller9b8671c2015-06-12 07:41:54 -070034#ifndef GRPC_INTERNAL_CORE_CHTTP2_INTERNAL_H
35#define GRPC_INTERNAL_CORE_CHTTP2_INTERNAL_H
36
37#include "src/core/transport/transport_impl.h"
Craig Tiller3208e392015-06-12 08:17:02 -070038#include "src/core/iomgr/endpoint.h"
Craig Tillerd20efd22015-06-12 16:17:09 -070039#include "src/core/transport/chttp2/frame.h"
Craig Tiller3208e392015-06-12 08:17:02 -070040#include "src/core/transport/chttp2/frame_data.h"
41#include "src/core/transport/chttp2/frame_goaway.h"
42#include "src/core/transport/chttp2/frame_ping.h"
43#include "src/core/transport/chttp2/frame_rst_stream.h"
44#include "src/core/transport/chttp2/frame_settings.h"
45#include "src/core/transport/chttp2/frame_window_update.h"
Craig Tiller3208e392015-06-12 08:17:02 -070046#include "src/core/transport/chttp2/hpack_parser.h"
Craig Tiller5dc3b302015-06-15 16:06:50 -070047#include "src/core/transport/chttp2/incoming_metadata.h"
Craig Tiller3208e392015-06-12 08:17:02 -070048#include "src/core/transport/chttp2/stream_encoder.h"
Craig Tiller5dc3b302015-06-15 16:06:50 -070049#include "src/core/transport/chttp2/stream_map.h"
Craig Tiller9b8671c2015-06-12 07:41:54 -070050
Craig Tillerb084d902015-06-12 07:50:02 -070051typedef struct grpc_chttp2_transport grpc_chttp2_transport;
52typedef struct grpc_chttp2_stream grpc_chttp2_stream;
Craig Tiller9b8671c2015-06-12 07:41:54 -070053
54/* streams are kept in various linked lists depending on what things need to
55 happen to them... this enum labels each list */
56typedef enum {
Craig Tiller5dc3b302015-06-15 16:06:50 -070057 GRPC_CHTTP2_LIST_ALL_STREAMS,
Craig Tiller6459db42015-06-15 17:11:45 -070058 GRPC_CHTTP2_LIST_READ_WRITE_STATE_CHANGED,
Craig Tiller5dc3b302015-06-15 16:06:50 -070059 GRPC_CHTTP2_LIST_WRITABLE,
Craig Tiller6459db42015-06-15 17:11:45 -070060 GRPC_CHTTP2_LIST_WRITING,
61 GRPC_CHTTP2_LIST_WRITTEN,
62 GRPC_CHTTP2_LIST_WRITABLE_WINDOW_UPDATE,
63 GRPC_CHTTP2_LIST_PARSING_SEEN,
Craig Tiller83fb0702015-06-16 21:13:07 -070064 GRPC_CHTTP2_LIST_CLOSED_WAITING_FOR_PARSING,
Craig Tillercf1e3192015-06-16 14:28:22 -070065 GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED,
Craig Tiller5dc3b302015-06-15 16:06:50 -070066 /** streams that are waiting to start because there are too many concurrent
67 streams on the connection */
68 GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY,
Craig Tiller9b8671c2015-06-12 07:41:54 -070069 STREAM_LIST_COUNT /* must be last */
Craig Tillerb084d902015-06-12 07:50:02 -070070} grpc_chttp2_stream_list_id;
Craig Tiller9b8671c2015-06-12 07:41:54 -070071
72/* deframer state for the overall http2 stream of bytes */
73typedef enum {
74 /* prefix: one entry per http2 connection prefix byte */
Craig Tillerab630732015-06-25 11:20:01 -070075 GRPC_DTS_CLIENT_PREFIX_0 = 0,
76 GRPC_DTS_CLIENT_PREFIX_1,
77 GRPC_DTS_CLIENT_PREFIX_2,
78 GRPC_DTS_CLIENT_PREFIX_3,
79 GRPC_DTS_CLIENT_PREFIX_4,
80 GRPC_DTS_CLIENT_PREFIX_5,
81 GRPC_DTS_CLIENT_PREFIX_6,
82 GRPC_DTS_CLIENT_PREFIX_7,
83 GRPC_DTS_CLIENT_PREFIX_8,
84 GRPC_DTS_CLIENT_PREFIX_9,
85 GRPC_DTS_CLIENT_PREFIX_10,
86 GRPC_DTS_CLIENT_PREFIX_11,
87 GRPC_DTS_CLIENT_PREFIX_12,
88 GRPC_DTS_CLIENT_PREFIX_13,
89 GRPC_DTS_CLIENT_PREFIX_14,
90 GRPC_DTS_CLIENT_PREFIX_15,
91 GRPC_DTS_CLIENT_PREFIX_16,
92 GRPC_DTS_CLIENT_PREFIX_17,
93 GRPC_DTS_CLIENT_PREFIX_18,
94 GRPC_DTS_CLIENT_PREFIX_19,
95 GRPC_DTS_CLIENT_PREFIX_20,
96 GRPC_DTS_CLIENT_PREFIX_21,
97 GRPC_DTS_CLIENT_PREFIX_22,
98 GRPC_DTS_CLIENT_PREFIX_23,
Craig Tiller9b8671c2015-06-12 07:41:54 -070099 /* frame header byte 0... */
100 /* must follow from the prefix states */
Craig Tillerab630732015-06-25 11:20:01 -0700101 GRPC_DTS_FH_0,
102 GRPC_DTS_FH_1,
103 GRPC_DTS_FH_2,
104 GRPC_DTS_FH_3,
105 GRPC_DTS_FH_4,
106 GRPC_DTS_FH_5,
107 GRPC_DTS_FH_6,
108 GRPC_DTS_FH_7,
Craig Tiller9b8671c2015-06-12 07:41:54 -0700109 /* ... frame header byte 8 */
Craig Tillerab630732015-06-25 11:20:01 -0700110 GRPC_DTS_FH_8,
Craig Tiller9b8671c2015-06-12 07:41:54 -0700111 /* inside a http2 frame */
Craig Tillerab630732015-06-25 11:20:01 -0700112 GRPC_DTS_FRAME
Craig Tillerb084d902015-06-12 07:50:02 -0700113} grpc_chttp2_deframe_transport_state;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700114
115typedef enum {
Craig Tillerab630732015-06-25 11:20:01 -0700116 GRPC_WRITE_STATE_OPEN,
117 GRPC_WRITE_STATE_QUEUED_CLOSE,
118 GRPC_WRITE_STATE_SENT_CLOSE
Craig Tillerb084d902015-06-12 07:50:02 -0700119} grpc_chttp2_write_state;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700120
121typedef enum {
Craig Tillerab630732015-06-25 11:20:01 -0700122 GRPC_DONT_SEND_CLOSED = 0,
123 GRPC_SEND_CLOSED,
124 GRPC_SEND_CLOSED_WITH_RST_STREAM
Craig Tillerb084d902015-06-12 07:50:02 -0700125} grpc_chttp2_send_closed;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700126
127typedef struct {
Craig Tillerb084d902015-06-12 07:50:02 -0700128 grpc_chttp2_stream *head;
129 grpc_chttp2_stream *tail;
130} grpc_chttp2_stream_list;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700131
132typedef struct {
Craig Tillerb084d902015-06-12 07:50:02 -0700133 grpc_chttp2_stream *next;
134 grpc_chttp2_stream *prev;
135} grpc_chttp2_stream_link;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700136
137typedef enum {
Craig Tiller606d8742015-06-15 06:58:50 -0700138 GRPC_CHTTP2_ERROR_STATE_NONE,
139 GRPC_CHTTP2_ERROR_STATE_SEEN,
140 GRPC_CHTTP2_ERROR_STATE_NOTIFIED
Craig Tillerb084d902015-06-12 07:50:02 -0700141} grpc_chttp2_error_state;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700142
143/* We keep several sets of connection wide parameters */
144typedef enum {
145 /* The settings our peer has asked for (and we have acked) */
Craig Tillerab630732015-06-25 11:20:01 -0700146 GRPC_PEER_SETTINGS = 0,
Craig Tiller9b8671c2015-06-12 07:41:54 -0700147 /* The settings we'd like to have */
Craig Tillerab630732015-06-25 11:20:01 -0700148 GRPC_LOCAL_SETTINGS,
Craig Tiller9b8671c2015-06-12 07:41:54 -0700149 /* The settings we've published to our peer */
Craig Tillerab630732015-06-25 11:20:01 -0700150 GRPC_SENT_SETTINGS,
Craig Tiller9b8671c2015-06-12 07:41:54 -0700151 /* The settings the peer has acked */
Craig Tillerab630732015-06-25 11:20:01 -0700152 GRPC_ACKED_SETTINGS,
153 GRPC_NUM_SETTING_SETS
Craig Tillerb084d902015-06-12 07:50:02 -0700154} grpc_chttp2_setting_set;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700155
156/* Outstanding ping request data */
Craig Tiller3719f072015-06-12 17:19:51 -0700157typedef struct grpc_chttp2_outstanding_ping {
Craig Tiller9b8671c2015-06-12 07:41:54 -0700158 gpr_uint8 id[8];
Craig Tiller3719f072015-06-12 17:19:51 -0700159 grpc_iomgr_closure *on_recv;
160 struct grpc_chttp2_outstanding_ping *next;
161 struct grpc_chttp2_outstanding_ping *prev;
Craig Tillerb084d902015-06-12 07:50:02 -0700162} grpc_chttp2_outstanding_ping;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700163
164typedef struct {
Craig Tillerd20efd22015-06-12 16:17:09 -0700165 /** data to write next write */
166 gpr_slice_buffer qbuf;
167 /** queued callbacks */
168 grpc_iomgr_closure *pending_closures;
169
170 /** window available for us to send to peer */
171 gpr_uint32 outgoing_window;
Craig Tiller3719f072015-06-12 17:19:51 -0700172 /** window available for peer to send to us - updated after parse */
173 gpr_uint32 incoming_window;
Craig Tillerd20efd22015-06-12 16:17:09 -0700174 /** how much window would we like to have for incoming_window */
175 gpr_uint32 connection_window_target;
176
Craig Tiller3719f072015-06-12 17:19:51 -0700177 /** is this transport a client? */
178 gpr_uint8 is_client;
Craig Tillerd20efd22015-06-12 16:17:09 -0700179 /** are the local settings dirty and need to be sent? */
180 gpr_uint8 dirtied_local_settings;
181 /** have local settings been sent? */
182 gpr_uint8 sent_local_settings;
183 /** bitmask of setting indexes to send out */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700184 gpr_uint32 force_send_settings;
Craig Tillerd20efd22015-06-12 16:17:09 -0700185 /** settings values */
Craig Tillerab630732015-06-25 11:20:01 -0700186 gpr_uint32 settings[GRPC_NUM_SETTING_SETS][GRPC_CHTTP2_NUM_SETTINGS];
Craig Tillerd20efd22015-06-12 16:17:09 -0700187
Craig Tiller606d8742015-06-15 06:58:50 -0700188 /** has there been a connection level error, and have we notified
189 anyone about it? */
190 grpc_chttp2_error_state error_state;
191
192 /** what is the next stream id to be allocated by this peer?
193 copied to next_stream_id in parsing when parsing commences */
194 gpr_uint32 next_stream_id;
195
Craig Tillerd20efd22015-06-12 16:17:09 -0700196 /** last received stream id */
197 gpr_uint32 last_incoming_stream_id;
Craig Tiller3719f072015-06-12 17:19:51 -0700198
199 /** pings awaiting responses */
200 grpc_chttp2_outstanding_ping pings;
Craig Tiller606d8742015-06-15 06:58:50 -0700201 /** next payload for an outgoing ping */
202 gpr_uint64 ping_counter;
203
204 /** concurrent stream count: updated when not parsing,
205 so this is a strict over-estimation on the client */
206 gpr_uint32 concurrent_stream_count;
207
Craig Tiller4aa71a12015-06-15 13:00:55 -0700208 /** is there a goaway available? (boolean) */
209 grpc_chttp2_error_state goaway_state;
Craig Tiller606d8742015-06-15 06:58:50 -0700210 /** what is the debug text of the goaway? */
211 gpr_slice goaway_text;
212 /** what is the status code of the goaway? */
213 grpc_status_code goaway_error;
Craig Tillerd20efd22015-06-12 16:17:09 -0700214} grpc_chttp2_transport_global;
215
216typedef struct {
217 /** data to write now */
218 gpr_slice_buffer outbuf;
219 /** hpack encoding */
220 grpc_chttp2_hpack_compressor hpack_compressor;
Craig Tiller285b8822015-06-17 15:58:13 -0700221 /** is this a client? */
222 gpr_uint8 is_client;
Craig Tillerd20efd22015-06-12 16:17:09 -0700223} grpc_chttp2_transport_writing;
224
225struct grpc_chttp2_transport_parsing {
226 /** is this transport a client? (boolean) */
227 gpr_uint8 is_client;
228
229 /** were settings updated? */
230 gpr_uint8 settings_updated;
231 /** was a settings ack received? */
232 gpr_uint8 settings_ack_received;
233 /** was a goaway frame received? */
234 gpr_uint8 goaway_received;
235
Craig Tiller3719f072015-06-12 17:19:51 -0700236 /** initial window change */
237 gpr_int64 initial_window_update;
238
Craig Tillerd20efd22015-06-12 16:17:09 -0700239 /** data to write later - after parsing */
240 gpr_slice_buffer qbuf;
241 /* metadata object cache */
242 grpc_mdstr *str_grpc_timeout;
243 /** parser for headers */
244 grpc_chttp2_hpack_parser hpack_parser;
245 /** simple one shot parsers */
246 union {
247 grpc_chttp2_window_update_parser window_update;
248 grpc_chttp2_settings_parser settings;
249 grpc_chttp2_ping_parser ping;
250 grpc_chttp2_rst_stream_parser rst_stream;
251 } simple;
252 /** parser for goaway frames */
253 grpc_chttp2_goaway_parser goaway_parser;
254
255 /** window available for peer to send to us */
256 gpr_uint32 incoming_window;
Craig Tiller285b8822015-06-17 15:58:13 -0700257 gpr_uint32 incoming_window_delta;
Craig Tillerd20efd22015-06-12 16:17:09 -0700258
259 /** next stream id available at the time of beginning parsing */
260 gpr_uint32 next_stream_id;
261 gpr_uint32 last_incoming_stream_id;
262
263 /* deframing */
264 grpc_chttp2_deframe_transport_state deframe_state;
265 gpr_uint8 incoming_frame_type;
266 gpr_uint8 incoming_frame_flags;
267 gpr_uint8 header_eof;
268 gpr_uint32 expect_continuation_stream_id;
269 gpr_uint32 incoming_frame_size;
270 gpr_uint32 incoming_stream_id;
271
272 /* active parser */
273 void *parser_data;
274 grpc_chttp2_stream_parsing *incoming_stream;
Craig Tiller4aa71a12015-06-15 13:00:55 -0700275 grpc_chttp2_parse_error (*parser)(
276 void *parser_user_data, grpc_chttp2_transport_parsing *transport_parsing,
277 grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last);
Craig Tillerd20efd22015-06-12 16:17:09 -0700278
279 /* received settings */
280 gpr_uint32 settings[GRPC_CHTTP2_NUM_SETTINGS];
281
282 /* goaway data */
283 grpc_status_code goaway_error;
284 gpr_uint32 goaway_last_stream_index;
285 gpr_slice goaway_text;
Craig Tiller3719f072015-06-12 17:19:51 -0700286
287 gpr_uint64 outgoing_window_update;
288
289 /** pings awaiting responses */
290 grpc_chttp2_outstanding_ping pings;
Craig Tillerd20efd22015-06-12 16:17:09 -0700291};
292
Craig Tillerb084d902015-06-12 07:50:02 -0700293struct grpc_chttp2_transport {
Craig Tiller9b8671c2015-06-12 07:41:54 -0700294 grpc_transport base; /* must be first */
295 grpc_endpoint *ep;
296 grpc_mdctx *metadata_context;
297 gpr_refcount refs;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700298
299 gpr_mu mu;
Craig Tiller606d8742015-06-15 06:58:50 -0700300
301 /** is the transport destroying itself? */
302 gpr_uint8 destroying;
303 /** has the upper layer closed the transport? */
304 gpr_uint8 closed;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700305
Craig Tillerd20efd22015-06-12 16:17:09 -0700306 /** is a thread currently writing */
307 gpr_uint8 writing_active;
Craig Tiller606d8742015-06-15 06:58:50 -0700308 /** is a thread currently parsing */
309 gpr_uint8 parsing_active;
Craig Tillerd20efd22015-06-12 16:17:09 -0700310
Craig Tiller606d8742015-06-15 06:58:50 -0700311 /** is there a read request to the endpoint outstanding? */
312 gpr_uint8 endpoint_reading;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700313
Craig Tiller606d8742015-06-15 06:58:50 -0700314 /** various lists of streams */
Craig Tillerb084d902015-06-12 07:50:02 -0700315 grpc_chttp2_stream_list lists[STREAM_LIST_COUNT];
Craig Tiller9b8671c2015-06-12 07:41:54 -0700316
Craig Tiller606d8742015-06-15 06:58:50 -0700317 /** global state for reading/writing */
Craig Tillerd20efd22015-06-12 16:17:09 -0700318 grpc_chttp2_transport_global global;
Craig Tiller606d8742015-06-15 06:58:50 -0700319 /** state only accessible by the chain of execution that
320 set writing_active=1 */
Craig Tillerd20efd22015-06-12 16:17:09 -0700321 grpc_chttp2_transport_writing writing;
Craig Tiller606d8742015-06-15 06:58:50 -0700322 /** state only accessible by the chain of execution that
323 set parsing_active=1 */
Craig Tillerd20efd22015-06-12 16:17:09 -0700324 grpc_chttp2_transport_parsing parsing;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700325
Craig Tiller606d8742015-06-15 06:58:50 -0700326 /** maps stream id to grpc_chttp2_stream objects;
327 owned by the parsing thread when parsing */
328 grpc_chttp2_stream_map parsing_stream_map;
329
330 /** streams created by the client (possibly during parsing);
331 merged with parsing_stream_map during unlock when no
332 parsing is occurring */
333 grpc_chttp2_stream_map new_stream_map;
334
Craig Tillerd20efd22015-06-12 16:17:09 -0700335 /** closure to execute writing */
336 grpc_iomgr_closure writing_action;
Craig Tiller8b2f1d72015-06-18 13:38:38 -0700337 /** closure to start reading from the endpoint */
338 grpc_iomgr_closure reading_action;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700339
Craig Tiller4aa71a12015-06-15 13:00:55 -0700340 /** address to place a newly accepted stream - set and unset by
Craig Tiller606d8742015-06-15 06:58:50 -0700341 grpc_chttp2_parsing_accept_stream; used by init_stream to
342 publish the accepted server stream */
343 grpc_chttp2_stream **accepting_stream;
344
Craig Tiller9b8671c2015-06-12 07:41:54 -0700345 struct {
346 /** is a thread currently performing channel callbacks */
347 gpr_uint8 executing;
348 /** transport channel-level callback */
349 const grpc_transport_callbacks *cb;
350 /** user data for cb calls */
351 void *cb_user_data;
352 /** closure for notifying transport closure */
353 grpc_iomgr_closure notify_closed;
354 } channel_callback;
355};
356
Craig Tillerd20efd22015-06-12 16:17:09 -0700357typedef struct {
358 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
Craig Tiller9b8671c2015-06-12 07:41:54 -0700359 gpr_uint32 id;
360
Craig Tillerd20efd22015-06-12 16:17:09 -0700361 grpc_iomgr_closure *send_done_closure;
362 grpc_iomgr_closure *recv_done_closure;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700363
Craig Tillerd20efd22015-06-12 16:17:09 -0700364 /** window available for us to send to peer */
365 gpr_int64 outgoing_window;
Craig Tiller3719f072015-06-12 17:19:51 -0700366 /** window available for peer to send to us - updated after parse */
367 gpr_uint32 incoming_window;
Craig Tillerd20efd22015-06-12 16:17:09 -0700368 /** stream ops the transport user would like to send */
369 grpc_stream_op_buffer *outgoing_sopb;
370 /** when the application requests writes be closed, the write_closed is
371 'queued'; when the close is flow controlled into the send path, we are
372 'sending' it; when the write has been performed it is 'sent' */
373 grpc_chttp2_write_state write_state;
374 /** is this stream closed (boolean) */
375 gpr_uint8 read_closed;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700376 /** has this stream been cancelled? (boolean) */
377 gpr_uint8 cancelled;
Craig Tillercec9eb92015-06-17 17:16:48 -0700378 grpc_status_code cancelled_status;
379 /** have we told the upper layer that this stream is cancelled? */
380 gpr_uint8 published_cancelled;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700381 /** is this stream in the stream map? (boolean) */
382 gpr_uint8 in_stream_map;
Craig Tiller606d8742015-06-15 06:58:50 -0700383
384 /** stream state already published to the upper layer */
385 grpc_stream_state published_state;
386 /** address to publish next stream state to */
387 grpc_stream_state *publish_state;
388 /** pointer to sop buffer to fill in with new stream ops */
Craig Tiller6905b7c2015-06-16 15:33:33 -0700389 grpc_stream_op_buffer *publish_sopb;
390 grpc_stream_op_buffer incoming_sopb;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700391
392 /** incoming metadata */
393 grpc_chttp2_incoming_metadata_buffer incoming_metadata;
Craig Tillerb787c502015-06-15 16:14:52 -0700394 grpc_chttp2_incoming_metadata_live_op_buffer outstanding_metadata;
Craig Tillerd20efd22015-06-12 16:17:09 -0700395} grpc_chttp2_stream_global;
396
397typedef struct {
398 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
399 gpr_uint32 id;
400 /** sops that have passed flow control to be written */
401 grpc_stream_op_buffer sopb;
402 /** how strongly should we indicate closure with the next write */
403 grpc_chttp2_send_closed send_closed;
404} grpc_chttp2_stream_writing;
405
406struct grpc_chttp2_stream_parsing {
407 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
408 gpr_uint32 id;
409 /** has this stream received a close */
410 gpr_uint8 received_close;
Craig Tiller3719f072015-06-12 17:19:51 -0700411 /** saw a rst_stream */
412 gpr_uint8 saw_rst_stream;
Craig Tiller606d8742015-06-15 06:58:50 -0700413 /** incoming_window has been reduced by this much during parsing */
414 gpr_uint32 incoming_window_delta;
Craig Tillerd20efd22015-06-12 16:17:09 -0700415 /** window available for peer to send to us */
416 gpr_uint32 incoming_window;
417 /** parsing state for data frames */
418 grpc_chttp2_data_parser data_parser;
Craig Tiller3719f072015-06-12 17:19:51 -0700419 /** reason give to rst_stream */
420 gpr_uint32 rst_stream_reason;
421 /* amount of window given */
422 gpr_uint64 outgoing_window_update;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700423
Craig Tiller5dc3b302015-06-15 16:06:50 -0700424 /** incoming metadata */
425 grpc_chttp2_incoming_metadata_buffer incoming_metadata;
Craig Tillerd20efd22015-06-12 16:17:09 -0700426};
427
428struct grpc_chttp2_stream {
429 grpc_chttp2_stream_global global;
430 grpc_chttp2_stream_writing writing;
Craig Tiller606d8742015-06-15 06:58:50 -0700431 grpc_chttp2_stream_parsing parsing;
Craig Tillerd20efd22015-06-12 16:17:09 -0700432
433 grpc_chttp2_stream_link links[STREAM_LIST_COUNT];
434 gpr_uint8 included[STREAM_LIST_COUNT];
Craig Tiller9b8671c2015-06-12 07:41:54 -0700435};
436
Craig Tillerd20efd22015-06-12 16:17:09 -0700437/** Transport writing call flow:
Craig Tiller4aa71a12015-06-15 13:00:55 -0700438 chttp2_transport.c calls grpc_chttp2_unlocking_check_writes to see if writes
439 are required;
440 if they are, chttp2_transport.c calls grpc_chttp2_perform_writes to do the
441 writes.
442 Once writes have been completed (meaning another write could potentially be
443 started),
444 grpc_chttp2_terminate_writing is called. This will call
445 grpc_chttp2_cleanup_writing, at which
Craig Tillerd20efd22015-06-12 16:17:09 -0700446 point the write phase is complete. */
447
Craig Tiller3208e392015-06-12 08:17:02 -0700448/** Someone is unlocking the transport mutex: check to see if writes
449 are required, and schedule them if so */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700450int grpc_chttp2_unlocking_check_writes(grpc_chttp2_transport_global *global,
451 grpc_chttp2_transport_writing *writing);
452void grpc_chttp2_perform_writes(
453 grpc_chttp2_transport_writing *transport_writing, grpc_endpoint *endpoint);
454void grpc_chttp2_terminate_writing(
455 grpc_chttp2_transport_writing *transport_writing, int success);
456void grpc_chttp2_cleanup_writing(grpc_chttp2_transport_global *global,
457 grpc_chttp2_transport_writing *writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700458
Craig Tiller4aa71a12015-06-15 13:00:55 -0700459void grpc_chttp2_prepare_to_read(grpc_chttp2_transport_global *global,
460 grpc_chttp2_transport_parsing *parsing);
Craig Tillerab630732015-06-25 11:20:01 -0700461/** Process one slice of incoming data; return 1 if the connection is still
462 viable after reading, or 0 if the connection should be torn down */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700463int grpc_chttp2_perform_read(grpc_chttp2_transport_parsing *transport_parsing,
464 gpr_slice slice);
465void grpc_chttp2_publish_reads(grpc_chttp2_transport_global *global,
466 grpc_chttp2_transport_parsing *parsing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700467
468/** Get a writable stream
Craig Tillerab630732015-06-25 11:20:01 -0700469 returns non-zero if there was a stream available */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700470void grpc_chttp2_list_add_writable_stream(
471 grpc_chttp2_transport_global *transport_global,
472 grpc_chttp2_stream_global *stream_global);
473int grpc_chttp2_list_pop_writable_stream(
474 grpc_chttp2_transport_global *transport_global,
475 grpc_chttp2_transport_writing *transport_writing,
476 grpc_chttp2_stream_global **stream_global,
477 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700478
Craig Tillercf1e3192015-06-16 14:28:22 -0700479void grpc_chttp2_list_add_incoming_window_updated(
480 grpc_chttp2_transport_global *transport_global,
481 grpc_chttp2_stream_global *stream_global);
482int grpc_chttp2_list_pop_incoming_window_updated(
483 grpc_chttp2_transport_global *transport_global,
484 grpc_chttp2_transport_parsing *transport_parsing,
485 grpc_chttp2_stream_global **stream_global,
486 grpc_chttp2_stream_parsing **stream_parsing);
Craig Tiller759eb322015-06-16 22:41:18 -0700487void grpc_chttp2_list_remove_incoming_window_updated(
488 grpc_chttp2_transport_global *transport_global,
489 grpc_chttp2_stream_global *stream_global);
Craig Tillercf1e3192015-06-16 14:28:22 -0700490
Craig Tiller4aa71a12015-06-15 13:00:55 -0700491void grpc_chttp2_list_add_writing_stream(
492 grpc_chttp2_transport_writing *transport_writing,
493 grpc_chttp2_stream_writing *stream_writing);
494int grpc_chttp2_list_have_writing_streams(
495 grpc_chttp2_transport_writing *transport_writing);
496int grpc_chttp2_list_pop_writing_stream(
497 grpc_chttp2_transport_writing *transport_writing,
498 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700499
Craig Tiller4aa71a12015-06-15 13:00:55 -0700500void grpc_chttp2_list_add_written_stream(
501 grpc_chttp2_transport_writing *transport_writing,
502 grpc_chttp2_stream_writing *stream_writing);
503int grpc_chttp2_list_pop_written_stream(
504 grpc_chttp2_transport_global *transport_global,
505 grpc_chttp2_transport_writing *transport_writing,
506 grpc_chttp2_stream_global **stream_global,
507 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700508
Craig Tiller4aa71a12015-06-15 13:00:55 -0700509void grpc_chttp2_list_add_writable_window_update_stream(
510 grpc_chttp2_transport_global *transport_global,
511 grpc_chttp2_stream_global *stream_global);
512int grpc_chttp2_list_pop_writable_window_update_stream(
513 grpc_chttp2_transport_global *transport_global,
514 grpc_chttp2_stream_global **stream_global);
Craig Tillerb951df12015-06-18 15:46:58 -0700515void grpc_chttp2_list_remove_writable_window_update_stream(
516 grpc_chttp2_transport_global *transport_global,
517 grpc_chttp2_stream_global *stream_global);
Craig Tillerd20efd22015-06-12 16:17:09 -0700518
Craig Tiller4aa71a12015-06-15 13:00:55 -0700519void grpc_chttp2_list_add_parsing_seen_stream(
520 grpc_chttp2_transport_parsing *transport_parsing,
521 grpc_chttp2_stream_parsing *stream_parsing);
522int grpc_chttp2_list_pop_parsing_seen_stream(
523 grpc_chttp2_transport_global *transport_global,
524 grpc_chttp2_transport_parsing *transport_parsing,
525 grpc_chttp2_stream_global **stream_global,
526 grpc_chttp2_stream_parsing **stream_parsing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700527
Craig Tiller5dc3b302015-06-15 16:06:50 -0700528void grpc_chttp2_list_add_waiting_for_concurrency(
529 grpc_chttp2_transport_global *transport_global,
530 grpc_chttp2_stream_global *stream_global);
531int grpc_chttp2_list_pop_waiting_for_concurrency(
532 grpc_chttp2_transport_global *transport_global,
533 grpc_chttp2_stream_global **stream_global);
534
Craig Tiller83fb0702015-06-16 21:13:07 -0700535void grpc_chttp2_list_add_closed_waiting_for_parsing(
Craig Tiller5dc3b302015-06-15 16:06:50 -0700536 grpc_chttp2_transport_global *transport_global,
537 grpc_chttp2_stream_global *stream_global);
Craig Tiller83fb0702015-06-16 21:13:07 -0700538int grpc_chttp2_list_pop_closed_waiting_for_parsing(
Craig Tiller5dc3b302015-06-15 16:06:50 -0700539 grpc_chttp2_transport_global *transport_global,
540 grpc_chttp2_stream_global **stream_global);
541
Craig Tiller6459db42015-06-15 17:11:45 -0700542void grpc_chttp2_list_add_read_write_state_changed(
543 grpc_chttp2_transport_global *transport_global,
544 grpc_chttp2_stream_global *stream_global);
Craig Tillerf73fcd12015-06-16 16:25:26 -0700545int grpc_chttp2_list_pop_read_write_state_changed(
546 grpc_chttp2_transport_global *transport_global,
547 grpc_chttp2_stream_global **stream_global);
Craig Tiller6459db42015-06-15 17:11:45 -0700548
Craig Tiller1937b062015-06-16 08:47:38 -0700549/** schedule a closure to run without the transport lock taken */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700550void grpc_chttp2_schedule_closure(
551 grpc_chttp2_transport_global *transport_global, grpc_iomgr_closure *closure,
552 int success);
Craig Tillerd20efd22015-06-12 16:17:09 -0700553
Craig Tiller4aa71a12015-06-15 13:00:55 -0700554grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream(
555 grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
556grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream(
557 grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
Craig Tillerd20efd22015-06-12 16:17:09 -0700558
Craig Tiller98505102015-06-16 11:33:15 -0700559void grpc_chttp2_add_incoming_goaway(
560 grpc_chttp2_transport_global *transport_global, gpr_uint32 goaway_error,
561 gpr_slice goaway_text);
Craig Tiller4aa71a12015-06-15 13:00:55 -0700562
Craig Tiller98505102015-06-16 11:33:15 -0700563void grpc_chttp2_register_stream(grpc_chttp2_transport *t,
564 grpc_chttp2_stream *s);
565void grpc_chttp2_unregister_stream(grpc_chttp2_transport *t,
566 grpc_chttp2_stream *s);
567void grpc_chttp2_for_all_streams(
568 grpc_chttp2_transport_global *transport_global, void *user_data,
569 void (*cb)(grpc_chttp2_transport_global *transport_global, void *user_data,
570 grpc_chttp2_stream_global *stream_global));
Craig Tiller5dc3b302015-06-15 16:06:50 -0700571
Craig Tiller285b8822015-06-17 15:58:13 -0700572void grpc_chttp2_parsing_become_skip_parser(
573 grpc_chttp2_transport_parsing *transport_parsing);
Craig Tiller66abdaa2015-06-17 08:00:39 -0700574
Craig Tillerd20efd22015-06-12 16:17:09 -0700575#define GRPC_CHTTP2_CLIENT_CONNECT_STRING "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
Craig Tiller4aa71a12015-06-15 13:00:55 -0700576#define GRPC_CHTTP2_CLIENT_CONNECT_STRLEN \
577 (sizeof(GRPC_CHTTP2_CLIENT_CONNECT_STRING) - 1)
Craig Tillerd20efd22015-06-12 16:17:09 -0700578
579extern int grpc_http_trace;
Craig Tiller285b8822015-06-17 15:58:13 -0700580extern int grpc_flowctl_trace;
Craig Tillerd20efd22015-06-12 16:17:09 -0700581
Craig Tillerab630732015-06-25 11:20:01 -0700582#define GRPC_CHTTP2_IF_TRACING(stmt) \
583 if (!(grpc_http_trace)) \
584 ; \
585 else \
Craig Tillerd20efd22015-06-12 16:17:09 -0700586 stmt
Craig Tiller3208e392015-06-12 08:17:02 -0700587
Craig Tiller285b8822015-06-17 15:58:13 -0700588#define GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(reason, transport, context, var, \
589 delta) \
590 if (!(grpc_flowctl_trace)) { \
591 } else { \
592 grpc_chttp2_flowctl_trace(__FILE__, __LINE__, reason, #context, #var, \
593 transport->is_client, context->id, context->var, \
594 delta); \
595 }
596
597#define GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT(reason, context, var, delta) \
598 if (!(grpc_flowctl_trace)) { \
599 } else { \
600 grpc_chttp2_flowctl_trace(__FILE__, __LINE__, reason, #context, #var, \
601 context->is_client, 0, context->var, delta); \
602 }
603
604void grpc_chttp2_flowctl_trace(const char *file, int line, const char *reason,
605 const char *context, const char *var,
606 int is_client, gpr_uint32 stream_id,
607 gpr_int64 current_value, gpr_int64 delta);
608
Craig Tiller9b8671c2015-06-12 07:41:54 -0700609#endif