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