blob: 644523e810469ae13e6728466484e84b230c4789 [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;
221} grpc_chttp2_transport_writing;
222
223struct grpc_chttp2_transport_parsing {
224 /** is this transport a client? (boolean) */
225 gpr_uint8 is_client;
226
227 /** were settings updated? */
228 gpr_uint8 settings_updated;
229 /** was a settings ack received? */
230 gpr_uint8 settings_ack_received;
231 /** was a goaway frame received? */
232 gpr_uint8 goaway_received;
233
Craig Tiller3719f072015-06-12 17:19:51 -0700234 /** initial window change */
235 gpr_int64 initial_window_update;
236
Craig Tillerd20efd22015-06-12 16:17:09 -0700237 /** data to write later - after parsing */
238 gpr_slice_buffer qbuf;
239 /* metadata object cache */
240 grpc_mdstr *str_grpc_timeout;
241 /** parser for headers */
242 grpc_chttp2_hpack_parser hpack_parser;
243 /** simple one shot parsers */
244 union {
245 grpc_chttp2_window_update_parser window_update;
246 grpc_chttp2_settings_parser settings;
247 grpc_chttp2_ping_parser ping;
248 grpc_chttp2_rst_stream_parser rst_stream;
249 } simple;
250 /** parser for goaway frames */
251 grpc_chttp2_goaway_parser goaway_parser;
252
253 /** window available for peer to send to us */
254 gpr_uint32 incoming_window;
255
256 /** next stream id available at the time of beginning parsing */
257 gpr_uint32 next_stream_id;
258 gpr_uint32 last_incoming_stream_id;
259
260 /* deframing */
261 grpc_chttp2_deframe_transport_state deframe_state;
262 gpr_uint8 incoming_frame_type;
263 gpr_uint8 incoming_frame_flags;
264 gpr_uint8 header_eof;
265 gpr_uint32 expect_continuation_stream_id;
266 gpr_uint32 incoming_frame_size;
267 gpr_uint32 incoming_stream_id;
268
269 /* active parser */
270 void *parser_data;
271 grpc_chttp2_stream_parsing *incoming_stream;
Craig Tiller4aa71a12015-06-15 13:00:55 -0700272 grpc_chttp2_parse_error (*parser)(
273 void *parser_user_data, grpc_chttp2_transport_parsing *transport_parsing,
274 grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last);
Craig Tillerd20efd22015-06-12 16:17:09 -0700275
276 /* received settings */
277 gpr_uint32 settings[GRPC_CHTTP2_NUM_SETTINGS];
278
279 /* goaway data */
280 grpc_status_code goaway_error;
281 gpr_uint32 goaway_last_stream_index;
282 gpr_slice goaway_text;
Craig Tiller3719f072015-06-12 17:19:51 -0700283
284 gpr_uint64 outgoing_window_update;
285
286 /** pings awaiting responses */
287 grpc_chttp2_outstanding_ping pings;
Craig Tillerd20efd22015-06-12 16:17:09 -0700288};
289
Craig Tillerb084d902015-06-12 07:50:02 -0700290struct grpc_chttp2_transport {
Craig Tiller9b8671c2015-06-12 07:41:54 -0700291 grpc_transport base; /* must be first */
292 grpc_endpoint *ep;
293 grpc_mdctx *metadata_context;
294 gpr_refcount refs;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700295
296 gpr_mu mu;
Craig Tiller606d8742015-06-15 06:58:50 -0700297
298 /** is the transport destroying itself? */
299 gpr_uint8 destroying;
300 /** has the upper layer closed the transport? */
301 gpr_uint8 closed;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700302
Craig Tillerd20efd22015-06-12 16:17:09 -0700303 /** is a thread currently writing */
304 gpr_uint8 writing_active;
Craig Tiller606d8742015-06-15 06:58:50 -0700305 /** is a thread currently parsing */
306 gpr_uint8 parsing_active;
Craig Tillerd20efd22015-06-12 16:17:09 -0700307
Craig Tiller606d8742015-06-15 06:58:50 -0700308 /** is there a read request to the endpoint outstanding? */
309 gpr_uint8 endpoint_reading;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700310
Craig Tiller606d8742015-06-15 06:58:50 -0700311 /** various lists of streams */
Craig Tillerb084d902015-06-12 07:50:02 -0700312 grpc_chttp2_stream_list lists[STREAM_LIST_COUNT];
Craig Tiller9b8671c2015-06-12 07:41:54 -0700313
Craig Tiller606d8742015-06-15 06:58:50 -0700314 /** global state for reading/writing */
Craig Tillerd20efd22015-06-12 16:17:09 -0700315 grpc_chttp2_transport_global global;
Craig Tiller606d8742015-06-15 06:58:50 -0700316 /** state only accessible by the chain of execution that
317 set writing_active=1 */
Craig Tillerd20efd22015-06-12 16:17:09 -0700318 grpc_chttp2_transport_writing writing;
Craig Tiller606d8742015-06-15 06:58:50 -0700319 /** state only accessible by the chain of execution that
320 set parsing_active=1 */
Craig Tillerd20efd22015-06-12 16:17:09 -0700321 grpc_chttp2_transport_parsing parsing;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700322
Craig Tiller606d8742015-06-15 06:58:50 -0700323 /** maps stream id to grpc_chttp2_stream objects;
324 owned by the parsing thread when parsing */
325 grpc_chttp2_stream_map parsing_stream_map;
326
327 /** streams created by the client (possibly during parsing);
328 merged with parsing_stream_map during unlock when no
329 parsing is occurring */
330 grpc_chttp2_stream_map new_stream_map;
331
Craig Tillerd20efd22015-06-12 16:17:09 -0700332 /** closure to execute writing */
333 grpc_iomgr_closure writing_action;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700334
Craig Tiller4aa71a12015-06-15 13:00:55 -0700335 /** address to place a newly accepted stream - set and unset by
Craig Tiller606d8742015-06-15 06:58:50 -0700336 grpc_chttp2_parsing_accept_stream; used by init_stream to
337 publish the accepted server stream */
338 grpc_chttp2_stream **accepting_stream;
339
Craig Tiller9b8671c2015-06-12 07:41:54 -0700340 struct {
341 /** is a thread currently performing channel callbacks */
342 gpr_uint8 executing;
343 /** transport channel-level callback */
344 const grpc_transport_callbacks *cb;
345 /** user data for cb calls */
346 void *cb_user_data;
347 /** closure for notifying transport closure */
348 grpc_iomgr_closure notify_closed;
349 } channel_callback;
Craig Tiller606d8742015-06-15 06:58:50 -0700350
351#if 0
352 /* basic state management - what are we doing at the moment? */
353 gpr_uint8 reading;
354 /** are we calling back any grpc_transport_op completion events */
355 gpr_uint8 calling_back_ops;
356 gpr_uint8 destroying;
357 gpr_uint8 closed;
358
359 /* stream indexing */
360 gpr_uint32 next_stream_id;
361
362 /* window management */
363 gpr_uint32 outgoing_window_update;
364
365 /* state for a stream that's not yet been created */
366 grpc_stream_op_buffer new_stream_sopb;
367
368 /* stream ops that need to be destroyed, but outside of the lock */
369 grpc_stream_op_buffer nuke_later_sopb;
370
371 /* pings */
372 gpr_int64 ping_counter;
373
374
375 grpc_chttp2_stream **accepting_stream;
376
377#endif
Craig Tiller9b8671c2015-06-12 07:41:54 -0700378};
379
Craig Tillerd20efd22015-06-12 16:17:09 -0700380typedef struct {
381 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
Craig Tiller9b8671c2015-06-12 07:41:54 -0700382 gpr_uint32 id;
383
Craig Tillerd20efd22015-06-12 16:17:09 -0700384 grpc_iomgr_closure *send_done_closure;
385 grpc_iomgr_closure *recv_done_closure;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700386
Craig Tillerd20efd22015-06-12 16:17:09 -0700387 /** window available for us to send to peer */
388 gpr_int64 outgoing_window;
Craig Tiller3719f072015-06-12 17:19:51 -0700389 /** window available for peer to send to us - updated after parse */
390 gpr_uint32 incoming_window;
Craig Tillerd20efd22015-06-12 16:17:09 -0700391 /** stream ops the transport user would like to send */
392 grpc_stream_op_buffer *outgoing_sopb;
393 /** when the application requests writes be closed, the write_closed is
394 'queued'; when the close is flow controlled into the send path, we are
395 'sending' it; when the write has been performed it is 'sent' */
396 grpc_chttp2_write_state write_state;
397 /** is this stream closed (boolean) */
398 gpr_uint8 read_closed;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700399 /** has this stream been cancelled? (boolean) */
400 gpr_uint8 cancelled;
401 /** is this stream in the stream map? (boolean) */
402 gpr_uint8 in_stream_map;
Craig Tiller606d8742015-06-15 06:58:50 -0700403
404 /** stream state already published to the upper layer */
405 grpc_stream_state published_state;
406 /** address to publish next stream state to */
407 grpc_stream_state *publish_state;
408 /** pointer to sop buffer to fill in with new stream ops */
Craig Tiller6905b7c2015-06-16 15:33:33 -0700409 grpc_stream_op_buffer *publish_sopb;
410 grpc_stream_op_buffer incoming_sopb;
Craig Tiller5dc3b302015-06-15 16:06:50 -0700411
412 /** incoming metadata */
413 grpc_chttp2_incoming_metadata_buffer incoming_metadata;
Craig Tillerb787c502015-06-15 16:14:52 -0700414 grpc_chttp2_incoming_metadata_live_op_buffer outstanding_metadata;
Craig Tillerd20efd22015-06-12 16:17:09 -0700415} grpc_chttp2_stream_global;
416
417typedef struct {
418 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
419 gpr_uint32 id;
420 /** sops that have passed flow control to be written */
421 grpc_stream_op_buffer sopb;
422 /** how strongly should we indicate closure with the next write */
423 grpc_chttp2_send_closed send_closed;
424} grpc_chttp2_stream_writing;
425
426struct grpc_chttp2_stream_parsing {
427 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
428 gpr_uint32 id;
429 /** has this stream received a close */
430 gpr_uint8 received_close;
Craig Tiller3719f072015-06-12 17:19:51 -0700431 /** saw a rst_stream */
432 gpr_uint8 saw_rst_stream;
Craig Tiller606d8742015-06-15 06:58:50 -0700433 /** incoming_window has been reduced by this much during parsing */
434 gpr_uint32 incoming_window_delta;
Craig Tillerd20efd22015-06-12 16:17:09 -0700435 /** window available for peer to send to us */
436 gpr_uint32 incoming_window;
437 /** parsing state for data frames */
438 grpc_chttp2_data_parser data_parser;
Craig Tiller3719f072015-06-12 17:19:51 -0700439 /** reason give to rst_stream */
440 gpr_uint32 rst_stream_reason;
441 /* amount of window given */
442 gpr_uint64 outgoing_window_update;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700443
Craig Tiller5dc3b302015-06-15 16:06:50 -0700444 /** incoming metadata */
445 grpc_chttp2_incoming_metadata_buffer incoming_metadata;
446
Craig Tiller98505102015-06-16 11:33:15 -0700447 /*
448 grpc_linked_mdelem *incoming_metadata;
449 size_t incoming_metadata_count;
450 size_t incoming_metadata_capacity;
451 grpc_linked_mdelem *old_incoming_metadata;
452 gpr_timespec incoming_deadline;
453 */
Craig Tillerd20efd22015-06-12 16:17:09 -0700454};
455
456struct grpc_chttp2_stream {
457 grpc_chttp2_stream_global global;
458 grpc_chttp2_stream_writing writing;
Craig Tiller606d8742015-06-15 06:58:50 -0700459 grpc_chttp2_stream_parsing parsing;
Craig Tillerd20efd22015-06-12 16:17:09 -0700460
461 grpc_chttp2_stream_link links[STREAM_LIST_COUNT];
462 gpr_uint8 included[STREAM_LIST_COUNT];
Craig Tiller9b8671c2015-06-12 07:41:54 -0700463
Craig Tiller606d8742015-06-15 06:58:50 -0700464#if 0
465 gpr_uint32 outgoing_window_update;
466 gpr_uint8 cancelled;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700467
Craig Tiller9b8671c2015-06-12 07:41:54 -0700468 grpc_stream_state callback_state;
469 grpc_stream_op_buffer callback_sopb;
Craig Tiller606d8742015-06-15 06:58:50 -0700470#endif
Craig Tiller9b8671c2015-06-12 07:41:54 -0700471};
472
Craig Tillerd20efd22015-06-12 16:17:09 -0700473/** Transport writing call flow:
Craig Tiller4aa71a12015-06-15 13:00:55 -0700474 chttp2_transport.c calls grpc_chttp2_unlocking_check_writes to see if writes
475 are required;
476 if they are, chttp2_transport.c calls grpc_chttp2_perform_writes to do the
477 writes.
478 Once writes have been completed (meaning another write could potentially be
479 started),
480 grpc_chttp2_terminate_writing is called. This will call
481 grpc_chttp2_cleanup_writing, at which
Craig Tillerd20efd22015-06-12 16:17:09 -0700482 point the write phase is complete. */
483
Craig Tiller3208e392015-06-12 08:17:02 -0700484/** Someone is unlocking the transport mutex: check to see if writes
485 are required, and schedule them if so */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700486int grpc_chttp2_unlocking_check_writes(grpc_chttp2_transport_global *global,
487 grpc_chttp2_transport_writing *writing);
488void grpc_chttp2_perform_writes(
489 grpc_chttp2_transport_writing *transport_writing, grpc_endpoint *endpoint);
490void grpc_chttp2_terminate_writing(
491 grpc_chttp2_transport_writing *transport_writing, int success);
492void grpc_chttp2_cleanup_writing(grpc_chttp2_transport_global *global,
493 grpc_chttp2_transport_writing *writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700494
Craig Tiller4aa71a12015-06-15 13:00:55 -0700495void grpc_chttp2_prepare_to_read(grpc_chttp2_transport_global *global,
496 grpc_chttp2_transport_parsing *parsing);
Craig Tiller1937b062015-06-16 08:47:38 -0700497/** Process one slice of incoming data */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700498int grpc_chttp2_perform_read(grpc_chttp2_transport_parsing *transport_parsing,
499 gpr_slice slice);
500void grpc_chttp2_publish_reads(grpc_chttp2_transport_global *global,
501 grpc_chttp2_transport_parsing *parsing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700502
503/** Get a writable stream
504 \return non-zero if there was a stream available */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700505void grpc_chttp2_list_add_writable_stream(
506 grpc_chttp2_transport_global *transport_global,
507 grpc_chttp2_stream_global *stream_global);
508int grpc_chttp2_list_pop_writable_stream(
509 grpc_chttp2_transport_global *transport_global,
510 grpc_chttp2_transport_writing *transport_writing,
511 grpc_chttp2_stream_global **stream_global,
512 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700513
Craig Tillercf1e3192015-06-16 14:28:22 -0700514void grpc_chttp2_list_add_incoming_window_updated(
515 grpc_chttp2_transport_global *transport_global,
516 grpc_chttp2_stream_global *stream_global);
517int grpc_chttp2_list_pop_incoming_window_updated(
518 grpc_chttp2_transport_global *transport_global,
519 grpc_chttp2_transport_parsing *transport_parsing,
520 grpc_chttp2_stream_global **stream_global,
521 grpc_chttp2_stream_parsing **stream_parsing);
Craig Tiller759eb322015-06-16 22:41:18 -0700522void grpc_chttp2_list_remove_incoming_window_updated(
523 grpc_chttp2_transport_global *transport_global,
524 grpc_chttp2_stream_global *stream_global);
Craig Tillercf1e3192015-06-16 14:28:22 -0700525
Craig Tiller4aa71a12015-06-15 13:00:55 -0700526void grpc_chttp2_list_add_writing_stream(
527 grpc_chttp2_transport_writing *transport_writing,
528 grpc_chttp2_stream_writing *stream_writing);
529int grpc_chttp2_list_have_writing_streams(
530 grpc_chttp2_transport_writing *transport_writing);
531int grpc_chttp2_list_pop_writing_stream(
532 grpc_chttp2_transport_writing *transport_writing,
533 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700534
Craig Tiller4aa71a12015-06-15 13:00:55 -0700535void grpc_chttp2_list_add_written_stream(
536 grpc_chttp2_transport_writing *transport_writing,
537 grpc_chttp2_stream_writing *stream_writing);
538int grpc_chttp2_list_pop_written_stream(
539 grpc_chttp2_transport_global *transport_global,
540 grpc_chttp2_transport_writing *transport_writing,
541 grpc_chttp2_stream_global **stream_global,
542 grpc_chttp2_stream_writing **stream_writing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700543
Craig Tiller4aa71a12015-06-15 13:00:55 -0700544void grpc_chttp2_list_add_writable_window_update_stream(
545 grpc_chttp2_transport_global *transport_global,
546 grpc_chttp2_stream_global *stream_global);
547int grpc_chttp2_list_pop_writable_window_update_stream(
548 grpc_chttp2_transport_global *transport_global,
549 grpc_chttp2_stream_global **stream_global);
Craig Tillerd20efd22015-06-12 16:17:09 -0700550
Craig Tiller4aa71a12015-06-15 13:00:55 -0700551void grpc_chttp2_list_add_parsing_seen_stream(
552 grpc_chttp2_transport_parsing *transport_parsing,
553 grpc_chttp2_stream_parsing *stream_parsing);
554int grpc_chttp2_list_pop_parsing_seen_stream(
555 grpc_chttp2_transport_global *transport_global,
556 grpc_chttp2_transport_parsing *transport_parsing,
557 grpc_chttp2_stream_global **stream_global,
558 grpc_chttp2_stream_parsing **stream_parsing);
Craig Tillerd20efd22015-06-12 16:17:09 -0700559
Craig Tiller5dc3b302015-06-15 16:06:50 -0700560void grpc_chttp2_list_add_waiting_for_concurrency(
561 grpc_chttp2_transport_global *transport_global,
562 grpc_chttp2_stream_global *stream_global);
563int grpc_chttp2_list_pop_waiting_for_concurrency(
564 grpc_chttp2_transport_global *transport_global,
565 grpc_chttp2_stream_global **stream_global);
566
Craig Tiller83fb0702015-06-16 21:13:07 -0700567void grpc_chttp2_list_add_closed_waiting_for_parsing(
Craig Tiller5dc3b302015-06-15 16:06:50 -0700568 grpc_chttp2_transport_global *transport_global,
569 grpc_chttp2_stream_global *stream_global);
Craig Tiller83fb0702015-06-16 21:13:07 -0700570int grpc_chttp2_list_pop_closed_waiting_for_parsing(
Craig Tiller5dc3b302015-06-15 16:06:50 -0700571 grpc_chttp2_transport_global *transport_global,
572 grpc_chttp2_stream_global **stream_global);
573
Craig Tiller6459db42015-06-15 17:11:45 -0700574void grpc_chttp2_list_add_read_write_state_changed(
575 grpc_chttp2_transport_global *transport_global,
576 grpc_chttp2_stream_global *stream_global);
Craig Tillerf73fcd12015-06-16 16:25:26 -0700577int grpc_chttp2_list_pop_read_write_state_changed(
578 grpc_chttp2_transport_global *transport_global,
579 grpc_chttp2_stream_global **stream_global);
Craig Tiller6459db42015-06-15 17:11:45 -0700580
Craig Tiller1937b062015-06-16 08:47:38 -0700581/** schedule a closure to run without the transport lock taken */
Craig Tiller4aa71a12015-06-15 13:00:55 -0700582void grpc_chttp2_schedule_closure(
583 grpc_chttp2_transport_global *transport_global, grpc_iomgr_closure *closure,
584 int success);
Craig Tillerd20efd22015-06-12 16:17:09 -0700585
Craig Tiller4aa71a12015-06-15 13:00:55 -0700586grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream(
587 grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
588grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream(
589 grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
Craig Tillerd20efd22015-06-12 16:17:09 -0700590
Craig Tiller98505102015-06-16 11:33:15 -0700591void grpc_chttp2_add_incoming_goaway(
592 grpc_chttp2_transport_global *transport_global, gpr_uint32 goaway_error,
593 gpr_slice goaway_text);
Craig Tiller4aa71a12015-06-15 13:00:55 -0700594
Craig Tiller98505102015-06-16 11:33:15 -0700595void grpc_chttp2_register_stream(grpc_chttp2_transport *t,
596 grpc_chttp2_stream *s);
597void grpc_chttp2_unregister_stream(grpc_chttp2_transport *t,
598 grpc_chttp2_stream *s);
599void grpc_chttp2_for_all_streams(
600 grpc_chttp2_transport_global *transport_global, void *user_data,
601 void (*cb)(grpc_chttp2_transport_global *transport_global, void *user_data,
602 grpc_chttp2_stream_global *stream_global));
Craig Tiller5dc3b302015-06-15 16:06:50 -0700603
Craig Tiller66abdaa2015-06-17 08:00:39 -0700604void grpc_chttp2_parsing_become_skip_parser(grpc_chttp2_transport_parsing *transport_parsing);
605
Craig Tillerd20efd22015-06-12 16:17:09 -0700606#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 -0700607#define GRPC_CHTTP2_CLIENT_CONNECT_STRLEN \
608 (sizeof(GRPC_CHTTP2_CLIENT_CONNECT_STRING) - 1)
Craig Tillerd20efd22015-06-12 16:17:09 -0700609
610extern int grpc_http_trace;
611
612#define IF_TRACING(stmt) \
613 if (!(grpc_http_trace)) \
614 ; \
615 else \
616 stmt
Craig Tiller3208e392015-06-12 08:17:02 -0700617
Craig Tiller9b8671c2015-06-12 07:41:54 -0700618#endif