blob: fa8f24a04ea4f4cf86141957d0aab1f27204d2d5 [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 Tarreau62f52692017-10-08 23:01:42 +020017#include <proto/connection.h>
18#include <proto/stream.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020020
21
Willy Tarreau5ab6b572017-09-22 08:05:00 +020022/* the h2c connection pool */
23static struct pool_head *pool2_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020024/* the h2s stream pool */
25static struct pool_head *pool2_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020026
27/* Connection flags (32 bit), in h2c->flags */
28#define H2_CF_NONE 0x00000000
29
30/* H2 connection state, in h2c->st0 */
31enum h2_cs {
32 H2_CS_PREFACE, // init done, waiting for connection preface
33 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
34 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
35 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
36 H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame
37 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
38 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
39 H2_CS_ENTRIES // must be last
40} __attribute__((packed));
41
42/* H2 connection descriptor */
43struct h2c {
44 struct connection *conn;
45
46 enum h2_cs st0; /* mux state */
47 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
48
49 /* 16 bit hole here */
50 uint32_t flags; /* connection flags: H2_CF_* */
51 int32_t max_id; /* highest ID known on this connection, <0 before preface */
52 uint32_t rcvd_c; /* newly received data to ACK for the connection */
53 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
54
55 /* states for the demux direction */
56 struct hpack_dht *ddht; /* demux dynamic header table */
57 struct buffer *dbuf; /* demux buffer */
58
59 int32_t dsi; /* demux stream ID (<0 = idle) */
60 int32_t dfl; /* demux frame length (if dsi >= 0) */
61 int8_t dft; /* demux frame type (if dsi >= 0) */
62 int8_t dff; /* demux frame flags (if dsi >= 0) */
63 /* 16 bit hole here */
64 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
65
66 /* states for the mux direction */
67 struct buffer *mbuf; /* mux buffer */
68 int32_t msi; /* mux stream ID (<0 = idle) */
69 int32_t mfl; /* mux frame length (if dsi >= 0) */
70 int8_t mft; /* mux frame type (if dsi >= 0) */
71 int8_t mff; /* mux frame flags (if dsi >= 0) */
72 /* 16 bit hole here */
73 int32_t miw; /* mux initial window size for all new streams */
74 int32_t mws; /* mux window size. Can be negative. */
75 int32_t mfs; /* mux's max frame size */
76
77 struct eb_root streams_by_id; /* all active streams by their ID */
78 struct list send_list; /* list of blocked streams requesting to send */
79 struct list fctl_list; /* list of streams blocked by connection's fctl */
80};
81
Willy Tarreau18312642017-10-11 07:57:07 +020082/* H2 stream state, in h2s->st */
83enum h2_ss {
84 H2_SS_IDLE = 0, // idle
85 H2_SS_RLOC, // reserved(local)
86 H2_SS_RREM, // reserved(remote)
87 H2_SS_OPEN, // open
88 H2_SS_HREM, // half-closed(remote)
89 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +020090 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
91 H2_SS_RESET, // closed after sending RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +020092 H2_SS_CLOSED, // closed
93 H2_SS_ENTRIES // must be last
94} __attribute__((packed));
95
96/* HTTP/2 stream flags (32 bit), in h2s->flags */
97#define H2_SF_NONE 0x00000000
98#define H2_SF_ES_RCVD 0x00000001
99#define H2_SF_ES_SENT 0x00000002
100
101#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
102#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
103
104/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
105 * it is being processed in the internal HTTP representation (H1 for now).
106 */
107struct h2s {
108 struct conn_stream *cs;
109 struct h2c *h2c;
110 struct h1m req, res; /* request and response parser state for H1 */
111 struct eb32_node by_id; /* place in h2c's streams_by_id */
112 struct list list; /* position in active/blocked lists if blocked>0 */
113 int32_t id; /* stream ID */
114 uint32_t flags; /* H2_SF_* */
115 int mws; /* mux window size for this stream */
116 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
117 enum h2_ss st;
118};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119
Willy Tarreauc6405142017-09-21 20:23:50 +0200120/* descriptor for an h2 frame header */
121struct h2_fh {
122 uint32_t len; /* length, host order, 24 bits */
123 uint32_t sid; /* stream id, host order, 31 bits */
124 uint8_t ft; /* frame type */
125 uint8_t ff; /* frame flags */
126};
127
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200128/* a few settings from the global section */
129static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200130static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200131static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200132
133
Willy Tarreau62f52692017-10-08 23:01:42 +0200134/*****************************************************************/
135/* functions below are dedicated to the mux setup and management */
136/*****************************************************************/
137
Willy Tarreau32218eb2017-09-22 08:07:25 +0200138/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
139static int h2c_frt_init(struct connection *conn)
140{
141 struct h2c *h2c;
142
143 h2c = pool_alloc2(pool2_h2c);
144 if (!h2c)
145 goto fail;
146
147 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
148 if (!h2c->ddht)
149 goto fail;
150
151 /* Initialise the context. */
152 h2c->st0 = H2_CS_PREFACE;
153 h2c->conn = conn;
154 h2c->max_id = -1;
155 h2c->errcode = H2_ERR_NO_ERROR;
156 h2c->flags = H2_CF_NONE;
157 h2c->rcvd_c = 0;
158 h2c->rcvd_s = 0;
159
160 h2c->dbuf = &buf_empty;
161 h2c->dsi = -1;
162 h2c->msi = -1;
163 h2c->last_sid = -1;
164
165 h2c->mbuf = &buf_empty;
166 h2c->miw = 65535; /* mux initial window size */
167 h2c->mws = 65535; /* mux window size */
168 h2c->mfs = 16384; /* initial max frame size */
169 h2c->streams_by_id = EB_ROOT_UNIQUE;
170 LIST_INIT(&h2c->send_list);
171 LIST_INIT(&h2c->fctl_list);
172 conn->mux_ctx = h2c;
173
174 conn_xprt_want_recv(conn);
175 /* mux->wake will be called soon to complete the operation */
176 return 0;
177 fail:
178 pool_free2(pool2_h2c, h2c);
179 return -1;
180}
181
Willy Tarreau62f52692017-10-08 23:01:42 +0200182/* Initialize the mux once it's attached. For outgoing connections, the context
183 * is already initialized before installing the mux, so we detect incoming
184 * connections from the fact that the context is still NULL. Returns < 0 on
185 * error.
186 */
187static int h2_init(struct connection *conn)
188{
189 if (conn->mux_ctx) {
190 /* we don't support outgoing connections for now */
191 return -1;
192 }
193
Willy Tarreau32218eb2017-09-22 08:07:25 +0200194 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200195}
196
197/* release function for a connection. This one should be called to free all
198 * resources allocated to the mux.
199 */
200static void h2_release(struct connection *conn)
201{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200202 struct h2c *h2c = conn->mux_ctx;
203
204 LIST_DEL(&conn->list);
205
206 if (h2c) {
207 hpack_dht_free(h2c->ddht);
208 pool_free2(pool2_h2c, h2c);
209 }
210
211 conn->mux = NULL;
212 conn->mux_ctx = NULL;
213
214 conn_stop_tracking(conn);
215 conn_full_close(conn);
216 if (conn->destroy_cb)
217 conn->destroy_cb(conn);
218 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200219}
220
221
222/*********************************************************/
223/* functions below are I/O callbacks from the connection */
224/*********************************************************/
225
226/* callback called on recv event by the connection handler */
227static void h2_recv(struct connection *conn)
228{
229}
230
231/* callback called on send event by the connection handler */
232static void h2_send(struct connection *conn)
233{
234}
235
236/* callback called on any event by the connection handler.
237 * It applies changes and returns zero, or < 0 if it wants immediate
238 * destruction of the connection (which normally doesn not happen in h2).
239 */
240static int h2_wake(struct connection *conn)
241{
242 return 0;
243}
244
245/*******************************************/
246/* functions below are used by the streams */
247/*******************************************/
248
249/*
250 * Attach a new stream to a connection
251 * (Used for outgoing connections)
252 */
253static struct conn_stream *h2_attach(struct connection *conn)
254{
255 return NULL;
256}
257
258/* callback used to update the mux's polling flags after changing a cs' status.
259 * The caller (cs_update_mux_polling) will take care of propagating any changes
260 * to the transport layer.
261 */
262static void h2_update_poll(struct conn_stream *cs)
263{
264}
265
266/*
267 * Detach the stream from the connection and possibly release the connection.
268 */
269static void h2_detach(struct conn_stream *cs)
270{
271}
272
273static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
274{
275}
276
277static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
278{
279}
280
281/*
282 * Called from the upper layer, to get more data
283 */
284static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
285{
286 /* FIXME: not handled for now */
287 cs->flags |= CS_FL_ERROR;
288 return 0;
289}
290
291/* Called from the upper layer, to send data */
292static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
293{
294 /* FIXME: not handled for now */
295 cs->flags |= CS_FL_ERROR;
296 return 0;
297}
298
299
300/*******************************************************/
301/* functions below are dedicated to the config parsers */
302/*******************************************************/
303
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200304/* config parser for global "tune.h2.header-table-size" */
305static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
306 struct proxy *defpx, const char *file, int line,
307 char **err)
308{
309 if (too_many_args(1, args, err, NULL))
310 return -1;
311
312 h2_settings_header_table_size = atoi(args[1]);
313 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
314 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
315 return -1;
316 }
317 return 0;
318}
Willy Tarreau62f52692017-10-08 23:01:42 +0200319
Willy Tarreaue6baec02017-07-27 11:45:11 +0200320/* config parser for global "tune.h2.initial-window-size" */
321static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
322 struct proxy *defpx, const char *file, int line,
323 char **err)
324{
325 if (too_many_args(1, args, err, NULL))
326 return -1;
327
328 h2_settings_initial_window_size = atoi(args[1]);
329 if (h2_settings_initial_window_size < 0) {
330 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
331 return -1;
332 }
333 return 0;
334}
335
Willy Tarreau5242ef82017-07-27 11:47:28 +0200336/* config parser for global "tune.h2.max-concurrent-streams" */
337static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
338 struct proxy *defpx, const char *file, int line,
339 char **err)
340{
341 if (too_many_args(1, args, err, NULL))
342 return -1;
343
344 h2_settings_max_concurrent_streams = atoi(args[1]);
345 if (h2_settings_max_concurrent_streams < 0) {
346 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
347 return -1;
348 }
349 return 0;
350}
351
Willy Tarreau62f52692017-10-08 23:01:42 +0200352
353/****************************************/
354/* MUX initialization and instanciation */
355/***************************************/
356
357/* The mux operations */
358const struct mux_ops h2_ops = {
359 .init = h2_init,
360 .recv = h2_recv,
361 .send = h2_send,
362 .wake = h2_wake,
363 .update_poll = h2_update_poll,
364 .rcv_buf = h2_rcv_buf,
365 .snd_buf = h2_snd_buf,
366 .attach = h2_attach,
367 .detach = h2_detach,
368 .shutr = h2_shutr,
369 .shutw = h2_shutw,
370 .release = h2_release,
371 .name = "H2",
372};
373
374/* ALPN selection : this mux registers ALPN tolen "h2" */
375static struct alpn_mux_list alpn_mux_h2 =
376 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
377
378/* config keyword parsers */
379static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200380 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +0200381 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +0200382 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +0200383 { 0, NULL, NULL }
384}};
385
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200386static void __h2_deinit(void)
387{
Willy Tarreau18312642017-10-11 07:57:07 +0200388 pool_destroy2(pool2_h2s);
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200389 pool_destroy2(pool2_h2c);
390}
391
Willy Tarreau62f52692017-10-08 23:01:42 +0200392__attribute__((constructor))
393static void __h2_init(void)
394{
395 alpn_register_mux(&alpn_mux_h2);
396 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200397 hap_register_post_deinit(__h2_deinit);
398 pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
Willy Tarreau18312642017-10-11 07:57:07 +0200399 pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +0200400}