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> |
| 16 | #include <common/hpack-tbl.h> |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame^] | 17 | #include <common/net_helper.h> |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 18 | #include <proto/applet.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 19 | #include <proto/connection.h> |
| 20 | #include <proto/stream.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 21 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 22 | |
| 23 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 24 | /* the h2c connection pool */ |
| 25 | static struct pool_head *pool2_h2c; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 26 | /* the h2s stream pool */ |
| 27 | static struct pool_head *pool2_h2s; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 28 | |
| 29 | /* Connection flags (32 bit), in h2c->flags */ |
| 30 | #define H2_CF_NONE 0x00000000 |
| 31 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 32 | /* Flags indicating why writing to the mux is blocked. */ |
| 33 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 34 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 35 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 36 | |
| 37 | /* Flags indicating why writing to the demux is blocked. */ |
| 38 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 39 | #define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full |
| 40 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 41 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 42 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 43 | #define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full |
| 44 | #define H2_CF_DEM_BLOCK_ANY 0x000000FC // aggregate of the demux flags above |
| 45 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 46 | /* H2 connection state, in h2c->st0 */ |
| 47 | enum h2_cs { |
| 48 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 49 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 50 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 51 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
| 52 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame |
| 53 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 54 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 55 | H2_CS_ENTRIES // must be last |
| 56 | } __attribute__((packed)); |
| 57 | |
| 58 | /* H2 connection descriptor */ |
| 59 | struct h2c { |
| 60 | struct connection *conn; |
| 61 | |
| 62 | enum h2_cs st0; /* mux state */ |
| 63 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 64 | |
| 65 | /* 16 bit hole here */ |
| 66 | uint32_t flags; /* connection flags: H2_CF_* */ |
| 67 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 68 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 69 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 70 | |
| 71 | /* states for the demux direction */ |
| 72 | struct hpack_dht *ddht; /* demux dynamic header table */ |
| 73 | struct buffer *dbuf; /* demux buffer */ |
| 74 | |
| 75 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 76 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 77 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 78 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
| 79 | /* 16 bit hole here */ |
| 80 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 81 | |
| 82 | /* states for the mux direction */ |
| 83 | struct buffer *mbuf; /* mux buffer */ |
| 84 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 85 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 86 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 87 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 88 | /* 16 bit hole here */ |
| 89 | int32_t miw; /* mux initial window size for all new streams */ |
| 90 | int32_t mws; /* mux window size. Can be negative. */ |
| 91 | int32_t mfs; /* mux's max frame size */ |
| 92 | |
| 93 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 94 | struct list send_list; /* list of blocked streams requesting to send */ |
| 95 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 96 | struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */ |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 97 | struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 100 | /* H2 stream state, in h2s->st */ |
| 101 | enum h2_ss { |
| 102 | H2_SS_IDLE = 0, // idle |
| 103 | H2_SS_RLOC, // reserved(local) |
| 104 | H2_SS_RREM, // reserved(remote) |
| 105 | H2_SS_OPEN, // open |
| 106 | H2_SS_HREM, // half-closed(remote) |
| 107 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 108 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
| 109 | H2_SS_RESET, // closed after sending RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 110 | H2_SS_CLOSED, // closed |
| 111 | H2_SS_ENTRIES // must be last |
| 112 | } __attribute__((packed)); |
| 113 | |
| 114 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 115 | #define H2_SF_NONE 0x00000000 |
| 116 | #define H2_SF_ES_RCVD 0x00000001 |
| 117 | #define H2_SF_ES_SENT 0x00000002 |
| 118 | |
| 119 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 120 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 121 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 122 | /* stream flags indicating the reason the stream is blocked */ |
| 123 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 124 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 125 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 126 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 127 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 128 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 129 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 130 | * it is being processed in the internal HTTP representation (H1 for now). |
| 131 | */ |
| 132 | struct h2s { |
| 133 | struct conn_stream *cs; |
| 134 | struct h2c *h2c; |
| 135 | struct h1m req, res; /* request and response parser state for H1 */ |
| 136 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
| 137 | struct list list; /* position in active/blocked lists if blocked>0 */ |
| 138 | int32_t id; /* stream ID */ |
| 139 | uint32_t flags; /* H2_SF_* */ |
| 140 | int mws; /* mux window size for this stream */ |
| 141 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 142 | enum h2_ss st; |
| 143 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 144 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 145 | /* descriptor for an h2 frame header */ |
| 146 | struct h2_fh { |
| 147 | uint32_t len; /* length, host order, 24 bits */ |
| 148 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 149 | uint8_t ft; /* frame type */ |
| 150 | uint8_t ff; /* frame flags */ |
| 151 | }; |
| 152 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 153 | /* a few settings from the global section */ |
| 154 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 155 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 156 | static int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 157 | |
| 158 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 159 | /*****************************************************/ |
| 160 | /* functions below are for dynamic buffer management */ |
| 161 | /*****************************************************/ |
| 162 | |
| 163 | /* re-enables receiving on mux <target> after a buffer was allocated. It returns |
| 164 | * 1 if the allocation succeeds, in which case the connection is woken up, or 0 |
| 165 | * if it's impossible to wake up and we prefer to be woken up later. |
| 166 | */ |
| 167 | static int h2_dbuf_available(void *target) |
| 168 | { |
| 169 | struct h2c *h2c = target; |
| 170 | |
| 171 | /* take the buffer now as we'll get scheduled waiting for ->wake() */ |
| 172 | if (b_alloc_margin(&h2c->dbuf, 0)) { |
| 173 | conn_xprt_want_recv(h2c->conn); |
| 174 | return 1; |
| 175 | } |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static inline struct buffer *h2_get_dbuf(struct h2c *h2c) |
| 180 | { |
| 181 | struct buffer *buf = NULL; |
| 182 | |
| 183 | if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) && |
| 184 | unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) { |
| 185 | h2c->dbuf_wait.target = h2c->conn; |
| 186 | h2c->dbuf_wait.wakeup_cb = h2_dbuf_available; |
| 187 | SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 188 | LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list); |
| 189 | SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 190 | __conn_xprt_stop_recv(h2c->conn); |
| 191 | } |
| 192 | return buf; |
| 193 | } |
| 194 | |
| 195 | static inline void h2_release_dbuf(struct h2c *h2c) |
| 196 | { |
| 197 | if (h2c->dbuf->size) { |
| 198 | b_free(&h2c->dbuf); |
| 199 | offer_buffers(h2c->dbuf_wait.target, |
| 200 | tasks_run_queue + applets_active_queue); |
| 201 | } |
| 202 | } |
| 203 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 204 | /* re-enables sending on mux <target> after a buffer was allocated. It returns |
| 205 | * 1 if the allocation succeeds, in which case the connection is woken up, or 0 |
| 206 | * if it's impossible to wake up and we prefer to be woken up later. |
| 207 | */ |
| 208 | static int h2_mbuf_available(void *target) |
| 209 | { |
| 210 | struct h2c *h2c = target; |
| 211 | |
| 212 | /* take the buffer now as we'll get scheduled waiting for ->wake(). */ |
| 213 | if (b_alloc_margin(&h2c->mbuf, 0)) { |
| 214 | /* FIXME: we should in fact call something like h2_update_poll() |
| 215 | * now to recompte the polling. For now it will be enough like |
| 216 | * this. |
| 217 | */ |
| 218 | conn_xprt_want_recv(h2c->conn); |
| 219 | return 1; |
| 220 | } |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static inline struct buffer *h2_get_mbuf(struct h2c *h2c) |
| 225 | { |
| 226 | struct buffer *buf = NULL; |
| 227 | |
| 228 | if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) && |
| 229 | unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) { |
| 230 | h2c->mbuf_wait.target = h2c; |
| 231 | h2c->mbuf_wait.wakeup_cb = h2_mbuf_available; |
| 232 | SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 233 | LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list); |
| 234 | SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 235 | |
| 236 | /* FIXME: we should in fact only block the direction being |
| 237 | * currently used. For now it will be enough like this. |
| 238 | */ |
| 239 | __conn_xprt_stop_send(h2c->conn); |
| 240 | __conn_xprt_stop_recv(h2c->conn); |
| 241 | } |
| 242 | return buf; |
| 243 | } |
| 244 | |
| 245 | static inline void h2_release_mbuf(struct h2c *h2c) |
| 246 | { |
| 247 | if (h2c->mbuf->size) { |
| 248 | b_free(&h2c->mbuf); |
| 249 | offer_buffers(h2c->mbuf_wait.target, |
| 250 | tasks_run_queue + applets_active_queue); |
| 251 | } |
| 252 | } |
| 253 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 254 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 255 | /*****************************************************************/ |
| 256 | /* functions below are dedicated to the mux setup and management */ |
| 257 | /*****************************************************************/ |
| 258 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 259 | /* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */ |
| 260 | static int h2c_frt_init(struct connection *conn) |
| 261 | { |
| 262 | struct h2c *h2c; |
| 263 | |
| 264 | h2c = pool_alloc2(pool2_h2c); |
| 265 | if (!h2c) |
| 266 | goto fail; |
| 267 | |
| 268 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 269 | if (!h2c->ddht) |
| 270 | goto fail; |
| 271 | |
| 272 | /* Initialise the context. */ |
| 273 | h2c->st0 = H2_CS_PREFACE; |
| 274 | h2c->conn = conn; |
| 275 | h2c->max_id = -1; |
| 276 | h2c->errcode = H2_ERR_NO_ERROR; |
| 277 | h2c->flags = H2_CF_NONE; |
| 278 | h2c->rcvd_c = 0; |
| 279 | h2c->rcvd_s = 0; |
| 280 | |
| 281 | h2c->dbuf = &buf_empty; |
| 282 | h2c->dsi = -1; |
| 283 | h2c->msi = -1; |
| 284 | h2c->last_sid = -1; |
| 285 | |
| 286 | h2c->mbuf = &buf_empty; |
| 287 | h2c->miw = 65535; /* mux initial window size */ |
| 288 | h2c->mws = 65535; /* mux window size */ |
| 289 | h2c->mfs = 16384; /* initial max frame size */ |
| 290 | h2c->streams_by_id = EB_ROOT_UNIQUE; |
| 291 | LIST_INIT(&h2c->send_list); |
| 292 | LIST_INIT(&h2c->fctl_list); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 293 | LIST_INIT(&h2c->dbuf_wait.list); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 294 | LIST_INIT(&h2c->mbuf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 295 | conn->mux_ctx = h2c; |
| 296 | |
| 297 | conn_xprt_want_recv(conn); |
| 298 | /* mux->wake will be called soon to complete the operation */ |
| 299 | return 0; |
| 300 | fail: |
| 301 | pool_free2(pool2_h2c, h2c); |
| 302 | return -1; |
| 303 | } |
| 304 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 305 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 306 | * is already initialized before installing the mux, so we detect incoming |
| 307 | * connections from the fact that the context is still NULL. Returns < 0 on |
| 308 | * error. |
| 309 | */ |
| 310 | static int h2_init(struct connection *conn) |
| 311 | { |
| 312 | if (conn->mux_ctx) { |
| 313 | /* we don't support outgoing connections for now */ |
| 314 | return -1; |
| 315 | } |
| 316 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 317 | return h2c_frt_init(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* release function for a connection. This one should be called to free all |
| 321 | * resources allocated to the mux. |
| 322 | */ |
| 323 | static void h2_release(struct connection *conn) |
| 324 | { |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 325 | struct h2c *h2c = conn->mux_ctx; |
| 326 | |
| 327 | LIST_DEL(&conn->list); |
| 328 | |
| 329 | if (h2c) { |
| 330 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 331 | h2_release_dbuf(h2c); |
| 332 | SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 333 | LIST_DEL(&h2c->dbuf_wait.list); |
| 334 | SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 335 | |
| 336 | h2_release_mbuf(h2c); |
| 337 | SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 338 | LIST_DEL(&h2c->mbuf_wait.list); |
| 339 | SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 340 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 341 | pool_free2(pool2_h2c, h2c); |
| 342 | } |
| 343 | |
| 344 | conn->mux = NULL; |
| 345 | conn->mux_ctx = NULL; |
| 346 | |
| 347 | conn_stop_tracking(conn); |
| 348 | conn_full_close(conn); |
| 349 | if (conn->destroy_cb) |
| 350 | conn->destroy_cb(conn); |
| 351 | conn_free(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 355 | /******************************************************/ |
| 356 | /* functions below are for the H2 protocol processing */ |
| 357 | /******************************************************/ |
| 358 | |
| 359 | /* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */ |
| 360 | static inline int h2s_id(const struct h2s *h2s) |
| 361 | { |
| 362 | return h2s ? h2s->id : 0; |
| 363 | } |
| 364 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 365 | /* returns true of the mux is currently busy as seen from stream <h2s> */ |
| 366 | static inline int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s) |
| 367 | { |
| 368 | if (h2c->msi < 0) |
| 369 | return 0; |
| 370 | |
| 371 | if (h2c->msi == h2s_id(h2s)) |
| 372 | return 0; |
| 373 | |
| 374 | return 1; |
| 375 | } |
| 376 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 377 | /* marks an error on the connection */ |
| 378 | static inline void h2c_error(struct h2c *h2c, enum h2_err err) |
| 379 | { |
| 380 | h2c->errcode = err; |
| 381 | h2c->st0 = H2_CS_ERROR; |
| 382 | } |
| 383 | |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 384 | /* marks an error on the stream */ |
| 385 | static inline void h2s_error(struct h2s *h2s, enum h2_err err) |
| 386 | { |
| 387 | if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) { |
| 388 | h2s->errcode = err; |
| 389 | h2s->st = H2_SS_ERROR; |
| 390 | if (h2s->cs) |
| 391 | h2s->cs->flags |= CS_FL_ERROR; |
| 392 | } |
| 393 | } |
| 394 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame^] | 395 | /* writes the 24-bit frame size <len> at address <frame> */ |
| 396 | static inline void h2_set_frame_size(void *frame, uint32_t len) |
| 397 | { |
| 398 | uint8_t *out = frame; |
| 399 | |
| 400 | *out = len >> 16; |
| 401 | write_n16(out + 1, len); |
| 402 | } |
| 403 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 404 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 405 | /*********************************************************/ |
| 406 | /* functions below are I/O callbacks from the connection */ |
| 407 | /*********************************************************/ |
| 408 | |
| 409 | /* callback called on recv event by the connection handler */ |
| 410 | static void h2_recv(struct connection *conn) |
| 411 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 412 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 413 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 414 | int max; |
| 415 | |
| 416 | if (conn->flags & CO_FL_ERROR) |
| 417 | goto error; |
| 418 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 419 | buf = h2_get_dbuf(h2c); |
| 420 | if (!buf) |
| 421 | return; |
| 422 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 423 | /* note: buf->o == 0 */ |
| 424 | max = buf->size - buf->i; |
| 425 | if (!max) { |
| 426 | /* FIXME: buffer full, add a flag, stop polling and wait */ |
| 427 | __conn_xprt_stop_recv(conn); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | conn->xprt->rcv_buf(conn, buf, max); |
| 432 | if (conn->flags & CO_FL_ERROR) |
| 433 | goto error; |
| 434 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 435 | if (!buf->i) |
| 436 | h2_release_dbuf(h2c); |
| 437 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 438 | if (buf->i == buf->size) { |
| 439 | /* buffer now full */ |
| 440 | __conn_xprt_stop_recv(conn); |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | /* FIXME: should we try to process streams here instead of doing it in ->wake ? */ |
| 445 | |
| 446 | if (conn_xprt_read0_pending(conn)) |
| 447 | __conn_xprt_stop_recv(conn); |
| 448 | return; |
| 449 | |
| 450 | error: |
| 451 | __conn_xprt_stop_recv(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* callback called on send event by the connection handler */ |
| 455 | static void h2_send(struct connection *conn) |
| 456 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 457 | struct h2c *h2c = conn->mux_ctx; |
| 458 | |
| 459 | /* FIXME: should we try to process pending streams here instead of doing it in ->wake ? */ |
| 460 | |
| 461 | if (conn->flags & CO_FL_ERROR) |
| 462 | goto error; |
| 463 | |
| 464 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 465 | /* a handshake was requested */ |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | if (!h2c->mbuf->o) { |
| 470 | /* nothing to send */ |
| 471 | goto done; |
| 472 | } |
| 473 | |
| 474 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 475 | /* output closed, nothing to send, clear the buffer to release it */ |
| 476 | h2c->mbuf->o = 0; |
| 477 | goto done; |
| 478 | } |
| 479 | |
| 480 | /* pending response data, we need to try to send or subscribe to |
| 481 | * writes. The snd_buf() function takes a "flags" argument which |
| 482 | * may be made of a combination of CO_SFL_MSG_MORE to indicate |
| 483 | * that more data immediately comes and CO_SFL_STREAMER to |
| 484 | * indicate that the connection is streaming lots of data (used |
| 485 | * to increase TLS record size at the expense of latency). The |
| 486 | * former could be sent any time there's a buffer full flag, as |
| 487 | * it indicates at least one stream attempted to send and failed |
| 488 | * so there are pending data. And alternative would be to set it |
| 489 | * as long as there's an active stream but that would be |
| 490 | * problematic for ACKs. The latter should possibly not be set |
| 491 | * for now. |
| 492 | */ |
| 493 | conn->xprt->snd_buf(conn, h2c->mbuf, 0); |
| 494 | |
| 495 | if (conn->flags & CO_FL_ERROR) |
| 496 | goto error; |
| 497 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 498 | if (!h2c->mbuf->o) |
| 499 | h2_release_mbuf(h2c); |
| 500 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 501 | if (h2c->mbuf->o) { |
| 502 | /* incomplete send, the snd_buf callback has already updated |
| 503 | * the connection flags. |
| 504 | * |
| 505 | * FIXME: we should arm a send timeout here |
| 506 | */ |
| 507 | __conn_xprt_want_send(conn); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | done: |
| 512 | /* FIXME: release the output buffer when empty or do it in ->wake() ? */ |
| 513 | __conn_xprt_stop_send(conn); |
| 514 | return; |
| 515 | |
| 516 | error: |
| 517 | /* FIXME: report an error somewhere in the mux */ |
| 518 | __conn_xprt_stop_send(conn); |
| 519 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* callback called on any event by the connection handler. |
| 523 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 524 | * destruction of the connection (which normally doesn not happen in h2). |
| 525 | */ |
| 526 | static int h2_wake(struct connection *conn) |
| 527 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 528 | struct h2c *h2c = conn->mux_ctx; |
| 529 | |
| 530 | if ((conn->flags & CO_FL_ERROR) && eb_is_empty(&h2c->streams_by_id)) { |
| 531 | h2_release(conn); |
| 532 | return -1; |
| 533 | } |
| 534 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | /*******************************************/ |
| 539 | /* functions below are used by the streams */ |
| 540 | /*******************************************/ |
| 541 | |
| 542 | /* |
| 543 | * Attach a new stream to a connection |
| 544 | * (Used for outgoing connections) |
| 545 | */ |
| 546 | static struct conn_stream *h2_attach(struct connection *conn) |
| 547 | { |
| 548 | return NULL; |
| 549 | } |
| 550 | |
| 551 | /* callback used to update the mux's polling flags after changing a cs' status. |
| 552 | * The caller (cs_update_mux_polling) will take care of propagating any changes |
| 553 | * to the transport layer. |
| 554 | */ |
| 555 | static void h2_update_poll(struct conn_stream *cs) |
| 556 | { |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Detach the stream from the connection and possibly release the connection. |
| 561 | */ |
| 562 | static void h2_detach(struct conn_stream *cs) |
| 563 | { |
| 564 | } |
| 565 | |
| 566 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 567 | { |
| 568 | } |
| 569 | |
| 570 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 571 | { |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * Called from the upper layer, to get more data |
| 576 | */ |
| 577 | static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count) |
| 578 | { |
| 579 | /* FIXME: not handled for now */ |
| 580 | cs->flags |= CS_FL_ERROR; |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | /* Called from the upper layer, to send data */ |
| 585 | static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags) |
| 586 | { |
| 587 | /* FIXME: not handled for now */ |
| 588 | cs->flags |= CS_FL_ERROR; |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | /*******************************************************/ |
| 594 | /* functions below are dedicated to the config parsers */ |
| 595 | /*******************************************************/ |
| 596 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 597 | /* config parser for global "tune.h2.header-table-size" */ |
| 598 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 599 | struct proxy *defpx, const char *file, int line, |
| 600 | char **err) |
| 601 | { |
| 602 | if (too_many_args(1, args, err, NULL)) |
| 603 | return -1; |
| 604 | |
| 605 | h2_settings_header_table_size = atoi(args[1]); |
| 606 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 607 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 608 | return -1; |
| 609 | } |
| 610 | return 0; |
| 611 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 612 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 613 | /* config parser for global "tune.h2.initial-window-size" */ |
| 614 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 615 | struct proxy *defpx, const char *file, int line, |
| 616 | char **err) |
| 617 | { |
| 618 | if (too_many_args(1, args, err, NULL)) |
| 619 | return -1; |
| 620 | |
| 621 | h2_settings_initial_window_size = atoi(args[1]); |
| 622 | if (h2_settings_initial_window_size < 0) { |
| 623 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 624 | return -1; |
| 625 | } |
| 626 | return 0; |
| 627 | } |
| 628 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 629 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 630 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 631 | struct proxy *defpx, const char *file, int line, |
| 632 | char **err) |
| 633 | { |
| 634 | if (too_many_args(1, args, err, NULL)) |
| 635 | return -1; |
| 636 | |
| 637 | h2_settings_max_concurrent_streams = atoi(args[1]); |
| 638 | if (h2_settings_max_concurrent_streams < 0) { |
| 639 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 640 | return -1; |
| 641 | } |
| 642 | return 0; |
| 643 | } |
| 644 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 645 | |
| 646 | /****************************************/ |
| 647 | /* MUX initialization and instanciation */ |
| 648 | /***************************************/ |
| 649 | |
| 650 | /* The mux operations */ |
| 651 | const struct mux_ops h2_ops = { |
| 652 | .init = h2_init, |
| 653 | .recv = h2_recv, |
| 654 | .send = h2_send, |
| 655 | .wake = h2_wake, |
| 656 | .update_poll = h2_update_poll, |
| 657 | .rcv_buf = h2_rcv_buf, |
| 658 | .snd_buf = h2_snd_buf, |
| 659 | .attach = h2_attach, |
| 660 | .detach = h2_detach, |
| 661 | .shutr = h2_shutr, |
| 662 | .shutw = h2_shutw, |
| 663 | .release = h2_release, |
| 664 | .name = "H2", |
| 665 | }; |
| 666 | |
| 667 | /* ALPN selection : this mux registers ALPN tolen "h2" */ |
| 668 | static struct alpn_mux_list alpn_mux_h2 = |
| 669 | { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops }; |
| 670 | |
| 671 | /* config keyword parsers */ |
| 672 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 673 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 674 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 675 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 676 | { 0, NULL, NULL } |
| 677 | }}; |
| 678 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 679 | static void __h2_deinit(void) |
| 680 | { |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 681 | pool_destroy2(pool2_h2s); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 682 | pool_destroy2(pool2_h2c); |
| 683 | } |
| 684 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 685 | __attribute__((constructor)) |
| 686 | static void __h2_init(void) |
| 687 | { |
| 688 | alpn_register_mux(&alpn_mux_h2); |
| 689 | cfg_register_keywords(&cfg_kws); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 690 | hap_register_post_deinit(__h2_deinit); |
| 691 | pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED); |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 692 | pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 693 | } |