blob: 5eba01a3e1be9e773f06f9a87e0ff51a0f87a5b9 [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"
46#include "src/core/transport/chttp2/stream_map.h"
47#include "src/core/transport/chttp2/hpack_parser.h"
48#include "src/core/transport/chttp2/stream_encoder.h"
Craig Tiller9b8671c2015-06-12 07:41:54 -070049
Craig Tillerb084d902015-06-12 07:50:02 -070050typedef struct grpc_chttp2_transport grpc_chttp2_transport;
51typedef struct grpc_chttp2_stream grpc_chttp2_stream;
Craig Tiller9b8671c2015-06-12 07:41:54 -070052
53/* streams are kept in various linked lists depending on what things need to
54 happen to them... this enum labels each list */
55typedef enum {
56 /* streams that have pending writes */
57 WRITABLE = 0,
58 /* streams that have been selected to be written */
59 WRITING,
60 /* streams that have just been written, and included a close */
61 WRITTEN_CLOSED,
62 /* streams that have been cancelled and have some pending state updates
63 to perform */
64 CANCELLED,
65 /* streams that want to send window updates */
66 WINDOW_UPDATE,
67 /* streams that are waiting to start because there are too many concurrent
68 streams on the connection */
69 WAITING_FOR_CONCURRENCY,
70 /* streams that have finished reading: we wait until unlock to coalesce
71 all changes into one callback */
72 FINISHED_READ_OP,
73 MAYBE_FINISH_READ_AFTER_PARSE,
74 PARSER_CHECK_WINDOW_UPDATES_AFTER_PARSE,
75 OTHER_CHECK_WINDOW_UPDATES_AFTER_PARSE,
76 NEW_OUTGOING_WINDOW,
77 STREAM_LIST_COUNT /* must be last */
Craig Tillerb084d902015-06-12 07:50:02 -070078} grpc_chttp2_stream_list_id;
Craig Tiller9b8671c2015-06-12 07:41:54 -070079
80/* deframer state for the overall http2 stream of bytes */
81typedef enum {
82 /* prefix: one entry per http2 connection prefix byte */
83 DTS_CLIENT_PREFIX_0 = 0,
84 DTS_CLIENT_PREFIX_1,
85 DTS_CLIENT_PREFIX_2,
86 DTS_CLIENT_PREFIX_3,
87 DTS_CLIENT_PREFIX_4,
88 DTS_CLIENT_PREFIX_5,
89 DTS_CLIENT_PREFIX_6,
90 DTS_CLIENT_PREFIX_7,
91 DTS_CLIENT_PREFIX_8,
92 DTS_CLIENT_PREFIX_9,
93 DTS_CLIENT_PREFIX_10,
94 DTS_CLIENT_PREFIX_11,
95 DTS_CLIENT_PREFIX_12,
96 DTS_CLIENT_PREFIX_13,
97 DTS_CLIENT_PREFIX_14,
98 DTS_CLIENT_PREFIX_15,
99 DTS_CLIENT_PREFIX_16,
100 DTS_CLIENT_PREFIX_17,
101 DTS_CLIENT_PREFIX_18,
102 DTS_CLIENT_PREFIX_19,
103 DTS_CLIENT_PREFIX_20,
104 DTS_CLIENT_PREFIX_21,
105 DTS_CLIENT_PREFIX_22,
106 DTS_CLIENT_PREFIX_23,
107 /* frame header byte 0... */
108 /* must follow from the prefix states */
109 DTS_FH_0,
110 DTS_FH_1,
111 DTS_FH_2,
112 DTS_FH_3,
113 DTS_FH_4,
114 DTS_FH_5,
115 DTS_FH_6,
116 DTS_FH_7,
117 /* ... frame header byte 8 */
118 DTS_FH_8,
119 /* inside a http2 frame */
120 DTS_FRAME
Craig Tillerb084d902015-06-12 07:50:02 -0700121} grpc_chttp2_deframe_transport_state;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700122
123typedef enum {
124 WRITE_STATE_OPEN,
125 WRITE_STATE_QUEUED_CLOSE,
126 WRITE_STATE_SENT_CLOSE
Craig Tillerb084d902015-06-12 07:50:02 -0700127} grpc_chttp2_write_state;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700128
129typedef enum {
130 DONT_SEND_CLOSED = 0,
131 SEND_CLOSED,
132 SEND_CLOSED_WITH_RST_STREAM
Craig Tillerb084d902015-06-12 07:50:02 -0700133} grpc_chttp2_send_closed;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700134
135typedef struct {
Craig Tillerb084d902015-06-12 07:50:02 -0700136 grpc_chttp2_stream *head;
137 grpc_chttp2_stream *tail;
138} grpc_chttp2_stream_list;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700139
140typedef struct {
Craig Tillerb084d902015-06-12 07:50:02 -0700141 grpc_chttp2_stream *next;
142 grpc_chttp2_stream *prev;
143} grpc_chttp2_stream_link;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700144
145typedef enum {
146 ERROR_STATE_NONE,
147 ERROR_STATE_SEEN,
148 ERROR_STATE_NOTIFIED
Craig Tillerb084d902015-06-12 07:50:02 -0700149} grpc_chttp2_error_state;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700150
151/* We keep several sets of connection wide parameters */
152typedef enum {
153 /* The settings our peer has asked for (and we have acked) */
154 PEER_SETTINGS = 0,
155 /* The settings we'd like to have */
156 LOCAL_SETTINGS,
157 /* The settings we've published to our peer */
158 SENT_SETTINGS,
159 /* The settings the peer has acked */
160 ACKED_SETTINGS,
161 NUM_SETTING_SETS
Craig Tillerb084d902015-06-12 07:50:02 -0700162} grpc_chttp2_setting_set;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700163
164/* Outstanding ping request data */
165typedef struct {
166 gpr_uint8 id[8];
167 void (*cb)(void *user_data);
168 void *user_data;
Craig Tillerb084d902015-06-12 07:50:02 -0700169} grpc_chttp2_outstanding_ping;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700170
171typedef struct {
172 grpc_status_code status;
173 gpr_slice debug;
Craig Tillerb084d902015-06-12 07:50:02 -0700174} grpc_chttp2_pending_goaway;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700175
Craig Tillerd20efd22015-06-12 16:17:09 -0700176typedef struct {
177 /** data to write next write */
178 gpr_slice_buffer qbuf;
179 /** queued callbacks */
180 grpc_iomgr_closure *pending_closures;
181
182 /** window available for us to send to peer */
183 gpr_uint32 outgoing_window;
184 /** how much window would we like to have for incoming_window */
185 gpr_uint32 connection_window_target;
186
187
188 /** are the local settings dirty and need to be sent? */
189 gpr_uint8 dirtied_local_settings;
190 /** have local settings been sent? */
191 gpr_uint8 sent_local_settings;
192 /** bitmask of setting indexes to send out */
193 gpr_uint32 force_send_settings;
194 /** settings values */
195 gpr_uint32 settings[NUM_SETTING_SETS][GRPC_CHTTP2_NUM_SETTINGS];
196
197 /** last received stream id */
198 gpr_uint32 last_incoming_stream_id;
199} grpc_chttp2_transport_global;
200
201typedef struct {
202 /** data to write now */
203 gpr_slice_buffer outbuf;
204 /** hpack encoding */
205 grpc_chttp2_hpack_compressor hpack_compressor;
206} grpc_chttp2_transport_writing;
207
208struct grpc_chttp2_transport_parsing {
209 /** is this transport a client? (boolean) */
210 gpr_uint8 is_client;
211
212 /** were settings updated? */
213 gpr_uint8 settings_updated;
214 /** was a settings ack received? */
215 gpr_uint8 settings_ack_received;
216 /** was a goaway frame received? */
217 gpr_uint8 goaway_received;
218
219 /** data to write later - after parsing */
220 gpr_slice_buffer qbuf;
221 /* metadata object cache */
222 grpc_mdstr *str_grpc_timeout;
223 /** parser for headers */
224 grpc_chttp2_hpack_parser hpack_parser;
225 /** simple one shot parsers */
226 union {
227 grpc_chttp2_window_update_parser window_update;
228 grpc_chttp2_settings_parser settings;
229 grpc_chttp2_ping_parser ping;
230 grpc_chttp2_rst_stream_parser rst_stream;
231 } simple;
232 /** parser for goaway frames */
233 grpc_chttp2_goaway_parser goaway_parser;
234
235 /** window available for peer to send to us */
236 gpr_uint32 incoming_window;
237
238 /** next stream id available at the time of beginning parsing */
239 gpr_uint32 next_stream_id;
240 gpr_uint32 last_incoming_stream_id;
241
242 /* deframing */
243 grpc_chttp2_deframe_transport_state deframe_state;
244 gpr_uint8 incoming_frame_type;
245 gpr_uint8 incoming_frame_flags;
246 gpr_uint8 header_eof;
247 gpr_uint32 expect_continuation_stream_id;
248 gpr_uint32 incoming_frame_size;
249 gpr_uint32 incoming_stream_id;
250
251 /* active parser */
252 void *parser_data;
253 grpc_chttp2_stream_parsing *incoming_stream;
254 grpc_chttp2_parse_error (*parser)(void *parser_user_data,
255 grpc_chttp2_transport_parsing *transport_parsing, grpc_chttp2_stream_parsing *stream_parsing,
256 gpr_slice slice, int is_last);
257
258 /* received settings */
259 gpr_uint32 settings[GRPC_CHTTP2_NUM_SETTINGS];
260
261 /* goaway data */
262 grpc_status_code goaway_error;
263 gpr_uint32 goaway_last_stream_index;
264 gpr_slice goaway_text;
265};
266
267
Craig Tillerb084d902015-06-12 07:50:02 -0700268struct grpc_chttp2_transport {
Craig Tiller9b8671c2015-06-12 07:41:54 -0700269 grpc_transport base; /* must be first */
270 grpc_endpoint *ep;
271 grpc_mdctx *metadata_context;
272 gpr_refcount refs;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700273
274 gpr_mu mu;
275 gpr_cv cv;
276
Craig Tillerd20efd22015-06-12 16:17:09 -0700277 /** is a thread currently writing */
278 gpr_uint8 writing_active;
279
Craig Tiller9b8671c2015-06-12 07:41:54 -0700280 /* basic state management - what are we doing at the moment? */
281 gpr_uint8 reading;
282 /** are we calling back any grpc_transport_op completion events */
283 gpr_uint8 calling_back_ops;
284 gpr_uint8 destroying;
285 gpr_uint8 closed;
Craig Tillerb084d902015-06-12 07:50:02 -0700286 grpc_chttp2_error_state error_state;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700287
288 /* stream indexing */
289 gpr_uint32 next_stream_id;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700290
291 /* window management */
Craig Tiller9b8671c2015-06-12 07:41:54 -0700292 gpr_uint32 outgoing_window_update;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700293
294 /* goaway */
Craig Tillerb084d902015-06-12 07:50:02 -0700295 grpc_chttp2_pending_goaway *pending_goaways;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700296 size_t num_pending_goaways;
297 size_t cap_pending_goaways;
298
299 /* state for a stream that's not yet been created */
300 grpc_stream_op_buffer new_stream_sopb;
301
302 /* stream ops that need to be destroyed, but outside of the lock */
303 grpc_stream_op_buffer nuke_later_sopb;
304
Craig Tillerb084d902015-06-12 07:50:02 -0700305 grpc_chttp2_stream_list lists[STREAM_LIST_COUNT];
Craig Tiller9b8671c2015-06-12 07:41:54 -0700306 grpc_chttp2_stream_map stream_map;
307
308 /* pings */
Craig Tillerb084d902015-06-12 07:50:02 -0700309 grpc_chttp2_outstanding_ping *pings;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700310 size_t ping_count;
311 size_t ping_capacity;
312 gpr_int64 ping_counter;
313
Craig Tillerd20efd22015-06-12 16:17:09 -0700314 grpc_chttp2_transport_global global;
315 grpc_chttp2_transport_writing writing;
316 grpc_chttp2_transport_parsing parsing;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700317
Craig Tillerd20efd22015-06-12 16:17:09 -0700318 /** closure to execute writing */
319 grpc_iomgr_closure writing_action;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700320
321 struct {
322 /** is a thread currently performing channel callbacks */
323 gpr_uint8 executing;
324 /** transport channel-level callback */
325 const grpc_transport_callbacks *cb;
326 /** user data for cb calls */
327 void *cb_user_data;
328 /** closure for notifying transport closure */
329 grpc_iomgr_closure notify_closed;
330 } channel_callback;
331};
332
Craig Tillerd20efd22015-06-12 16:17:09 -0700333typedef struct {
334 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
Craig Tiller9b8671c2015-06-12 07:41:54 -0700335 gpr_uint32 id;
336
Craig Tillerd20efd22015-06-12 16:17:09 -0700337 grpc_iomgr_closure *send_done_closure;
338 grpc_iomgr_closure *recv_done_closure;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700339
Craig Tillerd20efd22015-06-12 16:17:09 -0700340 /** window available for us to send to peer */
341 gpr_int64 outgoing_window;
342 /** stream ops the transport user would like to send */
343 grpc_stream_op_buffer *outgoing_sopb;
344 /** when the application requests writes be closed, the write_closed is
345 'queued'; when the close is flow controlled into the send path, we are
346 'sending' it; when the write has been performed it is 'sent' */
347 grpc_chttp2_write_state write_state;
348 /** is this stream closed (boolean) */
349 gpr_uint8 read_closed;
350} grpc_chttp2_stream_global;
351
352typedef struct {
353 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
354 gpr_uint32 id;
355 /** sops that have passed flow control to be written */
356 grpc_stream_op_buffer sopb;
357 /** how strongly should we indicate closure with the next write */
358 grpc_chttp2_send_closed send_closed;
359} grpc_chttp2_stream_writing;
360
361struct grpc_chttp2_stream_parsing {
362 /** HTTP2 stream id for this stream, or zero if one has not been assigned */
363 gpr_uint32 id;
364 /** has this stream received a close */
365 gpr_uint8 received_close;
366 /** incoming_window has been reduced during parsing */
367 gpr_uint8 incoming_window_changed;
368 /** saw an error on this stream during parsing (it should be cancelled) */
369 gpr_uint8 saw_error;
370 /** window available for peer to send to us */
371 gpr_uint32 incoming_window;
372 /** parsing state for data frames */
373 grpc_chttp2_data_parser data_parser;
Craig Tiller9b8671c2015-06-12 07:41:54 -0700374
375 /* incoming metadata */
376 grpc_linked_mdelem *incoming_metadata;
377 size_t incoming_metadata_count;
378 size_t incoming_metadata_capacity;
379 grpc_linked_mdelem *old_incoming_metadata;
380 gpr_timespec incoming_deadline;
Craig Tillerd20efd22015-06-12 16:17:09 -0700381};
382
383struct grpc_chttp2_stream {
384 grpc_chttp2_stream_global global;
385 grpc_chttp2_stream_writing writing;
386
387 gpr_uint32 outgoing_window_update;
388 gpr_uint8 cancelled;
389
390 grpc_chttp2_stream_link links[STREAM_LIST_COUNT];
391 gpr_uint8 included[STREAM_LIST_COUNT];
Craig Tiller9b8671c2015-06-12 07:41:54 -0700392
393 /* sops from application */
Craig Tiller9b8671c2015-06-12 07:41:54 -0700394 grpc_stream_op_buffer *incoming_sopb;
395 grpc_stream_state *publish_state;
396 grpc_stream_state published_state;
397
Craig Tiller9b8671c2015-06-12 07:41:54 -0700398 grpc_stream_state callback_state;
399 grpc_stream_op_buffer callback_sopb;
400};
401
Craig Tillerd20efd22015-06-12 16:17:09 -0700402/** Transport writing call flow:
403 chttp2_transport.c calls grpc_chttp2_unlocking_check_writes to see if writes are required;
404 if they are, chttp2_transport.c calls grpc_chttp2_perform_writes to do the writes.
405 Once writes have been completed (meaning another write could potentially be started),
406 grpc_chttp2_terminate_writing is called. This will call grpc_chttp2_cleanup_writing, at which
407 point the write phase is complete. */
408
Craig Tiller3208e392015-06-12 08:17:02 -0700409/** Someone is unlocking the transport mutex: check to see if writes
410 are required, and schedule them if so */
Craig Tillerd20efd22015-06-12 16:17:09 -0700411int grpc_chttp2_unlocking_check_writes(grpc_chttp2_transport_global *global, grpc_chttp2_transport_writing *writing);
412void grpc_chttp2_perform_writes(grpc_chttp2_transport_writing *transport_writing, grpc_endpoint *endpoint);
413void grpc_chttp2_terminate_writing(grpc_chttp2_transport_writing *transport_writing, int success);
414void grpc_chttp2_cleanup_writing(grpc_chttp2_transport_global *global, grpc_chttp2_transport_writing *writing);
415
416/** Process one slice of incoming data */
417int grpc_chttp2_perform_read(grpc_chttp2_transport_parsing *transport_parsing, gpr_slice slice);
418void grpc_chttp2_publish_reads(grpc_chttp2_transport_global *global, grpc_chttp2_transport_parsing *parsing);
419
420/** Get a writable stream
421 \return non-zero if there was a stream available */
422void grpc_chttp2_list_add_writable_stream(grpc_chttp2_transport_global *transport_global, grpc_chttp2_stream_global *stream_global);
423int grpc_chttp2_list_pop_writable_stream(grpc_chttp2_transport_global *transport_global, grpc_chttp2_transport_writing *transport_writing,
424 grpc_chttp2_stream_global **stream_global, grpc_chttp2_stream_writing **stream_writing);
425
426void grpc_chttp2_list_add_writing_stream(grpc_chttp2_transport_writing *transport_writing, grpc_chttp2_stream_writing *stream_writing);
427int grpc_chttp2_list_have_writing_streams(grpc_chttp2_transport_writing *transport_writing);
428int grpc_chttp2_list_pop_writing_stream(grpc_chttp2_transport_writing *transport_writing, grpc_chttp2_stream_writing **stream_writing);
429
430void grpc_chttp2_list_add_written_stream(grpc_chttp2_transport_writing *transport_writing, grpc_chttp2_stream_writing *stream_writing);
431int grpc_chttp2_list_pop_written_stream(grpc_chttp2_transport_global *transport_global, grpc_chttp2_transport_writing *transport_writing, grpc_chttp2_stream_global **stream_global, grpc_chttp2_stream_writing **stream_writing);
432
433int grpc_chttp2_list_pop_writable_window_update_stream(grpc_chttp2_transport_global *transport_global, grpc_chttp2_stream_global **stream_global);
434
435void grpc_chttp2_list_add_parsing_seen_stream(grpc_chttp2_transport_parsing *transport_parsing, grpc_chttp2_stream_parsing *stream_parsing);
436
437void grpc_chttp2_schedule_closure(grpc_chttp2_transport_global *transport_global, grpc_iomgr_closure *closure, int success);
438void grpc_chttp2_read_write_state_changed(grpc_chttp2_transport_global *transport_global, grpc_chttp2_stream_global *stream_global);
439
440grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream(grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
441grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream(grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id);
442
443#define GRPC_CHTTP2_FLOW_CTL_TRACE(a,b,c,d,e) do {} while (0)
444
445#define GRPC_CHTTP2_CLIENT_CONNECT_STRING "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
446#define GRPC_CHTTP2_CLIENT_CONNECT_STRLEN (sizeof(GRPC_CHTTP2_CLIENT_CONNECT_STRING)-1)
447
448extern int grpc_http_trace;
449
450#define IF_TRACING(stmt) \
451 if (!(grpc_http_trace)) \
452 ; \
453 else \
454 stmt
Craig Tiller3208e392015-06-12 08:17:02 -0700455
Craig Tiller9b8671c2015-06-12 07:41:54 -0700456#endif