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> |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 20 | #include <proto/h1.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 21 | #include <proto/stream.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 22 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 23 | |
| 24 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 25 | /* dummy streams returned for idle and closed states */ |
| 26 | static const struct h2s *h2_closed_stream; |
| 27 | static const struct h2s *h2_idle_stream; |
| 28 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 29 | /* the h2c connection pool */ |
| 30 | static struct pool_head *pool2_h2c; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 31 | /* the h2s stream pool */ |
| 32 | static struct pool_head *pool2_h2s; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 33 | |
| 34 | /* Connection flags (32 bit), in h2c->flags */ |
| 35 | #define H2_CF_NONE 0x00000000 |
| 36 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 37 | /* Flags indicating why writing to the mux is blocked. */ |
| 38 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 39 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 40 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 41 | |
| 42 | /* Flags indicating why writing to the demux is blocked. */ |
| 43 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 44 | #define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full |
| 45 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 46 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 47 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 48 | #define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full |
| 49 | #define H2_CF_DEM_BLOCK_ANY 0x000000FC // aggregate of the demux flags above |
| 50 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 51 | /* H2 connection state, in h2c->st0 */ |
| 52 | enum h2_cs { |
| 53 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 54 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 55 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 56 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
| 57 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame |
| 58 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 59 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 60 | H2_CS_ENTRIES // must be last |
| 61 | } __attribute__((packed)); |
| 62 | |
| 63 | /* H2 connection descriptor */ |
| 64 | struct h2c { |
| 65 | struct connection *conn; |
| 66 | |
| 67 | enum h2_cs st0; /* mux state */ |
| 68 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 69 | |
| 70 | /* 16 bit hole here */ |
| 71 | uint32_t flags; /* connection flags: H2_CF_* */ |
| 72 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 73 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 74 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 75 | |
| 76 | /* states for the demux direction */ |
| 77 | struct hpack_dht *ddht; /* demux dynamic header table */ |
| 78 | struct buffer *dbuf; /* demux buffer */ |
| 79 | |
| 80 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 81 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 82 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 83 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
| 84 | /* 16 bit hole here */ |
| 85 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 86 | |
| 87 | /* states for the mux direction */ |
| 88 | struct buffer *mbuf; /* mux buffer */ |
| 89 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 90 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 91 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 92 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 93 | /* 16 bit hole here */ |
| 94 | int32_t miw; /* mux initial window size for all new streams */ |
| 95 | int32_t mws; /* mux window size. Can be negative. */ |
| 96 | int32_t mfs; /* mux's max frame size */ |
| 97 | |
| 98 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 99 | struct list send_list; /* list of blocked streams requesting to send */ |
| 100 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 101 | struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */ |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 102 | struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 105 | /* H2 stream state, in h2s->st */ |
| 106 | enum h2_ss { |
| 107 | H2_SS_IDLE = 0, // idle |
| 108 | H2_SS_RLOC, // reserved(local) |
| 109 | H2_SS_RREM, // reserved(remote) |
| 110 | H2_SS_OPEN, // open |
| 111 | H2_SS_HREM, // half-closed(remote) |
| 112 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 113 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
| 114 | H2_SS_RESET, // closed after sending RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 115 | H2_SS_CLOSED, // closed |
| 116 | H2_SS_ENTRIES // must be last |
| 117 | } __attribute__((packed)); |
| 118 | |
| 119 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 120 | #define H2_SF_NONE 0x00000000 |
| 121 | #define H2_SF_ES_RCVD 0x00000001 |
| 122 | #define H2_SF_ES_SENT 0x00000002 |
| 123 | |
| 124 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 125 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 126 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 127 | /* stream flags indicating the reason the stream is blocked */ |
| 128 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 129 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 130 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 131 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 132 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 133 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 134 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 135 | * it is being processed in the internal HTTP representation (H1 for now). |
| 136 | */ |
| 137 | struct h2s { |
| 138 | struct conn_stream *cs; |
| 139 | struct h2c *h2c; |
| 140 | struct h1m req, res; /* request and response parser state for H1 */ |
| 141 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
| 142 | struct list list; /* position in active/blocked lists if blocked>0 */ |
| 143 | int32_t id; /* stream ID */ |
| 144 | uint32_t flags; /* H2_SF_* */ |
| 145 | int mws; /* mux window size for this stream */ |
| 146 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 147 | enum h2_ss st; |
| 148 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 149 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 150 | /* descriptor for an h2 frame header */ |
| 151 | struct h2_fh { |
| 152 | uint32_t len; /* length, host order, 24 bits */ |
| 153 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 154 | uint8_t ft; /* frame type */ |
| 155 | uint8_t ff; /* frame flags */ |
| 156 | }; |
| 157 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 158 | /* a few settings from the global section */ |
| 159 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 160 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 161 | static int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 162 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 163 | /* a dmumy closed stream */ |
| 164 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 165 | .cs = NULL, |
| 166 | .h2c = NULL, |
| 167 | .st = H2_SS_CLOSED, |
| 168 | .id = 0, |
| 169 | }; |
| 170 | |
| 171 | /* and a dummy idle stream for use with any unannounced stream */ |
| 172 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 173 | .cs = NULL, |
| 174 | .h2c = NULL, |
| 175 | .st = H2_SS_IDLE, |
| 176 | .id = 0, |
| 177 | }; |
| 178 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 179 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 180 | /*****************************************************/ |
| 181 | /* functions below are for dynamic buffer management */ |
| 182 | /*****************************************************/ |
| 183 | |
| 184 | /* re-enables receiving on mux <target> after a buffer was allocated. It returns |
| 185 | * 1 if the allocation succeeds, in which case the connection is woken up, or 0 |
| 186 | * if it's impossible to wake up and we prefer to be woken up later. |
| 187 | */ |
| 188 | static int h2_dbuf_available(void *target) |
| 189 | { |
| 190 | struct h2c *h2c = target; |
| 191 | |
| 192 | /* take the buffer now as we'll get scheduled waiting for ->wake() */ |
| 193 | if (b_alloc_margin(&h2c->dbuf, 0)) { |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 194 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
| 195 | if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
| 196 | conn_xprt_want_recv(h2c->conn); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 197 | return 1; |
| 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static inline struct buffer *h2_get_dbuf(struct h2c *h2c) |
| 203 | { |
| 204 | struct buffer *buf = NULL; |
| 205 | |
| 206 | if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) && |
| 207 | unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) { |
| 208 | h2c->dbuf_wait.target = h2c->conn; |
| 209 | h2c->dbuf_wait.wakeup_cb = h2_dbuf_available; |
| 210 | SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 211 | LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list); |
| 212 | SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 213 | __conn_xprt_stop_recv(h2c->conn); |
| 214 | } |
| 215 | return buf; |
| 216 | } |
| 217 | |
| 218 | static inline void h2_release_dbuf(struct h2c *h2c) |
| 219 | { |
| 220 | if (h2c->dbuf->size) { |
| 221 | b_free(&h2c->dbuf); |
| 222 | offer_buffers(h2c->dbuf_wait.target, |
| 223 | tasks_run_queue + applets_active_queue); |
| 224 | } |
| 225 | } |
| 226 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 227 | /* re-enables sending on mux <target> after a buffer was allocated. It returns |
| 228 | * 1 if the allocation succeeds, in which case the connection is woken up, or 0 |
| 229 | * if it's impossible to wake up and we prefer to be woken up later. |
| 230 | */ |
| 231 | static int h2_mbuf_available(void *target) |
| 232 | { |
| 233 | struct h2c *h2c = target; |
| 234 | |
| 235 | /* take the buffer now as we'll get scheduled waiting for ->wake(). */ |
| 236 | if (b_alloc_margin(&h2c->mbuf, 0)) { |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 237 | if (h2c->flags & H2_CF_MUX_MALLOC) { |
| 238 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
| 239 | if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY)) |
| 240 | conn_xprt_want_send(h2c->conn); |
| 241 | } |
| 242 | |
| 243 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 244 | h2c->flags &= ~H2_CF_DEM_MROOM; |
| 245 | if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
| 246 | conn_xprt_want_recv(h2c->conn); |
| 247 | } |
| 248 | |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 249 | /* FIXME: we should in fact call something like h2_update_poll() |
| 250 | * now to recompte the polling. For now it will be enough like |
| 251 | * this. |
| 252 | */ |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 253 | return 1; |
| 254 | } |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static inline struct buffer *h2_get_mbuf(struct h2c *h2c) |
| 259 | { |
| 260 | struct buffer *buf = NULL; |
| 261 | |
| 262 | if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) && |
| 263 | unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) { |
| 264 | h2c->mbuf_wait.target = h2c; |
| 265 | h2c->mbuf_wait.wakeup_cb = h2_mbuf_available; |
| 266 | SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 267 | LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list); |
| 268 | SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 269 | |
| 270 | /* FIXME: we should in fact only block the direction being |
| 271 | * currently used. For now it will be enough like this. |
| 272 | */ |
| 273 | __conn_xprt_stop_send(h2c->conn); |
| 274 | __conn_xprt_stop_recv(h2c->conn); |
| 275 | } |
| 276 | return buf; |
| 277 | } |
| 278 | |
| 279 | static inline void h2_release_mbuf(struct h2c *h2c) |
| 280 | { |
| 281 | if (h2c->mbuf->size) { |
| 282 | b_free(&h2c->mbuf); |
| 283 | offer_buffers(h2c->mbuf_wait.target, |
| 284 | tasks_run_queue + applets_active_queue); |
| 285 | } |
| 286 | } |
| 287 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 288 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 289 | /*****************************************************************/ |
| 290 | /* functions below are dedicated to the mux setup and management */ |
| 291 | /*****************************************************************/ |
| 292 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 293 | /* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */ |
| 294 | static int h2c_frt_init(struct connection *conn) |
| 295 | { |
| 296 | struct h2c *h2c; |
| 297 | |
| 298 | h2c = pool_alloc2(pool2_h2c); |
| 299 | if (!h2c) |
| 300 | goto fail; |
| 301 | |
| 302 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 303 | if (!h2c->ddht) |
| 304 | goto fail; |
| 305 | |
| 306 | /* Initialise the context. */ |
| 307 | h2c->st0 = H2_CS_PREFACE; |
| 308 | h2c->conn = conn; |
| 309 | h2c->max_id = -1; |
| 310 | h2c->errcode = H2_ERR_NO_ERROR; |
| 311 | h2c->flags = H2_CF_NONE; |
| 312 | h2c->rcvd_c = 0; |
| 313 | h2c->rcvd_s = 0; |
| 314 | |
| 315 | h2c->dbuf = &buf_empty; |
| 316 | h2c->dsi = -1; |
| 317 | h2c->msi = -1; |
| 318 | h2c->last_sid = -1; |
| 319 | |
| 320 | h2c->mbuf = &buf_empty; |
| 321 | h2c->miw = 65535; /* mux initial window size */ |
| 322 | h2c->mws = 65535; /* mux window size */ |
| 323 | h2c->mfs = 16384; /* initial max frame size */ |
| 324 | h2c->streams_by_id = EB_ROOT_UNIQUE; |
| 325 | LIST_INIT(&h2c->send_list); |
| 326 | LIST_INIT(&h2c->fctl_list); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 327 | LIST_INIT(&h2c->dbuf_wait.list); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 328 | LIST_INIT(&h2c->mbuf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 329 | conn->mux_ctx = h2c; |
| 330 | |
| 331 | conn_xprt_want_recv(conn); |
| 332 | /* mux->wake will be called soon to complete the operation */ |
| 333 | return 0; |
| 334 | fail: |
| 335 | pool_free2(pool2_h2c, h2c); |
| 336 | return -1; |
| 337 | } |
| 338 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 339 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 340 | * is already initialized before installing the mux, so we detect incoming |
| 341 | * connections from the fact that the context is still NULL. Returns < 0 on |
| 342 | * error. |
| 343 | */ |
| 344 | static int h2_init(struct connection *conn) |
| 345 | { |
| 346 | if (conn->mux_ctx) { |
| 347 | /* we don't support outgoing connections for now */ |
| 348 | return -1; |
| 349 | } |
| 350 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 351 | return h2c_frt_init(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 354 | /* returns the stream associated with id <id> or NULL if not found */ |
| 355 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 356 | { |
| 357 | struct eb32_node *node; |
| 358 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 359 | if (id > h2c->max_id) |
| 360 | return (struct h2s *)h2_idle_stream; |
| 361 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 362 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 363 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 364 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 365 | |
| 366 | return container_of(node, struct h2s, by_id); |
| 367 | } |
| 368 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 369 | /* release function for a connection. This one should be called to free all |
| 370 | * resources allocated to the mux. |
| 371 | */ |
| 372 | static void h2_release(struct connection *conn) |
| 373 | { |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 374 | struct h2c *h2c = conn->mux_ctx; |
| 375 | |
| 376 | LIST_DEL(&conn->list); |
| 377 | |
| 378 | if (h2c) { |
| 379 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 380 | h2_release_dbuf(h2c); |
| 381 | SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 382 | LIST_DEL(&h2c->dbuf_wait.list); |
| 383 | SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 384 | |
| 385 | h2_release_mbuf(h2c); |
| 386 | SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 387 | LIST_DEL(&h2c->mbuf_wait.list); |
| 388 | SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 389 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 390 | pool_free2(pool2_h2c, h2c); |
| 391 | } |
| 392 | |
| 393 | conn->mux = NULL; |
| 394 | conn->mux_ctx = NULL; |
| 395 | |
| 396 | conn_stop_tracking(conn); |
| 397 | conn_full_close(conn); |
| 398 | if (conn->destroy_cb) |
| 399 | conn->destroy_cb(conn); |
| 400 | conn_free(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 404 | /******************************************************/ |
| 405 | /* functions below are for the H2 protocol processing */ |
| 406 | /******************************************************/ |
| 407 | |
| 408 | /* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */ |
| 409 | static inline int h2s_id(const struct h2s *h2s) |
| 410 | { |
| 411 | return h2s ? h2s->id : 0; |
| 412 | } |
| 413 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 414 | /* returns true of the mux is currently busy as seen from stream <h2s> */ |
| 415 | static inline int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s) |
| 416 | { |
| 417 | if (h2c->msi < 0) |
| 418 | return 0; |
| 419 | |
| 420 | if (h2c->msi == h2s_id(h2s)) |
| 421 | return 0; |
| 422 | |
| 423 | return 1; |
| 424 | } |
| 425 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 426 | /* marks an error on the connection */ |
| 427 | static inline void h2c_error(struct h2c *h2c, enum h2_err err) |
| 428 | { |
| 429 | h2c->errcode = err; |
| 430 | h2c->st0 = H2_CS_ERROR; |
| 431 | } |
| 432 | |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 433 | /* marks an error on the stream */ |
| 434 | static inline void h2s_error(struct h2s *h2s, enum h2_err err) |
| 435 | { |
| 436 | if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) { |
| 437 | h2s->errcode = err; |
| 438 | h2s->st = H2_SS_ERROR; |
| 439 | if (h2s->cs) |
| 440 | h2s->cs->flags |= CS_FL_ERROR; |
| 441 | } |
| 442 | } |
| 443 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 444 | /* writes the 24-bit frame size <len> at address <frame> */ |
| 445 | static inline void h2_set_frame_size(void *frame, uint32_t len) |
| 446 | { |
| 447 | uint8_t *out = frame; |
| 448 | |
| 449 | *out = len >> 16; |
| 450 | write_n16(out + 1, len); |
| 451 | } |
| 452 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 453 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 454 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 455 | * the caller's responsibility to verify that there are at least <bytes> bytes |
| 456 | * available in the buffer's input prior to calling this function. |
| 457 | */ |
| 458 | static inline void h2_get_buf_bytes(void *dst, size_t bytes, |
| 459 | const struct buffer *b, int o) |
| 460 | { |
| 461 | readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 462 | } |
| 463 | |
| 464 | static inline uint16_t h2_get_n16(const struct buffer *b, int o) |
| 465 | { |
| 466 | return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 467 | } |
| 468 | |
| 469 | static inline uint32_t h2_get_n32(const struct buffer *b, int o) |
| 470 | { |
| 471 | return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 472 | } |
| 473 | |
| 474 | static inline uint64_t h2_get_n64(const struct buffer *b, int o) |
| 475 | { |
| 476 | return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 477 | } |
| 478 | |
| 479 | |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 480 | /* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm |
| 481 | * is not obvious. It turns out that H2 headers are neither aligned nor do they |
| 482 | * use regular sizes. And to add to the trouble, the buffer may wrap so each |
| 483 | * byte read must be checked. The header is formed like this : |
| 484 | * |
| 485 | * b0 b1 b2 b3 b4 b5..b8 |
| 486 | * +----------+---------+--------+----+----+----------------------+ |
| 487 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 488 | * +----------+---------+--------+----+----+----------------------+ |
| 489 | * |
| 490 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 491 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 492 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 493 | * the word, and only one extra read is needed to fetch len[16:23]. |
| 494 | * Returns zero if some bytes are missing, otherwise non-zero on success. |
| 495 | */ |
| 496 | static int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h) |
| 497 | { |
| 498 | uint64_t w; |
| 499 | |
| 500 | if (b->i < 9) |
| 501 | return 0; |
| 502 | |
| 503 | w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data); |
| 504 | h->len = *b->p << 16; |
| 505 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 506 | h->ff = w >> 32; |
| 507 | h->ft = w >> 40; |
| 508 | h->len += w >> 48; |
| 509 | return 1; |
| 510 | } |
| 511 | |
| 512 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 513 | * h2_peek_frame_hdr() above. |
| 514 | */ |
| 515 | static inline void h2_skip_frame_hdr(struct buffer *b) |
| 516 | { |
| 517 | bi_del(b, 9); |
| 518 | } |
| 519 | |
| 520 | /* same as above, automatically advances the buffer on success */ |
| 521 | static inline int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h) |
| 522 | { |
| 523 | int ret; |
| 524 | |
| 525 | ret = h2_peek_frame_hdr(b, h); |
| 526 | if (ret > 0) |
| 527 | h2_skip_frame_hdr(b); |
| 528 | return ret; |
| 529 | } |
| 530 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 531 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 532 | * case of memory allocation error. |
| 533 | */ |
| 534 | static struct h2s *h2c_stream_new(struct h2c *h2c, int id) |
| 535 | { |
| 536 | struct conn_stream *cs; |
| 537 | struct h2s *h2s; |
| 538 | |
| 539 | h2s = pool_alloc2(pool2_h2s); |
| 540 | if (!h2s) |
| 541 | goto out; |
| 542 | |
| 543 | h2s->h2c = h2c; |
| 544 | h2s->mws = h2c->miw; |
| 545 | h2s->flags = H2_SF_NONE; |
| 546 | h2s->errcode = H2_ERR_NO_ERROR; |
| 547 | h2s->st = H2_SS_IDLE; |
| 548 | h1m_init(&h2s->req); |
| 549 | h1m_init(&h2s->res); |
| 550 | h2s->by_id.key = h2s->id = id; |
| 551 | h2c->max_id = id; |
| 552 | LIST_INIT(&h2s->list); |
| 553 | |
| 554 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
| 555 | |
| 556 | cs = cs_new(h2c->conn); |
| 557 | if (!cs) |
| 558 | goto out_close; |
| 559 | |
| 560 | h2s->cs = cs; |
| 561 | cs->ctx = h2s; |
| 562 | |
| 563 | if (stream_create_from_cs(cs) < 0) |
| 564 | goto out_free_cs; |
| 565 | |
| 566 | /* OK done, the stream lives its own life now */ |
| 567 | return h2s; |
| 568 | |
| 569 | out_free_cs: |
| 570 | cs_free(cs); |
| 571 | out_close: |
| 572 | eb32_delete(&h2s->by_id); |
| 573 | pool_free2(pool2_h2s, h2s); |
| 574 | h2s = NULL; |
| 575 | out: |
| 576 | return h2s; |
| 577 | } |
| 578 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 579 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 580 | /*********************************************************/ |
| 581 | /* functions below are I/O callbacks from the connection */ |
| 582 | /*********************************************************/ |
| 583 | |
| 584 | /* callback called on recv event by the connection handler */ |
| 585 | static void h2_recv(struct connection *conn) |
| 586 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 587 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 588 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 589 | int max; |
| 590 | |
| 591 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 592 | return; |
| 593 | |
| 594 | if (h2c->flags & H2_CF_DEM_BLOCK_ANY) |
| 595 | return; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 596 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 597 | buf = h2_get_dbuf(h2c); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 598 | if (!buf) { |
| 599 | h2c->flags |= H2_CF_DEM_DALLOC; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 600 | return; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 601 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 602 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 603 | /* note: buf->o == 0 */ |
| 604 | max = buf->size - buf->i; |
| 605 | if (!max) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 606 | h2c->flags |= H2_CF_DEM_DFULL; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 607 | return; |
| 608 | } |
| 609 | |
| 610 | conn->xprt->rcv_buf(conn, buf, max); |
| 611 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 612 | return; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 613 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 614 | if (!buf->i) { |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 615 | h2_release_dbuf(h2c); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 616 | return; |
| 617 | } |
| 618 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 619 | if (buf->i == buf->size) |
| 620 | h2c->flags |= H2_CF_DEM_DFULL; |
| 621 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 622 | /* FIXME: should we try to process streams here instead of doing it in ->wake ? */ |
| 623 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 624 | /* after streams have been processed, we should have made some room */ |
| 625 | if (buf->i != buf->size) |
| 626 | h2c->flags &= ~H2_CF_DEM_DFULL; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 627 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /* callback called on send event by the connection handler */ |
| 631 | static void h2_send(struct connection *conn) |
| 632 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 633 | struct h2c *h2c = conn->mux_ctx; |
| 634 | |
| 635 | /* FIXME: should we try to process pending streams here instead of doing it in ->wake ? */ |
| 636 | |
| 637 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 638 | return; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 639 | |
| 640 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 641 | /* a handshake was requested */ |
| 642 | return; |
| 643 | } |
| 644 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 645 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 646 | /* output closed, nothing to send, clear the buffer to release it */ |
| 647 | h2c->mbuf->o = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* pending response data, we need to try to send or subscribe to |
| 651 | * writes. The snd_buf() function takes a "flags" argument which |
| 652 | * may be made of a combination of CO_SFL_MSG_MORE to indicate |
| 653 | * that more data immediately comes and CO_SFL_STREAMER to |
| 654 | * indicate that the connection is streaming lots of data (used |
| 655 | * to increase TLS record size at the expense of latency). The |
| 656 | * former could be sent any time there's a buffer full flag, as |
| 657 | * it indicates at least one stream attempted to send and failed |
| 658 | * so there are pending data. And alternative would be to set it |
| 659 | * as long as there's an active stream but that would be |
| 660 | * problematic for ACKs. The latter should possibly not be set |
| 661 | * for now. |
| 662 | */ |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 663 | if (conn->xprt->snd_buf(conn, h2c->mbuf, 0) > 0) |
| 664 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 665 | |
| 666 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 667 | return; |
| 668 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 669 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 670 | /* call the wake up function of all streams attached to the connection */ |
| 671 | static void h2_wake_all_streams(struct h2c *h2c) |
| 672 | { |
| 673 | struct eb32_node *node; |
| 674 | struct h2s *h2s; |
| 675 | unsigned int flags = 0; |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 676 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 677 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
| 678 | flags |= CS_FL_ERROR; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 679 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 680 | if (conn_xprt_read0_pending(h2c->conn)) |
| 681 | flags |= CS_FL_EOS; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 682 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 683 | node = eb32_first(&h2c->streams_by_id); |
| 684 | while (node) { |
| 685 | h2s = container_of(node, struct h2s, by_id); |
| 686 | node = eb32_next(node); |
| 687 | if (h2s->cs) { |
| 688 | h2s->cs->flags |= flags; |
| 689 | /* recv is used to force to detect CS_FL_EOS that wake() |
| 690 | * doesn't handle in the stream int code. |
| 691 | */ |
| 692 | h2s->cs->data_cb->recv(h2s->cs); |
| 693 | h2s->cs->data_cb->wake(h2s->cs); |
| 694 | } |
| 695 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* callback called on any event by the connection handler. |
| 699 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 700 | * destruction of the connection (which normally doesn not happen in h2). |
| 701 | */ |
| 702 | static int h2_wake(struct connection *conn) |
| 703 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 704 | struct h2c *h2c = conn->mux_ctx; |
| 705 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 706 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 707 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 708 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 709 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 710 | h2_wake_all_streams(h2c); |
| 711 | |
| 712 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 713 | /* no more stream, kill the connection now */ |
| 714 | h2_release(conn); |
| 715 | return -1; |
| 716 | } |
| 717 | else { |
| 718 | /* some streams still there, we need to signal them all and |
| 719 | * wait for their departure. |
| 720 | */ |
| 721 | __conn_xprt_stop_recv(conn); |
| 722 | __conn_xprt_stop_send(conn); |
| 723 | return 0; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | if (!h2c->dbuf->i) |
| 728 | h2_release_dbuf(h2c); |
| 729 | |
| 730 | /* stop being notified of incoming data if we can't process them */ |
| 731 | if (h2c->st0 >= H2_CS_ERROR || |
| 732 | (h2c->flags & H2_CF_DEM_BLOCK_ANY) || conn_xprt_read0_pending(conn)) { |
| 733 | /* FIXME: we should clear a read timeout here */ |
| 734 | __conn_xprt_stop_recv(conn); |
| 735 | } |
| 736 | else { |
| 737 | /* FIXME: we should (re-)arm a read timeout here */ |
| 738 | __conn_xprt_want_recv(conn); |
| 739 | } |
| 740 | |
| 741 | /* adjust output polling */ |
Willy Tarreau | 5160683 | 2017-10-17 15:30:07 +0200 | [diff] [blame^] | 742 | if (!(conn->flags & CO_FL_SOCK_WR_SH) && |
| 743 | (h2c->st0 == H2_CS_ERROR || |
| 744 | h2c->mbuf->o || |
| 745 | (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) || |
| 746 | (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 747 | /* FIXME: we should (re-)arm a send timeout here */ |
| 748 | __conn_xprt_want_send(conn); |
| 749 | } |
| 750 | else { |
| 751 | /* FIXME: we should clear a send timeout here */ |
| 752 | h2_release_mbuf(h2c); |
| 753 | __conn_xprt_stop_send(conn); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 754 | } |
| 755 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | /*******************************************/ |
| 760 | /* functions below are used by the streams */ |
| 761 | /*******************************************/ |
| 762 | |
| 763 | /* |
| 764 | * Attach a new stream to a connection |
| 765 | * (Used for outgoing connections) |
| 766 | */ |
| 767 | static struct conn_stream *h2_attach(struct connection *conn) |
| 768 | { |
| 769 | return NULL; |
| 770 | } |
| 771 | |
| 772 | /* callback used to update the mux's polling flags after changing a cs' status. |
| 773 | * The caller (cs_update_mux_polling) will take care of propagating any changes |
| 774 | * to the transport layer. |
| 775 | */ |
| 776 | static void h2_update_poll(struct conn_stream *cs) |
| 777 | { |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * Detach the stream from the connection and possibly release the connection. |
| 782 | */ |
| 783 | static void h2_detach(struct conn_stream *cs) |
| 784 | { |
| 785 | } |
| 786 | |
| 787 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 788 | { |
| 789 | } |
| 790 | |
| 791 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 792 | { |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * Called from the upper layer, to get more data |
| 797 | */ |
| 798 | static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count) |
| 799 | { |
| 800 | /* FIXME: not handled for now */ |
| 801 | cs->flags |= CS_FL_ERROR; |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | /* Called from the upper layer, to send data */ |
| 806 | static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags) |
| 807 | { |
| 808 | /* FIXME: not handled for now */ |
| 809 | cs->flags |= CS_FL_ERROR; |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | |
| 814 | /*******************************************************/ |
| 815 | /* functions below are dedicated to the config parsers */ |
| 816 | /*******************************************************/ |
| 817 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 818 | /* config parser for global "tune.h2.header-table-size" */ |
| 819 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 820 | struct proxy *defpx, const char *file, int line, |
| 821 | char **err) |
| 822 | { |
| 823 | if (too_many_args(1, args, err, NULL)) |
| 824 | return -1; |
| 825 | |
| 826 | h2_settings_header_table_size = atoi(args[1]); |
| 827 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 828 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 829 | return -1; |
| 830 | } |
| 831 | return 0; |
| 832 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 833 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 834 | /* config parser for global "tune.h2.initial-window-size" */ |
| 835 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 836 | struct proxy *defpx, const char *file, int line, |
| 837 | char **err) |
| 838 | { |
| 839 | if (too_many_args(1, args, err, NULL)) |
| 840 | return -1; |
| 841 | |
| 842 | h2_settings_initial_window_size = atoi(args[1]); |
| 843 | if (h2_settings_initial_window_size < 0) { |
| 844 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 845 | return -1; |
| 846 | } |
| 847 | return 0; |
| 848 | } |
| 849 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 850 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 851 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 852 | struct proxy *defpx, const char *file, int line, |
| 853 | char **err) |
| 854 | { |
| 855 | if (too_many_args(1, args, err, NULL)) |
| 856 | return -1; |
| 857 | |
| 858 | h2_settings_max_concurrent_streams = atoi(args[1]); |
| 859 | if (h2_settings_max_concurrent_streams < 0) { |
| 860 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 861 | return -1; |
| 862 | } |
| 863 | return 0; |
| 864 | } |
| 865 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 866 | |
| 867 | /****************************************/ |
| 868 | /* MUX initialization and instanciation */ |
| 869 | /***************************************/ |
| 870 | |
| 871 | /* The mux operations */ |
| 872 | const struct mux_ops h2_ops = { |
| 873 | .init = h2_init, |
| 874 | .recv = h2_recv, |
| 875 | .send = h2_send, |
| 876 | .wake = h2_wake, |
| 877 | .update_poll = h2_update_poll, |
| 878 | .rcv_buf = h2_rcv_buf, |
| 879 | .snd_buf = h2_snd_buf, |
| 880 | .attach = h2_attach, |
| 881 | .detach = h2_detach, |
| 882 | .shutr = h2_shutr, |
| 883 | .shutw = h2_shutw, |
| 884 | .release = h2_release, |
| 885 | .name = "H2", |
| 886 | }; |
| 887 | |
| 888 | /* ALPN selection : this mux registers ALPN tolen "h2" */ |
| 889 | static struct alpn_mux_list alpn_mux_h2 = |
| 890 | { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops }; |
| 891 | |
| 892 | /* config keyword parsers */ |
| 893 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 894 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 895 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 896 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 897 | { 0, NULL, NULL } |
| 898 | }}; |
| 899 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 900 | static void __h2_deinit(void) |
| 901 | { |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 902 | pool_destroy2(pool2_h2s); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 903 | pool_destroy2(pool2_h2c); |
| 904 | } |
| 905 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 906 | __attribute__((constructor)) |
| 907 | static void __h2_init(void) |
| 908 | { |
| 909 | alpn_register_mux(&alpn_mux_h2); |
| 910 | cfg_register_keywords(&cfg_kws); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 911 | hap_register_post_deinit(__h2_deinit); |
| 912 | pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED); |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 913 | pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 914 | } |