Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP/2 mux-demux for connections |
| 3 | * |
| 4 | * Copyright 2017 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/cfgparse.h> |
| 14 | #include <common/config.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 15 | #include <common/h2.h> |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 16 | #include <common/hpack-dec.h> |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 17 | #include <common/hpack-enc.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 18 | #include <common/hpack-tbl.h> |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 19 | #include <common/net_helper.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 20 | #include <proto/connection.h> |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 21 | #include <proto/h1.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 22 | #include <proto/stream.h> |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 23 | #include <types/session.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 24 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 25 | |
| 26 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 27 | /* dummy streams returned for idle and closed states */ |
| 28 | static const struct h2s *h2_closed_stream; |
| 29 | static const struct h2s *h2_idle_stream; |
| 30 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 31 | /* the h2c connection pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 32 | static struct pool_head *pool_head_h2c; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 33 | /* the h2s stream pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 34 | static struct pool_head *pool_head_h2s; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 35 | |
| 36 | /* Connection flags (32 bit), in h2c->flags */ |
| 37 | #define H2_CF_NONE 0x00000000 |
| 38 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 39 | /* Flags indicating why writing to the mux is blocked. */ |
| 40 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 41 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 42 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 43 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 44 | /* Flags indicating why writing to the demux is blocked. |
| 45 | * The first two ones directly affect the ability for the mux to receive data |
| 46 | * from the connection. The other ones affect the mux's ability to demux |
| 47 | * received data. |
| 48 | */ |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 49 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 50 | #define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 51 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 52 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 53 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 54 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 55 | #define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 56 | #define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave |
| 57 | #define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 58 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 59 | /* other flags */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 60 | #define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent |
| 61 | #define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent |
| 62 | #define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 63 | |
| 64 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 65 | /* H2 connection state, in h2c->st0 */ |
| 66 | enum h2_cs { |
| 67 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 68 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 69 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 70 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 71 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame |
| 72 | H2_CS_FRAME_E, // frame payload OK, trying to send RST frame |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 73 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 74 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 75 | H2_CS_ENTRIES // must be last |
| 76 | } __attribute__((packed)); |
| 77 | |
| 78 | /* H2 connection descriptor */ |
| 79 | struct h2c { |
| 80 | struct connection *conn; |
| 81 | |
| 82 | enum h2_cs st0; /* mux state */ |
| 83 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 84 | |
| 85 | /* 16 bit hole here */ |
| 86 | uint32_t flags; /* connection flags: H2_CF_* */ |
| 87 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 88 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 89 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 90 | |
| 91 | /* states for the demux direction */ |
| 92 | struct hpack_dht *ddht; /* demux dynamic header table */ |
| 93 | struct buffer *dbuf; /* demux buffer */ |
| 94 | |
| 95 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 96 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 97 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 98 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 99 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 100 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 101 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 102 | |
| 103 | /* states for the mux direction */ |
| 104 | struct buffer *mbuf; /* mux buffer */ |
| 105 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 106 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 107 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 108 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 109 | /* 16 bit hole here */ |
| 110 | int32_t miw; /* mux initial window size for all new streams */ |
| 111 | int32_t mws; /* mux window size. Can be negative. */ |
| 112 | int32_t mfs; /* mux's max frame size */ |
| 113 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 114 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 115 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 116 | unsigned int nb_streams; /* number of streams in the tree */ |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 117 | unsigned int nb_cs; /* number of attached conn_streams */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 118 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 119 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 120 | struct list send_list; /* list of blocked streams requesting to send */ |
| 121 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 122 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 123 | }; |
| 124 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 125 | /* H2 stream state, in h2s->st */ |
| 126 | enum h2_ss { |
| 127 | H2_SS_IDLE = 0, // idle |
| 128 | H2_SS_RLOC, // reserved(local) |
| 129 | H2_SS_RREM, // reserved(remote) |
| 130 | H2_SS_OPEN, // open |
| 131 | H2_SS_HREM, // half-closed(remote) |
| 132 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 133 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 134 | H2_SS_CLOSED, // closed |
| 135 | H2_SS_ENTRIES // must be last |
| 136 | } __attribute__((packed)); |
| 137 | |
| 138 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 139 | #define H2_SF_NONE 0x00000000 |
| 140 | #define H2_SF_ES_RCVD 0x00000001 |
| 141 | #define H2_SF_ES_SENT 0x00000002 |
| 142 | |
| 143 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 144 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 145 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 146 | /* stream flags indicating the reason the stream is blocked */ |
| 147 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 148 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 149 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 150 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 151 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 152 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 153 | /* stream flags indicating how data is supposed to be sent */ |
| 154 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 155 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 156 | |
| 157 | /* step we're currently in when sending chunks. This is needed because we may |
| 158 | * have to transfer chunks as large as a full buffer so there's no room left |
| 159 | * for size nor crlf around. |
| 160 | */ |
| 161 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 162 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 163 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 164 | |
| 165 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 166 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 167 | #define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 168 | #define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 169 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 170 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 171 | * it is being processed in the internal HTTP representation (H1 for now). |
| 172 | */ |
| 173 | struct h2s { |
| 174 | struct conn_stream *cs; |
| 175 | struct h2c *h2c; |
| 176 | struct h1m req, res; /* request and response parser state for H1 */ |
| 177 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
| 178 | struct list list; /* position in active/blocked lists if blocked>0 */ |
| 179 | int32_t id; /* stream ID */ |
| 180 | uint32_t flags; /* H2_SF_* */ |
| 181 | int mws; /* mux window size for this stream */ |
| 182 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 183 | enum h2_ss st; |
| 184 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 185 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 186 | /* descriptor for an h2 frame header */ |
| 187 | struct h2_fh { |
| 188 | uint32_t len; /* length, host order, 24 bits */ |
| 189 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 190 | uint8_t ft; /* frame type */ |
| 191 | uint8_t ff; /* frame flags */ |
| 192 | }; |
| 193 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 194 | /* a few settings from the global section */ |
| 195 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 196 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 197 | static int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 198 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 199 | /* a dmumy closed stream */ |
| 200 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 201 | .cs = NULL, |
| 202 | .h2c = NULL, |
| 203 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 204 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 205 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 206 | .id = 0, |
| 207 | }; |
| 208 | |
| 209 | /* and a dummy idle stream for use with any unannounced stream */ |
| 210 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 211 | .cs = NULL, |
| 212 | .h2c = NULL, |
| 213 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 214 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 215 | .id = 0, |
| 216 | }; |
| 217 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 218 | static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 219 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 220 | /*****************************************************/ |
| 221 | /* functions below are for dynamic buffer management */ |
| 222 | /*****************************************************/ |
| 223 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 224 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 225 | * to receive data into the buffer and/or demux pending data. The condition is |
| 226 | * a bit complex due to some API limits for now. The rules are the following : |
| 227 | * - if an error or a shutdown was detected on the connection and the buffer |
| 228 | * is empty, we must not attempt to receive |
| 229 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 230 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 231 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 232 | * regardless of whether the demux buffer is full or not, so that only |
| 233 | * de demux part decides whether or not to block. This is needed because |
| 234 | * the connection API indeed prevents us from re-enabling receipt that is |
| 235 | * already enabled in a polled state, so we must always immediately stop |
| 236 | * as soon as the demux can't proceed so as never to hit an end of read |
| 237 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 238 | * - otherwise must may not attempt |
| 239 | */ |
| 240 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 241 | { |
| 242 | if (h2c->dbuf->i == 0 && |
| 243 | (h2c->st0 >= H2_CS_ERROR || |
| 244 | h2c->conn->flags & CO_FL_ERROR || |
| 245 | conn_xprt_read0_pending(h2c->conn))) |
| 246 | return 0; |
| 247 | |
| 248 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 249 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 250 | return 1; |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 255 | /* returns true if the connection has too many conn_streams attached */ |
| 256 | static inline int h2_has_too_many_cs(const struct h2c *h2c) |
| 257 | { |
| 258 | return h2c->nb_cs >= h2_settings_max_concurrent_streams; |
| 259 | } |
| 260 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 261 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 262 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 263 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 264 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 265 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 266 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 267 | { |
| 268 | struct h2c *h2c = target; |
| 269 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 270 | if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) { |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 271 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 272 | if (h2_recv_allowed(h2c)) |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 273 | conn_xprt_want_recv(h2c->conn); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 274 | return 1; |
| 275 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 276 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 277 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) { |
| 278 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
| 279 | if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY)) |
| 280 | conn_xprt_want_send(h2c->conn); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 281 | |
| 282 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 283 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 284 | if (h2_recv_allowed(h2c)) |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 285 | conn_xprt_want_recv(h2c->conn); |
| 286 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 287 | return 1; |
| 288 | } |
| 289 | return 0; |
| 290 | } |
| 291 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 292 | static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer **bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 293 | { |
| 294 | struct buffer *buf = NULL; |
| 295 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 296 | if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) && |
| 297 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 298 | h2c->buf_wait.target = h2c; |
| 299 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 300 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 301 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 302 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 303 | __conn_xprt_stop_recv(h2c->conn); |
| 304 | } |
| 305 | return buf; |
| 306 | } |
| 307 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 308 | static inline void h2_release_buf(struct h2c *h2c, struct buffer **bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 309 | { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 310 | if ((*bptr)->size) { |
| 311 | b_free(bptr); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 312 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 316 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 317 | /*****************************************************************/ |
| 318 | /* functions below are dedicated to the mux setup and management */ |
| 319 | /*****************************************************************/ |
| 320 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 321 | /* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */ |
| 322 | static int h2c_frt_init(struct connection *conn) |
| 323 | { |
| 324 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 325 | struct task *t = NULL; |
| 326 | struct session *sess = conn->owner; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 327 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 328 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 329 | if (!h2c) |
| 330 | goto fail; |
| 331 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 332 | |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 333 | h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client; |
| 334 | if (tick_isset(sess->fe->timeout.clientfin)) |
| 335 | h2c->shut_timeout = sess->fe->timeout.clientfin; |
| 336 | |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 337 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 338 | if (tick_isset(h2c->timeout)) { |
| 339 | t = task_new(tid_bit); |
| 340 | if (!t) |
| 341 | goto fail; |
| 342 | |
| 343 | h2c->task = t; |
| 344 | t->process = h2_timeout_task; |
| 345 | t->context = h2c; |
| 346 | t->expire = tick_add(now_ms, h2c->timeout); |
| 347 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 348 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 349 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 350 | if (!h2c->ddht) |
| 351 | goto fail; |
| 352 | |
| 353 | /* Initialise the context. */ |
| 354 | h2c->st0 = H2_CS_PREFACE; |
| 355 | h2c->conn = conn; |
| 356 | h2c->max_id = -1; |
| 357 | h2c->errcode = H2_ERR_NO_ERROR; |
| 358 | h2c->flags = H2_CF_NONE; |
| 359 | h2c->rcvd_c = 0; |
| 360 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 361 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 362 | h2c->nb_cs = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 363 | |
| 364 | h2c->dbuf = &buf_empty; |
| 365 | h2c->dsi = -1; |
| 366 | h2c->msi = -1; |
| 367 | h2c->last_sid = -1; |
| 368 | |
| 369 | h2c->mbuf = &buf_empty; |
| 370 | h2c->miw = 65535; /* mux initial window size */ |
| 371 | h2c->mws = 65535; /* mux window size */ |
| 372 | h2c->mfs = 16384; /* initial max frame size */ |
| 373 | h2c->streams_by_id = EB_ROOT_UNIQUE; |
| 374 | LIST_INIT(&h2c->send_list); |
| 375 | LIST_INIT(&h2c->fctl_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 376 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 377 | conn->mux_ctx = h2c; |
| 378 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 379 | if (t) |
| 380 | task_queue(t); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 381 | conn_xprt_want_recv(conn); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 382 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 383 | /* mux->wake will be called soon to complete the operation */ |
| 384 | return 0; |
| 385 | fail: |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 386 | if (t) |
| 387 | task_free(t); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 388 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 389 | return -1; |
| 390 | } |
| 391 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 392 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 393 | * is already initialized before installing the mux, so we detect incoming |
| 394 | * connections from the fact that the context is still NULL. Returns < 0 on |
| 395 | * error. |
| 396 | */ |
| 397 | static int h2_init(struct connection *conn) |
| 398 | { |
| 399 | if (conn->mux_ctx) { |
| 400 | /* we don't support outgoing connections for now */ |
| 401 | return -1; |
| 402 | } |
| 403 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 404 | return h2c_frt_init(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 407 | /* returns the stream associated with id <id> or NULL if not found */ |
| 408 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 409 | { |
| 410 | struct eb32_node *node; |
| 411 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 412 | if (id > h2c->max_id) |
| 413 | return (struct h2s *)h2_idle_stream; |
| 414 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 415 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 416 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 417 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 418 | |
| 419 | return container_of(node, struct h2s, by_id); |
| 420 | } |
| 421 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 422 | /* release function for a connection. This one should be called to free all |
| 423 | * resources allocated to the mux. |
| 424 | */ |
| 425 | static void h2_release(struct connection *conn) |
| 426 | { |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 427 | struct h2c *h2c = conn->mux_ctx; |
| 428 | |
| 429 | LIST_DEL(&conn->list); |
| 430 | |
| 431 | if (h2c) { |
| 432 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 433 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 434 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 435 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 436 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 437 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 438 | h2_release_buf(h2c, &h2c->dbuf); |
| 439 | h2_release_buf(h2c, &h2c->mbuf); |
| 440 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 441 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 442 | h2c->task->context = NULL; |
| 443 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 444 | h2c->task = NULL; |
| 445 | } |
| 446 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 447 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | conn->mux = NULL; |
| 451 | conn->mux_ctx = NULL; |
| 452 | |
| 453 | conn_stop_tracking(conn); |
| 454 | conn_full_close(conn); |
| 455 | if (conn->destroy_cb) |
| 456 | conn->destroy_cb(conn); |
| 457 | conn_free(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 461 | /******************************************************/ |
| 462 | /* functions below are for the H2 protocol processing */ |
| 463 | /******************************************************/ |
| 464 | |
| 465 | /* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 466 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 467 | { |
| 468 | return h2s ? h2s->id : 0; |
| 469 | } |
| 470 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 471 | /* returns true of the mux is currently busy as seen from stream <h2s> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 472 | static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s) |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 473 | { |
| 474 | if (h2c->msi < 0) |
| 475 | return 0; |
| 476 | |
| 477 | if (h2c->msi == h2s_id(h2s)) |
| 478 | return 0; |
| 479 | |
| 480 | return 1; |
| 481 | } |
| 482 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 483 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 484 | static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err) |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 485 | { |
| 486 | h2c->errcode = err; |
| 487 | h2c->st0 = H2_CS_ERROR; |
| 488 | } |
| 489 | |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 490 | /* marks an error on the stream */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 491 | static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err) |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 492 | { |
| 493 | if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) { |
| 494 | h2s->errcode = err; |
| 495 | h2s->st = H2_SS_ERROR; |
| 496 | if (h2s->cs) |
| 497 | h2s->cs->flags |= CS_FL_ERROR; |
| 498 | } |
| 499 | } |
| 500 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 501 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 502 | static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len) |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 503 | { |
| 504 | uint8_t *out = frame; |
| 505 | |
| 506 | *out = len >> 16; |
| 507 | write_n16(out + 1, len); |
| 508 | } |
| 509 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 510 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 511 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 512 | * the caller's responsibility to verify that there are at least <bytes> bytes |
| 513 | * available in the buffer's input prior to calling this function. |
| 514 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 515 | static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes, |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 516 | const struct buffer *b, int o) |
| 517 | { |
| 518 | readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 519 | } |
| 520 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 521 | static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 522 | { |
| 523 | return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 524 | } |
| 525 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 526 | static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 527 | { |
| 528 | return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 529 | } |
| 530 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 531 | static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 532 | { |
| 533 | return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 534 | } |
| 535 | |
| 536 | |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 537 | /* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm |
| 538 | * is not obvious. It turns out that H2 headers are neither aligned nor do they |
| 539 | * use regular sizes. And to add to the trouble, the buffer may wrap so each |
| 540 | * byte read must be checked. The header is formed like this : |
| 541 | * |
| 542 | * b0 b1 b2 b3 b4 b5..b8 |
| 543 | * +----------+---------+--------+----+----+----------------------+ |
| 544 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 545 | * +----------+---------+--------+----+----+----------------------+ |
| 546 | * |
| 547 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 548 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 549 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 550 | * the word, and only one extra read is needed to fetch len[16:23]. |
| 551 | * Returns zero if some bytes are missing, otherwise non-zero on success. |
| 552 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 553 | static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 554 | { |
| 555 | uint64_t w; |
| 556 | |
| 557 | if (b->i < 9) |
| 558 | return 0; |
| 559 | |
| 560 | w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data); |
| 561 | h->len = *b->p << 16; |
| 562 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 563 | h->ff = w >> 32; |
| 564 | h->ft = w >> 40; |
| 565 | h->len += w >> 48; |
| 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 570 | * h2_peek_frame_hdr() above. |
| 571 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 572 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 573 | { |
| 574 | bi_del(b, 9); |
| 575 | } |
| 576 | |
| 577 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 578 | static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 579 | { |
| 580 | int ret; |
| 581 | |
| 582 | ret = h2_peek_frame_hdr(b, h); |
| 583 | if (ret > 0) |
| 584 | h2_skip_frame_hdr(b); |
| 585 | return ret; |
| 586 | } |
| 587 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 588 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 589 | * its connection if the stream was not yet closed. Please use this exclusively |
| 590 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 591 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 592 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 593 | { |
| 594 | if (h2s->st != H2_SS_CLOSED) |
| 595 | h2s->h2c->nb_streams--; |
| 596 | h2s->st = H2_SS_CLOSED; |
| 597 | } |
| 598 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 599 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 600 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 601 | { |
| 602 | h2s_close(h2s); |
Willy Tarreau | 4a333d3 | 2018-03-28 11:29:04 +0200 | [diff] [blame] | 603 | LIST_DEL(&h2s->list); |
| 604 | LIST_INIT(&h2s->list); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 605 | eb32_delete(&h2s->by_id); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 606 | pool_free(pool_head_h2s, h2s); |
| 607 | } |
| 608 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 609 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 610 | * case of memory allocation error. |
| 611 | */ |
| 612 | static struct h2s *h2c_stream_new(struct h2c *h2c, int id) |
| 613 | { |
| 614 | struct conn_stream *cs; |
| 615 | struct h2s *h2s; |
| 616 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 617 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 618 | if (!h2s) |
| 619 | goto out; |
| 620 | |
| 621 | h2s->h2c = h2c; |
| 622 | h2s->mws = h2c->miw; |
| 623 | h2s->flags = H2_SF_NONE; |
| 624 | h2s->errcode = H2_ERR_NO_ERROR; |
| 625 | h2s->st = H2_SS_IDLE; |
| 626 | h1m_init(&h2s->req); |
| 627 | h1m_init(&h2s->res); |
| 628 | h2s->by_id.key = h2s->id = id; |
| 629 | h2c->max_id = id; |
| 630 | LIST_INIT(&h2s->list); |
| 631 | |
| 632 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 633 | h2c->nb_streams++; |
| 634 | if (h2c->nb_streams > h2_settings_max_concurrent_streams) |
| 635 | goto out_close; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 636 | |
| 637 | cs = cs_new(h2c->conn); |
| 638 | if (!cs) |
| 639 | goto out_close; |
| 640 | |
| 641 | h2s->cs = cs; |
| 642 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 643 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 644 | |
| 645 | if (stream_create_from_cs(cs) < 0) |
| 646 | goto out_free_cs; |
| 647 | |
| 648 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 649 | if (h2_has_too_many_cs(h2c)) |
| 650 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 651 | return h2s; |
| 652 | |
| 653 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 654 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 655 | cs_free(cs); |
| 656 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 657 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 658 | h2s = NULL; |
| 659 | out: |
| 660 | return h2s; |
| 661 | } |
| 662 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 663 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 664 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 665 | * the various settings codes. |
| 666 | */ |
| 667 | static int h2c_snd_settings(struct h2c *h2c) |
| 668 | { |
| 669 | struct buffer *res; |
| 670 | char buf_data[100]; // enough for 15 settings |
| 671 | struct chunk buf; |
| 672 | int ret; |
| 673 | |
| 674 | if (h2c_mux_busy(h2c, NULL)) { |
| 675 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 676 | return 0; |
| 677 | } |
| 678 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 679 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 680 | if (!res) { |
| 681 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 682 | h2c->flags |= H2_CF_DEM_MROOM; |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 687 | chunk_memcpy(&buf, |
| 688 | "\x00\x00\x00" /* length : 0 for now */ |
| 689 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 690 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 691 | 9); |
| 692 | |
| 693 | if (h2_settings_header_table_size != 4096) { |
| 694 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 695 | |
| 696 | write_n32(str + 2, h2_settings_header_table_size); |
| 697 | chunk_memcat(&buf, str, 6); |
| 698 | } |
| 699 | |
| 700 | if (h2_settings_initial_window_size != 65535) { |
| 701 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 702 | |
| 703 | write_n32(str + 2, h2_settings_initial_window_size); |
| 704 | chunk_memcat(&buf, str, 6); |
| 705 | } |
| 706 | |
| 707 | if (h2_settings_max_concurrent_streams != 0) { |
| 708 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 709 | |
| 710 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 711 | * the protocol, so never send this value! |
| 712 | */ |
| 713 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 714 | chunk_memcat(&buf, str, 6); |
| 715 | } |
| 716 | |
| 717 | if (global.tune.bufsize != 16384) { |
| 718 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 719 | |
| 720 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 721 | * match bufsize - rewrite size, but at the moment it seems |
| 722 | * that clients don't take care of it. |
| 723 | */ |
| 724 | write_n32(str + 2, global.tune.bufsize); |
| 725 | chunk_memcat(&buf, str, 6); |
| 726 | } |
| 727 | |
| 728 | h2_set_frame_size(buf.str, buf.len - 9); |
| 729 | ret = bo_istput(res, ist2(buf.str, buf.len)); |
| 730 | if (unlikely(ret <= 0)) { |
| 731 | if (!ret) { |
| 732 | h2c->flags |= H2_CF_MUX_MFULL; |
| 733 | h2c->flags |= H2_CF_DEM_MROOM; |
| 734 | return 0; |
| 735 | } |
| 736 | else { |
| 737 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 738 | return 0; |
| 739 | } |
| 740 | } |
| 741 | return ret; |
| 742 | } |
| 743 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 744 | /* Try to receive a connection preface, then upon success try to send our |
| 745 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 746 | * missing data. It may return an error in h2c. |
| 747 | */ |
| 748 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 749 | { |
| 750 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 751 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 752 | |
| 753 | ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE)); |
| 754 | |
| 755 | if (unlikely(ret1 <= 0)) { |
| 756 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 757 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 758 | return 0; |
| 759 | } |
| 760 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 761 | ret2 = h2c_snd_settings(h2c); |
| 762 | if (ret2 > 0) |
| 763 | bi_del(h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 764 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 765 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 768 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 769 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 770 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 771 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 772 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 773 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 774 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 775 | * case so that it is safe to use h2c_error() to report such errors. |
| 776 | */ |
| 777 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 778 | { |
| 779 | struct buffer *res; |
| 780 | char str[17]; |
| 781 | int ret; |
| 782 | |
| 783 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 784 | return 1; // claim that it worked |
| 785 | |
| 786 | if (h2c_mux_busy(h2c, h2s)) { |
| 787 | if (h2s) |
| 788 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 789 | else |
| 790 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 791 | return 0; |
| 792 | } |
| 793 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 794 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 795 | if (!res) { |
| 796 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 797 | if (h2s) |
| 798 | h2s->flags |= H2_SF_BLK_MROOM; |
| 799 | else |
| 800 | h2c->flags |= H2_CF_DEM_MROOM; |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 805 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 806 | |
| 807 | if (h2c->last_sid < 0) |
| 808 | h2c->last_sid = h2c->max_id; |
| 809 | |
| 810 | write_n32(str + 9, h2c->last_sid); |
| 811 | write_n32(str + 13, h2c->errcode); |
| 812 | ret = bo_istput(res, ist2(str, 17)); |
| 813 | if (unlikely(ret <= 0)) { |
| 814 | if (!ret) { |
| 815 | h2c->flags |= H2_CF_MUX_MFULL; |
| 816 | if (h2s) |
| 817 | h2s->flags |= H2_SF_BLK_MROOM; |
| 818 | else |
| 819 | h2c->flags |= H2_CF_DEM_MROOM; |
| 820 | return 0; |
| 821 | } |
| 822 | else { |
| 823 | /* we cannot report this error using GOAWAY, so we mark |
| 824 | * it and claim a success. |
| 825 | */ |
| 826 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 827 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 828 | return 1; |
| 829 | } |
| 830 | } |
| 831 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 832 | return ret; |
| 833 | } |
| 834 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 835 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 836 | * during mux operations. This stream must be valid and cannot be closed |
| 837 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 838 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 839 | * not yet. |
| 840 | * |
| 841 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 842 | * to write the message, it subscribes the stream to future notifications. |
| 843 | */ |
| 844 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 845 | { |
| 846 | struct buffer *res; |
| 847 | char str[13]; |
| 848 | int ret; |
| 849 | |
| 850 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 851 | return 1; |
| 852 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 853 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 854 | * RST_STREAM in response to a RST_STREAM frame. |
| 855 | */ |
| 856 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 857 | ret = 1; |
| 858 | goto ignore; |
| 859 | } |
| 860 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 861 | if (h2c_mux_busy(h2c, h2s)) { |
| 862 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 863 | return 0; |
| 864 | } |
| 865 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 866 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 867 | if (!res) { |
| 868 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 869 | h2s->flags |= H2_SF_BLK_MROOM; |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | /* len: 4, type: 3, flags: none */ |
| 874 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 875 | write_n32(str + 5, h2s->id); |
| 876 | write_n32(str + 9, h2s->errcode); |
| 877 | ret = bo_istput(res, ist2(str, 13)); |
| 878 | |
| 879 | if (unlikely(ret <= 0)) { |
| 880 | if (!ret) { |
| 881 | h2c->flags |= H2_CF_MUX_MFULL; |
| 882 | h2s->flags |= H2_SF_BLK_MROOM; |
| 883 | return 0; |
| 884 | } |
| 885 | else { |
| 886 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 887 | return 0; |
| 888 | } |
| 889 | } |
| 890 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 891 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 892 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 893 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 898 | * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the |
| 899 | * error code unless the stream's state already is IDLE or CLOSED in which |
| 900 | * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if |
| 901 | * it was not yet. |
| 902 | * |
| 903 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 904 | * to write the message, it blocks the demuxer and subscribes it to future |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 905 | * notifications. It's worth mentionning that an RST may even be sent for a |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 906 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 907 | */ |
| 908 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 909 | { |
| 910 | struct buffer *res; |
| 911 | char str[13]; |
| 912 | int ret; |
| 913 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 914 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 915 | * RST_STREAM in response to a RST_STREAM frame. |
| 916 | */ |
| 917 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 918 | ret = 1; |
| 919 | goto ignore; |
| 920 | } |
| 921 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 922 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 923 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 924 | return 0; |
| 925 | } |
| 926 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 927 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 928 | if (!res) { |
| 929 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 930 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | /* len: 4, type: 3, flags: none */ |
| 935 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 936 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 937 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 938 | write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ? |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 939 | h2s->errcode : H2_ERR_STREAM_CLOSED); |
| 940 | ret = bo_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 941 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 942 | if (unlikely(ret <= 0)) { |
| 943 | if (!ret) { |
| 944 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 945 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 946 | return 0; |
| 947 | } |
| 948 | else { |
| 949 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 950 | return 0; |
| 951 | } |
| 952 | } |
| 953 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 954 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 955 | if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 956 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 957 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 958 | } |
| 959 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 960 | return ret; |
| 961 | } |
| 962 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 963 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 964 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 965 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 966 | * on success or zero if nothing was done. In case of lack of room to write the |
| 967 | * message, it subscribes the requesting stream to future notifications. |
| 968 | */ |
| 969 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 970 | { |
| 971 | struct h2c *h2c = h2s->h2c; |
| 972 | struct buffer *res; |
| 973 | char str[9]; |
| 974 | int ret; |
| 975 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 976 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 977 | return 1; |
| 978 | |
| 979 | if (h2c_mux_busy(h2c, h2s)) { |
| 980 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 981 | return 0; |
| 982 | } |
| 983 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 984 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 985 | if (!res) { |
| 986 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 987 | h2s->flags |= H2_SF_BLK_MROOM; |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 992 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 993 | write_n32(str + 5, h2s->id); |
| 994 | ret = bo_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 995 | if (likely(ret > 0)) { |
| 996 | h2s->flags |= H2_SF_ES_SENT; |
| 997 | } |
| 998 | else if (!ret) { |
| 999 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1000 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1001 | return 0; |
| 1002 | } |
| 1003 | else { |
| 1004 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1005 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1006 | } |
| 1007 | return ret; |
| 1008 | } |
| 1009 | |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1010 | /* wake the streams attached to the connection, whose id is greater than <last>, |
| 1011 | * and assign their conn_stream the CS_FL_* flags <flags> in addition to |
| 1012 | * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The |
| 1013 | * stream's state is automatically updated accordingly. |
| 1014 | */ |
| 1015 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 1016 | { |
| 1017 | struct eb32_node *node; |
| 1018 | struct h2s *h2s; |
| 1019 | |
| 1020 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
| 1021 | flags |= CS_FL_ERROR; |
| 1022 | |
| 1023 | if (conn_xprt_read0_pending(h2c->conn)) |
| 1024 | flags |= CS_FL_EOS; |
| 1025 | |
| 1026 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1027 | while (node) { |
| 1028 | h2s = container_of(node, struct h2s, by_id); |
| 1029 | if (h2s->id <= last) |
| 1030 | break; |
| 1031 | node = eb32_next(node); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1032 | |
| 1033 | if (!h2s->cs) { |
| 1034 | /* this stream was already orphaned */ |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1035 | h2s_destroy(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1036 | continue; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1037 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1038 | |
| 1039 | h2s->cs->flags |= flags; |
| 1040 | /* recv is used to force to detect CS_FL_EOS that wake() |
| 1041 | * doesn't handle in the stream int code. |
| 1042 | */ |
| 1043 | h2s->cs->data_cb->recv(h2s->cs); |
| 1044 | h2s->cs->data_cb->wake(h2s->cs); |
| 1045 | |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1046 | if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR) |
| 1047 | h2s->st = H2_SS_ERROR; |
| 1048 | else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN) |
| 1049 | h2s->st = H2_SS_HREM; |
| 1050 | else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1051 | h2s_close(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1055 | /* Increase all streams' outgoing window size by the difference passed in |
| 1056 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1057 | * window size is different. The difference may be negative and the resulting |
| 1058 | * window size as well, for the time it takes to receive some window updates. |
| 1059 | */ |
| 1060 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1061 | { |
| 1062 | struct h2s *h2s; |
| 1063 | struct eb32_node *node; |
| 1064 | |
| 1065 | if (!diff) |
| 1066 | return; |
| 1067 | |
| 1068 | node = eb32_first(&h2c->streams_by_id); |
| 1069 | while (node) { |
| 1070 | h2s = container_of(node, struct h2s, by_id); |
| 1071 | h2s->mws += diff; |
| 1072 | node = eb32_next(node); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1077 | * ACKs it if needed. Returns > 0 on success or zero on missing data. It may |
| 1078 | * return an error in h2c. Described in RFC7540#6.5. |
| 1079 | */ |
| 1080 | static int h2c_handle_settings(struct h2c *h2c) |
| 1081 | { |
| 1082 | unsigned int offset; |
| 1083 | int error; |
| 1084 | |
| 1085 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1086 | if (h2c->dfl) { |
| 1087 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1088 | goto fail; |
| 1089 | } |
| 1090 | return 1; |
| 1091 | } |
| 1092 | |
| 1093 | if (h2c->dsi != 0) { |
| 1094 | error = H2_ERR_PROTOCOL_ERROR; |
| 1095 | goto fail; |
| 1096 | } |
| 1097 | |
| 1098 | if (h2c->dfl % 6) { |
| 1099 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1100 | goto fail; |
| 1101 | } |
| 1102 | |
| 1103 | /* that's the limit we can process */ |
| 1104 | if (h2c->dfl > global.tune.bufsize) { |
| 1105 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1106 | goto fail; |
| 1107 | } |
| 1108 | |
| 1109 | /* process full frame only */ |
| 1110 | if (h2c->dbuf->i < h2c->dfl) |
| 1111 | return 0; |
| 1112 | |
| 1113 | /* parse the frame */ |
| 1114 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
| 1115 | uint16_t type = h2_get_n16(h2c->dbuf, offset); |
| 1116 | int32_t arg = h2_get_n32(h2c->dbuf, offset + 2); |
| 1117 | |
| 1118 | switch (type) { |
| 1119 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1120 | /* we need to update all existing streams with the |
| 1121 | * difference from the previous iws. |
| 1122 | */ |
| 1123 | if (arg < 0) { // RFC7540#6.5.2 |
| 1124 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1125 | goto fail; |
| 1126 | } |
| 1127 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1128 | h2c->miw = arg; |
| 1129 | break; |
| 1130 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1131 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1132 | error = H2_ERR_PROTOCOL_ERROR; |
| 1133 | goto fail; |
| 1134 | } |
| 1135 | h2c->mfs = arg; |
| 1136 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1137 | case H2_SETTINGS_ENABLE_PUSH: |
| 1138 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1139 | error = H2_ERR_PROTOCOL_ERROR; |
| 1140 | goto fail; |
| 1141 | } |
| 1142 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | /* need to ACK this frame now */ |
| 1147 | h2c->st0 = H2_CS_FRAME_A; |
| 1148 | return 1; |
| 1149 | fail: |
| 1150 | h2c_error(h2c, error); |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1155 | * success or one of the h2_status values. |
| 1156 | */ |
| 1157 | static int h2c_ack_settings(struct h2c *h2c) |
| 1158 | { |
| 1159 | struct buffer *res; |
| 1160 | char str[9]; |
| 1161 | int ret = -1; |
| 1162 | |
| 1163 | if (h2c_mux_busy(h2c, NULL)) { |
| 1164 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1168 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1169 | if (!res) { |
| 1170 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1171 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | memcpy(str, |
| 1176 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1177 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1178 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1179 | |
| 1180 | ret = bo_istput(res, ist2(str, 9)); |
| 1181 | if (unlikely(ret <= 0)) { |
| 1182 | if (!ret) { |
| 1183 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1184 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1185 | return 0; |
| 1186 | } |
| 1187 | else { |
| 1188 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1189 | return 0; |
| 1190 | } |
| 1191 | } |
| 1192 | return ret; |
| 1193 | } |
| 1194 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1195 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1196 | * the pointer to the payload in <payload>. Returns > 0 on success or zero on |
| 1197 | * missing data. It may return an error in h2c. |
| 1198 | */ |
| 1199 | static int h2c_handle_ping(struct h2c *h2c) |
| 1200 | { |
| 1201 | /* frame length must be exactly 8 */ |
| 1202 | if (h2c->dfl != 8) { |
| 1203 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1208 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1209 | h2c->st0 = H2_CS_FRAME_A; |
| 1210 | return 1; |
| 1211 | } |
| 1212 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1213 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1214 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1215 | * error in h2c. |
| 1216 | */ |
| 1217 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1218 | { |
| 1219 | struct buffer *res; |
| 1220 | char str[13]; |
| 1221 | int ret = -1; |
| 1222 | |
| 1223 | if (h2c_mux_busy(h2c, NULL)) { |
| 1224 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1228 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1229 | if (!res) { |
| 1230 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1231 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | /* length: 4, type: 8, flags: none */ |
| 1236 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1237 | write_n32(str + 5, sid); |
| 1238 | write_n32(str + 9, increment); |
| 1239 | |
| 1240 | ret = bo_istput(res, ist2(str, 13)); |
| 1241 | |
| 1242 | if (unlikely(ret <= 0)) { |
| 1243 | if (!ret) { |
| 1244 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1245 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1246 | return 0; |
| 1247 | } |
| 1248 | else { |
| 1249 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1250 | return 0; |
| 1251 | } |
| 1252 | } |
| 1253 | return ret; |
| 1254 | } |
| 1255 | |
| 1256 | /* try to send pending window update for the connection. It's safe to call it |
| 1257 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1258 | * failure. It may return an error in h2c. |
| 1259 | */ |
| 1260 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1261 | { |
| 1262 | int ret = 1; |
| 1263 | |
| 1264 | if (h2c->rcvd_c <= 0) |
| 1265 | return 1; |
| 1266 | |
| 1267 | /* send WU for the connection */ |
| 1268 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1269 | if (ret > 0) |
| 1270 | h2c->rcvd_c = 0; |
| 1271 | |
| 1272 | return ret; |
| 1273 | } |
| 1274 | |
| 1275 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1276 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1277 | * room or failure. It may return an error in h2c. |
| 1278 | */ |
| 1279 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1280 | { |
| 1281 | int ret = 1; |
| 1282 | |
| 1283 | if (h2c->rcvd_s <= 0) |
| 1284 | return 1; |
| 1285 | |
| 1286 | /* send WU for the stream */ |
| 1287 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1288 | if (ret > 0) |
| 1289 | h2c->rcvd_s = 0; |
| 1290 | |
| 1291 | return ret; |
| 1292 | } |
| 1293 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1294 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1295 | * success, 0 on missing data or one of the h2_status values. |
| 1296 | */ |
| 1297 | static int h2c_ack_ping(struct h2c *h2c) |
| 1298 | { |
| 1299 | struct buffer *res; |
| 1300 | char str[17]; |
| 1301 | int ret = -1; |
| 1302 | |
| 1303 | if (h2c->dbuf->i < 8) |
| 1304 | return 0; |
| 1305 | |
| 1306 | if (h2c_mux_busy(h2c, NULL)) { |
| 1307 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1311 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1312 | if (!res) { |
| 1313 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1314 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | memcpy(str, |
| 1319 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1320 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1321 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1322 | |
| 1323 | /* copy the original payload */ |
| 1324 | h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0); |
| 1325 | |
| 1326 | ret = bo_istput(res, ist2(str, 17)); |
| 1327 | if (unlikely(ret <= 0)) { |
| 1328 | if (!ret) { |
| 1329 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1330 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1331 | return 0; |
| 1332 | } |
| 1333 | else { |
| 1334 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1335 | return 0; |
| 1336 | } |
| 1337 | } |
| 1338 | return ret; |
| 1339 | } |
| 1340 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1341 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1342 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1343 | * h2c or h2s. Described in RFC7540#6.9. |
| 1344 | */ |
| 1345 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1346 | { |
| 1347 | int32_t inc; |
| 1348 | int error; |
| 1349 | |
| 1350 | if (h2c->dfl != 4) { |
| 1351 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1352 | goto conn_err; |
| 1353 | } |
| 1354 | |
| 1355 | /* process full frame only */ |
| 1356 | if (h2c->dbuf->i < h2c->dfl) |
| 1357 | return 0; |
| 1358 | |
| 1359 | inc = h2_get_n32(h2c->dbuf, 0); |
| 1360 | |
| 1361 | if (h2c->dsi != 0) { |
| 1362 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1363 | |
| 1364 | /* it's not an error to receive WU on a closed stream */ |
| 1365 | if (h2s->st == H2_SS_CLOSED) |
| 1366 | return 1; |
| 1367 | |
| 1368 | if (!inc) { |
| 1369 | error = H2_ERR_PROTOCOL_ERROR; |
| 1370 | goto strm_err; |
| 1371 | } |
| 1372 | |
| 1373 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1374 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1375 | goto strm_err; |
| 1376 | } |
| 1377 | |
| 1378 | h2s->mws += inc; |
| 1379 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1380 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
| 1381 | if (h2s->cs && LIST_ISEMPTY(&h2s->list) && |
| 1382 | (h2s->cs->flags & CS_FL_DATA_WR_ENA)) { |
| 1383 | /* This stream wanted to send but could not due to its |
| 1384 | * own flow control. We can put it back into the send |
| 1385 | * list now, it will be handled upon next send() call. |
| 1386 | */ |
| 1387 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | else { |
| 1392 | /* connection window update */ |
| 1393 | if (!inc) { |
| 1394 | error = H2_ERR_PROTOCOL_ERROR; |
| 1395 | goto conn_err; |
| 1396 | } |
| 1397 | |
| 1398 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1399 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1400 | goto conn_err; |
| 1401 | } |
| 1402 | |
| 1403 | h2c->mws += inc; |
| 1404 | } |
| 1405 | |
| 1406 | return 1; |
| 1407 | |
| 1408 | conn_err: |
| 1409 | h2c_error(h2c, error); |
| 1410 | return 0; |
| 1411 | |
| 1412 | strm_err: |
| 1413 | if (h2s) { |
| 1414 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1415 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1416 | } |
| 1417 | else |
| 1418 | h2c_error(h2c, error); |
| 1419 | return 0; |
| 1420 | } |
| 1421 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1422 | /* processes a GOAWAY frame, and signals all streams whose ID is greater than |
| 1423 | * the last ID. Returns > 0 on success or zero on missing data. It may return |
| 1424 | * an error in h2c. Described in RFC7540#6.8. |
| 1425 | */ |
| 1426 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1427 | { |
| 1428 | int error; |
| 1429 | int last; |
| 1430 | |
| 1431 | if (h2c->dsi != 0) { |
| 1432 | error = H2_ERR_PROTOCOL_ERROR; |
| 1433 | goto conn_err; |
| 1434 | } |
| 1435 | |
| 1436 | if (h2c->dfl < 8) { |
| 1437 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1438 | goto conn_err; |
| 1439 | } |
| 1440 | |
| 1441 | /* process full frame only */ |
| 1442 | if (h2c->dbuf->i < h2c->dfl) |
| 1443 | return 0; |
| 1444 | |
| 1445 | last = h2_get_n32(h2c->dbuf, 0); |
| 1446 | h2c->errcode = h2_get_n32(h2c->dbuf, 4); |
| 1447 | h2_wake_some_streams(h2c, last, CS_FL_ERROR); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1448 | if (h2c->last_sid < 0) |
| 1449 | h2c->last_sid = last; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1450 | return 1; |
| 1451 | |
| 1452 | conn_err: |
| 1453 | h2c_error(h2c, error); |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1457 | /* processes a PRIORITY frame, and either skips it or rejects if it is |
| 1458 | * invalid. Returns > 0 on success or zero on missing data. It may return |
| 1459 | * an error in h2c. Described in RFC7540#6.3. |
| 1460 | */ |
| 1461 | static int h2c_handle_priority(struct h2c *h2c) |
| 1462 | { |
| 1463 | int error; |
| 1464 | |
| 1465 | if (h2c->dsi == 0) { |
| 1466 | error = H2_ERR_PROTOCOL_ERROR; |
| 1467 | goto conn_err; |
| 1468 | } |
| 1469 | |
| 1470 | if (h2c->dfl != 5) { |
| 1471 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1472 | goto conn_err; |
| 1473 | } |
| 1474 | |
| 1475 | /* process full frame only */ |
| 1476 | if (h2c->dbuf->i < h2c->dfl) |
| 1477 | return 0; |
| 1478 | |
| 1479 | if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) { |
| 1480 | /* 7540#5.3 : can't depend on itself */ |
| 1481 | error = H2_ERR_PROTOCOL_ERROR; |
| 1482 | goto conn_err; |
| 1483 | } |
| 1484 | return 1; |
| 1485 | |
| 1486 | conn_err: |
| 1487 | h2c_error(h2c, error); |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1491 | /* processes an RST_STREAM frame, and sets the 32-bit error code on the stream. |
| 1492 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1493 | * h2c. Described in RFC7540#6.4. |
| 1494 | */ |
| 1495 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1496 | { |
| 1497 | int error; |
| 1498 | |
| 1499 | if (h2c->dsi == 0) { |
| 1500 | error = H2_ERR_PROTOCOL_ERROR; |
| 1501 | goto conn_err; |
| 1502 | } |
| 1503 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1504 | if (h2c->dfl != 4) { |
| 1505 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1506 | goto conn_err; |
| 1507 | } |
| 1508 | |
| 1509 | /* process full frame only */ |
| 1510 | if (h2c->dbuf->i < h2c->dfl) |
| 1511 | return 0; |
| 1512 | |
| 1513 | /* late RST, already handled */ |
| 1514 | if (h2s->st == H2_SS_CLOSED) |
| 1515 | return 1; |
| 1516 | |
| 1517 | h2s->errcode = h2_get_n32(h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1518 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1519 | |
| 1520 | if (h2s->cs) { |
Willy Tarreau | 2153d3c | 2017-12-15 11:56:29 +0100 | [diff] [blame] | 1521 | h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1522 | /* recv is used to force to detect CS_FL_EOS that wake() |
| 1523 | * doesn't handle in the stream-int code. |
| 1524 | */ |
| 1525 | h2s->cs->data_cb->recv(h2s->cs); |
| 1526 | h2s->cs->data_cb->wake(h2s->cs); |
| 1527 | } |
| 1528 | |
| 1529 | h2s->flags |= H2_SF_RST_RCVD; |
| 1530 | return 1; |
| 1531 | |
| 1532 | conn_err: |
| 1533 | h2c_error(h2c, error); |
| 1534 | return 0; |
| 1535 | } |
| 1536 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1537 | /* processes a HEADERS frame. Returns > 0 on success or zero on missing data. |
| 1538 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 1539 | * errors here are reported as connection errors since it's impossible to |
| 1540 | * recover from such errors after the compression context has been altered. |
| 1541 | */ |
| 1542 | static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 1543 | { |
| 1544 | int error; |
| 1545 | |
| 1546 | if (!h2c->dfl) { |
| 1547 | error = H2_ERR_PROTOCOL_ERROR; // empty headers frame! |
| 1548 | goto strm_err; |
| 1549 | } |
| 1550 | |
| 1551 | if (!h2c->dbuf->size) |
| 1552 | return 0; // empty buffer |
| 1553 | |
| 1554 | if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size) |
| 1555 | return 0; // incomplete frame |
| 1556 | |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1557 | if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1558 | return 0; // too many cs still present |
| 1559 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1560 | /* now either the frame is complete or the buffer is complete */ |
| 1561 | if (h2s->st != H2_SS_IDLE) { |
| 1562 | /* FIXME: stream already exists, this is only allowed for |
| 1563 | * trailers (not supported for now). |
| 1564 | */ |
| 1565 | error = H2_ERR_PROTOCOL_ERROR; |
| 1566 | goto conn_err; |
| 1567 | } |
| 1568 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1569 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1570 | error = H2_ERR_PROTOCOL_ERROR; |
| 1571 | goto conn_err; |
| 1572 | } |
| 1573 | |
| 1574 | h2s = h2c_stream_new(h2c, h2c->dsi); |
| 1575 | if (!h2s) { |
| 1576 | error = H2_ERR_INTERNAL_ERROR; |
| 1577 | goto conn_err; |
| 1578 | } |
| 1579 | |
| 1580 | h2s->st = H2_SS_OPEN; |
| 1581 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 1582 | h2s->st = H2_SS_HREM; |
| 1583 | h2s->flags |= H2_SF_ES_RCVD; |
| 1584 | } |
| 1585 | |
| 1586 | /* call the upper layers to process the frame, then let the upper layer |
| 1587 | * notify the stream about any change. |
| 1588 | */ |
| 1589 | h2s->cs->data_cb->recv(h2s->cs); |
| 1590 | |
| 1591 | if (h2s->cs->data_cb->wake(h2s->cs) < 0) { |
| 1592 | /* FIXME: cs has already been destroyed, but we have to kill h2s. */ |
| 1593 | error = H2_ERR_INTERNAL_ERROR; |
| 1594 | goto conn_err; |
| 1595 | } |
| 1596 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1597 | if (h2c->st0 >= H2_CS_ERROR) |
| 1598 | return 0; |
| 1599 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1600 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1601 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1602 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1603 | } |
| 1604 | else { |
| 1605 | /* update the max stream ID if the request is being processed */ |
| 1606 | if (h2s->id > h2c->max_id) |
| 1607 | h2c->max_id = h2s->id; |
| 1608 | } |
| 1609 | |
| 1610 | return 1; |
| 1611 | |
| 1612 | conn_err: |
| 1613 | h2c_error(h2c, error); |
| 1614 | return 0; |
| 1615 | |
| 1616 | strm_err: |
| 1617 | if (h2s) { |
| 1618 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1619 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1620 | } |
| 1621 | else |
| 1622 | h2c_error(h2c, error); |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1626 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 1627 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 1628 | */ |
| 1629 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 1630 | { |
| 1631 | int error; |
| 1632 | |
| 1633 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 1634 | * to signal an end of stream (with the ES flag). |
| 1635 | */ |
| 1636 | |
| 1637 | if (!h2c->dbuf->size && h2c->dfl) |
| 1638 | return 0; // empty buffer |
| 1639 | |
| 1640 | if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size) |
| 1641 | return 0; // incomplete frame |
| 1642 | |
| 1643 | /* now either the frame is complete or the buffer is complete */ |
| 1644 | |
| 1645 | if (!h2c->dsi) { |
| 1646 | /* RFC7540#6.1 */ |
| 1647 | error = H2_ERR_PROTOCOL_ERROR; |
| 1648 | goto conn_err; |
| 1649 | } |
| 1650 | |
| 1651 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 1652 | /* RFC7540#6.1 */ |
| 1653 | error = H2_ERR_STREAM_CLOSED; |
| 1654 | goto strm_err; |
| 1655 | } |
| 1656 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1657 | /* call the upper layers to process the frame, then let the upper layer |
| 1658 | * notify the stream about any change. |
| 1659 | */ |
| 1660 | if (!h2s->cs) { |
| 1661 | error = H2_ERR_STREAM_CLOSED; |
| 1662 | goto strm_err; |
| 1663 | } |
| 1664 | |
| 1665 | h2s->cs->data_cb->recv(h2s->cs); |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1666 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1667 | if (h2s->cs->data_cb->wake(h2s->cs) < 0) { |
| 1668 | /* cs has just been destroyed, we have to kill h2s. */ |
| 1669 | error = H2_ERR_STREAM_CLOSED; |
| 1670 | goto strm_err; |
| 1671 | } |
| 1672 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1673 | if (h2c->st0 >= H2_CS_ERROR) |
| 1674 | return 0; |
| 1675 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1676 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1677 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1678 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | /* check for completion : the callee will change this to FRAME_A or |
| 1682 | * FRAME_H once done. |
| 1683 | */ |
| 1684 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1685 | return 0; |
| 1686 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 1687 | |
| 1688 | /* last frame */ |
| 1689 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 1690 | h2s->st = H2_SS_HREM; |
| 1691 | h2s->flags |= H2_SF_ES_RCVD; |
| 1692 | } |
| 1693 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1694 | return 1; |
| 1695 | |
| 1696 | conn_err: |
| 1697 | h2c_error(h2c, error); |
| 1698 | return 0; |
| 1699 | |
| 1700 | strm_err: |
| 1701 | if (h2s) { |
| 1702 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1703 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1704 | } |
| 1705 | else |
| 1706 | h2c_error(h2c, error); |
| 1707 | return 0; |
| 1708 | } |
| 1709 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 1710 | /* process Rx frames to be demultiplexed */ |
| 1711 | static void h2_process_demux(struct h2c *h2c) |
| 1712 | { |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 1713 | struct h2s *h2s; |
| 1714 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1715 | if (h2c->st0 >= H2_CS_ERROR) |
| 1716 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1717 | |
| 1718 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 1719 | if (h2c->st0 == H2_CS_PREFACE) { |
| 1720 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 1721 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1722 | if (h2c->st0 == H2_CS_ERROR) |
| 1723 | h2c->st0 = H2_CS_ERROR2; |
| 1724 | goto fail; |
| 1725 | } |
| 1726 | |
| 1727 | h2c->max_id = 0; |
| 1728 | h2c->st0 = H2_CS_SETTINGS1; |
| 1729 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 1730 | |
| 1731 | if (h2c->st0 == H2_CS_SETTINGS1) { |
| 1732 | struct h2_fh hdr; |
| 1733 | |
| 1734 | /* ensure that what is pending is a valid SETTINGS frame |
| 1735 | * without an ACK. |
| 1736 | */ |
| 1737 | if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) { |
| 1738 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1739 | if (h2c->st0 == H2_CS_ERROR) |
| 1740 | h2c->st0 = H2_CS_ERROR2; |
| 1741 | goto fail; |
| 1742 | } |
| 1743 | |
| 1744 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 1745 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1746 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1747 | h2c->st0 = H2_CS_ERROR2; |
| 1748 | goto fail; |
| 1749 | } |
| 1750 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 1751 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 1752 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1753 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1754 | h2c->st0 = H2_CS_ERROR2; |
| 1755 | goto fail; |
| 1756 | } |
| 1757 | |
| 1758 | /* that's OK, switch to FRAME_P to process it */ |
| 1759 | h2c->dfl = hdr.len; |
| 1760 | h2c->dsi = hdr.sid; |
| 1761 | h2c->dft = hdr.ft; |
| 1762 | h2c->dff = hdr.ff; |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 1763 | h2c->dpl = 0; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 1764 | h2c->st0 = H2_CS_FRAME_P; |
| 1765 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1766 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1767 | |
| 1768 | /* process as many incoming frames as possible below */ |
| 1769 | while (h2c->dbuf->i) { |
| 1770 | int ret = 0; |
| 1771 | |
| 1772 | if (h2c->st0 >= H2_CS_ERROR) |
| 1773 | break; |
| 1774 | |
| 1775 | if (h2c->st0 == H2_CS_FRAME_H) { |
| 1776 | struct h2_fh hdr; |
| 1777 | |
| 1778 | if (!h2_peek_frame_hdr(h2c->dbuf, &hdr)) |
| 1779 | break; |
| 1780 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 1781 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1782 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1783 | h2c->st0 = H2_CS_ERROR; |
| 1784 | break; |
| 1785 | } |
| 1786 | |
| 1787 | h2c->dfl = hdr.len; |
| 1788 | h2c->dsi = hdr.sid; |
| 1789 | h2c->dft = hdr.ft; |
| 1790 | h2c->dff = hdr.ff; |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 1791 | h2c->dpl = 0; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1792 | h2c->st0 = H2_CS_FRAME_P; |
| 1793 | h2_skip_frame_hdr(h2c->dbuf); |
| 1794 | } |
| 1795 | |
| 1796 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 1797 | h2s = h2c_st_by_id(h2c, h2c->dsi); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1798 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 1799 | if (h2c->st0 == H2_CS_FRAME_E) |
| 1800 | goto strm_err; |
| 1801 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 1802 | if (h2s->st == H2_SS_IDLE && |
| 1803 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 1804 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 1805 | * this state MUST be treated as a connection error |
| 1806 | */ |
| 1807 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1808 | h2c->st0 = H2_CS_ERROR; |
| 1809 | break; |
| 1810 | } |
| 1811 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 1812 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 1813 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 1814 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
| 1815 | * this state MUST be treated as a stream error |
| 1816 | */ |
| 1817 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1818 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 1819 | goto strm_err; |
| 1820 | } |
| 1821 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 1822 | /* Below the management of frames received in closed state is a |
| 1823 | * bit hackish because the spec makes strong differences between |
| 1824 | * streams closed by receiving RST, sending RST, and seeing ES |
| 1825 | * in both directions. In addition to this, the creation of a |
| 1826 | * new stream reusing the identifier of a closed one will be |
| 1827 | * detected here. Given that we cannot keep track of all closed |
| 1828 | * streams forever, we consider that unknown closed streams were |
| 1829 | * closed on RST received, which allows us to respond with an |
| 1830 | * RST without breaking the connection (eg: to abort a transfer). |
| 1831 | * Some frames have to be silently ignored as well. |
| 1832 | */ |
| 1833 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
| 1834 | if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) { |
| 1835 | /* #5.1.1: The identifier of a newly |
| 1836 | * established stream MUST be numerically |
| 1837 | * greater than all streams that the initiating |
| 1838 | * endpoint has opened or reserved. This |
| 1839 | * governs streams that are opened using a |
| 1840 | * HEADERS frame and streams that are reserved |
| 1841 | * using PUSH_PROMISE. An endpoint that |
| 1842 | * receives an unexpected stream identifier |
| 1843 | * MUST respond with a connection error. |
| 1844 | */ |
| 1845 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 1846 | goto strm_err; |
| 1847 | } |
| 1848 | |
| 1849 | if (h2s->flags & H2_SF_RST_RCVD) { |
| 1850 | /* RFC7540#5.1:closed: an endpoint that |
| 1851 | * receives any frame other than PRIORITY after |
| 1852 | * receiving a RST_STREAM MUST treat that as a |
| 1853 | * stream error of type STREAM_CLOSED. |
| 1854 | * |
| 1855 | * Note that old streams fall into this category |
| 1856 | * and will lead to an RST being sent. |
| 1857 | */ |
| 1858 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 1859 | h2c->st0 = H2_CS_FRAME_E; |
| 1860 | goto strm_err; |
| 1861 | } |
| 1862 | |
| 1863 | /* RFC7540#5.1:closed: if this state is reached as a |
| 1864 | * result of sending a RST_STREAM frame, the peer that |
| 1865 | * receives the RST_STREAM might have already sent |
| 1866 | * frames on the stream that cannot be withdrawn. An |
| 1867 | * endpoint MUST ignore frames that it receives on |
| 1868 | * closed streams after it has sent a RST_STREAM |
| 1869 | * frame. An endpoint MAY choose to limit the period |
| 1870 | * over which it ignores frames and treat frames that |
| 1871 | * arrive after this time as being in error. |
| 1872 | */ |
| 1873 | if (!(h2s->flags & H2_SF_RST_SENT)) { |
| 1874 | /* RFC7540#5.1:closed: any frame other than |
| 1875 | * PRIO/WU/RST in this state MUST be treated as |
| 1876 | * a connection error |
| 1877 | */ |
| 1878 | if (h2c->dft != H2_FT_RST_STREAM && |
| 1879 | h2c->dft != H2_FT_PRIORITY && |
| 1880 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 1881 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 1882 | goto strm_err; |
| 1883 | } |
| 1884 | } |
| 1885 | } |
| 1886 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 1887 | #if 0 |
| 1888 | // problem below: it is not possible to completely ignore such |
| 1889 | // streams as we need to maintain the compression state as well |
| 1890 | // and for this we need to completely process these frames (eg: |
| 1891 | // HEADERS frames) as well as counting DATA frames to emit |
| 1892 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 1893 | // This is a typical case of layer violation where the |
| 1894 | // transported contents are critical to the connection's |
| 1895 | // validity and must be ignored at the same time :-( |
| 1896 | |
| 1897 | /* graceful shutdown, ignore streams whose ID is higher than |
| 1898 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 1899 | */ |
| 1900 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
| 1901 | ret = MIN(h2c->dbuf->i, h2c->dfl); |
| 1902 | bi_del(h2c->dbuf, ret); |
| 1903 | h2c->dfl -= ret; |
| 1904 | ret = h2c->dfl == 0; |
| 1905 | goto strm_err; |
| 1906 | } |
| 1907 | #endif |
| 1908 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1909 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1910 | case H2_FT_SETTINGS: |
| 1911 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1912 | ret = h2c_handle_settings(h2c); |
| 1913 | |
| 1914 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1915 | ret = h2c_ack_settings(h2c); |
| 1916 | break; |
| 1917 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1918 | case H2_FT_PING: |
| 1919 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1920 | ret = h2c_handle_ping(h2c); |
| 1921 | |
| 1922 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1923 | ret = h2c_ack_ping(h2c); |
| 1924 | break; |
| 1925 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1926 | case H2_FT_WINDOW_UPDATE: |
| 1927 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1928 | ret = h2c_handle_window_update(h2c, h2s); |
| 1929 | break; |
| 1930 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 1931 | case H2_FT_CONTINUATION: |
| 1932 | /* we currently don't support CONTINUATION frames since |
| 1933 | * we have nowhere to store the partial HEADERS frame. |
| 1934 | * Let's abort the stream on an INTERNAL_ERROR here. |
| 1935 | */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1936 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 1937 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1938 | h2c->st0 = H2_CS_FRAME_E; |
| 1939 | } |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 1940 | break; |
| 1941 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1942 | case H2_FT_HEADERS: |
| 1943 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1944 | ret = h2c_frt_handle_headers(h2c, h2s); |
| 1945 | break; |
| 1946 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1947 | case H2_FT_DATA: |
| 1948 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1949 | ret = h2c_frt_handle_data(h2c, h2s); |
| 1950 | |
| 1951 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1952 | ret = h2c_send_strm_wu(h2c); |
| 1953 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1954 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1955 | case H2_FT_PRIORITY: |
| 1956 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1957 | ret = h2c_handle_priority(h2c); |
| 1958 | break; |
| 1959 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1960 | case H2_FT_RST_STREAM: |
| 1961 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1962 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 1963 | break; |
| 1964 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1965 | case H2_FT_GOAWAY: |
| 1966 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1967 | ret = h2c_handle_goaway(h2c); |
| 1968 | break; |
| 1969 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 1970 | case H2_FT_PUSH_PROMISE: |
| 1971 | /* not permitted here, RFC7540#5.1 */ |
| 1972 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 1973 | break; |
| 1974 | |
| 1975 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1976 | default: |
| 1977 | /* drop frames that we ignore. They may be larger than |
| 1978 | * the buffer so we drain all of their contents until |
| 1979 | * we reach the end. |
| 1980 | */ |
| 1981 | ret = MIN(h2c->dbuf->i, h2c->dfl); |
| 1982 | bi_del(h2c->dbuf, ret); |
| 1983 | h2c->dfl -= ret; |
| 1984 | ret = h2c->dfl == 0; |
| 1985 | } |
| 1986 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 1987 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1988 | /* We may have to send an RST if not done yet */ |
| 1989 | if (h2s->st == H2_SS_ERROR) |
| 1990 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1991 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1992 | if (h2c->st0 == H2_CS_FRAME_E) |
| 1993 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1994 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1995 | /* error or missing data condition met above ? */ |
| 1996 | if (ret <= 0) |
| 1997 | break; |
| 1998 | |
| 1999 | if (h2c->st0 != H2_CS_FRAME_H) { |
| 2000 | bi_del(h2c->dbuf, h2c->dfl); |
| 2001 | h2c->st0 = H2_CS_FRAME_H; |
| 2002 | } |
| 2003 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2004 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2005 | if (h2c->rcvd_c > 0 && |
| 2006 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2007 | h2c_send_conn_wu(h2c); |
| 2008 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2009 | fail: |
| 2010 | /* we can go here on missing data, blocked response or error */ |
| 2011 | return; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2015 | * the end. |
| 2016 | */ |
| 2017 | static int h2_process_mux(struct h2c *h2c) |
| 2018 | { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2019 | struct h2s *h2s, *h2s_back; |
| 2020 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2021 | /* start by sending possibly pending window updates */ |
| 2022 | if (h2c->rcvd_c > 0 && |
| 2023 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2024 | h2c_send_conn_wu(h2c) < 0) |
| 2025 | goto fail; |
| 2026 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2027 | /* First we always process the flow control list because the streams |
| 2028 | * waiting there were already elected for immediate emission but were |
| 2029 | * blocked just on this. |
| 2030 | */ |
| 2031 | |
| 2032 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
| 2033 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2034 | h2c->st0 >= H2_CS_ERROR) |
| 2035 | break; |
| 2036 | |
| 2037 | /* In theory it's possible that h2s->cs == NULL here : |
| 2038 | * - client sends crap that causes a parse error |
| 2039 | * - RST_STREAM is produced and CS_FL_ERROR at the same time |
| 2040 | * - RST_STREAM cannot be emitted because mux is busy/full |
| 2041 | * - stream gets notified, detaches and quits |
| 2042 | * - mux buffer gets ready and wakes pending streams up |
| 2043 | * - bam! |
| 2044 | */ |
| 2045 | h2s->flags &= ~H2_SF_BLK_ANY; |
| 2046 | |
| 2047 | if (h2s->cs) { |
| 2048 | h2s->cs->data_cb->send(h2s->cs); |
| 2049 | h2s->cs->data_cb->wake(h2s->cs); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2050 | } else { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 2051 | h2s_send_rst_stream(h2c, h2s); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | /* depending on callee's blocking reasons, we may queue in send |
| 2055 | * list or completely dequeue. |
| 2056 | */ |
| 2057 | if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) { |
| 2058 | if (h2s->flags & H2_SF_BLK_ANY) { |
| 2059 | LIST_DEL(&h2s->list); |
| 2060 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 2061 | } |
| 2062 | else { |
| 2063 | LIST_DEL(&h2s->list); |
| 2064 | LIST_INIT(&h2s->list); |
| 2065 | if (h2s->cs) |
| 2066 | h2s->cs->flags &= ~CS_FL_DATA_WR_ENA; |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2067 | else { |
| 2068 | /* just sent the last frame for this orphaned stream */ |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 2069 | h2s_destroy(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2070 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
| 2076 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2077 | break; |
| 2078 | |
| 2079 | /* In theory it's possible that h2s->cs == NULL here : |
| 2080 | * - client sends crap that causes a parse error |
| 2081 | * - RST_STREAM is produced and CS_FL_ERROR at the same time |
| 2082 | * - RST_STREAM cannot be emitted because mux is busy/full |
| 2083 | * - stream gets notified, detaches and quits |
| 2084 | * - mux buffer gets ready and wakes pending streams up |
| 2085 | * - bam! |
| 2086 | */ |
| 2087 | h2s->flags &= ~H2_SF_BLK_ANY; |
| 2088 | |
| 2089 | if (h2s->cs) { |
| 2090 | h2s->cs->data_cb->send(h2s->cs); |
| 2091 | h2s->cs->data_cb->wake(h2s->cs); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2092 | } else { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 2093 | h2s_send_rst_stream(h2c, h2s); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2094 | } |
| 2095 | /* depending on callee's blocking reasons, we may queue in fctl |
| 2096 | * list or completely dequeue. |
| 2097 | */ |
| 2098 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 2099 | /* stream hit the connection's flow control */ |
| 2100 | LIST_DEL(&h2s->list); |
| 2101 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 2102 | } |
| 2103 | else if (!(h2s->flags & H2_SF_BLK_ANY)) { |
| 2104 | LIST_DEL(&h2s->list); |
| 2105 | LIST_INIT(&h2s->list); |
| 2106 | if (h2s->cs) |
| 2107 | h2s->cs->flags &= ~CS_FL_DATA_WR_ENA; |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2108 | else { |
| 2109 | /* just sent the last frame for this orphaned stream */ |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 2110 | h2s_destroy(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2111 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2115 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2116 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2117 | if (h2c->st0 == H2_CS_ERROR) { |
| 2118 | if (h2c->max_id >= 0) { |
| 2119 | h2c_send_goaway_error(h2c, NULL); |
| 2120 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2121 | return 0; |
| 2122 | } |
| 2123 | |
| 2124 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2125 | } |
| 2126 | return 1; |
| 2127 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2128 | return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2129 | } |
| 2130 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 2131 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2132 | /*********************************************************/ |
| 2133 | /* functions below are I/O callbacks from the connection */ |
| 2134 | /*********************************************************/ |
| 2135 | |
| 2136 | /* callback called on recv event by the connection handler */ |
| 2137 | static void h2_recv(struct connection *conn) |
| 2138 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2139 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2140 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2141 | int max; |
| 2142 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2143 | if (!h2_recv_allowed(h2c)) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2144 | return; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2145 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2146 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2147 | if (!buf) { |
| 2148 | h2c->flags |= H2_CF_DEM_DALLOC; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2149 | return; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2150 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2151 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2152 | /* note: buf->o == 0 */ |
| 2153 | max = buf->size - buf->i; |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2154 | if (max) |
| 2155 | conn->xprt->rcv_buf(conn, buf, max); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2156 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2157 | if (!buf->i) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2158 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2159 | return; |
| 2160 | } |
| 2161 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2162 | if (buf->i == buf->size) |
| 2163 | h2c->flags |= H2_CF_DEM_DFULL; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2164 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | /* callback called on send event by the connection handler */ |
| 2168 | static void h2_send(struct connection *conn) |
| 2169 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2170 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2171 | int done; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2172 | |
| 2173 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2174 | return; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2175 | |
| 2176 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2177 | /* a handshake was requested */ |
| 2178 | return; |
| 2179 | } |
| 2180 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2181 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2182 | * pending streams into the existing buffer until it's reportedly full |
| 2183 | * or the end of send requests is reached. Then it tries to send this |
| 2184 | * buffer's contents out, marks it not full if at least one byte could |
| 2185 | * be sent, and tries again. |
| 2186 | * |
| 2187 | * The snd_buf() function normally takes a "flags" argument which may |
| 2188 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2189 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2190 | * connection is streaming lots of data (used to increase TLS record |
| 2191 | * size at the expense of latency). The former can be sent any time |
| 2192 | * there's a buffer full flag, as it indicates at least one stream |
| 2193 | * attempted to send and failed so there are pending data. An |
| 2194 | * alternative would be to set it as long as there's an active stream |
| 2195 | * but that would be problematic for ACKs until we have an absolute |
| 2196 | * guarantee that all waiters have at least one byte to send. The |
| 2197 | * latter should possibly not be set for now. |
| 2198 | */ |
| 2199 | |
| 2200 | done = 0; |
| 2201 | while (!done) { |
| 2202 | unsigned int flags = 0; |
| 2203 | |
| 2204 | /* fill as much as we can into the current buffer */ |
| 2205 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2206 | done = h2_process_mux(h2c); |
| 2207 | |
| 2208 | if (conn->flags & CO_FL_ERROR) |
| 2209 | break; |
| 2210 | |
| 2211 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2212 | flags |= CO_SFL_MSG_MORE; |
| 2213 | |
Willy Tarreau | 319994a | 2017-11-07 11:03:56 +0100 | [diff] [blame] | 2214 | if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0) |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2215 | break; |
| 2216 | |
| 2217 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2218 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2219 | } |
| 2220 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2221 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2222 | /* output closed, nothing to send, clear the buffer to release it */ |
| 2223 | h2c->mbuf->o = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2224 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2225 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2226 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2227 | /* callback called on any event by the connection handler. |
| 2228 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2229 | * destruction of the connection (which normally doesn not happen in h2). |
| 2230 | */ |
| 2231 | static int h2_wake(struct connection *conn) |
| 2232 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2233 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2234 | struct session *sess = conn->owner; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2235 | |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2236 | if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
| 2237 | h2_process_demux(h2c); |
| 2238 | |
| 2239 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
| 2240 | h2c->dbuf->i = 0; |
| 2241 | |
| 2242 | if (h2c->dbuf->i != h2c->dbuf->size) |
| 2243 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2244 | } |
| 2245 | |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2246 | if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) { |
| 2247 | /* frontend is stopping, reload likely in progress, let's try |
| 2248 | * to announce a graceful shutdown if not yet done. We don't |
| 2249 | * care if it fails, it will be tried again later. |
| 2250 | */ |
| 2251 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2252 | if (h2c->last_sid < 0) |
| 2253 | h2c->last_sid = (1U << 31) - 1; |
| 2254 | h2c_send_goaway_error(h2c, NULL); |
| 2255 | } |
| 2256 | } |
| 2257 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2258 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2259 | * If we received early data, and the handshake is done, wake |
| 2260 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2261 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2262 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2263 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2264 | struct eb32_node *node; |
| 2265 | struct h2s *h2s; |
| 2266 | |
| 2267 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2268 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2269 | |
| 2270 | while (node) { |
| 2271 | h2s = container_of(node, struct h2s, by_id); |
| 2272 | if (h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
| 2273 | h2s->cs->data_cb->wake(h2s->cs); |
| 2274 | node = eb32_next(node); |
| 2275 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2276 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2277 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2278 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2279 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2280 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2281 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2282 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2283 | |
| 2284 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2285 | /* no more stream, kill the connection now */ |
| 2286 | h2_release(conn); |
| 2287 | return -1; |
| 2288 | } |
| 2289 | else { |
| 2290 | /* some streams still there, we need to signal them all and |
| 2291 | * wait for their departure. |
| 2292 | */ |
| 2293 | __conn_xprt_stop_recv(conn); |
| 2294 | __conn_xprt_stop_send(conn); |
| 2295 | return 0; |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | if (!h2c->dbuf->i) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2300 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2301 | |
| 2302 | /* stop being notified of incoming data if we can't process them */ |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2303 | if (!h2_recv_allowed(h2c)) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2304 | __conn_xprt_stop_recv(conn); |
| 2305 | } |
| 2306 | else { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2307 | __conn_xprt_want_recv(conn); |
| 2308 | } |
| 2309 | |
| 2310 | /* adjust output polling */ |
Willy Tarreau | 5160683 | 2017-10-17 15:30:07 +0200 | [diff] [blame] | 2311 | if (!(conn->flags & CO_FL_SOCK_WR_SH) && |
| 2312 | (h2c->st0 == H2_CS_ERROR || |
| 2313 | h2c->mbuf->o || |
| 2314 | (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) || |
| 2315 | (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2316 | __conn_xprt_want_send(conn); |
| 2317 | } |
| 2318 | else { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2319 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2320 | __conn_xprt_stop_send(conn); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2321 | } |
| 2322 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2323 | if (h2c->task) { |
Willy Tarreau | 84b118f | 2018-03-05 16:10:54 +0100 | [diff] [blame] | 2324 | if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2325 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2326 | task_queue(h2c->task); |
| 2327 | } |
| 2328 | else |
| 2329 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2330 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2331 | return 0; |
| 2332 | } |
| 2333 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2334 | /* Connection timeout management. The principle is that if there's no receipt |
| 2335 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2336 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2337 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2338 | * GOAWAY frame. |
| 2339 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2340 | static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2341 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2342 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2343 | int expired = tick_is_expired(t->expire, now_ms); |
| 2344 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2345 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2346 | return t; |
| 2347 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2348 | task_delete(t); |
| 2349 | task_free(t); |
| 2350 | |
| 2351 | if (!h2c) { |
| 2352 | /* resources were already deleted */ |
| 2353 | return NULL; |
| 2354 | } |
| 2355 | |
| 2356 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2357 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2358 | h2_wake_some_streams(h2c, 0, 0); |
| 2359 | |
| 2360 | if (h2c->mbuf->o) { |
| 2361 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2362 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2363 | } |
| 2364 | |
| 2365 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2366 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2367 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2368 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2369 | |
| 2370 | if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) |
| 2371 | h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0); |
| 2372 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2373 | /* either we can release everything now or it will be done later once |
| 2374 | * the last stream closes. |
| 2375 | */ |
| 2376 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2377 | h2_release(h2c->conn); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2378 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2379 | return NULL; |
| 2380 | } |
| 2381 | |
| 2382 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2383 | /*******************************************/ |
| 2384 | /* functions below are used by the streams */ |
| 2385 | /*******************************************/ |
| 2386 | |
| 2387 | /* |
| 2388 | * Attach a new stream to a connection |
| 2389 | * (Used for outgoing connections) |
| 2390 | */ |
| 2391 | static struct conn_stream *h2_attach(struct connection *conn) |
| 2392 | { |
| 2393 | return NULL; |
| 2394 | } |
| 2395 | |
| 2396 | /* callback used to update the mux's polling flags after changing a cs' status. |
| 2397 | * The caller (cs_update_mux_polling) will take care of propagating any changes |
| 2398 | * to the transport layer. |
| 2399 | */ |
| 2400 | static void h2_update_poll(struct conn_stream *cs) |
| 2401 | { |
Willy Tarreau | 1d39322 | 2017-10-17 10:26:19 +0200 | [diff] [blame] | 2402 | struct h2s *h2s = cs->ctx; |
| 2403 | |
| 2404 | if (!h2s) |
| 2405 | return; |
| 2406 | |
Willy Tarreau | d7739c8 | 2017-10-30 15:38:23 +0100 | [diff] [blame] | 2407 | /* we may unblock a blocked read */ |
| 2408 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2409 | if (cs->flags & CS_FL_DATA_RD_ENA) { |
| 2410 | /* the stream indicates it's willing to read */ |
Willy Tarreau | d7739c8 | 2017-10-30 15:38:23 +0100 | [diff] [blame] | 2411 | h2s->h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2412 | if (h2s->h2c->dsi == h2s->id) { |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2413 | conn_xprt_want_recv(cs->conn); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2414 | conn_xprt_want_send(cs->conn); |
| 2415 | } |
Willy Tarreau | d7739c8 | 2017-10-30 15:38:23 +0100 | [diff] [blame] | 2416 | } |
| 2417 | |
Willy Tarreau | 1d39322 | 2017-10-17 10:26:19 +0200 | [diff] [blame] | 2418 | /* Note: the stream and stream-int code doesn't allow us to perform a |
| 2419 | * synchronous send() here unfortunately, because this code is called |
| 2420 | * as si_update() from the process_stream() context. This means that |
| 2421 | * we have to queue the current cs and defer its processing after the |
| 2422 | * connection's cs list is processed anyway. |
| 2423 | */ |
| 2424 | |
| 2425 | if (cs->flags & CS_FL_DATA_WR_ENA) { |
| 2426 | if (LIST_ISEMPTY(&h2s->list)) { |
| 2427 | if (LIST_ISEMPTY(&h2s->h2c->send_list) && |
| 2428 | !h2s->h2c->mbuf->o && // not yet subscribed |
| 2429 | !(cs->conn->flags & CO_FL_SOCK_WR_SH)) |
| 2430 | conn_xprt_want_send(cs->conn); |
| 2431 | LIST_ADDQ(&h2s->h2c->send_list, &h2s->list); |
| 2432 | } |
| 2433 | } |
| 2434 | else if (!LIST_ISEMPTY(&h2s->list)) { |
| 2435 | LIST_DEL(&h2s->list); |
| 2436 | LIST_INIT(&h2s->list); |
| 2437 | h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL); |
| 2438 | } |
| 2439 | |
| 2440 | /* this can happen from within si_chk_snd() */ |
| 2441 | if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
| 2442 | conn_xprt_want_send(cs->conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | /* |
| 2446 | * Detach the stream from the connection and possibly release the connection. |
| 2447 | */ |
| 2448 | static void h2_detach(struct conn_stream *cs) |
| 2449 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2450 | struct h2s *h2s = cs->ctx; |
| 2451 | struct h2c *h2c; |
| 2452 | |
| 2453 | cs->ctx = NULL; |
| 2454 | if (!h2s) |
| 2455 | return; |
| 2456 | |
| 2457 | h2c = h2s->h2c; |
| 2458 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 2459 | h2c->nb_cs--; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 2460 | if (h2c->flags & H2_CF_DEM_TOOMANY && |
| 2461 | !h2_has_too_many_cs(h2c)) { |
| 2462 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
| 2463 | if (h2_recv_allowed(h2c)) { |
| 2464 | __conn_xprt_want_recv(h2c->conn); |
| 2465 | conn_xprt_want_send(h2c->conn); |
| 2466 | } |
| 2467 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2468 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2469 | /* this stream may be blocked waiting for some data to leave (possibly |
| 2470 | * an ES or RST frame), so orphan it in this case. |
| 2471 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 2472 | if (!(cs->conn->flags & CO_FL_ERROR) && |
| 2473 | (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2474 | return; |
| 2475 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 2476 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 2477 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 2478 | /* unblock the connection if it was blocked on this |
| 2479 | * stream. |
| 2480 | */ |
| 2481 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 2482 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
| 2483 | conn_xprt_want_recv(cs->conn); |
| 2484 | conn_xprt_want_send(cs->conn); |
| 2485 | } |
| 2486 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 2487 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2488 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2489 | /* We don't want to close right now unless we're removing the |
| 2490 | * last stream, and either the connection is in error, or it |
| 2491 | * reached the ID already specified in a GOAWAY frame received |
| 2492 | * or sent (as seen by last_sid >= 0). |
| 2493 | */ |
| 2494 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 2495 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
Willy Tarreau | 42d55b9 | 2018-06-13 14:24:56 +0200 | [diff] [blame] | 2496 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2497 | (h2c->flags & H2_CF_GOAWAY_FAILED) || |
| 2498 | (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */ |
| 2499 | (conn_xprt_read0_pending(h2c->conn) || |
| 2500 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) { |
| 2501 | /* no more stream will come, kill it now */ |
| 2502 | h2_release(h2c->conn); |
| 2503 | } |
| 2504 | else if (h2c->task) { |
| 2505 | if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) { |
| 2506 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 2507 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 2508 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2509 | else |
| 2510 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2511 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 2515 | { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2516 | struct h2s *h2s = cs->ctx; |
| 2517 | |
| 2518 | if (!mode) |
| 2519 | return; |
| 2520 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2521 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2522 | return; |
| 2523 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2524 | /* if no outgoing data was seen on this stream, it means it was |
| 2525 | * closed with a "tcp-request content" rule that is normally |
| 2526 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 2527 | * case we send a goaway to close the connection. |
| 2528 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2529 | if (!(h2s->flags & H2_SF_RST_SENT) && |
| 2530 | h2s_send_rst_stream(h2s->h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2531 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2532 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2533 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 2534 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
| 2535 | h2c_send_goaway_error(h2s->h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2536 | goto add_to_list; |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2537 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2538 | if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
| 2539 | conn_xprt_want_send(cs->conn); |
| 2540 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2541 | h2s_close(h2s); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2542 | |
| 2543 | add_to_list: |
| 2544 | if (LIST_ISEMPTY(&h2s->list)) { |
| 2545 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 2546 | LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list); |
| 2547 | else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) |
| 2548 | LIST_ADDQ(&h2s->h2c->send_list, &h2s->list); |
| 2549 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 2553 | { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2554 | struct h2s *h2s = cs->ctx; |
| 2555 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2556 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2557 | return; |
| 2558 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 2559 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2560 | /* we can cleanly close using an empty data frame only after headers */ |
| 2561 | |
| 2562 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 2563 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2564 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2565 | |
| 2566 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2567 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2568 | else |
| 2569 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2570 | } else { |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2571 | /* if no outgoing data was seen on this stream, it means it was |
| 2572 | * closed with a "tcp-request content" rule that is normally |
| 2573 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 2574 | * case we send a goaway to close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 2575 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2576 | if (!(h2s->flags & H2_SF_RST_SENT) && |
| 2577 | h2s_send_rst_stream(h2s->h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2578 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2579 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2580 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 2581 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 2582 | h2c_send_goaway_error(h2s->h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2583 | goto add_to_list; |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 2584 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2585 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
| 2589 | conn_xprt_want_send(cs->conn); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2590 | |
| 2591 | add_to_list: |
| 2592 | if (LIST_ISEMPTY(&h2s->list)) { |
| 2593 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 2594 | LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list); |
| 2595 | else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) |
| 2596 | LIST_ADDQ(&h2s->h2c->send_list, &h2s->list); |
| 2597 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2598 | } |
| 2599 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2600 | /* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 |
| 2601 | * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't |
| 2602 | * proceed. Stream errors are reported in h2s->errcode and connection errors |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 2603 | * in h2c->errcode. |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2604 | */ |
| 2605 | static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count) |
| 2606 | { |
| 2607 | struct h2c *h2c = h2s->h2c; |
| 2608 | const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2609 | struct chunk *tmp = get_trash_chunk(); |
| 2610 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2611 | struct chunk *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 2612 | unsigned int msgf; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2613 | int flen = h2c->dfl; |
| 2614 | int outlen = 0; |
| 2615 | int wrap; |
| 2616 | int try; |
| 2617 | |
| 2618 | if (!h2c->dfl) { |
| 2619 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame! |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2620 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2621 | return 0; |
| 2622 | } |
| 2623 | |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 2624 | if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size) |
| 2625 | return 0; // incomplete input frame |
| 2626 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2627 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
| 2628 | wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p; |
| 2629 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2630 | copy = alloc_trash_chunk(); |
| 2631 | if (!copy) { |
| 2632 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 2633 | goto fail; |
| 2634 | } |
| 2635 | memcpy(copy->str, h2c->dbuf->p, wrap); |
| 2636 | memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap); |
| 2637 | hdrs = (uint8_t *)copy->str; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | /* The padlen is the first byte before data, and the padding appears |
| 2641 | * after data. padlen+data+padding are included in flen. |
| 2642 | */ |
| 2643 | if (h2c->dff & H2_F_HEADERS_PADDED) { |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2644 | h2c->dpl = *hdrs; |
| 2645 | if (h2c->dpl >= flen) { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2646 | /* RFC7540#6.2 : pad length = length of frame payload or greater */ |
| 2647 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2648 | return 0; |
| 2649 | } |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2650 | flen -= h2c->dpl + 1; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2651 | hdrs += 1; // skip Pad Length |
| 2652 | } |
| 2653 | |
| 2654 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 2655 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 2656 | if (read_n32(hdrs) == h2s->id) { |
| 2657 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 2658 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2659 | return 0;//goto fail_stream; |
| 2660 | } |
| 2661 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2662 | hdrs += 5; // stream dep = 4, weight = 1 |
| 2663 | flen -= 5; |
| 2664 | } |
| 2665 | |
| 2666 | /* FIXME: lack of END_HEADERS means there's a continuation frame, we |
| 2667 | * don't support this for now and can't even decompress so we have to |
| 2668 | * break the connection. |
| 2669 | */ |
| 2670 | if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) { |
| 2671 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2672 | goto fail; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2673 | } |
| 2674 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2675 | /* we can't retry a failed decompression operation so we must be very |
| 2676 | * careful not to take any risks. In practice the output buffer is |
| 2677 | * always empty except maybe for trailers, so these operations almost |
| 2678 | * never happen. |
| 2679 | */ |
| 2680 | if (unlikely(buf->o)) { |
| 2681 | /* need to let the output buffer flush and |
| 2682 | * mark the buffer for later wake up. |
| 2683 | */ |
| 2684 | goto fail; |
| 2685 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2686 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2687 | if (unlikely(buffer_space_wraps(buf))) { |
| 2688 | /* it doesn't fit and the buffer is fragmented, |
| 2689 | * so let's defragment it and try again. |
| 2690 | */ |
| 2691 | buffer_slow_realign(buf); |
| 2692 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2693 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2694 | /* first check if we have some room after p+i */ |
| 2695 | try = buf->data + buf->size - (buf->p + buf->i); |
| 2696 | |
| 2697 | /* otherwise continue between data and p-o */ |
| 2698 | if (try <= 0) { |
| 2699 | try = buf->p - (buf->data + buf->o); |
| 2700 | if (try <= 0) |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2701 | goto fail; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2702 | } |
| 2703 | if (try > count) |
| 2704 | try = count; |
| 2705 | |
| 2706 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 2707 | sizeof(list)/sizeof(list[0]), tmp); |
| 2708 | if (outlen < 0) { |
| 2709 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 2710 | goto fail; |
| 2711 | } |
| 2712 | |
| 2713 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 2714 | msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY; |
| 2715 | outlen = h2_make_h1_request(list, bi_end(buf), try, &msgf); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2716 | |
| 2717 | if (outlen < 0) { |
| 2718 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 2719 | goto fail; |
| 2720 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2721 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 2722 | if (msgf & H2_MSGF_BODY) { |
| 2723 | /* a payload is present */ |
| 2724 | if (msgf & H2_MSGF_BODY_CL) |
| 2725 | h2s->flags |= H2_SF_DATA_CLEN; |
| 2726 | else if (!(msgf & H2_MSGF_BODY_TUNNEL)) |
| 2727 | h2s->flags |= H2_SF_DATA_CHNK; |
| 2728 | } |
| 2729 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2730 | /* now consume the input data */ |
| 2731 | bi_del(h2c->dbuf, h2c->dfl); |
| 2732 | h2c->st0 = H2_CS_FRAME_H; |
| 2733 | buf->i += outlen; |
| 2734 | |
| 2735 | /* don't send it before returning data! |
| 2736 | * FIXME: should we instead try to send it much later, after the |
| 2737 | * response ? This would require that we keep a copy of it in h2s. |
| 2738 | */ |
| 2739 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 2740 | h2s->cs->flags |= CS_FL_EOS; |
| 2741 | h2s->flags |= H2_SF_ES_RCVD; |
| 2742 | } |
| 2743 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2744 | leave: |
| 2745 | free_trash_chunk(copy); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2746 | return outlen; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2747 | fail: |
| 2748 | outlen = 0; |
| 2749 | goto leave; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2750 | } |
| 2751 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2752 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 2753 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 2754 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 2755 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 2756 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 2757 | * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame |
| 2758 | * parser state is automatically updated. Returns the number of bytes emitted |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2759 | * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be |
| 2760 | * checked to know if some data remain pending (an empty DATA frame can return |
| 2761 | * 0 as a valid result). Stream errors are reported in h2s->errcode and |
| 2762 | * connection errors in h2c->errcode. The caller must already have checked the |
| 2763 | * frame header and ensured that the frame was complete or the buffer full. It |
| 2764 | * changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2765 | */ |
| 2766 | static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count) |
| 2767 | { |
| 2768 | struct h2c *h2c = h2s->h2c; |
| 2769 | int block1, block2; |
| 2770 | unsigned int flen = h2c->dfl; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2771 | unsigned int chklen = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2772 | |
Willy Tarreau | c9ede6c | 2017-12-10 21:28:43 +0100 | [diff] [blame] | 2773 | h2s->cs->flags &= ~CS_FL_RCV_MORE; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2774 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2775 | |
| 2776 | /* The padlen is the first byte before data, and the padding appears |
| 2777 | * after data. padlen+data+padding are included in flen. |
| 2778 | */ |
Willy Tarreau | 7912781 | 2017-12-03 21:06:59 +0100 | [diff] [blame] | 2779 | if (h2c->dff & H2_F_DATA_PADDED) { |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2780 | if (h2c->dbuf->i < 1) |
| 2781 | return 0; |
| 2782 | |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2783 | h2c->dpl = *(uint8_t *)bi_ptr(h2c->dbuf); |
| 2784 | if (h2c->dpl >= h2c->dfl) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2785 | /* RFC7540#6.1 : pad length = length of frame payload or greater */ |
| 2786 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2787 | return 0; |
| 2788 | } |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2789 | |
| 2790 | /* skip the padlen byte */ |
| 2791 | bi_del(h2c->dbuf, 1); |
| 2792 | h2c->dfl--; |
| 2793 | h2c->rcvd_c++; h2c->rcvd_s++; |
| 2794 | h2c->dff &= ~H2_F_DATA_PADDED; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2795 | } |
| 2796 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2797 | flen = h2c->dfl - h2c->dpl; |
| 2798 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 2799 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2800 | |
| 2801 | if (flen > h2c->dbuf->i) { |
| 2802 | flen = h2c->dbuf->i; |
| 2803 | if (!flen) |
| 2804 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2805 | } |
| 2806 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2807 | /* chunked-encoding requires more room */ |
| 2808 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 2809 | chklen = MIN(flen, count); |
| 2810 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 2811 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 2812 | (chklen < 1048576) ? 4 : 8; |
| 2813 | chklen += 4; // CRLF, CRLF |
| 2814 | } |
| 2815 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2816 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2817 | if (flen + chklen > count) { |
| 2818 | if (chklen >= count) |
| 2819 | goto full; |
| 2820 | flen = count - chklen; |
| 2821 | } |
| 2822 | |
| 2823 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 2824 | /* emit the chunk size */ |
| 2825 | unsigned int chksz = flen; |
| 2826 | char str[10]; |
| 2827 | char *beg; |
| 2828 | |
| 2829 | beg = str + sizeof(str); |
| 2830 | *--beg = '\n'; |
| 2831 | *--beg = '\r'; |
| 2832 | do { |
| 2833 | *--beg = hextab[chksz & 0xF]; |
| 2834 | } while (chksz >>= 4); |
| 2835 | bi_putblk(buf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2836 | } |
| 2837 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2838 | /* Block1 is the length of the first block before the buffer wraps, |
| 2839 | * block2 is the optional second block to reach the end of the frame. |
| 2840 | */ |
| 2841 | block1 = bi_contig_data(h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2842 | if (block1 > flen) |
| 2843 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2844 | block2 = flen - block1; |
| 2845 | |
| 2846 | if (block1) |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2847 | bi_putblk(buf, b_ptr(h2c->dbuf, 0), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2848 | |
| 2849 | if (block2) |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2850 | bi_putblk(buf, b_ptr(h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2851 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2852 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 2853 | /* emit the CRLF */ |
| 2854 | bi_putblk(buf, "\r\n", 2); |
| 2855 | } |
| 2856 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2857 | /* now mark the input data as consumed (will be deleted from the buffer |
| 2858 | * by the caller when seeing FRAME_A after sending the window update). |
| 2859 | */ |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2860 | bi_del(h2c->dbuf, flen); |
| 2861 | h2c->dfl -= flen; |
| 2862 | h2c->rcvd_c += flen; |
| 2863 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 2864 | |
| 2865 | if (h2c->dfl > h2c->dpl) { |
| 2866 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2867 | goto more; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2868 | } |
| 2869 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 2870 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2871 | /* here we're done with the frame, all the payload (except padding) was |
| 2872 | * transferred. |
| 2873 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2874 | |
| 2875 | if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) { |
| 2876 | /* emit the trailing 0 CRLF CRLF */ |
| 2877 | if (count < 5) |
| 2878 | goto more; |
| 2879 | chklen += 5; |
| 2880 | bi_putblk(buf, "0\r\n\r\n", 5); |
| 2881 | } |
| 2882 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 2883 | h2c->rcvd_c += h2c->dpl; |
| 2884 | h2c->rcvd_s += h2c->dpl; |
| 2885 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2886 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
| 2887 | |
| 2888 | /* don't send it before returning data! |
| 2889 | * FIXME: should we instead try to send it much later, after the |
| 2890 | * response ? This would require that we keep a copy of it in h2s. |
| 2891 | */ |
Willy Tarreau | 7912781 | 2017-12-03 21:06:59 +0100 | [diff] [blame] | 2892 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2893 | h2s->cs->flags |= CS_FL_EOS; |
| 2894 | h2s->flags |= H2_SF_ES_RCVD; |
| 2895 | } |
| 2896 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2897 | return flen + chklen; |
| 2898 | full: |
| 2899 | flen = chklen = 0; |
| 2900 | more: |
| 2901 | h2c->flags |= H2_CF_DEM_SFULL; |
| 2902 | h2s->cs->flags |= CS_FL_RCV_MORE; |
| 2903 | return flen + chklen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2904 | } |
| 2905 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2906 | /* |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2907 | * Called from the upper layer to get more data, up to <count> bytes. The |
| 2908 | * caller is responsible for never asking for more data than what is available |
| 2909 | * in the buffer. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2910 | */ |
| 2911 | static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count) |
| 2912 | { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2913 | struct h2s *h2s = cs->ctx; |
| 2914 | struct h2c *h2c = h2s->h2c; |
| 2915 | int ret = 0; |
| 2916 | |
| 2917 | if (h2c->st0 != H2_CS_FRAME_P) |
| 2918 | return 0; // no pre-parsed frame yet |
| 2919 | |
| 2920 | if (h2c->dsi != h2s->id) |
| 2921 | return 0; // not for us |
| 2922 | |
| 2923 | if (!h2c->dbuf->size) |
| 2924 | return 0; // empty buffer |
| 2925 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2926 | switch (h2c->dft) { |
| 2927 | case H2_FT_HEADERS: |
| 2928 | ret = h2_frt_decode_headers(h2s, buf, count); |
| 2929 | break; |
| 2930 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2931 | case H2_FT_DATA: |
| 2932 | ret = h2_frt_transfer_data(h2s, buf, count); |
| 2933 | break; |
| 2934 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2935 | default: |
| 2936 | ret = 0; |
| 2937 | } |
| 2938 | return ret; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2939 | } |
| 2940 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2941 | /* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf> |
| 2942 | * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of |
| 2943 | * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds |
| 2944 | * to the number of buffer bytes consumed. |
| 2945 | */ |
| 2946 | static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf) |
| 2947 | { |
| 2948 | struct http_hdr list[MAX_HTTP_HDR]; |
| 2949 | struct h2c *h2c = h2s->h2c; |
| 2950 | struct h1m *h1m = &h2s->res; |
| 2951 | struct chunk outbuf; |
| 2952 | int es_now = 0; |
| 2953 | int ret = 0; |
| 2954 | int hdr; |
| 2955 | |
| 2956 | if (h2c_mux_busy(h2c, h2s)) { |
| 2957 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 2958 | return 0; |
| 2959 | } |
| 2960 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2961 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2962 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 2963 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2964 | return 0; |
| 2965 | } |
| 2966 | |
| 2967 | /* First, try to parse the H1 response and index it into <list>. |
| 2968 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 2969 | * block does not wrap and we can safely read it this way without |
| 2970 | * having to realign the buffer. |
| 2971 | */ |
| 2972 | ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o, |
| 2973 | list, sizeof(list)/sizeof(list[0]), h1m); |
| 2974 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 2975 | /* incomplete or invalid response, this is abnormal coming from |
| 2976 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 2977 | * so that won't be fixed, raise an error now. |
| 2978 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2979 | * FIXME: we should instead add the ability to only return a |
| 2980 | * 502 bad gateway. But in theory this is not supposed to |
| 2981 | * happen. |
| 2982 | */ |
| 2983 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2984 | ret = 0; |
| 2985 | goto end; |
| 2986 | } |
| 2987 | |
| 2988 | chunk_reset(&outbuf); |
| 2989 | |
| 2990 | while (1) { |
| 2991 | outbuf.str = bo_end(h2c->mbuf); |
| 2992 | outbuf.size = bo_contig_space(h2c->mbuf); |
| 2993 | outbuf.len = 0; |
| 2994 | |
| 2995 | if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf)) |
| 2996 | break; |
| 2997 | realign_again: |
| 2998 | buffer_slow_realign(h2c->mbuf); |
| 2999 | } |
| 3000 | |
| 3001 | if (outbuf.size < 9) { |
| 3002 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3003 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3004 | ret = 0; |
| 3005 | goto end; |
| 3006 | } |
| 3007 | |
| 3008 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 3009 | memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5); |
| 3010 | write_n32(outbuf.str + 5, h2s->id); // 4 bytes |
| 3011 | outbuf.len = 9; |
| 3012 | |
| 3013 | /* encode status, which necessarily is the first one */ |
| 3014 | if (outbuf.len < outbuf.size && h1m->status == 200) |
| 3015 | outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200") |
| 3016 | else if (outbuf.len < outbuf.size && h1m->status == 304) |
| 3017 | outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304") |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3018 | else if (unlikely(list[0].v.len != 3)) { |
| 3019 | /* this is an unparsable response */ |
| 3020 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3021 | ret = 0; |
| 3022 | goto end; |
| 3023 | } |
| 3024 | else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3025 | /* basic encoding of the status code */ |
| 3026 | outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8) |
| 3027 | outbuf.str[outbuf.len++] = 0x03; // 3 bytes status |
| 3028 | outbuf.str[outbuf.len++] = list[0].v.ptr[0]; |
| 3029 | outbuf.str[outbuf.len++] = list[0].v.ptr[1]; |
| 3030 | outbuf.str[outbuf.len++] = list[0].v.ptr[2]; |
| 3031 | } |
| 3032 | else { |
| 3033 | if (buffer_space_wraps(h2c->mbuf)) |
| 3034 | goto realign_again; |
| 3035 | |
| 3036 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3037 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3038 | ret = 0; |
| 3039 | goto end; |
| 3040 | } |
| 3041 | |
| 3042 | /* encode all headers, stop at empty name */ |
| 3043 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3044 | /* these ones do not exist in H2 and must be dropped. */ |
| 3045 | if (isteq(list[hdr].n, ist("connection")) || |
| 3046 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3047 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3048 | isteq(list[hdr].n, ist("upgrade")) || |
| 3049 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3050 | continue; |
| 3051 | |
| 3052 | if (isteq(list[hdr].n, ist(""))) |
| 3053 | break; // end |
| 3054 | |
| 3055 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3056 | /* output full */ |
| 3057 | if (buffer_space_wraps(h2c->mbuf)) |
| 3058 | goto realign_again; |
| 3059 | |
| 3060 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3061 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3062 | ret = 0; |
| 3063 | goto end; |
| 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | /* we may need to add END_STREAM */ |
| 3068 | if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW) |
| 3069 | es_now = 1; |
| 3070 | |
| 3071 | /* update the frame's size */ |
| 3072 | h2_set_frame_size(outbuf.str, outbuf.len - 9); |
| 3073 | |
| 3074 | if (es_now) |
| 3075 | outbuf.str[4] |= H2_F_HEADERS_END_STREAM; |
| 3076 | |
| 3077 | /* consume incoming H1 response */ |
| 3078 | bo_del(buf, ret); |
| 3079 | |
| 3080 | /* commit the H2 response */ |
| 3081 | h2c->mbuf->o += outbuf.len; |
| 3082 | h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3083 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3084 | |
| 3085 | /* for now we don't implemented CONTINUATION, so we wait for a |
| 3086 | * body or directly end in TRL2. |
| 3087 | */ |
| 3088 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3089 | // trim any possibly pending data (eg: inconsistent content-length) |
| 3090 | bo_del(buf, buf->o); |
| 3091 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3092 | h1m->state = HTTP_MSG_DONE; |
| 3093 | h2s->flags |= H2_SF_ES_SENT; |
| 3094 | if (h2s->st == H2_SS_OPEN) |
| 3095 | h2s->st = H2_SS_HLOC; |
| 3096 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3097 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3098 | } |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3099 | else if (h1m->status >= 100 && h1m->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3100 | /* we'll let the caller check if it has more headers to send */ |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3101 | h1m->state = HTTP_MSG_RPBEFORE; |
| 3102 | h1m->status = 0; |
| 3103 | h1m->flags = 0; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3104 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3105 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3106 | else |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 3107 | h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3108 | |
| 3109 | end: |
| 3110 | //fprintf(stderr, "[%d] sent simple H2 response (sid=%d) = %d bytes (%d in, ep=%u, es=%s)\n", h2c->st0, h2s->id, outbuf.len, ret, h1m->err_pos, h1_msg_state_str(h1m->err_state)); |
| 3111 | return ret; |
| 3112 | } |
| 3113 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3114 | /* Try to send a DATA frame matching HTTP/1 response present in the response |
| 3115 | * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error |
| 3116 | * (one of the H2_ERR* or h2_status codes), >0 on success in which case it |
| 3117 | * corresponds to the number of buffer bytes consumed. |
| 3118 | */ |
| 3119 | static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf) |
| 3120 | { |
| 3121 | struct h2c *h2c = h2s->h2c; |
| 3122 | struct h1m *h1m = &h2s->res; |
| 3123 | struct chunk outbuf; |
| 3124 | int ret = 0; |
| 3125 | int total = 0; |
| 3126 | int es_now = 0; |
| 3127 | int size = 0; |
| 3128 | char *blk1, *blk2; |
| 3129 | int len1, len2; |
| 3130 | |
| 3131 | if (h2c_mux_busy(h2c, h2s)) { |
| 3132 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3133 | goto end; |
| 3134 | } |
| 3135 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3136 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3137 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3138 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3139 | goto end; |
| 3140 | } |
| 3141 | |
| 3142 | new_frame: |
| 3143 | if (!buf->o) |
| 3144 | goto end; |
| 3145 | |
| 3146 | chunk_reset(&outbuf); |
| 3147 | |
| 3148 | while (1) { |
| 3149 | outbuf.str = bo_end(h2c->mbuf); |
| 3150 | outbuf.size = bo_contig_space(h2c->mbuf); |
| 3151 | outbuf.len = 0; |
| 3152 | |
| 3153 | if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf)) |
| 3154 | break; |
| 3155 | realign_again: |
| 3156 | buffer_slow_realign(h2c->mbuf); |
| 3157 | } |
| 3158 | |
| 3159 | if (outbuf.size < 9) { |
| 3160 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3161 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3162 | goto end; |
| 3163 | } |
| 3164 | |
| 3165 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 3166 | memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5); |
| 3167 | write_n32(outbuf.str + 5, h2s->id); // 4 bytes |
| 3168 | outbuf.len = 9; |
| 3169 | |
| 3170 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 3171 | case 0: /* no content length, read till SHUTW */ |
| 3172 | size = buf->o; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 3173 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3174 | break; |
| 3175 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
| 3176 | size = buf->o; |
| 3177 | if ((long long)size > h1m->curr_len) |
| 3178 | size = h1m->curr_len; |
| 3179 | break; |
| 3180 | default: /* te:chunked : parse chunks */ |
| 3181 | if (h1m->state == HTTP_MSG_CHUNK_CRLF) { |
| 3182 | ret = h1_skip_chunk_crlf(buf, -buf->o, 0); |
| 3183 | if (!ret) |
| 3184 | goto end; |
| 3185 | |
| 3186 | if (ret < 0) { |
| 3187 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
| 3188 | h1m->err_pos = ret; |
| 3189 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3190 | goto end; |
| 3191 | } |
| 3192 | bo_del(buf, ret); |
| 3193 | total += ret; |
| 3194 | h1m->state = HTTP_MSG_CHUNK_SIZE; |
| 3195 | } |
| 3196 | |
| 3197 | if (h1m->state == HTTP_MSG_CHUNK_SIZE) { |
| 3198 | unsigned int chunk; |
| 3199 | |
| 3200 | ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk); |
| 3201 | if (!ret) |
| 3202 | goto end; |
| 3203 | |
| 3204 | if (ret < 0) { |
| 3205 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
| 3206 | h1m->err_pos = ret; |
| 3207 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3208 | goto end; |
| 3209 | } |
| 3210 | |
| 3211 | size = chunk; |
| 3212 | h1m->curr_len = chunk; |
| 3213 | h1m->body_len += chunk; |
| 3214 | bo_del(buf, ret); |
| 3215 | total += ret; |
| 3216 | h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
| 3217 | if (!size) |
| 3218 | goto send_empty; |
| 3219 | } |
| 3220 | |
| 3221 | /* in MSG_DATA state, continue below */ |
| 3222 | size = h1m->curr_len; |
| 3223 | break; |
| 3224 | } |
| 3225 | |
| 3226 | /* we have in <size> the exact number of bytes we need to copy from |
| 3227 | * the H1 buffer. We need to check this against the connection's and |
| 3228 | * the stream's send windows, and to ensure that this fits in the max |
| 3229 | * frame size and in the buffer's available space minus 9 bytes (for |
| 3230 | * the frame header). The connection's flow control is applied last so |
| 3231 | * that we can use a separate list of streams which are immediately |
| 3232 | * unblocked on window opening. Note: we don't implement padding. |
| 3233 | */ |
| 3234 | |
| 3235 | if (size > buf->o) |
| 3236 | size = buf->o; |
| 3237 | |
| 3238 | if (size > h2s->mws) |
| 3239 | size = h2s->mws; |
| 3240 | |
| 3241 | if (size <= 0) { |
| 3242 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 3243 | goto end; |
| 3244 | } |
| 3245 | |
| 3246 | if (h2c->mfs && size > h2c->mfs) |
| 3247 | size = h2c->mfs; |
| 3248 | |
| 3249 | if (size + 9 > outbuf.size) { |
| 3250 | /* we have an opportunity for enlarging the too small |
| 3251 | * available space, let's try. |
| 3252 | */ |
| 3253 | if (buffer_space_wraps(h2c->mbuf)) |
| 3254 | goto realign_again; |
| 3255 | size = outbuf.size - 9; |
| 3256 | } |
| 3257 | |
| 3258 | if (size <= 0) { |
| 3259 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3260 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3261 | goto end; |
| 3262 | } |
| 3263 | |
| 3264 | if (size > h2c->mws) |
| 3265 | size = h2c->mws; |
| 3266 | |
| 3267 | if (size <= 0) { |
| 3268 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 3269 | goto end; |
| 3270 | } |
| 3271 | |
| 3272 | /* copy whatever we can */ |
| 3273 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
| 3274 | ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2); |
| 3275 | if (ret == 1) |
| 3276 | len2 = 0; |
| 3277 | |
| 3278 | if (!ret || len1 + len2 < size) { |
| 3279 | /* FIXME: must normally never happen */ |
| 3280 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3281 | goto end; |
| 3282 | } |
| 3283 | |
| 3284 | /* limit len1/len2 to size */ |
| 3285 | if (len1 + len2 > size) { |
| 3286 | int sub = len1 + len2 - size; |
| 3287 | |
| 3288 | if (len2 > sub) |
| 3289 | len2 -= sub; |
| 3290 | else { |
| 3291 | sub -= len2; |
| 3292 | len2 = 0; |
| 3293 | len1 -= sub; |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | /* now let's copy this this into the output buffer */ |
| 3298 | memcpy(outbuf.str + 9, blk1, len1); |
| 3299 | if (len2) |
| 3300 | memcpy(outbuf.str + 9 + len1, blk2, len2); |
| 3301 | |
| 3302 | send_empty: |
| 3303 | /* we may need to add END_STREAM */ |
| 3304 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 3305 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 3306 | * |
| 3307 | * FIXME: what we do here is not correct because we send end_stream |
| 3308 | * before knowing if we'll have to send a HEADERS frame for the |
| 3309 | * trailers. More importantly we're not consuming the trailing CRLF |
| 3310 | * after the end of trailers, so it will be left to the caller to |
| 3311 | * eat it. The right way to do it would be to measure trailers here |
| 3312 | * and to send ES only if there are no trailers. |
| 3313 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3314 | */ |
| 3315 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
| 3316 | !h1m->curr_len || h1m->state >= HTTP_MSG_DONE) |
| 3317 | es_now = 1; |
| 3318 | |
| 3319 | /* update the frame's size */ |
| 3320 | h2_set_frame_size(outbuf.str, size); |
| 3321 | |
| 3322 | if (es_now) |
| 3323 | outbuf.str[4] |= H2_F_DATA_END_STREAM; |
| 3324 | |
| 3325 | /* commit the H2 response */ |
| 3326 | h2c->mbuf->o += size + 9; |
| 3327 | h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9); |
| 3328 | |
| 3329 | /* consume incoming H1 response */ |
| 3330 | if (size > 0) { |
| 3331 | bo_del(buf, size); |
| 3332 | total += size; |
| 3333 | h1m->curr_len -= size; |
| 3334 | h2s->mws -= size; |
| 3335 | h2c->mws -= size; |
| 3336 | |
| 3337 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
| 3338 | h1m->state = HTTP_MSG_CHUNK_CRLF; |
| 3339 | goto new_frame; |
| 3340 | } |
| 3341 | } |
| 3342 | |
| 3343 | if (es_now) { |
| 3344 | if (h2s->st == H2_SS_OPEN) |
| 3345 | h2s->st = H2_SS_HLOC; |
| 3346 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3347 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3348 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3349 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 3350 | // trim any possibly pending data (eg: inconsistent content-length) |
| 3351 | bo_del(buf, buf->o); |
| 3352 | |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3353 | h1m->state = HTTP_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3354 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3355 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3356 | h2s->flags |= H2_SF_ES_SENT; |
| 3357 | } |
| 3358 | |
| 3359 | end: |
Willy Tarreau | 506a29a | 2018-07-18 10:07:58 +0200 | [diff] [blame] | 3360 | trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%d in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) buf->o=%u", h2c->st0, h2s->id, size+9, total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)buf->o); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3361 | return total; |
| 3362 | } |
| 3363 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3364 | /* Called from the upper layer, to send data */ |
| 3365 | static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags) |
| 3366 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3367 | struct h2s *h2s = cs->ctx; |
| 3368 | int total = 0; |
| 3369 | |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 3370 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o) |
| 3371 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 3372 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3373 | while (h2s->res.state < HTTP_MSG_DONE && buf->o) { |
| 3374 | if (h2s->res.state < HTTP_MSG_BODY) { |
| 3375 | total += h2s_frt_make_resp_headers(h2s, buf); |
| 3376 | |
Willy Tarreau | 9470d2c | 2017-12-03 10:42:59 +0100 | [diff] [blame] | 3377 | if (h2s->st >= H2_SS_ERROR) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3378 | break; |
| 3379 | |
| 3380 | if (h2s->flags & H2_SF_BLK_ANY) |
| 3381 | break; |
| 3382 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3383 | else if (h2s->res.state < HTTP_MSG_TRAILERS) { |
| 3384 | total += h2s_frt_make_resp_data(h2s, buf); |
| 3385 | |
Willy Tarreau | 9470d2c | 2017-12-03 10:42:59 +0100 | [diff] [blame] | 3386 | if (h2s->st >= H2_SS_ERROR) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3387 | break; |
| 3388 | |
| 3389 | if (h2s->flags & H2_SF_BLK_ANY) |
| 3390 | break; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3391 | } |
| 3392 | else if (h2s->res.state == HTTP_MSG_TRAILERS) { |
| 3393 | /* consume the trailers if any (we don't forward them for now) */ |
| 3394 | int count = h1_measure_trailers(buf); |
| 3395 | |
| 3396 | if (unlikely(count <= 0)) { |
| 3397 | if (count < 0) |
| 3398 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3399 | break; |
| 3400 | } |
| 3401 | total += count; |
| 3402 | bo_del(buf, count); |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3403 | |
| 3404 | // trim any possibly pending data (eg: extra CR-LF, ...) |
| 3405 | bo_del(buf, buf->o); |
| 3406 | |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3407 | h2s->res.state = HTTP_MSG_DONE; |
| 3408 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3409 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3410 | else { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3411 | cs->flags |= CS_FL_ERROR; |
| 3412 | break; |
| 3413 | } |
| 3414 | } |
| 3415 | |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 3416 | if (h2s->st >= H2_SS_ERROR) { |
| 3417 | /* trim any possibly pending data after we close (extra CR-LF, |
| 3418 | * unprocessed trailers, abnormal extra data, ...) |
| 3419 | */ |
| 3420 | bo_del(buf, buf->o); |
| 3421 | } |
| 3422 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 3423 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 3424 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 3425 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 3426 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3427 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 3428 | } |
| 3429 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3430 | if (h2s->flags & H2_SF_BLK_SFCTL) { |
| 3431 | /* stream flow control, quit the list */ |
| 3432 | LIST_DEL(&h2s->list); |
| 3433 | LIST_INIT(&h2s->list); |
| 3434 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3435 | else if (LIST_ISEMPTY(&h2s->list)) { |
| 3436 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 3437 | LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list); |
| 3438 | else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) |
| 3439 | LIST_ADDQ(&h2s->h2c->send_list, &h2s->list); |
| 3440 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3441 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3442 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3443 | } |
| 3444 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 3445 | /* for debugging with CLI's "show fd" command */ |
| 3446 | static void h2_show_fd(struct chunk *msg, struct connection *conn) |
| 3447 | { |
| 3448 | struct h2c *h2c = conn->mux_ctx; |
| 3449 | struct h2s *h2s; |
| 3450 | struct eb32_node *node; |
| 3451 | int fctl_cnt = 0; |
| 3452 | int send_cnt = 0; |
| 3453 | int tree_cnt = 0; |
| 3454 | int orph_cnt = 0; |
| 3455 | |
| 3456 | if (!h2c) |
| 3457 | return; |
| 3458 | |
| 3459 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
| 3460 | fctl_cnt++; |
| 3461 | |
| 3462 | list_for_each_entry(h2s, &h2c->send_list, list) |
| 3463 | send_cnt++; |
| 3464 | |
| 3465 | node = eb32_first(&h2c->streams_by_id); |
| 3466 | while (node) { |
| 3467 | h2s = container_of(node, struct h2s, by_id); |
| 3468 | tree_cnt++; |
| 3469 | if (!h2s->cs) |
| 3470 | orph_cnt++; |
| 3471 | node = eb32_next(node); |
| 3472 | } |
| 3473 | |
Willy Tarreau | c65edac | 2018-07-19 10:54:43 +0200 | [diff] [blame] | 3474 | chunk_appendf(msg, " st0=%d flg=0x%08x nbst=%u nbcs=%u fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u", |
Willy Tarreau | 506a29a | 2018-07-18 10:07:58 +0200 | [diff] [blame] | 3475 | h2c->st0, h2c->flags, h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt, (unsigned int)h2c->dbuf->i, (unsigned int)h2c->dbuf->size, (unsigned int)h2c->mbuf->o, (unsigned int)h2c->mbuf->size); |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 3476 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3477 | |
| 3478 | /*******************************************************/ |
| 3479 | /* functions below are dedicated to the config parsers */ |
| 3480 | /*******************************************************/ |
| 3481 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 3482 | /* config parser for global "tune.h2.header-table-size" */ |
| 3483 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 3484 | struct proxy *defpx, const char *file, int line, |
| 3485 | char **err) |
| 3486 | { |
| 3487 | if (too_many_args(1, args, err, NULL)) |
| 3488 | return -1; |
| 3489 | |
| 3490 | h2_settings_header_table_size = atoi(args[1]); |
| 3491 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 3492 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 3493 | return -1; |
| 3494 | } |
| 3495 | return 0; |
| 3496 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3497 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 3498 | /* config parser for global "tune.h2.initial-window-size" */ |
| 3499 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 3500 | struct proxy *defpx, const char *file, int line, |
| 3501 | char **err) |
| 3502 | { |
| 3503 | if (too_many_args(1, args, err, NULL)) |
| 3504 | return -1; |
| 3505 | |
| 3506 | h2_settings_initial_window_size = atoi(args[1]); |
| 3507 | if (h2_settings_initial_window_size < 0) { |
| 3508 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 3509 | return -1; |
| 3510 | } |
| 3511 | return 0; |
| 3512 | } |
| 3513 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 3514 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 3515 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 3516 | struct proxy *defpx, const char *file, int line, |
| 3517 | char **err) |
| 3518 | { |
| 3519 | if (too_many_args(1, args, err, NULL)) |
| 3520 | return -1; |
| 3521 | |
| 3522 | h2_settings_max_concurrent_streams = atoi(args[1]); |
| 3523 | if (h2_settings_max_concurrent_streams < 0) { |
| 3524 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 3525 | return -1; |
| 3526 | } |
| 3527 | return 0; |
| 3528 | } |
| 3529 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3530 | |
| 3531 | /****************************************/ |
| 3532 | /* MUX initialization and instanciation */ |
| 3533 | /***************************************/ |
| 3534 | |
| 3535 | /* The mux operations */ |
| 3536 | const struct mux_ops h2_ops = { |
| 3537 | .init = h2_init, |
| 3538 | .recv = h2_recv, |
| 3539 | .send = h2_send, |
| 3540 | .wake = h2_wake, |
| 3541 | .update_poll = h2_update_poll, |
| 3542 | .rcv_buf = h2_rcv_buf, |
| 3543 | .snd_buf = h2_snd_buf, |
| 3544 | .attach = h2_attach, |
| 3545 | .detach = h2_detach, |
| 3546 | .shutr = h2_shutr, |
| 3547 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 3548 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 3549 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3550 | .name = "H2", |
| 3551 | }; |
| 3552 | |
| 3553 | /* ALPN selection : this mux registers ALPN tolen "h2" */ |
| 3554 | static struct alpn_mux_list alpn_mux_h2 = |
| 3555 | { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops }; |
| 3556 | |
| 3557 | /* config keyword parsers */ |
| 3558 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 3559 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 3560 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 3561 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3562 | { 0, NULL, NULL } |
| 3563 | }}; |
| 3564 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3565 | static void __h2_deinit(void) |
| 3566 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3567 | pool_destroy(pool_head_h2s); |
| 3568 | pool_destroy(pool_head_h2c); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3569 | } |
| 3570 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3571 | __attribute__((constructor)) |
| 3572 | static void __h2_init(void) |
| 3573 | { |
| 3574 | alpn_register_mux(&alpn_mux_h2); |
| 3575 | cfg_register_keywords(&cfg_kws); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3576 | hap_register_post_deinit(__h2_deinit); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3577 | pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED); |
| 3578 | pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3579 | } |