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