blob: ff9df381cf58619fa2bc56eb9b9736b8b3344f21 [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 Tarreau71681172017-10-23 14:39:06 +0200383
Willy Tarreau62f52692017-10-08 23:01:42 +0200384/*********************************************************/
385/* functions below are I/O callbacks from the connection */
386/*********************************************************/
387
388/* callback called on recv event by the connection handler */
389static void h2_recv(struct connection *conn)
390{
Willy Tarreaua2af5122017-10-09 11:56:46 +0200391 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200392 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +0200393 int max;
394
395 if (conn->flags & CO_FL_ERROR)
396 goto error;
397
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200398 buf = h2_get_dbuf(h2c);
399 if (!buf)
400 return;
401
Willy Tarreaua2af5122017-10-09 11:56:46 +0200402 /* note: buf->o == 0 */
403 max = buf->size - buf->i;
404 if (!max) {
405 /* FIXME: buffer full, add a flag, stop polling and wait */
406 __conn_xprt_stop_recv(conn);
407 return;
408 }
409
410 conn->xprt->rcv_buf(conn, buf, max);
411 if (conn->flags & CO_FL_ERROR)
412 goto error;
413
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200414 if (!buf->i)
415 h2_release_dbuf(h2c);
416
Willy Tarreaua2af5122017-10-09 11:56:46 +0200417 if (buf->i == buf->size) {
418 /* buffer now full */
419 __conn_xprt_stop_recv(conn);
420 return;
421 }
422
423 /* FIXME: should we try to process streams here instead of doing it in ->wake ? */
424
425 if (conn_xprt_read0_pending(conn))
426 __conn_xprt_stop_recv(conn);
427 return;
428
429 error:
430 __conn_xprt_stop_recv(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200431}
432
433/* callback called on send event by the connection handler */
434static void h2_send(struct connection *conn)
435{
Willy Tarreaua2af5122017-10-09 11:56:46 +0200436 struct h2c *h2c = conn->mux_ctx;
437
438 /* FIXME: should we try to process pending streams here instead of doing it in ->wake ? */
439
440 if (conn->flags & CO_FL_ERROR)
441 goto error;
442
443 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
444 /* a handshake was requested */
445 return;
446 }
447
448 if (!h2c->mbuf->o) {
449 /* nothing to send */
450 goto done;
451 }
452
453 if (conn->flags & CO_FL_SOCK_WR_SH) {
454 /* output closed, nothing to send, clear the buffer to release it */
455 h2c->mbuf->o = 0;
456 goto done;
457 }
458
459 /* pending response data, we need to try to send or subscribe to
460 * writes. The snd_buf() function takes a "flags" argument which
461 * may be made of a combination of CO_SFL_MSG_MORE to indicate
462 * that more data immediately comes and CO_SFL_STREAMER to
463 * indicate that the connection is streaming lots of data (used
464 * to increase TLS record size at the expense of latency). The
465 * former could be sent any time there's a buffer full flag, as
466 * it indicates at least one stream attempted to send and failed
467 * so there are pending data. And alternative would be to set it
468 * as long as there's an active stream but that would be
469 * problematic for ACKs. The latter should possibly not be set
470 * for now.
471 */
472 conn->xprt->snd_buf(conn, h2c->mbuf, 0);
473
474 if (conn->flags & CO_FL_ERROR)
475 goto error;
476
Willy Tarreau14398122017-09-22 14:26:04 +0200477 if (!h2c->mbuf->o)
478 h2_release_mbuf(h2c);
479
Willy Tarreaua2af5122017-10-09 11:56:46 +0200480 if (h2c->mbuf->o) {
481 /* incomplete send, the snd_buf callback has already updated
482 * the connection flags.
483 *
484 * FIXME: we should arm a send timeout here
485 */
486 __conn_xprt_want_send(conn);
487 return;
488 }
489
490 done:
491 /* FIXME: release the output buffer when empty or do it in ->wake() ? */
492 __conn_xprt_stop_send(conn);
493 return;
494
495 error:
496 /* FIXME: report an error somewhere in the mux */
497 __conn_xprt_stop_send(conn);
498 return;
Willy Tarreau62f52692017-10-08 23:01:42 +0200499}
500
501/* callback called on any event by the connection handler.
502 * It applies changes and returns zero, or < 0 if it wants immediate
503 * destruction of the connection (which normally doesn not happen in h2).
504 */
505static int h2_wake(struct connection *conn)
506{
Willy Tarreaua2af5122017-10-09 11:56:46 +0200507 struct h2c *h2c = conn->mux_ctx;
508
509 if ((conn->flags & CO_FL_ERROR) && eb_is_empty(&h2c->streams_by_id)) {
510 h2_release(conn);
511 return -1;
512 }
513
Willy Tarreau62f52692017-10-08 23:01:42 +0200514 return 0;
515}
516
517/*******************************************/
518/* functions below are used by the streams */
519/*******************************************/
520
521/*
522 * Attach a new stream to a connection
523 * (Used for outgoing connections)
524 */
525static struct conn_stream *h2_attach(struct connection *conn)
526{
527 return NULL;
528}
529
530/* callback used to update the mux's polling flags after changing a cs' status.
531 * The caller (cs_update_mux_polling) will take care of propagating any changes
532 * to the transport layer.
533 */
534static void h2_update_poll(struct conn_stream *cs)
535{
536}
537
538/*
539 * Detach the stream from the connection and possibly release the connection.
540 */
541static void h2_detach(struct conn_stream *cs)
542{
543}
544
545static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
546{
547}
548
549static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
550{
551}
552
553/*
554 * Called from the upper layer, to get more data
555 */
556static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
557{
558 /* FIXME: not handled for now */
559 cs->flags |= CS_FL_ERROR;
560 return 0;
561}
562
563/* Called from the upper layer, to send data */
564static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
565{
566 /* FIXME: not handled for now */
567 cs->flags |= CS_FL_ERROR;
568 return 0;
569}
570
571
572/*******************************************************/
573/* functions below are dedicated to the config parsers */
574/*******************************************************/
575
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200576/* config parser for global "tune.h2.header-table-size" */
577static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
578 struct proxy *defpx, const char *file, int line,
579 char **err)
580{
581 if (too_many_args(1, args, err, NULL))
582 return -1;
583
584 h2_settings_header_table_size = atoi(args[1]);
585 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
586 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
587 return -1;
588 }
589 return 0;
590}
Willy Tarreau62f52692017-10-08 23:01:42 +0200591
Willy Tarreaue6baec02017-07-27 11:45:11 +0200592/* config parser for global "tune.h2.initial-window-size" */
593static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
594 struct proxy *defpx, const char *file, int line,
595 char **err)
596{
597 if (too_many_args(1, args, err, NULL))
598 return -1;
599
600 h2_settings_initial_window_size = atoi(args[1]);
601 if (h2_settings_initial_window_size < 0) {
602 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
603 return -1;
604 }
605 return 0;
606}
607
Willy Tarreau5242ef82017-07-27 11:47:28 +0200608/* config parser for global "tune.h2.max-concurrent-streams" */
609static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
610 struct proxy *defpx, const char *file, int line,
611 char **err)
612{
613 if (too_many_args(1, args, err, NULL))
614 return -1;
615
616 h2_settings_max_concurrent_streams = atoi(args[1]);
617 if (h2_settings_max_concurrent_streams < 0) {
618 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
619 return -1;
620 }
621 return 0;
622}
623
Willy Tarreau62f52692017-10-08 23:01:42 +0200624
625/****************************************/
626/* MUX initialization and instanciation */
627/***************************************/
628
629/* The mux operations */
630const struct mux_ops h2_ops = {
631 .init = h2_init,
632 .recv = h2_recv,
633 .send = h2_send,
634 .wake = h2_wake,
635 .update_poll = h2_update_poll,
636 .rcv_buf = h2_rcv_buf,
637 .snd_buf = h2_snd_buf,
638 .attach = h2_attach,
639 .detach = h2_detach,
640 .shutr = h2_shutr,
641 .shutw = h2_shutw,
642 .release = h2_release,
643 .name = "H2",
644};
645
646/* ALPN selection : this mux registers ALPN tolen "h2" */
647static struct alpn_mux_list alpn_mux_h2 =
648 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
649
650/* config keyword parsers */
651static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200652 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +0200653 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +0200654 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +0200655 { 0, NULL, NULL }
656}};
657
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200658static void __h2_deinit(void)
659{
Willy Tarreau18312642017-10-11 07:57:07 +0200660 pool_destroy2(pool2_h2s);
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200661 pool_destroy2(pool2_h2c);
662}
663
Willy Tarreau62f52692017-10-08 23:01:42 +0200664__attribute__((constructor))
665static void __h2_init(void)
666{
667 alpn_register_mux(&alpn_mux_h2);
668 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200669 hap_register_post_deinit(__h2_deinit);
670 pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
Willy Tarreau18312642017-10-11 07:57:07 +0200671 pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +0200672}