blob: 7cdf31c3251c1c571db3411ae12624e2e1218562 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
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 Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
16#include <common/hpack-tbl.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020017#include <common/net_helper.h>
Willy Tarreau35dbd5d2017-09-22 09:13:49 +020018#include <proto/applet.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020019#include <proto/connection.h>
20#include <proto/stream.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020021#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020022
23
Willy Tarreau5ab6b572017-09-22 08:05:00 +020024/* the h2c connection pool */
25static struct pool_head *pool2_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020026/* the h2s stream pool */
27static struct pool_head *pool2_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020028
29/* Connection flags (32 bit), in h2c->flags */
30#define H2_CF_NONE 0x00000000
31
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020032/* 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 Tarreau5ab6b572017-09-22 08:05:00 +020046/* H2 connection state, in h2c->st0 */
47enum 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 */
59struct 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 Tarreau35dbd5d2017-09-22 09:13:49 +020096 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +020097 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098};
99
Willy Tarreau18312642017-10-11 07:57:07 +0200100/* H2 stream state, in h2s->st */
101enum 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 Tarreau96060ba2017-10-16 18:34:34 +0200108 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
109 H2_SS_RESET, // closed after sending RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200110 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 Tarreau2e5b60e2017-09-25 11:49:03 +0200122/* 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 Tarreau18312642017-10-11 07:57:07 +0200129/* 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 */
132struct 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 Tarreau5ab6b572017-09-22 08:05:00 +0200144
Willy Tarreauc6405142017-09-21 20:23:50 +0200145/* descriptor for an h2 frame header */
146struct 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 Tarreaufe20e5b2017-07-27 11:42:14 +0200153/* a few settings from the global section */
154static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200155static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200156static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200157
158
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200159/*****************************************************/
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 */
167static 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
179static 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
195static 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 Tarreau14398122017-09-22 14:26:04 +0200204/* 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 */
208static 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
224static 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
245static 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 Tarreau35dbd5d2017-09-22 09:13:49 +0200254
Willy Tarreau62f52692017-10-08 23:01:42 +0200255/*****************************************************************/
256/* functions below are dedicated to the mux setup and management */
257/*****************************************************************/
258
Willy Tarreau32218eb2017-09-22 08:07:25 +0200259/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
260static 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 Tarreau35dbd5d2017-09-22 09:13:49 +0200293 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200294 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200295 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 Tarreau62f52692017-10-08 23:01:42 +0200305/* 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 */
310static 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 Tarreau32218eb2017-09-22 08:07:25 +0200317 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200318}
319
320/* release function for a connection. This one should be called to free all
321 * resources allocated to the mux.
322 */
323static void h2_release(struct connection *conn)
324{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200325 struct h2c *h2c = conn->mux_ctx;
326
327 LIST_DEL(&conn->list);
328
329 if (h2c) {
330 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200331 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 Tarreau14398122017-09-22 14:26:04 +0200335
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 Tarreau32218eb2017-09-22 08:07:25 +0200341 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 Tarreau62f52692017-10-08 23:01:42 +0200352}
353
354
Willy Tarreau71681172017-10-23 14:39:06 +0200355/******************************************************/
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 */
360static inline int h2s_id(const struct h2s *h2s)
361{
362 return h2s ? h2s->id : 0;
363}
364
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200365/* returns true of the mux is currently busy as seen from stream <h2s> */
366static 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 Tarreau741d6df2017-10-17 08:00:59 +0200377/* marks an error on the connection */
378static 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 Tarreau2e43f082017-10-17 08:03:59 +0200384/* marks an error on the stream */
385static 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 Tarreaue4820742017-07-27 13:37:23 +0200395/* writes the 24-bit frame size <len> at address <frame> */
396static 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 Tarreau71681172017-10-23 14:39:06 +0200404
Willy Tarreau62f52692017-10-08 23:01:42 +0200405/*********************************************************/
406/* functions below are I/O callbacks from the connection */
407/*********************************************************/
408
409/* callback called on recv event by the connection handler */
410static void h2_recv(struct connection *conn)
411{
Willy Tarreaua2af5122017-10-09 11:56:46 +0200412 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200413 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +0200414 int max;
415
416 if (conn->flags & CO_FL_ERROR)
417 goto error;
418
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200419 buf = h2_get_dbuf(h2c);
420 if (!buf)
421 return;
422
Willy Tarreaua2af5122017-10-09 11:56:46 +0200423 /* 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 Tarreau35dbd5d2017-09-22 09:13:49 +0200435 if (!buf->i)
436 h2_release_dbuf(h2c);
437
Willy Tarreaua2af5122017-10-09 11:56:46 +0200438 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 Tarreau62f52692017-10-08 23:01:42 +0200452}
453
454/* callback called on send event by the connection handler */
455static void h2_send(struct connection *conn)
456{
Willy Tarreaua2af5122017-10-09 11:56:46 +0200457 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 Tarreau14398122017-09-22 14:26:04 +0200498 if (!h2c->mbuf->o)
499 h2_release_mbuf(h2c);
500
Willy Tarreaua2af5122017-10-09 11:56:46 +0200501 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 Tarreau62f52692017-10-08 23:01:42 +0200520}
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 */
526static int h2_wake(struct connection *conn)
527{
Willy Tarreaua2af5122017-10-09 11:56:46 +0200528 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 Tarreau62f52692017-10-08 23:01:42 +0200535 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 */
546static 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 */
555static 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 */
562static void h2_detach(struct conn_stream *cs)
563{
564}
565
566static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
567{
568}
569
570static 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 */
577static 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 */
585static 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 Tarreaufe20e5b2017-07-27 11:42:14 +0200597/* config parser for global "tune.h2.header-table-size" */
598static 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 Tarreau62f52692017-10-08 23:01:42 +0200612
Willy Tarreaue6baec02017-07-27 11:45:11 +0200613/* config parser for global "tune.h2.initial-window-size" */
614static 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 Tarreau5242ef82017-07-27 11:47:28 +0200629/* config parser for global "tune.h2.max-concurrent-streams" */
630static 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 Tarreau62f52692017-10-08 23:01:42 +0200645
646/****************************************/
647/* MUX initialization and instanciation */
648/***************************************/
649
650/* The mux operations */
651const 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" */
668static struct alpn_mux_list alpn_mux_h2 =
669 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
670
671/* config keyword parsers */
672static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200673 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +0200674 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +0200675 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +0200676 { 0, NULL, NULL }
677}};
678
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200679static void __h2_deinit(void)
680{
Willy Tarreau18312642017-10-11 07:57:07 +0200681 pool_destroy2(pool2_h2s);
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200682 pool_destroy2(pool2_h2c);
683}
684
Willy Tarreau62f52692017-10-08 23:01:42 +0200685__attribute__((constructor))
686static void __h2_init(void)
687{
688 alpn_register_mux(&alpn_mux_h2);
689 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200690 hap_register_post_deinit(__h2_deinit);
691 pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
Willy Tarreau18312642017-10-11 07:57:07 +0200692 pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +0200693}