blob: 7bb51ea4185da9408ef835f78c611b7095bab20d [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 */
Willy Tarreaubafbe012017-11-24 17:34:44 +010033static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020034/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010035static struct pool_head *pool_head_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
Willy Tarreau315d8072017-12-10 22:17:57 +010045/* Flags indicating why writing to the demux is blocked.
46 * The first two ones directly affect the ability for the mux to receive data
47 * from the connection. The other ones affect the mux's ability to demux
48 * received data.
49 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020050#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
51#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010052
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020053#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
54#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
55#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
56#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010057#define H2_CF_DEM_BLOCK_ANY 0x000000F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
60#define H2_CF_GOAWAY_SENT 0x00000100 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00000200 // a GOAWAY frame failed to be sent
62
63
Willy Tarreau5ab6b572017-09-22 08:05:00 +020064/* H2 connection state, in h2c->st0 */
65enum h2_cs {
66 H2_CS_PREFACE, // init done, waiting for connection preface
67 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
68 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
69 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010070 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
71 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020072 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
73 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
74 H2_CS_ENTRIES // must be last
75} __attribute__((packed));
76
77/* H2 connection descriptor */
78struct h2c {
79 struct connection *conn;
80
81 enum h2_cs st0; /* mux state */
82 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
83
84 /* 16 bit hole here */
85 uint32_t flags; /* connection flags: H2_CF_* */
86 int32_t max_id; /* highest ID known on this connection, <0 before preface */
87 uint32_t rcvd_c; /* newly received data to ACK for the connection */
88 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
89
90 /* states for the demux direction */
91 struct hpack_dht *ddht; /* demux dynamic header table */
92 struct buffer *dbuf; /* demux buffer */
93
94 int32_t dsi; /* demux stream ID (<0 = idle) */
95 int32_t dfl; /* demux frame length (if dsi >= 0) */
96 int8_t dft; /* demux frame type (if dsi >= 0) */
97 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010098 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
99 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200100 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
101
102 /* states for the mux direction */
103 struct buffer *mbuf; /* mux buffer */
104 int32_t msi; /* mux stream ID (<0 = idle) */
105 int32_t mfl; /* mux frame length (if dsi >= 0) */
106 int8_t mft; /* mux frame type (if dsi >= 0) */
107 int8_t mff; /* mux frame flags (if dsi >= 0) */
108 /* 16 bit hole here */
109 int32_t miw; /* mux initial window size for all new streams */
110 int32_t mws; /* mux window size. Can be negative. */
111 int32_t mfs; /* mux's max frame size */
112
Willy Tarreauea392822017-10-31 10:02:25 +0100113 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100114 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100115 unsigned int nb_streams; /* number of streams in the tree */
116 /* 32 bit hole here */
Willy Tarreauea392822017-10-31 10:02:25 +0100117 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200118 struct eb_root streams_by_id; /* all active streams by their ID */
119 struct list send_list; /* list of blocked streams requesting to send */
120 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200121 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +0200122 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200123};
124
Willy Tarreau18312642017-10-11 07:57:07 +0200125/* H2 stream state, in h2s->st */
126enum h2_ss {
127 H2_SS_IDLE = 0, // idle
128 H2_SS_RLOC, // reserved(local)
129 H2_SS_RREM, // reserved(remote)
130 H2_SS_OPEN, // open
131 H2_SS_HREM, // half-closed(remote)
132 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200133 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200134 H2_SS_CLOSED, // closed
135 H2_SS_ENTRIES // must be last
136} __attribute__((packed));
137
138/* HTTP/2 stream flags (32 bit), in h2s->flags */
139#define H2_SF_NONE 0x00000000
140#define H2_SF_ES_RCVD 0x00000001
141#define H2_SF_ES_SENT 0x00000002
142
143#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
144#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
145
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200146/* stream flags indicating the reason the stream is blocked */
147#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
148#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
149#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
150#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
151#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
152
Willy Tarreau454f9052017-10-26 19:40:35 +0200153/* stream flags indicating how data is supposed to be sent */
154#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
155#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
156
157/* step we're currently in when sending chunks. This is needed because we may
158 * have to transfer chunks as large as a full buffer so there's no room left
159 * for size nor crlf around.
160 */
161#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
162#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
163#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
164
165#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
166
Willy Tarreau67434202017-11-06 20:20:51 +0100167#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100168#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100169
Willy Tarreau18312642017-10-11 07:57:07 +0200170/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
171 * it is being processed in the internal HTTP representation (H1 for now).
172 */
173struct h2s {
174 struct conn_stream *cs;
175 struct h2c *h2c;
176 struct h1m req, res; /* request and response parser state for H1 */
177 struct eb32_node by_id; /* place in h2c's streams_by_id */
178 struct list list; /* position in active/blocked lists if blocked>0 */
179 int32_t id; /* stream ID */
180 uint32_t flags; /* H2_SF_* */
181 int mws; /* mux window size for this stream */
182 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
183 enum h2_ss st;
184};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200185
Willy Tarreauc6405142017-09-21 20:23:50 +0200186/* descriptor for an h2 frame header */
187struct h2_fh {
188 uint32_t len; /* length, host order, 24 bits */
189 uint32_t sid; /* stream id, host order, 31 bits */
190 uint8_t ft; /* frame type */
191 uint8_t ff; /* frame flags */
192};
193
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200194/* a few settings from the global section */
195static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200196static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200197static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200198
Willy Tarreau2a856182017-05-16 15:20:39 +0200199/* a dmumy closed stream */
200static const struct h2s *h2_closed_stream = &(const struct h2s){
201 .cs = NULL,
202 .h2c = NULL,
203 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100204 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100205 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200206 .id = 0,
207};
208
209/* and a dummy idle stream for use with any unannounced stream */
210static const struct h2s *h2_idle_stream = &(const struct h2s){
211 .cs = NULL,
212 .h2c = NULL,
213 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100214 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200215 .id = 0,
216};
217
Willy Tarreauea392822017-10-31 10:02:25 +0100218static struct task *h2_timeout_task(struct task *t);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200219
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200220/*****************************************************/
221/* functions below are for dynamic buffer management */
222/*****************************************************/
223
Willy Tarreau315d8072017-12-10 22:17:57 +0100224/* indicates whether or not the we may call the h2_recv() function to attempt
225 * to receive data into the buffer and/or demux pending data. The condition is
226 * a bit complex due to some API limits for now. The rules are the following :
227 * - if an error or a shutdown was detected on the connection and the buffer
228 * is empty, we must not attempt to receive
229 * - if the demux buf failed to be allocated, we must not try to receive and
230 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100231 * - if no flag indicates a blocking condition, we may attempt to receive,
232 * regardless of whether the demux buffer is full or not, so that only
233 * de demux part decides whether or not to block. This is needed because
234 * the connection API indeed prevents us from re-enabling receipt that is
235 * already enabled in a polled state, so we must always immediately stop
236 * as soon as the demux can't proceed so as never to hit an end of read
237 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100238 * - otherwise must may not attempt
239 */
240static inline int h2_recv_allowed(const struct h2c *h2c)
241{
242 if (h2c->dbuf->i == 0 &&
243 (h2c->st0 >= H2_CS_ERROR ||
244 h2c->conn->flags & CO_FL_ERROR ||
245 conn_xprt_read0_pending(h2c->conn)))
246 return 0;
247
248 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100249 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100250 return 1;
251
252 return 0;
253}
254
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200255/* re-enables receiving on mux <target> after a buffer was allocated. It returns
256 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
257 * if it's impossible to wake up and we prefer to be woken up later.
258 */
259static int h2_dbuf_available(void *target)
260{
261 struct h2c *h2c = target;
262
263 /* take the buffer now as we'll get scheduled waiting for ->wake() */
264 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200265 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100266 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200267 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200268 return 1;
269 }
270 return 0;
271}
272
273static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
274{
275 struct buffer *buf = NULL;
276
277 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
278 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
279 h2c->dbuf_wait.target = h2c->conn;
280 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100281 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200282 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100283 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200284 __conn_xprt_stop_recv(h2c->conn);
285 }
286 return buf;
287}
288
289static inline void h2_release_dbuf(struct h2c *h2c)
290{
291 if (h2c->dbuf->size) {
292 b_free(&h2c->dbuf);
293 offer_buffers(h2c->dbuf_wait.target,
294 tasks_run_queue + applets_active_queue);
295 }
296}
297
Willy Tarreau14398122017-09-22 14:26:04 +0200298/* re-enables sending on mux <target> after a buffer was allocated. It returns
299 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
300 * if it's impossible to wake up and we prefer to be woken up later.
301 */
302static int h2_mbuf_available(void *target)
303{
304 struct h2c *h2c = target;
305
306 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
307 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200308 if (h2c->flags & H2_CF_MUX_MALLOC) {
309 h2c->flags &= ~H2_CF_MUX_MALLOC;
310 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
311 conn_xprt_want_send(h2c->conn);
312 }
313
314 if (h2c->flags & H2_CF_DEM_MROOM) {
315 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100316 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200317 conn_xprt_want_recv(h2c->conn);
318 }
319
Willy Tarreau14398122017-09-22 14:26:04 +0200320 /* FIXME: we should in fact call something like h2_update_poll()
321 * now to recompte the polling. For now it will be enough like
322 * this.
323 */
Willy Tarreau14398122017-09-22 14:26:04 +0200324 return 1;
325 }
326 return 0;
327}
328
329static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
330{
331 struct buffer *buf = NULL;
332
333 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
334 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
335 h2c->mbuf_wait.target = h2c;
336 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100337 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200338 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100339 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200340
341 /* FIXME: we should in fact only block the direction being
342 * currently used. For now it will be enough like this.
343 */
344 __conn_xprt_stop_send(h2c->conn);
345 __conn_xprt_stop_recv(h2c->conn);
346 }
347 return buf;
348}
349
350static inline void h2_release_mbuf(struct h2c *h2c)
351{
352 if (h2c->mbuf->size) {
353 b_free(&h2c->mbuf);
354 offer_buffers(h2c->mbuf_wait.target,
355 tasks_run_queue + applets_active_queue);
356 }
357}
358
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200359
Willy Tarreau62f52692017-10-08 23:01:42 +0200360/*****************************************************************/
361/* functions below are dedicated to the mux setup and management */
362/*****************************************************************/
363
Willy Tarreau32218eb2017-09-22 08:07:25 +0200364/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
365static int h2c_frt_init(struct connection *conn)
366{
367 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100368 struct task *t = NULL;
369 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200370
Willy Tarreaubafbe012017-11-24 17:34:44 +0100371 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200372 if (!h2c)
373 goto fail;
374
Willy Tarreau3f133572017-10-31 19:21:06 +0100375
Willy Tarreau599391a2017-11-24 10:16:00 +0100376 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
377 if (tick_isset(sess->fe->timeout.clientfin))
378 h2c->shut_timeout = sess->fe->timeout.clientfin;
379
Willy Tarreau33400292017-11-05 11:23:40 +0100380 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100381 if (tick_isset(h2c->timeout)) {
382 t = task_new(tid_bit);
383 if (!t)
384 goto fail;
385
386 h2c->task = t;
387 t->process = h2_timeout_task;
388 t->context = h2c;
389 t->expire = tick_add(now_ms, h2c->timeout);
390 }
Willy Tarreauea392822017-10-31 10:02:25 +0100391
Willy Tarreau32218eb2017-09-22 08:07:25 +0200392 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
393 if (!h2c->ddht)
394 goto fail;
395
396 /* Initialise the context. */
397 h2c->st0 = H2_CS_PREFACE;
398 h2c->conn = conn;
399 h2c->max_id = -1;
400 h2c->errcode = H2_ERR_NO_ERROR;
401 h2c->flags = H2_CF_NONE;
402 h2c->rcvd_c = 0;
403 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100404 h2c->nb_streams = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200405
406 h2c->dbuf = &buf_empty;
407 h2c->dsi = -1;
408 h2c->msi = -1;
409 h2c->last_sid = -1;
410
411 h2c->mbuf = &buf_empty;
412 h2c->miw = 65535; /* mux initial window size */
413 h2c->mws = 65535; /* mux window size */
414 h2c->mfs = 16384; /* initial max frame size */
415 h2c->streams_by_id = EB_ROOT_UNIQUE;
416 LIST_INIT(&h2c->send_list);
417 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200418 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200419 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420 conn->mux_ctx = h2c;
421
Willy Tarreau3f133572017-10-31 19:21:06 +0100422 if (t)
423 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200424 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100425
Willy Tarreau32218eb2017-09-22 08:07:25 +0200426 /* mux->wake will be called soon to complete the operation */
427 return 0;
428 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100429 if (t)
430 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100431 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200432 return -1;
433}
434
Willy Tarreau62f52692017-10-08 23:01:42 +0200435/* Initialize the mux once it's attached. For outgoing connections, the context
436 * is already initialized before installing the mux, so we detect incoming
437 * connections from the fact that the context is still NULL. Returns < 0 on
438 * error.
439 */
440static int h2_init(struct connection *conn)
441{
442 if (conn->mux_ctx) {
443 /* we don't support outgoing connections for now */
444 return -1;
445 }
446
Willy Tarreau32218eb2017-09-22 08:07:25 +0200447 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200448}
449
Willy Tarreau2373acc2017-10-12 17:35:14 +0200450/* returns the stream associated with id <id> or NULL if not found */
451static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
452{
453 struct eb32_node *node;
454
Willy Tarreau2a856182017-05-16 15:20:39 +0200455 if (id > h2c->max_id)
456 return (struct h2s *)h2_idle_stream;
457
Willy Tarreau2373acc2017-10-12 17:35:14 +0200458 node = eb32_lookup(&h2c->streams_by_id, id);
459 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200460 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200461
462 return container_of(node, struct h2s, by_id);
463}
464
Willy Tarreau62f52692017-10-08 23:01:42 +0200465/* release function for a connection. This one should be called to free all
466 * resources allocated to the mux.
467 */
468static void h2_release(struct connection *conn)
469{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200470 struct h2c *h2c = conn->mux_ctx;
471
472 LIST_DEL(&conn->list);
473
474 if (h2c) {
475 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200476 h2_release_dbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100477 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200478 LIST_DEL(&h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100479 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200480
481 h2_release_mbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100482 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200483 LIST_DEL(&h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100484 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200485
Willy Tarreauea392822017-10-31 10:02:25 +0100486 if (h2c->task) {
487 task_delete(h2c->task);
488 task_free(h2c->task);
489 h2c->task = NULL;
490 }
491
Willy Tarreaubafbe012017-11-24 17:34:44 +0100492 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200493 }
494
495 conn->mux = NULL;
496 conn->mux_ctx = NULL;
497
498 conn_stop_tracking(conn);
499 conn_full_close(conn);
500 if (conn->destroy_cb)
501 conn->destroy_cb(conn);
502 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200503}
504
505
Willy Tarreau71681172017-10-23 14:39:06 +0200506/******************************************************/
507/* functions below are for the H2 protocol processing */
508/******************************************************/
509
510/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100511static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200512{
513 return h2s ? h2s->id : 0;
514}
515
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200516/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100517static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200518{
519 if (h2c->msi < 0)
520 return 0;
521
522 if (h2c->msi == h2s_id(h2s))
523 return 0;
524
525 return 1;
526}
527
Willy Tarreau741d6df2017-10-17 08:00:59 +0200528/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100529static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200530{
531 h2c->errcode = err;
532 h2c->st0 = H2_CS_ERROR;
533}
534
Willy Tarreau2e43f082017-10-17 08:03:59 +0200535/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100536static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200537{
538 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
539 h2s->errcode = err;
540 h2s->st = H2_SS_ERROR;
541 if (h2s->cs)
542 h2s->cs->flags |= CS_FL_ERROR;
543 }
544}
545
Willy Tarreaue4820742017-07-27 13:37:23 +0200546/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100547static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200548{
549 uint8_t *out = frame;
550
551 *out = len >> 16;
552 write_n16(out + 1, len);
553}
554
Willy Tarreau54c15062017-10-10 17:10:03 +0200555/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
556 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
557 * the caller's responsibility to verify that there are at least <bytes> bytes
558 * available in the buffer's input prior to calling this function.
559 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100560static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200561 const struct buffer *b, int o)
562{
563 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
564}
565
Willy Tarreau1f094672017-11-20 21:27:45 +0100566static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200567{
568 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
569}
570
Willy Tarreau1f094672017-11-20 21:27:45 +0100571static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200572{
573 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
574}
575
Willy Tarreau1f094672017-11-20 21:27:45 +0100576static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200577{
578 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
579}
580
581
Willy Tarreau715d5312017-07-11 15:20:24 +0200582/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
583 * is not obvious. It turns out that H2 headers are neither aligned nor do they
584 * use regular sizes. And to add to the trouble, the buffer may wrap so each
585 * byte read must be checked. The header is formed like this :
586 *
587 * b0 b1 b2 b3 b4 b5..b8
588 * +----------+---------+--------+----+----+----------------------+
589 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
590 * +----------+---------+--------+----+----+----------------------+
591 *
592 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
593 * we get the sid properly aligned and ordered, and 16 bits of len properly
594 * ordered as well. The type and flags can be extracted using bit shifts from
595 * the word, and only one extra read is needed to fetch len[16:23].
596 * Returns zero if some bytes are missing, otherwise non-zero on success.
597 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100598static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200599{
600 uint64_t w;
601
602 if (b->i < 9)
603 return 0;
604
605 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
606 h->len = *b->p << 16;
607 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
608 h->ff = w >> 32;
609 h->ft = w >> 40;
610 h->len += w >> 48;
611 return 1;
612}
613
614/* skip the next 9 bytes corresponding to the frame header possibly parsed by
615 * h2_peek_frame_hdr() above.
616 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100617static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200618{
619 bi_del(b, 9);
620}
621
622/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100623static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200624{
625 int ret;
626
627 ret = h2_peek_frame_hdr(b, h);
628 if (ret > 0)
629 h2_skip_frame_hdr(b);
630 return ret;
631}
632
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100633/* marks stream <h2s> as CLOSED for connection <h2c> and decrement the number
634 * of active streams for this connection if the stream was not yet closed.
635 * Please use this exclusively before closing a stream to ensure stream count
636 * is well maintained.
637 */
638static inline void h2c_stream_close(struct h2c *h2c, struct h2s *h2s)
639{
640 if (h2s->st != H2_SS_CLOSED)
641 h2s->h2c->nb_streams--;
642 h2s->st = H2_SS_CLOSED;
643}
644
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200645/* creates a new stream <id> on the h2c connection and returns it, or NULL in
646 * case of memory allocation error.
647 */
648static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
649{
650 struct conn_stream *cs;
651 struct h2s *h2s;
652
Willy Tarreaubafbe012017-11-24 17:34:44 +0100653 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200654 if (!h2s)
655 goto out;
656
657 h2s->h2c = h2c;
658 h2s->mws = h2c->miw;
659 h2s->flags = H2_SF_NONE;
660 h2s->errcode = H2_ERR_NO_ERROR;
661 h2s->st = H2_SS_IDLE;
662 h1m_init(&h2s->req);
663 h1m_init(&h2s->res);
664 h2s->by_id.key = h2s->id = id;
665 h2c->max_id = id;
666 LIST_INIT(&h2s->list);
667
668 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100669 h2c->nb_streams++;
670 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
671 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200672
673 cs = cs_new(h2c->conn);
674 if (!cs)
675 goto out_close;
676
677 h2s->cs = cs;
678 cs->ctx = h2s;
679
680 if (stream_create_from_cs(cs) < 0)
681 goto out_free_cs;
682
683 /* OK done, the stream lives its own life now */
684 return h2s;
685
686 out_free_cs:
687 cs_free(cs);
688 out_close:
Willy Tarreau49745612017-12-03 18:56:02 +0100689 h2c->nb_streams--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200690 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100691 pool_free(pool_head_h2s, h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200692 h2s = NULL;
693 out:
694 return h2s;
695}
696
Willy Tarreaube5b7152017-09-25 16:25:39 +0200697/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
698 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
699 * the various settings codes.
700 */
701static int h2c_snd_settings(struct h2c *h2c)
702{
703 struct buffer *res;
704 char buf_data[100]; // enough for 15 settings
705 struct chunk buf;
706 int ret;
707
708 if (h2c_mux_busy(h2c, NULL)) {
709 h2c->flags |= H2_CF_DEM_MBUSY;
710 return 0;
711 }
712
713 res = h2_get_mbuf(h2c);
714 if (!res) {
715 h2c->flags |= H2_CF_MUX_MALLOC;
716 h2c->flags |= H2_CF_DEM_MROOM;
717 return 0;
718 }
719
720 chunk_init(&buf, buf_data, sizeof(buf_data));
721 chunk_memcpy(&buf,
722 "\x00\x00\x00" /* length : 0 for now */
723 "\x04\x00" /* type : 4 (settings), flags : 0 */
724 "\x00\x00\x00\x00", /* stream ID : 0 */
725 9);
726
727 if (h2_settings_header_table_size != 4096) {
728 char str[6] = "\x00\x01"; /* header_table_size */
729
730 write_n32(str + 2, h2_settings_header_table_size);
731 chunk_memcat(&buf, str, 6);
732 }
733
734 if (h2_settings_initial_window_size != 65535) {
735 char str[6] = "\x00\x04"; /* initial_window_size */
736
737 write_n32(str + 2, h2_settings_initial_window_size);
738 chunk_memcat(&buf, str, 6);
739 }
740
741 if (h2_settings_max_concurrent_streams != 0) {
742 char str[6] = "\x00\x03"; /* max_concurrent_streams */
743
744 /* Note: 0 means "unlimited" for haproxy's config but not for
745 * the protocol, so never send this value!
746 */
747 write_n32(str + 2, h2_settings_max_concurrent_streams);
748 chunk_memcat(&buf, str, 6);
749 }
750
751 if (global.tune.bufsize != 16384) {
752 char str[6] = "\x00\x05"; /* max_frame_size */
753
754 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
755 * match bufsize - rewrite size, but at the moment it seems
756 * that clients don't take care of it.
757 */
758 write_n32(str + 2, global.tune.bufsize);
759 chunk_memcat(&buf, str, 6);
760 }
761
762 h2_set_frame_size(buf.str, buf.len - 9);
763 ret = bo_istput(res, ist2(buf.str, buf.len));
764 if (unlikely(ret <= 0)) {
765 if (!ret) {
766 h2c->flags |= H2_CF_MUX_MFULL;
767 h2c->flags |= H2_CF_DEM_MROOM;
768 return 0;
769 }
770 else {
771 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
772 return 0;
773 }
774 }
775 return ret;
776}
777
Willy Tarreau52eed752017-09-22 15:05:09 +0200778/* Try to receive a connection preface, then upon success try to send our
779 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
780 * missing data. It may return an error in h2c.
781 */
782static int h2c_frt_recv_preface(struct h2c *h2c)
783{
784 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200785 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200786
787 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
788
789 if (unlikely(ret1 <= 0)) {
790 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
791 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
792 return 0;
793 }
794
Willy Tarreaube5b7152017-09-25 16:25:39 +0200795 ret2 = h2c_snd_settings(h2c);
796 if (ret2 > 0)
797 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200798
Willy Tarreaube5b7152017-09-25 16:25:39 +0200799 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200800}
801
Willy Tarreau081d4722017-05-16 21:51:05 +0200802/* try to send a GOAWAY frame on the connection to report an error or a graceful
803 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
804 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
805 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
806 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
807 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
808 * on unrecoverable failure. It will not attempt to send one again in this last
809 * case so that it is safe to use h2c_error() to report such errors.
810 */
811static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
812{
813 struct buffer *res;
814 char str[17];
815 int ret;
816
817 if (h2c->flags & H2_CF_GOAWAY_FAILED)
818 return 1; // claim that it worked
819
820 if (h2c_mux_busy(h2c, h2s)) {
821 if (h2s)
822 h2s->flags |= H2_SF_BLK_MBUSY;
823 else
824 h2c->flags |= H2_CF_DEM_MBUSY;
825 return 0;
826 }
827
828 res = h2_get_mbuf(h2c);
829 if (!res) {
830 h2c->flags |= H2_CF_MUX_MALLOC;
831 if (h2s)
832 h2s->flags |= H2_SF_BLK_MROOM;
833 else
834 h2c->flags |= H2_CF_DEM_MROOM;
835 return 0;
836 }
837
838 /* len: 8, type: 7, flags: none, sid: 0 */
839 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
840
841 if (h2c->last_sid < 0)
842 h2c->last_sid = h2c->max_id;
843
844 write_n32(str + 9, h2c->last_sid);
845 write_n32(str + 13, h2c->errcode);
846 ret = bo_istput(res, ist2(str, 17));
847 if (unlikely(ret <= 0)) {
848 if (!ret) {
849 h2c->flags |= H2_CF_MUX_MFULL;
850 if (h2s)
851 h2s->flags |= H2_SF_BLK_MROOM;
852 else
853 h2c->flags |= H2_CF_DEM_MROOM;
854 return 0;
855 }
856 else {
857 /* we cannot report this error using GOAWAY, so we mark
858 * it and claim a success.
859 */
860 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
861 h2c->flags |= H2_CF_GOAWAY_FAILED;
862 return 1;
863 }
864 }
865 h2c->flags |= H2_CF_GOAWAY_SENT;
866 return ret;
867}
868
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100869/* Try to send an RST_STREAM frame on the connection for the indicated stream
870 * during mux operations. This stream must be valid and cannot be closed
871 * already. h2s->id will be used for the stream ID and h2s->errcode will be
872 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
873 * not yet.
874 *
875 * Returns > 0 on success or zero if nothing was done. In case of lack of room
876 * to write the message, it subscribes the stream to future notifications.
877 */
878static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
879{
880 struct buffer *res;
881 char str[13];
882 int ret;
883
884 if (!h2s || h2s->st == H2_SS_CLOSED)
885 return 1;
886
887 if (h2c_mux_busy(h2c, h2s)) {
888 h2s->flags |= H2_SF_BLK_MBUSY;
889 return 0;
890 }
891
892 res = h2_get_mbuf(h2c);
893 if (!res) {
894 h2c->flags |= H2_CF_MUX_MALLOC;
895 h2s->flags |= H2_SF_BLK_MROOM;
896 return 0;
897 }
898
899 /* len: 4, type: 3, flags: none */
900 memcpy(str, "\x00\x00\x04\x03\x00", 5);
901 write_n32(str + 5, h2s->id);
902 write_n32(str + 9, h2s->errcode);
903 ret = bo_istput(res, ist2(str, 13));
904
905 if (unlikely(ret <= 0)) {
906 if (!ret) {
907 h2c->flags |= H2_CF_MUX_MFULL;
908 h2s->flags |= H2_SF_BLK_MROOM;
909 return 0;
910 }
911 else {
912 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
913 return 0;
914 }
915 }
916
917 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100918 h2c_stream_close(h2c, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100919 return ret;
920}
921
922/* Try to send an RST_STREAM frame on the connection for the stream being
923 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
924 * error code unless the stream's state already is IDLE or CLOSED in which
925 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
926 * it was not yet.
927 *
928 * Returns > 0 on success or zero if nothing was done. In case of lack of room
929 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200930 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100931 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200932 */
933static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
934{
935 struct buffer *res;
936 char str[13];
937 int ret;
938
939 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100940 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200941 return 0;
942 }
943
944 res = h2_get_mbuf(h2c);
945 if (!res) {
946 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100947 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200948 return 0;
949 }
950
951 /* len: 4, type: 3, flags: none */
952 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100953
Willy Tarreau27a84c92017-10-17 08:10:17 +0200954 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100955 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200956 h2s->errcode : H2_ERR_STREAM_CLOSED);
957 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100958
Willy Tarreau27a84c92017-10-17 08:10:17 +0200959 if (unlikely(ret <= 0)) {
960 if (!ret) {
961 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100962 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200963 return 0;
964 }
965 else {
966 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
967 return 0;
968 }
969 }
970
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100971 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200972 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100973 h2c_stream_close(h2c, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100974 }
975
Willy Tarreau27a84c92017-10-17 08:10:17 +0200976 return ret;
977}
978
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100979/* try to send an empty DATA frame with the ES flag set to notify about the
980 * end of stream and match a shutdown(write). If an ES was already sent as
981 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
982 * on success or zero if nothing was done. In case of lack of room to write the
983 * message, it subscribes the requesting stream to future notifications.
984 */
985static int h2_send_empty_data_es(struct h2s *h2s)
986{
987 struct h2c *h2c = h2s->h2c;
988 struct buffer *res;
989 char str[9];
990 int ret;
991
Willy Tarreau721c9742017-11-07 11:05:42 +0100992 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100993 return 1;
994
995 if (h2c_mux_busy(h2c, h2s)) {
996 h2s->flags |= H2_SF_BLK_MBUSY;
997 return 0;
998 }
999
1000 res = h2_get_mbuf(h2c);
1001 if (!res) {
1002 h2c->flags |= H2_CF_MUX_MALLOC;
1003 h2s->flags |= H2_SF_BLK_MROOM;
1004 return 0;
1005 }
1006
1007 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1008 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1009 write_n32(str + 5, h2s->id);
1010 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001011 if (likely(ret > 0)) {
1012 h2s->flags |= H2_SF_ES_SENT;
1013 }
1014 else if (!ret) {
1015 h2c->flags |= H2_CF_MUX_MFULL;
1016 h2s->flags |= H2_SF_BLK_MROOM;
1017 return 0;
1018 }
1019 else {
1020 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1021 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001022 }
1023 return ret;
1024}
1025
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001026/* wake the streams attached to the connection, whose id is greater than <last>,
1027 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1028 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1029 * stream's state is automatically updated accordingly.
1030 */
1031static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1032{
1033 struct eb32_node *node;
1034 struct h2s *h2s;
1035
1036 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1037 flags |= CS_FL_ERROR;
1038
1039 if (conn_xprt_read0_pending(h2c->conn))
1040 flags |= CS_FL_EOS;
1041
1042 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1043 while (node) {
1044 h2s = container_of(node, struct h2s, by_id);
1045 if (h2s->id <= last)
1046 break;
1047 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001048
1049 if (!h2s->cs) {
1050 /* this stream was already orphaned */
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001051 h2c_stream_close(h2c, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001052 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001053 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001054 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001055 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001056
1057 h2s->cs->flags |= flags;
1058 /* recv is used to force to detect CS_FL_EOS that wake()
1059 * doesn't handle in the stream int code.
1060 */
1061 h2s->cs->data_cb->recv(h2s->cs);
1062 h2s->cs->data_cb->wake(h2s->cs);
1063
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001064 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1065 h2s->st = H2_SS_ERROR;
1066 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1067 h2s->st = H2_SS_HREM;
1068 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001069 h2c_stream_close(h2c, h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001070 }
1071}
1072
Willy Tarreau3421aba2017-07-27 15:41:03 +02001073/* Increase all streams' outgoing window size by the difference passed in
1074 * argument. This is needed upon receipt of the settings frame if the initial
1075 * window size is different. The difference may be negative and the resulting
1076 * window size as well, for the time it takes to receive some window updates.
1077 */
1078static void h2c_update_all_ws(struct h2c *h2c, int diff)
1079{
1080 struct h2s *h2s;
1081 struct eb32_node *node;
1082
1083 if (!diff)
1084 return;
1085
1086 node = eb32_first(&h2c->streams_by_id);
1087 while (node) {
1088 h2s = container_of(node, struct h2s, by_id);
1089 h2s->mws += diff;
1090 node = eb32_next(node);
1091 }
1092}
1093
1094/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1095 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1096 * return an error in h2c. Described in RFC7540#6.5.
1097 */
1098static int h2c_handle_settings(struct h2c *h2c)
1099{
1100 unsigned int offset;
1101 int error;
1102
1103 if (h2c->dff & H2_F_SETTINGS_ACK) {
1104 if (h2c->dfl) {
1105 error = H2_ERR_FRAME_SIZE_ERROR;
1106 goto fail;
1107 }
1108 return 1;
1109 }
1110
1111 if (h2c->dsi != 0) {
1112 error = H2_ERR_PROTOCOL_ERROR;
1113 goto fail;
1114 }
1115
1116 if (h2c->dfl % 6) {
1117 error = H2_ERR_FRAME_SIZE_ERROR;
1118 goto fail;
1119 }
1120
1121 /* that's the limit we can process */
1122 if (h2c->dfl > global.tune.bufsize) {
1123 error = H2_ERR_FRAME_SIZE_ERROR;
1124 goto fail;
1125 }
1126
1127 /* process full frame only */
1128 if (h2c->dbuf->i < h2c->dfl)
1129 return 0;
1130
1131 /* parse the frame */
1132 for (offset = 0; offset < h2c->dfl; offset += 6) {
1133 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1134 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1135
1136 switch (type) {
1137 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1138 /* we need to update all existing streams with the
1139 * difference from the previous iws.
1140 */
1141 if (arg < 0) { // RFC7540#6.5.2
1142 error = H2_ERR_FLOW_CONTROL_ERROR;
1143 goto fail;
1144 }
1145 h2c_update_all_ws(h2c, arg - h2c->miw);
1146 h2c->miw = arg;
1147 break;
1148 case H2_SETTINGS_MAX_FRAME_SIZE:
1149 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1150 error = H2_ERR_PROTOCOL_ERROR;
1151 goto fail;
1152 }
1153 h2c->mfs = arg;
1154 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001155 case H2_SETTINGS_ENABLE_PUSH:
1156 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1157 error = H2_ERR_PROTOCOL_ERROR;
1158 goto fail;
1159 }
1160 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001161 }
1162 }
1163
1164 /* need to ACK this frame now */
1165 h2c->st0 = H2_CS_FRAME_A;
1166 return 1;
1167 fail:
1168 h2c_error(h2c, error);
1169 return 0;
1170}
1171
1172/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1173 * success or one of the h2_status values.
1174 */
1175static int h2c_ack_settings(struct h2c *h2c)
1176{
1177 struct buffer *res;
1178 char str[9];
1179 int ret = -1;
1180
1181 if (h2c_mux_busy(h2c, NULL)) {
1182 h2c->flags |= H2_CF_DEM_MBUSY;
1183 return 0;
1184 }
1185
1186 res = h2_get_mbuf(h2c);
1187 if (!res) {
1188 h2c->flags |= H2_CF_MUX_MALLOC;
1189 h2c->flags |= H2_CF_DEM_MROOM;
1190 return 0;
1191 }
1192
1193 memcpy(str,
1194 "\x00\x00\x00" /* length : 0 (no data) */
1195 "\x04" "\x01" /* type : 4, flags : ACK */
1196 "\x00\x00\x00\x00" /* stream ID */, 9);
1197
1198 ret = bo_istput(res, ist2(str, 9));
1199 if (unlikely(ret <= 0)) {
1200 if (!ret) {
1201 h2c->flags |= H2_CF_MUX_MFULL;
1202 h2c->flags |= H2_CF_DEM_MROOM;
1203 return 0;
1204 }
1205 else {
1206 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1207 return 0;
1208 }
1209 }
1210 return ret;
1211}
1212
Willy Tarreaucf68c782017-10-10 17:11:41 +02001213/* processes a PING frame and schedules an ACK if needed. The caller must pass
1214 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1215 * missing data. It may return an error in h2c.
1216 */
1217static int h2c_handle_ping(struct h2c *h2c)
1218{
1219 /* frame length must be exactly 8 */
1220 if (h2c->dfl != 8) {
1221 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1222 return 0;
1223 }
1224
1225 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001226 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001227 h2c->st0 = H2_CS_FRAME_A;
1228 return 1;
1229}
1230
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001231/* Try to send a window update for stream id <sid> and value <increment>.
1232 * Returns > 0 on success or zero on missing room or failure. It may return an
1233 * error in h2c.
1234 */
1235static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1236{
1237 struct buffer *res;
1238 char str[13];
1239 int ret = -1;
1240
1241 if (h2c_mux_busy(h2c, NULL)) {
1242 h2c->flags |= H2_CF_DEM_MBUSY;
1243 return 0;
1244 }
1245
1246 res = h2_get_mbuf(h2c);
1247 if (!res) {
1248 h2c->flags |= H2_CF_MUX_MALLOC;
1249 h2c->flags |= H2_CF_DEM_MROOM;
1250 return 0;
1251 }
1252
1253 /* length: 4, type: 8, flags: none */
1254 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1255 write_n32(str + 5, sid);
1256 write_n32(str + 9, increment);
1257
1258 ret = bo_istput(res, ist2(str, 13));
1259
1260 if (unlikely(ret <= 0)) {
1261 if (!ret) {
1262 h2c->flags |= H2_CF_MUX_MFULL;
1263 h2c->flags |= H2_CF_DEM_MROOM;
1264 return 0;
1265 }
1266 else {
1267 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1268 return 0;
1269 }
1270 }
1271 return ret;
1272}
1273
1274/* try to send pending window update for the connection. It's safe to call it
1275 * with no pending updates. Returns > 0 on success or zero on missing room or
1276 * failure. It may return an error in h2c.
1277 */
1278static int h2c_send_conn_wu(struct h2c *h2c)
1279{
1280 int ret = 1;
1281
1282 if (h2c->rcvd_c <= 0)
1283 return 1;
1284
1285 /* send WU for the connection */
1286 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1287 if (ret > 0)
1288 h2c->rcvd_c = 0;
1289
1290 return ret;
1291}
1292
1293/* try to send pending window update for the current dmux stream. It's safe to
1294 * call it with no pending updates. Returns > 0 on success or zero on missing
1295 * room or failure. It may return an error in h2c.
1296 */
1297static int h2c_send_strm_wu(struct h2c *h2c)
1298{
1299 int ret = 1;
1300
1301 if (h2c->rcvd_s <= 0)
1302 return 1;
1303
1304 /* send WU for the stream */
1305 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1306 if (ret > 0)
1307 h2c->rcvd_s = 0;
1308
1309 return ret;
1310}
1311
Willy Tarreaucf68c782017-10-10 17:11:41 +02001312/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1313 * success, 0 on missing data or one of the h2_status values.
1314 */
1315static int h2c_ack_ping(struct h2c *h2c)
1316{
1317 struct buffer *res;
1318 char str[17];
1319 int ret = -1;
1320
1321 if (h2c->dbuf->i < 8)
1322 return 0;
1323
1324 if (h2c_mux_busy(h2c, NULL)) {
1325 h2c->flags |= H2_CF_DEM_MBUSY;
1326 return 0;
1327 }
1328
1329 res = h2_get_mbuf(h2c);
1330 if (!res) {
1331 h2c->flags |= H2_CF_MUX_MALLOC;
1332 h2c->flags |= H2_CF_DEM_MROOM;
1333 return 0;
1334 }
1335
1336 memcpy(str,
1337 "\x00\x00\x08" /* length : 8 (same payload) */
1338 "\x06" "\x01" /* type : 6, flags : ACK */
1339 "\x00\x00\x00\x00" /* stream ID */, 9);
1340
1341 /* copy the original payload */
1342 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1343
1344 ret = bo_istput(res, ist2(str, 17));
1345 if (unlikely(ret <= 0)) {
1346 if (!ret) {
1347 h2c->flags |= H2_CF_MUX_MFULL;
1348 h2c->flags |= H2_CF_DEM_MROOM;
1349 return 0;
1350 }
1351 else {
1352 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1353 return 0;
1354 }
1355 }
1356 return ret;
1357}
1358
Willy Tarreau26f95952017-07-27 17:18:30 +02001359/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1360 * Returns > 0 on success or zero on missing data. It may return an error in
1361 * h2c or h2s. Described in RFC7540#6.9.
1362 */
1363static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1364{
1365 int32_t inc;
1366 int error;
1367
1368 if (h2c->dfl != 4) {
1369 error = H2_ERR_FRAME_SIZE_ERROR;
1370 goto conn_err;
1371 }
1372
1373 /* process full frame only */
1374 if (h2c->dbuf->i < h2c->dfl)
1375 return 0;
1376
1377 inc = h2_get_n32(h2c->dbuf, 0);
1378
1379 if (h2c->dsi != 0) {
1380 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001381
1382 /* it's not an error to receive WU on a closed stream */
1383 if (h2s->st == H2_SS_CLOSED)
1384 return 1;
1385
1386 if (!inc) {
1387 error = H2_ERR_PROTOCOL_ERROR;
1388 goto strm_err;
1389 }
1390
1391 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1392 error = H2_ERR_FLOW_CONTROL_ERROR;
1393 goto strm_err;
1394 }
1395
1396 h2s->mws += inc;
1397 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1398 h2s->flags &= ~H2_SF_BLK_SFCTL;
1399 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1400 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1401 /* This stream wanted to send but could not due to its
1402 * own flow control. We can put it back into the send
1403 * list now, it will be handled upon next send() call.
1404 */
1405 LIST_ADDQ(&h2c->send_list, &h2s->list);
1406 }
1407 }
1408 }
1409 else {
1410 /* connection window update */
1411 if (!inc) {
1412 error = H2_ERR_PROTOCOL_ERROR;
1413 goto conn_err;
1414 }
1415
1416 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1417 error = H2_ERR_FLOW_CONTROL_ERROR;
1418 goto conn_err;
1419 }
1420
1421 h2c->mws += inc;
1422 }
1423
1424 return 1;
1425
1426 conn_err:
1427 h2c_error(h2c, error);
1428 return 0;
1429
1430 strm_err:
1431 if (h2s) {
1432 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001433 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001434 }
1435 else
1436 h2c_error(h2c, error);
1437 return 0;
1438}
1439
Willy Tarreaue96b0922017-10-30 00:28:29 +01001440/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1441 * the last ID. Returns > 0 on success or zero on missing data. It may return
1442 * an error in h2c. Described in RFC7540#6.8.
1443 */
1444static int h2c_handle_goaway(struct h2c *h2c)
1445{
1446 int error;
1447 int last;
1448
1449 if (h2c->dsi != 0) {
1450 error = H2_ERR_PROTOCOL_ERROR;
1451 goto conn_err;
1452 }
1453
1454 if (h2c->dfl < 8) {
1455 error = H2_ERR_FRAME_SIZE_ERROR;
1456 goto conn_err;
1457 }
1458
1459 /* process full frame only */
1460 if (h2c->dbuf->i < h2c->dfl)
1461 return 0;
1462
1463 last = h2_get_n32(h2c->dbuf, 0);
1464 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1465 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001466 if (h2c->last_sid < 0)
1467 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001468 return 1;
1469
1470 conn_err:
1471 h2c_error(h2c, error);
1472 return 0;
1473}
1474
Willy Tarreau92153fc2017-12-03 19:46:19 +01001475/* processes a PRIORITY frame, and either skips it or rejects if it is
1476 * invalid. Returns > 0 on success or zero on missing data. It may return
1477 * an error in h2c. Described in RFC7540#6.3.
1478 */
1479static int h2c_handle_priority(struct h2c *h2c)
1480{
1481 int error;
1482
1483 if (h2c->dsi == 0) {
1484 error = H2_ERR_PROTOCOL_ERROR;
1485 goto conn_err;
1486 }
1487
1488 if (h2c->dfl != 5) {
1489 error = H2_ERR_FRAME_SIZE_ERROR;
1490 goto conn_err;
1491 }
1492
1493 /* process full frame only */
1494 if (h2c->dbuf->i < h2c->dfl)
1495 return 0;
1496
1497 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1498 /* 7540#5.3 : can't depend on itself */
1499 error = H2_ERR_PROTOCOL_ERROR;
1500 goto conn_err;
1501 }
1502 return 1;
1503
1504 conn_err:
1505 h2c_error(h2c, error);
1506 return 0;
1507}
1508
Willy Tarreaucd234e92017-08-18 10:59:39 +02001509/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1510 * Returns > 0 on success or zero on missing data. It may return an error in
1511 * h2c. Described in RFC7540#6.4.
1512 */
1513static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1514{
1515 int error;
1516
1517 if (h2c->dsi == 0) {
1518 error = H2_ERR_PROTOCOL_ERROR;
1519 goto conn_err;
1520 }
1521
Willy Tarreaucd234e92017-08-18 10:59:39 +02001522 if (h2c->dfl != 4) {
1523 error = H2_ERR_FRAME_SIZE_ERROR;
1524 goto conn_err;
1525 }
1526
1527 /* process full frame only */
1528 if (h2c->dbuf->i < h2c->dfl)
1529 return 0;
1530
1531 /* late RST, already handled */
1532 if (h2s->st == H2_SS_CLOSED)
1533 return 1;
1534
1535 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001536 h2c_stream_close(h2c, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001537
1538 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001539 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001540 /* recv is used to force to detect CS_FL_EOS that wake()
1541 * doesn't handle in the stream-int code.
1542 */
1543 h2s->cs->data_cb->recv(h2s->cs);
1544 h2s->cs->data_cb->wake(h2s->cs);
1545 }
1546
1547 h2s->flags |= H2_SF_RST_RCVD;
1548 return 1;
1549
1550 conn_err:
1551 h2c_error(h2c, error);
1552 return 0;
1553}
1554
Willy Tarreau13278b42017-10-13 19:23:14 +02001555/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1556 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1557 * errors here are reported as connection errors since it's impossible to
1558 * recover from such errors after the compression context has been altered.
1559 */
1560static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1561{
1562 int error;
1563
1564 if (!h2c->dfl) {
1565 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1566 goto strm_err;
1567 }
1568
1569 if (!h2c->dbuf->size)
1570 return 0; // empty buffer
1571
1572 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1573 return 0; // incomplete frame
1574
1575 /* now either the frame is complete or the buffer is complete */
1576 if (h2s->st != H2_SS_IDLE) {
1577 /* FIXME: stream already exists, this is only allowed for
1578 * trailers (not supported for now).
1579 */
1580 error = H2_ERR_PROTOCOL_ERROR;
1581 goto conn_err;
1582 }
1583 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1584 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1585 error = H2_ERR_PROTOCOL_ERROR;
1586 goto conn_err;
1587 }
1588
1589 h2s = h2c_stream_new(h2c, h2c->dsi);
1590 if (!h2s) {
1591 error = H2_ERR_INTERNAL_ERROR;
1592 goto conn_err;
1593 }
1594
1595 h2s->st = H2_SS_OPEN;
1596 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1597 h2s->st = H2_SS_HREM;
1598 h2s->flags |= H2_SF_ES_RCVD;
1599 }
1600
1601 /* call the upper layers to process the frame, then let the upper layer
1602 * notify the stream about any change.
1603 */
1604 h2s->cs->data_cb->recv(h2s->cs);
1605
1606 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1607 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1608 error = H2_ERR_INTERNAL_ERROR;
1609 goto conn_err;
1610 }
1611
Willy Tarreau8f650c32017-11-21 19:36:21 +01001612 if (h2c->st0 >= H2_CS_ERROR)
1613 return 0;
1614
Willy Tarreau721c9742017-11-07 11:05:42 +01001615 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001616 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001617 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001618 }
1619 else {
1620 /* update the max stream ID if the request is being processed */
1621 if (h2s->id > h2c->max_id)
1622 h2c->max_id = h2s->id;
1623 }
1624
1625 return 1;
1626
1627 conn_err:
1628 h2c_error(h2c, error);
1629 return 0;
1630
1631 strm_err:
1632 if (h2s) {
1633 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001634 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001635 }
1636 else
1637 h2c_error(h2c, error);
1638 return 0;
1639}
1640
Willy Tarreau454f9052017-10-26 19:40:35 +02001641/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1642 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1643 */
1644static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1645{
1646 int error;
1647
1648 /* note that empty DATA frames are perfectly valid and sometimes used
1649 * to signal an end of stream (with the ES flag).
1650 */
1651
1652 if (!h2c->dbuf->size && h2c->dfl)
1653 return 0; // empty buffer
1654
1655 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1656 return 0; // incomplete frame
1657
1658 /* now either the frame is complete or the buffer is complete */
1659
1660 if (!h2c->dsi) {
1661 /* RFC7540#6.1 */
1662 error = H2_ERR_PROTOCOL_ERROR;
1663 goto conn_err;
1664 }
1665
1666 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1667 /* RFC7540#6.1 */
1668 error = H2_ERR_STREAM_CLOSED;
1669 goto strm_err;
1670 }
1671
Willy Tarreau454f9052017-10-26 19:40:35 +02001672 /* call the upper layers to process the frame, then let the upper layer
1673 * notify the stream about any change.
1674 */
1675 if (!h2s->cs) {
1676 error = H2_ERR_STREAM_CLOSED;
1677 goto strm_err;
1678 }
1679
1680 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001681
Willy Tarreau454f9052017-10-26 19:40:35 +02001682 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1683 /* cs has just been destroyed, we have to kill h2s. */
1684 error = H2_ERR_STREAM_CLOSED;
1685 goto strm_err;
1686 }
1687
Willy Tarreau8f650c32017-11-21 19:36:21 +01001688 if (h2c->st0 >= H2_CS_ERROR)
1689 return 0;
1690
Willy Tarreau721c9742017-11-07 11:05:42 +01001691 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001692 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001693 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001694 }
1695
1696 /* check for completion : the callee will change this to FRAME_A or
1697 * FRAME_H once done.
1698 */
1699 if (h2c->st0 == H2_CS_FRAME_P)
1700 return 0;
1701
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001702
1703 /* last frame */
1704 if (h2c->dff & H2_F_DATA_END_STREAM) {
1705 h2s->st = H2_SS_HREM;
1706 h2s->flags |= H2_SF_ES_RCVD;
1707 }
1708
Willy Tarreau454f9052017-10-26 19:40:35 +02001709 return 1;
1710
1711 conn_err:
1712 h2c_error(h2c, error);
1713 return 0;
1714
1715 strm_err:
1716 if (h2s) {
1717 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001718 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001719 }
1720 else
1721 h2c_error(h2c, error);
1722 return 0;
1723}
1724
Willy Tarreaubc933932017-10-09 16:21:43 +02001725/* process Rx frames to be demultiplexed */
1726static void h2_process_demux(struct h2c *h2c)
1727{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001728 struct h2s *h2s;
1729
Willy Tarreau081d4722017-05-16 21:51:05 +02001730 if (h2c->st0 >= H2_CS_ERROR)
1731 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001732
1733 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1734 if (h2c->st0 == H2_CS_PREFACE) {
1735 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1736 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1737 if (h2c->st0 == H2_CS_ERROR)
1738 h2c->st0 = H2_CS_ERROR2;
1739 goto fail;
1740 }
1741
1742 h2c->max_id = 0;
1743 h2c->st0 = H2_CS_SETTINGS1;
1744 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001745
1746 if (h2c->st0 == H2_CS_SETTINGS1) {
1747 struct h2_fh hdr;
1748
1749 /* ensure that what is pending is a valid SETTINGS frame
1750 * without an ACK.
1751 */
1752 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1753 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1754 if (h2c->st0 == H2_CS_ERROR)
1755 h2c->st0 = H2_CS_ERROR2;
1756 goto fail;
1757 }
1758
1759 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1760 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1761 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1762 h2c->st0 = H2_CS_ERROR2;
1763 goto fail;
1764 }
1765
1766 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1767 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1768 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1769 h2c->st0 = H2_CS_ERROR2;
1770 goto fail;
1771 }
1772
1773 /* that's OK, switch to FRAME_P to process it */
1774 h2c->dfl = hdr.len;
1775 h2c->dsi = hdr.sid;
1776 h2c->dft = hdr.ft;
1777 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001778 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001779 h2c->st0 = H2_CS_FRAME_P;
1780 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001781 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001782
1783 /* process as many incoming frames as possible below */
1784 while (h2c->dbuf->i) {
1785 int ret = 0;
1786
1787 if (h2c->st0 >= H2_CS_ERROR)
1788 break;
1789
1790 if (h2c->st0 == H2_CS_FRAME_H) {
1791 struct h2_fh hdr;
1792
1793 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1794 break;
1795
1796 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1797 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1798 h2c->st0 = H2_CS_ERROR;
1799 break;
1800 }
1801
1802 h2c->dfl = hdr.len;
1803 h2c->dsi = hdr.sid;
1804 h2c->dft = hdr.ft;
1805 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001806 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001807 h2c->st0 = H2_CS_FRAME_P;
1808 h2_skip_frame_hdr(h2c->dbuf);
1809 }
1810
1811 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001812 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001813
Willy Tarreaud7901432017-12-29 11:34:40 +01001814 if (h2c->st0 == H2_CS_FRAME_E)
1815 goto strm_err;
1816
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001817 if (h2s->st == H2_SS_IDLE &&
1818 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1819 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1820 * this state MUST be treated as a connection error
1821 */
1822 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1823 h2c->st0 = H2_CS_ERROR;
1824 break;
1825 }
1826
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001827 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1828 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1829 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1830 * this state MUST be treated as a stream error
1831 */
1832 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001833 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001834 goto strm_err;
1835 }
1836
Willy Tarreauab837502017-12-27 15:07:30 +01001837 /* Below the management of frames received in closed state is a
1838 * bit hackish because the spec makes strong differences between
1839 * streams closed by receiving RST, sending RST, and seeing ES
1840 * in both directions. In addition to this, the creation of a
1841 * new stream reusing the identifier of a closed one will be
1842 * detected here. Given that we cannot keep track of all closed
1843 * streams forever, we consider that unknown closed streams were
1844 * closed on RST received, which allows us to respond with an
1845 * RST without breaking the connection (eg: to abort a transfer).
1846 * Some frames have to be silently ignored as well.
1847 */
1848 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1849 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1850 /* #5.1.1: The identifier of a newly
1851 * established stream MUST be numerically
1852 * greater than all streams that the initiating
1853 * endpoint has opened or reserved. This
1854 * governs streams that are opened using a
1855 * HEADERS frame and streams that are reserved
1856 * using PUSH_PROMISE. An endpoint that
1857 * receives an unexpected stream identifier
1858 * MUST respond with a connection error.
1859 */
1860 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1861 goto strm_err;
1862 }
1863
1864 if (h2s->flags & H2_SF_RST_RCVD) {
1865 /* RFC7540#5.1:closed: an endpoint that
1866 * receives any frame other than PRIORITY after
1867 * receiving a RST_STREAM MUST treat that as a
1868 * stream error of type STREAM_CLOSED.
1869 *
1870 * Note that old streams fall into this category
1871 * and will lead to an RST being sent.
1872 */
1873 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1874 h2c->st0 = H2_CS_FRAME_E;
1875 goto strm_err;
1876 }
1877
1878 /* RFC7540#5.1:closed: if this state is reached as a
1879 * result of sending a RST_STREAM frame, the peer that
1880 * receives the RST_STREAM might have already sent
1881 * frames on the stream that cannot be withdrawn. An
1882 * endpoint MUST ignore frames that it receives on
1883 * closed streams after it has sent a RST_STREAM
1884 * frame. An endpoint MAY choose to limit the period
1885 * over which it ignores frames and treat frames that
1886 * arrive after this time as being in error.
1887 */
1888 if (!(h2s->flags & H2_SF_RST_SENT)) {
1889 /* RFC7540#5.1:closed: any frame other than
1890 * PRIO/WU/RST in this state MUST be treated as
1891 * a connection error
1892 */
1893 if (h2c->dft != H2_FT_RST_STREAM &&
1894 h2c->dft != H2_FT_PRIORITY &&
1895 h2c->dft != H2_FT_WINDOW_UPDATE) {
1896 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1897 goto strm_err;
1898 }
1899 }
1900 }
1901
Willy Tarreauc0da1962017-10-30 18:38:00 +01001902#if 0
1903 // problem below: it is not possible to completely ignore such
1904 // streams as we need to maintain the compression state as well
1905 // and for this we need to completely process these frames (eg:
1906 // HEADERS frames) as well as counting DATA frames to emit
1907 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1908 // This is a typical case of layer violation where the
1909 // transported contents are critical to the connection's
1910 // validity and must be ignored at the same time :-(
1911
1912 /* graceful shutdown, ignore streams whose ID is higher than
1913 * the one advertised in GOAWAY. RFC7540#6.8.
1914 */
1915 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1916 ret = MIN(h2c->dbuf->i, h2c->dfl);
1917 bi_del(h2c->dbuf, ret);
1918 h2c->dfl -= ret;
1919 ret = h2c->dfl == 0;
1920 goto strm_err;
1921 }
1922#endif
1923
Willy Tarreau7e98c052017-10-10 15:56:59 +02001924 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001925 case H2_FT_SETTINGS:
1926 if (h2c->st0 == H2_CS_FRAME_P)
1927 ret = h2c_handle_settings(h2c);
1928
1929 if (h2c->st0 == H2_CS_FRAME_A)
1930 ret = h2c_ack_settings(h2c);
1931 break;
1932
Willy Tarreaucf68c782017-10-10 17:11:41 +02001933 case H2_FT_PING:
1934 if (h2c->st0 == H2_CS_FRAME_P)
1935 ret = h2c_handle_ping(h2c);
1936
1937 if (h2c->st0 == H2_CS_FRAME_A)
1938 ret = h2c_ack_ping(h2c);
1939 break;
1940
Willy Tarreau26f95952017-07-27 17:18:30 +02001941 case H2_FT_WINDOW_UPDATE:
1942 if (h2c->st0 == H2_CS_FRAME_P)
1943 ret = h2c_handle_window_update(h2c, h2s);
1944 break;
1945
Willy Tarreau61290ec2017-10-17 08:19:21 +02001946 case H2_FT_CONTINUATION:
1947 /* we currently don't support CONTINUATION frames since
1948 * we have nowhere to store the partial HEADERS frame.
1949 * Let's abort the stream on an INTERNAL_ERROR here.
1950 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001951 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001952 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001953 h2c->st0 = H2_CS_FRAME_E;
1954 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001955 break;
1956
Willy Tarreau13278b42017-10-13 19:23:14 +02001957 case H2_FT_HEADERS:
1958 if (h2c->st0 == H2_CS_FRAME_P)
1959 ret = h2c_frt_handle_headers(h2c, h2s);
1960 break;
1961
Willy Tarreau454f9052017-10-26 19:40:35 +02001962 case H2_FT_DATA:
1963 if (h2c->st0 == H2_CS_FRAME_P)
1964 ret = h2c_frt_handle_data(h2c, h2s);
1965
1966 if (h2c->st0 == H2_CS_FRAME_A)
1967 ret = h2c_send_strm_wu(h2c);
1968 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001969
Willy Tarreau92153fc2017-12-03 19:46:19 +01001970 case H2_FT_PRIORITY:
1971 if (h2c->st0 == H2_CS_FRAME_P)
1972 ret = h2c_handle_priority(h2c);
1973 break;
1974
Willy Tarreaucd234e92017-08-18 10:59:39 +02001975 case H2_FT_RST_STREAM:
1976 if (h2c->st0 == H2_CS_FRAME_P)
1977 ret = h2c_handle_rst_stream(h2c, h2s);
1978 break;
1979
Willy Tarreaue96b0922017-10-30 00:28:29 +01001980 case H2_FT_GOAWAY:
1981 if (h2c->st0 == H2_CS_FRAME_P)
1982 ret = h2c_handle_goaway(h2c);
1983 break;
1984
Willy Tarreau1c661982017-10-30 13:52:01 +01001985 case H2_FT_PUSH_PROMISE:
1986 /* not permitted here, RFC7540#5.1 */
1987 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001988 break;
1989
1990 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001991 default:
1992 /* drop frames that we ignore. They may be larger than
1993 * the buffer so we drain all of their contents until
1994 * we reach the end.
1995 */
1996 ret = MIN(h2c->dbuf->i, h2c->dfl);
1997 bi_del(h2c->dbuf, ret);
1998 h2c->dfl -= ret;
1999 ret = h2c->dfl == 0;
2000 }
2001
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002002 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002003 /* We may have to send an RST if not done yet */
2004 if (h2s->st == H2_SS_ERROR)
2005 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002006
Willy Tarreaua20a5192017-12-27 11:02:06 +01002007 if (h2c->st0 == H2_CS_FRAME_E)
2008 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002009
Willy Tarreau7e98c052017-10-10 15:56:59 +02002010 /* error or missing data condition met above ? */
2011 if (ret <= 0)
2012 break;
2013
2014 if (h2c->st0 != H2_CS_FRAME_H) {
2015 bi_del(h2c->dbuf, h2c->dfl);
2016 h2c->st0 = H2_CS_FRAME_H;
2017 }
2018 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002019
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002020 if (h2c->rcvd_c > 0 &&
2021 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2022 h2c_send_conn_wu(h2c);
2023
Willy Tarreau52eed752017-09-22 15:05:09 +02002024 fail:
2025 /* we can go here on missing data, blocked response or error */
2026 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002027}
2028
2029/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2030 * the end.
2031 */
2032static int h2_process_mux(struct h2c *h2c)
2033{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002034 struct h2s *h2s, *h2s_back;
2035
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002036 /* start by sending possibly pending window updates */
2037 if (h2c->rcvd_c > 0 &&
2038 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2039 h2c_send_conn_wu(h2c) < 0)
2040 goto fail;
2041
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002042 /* First we always process the flow control list because the streams
2043 * waiting there were already elected for immediate emission but were
2044 * blocked just on this.
2045 */
2046
2047 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2048 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2049 h2c->st0 >= H2_CS_ERROR)
2050 break;
2051
2052 /* In theory it's possible that h2s->cs == NULL here :
2053 * - client sends crap that causes a parse error
2054 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2055 * - RST_STREAM cannot be emitted because mux is busy/full
2056 * - stream gets notified, detaches and quits
2057 * - mux buffer gets ready and wakes pending streams up
2058 * - bam!
2059 */
2060 h2s->flags &= ~H2_SF_BLK_ANY;
2061
2062 if (h2s->cs) {
2063 h2s->cs->data_cb->send(h2s->cs);
2064 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002065 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002066 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002067 }
2068
2069 /* depending on callee's blocking reasons, we may queue in send
2070 * list or completely dequeue.
2071 */
2072 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2073 if (h2s->flags & H2_SF_BLK_ANY) {
2074 LIST_DEL(&h2s->list);
2075 LIST_ADDQ(&h2c->send_list, &h2s->list);
2076 }
2077 else {
2078 LIST_DEL(&h2s->list);
2079 LIST_INIT(&h2s->list);
2080 if (h2s->cs)
2081 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002082 else {
2083 /* just sent the last frame for this orphaned stream */
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002084 h2c_stream_close(h2c, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002085 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002086 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002087 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002088 }
2089 }
2090 }
2091
2092 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2093 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2094 break;
2095
2096 /* In theory it's possible that h2s->cs == NULL here :
2097 * - client sends crap that causes a parse error
2098 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2099 * - RST_STREAM cannot be emitted because mux is busy/full
2100 * - stream gets notified, detaches and quits
2101 * - mux buffer gets ready and wakes pending streams up
2102 * - bam!
2103 */
2104 h2s->flags &= ~H2_SF_BLK_ANY;
2105
2106 if (h2s->cs) {
2107 h2s->cs->data_cb->send(h2s->cs);
2108 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002109 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002110 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002111 }
2112 /* depending on callee's blocking reasons, we may queue in fctl
2113 * list or completely dequeue.
2114 */
2115 if (h2s->flags & H2_SF_BLK_MFCTL) {
2116 /* stream hit the connection's flow control */
2117 LIST_DEL(&h2s->list);
2118 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2119 }
2120 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2121 LIST_DEL(&h2s->list);
2122 LIST_INIT(&h2s->list);
2123 if (h2s->cs)
2124 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002125 else {
2126 /* just sent the last frame for this orphaned stream */
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002127 h2c_stream_close(h2c, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002128 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002129 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002130 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002131 }
2132 }
2133
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002134 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002135 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002136 if (h2c->st0 == H2_CS_ERROR) {
2137 if (h2c->max_id >= 0) {
2138 h2c_send_goaway_error(h2c, NULL);
2139 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2140 return 0;
2141 }
2142
2143 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2144 }
2145 return 1;
2146 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002147 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002148}
2149
Willy Tarreau71681172017-10-23 14:39:06 +02002150
Willy Tarreau62f52692017-10-08 23:01:42 +02002151/*********************************************************/
2152/* functions below are I/O callbacks from the connection */
2153/*********************************************************/
2154
2155/* callback called on recv event by the connection handler */
2156static void h2_recv(struct connection *conn)
2157{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002158 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002159 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002160 int max;
2161
Willy Tarreau315d8072017-12-10 22:17:57 +01002162 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002163 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002164
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002165 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002166 if (!buf) {
2167 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002168 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002169 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002170
Willy Tarreaua2af5122017-10-09 11:56:46 +02002171 /* note: buf->o == 0 */
2172 max = buf->size - buf->i;
Willy Tarreau315d8072017-12-10 22:17:57 +01002173 if (max)
2174 conn->xprt->rcv_buf(conn, buf, max);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002175
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002176 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002177 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002178 return;
2179 }
2180
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002181 if (buf->i == buf->size)
2182 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002183 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002184}
2185
2186/* callback called on send event by the connection handler */
2187static void h2_send(struct connection *conn)
2188{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002189 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002190 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002191
2192 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002193 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002194
2195 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2196 /* a handshake was requested */
2197 return;
2198 }
2199
Willy Tarreaubc933932017-10-09 16:21:43 +02002200 /* This loop is quite simple : it tries to fill as much as it can from
2201 * pending streams into the existing buffer until it's reportedly full
2202 * or the end of send requests is reached. Then it tries to send this
2203 * buffer's contents out, marks it not full if at least one byte could
2204 * be sent, and tries again.
2205 *
2206 * The snd_buf() function normally takes a "flags" argument which may
2207 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2208 * data immediately comes and CO_SFL_STREAMER to indicate that the
2209 * connection is streaming lots of data (used to increase TLS record
2210 * size at the expense of latency). The former can be sent any time
2211 * there's a buffer full flag, as it indicates at least one stream
2212 * attempted to send and failed so there are pending data. An
2213 * alternative would be to set it as long as there's an active stream
2214 * but that would be problematic for ACKs until we have an absolute
2215 * guarantee that all waiters have at least one byte to send. The
2216 * latter should possibly not be set for now.
2217 */
2218
2219 done = 0;
2220 while (!done) {
2221 unsigned int flags = 0;
2222
2223 /* fill as much as we can into the current buffer */
2224 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2225 done = h2_process_mux(h2c);
2226
2227 if (conn->flags & CO_FL_ERROR)
2228 break;
2229
2230 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2231 flags |= CO_SFL_MSG_MORE;
2232
Willy Tarreau319994a2017-11-07 11:03:56 +01002233 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002234 break;
2235
2236 /* wrote at least one byte, the buffer is not full anymore */
2237 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2238 }
2239
Willy Tarreaua2af5122017-10-09 11:56:46 +02002240 if (conn->flags & CO_FL_SOCK_WR_SH) {
2241 /* output closed, nothing to send, clear the buffer to release it */
2242 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002243 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002244}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002245
Willy Tarreau62f52692017-10-08 23:01:42 +02002246/* callback called on any event by the connection handler.
2247 * It applies changes and returns zero, or < 0 if it wants immediate
2248 * destruction of the connection (which normally doesn not happen in h2).
2249 */
2250static int h2_wake(struct connection *conn)
2251{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002252 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002253 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002254
Willy Tarreaud13bf272017-12-14 10:34:52 +01002255 if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
2256 h2_process_demux(h2c);
2257
2258 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
2259 h2c->dbuf->i = 0;
2260
2261 if (h2c->dbuf->i != h2c->dbuf->size)
2262 h2c->flags &= ~H2_CF_DEM_DFULL;
2263 }
2264
Willy Tarreau8ec14062017-12-30 18:08:13 +01002265 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2266 /* frontend is stopping, reload likely in progress, let's try
2267 * to announce a graceful shutdown if not yet done. We don't
2268 * care if it fails, it will be tried again later.
2269 */
2270 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2271 if (h2c->last_sid < 0)
2272 h2c->last_sid = (1U << 31) - 1;
2273 h2c_send_goaway_error(h2c, NULL);
2274 }
2275 }
2276
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002277 /*
2278 * If we received early data, try to wake any stream, just in case
2279 * at least one of them was waiting for the handshake
2280 */
2281 if ((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA | CO_FL_HANDSHAKE)) ==
2282 CO_FL_EARLY_DATA) {
2283 h2_wake_some_streams(h2c, 0, 0);
2284 conn->flags &= ~CO_FL_EARLY_DATA;
2285 }
Willy Tarreau26bd7612017-10-09 16:47:04 +02002286 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002287 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2288 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2289 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002290 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002291
2292 if (eb_is_empty(&h2c->streams_by_id)) {
2293 /* no more stream, kill the connection now */
2294 h2_release(conn);
2295 return -1;
2296 }
2297 else {
2298 /* some streams still there, we need to signal them all and
2299 * wait for their departure.
2300 */
2301 __conn_xprt_stop_recv(conn);
2302 __conn_xprt_stop_send(conn);
2303 return 0;
2304 }
2305 }
2306
2307 if (!h2c->dbuf->i)
2308 h2_release_dbuf(h2c);
2309
2310 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002311 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002312 __conn_xprt_stop_recv(conn);
2313 }
2314 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002315 __conn_xprt_want_recv(conn);
2316 }
2317
2318 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002319 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2320 (h2c->st0 == H2_CS_ERROR ||
2321 h2c->mbuf->o ||
2322 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2323 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002324 __conn_xprt_want_send(conn);
2325 }
2326 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002327 h2_release_mbuf(h2c);
2328 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002329 }
2330
Willy Tarreau3f133572017-10-31 19:21:06 +01002331 if (h2c->task) {
2332 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002333 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002334 task_queue(h2c->task);
2335 }
2336 else
2337 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002338 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002339 return 0;
2340}
2341
Willy Tarreauea392822017-10-31 10:02:25 +01002342/* Connection timeout management. The principle is that if there's no receipt
2343 * nor sending for a certain amount of time, the connection is closed. If the
2344 * MUX buffer still has lying data or is not allocatable, the connection is
2345 * immediately killed. If it's allocatable and empty, we attempt to send a
2346 * GOAWAY frame.
2347 */
2348static struct task *h2_timeout_task(struct task *t)
2349{
2350 struct h2c *h2c = t->context;
2351 int expired = tick_is_expired(t->expire, now_ms);
2352
2353 if (!expired)
2354 return t;
2355
2356 h2c_error(h2c, H2_ERR_NO_ERROR);
2357 h2_wake_some_streams(h2c, 0, 0);
2358
2359 if (h2c->mbuf->o) {
2360 /* don't even try to send a GOAWAY, the buffer is stuck */
2361 h2c->flags |= H2_CF_GOAWAY_FAILED;
2362 }
2363
2364 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002365 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002366 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2367 h2c->flags |= H2_CF_GOAWAY_FAILED;
2368
2369 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2370 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2371
2372 if (!eb_is_empty(&h2c->streams_by_id))
2373 goto wait;
2374
2375 h2_release(h2c->conn);
2376 return NULL;
2377
2378 wait:
2379 /* the streams have been notified, we must let them finish and close */
2380 h2c->task = NULL;
2381 task_delete(t);
2382 task_free(t);
2383 return NULL;
2384}
2385
2386
Willy Tarreau62f52692017-10-08 23:01:42 +02002387/*******************************************/
2388/* functions below are used by the streams */
2389/*******************************************/
2390
2391/*
2392 * Attach a new stream to a connection
2393 * (Used for outgoing connections)
2394 */
2395static struct conn_stream *h2_attach(struct connection *conn)
2396{
2397 return NULL;
2398}
2399
2400/* callback used to update the mux's polling flags after changing a cs' status.
2401 * The caller (cs_update_mux_polling) will take care of propagating any changes
2402 * to the transport layer.
2403 */
2404static void h2_update_poll(struct conn_stream *cs)
2405{
Willy Tarreau1d393222017-10-17 10:26:19 +02002406 struct h2s *h2s = cs->ctx;
2407
2408 if (!h2s)
2409 return;
2410
Willy Tarreaud7739c82017-10-30 15:38:23 +01002411 /* we may unblock a blocked read */
2412
Willy Tarreau315d8072017-12-10 22:17:57 +01002413 if (cs->flags & CS_FL_DATA_RD_ENA) {
2414 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002415 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002416 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002417 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002418 conn_xprt_want_send(cs->conn);
2419 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002420 }
2421
Willy Tarreau1d393222017-10-17 10:26:19 +02002422 /* Note: the stream and stream-int code doesn't allow us to perform a
2423 * synchronous send() here unfortunately, because this code is called
2424 * as si_update() from the process_stream() context. This means that
2425 * we have to queue the current cs and defer its processing after the
2426 * connection's cs list is processed anyway.
2427 */
2428
2429 if (cs->flags & CS_FL_DATA_WR_ENA) {
2430 if (LIST_ISEMPTY(&h2s->list)) {
2431 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2432 !h2s->h2c->mbuf->o && // not yet subscribed
2433 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2434 conn_xprt_want_send(cs->conn);
2435 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2436 }
2437 }
2438 else if (!LIST_ISEMPTY(&h2s->list)) {
2439 LIST_DEL(&h2s->list);
2440 LIST_INIT(&h2s->list);
2441 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2442 }
2443
2444 /* this can happen from within si_chk_snd() */
2445 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2446 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002447}
2448
2449/*
2450 * Detach the stream from the connection and possibly release the connection.
2451 */
2452static void h2_detach(struct conn_stream *cs)
2453{
Willy Tarreau60935142017-10-16 18:11:19 +02002454 struct h2s *h2s = cs->ctx;
2455 struct h2c *h2c;
2456
2457 cs->ctx = NULL;
2458 if (!h2s)
2459 return;
2460
2461 h2c = h2s->h2c;
2462 h2s->cs = NULL;
2463
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002464 /* this stream may be blocked waiting for some data to leave (possibly
2465 * an ES or RST frame), so orphan it in this case.
2466 */
2467 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2468 return;
2469
Willy Tarreau541dd822017-11-23 18:12:50 +01002470 /* the stream could be in the send list */
2471 LIST_DEL(&h2s->list);
2472
Willy Tarreau45f752e2017-10-30 15:44:59 +01002473 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2474 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2475 /* unblock the connection if it was blocked on this
2476 * stream.
2477 */
2478 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2479 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2480 conn_xprt_want_recv(cs->conn);
2481 conn_xprt_want_send(cs->conn);
2482 }
2483
Willy Tarreau60935142017-10-16 18:11:19 +02002484 if (h2s->by_id.node.leaf_p) {
2485 /* h2s still attached to the h2c */
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002486 h2c_stream_close(h2c, h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002487 eb32_delete(&h2s->by_id);
2488
2489 /* We don't want to close right now unless we're removing the
2490 * last stream, and either the connection is in error, or it
2491 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002492 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002493 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002494 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2495 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002496 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002497 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2498 (conn_xprt_read0_pending(h2c->conn) ||
2499 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002500 /* no more stream will come, kill it now */
2501 h2_release(h2c->conn);
2502 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002503 else if (h2c->task) {
2504 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002505 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002506 task_queue(h2c->task);
2507 }
2508 else
2509 h2c->task->expire = TICK_ETERNITY;
2510 }
Willy Tarreau60935142017-10-16 18:11:19 +02002511 }
Willy Tarreaubafbe012017-11-24 17:34:44 +01002512 pool_free(pool_head_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002513}
2514
2515static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2516{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002517 struct h2s *h2s = cs->ctx;
2518
2519 if (!mode)
2520 return;
2521
Willy Tarreau721c9742017-11-07 11:05:42 +01002522 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002523 return;
2524
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002525 /* if no outgoing data was seen on this stream, it means it was
2526 * closed with a "tcp-request content" rule that is normally
2527 * used to kill the connection ASAP (eg: limit abuse). In this
2528 * case we send a goaway to close the connection.
2529 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002530 if (!(h2s->flags & H2_SF_RST_SENT) &&
2531 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2532 return;
2533
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002534 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2535 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2536 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2537 return;
2538
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002539 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2540 conn_xprt_want_send(cs->conn);
2541
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002542 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002543}
2544
2545static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2546{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002547 struct h2s *h2s = cs->ctx;
2548
Willy Tarreau721c9742017-11-07 11:05:42 +01002549 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002550 return;
2551
Willy Tarreau67434202017-11-06 20:20:51 +01002552 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002553 /* we can cleanly close using an empty data frame only after headers */
2554
2555 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2556 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002557 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002558
2559 if (h2s->st == H2_SS_HREM)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002560 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002561 else
2562 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002563 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002564 /* if no outgoing data was seen on this stream, it means it was
2565 * closed with a "tcp-request content" rule that is normally
2566 * used to kill the connection ASAP (eg: limit abuse). In this
2567 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002568 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002569 if (!(h2s->flags & H2_SF_RST_SENT) &&
2570 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2571 return;
2572
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002573 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2574 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002575 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2576 return;
2577
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002578 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002579 }
2580
2581 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2582 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002583}
2584
Willy Tarreau13278b42017-10-13 19:23:14 +02002585/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2586 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2587 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002588 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002589 */
2590static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2591{
2592 struct h2c *h2c = h2s->h2c;
2593 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002594 struct chunk *tmp = get_trash_chunk();
2595 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002596 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002597 int flen = h2c->dfl;
2598 int outlen = 0;
2599 int wrap;
2600 int try;
2601
2602 if (!h2c->dfl) {
2603 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002604 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002605 return 0;
2606 }
2607
Willy Tarreau68472622017-12-11 18:36:37 +01002608 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2609 return 0; // incomplete input frame
2610
Willy Tarreau13278b42017-10-13 19:23:14 +02002611 /* if the input buffer wraps, take a temporary copy of it (rare) */
2612 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2613 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002614 copy = alloc_trash_chunk();
2615 if (!copy) {
2616 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2617 goto fail;
2618 }
2619 memcpy(copy->str, h2c->dbuf->p, wrap);
2620 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2621 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002622 }
2623
2624 /* The padlen is the first byte before data, and the padding appears
2625 * after data. padlen+data+padding are included in flen.
2626 */
2627 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002628 h2c->dpl = *hdrs;
2629 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002630 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2631 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002632 return 0;
2633 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002634 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002635 hdrs += 1; // skip Pad Length
2636 }
2637
2638 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2639 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002640 if (read_n32(hdrs) == h2s->id) {
2641 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2642 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2643 return 0;//goto fail_stream;
2644 }
2645
Willy Tarreau13278b42017-10-13 19:23:14 +02002646 hdrs += 5; // stream dep = 4, weight = 1
2647 flen -= 5;
2648 }
2649
2650 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2651 * don't support this for now and can't even decompress so we have to
2652 * break the connection.
2653 */
2654 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2655 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002656 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002657 }
2658
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002659 /* we can't retry a failed decompression operation so we must be very
2660 * careful not to take any risks. In practice the output buffer is
2661 * always empty except maybe for trailers, so these operations almost
2662 * never happen.
2663 */
2664 if (unlikely(buf->o)) {
2665 /* need to let the output buffer flush and
2666 * mark the buffer for later wake up.
2667 */
2668 goto fail;
2669 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002670
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002671 if (unlikely(buffer_space_wraps(buf))) {
2672 /* it doesn't fit and the buffer is fragmented,
2673 * so let's defragment it and try again.
2674 */
2675 buffer_slow_realign(buf);
2676 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002677
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002678 /* first check if we have some room after p+i */
2679 try = buf->data + buf->size - (buf->p + buf->i);
2680
2681 /* otherwise continue between data and p-o */
2682 if (try <= 0) {
2683 try = buf->p - (buf->data + buf->o);
2684 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002685 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002686 }
2687 if (try > count)
2688 try = count;
2689
2690 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2691 sizeof(list)/sizeof(list[0]), tmp);
2692 if (outlen < 0) {
2693 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2694 goto fail;
2695 }
2696
2697 /* OK now we have our header list in <list> */
2698 outlen = h2_make_h1_request(list, bi_end(buf), try);
2699
2700 if (outlen < 0) {
2701 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2702 goto fail;
2703 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002704
2705 /* now consume the input data */
2706 bi_del(h2c->dbuf, h2c->dfl);
2707 h2c->st0 = H2_CS_FRAME_H;
2708 buf->i += outlen;
2709
2710 /* don't send it before returning data!
2711 * FIXME: should we instead try to send it much later, after the
2712 * response ? This would require that we keep a copy of it in h2s.
2713 */
2714 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2715 h2s->cs->flags |= CS_FL_EOS;
2716 h2s->flags |= H2_SF_ES_RCVD;
2717 }
2718
Willy Tarreau68dd9852017-07-03 14:44:26 +02002719 leave:
2720 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002721 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002722 fail:
2723 outlen = 0;
2724 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002725}
2726
Willy Tarreau454f9052017-10-26 19:40:35 +02002727/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2728 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2729 * in use, a new chunk is emitted for each frame. This is supposed to fit
2730 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2731 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2732 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2733 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002734 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2735 * checked to know if some data remain pending (an empty DATA frame can return
2736 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2737 * connection errors in h2c->errcode. The caller must already have checked the
2738 * frame header and ensured that the frame was complete or the buffer full. It
2739 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002740 */
2741static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2742{
2743 struct h2c *h2c = h2s->h2c;
2744 int block1, block2;
2745 unsigned int flen = h2c->dfl;
Willy Tarreau454f9052017-10-26 19:40:35 +02002746
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002747 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002748 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002749
2750 /* The padlen is the first byte before data, and the padding appears
2751 * after data. padlen+data+padding are included in flen.
2752 */
Willy Tarreau79127812017-12-03 21:06:59 +01002753 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002754 if (h2c->dbuf->i < 1)
2755 return 0;
2756
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002757 h2c->dpl = *(uint8_t *)bi_ptr(h2c->dbuf);
2758 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002759 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2760 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002761 return 0;
2762 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002763
2764 /* skip the padlen byte */
2765 bi_del(h2c->dbuf, 1);
2766 h2c->dfl--;
2767 h2c->rcvd_c++; h2c->rcvd_s++;
2768 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002769 }
2770
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002771 flen = h2c->dfl - h2c->dpl;
2772 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002773 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002774
2775 if (flen > h2c->dbuf->i) {
2776 flen = h2c->dbuf->i;
2777 if (!flen)
2778 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002779 }
2780
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002781 /* does it fit in output buffer or should we wait ? */
2782 if (flen > count) {
2783 flen = count;
2784 if (!flen) {
2785 h2c->flags |= H2_CF_DEM_SFULL;
2786 h2s->cs->flags |= CS_FL_RCV_MORE;
2787 return 0;
2788 }
2789 }
2790
Willy Tarreau454f9052017-10-26 19:40:35 +02002791 /* Block1 is the length of the first block before the buffer wraps,
2792 * block2 is the optional second block to reach the end of the frame.
2793 */
2794 block1 = bi_contig_data(h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002795 if (block1 > flen)
2796 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002797 block2 = flen - block1;
2798
2799 if (block1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002800 bi_putblk(buf, b_ptr(h2c->dbuf, 0), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002801
2802 if (block2)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002803 bi_putblk(buf, b_ptr(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002804
2805 /* now mark the input data as consumed (will be deleted from the buffer
2806 * by the caller when seeing FRAME_A after sending the window update).
2807 */
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002808 bi_del(h2c->dbuf, flen);
2809 h2c->dfl -= flen;
2810 h2c->rcvd_c += flen;
2811 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2812
2813 if (h2c->dfl > h2c->dpl) {
2814 /* more data available, transfer stalled on stream full */
2815 h2c->flags |= H2_CF_DEM_SFULL;
2816 h2s->cs->flags |= CS_FL_RCV_MORE;
2817 return flen;
2818 }
2819
Willy Tarreau4a28da12018-01-04 14:41:00 +01002820 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002821 /* here we're done with the frame, all the payload (except padding) was
2822 * transferred.
2823 */
Willy Tarreau454f9052017-10-26 19:40:35 +02002824 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2825
2826 /* don't send it before returning data!
2827 * FIXME: should we instead try to send it much later, after the
2828 * response ? This would require that we keep a copy of it in h2s.
2829 */
Willy Tarreau79127812017-12-03 21:06:59 +01002830 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002831 h2s->cs->flags |= CS_FL_EOS;
2832 h2s->flags |= H2_SF_ES_RCVD;
2833 }
2834
2835 return flen;
2836}
2837
Willy Tarreau62f52692017-10-08 23:01:42 +02002838/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002839 * Called from the upper layer to get more data, up to <count> bytes. The
2840 * caller is responsible for never asking for more data than what is available
2841 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002842 */
2843static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2844{
Willy Tarreau13278b42017-10-13 19:23:14 +02002845 struct h2s *h2s = cs->ctx;
2846 struct h2c *h2c = h2s->h2c;
2847 int ret = 0;
2848
2849 if (h2c->st0 != H2_CS_FRAME_P)
2850 return 0; // no pre-parsed frame yet
2851
2852 if (h2c->dsi != h2s->id)
2853 return 0; // not for us
2854
2855 if (!h2c->dbuf->size)
2856 return 0; // empty buffer
2857
Willy Tarreau13278b42017-10-13 19:23:14 +02002858 switch (h2c->dft) {
2859 case H2_FT_HEADERS:
2860 ret = h2_frt_decode_headers(h2s, buf, count);
2861 break;
2862
Willy Tarreau454f9052017-10-26 19:40:35 +02002863 case H2_FT_DATA:
2864 ret = h2_frt_transfer_data(h2s, buf, count);
2865 break;
2866
Willy Tarreau13278b42017-10-13 19:23:14 +02002867 default:
2868 ret = 0;
2869 }
2870 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002871}
2872
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002873/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2874 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2875 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2876 * to the number of buffer bytes consumed.
2877 */
2878static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2879{
2880 struct http_hdr list[MAX_HTTP_HDR];
2881 struct h2c *h2c = h2s->h2c;
2882 struct h1m *h1m = &h2s->res;
2883 struct chunk outbuf;
2884 int es_now = 0;
2885 int ret = 0;
2886 int hdr;
2887
2888 if (h2c_mux_busy(h2c, h2s)) {
2889 h2s->flags |= H2_SF_BLK_MBUSY;
2890 return 0;
2891 }
2892
2893 if (!h2_get_mbuf(h2c)) {
2894 h2c->flags |= H2_CF_MUX_MALLOC;
2895 h2s->flags |= H2_SF_BLK_MROOM;
2896 return 0;
2897 }
2898
2899 /* First, try to parse the H1 response and index it into <list>.
2900 * NOTE! Since it comes from haproxy, we *know* that a response header
2901 * block does not wrap and we can safely read it this way without
2902 * having to realign the buffer.
2903 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002904 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002905 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2906 list, sizeof(list)/sizeof(list[0]), h1m);
2907 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002908 /* incomplete or invalid response, this is abnormal coming from
2909 * haproxy and may only result in a bad errorfile or bad Lua code
2910 * so that won't be fixed, raise an error now.
2911 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002912 * FIXME: we should instead add the ability to only return a
2913 * 502 bad gateway. But in theory this is not supposed to
2914 * happen.
2915 */
2916 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2917 ret = 0;
2918 goto end;
2919 }
2920
2921 chunk_reset(&outbuf);
2922
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002923 try_again:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002924 while (1) {
2925 outbuf.str = bo_end(h2c->mbuf);
2926 outbuf.size = bo_contig_space(h2c->mbuf);
2927 outbuf.len = 0;
2928
2929 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2930 break;
2931 realign_again:
2932 buffer_slow_realign(h2c->mbuf);
2933 }
2934
2935 if (outbuf.size < 9) {
2936 h2c->flags |= H2_CF_MUX_MFULL;
2937 h2s->flags |= H2_SF_BLK_MROOM;
2938 ret = 0;
2939 goto end;
2940 }
2941
2942 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2943 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2944 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2945 outbuf.len = 9;
2946
2947 /* encode status, which necessarily is the first one */
2948 if (outbuf.len < outbuf.size && h1m->status == 200)
2949 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2950 else if (outbuf.len < outbuf.size && h1m->status == 304)
2951 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002952 else if (unlikely(list[0].v.len != 3)) {
2953 /* this is an unparsable response */
2954 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2955 ret = 0;
2956 goto end;
2957 }
2958 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002959 /* basic encoding of the status code */
2960 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2961 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2962 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2963 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2964 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2965 }
2966 else {
2967 if (buffer_space_wraps(h2c->mbuf))
2968 goto realign_again;
2969
2970 h2c->flags |= H2_CF_MUX_MFULL;
2971 h2s->flags |= H2_SF_BLK_MROOM;
2972 ret = 0;
2973 goto end;
2974 }
2975
2976 /* encode all headers, stop at empty name */
2977 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002978 /* these ones do not exist in H2 and must be dropped. */
2979 if (isteq(list[hdr].n, ist("connection")) ||
2980 isteq(list[hdr].n, ist("proxy-connection")) ||
2981 isteq(list[hdr].n, ist("keep-alive")) ||
2982 isteq(list[hdr].n, ist("upgrade")) ||
2983 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002984 continue;
2985
2986 if (isteq(list[hdr].n, ist("")))
2987 break; // end
2988
2989 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2990 /* output full */
2991 if (buffer_space_wraps(h2c->mbuf))
2992 goto realign_again;
2993
2994 h2c->flags |= H2_CF_MUX_MFULL;
2995 h2s->flags |= H2_SF_BLK_MROOM;
2996 ret = 0;
2997 goto end;
2998 }
2999 }
3000
3001 /* we may need to add END_STREAM */
3002 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3003 es_now = 1;
3004
3005 /* update the frame's size */
3006 h2_set_frame_size(outbuf.str, outbuf.len - 9);
3007
3008 if (es_now)
3009 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
3010
3011 /* consume incoming H1 response */
3012 bo_del(buf, ret);
3013
3014 /* commit the H2 response */
3015 h2c->mbuf->o += outbuf.len;
3016 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01003017 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003018
3019 /* for now we don't implemented CONTINUATION, so we wait for a
3020 * body or directly end in TRL2.
3021 */
3022 if (es_now) {
3023 h1m->state = HTTP_MSG_DONE;
3024 h2s->flags |= H2_SF_ES_SENT;
3025 if (h2s->st == H2_SS_OPEN)
3026 h2s->st = H2_SS_HLOC;
3027 else
Willy Tarreau91bfdd72017-12-14 12:00:14 +01003028 h2c_stream_close(h2c, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003029 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003030 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003031 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003032 h1m->state = HTTP_MSG_RPBEFORE;
3033 h1m->status = 0;
3034 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003035 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003036 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003037 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003038 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003039
3040 end:
3041 //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));
3042 return ret;
3043}
3044
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003045/* Try to send a DATA frame matching HTTP/1 response present in the response
3046 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
3047 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
3048 * corresponds to the number of buffer bytes consumed.
3049 */
3050static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
3051{
3052 struct h2c *h2c = h2s->h2c;
3053 struct h1m *h1m = &h2s->res;
3054 struct chunk outbuf;
3055 int ret = 0;
3056 int total = 0;
3057 int es_now = 0;
3058 int size = 0;
3059 char *blk1, *blk2;
3060 int len1, len2;
3061
3062 if (h2c_mux_busy(h2c, h2s)) {
3063 h2s->flags |= H2_SF_BLK_MBUSY;
3064 goto end;
3065 }
3066
3067 if (!h2_get_mbuf(h2c)) {
3068 h2c->flags |= H2_CF_MUX_MALLOC;
3069 h2s->flags |= H2_SF_BLK_MROOM;
3070 goto end;
3071 }
3072
3073 new_frame:
3074 if (!buf->o)
3075 goto end;
3076
3077 chunk_reset(&outbuf);
3078
3079 while (1) {
3080 outbuf.str = bo_end(h2c->mbuf);
3081 outbuf.size = bo_contig_space(h2c->mbuf);
3082 outbuf.len = 0;
3083
3084 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
3085 break;
3086 realign_again:
3087 buffer_slow_realign(h2c->mbuf);
3088 }
3089
3090 if (outbuf.size < 9) {
3091 h2c->flags |= H2_CF_MUX_MFULL;
3092 h2s->flags |= H2_SF_BLK_MROOM;
3093 goto end;
3094 }
3095
3096 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3097 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3098 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3099 outbuf.len = 9;
3100
3101 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3102 case 0: /* no content length, read till SHUTW */
3103 size = buf->o;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003104 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003105 break;
3106 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
3107 size = buf->o;
3108 if ((long long)size > h1m->curr_len)
3109 size = h1m->curr_len;
3110 break;
3111 default: /* te:chunked : parse chunks */
3112 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
3113 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
3114 if (!ret)
3115 goto end;
3116
3117 if (ret < 0) {
3118 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3119 h1m->err_pos = ret;
3120 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3121 goto end;
3122 }
3123 bo_del(buf, ret);
3124 total += ret;
3125 h1m->state = HTTP_MSG_CHUNK_SIZE;
3126 }
3127
3128 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3129 unsigned int chunk;
3130
3131 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
3132 if (!ret)
3133 goto end;
3134
3135 if (ret < 0) {
3136 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3137 h1m->err_pos = ret;
3138 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3139 goto end;
3140 }
3141
3142 size = chunk;
3143 h1m->curr_len = chunk;
3144 h1m->body_len += chunk;
3145 bo_del(buf, ret);
3146 total += ret;
3147 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3148 if (!size)
3149 goto send_empty;
3150 }
3151
3152 /* in MSG_DATA state, continue below */
3153 size = h1m->curr_len;
3154 break;
3155 }
3156
3157 /* we have in <size> the exact number of bytes we need to copy from
3158 * the H1 buffer. We need to check this against the connection's and
3159 * the stream's send windows, and to ensure that this fits in the max
3160 * frame size and in the buffer's available space minus 9 bytes (for
3161 * the frame header). The connection's flow control is applied last so
3162 * that we can use a separate list of streams which are immediately
3163 * unblocked on window opening. Note: we don't implement padding.
3164 */
3165
3166 if (size > buf->o)
3167 size = buf->o;
3168
3169 if (size > h2s->mws)
3170 size = h2s->mws;
3171
3172 if (size <= 0) {
3173 h2s->flags |= H2_SF_BLK_SFCTL;
3174 goto end;
3175 }
3176
3177 if (h2c->mfs && size > h2c->mfs)
3178 size = h2c->mfs;
3179
3180 if (size + 9 > outbuf.size) {
3181 /* we have an opportunity for enlarging the too small
3182 * available space, let's try.
3183 */
3184 if (buffer_space_wraps(h2c->mbuf))
3185 goto realign_again;
3186 size = outbuf.size - 9;
3187 }
3188
3189 if (size <= 0) {
3190 h2c->flags |= H2_CF_MUX_MFULL;
3191 h2s->flags |= H2_SF_BLK_MROOM;
3192 goto end;
3193 }
3194
3195 if (size > h2c->mws)
3196 size = h2c->mws;
3197
3198 if (size <= 0) {
3199 h2s->flags |= H2_SF_BLK_MFCTL;
3200 goto end;
3201 }
3202
3203 /* copy whatever we can */
3204 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
3205 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
3206 if (ret == 1)
3207 len2 = 0;
3208
3209 if (!ret || len1 + len2 < size) {
3210 /* FIXME: must normally never happen */
3211 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3212 goto end;
3213 }
3214
3215 /* limit len1/len2 to size */
3216 if (len1 + len2 > size) {
3217 int sub = len1 + len2 - size;
3218
3219 if (len2 > sub)
3220 len2 -= sub;
3221 else {
3222 sub -= len2;
3223 len2 = 0;
3224 len1 -= sub;
3225 }
3226 }
3227
3228 /* now let's copy this this into the output buffer */
3229 memcpy(outbuf.str + 9, blk1, len1);
3230 if (len2)
3231 memcpy(outbuf.str + 9 + len1, blk2, len2);
3232
3233 send_empty:
3234 /* we may need to add END_STREAM */
3235 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3236 * could rely on the MSG_MORE flag as a hint for this ?
3237 */
3238 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3239 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3240 es_now = 1;
3241
3242 /* update the frame's size */
3243 h2_set_frame_size(outbuf.str, size);
3244
3245 if (es_now)
3246 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3247
3248 /* commit the H2 response */
3249 h2c->mbuf->o += size + 9;
3250 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3251
3252 /* consume incoming H1 response */
3253 if (size > 0) {
3254 bo_del(buf, size);
3255 total += size;
3256 h1m->curr_len -= size;
3257 h2s->mws -= size;
3258 h2c->mws -= size;
3259
3260 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3261 h1m->state = HTTP_MSG_CHUNK_CRLF;
3262 goto new_frame;
3263 }
3264 }
3265
3266 if (es_now) {
3267 if (h2s->st == H2_SS_OPEN)
3268 h2s->st = H2_SS_HLOC;
3269 else
Willy Tarreau91bfdd72017-12-14 12:00:14 +01003270 h2c_stream_close(h2c, h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003271
3272 if (!(h1m->flags & H1_MF_CHNK))
3273 h1m->state = HTTP_MSG_DONE;
3274
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003275 h2s->flags |= H2_SF_ES_SENT;
3276 }
3277
3278 end:
3279 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);
3280 return total;
3281}
3282
Willy Tarreau62f52692017-10-08 23:01:42 +02003283/* Called from the upper layer, to send data */
3284static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3285{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003286 struct h2s *h2s = cs->ctx;
3287 int total = 0;
3288
Willy Tarreauc4312d32017-11-07 12:01:53 +01003289 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3290 h2s->flags |= H2_SF_OUTGOING_DATA;
3291
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003292 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3293 if (h2s->res.state < HTTP_MSG_BODY) {
3294 total += h2s_frt_make_resp_headers(h2s, buf);
3295
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003296 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003297 break;
3298
3299 if (h2s->flags & H2_SF_BLK_ANY)
3300 break;
3301 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003302 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3303 total += h2s_frt_make_resp_data(h2s, buf);
3304
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003305 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003306 break;
3307
3308 if (h2s->flags & H2_SF_BLK_ANY)
3309 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003310 }
3311 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3312 /* consume the trailers if any (we don't forward them for now) */
3313 int count = h1_measure_trailers(buf);
3314
3315 if (unlikely(count <= 0)) {
3316 if (count < 0)
3317 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3318 break;
3319 }
3320 total += count;
3321 bo_del(buf, count);
3322 h2s->res.state = HTTP_MSG_DONE;
3323 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003324 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003325 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003326 cs->flags |= CS_FL_ERROR;
3327 break;
3328 }
3329 }
3330
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003331 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003332 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003333 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003334 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01003335 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003336 }
3337
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003338 if (h2s->flags & H2_SF_BLK_SFCTL) {
3339 /* stream flow control, quit the list */
3340 LIST_DEL(&h2s->list);
3341 LIST_INIT(&h2s->list);
3342 }
3343
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003344 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003345}
3346
3347
3348/*******************************************************/
3349/* functions below are dedicated to the config parsers */
3350/*******************************************************/
3351
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003352/* config parser for global "tune.h2.header-table-size" */
3353static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3354 struct proxy *defpx, const char *file, int line,
3355 char **err)
3356{
3357 if (too_many_args(1, args, err, NULL))
3358 return -1;
3359
3360 h2_settings_header_table_size = atoi(args[1]);
3361 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3362 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3363 return -1;
3364 }
3365 return 0;
3366}
Willy Tarreau62f52692017-10-08 23:01:42 +02003367
Willy Tarreaue6baec02017-07-27 11:45:11 +02003368/* config parser for global "tune.h2.initial-window-size" */
3369static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3370 struct proxy *defpx, const char *file, int line,
3371 char **err)
3372{
3373 if (too_many_args(1, args, err, NULL))
3374 return -1;
3375
3376 h2_settings_initial_window_size = atoi(args[1]);
3377 if (h2_settings_initial_window_size < 0) {
3378 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3379 return -1;
3380 }
3381 return 0;
3382}
3383
Willy Tarreau5242ef82017-07-27 11:47:28 +02003384/* config parser for global "tune.h2.max-concurrent-streams" */
3385static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3386 struct proxy *defpx, const char *file, int line,
3387 char **err)
3388{
3389 if (too_many_args(1, args, err, NULL))
3390 return -1;
3391
3392 h2_settings_max_concurrent_streams = atoi(args[1]);
3393 if (h2_settings_max_concurrent_streams < 0) {
3394 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3395 return -1;
3396 }
3397 return 0;
3398}
3399
Willy Tarreau62f52692017-10-08 23:01:42 +02003400
3401/****************************************/
3402/* MUX initialization and instanciation */
3403/***************************************/
3404
3405/* The mux operations */
3406const struct mux_ops h2_ops = {
3407 .init = h2_init,
3408 .recv = h2_recv,
3409 .send = h2_send,
3410 .wake = h2_wake,
3411 .update_poll = h2_update_poll,
3412 .rcv_buf = h2_rcv_buf,
3413 .snd_buf = h2_snd_buf,
3414 .attach = h2_attach,
3415 .detach = h2_detach,
3416 .shutr = h2_shutr,
3417 .shutw = h2_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003418 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003419 .name = "H2",
3420};
3421
3422/* ALPN selection : this mux registers ALPN tolen "h2" */
3423static struct alpn_mux_list alpn_mux_h2 =
3424 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3425
3426/* config keyword parsers */
3427static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003428 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003429 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003430 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003431 { 0, NULL, NULL }
3432}};
3433
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003434static void __h2_deinit(void)
3435{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003436 pool_destroy(pool_head_h2s);
3437 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003438}
3439
Willy Tarreau62f52692017-10-08 23:01:42 +02003440__attribute__((constructor))
3441static void __h2_init(void)
3442{
3443 alpn_register_mux(&alpn_mux_h2);
3444 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003445 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003446 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3447 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003448}