blob: 7fc6ec018b9c5826d29302e6d4c126e83ccadcd6 [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>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau35dbd5d2017-09-22 09:13:49 +020020#include <proto/applet.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020021#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020022#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010024#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020025#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026
27
Willy Tarreau2a856182017-05-16 15:20:39 +020028/* dummy streams returned for idle and closed states */
29static const struct h2s *h2_closed_stream;
30static const struct h2s *h2_idle_stream;
31
Willy Tarreau5ab6b572017-09-22 08:05:00 +020032/* the h2c connection pool */
33static struct pool_head *pool2_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020034/* the h2s stream pool */
35static struct pool_head *pool2_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020036
37/* Connection flags (32 bit), in h2c->flags */
38#define H2_CF_NONE 0x00000000
39
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020040/* Flags indicating why writing to the mux is blocked. */
41#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
42#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
43#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
44
45/* Flags indicating why writing to the demux is blocked. */
46#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
47#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
48#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
49#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
50#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
51#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
52#define H2_CF_DEM_BLOCK_ANY 0x000000FC // aggregate of the demux flags above
53
Willy Tarreau081d4722017-05-16 21:51:05 +020054/* other flags */
55#define H2_CF_GOAWAY_SENT 0x00000100 // a GOAWAY frame was successfully sent
56#define H2_CF_GOAWAY_FAILED 0x00000200 // a GOAWAY frame failed to be sent
57
58
Willy Tarreau5ab6b572017-09-22 08:05:00 +020059/* H2 connection state, in h2c->st0 */
60enum h2_cs {
61 H2_CS_PREFACE, // init done, waiting for connection preface
62 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
63 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
64 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
65 H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame
66 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
67 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
68 H2_CS_ENTRIES // must be last
69} __attribute__((packed));
70
71/* H2 connection descriptor */
72struct h2c {
73 struct connection *conn;
74
75 enum h2_cs st0; /* mux state */
76 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
77
78 /* 16 bit hole here */
79 uint32_t flags; /* connection flags: H2_CF_* */
80 int32_t max_id; /* highest ID known on this connection, <0 before preface */
81 uint32_t rcvd_c; /* newly received data to ACK for the connection */
82 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
83
84 /* states for the demux direction */
85 struct hpack_dht *ddht; /* demux dynamic header table */
86 struct buffer *dbuf; /* demux buffer */
87
88 int32_t dsi; /* demux stream ID (<0 = idle) */
89 int32_t dfl; /* demux frame length (if dsi >= 0) */
90 int8_t dft; /* demux frame type (if dsi >= 0) */
91 int8_t dff; /* demux frame flags (if dsi >= 0) */
92 /* 16 bit hole here */
93 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
94
95 /* states for the mux direction */
96 struct buffer *mbuf; /* mux buffer */
97 int32_t msi; /* mux stream ID (<0 = idle) */
98 int32_t mfl; /* mux frame length (if dsi >= 0) */
99 int8_t mft; /* mux frame type (if dsi >= 0) */
100 int8_t mff; /* mux frame flags (if dsi >= 0) */
101 /* 16 bit hole here */
102 int32_t miw; /* mux initial window size for all new streams */
103 int32_t mws; /* mux window size. Can be negative. */
104 int32_t mfs; /* mux's max frame size */
105
Willy Tarreauea392822017-10-31 10:02:25 +0100106 int timeout; /* idle timeout duration in ticks */
107 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200108 struct eb_root streams_by_id; /* all active streams by their ID */
109 struct list send_list; /* list of blocked streams requesting to send */
110 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200111 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +0200112 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200113};
114
Willy Tarreau18312642017-10-11 07:57:07 +0200115/* H2 stream state, in h2s->st */
116enum h2_ss {
117 H2_SS_IDLE = 0, // idle
118 H2_SS_RLOC, // reserved(local)
119 H2_SS_RREM, // reserved(remote)
120 H2_SS_OPEN, // open
121 H2_SS_HREM, // half-closed(remote)
122 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200123 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200124 H2_SS_CLOSED, // closed
125 H2_SS_ENTRIES // must be last
126} __attribute__((packed));
127
128/* HTTP/2 stream flags (32 bit), in h2s->flags */
129#define H2_SF_NONE 0x00000000
130#define H2_SF_ES_RCVD 0x00000001
131#define H2_SF_ES_SENT 0x00000002
132
133#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
134#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
135
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200136/* stream flags indicating the reason the stream is blocked */
137#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
138#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
139#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
140#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
141#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
142
Willy Tarreau454f9052017-10-26 19:40:35 +0200143/* stream flags indicating how data is supposed to be sent */
144#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
145#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
146
147/* step we're currently in when sending chunks. This is needed because we may
148 * have to transfer chunks as large as a full buffer so there's no room left
149 * for size nor crlf around.
150 */
151#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
152#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
153#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
154
155#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
156
Willy Tarreau67434202017-11-06 20:20:51 +0100157#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100158#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100159
Willy Tarreau18312642017-10-11 07:57:07 +0200160/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
161 * it is being processed in the internal HTTP representation (H1 for now).
162 */
163struct h2s {
164 struct conn_stream *cs;
165 struct h2c *h2c;
166 struct h1m req, res; /* request and response parser state for H1 */
167 struct eb32_node by_id; /* place in h2c's streams_by_id */
168 struct list list; /* position in active/blocked lists if blocked>0 */
169 int32_t id; /* stream ID */
170 uint32_t flags; /* H2_SF_* */
171 int mws; /* mux window size for this stream */
172 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
173 enum h2_ss st;
174};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200175
Willy Tarreauc6405142017-09-21 20:23:50 +0200176/* descriptor for an h2 frame header */
177struct h2_fh {
178 uint32_t len; /* length, host order, 24 bits */
179 uint32_t sid; /* stream id, host order, 31 bits */
180 uint8_t ft; /* frame type */
181 uint8_t ff; /* frame flags */
182};
183
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200184/* a few settings from the global section */
185static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200186static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200187static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200188
Willy Tarreau2a856182017-05-16 15:20:39 +0200189/* a dmumy closed stream */
190static const struct h2s *h2_closed_stream = &(const struct h2s){
191 .cs = NULL,
192 .h2c = NULL,
193 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100194 .errcode = H2_ERR_STREAM_CLOSED,
195 .flags = H2_SF_RST_SENT,
Willy Tarreau2a856182017-05-16 15:20:39 +0200196 .id = 0,
197};
198
199/* and a dummy idle stream for use with any unannounced stream */
200static const struct h2s *h2_idle_stream = &(const struct h2s){
201 .cs = NULL,
202 .h2c = NULL,
203 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100204 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200205 .id = 0,
206};
207
Willy Tarreauea392822017-10-31 10:02:25 +0100208static struct task *h2_timeout_task(struct task *t);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200209
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200210/*****************************************************/
211/* functions below are for dynamic buffer management */
212/*****************************************************/
213
214/* re-enables receiving on mux <target> after a buffer was allocated. It returns
215 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
216 * if it's impossible to wake up and we prefer to be woken up later.
217 */
218static int h2_dbuf_available(void *target)
219{
220 struct h2c *h2c = target;
221
222 /* take the buffer now as we'll get scheduled waiting for ->wake() */
223 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200224 h2c->flags &= ~H2_CF_DEM_DALLOC;
225 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
226 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200227 return 1;
228 }
229 return 0;
230}
231
232static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
233{
234 struct buffer *buf = NULL;
235
236 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
237 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
238 h2c->dbuf_wait.target = h2c->conn;
239 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100240 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200241 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100242 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200243 __conn_xprt_stop_recv(h2c->conn);
244 }
245 return buf;
246}
247
248static inline void h2_release_dbuf(struct h2c *h2c)
249{
250 if (h2c->dbuf->size) {
251 b_free(&h2c->dbuf);
252 offer_buffers(h2c->dbuf_wait.target,
253 tasks_run_queue + applets_active_queue);
254 }
255}
256
Willy Tarreau14398122017-09-22 14:26:04 +0200257/* re-enables sending on mux <target> after a buffer was allocated. It returns
258 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
259 * if it's impossible to wake up and we prefer to be woken up later.
260 */
261static int h2_mbuf_available(void *target)
262{
263 struct h2c *h2c = target;
264
265 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
266 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200267 if (h2c->flags & H2_CF_MUX_MALLOC) {
268 h2c->flags &= ~H2_CF_MUX_MALLOC;
269 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
270 conn_xprt_want_send(h2c->conn);
271 }
272
273 if (h2c->flags & H2_CF_DEM_MROOM) {
274 h2c->flags &= ~H2_CF_DEM_MROOM;
275 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
276 conn_xprt_want_recv(h2c->conn);
277 }
278
Willy Tarreau14398122017-09-22 14:26:04 +0200279 /* FIXME: we should in fact call something like h2_update_poll()
280 * now to recompte the polling. For now it will be enough like
281 * this.
282 */
Willy Tarreau14398122017-09-22 14:26:04 +0200283 return 1;
284 }
285 return 0;
286}
287
288static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
289{
290 struct buffer *buf = NULL;
291
292 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
293 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
294 h2c->mbuf_wait.target = h2c;
295 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100296 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200297 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100298 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200299
300 /* FIXME: we should in fact only block the direction being
301 * currently used. For now it will be enough like this.
302 */
303 __conn_xprt_stop_send(h2c->conn);
304 __conn_xprt_stop_recv(h2c->conn);
305 }
306 return buf;
307}
308
309static inline void h2_release_mbuf(struct h2c *h2c)
310{
311 if (h2c->mbuf->size) {
312 b_free(&h2c->mbuf);
313 offer_buffers(h2c->mbuf_wait.target,
314 tasks_run_queue + applets_active_queue);
315 }
316}
317
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200318
Willy Tarreau62f52692017-10-08 23:01:42 +0200319/*****************************************************************/
320/* functions below are dedicated to the mux setup and management */
321/*****************************************************************/
322
Willy Tarreau32218eb2017-09-22 08:07:25 +0200323/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
324static int h2c_frt_init(struct connection *conn)
325{
326 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100327 struct task *t = NULL;
328 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200329
330 h2c = pool_alloc2(pool2_h2c);
331 if (!h2c)
332 goto fail;
333
Willy Tarreau3f133572017-10-31 19:21:06 +0100334
335 h2c->timeout = sess->fe->timeout.client;
Willy Tarreau33400292017-11-05 11:23:40 +0100336 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100337 if (tick_isset(h2c->timeout)) {
338 t = task_new(tid_bit);
339 if (!t)
340 goto fail;
341
342 h2c->task = t;
343 t->process = h2_timeout_task;
344 t->context = h2c;
345 t->expire = tick_add(now_ms, h2c->timeout);
346 }
Willy Tarreauea392822017-10-31 10:02:25 +0100347
Willy Tarreau32218eb2017-09-22 08:07:25 +0200348 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
349 if (!h2c->ddht)
350 goto fail;
351
352 /* Initialise the context. */
353 h2c->st0 = H2_CS_PREFACE;
354 h2c->conn = conn;
355 h2c->max_id = -1;
356 h2c->errcode = H2_ERR_NO_ERROR;
357 h2c->flags = H2_CF_NONE;
358 h2c->rcvd_c = 0;
359 h2c->rcvd_s = 0;
360
361 h2c->dbuf = &buf_empty;
362 h2c->dsi = -1;
363 h2c->msi = -1;
364 h2c->last_sid = -1;
365
366 h2c->mbuf = &buf_empty;
367 h2c->miw = 65535; /* mux initial window size */
368 h2c->mws = 65535; /* mux window size */
369 h2c->mfs = 16384; /* initial max frame size */
370 h2c->streams_by_id = EB_ROOT_UNIQUE;
371 LIST_INIT(&h2c->send_list);
372 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200373 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200374 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200375 conn->mux_ctx = h2c;
376
Willy Tarreau3f133572017-10-31 19:21:06 +0100377 if (t)
378 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200379 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100380
Willy Tarreau32218eb2017-09-22 08:07:25 +0200381 /* mux->wake will be called soon to complete the operation */
382 return 0;
383 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100384 if (t)
385 task_free(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200386 pool_free2(pool2_h2c, h2c);
387 return -1;
388}
389
Willy Tarreau62f52692017-10-08 23:01:42 +0200390/* Initialize the mux once it's attached. For outgoing connections, the context
391 * is already initialized before installing the mux, so we detect incoming
392 * connections from the fact that the context is still NULL. Returns < 0 on
393 * error.
394 */
395static int h2_init(struct connection *conn)
396{
397 if (conn->mux_ctx) {
398 /* we don't support outgoing connections for now */
399 return -1;
400 }
401
Willy Tarreau32218eb2017-09-22 08:07:25 +0200402 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200403}
404
Willy Tarreau2373acc2017-10-12 17:35:14 +0200405/* returns the stream associated with id <id> or NULL if not found */
406static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
407{
408 struct eb32_node *node;
409
Willy Tarreau2a856182017-05-16 15:20:39 +0200410 if (id > h2c->max_id)
411 return (struct h2s *)h2_idle_stream;
412
Willy Tarreau2373acc2017-10-12 17:35:14 +0200413 node = eb32_lookup(&h2c->streams_by_id, id);
414 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200415 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200416
417 return container_of(node, struct h2s, by_id);
418}
419
Willy Tarreau62f52692017-10-08 23:01:42 +0200420/* release function for a connection. This one should be called to free all
421 * resources allocated to the mux.
422 */
423static void h2_release(struct connection *conn)
424{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200425 struct h2c *h2c = conn->mux_ctx;
426
427 LIST_DEL(&conn->list);
428
429 if (h2c) {
430 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200431 h2_release_dbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100432 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200433 LIST_DEL(&h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100434 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200435
436 h2_release_mbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100437 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200438 LIST_DEL(&h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100439 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200440
Willy Tarreauea392822017-10-31 10:02:25 +0100441 if (h2c->task) {
442 task_delete(h2c->task);
443 task_free(h2c->task);
444 h2c->task = NULL;
445 }
446
Willy Tarreau32218eb2017-09-22 08:07:25 +0200447 pool_free2(pool2_h2c, h2c);
448 }
449
450 conn->mux = NULL;
451 conn->mux_ctx = NULL;
452
453 conn_stop_tracking(conn);
454 conn_full_close(conn);
455 if (conn->destroy_cb)
456 conn->destroy_cb(conn);
457 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200458}
459
460
Willy Tarreau71681172017-10-23 14:39:06 +0200461/******************************************************/
462/* functions below are for the H2 protocol processing */
463/******************************************************/
464
465/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
466static inline int h2s_id(const struct h2s *h2s)
467{
468 return h2s ? h2s->id : 0;
469}
470
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200471/* returns true of the mux is currently busy as seen from stream <h2s> */
472static inline int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
473{
474 if (h2c->msi < 0)
475 return 0;
476
477 if (h2c->msi == h2s_id(h2s))
478 return 0;
479
480 return 1;
481}
482
Willy Tarreau741d6df2017-10-17 08:00:59 +0200483/* marks an error on the connection */
484static inline void h2c_error(struct h2c *h2c, enum h2_err err)
485{
486 h2c->errcode = err;
487 h2c->st0 = H2_CS_ERROR;
488}
489
Willy Tarreau2e43f082017-10-17 08:03:59 +0200490/* marks an error on the stream */
491static inline void h2s_error(struct h2s *h2s, enum h2_err err)
492{
493 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
494 h2s->errcode = err;
495 h2s->st = H2_SS_ERROR;
496 if (h2s->cs)
497 h2s->cs->flags |= CS_FL_ERROR;
498 }
499}
500
Willy Tarreaue4820742017-07-27 13:37:23 +0200501/* writes the 24-bit frame size <len> at address <frame> */
502static inline void h2_set_frame_size(void *frame, uint32_t len)
503{
504 uint8_t *out = frame;
505
506 *out = len >> 16;
507 write_n16(out + 1, len);
508}
509
Willy Tarreau54c15062017-10-10 17:10:03 +0200510/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
511 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
512 * the caller's responsibility to verify that there are at least <bytes> bytes
513 * available in the buffer's input prior to calling this function.
514 */
515static inline void h2_get_buf_bytes(void *dst, size_t bytes,
516 const struct buffer *b, int o)
517{
518 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
519}
520
521static inline uint16_t h2_get_n16(const struct buffer *b, int o)
522{
523 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
524}
525
526static inline uint32_t h2_get_n32(const struct buffer *b, int o)
527{
528 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
529}
530
531static inline uint64_t h2_get_n64(const struct buffer *b, int o)
532{
533 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
534}
535
536
Willy Tarreau715d5312017-07-11 15:20:24 +0200537/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
538 * is not obvious. It turns out that H2 headers are neither aligned nor do they
539 * use regular sizes. And to add to the trouble, the buffer may wrap so each
540 * byte read must be checked. The header is formed like this :
541 *
542 * b0 b1 b2 b3 b4 b5..b8
543 * +----------+---------+--------+----+----+----------------------+
544 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
545 * +----------+---------+--------+----+----+----------------------+
546 *
547 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
548 * we get the sid properly aligned and ordered, and 16 bits of len properly
549 * ordered as well. The type and flags can be extracted using bit shifts from
550 * the word, and only one extra read is needed to fetch len[16:23].
551 * Returns zero if some bytes are missing, otherwise non-zero on success.
552 */
553static int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
554{
555 uint64_t w;
556
557 if (b->i < 9)
558 return 0;
559
560 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
561 h->len = *b->p << 16;
562 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
563 h->ff = w >> 32;
564 h->ft = w >> 40;
565 h->len += w >> 48;
566 return 1;
567}
568
569/* skip the next 9 bytes corresponding to the frame header possibly parsed by
570 * h2_peek_frame_hdr() above.
571 */
572static inline void h2_skip_frame_hdr(struct buffer *b)
573{
574 bi_del(b, 9);
575}
576
577/* same as above, automatically advances the buffer on success */
578static inline int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
579{
580 int ret;
581
582 ret = h2_peek_frame_hdr(b, h);
583 if (ret > 0)
584 h2_skip_frame_hdr(b);
585 return ret;
586}
587
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200588/* creates a new stream <id> on the h2c connection and returns it, or NULL in
589 * case of memory allocation error.
590 */
591static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
592{
593 struct conn_stream *cs;
594 struct h2s *h2s;
595
596 h2s = pool_alloc2(pool2_h2s);
597 if (!h2s)
598 goto out;
599
600 h2s->h2c = h2c;
601 h2s->mws = h2c->miw;
602 h2s->flags = H2_SF_NONE;
603 h2s->errcode = H2_ERR_NO_ERROR;
604 h2s->st = H2_SS_IDLE;
605 h1m_init(&h2s->req);
606 h1m_init(&h2s->res);
607 h2s->by_id.key = h2s->id = id;
608 h2c->max_id = id;
609 LIST_INIT(&h2s->list);
610
611 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
612
613 cs = cs_new(h2c->conn);
614 if (!cs)
615 goto out_close;
616
617 h2s->cs = cs;
618 cs->ctx = h2s;
619
620 if (stream_create_from_cs(cs) < 0)
621 goto out_free_cs;
622
623 /* OK done, the stream lives its own life now */
624 return h2s;
625
626 out_free_cs:
627 cs_free(cs);
628 out_close:
629 eb32_delete(&h2s->by_id);
630 pool_free2(pool2_h2s, h2s);
631 h2s = NULL;
632 out:
633 return h2s;
634}
635
Willy Tarreaube5b7152017-09-25 16:25:39 +0200636/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
637 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
638 * the various settings codes.
639 */
640static int h2c_snd_settings(struct h2c *h2c)
641{
642 struct buffer *res;
643 char buf_data[100]; // enough for 15 settings
644 struct chunk buf;
645 int ret;
646
647 if (h2c_mux_busy(h2c, NULL)) {
648 h2c->flags |= H2_CF_DEM_MBUSY;
649 return 0;
650 }
651
652 res = h2_get_mbuf(h2c);
653 if (!res) {
654 h2c->flags |= H2_CF_MUX_MALLOC;
655 h2c->flags |= H2_CF_DEM_MROOM;
656 return 0;
657 }
658
659 chunk_init(&buf, buf_data, sizeof(buf_data));
660 chunk_memcpy(&buf,
661 "\x00\x00\x00" /* length : 0 for now */
662 "\x04\x00" /* type : 4 (settings), flags : 0 */
663 "\x00\x00\x00\x00", /* stream ID : 0 */
664 9);
665
666 if (h2_settings_header_table_size != 4096) {
667 char str[6] = "\x00\x01"; /* header_table_size */
668
669 write_n32(str + 2, h2_settings_header_table_size);
670 chunk_memcat(&buf, str, 6);
671 }
672
673 if (h2_settings_initial_window_size != 65535) {
674 char str[6] = "\x00\x04"; /* initial_window_size */
675
676 write_n32(str + 2, h2_settings_initial_window_size);
677 chunk_memcat(&buf, str, 6);
678 }
679
680 if (h2_settings_max_concurrent_streams != 0) {
681 char str[6] = "\x00\x03"; /* max_concurrent_streams */
682
683 /* Note: 0 means "unlimited" for haproxy's config but not for
684 * the protocol, so never send this value!
685 */
686 write_n32(str + 2, h2_settings_max_concurrent_streams);
687 chunk_memcat(&buf, str, 6);
688 }
689
690 if (global.tune.bufsize != 16384) {
691 char str[6] = "\x00\x05"; /* max_frame_size */
692
693 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
694 * match bufsize - rewrite size, but at the moment it seems
695 * that clients don't take care of it.
696 */
697 write_n32(str + 2, global.tune.bufsize);
698 chunk_memcat(&buf, str, 6);
699 }
700
701 h2_set_frame_size(buf.str, buf.len - 9);
702 ret = bo_istput(res, ist2(buf.str, buf.len));
703 if (unlikely(ret <= 0)) {
704 if (!ret) {
705 h2c->flags |= H2_CF_MUX_MFULL;
706 h2c->flags |= H2_CF_DEM_MROOM;
707 return 0;
708 }
709 else {
710 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
711 return 0;
712 }
713 }
714 return ret;
715}
716
Willy Tarreau52eed752017-09-22 15:05:09 +0200717/* Try to receive a connection preface, then upon success try to send our
718 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
719 * missing data. It may return an error in h2c.
720 */
721static int h2c_frt_recv_preface(struct h2c *h2c)
722{
723 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200724 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200725
726 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
727
728 if (unlikely(ret1 <= 0)) {
729 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
730 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
731 return 0;
732 }
733
Willy Tarreaube5b7152017-09-25 16:25:39 +0200734 ret2 = h2c_snd_settings(h2c);
735 if (ret2 > 0)
736 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200737
Willy Tarreaube5b7152017-09-25 16:25:39 +0200738 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200739}
740
Willy Tarreau081d4722017-05-16 21:51:05 +0200741/* try to send a GOAWAY frame on the connection to report an error or a graceful
742 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
743 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
744 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
745 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
746 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
747 * on unrecoverable failure. It will not attempt to send one again in this last
748 * case so that it is safe to use h2c_error() to report such errors.
749 */
750static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
751{
752 struct buffer *res;
753 char str[17];
754 int ret;
755
756 if (h2c->flags & H2_CF_GOAWAY_FAILED)
757 return 1; // claim that it worked
758
759 if (h2c_mux_busy(h2c, h2s)) {
760 if (h2s)
761 h2s->flags |= H2_SF_BLK_MBUSY;
762 else
763 h2c->flags |= H2_CF_DEM_MBUSY;
764 return 0;
765 }
766
767 res = h2_get_mbuf(h2c);
768 if (!res) {
769 h2c->flags |= H2_CF_MUX_MALLOC;
770 if (h2s)
771 h2s->flags |= H2_SF_BLK_MROOM;
772 else
773 h2c->flags |= H2_CF_DEM_MROOM;
774 return 0;
775 }
776
777 /* len: 8, type: 7, flags: none, sid: 0 */
778 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
779
780 if (h2c->last_sid < 0)
781 h2c->last_sid = h2c->max_id;
782
783 write_n32(str + 9, h2c->last_sid);
784 write_n32(str + 13, h2c->errcode);
785 ret = bo_istput(res, ist2(str, 17));
786 if (unlikely(ret <= 0)) {
787 if (!ret) {
788 h2c->flags |= H2_CF_MUX_MFULL;
789 if (h2s)
790 h2s->flags |= H2_SF_BLK_MROOM;
791 else
792 h2c->flags |= H2_CF_DEM_MROOM;
793 return 0;
794 }
795 else {
796 /* we cannot report this error using GOAWAY, so we mark
797 * it and claim a success.
798 */
799 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
800 h2c->flags |= H2_CF_GOAWAY_FAILED;
801 return 1;
802 }
803 }
804 h2c->flags |= H2_CF_GOAWAY_SENT;
805 return ret;
806}
807
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100808/* Try to send an RST_STREAM frame on the connection for the indicated stream
809 * during mux operations. This stream must be valid and cannot be closed
810 * already. h2s->id will be used for the stream ID and h2s->errcode will be
811 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
812 * not yet.
813 *
814 * Returns > 0 on success or zero if nothing was done. In case of lack of room
815 * to write the message, it subscribes the stream to future notifications.
816 */
817static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
818{
819 struct buffer *res;
820 char str[13];
821 int ret;
822
823 if (!h2s || h2s->st == H2_SS_CLOSED)
824 return 1;
825
826 if (h2c_mux_busy(h2c, h2s)) {
827 h2s->flags |= H2_SF_BLK_MBUSY;
828 return 0;
829 }
830
831 res = h2_get_mbuf(h2c);
832 if (!res) {
833 h2c->flags |= H2_CF_MUX_MALLOC;
834 h2s->flags |= H2_SF_BLK_MROOM;
835 return 0;
836 }
837
838 /* len: 4, type: 3, flags: none */
839 memcpy(str, "\x00\x00\x04\x03\x00", 5);
840 write_n32(str + 5, h2s->id);
841 write_n32(str + 9, h2s->errcode);
842 ret = bo_istput(res, ist2(str, 13));
843
844 if (unlikely(ret <= 0)) {
845 if (!ret) {
846 h2c->flags |= H2_CF_MUX_MFULL;
847 h2s->flags |= H2_SF_BLK_MROOM;
848 return 0;
849 }
850 else {
851 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
852 return 0;
853 }
854 }
855
856 h2s->flags |= H2_SF_RST_SENT;
857 h2s->st = H2_SS_CLOSED;
858 return ret;
859}
860
861/* Try to send an RST_STREAM frame on the connection for the stream being
862 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
863 * error code unless the stream's state already is IDLE or CLOSED in which
864 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
865 * it was not yet.
866 *
867 * Returns > 0 on success or zero if nothing was done. In case of lack of room
868 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200869 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100870 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200871 */
872static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
873{
874 struct buffer *res;
875 char str[13];
876 int ret;
877
878 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100879 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200880 return 0;
881 }
882
883 res = h2_get_mbuf(h2c);
884 if (!res) {
885 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100886 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200887 return 0;
888 }
889
890 /* len: 4, type: 3, flags: none */
891 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100892
Willy Tarreau27a84c92017-10-17 08:10:17 +0200893 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100894 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200895 h2s->errcode : H2_ERR_STREAM_CLOSED);
896 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100897
Willy Tarreau27a84c92017-10-17 08:10:17 +0200898 if (unlikely(ret <= 0)) {
899 if (!ret) {
900 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100901 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200902 return 0;
903 }
904 else {
905 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
906 return 0;
907 }
908 }
909
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100910 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200911 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100912 h2s->st = H2_SS_CLOSED;
913 }
914
Willy Tarreau27a84c92017-10-17 08:10:17 +0200915 return ret;
916}
917
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100918/* try to send an empty DATA frame with the ES flag set to notify about the
919 * end of stream and match a shutdown(write). If an ES was already sent as
920 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
921 * on success or zero if nothing was done. In case of lack of room to write the
922 * message, it subscribes the requesting stream to future notifications.
923 */
924static int h2_send_empty_data_es(struct h2s *h2s)
925{
926 struct h2c *h2c = h2s->h2c;
927 struct buffer *res;
928 char str[9];
929 int ret;
930
Willy Tarreau721c9742017-11-07 11:05:42 +0100931 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100932 return 1;
933
934 if (h2c_mux_busy(h2c, h2s)) {
935 h2s->flags |= H2_SF_BLK_MBUSY;
936 return 0;
937 }
938
939 res = h2_get_mbuf(h2c);
940 if (!res) {
941 h2c->flags |= H2_CF_MUX_MALLOC;
942 h2s->flags |= H2_SF_BLK_MROOM;
943 return 0;
944 }
945
946 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
947 memcpy(str, "\x00\x00\x00\x00\x01", 5);
948 write_n32(str + 5, h2s->id);
949 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100950 if (likely(ret > 0)) {
951 h2s->flags |= H2_SF_ES_SENT;
952 }
953 else if (!ret) {
954 h2c->flags |= H2_CF_MUX_MFULL;
955 h2s->flags |= H2_SF_BLK_MROOM;
956 return 0;
957 }
958 else {
959 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
960 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100961 }
962 return ret;
963}
964
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100965/* wake the streams attached to the connection, whose id is greater than <last>,
966 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
967 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
968 * stream's state is automatically updated accordingly.
969 */
970static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
971{
972 struct eb32_node *node;
973 struct h2s *h2s;
974
975 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
976 flags |= CS_FL_ERROR;
977
978 if (conn_xprt_read0_pending(h2c->conn))
979 flags |= CS_FL_EOS;
980
981 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
982 while (node) {
983 h2s = container_of(node, struct h2s, by_id);
984 if (h2s->id <= last)
985 break;
986 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +0100987
988 if (!h2s->cs) {
989 /* this stream was already orphaned */
990 eb32_delete(&h2s->by_id);
991 pool_free2(pool2_h2s, h2s);
992 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100993 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +0100994
995 h2s->cs->flags |= flags;
996 /* recv is used to force to detect CS_FL_EOS that wake()
997 * doesn't handle in the stream int code.
998 */
999 h2s->cs->data_cb->recv(h2s->cs);
1000 h2s->cs->data_cb->wake(h2s->cs);
1001
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001002 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1003 h2s->st = H2_SS_ERROR;
1004 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1005 h2s->st = H2_SS_HREM;
1006 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
1007 h2s->st = H2_SS_CLOSED;
1008 }
1009}
1010
Willy Tarreau3421aba2017-07-27 15:41:03 +02001011/* Increase all streams' outgoing window size by the difference passed in
1012 * argument. This is needed upon receipt of the settings frame if the initial
1013 * window size is different. The difference may be negative and the resulting
1014 * window size as well, for the time it takes to receive some window updates.
1015 */
1016static void h2c_update_all_ws(struct h2c *h2c, int diff)
1017{
1018 struct h2s *h2s;
1019 struct eb32_node *node;
1020
1021 if (!diff)
1022 return;
1023
1024 node = eb32_first(&h2c->streams_by_id);
1025 while (node) {
1026 h2s = container_of(node, struct h2s, by_id);
1027 h2s->mws += diff;
1028 node = eb32_next(node);
1029 }
1030}
1031
1032/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1033 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1034 * return an error in h2c. Described in RFC7540#6.5.
1035 */
1036static int h2c_handle_settings(struct h2c *h2c)
1037{
1038 unsigned int offset;
1039 int error;
1040
1041 if (h2c->dff & H2_F_SETTINGS_ACK) {
1042 if (h2c->dfl) {
1043 error = H2_ERR_FRAME_SIZE_ERROR;
1044 goto fail;
1045 }
1046 return 1;
1047 }
1048
1049 if (h2c->dsi != 0) {
1050 error = H2_ERR_PROTOCOL_ERROR;
1051 goto fail;
1052 }
1053
1054 if (h2c->dfl % 6) {
1055 error = H2_ERR_FRAME_SIZE_ERROR;
1056 goto fail;
1057 }
1058
1059 /* that's the limit we can process */
1060 if (h2c->dfl > global.tune.bufsize) {
1061 error = H2_ERR_FRAME_SIZE_ERROR;
1062 goto fail;
1063 }
1064
1065 /* process full frame only */
1066 if (h2c->dbuf->i < h2c->dfl)
1067 return 0;
1068
1069 /* parse the frame */
1070 for (offset = 0; offset < h2c->dfl; offset += 6) {
1071 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1072 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1073
1074 switch (type) {
1075 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1076 /* we need to update all existing streams with the
1077 * difference from the previous iws.
1078 */
1079 if (arg < 0) { // RFC7540#6.5.2
1080 error = H2_ERR_FLOW_CONTROL_ERROR;
1081 goto fail;
1082 }
1083 h2c_update_all_ws(h2c, arg - h2c->miw);
1084 h2c->miw = arg;
1085 break;
1086 case H2_SETTINGS_MAX_FRAME_SIZE:
1087 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1088 error = H2_ERR_PROTOCOL_ERROR;
1089 goto fail;
1090 }
1091 h2c->mfs = arg;
1092 break;
1093 }
1094 }
1095
1096 /* need to ACK this frame now */
1097 h2c->st0 = H2_CS_FRAME_A;
1098 return 1;
1099 fail:
1100 h2c_error(h2c, error);
1101 return 0;
1102}
1103
1104/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1105 * success or one of the h2_status values.
1106 */
1107static int h2c_ack_settings(struct h2c *h2c)
1108{
1109 struct buffer *res;
1110 char str[9];
1111 int ret = -1;
1112
1113 if (h2c_mux_busy(h2c, NULL)) {
1114 h2c->flags |= H2_CF_DEM_MBUSY;
1115 return 0;
1116 }
1117
1118 res = h2_get_mbuf(h2c);
1119 if (!res) {
1120 h2c->flags |= H2_CF_MUX_MALLOC;
1121 h2c->flags |= H2_CF_DEM_MROOM;
1122 return 0;
1123 }
1124
1125 memcpy(str,
1126 "\x00\x00\x00" /* length : 0 (no data) */
1127 "\x04" "\x01" /* type : 4, flags : ACK */
1128 "\x00\x00\x00\x00" /* stream ID */, 9);
1129
1130 ret = bo_istput(res, ist2(str, 9));
1131 if (unlikely(ret <= 0)) {
1132 if (!ret) {
1133 h2c->flags |= H2_CF_MUX_MFULL;
1134 h2c->flags |= H2_CF_DEM_MROOM;
1135 return 0;
1136 }
1137 else {
1138 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1139 return 0;
1140 }
1141 }
1142 return ret;
1143}
1144
Willy Tarreaucf68c782017-10-10 17:11:41 +02001145/* processes a PING frame and schedules an ACK if needed. The caller must pass
1146 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1147 * missing data. It may return an error in h2c.
1148 */
1149static int h2c_handle_ping(struct h2c *h2c)
1150{
1151 /* frame length must be exactly 8 */
1152 if (h2c->dfl != 8) {
1153 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1154 return 0;
1155 }
1156
1157 /* schedule a response */
1158 if (!(h2c->dft & H2_F_PING_ACK))
1159 h2c->st0 = H2_CS_FRAME_A;
1160 return 1;
1161}
1162
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001163/* Try to send a window update for stream id <sid> and value <increment>.
1164 * Returns > 0 on success or zero on missing room or failure. It may return an
1165 * error in h2c.
1166 */
1167static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1168{
1169 struct buffer *res;
1170 char str[13];
1171 int ret = -1;
1172
1173 if (h2c_mux_busy(h2c, NULL)) {
1174 h2c->flags |= H2_CF_DEM_MBUSY;
1175 return 0;
1176 }
1177
1178 res = h2_get_mbuf(h2c);
1179 if (!res) {
1180 h2c->flags |= H2_CF_MUX_MALLOC;
1181 h2c->flags |= H2_CF_DEM_MROOM;
1182 return 0;
1183 }
1184
1185 /* length: 4, type: 8, flags: none */
1186 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1187 write_n32(str + 5, sid);
1188 write_n32(str + 9, increment);
1189
1190 ret = bo_istput(res, ist2(str, 13));
1191
1192 if (unlikely(ret <= 0)) {
1193 if (!ret) {
1194 h2c->flags |= H2_CF_MUX_MFULL;
1195 h2c->flags |= H2_CF_DEM_MROOM;
1196 return 0;
1197 }
1198 else {
1199 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1200 return 0;
1201 }
1202 }
1203 return ret;
1204}
1205
1206/* try to send pending window update for the connection. It's safe to call it
1207 * with no pending updates. Returns > 0 on success or zero on missing room or
1208 * failure. It may return an error in h2c.
1209 */
1210static int h2c_send_conn_wu(struct h2c *h2c)
1211{
1212 int ret = 1;
1213
1214 if (h2c->rcvd_c <= 0)
1215 return 1;
1216
1217 /* send WU for the connection */
1218 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1219 if (ret > 0)
1220 h2c->rcvd_c = 0;
1221
1222 return ret;
1223}
1224
1225/* try to send pending window update for the current dmux stream. It's safe to
1226 * call it with no pending updates. Returns > 0 on success or zero on missing
1227 * room or failure. It may return an error in h2c.
1228 */
1229static int h2c_send_strm_wu(struct h2c *h2c)
1230{
1231 int ret = 1;
1232
1233 if (h2c->rcvd_s <= 0)
1234 return 1;
1235
1236 /* send WU for the stream */
1237 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1238 if (ret > 0)
1239 h2c->rcvd_s = 0;
1240
1241 return ret;
1242}
1243
Willy Tarreaucf68c782017-10-10 17:11:41 +02001244/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1245 * success, 0 on missing data or one of the h2_status values.
1246 */
1247static int h2c_ack_ping(struct h2c *h2c)
1248{
1249 struct buffer *res;
1250 char str[17];
1251 int ret = -1;
1252
1253 if (h2c->dbuf->i < 8)
1254 return 0;
1255
1256 if (h2c_mux_busy(h2c, NULL)) {
1257 h2c->flags |= H2_CF_DEM_MBUSY;
1258 return 0;
1259 }
1260
1261 res = h2_get_mbuf(h2c);
1262 if (!res) {
1263 h2c->flags |= H2_CF_MUX_MALLOC;
1264 h2c->flags |= H2_CF_DEM_MROOM;
1265 return 0;
1266 }
1267
1268 memcpy(str,
1269 "\x00\x00\x08" /* length : 8 (same payload) */
1270 "\x06" "\x01" /* type : 6, flags : ACK */
1271 "\x00\x00\x00\x00" /* stream ID */, 9);
1272
1273 /* copy the original payload */
1274 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1275
1276 ret = bo_istput(res, ist2(str, 17));
1277 if (unlikely(ret <= 0)) {
1278 if (!ret) {
1279 h2c->flags |= H2_CF_MUX_MFULL;
1280 h2c->flags |= H2_CF_DEM_MROOM;
1281 return 0;
1282 }
1283 else {
1284 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1285 return 0;
1286 }
1287 }
1288 return ret;
1289}
1290
Willy Tarreau26f95952017-07-27 17:18:30 +02001291/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1292 * Returns > 0 on success or zero on missing data. It may return an error in
1293 * h2c or h2s. Described in RFC7540#6.9.
1294 */
1295static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1296{
1297 int32_t inc;
1298 int error;
1299
1300 if (h2c->dfl != 4) {
1301 error = H2_ERR_FRAME_SIZE_ERROR;
1302 goto conn_err;
1303 }
1304
1305 /* process full frame only */
1306 if (h2c->dbuf->i < h2c->dfl)
1307 return 0;
1308
1309 inc = h2_get_n32(h2c->dbuf, 0);
1310
1311 if (h2c->dsi != 0) {
1312 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001313
1314 /* it's not an error to receive WU on a closed stream */
1315 if (h2s->st == H2_SS_CLOSED)
1316 return 1;
1317
1318 if (!inc) {
1319 error = H2_ERR_PROTOCOL_ERROR;
1320 goto strm_err;
1321 }
1322
1323 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1324 error = H2_ERR_FLOW_CONTROL_ERROR;
1325 goto strm_err;
1326 }
1327
1328 h2s->mws += inc;
1329 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1330 h2s->flags &= ~H2_SF_BLK_SFCTL;
1331 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1332 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1333 /* This stream wanted to send but could not due to its
1334 * own flow control. We can put it back into the send
1335 * list now, it will be handled upon next send() call.
1336 */
1337 LIST_ADDQ(&h2c->send_list, &h2s->list);
1338 }
1339 }
1340 }
1341 else {
1342 /* connection window update */
1343 if (!inc) {
1344 error = H2_ERR_PROTOCOL_ERROR;
1345 goto conn_err;
1346 }
1347
1348 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1349 error = H2_ERR_FLOW_CONTROL_ERROR;
1350 goto conn_err;
1351 }
1352
1353 h2c->mws += inc;
1354 }
1355
1356 return 1;
1357
1358 conn_err:
1359 h2c_error(h2c, error);
1360 return 0;
1361
1362 strm_err:
1363 if (h2s) {
1364 h2s_error(h2s, error);
1365 h2c->st0 = H2_CS_FRAME_A;
1366 }
1367 else
1368 h2c_error(h2c, error);
1369 return 0;
1370}
1371
Willy Tarreaue96b0922017-10-30 00:28:29 +01001372/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1373 * the last ID. Returns > 0 on success or zero on missing data. It may return
1374 * an error in h2c. Described in RFC7540#6.8.
1375 */
1376static int h2c_handle_goaway(struct h2c *h2c)
1377{
1378 int error;
1379 int last;
1380
1381 if (h2c->dsi != 0) {
1382 error = H2_ERR_PROTOCOL_ERROR;
1383 goto conn_err;
1384 }
1385
1386 if (h2c->dfl < 8) {
1387 error = H2_ERR_FRAME_SIZE_ERROR;
1388 goto conn_err;
1389 }
1390
1391 /* process full frame only */
1392 if (h2c->dbuf->i < h2c->dfl)
1393 return 0;
1394
1395 last = h2_get_n32(h2c->dbuf, 0);
1396 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1397 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
1398 return 1;
1399
1400 conn_err:
1401 h2c_error(h2c, error);
1402 return 0;
1403}
1404
Willy Tarreaucd234e92017-08-18 10:59:39 +02001405/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1406 * Returns > 0 on success or zero on missing data. It may return an error in
1407 * h2c. Described in RFC7540#6.4.
1408 */
1409static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1410{
1411 int error;
1412
1413 if (h2c->dsi == 0) {
1414 error = H2_ERR_PROTOCOL_ERROR;
1415 goto conn_err;
1416 }
1417
Willy Tarreaucd234e92017-08-18 10:59:39 +02001418 if (h2c->dfl != 4) {
1419 error = H2_ERR_FRAME_SIZE_ERROR;
1420 goto conn_err;
1421 }
1422
1423 /* process full frame only */
1424 if (h2c->dbuf->i < h2c->dfl)
1425 return 0;
1426
1427 /* late RST, already handled */
1428 if (h2s->st == H2_SS_CLOSED)
1429 return 1;
1430
1431 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
1432 h2s->st = H2_SS_CLOSED;
1433
1434 if (h2s->cs) {
1435 h2s->cs->flags |= CS_FL_EOS;
1436 /* recv is used to force to detect CS_FL_EOS that wake()
1437 * doesn't handle in the stream-int code.
1438 */
1439 h2s->cs->data_cb->recv(h2s->cs);
1440 h2s->cs->data_cb->wake(h2s->cs);
1441 }
1442
1443 h2s->flags |= H2_SF_RST_RCVD;
1444 return 1;
1445
1446 conn_err:
1447 h2c_error(h2c, error);
1448 return 0;
1449}
1450
Willy Tarreau13278b42017-10-13 19:23:14 +02001451/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1452 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1453 * errors here are reported as connection errors since it's impossible to
1454 * recover from such errors after the compression context has been altered.
1455 */
1456static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1457{
1458 int error;
1459
1460 if (!h2c->dfl) {
1461 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1462 goto strm_err;
1463 }
1464
1465 if (!h2c->dbuf->size)
1466 return 0; // empty buffer
1467
1468 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1469 return 0; // incomplete frame
1470
1471 /* now either the frame is complete or the buffer is complete */
1472 if (h2s->st != H2_SS_IDLE) {
1473 /* FIXME: stream already exists, this is only allowed for
1474 * trailers (not supported for now).
1475 */
1476 error = H2_ERR_PROTOCOL_ERROR;
1477 goto conn_err;
1478 }
1479 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1480 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1481 error = H2_ERR_PROTOCOL_ERROR;
1482 goto conn_err;
1483 }
1484
1485 h2s = h2c_stream_new(h2c, h2c->dsi);
1486 if (!h2s) {
1487 error = H2_ERR_INTERNAL_ERROR;
1488 goto conn_err;
1489 }
1490
1491 h2s->st = H2_SS_OPEN;
1492 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1493 h2s->st = H2_SS_HREM;
1494 h2s->flags |= H2_SF_ES_RCVD;
1495 }
1496
1497 /* call the upper layers to process the frame, then let the upper layer
1498 * notify the stream about any change.
1499 */
1500 h2s->cs->data_cb->recv(h2s->cs);
1501
1502 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1503 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1504 error = H2_ERR_INTERNAL_ERROR;
1505 goto conn_err;
1506 }
1507
Willy Tarreau721c9742017-11-07 11:05:42 +01001508 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001509 /* stream error : send RST_STREAM */
1510 h2c->st0 = H2_CS_FRAME_A;
1511 }
1512 else {
1513 /* update the max stream ID if the request is being processed */
1514 if (h2s->id > h2c->max_id)
1515 h2c->max_id = h2s->id;
1516 }
1517
1518 return 1;
1519
1520 conn_err:
1521 h2c_error(h2c, error);
1522 return 0;
1523
1524 strm_err:
1525 if (h2s) {
1526 h2s_error(h2s, error);
1527 h2c->st0 = H2_CS_FRAME_A;
1528 }
1529 else
1530 h2c_error(h2c, error);
1531 return 0;
1532}
1533
Willy Tarreau454f9052017-10-26 19:40:35 +02001534/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1535 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1536 */
1537static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1538{
1539 int error;
1540
1541 /* note that empty DATA frames are perfectly valid and sometimes used
1542 * to signal an end of stream (with the ES flag).
1543 */
1544
1545 if (!h2c->dbuf->size && h2c->dfl)
1546 return 0; // empty buffer
1547
1548 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1549 return 0; // incomplete frame
1550
1551 /* now either the frame is complete or the buffer is complete */
1552
1553 if (!h2c->dsi) {
1554 /* RFC7540#6.1 */
1555 error = H2_ERR_PROTOCOL_ERROR;
1556 goto conn_err;
1557 }
1558
1559 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1560 /* RFC7540#6.1 */
1561 error = H2_ERR_STREAM_CLOSED;
1562 goto strm_err;
1563 }
1564
1565 /* last frame */
1566 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1567 h2s->st = H2_SS_HREM;
1568 h2s->flags |= H2_SF_ES_RCVD;
1569 }
1570
1571 /* call the upper layers to process the frame, then let the upper layer
1572 * notify the stream about any change.
1573 */
1574 if (!h2s->cs) {
1575 error = H2_ERR_STREAM_CLOSED;
1576 goto strm_err;
1577 }
1578
1579 h2s->cs->data_cb->recv(h2s->cs);
1580 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1581 /* cs has just been destroyed, we have to kill h2s. */
1582 error = H2_ERR_STREAM_CLOSED;
1583 goto strm_err;
1584 }
1585
Willy Tarreau721c9742017-11-07 11:05:42 +01001586 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001587 /* stream error : send RST_STREAM */
1588 h2c->st0 = H2_CS_FRAME_A;
1589 }
1590
1591 /* check for completion : the callee will change this to FRAME_A or
1592 * FRAME_H once done.
1593 */
1594 if (h2c->st0 == H2_CS_FRAME_P)
1595 return 0;
1596
1597 return 1;
1598
1599 conn_err:
1600 h2c_error(h2c, error);
1601 return 0;
1602
1603 strm_err:
1604 if (h2s) {
1605 h2s_error(h2s, error);
1606 h2c->st0 = H2_CS_FRAME_A;
1607 }
1608 else
1609 h2c_error(h2c, error);
1610 return 0;
1611}
1612
Willy Tarreaubc933932017-10-09 16:21:43 +02001613/* process Rx frames to be demultiplexed */
1614static void h2_process_demux(struct h2c *h2c)
1615{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001616 struct h2s *h2s;
1617
Willy Tarreau081d4722017-05-16 21:51:05 +02001618 if (h2c->st0 >= H2_CS_ERROR)
1619 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001620
1621 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1622 if (h2c->st0 == H2_CS_PREFACE) {
1623 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1624 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1625 if (h2c->st0 == H2_CS_ERROR)
1626 h2c->st0 = H2_CS_ERROR2;
1627 goto fail;
1628 }
1629
1630 h2c->max_id = 0;
1631 h2c->st0 = H2_CS_SETTINGS1;
1632 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001633
1634 if (h2c->st0 == H2_CS_SETTINGS1) {
1635 struct h2_fh hdr;
1636
1637 /* ensure that what is pending is a valid SETTINGS frame
1638 * without an ACK.
1639 */
1640 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1641 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1642 if (h2c->st0 == H2_CS_ERROR)
1643 h2c->st0 = H2_CS_ERROR2;
1644 goto fail;
1645 }
1646
1647 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1648 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1649 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1650 h2c->st0 = H2_CS_ERROR2;
1651 goto fail;
1652 }
1653
1654 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1655 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1656 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1657 h2c->st0 = H2_CS_ERROR2;
1658 goto fail;
1659 }
1660
1661 /* that's OK, switch to FRAME_P to process it */
1662 h2c->dfl = hdr.len;
1663 h2c->dsi = hdr.sid;
1664 h2c->dft = hdr.ft;
1665 h2c->dff = hdr.ff;
1666 h2c->st0 = H2_CS_FRAME_P;
1667 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001668 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001669
1670 /* process as many incoming frames as possible below */
1671 while (h2c->dbuf->i) {
1672 int ret = 0;
1673
1674 if (h2c->st0 >= H2_CS_ERROR)
1675 break;
1676
1677 if (h2c->st0 == H2_CS_FRAME_H) {
1678 struct h2_fh hdr;
1679
1680 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1681 break;
1682
1683 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1684 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1685 h2c->st0 = H2_CS_ERROR;
1686 break;
1687 }
1688
1689 h2c->dfl = hdr.len;
1690 h2c->dsi = hdr.sid;
1691 h2c->dft = hdr.ft;
1692 h2c->dff = hdr.ff;
1693 h2c->st0 = H2_CS_FRAME_P;
1694 h2_skip_frame_hdr(h2c->dbuf);
1695 }
1696
1697 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001698 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001699
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001700 if (h2s->st == H2_SS_IDLE &&
1701 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1702 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1703 * this state MUST be treated as a connection error
1704 */
1705 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1706 h2c->st0 = H2_CS_ERROR;
1707 break;
1708 }
1709
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001710 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1711 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1712 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1713 * this state MUST be treated as a stream error
1714 */
1715 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1716 goto strm_err;
1717 }
1718
Willy Tarreauc0da1962017-10-30 18:38:00 +01001719#if 0
1720 // problem below: it is not possible to completely ignore such
1721 // streams as we need to maintain the compression state as well
1722 // and for this we need to completely process these frames (eg:
1723 // HEADERS frames) as well as counting DATA frames to emit
1724 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1725 // This is a typical case of layer violation where the
1726 // transported contents are critical to the connection's
1727 // validity and must be ignored at the same time :-(
1728
1729 /* graceful shutdown, ignore streams whose ID is higher than
1730 * the one advertised in GOAWAY. RFC7540#6.8.
1731 */
1732 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1733 ret = MIN(h2c->dbuf->i, h2c->dfl);
1734 bi_del(h2c->dbuf, ret);
1735 h2c->dfl -= ret;
1736 ret = h2c->dfl == 0;
1737 goto strm_err;
1738 }
1739#endif
1740
Willy Tarreau7e98c052017-10-10 15:56:59 +02001741 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001742 case H2_FT_SETTINGS:
1743 if (h2c->st0 == H2_CS_FRAME_P)
1744 ret = h2c_handle_settings(h2c);
1745
1746 if (h2c->st0 == H2_CS_FRAME_A)
1747 ret = h2c_ack_settings(h2c);
1748 break;
1749
Willy Tarreaucf68c782017-10-10 17:11:41 +02001750 case H2_FT_PING:
1751 if (h2c->st0 == H2_CS_FRAME_P)
1752 ret = h2c_handle_ping(h2c);
1753
1754 if (h2c->st0 == H2_CS_FRAME_A)
1755 ret = h2c_ack_ping(h2c);
1756 break;
1757
Willy Tarreau26f95952017-07-27 17:18:30 +02001758 case H2_FT_WINDOW_UPDATE:
1759 if (h2c->st0 == H2_CS_FRAME_P)
1760 ret = h2c_handle_window_update(h2c, h2s);
1761 break;
1762
Willy Tarreau61290ec2017-10-17 08:19:21 +02001763 case H2_FT_CONTINUATION:
1764 /* we currently don't support CONTINUATION frames since
1765 * we have nowhere to store the partial HEADERS frame.
1766 * Let's abort the stream on an INTERNAL_ERROR here.
1767 */
1768 if (h2c->st0 == H2_CS_FRAME_P)
1769 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1770 break;
1771
Willy Tarreau13278b42017-10-13 19:23:14 +02001772 case H2_FT_HEADERS:
1773 if (h2c->st0 == H2_CS_FRAME_P)
1774 ret = h2c_frt_handle_headers(h2c, h2s);
1775 break;
1776
Willy Tarreau454f9052017-10-26 19:40:35 +02001777 case H2_FT_DATA:
1778 if (h2c->st0 == H2_CS_FRAME_P)
1779 ret = h2c_frt_handle_data(h2c, h2s);
1780
1781 if (h2c->st0 == H2_CS_FRAME_A)
1782 ret = h2c_send_strm_wu(h2c);
1783 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001784
1785 case H2_FT_RST_STREAM:
1786 if (h2c->st0 == H2_CS_FRAME_P)
1787 ret = h2c_handle_rst_stream(h2c, h2s);
1788 break;
1789
Willy Tarreaue96b0922017-10-30 00:28:29 +01001790 case H2_FT_GOAWAY:
1791 if (h2c->st0 == H2_CS_FRAME_P)
1792 ret = h2c_handle_goaway(h2c);
1793 break;
1794
Willy Tarreau1c661982017-10-30 13:52:01 +01001795 case H2_FT_PUSH_PROMISE:
1796 /* not permitted here, RFC7540#5.1 */
1797 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001798 break;
1799
1800 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001801 default:
1802 /* drop frames that we ignore. They may be larger than
1803 * the buffer so we drain all of their contents until
1804 * we reach the end.
1805 */
1806 ret = MIN(h2c->dbuf->i, h2c->dfl);
1807 bi_del(h2c->dbuf, ret);
1808 h2c->dfl -= ret;
1809 ret = h2c->dfl == 0;
1810 }
1811
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001812 strm_err:
Willy Tarreau27a84c92017-10-17 08:10:17 +02001813 /* RST are sent similarly to frame acks */
1814 if (h2s->st == H2_SS_ERROR) {
1815 if (h2c->st0 == H2_CS_FRAME_P)
1816 h2c->st0 = H2_CS_FRAME_A;
1817
1818 if (h2c->st0 == H2_CS_FRAME_A)
1819 ret = h2c_send_rst_stream(h2c, h2s);
1820 }
1821
Willy Tarreau7e98c052017-10-10 15:56:59 +02001822 /* error or missing data condition met above ? */
1823 if (ret <= 0)
1824 break;
1825
1826 if (h2c->st0 != H2_CS_FRAME_H) {
1827 bi_del(h2c->dbuf, h2c->dfl);
1828 h2c->st0 = H2_CS_FRAME_H;
1829 }
1830 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001831
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001832 if (h2c->rcvd_c > 0 &&
1833 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1834 h2c_send_conn_wu(h2c);
1835
Willy Tarreau52eed752017-09-22 15:05:09 +02001836 fail:
1837 /* we can go here on missing data, blocked response or error */
1838 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001839}
1840
1841/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1842 * the end.
1843 */
1844static int h2_process_mux(struct h2c *h2c)
1845{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001846 struct h2s *h2s, *h2s_back;
1847
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001848 /* start by sending possibly pending window updates */
1849 if (h2c->rcvd_c > 0 &&
1850 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
1851 h2c_send_conn_wu(h2c) < 0)
1852 goto fail;
1853
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001854 /* First we always process the flow control list because the streams
1855 * waiting there were already elected for immediate emission but were
1856 * blocked just on this.
1857 */
1858
1859 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
1860 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
1861 h2c->st0 >= H2_CS_ERROR)
1862 break;
1863
1864 /* In theory it's possible that h2s->cs == NULL here :
1865 * - client sends crap that causes a parse error
1866 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1867 * - RST_STREAM cannot be emitted because mux is busy/full
1868 * - stream gets notified, detaches and quits
1869 * - mux buffer gets ready and wakes pending streams up
1870 * - bam!
1871 */
1872 h2s->flags &= ~H2_SF_BLK_ANY;
1873
1874 if (h2s->cs) {
1875 h2s->cs->data_cb->send(h2s->cs);
1876 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001877 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001878 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001879 }
1880
1881 /* depending on callee's blocking reasons, we may queue in send
1882 * list or completely dequeue.
1883 */
1884 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
1885 if (h2s->flags & H2_SF_BLK_ANY) {
1886 LIST_DEL(&h2s->list);
1887 LIST_ADDQ(&h2c->send_list, &h2s->list);
1888 }
1889 else {
1890 LIST_DEL(&h2s->list);
1891 LIST_INIT(&h2s->list);
1892 if (h2s->cs)
1893 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001894 else {
1895 /* just sent the last frame for this orphaned stream */
1896 eb32_delete(&h2s->by_id);
1897 pool_free2(pool2_h2s, h2s);
1898 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001899 }
1900 }
1901 }
1902
1903 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
1904 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
1905 break;
1906
1907 /* In theory it's possible that h2s->cs == NULL here :
1908 * - client sends crap that causes a parse error
1909 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1910 * - RST_STREAM cannot be emitted because mux is busy/full
1911 * - stream gets notified, detaches and quits
1912 * - mux buffer gets ready and wakes pending streams up
1913 * - bam!
1914 */
1915 h2s->flags &= ~H2_SF_BLK_ANY;
1916
1917 if (h2s->cs) {
1918 h2s->cs->data_cb->send(h2s->cs);
1919 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001920 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001921 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001922 }
1923 /* depending on callee's blocking reasons, we may queue in fctl
1924 * list or completely dequeue.
1925 */
1926 if (h2s->flags & H2_SF_BLK_MFCTL) {
1927 /* stream hit the connection's flow control */
1928 LIST_DEL(&h2s->list);
1929 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
1930 }
1931 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
1932 LIST_DEL(&h2s->list);
1933 LIST_INIT(&h2s->list);
1934 if (h2s->cs)
1935 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001936 else {
1937 /* just sent the last frame for this orphaned stream */
1938 eb32_delete(&h2s->by_id);
1939 pool_free2(pool2_h2s, h2s);
1940 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001941 }
1942 }
1943
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001944 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01001945 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001946 if (h2c->st0 == H2_CS_ERROR) {
1947 if (h2c->max_id >= 0) {
1948 h2c_send_goaway_error(h2c, NULL);
1949 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
1950 return 0;
1951 }
1952
1953 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
1954 }
1955 return 1;
1956 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001957 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02001958}
1959
Willy Tarreau71681172017-10-23 14:39:06 +02001960
Willy Tarreau62f52692017-10-08 23:01:42 +02001961/*********************************************************/
1962/* functions below are I/O callbacks from the connection */
1963/*********************************************************/
1964
1965/* callback called on recv event by the connection handler */
1966static void h2_recv(struct connection *conn)
1967{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001968 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001969 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001970 int max;
1971
1972 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001973 return;
1974
1975 if (h2c->flags & H2_CF_DEM_BLOCK_ANY)
1976 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001977
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001978 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001979 if (!buf) {
1980 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001981 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001982 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001983
Willy Tarreaua2af5122017-10-09 11:56:46 +02001984 /* note: buf->o == 0 */
1985 max = buf->size - buf->i;
1986 if (!max) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001987 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001988 return;
1989 }
1990
1991 conn->xprt->rcv_buf(conn, buf, max);
1992 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001993 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001994
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001995 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001996 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02001997 return;
1998 }
1999
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002000 if (buf->i == buf->size)
2001 h2c->flags |= H2_CF_DEM_DFULL;
2002
Willy Tarreaubc933932017-10-09 16:21:43 +02002003 h2_process_demux(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002004
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002005 /* after streams have been processed, we should have made some room */
Willy Tarreau081d4722017-05-16 21:51:05 +02002006 if (h2c->st0 >= H2_CS_ERROR)
2007 buf->i = 0;
2008
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002009 if (buf->i != buf->size)
2010 h2c->flags &= ~H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002011 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002012}
2013
2014/* callback called on send event by the connection handler */
2015static void h2_send(struct connection *conn)
2016{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002017 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002018 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002019
2020 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002021 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002022
2023 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2024 /* a handshake was requested */
2025 return;
2026 }
2027
Willy Tarreaubc933932017-10-09 16:21:43 +02002028 /* This loop is quite simple : it tries to fill as much as it can from
2029 * pending streams into the existing buffer until it's reportedly full
2030 * or the end of send requests is reached. Then it tries to send this
2031 * buffer's contents out, marks it not full if at least one byte could
2032 * be sent, and tries again.
2033 *
2034 * The snd_buf() function normally takes a "flags" argument which may
2035 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2036 * data immediately comes and CO_SFL_STREAMER to indicate that the
2037 * connection is streaming lots of data (used to increase TLS record
2038 * size at the expense of latency). The former can be sent any time
2039 * there's a buffer full flag, as it indicates at least one stream
2040 * attempted to send and failed so there are pending data. An
2041 * alternative would be to set it as long as there's an active stream
2042 * but that would be problematic for ACKs until we have an absolute
2043 * guarantee that all waiters have at least one byte to send. The
2044 * latter should possibly not be set for now.
2045 */
2046
2047 done = 0;
2048 while (!done) {
2049 unsigned int flags = 0;
2050
2051 /* fill as much as we can into the current buffer */
2052 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2053 done = h2_process_mux(h2c);
2054
2055 if (conn->flags & CO_FL_ERROR)
2056 break;
2057
2058 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2059 flags |= CO_SFL_MSG_MORE;
2060
Willy Tarreau319994a2017-11-07 11:03:56 +01002061 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002062 break;
2063
2064 /* wrote at least one byte, the buffer is not full anymore */
2065 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2066 }
2067
Willy Tarreaua2af5122017-10-09 11:56:46 +02002068 if (conn->flags & CO_FL_SOCK_WR_SH) {
2069 /* output closed, nothing to send, clear the buffer to release it */
2070 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002071 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002072}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002073
Willy Tarreau62f52692017-10-08 23:01:42 +02002074/* callback called on any event by the connection handler.
2075 * It applies changes and returns zero, or < 0 if it wants immediate
2076 * destruction of the connection (which normally doesn not happen in h2).
2077 */
2078static int h2_wake(struct connection *conn)
2079{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002080 struct h2c *h2c = conn->mux_ctx;
2081
Willy Tarreau26bd7612017-10-09 16:47:04 +02002082 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002083 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2084 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2085 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002086 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002087
2088 if (eb_is_empty(&h2c->streams_by_id)) {
2089 /* no more stream, kill the connection now */
2090 h2_release(conn);
2091 return -1;
2092 }
2093 else {
2094 /* some streams still there, we need to signal them all and
2095 * wait for their departure.
2096 */
2097 __conn_xprt_stop_recv(conn);
2098 __conn_xprt_stop_send(conn);
2099 return 0;
2100 }
2101 }
2102
2103 if (!h2c->dbuf->i)
2104 h2_release_dbuf(h2c);
2105
2106 /* stop being notified of incoming data if we can't process them */
2107 if (h2c->st0 >= H2_CS_ERROR ||
2108 (h2c->flags & H2_CF_DEM_BLOCK_ANY) || conn_xprt_read0_pending(conn)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002109 __conn_xprt_stop_recv(conn);
2110 }
2111 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002112 __conn_xprt_want_recv(conn);
2113 }
2114
2115 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002116 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2117 (h2c->st0 == H2_CS_ERROR ||
2118 h2c->mbuf->o ||
2119 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2120 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002121 __conn_xprt_want_send(conn);
2122 }
2123 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002124 h2_release_mbuf(h2c);
2125 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002126 }
2127
Willy Tarreau3f133572017-10-31 19:21:06 +01002128 if (h2c->task) {
2129 if (eb_is_empty(&h2c->streams_by_id)) {
2130 h2c->task->expire = tick_add(now_ms, h2c->timeout);
2131 task_queue(h2c->task);
2132 }
2133 else
2134 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002135 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002136 return 0;
2137}
2138
Willy Tarreauea392822017-10-31 10:02:25 +01002139/* Connection timeout management. The principle is that if there's no receipt
2140 * nor sending for a certain amount of time, the connection is closed. If the
2141 * MUX buffer still has lying data or is not allocatable, the connection is
2142 * immediately killed. If it's allocatable and empty, we attempt to send a
2143 * GOAWAY frame.
2144 */
2145static struct task *h2_timeout_task(struct task *t)
2146{
2147 struct h2c *h2c = t->context;
2148 int expired = tick_is_expired(t->expire, now_ms);
2149
2150 if (!expired)
2151 return t;
2152
2153 h2c_error(h2c, H2_ERR_NO_ERROR);
2154 h2_wake_some_streams(h2c, 0, 0);
2155
2156 if (h2c->mbuf->o) {
2157 /* don't even try to send a GOAWAY, the buffer is stuck */
2158 h2c->flags |= H2_CF_GOAWAY_FAILED;
2159 }
2160
2161 /* try to send but no need to insist */
2162 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2163 h2c->flags |= H2_CF_GOAWAY_FAILED;
2164
2165 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2166 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2167
2168 if (!eb_is_empty(&h2c->streams_by_id))
2169 goto wait;
2170
2171 h2_release(h2c->conn);
2172 return NULL;
2173
2174 wait:
2175 /* the streams have been notified, we must let them finish and close */
2176 h2c->task = NULL;
2177 task_delete(t);
2178 task_free(t);
2179 return NULL;
2180}
2181
2182
Willy Tarreau62f52692017-10-08 23:01:42 +02002183/*******************************************/
2184/* functions below are used by the streams */
2185/*******************************************/
2186
2187/*
2188 * Attach a new stream to a connection
2189 * (Used for outgoing connections)
2190 */
2191static struct conn_stream *h2_attach(struct connection *conn)
2192{
2193 return NULL;
2194}
2195
2196/* callback used to update the mux's polling flags after changing a cs' status.
2197 * The caller (cs_update_mux_polling) will take care of propagating any changes
2198 * to the transport layer.
2199 */
2200static void h2_update_poll(struct conn_stream *cs)
2201{
Willy Tarreau1d393222017-10-17 10:26:19 +02002202 struct h2s *h2s = cs->ctx;
2203
2204 if (!h2s)
2205 return;
2206
Willy Tarreaud7739c82017-10-30 15:38:23 +01002207 /* we may unblock a blocked read */
2208
2209 if (cs->flags & CS_FL_DATA_RD_ENA &&
2210 h2s->h2c->flags & H2_CF_DEM_SFULL && h2s->h2c->dsi == h2s->id) {
2211 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
2212 conn_xprt_want_recv(cs->conn);
2213 }
2214
Willy Tarreau1d393222017-10-17 10:26:19 +02002215 /* Note: the stream and stream-int code doesn't allow us to perform a
2216 * synchronous send() here unfortunately, because this code is called
2217 * as si_update() from the process_stream() context. This means that
2218 * we have to queue the current cs and defer its processing after the
2219 * connection's cs list is processed anyway.
2220 */
2221
2222 if (cs->flags & CS_FL_DATA_WR_ENA) {
2223 if (LIST_ISEMPTY(&h2s->list)) {
2224 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2225 !h2s->h2c->mbuf->o && // not yet subscribed
2226 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2227 conn_xprt_want_send(cs->conn);
2228 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2229 }
2230 }
2231 else if (!LIST_ISEMPTY(&h2s->list)) {
2232 LIST_DEL(&h2s->list);
2233 LIST_INIT(&h2s->list);
2234 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2235 }
2236
2237 /* this can happen from within si_chk_snd() */
2238 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2239 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002240}
2241
2242/*
2243 * Detach the stream from the connection and possibly release the connection.
2244 */
2245static void h2_detach(struct conn_stream *cs)
2246{
Willy Tarreau60935142017-10-16 18:11:19 +02002247 struct h2s *h2s = cs->ctx;
2248 struct h2c *h2c;
2249
2250 cs->ctx = NULL;
2251 if (!h2s)
2252 return;
2253
2254 h2c = h2s->h2c;
2255 h2s->cs = NULL;
2256
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002257 /* this stream may be blocked waiting for some data to leave (possibly
2258 * an ES or RST frame), so orphan it in this case.
2259 */
2260 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2261 return;
2262
Willy Tarreau45f752e2017-10-30 15:44:59 +01002263 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2264 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2265 /* unblock the connection if it was blocked on this
2266 * stream.
2267 */
2268 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2269 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2270 conn_xprt_want_recv(cs->conn);
2271 conn_xprt_want_send(cs->conn);
2272 }
2273
Willy Tarreau60935142017-10-16 18:11:19 +02002274 if (h2s->by_id.node.leaf_p) {
2275 /* h2s still attached to the h2c */
2276 eb32_delete(&h2s->by_id);
2277
2278 /* We don't want to close right now unless we're removing the
2279 * last stream, and either the connection is in error, or it
2280 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002281 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002282 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002283 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2284 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002285 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002286 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2287 (conn_xprt_read0_pending(h2c->conn) ||
2288 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002289 /* no more stream will come, kill it now */
2290 h2_release(h2c->conn);
2291 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002292 else if (h2c->task) {
2293 if (eb_is_empty(&h2c->streams_by_id)) {
2294 h2c->task->expire = tick_add(now_ms, h2c->timeout);
2295 task_queue(h2c->task);
2296 }
2297 else
2298 h2c->task->expire = TICK_ETERNITY;
2299 }
Willy Tarreau60935142017-10-16 18:11:19 +02002300 }
2301 pool_free2(pool2_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002302}
2303
2304static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2305{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002306 struct h2s *h2s = cs->ctx;
2307
2308 if (!mode)
2309 return;
2310
Willy Tarreau721c9742017-11-07 11:05:42 +01002311 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002312 return;
2313
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002314 /* if no outgoing data was seen on this stream, it means it was
2315 * closed with a "tcp-request content" rule that is normally
2316 * used to kill the connection ASAP (eg: limit abuse). In this
2317 * case we send a goaway to close the connection.
2318 */
2319 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2320 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2321 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2322 return;
2323
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002324 if (h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002325 return;
2326
2327 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2328 conn_xprt_want_send(cs->conn);
2329
2330 h2s->st = H2_SS_CLOSED;
Willy Tarreau62f52692017-10-08 23:01:42 +02002331}
2332
2333static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2334{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002335 struct h2s *h2s = cs->ctx;
2336
Willy Tarreau721c9742017-11-07 11:05:42 +01002337 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002338 return;
2339
Willy Tarreau67434202017-11-06 20:20:51 +01002340 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002341 /* we can cleanly close using an empty data frame only after headers */
2342
2343 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2344 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002345 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002346
2347 if (h2s->st == H2_SS_HREM)
2348 h2s->st = H2_SS_CLOSED;
2349 else
2350 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002351 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002352 /* if no outgoing data was seen on this stream, it means it was
2353 * closed with a "tcp-request content" rule that is normally
2354 * used to kill the connection ASAP (eg: limit abuse). In this
2355 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002356 */
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002357 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2358 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002359 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2360 return;
2361
Willy Tarreau58e32082017-11-07 14:41:09 +01002362 if (!(h2s->flags & H2_SF_RST_SENT) &&
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002363 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002364 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002365
2366 h2s->st = H2_SS_CLOSED;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002367 }
2368
2369 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2370 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002371}
2372
Willy Tarreau13278b42017-10-13 19:23:14 +02002373/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2374 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2375 * proceed. Stream errors are reported in h2s->errcode and connection errors
2376 * in h2c->errcode. The caller must already have checked the frame header and
2377 * ensured that the frame was complete or the buffer full.
2378 */
2379static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2380{
2381 struct h2c *h2c = h2s->h2c;
2382 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002383 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002384 int flen = h2c->dfl;
2385 int outlen = 0;
2386 int wrap;
2387 int try;
2388
2389 if (!h2c->dfl) {
2390 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
2391 return 0;
2392 }
2393
2394 /* if the input buffer wraps, take a temporary copy of it (rare) */
2395 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2396 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002397 copy = alloc_trash_chunk();
2398 if (!copy) {
2399 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2400 goto fail;
2401 }
2402 memcpy(copy->str, h2c->dbuf->p, wrap);
2403 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2404 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002405 }
2406
2407 /* The padlen is the first byte before data, and the padding appears
2408 * after data. padlen+data+padding are included in flen.
2409 */
2410 if (h2c->dff & H2_F_HEADERS_PADDED) {
2411 if (*hdrs >= flen) {
2412 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2413 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002414 return 0;
2415 }
2416 flen -= *hdrs + 1;
2417 hdrs += 1; // skip Pad Length
2418 }
2419
2420 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2421 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
2422 hdrs += 5; // stream dep = 4, weight = 1
2423 flen -= 5;
2424 }
2425
2426 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2427 * don't support this for now and can't even decompress so we have to
2428 * break the connection.
2429 */
2430 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2431 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002432 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002433 }
2434
2435 do {
2436 /* first check if we have some room after p+i */
2437 try = buf->data + buf->size - (buf->p + buf->i);
2438
2439 /* otherwise continue between data and p-o */
2440 if (try <= 0) {
2441 try = buf->p - (buf->data + buf->o);
2442 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002443 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002444 }
2445 if (try > count)
2446 try = count;
2447
2448 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, bi_end(buf), try);
2449 if (outlen == -HPACK_ERR_TOO_LARGE) {
2450 if (buffer_space_wraps(buf)) {
2451 /* it doesn't fit and the buffer is fragmented,
2452 * so let's defragment it and try again.
2453 */
2454 buffer_slow_realign(buf);
2455 }
2456 else if (buf->o) {
2457 /* need to let the output buffer flush and
2458 * mark the buffer for later wake up.
2459 */
Willy Tarreau68dd9852017-07-03 14:44:26 +02002460 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002461 }
2462 else {
2463 /* no other way around */
2464 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002465 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002466 }
2467 }
2468 else if (outlen < 0) {
2469 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002470 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002471 }
2472 } while (outlen < 0);
2473
2474 /* now consume the input data */
2475 bi_del(h2c->dbuf, h2c->dfl);
2476 h2c->st0 = H2_CS_FRAME_H;
2477 buf->i += outlen;
2478
2479 /* don't send it before returning data!
2480 * FIXME: should we instead try to send it much later, after the
2481 * response ? This would require that we keep a copy of it in h2s.
2482 */
2483 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2484 h2s->cs->flags |= CS_FL_EOS;
2485 h2s->flags |= H2_SF_ES_RCVD;
2486 }
2487
Willy Tarreau68dd9852017-07-03 14:44:26 +02002488 leave:
2489 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002490 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002491 fail:
2492 outlen = 0;
2493 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002494}
2495
Willy Tarreau454f9052017-10-26 19:40:35 +02002496/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2497 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2498 * in use, a new chunk is emitted for each frame. This is supposed to fit
2499 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2500 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2501 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2502 * parser state is automatically updated. Returns the number of bytes emitted
2503 * if > 0, or 0 if it couldn't proceed. Stream errors are reported in
2504 * h2s->errcode and connection errors in h2c->errcode. The caller must already
2505 * have checked the frame header and ensured that the frame was complete or the
2506 * buffer full. It changes the frame state to FRAME_A once done.
2507 */
2508static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2509{
2510 struct h2c *h2c = h2s->h2c;
2511 int block1, block2;
2512 unsigned int flen = h2c->dfl;
2513 unsigned int padlen = 0;
2514 int offset = 0;
2515
2516 if (h2c->dbuf->i < flen)
2517 return 0;
2518
2519 /* The padlen is the first byte before data, and the padding appears
2520 * after data. padlen+data+padding are included in flen.
2521 */
2522 if (h2c->dff & H2_F_HEADERS_PADDED) {
2523 padlen = *(uint8_t *)bi_ptr(h2c->dbuf);
2524 if (padlen >= flen) {
2525 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2526 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002527 return 0;
2528 }
2529 flen -= padlen + 1;
2530 offset = 1; // skip Pad Length
2531 }
2532
2533 /* does it fit in output buffer or should we wait ? */
2534 if (buf->i + buf->o + flen > buf->size) {
2535 h2c->flags |= H2_CF_DEM_SFULL;
2536 return 0;
2537 }
2538
2539 /* Block1 is the length of the first block before the buffer wraps,
2540 * block2 is the optional second block to reach the end of the frame.
2541 */
2542 block1 = bi_contig_data(h2c->dbuf);
2543 if (block1 > offset + flen)
2544 block1 = offset + flen;
2545 block1 -= offset; // skip Pad Length
2546 block2 = flen - block1;
2547
2548 if (block1)
2549 bi_putblk(buf, b_ptr(h2c->dbuf, offset), block1);
2550
2551 if (block2)
2552 bi_putblk(buf, b_ptr(h2c->dbuf, offset + block1), block2);
2553
2554 /* now mark the input data as consumed (will be deleted from the buffer
2555 * by the caller when seeing FRAME_A after sending the window update).
2556 */
2557 h2c->rcvd_c += h2c->dfl;
2558 h2c->rcvd_s += h2c->dfl; // warning, this can also affect the closed streams!
2559 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2560
2561 /* don't send it before returning data!
2562 * FIXME: should we instead try to send it much later, after the
2563 * response ? This would require that we keep a copy of it in h2s.
2564 */
2565 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2566 h2s->cs->flags |= CS_FL_EOS;
2567 h2s->flags |= H2_SF_ES_RCVD;
2568 }
2569
2570 return flen;
2571}
2572
Willy Tarreau62f52692017-10-08 23:01:42 +02002573/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002574 * Called from the upper layer to get more data, up to <count> bytes. The
2575 * caller is responsible for never asking for more data than what is available
2576 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002577 */
2578static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2579{
Willy Tarreau13278b42017-10-13 19:23:14 +02002580 struct h2s *h2s = cs->ctx;
2581 struct h2c *h2c = h2s->h2c;
2582 int ret = 0;
2583
2584 if (h2c->st0 != H2_CS_FRAME_P)
2585 return 0; // no pre-parsed frame yet
2586
2587 if (h2c->dsi != h2s->id)
2588 return 0; // not for us
2589
2590 if (!h2c->dbuf->size)
2591 return 0; // empty buffer
2592
2593 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2594 return 0; // incomplete input frame
2595
2596 switch (h2c->dft) {
2597 case H2_FT_HEADERS:
2598 ret = h2_frt_decode_headers(h2s, buf, count);
2599 break;
2600
Willy Tarreau454f9052017-10-26 19:40:35 +02002601 case H2_FT_DATA:
2602 ret = h2_frt_transfer_data(h2s, buf, count);
2603 break;
2604
Willy Tarreau13278b42017-10-13 19:23:14 +02002605 default:
2606 ret = 0;
2607 }
2608 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002609}
2610
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002611/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2612 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2613 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2614 * to the number of buffer bytes consumed.
2615 */
2616static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2617{
2618 struct http_hdr list[MAX_HTTP_HDR];
2619 struct h2c *h2c = h2s->h2c;
2620 struct h1m *h1m = &h2s->res;
2621 struct chunk outbuf;
2622 int es_now = 0;
2623 int ret = 0;
2624 int hdr;
2625
2626 if (h2c_mux_busy(h2c, h2s)) {
2627 h2s->flags |= H2_SF_BLK_MBUSY;
2628 return 0;
2629 }
2630
2631 if (!h2_get_mbuf(h2c)) {
2632 h2c->flags |= H2_CF_MUX_MALLOC;
2633 h2s->flags |= H2_SF_BLK_MROOM;
2634 return 0;
2635 }
2636
2637 /* First, try to parse the H1 response and index it into <list>.
2638 * NOTE! Since it comes from haproxy, we *know* that a response header
2639 * block does not wrap and we can safely read it this way without
2640 * having to realign the buffer.
2641 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002642 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002643 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2644 list, sizeof(list)/sizeof(list[0]), h1m);
2645 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002646 /* incomplete or invalid response, this is abnormal coming from
2647 * haproxy and may only result in a bad errorfile or bad Lua code
2648 * so that won't be fixed, raise an error now.
2649 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002650 * FIXME: we should instead add the ability to only return a
2651 * 502 bad gateway. But in theory this is not supposed to
2652 * happen.
2653 */
2654 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2655 ret = 0;
2656 goto end;
2657 }
2658
2659 chunk_reset(&outbuf);
2660
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002661 try_again:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002662 while (1) {
2663 outbuf.str = bo_end(h2c->mbuf);
2664 outbuf.size = bo_contig_space(h2c->mbuf);
2665 outbuf.len = 0;
2666
2667 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2668 break;
2669 realign_again:
2670 buffer_slow_realign(h2c->mbuf);
2671 }
2672
2673 if (outbuf.size < 9) {
2674 h2c->flags |= H2_CF_MUX_MFULL;
2675 h2s->flags |= H2_SF_BLK_MROOM;
2676 ret = 0;
2677 goto end;
2678 }
2679
2680 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2681 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2682 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2683 outbuf.len = 9;
2684
2685 /* encode status, which necessarily is the first one */
2686 if (outbuf.len < outbuf.size && h1m->status == 200)
2687 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2688 else if (outbuf.len < outbuf.size && h1m->status == 304)
2689 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002690 else if (unlikely(list[0].v.len != 3)) {
2691 /* this is an unparsable response */
2692 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2693 ret = 0;
2694 goto end;
2695 }
2696 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002697 /* basic encoding of the status code */
2698 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2699 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2700 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2701 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2702 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2703 }
2704 else {
2705 if (buffer_space_wraps(h2c->mbuf))
2706 goto realign_again;
2707
2708 h2c->flags |= H2_CF_MUX_MFULL;
2709 h2s->flags |= H2_SF_BLK_MROOM;
2710 ret = 0;
2711 goto end;
2712 }
2713
2714 /* encode all headers, stop at empty name */
2715 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002716 /* these ones do not exist in H2 and must be dropped. But if we
2717 * see "connection: close", we also perform a graceful shutdown
2718 * on the connection. Note that the match is not perfect but it
2719 * is sufficient for dealing with some deny rules.
2720 */
2721 if (isteq(list[hdr].n, ist("connection"))) {
2722 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2723 word_match(list[hdr].v.ptr, list[hdr].v.len, "close", 5)) {
2724 if (h2c->last_sid < 0)
2725 h2c->last_sid = (1U << 31) - 1;
2726 if (h2c_send_goaway_error(h2c, h2s) <= 0) {
2727 ret = 0;
2728 goto end;
2729 }
2730 /* OK sent, but this changed the output buffer's
2731 * contents hence the write position.
2732 */
2733 goto try_again;
2734 }
2735 continue;
2736 }
2737 else if (isteq(list[hdr].n, ist("proxy-connection")) ||
2738 isteq(list[hdr].n, ist("keep-alive")) ||
2739 isteq(list[hdr].n, ist("upgrade")) ||
2740 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002741 continue;
2742
2743 if (isteq(list[hdr].n, ist("")))
2744 break; // end
2745
2746 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2747 /* output full */
2748 if (buffer_space_wraps(h2c->mbuf))
2749 goto realign_again;
2750
2751 h2c->flags |= H2_CF_MUX_MFULL;
2752 h2s->flags |= H2_SF_BLK_MROOM;
2753 ret = 0;
2754 goto end;
2755 }
2756 }
2757
2758 /* we may need to add END_STREAM */
2759 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2760 es_now = 1;
2761
2762 /* update the frame's size */
2763 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2764
2765 if (es_now)
2766 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2767
2768 /* consume incoming H1 response */
2769 bo_del(buf, ret);
2770
2771 /* commit the H2 response */
2772 h2c->mbuf->o += outbuf.len;
2773 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01002774 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002775
2776 /* for now we don't implemented CONTINUATION, so we wait for a
2777 * body or directly end in TRL2.
2778 */
2779 if (es_now) {
2780 h1m->state = HTTP_MSG_DONE;
2781 h2s->flags |= H2_SF_ES_SENT;
2782 if (h2s->st == H2_SS_OPEN)
2783 h2s->st = H2_SS_HLOC;
2784 else
2785 h2s->st = H2_SS_CLOSED;
2786 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01002787 else if (h1m->status >= 100 && h1m->status < 200) {
2788 h1m->state = HTTP_MSG_RPBEFORE;
2789 h1m->status = 0;
2790 h1m->flags = 0;
2791 goto next_header_block;
2792 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002793 else
2794 h1m->state = (h1m->flags & H1_MF_CLEN) ? HTTP_MSG_BODY : HTTP_MSG_CHUNK_SIZE;
2795
2796 end:
2797 //fprintf(stderr, "[%d] sent simple H2 response (sid=%d) = %d bytes (%d in, ep=%u, es=%s)\n", h2c->st0, h2s->id, outbuf.len, ret, h1m->err_pos, h1_msg_state_str(h1m->err_state));
2798 return ret;
2799}
2800
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002801/* Try to send a DATA frame matching HTTP/1 response present in the response
2802 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
2803 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
2804 * corresponds to the number of buffer bytes consumed.
2805 */
2806static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
2807{
2808 struct h2c *h2c = h2s->h2c;
2809 struct h1m *h1m = &h2s->res;
2810 struct chunk outbuf;
2811 int ret = 0;
2812 int total = 0;
2813 int es_now = 0;
2814 int size = 0;
2815 char *blk1, *blk2;
2816 int len1, len2;
2817
2818 if (h2c_mux_busy(h2c, h2s)) {
2819 h2s->flags |= H2_SF_BLK_MBUSY;
2820 goto end;
2821 }
2822
2823 if (!h2_get_mbuf(h2c)) {
2824 h2c->flags |= H2_CF_MUX_MALLOC;
2825 h2s->flags |= H2_SF_BLK_MROOM;
2826 goto end;
2827 }
2828
2829 new_frame:
2830 if (!buf->o)
2831 goto end;
2832
2833 chunk_reset(&outbuf);
2834
2835 while (1) {
2836 outbuf.str = bo_end(h2c->mbuf);
2837 outbuf.size = bo_contig_space(h2c->mbuf);
2838 outbuf.len = 0;
2839
2840 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2841 break;
2842 realign_again:
2843 buffer_slow_realign(h2c->mbuf);
2844 }
2845
2846 if (outbuf.size < 9) {
2847 h2c->flags |= H2_CF_MUX_MFULL;
2848 h2s->flags |= H2_SF_BLK_MROOM;
2849 goto end;
2850 }
2851
2852 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
2853 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
2854 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2855 outbuf.len = 9;
2856
2857 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
2858 case 0: /* no content length, read till SHUTW */
2859 size = buf->o;
2860 break;
2861 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
2862 size = buf->o;
2863 if ((long long)size > h1m->curr_len)
2864 size = h1m->curr_len;
2865 break;
2866 default: /* te:chunked : parse chunks */
2867 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
2868 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
2869 if (!ret)
2870 goto end;
2871
2872 if (ret < 0) {
2873 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2874 h1m->err_pos = ret;
2875 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2876 goto end;
2877 }
2878 bo_del(buf, ret);
2879 total += ret;
2880 h1m->state = HTTP_MSG_CHUNK_SIZE;
2881 }
2882
2883 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
2884 unsigned int chunk;
2885
2886 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
2887 if (!ret)
2888 goto end;
2889
2890 if (ret < 0) {
2891 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2892 h1m->err_pos = ret;
2893 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2894 goto end;
2895 }
2896
2897 size = chunk;
2898 h1m->curr_len = chunk;
2899 h1m->body_len += chunk;
2900 bo_del(buf, ret);
2901 total += ret;
2902 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
2903 if (!size)
2904 goto send_empty;
2905 }
2906
2907 /* in MSG_DATA state, continue below */
2908 size = h1m->curr_len;
2909 break;
2910 }
2911
2912 /* we have in <size> the exact number of bytes we need to copy from
2913 * the H1 buffer. We need to check this against the connection's and
2914 * the stream's send windows, and to ensure that this fits in the max
2915 * frame size and in the buffer's available space minus 9 bytes (for
2916 * the frame header). The connection's flow control is applied last so
2917 * that we can use a separate list of streams which are immediately
2918 * unblocked on window opening. Note: we don't implement padding.
2919 */
2920
2921 if (size > buf->o)
2922 size = buf->o;
2923
2924 if (size > h2s->mws)
2925 size = h2s->mws;
2926
2927 if (size <= 0) {
2928 h2s->flags |= H2_SF_BLK_SFCTL;
2929 goto end;
2930 }
2931
2932 if (h2c->mfs && size > h2c->mfs)
2933 size = h2c->mfs;
2934
2935 if (size + 9 > outbuf.size) {
2936 /* we have an opportunity for enlarging the too small
2937 * available space, let's try.
2938 */
2939 if (buffer_space_wraps(h2c->mbuf))
2940 goto realign_again;
2941 size = outbuf.size - 9;
2942 }
2943
2944 if (size <= 0) {
2945 h2c->flags |= H2_CF_MUX_MFULL;
2946 h2s->flags |= H2_SF_BLK_MROOM;
2947 goto end;
2948 }
2949
2950 if (size > h2c->mws)
2951 size = h2c->mws;
2952
2953 if (size <= 0) {
2954 h2s->flags |= H2_SF_BLK_MFCTL;
2955 goto end;
2956 }
2957
2958 /* copy whatever we can */
2959 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
2960 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
2961 if (ret == 1)
2962 len2 = 0;
2963
2964 if (!ret || len1 + len2 < size) {
2965 /* FIXME: must normally never happen */
2966 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2967 goto end;
2968 }
2969
2970 /* limit len1/len2 to size */
2971 if (len1 + len2 > size) {
2972 int sub = len1 + len2 - size;
2973
2974 if (len2 > sub)
2975 len2 -= sub;
2976 else {
2977 sub -= len2;
2978 len2 = 0;
2979 len1 -= sub;
2980 }
2981 }
2982
2983 /* now let's copy this this into the output buffer */
2984 memcpy(outbuf.str + 9, blk1, len1);
2985 if (len2)
2986 memcpy(outbuf.str + 9 + len1, blk2, len2);
2987
2988 send_empty:
2989 /* we may need to add END_STREAM */
2990 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
2991 * could rely on the MSG_MORE flag as a hint for this ?
2992 */
2993 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
2994 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
2995 es_now = 1;
2996
2997 /* update the frame's size */
2998 h2_set_frame_size(outbuf.str, size);
2999
3000 if (es_now)
3001 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3002
3003 /* commit the H2 response */
3004 h2c->mbuf->o += size + 9;
3005 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3006
3007 /* consume incoming H1 response */
3008 if (size > 0) {
3009 bo_del(buf, size);
3010 total += size;
3011 h1m->curr_len -= size;
3012 h2s->mws -= size;
3013 h2c->mws -= size;
3014
3015 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3016 h1m->state = HTTP_MSG_CHUNK_CRLF;
3017 goto new_frame;
3018 }
3019 }
3020
3021 if (es_now) {
3022 if (h2s->st == H2_SS_OPEN)
3023 h2s->st = H2_SS_HLOC;
3024 else
3025 h2s->st = H2_SS_CLOSED;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003026
3027 if (!(h1m->flags & H1_MF_CHNK))
3028 h1m->state = HTTP_MSG_DONE;
3029
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003030 h2s->flags |= H2_SF_ES_SENT;
3031 }
3032
3033 end:
3034 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%d in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) buf->o=%d", h2c->st0, h2s->id, size+9, total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, buf->o);
3035 return total;
3036}
3037
Willy Tarreau62f52692017-10-08 23:01:42 +02003038/* Called from the upper layer, to send data */
3039static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3040{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003041 struct h2s *h2s = cs->ctx;
3042 int total = 0;
3043
Willy Tarreauc4312d32017-11-07 12:01:53 +01003044 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3045 h2s->flags |= H2_SF_OUTGOING_DATA;
3046
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003047 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3048 if (h2s->res.state < HTTP_MSG_BODY) {
3049 total += h2s_frt_make_resp_headers(h2s, buf);
3050
3051 if (h2s->st == H2_SS_ERROR)
3052 break;
3053
3054 if (h2s->flags & H2_SF_BLK_ANY)
3055 break;
3056 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003057 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3058 total += h2s_frt_make_resp_data(h2s, buf);
3059
3060 if (h2s->st == H2_SS_ERROR)
3061 break;
3062
3063 if (h2s->flags & H2_SF_BLK_ANY)
3064 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003065 }
3066 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3067 /* consume the trailers if any (we don't forward them for now) */
3068 int count = h1_measure_trailers(buf);
3069
3070 if (unlikely(count <= 0)) {
3071 if (count < 0)
3072 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3073 break;
3074 }
3075 total += count;
3076 bo_del(buf, count);
3077 h2s->res.state = HTTP_MSG_DONE;
3078 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003079 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003080 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003081 cs->flags |= CS_FL_ERROR;
3082 break;
3083 }
3084 }
3085
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003086 /* RST are sent similarly to frame acks */
3087 if (h2s->st == H2_SS_ERROR) {
3088 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003089 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003090 h2s->st = H2_SS_CLOSED;
3091 }
3092
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003093 if (h2s->flags & H2_SF_BLK_SFCTL) {
3094 /* stream flow control, quit the list */
3095 LIST_DEL(&h2s->list);
3096 LIST_INIT(&h2s->list);
3097 }
3098
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003099 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003100}
3101
3102
3103/*******************************************************/
3104/* functions below are dedicated to the config parsers */
3105/*******************************************************/
3106
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003107/* config parser for global "tune.h2.header-table-size" */
3108static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3109 struct proxy *defpx, const char *file, int line,
3110 char **err)
3111{
3112 if (too_many_args(1, args, err, NULL))
3113 return -1;
3114
3115 h2_settings_header_table_size = atoi(args[1]);
3116 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3117 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3118 return -1;
3119 }
3120 return 0;
3121}
Willy Tarreau62f52692017-10-08 23:01:42 +02003122
Willy Tarreaue6baec02017-07-27 11:45:11 +02003123/* config parser for global "tune.h2.initial-window-size" */
3124static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3125 struct proxy *defpx, const char *file, int line,
3126 char **err)
3127{
3128 if (too_many_args(1, args, err, NULL))
3129 return -1;
3130
3131 h2_settings_initial_window_size = atoi(args[1]);
3132 if (h2_settings_initial_window_size < 0) {
3133 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3134 return -1;
3135 }
3136 return 0;
3137}
3138
Willy Tarreau5242ef82017-07-27 11:47:28 +02003139/* config parser for global "tune.h2.max-concurrent-streams" */
3140static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3141 struct proxy *defpx, const char *file, int line,
3142 char **err)
3143{
3144 if (too_many_args(1, args, err, NULL))
3145 return -1;
3146
3147 h2_settings_max_concurrent_streams = atoi(args[1]);
3148 if (h2_settings_max_concurrent_streams < 0) {
3149 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3150 return -1;
3151 }
3152 return 0;
3153}
3154
Willy Tarreau62f52692017-10-08 23:01:42 +02003155
3156/****************************************/
3157/* MUX initialization and instanciation */
3158/***************************************/
3159
3160/* The mux operations */
3161const struct mux_ops h2_ops = {
3162 .init = h2_init,
3163 .recv = h2_recv,
3164 .send = h2_send,
3165 .wake = h2_wake,
3166 .update_poll = h2_update_poll,
3167 .rcv_buf = h2_rcv_buf,
3168 .snd_buf = h2_snd_buf,
3169 .attach = h2_attach,
3170 .detach = h2_detach,
3171 .shutr = h2_shutr,
3172 .shutw = h2_shutw,
Willy Tarreau62f52692017-10-08 23:01:42 +02003173 .name = "H2",
3174};
3175
3176/* ALPN selection : this mux registers ALPN tolen "h2" */
3177static struct alpn_mux_list alpn_mux_h2 =
3178 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3179
3180/* config keyword parsers */
3181static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003182 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003183 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003184 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003185 { 0, NULL, NULL }
3186}};
3187
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003188static void __h2_deinit(void)
3189{
Willy Tarreau18312642017-10-11 07:57:07 +02003190 pool_destroy2(pool2_h2s);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003191 pool_destroy2(pool2_h2c);
3192}
3193
Willy Tarreau62f52692017-10-08 23:01:42 +02003194__attribute__((constructor))
3195static void __h2_init(void)
3196{
3197 alpn_register_mux(&alpn_mux_h2);
3198 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003199 hap_register_post_deinit(__h2_deinit);
3200 pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
Willy Tarreau18312642017-10-11 07:57:07 +02003201 pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003202}