blob: ebefd5200ae2d9f55c2b65936c4ae99a6bf50a54 [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
Olivier Houchard6fa63d92017-11-27 18:41:32 +010062#define H2_CF_WAIT_FOR_HS 0x00000400 // We did check that at least a stream was waiting for handshake
Willy Tarreau081d4722017-05-16 21:51:05 +020063
64
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
93 struct buffer *dbuf; /* demux buffer */
94
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
104 struct buffer *mbuf; /* mux buffer */
105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
117 /* 32 bit hole here */
Willy Tarreauea392822017-10-31 10:02:25 +0100118 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119 struct eb_root streams_by_id; /* all active streams by their ID */
120 struct list send_list; /* list of blocked streams requesting to send */
121 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
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 Tarreau44e973f2018-03-01 17:49:30 +0100255/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
256 * flags are used to figure what buffer was requested. It returns 1 if the
257 * allocation succeeds, in which case the connection is woken up, or 0 if it's
258 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200259 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100260static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200261{
262 struct h2c *h2c = target;
263
Willy Tarreau44e973f2018-03-01 17:49:30 +0100264 if ((h2c->flags & H2_CF_DEM_DALLOC) && 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 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200270
Willy Tarreau44e973f2018-03-01 17:49:30 +0100271 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
272 h2c->flags &= ~H2_CF_MUX_MALLOC;
273 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
274 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200275
276 if (h2c->flags & H2_CF_DEM_MROOM) {
277 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100278 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200279 conn_xprt_want_recv(h2c->conn);
280 }
Willy Tarreau14398122017-09-22 14:26:04 +0200281 return 1;
282 }
283 return 0;
284}
285
Willy Tarreau44e973f2018-03-01 17:49:30 +0100286static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer **bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200287{
288 struct buffer *buf = NULL;
289
Willy Tarreau44e973f2018-03-01 17:49:30 +0100290 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
291 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
292 h2c->buf_wait.target = h2c;
293 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100294 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100295 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100296 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200297 __conn_xprt_stop_recv(h2c->conn);
298 }
299 return buf;
300}
301
Willy Tarreau44e973f2018-03-01 17:49:30 +0100302static inline void h2_release_buf(struct h2c *h2c, struct buffer **bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200303{
Willy Tarreau44e973f2018-03-01 17:49:30 +0100304 if ((*bptr)->size) {
305 b_free(bptr);
306 offer_buffers(h2c->buf_wait.target,
Willy Tarreau14398122017-09-22 14:26:04 +0200307 tasks_run_queue + applets_active_queue);
308 }
309}
310
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200311
Willy Tarreau62f52692017-10-08 23:01:42 +0200312/*****************************************************************/
313/* functions below are dedicated to the mux setup and management */
314/*****************************************************************/
315
Willy Tarreau32218eb2017-09-22 08:07:25 +0200316/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
317static int h2c_frt_init(struct connection *conn)
318{
319 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100320 struct task *t = NULL;
321 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200322
Willy Tarreaubafbe012017-11-24 17:34:44 +0100323 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200324 if (!h2c)
325 goto fail;
326
Willy Tarreau3f133572017-10-31 19:21:06 +0100327
Willy Tarreau599391a2017-11-24 10:16:00 +0100328 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
329 if (tick_isset(sess->fe->timeout.clientfin))
330 h2c->shut_timeout = sess->fe->timeout.clientfin;
331
Willy Tarreau33400292017-11-05 11:23:40 +0100332 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100333 if (tick_isset(h2c->timeout)) {
334 t = task_new(tid_bit);
335 if (!t)
336 goto fail;
337
338 h2c->task = t;
339 t->process = h2_timeout_task;
340 t->context = h2c;
341 t->expire = tick_add(now_ms, h2c->timeout);
342 }
Willy Tarreauea392822017-10-31 10:02:25 +0100343
Willy Tarreau32218eb2017-09-22 08:07:25 +0200344 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
345 if (!h2c->ddht)
346 goto fail;
347
348 /* Initialise the context. */
349 h2c->st0 = H2_CS_PREFACE;
350 h2c->conn = conn;
351 h2c->max_id = -1;
352 h2c->errcode = H2_ERR_NO_ERROR;
353 h2c->flags = H2_CF_NONE;
354 h2c->rcvd_c = 0;
355 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100356 h2c->nb_streams = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200357
358 h2c->dbuf = &buf_empty;
359 h2c->dsi = -1;
360 h2c->msi = -1;
361 h2c->last_sid = -1;
362
363 h2c->mbuf = &buf_empty;
364 h2c->miw = 65535; /* mux initial window size */
365 h2c->mws = 65535; /* mux window size */
366 h2c->mfs = 16384; /* initial max frame size */
367 h2c->streams_by_id = EB_ROOT_UNIQUE;
368 LIST_INIT(&h2c->send_list);
369 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100370 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200371 conn->mux_ctx = h2c;
372
Willy Tarreau3f133572017-10-31 19:21:06 +0100373 if (t)
374 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200375 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100376
Willy Tarreau32218eb2017-09-22 08:07:25 +0200377 /* mux->wake will be called soon to complete the operation */
378 return 0;
379 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100380 if (t)
381 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100382 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200383 return -1;
384}
385
Willy Tarreau62f52692017-10-08 23:01:42 +0200386/* Initialize the mux once it's attached. For outgoing connections, the context
387 * is already initialized before installing the mux, so we detect incoming
388 * connections from the fact that the context is still NULL. Returns < 0 on
389 * error.
390 */
391static int h2_init(struct connection *conn)
392{
393 if (conn->mux_ctx) {
394 /* we don't support outgoing connections for now */
395 return -1;
396 }
397
Willy Tarreau32218eb2017-09-22 08:07:25 +0200398 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200399}
400
Willy Tarreau2373acc2017-10-12 17:35:14 +0200401/* returns the stream associated with id <id> or NULL if not found */
402static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
403{
404 struct eb32_node *node;
405
Willy Tarreau2a856182017-05-16 15:20:39 +0200406 if (id > h2c->max_id)
407 return (struct h2s *)h2_idle_stream;
408
Willy Tarreau2373acc2017-10-12 17:35:14 +0200409 node = eb32_lookup(&h2c->streams_by_id, id);
410 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200411 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200412
413 return container_of(node, struct h2s, by_id);
414}
415
Willy Tarreau62f52692017-10-08 23:01:42 +0200416/* release function for a connection. This one should be called to free all
417 * resources allocated to the mux.
418 */
419static void h2_release(struct connection *conn)
420{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200421 struct h2c *h2c = conn->mux_ctx;
422
423 LIST_DEL(&conn->list);
424
425 if (h2c) {
426 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200427
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100428 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100429 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100430 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200431
Willy Tarreau44e973f2018-03-01 17:49:30 +0100432 h2_release_buf(h2c, &h2c->dbuf);
433 h2_release_buf(h2c, &h2c->mbuf);
434
Willy Tarreauea392822017-10-31 10:02:25 +0100435 if (h2c->task) {
436 task_delete(h2c->task);
437 task_free(h2c->task);
438 h2c->task = NULL;
439 }
440
Willy Tarreaubafbe012017-11-24 17:34:44 +0100441 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200442 }
443
444 conn->mux = NULL;
445 conn->mux_ctx = NULL;
446
447 conn_stop_tracking(conn);
448 conn_full_close(conn);
449 if (conn->destroy_cb)
450 conn->destroy_cb(conn);
451 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200452}
453
454
Willy Tarreau71681172017-10-23 14:39:06 +0200455/******************************************************/
456/* functions below are for the H2 protocol processing */
457/******************************************************/
458
459/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100460static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200461{
462 return h2s ? h2s->id : 0;
463}
464
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200465/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100466static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200467{
468 if (h2c->msi < 0)
469 return 0;
470
471 if (h2c->msi == h2s_id(h2s))
472 return 0;
473
474 return 1;
475}
476
Willy Tarreau741d6df2017-10-17 08:00:59 +0200477/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100478static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200479{
480 h2c->errcode = err;
481 h2c->st0 = H2_CS_ERROR;
482}
483
Willy Tarreau2e43f082017-10-17 08:03:59 +0200484/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100485static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200486{
487 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
488 h2s->errcode = err;
489 h2s->st = H2_SS_ERROR;
490 if (h2s->cs)
491 h2s->cs->flags |= CS_FL_ERROR;
492 }
493}
494
Willy Tarreaue4820742017-07-27 13:37:23 +0200495/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100496static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200497{
498 uint8_t *out = frame;
499
500 *out = len >> 16;
501 write_n16(out + 1, len);
502}
503
Willy Tarreau54c15062017-10-10 17:10:03 +0200504/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
505 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
506 * the caller's responsibility to verify that there are at least <bytes> bytes
507 * available in the buffer's input prior to calling this function.
508 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100509static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200510 const struct buffer *b, int o)
511{
512 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
513}
514
Willy Tarreau1f094672017-11-20 21:27:45 +0100515static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200516{
517 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
518}
519
Willy Tarreau1f094672017-11-20 21:27:45 +0100520static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200521{
522 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
523}
524
Willy Tarreau1f094672017-11-20 21:27:45 +0100525static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200526{
527 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
528}
529
530
Willy Tarreau715d5312017-07-11 15:20:24 +0200531/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
532 * is not obvious. It turns out that H2 headers are neither aligned nor do they
533 * use regular sizes. And to add to the trouble, the buffer may wrap so each
534 * byte read must be checked. The header is formed like this :
535 *
536 * b0 b1 b2 b3 b4 b5..b8
537 * +----------+---------+--------+----+----+----------------------+
538 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
539 * +----------+---------+--------+----+----+----------------------+
540 *
541 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
542 * we get the sid properly aligned and ordered, and 16 bits of len properly
543 * ordered as well. The type and flags can be extracted using bit shifts from
544 * the word, and only one extra read is needed to fetch len[16:23].
545 * Returns zero if some bytes are missing, otherwise non-zero on success.
546 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100547static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200548{
549 uint64_t w;
550
551 if (b->i < 9)
552 return 0;
553
554 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
555 h->len = *b->p << 16;
556 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
557 h->ff = w >> 32;
558 h->ft = w >> 40;
559 h->len += w >> 48;
560 return 1;
561}
562
563/* skip the next 9 bytes corresponding to the frame header possibly parsed by
564 * h2_peek_frame_hdr() above.
565 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100566static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200567{
568 bi_del(b, 9);
569}
570
571/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100572static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200573{
574 int ret;
575
576 ret = h2_peek_frame_hdr(b, h);
577 if (ret > 0)
578 h2_skip_frame_hdr(b);
579 return ret;
580}
581
Willy Tarreau00dd0782018-03-01 16:31:34 +0100582/* marks stream <h2s> as CLOSED and decrement the number of active streams for
583 * its connection if the stream was not yet closed. Please use this exclusively
584 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100585 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100586static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100587{
588 if (h2s->st != H2_SS_CLOSED)
589 h2s->h2c->nb_streams--;
590 h2s->st = H2_SS_CLOSED;
591}
592
Willy Tarreau0a10de62018-03-01 16:27:53 +0100593/* detaches an H2 stream from its H2C. */
594static void h2s_detach(struct h2s *h2s)
595{
596 h2s_close(h2s);
597 eb32_delete(&h2s->by_id);
598}
599
600/* releases an H2 stream back to the pool, and detaches it from the h2c. */
601static void h2s_free(struct h2s *h2s)
602{
603 pool_free(pool_head_h2s, h2s);
604}
605
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200606/* creates a new stream <id> on the h2c connection and returns it, or NULL in
607 * case of memory allocation error.
608 */
609static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
610{
611 struct conn_stream *cs;
612 struct h2s *h2s;
613
Willy Tarreaubafbe012017-11-24 17:34:44 +0100614 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200615 if (!h2s)
616 goto out;
617
618 h2s->h2c = h2c;
619 h2s->mws = h2c->miw;
620 h2s->flags = H2_SF_NONE;
621 h2s->errcode = H2_ERR_NO_ERROR;
622 h2s->st = H2_SS_IDLE;
623 h1m_init(&h2s->req);
624 h1m_init(&h2s->res);
625 h2s->by_id.key = h2s->id = id;
626 h2c->max_id = id;
627 LIST_INIT(&h2s->list);
628
629 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100630 h2c->nb_streams++;
631 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
632 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200633
634 cs = cs_new(h2c->conn);
635 if (!cs)
636 goto out_close;
637
638 h2s->cs = cs;
639 cs->ctx = h2s;
640
641 if (stream_create_from_cs(cs) < 0)
642 goto out_free_cs;
643
644 /* OK done, the stream lives its own life now */
645 return h2s;
646
647 out_free_cs:
648 cs_free(cs);
649 out_close:
Willy Tarreau0a10de62018-03-01 16:27:53 +0100650 h2s_detach(h2s);
651 h2s_free(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200652 h2s = NULL;
653 out:
654 return h2s;
655}
656
Willy Tarreaube5b7152017-09-25 16:25:39 +0200657/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
658 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
659 * the various settings codes.
660 */
661static int h2c_snd_settings(struct h2c *h2c)
662{
663 struct buffer *res;
664 char buf_data[100]; // enough for 15 settings
665 struct chunk buf;
666 int ret;
667
668 if (h2c_mux_busy(h2c, NULL)) {
669 h2c->flags |= H2_CF_DEM_MBUSY;
670 return 0;
671 }
672
Willy Tarreau44e973f2018-03-01 17:49:30 +0100673 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200674 if (!res) {
675 h2c->flags |= H2_CF_MUX_MALLOC;
676 h2c->flags |= H2_CF_DEM_MROOM;
677 return 0;
678 }
679
680 chunk_init(&buf, buf_data, sizeof(buf_data));
681 chunk_memcpy(&buf,
682 "\x00\x00\x00" /* length : 0 for now */
683 "\x04\x00" /* type : 4 (settings), flags : 0 */
684 "\x00\x00\x00\x00", /* stream ID : 0 */
685 9);
686
687 if (h2_settings_header_table_size != 4096) {
688 char str[6] = "\x00\x01"; /* header_table_size */
689
690 write_n32(str + 2, h2_settings_header_table_size);
691 chunk_memcat(&buf, str, 6);
692 }
693
694 if (h2_settings_initial_window_size != 65535) {
695 char str[6] = "\x00\x04"; /* initial_window_size */
696
697 write_n32(str + 2, h2_settings_initial_window_size);
698 chunk_memcat(&buf, str, 6);
699 }
700
701 if (h2_settings_max_concurrent_streams != 0) {
702 char str[6] = "\x00\x03"; /* max_concurrent_streams */
703
704 /* Note: 0 means "unlimited" for haproxy's config but not for
705 * the protocol, so never send this value!
706 */
707 write_n32(str + 2, h2_settings_max_concurrent_streams);
708 chunk_memcat(&buf, str, 6);
709 }
710
711 if (global.tune.bufsize != 16384) {
712 char str[6] = "\x00\x05"; /* max_frame_size */
713
714 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
715 * match bufsize - rewrite size, but at the moment it seems
716 * that clients don't take care of it.
717 */
718 write_n32(str + 2, global.tune.bufsize);
719 chunk_memcat(&buf, str, 6);
720 }
721
722 h2_set_frame_size(buf.str, buf.len - 9);
723 ret = bo_istput(res, ist2(buf.str, buf.len));
724 if (unlikely(ret <= 0)) {
725 if (!ret) {
726 h2c->flags |= H2_CF_MUX_MFULL;
727 h2c->flags |= H2_CF_DEM_MROOM;
728 return 0;
729 }
730 else {
731 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
732 return 0;
733 }
734 }
735 return ret;
736}
737
Willy Tarreau52eed752017-09-22 15:05:09 +0200738/* Try to receive a connection preface, then upon success try to send our
739 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
740 * missing data. It may return an error in h2c.
741 */
742static int h2c_frt_recv_preface(struct h2c *h2c)
743{
744 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200745 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200746
747 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
748
749 if (unlikely(ret1 <= 0)) {
750 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
751 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
752 return 0;
753 }
754
Willy Tarreaube5b7152017-09-25 16:25:39 +0200755 ret2 = h2c_snd_settings(h2c);
756 if (ret2 > 0)
757 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200758
Willy Tarreaube5b7152017-09-25 16:25:39 +0200759 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200760}
761
Willy Tarreau081d4722017-05-16 21:51:05 +0200762/* try to send a GOAWAY frame on the connection to report an error or a graceful
763 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
764 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
765 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
766 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
767 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
768 * on unrecoverable failure. It will not attempt to send one again in this last
769 * case so that it is safe to use h2c_error() to report such errors.
770 */
771static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
772{
773 struct buffer *res;
774 char str[17];
775 int ret;
776
777 if (h2c->flags & H2_CF_GOAWAY_FAILED)
778 return 1; // claim that it worked
779
780 if (h2c_mux_busy(h2c, h2s)) {
781 if (h2s)
782 h2s->flags |= H2_SF_BLK_MBUSY;
783 else
784 h2c->flags |= H2_CF_DEM_MBUSY;
785 return 0;
786 }
787
Willy Tarreau44e973f2018-03-01 17:49:30 +0100788 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200789 if (!res) {
790 h2c->flags |= H2_CF_MUX_MALLOC;
791 if (h2s)
792 h2s->flags |= H2_SF_BLK_MROOM;
793 else
794 h2c->flags |= H2_CF_DEM_MROOM;
795 return 0;
796 }
797
798 /* len: 8, type: 7, flags: none, sid: 0 */
799 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
800
801 if (h2c->last_sid < 0)
802 h2c->last_sid = h2c->max_id;
803
804 write_n32(str + 9, h2c->last_sid);
805 write_n32(str + 13, h2c->errcode);
806 ret = bo_istput(res, ist2(str, 17));
807 if (unlikely(ret <= 0)) {
808 if (!ret) {
809 h2c->flags |= H2_CF_MUX_MFULL;
810 if (h2s)
811 h2s->flags |= H2_SF_BLK_MROOM;
812 else
813 h2c->flags |= H2_CF_DEM_MROOM;
814 return 0;
815 }
816 else {
817 /* we cannot report this error using GOAWAY, so we mark
818 * it and claim a success.
819 */
820 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
821 h2c->flags |= H2_CF_GOAWAY_FAILED;
822 return 1;
823 }
824 }
825 h2c->flags |= H2_CF_GOAWAY_SENT;
826 return ret;
827}
828
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100829/* Try to send an RST_STREAM frame on the connection for the indicated stream
830 * during mux operations. This stream must be valid and cannot be closed
831 * already. h2s->id will be used for the stream ID and h2s->errcode will be
832 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
833 * not yet.
834 *
835 * Returns > 0 on success or zero if nothing was done. In case of lack of room
836 * to write the message, it subscribes the stream to future notifications.
837 */
838static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
839{
840 struct buffer *res;
841 char str[13];
842 int ret;
843
844 if (!h2s || h2s->st == H2_SS_CLOSED)
845 return 1;
846
847 if (h2c_mux_busy(h2c, h2s)) {
848 h2s->flags |= H2_SF_BLK_MBUSY;
849 return 0;
850 }
851
Willy Tarreau44e973f2018-03-01 17:49:30 +0100852 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100853 if (!res) {
854 h2c->flags |= H2_CF_MUX_MALLOC;
855 h2s->flags |= H2_SF_BLK_MROOM;
856 return 0;
857 }
858
859 /* len: 4, type: 3, flags: none */
860 memcpy(str, "\x00\x00\x04\x03\x00", 5);
861 write_n32(str + 5, h2s->id);
862 write_n32(str + 9, h2s->errcode);
863 ret = bo_istput(res, ist2(str, 13));
864
865 if (unlikely(ret <= 0)) {
866 if (!ret) {
867 h2c->flags |= H2_CF_MUX_MFULL;
868 h2s->flags |= H2_SF_BLK_MROOM;
869 return 0;
870 }
871 else {
872 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
873 return 0;
874 }
875 }
876
877 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100878 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100879 return ret;
880}
881
882/* Try to send an RST_STREAM frame on the connection for the stream being
883 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
884 * error code unless the stream's state already is IDLE or CLOSED in which
885 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
886 * it was not yet.
887 *
888 * Returns > 0 on success or zero if nothing was done. In case of lack of room
889 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200890 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100891 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200892 */
893static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
894{
895 struct buffer *res;
896 char str[13];
897 int ret;
898
899 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100900 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200901 return 0;
902 }
903
Willy Tarreau44e973f2018-03-01 17:49:30 +0100904 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200905 if (!res) {
906 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100907 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200908 return 0;
909 }
910
911 /* len: 4, type: 3, flags: none */
912 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100913
Willy Tarreau27a84c92017-10-17 08:10:17 +0200914 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100915 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200916 h2s->errcode : H2_ERR_STREAM_CLOSED);
917 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100918
Willy Tarreau27a84c92017-10-17 08:10:17 +0200919 if (unlikely(ret <= 0)) {
920 if (!ret) {
921 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100922 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200923 return 0;
924 }
925 else {
926 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
927 return 0;
928 }
929 }
930
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100931 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200932 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100933 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100934 }
935
Willy Tarreau27a84c92017-10-17 08:10:17 +0200936 return ret;
937}
938
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100939/* try to send an empty DATA frame with the ES flag set to notify about the
940 * end of stream and match a shutdown(write). If an ES was already sent as
941 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
942 * on success or zero if nothing was done. In case of lack of room to write the
943 * message, it subscribes the requesting stream to future notifications.
944 */
945static int h2_send_empty_data_es(struct h2s *h2s)
946{
947 struct h2c *h2c = h2s->h2c;
948 struct buffer *res;
949 char str[9];
950 int ret;
951
Willy Tarreau721c9742017-11-07 11:05:42 +0100952 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100953 return 1;
954
955 if (h2c_mux_busy(h2c, h2s)) {
956 h2s->flags |= H2_SF_BLK_MBUSY;
957 return 0;
958 }
959
Willy Tarreau44e973f2018-03-01 17:49:30 +0100960 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100961 if (!res) {
962 h2c->flags |= H2_CF_MUX_MALLOC;
963 h2s->flags |= H2_SF_BLK_MROOM;
964 return 0;
965 }
966
967 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
968 memcpy(str, "\x00\x00\x00\x00\x01", 5);
969 write_n32(str + 5, h2s->id);
970 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100971 if (likely(ret > 0)) {
972 h2s->flags |= H2_SF_ES_SENT;
973 }
974 else if (!ret) {
975 h2c->flags |= H2_CF_MUX_MFULL;
976 h2s->flags |= H2_SF_BLK_MROOM;
977 return 0;
978 }
979 else {
980 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
981 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100982 }
983 return ret;
984}
985
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100986/* wake the streams attached to the connection, whose id is greater than <last>,
987 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
988 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
989 * stream's state is automatically updated accordingly.
990 */
991static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
992{
993 struct eb32_node *node;
994 struct h2s *h2s;
995
996 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
997 flags |= CS_FL_ERROR;
998
999 if (conn_xprt_read0_pending(h2c->conn))
1000 flags |= CS_FL_EOS;
1001
1002 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1003 while (node) {
1004 h2s = container_of(node, struct h2s, by_id);
1005 if (h2s->id <= last)
1006 break;
1007 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001008
1009 if (!h2s->cs) {
1010 /* this stream was already orphaned */
Willy Tarreau0a10de62018-03-01 16:27:53 +01001011 h2s_detach(h2s);
1012 h2s_free(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001013 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001014 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001015
1016 h2s->cs->flags |= flags;
1017 /* recv is used to force to detect CS_FL_EOS that wake()
1018 * doesn't handle in the stream int code.
1019 */
1020 h2s->cs->data_cb->recv(h2s->cs);
1021 h2s->cs->data_cb->wake(h2s->cs);
1022
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001023 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1024 h2s->st = H2_SS_ERROR;
1025 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1026 h2s->st = H2_SS_HREM;
1027 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001028 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001029 }
1030}
1031
Willy Tarreau3421aba2017-07-27 15:41:03 +02001032/* Increase all streams' outgoing window size by the difference passed in
1033 * argument. This is needed upon receipt of the settings frame if the initial
1034 * window size is different. The difference may be negative and the resulting
1035 * window size as well, for the time it takes to receive some window updates.
1036 */
1037static void h2c_update_all_ws(struct h2c *h2c, int diff)
1038{
1039 struct h2s *h2s;
1040 struct eb32_node *node;
1041
1042 if (!diff)
1043 return;
1044
1045 node = eb32_first(&h2c->streams_by_id);
1046 while (node) {
1047 h2s = container_of(node, struct h2s, by_id);
1048 h2s->mws += diff;
1049 node = eb32_next(node);
1050 }
1051}
1052
1053/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1054 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1055 * return an error in h2c. Described in RFC7540#6.5.
1056 */
1057static int h2c_handle_settings(struct h2c *h2c)
1058{
1059 unsigned int offset;
1060 int error;
1061
1062 if (h2c->dff & H2_F_SETTINGS_ACK) {
1063 if (h2c->dfl) {
1064 error = H2_ERR_FRAME_SIZE_ERROR;
1065 goto fail;
1066 }
1067 return 1;
1068 }
1069
1070 if (h2c->dsi != 0) {
1071 error = H2_ERR_PROTOCOL_ERROR;
1072 goto fail;
1073 }
1074
1075 if (h2c->dfl % 6) {
1076 error = H2_ERR_FRAME_SIZE_ERROR;
1077 goto fail;
1078 }
1079
1080 /* that's the limit we can process */
1081 if (h2c->dfl > global.tune.bufsize) {
1082 error = H2_ERR_FRAME_SIZE_ERROR;
1083 goto fail;
1084 }
1085
1086 /* process full frame only */
1087 if (h2c->dbuf->i < h2c->dfl)
1088 return 0;
1089
1090 /* parse the frame */
1091 for (offset = 0; offset < h2c->dfl; offset += 6) {
1092 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1093 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1094
1095 switch (type) {
1096 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1097 /* we need to update all existing streams with the
1098 * difference from the previous iws.
1099 */
1100 if (arg < 0) { // RFC7540#6.5.2
1101 error = H2_ERR_FLOW_CONTROL_ERROR;
1102 goto fail;
1103 }
1104 h2c_update_all_ws(h2c, arg - h2c->miw);
1105 h2c->miw = arg;
1106 break;
1107 case H2_SETTINGS_MAX_FRAME_SIZE:
1108 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1109 error = H2_ERR_PROTOCOL_ERROR;
1110 goto fail;
1111 }
1112 h2c->mfs = arg;
1113 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001114 case H2_SETTINGS_ENABLE_PUSH:
1115 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1116 error = H2_ERR_PROTOCOL_ERROR;
1117 goto fail;
1118 }
1119 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001120 }
1121 }
1122
1123 /* need to ACK this frame now */
1124 h2c->st0 = H2_CS_FRAME_A;
1125 return 1;
1126 fail:
1127 h2c_error(h2c, error);
1128 return 0;
1129}
1130
1131/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1132 * success or one of the h2_status values.
1133 */
1134static int h2c_ack_settings(struct h2c *h2c)
1135{
1136 struct buffer *res;
1137 char str[9];
1138 int ret = -1;
1139
1140 if (h2c_mux_busy(h2c, NULL)) {
1141 h2c->flags |= H2_CF_DEM_MBUSY;
1142 return 0;
1143 }
1144
Willy Tarreau44e973f2018-03-01 17:49:30 +01001145 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001146 if (!res) {
1147 h2c->flags |= H2_CF_MUX_MALLOC;
1148 h2c->flags |= H2_CF_DEM_MROOM;
1149 return 0;
1150 }
1151
1152 memcpy(str,
1153 "\x00\x00\x00" /* length : 0 (no data) */
1154 "\x04" "\x01" /* type : 4, flags : ACK */
1155 "\x00\x00\x00\x00" /* stream ID */, 9);
1156
1157 ret = bo_istput(res, ist2(str, 9));
1158 if (unlikely(ret <= 0)) {
1159 if (!ret) {
1160 h2c->flags |= H2_CF_MUX_MFULL;
1161 h2c->flags |= H2_CF_DEM_MROOM;
1162 return 0;
1163 }
1164 else {
1165 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1166 return 0;
1167 }
1168 }
1169 return ret;
1170}
1171
Willy Tarreaucf68c782017-10-10 17:11:41 +02001172/* processes a PING frame and schedules an ACK if needed. The caller must pass
1173 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1174 * missing data. It may return an error in h2c.
1175 */
1176static int h2c_handle_ping(struct h2c *h2c)
1177{
1178 /* frame length must be exactly 8 */
1179 if (h2c->dfl != 8) {
1180 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1181 return 0;
1182 }
1183
1184 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001185 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001186 h2c->st0 = H2_CS_FRAME_A;
1187 return 1;
1188}
1189
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001190/* Try to send a window update for stream id <sid> and value <increment>.
1191 * Returns > 0 on success or zero on missing room or failure. It may return an
1192 * error in h2c.
1193 */
1194static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1195{
1196 struct buffer *res;
1197 char str[13];
1198 int ret = -1;
1199
1200 if (h2c_mux_busy(h2c, NULL)) {
1201 h2c->flags |= H2_CF_DEM_MBUSY;
1202 return 0;
1203 }
1204
Willy Tarreau44e973f2018-03-01 17:49:30 +01001205 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001206 if (!res) {
1207 h2c->flags |= H2_CF_MUX_MALLOC;
1208 h2c->flags |= H2_CF_DEM_MROOM;
1209 return 0;
1210 }
1211
1212 /* length: 4, type: 8, flags: none */
1213 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1214 write_n32(str + 5, sid);
1215 write_n32(str + 9, increment);
1216
1217 ret = bo_istput(res, ist2(str, 13));
1218
1219 if (unlikely(ret <= 0)) {
1220 if (!ret) {
1221 h2c->flags |= H2_CF_MUX_MFULL;
1222 h2c->flags |= H2_CF_DEM_MROOM;
1223 return 0;
1224 }
1225 else {
1226 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1227 return 0;
1228 }
1229 }
1230 return ret;
1231}
1232
1233/* try to send pending window update for the connection. It's safe to call it
1234 * with no pending updates. Returns > 0 on success or zero on missing room or
1235 * failure. It may return an error in h2c.
1236 */
1237static int h2c_send_conn_wu(struct h2c *h2c)
1238{
1239 int ret = 1;
1240
1241 if (h2c->rcvd_c <= 0)
1242 return 1;
1243
1244 /* send WU for the connection */
1245 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1246 if (ret > 0)
1247 h2c->rcvd_c = 0;
1248
1249 return ret;
1250}
1251
1252/* try to send pending window update for the current dmux stream. It's safe to
1253 * call it with no pending updates. Returns > 0 on success or zero on missing
1254 * room or failure. It may return an error in h2c.
1255 */
1256static int h2c_send_strm_wu(struct h2c *h2c)
1257{
1258 int ret = 1;
1259
1260 if (h2c->rcvd_s <= 0)
1261 return 1;
1262
1263 /* send WU for the stream */
1264 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1265 if (ret > 0)
1266 h2c->rcvd_s = 0;
1267
1268 return ret;
1269}
1270
Willy Tarreaucf68c782017-10-10 17:11:41 +02001271/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1272 * success, 0 on missing data or one of the h2_status values.
1273 */
1274static int h2c_ack_ping(struct h2c *h2c)
1275{
1276 struct buffer *res;
1277 char str[17];
1278 int ret = -1;
1279
1280 if (h2c->dbuf->i < 8)
1281 return 0;
1282
1283 if (h2c_mux_busy(h2c, NULL)) {
1284 h2c->flags |= H2_CF_DEM_MBUSY;
1285 return 0;
1286 }
1287
Willy Tarreau44e973f2018-03-01 17:49:30 +01001288 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001289 if (!res) {
1290 h2c->flags |= H2_CF_MUX_MALLOC;
1291 h2c->flags |= H2_CF_DEM_MROOM;
1292 return 0;
1293 }
1294
1295 memcpy(str,
1296 "\x00\x00\x08" /* length : 8 (same payload) */
1297 "\x06" "\x01" /* type : 6, flags : ACK */
1298 "\x00\x00\x00\x00" /* stream ID */, 9);
1299
1300 /* copy the original payload */
1301 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1302
1303 ret = bo_istput(res, ist2(str, 17));
1304 if (unlikely(ret <= 0)) {
1305 if (!ret) {
1306 h2c->flags |= H2_CF_MUX_MFULL;
1307 h2c->flags |= H2_CF_DEM_MROOM;
1308 return 0;
1309 }
1310 else {
1311 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1312 return 0;
1313 }
1314 }
1315 return ret;
1316}
1317
Willy Tarreau26f95952017-07-27 17:18:30 +02001318/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1319 * Returns > 0 on success or zero on missing data. It may return an error in
1320 * h2c or h2s. Described in RFC7540#6.9.
1321 */
1322static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1323{
1324 int32_t inc;
1325 int error;
1326
1327 if (h2c->dfl != 4) {
1328 error = H2_ERR_FRAME_SIZE_ERROR;
1329 goto conn_err;
1330 }
1331
1332 /* process full frame only */
1333 if (h2c->dbuf->i < h2c->dfl)
1334 return 0;
1335
1336 inc = h2_get_n32(h2c->dbuf, 0);
1337
1338 if (h2c->dsi != 0) {
1339 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001340
1341 /* it's not an error to receive WU on a closed stream */
1342 if (h2s->st == H2_SS_CLOSED)
1343 return 1;
1344
1345 if (!inc) {
1346 error = H2_ERR_PROTOCOL_ERROR;
1347 goto strm_err;
1348 }
1349
1350 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1351 error = H2_ERR_FLOW_CONTROL_ERROR;
1352 goto strm_err;
1353 }
1354
1355 h2s->mws += inc;
1356 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1357 h2s->flags &= ~H2_SF_BLK_SFCTL;
1358 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1359 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1360 /* This stream wanted to send but could not due to its
1361 * own flow control. We can put it back into the send
1362 * list now, it will be handled upon next send() call.
1363 */
1364 LIST_ADDQ(&h2c->send_list, &h2s->list);
1365 }
1366 }
1367 }
1368 else {
1369 /* connection window update */
1370 if (!inc) {
1371 error = H2_ERR_PROTOCOL_ERROR;
1372 goto conn_err;
1373 }
1374
1375 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1376 error = H2_ERR_FLOW_CONTROL_ERROR;
1377 goto conn_err;
1378 }
1379
1380 h2c->mws += inc;
1381 }
1382
1383 return 1;
1384
1385 conn_err:
1386 h2c_error(h2c, error);
1387 return 0;
1388
1389 strm_err:
1390 if (h2s) {
1391 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001392 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001393 }
1394 else
1395 h2c_error(h2c, error);
1396 return 0;
1397}
1398
Willy Tarreaue96b0922017-10-30 00:28:29 +01001399/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1400 * the last ID. Returns > 0 on success or zero on missing data. It may return
1401 * an error in h2c. Described in RFC7540#6.8.
1402 */
1403static int h2c_handle_goaway(struct h2c *h2c)
1404{
1405 int error;
1406 int last;
1407
1408 if (h2c->dsi != 0) {
1409 error = H2_ERR_PROTOCOL_ERROR;
1410 goto conn_err;
1411 }
1412
1413 if (h2c->dfl < 8) {
1414 error = H2_ERR_FRAME_SIZE_ERROR;
1415 goto conn_err;
1416 }
1417
1418 /* process full frame only */
1419 if (h2c->dbuf->i < h2c->dfl)
1420 return 0;
1421
1422 last = h2_get_n32(h2c->dbuf, 0);
1423 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1424 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001425 if (h2c->last_sid < 0)
1426 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001427 return 1;
1428
1429 conn_err:
1430 h2c_error(h2c, error);
1431 return 0;
1432}
1433
Willy Tarreau92153fc2017-12-03 19:46:19 +01001434/* processes a PRIORITY frame, and either skips it or rejects if it is
1435 * invalid. Returns > 0 on success or zero on missing data. It may return
1436 * an error in h2c. Described in RFC7540#6.3.
1437 */
1438static int h2c_handle_priority(struct h2c *h2c)
1439{
1440 int error;
1441
1442 if (h2c->dsi == 0) {
1443 error = H2_ERR_PROTOCOL_ERROR;
1444 goto conn_err;
1445 }
1446
1447 if (h2c->dfl != 5) {
1448 error = H2_ERR_FRAME_SIZE_ERROR;
1449 goto conn_err;
1450 }
1451
1452 /* process full frame only */
1453 if (h2c->dbuf->i < h2c->dfl)
1454 return 0;
1455
1456 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1457 /* 7540#5.3 : can't depend on itself */
1458 error = H2_ERR_PROTOCOL_ERROR;
1459 goto conn_err;
1460 }
1461 return 1;
1462
1463 conn_err:
1464 h2c_error(h2c, error);
1465 return 0;
1466}
1467
Willy Tarreaucd234e92017-08-18 10:59:39 +02001468/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1469 * Returns > 0 on success or zero on missing data. It may return an error in
1470 * h2c. Described in RFC7540#6.4.
1471 */
1472static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1473{
1474 int error;
1475
1476 if (h2c->dsi == 0) {
1477 error = H2_ERR_PROTOCOL_ERROR;
1478 goto conn_err;
1479 }
1480
Willy Tarreaucd234e92017-08-18 10:59:39 +02001481 if (h2c->dfl != 4) {
1482 error = H2_ERR_FRAME_SIZE_ERROR;
1483 goto conn_err;
1484 }
1485
1486 /* process full frame only */
1487 if (h2c->dbuf->i < h2c->dfl)
1488 return 0;
1489
1490 /* late RST, already handled */
1491 if (h2s->st == H2_SS_CLOSED)
1492 return 1;
1493
1494 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001495 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001496
1497 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001498 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001499 /* recv is used to force to detect CS_FL_EOS that wake()
1500 * doesn't handle in the stream-int code.
1501 */
1502 h2s->cs->data_cb->recv(h2s->cs);
1503 h2s->cs->data_cb->wake(h2s->cs);
1504 }
1505
1506 h2s->flags |= H2_SF_RST_RCVD;
1507 return 1;
1508
1509 conn_err:
1510 h2c_error(h2c, error);
1511 return 0;
1512}
1513
Willy Tarreau13278b42017-10-13 19:23:14 +02001514/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1515 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1516 * errors here are reported as connection errors since it's impossible to
1517 * recover from such errors after the compression context has been altered.
1518 */
1519static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1520{
1521 int error;
1522
1523 if (!h2c->dfl) {
1524 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1525 goto strm_err;
1526 }
1527
1528 if (!h2c->dbuf->size)
1529 return 0; // empty buffer
1530
1531 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1532 return 0; // incomplete frame
1533
1534 /* now either the frame is complete or the buffer is complete */
1535 if (h2s->st != H2_SS_IDLE) {
1536 /* FIXME: stream already exists, this is only allowed for
1537 * trailers (not supported for now).
1538 */
1539 error = H2_ERR_PROTOCOL_ERROR;
1540 goto conn_err;
1541 }
1542 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1543 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1544 error = H2_ERR_PROTOCOL_ERROR;
1545 goto conn_err;
1546 }
1547
1548 h2s = h2c_stream_new(h2c, h2c->dsi);
1549 if (!h2s) {
1550 error = H2_ERR_INTERNAL_ERROR;
1551 goto conn_err;
1552 }
1553
1554 h2s->st = H2_SS_OPEN;
1555 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1556 h2s->st = H2_SS_HREM;
1557 h2s->flags |= H2_SF_ES_RCVD;
1558 }
1559
1560 /* call the upper layers to process the frame, then let the upper layer
1561 * notify the stream about any change.
1562 */
1563 h2s->cs->data_cb->recv(h2s->cs);
1564
1565 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1566 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1567 error = H2_ERR_INTERNAL_ERROR;
1568 goto conn_err;
1569 }
1570
Willy Tarreau8f650c32017-11-21 19:36:21 +01001571 if (h2c->st0 >= H2_CS_ERROR)
1572 return 0;
1573
Willy Tarreau721c9742017-11-07 11:05:42 +01001574 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001575 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001576 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001577 }
1578 else {
1579 /* update the max stream ID if the request is being processed */
1580 if (h2s->id > h2c->max_id)
1581 h2c->max_id = h2s->id;
1582 }
1583
1584 return 1;
1585
1586 conn_err:
1587 h2c_error(h2c, error);
1588 return 0;
1589
1590 strm_err:
1591 if (h2s) {
1592 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001593 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001594 }
1595 else
1596 h2c_error(h2c, error);
1597 return 0;
1598}
1599
Willy Tarreau454f9052017-10-26 19:40:35 +02001600/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1601 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1602 */
1603static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1604{
1605 int error;
1606
1607 /* note that empty DATA frames are perfectly valid and sometimes used
1608 * to signal an end of stream (with the ES flag).
1609 */
1610
1611 if (!h2c->dbuf->size && h2c->dfl)
1612 return 0; // empty buffer
1613
1614 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1615 return 0; // incomplete frame
1616
1617 /* now either the frame is complete or the buffer is complete */
1618
1619 if (!h2c->dsi) {
1620 /* RFC7540#6.1 */
1621 error = H2_ERR_PROTOCOL_ERROR;
1622 goto conn_err;
1623 }
1624
1625 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1626 /* RFC7540#6.1 */
1627 error = H2_ERR_STREAM_CLOSED;
1628 goto strm_err;
1629 }
1630
Willy Tarreau454f9052017-10-26 19:40:35 +02001631 /* call the upper layers to process the frame, then let the upper layer
1632 * notify the stream about any change.
1633 */
1634 if (!h2s->cs) {
1635 error = H2_ERR_STREAM_CLOSED;
1636 goto strm_err;
1637 }
1638
1639 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001640
Willy Tarreau454f9052017-10-26 19:40:35 +02001641 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1642 /* cs has just been destroyed, we have to kill h2s. */
1643 error = H2_ERR_STREAM_CLOSED;
1644 goto strm_err;
1645 }
1646
Willy Tarreau8f650c32017-11-21 19:36:21 +01001647 if (h2c->st0 >= H2_CS_ERROR)
1648 return 0;
1649
Willy Tarreau721c9742017-11-07 11:05:42 +01001650 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001651 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001652 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001653 }
1654
1655 /* check for completion : the callee will change this to FRAME_A or
1656 * FRAME_H once done.
1657 */
1658 if (h2c->st0 == H2_CS_FRAME_P)
1659 return 0;
1660
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001661
1662 /* last frame */
1663 if (h2c->dff & H2_F_DATA_END_STREAM) {
1664 h2s->st = H2_SS_HREM;
1665 h2s->flags |= H2_SF_ES_RCVD;
1666 }
1667
Willy Tarreau454f9052017-10-26 19:40:35 +02001668 return 1;
1669
1670 conn_err:
1671 h2c_error(h2c, error);
1672 return 0;
1673
1674 strm_err:
1675 if (h2s) {
1676 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001677 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001678 }
1679 else
1680 h2c_error(h2c, error);
1681 return 0;
1682}
1683
Willy Tarreaubc933932017-10-09 16:21:43 +02001684/* process Rx frames to be demultiplexed */
1685static void h2_process_demux(struct h2c *h2c)
1686{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001687 struct h2s *h2s;
1688
Willy Tarreau081d4722017-05-16 21:51:05 +02001689 if (h2c->st0 >= H2_CS_ERROR)
1690 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001691
1692 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1693 if (h2c->st0 == H2_CS_PREFACE) {
1694 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1695 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1696 if (h2c->st0 == H2_CS_ERROR)
1697 h2c->st0 = H2_CS_ERROR2;
1698 goto fail;
1699 }
1700
1701 h2c->max_id = 0;
1702 h2c->st0 = H2_CS_SETTINGS1;
1703 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001704
1705 if (h2c->st0 == H2_CS_SETTINGS1) {
1706 struct h2_fh hdr;
1707
1708 /* ensure that what is pending is a valid SETTINGS frame
1709 * without an ACK.
1710 */
1711 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1712 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1713 if (h2c->st0 == H2_CS_ERROR)
1714 h2c->st0 = H2_CS_ERROR2;
1715 goto fail;
1716 }
1717
1718 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1719 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1720 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1721 h2c->st0 = H2_CS_ERROR2;
1722 goto fail;
1723 }
1724
1725 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1726 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1727 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1728 h2c->st0 = H2_CS_ERROR2;
1729 goto fail;
1730 }
1731
1732 /* that's OK, switch to FRAME_P to process it */
1733 h2c->dfl = hdr.len;
1734 h2c->dsi = hdr.sid;
1735 h2c->dft = hdr.ft;
1736 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001737 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001738 h2c->st0 = H2_CS_FRAME_P;
1739 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001740 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001741
1742 /* process as many incoming frames as possible below */
1743 while (h2c->dbuf->i) {
1744 int ret = 0;
1745
1746 if (h2c->st0 >= H2_CS_ERROR)
1747 break;
1748
1749 if (h2c->st0 == H2_CS_FRAME_H) {
1750 struct h2_fh hdr;
1751
1752 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1753 break;
1754
1755 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1756 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1757 h2c->st0 = H2_CS_ERROR;
1758 break;
1759 }
1760
1761 h2c->dfl = hdr.len;
1762 h2c->dsi = hdr.sid;
1763 h2c->dft = hdr.ft;
1764 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001765 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001766 h2c->st0 = H2_CS_FRAME_P;
1767 h2_skip_frame_hdr(h2c->dbuf);
1768 }
1769
1770 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001771 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001772
Willy Tarreaud7901432017-12-29 11:34:40 +01001773 if (h2c->st0 == H2_CS_FRAME_E)
1774 goto strm_err;
1775
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001776 if (h2s->st == H2_SS_IDLE &&
1777 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1778 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1779 * this state MUST be treated as a connection error
1780 */
1781 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1782 h2c->st0 = H2_CS_ERROR;
1783 break;
1784 }
1785
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001786 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1787 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1788 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1789 * this state MUST be treated as a stream error
1790 */
1791 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001792 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001793 goto strm_err;
1794 }
1795
Willy Tarreauab837502017-12-27 15:07:30 +01001796 /* Below the management of frames received in closed state is a
1797 * bit hackish because the spec makes strong differences between
1798 * streams closed by receiving RST, sending RST, and seeing ES
1799 * in both directions. In addition to this, the creation of a
1800 * new stream reusing the identifier of a closed one will be
1801 * detected here. Given that we cannot keep track of all closed
1802 * streams forever, we consider that unknown closed streams were
1803 * closed on RST received, which allows us to respond with an
1804 * RST without breaking the connection (eg: to abort a transfer).
1805 * Some frames have to be silently ignored as well.
1806 */
1807 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1808 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1809 /* #5.1.1: The identifier of a newly
1810 * established stream MUST be numerically
1811 * greater than all streams that the initiating
1812 * endpoint has opened or reserved. This
1813 * governs streams that are opened using a
1814 * HEADERS frame and streams that are reserved
1815 * using PUSH_PROMISE. An endpoint that
1816 * receives an unexpected stream identifier
1817 * MUST respond with a connection error.
1818 */
1819 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1820 goto strm_err;
1821 }
1822
1823 if (h2s->flags & H2_SF_RST_RCVD) {
1824 /* RFC7540#5.1:closed: an endpoint that
1825 * receives any frame other than PRIORITY after
1826 * receiving a RST_STREAM MUST treat that as a
1827 * stream error of type STREAM_CLOSED.
1828 *
1829 * Note that old streams fall into this category
1830 * and will lead to an RST being sent.
1831 */
1832 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1833 h2c->st0 = H2_CS_FRAME_E;
1834 goto strm_err;
1835 }
1836
1837 /* RFC7540#5.1:closed: if this state is reached as a
1838 * result of sending a RST_STREAM frame, the peer that
1839 * receives the RST_STREAM might have already sent
1840 * frames on the stream that cannot be withdrawn. An
1841 * endpoint MUST ignore frames that it receives on
1842 * closed streams after it has sent a RST_STREAM
1843 * frame. An endpoint MAY choose to limit the period
1844 * over which it ignores frames and treat frames that
1845 * arrive after this time as being in error.
1846 */
1847 if (!(h2s->flags & H2_SF_RST_SENT)) {
1848 /* RFC7540#5.1:closed: any frame other than
1849 * PRIO/WU/RST in this state MUST be treated as
1850 * a connection error
1851 */
1852 if (h2c->dft != H2_FT_RST_STREAM &&
1853 h2c->dft != H2_FT_PRIORITY &&
1854 h2c->dft != H2_FT_WINDOW_UPDATE) {
1855 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1856 goto strm_err;
1857 }
1858 }
1859 }
1860
Willy Tarreauc0da1962017-10-30 18:38:00 +01001861#if 0
1862 // problem below: it is not possible to completely ignore such
1863 // streams as we need to maintain the compression state as well
1864 // and for this we need to completely process these frames (eg:
1865 // HEADERS frames) as well as counting DATA frames to emit
1866 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1867 // This is a typical case of layer violation where the
1868 // transported contents are critical to the connection's
1869 // validity and must be ignored at the same time :-(
1870
1871 /* graceful shutdown, ignore streams whose ID is higher than
1872 * the one advertised in GOAWAY. RFC7540#6.8.
1873 */
1874 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1875 ret = MIN(h2c->dbuf->i, h2c->dfl);
1876 bi_del(h2c->dbuf, ret);
1877 h2c->dfl -= ret;
1878 ret = h2c->dfl == 0;
1879 goto strm_err;
1880 }
1881#endif
1882
Willy Tarreau7e98c052017-10-10 15:56:59 +02001883 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001884 case H2_FT_SETTINGS:
1885 if (h2c->st0 == H2_CS_FRAME_P)
1886 ret = h2c_handle_settings(h2c);
1887
1888 if (h2c->st0 == H2_CS_FRAME_A)
1889 ret = h2c_ack_settings(h2c);
1890 break;
1891
Willy Tarreaucf68c782017-10-10 17:11:41 +02001892 case H2_FT_PING:
1893 if (h2c->st0 == H2_CS_FRAME_P)
1894 ret = h2c_handle_ping(h2c);
1895
1896 if (h2c->st0 == H2_CS_FRAME_A)
1897 ret = h2c_ack_ping(h2c);
1898 break;
1899
Willy Tarreau26f95952017-07-27 17:18:30 +02001900 case H2_FT_WINDOW_UPDATE:
1901 if (h2c->st0 == H2_CS_FRAME_P)
1902 ret = h2c_handle_window_update(h2c, h2s);
1903 break;
1904
Willy Tarreau61290ec2017-10-17 08:19:21 +02001905 case H2_FT_CONTINUATION:
1906 /* we currently don't support CONTINUATION frames since
1907 * we have nowhere to store the partial HEADERS frame.
1908 * Let's abort the stream on an INTERNAL_ERROR here.
1909 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001910 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001911 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001912 h2c->st0 = H2_CS_FRAME_E;
1913 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001914 break;
1915
Willy Tarreau13278b42017-10-13 19:23:14 +02001916 case H2_FT_HEADERS:
1917 if (h2c->st0 == H2_CS_FRAME_P)
1918 ret = h2c_frt_handle_headers(h2c, h2s);
1919 break;
1920
Willy Tarreau454f9052017-10-26 19:40:35 +02001921 case H2_FT_DATA:
1922 if (h2c->st0 == H2_CS_FRAME_P)
1923 ret = h2c_frt_handle_data(h2c, h2s);
1924
1925 if (h2c->st0 == H2_CS_FRAME_A)
1926 ret = h2c_send_strm_wu(h2c);
1927 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001928
Willy Tarreau92153fc2017-12-03 19:46:19 +01001929 case H2_FT_PRIORITY:
1930 if (h2c->st0 == H2_CS_FRAME_P)
1931 ret = h2c_handle_priority(h2c);
1932 break;
1933
Willy Tarreaucd234e92017-08-18 10:59:39 +02001934 case H2_FT_RST_STREAM:
1935 if (h2c->st0 == H2_CS_FRAME_P)
1936 ret = h2c_handle_rst_stream(h2c, h2s);
1937 break;
1938
Willy Tarreaue96b0922017-10-30 00:28:29 +01001939 case H2_FT_GOAWAY:
1940 if (h2c->st0 == H2_CS_FRAME_P)
1941 ret = h2c_handle_goaway(h2c);
1942 break;
1943
Willy Tarreau1c661982017-10-30 13:52:01 +01001944 case H2_FT_PUSH_PROMISE:
1945 /* not permitted here, RFC7540#5.1 */
1946 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001947 break;
1948
1949 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001950 default:
1951 /* drop frames that we ignore. They may be larger than
1952 * the buffer so we drain all of their contents until
1953 * we reach the end.
1954 */
1955 ret = MIN(h2c->dbuf->i, h2c->dfl);
1956 bi_del(h2c->dbuf, ret);
1957 h2c->dfl -= ret;
1958 ret = h2c->dfl == 0;
1959 }
1960
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001961 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01001962 /* We may have to send an RST if not done yet */
1963 if (h2s->st == H2_SS_ERROR)
1964 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001965
Willy Tarreaua20a5192017-12-27 11:02:06 +01001966 if (h2c->st0 == H2_CS_FRAME_E)
1967 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001968
Willy Tarreau7e98c052017-10-10 15:56:59 +02001969 /* error or missing data condition met above ? */
1970 if (ret <= 0)
1971 break;
1972
1973 if (h2c->st0 != H2_CS_FRAME_H) {
1974 bi_del(h2c->dbuf, h2c->dfl);
1975 h2c->st0 = H2_CS_FRAME_H;
1976 }
1977 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001978
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001979 if (h2c->rcvd_c > 0 &&
1980 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1981 h2c_send_conn_wu(h2c);
1982
Willy Tarreau52eed752017-09-22 15:05:09 +02001983 fail:
1984 /* we can go here on missing data, blocked response or error */
1985 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001986}
1987
1988/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1989 * the end.
1990 */
1991static int h2_process_mux(struct h2c *h2c)
1992{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001993 struct h2s *h2s, *h2s_back;
1994
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001995 /* start by sending possibly pending window updates */
1996 if (h2c->rcvd_c > 0 &&
1997 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
1998 h2c_send_conn_wu(h2c) < 0)
1999 goto fail;
2000
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002001 /* First we always process the flow control list because the streams
2002 * waiting there were already elected for immediate emission but were
2003 * blocked just on this.
2004 */
2005
2006 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2007 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2008 h2c->st0 >= H2_CS_ERROR)
2009 break;
2010
2011 /* In theory it's possible that h2s->cs == NULL here :
2012 * - client sends crap that causes a parse error
2013 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2014 * - RST_STREAM cannot be emitted because mux is busy/full
2015 * - stream gets notified, detaches and quits
2016 * - mux buffer gets ready and wakes pending streams up
2017 * - bam!
2018 */
2019 h2s->flags &= ~H2_SF_BLK_ANY;
2020
2021 if (h2s->cs) {
2022 h2s->cs->data_cb->send(h2s->cs);
2023 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002024 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002025 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002026 }
2027
2028 /* depending on callee's blocking reasons, we may queue in send
2029 * list or completely dequeue.
2030 */
2031 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2032 if (h2s->flags & H2_SF_BLK_ANY) {
2033 LIST_DEL(&h2s->list);
2034 LIST_ADDQ(&h2c->send_list, &h2s->list);
2035 }
2036 else {
2037 LIST_DEL(&h2s->list);
2038 LIST_INIT(&h2s->list);
2039 if (h2s->cs)
2040 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002041 else {
2042 /* just sent the last frame for this orphaned stream */
Willy Tarreau0a10de62018-03-01 16:27:53 +01002043 h2s_detach(h2s);
2044 h2s_free(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002045 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002046 }
2047 }
2048 }
2049
2050 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2051 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2052 break;
2053
2054 /* In theory it's possible that h2s->cs == NULL here :
2055 * - client sends crap that causes a parse error
2056 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2057 * - RST_STREAM cannot be emitted because mux is busy/full
2058 * - stream gets notified, detaches and quits
2059 * - mux buffer gets ready and wakes pending streams up
2060 * - bam!
2061 */
2062 h2s->flags &= ~H2_SF_BLK_ANY;
2063
2064 if (h2s->cs) {
2065 h2s->cs->data_cb->send(h2s->cs);
2066 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002067 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002068 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002069 }
2070 /* depending on callee's blocking reasons, we may queue in fctl
2071 * list or completely dequeue.
2072 */
2073 if (h2s->flags & H2_SF_BLK_MFCTL) {
2074 /* stream hit the connection's flow control */
2075 LIST_DEL(&h2s->list);
2076 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2077 }
2078 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2079 LIST_DEL(&h2s->list);
2080 LIST_INIT(&h2s->list);
2081 if (h2s->cs)
2082 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002083 else {
2084 /* just sent the last frame for this orphaned stream */
Willy Tarreau0a10de62018-03-01 16:27:53 +01002085 h2s_detach(h2s);
2086 h2s_free(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002087 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002088 }
2089 }
2090
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002091 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002092 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002093 if (h2c->st0 == H2_CS_ERROR) {
2094 if (h2c->max_id >= 0) {
2095 h2c_send_goaway_error(h2c, NULL);
2096 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2097 return 0;
2098 }
2099
2100 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2101 }
2102 return 1;
2103 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002104 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002105}
2106
Willy Tarreau71681172017-10-23 14:39:06 +02002107
Willy Tarreau62f52692017-10-08 23:01:42 +02002108/*********************************************************/
2109/* functions below are I/O callbacks from the connection */
2110/*********************************************************/
2111
2112/* callback called on recv event by the connection handler */
2113static void h2_recv(struct connection *conn)
2114{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002115 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002116 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002117 int max;
2118
Willy Tarreau315d8072017-12-10 22:17:57 +01002119 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002120 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002121
Willy Tarreau44e973f2018-03-01 17:49:30 +01002122 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002123 if (!buf) {
2124 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002125 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002126 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002127
Willy Tarreaua2af5122017-10-09 11:56:46 +02002128 /* note: buf->o == 0 */
2129 max = buf->size - buf->i;
Willy Tarreau315d8072017-12-10 22:17:57 +01002130 if (max)
2131 conn->xprt->rcv_buf(conn, buf, max);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002132
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002133 if (!buf->i) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002134 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002135 return;
2136 }
2137
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002138 if (buf->i == buf->size)
2139 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002140 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002141}
2142
2143/* callback called on send event by the connection handler */
2144static void h2_send(struct connection *conn)
2145{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002146 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002147 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002148
2149 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002150 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002151
2152 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2153 /* a handshake was requested */
2154 return;
2155 }
2156
Willy Tarreaubc933932017-10-09 16:21:43 +02002157 /* This loop is quite simple : it tries to fill as much as it can from
2158 * pending streams into the existing buffer until it's reportedly full
2159 * or the end of send requests is reached. Then it tries to send this
2160 * buffer's contents out, marks it not full if at least one byte could
2161 * be sent, and tries again.
2162 *
2163 * The snd_buf() function normally takes a "flags" argument which may
2164 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2165 * data immediately comes and CO_SFL_STREAMER to indicate that the
2166 * connection is streaming lots of data (used to increase TLS record
2167 * size at the expense of latency). The former can be sent any time
2168 * there's a buffer full flag, as it indicates at least one stream
2169 * attempted to send and failed so there are pending data. An
2170 * alternative would be to set it as long as there's an active stream
2171 * but that would be problematic for ACKs until we have an absolute
2172 * guarantee that all waiters have at least one byte to send. The
2173 * latter should possibly not be set for now.
2174 */
2175
2176 done = 0;
2177 while (!done) {
2178 unsigned int flags = 0;
2179
2180 /* fill as much as we can into the current buffer */
2181 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2182 done = h2_process_mux(h2c);
2183
2184 if (conn->flags & CO_FL_ERROR)
2185 break;
2186
2187 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2188 flags |= CO_SFL_MSG_MORE;
2189
Willy Tarreau319994a2017-11-07 11:03:56 +01002190 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002191 break;
2192
2193 /* wrote at least one byte, the buffer is not full anymore */
2194 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2195 }
2196
Willy Tarreaua2af5122017-10-09 11:56:46 +02002197 if (conn->flags & CO_FL_SOCK_WR_SH) {
2198 /* output closed, nothing to send, clear the buffer to release it */
2199 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002200 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002201}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002202
Willy Tarreau62f52692017-10-08 23:01:42 +02002203/* callback called on any event by the connection handler.
2204 * It applies changes and returns zero, or < 0 if it wants immediate
2205 * destruction of the connection (which normally doesn not happen in h2).
2206 */
2207static int h2_wake(struct connection *conn)
2208{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002209 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002210 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002211
Willy Tarreaud13bf272017-12-14 10:34:52 +01002212 if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
2213 h2_process_demux(h2c);
2214
2215 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
2216 h2c->dbuf->i = 0;
2217
2218 if (h2c->dbuf->i != h2c->dbuf->size)
2219 h2c->flags &= ~H2_CF_DEM_DFULL;
2220 }
2221
Willy Tarreau8ec14062017-12-30 18:08:13 +01002222 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2223 /* frontend is stopping, reload likely in progress, let's try
2224 * to announce a graceful shutdown if not yet done. We don't
2225 * care if it fails, it will be tried again later.
2226 */
2227 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2228 if (h2c->last_sid < 0)
2229 h2c->last_sid = (1U << 31) - 1;
2230 h2c_send_goaway_error(h2c, NULL);
2231 }
2232 }
2233
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002234 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002235 * If we received early data, and the handshake is done, wake
2236 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002237 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002238 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2239 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2240 struct eb32_node *node;
2241 struct h2s *h2s;
2242
2243 h2c->flags |= H2_CF_WAIT_FOR_HS;
2244 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2245
2246 while (node) {
2247 h2s = container_of(node, struct h2s, by_id);
2248 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2249 h2s->cs->data_cb->wake(h2s->cs);
2250 node = eb32_next(node);
2251 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002252 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002253
Willy Tarreau26bd7612017-10-09 16:47:04 +02002254 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002255 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2256 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2257 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002258 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002259
2260 if (eb_is_empty(&h2c->streams_by_id)) {
2261 /* no more stream, kill the connection now */
2262 h2_release(conn);
2263 return -1;
2264 }
2265 else {
2266 /* some streams still there, we need to signal them all and
2267 * wait for their departure.
2268 */
2269 __conn_xprt_stop_recv(conn);
2270 __conn_xprt_stop_send(conn);
2271 return 0;
2272 }
2273 }
2274
2275 if (!h2c->dbuf->i)
Willy Tarreau44e973f2018-03-01 17:49:30 +01002276 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002277
2278 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002279 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002280 __conn_xprt_stop_recv(conn);
2281 }
2282 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002283 __conn_xprt_want_recv(conn);
2284 }
2285
2286 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002287 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2288 (h2c->st0 == H2_CS_ERROR ||
2289 h2c->mbuf->o ||
2290 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2291 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002292 __conn_xprt_want_send(conn);
2293 }
2294 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002295 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002296 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002297 }
2298
Willy Tarreau3f133572017-10-31 19:21:06 +01002299 if (h2c->task) {
Willy Tarreau84b118f2018-03-05 16:10:54 +01002300 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002301 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002302 task_queue(h2c->task);
2303 }
2304 else
2305 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002306 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002307 return 0;
2308}
2309
Willy Tarreauea392822017-10-31 10:02:25 +01002310/* Connection timeout management. The principle is that if there's no receipt
2311 * nor sending for a certain amount of time, the connection is closed. If the
2312 * MUX buffer still has lying data or is not allocatable, the connection is
2313 * immediately killed. If it's allocatable and empty, we attempt to send a
2314 * GOAWAY frame.
2315 */
2316static struct task *h2_timeout_task(struct task *t)
2317{
2318 struct h2c *h2c = t->context;
2319 int expired = tick_is_expired(t->expire, now_ms);
2320
2321 if (!expired)
2322 return t;
2323
2324 h2c_error(h2c, H2_ERR_NO_ERROR);
2325 h2_wake_some_streams(h2c, 0, 0);
2326
2327 if (h2c->mbuf->o) {
2328 /* don't even try to send a GOAWAY, the buffer is stuck */
2329 h2c->flags |= H2_CF_GOAWAY_FAILED;
2330 }
2331
2332 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002333 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002334 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2335 h2c->flags |= H2_CF_GOAWAY_FAILED;
2336
2337 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2338 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2339
2340 if (!eb_is_empty(&h2c->streams_by_id))
2341 goto wait;
2342
2343 h2_release(h2c->conn);
2344 return NULL;
2345
2346 wait:
2347 /* the streams have been notified, we must let them finish and close */
2348 h2c->task = NULL;
2349 task_delete(t);
2350 task_free(t);
2351 return NULL;
2352}
2353
2354
Willy Tarreau62f52692017-10-08 23:01:42 +02002355/*******************************************/
2356/* functions below are used by the streams */
2357/*******************************************/
2358
2359/*
2360 * Attach a new stream to a connection
2361 * (Used for outgoing connections)
2362 */
2363static struct conn_stream *h2_attach(struct connection *conn)
2364{
2365 return NULL;
2366}
2367
2368/* callback used to update the mux's polling flags after changing a cs' status.
2369 * The caller (cs_update_mux_polling) will take care of propagating any changes
2370 * to the transport layer.
2371 */
2372static void h2_update_poll(struct conn_stream *cs)
2373{
Willy Tarreau1d393222017-10-17 10:26:19 +02002374 struct h2s *h2s = cs->ctx;
2375
2376 if (!h2s)
2377 return;
2378
Willy Tarreaud7739c82017-10-30 15:38:23 +01002379 /* we may unblock a blocked read */
2380
Willy Tarreau315d8072017-12-10 22:17:57 +01002381 if (cs->flags & CS_FL_DATA_RD_ENA) {
2382 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002383 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002384 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002385 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002386 conn_xprt_want_send(cs->conn);
2387 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002388 }
2389
Willy Tarreau1d393222017-10-17 10:26:19 +02002390 /* Note: the stream and stream-int code doesn't allow us to perform a
2391 * synchronous send() here unfortunately, because this code is called
2392 * as si_update() from the process_stream() context. This means that
2393 * we have to queue the current cs and defer its processing after the
2394 * connection's cs list is processed anyway.
2395 */
2396
2397 if (cs->flags & CS_FL_DATA_WR_ENA) {
2398 if (LIST_ISEMPTY(&h2s->list)) {
2399 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2400 !h2s->h2c->mbuf->o && // not yet subscribed
2401 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2402 conn_xprt_want_send(cs->conn);
2403 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2404 }
2405 }
2406 else if (!LIST_ISEMPTY(&h2s->list)) {
2407 LIST_DEL(&h2s->list);
2408 LIST_INIT(&h2s->list);
2409 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2410 }
2411
2412 /* this can happen from within si_chk_snd() */
2413 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2414 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002415}
2416
2417/*
2418 * Detach the stream from the connection and possibly release the connection.
2419 */
2420static void h2_detach(struct conn_stream *cs)
2421{
Willy Tarreau60935142017-10-16 18:11:19 +02002422 struct h2s *h2s = cs->ctx;
2423 struct h2c *h2c;
2424
2425 cs->ctx = NULL;
2426 if (!h2s)
2427 return;
2428
2429 h2c = h2s->h2c;
2430 h2s->cs = NULL;
2431
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002432 /* this stream may be blocked waiting for some data to leave (possibly
2433 * an ES or RST frame), so orphan it in this case.
2434 */
2435 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2436 return;
2437
Willy Tarreau541dd822017-11-23 18:12:50 +01002438 /* the stream could be in the send list */
2439 LIST_DEL(&h2s->list);
2440
Willy Tarreau45f752e2017-10-30 15:44:59 +01002441 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2442 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2443 /* unblock the connection if it was blocked on this
2444 * stream.
2445 */
2446 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2447 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2448 conn_xprt_want_recv(cs->conn);
2449 conn_xprt_want_send(cs->conn);
2450 }
2451
Willy Tarreau60935142017-10-16 18:11:19 +02002452 if (h2s->by_id.node.leaf_p) {
2453 /* h2s still attached to the h2c */
Willy Tarreau0a10de62018-03-01 16:27:53 +01002454 h2s_detach(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002455
2456 /* We don't want to close right now unless we're removing the
2457 * last stream, and either the connection is in error, or it
2458 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002459 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002460 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002461 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2462 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002463 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002464 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2465 (conn_xprt_read0_pending(h2c->conn) ||
2466 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002467 /* no more stream will come, kill it now */
2468 h2_release(h2c->conn);
2469 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002470 else if (h2c->task) {
Willy Tarreau84b118f2018-03-05 16:10:54 +01002471 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002472 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002473 task_queue(h2c->task);
2474 }
2475 else
2476 h2c->task->expire = TICK_ETERNITY;
2477 }
Willy Tarreau60935142017-10-16 18:11:19 +02002478 }
Willy Tarreau0a10de62018-03-01 16:27:53 +01002479 h2s_free(h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002480}
2481
2482static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2483{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002484 struct h2s *h2s = cs->ctx;
2485
2486 if (!mode)
2487 return;
2488
Willy Tarreau721c9742017-11-07 11:05:42 +01002489 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002490 return;
2491
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002492 /* if no outgoing data was seen on this stream, it means it was
2493 * closed with a "tcp-request content" rule that is normally
2494 * used to kill the connection ASAP (eg: limit abuse). In this
2495 * case we send a goaway to close the connection.
2496 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002497 if (!(h2s->flags & H2_SF_RST_SENT) &&
2498 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2499 return;
2500
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002501 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2502 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2503 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2504 return;
2505
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002506 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2507 conn_xprt_want_send(cs->conn);
2508
Willy Tarreau00dd0782018-03-01 16:31:34 +01002509 h2s_close(h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002510}
2511
2512static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2513{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002514 struct h2s *h2s = cs->ctx;
2515
Willy Tarreau721c9742017-11-07 11:05:42 +01002516 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002517 return;
2518
Willy Tarreau67434202017-11-06 20:20:51 +01002519 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002520 /* we can cleanly close using an empty data frame only after headers */
2521
2522 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2523 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002524 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002525
2526 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002527 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002528 else
2529 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002530 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002531 /* if no outgoing data was seen on this stream, it means it was
2532 * closed with a "tcp-request content" rule that is normally
2533 * used to kill the connection ASAP (eg: limit abuse). In this
2534 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002535 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002536 if (!(h2s->flags & H2_SF_RST_SENT) &&
2537 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2538 return;
2539
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002540 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2541 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002542 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2543 return;
2544
Willy Tarreau00dd0782018-03-01 16:31:34 +01002545 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002546 }
2547
2548 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2549 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002550}
2551
Willy Tarreau13278b42017-10-13 19:23:14 +02002552/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2553 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2554 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002555 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002556 */
2557static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2558{
2559 struct h2c *h2c = h2s->h2c;
2560 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002561 struct chunk *tmp = get_trash_chunk();
2562 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002563 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002564 int flen = h2c->dfl;
2565 int outlen = 0;
2566 int wrap;
2567 int try;
2568
2569 if (!h2c->dfl) {
2570 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002571 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002572 return 0;
2573 }
2574
Willy Tarreau68472622017-12-11 18:36:37 +01002575 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2576 return 0; // incomplete input frame
2577
Willy Tarreau13278b42017-10-13 19:23:14 +02002578 /* if the input buffer wraps, take a temporary copy of it (rare) */
2579 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2580 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002581 copy = alloc_trash_chunk();
2582 if (!copy) {
2583 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2584 goto fail;
2585 }
2586 memcpy(copy->str, h2c->dbuf->p, wrap);
2587 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2588 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002589 }
2590
2591 /* The padlen is the first byte before data, and the padding appears
2592 * after data. padlen+data+padding are included in flen.
2593 */
2594 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002595 h2c->dpl = *hdrs;
2596 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002597 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2598 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002599 return 0;
2600 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002601 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002602 hdrs += 1; // skip Pad Length
2603 }
2604
2605 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2606 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002607 if (read_n32(hdrs) == h2s->id) {
2608 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2609 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2610 return 0;//goto fail_stream;
2611 }
2612
Willy Tarreau13278b42017-10-13 19:23:14 +02002613 hdrs += 5; // stream dep = 4, weight = 1
2614 flen -= 5;
2615 }
2616
2617 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2618 * don't support this for now and can't even decompress so we have to
2619 * break the connection.
2620 */
2621 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2622 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002623 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002624 }
2625
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002626 /* we can't retry a failed decompression operation so we must be very
2627 * careful not to take any risks. In practice the output buffer is
2628 * always empty except maybe for trailers, so these operations almost
2629 * never happen.
2630 */
2631 if (unlikely(buf->o)) {
2632 /* need to let the output buffer flush and
2633 * mark the buffer for later wake up.
2634 */
2635 goto fail;
2636 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002637
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002638 if (unlikely(buffer_space_wraps(buf))) {
2639 /* it doesn't fit and the buffer is fragmented,
2640 * so let's defragment it and try again.
2641 */
2642 buffer_slow_realign(buf);
2643 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002644
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002645 /* first check if we have some room after p+i */
2646 try = buf->data + buf->size - (buf->p + buf->i);
2647
2648 /* otherwise continue between data and p-o */
2649 if (try <= 0) {
2650 try = buf->p - (buf->data + buf->o);
2651 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002652 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002653 }
2654 if (try > count)
2655 try = count;
2656
2657 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2658 sizeof(list)/sizeof(list[0]), tmp);
2659 if (outlen < 0) {
2660 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2661 goto fail;
2662 }
2663
2664 /* OK now we have our header list in <list> */
2665 outlen = h2_make_h1_request(list, bi_end(buf), try);
2666
2667 if (outlen < 0) {
2668 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2669 goto fail;
2670 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002671
2672 /* now consume the input data */
2673 bi_del(h2c->dbuf, h2c->dfl);
2674 h2c->st0 = H2_CS_FRAME_H;
2675 buf->i += outlen;
2676
2677 /* don't send it before returning data!
2678 * FIXME: should we instead try to send it much later, after the
2679 * response ? This would require that we keep a copy of it in h2s.
2680 */
2681 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2682 h2s->cs->flags |= CS_FL_EOS;
2683 h2s->flags |= H2_SF_ES_RCVD;
2684 }
2685
Willy Tarreau68dd9852017-07-03 14:44:26 +02002686 leave:
2687 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002688 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002689 fail:
2690 outlen = 0;
2691 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002692}
2693
Willy Tarreau454f9052017-10-26 19:40:35 +02002694/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2695 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2696 * in use, a new chunk is emitted for each frame. This is supposed to fit
2697 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2698 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2699 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2700 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002701 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2702 * checked to know if some data remain pending (an empty DATA frame can return
2703 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2704 * connection errors in h2c->errcode. The caller must already have checked the
2705 * frame header and ensured that the frame was complete or the buffer full. It
2706 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002707 */
2708static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2709{
2710 struct h2c *h2c = h2s->h2c;
2711 int block1, block2;
2712 unsigned int flen = h2c->dfl;
Willy Tarreau454f9052017-10-26 19:40:35 +02002713
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002714 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002715 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002716
2717 /* The padlen is the first byte before data, and the padding appears
2718 * after data. padlen+data+padding are included in flen.
2719 */
Willy Tarreau79127812017-12-03 21:06:59 +01002720 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002721 if (h2c->dbuf->i < 1)
2722 return 0;
2723
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002724 h2c->dpl = *(uint8_t *)bi_ptr(h2c->dbuf);
2725 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002726 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2727 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002728 return 0;
2729 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002730
2731 /* skip the padlen byte */
2732 bi_del(h2c->dbuf, 1);
2733 h2c->dfl--;
2734 h2c->rcvd_c++; h2c->rcvd_s++;
2735 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002736 }
2737
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002738 flen = h2c->dfl - h2c->dpl;
2739 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002740 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002741
2742 if (flen > h2c->dbuf->i) {
2743 flen = h2c->dbuf->i;
2744 if (!flen)
2745 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002746 }
2747
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002748 /* does it fit in output buffer or should we wait ? */
2749 if (flen > count) {
2750 flen = count;
2751 if (!flen) {
2752 h2c->flags |= H2_CF_DEM_SFULL;
2753 h2s->cs->flags |= CS_FL_RCV_MORE;
2754 return 0;
2755 }
2756 }
2757
Willy Tarreau454f9052017-10-26 19:40:35 +02002758 /* Block1 is the length of the first block before the buffer wraps,
2759 * block2 is the optional second block to reach the end of the frame.
2760 */
2761 block1 = bi_contig_data(h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002762 if (block1 > flen)
2763 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002764 block2 = flen - block1;
2765
2766 if (block1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002767 bi_putblk(buf, b_ptr(h2c->dbuf, 0), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002768
2769 if (block2)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002770 bi_putblk(buf, b_ptr(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002771
2772 /* now mark the input data as consumed (will be deleted from the buffer
2773 * by the caller when seeing FRAME_A after sending the window update).
2774 */
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002775 bi_del(h2c->dbuf, flen);
2776 h2c->dfl -= flen;
2777 h2c->rcvd_c += flen;
2778 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2779
2780 if (h2c->dfl > h2c->dpl) {
2781 /* more data available, transfer stalled on stream full */
2782 h2c->flags |= H2_CF_DEM_SFULL;
2783 h2s->cs->flags |= CS_FL_RCV_MORE;
2784 return flen;
2785 }
2786
Willy Tarreau4a28da12018-01-04 14:41:00 +01002787 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002788 /* here we're done with the frame, all the payload (except padding) was
2789 * transferred.
2790 */
Willy Tarreau454f9052017-10-26 19:40:35 +02002791 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2792
2793 /* don't send it before returning data!
2794 * FIXME: should we instead try to send it much later, after the
2795 * response ? This would require that we keep a copy of it in h2s.
2796 */
Willy Tarreau79127812017-12-03 21:06:59 +01002797 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002798 h2s->cs->flags |= CS_FL_EOS;
2799 h2s->flags |= H2_SF_ES_RCVD;
2800 }
2801
2802 return flen;
2803}
2804
Willy Tarreau62f52692017-10-08 23:01:42 +02002805/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002806 * Called from the upper layer to get more data, up to <count> bytes. The
2807 * caller is responsible for never asking for more data than what is available
2808 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002809 */
2810static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2811{
Willy Tarreau13278b42017-10-13 19:23:14 +02002812 struct h2s *h2s = cs->ctx;
2813 struct h2c *h2c = h2s->h2c;
2814 int ret = 0;
2815
2816 if (h2c->st0 != H2_CS_FRAME_P)
2817 return 0; // no pre-parsed frame yet
2818
2819 if (h2c->dsi != h2s->id)
2820 return 0; // not for us
2821
2822 if (!h2c->dbuf->size)
2823 return 0; // empty buffer
2824
Willy Tarreau13278b42017-10-13 19:23:14 +02002825 switch (h2c->dft) {
2826 case H2_FT_HEADERS:
2827 ret = h2_frt_decode_headers(h2s, buf, count);
2828 break;
2829
Willy Tarreau454f9052017-10-26 19:40:35 +02002830 case H2_FT_DATA:
2831 ret = h2_frt_transfer_data(h2s, buf, count);
2832 break;
2833
Willy Tarreau13278b42017-10-13 19:23:14 +02002834 default:
2835 ret = 0;
2836 }
2837 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002838}
2839
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002840/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2841 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2842 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2843 * to the number of buffer bytes consumed.
2844 */
2845static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2846{
2847 struct http_hdr list[MAX_HTTP_HDR];
2848 struct h2c *h2c = h2s->h2c;
2849 struct h1m *h1m = &h2s->res;
2850 struct chunk outbuf;
2851 int es_now = 0;
2852 int ret = 0;
2853 int hdr;
2854
2855 if (h2c_mux_busy(h2c, h2s)) {
2856 h2s->flags |= H2_SF_BLK_MBUSY;
2857 return 0;
2858 }
2859
Willy Tarreau44e973f2018-03-01 17:49:30 +01002860 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002861 h2c->flags |= H2_CF_MUX_MALLOC;
2862 h2s->flags |= H2_SF_BLK_MROOM;
2863 return 0;
2864 }
2865
2866 /* First, try to parse the H1 response and index it into <list>.
2867 * NOTE! Since it comes from haproxy, we *know* that a response header
2868 * block does not wrap and we can safely read it this way without
2869 * having to realign the buffer.
2870 */
2871 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2872 list, sizeof(list)/sizeof(list[0]), h1m);
2873 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002874 /* incomplete or invalid response, this is abnormal coming from
2875 * haproxy and may only result in a bad errorfile or bad Lua code
2876 * so that won't be fixed, raise an error now.
2877 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002878 * FIXME: we should instead add the ability to only return a
2879 * 502 bad gateway. But in theory this is not supposed to
2880 * happen.
2881 */
2882 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2883 ret = 0;
2884 goto end;
2885 }
2886
2887 chunk_reset(&outbuf);
2888
2889 while (1) {
2890 outbuf.str = bo_end(h2c->mbuf);
2891 outbuf.size = bo_contig_space(h2c->mbuf);
2892 outbuf.len = 0;
2893
2894 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2895 break;
2896 realign_again:
2897 buffer_slow_realign(h2c->mbuf);
2898 }
2899
2900 if (outbuf.size < 9) {
2901 h2c->flags |= H2_CF_MUX_MFULL;
2902 h2s->flags |= H2_SF_BLK_MROOM;
2903 ret = 0;
2904 goto end;
2905 }
2906
2907 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2908 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2909 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2910 outbuf.len = 9;
2911
2912 /* encode status, which necessarily is the first one */
2913 if (outbuf.len < outbuf.size && h1m->status == 200)
2914 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2915 else if (outbuf.len < outbuf.size && h1m->status == 304)
2916 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002917 else if (unlikely(list[0].v.len != 3)) {
2918 /* this is an unparsable response */
2919 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2920 ret = 0;
2921 goto end;
2922 }
2923 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002924 /* basic encoding of the status code */
2925 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2926 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2927 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2928 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2929 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2930 }
2931 else {
2932 if (buffer_space_wraps(h2c->mbuf))
2933 goto realign_again;
2934
2935 h2c->flags |= H2_CF_MUX_MFULL;
2936 h2s->flags |= H2_SF_BLK_MROOM;
2937 ret = 0;
2938 goto end;
2939 }
2940
2941 /* encode all headers, stop at empty name */
2942 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002943 /* these ones do not exist in H2 and must be dropped. */
2944 if (isteq(list[hdr].n, ist("connection")) ||
2945 isteq(list[hdr].n, ist("proxy-connection")) ||
2946 isteq(list[hdr].n, ist("keep-alive")) ||
2947 isteq(list[hdr].n, ist("upgrade")) ||
2948 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002949 continue;
2950
2951 if (isteq(list[hdr].n, ist("")))
2952 break; // end
2953
2954 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2955 /* output full */
2956 if (buffer_space_wraps(h2c->mbuf))
2957 goto realign_again;
2958
2959 h2c->flags |= H2_CF_MUX_MFULL;
2960 h2s->flags |= H2_SF_BLK_MROOM;
2961 ret = 0;
2962 goto end;
2963 }
2964 }
2965
2966 /* we may need to add END_STREAM */
2967 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2968 es_now = 1;
2969
2970 /* update the frame's size */
2971 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2972
2973 if (es_now)
2974 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2975
2976 /* consume incoming H1 response */
2977 bo_del(buf, ret);
2978
2979 /* commit the H2 response */
2980 h2c->mbuf->o += outbuf.len;
2981 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01002982 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002983
2984 /* for now we don't implemented CONTINUATION, so we wait for a
2985 * body or directly end in TRL2.
2986 */
2987 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01002988 // trim any possibly pending data (eg: inconsistent content-length)
2989 bo_del(buf, buf->o);
2990
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002991 h1m->state = HTTP_MSG_DONE;
2992 h2s->flags |= H2_SF_ES_SENT;
2993 if (h2s->st == H2_SS_OPEN)
2994 h2s->st = H2_SS_HLOC;
2995 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01002996 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002997 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01002998 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01002999 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003000 h1m->state = HTTP_MSG_RPBEFORE;
3001 h1m->status = 0;
3002 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003003 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003004 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003005 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003006 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003007
3008 end:
3009 //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));
3010 return ret;
3011}
3012
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003013/* Try to send a DATA frame matching HTTP/1 response present in the response
3014 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
3015 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
3016 * corresponds to the number of buffer bytes consumed.
3017 */
3018static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
3019{
3020 struct h2c *h2c = h2s->h2c;
3021 struct h1m *h1m = &h2s->res;
3022 struct chunk outbuf;
3023 int ret = 0;
3024 int total = 0;
3025 int es_now = 0;
3026 int size = 0;
3027 char *blk1, *blk2;
3028 int len1, len2;
3029
3030 if (h2c_mux_busy(h2c, h2s)) {
3031 h2s->flags |= H2_SF_BLK_MBUSY;
3032 goto end;
3033 }
3034
Willy Tarreau44e973f2018-03-01 17:49:30 +01003035 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003036 h2c->flags |= H2_CF_MUX_MALLOC;
3037 h2s->flags |= H2_SF_BLK_MROOM;
3038 goto end;
3039 }
3040
3041 new_frame:
3042 if (!buf->o)
3043 goto end;
3044
3045 chunk_reset(&outbuf);
3046
3047 while (1) {
3048 outbuf.str = bo_end(h2c->mbuf);
3049 outbuf.size = bo_contig_space(h2c->mbuf);
3050 outbuf.len = 0;
3051
3052 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
3053 break;
3054 realign_again:
3055 buffer_slow_realign(h2c->mbuf);
3056 }
3057
3058 if (outbuf.size < 9) {
3059 h2c->flags |= H2_CF_MUX_MFULL;
3060 h2s->flags |= H2_SF_BLK_MROOM;
3061 goto end;
3062 }
3063
3064 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3065 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3066 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3067 outbuf.len = 9;
3068
3069 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3070 case 0: /* no content length, read till SHUTW */
3071 size = buf->o;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003072 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003073 break;
3074 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
3075 size = buf->o;
3076 if ((long long)size > h1m->curr_len)
3077 size = h1m->curr_len;
3078 break;
3079 default: /* te:chunked : parse chunks */
3080 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
3081 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
3082 if (!ret)
3083 goto end;
3084
3085 if (ret < 0) {
3086 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3087 h1m->err_pos = ret;
3088 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3089 goto end;
3090 }
3091 bo_del(buf, ret);
3092 total += ret;
3093 h1m->state = HTTP_MSG_CHUNK_SIZE;
3094 }
3095
3096 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3097 unsigned int chunk;
3098
3099 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
3100 if (!ret)
3101 goto end;
3102
3103 if (ret < 0) {
3104 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3105 h1m->err_pos = ret;
3106 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3107 goto end;
3108 }
3109
3110 size = chunk;
3111 h1m->curr_len = chunk;
3112 h1m->body_len += chunk;
3113 bo_del(buf, ret);
3114 total += ret;
3115 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3116 if (!size)
3117 goto send_empty;
3118 }
3119
3120 /* in MSG_DATA state, continue below */
3121 size = h1m->curr_len;
3122 break;
3123 }
3124
3125 /* we have in <size> the exact number of bytes we need to copy from
3126 * the H1 buffer. We need to check this against the connection's and
3127 * the stream's send windows, and to ensure that this fits in the max
3128 * frame size and in the buffer's available space minus 9 bytes (for
3129 * the frame header). The connection's flow control is applied last so
3130 * that we can use a separate list of streams which are immediately
3131 * unblocked on window opening. Note: we don't implement padding.
3132 */
3133
3134 if (size > buf->o)
3135 size = buf->o;
3136
3137 if (size > h2s->mws)
3138 size = h2s->mws;
3139
3140 if (size <= 0) {
3141 h2s->flags |= H2_SF_BLK_SFCTL;
3142 goto end;
3143 }
3144
3145 if (h2c->mfs && size > h2c->mfs)
3146 size = h2c->mfs;
3147
3148 if (size + 9 > outbuf.size) {
3149 /* we have an opportunity for enlarging the too small
3150 * available space, let's try.
3151 */
3152 if (buffer_space_wraps(h2c->mbuf))
3153 goto realign_again;
3154 size = outbuf.size - 9;
3155 }
3156
3157 if (size <= 0) {
3158 h2c->flags |= H2_CF_MUX_MFULL;
3159 h2s->flags |= H2_SF_BLK_MROOM;
3160 goto end;
3161 }
3162
3163 if (size > h2c->mws)
3164 size = h2c->mws;
3165
3166 if (size <= 0) {
3167 h2s->flags |= H2_SF_BLK_MFCTL;
3168 goto end;
3169 }
3170
3171 /* copy whatever we can */
3172 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
3173 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
3174 if (ret == 1)
3175 len2 = 0;
3176
3177 if (!ret || len1 + len2 < size) {
3178 /* FIXME: must normally never happen */
3179 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3180 goto end;
3181 }
3182
3183 /* limit len1/len2 to size */
3184 if (len1 + len2 > size) {
3185 int sub = len1 + len2 - size;
3186
3187 if (len2 > sub)
3188 len2 -= sub;
3189 else {
3190 sub -= len2;
3191 len2 = 0;
3192 len1 -= sub;
3193 }
3194 }
3195
3196 /* now let's copy this this into the output buffer */
3197 memcpy(outbuf.str + 9, blk1, len1);
3198 if (len2)
3199 memcpy(outbuf.str + 9 + len1, blk2, len2);
3200
3201 send_empty:
3202 /* we may need to add END_STREAM */
3203 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3204 * could rely on the MSG_MORE flag as a hint for this ?
3205 */
3206 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3207 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3208 es_now = 1;
3209
3210 /* update the frame's size */
3211 h2_set_frame_size(outbuf.str, size);
3212
3213 if (es_now)
3214 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3215
3216 /* commit the H2 response */
3217 h2c->mbuf->o += size + 9;
3218 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3219
3220 /* consume incoming H1 response */
3221 if (size > 0) {
3222 bo_del(buf, size);
3223 total += size;
3224 h1m->curr_len -= size;
3225 h2s->mws -= size;
3226 h2c->mws -= size;
3227
3228 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3229 h1m->state = HTTP_MSG_CHUNK_CRLF;
3230 goto new_frame;
3231 }
3232 }
3233
3234 if (es_now) {
3235 if (h2s->st == H2_SS_OPEN)
3236 h2s->st = H2_SS_HLOC;
3237 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003238 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003239
Willy Tarreau35a62702018-02-27 15:37:25 +01003240 if (!(h1m->flags & H1_MF_CHNK)) {
3241 // trim any possibly pending data (eg: inconsistent content-length)
3242 bo_del(buf, buf->o);
3243
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003244 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003245 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003246
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003247 h2s->flags |= H2_SF_ES_SENT;
3248 }
3249
3250 end:
3251 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);
3252 return total;
3253}
3254
Willy Tarreau62f52692017-10-08 23:01:42 +02003255/* Called from the upper layer, to send data */
3256static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3257{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003258 struct h2s *h2s = cs->ctx;
3259 int total = 0;
3260
Willy Tarreauc4312d32017-11-07 12:01:53 +01003261 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3262 h2s->flags |= H2_SF_OUTGOING_DATA;
3263
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003264 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3265 if (h2s->res.state < HTTP_MSG_BODY) {
3266 total += h2s_frt_make_resp_headers(h2s, buf);
3267
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003268 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003269 break;
3270
3271 if (h2s->flags & H2_SF_BLK_ANY)
3272 break;
3273 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003274 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3275 total += h2s_frt_make_resp_data(h2s, buf);
3276
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003277 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003278 break;
3279
3280 if (h2s->flags & H2_SF_BLK_ANY)
3281 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003282 }
3283 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3284 /* consume the trailers if any (we don't forward them for now) */
3285 int count = h1_measure_trailers(buf);
3286
3287 if (unlikely(count <= 0)) {
3288 if (count < 0)
3289 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3290 break;
3291 }
3292 total += count;
3293 bo_del(buf, count);
Willy Tarreau35a62702018-02-27 15:37:25 +01003294
3295 // trim any possibly pending data (eg: extra CR-LF, ...)
3296 bo_del(buf, buf->o);
3297
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003298 h2s->res.state = HTTP_MSG_DONE;
3299 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003300 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003301 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003302 cs->flags |= CS_FL_ERROR;
3303 break;
3304 }
3305 }
3306
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003307 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003308 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003309 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003310 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003311 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003312 }
3313
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003314 if (h2s->flags & H2_SF_BLK_SFCTL) {
3315 /* stream flow control, quit the list */
3316 LIST_DEL(&h2s->list);
3317 LIST_INIT(&h2s->list);
3318 }
3319
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003320 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003321}
3322
3323
3324/*******************************************************/
3325/* functions below are dedicated to the config parsers */
3326/*******************************************************/
3327
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003328/* config parser for global "tune.h2.header-table-size" */
3329static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3330 struct proxy *defpx, const char *file, int line,
3331 char **err)
3332{
3333 if (too_many_args(1, args, err, NULL))
3334 return -1;
3335
3336 h2_settings_header_table_size = atoi(args[1]);
3337 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3338 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3339 return -1;
3340 }
3341 return 0;
3342}
Willy Tarreau62f52692017-10-08 23:01:42 +02003343
Willy Tarreaue6baec02017-07-27 11:45:11 +02003344/* config parser for global "tune.h2.initial-window-size" */
3345static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3346 struct proxy *defpx, const char *file, int line,
3347 char **err)
3348{
3349 if (too_many_args(1, args, err, NULL))
3350 return -1;
3351
3352 h2_settings_initial_window_size = atoi(args[1]);
3353 if (h2_settings_initial_window_size < 0) {
3354 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3355 return -1;
3356 }
3357 return 0;
3358}
3359
Willy Tarreau5242ef82017-07-27 11:47:28 +02003360/* config parser for global "tune.h2.max-concurrent-streams" */
3361static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3362 struct proxy *defpx, const char *file, int line,
3363 char **err)
3364{
3365 if (too_many_args(1, args, err, NULL))
3366 return -1;
3367
3368 h2_settings_max_concurrent_streams = atoi(args[1]);
3369 if (h2_settings_max_concurrent_streams < 0) {
3370 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3371 return -1;
3372 }
3373 return 0;
3374}
3375
Willy Tarreau62f52692017-10-08 23:01:42 +02003376
3377/****************************************/
3378/* MUX initialization and instanciation */
3379/***************************************/
3380
3381/* The mux operations */
3382const struct mux_ops h2_ops = {
3383 .init = h2_init,
3384 .recv = h2_recv,
3385 .send = h2_send,
3386 .wake = h2_wake,
3387 .update_poll = h2_update_poll,
3388 .rcv_buf = h2_rcv_buf,
3389 .snd_buf = h2_snd_buf,
3390 .attach = h2_attach,
3391 .detach = h2_detach,
3392 .shutr = h2_shutr,
3393 .shutw = h2_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003394 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003395 .name = "H2",
3396};
3397
3398/* ALPN selection : this mux registers ALPN tolen "h2" */
3399static struct alpn_mux_list alpn_mux_h2 =
3400 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3401
3402/* config keyword parsers */
3403static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003404 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003405 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003406 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003407 { 0, NULL, NULL }
3408}};
3409
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003410static void __h2_deinit(void)
3411{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003412 pool_destroy(pool_head_h2s);
3413 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003414}
3415
Willy Tarreau62f52692017-10-08 23:01:42 +02003416__attribute__((constructor))
3417static void __h2_init(void)
3418{
3419 alpn_register_mux(&alpn_mux_h2);
3420 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003421 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003422 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3423 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003424}