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" |
Craig Tiller | 3208e39 | 2015-06-12 08:17:02 -0700 | [diff] [blame] | 46 | #include "src/core/transport/chttp2/hpack_parser.h" |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame^] | 47 | #include "src/core/transport/chttp2/incoming_metadata.h" |
Craig Tiller | 3208e39 | 2015-06-12 08:17:02 -0700 | [diff] [blame] | 48 | #include "src/core/transport/chttp2/stream_encoder.h" |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame^] | 49 | #include "src/core/transport/chttp2/stream_map.h" |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 50 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 51 | typedef struct grpc_chttp2_transport grpc_chttp2_transport; |
| 52 | typedef struct grpc_chttp2_stream grpc_chttp2_stream; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 53 | |
| 54 | /* streams are kept in various linked lists depending on what things need to |
| 55 | happen to them... this enum labels each list */ |
| 56 | typedef enum { |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame^] | 57 | GRPC_CHTTP2_LIST_ALL_STREAMS, |
| 58 | GRPC_CHTTP2_LIST_WRITABLE, |
| 59 | /** streams that are waiting to start because there are too many concurrent |
| 60 | streams on the connection */ |
| 61 | GRPC_CHTTP2_LIST_WAITING_FOR_CONCURRENCY, |
| 62 | #if 0 |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 63 | /* streams that have pending writes */ |
| 64 | WRITABLE = 0, |
| 65 | /* streams that have been selected to be written */ |
| 66 | WRITING, |
| 67 | /* streams that have just been written, and included a close */ |
| 68 | WRITTEN_CLOSED, |
| 69 | /* streams that have been cancelled and have some pending state updates |
| 70 | to perform */ |
| 71 | CANCELLED, |
| 72 | /* streams that want to send window updates */ |
| 73 | WINDOW_UPDATE, |
| 74 | /* streams that are waiting to start because there are too many concurrent |
| 75 | streams on the connection */ |
| 76 | WAITING_FOR_CONCURRENCY, |
| 77 | /* streams that have finished reading: we wait until unlock to coalesce |
| 78 | all changes into one callback */ |
| 79 | FINISHED_READ_OP, |
| 80 | MAYBE_FINISH_READ_AFTER_PARSE, |
| 81 | PARSER_CHECK_WINDOW_UPDATES_AFTER_PARSE, |
| 82 | OTHER_CHECK_WINDOW_UPDATES_AFTER_PARSE, |
| 83 | NEW_OUTGOING_WINDOW, |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame^] | 84 | #endif |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 85 | STREAM_LIST_COUNT /* must be last */ |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 86 | } grpc_chttp2_stream_list_id; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 87 | |
| 88 | /* deframer state for the overall http2 stream of bytes */ |
| 89 | typedef enum { |
| 90 | /* prefix: one entry per http2 connection prefix byte */ |
| 91 | DTS_CLIENT_PREFIX_0 = 0, |
| 92 | DTS_CLIENT_PREFIX_1, |
| 93 | DTS_CLIENT_PREFIX_2, |
| 94 | DTS_CLIENT_PREFIX_3, |
| 95 | DTS_CLIENT_PREFIX_4, |
| 96 | DTS_CLIENT_PREFIX_5, |
| 97 | DTS_CLIENT_PREFIX_6, |
| 98 | DTS_CLIENT_PREFIX_7, |
| 99 | DTS_CLIENT_PREFIX_8, |
| 100 | DTS_CLIENT_PREFIX_9, |
| 101 | DTS_CLIENT_PREFIX_10, |
| 102 | DTS_CLIENT_PREFIX_11, |
| 103 | DTS_CLIENT_PREFIX_12, |
| 104 | DTS_CLIENT_PREFIX_13, |
| 105 | DTS_CLIENT_PREFIX_14, |
| 106 | DTS_CLIENT_PREFIX_15, |
| 107 | DTS_CLIENT_PREFIX_16, |
| 108 | DTS_CLIENT_PREFIX_17, |
| 109 | DTS_CLIENT_PREFIX_18, |
| 110 | DTS_CLIENT_PREFIX_19, |
| 111 | DTS_CLIENT_PREFIX_20, |
| 112 | DTS_CLIENT_PREFIX_21, |
| 113 | DTS_CLIENT_PREFIX_22, |
| 114 | DTS_CLIENT_PREFIX_23, |
| 115 | /* frame header byte 0... */ |
| 116 | /* must follow from the prefix states */ |
| 117 | DTS_FH_0, |
| 118 | DTS_FH_1, |
| 119 | DTS_FH_2, |
| 120 | DTS_FH_3, |
| 121 | DTS_FH_4, |
| 122 | DTS_FH_5, |
| 123 | DTS_FH_6, |
| 124 | DTS_FH_7, |
| 125 | /* ... frame header byte 8 */ |
| 126 | DTS_FH_8, |
| 127 | /* inside a http2 frame */ |
| 128 | DTS_FRAME |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 129 | } grpc_chttp2_deframe_transport_state; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 130 | |
| 131 | typedef enum { |
| 132 | WRITE_STATE_OPEN, |
| 133 | WRITE_STATE_QUEUED_CLOSE, |
| 134 | WRITE_STATE_SENT_CLOSE |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 135 | } grpc_chttp2_write_state; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 136 | |
| 137 | typedef enum { |
| 138 | DONT_SEND_CLOSED = 0, |
| 139 | SEND_CLOSED, |
| 140 | SEND_CLOSED_WITH_RST_STREAM |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 141 | } grpc_chttp2_send_closed; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 142 | |
| 143 | typedef struct { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 144 | grpc_chttp2_stream *head; |
| 145 | grpc_chttp2_stream *tail; |
| 146 | } grpc_chttp2_stream_list; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 147 | |
| 148 | typedef struct { |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 149 | grpc_chttp2_stream *next; |
| 150 | grpc_chttp2_stream *prev; |
| 151 | } grpc_chttp2_stream_link; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 152 | |
| 153 | typedef enum { |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 154 | GRPC_CHTTP2_ERROR_STATE_NONE, |
| 155 | GRPC_CHTTP2_ERROR_STATE_SEEN, |
| 156 | GRPC_CHTTP2_ERROR_STATE_NOTIFIED |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 157 | } grpc_chttp2_error_state; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 158 | |
| 159 | /* We keep several sets of connection wide parameters */ |
| 160 | typedef enum { |
| 161 | /* The settings our peer has asked for (and we have acked) */ |
| 162 | PEER_SETTINGS = 0, |
| 163 | /* The settings we'd like to have */ |
| 164 | LOCAL_SETTINGS, |
| 165 | /* The settings we've published to our peer */ |
| 166 | SENT_SETTINGS, |
| 167 | /* The settings the peer has acked */ |
| 168 | ACKED_SETTINGS, |
| 169 | NUM_SETTING_SETS |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 170 | } grpc_chttp2_setting_set; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 171 | |
| 172 | /* Outstanding ping request data */ |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 173 | typedef struct grpc_chttp2_outstanding_ping { |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 174 | gpr_uint8 id[8]; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 175 | grpc_iomgr_closure *on_recv; |
| 176 | struct grpc_chttp2_outstanding_ping *next; |
| 177 | struct grpc_chttp2_outstanding_ping *prev; |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 178 | } grpc_chttp2_outstanding_ping; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 179 | |
| 180 | typedef struct { |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 181 | /** data to write next write */ |
| 182 | gpr_slice_buffer qbuf; |
| 183 | /** queued callbacks */ |
| 184 | grpc_iomgr_closure *pending_closures; |
| 185 | |
| 186 | /** window available for us to send to peer */ |
| 187 | gpr_uint32 outgoing_window; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 188 | /** window available for peer to send to us - updated after parse */ |
| 189 | gpr_uint32 incoming_window; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 190 | /** how much window would we like to have for incoming_window */ |
| 191 | gpr_uint32 connection_window_target; |
| 192 | |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 193 | /** is this transport a client? */ |
| 194 | gpr_uint8 is_client; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 195 | /** are the local settings dirty and need to be sent? */ |
| 196 | gpr_uint8 dirtied_local_settings; |
| 197 | /** have local settings been sent? */ |
| 198 | gpr_uint8 sent_local_settings; |
| 199 | /** bitmask of setting indexes to send out */ |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 200 | gpr_uint32 force_send_settings; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 201 | /** settings values */ |
| 202 | gpr_uint32 settings[NUM_SETTING_SETS][GRPC_CHTTP2_NUM_SETTINGS]; |
| 203 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 204 | /** has there been a connection level error, and have we notified |
| 205 | anyone about it? */ |
| 206 | grpc_chttp2_error_state error_state; |
| 207 | |
| 208 | /** what is the next stream id to be allocated by this peer? |
| 209 | copied to next_stream_id in parsing when parsing commences */ |
| 210 | gpr_uint32 next_stream_id; |
| 211 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 212 | /** last received stream id */ |
| 213 | gpr_uint32 last_incoming_stream_id; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 214 | |
| 215 | /** pings awaiting responses */ |
| 216 | grpc_chttp2_outstanding_ping pings; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 217 | /** next payload for an outgoing ping */ |
| 218 | gpr_uint64 ping_counter; |
| 219 | |
| 220 | /** concurrent stream count: updated when not parsing, |
| 221 | so this is a strict over-estimation on the client */ |
| 222 | gpr_uint32 concurrent_stream_count; |
| 223 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 224 | /** is there a goaway available? (boolean) */ |
| 225 | grpc_chttp2_error_state goaway_state; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 226 | /** what is the debug text of the goaway? */ |
| 227 | gpr_slice goaway_text; |
| 228 | /** what is the status code of the goaway? */ |
| 229 | grpc_status_code goaway_error; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 230 | } grpc_chttp2_transport_global; |
| 231 | |
| 232 | typedef struct { |
| 233 | /** data to write now */ |
| 234 | gpr_slice_buffer outbuf; |
| 235 | /** hpack encoding */ |
| 236 | grpc_chttp2_hpack_compressor hpack_compressor; |
| 237 | } grpc_chttp2_transport_writing; |
| 238 | |
| 239 | struct grpc_chttp2_transport_parsing { |
| 240 | /** is this transport a client? (boolean) */ |
| 241 | gpr_uint8 is_client; |
| 242 | |
| 243 | /** were settings updated? */ |
| 244 | gpr_uint8 settings_updated; |
| 245 | /** was a settings ack received? */ |
| 246 | gpr_uint8 settings_ack_received; |
| 247 | /** was a goaway frame received? */ |
| 248 | gpr_uint8 goaway_received; |
| 249 | |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 250 | /** initial window change */ |
| 251 | gpr_int64 initial_window_update; |
| 252 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 253 | /** data to write later - after parsing */ |
| 254 | gpr_slice_buffer qbuf; |
| 255 | /* metadata object cache */ |
| 256 | grpc_mdstr *str_grpc_timeout; |
| 257 | /** parser for headers */ |
| 258 | grpc_chttp2_hpack_parser hpack_parser; |
| 259 | /** simple one shot parsers */ |
| 260 | union { |
| 261 | grpc_chttp2_window_update_parser window_update; |
| 262 | grpc_chttp2_settings_parser settings; |
| 263 | grpc_chttp2_ping_parser ping; |
| 264 | grpc_chttp2_rst_stream_parser rst_stream; |
| 265 | } simple; |
| 266 | /** parser for goaway frames */ |
| 267 | grpc_chttp2_goaway_parser goaway_parser; |
| 268 | |
| 269 | /** window available for peer to send to us */ |
| 270 | gpr_uint32 incoming_window; |
| 271 | |
| 272 | /** next stream id available at the time of beginning parsing */ |
| 273 | gpr_uint32 next_stream_id; |
| 274 | gpr_uint32 last_incoming_stream_id; |
| 275 | |
| 276 | /* deframing */ |
| 277 | grpc_chttp2_deframe_transport_state deframe_state; |
| 278 | gpr_uint8 incoming_frame_type; |
| 279 | gpr_uint8 incoming_frame_flags; |
| 280 | gpr_uint8 header_eof; |
| 281 | gpr_uint32 expect_continuation_stream_id; |
| 282 | gpr_uint32 incoming_frame_size; |
| 283 | gpr_uint32 incoming_stream_id; |
| 284 | |
| 285 | /* active parser */ |
| 286 | void *parser_data; |
| 287 | grpc_chttp2_stream_parsing *incoming_stream; |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 288 | grpc_chttp2_parse_error (*parser)( |
| 289 | void *parser_user_data, grpc_chttp2_transport_parsing *transport_parsing, |
| 290 | grpc_chttp2_stream_parsing *stream_parsing, gpr_slice slice, int is_last); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 291 | |
| 292 | /* received settings */ |
| 293 | gpr_uint32 settings[GRPC_CHTTP2_NUM_SETTINGS]; |
| 294 | |
| 295 | /* goaway data */ |
| 296 | grpc_status_code goaway_error; |
| 297 | gpr_uint32 goaway_last_stream_index; |
| 298 | gpr_slice goaway_text; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 299 | |
| 300 | gpr_uint64 outgoing_window_update; |
| 301 | |
| 302 | /** pings awaiting responses */ |
| 303 | grpc_chttp2_outstanding_ping pings; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 304 | }; |
| 305 | |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 306 | struct grpc_chttp2_transport { |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 307 | grpc_transport base; /* must be first */ |
| 308 | grpc_endpoint *ep; |
| 309 | grpc_mdctx *metadata_context; |
| 310 | gpr_refcount refs; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 311 | |
| 312 | gpr_mu mu; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 313 | |
| 314 | /** is the transport destroying itself? */ |
| 315 | gpr_uint8 destroying; |
| 316 | /** has the upper layer closed the transport? */ |
| 317 | gpr_uint8 closed; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 318 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 319 | /** is a thread currently writing */ |
| 320 | gpr_uint8 writing_active; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 321 | /** is a thread currently parsing */ |
| 322 | gpr_uint8 parsing_active; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 323 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 324 | /** is there a read request to the endpoint outstanding? */ |
| 325 | gpr_uint8 endpoint_reading; |
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 | /** various lists of streams */ |
Craig Tiller | b084d90 | 2015-06-12 07:50:02 -0700 | [diff] [blame] | 328 | grpc_chttp2_stream_list lists[STREAM_LIST_COUNT]; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 329 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 330 | /** global state for reading/writing */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 331 | grpc_chttp2_transport_global global; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 332 | /** state only accessible by the chain of execution that |
| 333 | set writing_active=1 */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 334 | grpc_chttp2_transport_writing writing; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 335 | /** state only accessible by the chain of execution that |
| 336 | set parsing_active=1 */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 337 | grpc_chttp2_transport_parsing parsing; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 338 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 339 | /** maps stream id to grpc_chttp2_stream objects; |
| 340 | owned by the parsing thread when parsing */ |
| 341 | grpc_chttp2_stream_map parsing_stream_map; |
| 342 | |
| 343 | /** streams created by the client (possibly during parsing); |
| 344 | merged with parsing_stream_map during unlock when no |
| 345 | parsing is occurring */ |
| 346 | grpc_chttp2_stream_map new_stream_map; |
| 347 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 348 | /** closure to execute writing */ |
| 349 | grpc_iomgr_closure writing_action; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 350 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 351 | /** address to place a newly accepted stream - set and unset by |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 352 | grpc_chttp2_parsing_accept_stream; used by init_stream to |
| 353 | publish the accepted server stream */ |
| 354 | grpc_chttp2_stream **accepting_stream; |
| 355 | |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 356 | struct { |
| 357 | /** is a thread currently performing channel callbacks */ |
| 358 | gpr_uint8 executing; |
| 359 | /** transport channel-level callback */ |
| 360 | const grpc_transport_callbacks *cb; |
| 361 | /** user data for cb calls */ |
| 362 | void *cb_user_data; |
| 363 | /** closure for notifying transport closure */ |
| 364 | grpc_iomgr_closure notify_closed; |
| 365 | } channel_callback; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 366 | |
| 367 | #if 0 |
| 368 | /* basic state management - what are we doing at the moment? */ |
| 369 | gpr_uint8 reading; |
| 370 | /** are we calling back any grpc_transport_op completion events */ |
| 371 | gpr_uint8 calling_back_ops; |
| 372 | gpr_uint8 destroying; |
| 373 | gpr_uint8 closed; |
| 374 | |
| 375 | /* stream indexing */ |
| 376 | gpr_uint32 next_stream_id; |
| 377 | |
| 378 | /* window management */ |
| 379 | gpr_uint32 outgoing_window_update; |
| 380 | |
| 381 | /* state for a stream that's not yet been created */ |
| 382 | grpc_stream_op_buffer new_stream_sopb; |
| 383 | |
| 384 | /* stream ops that need to be destroyed, but outside of the lock */ |
| 385 | grpc_stream_op_buffer nuke_later_sopb; |
| 386 | |
| 387 | /* pings */ |
| 388 | gpr_int64 ping_counter; |
| 389 | |
| 390 | |
| 391 | grpc_chttp2_stream **accepting_stream; |
| 392 | |
| 393 | #endif |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 394 | }; |
| 395 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 396 | typedef struct { |
| 397 | /** 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] | 398 | gpr_uint32 id; |
| 399 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 400 | grpc_iomgr_closure *send_done_closure; |
| 401 | grpc_iomgr_closure *recv_done_closure; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 402 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 403 | /** window available for us to send to peer */ |
| 404 | gpr_int64 outgoing_window; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 405 | /** window available for peer to send to us - updated after parse */ |
| 406 | gpr_uint32 incoming_window; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 407 | /** stream ops the transport user would like to send */ |
| 408 | grpc_stream_op_buffer *outgoing_sopb; |
| 409 | /** when the application requests writes be closed, the write_closed is |
| 410 | 'queued'; when the close is flow controlled into the send path, we are |
| 411 | 'sending' it; when the write has been performed it is 'sent' */ |
| 412 | grpc_chttp2_write_state write_state; |
| 413 | /** is this stream closed (boolean) */ |
| 414 | gpr_uint8 read_closed; |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame^] | 415 | /** has this stream been cancelled? (boolean) */ |
| 416 | gpr_uint8 cancelled; |
| 417 | /** is this stream in the stream map? (boolean) */ |
| 418 | gpr_uint8 in_stream_map; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 419 | |
| 420 | /** stream state already published to the upper layer */ |
| 421 | grpc_stream_state published_state; |
| 422 | /** address to publish next stream state to */ |
| 423 | grpc_stream_state *publish_state; |
| 424 | /** pointer to sop buffer to fill in with new stream ops */ |
| 425 | grpc_stream_op_buffer *incoming_sopb; |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame^] | 426 | |
| 427 | /** incoming metadata */ |
| 428 | grpc_chttp2_incoming_metadata_buffer incoming_metadata; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 429 | } grpc_chttp2_stream_global; |
| 430 | |
| 431 | typedef struct { |
| 432 | /** HTTP2 stream id for this stream, or zero if one has not been assigned */ |
| 433 | gpr_uint32 id; |
| 434 | /** sops that have passed flow control to be written */ |
| 435 | grpc_stream_op_buffer sopb; |
| 436 | /** how strongly should we indicate closure with the next write */ |
| 437 | grpc_chttp2_send_closed send_closed; |
| 438 | } grpc_chttp2_stream_writing; |
| 439 | |
| 440 | struct grpc_chttp2_stream_parsing { |
| 441 | /** HTTP2 stream id for this stream, or zero if one has not been assigned */ |
| 442 | gpr_uint32 id; |
| 443 | /** has this stream received a close */ |
| 444 | gpr_uint8 received_close; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 445 | /** saw a rst_stream */ |
| 446 | gpr_uint8 saw_rst_stream; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 447 | /** incoming_window has been reduced by this much during parsing */ |
| 448 | gpr_uint32 incoming_window_delta; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 449 | /** window available for peer to send to us */ |
| 450 | gpr_uint32 incoming_window; |
| 451 | /** parsing state for data frames */ |
| 452 | grpc_chttp2_data_parser data_parser; |
Craig Tiller | 3719f07 | 2015-06-12 17:19:51 -0700 | [diff] [blame] | 453 | /** reason give to rst_stream */ |
| 454 | gpr_uint32 rst_stream_reason; |
| 455 | /* amount of window given */ |
| 456 | gpr_uint64 outgoing_window_update; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 457 | |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame^] | 458 | /** incoming metadata */ |
| 459 | grpc_chttp2_incoming_metadata_buffer incoming_metadata; |
| 460 | |
| 461 | /* |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 462 | grpc_linked_mdelem *incoming_metadata; |
| 463 | size_t incoming_metadata_count; |
| 464 | size_t incoming_metadata_capacity; |
| 465 | grpc_linked_mdelem *old_incoming_metadata; |
| 466 | gpr_timespec incoming_deadline; |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame^] | 467 | */ |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 468 | }; |
| 469 | |
| 470 | struct grpc_chttp2_stream { |
| 471 | grpc_chttp2_stream_global global; |
| 472 | grpc_chttp2_stream_writing writing; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 473 | grpc_chttp2_stream_parsing parsing; |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 474 | |
| 475 | grpc_chttp2_stream_link links[STREAM_LIST_COUNT]; |
| 476 | gpr_uint8 included[STREAM_LIST_COUNT]; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 477 | |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 478 | #if 0 |
| 479 | gpr_uint32 outgoing_window_update; |
| 480 | gpr_uint8 cancelled; |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 481 | |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 482 | grpc_stream_state callback_state; |
| 483 | grpc_stream_op_buffer callback_sopb; |
Craig Tiller | 606d874 | 2015-06-15 06:58:50 -0700 | [diff] [blame] | 484 | #endif |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 485 | }; |
| 486 | |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 487 | /** Transport writing call flow: |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 488 | chttp2_transport.c calls grpc_chttp2_unlocking_check_writes to see if writes |
| 489 | are required; |
| 490 | if they are, chttp2_transport.c calls grpc_chttp2_perform_writes to do the |
| 491 | writes. |
| 492 | Once writes have been completed (meaning another write could potentially be |
| 493 | started), |
| 494 | grpc_chttp2_terminate_writing is called. This will call |
| 495 | grpc_chttp2_cleanup_writing, at which |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 496 | point the write phase is complete. */ |
| 497 | |
Craig Tiller | 3208e39 | 2015-06-12 08:17:02 -0700 | [diff] [blame] | 498 | /** Someone is unlocking the transport mutex: check to see if writes |
| 499 | are required, and schedule them if so */ |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 500 | int grpc_chttp2_unlocking_check_writes(grpc_chttp2_transport_global *global, |
| 501 | grpc_chttp2_transport_writing *writing); |
| 502 | void grpc_chttp2_perform_writes( |
| 503 | grpc_chttp2_transport_writing *transport_writing, grpc_endpoint *endpoint); |
| 504 | void grpc_chttp2_terminate_writing( |
| 505 | grpc_chttp2_transport_writing *transport_writing, int success); |
| 506 | void grpc_chttp2_cleanup_writing(grpc_chttp2_transport_global *global, |
| 507 | grpc_chttp2_transport_writing *writing); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 508 | |
| 509 | /** Process one slice of incoming data */ |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 510 | void grpc_chttp2_prepare_to_read(grpc_chttp2_transport_global *global, |
| 511 | grpc_chttp2_transport_parsing *parsing); |
| 512 | int grpc_chttp2_perform_read(grpc_chttp2_transport_parsing *transport_parsing, |
| 513 | gpr_slice slice); |
| 514 | void grpc_chttp2_publish_reads(grpc_chttp2_transport_global *global, |
| 515 | grpc_chttp2_transport_parsing *parsing); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 516 | |
| 517 | /** Get a writable stream |
| 518 | \return non-zero if there was a stream available */ |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 519 | void grpc_chttp2_list_add_writable_stream( |
| 520 | grpc_chttp2_transport_global *transport_global, |
| 521 | grpc_chttp2_stream_global *stream_global); |
| 522 | int grpc_chttp2_list_pop_writable_stream( |
| 523 | grpc_chttp2_transport_global *transport_global, |
| 524 | grpc_chttp2_transport_writing *transport_writing, |
| 525 | grpc_chttp2_stream_global **stream_global, |
| 526 | grpc_chttp2_stream_writing **stream_writing); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 527 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 528 | void grpc_chttp2_list_add_writing_stream( |
| 529 | grpc_chttp2_transport_writing *transport_writing, |
| 530 | grpc_chttp2_stream_writing *stream_writing); |
| 531 | int grpc_chttp2_list_have_writing_streams( |
| 532 | grpc_chttp2_transport_writing *transport_writing); |
| 533 | int grpc_chttp2_list_pop_writing_stream( |
| 534 | grpc_chttp2_transport_writing *transport_writing, |
| 535 | grpc_chttp2_stream_writing **stream_writing); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 536 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 537 | void grpc_chttp2_list_add_written_stream( |
| 538 | grpc_chttp2_transport_writing *transport_writing, |
| 539 | grpc_chttp2_stream_writing *stream_writing); |
| 540 | int grpc_chttp2_list_pop_written_stream( |
| 541 | grpc_chttp2_transport_global *transport_global, |
| 542 | grpc_chttp2_transport_writing *transport_writing, |
| 543 | grpc_chttp2_stream_global **stream_global, |
| 544 | grpc_chttp2_stream_writing **stream_writing); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 545 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 546 | void grpc_chttp2_list_add_writable_window_update_stream( |
| 547 | grpc_chttp2_transport_global *transport_global, |
| 548 | grpc_chttp2_stream_global *stream_global); |
| 549 | int grpc_chttp2_list_pop_writable_window_update_stream( |
| 550 | grpc_chttp2_transport_global *transport_global, |
| 551 | grpc_chttp2_stream_global **stream_global); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 552 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 553 | void grpc_chttp2_list_add_parsing_seen_stream( |
| 554 | grpc_chttp2_transport_parsing *transport_parsing, |
| 555 | grpc_chttp2_stream_parsing *stream_parsing); |
| 556 | int grpc_chttp2_list_pop_parsing_seen_stream( |
| 557 | grpc_chttp2_transport_global *transport_global, |
| 558 | grpc_chttp2_transport_parsing *transport_parsing, |
| 559 | grpc_chttp2_stream_global **stream_global, |
| 560 | grpc_chttp2_stream_parsing **stream_parsing); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 561 | |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame^] | 562 | void grpc_chttp2_list_add_waiting_for_concurrency( |
| 563 | grpc_chttp2_transport_global *transport_global, |
| 564 | grpc_chttp2_stream_global *stream_global); |
| 565 | int grpc_chttp2_list_pop_waiting_for_concurrency( |
| 566 | grpc_chttp2_transport_global *transport_global, |
| 567 | grpc_chttp2_stream_global **stream_global); |
| 568 | |
| 569 | void grpc_chttp2_list_add_cancelled_waiting_for_parsing( |
| 570 | grpc_chttp2_transport_global *transport_global, |
| 571 | grpc_chttp2_stream_global *stream_global); |
| 572 | int grpc_chttp2_list_pop_cancelled_waiting_for_parsing( |
| 573 | grpc_chttp2_transport_global *transport_global, |
| 574 | grpc_chttp2_stream_global **stream_global); |
| 575 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 576 | void grpc_chttp2_schedule_closure( |
| 577 | grpc_chttp2_transport_global *transport_global, grpc_iomgr_closure *closure, |
| 578 | int success); |
| 579 | void grpc_chttp2_read_write_state_changed( |
| 580 | grpc_chttp2_transport_global *transport_global, |
| 581 | grpc_chttp2_stream_global *stream_global); |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame^] | 582 | void grpc_chttp2_incoming_window_state_changed( |
| 583 | grpc_chttp2_transport_global *transport_global, |
| 584 | grpc_chttp2_stream_global *stream_global); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 585 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 586 | grpc_chttp2_stream_parsing *grpc_chttp2_parsing_lookup_stream( |
| 587 | grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id); |
| 588 | grpc_chttp2_stream_parsing *grpc_chttp2_parsing_accept_stream( |
| 589 | grpc_chttp2_transport_parsing *transport_parsing, gpr_uint32 id); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 590 | |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 591 | void grpc_chttp2_add_incoming_goaway(grpc_chttp2_transport_global *transport_global, gpr_uint32 goaway_error, |
| 592 | gpr_slice goaway_text); |
| 593 | |
Craig Tiller | 5dc3b30 | 2015-06-15 16:06:50 -0700 | [diff] [blame^] | 594 | void grpc_chttp2_remove_from_stream_map(grpc_chttp2_transport_global *transport_global, grpc_chttp2_stream_global *stream_global); |
| 595 | |
| 596 | void grpc_chttp2_for_all_streams(grpc_chttp2_transport_global *transport_global, void *user_data, void (*cb)(grpc_chttp2_transport_global *transport_global, void *user_data, grpc_chttp2_stream_global *stream_global)); |
| 597 | |
| 598 | void grpc_chttp2_flowctl_trace(grpc_chttp2_transport *t, const char *flow, |
| 599 | gpr_int32 window, gpr_uint32 id, gpr_int32 delta); |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 600 | |
| 601 | #define GRPC_CHTTP2_CLIENT_CONNECT_STRING "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" |
Craig Tiller | 4aa71a1 | 2015-06-15 13:00:55 -0700 | [diff] [blame] | 602 | #define GRPC_CHTTP2_CLIENT_CONNECT_STRLEN \ |
| 603 | (sizeof(GRPC_CHTTP2_CLIENT_CONNECT_STRING) - 1) |
Craig Tiller | d20efd2 | 2015-06-12 16:17:09 -0700 | [diff] [blame] | 604 | |
| 605 | extern int grpc_http_trace; |
| 606 | |
| 607 | #define IF_TRACING(stmt) \ |
| 608 | if (!(grpc_http_trace)) \ |
| 609 | ; \ |
| 610 | else \ |
| 611 | stmt |
Craig Tiller | 3208e39 | 2015-06-12 08:17:02 -0700 | [diff] [blame] | 612 | |
Craig Tiller | 9b8671c | 2015-06-12 07:41:54 -0700 | [diff] [blame] | 613 | #endif |