blob: 4517b2fe455ad391ddd75b280d42688be2342e88 [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
31/* H2 connection state, in h2c->st0 */
32enum h2_cs {
33 H2_CS_PREFACE, // init done, waiting for connection preface
34 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
35 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
36 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
37 H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame
38 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
39 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
40 H2_CS_ENTRIES // must be last
41} __attribute__((packed));
42
43/* H2 connection descriptor */
44struct h2c {
45 struct connection *conn;
46
47 enum h2_cs st0; /* mux state */
48 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
49
50 /* 16 bit hole here */
51 uint32_t flags; /* connection flags: H2_CF_* */
52 int32_t max_id; /* highest ID known on this connection, <0 before preface */
53 uint32_t rcvd_c; /* newly received data to ACK for the connection */
54 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
55
56 /* states for the demux direction */
57 struct hpack_dht *ddht; /* demux dynamic header table */
58 struct buffer *dbuf; /* demux buffer */
59
60 int32_t dsi; /* demux stream ID (<0 = idle) */
61 int32_t dfl; /* demux frame length (if dsi >= 0) */
62 int8_t dft; /* demux frame type (if dsi >= 0) */
63 int8_t dff; /* demux frame flags (if dsi >= 0) */
64 /* 16 bit hole here */
65 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
66
67 /* states for the mux direction */
68 struct buffer *mbuf; /* mux buffer */
69 int32_t msi; /* mux stream ID (<0 = idle) */
70 int32_t mfl; /* mux frame length (if dsi >= 0) */
71 int8_t mft; /* mux frame type (if dsi >= 0) */
72 int8_t mff; /* mux frame flags (if dsi >= 0) */
73 /* 16 bit hole here */
74 int32_t miw; /* mux initial window size for all new streams */
75 int32_t mws; /* mux window size. Can be negative. */
76 int32_t mfs; /* mux's max frame size */
77
78 struct eb_root streams_by_id; /* all active streams by their ID */
79 struct list send_list; /* list of blocked streams requesting to send */
80 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau35dbd5d2017-09-22 09:13:49 +020081 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020082};
83
Willy Tarreau18312642017-10-11 07:57:07 +020084/* H2 stream state, in h2s->st */
85enum h2_ss {
86 H2_SS_IDLE = 0, // idle
87 H2_SS_RLOC, // reserved(local)
88 H2_SS_RREM, // reserved(remote)
89 H2_SS_OPEN, // open
90 H2_SS_HREM, // half-closed(remote)
91 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +020092 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
93 H2_SS_RESET, // closed after sending RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +020094 H2_SS_CLOSED, // closed
95 H2_SS_ENTRIES // must be last
96} __attribute__((packed));
97
98/* HTTP/2 stream flags (32 bit), in h2s->flags */
99#define H2_SF_NONE 0x00000000
100#define H2_SF_ES_RCVD 0x00000001
101#define H2_SF_ES_SENT 0x00000002
102
103#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
104#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
105
106/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
107 * it is being processed in the internal HTTP representation (H1 for now).
108 */
109struct h2s {
110 struct conn_stream *cs;
111 struct h2c *h2c;
112 struct h1m req, res; /* request and response parser state for H1 */
113 struct eb32_node by_id; /* place in h2c's streams_by_id */
114 struct list list; /* position in active/blocked lists if blocked>0 */
115 int32_t id; /* stream ID */
116 uint32_t flags; /* H2_SF_* */
117 int mws; /* mux window size for this stream */
118 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
119 enum h2_ss st;
120};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200121
Willy Tarreauc6405142017-09-21 20:23:50 +0200122/* descriptor for an h2 frame header */
123struct h2_fh {
124 uint32_t len; /* length, host order, 24 bits */
125 uint32_t sid; /* stream id, host order, 31 bits */
126 uint8_t ft; /* frame type */
127 uint8_t ff; /* frame flags */
128};
129
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200130/* a few settings from the global section */
131static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200132static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200133static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200134
135
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200136/*****************************************************/
137/* functions below are for dynamic buffer management */
138/*****************************************************/
139
140/* re-enables receiving on mux <target> after a buffer was allocated. It returns
141 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
142 * if it's impossible to wake up and we prefer to be woken up later.
143 */
144static int h2_dbuf_available(void *target)
145{
146 struct h2c *h2c = target;
147
148 /* take the buffer now as we'll get scheduled waiting for ->wake() */
149 if (b_alloc_margin(&h2c->dbuf, 0)) {
150 conn_xprt_want_recv(h2c->conn);
151 return 1;
152 }
153 return 0;
154}
155
156static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
157{
158 struct buffer *buf = NULL;
159
160 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
161 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
162 h2c->dbuf_wait.target = h2c->conn;
163 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
164 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
165 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
166 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
167 __conn_xprt_stop_recv(h2c->conn);
168 }
169 return buf;
170}
171
172static inline void h2_release_dbuf(struct h2c *h2c)
173{
174 if (h2c->dbuf->size) {
175 b_free(&h2c->dbuf);
176 offer_buffers(h2c->dbuf_wait.target,
177 tasks_run_queue + applets_active_queue);
178 }
179}
180
181
Willy Tarreau62f52692017-10-08 23:01:42 +0200182/*****************************************************************/
183/* functions below are dedicated to the mux setup and management */
184/*****************************************************************/
185
Willy Tarreau32218eb2017-09-22 08:07:25 +0200186/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
187static int h2c_frt_init(struct connection *conn)
188{
189 struct h2c *h2c;
190
191 h2c = pool_alloc2(pool2_h2c);
192 if (!h2c)
193 goto fail;
194
195 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
196 if (!h2c->ddht)
197 goto fail;
198
199 /* Initialise the context. */
200 h2c->st0 = H2_CS_PREFACE;
201 h2c->conn = conn;
202 h2c->max_id = -1;
203 h2c->errcode = H2_ERR_NO_ERROR;
204 h2c->flags = H2_CF_NONE;
205 h2c->rcvd_c = 0;
206 h2c->rcvd_s = 0;
207
208 h2c->dbuf = &buf_empty;
209 h2c->dsi = -1;
210 h2c->msi = -1;
211 h2c->last_sid = -1;
212
213 h2c->mbuf = &buf_empty;
214 h2c->miw = 65535; /* mux initial window size */
215 h2c->mws = 65535; /* mux window size */
216 h2c->mfs = 16384; /* initial max frame size */
217 h2c->streams_by_id = EB_ROOT_UNIQUE;
218 LIST_INIT(&h2c->send_list);
219 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200220 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200221 conn->mux_ctx = h2c;
222
223 conn_xprt_want_recv(conn);
224 /* mux->wake will be called soon to complete the operation */
225 return 0;
226 fail:
227 pool_free2(pool2_h2c, h2c);
228 return -1;
229}
230
Willy Tarreau62f52692017-10-08 23:01:42 +0200231/* Initialize the mux once it's attached. For outgoing connections, the context
232 * is already initialized before installing the mux, so we detect incoming
233 * connections from the fact that the context is still NULL. Returns < 0 on
234 * error.
235 */
236static int h2_init(struct connection *conn)
237{
238 if (conn->mux_ctx) {
239 /* we don't support outgoing connections for now */
240 return -1;
241 }
242
Willy Tarreau32218eb2017-09-22 08:07:25 +0200243 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200244}
245
246/* release function for a connection. This one should be called to free all
247 * resources allocated to the mux.
248 */
249static void h2_release(struct connection *conn)
250{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200251 struct h2c *h2c = conn->mux_ctx;
252
253 LIST_DEL(&conn->list);
254
255 if (h2c) {
256 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200257 h2_release_dbuf(h2c);
258 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
259 LIST_DEL(&h2c->dbuf_wait.list);
260 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200261 pool_free2(pool2_h2c, h2c);
262 }
263
264 conn->mux = NULL;
265 conn->mux_ctx = NULL;
266
267 conn_stop_tracking(conn);
268 conn_full_close(conn);
269 if (conn->destroy_cb)
270 conn->destroy_cb(conn);
271 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200272}
273
274
275/*********************************************************/
276/* functions below are I/O callbacks from the connection */
277/*********************************************************/
278
279/* callback called on recv event by the connection handler */
280static void h2_recv(struct connection *conn)
281{
Willy Tarreaua2af5122017-10-09 11:56:46 +0200282 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200283 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +0200284 int max;
285
286 if (conn->flags & CO_FL_ERROR)
287 goto error;
288
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200289 buf = h2_get_dbuf(h2c);
290 if (!buf)
291 return;
292
Willy Tarreaua2af5122017-10-09 11:56:46 +0200293 /* note: buf->o == 0 */
294 max = buf->size - buf->i;
295 if (!max) {
296 /* FIXME: buffer full, add a flag, stop polling and wait */
297 __conn_xprt_stop_recv(conn);
298 return;
299 }
300
301 conn->xprt->rcv_buf(conn, buf, max);
302 if (conn->flags & CO_FL_ERROR)
303 goto error;
304
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200305 if (!buf->i)
306 h2_release_dbuf(h2c);
307
Willy Tarreaua2af5122017-10-09 11:56:46 +0200308 if (buf->i == buf->size) {
309 /* buffer now full */
310 __conn_xprt_stop_recv(conn);
311 return;
312 }
313
314 /* FIXME: should we try to process streams here instead of doing it in ->wake ? */
315
316 if (conn_xprt_read0_pending(conn))
317 __conn_xprt_stop_recv(conn);
318 return;
319
320 error:
321 __conn_xprt_stop_recv(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200322}
323
324/* callback called on send event by the connection handler */
325static void h2_send(struct connection *conn)
326{
Willy Tarreaua2af5122017-10-09 11:56:46 +0200327 struct h2c *h2c = conn->mux_ctx;
328
329 /* FIXME: should we try to process pending streams here instead of doing it in ->wake ? */
330
331 if (conn->flags & CO_FL_ERROR)
332 goto error;
333
334 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
335 /* a handshake was requested */
336 return;
337 }
338
339 if (!h2c->mbuf->o) {
340 /* nothing to send */
341 goto done;
342 }
343
344 if (conn->flags & CO_FL_SOCK_WR_SH) {
345 /* output closed, nothing to send, clear the buffer to release it */
346 h2c->mbuf->o = 0;
347 goto done;
348 }
349
350 /* pending response data, we need to try to send or subscribe to
351 * writes. The snd_buf() function takes a "flags" argument which
352 * may be made of a combination of CO_SFL_MSG_MORE to indicate
353 * that more data immediately comes and CO_SFL_STREAMER to
354 * indicate that the connection is streaming lots of data (used
355 * to increase TLS record size at the expense of latency). The
356 * former could be sent any time there's a buffer full flag, as
357 * it indicates at least one stream attempted to send and failed
358 * so there are pending data. And alternative would be to set it
359 * as long as there's an active stream but that would be
360 * problematic for ACKs. The latter should possibly not be set
361 * for now.
362 */
363 conn->xprt->snd_buf(conn, h2c->mbuf, 0);
364
365 if (conn->flags & CO_FL_ERROR)
366 goto error;
367
368 if (h2c->mbuf->o) {
369 /* incomplete send, the snd_buf callback has already updated
370 * the connection flags.
371 *
372 * FIXME: we should arm a send timeout here
373 */
374 __conn_xprt_want_send(conn);
375 return;
376 }
377
378 done:
379 /* FIXME: release the output buffer when empty or do it in ->wake() ? */
380 __conn_xprt_stop_send(conn);
381 return;
382
383 error:
384 /* FIXME: report an error somewhere in the mux */
385 __conn_xprt_stop_send(conn);
386 return;
Willy Tarreau62f52692017-10-08 23:01:42 +0200387}
388
389/* callback called on any event by the connection handler.
390 * It applies changes and returns zero, or < 0 if it wants immediate
391 * destruction of the connection (which normally doesn not happen in h2).
392 */
393static int h2_wake(struct connection *conn)
394{
Willy Tarreaua2af5122017-10-09 11:56:46 +0200395 struct h2c *h2c = conn->mux_ctx;
396
397 if ((conn->flags & CO_FL_ERROR) && eb_is_empty(&h2c->streams_by_id)) {
398 h2_release(conn);
399 return -1;
400 }
401
Willy Tarreau62f52692017-10-08 23:01:42 +0200402 return 0;
403}
404
405/*******************************************/
406/* functions below are used by the streams */
407/*******************************************/
408
409/*
410 * Attach a new stream to a connection
411 * (Used for outgoing connections)
412 */
413static struct conn_stream *h2_attach(struct connection *conn)
414{
415 return NULL;
416}
417
418/* callback used to update the mux's polling flags after changing a cs' status.
419 * The caller (cs_update_mux_polling) will take care of propagating any changes
420 * to the transport layer.
421 */
422static void h2_update_poll(struct conn_stream *cs)
423{
424}
425
426/*
427 * Detach the stream from the connection and possibly release the connection.
428 */
429static void h2_detach(struct conn_stream *cs)
430{
431}
432
433static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
434{
435}
436
437static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
438{
439}
440
441/*
442 * Called from the upper layer, to get more data
443 */
444static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
445{
446 /* FIXME: not handled for now */
447 cs->flags |= CS_FL_ERROR;
448 return 0;
449}
450
451/* Called from the upper layer, to send data */
452static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
453{
454 /* FIXME: not handled for now */
455 cs->flags |= CS_FL_ERROR;
456 return 0;
457}
458
459
460/*******************************************************/
461/* functions below are dedicated to the config parsers */
462/*******************************************************/
463
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200464/* config parser for global "tune.h2.header-table-size" */
465static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
466 struct proxy *defpx, const char *file, int line,
467 char **err)
468{
469 if (too_many_args(1, args, err, NULL))
470 return -1;
471
472 h2_settings_header_table_size = atoi(args[1]);
473 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
474 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
475 return -1;
476 }
477 return 0;
478}
Willy Tarreau62f52692017-10-08 23:01:42 +0200479
Willy Tarreaue6baec02017-07-27 11:45:11 +0200480/* config parser for global "tune.h2.initial-window-size" */
481static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
482 struct proxy *defpx, const char *file, int line,
483 char **err)
484{
485 if (too_many_args(1, args, err, NULL))
486 return -1;
487
488 h2_settings_initial_window_size = atoi(args[1]);
489 if (h2_settings_initial_window_size < 0) {
490 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
491 return -1;
492 }
493 return 0;
494}
495
Willy Tarreau5242ef82017-07-27 11:47:28 +0200496/* config parser for global "tune.h2.max-concurrent-streams" */
497static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
498 struct proxy *defpx, const char *file, int line,
499 char **err)
500{
501 if (too_many_args(1, args, err, NULL))
502 return -1;
503
504 h2_settings_max_concurrent_streams = atoi(args[1]);
505 if (h2_settings_max_concurrent_streams < 0) {
506 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
507 return -1;
508 }
509 return 0;
510}
511
Willy Tarreau62f52692017-10-08 23:01:42 +0200512
513/****************************************/
514/* MUX initialization and instanciation */
515/***************************************/
516
517/* The mux operations */
518const struct mux_ops h2_ops = {
519 .init = h2_init,
520 .recv = h2_recv,
521 .send = h2_send,
522 .wake = h2_wake,
523 .update_poll = h2_update_poll,
524 .rcv_buf = h2_rcv_buf,
525 .snd_buf = h2_snd_buf,
526 .attach = h2_attach,
527 .detach = h2_detach,
528 .shutr = h2_shutr,
529 .shutw = h2_shutw,
530 .release = h2_release,
531 .name = "H2",
532};
533
534/* ALPN selection : this mux registers ALPN tolen "h2" */
535static struct alpn_mux_list alpn_mux_h2 =
536 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
537
538/* config keyword parsers */
539static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200540 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +0200541 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +0200542 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +0200543 { 0, NULL, NULL }
544}};
545
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200546static void __h2_deinit(void)
547{
Willy Tarreau18312642017-10-11 07:57:07 +0200548 pool_destroy2(pool2_h2s);
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200549 pool_destroy2(pool2_h2c);
550}
551
Willy Tarreau62f52692017-10-08 23:01:42 +0200552__attribute__((constructor))
553static void __h2_init(void)
554{
555 alpn_register_mux(&alpn_mux_h2);
556 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200557 hap_register_post_deinit(__h2_deinit);
558 pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
Willy Tarreau18312642017-10-11 07:57:07 +0200559 pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +0200560}