blob: 7f50e711d6c4107a9bd28560019362dc4c82b383 [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 */
75 DTS_CLIENT_PREFIX_0 = 0,
76 DTS_CLIENT_PREFIX_1,
77 DTS_CLIENT_PREFIX_2,
78 DTS_CLIENT_PREFIX_3,
79 DTS_CLIENT_PREFIX_4,
80 DTS_CLIENT_PREFIX_5,
81 DTS_CLIENT_PREFIX_6,
82 DTS_CLIENT_PREFIX_7,
83 DTS_CLIENT_PREFIX_8,
84 DTS_CLIENT_PREFIX_9,
85 DTS_CLIENT_PREFIX_10,
86 DTS_CLIENT_PREFIX_11,
87 DTS_CLIENT_PREFIX_12,
88 DTS_CLIENT_PREFIX_13,
89 DTS_CLIENT_PREFIX_14,
90 DTS_CLIENT_PREFIX_15,
91 DTS_CLIENT_PREFIX_16,
92 DTS_CLIENT_PREFIX_17,
93 DTS_CLIENT_PREFIX_18,
94 DTS_CLIENT_PREFIX_19,
95 DTS_CLIENT_PREFIX_20,
96 DTS_CLIENT_PREFIX_21,
97 DTS_CLIENT_PREFIX_22,
98 DTS_CLIENT_PREFIX_23,
99 /* frame header byte 0... */
100 /* must follow from the prefix states */
101 DTS_FH_0,
102 DTS_FH_1,
103 DTS_FH_2,
104 DTS_FH_3,
105 DTS_FH_4,
106 DTS_FH_5,
107 DTS_FH_6,
108 DTS_FH_7,
109 /* ... frame header byte 8 */
110 DTS_FH_8,
111 /* inside a http2 frame */
112 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 {
116 WRITE_STATE_OPEN,
117 WRITE_STATE_QUEUED_CLOSE,
118 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 {
122 DONT_SEND_CLOSED = 0,
123 SEND_CLOSED,
124 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) */
146 PEER_SETTINGS = 0,
147 /* The settings we'd like to have */
148 LOCAL_SETTINGS,
149 /* The settings we've published to our peer */
150 SENT_SETTINGS,
151 /* The settings the peer has acked */
152 ACKED_SETTINGS,
153 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 */
186 gpr_uint32 settings[NUM_SETTING_SETS][GRPC_CHTTP2_NUM_SETTINGS];
187
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;
Craig Tiller606d8742015-06-15 06:58:50 -0700355
356#if 0
357 /* basic state management - what are we doing at the moment? */
358 gpr_uint8 reading;
359 /** are we calling back any grpc_transport_op completion events */
360 gpr_uint8 calling_back_ops;
361 gpr_uint8 destroying;
362 gpr_uint8 closed;
363
364 /* stream indexing */
365 gpr_uint32 next_stream_id;
366
367 /* window management */
368 gpr_uint32 outgoing_window_update;
369
370 /* state for a stream that's not yet been created */
371 grpc_stream_op_buffer new_stream_sopb;
372
373 /* stream ops that need to be destroyed, but outside of the lock */
374 grpc_stream_op_buffer nuke_later_sopb;
375
376 /* pings */
377 gpr_int64 ping_counter;
378
379
380 grpc_chttp2_stream **accepting_stream;
381
382#endif
Craig Tiller9b8671c2015-06-12 07:41:54 -0700383};
384
Craig Tillerd20efd22015-06-12 16:17:09 -0700385typedef struct {
386 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
Craig Tiller9b8671c2015-06-12 07:41:54 -0700387 gpr_uint32 id;
388
Craig Tillerd20efd22015-06-12 16:17:09 -0700389 grpc_iomgr_closure *send_done_closure;
390 grpc_iomgr_closure *recv_done_closure;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700391
Craig Tillerd20efd22015-06-12 16:17:09 -0700392 /** window available for us to send to peer */
393 gpr_int64 outgoing_window;
Craig Tiller3719f072015-06-12 17:19:51 -0700394 /** window available for peer to send to us - updated after parse */
395 gpr_uint32 incoming_window;
Craig Tillerd20efd22015-06-12 16:17:09 -0700396 /** stream ops the transport user would like to send */
397 grpc_stream_op_buffer *outgoing_sopb;
398 /** when the application requests writes be closed, the write_closed is
399 'queued'; when the close is flow controlled into the send path, we are
400 'sending' it; when the write has been performed it is 'sent' */
401 grpc_chttp2_write_state write_state;
402 /** is this stream closed (boolean) */
403 gpr_uint8 read_closed;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700404 /** has this stream been cancelled? (boolean) */
405 gpr_uint8 cancelled;
Craig Tillercec9eb92015-06-17 17:16:48 -0700406 grpc_status_code cancelled_status;
407 /** have we told the upper layer that this stream is cancelled? */
408 gpr_uint8 published_cancelled;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700409 /** is this stream in the stream map? (boolean) */
410 gpr_uint8 in_stream_map;
Craig Tiller606d8742015-06-15 06:58:50 -0700411
412 /** stream state already published to the upper layer */
413 grpc_stream_state published_state;
414 /** address to publish next stream state to */
415 grpc_stream_state *publish_state;
416 /** pointer to sop buffer to fill in with new stream ops */
Craig Tiller6905b7c2015-06-16 15:33:33 -0700417 grpc_stream_op_buffer *publish_sopb;
418 grpc_stream_op_buffer incoming_sopb;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700419
420 /** incoming metadata */
421 grpc_chttp2_incoming_metadata_buffer incoming_metadata;
Craig Tillerb787c502015-06-15 16:14:52 -0700422 grpc_chttp2_incoming_metadata_live_op_buffer outstanding_metadata;
Craig Tillerd20efd22015-06-12 16:17:09 -0700423} grpc_chttp2_stream_global;
424
425typedef struct {
426 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
427 gpr_uint32 id;
428 /** sops that have passed flow control to be written */
429 grpc_stream_op_buffer sopb;
430 /** how strongly should we indicate closure with the next write */
431 grpc_chttp2_send_closed send_closed;
432} grpc_chttp2_stream_writing;
433
434struct grpc_chttp2_stream_parsing {
435 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
436 gpr_uint32 id;
437 /** has this stream received a close */
438 gpr_uint8 received_close;
Craig Tiller3719f072015-06-12 17:19:51 -0700439 /** saw a rst_stream */
440 gpr_uint8 saw_rst_stream;
Craig Tiller606d8742015-06-15 06:58:50 -0700441 /** incoming_window has been reduced by this much during parsing */
442 gpr_uint32 incoming_window_delta;
Craig Tillerd20efd22015-06-12 16:17:09 -0700443 /** window available for peer to send to us */
444 gpr_uint32 incoming_window;
445 /** parsing state for data frames */
446 grpc_chttp2_data_parser data_parser;
Craig Tiller3719f072015-06-12 17:19:51 -0700447 /** reason give to rst_stream */
448 gpr_uint32 rst_stream_reason;
449 /* amount of window given */
450 gpr_uint64 outgoing_window_update;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700451
Craig Tiller5dc3b302015-06-15 16:06:50 -0700452 /** incoming metadata */
453 grpc_chttp2_incoming_metadata_buffer incoming_metadata;
454
Craig Tiller98505102015-06-16 11:33:15 -0700455 /*
456 grpc_linked_mdelem *incoming_metadata;
457 size_t incoming_metadata_count;
458 size_t incoming_metadata_capacity;
459 grpc_linked_mdelem *old_incoming_metadata;
460 gpr_timespec incoming_deadline;
461 */
Craig Tillerd20efd22015-06-12 16:17:09 -0700462};
463
464struct grpc_chttp2_stream {
465 grpc_chttp2_stream_global global;
466 grpc_chttp2_stream_writing writing;
Craig Tiller606d8742015-06-15 06:58:50 -0700467 grpc_chttp2_stream_parsing parsing;
Craig Tillerd20efd22015-06-12 16:17:09 -0700468
469 grpc_chttp2_stream_link links[STREAM_LIST_COUNT];
470 gpr_uint8 included[STREAM_LIST_COUNT];
Craig Tiller9b8671c2015-06-12 07:41:54 -0700471
Craig Tiller606d8742015-06-15 06:58:50 -0700472#if 0
473 gpr_uint32 outgoing_window_update;
474 gpr_uint8 cancelled;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700475
Craig Tiller9b8671c2015-06-12 07:41:54 -0700476 grpc_stream_state callback_state;
477 grpc_stream_op_buffer callback_sopb;
Craig Tiller606d8742015-06-15 06:58:50 -0700478#endif
Craig Tiller9b8671c2015-06-12 07:41:54 -0700479};
480
Craig Tillerd20efd22015-06-12 16:17:09 -0700481/** Transport writing call flow:
Craig Tiller4aa71a12015-06-15 13:00:55 -0700482 chttp2_transport.c calls grpc_chttp2_unlocking_check_writes to see if writes
483 are required;
484 if they are, chttp2_transport.c calls grpc_chttp2_perform_writes to do the
485 writes.
486 Once writes have been completed (meaning another write could potentially be
487 started),
488 grpc_chttp2_terminate_writing is called. This will call
489 grpc_chttp2_cleanup_writing, at which
Craig Tillerd20efd22015-06-12 16:17:09 -0700490 point the write phase is complete. */
491
Craig Tiller3208e392015-06-12 08:17:02 -0700492/** Someone is unlocking the transport mutex: check to see if writes
493 are required, and schedule them if so */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700494int grpc_chttp2_unlocking_check_writes(grpc_chttp2_transport_global *global,
495 grpc_chttp2_transport_writing *writing);
496void grpc_chttp2_perform_writes(
497 grpc_chttp2_transport_writing *transport_writing, grpc_endpoint *endpoint);
498void grpc_chttp2_terminate_writing(
499 grpc_chttp2_transport_writing *transport_writing, int success);
500void grpc_chttp2_cleanup_writing(grpc_chttp2_transport_global *global,
501 grpc_chttp2_transport_writing *writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700502
Craig Tiller4aa71a12015-06-15 13:00:55 -0700503void grpc_chttp2_prepare_to_read(grpc_chttp2_transport_global *global,
504 grpc_chttp2_transport_parsing *parsing);
Craig Tiller1937b062015-06-16 08:47:38 -0700505/** Process one slice of incoming data */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700506int grpc_chttp2_perform_read(grpc_chttp2_transport_parsing *transport_parsing,
507 gpr_slice slice);
508void grpc_chttp2_publish_reads(grpc_chttp2_transport_global *global,
509 grpc_chttp2_transport_parsing *parsing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700510
511/** Get a writable stream
512 \return non-zero if there was a stream available */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700513void grpc_chttp2_list_add_writable_stream(
514 grpc_chttp2_transport_global *transport_global,
515 grpc_chttp2_stream_global *stream_global);
516int grpc_chttp2_list_pop_writable_stream(
517 grpc_chttp2_transport_global *transport_global,
518 grpc_chttp2_transport_writing *transport_writing,
519 grpc_chttp2_stream_global **stream_global,
520 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700521
Craig Tillercf1e3192015-06-16 14:28:22 -0700522void grpc_chttp2_list_add_incoming_window_updated(
523 grpc_chttp2_transport_global *transport_global,
524 grpc_chttp2_stream_global *stream_global);
525int grpc_chttp2_list_pop_incoming_window_updated(
526 grpc_chttp2_transport_global *transport_global,
527 grpc_chttp2_transport_parsing *transport_parsing,
528 grpc_chttp2_stream_global **stream_global,
529 grpc_chttp2_stream_parsing **stream_parsing);
Craig Tiller759eb322015-06-16 22:41:18 -0700530void grpc_chttp2_list_remove_incoming_window_updated(
531 grpc_chttp2_transport_global *transport_global,
532 grpc_chttp2_stream_global *stream_global);
Craig Tillercf1e3192015-06-16 14:28:22 -0700533
Craig Tiller4aa71a12015-06-15 13:00:55 -0700534void grpc_chttp2_list_add_writing_stream(
535 grpc_chttp2_transport_writing *transport_writing,
536 grpc_chttp2_stream_writing *stream_writing);
537int grpc_chttp2_list_have_writing_streams(
538 grpc_chttp2_transport_writing *transport_writing);
539int grpc_chttp2_list_pop_writing_stream(
540 grpc_chttp2_transport_writing *transport_writing,
541 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700542
Craig Tiller4aa71a12015-06-15 13:00:55 -0700543void grpc_chttp2_list_add_written_stream(
544 grpc_chttp2_transport_writing *transport_writing,
545 grpc_chttp2_stream_writing *stream_writing);
546int grpc_chttp2_list_pop_written_stream(
547 grpc_chttp2_transport_global *transport_global,
548 grpc_chttp2_transport_writing *transport_writing,
549 grpc_chttp2_stream_global **stream_global,
550 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700551
Craig Tiller4aa71a12015-06-15 13:00:55 -0700552void grpc_chttp2_list_add_writable_window_update_stream(
553 grpc_chttp2_transport_global *transport_global,
554 grpc_chttp2_stream_global *stream_global);
555int grpc_chttp2_list_pop_writable_window_update_stream(
556 grpc_chttp2_transport_global *transport_global,
557 grpc_chttp2_stream_global **stream_global);
Craig Tillerd20efd22015-06-12 16:17:09 -0700558
Craig Tiller4aa71a12015-06-15 13:00:55 -0700559void grpc_chttp2_list_add_parsing_seen_stream(
560 grpc_chttp2_transport_parsing *transport_parsing,
561 grpc_chttp2_stream_parsing *stream_parsing);
562int grpc_chttp2_list_pop_parsing_seen_stream(
563 grpc_chttp2_transport_global *transport_global,
564 grpc_chttp2_transport_parsing *transport_parsing,
565 grpc_chttp2_stream_global **stream_global,
566 grpc_chttp2_stream_parsing **stream_parsing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700567
Craig Tiller5dc3b302015-06-15 16:06:50 -0700568void grpc_chttp2_list_add_waiting_for_concurrency(
569 grpc_chttp2_transport_global *transport_global,
570 grpc_chttp2_stream_global *stream_global);
571int grpc_chttp2_list_pop_waiting_for_concurrency(
572 grpc_chttp2_transport_global *transport_global,
573 grpc_chttp2_stream_global **stream_global);
574
Craig Tiller83fb0702015-06-16 21:13:07 -0700575void grpc_chttp2_list_add_closed_waiting_for_parsing(
Craig Tiller5dc3b302015-06-15 16:06:50 -0700576 grpc_chttp2_transport_global *transport_global,
577 grpc_chttp2_stream_global *stream_global);
Craig Tiller83fb0702015-06-16 21:13:07 -0700578int grpc_chttp2_list_pop_closed_waiting_for_parsing(
Craig Tiller5dc3b302015-06-15 16:06:50 -0700579 grpc_chttp2_transport_global *transport_global,
580 grpc_chttp2_stream_global **stream_global);
581
Craig Tiller6459db42015-06-15 17:11:45 -0700582void grpc_chttp2_list_add_read_write_state_changed(
583 grpc_chttp2_transport_global *transport_global,
584 grpc_chttp2_stream_global *stream_global);
Craig Tillerf73fcd12015-06-16 16:25:26 -0700585int grpc_chttp2_list_pop_read_write_state_changed(
586 grpc_chttp2_transport_global *transport_global,
587 grpc_chttp2_stream_global **stream_global);
Craig Tiller6459db42015-06-15 17:11:45 -0700588
Craig Tiller1937b062015-06-16 08:47:38 -0700589/** schedule a closure to run without the transport lock taken */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700590void grpc_chttp2_schedule_closure(
591 grpc_chttp2_transport_global *transport_global, grpc_iomgr_closure *closure,
592 int success);
Craig Tillerd20efd22015-06-12 16:17:09 -0700593
Craig Tiller4aa71a12015-06-15 13:00:55 -0700594grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream(
595 grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
596grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream(
597 grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
Craig Tillerd20efd22015-06-12 16:17:09 -0700598
Craig Tiller98505102015-06-16 11:33:15 -0700599void grpc_chttp2_add_incoming_goaway(
600 grpc_chttp2_transport_global *transport_global, gpr_uint32 goaway_error,
601 gpr_slice goaway_text);
Craig Tiller4aa71a12015-06-15 13:00:55 -0700602
Craig Tiller98505102015-06-16 11:33:15 -0700603void grpc_chttp2_register_stream(grpc_chttp2_transport *t,
604 grpc_chttp2_stream *s);
605void grpc_chttp2_unregister_stream(grpc_chttp2_transport *t,
606 grpc_chttp2_stream *s);
607void grpc_chttp2_for_all_streams(
608 grpc_chttp2_transport_global *transport_global, void *user_data,
609 void (*cb)(grpc_chttp2_transport_global *transport_global, void *user_data,
610 grpc_chttp2_stream_global *stream_global));
Craig Tiller5dc3b302015-06-15 16:06:50 -0700611
Craig Tiller285b8822015-06-17 15:58:13 -0700612void grpc_chttp2_parsing_become_skip_parser(
613 grpc_chttp2_transport_parsing *transport_parsing);
Craig Tiller66abdaa2015-06-17 08:00:39 -0700614
Craig Tillerd20efd22015-06-12 16:17:09 -0700615#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 -0700616#define GRPC_CHTTP2_CLIENT_CONNECT_STRLEN \
617 (sizeof(GRPC_CHTTP2_CLIENT_CONNECT_STRING) - 1)
Craig Tillerd20efd22015-06-12 16:17:09 -0700618
619extern int grpc_http_trace;
Craig Tiller285b8822015-06-17 15:58:13 -0700620extern int grpc_flowctl_trace;
Craig Tillerd20efd22015-06-12 16:17:09 -0700621
622#define IF_TRACING(stmt) \
623 if (!(grpc_http_trace)) \
624 ; \
625 else \
626 stmt
Craig Tiller3208e392015-06-12 08:17:02 -0700627
Craig Tiller285b8822015-06-17 15:58:13 -0700628#define GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(reason, transport, context, var, \
629 delta) \
630 if (!(grpc_flowctl_trace)) { \
631 } else { \
632 grpc_chttp2_flowctl_trace(__FILE__, __LINE__, reason, #context, #var, \
633 transport->is_client, context->id, context->var, \
634 delta); \
635 }
636
637#define GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT(reason, context, var, delta) \
638 if (!(grpc_flowctl_trace)) { \
639 } else { \
640 grpc_chttp2_flowctl_trace(__FILE__, __LINE__, reason, #context, #var, \
641 context->is_client, 0, context->var, delta); \
642 }
643
644void grpc_chttp2_flowctl_trace(const char *file, int line, const char *reason,
645 const char *context, const char *var,
646 int is_client, gpr_uint32 stream_id,
647 gpr_int64 current_value, gpr_int64 delta);
648
Craig Tiller9b8671c2015-06-12 07:41:54 -0700649#endif