blob: 2b4dc5e6b8e175e77e89848c97b7b2c847878011 [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 Tiller9b8671c2015-06-12 07:41:54 -0700337
Craig Tiller4aa71a12015-06-15 13:00:55 -0700338 /** address to place a newly accepted stream - set and unset by
Craig Tiller606d8742015-06-15 06:58:50 -0700339 grpc_chttp2_parsing_accept_stream; used by init_stream to
340 publish the accepted server stream */
341 grpc_chttp2_stream **accepting_stream;
342
Craig Tiller9b8671c2015-06-12 07:41:54 -0700343 struct {
344 /** is a thread currently performing channel callbacks */
345 gpr_uint8 executing;
346 /** transport channel-level callback */
347 const grpc_transport_callbacks *cb;
348 /** user data for cb calls */
349 void *cb_user_data;
350 /** closure for notifying transport closure */
351 grpc_iomgr_closure notify_closed;
352 } channel_callback;
Craig Tiller606d8742015-06-15 06:58:50 -0700353
354#if 0
355 /* basic state management - what are we doing at the moment? */
356 gpr_uint8 reading;
357 /** are we calling back any grpc_transport_op completion events */
358 gpr_uint8 calling_back_ops;
359 gpr_uint8 destroying;
360 gpr_uint8 closed;
361
362 /* stream indexing */
363 gpr_uint32 next_stream_id;
364
365 /* window management */
366 gpr_uint32 outgoing_window_update;
367
368 /* state for a stream that's not yet been created */
369 grpc_stream_op_buffer new_stream_sopb;
370
371 /* stream ops that need to be destroyed, but outside of the lock */
372 grpc_stream_op_buffer nuke_later_sopb;
373
374 /* pings */
375 gpr_int64 ping_counter;
376
377
378 grpc_chttp2_stream **accepting_stream;
379
380#endif
Craig Tiller9b8671c2015-06-12 07:41:54 -0700381};
382
Craig Tillerd20efd22015-06-12 16:17:09 -0700383typedef struct {
384 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
Craig Tiller9b8671c2015-06-12 07:41:54 -0700385 gpr_uint32 id;
386
Craig Tillerd20efd22015-06-12 16:17:09 -0700387 grpc_iomgr_closure *send_done_closure;
388 grpc_iomgr_closure *recv_done_closure;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700389
Craig Tillerd20efd22015-06-12 16:17:09 -0700390 /** window available for us to send to peer */
391 gpr_int64 outgoing_window;
Craig Tiller3719f072015-06-12 17:19:51 -0700392 /** window available for peer to send to us - updated after parse */
393 gpr_uint32 incoming_window;
Craig Tillerd20efd22015-06-12 16:17:09 -0700394 /** stream ops the transport user would like to send */
395 grpc_stream_op_buffer *outgoing_sopb;
396 /** when the application requests writes be closed, the write_closed is
397 'queued'; when the close is flow controlled into the send path, we are
398 'sending' it; when the write has been performed it is 'sent' */
399 grpc_chttp2_write_state write_state;
400 /** is this stream closed (boolean) */
401 gpr_uint8 read_closed;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700402 /** has this stream been cancelled? (boolean) */
403 gpr_uint8 cancelled;
Craig Tillercec9eb92015-06-17 17:16:48 -0700404 grpc_status_code cancelled_status;
405 /** have we told the upper layer that this stream is cancelled? */
406 gpr_uint8 published_cancelled;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700407 /** is this stream in the stream map? (boolean) */
408 gpr_uint8 in_stream_map;
Craig Tiller606d8742015-06-15 06:58:50 -0700409
410 /** stream state already published to the upper layer */
411 grpc_stream_state published_state;
412 /** address to publish next stream state to */
413 grpc_stream_state *publish_state;
414 /** pointer to sop buffer to fill in with new stream ops */
Craig Tiller6905b7c2015-06-16 15:33:33 -0700415 grpc_stream_op_buffer *publish_sopb;
416 grpc_stream_op_buffer incoming_sopb;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700417
418 /** incoming metadata */
419 grpc_chttp2_incoming_metadata_buffer incoming_metadata;
Craig Tillerb787c502015-06-15 16:14:52 -0700420 grpc_chttp2_incoming_metadata_live_op_buffer outstanding_metadata;
Craig Tillerd20efd22015-06-12 16:17:09 -0700421} grpc_chttp2_stream_global;
422
423typedef struct {
424 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
425 gpr_uint32 id;
426 /** sops that have passed flow control to be written */
427 grpc_stream_op_buffer sopb;
428 /** how strongly should we indicate closure with the next write */
429 grpc_chttp2_send_closed send_closed;
430} grpc_chttp2_stream_writing;
431
432struct grpc_chttp2_stream_parsing {
433 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
434 gpr_uint32 id;
435 /** has this stream received a close */
436 gpr_uint8 received_close;
Craig Tiller3719f072015-06-12 17:19:51 -0700437 /** saw a rst_stream */
438 gpr_uint8 saw_rst_stream;
Craig Tiller606d8742015-06-15 06:58:50 -0700439 /** incoming_window has been reduced by this much during parsing */
440 gpr_uint32 incoming_window_delta;
Craig Tillerd20efd22015-06-12 16:17:09 -0700441 /** window available for peer to send to us */
442 gpr_uint32 incoming_window;
443 /** parsing state for data frames */
444 grpc_chttp2_data_parser data_parser;
Craig Tiller3719f072015-06-12 17:19:51 -0700445 /** reason give to rst_stream */
446 gpr_uint32 rst_stream_reason;
447 /* amount of window given */
448 gpr_uint64 outgoing_window_update;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700449
Craig Tiller5dc3b302015-06-15 16:06:50 -0700450 /** incoming metadata */
451 grpc_chttp2_incoming_metadata_buffer incoming_metadata;
452
Craig Tiller98505102015-06-16 11:33:15 -0700453 /*
454 grpc_linked_mdelem *incoming_metadata;
455 size_t incoming_metadata_count;
456 size_t incoming_metadata_capacity;
457 grpc_linked_mdelem *old_incoming_metadata;
458 gpr_timespec incoming_deadline;
459 */
Craig Tillerd20efd22015-06-12 16:17:09 -0700460};
461
462struct grpc_chttp2_stream {
463 grpc_chttp2_stream_global global;
464 grpc_chttp2_stream_writing writing;
Craig Tiller606d8742015-06-15 06:58:50 -0700465 grpc_chttp2_stream_parsing parsing;
Craig Tillerd20efd22015-06-12 16:17:09 -0700466
467 grpc_chttp2_stream_link links[STREAM_LIST_COUNT];
468 gpr_uint8 included[STREAM_LIST_COUNT];
Craig Tiller9b8671c2015-06-12 07:41:54 -0700469
Craig Tiller606d8742015-06-15 06:58:50 -0700470#if 0
471 gpr_uint32 outgoing_window_update;
472 gpr_uint8 cancelled;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700473
Craig Tiller9b8671c2015-06-12 07:41:54 -0700474 grpc_stream_state callback_state;
475 grpc_stream_op_buffer callback_sopb;
Craig Tiller606d8742015-06-15 06:58:50 -0700476#endif
Craig Tiller9b8671c2015-06-12 07:41:54 -0700477};
478
Craig Tillerd20efd22015-06-12 16:17:09 -0700479/** Transport writing call flow:
Craig Tiller4aa71a12015-06-15 13:00:55 -0700480 chttp2_transport.c calls grpc_chttp2_unlocking_check_writes to see if writes
481 are required;
482 if they are, chttp2_transport.c calls grpc_chttp2_perform_writes to do the
483 writes.
484 Once writes have been completed (meaning another write could potentially be
485 started),
486 grpc_chttp2_terminate_writing is called. This will call
487 grpc_chttp2_cleanup_writing, at which
Craig Tillerd20efd22015-06-12 16:17:09 -0700488 point the write phase is complete. */
489
Craig Tiller3208e392015-06-12 08:17:02 -0700490/** Someone is unlocking the transport mutex: check to see if writes
491 are required, and schedule them if so */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700492int grpc_chttp2_unlocking_check_writes(grpc_chttp2_transport_global *global,
493 grpc_chttp2_transport_writing *writing);
494void grpc_chttp2_perform_writes(
495 grpc_chttp2_transport_writing *transport_writing, grpc_endpoint *endpoint);
496void grpc_chttp2_terminate_writing(
497 grpc_chttp2_transport_writing *transport_writing, int success);
498void grpc_chttp2_cleanup_writing(grpc_chttp2_transport_global *global,
499 grpc_chttp2_transport_writing *writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700500
Craig Tiller4aa71a12015-06-15 13:00:55 -0700501void grpc_chttp2_prepare_to_read(grpc_chttp2_transport_global *global,
502 grpc_chttp2_transport_parsing *parsing);
Craig Tiller1937b062015-06-16 08:47:38 -0700503/** Process one slice of incoming data */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700504int grpc_chttp2_perform_read(grpc_chttp2_transport_parsing *transport_parsing,
505 gpr_slice slice);
506void grpc_chttp2_publish_reads(grpc_chttp2_transport_global *global,
507 grpc_chttp2_transport_parsing *parsing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700508
509/** Get a writable stream
510 \return non-zero if there was a stream available */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700511void grpc_chttp2_list_add_writable_stream(
512 grpc_chttp2_transport_global *transport_global,
513 grpc_chttp2_stream_global *stream_global);
514int grpc_chttp2_list_pop_writable_stream(
515 grpc_chttp2_transport_global *transport_global,
516 grpc_chttp2_transport_writing *transport_writing,
517 grpc_chttp2_stream_global **stream_global,
518 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700519
Craig Tillercf1e3192015-06-16 14:28:22 -0700520void grpc_chttp2_list_add_incoming_window_updated(
521 grpc_chttp2_transport_global *transport_global,
522 grpc_chttp2_stream_global *stream_global);
523int grpc_chttp2_list_pop_incoming_window_updated(
524 grpc_chttp2_transport_global *transport_global,
525 grpc_chttp2_transport_parsing *transport_parsing,
526 grpc_chttp2_stream_global **stream_global,
527 grpc_chttp2_stream_parsing **stream_parsing);
Craig Tiller759eb322015-06-16 22:41:18 -0700528void grpc_chttp2_list_remove_incoming_window_updated(
529 grpc_chttp2_transport_global *transport_global,
530 grpc_chttp2_stream_global *stream_global);
Craig Tillercf1e3192015-06-16 14:28:22 -0700531
Craig Tiller4aa71a12015-06-15 13:00:55 -0700532void grpc_chttp2_list_add_writing_stream(
533 grpc_chttp2_transport_writing *transport_writing,
534 grpc_chttp2_stream_writing *stream_writing);
535int grpc_chttp2_list_have_writing_streams(
536 grpc_chttp2_transport_writing *transport_writing);
537int grpc_chttp2_list_pop_writing_stream(
538 grpc_chttp2_transport_writing *transport_writing,
539 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700540
Craig Tiller4aa71a12015-06-15 13:00:55 -0700541void grpc_chttp2_list_add_written_stream(
542 grpc_chttp2_transport_writing *transport_writing,
543 grpc_chttp2_stream_writing *stream_writing);
544int grpc_chttp2_list_pop_written_stream(
545 grpc_chttp2_transport_global *transport_global,
546 grpc_chttp2_transport_writing *transport_writing,
547 grpc_chttp2_stream_global **stream_global,
548 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700549
Craig Tiller4aa71a12015-06-15 13:00:55 -0700550void grpc_chttp2_list_add_writable_window_update_stream(
551 grpc_chttp2_transport_global *transport_global,
552 grpc_chttp2_stream_global *stream_global);
553int grpc_chttp2_list_pop_writable_window_update_stream(
554 grpc_chttp2_transport_global *transport_global,
555 grpc_chttp2_stream_global **stream_global);
Craig Tillerd20efd22015-06-12 16:17:09 -0700556
Craig Tiller4aa71a12015-06-15 13:00:55 -0700557void grpc_chttp2_list_add_parsing_seen_stream(
558 grpc_chttp2_transport_parsing *transport_parsing,
559 grpc_chttp2_stream_parsing *stream_parsing);
560int grpc_chttp2_list_pop_parsing_seen_stream(
561 grpc_chttp2_transport_global *transport_global,
562 grpc_chttp2_transport_parsing *transport_parsing,
563 grpc_chttp2_stream_global **stream_global,
564 grpc_chttp2_stream_parsing **stream_parsing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700565
Craig Tiller5dc3b302015-06-15 16:06:50 -0700566void grpc_chttp2_list_add_waiting_for_concurrency(
567 grpc_chttp2_transport_global *transport_global,
568 grpc_chttp2_stream_global *stream_global);
569int grpc_chttp2_list_pop_waiting_for_concurrency(
570 grpc_chttp2_transport_global *transport_global,
571 grpc_chttp2_stream_global **stream_global);
572
Craig Tiller83fb0702015-06-16 21:13:07 -0700573void grpc_chttp2_list_add_closed_waiting_for_parsing(
Craig Tiller5dc3b302015-06-15 16:06:50 -0700574 grpc_chttp2_transport_global *transport_global,
575 grpc_chttp2_stream_global *stream_global);
Craig Tiller83fb0702015-06-16 21:13:07 -0700576int grpc_chttp2_list_pop_closed_waiting_for_parsing(
Craig Tiller5dc3b302015-06-15 16:06:50 -0700577 grpc_chttp2_transport_global *transport_global,
578 grpc_chttp2_stream_global **stream_global);
579
Craig Tiller6459db42015-06-15 17:11:45 -0700580void grpc_chttp2_list_add_read_write_state_changed(
581 grpc_chttp2_transport_global *transport_global,
582 grpc_chttp2_stream_global *stream_global);
Craig Tillerf73fcd12015-06-16 16:25:26 -0700583int grpc_chttp2_list_pop_read_write_state_changed(
584 grpc_chttp2_transport_global *transport_global,
585 grpc_chttp2_stream_global **stream_global);
Craig Tiller6459db42015-06-15 17:11:45 -0700586
Craig Tiller1937b062015-06-16 08:47:38 -0700587/** schedule a closure to run without the transport lock taken */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700588void grpc_chttp2_schedule_closure(
589 grpc_chttp2_transport_global *transport_global, grpc_iomgr_closure *closure,
590 int success);
Craig Tillerd20efd22015-06-12 16:17:09 -0700591
Craig Tiller4aa71a12015-06-15 13:00:55 -0700592grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream(
593 grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
594grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream(
595 grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
Craig Tillerd20efd22015-06-12 16:17:09 -0700596
Craig Tiller98505102015-06-16 11:33:15 -0700597void grpc_chttp2_add_incoming_goaway(
598 grpc_chttp2_transport_global *transport_global, gpr_uint32 goaway_error,
599 gpr_slice goaway_text);
Craig Tiller4aa71a12015-06-15 13:00:55 -0700600
Craig Tiller98505102015-06-16 11:33:15 -0700601void grpc_chttp2_register_stream(grpc_chttp2_transport *t,
602 grpc_chttp2_stream *s);
603void grpc_chttp2_unregister_stream(grpc_chttp2_transport *t,
604 grpc_chttp2_stream *s);
605void grpc_chttp2_for_all_streams(
606 grpc_chttp2_transport_global *transport_global, void *user_data,
607 void (*cb)(grpc_chttp2_transport_global *transport_global, void *user_data,
608 grpc_chttp2_stream_global *stream_global));
Craig Tiller5dc3b302015-06-15 16:06:50 -0700609
Craig Tiller285b8822015-06-17 15:58:13 -0700610void grpc_chttp2_parsing_become_skip_parser(
611 grpc_chttp2_transport_parsing *transport_parsing);
Craig Tiller66abdaa2015-06-17 08:00:39 -0700612
Craig Tillerd20efd22015-06-12 16:17:09 -0700613#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 -0700614#define GRPC_CHTTP2_CLIENT_CONNECT_STRLEN \
615 (sizeof(GRPC_CHTTP2_CLIENT_CONNECT_STRING) - 1)
Craig Tillerd20efd22015-06-12 16:17:09 -0700616
617extern int grpc_http_trace;
Craig Tiller285b8822015-06-17 15:58:13 -0700618extern int grpc_flowctl_trace;
Craig Tillerd20efd22015-06-12 16:17:09 -0700619
620#define IF_TRACING(stmt) \
621 if (!(grpc_http_trace)) \
622 ; \
623 else \
624 stmt
Craig Tiller3208e392015-06-12 08:17:02 -0700625
Craig Tiller285b8822015-06-17 15:58:13 -0700626#define GRPC_CHTTP2_FLOWCTL_TRACE_STREAM(reason, transport, context, var, \
627 delta) \
628 if (!(grpc_flowctl_trace)) { \
629 } else { \
630 grpc_chttp2_flowctl_trace(__FILE__, __LINE__, reason, #context, #var, \
631 transport->is_client, context->id, context->var, \
632 delta); \
633 }
634
635#define GRPC_CHTTP2_FLOWCTL_TRACE_TRANSPORT(reason, context, var, delta) \
636 if (!(grpc_flowctl_trace)) { \
637 } else { \
638 grpc_chttp2_flowctl_trace(__FILE__, __LINE__, reason, #context, #var, \
639 context->is_client, 0, context->var, delta); \
640 }
641
642void grpc_chttp2_flowctl_trace(const char *file, int line, const char *reason,
643 const char *context, const char *var,
644 int is_client, gpr_uint32 stream_id,
645 gpr_int64 current_value, gpr_int64 delta);
646
Craig Tiller9b8671c2015-06-12 07:41:54 -0700647#endif