Craig Tiller | 640d3bd | 2015-06-12 07:51:51 -0700 | [diff] [blame] | 1 | /* |
| 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 Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 34 | #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 Tiller | 3208e39 | 2015-06-12 08:17:02 -0700 | [diff] [blame] | 38 | #include "src/core/iomgr/endpoint.h" |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 39 | #include "src/core/transport/chttp2/frame.h" |
Craig Tiller | 3208e39 | 2015-06-12 08:17:02 -0700 | [diff] [blame] | 40 | #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 Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 49 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 50 | typedef struct grpc_chttp2_transport grpc_chttp2_transport; |
| 51 | typedef struct grpc_chttp2_stream grpc_chttp2_stream; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 52 | |
| 53 | /* streams are kept in various linked lists depending on what things need to |
| 54 | happen to them... this enum labels each list */ |
| 55 | typedef 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 Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 78 | } grpc_chttp2_stream_list_id; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 79 | |
| 80 | /* deframer state for the overall http2 stream of bytes */ |
| 81 | typedef 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 Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 121 | } grpc_chttp2_deframe_transport_state; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 122 | |
| 123 | typedef enum { |
| 124 | WRITE_STATE_OPEN, |
| 125 | WRITE_STATE_QUEUED_CLOSE, |
| 126 | WRITE_STATE_SENT_CLOSE |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 127 | } grpc_chttp2_write_state; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 128 | |
| 129 | typedef enum { |
| 130 | DONT_SEND_CLOSED = 0, |
| 131 | SEND_CLOSED, |
| 132 | SEND_CLOSED_WITH_RST_STREAM |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 133 | } grpc_chttp2_send_closed; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 134 | |
| 135 | typedef struct { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 136 | grpc_chttp2_stream *head; |
| 137 | grpc_chttp2_stream *tail; |
| 138 | } grpc_chttp2_stream_list; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 139 | |
| 140 | typedef struct { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 141 | grpc_chttp2_stream *next; |
| 142 | grpc_chttp2_stream *prev; |
| 143 | } grpc_chttp2_stream_link; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 144 | |
| 145 | typedef enum { |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 146 | GRPC_CHTTP2_ERROR_STATE_NONE, |
| 147 | GRPC_CHTTP2_ERROR_STATE_SEEN, |
| 148 | GRPC_CHTTP2_ERROR_STATE_NOTIFIED |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 149 | } grpc_chttp2_error_state; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 150 | |
| 151 | /* We keep several sets of connection wide parameters */ |
| 152 | typedef 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 Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 162 | } grpc_chttp2_setting_set; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 163 | |
| 164 | /* Outstanding ping request data */ |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 165 | typedef struct grpc_chttp2_outstanding_ping { |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 166 | gpr_uint8 id[8]; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 167 | grpc_iomgr_closure *on_recv; |
| 168 | struct grpc_chttp2_outstanding_ping *next; |
| 169 | struct grpc_chttp2_outstanding_ping *prev; |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 170 | } grpc_chttp2_outstanding_ping; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 171 | |
| 172 | typedef struct { |
| 173 | grpc_status_code status; |
| 174 | gpr_slice debug; |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 175 | } grpc_chttp2_pending_goaway; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 176 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 177 | typedef struct { |
| 178 | /** data to write next write */ |
| 179 | gpr_slice_buffer qbuf; |
| 180 | /** queued callbacks */ |
| 181 | grpc_iomgr_closure *pending_closures; |
| 182 | |
| 183 | /** window available for us to send to peer */ |
| 184 | gpr_uint32 outgoing_window; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 185 | /** window available for peer to send to us - updated after parse */ |
| 186 | gpr_uint32 incoming_window; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 187 | /** how much window would we like to have for incoming_window */ |
| 188 | gpr_uint32 connection_window_target; |
| 189 | |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 190 | /** is this transport a client? */ |
| 191 | gpr_uint8 is_client; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 192 | /** are the local settings dirty and need to be sent? */ |
| 193 | gpr_uint8 dirtied_local_settings; |
| 194 | /** have local settings been sent? */ |
| 195 | gpr_uint8 sent_local_settings; |
| 196 | /** bitmask of setting indexes to send out */ |
| 197 | gpr_uint32 force_send_settings; |
| 198 | /** settings values */ |
| 199 | gpr_uint32 settings[NUM_SETTING_SETS][GRPC_CHTTP2_NUM_SETTINGS]; |
| 200 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 201 | /** has there been a connection level error, and have we notified |
| 202 | anyone about it? */ |
| 203 | grpc_chttp2_error_state error_state; |
| 204 | |
| 205 | /** what is the next stream id to be allocated by this peer? |
| 206 | copied to next_stream_id in parsing when parsing commences */ |
| 207 | gpr_uint32 next_stream_id; |
| 208 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 209 | /** last received stream id */ |
| 210 | gpr_uint32 last_incoming_stream_id; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 211 | |
| 212 | /** pings awaiting responses */ |
| 213 | grpc_chttp2_outstanding_ping pings; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 214 | /** next payload for an outgoing ping */ |
| 215 | gpr_uint64 ping_counter; |
| 216 | |
| 217 | /** concurrent stream count: updated when not parsing, |
| 218 | so this is a strict over-estimation on the client */ |
| 219 | gpr_uint32 concurrent_stream_count; |
| 220 | |
| 221 | /** is there a goaway available? */ |
| 222 | gpr_uint8 have_goaway; |
| 223 | /** what is the debug text of the goaway? */ |
| 224 | gpr_slice goaway_text; |
| 225 | /** what is the status code of the goaway? */ |
| 226 | grpc_status_code goaway_error; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 227 | } grpc_chttp2_transport_global; |
| 228 | |
| 229 | typedef struct { |
| 230 | /** data to write now */ |
| 231 | gpr_slice_buffer outbuf; |
| 232 | /** hpack encoding */ |
| 233 | grpc_chttp2_hpack_compressor hpack_compressor; |
| 234 | } grpc_chttp2_transport_writing; |
| 235 | |
| 236 | struct grpc_chttp2_transport_parsing { |
| 237 | /** is this transport a client? (boolean) */ |
| 238 | gpr_uint8 is_client; |
| 239 | |
| 240 | /** were settings updated? */ |
| 241 | gpr_uint8 settings_updated; |
| 242 | /** was a settings ack received? */ |
| 243 | gpr_uint8 settings_ack_received; |
| 244 | /** was a goaway frame received? */ |
| 245 | gpr_uint8 goaway_received; |
| 246 | |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 247 | /** initial window change */ |
| 248 | gpr_int64 initial_window_update; |
| 249 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 250 | /** data to write later - after parsing */ |
| 251 | gpr_slice_buffer qbuf; |
| 252 | /* metadata object cache */ |
| 253 | grpc_mdstr *str_grpc_timeout; |
| 254 | /** parser for headers */ |
| 255 | grpc_chttp2_hpack_parser hpack_parser; |
| 256 | /** simple one shot parsers */ |
| 257 | union { |
| 258 | grpc_chttp2_window_update_parser window_update; |
| 259 | grpc_chttp2_settings_parser settings; |
| 260 | grpc_chttp2_ping_parser ping; |
| 261 | grpc_chttp2_rst_stream_parser rst_stream; |
| 262 | } simple; |
| 263 | /** parser for goaway frames */ |
| 264 | grpc_chttp2_goaway_parser goaway_parser; |
| 265 | |
| 266 | /** window available for peer to send to us */ |
| 267 | gpr_uint32 incoming_window; |
| 268 | |
| 269 | /** next stream id available at the time of beginning parsing */ |
| 270 | gpr_uint32 next_stream_id; |
| 271 | gpr_uint32 last_incoming_stream_id; |
| 272 | |
| 273 | /* deframing */ |
| 274 | grpc_chttp2_deframe_transport_state deframe_state; |
| 275 | gpr_uint8 incoming_frame_type; |
| 276 | gpr_uint8 incoming_frame_flags; |
| 277 | gpr_uint8 header_eof; |
| 278 | gpr_uint32 expect_continuation_stream_id; |
| 279 | gpr_uint32 incoming_frame_size; |
| 280 | gpr_uint32 incoming_stream_id; |
| 281 | |
| 282 | /* active parser */ |
| 283 | void *parser_data; |
| 284 | grpc_chttp2_stream_parsing *incoming_stream; |
| 285 | grpc_chttp2_parse_error (*parser)(void *parser_user_data, |
| 286 | grpc_chttp2_transport_parsing *transport_parsing, grpc_chttp2_stream_parsing *stream_parsing, |
| 287 | gpr_slice slice, int is_last); |
| 288 | |
| 289 | /* received settings */ |
| 290 | gpr_uint32 settings[GRPC_CHTTP2_NUM_SETTINGS]; |
| 291 | |
| 292 | /* goaway data */ |
| 293 | grpc_status_code goaway_error; |
| 294 | gpr_uint32 goaway_last_stream_index; |
| 295 | gpr_slice goaway_text; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 296 | |
| 297 | gpr_uint64 outgoing_window_update; |
| 298 | |
| 299 | /** pings awaiting responses */ |
| 300 | grpc_chttp2_outstanding_ping pings; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 301 | }; |
| 302 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 303 | struct grpc_chttp2_transport { |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 304 | grpc_transport base; /* must be first */ |
| 305 | grpc_endpoint *ep; |
| 306 | grpc_mdctx *metadata_context; |
| 307 | gpr_refcount refs; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 308 | |
| 309 | gpr_mu mu; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 310 | |
| 311 | /** is the transport destroying itself? */ |
| 312 | gpr_uint8 destroying; |
| 313 | /** has the upper layer closed the transport? */ |
| 314 | gpr_uint8 closed; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 315 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 316 | /** is a thread currently writing */ |
| 317 | gpr_uint8 writing_active; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 318 | /** is a thread currently parsing */ |
| 319 | gpr_uint8 parsing_active; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 320 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 321 | /** is there a read request to the endpoint outstanding? */ |
| 322 | gpr_uint8 endpoint_reading; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 323 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 324 | /** various lists of streams */ |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 325 | grpc_chttp2_stream_list lists[STREAM_LIST_COUNT]; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 326 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 327 | /** global state for reading/writing */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 328 | grpc_chttp2_transport_global global; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 329 | /** state only accessible by the chain of execution that |
| 330 | set writing_active=1 */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 331 | grpc_chttp2_transport_writing writing; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 332 | /** state only accessible by the chain of execution that |
| 333 | set parsing_active=1 */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 334 | grpc_chttp2_transport_parsing parsing; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 335 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 336 | /** maps stream id to grpc_chttp2_stream objects; |
| 337 | owned by the parsing thread when parsing */ |
| 338 | grpc_chttp2_stream_map parsing_stream_map; |
| 339 | |
| 340 | /** streams created by the client (possibly during parsing); |
| 341 | merged with parsing_stream_map during unlock when no |
| 342 | parsing is occurring */ |
| 343 | grpc_chttp2_stream_map new_stream_map; |
| 344 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 345 | /** closure to execute writing */ |
| 346 | grpc_iomgr_closure writing_action; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 347 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 348 | /** address to place a newly accepted stream - set and unset by |
| 349 | grpc_chttp2_parsing_accept_stream; used by init_stream to |
| 350 | publish the accepted server stream */ |
| 351 | grpc_chttp2_stream **accepting_stream; |
| 352 | |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 353 | struct { |
| 354 | /** is a thread currently performing channel callbacks */ |
| 355 | gpr_uint8 executing; |
| 356 | /** transport channel-level callback */ |
| 357 | const grpc_transport_callbacks *cb; |
| 358 | /** user data for cb calls */ |
| 359 | void *cb_user_data; |
| 360 | /** closure for notifying transport closure */ |
| 361 | grpc_iomgr_closure notify_closed; |
| 362 | } channel_callback; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 363 | |
| 364 | #if 0 |
| 365 | /* basic state management - what are we doing at the moment? */ |
| 366 | gpr_uint8 reading; |
| 367 | /** are we calling back any grpc_transport_op completion events */ |
| 368 | gpr_uint8 calling_back_ops; |
| 369 | gpr_uint8 destroying; |
| 370 | gpr_uint8 closed; |
| 371 | |
| 372 | /* stream indexing */ |
| 373 | gpr_uint32 next_stream_id; |
| 374 | |
| 375 | /* window management */ |
| 376 | gpr_uint32 outgoing_window_update; |
| 377 | |
| 378 | /* state for a stream that's not yet been created */ |
| 379 | grpc_stream_op_buffer new_stream_sopb; |
| 380 | |
| 381 | /* stream ops that need to be destroyed, but outside of the lock */ |
| 382 | grpc_stream_op_buffer nuke_later_sopb; |
| 383 | |
| 384 | /* pings */ |
| 385 | gpr_int64 ping_counter; |
| 386 | |
| 387 | |
| 388 | grpc_chttp2_stream **accepting_stream; |
| 389 | |
| 390 | #endif |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 391 | }; |
| 392 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 393 | typedef struct { |
| 394 | /** HTTP2 stream id for this stream, or zero if one has not been assigned */ |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 395 | gpr_uint32 id; |
| 396 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 397 | grpc_iomgr_closure *send_done_closure; |
| 398 | grpc_iomgr_closure *recv_done_closure; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 399 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 400 | /** window available for us to send to peer */ |
| 401 | gpr_int64 outgoing_window; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 402 | /** window available for peer to send to us - updated after parse */ |
| 403 | gpr_uint32 incoming_window; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 404 | /** stream ops the transport user would like to send */ |
| 405 | grpc_stream_op_buffer *outgoing_sopb; |
| 406 | /** when the application requests writes be closed, the write_closed is |
| 407 | 'queued'; when the close is flow controlled into the send path, we are |
| 408 | 'sending' it; when the write has been performed it is 'sent' */ |
| 409 | grpc_chttp2_write_state write_state; |
| 410 | /** is this stream closed (boolean) */ |
| 411 | gpr_uint8 read_closed; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 412 | |
| 413 | /** stream state already published to the upper layer */ |
| 414 | grpc_stream_state published_state; |
| 415 | /** address to publish next stream state to */ |
| 416 | grpc_stream_state *publish_state; |
| 417 | /** pointer to sop buffer to fill in with new stream ops */ |
| 418 | grpc_stream_op_buffer *incoming_sopb; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 419 | } grpc_chttp2_stream_global; |
| 420 | |
| 421 | typedef struct { |
| 422 | /** HTTP2 stream id for this stream, or zero if one has not been assigned */ |
| 423 | gpr_uint32 id; |
| 424 | /** sops that have passed flow control to be written */ |
| 425 | grpc_stream_op_buffer sopb; |
| 426 | /** how strongly should we indicate closure with the next write */ |
| 427 | grpc_chttp2_send_closed send_closed; |
| 428 | } grpc_chttp2_stream_writing; |
| 429 | |
| 430 | struct grpc_chttp2_stream_parsing { |
| 431 | /** HTTP2 stream id for this stream, or zero if one has not been assigned */ |
| 432 | gpr_uint32 id; |
| 433 | /** has this stream received a close */ |
| 434 | gpr_uint8 received_close; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 435 | /** saw an error on this stream during parsing (it should be cancelled) */ |
| 436 | gpr_uint8 saw_error; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 437 | /** saw a rst_stream */ |
| 438 | gpr_uint8 saw_rst_stream; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 439 | /** incoming_window has been reduced by this much during parsing */ |
| 440 | gpr_uint32 incoming_window_delta; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 441 | /** 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 Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 445 | /** reason give to rst_stream */ |
| 446 | gpr_uint32 rst_stream_reason; |
| 447 | /* amount of window given */ |
| 448 | gpr_uint64 outgoing_window_update; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 449 | |
| 450 | /* incoming metadata */ |
| 451 | grpc_linked_mdelem *incoming_metadata; |
| 452 | size_t incoming_metadata_count; |
| 453 | size_t incoming_metadata_capacity; |
| 454 | grpc_linked_mdelem *old_incoming_metadata; |
| 455 | gpr_timespec incoming_deadline; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 456 | }; |
| 457 | |
| 458 | struct grpc_chttp2_stream { |
| 459 | grpc_chttp2_stream_global global; |
| 460 | grpc_chttp2_stream_writing writing; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 461 | grpc_chttp2_stream_parsing parsing; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 462 | |
| 463 | grpc_chttp2_stream_link links[STREAM_LIST_COUNT]; |
| 464 | gpr_uint8 included[STREAM_LIST_COUNT]; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 465 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 466 | #if 0 |
| 467 | gpr_uint32 outgoing_window_update; |
| 468 | gpr_uint8 cancelled; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 469 | |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 470 | grpc_stream_state callback_state; |
| 471 | grpc_stream_op_buffer callback_sopb; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 472 | #endif |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 473 | }; |
| 474 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 475 | /** Transport writing call flow: |
| 476 | chttp2_transport.c calls grpc_chttp2_unlocking_check_writes to see if writes are required; |
| 477 | if they are, chttp2_transport.c calls grpc_chttp2_perform_writes to do the writes. |
| 478 | Once writes have been completed (meaning another write could potentially be started), |
| 479 | grpc_chttp2_terminate_writing is called. This will call grpc_chttp2_cleanup_writing, at which |
| 480 | point the write phase is complete. */ |
| 481 | |
Craig Tiller | 3208e39 | 2015-06-12 08:17:02 -0700 | [diff] [blame] | 482 | /** Someone is unlocking the transport mutex: check to see if writes |
| 483 | are required, and schedule them if so */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 484 | int grpc_chttp2_unlocking_check_writes(grpc_chttp2_transport_global *global, grpc_chttp2_transport_writing *writing); |
| 485 | void grpc_chttp2_perform_writes(grpc_chttp2_transport_writing *transport_writing, grpc_endpoint *endpoint); |
| 486 | void grpc_chttp2_terminate_writing(grpc_chttp2_transport_writing *transport_writing, int success); |
| 487 | void grpc_chttp2_cleanup_writing(grpc_chttp2_transport_global *global, grpc_chttp2_transport_writing *writing); |
| 488 | |
| 489 | /** Process one slice of incoming data */ |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 490 | void grpc_chttp2_prepare_to_read(grpc_chttp2_transport_global *global, grpc_chttp2_transport_parsing *parsing); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 491 | int grpc_chttp2_perform_read(grpc_chttp2_transport_parsing *transport_parsing, gpr_slice slice); |
| 492 | void grpc_chttp2_publish_reads(grpc_chttp2_transport_global *global, grpc_chttp2_transport_parsing *parsing); |
| 493 | |
| 494 | /** Get a writable stream |
| 495 | \return non-zero if there was a stream available */ |
| 496 | void grpc_chttp2_list_add_writable_stream(grpc_chttp2_transport_global *transport_global, grpc_chttp2_stream_global *stream_global); |
| 497 | int grpc_chttp2_list_pop_writable_stream(grpc_chttp2_transport_global *transport_global, grpc_chttp2_transport_writing *transport_writing, |
| 498 | grpc_chttp2_stream_global **stream_global, grpc_chttp2_stream_writing **stream_writing); |
| 499 | |
| 500 | void grpc_chttp2_list_add_writing_stream(grpc_chttp2_transport_writing *transport_writing, grpc_chttp2_stream_writing *stream_writing); |
| 501 | int grpc_chttp2_list_have_writing_streams(grpc_chttp2_transport_writing *transport_writing); |
| 502 | int grpc_chttp2_list_pop_writing_stream(grpc_chttp2_transport_writing *transport_writing, grpc_chttp2_stream_writing **stream_writing); |
| 503 | |
| 504 | void grpc_chttp2_list_add_written_stream(grpc_chttp2_transport_writing *transport_writing, grpc_chttp2_stream_writing *stream_writing); |
| 505 | int 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); |
| 506 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 507 | void grpc_chttp2_list_add_writable_window_update_stream(grpc_chttp2_transport_global *transport_global, grpc_chttp2_stream_global *stream_global); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 508 | int grpc_chttp2_list_pop_writable_window_update_stream(grpc_chttp2_transport_global *transport_global, grpc_chttp2_stream_global **stream_global); |
| 509 | |
| 510 | void grpc_chttp2_list_add_parsing_seen_stream(grpc_chttp2_transport_parsing *transport_parsing, grpc_chttp2_stream_parsing *stream_parsing); |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame^] | 511 | int grpc_chttp2_list_pop_parsing_seen_stream(grpc_chttp2_transport_global *transport_global, grpc_chttp2_transport_parsing *transport_parsing, grpc_chttp2_stream_global **stream_global, grpc_chttp2_stream_parsing **stream_parsing); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 512 | |
| 513 | void grpc_chttp2_schedule_closure(grpc_chttp2_transport_global *transport_global, grpc_iomgr_closure *closure, int success); |
| 514 | void grpc_chttp2_read_write_state_changed(grpc_chttp2_transport_global *transport_global, grpc_chttp2_stream_global *stream_global); |
| 515 | |
| 516 | grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream(grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id); |
| 517 | grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream(grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id); |
| 518 | |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 519 | void grpc_chttp2_parsing_add_metadata_batch(grpc_chttp2_transport_parsing *transport_parsing, grpc_chttp2_stream_parsing *stream_parsing); |
| 520 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 521 | #define GRPC_CHTTP2_FLOW_CTL_TRACE(a,b,c,d,e) do {} while (0) |
| 522 | |
| 523 | #define GRPC_CHTTP2_CLIENT_CONNECT_STRING "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" |
| 524 | #define GRPC_CHTTP2_CLIENT_CONNECT_STRLEN (sizeof(GRPC_CHTTP2_CLIENT_CONNECT_STRING)-1) |
| 525 | |
| 526 | extern int grpc_http_trace; |
| 527 | |
| 528 | #define IF_TRACING(stmt) \ |
| 529 | if (!(grpc_http_trace)) \ |
| 530 | ; \ |
| 531 | else \ |
| 532 | stmt |
Craig Tiller | 3208e39 | 2015-06-12 08:17:02 -0700 | [diff] [blame] | 533 | |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 534 | #endif |