blob: 7bd91334b4fb65933de35fecd3eae02a237b166c [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,
59 GRPC_CHTTP2_LIST_INCOMING_WINDOW_STATE_CHANGED,
Craig Tiller5dc3b302015-06-15 16:06:50 -070060 GRPC_CHTTP2_LIST_WRITABLE,
Craig Tiller6459db42015-06-15 17:11:45 -070061 GRPC_CHTTP2_LIST_WRITING,
62 GRPC_CHTTP2_LIST_WRITTEN,
63 GRPC_CHTTP2_LIST_WRITABLE_WINDOW_UPDATE,
64 GRPC_CHTTP2_LIST_PARSING_SEEN,
Craig Tiller83fb0702015-06-16 21:13:07 -070065 GRPC_CHTTP2_LIST_CLOSED_WAITING_FOR_PARSING,
Craig Tillercf1e3192015-06-16 14:28:22 -070066 GRPC_CHTTP2_LIST_INCOMING_WINDOW_UPDATED,
Craig Tiller5dc3b302015-06-15 16:06:50 -070067 /** streams that are waiting to start because there are too many concurrent
68 streams on the connection */
69 GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY,
70#if 0
Craig Tiller9b8671c2015-06-12 07:41:54 -070071 /* streams that have pending writes */
72 WRITABLE = 0,
73 /* streams that have been selected to be written */
74 WRITING,
75 /* streams that have just been written, and included a close */
76 WRITTEN_CLOSED,
77 /* streams that have been cancelled and have some pending state updates
78 to perform */
79 CANCELLED,
80 /* streams that want to send window updates */
81 WINDOW_UPDATE,
82 /* streams that are waiting to start because there are too many concurrent
83 streams on the connection */
84 WAITING_FOR_CONCURRENCY,
85 /* streams that have finished reading: we wait until unlock to coalesce
86 all changes into one callback */
87 FINISHED_READ_OP,
88 MAYBE_FINISH_READ_AFTER_PARSE,
89 PARSER_CHECK_WINDOW_UPDATES_AFTER_PARSE,
90 OTHER_CHECK_WINDOW_UPDATES_AFTER_PARSE,
91 NEW_OUTGOING_WINDOW,
Craig Tiller98505102015-06-16 11:33:15 -070092#endif
Craig Tiller9b8671c2015-06-12 07:41:54 -070093 STREAM_LIST_COUNT /* must be last */
Craig Tillerb084d902015-06-12 07:50:02 -070094} grpc_chttp2_stream_list_id;
Craig Tiller9b8671c2015-06-12 07:41:54 -070095
96/* deframer state for the overall http2 stream of bytes */
97typedef enum {
98 /* prefix: one entry per http2 connection prefix byte */
99 DTS_CLIENT_PREFIX_0 = 0,
100 DTS_CLIENT_PREFIX_1,
101 DTS_CLIENT_PREFIX_2,
102 DTS_CLIENT_PREFIX_3,
103 DTS_CLIENT_PREFIX_4,
104 DTS_CLIENT_PREFIX_5,
105 DTS_CLIENT_PREFIX_6,
106 DTS_CLIENT_PREFIX_7,
107 DTS_CLIENT_PREFIX_8,
108 DTS_CLIENT_PREFIX_9,
109 DTS_CLIENT_PREFIX_10,
110 DTS_CLIENT_PREFIX_11,
111 DTS_CLIENT_PREFIX_12,
112 DTS_CLIENT_PREFIX_13,
113 DTS_CLIENT_PREFIX_14,
114 DTS_CLIENT_PREFIX_15,
115 DTS_CLIENT_PREFIX_16,
116 DTS_CLIENT_PREFIX_17,
117 DTS_CLIENT_PREFIX_18,
118 DTS_CLIENT_PREFIX_19,
119 DTS_CLIENT_PREFIX_20,
120 DTS_CLIENT_PREFIX_21,
121 DTS_CLIENT_PREFIX_22,
122 DTS_CLIENT_PREFIX_23,
123 /* frame header byte 0... */
124 /* must follow from the prefix states */
125 DTS_FH_0,
126 DTS_FH_1,
127 DTS_FH_2,
128 DTS_FH_3,
129 DTS_FH_4,
130 DTS_FH_5,
131 DTS_FH_6,
132 DTS_FH_7,
133 /* ... frame header byte 8 */
134 DTS_FH_8,
135 /* inside a http2 frame */
136 DTS_FRAME
Craig Tillerb084d902015-06-12 07:50:02 -0700137} grpc_chttp2_deframe_transport_state;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700138
139typedef enum {
140 WRITE_STATE_OPEN,
141 WRITE_STATE_QUEUED_CLOSE,
142 WRITE_STATE_SENT_CLOSE
Craig Tillerb084d902015-06-12 07:50:02 -0700143} grpc_chttp2_write_state;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700144
145typedef enum {
146 DONT_SEND_CLOSED = 0,
147 SEND_CLOSED,
148 SEND_CLOSED_WITH_RST_STREAM
Craig Tillerb084d902015-06-12 07:50:02 -0700149} grpc_chttp2_send_closed;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700150
151typedef struct {
Craig Tillerb084d902015-06-12 07:50:02 -0700152 grpc_chttp2_stream *head;
153 grpc_chttp2_stream *tail;
154} grpc_chttp2_stream_list;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700155
156typedef struct {
Craig Tillerb084d902015-06-12 07:50:02 -0700157 grpc_chttp2_stream *next;
158 grpc_chttp2_stream *prev;
159} grpc_chttp2_stream_link;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700160
161typedef enum {
Craig Tiller606d8742015-06-15 06:58:50 -0700162 GRPC_CHTTP2_ERROR_STATE_NONE,
163 GRPC_CHTTP2_ERROR_STATE_SEEN,
164 GRPC_CHTTP2_ERROR_STATE_NOTIFIED
Craig Tillerb084d902015-06-12 07:50:02 -0700165} grpc_chttp2_error_state;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700166
167/* We keep several sets of connection wide parameters */
168typedef enum {
169 /* The settings our peer has asked for (and we have acked) */
170 PEER_SETTINGS = 0,
171 /* The settings we'd like to have */
172 LOCAL_SETTINGS,
173 /* The settings we've published to our peer */
174 SENT_SETTINGS,
175 /* The settings the peer has acked */
176 ACKED_SETTINGS,
177 NUM_SETTING_SETS
Craig Tillerb084d902015-06-12 07:50:02 -0700178} grpc_chttp2_setting_set;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700179
180/* Outstanding ping request data */
Craig Tiller3719f072015-06-12 17:19:51 -0700181typedef struct grpc_chttp2_outstanding_ping {
Craig Tiller9b8671c2015-06-12 07:41:54 -0700182 gpr_uint8 id[8];
Craig Tiller3719f072015-06-12 17:19:51 -0700183 grpc_iomgr_closure *on_recv;
184 struct grpc_chttp2_outstanding_ping *next;
185 struct grpc_chttp2_outstanding_ping *prev;
Craig Tillerb084d902015-06-12 07:50:02 -0700186} grpc_chttp2_outstanding_ping;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700187
188typedef struct {
Craig Tillerd20efd22015-06-12 16:17:09 -0700189 /** data to write next write */
190 gpr_slice_buffer qbuf;
191 /** queued callbacks */
192 grpc_iomgr_closure *pending_closures;
193
194 /** window available for us to send to peer */
195 gpr_uint32 outgoing_window;
Craig Tiller3719f072015-06-12 17:19:51 -0700196 /** window available for peer to send to us - updated after parse */
197 gpr_uint32 incoming_window;
Craig Tillerd20efd22015-06-12 16:17:09 -0700198 /** how much window would we like to have for incoming_window */
199 gpr_uint32 connection_window_target;
200
Craig Tiller3719f072015-06-12 17:19:51 -0700201 /** is this transport a client? */
202 gpr_uint8 is_client;
Craig Tillerd20efd22015-06-12 16:17:09 -0700203 /** are the local settings dirty and need to be sent? */
204 gpr_uint8 dirtied_local_settings;
205 /** have local settings been sent? */
206 gpr_uint8 sent_local_settings;
207 /** bitmask of setting indexes to send out */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700208 gpr_uint32 force_send_settings;
Craig Tillerd20efd22015-06-12 16:17:09 -0700209 /** settings values */
210 gpr_uint32 settings[NUM_SETTING_SETS][GRPC_CHTTP2_NUM_SETTINGS];
211
Craig Tiller606d8742015-06-15 06:58:50 -0700212 /** has there been a connection level error, and have we notified
213 anyone about it? */
214 grpc_chttp2_error_state error_state;
215
216 /** what is the next stream id to be allocated by this peer?
217 copied to next_stream_id in parsing when parsing commences */
218 gpr_uint32 next_stream_id;
219
Craig Tillerd20efd22015-06-12 16:17:09 -0700220 /** last received stream id */
221 gpr_uint32 last_incoming_stream_id;
Craig Tiller3719f072015-06-12 17:19:51 -0700222
223 /** pings awaiting responses */
224 grpc_chttp2_outstanding_ping pings;
Craig Tiller606d8742015-06-15 06:58:50 -0700225 /** next payload for an outgoing ping */
226 gpr_uint64 ping_counter;
227
228 /** concurrent stream count: updated when not parsing,
229 so this is a strict over-estimation on the client */
230 gpr_uint32 concurrent_stream_count;
231
Craig Tiller4aa71a12015-06-15 13:00:55 -0700232 /** is there a goaway available? (boolean) */
233 grpc_chttp2_error_state goaway_state;
Craig Tiller606d8742015-06-15 06:58:50 -0700234 /** what is the debug text of the goaway? */
235 gpr_slice goaway_text;
236 /** what is the status code of the goaway? */
237 grpc_status_code goaway_error;
Craig Tillerd20efd22015-06-12 16:17:09 -0700238} grpc_chttp2_transport_global;
239
240typedef struct {
241 /** data to write now */
242 gpr_slice_buffer outbuf;
243 /** hpack encoding */
244 grpc_chttp2_hpack_compressor hpack_compressor;
245} grpc_chttp2_transport_writing;
246
247struct grpc_chttp2_transport_parsing {
248 /** is this transport a client? (boolean) */
249 gpr_uint8 is_client;
250
251 /** were settings updated? */
252 gpr_uint8 settings_updated;
253 /** was a settings ack received? */
254 gpr_uint8 settings_ack_received;
255 /** was a goaway frame received? */
256 gpr_uint8 goaway_received;
257
Craig Tiller3719f072015-06-12 17:19:51 -0700258 /** initial window change */
259 gpr_int64 initial_window_update;
260
Craig Tillerd20efd22015-06-12 16:17:09 -0700261 /** data to write later - after parsing */
262 gpr_slice_buffer qbuf;
263 /* metadata object cache */
264 grpc_mdstr *str_grpc_timeout;
265 /** parser for headers */
266 grpc_chttp2_hpack_parser hpack_parser;
267 /** simple one shot parsers */
268 union {
269 grpc_chttp2_window_update_parser window_update;
270 grpc_chttp2_settings_parser settings;
271 grpc_chttp2_ping_parser ping;
272 grpc_chttp2_rst_stream_parser rst_stream;
273 } simple;
274 /** parser for goaway frames */
275 grpc_chttp2_goaway_parser goaway_parser;
276
277 /** window available for peer to send to us */
278 gpr_uint32 incoming_window;
279
280 /** next stream id available at the time of beginning parsing */
281 gpr_uint32 next_stream_id;
282 gpr_uint32 last_incoming_stream_id;
283
284 /* deframing */
285 grpc_chttp2_deframe_transport_state deframe_state;
286 gpr_uint8 incoming_frame_type;
287 gpr_uint8 incoming_frame_flags;
288 gpr_uint8 header_eof;
289 gpr_uint32 expect_continuation_stream_id;
290 gpr_uint32 incoming_frame_size;
291 gpr_uint32 incoming_stream_id;
292
293 /* active parser */
294 void *parser_data;
295 grpc_chttp2_stream_parsing *incoming_stream;
Craig Tiller4aa71a12015-06-15 13:00:55 -0700296 grpc_chttp2_parse_error (*parser)(
297 void *parser_user_data, grpc_chttp2_transport_parsing *transport_parsing,
298 grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last);
Craig Tillerd20efd22015-06-12 16:17:09 -0700299
300 /* received settings */
301 gpr_uint32 settings[GRPC_CHTTP2_NUM_SETTINGS];
302
303 /* goaway data */
304 grpc_status_code goaway_error;
305 gpr_uint32 goaway_last_stream_index;
306 gpr_slice goaway_text;
Craig Tiller3719f072015-06-12 17:19:51 -0700307
308 gpr_uint64 outgoing_window_update;
309
310 /** pings awaiting responses */
311 grpc_chttp2_outstanding_ping pings;
Craig Tillerd20efd22015-06-12 16:17:09 -0700312};
313
Craig Tillerb084d902015-06-12 07:50:02 -0700314struct grpc_chttp2_transport {
Craig Tiller9b8671c2015-06-12 07:41:54 -0700315 grpc_transport base; /* must be first */
316 grpc_endpoint *ep;
317 grpc_mdctx *metadata_context;
318 gpr_refcount refs;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700319
320 gpr_mu mu;
Craig Tiller606d8742015-06-15 06:58:50 -0700321
322 /** is the transport destroying itself? */
323 gpr_uint8 destroying;
324 /** has the upper layer closed the transport? */
325 gpr_uint8 closed;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700326
Craig Tillerd20efd22015-06-12 16:17:09 -0700327 /** is a thread currently writing */
328 gpr_uint8 writing_active;
Craig Tiller606d8742015-06-15 06:58:50 -0700329 /** is a thread currently parsing */
330 gpr_uint8 parsing_active;
Craig Tillerd20efd22015-06-12 16:17:09 -0700331
Craig Tiller606d8742015-06-15 06:58:50 -0700332 /** is there a read request to the endpoint outstanding? */
333 gpr_uint8 endpoint_reading;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700334
Craig Tiller606d8742015-06-15 06:58:50 -0700335 /** various lists of streams */
Craig Tillerb084d902015-06-12 07:50:02 -0700336 grpc_chttp2_stream_list lists[STREAM_LIST_COUNT];
Craig Tiller9b8671c2015-06-12 07:41:54 -0700337
Craig Tiller606d8742015-06-15 06:58:50 -0700338 /** global state for reading/writing */
Craig Tillerd20efd22015-06-12 16:17:09 -0700339 grpc_chttp2_transport_global global;
Craig Tiller606d8742015-06-15 06:58:50 -0700340 /** state only accessible by the chain of execution that
341 set writing_active=1 */
Craig Tillerd20efd22015-06-12 16:17:09 -0700342 grpc_chttp2_transport_writing writing;
Craig Tiller606d8742015-06-15 06:58:50 -0700343 /** state only accessible by the chain of execution that
344 set parsing_active=1 */
Craig Tillerd20efd22015-06-12 16:17:09 -0700345 grpc_chttp2_transport_parsing parsing;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700346
Craig Tiller606d8742015-06-15 06:58:50 -0700347 /** maps stream id to grpc_chttp2_stream objects;
348 owned by the parsing thread when parsing */
349 grpc_chttp2_stream_map parsing_stream_map;
350
351 /** streams created by the client (possibly during parsing);
352 merged with parsing_stream_map during unlock when no
353 parsing is occurring */
354 grpc_chttp2_stream_map new_stream_map;
355
Craig Tillerd20efd22015-06-12 16:17:09 -0700356 /** closure to execute writing */
357 grpc_iomgr_closure writing_action;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700358
Craig Tiller4aa71a12015-06-15 13:00:55 -0700359 /** address to place a newly accepted stream - set and unset by
Craig Tiller606d8742015-06-15 06:58:50 -0700360 grpc_chttp2_parsing_accept_stream; used by init_stream to
361 publish the accepted server stream */
362 grpc_chttp2_stream **accepting_stream;
363
Craig Tiller9b8671c2015-06-12 07:41:54 -0700364 struct {
365 /** is a thread currently performing channel callbacks */
366 gpr_uint8 executing;
367 /** transport channel-level callback */
368 const grpc_transport_callbacks *cb;
369 /** user data for cb calls */
370 void *cb_user_data;
371 /** closure for notifying transport closure */
372 grpc_iomgr_closure notify_closed;
373 } channel_callback;
Craig Tiller606d8742015-06-15 06:58:50 -0700374
375#if 0
376 /* basic state management - what are we doing at the moment? */
377 gpr_uint8 reading;
378 /** are we calling back any grpc_transport_op completion events */
379 gpr_uint8 calling_back_ops;
380 gpr_uint8 destroying;
381 gpr_uint8 closed;
382
383 /* stream indexing */
384 gpr_uint32 next_stream_id;
385
386 /* window management */
387 gpr_uint32 outgoing_window_update;
388
389 /* state for a stream that's not yet been created */
390 grpc_stream_op_buffer new_stream_sopb;
391
392 /* stream ops that need to be destroyed, but outside of the lock */
393 grpc_stream_op_buffer nuke_later_sopb;
394
395 /* pings */
396 gpr_int64 ping_counter;
397
398
399 grpc_chttp2_stream **accepting_stream;
400
401#endif
Craig Tiller9b8671c2015-06-12 07:41:54 -0700402};
403
Craig Tillerd20efd22015-06-12 16:17:09 -0700404typedef struct {
405 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
Craig Tiller9b8671c2015-06-12 07:41:54 -0700406 gpr_uint32 id;
407
Craig Tillerd20efd22015-06-12 16:17:09 -0700408 grpc_iomgr_closure *send_done_closure;
409 grpc_iomgr_closure *recv_done_closure;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700410
Craig Tillerd20efd22015-06-12 16:17:09 -0700411 /** window available for us to send to peer */
412 gpr_int64 outgoing_window;
Craig Tiller3719f072015-06-12 17:19:51 -0700413 /** window available for peer to send to us - updated after parse */
414 gpr_uint32 incoming_window;
Craig Tillerd20efd22015-06-12 16:17:09 -0700415 /** stream ops the transport user would like to send */
416 grpc_stream_op_buffer *outgoing_sopb;
417 /** when the application requests writes be closed, the write_closed is
418 'queued'; when the close is flow controlled into the send path, we are
419 'sending' it; when the write has been performed it is 'sent' */
420 grpc_chttp2_write_state write_state;
421 /** is this stream closed (boolean) */
422 gpr_uint8 read_closed;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700423 /** has this stream been cancelled? (boolean) */
424 gpr_uint8 cancelled;
425 /** is this stream in the stream map? (boolean) */
426 gpr_uint8 in_stream_map;
Craig Tiller606d8742015-06-15 06:58:50 -0700427
428 /** stream state already published to the upper layer */
429 grpc_stream_state published_state;
430 /** address to publish next stream state to */
431 grpc_stream_state *publish_state;
432 /** pointer to sop buffer to fill in with new stream ops */
Craig Tiller6905b7c2015-06-16 15:33:33 -0700433 grpc_stream_op_buffer *publish_sopb;
434 grpc_stream_op_buffer incoming_sopb;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700435
436 /** incoming metadata */
437 grpc_chttp2_incoming_metadata_buffer incoming_metadata;
Craig Tillerb787c502015-06-15 16:14:52 -0700438 grpc_chttp2_incoming_metadata_live_op_buffer outstanding_metadata;
Craig Tillerd20efd22015-06-12 16:17:09 -0700439} grpc_chttp2_stream_global;
440
441typedef struct {
442 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
443 gpr_uint32 id;
444 /** sops that have passed flow control to be written */
445 grpc_stream_op_buffer sopb;
446 /** how strongly should we indicate closure with the next write */
447 grpc_chttp2_send_closed send_closed;
448} grpc_chttp2_stream_writing;
449
450struct grpc_chttp2_stream_parsing {
451 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
452 gpr_uint32 id;
453 /** has this stream received a close */
454 gpr_uint8 received_close;
Craig Tiller3719f072015-06-12 17:19:51 -0700455 /** saw a rst_stream */
456 gpr_uint8 saw_rst_stream;
Craig Tiller606d8742015-06-15 06:58:50 -0700457 /** incoming_window has been reduced by this much during parsing */
458 gpr_uint32 incoming_window_delta;
Craig Tillerd20efd22015-06-12 16:17:09 -0700459 /** window available for peer to send to us */
460 gpr_uint32 incoming_window;
461 /** parsing state for data frames */
462 grpc_chttp2_data_parser data_parser;
Craig Tiller3719f072015-06-12 17:19:51 -0700463 /** reason give to rst_stream */
464 gpr_uint32 rst_stream_reason;
465 /* amount of window given */
466 gpr_uint64 outgoing_window_update;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700467
Craig Tiller5dc3b302015-06-15 16:06:50 -0700468 /** incoming metadata */
469 grpc_chttp2_incoming_metadata_buffer incoming_metadata;
470
Craig Tiller98505102015-06-16 11:33:15 -0700471 /*
472 grpc_linked_mdelem *incoming_metadata;
473 size_t incoming_metadata_count;
474 size_t incoming_metadata_capacity;
475 grpc_linked_mdelem *old_incoming_metadata;
476 gpr_timespec incoming_deadline;
477 */
Craig Tillerd20efd22015-06-12 16:17:09 -0700478};
479
480struct grpc_chttp2_stream {
481 grpc_chttp2_stream_global global;
482 grpc_chttp2_stream_writing writing;
Craig Tiller606d8742015-06-15 06:58:50 -0700483 grpc_chttp2_stream_parsing parsing;
Craig Tillerd20efd22015-06-12 16:17:09 -0700484
485 grpc_chttp2_stream_link links[STREAM_LIST_COUNT];
486 gpr_uint8 included[STREAM_LIST_COUNT];
Craig Tiller9b8671c2015-06-12 07:41:54 -0700487
Craig Tiller606d8742015-06-15 06:58:50 -0700488#if 0
489 gpr_uint32 outgoing_window_update;
490 gpr_uint8 cancelled;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700491
Craig Tiller9b8671c2015-06-12 07:41:54 -0700492 grpc_stream_state callback_state;
493 grpc_stream_op_buffer callback_sopb;
Craig Tiller606d8742015-06-15 06:58:50 -0700494#endif
Craig Tiller9b8671c2015-06-12 07:41:54 -0700495};
496
Craig Tillerd20efd22015-06-12 16:17:09 -0700497/** Transport writing call flow:
Craig Tiller4aa71a12015-06-15 13:00:55 -0700498 chttp2_transport.c calls grpc_chttp2_unlocking_check_writes to see if writes
499 are required;
500 if they are, chttp2_transport.c calls grpc_chttp2_perform_writes to do the
501 writes.
502 Once writes have been completed (meaning another write could potentially be
503 started),
504 grpc_chttp2_terminate_writing is called. This will call
505 grpc_chttp2_cleanup_writing, at which
Craig Tillerd20efd22015-06-12 16:17:09 -0700506 point the write phase is complete. */
507
Craig Tiller3208e392015-06-12 08:17:02 -0700508/** Someone is unlocking the transport mutex: check to see if writes
509 are required, and schedule them if so */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700510int grpc_chttp2_unlocking_check_writes(grpc_chttp2_transport_global *global,
511 grpc_chttp2_transport_writing *writing);
512void grpc_chttp2_perform_writes(
513 grpc_chttp2_transport_writing *transport_writing, grpc_endpoint *endpoint);
514void grpc_chttp2_terminate_writing(
515 grpc_chttp2_transport_writing *transport_writing, int success);
516void grpc_chttp2_cleanup_writing(grpc_chttp2_transport_global *global,
517 grpc_chttp2_transport_writing *writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700518
Craig Tiller4aa71a12015-06-15 13:00:55 -0700519void grpc_chttp2_prepare_to_read(grpc_chttp2_transport_global *global,
520 grpc_chttp2_transport_parsing *parsing);
Craig Tiller1937b062015-06-16 08:47:38 -0700521/** Process one slice of incoming data */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700522int grpc_chttp2_perform_read(grpc_chttp2_transport_parsing *transport_parsing,
523 gpr_slice slice);
524void grpc_chttp2_publish_reads(grpc_chttp2_transport_global *global,
525 grpc_chttp2_transport_parsing *parsing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700526
527/** Get a writable stream
528 \return non-zero if there was a stream available */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700529void grpc_chttp2_list_add_writable_stream(
530 grpc_chttp2_transport_global *transport_global,
531 grpc_chttp2_stream_global *stream_global);
532int grpc_chttp2_list_pop_writable_stream(
533 grpc_chttp2_transport_global *transport_global,
534 grpc_chttp2_transport_writing *transport_writing,
535 grpc_chttp2_stream_global **stream_global,
536 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700537
Craig Tillercf1e3192015-06-16 14:28:22 -0700538void grpc_chttp2_list_add_incoming_window_updated(
539 grpc_chttp2_transport_global *transport_global,
540 grpc_chttp2_stream_global *stream_global);
541int grpc_chttp2_list_pop_incoming_window_updated(
542 grpc_chttp2_transport_global *transport_global,
543 grpc_chttp2_transport_parsing *transport_parsing,
544 grpc_chttp2_stream_global **stream_global,
545 grpc_chttp2_stream_parsing **stream_parsing);
546
Craig Tiller4aa71a12015-06-15 13:00:55 -0700547void grpc_chttp2_list_add_writing_stream(
548 grpc_chttp2_transport_writing *transport_writing,
549 grpc_chttp2_stream_writing *stream_writing);
550int grpc_chttp2_list_have_writing_streams(
551 grpc_chttp2_transport_writing *transport_writing);
552int grpc_chttp2_list_pop_writing_stream(
553 grpc_chttp2_transport_writing *transport_writing,
554 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700555
Craig Tiller4aa71a12015-06-15 13:00:55 -0700556void grpc_chttp2_list_add_written_stream(
557 grpc_chttp2_transport_writing *transport_writing,
558 grpc_chttp2_stream_writing *stream_writing);
559int grpc_chttp2_list_pop_written_stream(
560 grpc_chttp2_transport_global *transport_global,
561 grpc_chttp2_transport_writing *transport_writing,
562 grpc_chttp2_stream_global **stream_global,
563 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700564
Craig Tiller4aa71a12015-06-15 13:00:55 -0700565void grpc_chttp2_list_add_writable_window_update_stream(
566 grpc_chttp2_transport_global *transport_global,
567 grpc_chttp2_stream_global *stream_global);
568int grpc_chttp2_list_pop_writable_window_update_stream(
569 grpc_chttp2_transport_global *transport_global,
570 grpc_chttp2_stream_global **stream_global);
Craig Tillerd20efd22015-06-12 16:17:09 -0700571
Craig Tiller4aa71a12015-06-15 13:00:55 -0700572void grpc_chttp2_list_add_parsing_seen_stream(
573 grpc_chttp2_transport_parsing *transport_parsing,
574 grpc_chttp2_stream_parsing *stream_parsing);
575int grpc_chttp2_list_pop_parsing_seen_stream(
576 grpc_chttp2_transport_global *transport_global,
577 grpc_chttp2_transport_parsing *transport_parsing,
578 grpc_chttp2_stream_global **stream_global,
579 grpc_chttp2_stream_parsing **stream_parsing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700580
Craig Tiller5dc3b302015-06-15 16:06:50 -0700581void grpc_chttp2_list_add_waiting_for_concurrency(
582 grpc_chttp2_transport_global *transport_global,
583 grpc_chttp2_stream_global *stream_global);
584int grpc_chttp2_list_pop_waiting_for_concurrency(
585 grpc_chttp2_transport_global *transport_global,
586 grpc_chttp2_stream_global **stream_global);
587
Craig Tiller83fb0702015-06-16 21:13:07 -0700588void grpc_chttp2_list_add_closed_waiting_for_parsing(
Craig Tiller5dc3b302015-06-15 16:06:50 -0700589 grpc_chttp2_transport_global *transport_global,
590 grpc_chttp2_stream_global *stream_global);
Craig Tiller83fb0702015-06-16 21:13:07 -0700591int grpc_chttp2_list_pop_closed_waiting_for_parsing(
Craig Tiller5dc3b302015-06-15 16:06:50 -0700592 grpc_chttp2_transport_global *transport_global,
593 grpc_chttp2_stream_global **stream_global);
594
Craig Tiller6459db42015-06-15 17:11:45 -0700595void grpc_chttp2_list_add_read_write_state_changed(
596 grpc_chttp2_transport_global *transport_global,
597 grpc_chttp2_stream_global *stream_global);
Craig Tillerf73fcd12015-06-16 16:25:26 -0700598int grpc_chttp2_list_pop_read_write_state_changed(
599 grpc_chttp2_transport_global *transport_global,
600 grpc_chttp2_stream_global **stream_global);
Craig Tiller6459db42015-06-15 17:11:45 -0700601
602void grpc_chttp2_list_add_incoming_window_state_changed(
603 grpc_chttp2_transport_global *transport_global,
604 grpc_chttp2_stream_global *stream_global);
605
Craig Tiller1937b062015-06-16 08:47:38 -0700606/** schedule a closure to run without the transport lock taken */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700607void grpc_chttp2_schedule_closure(
608 grpc_chttp2_transport_global *transport_global, grpc_iomgr_closure *closure,
609 int success);
Craig Tillerd20efd22015-06-12 16:17:09 -0700610
Craig Tiller4aa71a12015-06-15 13:00:55 -0700611grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream(
612 grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
Craig Tiller83fb0702015-06-16 21:13:07 -0700613void grpc_chttp2_parsing_remove_stream(
614 grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
Craig Tiller4aa71a12015-06-15 13:00:55 -0700615grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream(
616 grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
Craig Tillerd20efd22015-06-12 16:17:09 -0700617
Craig Tiller98505102015-06-16 11:33:15 -0700618void grpc_chttp2_add_incoming_goaway(
619 grpc_chttp2_transport_global *transport_global, gpr_uint32 goaway_error,
620 gpr_slice goaway_text);
Craig Tiller4aa71a12015-06-15 13:00:55 -0700621
Craig Tiller98505102015-06-16 11:33:15 -0700622void grpc_chttp2_remove_from_stream_map(
623 grpc_chttp2_transport_global *transport_global,
624 grpc_chttp2_stream_global *stream_global);
Craig Tiller5dc3b302015-06-15 16:06:50 -0700625
Craig Tiller98505102015-06-16 11:33:15 -0700626void grpc_chttp2_register_stream(grpc_chttp2_transport *t,
627 grpc_chttp2_stream *s);
628void grpc_chttp2_unregister_stream(grpc_chttp2_transport *t,
629 grpc_chttp2_stream *s);
630void grpc_chttp2_for_all_streams(
631 grpc_chttp2_transport_global *transport_global, void *user_data,
632 void (*cb)(grpc_chttp2_transport_global *transport_global, void *user_data,
633 grpc_chttp2_stream_global *stream_global));
Craig Tiller5dc3b302015-06-15 16:06:50 -0700634
Craig Tillerd20efd22015-06-12 16:17:09 -0700635#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 -0700636#define GRPC_CHTTP2_CLIENT_CONNECT_STRLEN \
637 (sizeof(GRPC_CHTTP2_CLIENT_CONNECT_STRING) - 1)
Craig Tillerd20efd22015-06-12 16:17:09 -0700638
639extern int grpc_http_trace;
640
641#define IF_TRACING(stmt) \
642 if (!(grpc_http_trace)) \
643 ; \
644 else \
645 stmt
Craig Tiller3208e392015-06-12 08:17:02 -0700646
Craig Tiller9b8671c2015-06-12 07:41:54 -0700647#endif