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