blob: 6c63b86125922cf823dc924982d1f0092f20f3ac [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 Tarreau35dbd5d2017-09-22 09:13:49 +0200122 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +0200123 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200124};
125
Willy Tarreau18312642017-10-11 07:57:07 +0200126/* H2 stream state, in h2s->st */
127enum h2_ss {
128 H2_SS_IDLE = 0, // idle
129 H2_SS_RLOC, // reserved(local)
130 H2_SS_RREM, // reserved(remote)
131 H2_SS_OPEN, // open
132 H2_SS_HREM, // half-closed(remote)
133 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200134 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200135 H2_SS_CLOSED, // closed
136 H2_SS_ENTRIES // must be last
137} __attribute__((packed));
138
139/* HTTP/2 stream flags (32 bit), in h2s->flags */
140#define H2_SF_NONE 0x00000000
141#define H2_SF_ES_RCVD 0x00000001
142#define H2_SF_ES_SENT 0x00000002
143
144#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
145#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
146
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200147/* stream flags indicating the reason the stream is blocked */
148#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
149#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
150#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
151#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
152#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
153
Willy Tarreau454f9052017-10-26 19:40:35 +0200154/* stream flags indicating how data is supposed to be sent */
155#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
156#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
157
158/* step we're currently in when sending chunks. This is needed because we may
159 * have to transfer chunks as large as a full buffer so there's no room left
160 * for size nor crlf around.
161 */
162#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
163#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
164#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
165
166#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
167
Willy Tarreau67434202017-11-06 20:20:51 +0100168#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100169#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100170
Willy Tarreau18312642017-10-11 07:57:07 +0200171/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
172 * it is being processed in the internal HTTP representation (H1 for now).
173 */
174struct h2s {
175 struct conn_stream *cs;
176 struct h2c *h2c;
177 struct h1m req, res; /* request and response parser state for H1 */
178 struct eb32_node by_id; /* place in h2c's streams_by_id */
179 struct list list; /* position in active/blocked lists if blocked>0 */
180 int32_t id; /* stream ID */
181 uint32_t flags; /* H2_SF_* */
182 int mws; /* mux window size for this stream */
183 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
184 enum h2_ss st;
185};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200186
Willy Tarreauc6405142017-09-21 20:23:50 +0200187/* descriptor for an h2 frame header */
188struct h2_fh {
189 uint32_t len; /* length, host order, 24 bits */
190 uint32_t sid; /* stream id, host order, 31 bits */
191 uint8_t ft; /* frame type */
192 uint8_t ff; /* frame flags */
193};
194
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200195/* a few settings from the global section */
196static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200197static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200198static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200199
Willy Tarreau2a856182017-05-16 15:20:39 +0200200/* a dmumy closed stream */
201static const struct h2s *h2_closed_stream = &(const struct h2s){
202 .cs = NULL,
203 .h2c = NULL,
204 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100205 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100206 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200207 .id = 0,
208};
209
210/* and a dummy idle stream for use with any unannounced stream */
211static const struct h2s *h2_idle_stream = &(const struct h2s){
212 .cs = NULL,
213 .h2c = NULL,
214 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100215 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200216 .id = 0,
217};
218
Willy Tarreauea392822017-10-31 10:02:25 +0100219static struct task *h2_timeout_task(struct task *t);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200220
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200221/*****************************************************/
222/* functions below are for dynamic buffer management */
223/*****************************************************/
224
Willy Tarreau315d8072017-12-10 22:17:57 +0100225/* indicates whether or not the we may call the h2_recv() function to attempt
226 * to receive data into the buffer and/or demux pending data. The condition is
227 * a bit complex due to some API limits for now. The rules are the following :
228 * - if an error or a shutdown was detected on the connection and the buffer
229 * is empty, we must not attempt to receive
230 * - if the demux buf failed to be allocated, we must not try to receive and
231 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100232 * - if no flag indicates a blocking condition, we may attempt to receive,
233 * regardless of whether the demux buffer is full or not, so that only
234 * de demux part decides whether or not to block. This is needed because
235 * the connection API indeed prevents us from re-enabling receipt that is
236 * already enabled in a polled state, so we must always immediately stop
237 * as soon as the demux can't proceed so as never to hit an end of read
238 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100239 * - otherwise must may not attempt
240 */
241static inline int h2_recv_allowed(const struct h2c *h2c)
242{
243 if (h2c->dbuf->i == 0 &&
244 (h2c->st0 >= H2_CS_ERROR ||
245 h2c->conn->flags & CO_FL_ERROR ||
246 conn_xprt_read0_pending(h2c->conn)))
247 return 0;
248
249 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100250 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100251 return 1;
252
253 return 0;
254}
255
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200256/* re-enables receiving on mux <target> after a buffer was allocated. It returns
257 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
258 * if it's impossible to wake up and we prefer to be woken up later.
259 */
260static int h2_dbuf_available(void *target)
261{
262 struct h2c *h2c = target;
263
264 /* take the buffer now as we'll get scheduled waiting for ->wake() */
265 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200266 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100267 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200268 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200269 return 1;
270 }
271 return 0;
272}
273
274static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
275{
276 struct buffer *buf = NULL;
277
278 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
279 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
280 h2c->dbuf_wait.target = h2c->conn;
281 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100282 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200283 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100284 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200285 __conn_xprt_stop_recv(h2c->conn);
286 }
287 return buf;
288}
289
290static inline void h2_release_dbuf(struct h2c *h2c)
291{
292 if (h2c->dbuf->size) {
293 b_free(&h2c->dbuf);
294 offer_buffers(h2c->dbuf_wait.target,
295 tasks_run_queue + applets_active_queue);
296 }
297}
298
Willy Tarreau14398122017-09-22 14:26:04 +0200299/* re-enables sending on mux <target> after a buffer was allocated. It returns
300 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
301 * if it's impossible to wake up and we prefer to be woken up later.
302 */
303static int h2_mbuf_available(void *target)
304{
305 struct h2c *h2c = target;
306
307 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
308 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200309 if (h2c->flags & H2_CF_MUX_MALLOC) {
310 h2c->flags &= ~H2_CF_MUX_MALLOC;
311 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
312 conn_xprt_want_send(h2c->conn);
313 }
314
315 if (h2c->flags & H2_CF_DEM_MROOM) {
316 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100317 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200318 conn_xprt_want_recv(h2c->conn);
319 }
320
Willy Tarreau14398122017-09-22 14:26:04 +0200321 /* FIXME: we should in fact call something like h2_update_poll()
322 * now to recompte the polling. For now it will be enough like
323 * this.
324 */
Willy Tarreau14398122017-09-22 14:26:04 +0200325 return 1;
326 }
327 return 0;
328}
329
330static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
331{
332 struct buffer *buf = NULL;
333
334 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
335 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
336 h2c->mbuf_wait.target = h2c;
337 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100338 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200339 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100340 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200341
342 /* FIXME: we should in fact only block the direction being
343 * currently used. For now it will be enough like this.
344 */
345 __conn_xprt_stop_send(h2c->conn);
346 __conn_xprt_stop_recv(h2c->conn);
347 }
348 return buf;
349}
350
351static inline void h2_release_mbuf(struct h2c *h2c)
352{
353 if (h2c->mbuf->size) {
354 b_free(&h2c->mbuf);
355 offer_buffers(h2c->mbuf_wait.target,
356 tasks_run_queue + applets_active_queue);
357 }
358}
359
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200360
Willy Tarreau62f52692017-10-08 23:01:42 +0200361/*****************************************************************/
362/* functions below are dedicated to the mux setup and management */
363/*****************************************************************/
364
Willy Tarreau32218eb2017-09-22 08:07:25 +0200365/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
366static int h2c_frt_init(struct connection *conn)
367{
368 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100369 struct task *t = NULL;
370 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200371
Willy Tarreaubafbe012017-11-24 17:34:44 +0100372 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200373 if (!h2c)
374 goto fail;
375
Willy Tarreau3f133572017-10-31 19:21:06 +0100376
Willy Tarreau599391a2017-11-24 10:16:00 +0100377 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
378 if (tick_isset(sess->fe->timeout.clientfin))
379 h2c->shut_timeout = sess->fe->timeout.clientfin;
380
Willy Tarreau33400292017-11-05 11:23:40 +0100381 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100382 if (tick_isset(h2c->timeout)) {
383 t = task_new(tid_bit);
384 if (!t)
385 goto fail;
386
387 h2c->task = t;
388 t->process = h2_timeout_task;
389 t->context = h2c;
390 t->expire = tick_add(now_ms, h2c->timeout);
391 }
Willy Tarreauea392822017-10-31 10:02:25 +0100392
Willy Tarreau32218eb2017-09-22 08:07:25 +0200393 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
394 if (!h2c->ddht)
395 goto fail;
396
397 /* Initialise the context. */
398 h2c->st0 = H2_CS_PREFACE;
399 h2c->conn = conn;
400 h2c->max_id = -1;
401 h2c->errcode = H2_ERR_NO_ERROR;
402 h2c->flags = H2_CF_NONE;
403 h2c->rcvd_c = 0;
404 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100405 h2c->nb_streams = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200406
407 h2c->dbuf = &buf_empty;
408 h2c->dsi = -1;
409 h2c->msi = -1;
410 h2c->last_sid = -1;
411
412 h2c->mbuf = &buf_empty;
413 h2c->miw = 65535; /* mux initial window size */
414 h2c->mws = 65535; /* mux window size */
415 h2c->mfs = 16384; /* initial max frame size */
416 h2c->streams_by_id = EB_ROOT_UNIQUE;
417 LIST_INIT(&h2c->send_list);
418 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200419 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200420 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200421 conn->mux_ctx = h2c;
422
Willy Tarreau3f133572017-10-31 19:21:06 +0100423 if (t)
424 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200425 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100426
Willy Tarreau32218eb2017-09-22 08:07:25 +0200427 /* mux->wake will be called soon to complete the operation */
428 return 0;
429 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100430 if (t)
431 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100432 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200433 return -1;
434}
435
Willy Tarreau62f52692017-10-08 23:01:42 +0200436/* Initialize the mux once it's attached. For outgoing connections, the context
437 * is already initialized before installing the mux, so we detect incoming
438 * connections from the fact that the context is still NULL. Returns < 0 on
439 * error.
440 */
441static int h2_init(struct connection *conn)
442{
443 if (conn->mux_ctx) {
444 /* we don't support outgoing connections for now */
445 return -1;
446 }
447
Willy Tarreau32218eb2017-09-22 08:07:25 +0200448 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200449}
450
Willy Tarreau2373acc2017-10-12 17:35:14 +0200451/* returns the stream associated with id <id> or NULL if not found */
452static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
453{
454 struct eb32_node *node;
455
Willy Tarreau2a856182017-05-16 15:20:39 +0200456 if (id > h2c->max_id)
457 return (struct h2s *)h2_idle_stream;
458
Willy Tarreau2373acc2017-10-12 17:35:14 +0200459 node = eb32_lookup(&h2c->streams_by_id, id);
460 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200461 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200462
463 return container_of(node, struct h2s, by_id);
464}
465
Willy Tarreau62f52692017-10-08 23:01:42 +0200466/* release function for a connection. This one should be called to free all
467 * resources allocated to the mux.
468 */
469static void h2_release(struct connection *conn)
470{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200471 struct h2c *h2c = conn->mux_ctx;
472
473 LIST_DEL(&conn->list);
474
475 if (h2c) {
476 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200477 h2_release_dbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100478 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200479 LIST_DEL(&h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100480 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200481
482 h2_release_mbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100483 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200484 LIST_DEL(&h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100485 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200486
Willy Tarreauea392822017-10-31 10:02:25 +0100487 if (h2c->task) {
488 task_delete(h2c->task);
489 task_free(h2c->task);
490 h2c->task = NULL;
491 }
492
Willy Tarreaubafbe012017-11-24 17:34:44 +0100493 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200494 }
495
496 conn->mux = NULL;
497 conn->mux_ctx = NULL;
498
499 conn_stop_tracking(conn);
500 conn_full_close(conn);
501 if (conn->destroy_cb)
502 conn->destroy_cb(conn);
503 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200504}
505
506
Willy Tarreau71681172017-10-23 14:39:06 +0200507/******************************************************/
508/* functions below are for the H2 protocol processing */
509/******************************************************/
510
511/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100512static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200513{
514 return h2s ? h2s->id : 0;
515}
516
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200517/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100518static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200519{
520 if (h2c->msi < 0)
521 return 0;
522
523 if (h2c->msi == h2s_id(h2s))
524 return 0;
525
526 return 1;
527}
528
Willy Tarreau741d6df2017-10-17 08:00:59 +0200529/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100530static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200531{
532 h2c->errcode = err;
533 h2c->st0 = H2_CS_ERROR;
534}
535
Willy Tarreau2e43f082017-10-17 08:03:59 +0200536/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100537static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200538{
539 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
540 h2s->errcode = err;
541 h2s->st = H2_SS_ERROR;
542 if (h2s->cs)
543 h2s->cs->flags |= CS_FL_ERROR;
544 }
545}
546
Willy Tarreaue4820742017-07-27 13:37:23 +0200547/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100548static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200549{
550 uint8_t *out = frame;
551
552 *out = len >> 16;
553 write_n16(out + 1, len);
554}
555
Willy Tarreau54c15062017-10-10 17:10:03 +0200556/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
557 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
558 * the caller's responsibility to verify that there are at least <bytes> bytes
559 * available in the buffer's input prior to calling this function.
560 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100561static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200562 const struct buffer *b, int o)
563{
564 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
565}
566
Willy Tarreau1f094672017-11-20 21:27:45 +0100567static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200568{
569 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
570}
571
Willy Tarreau1f094672017-11-20 21:27:45 +0100572static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200573{
574 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
575}
576
Willy Tarreau1f094672017-11-20 21:27:45 +0100577static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200578{
579 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
580}
581
582
Willy Tarreau715d5312017-07-11 15:20:24 +0200583/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
584 * is not obvious. It turns out that H2 headers are neither aligned nor do they
585 * use regular sizes. And to add to the trouble, the buffer may wrap so each
586 * byte read must be checked. The header is formed like this :
587 *
588 * b0 b1 b2 b3 b4 b5..b8
589 * +----------+---------+--------+----+----+----------------------+
590 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
591 * +----------+---------+--------+----+----+----------------------+
592 *
593 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
594 * we get the sid properly aligned and ordered, and 16 bits of len properly
595 * ordered as well. The type and flags can be extracted using bit shifts from
596 * the word, and only one extra read is needed to fetch len[16:23].
597 * Returns zero if some bytes are missing, otherwise non-zero on success.
598 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100599static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200600{
601 uint64_t w;
602
603 if (b->i < 9)
604 return 0;
605
606 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
607 h->len = *b->p << 16;
608 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
609 h->ff = w >> 32;
610 h->ft = w >> 40;
611 h->len += w >> 48;
612 return 1;
613}
614
615/* skip the next 9 bytes corresponding to the frame header possibly parsed by
616 * h2_peek_frame_hdr() above.
617 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100618static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200619{
620 bi_del(b, 9);
621}
622
623/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100624static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200625{
626 int ret;
627
628 ret = h2_peek_frame_hdr(b, h);
629 if (ret > 0)
630 h2_skip_frame_hdr(b);
631 return ret;
632}
633
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100634/* marks stream <h2s> as CLOSED for connection <h2c> and decrement the number
635 * of active streams for this connection if the stream was not yet closed.
636 * Please use this exclusively before closing a stream to ensure stream count
637 * is well maintained.
638 */
639static inline void h2c_stream_close(struct h2c *h2c, struct h2s *h2s)
640{
641 if (h2s->st != H2_SS_CLOSED)
642 h2s->h2c->nb_streams--;
643 h2s->st = H2_SS_CLOSED;
644}
645
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200646/* creates a new stream <id> on the h2c connection and returns it, or NULL in
647 * case of memory allocation error.
648 */
649static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
650{
651 struct conn_stream *cs;
652 struct h2s *h2s;
653
Willy Tarreaubafbe012017-11-24 17:34:44 +0100654 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200655 if (!h2s)
656 goto out;
657
658 h2s->h2c = h2c;
659 h2s->mws = h2c->miw;
660 h2s->flags = H2_SF_NONE;
661 h2s->errcode = H2_ERR_NO_ERROR;
662 h2s->st = H2_SS_IDLE;
663 h1m_init(&h2s->req);
664 h1m_init(&h2s->res);
665 h2s->by_id.key = h2s->id = id;
666 h2c->max_id = id;
667 LIST_INIT(&h2s->list);
668
669 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100670 h2c->nb_streams++;
671 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
672 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200673
674 cs = cs_new(h2c->conn);
675 if (!cs)
676 goto out_close;
677
678 h2s->cs = cs;
679 cs->ctx = h2s;
680
681 if (stream_create_from_cs(cs) < 0)
682 goto out_free_cs;
683
684 /* OK done, the stream lives its own life now */
685 return h2s;
686
687 out_free_cs:
688 cs_free(cs);
689 out_close:
Willy Tarreau49745612017-12-03 18:56:02 +0100690 h2c->nb_streams--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200691 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100692 pool_free(pool_head_h2s, h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200693 h2s = NULL;
694 out:
695 return h2s;
696}
697
Willy Tarreaube5b7152017-09-25 16:25:39 +0200698/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
699 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
700 * the various settings codes.
701 */
702static int h2c_snd_settings(struct h2c *h2c)
703{
704 struct buffer *res;
705 char buf_data[100]; // enough for 15 settings
706 struct chunk buf;
707 int ret;
708
709 if (h2c_mux_busy(h2c, NULL)) {
710 h2c->flags |= H2_CF_DEM_MBUSY;
711 return 0;
712 }
713
714 res = h2_get_mbuf(h2c);
715 if (!res) {
716 h2c->flags |= H2_CF_MUX_MALLOC;
717 h2c->flags |= H2_CF_DEM_MROOM;
718 return 0;
719 }
720
721 chunk_init(&buf, buf_data, sizeof(buf_data));
722 chunk_memcpy(&buf,
723 "\x00\x00\x00" /* length : 0 for now */
724 "\x04\x00" /* type : 4 (settings), flags : 0 */
725 "\x00\x00\x00\x00", /* stream ID : 0 */
726 9);
727
728 if (h2_settings_header_table_size != 4096) {
729 char str[6] = "\x00\x01"; /* header_table_size */
730
731 write_n32(str + 2, h2_settings_header_table_size);
732 chunk_memcat(&buf, str, 6);
733 }
734
735 if (h2_settings_initial_window_size != 65535) {
736 char str[6] = "\x00\x04"; /* initial_window_size */
737
738 write_n32(str + 2, h2_settings_initial_window_size);
739 chunk_memcat(&buf, str, 6);
740 }
741
742 if (h2_settings_max_concurrent_streams != 0) {
743 char str[6] = "\x00\x03"; /* max_concurrent_streams */
744
745 /* Note: 0 means "unlimited" for haproxy's config but not for
746 * the protocol, so never send this value!
747 */
748 write_n32(str + 2, h2_settings_max_concurrent_streams);
749 chunk_memcat(&buf, str, 6);
750 }
751
752 if (global.tune.bufsize != 16384) {
753 char str[6] = "\x00\x05"; /* max_frame_size */
754
755 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
756 * match bufsize - rewrite size, but at the moment it seems
757 * that clients don't take care of it.
758 */
759 write_n32(str + 2, global.tune.bufsize);
760 chunk_memcat(&buf, str, 6);
761 }
762
763 h2_set_frame_size(buf.str, buf.len - 9);
764 ret = bo_istput(res, ist2(buf.str, buf.len));
765 if (unlikely(ret <= 0)) {
766 if (!ret) {
767 h2c->flags |= H2_CF_MUX_MFULL;
768 h2c->flags |= H2_CF_DEM_MROOM;
769 return 0;
770 }
771 else {
772 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
773 return 0;
774 }
775 }
776 return ret;
777}
778
Willy Tarreau52eed752017-09-22 15:05:09 +0200779/* Try to receive a connection preface, then upon success try to send our
780 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
781 * missing data. It may return an error in h2c.
782 */
783static int h2c_frt_recv_preface(struct h2c *h2c)
784{
785 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200786 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200787
788 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
789
790 if (unlikely(ret1 <= 0)) {
791 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
792 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
793 return 0;
794 }
795
Willy Tarreaube5b7152017-09-25 16:25:39 +0200796 ret2 = h2c_snd_settings(h2c);
797 if (ret2 > 0)
798 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200799
Willy Tarreaube5b7152017-09-25 16:25:39 +0200800 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200801}
802
Willy Tarreau081d4722017-05-16 21:51:05 +0200803/* try to send a GOAWAY frame on the connection to report an error or a graceful
804 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
805 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
806 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
807 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
808 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
809 * on unrecoverable failure. It will not attempt to send one again in this last
810 * case so that it is safe to use h2c_error() to report such errors.
811 */
812static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
813{
814 struct buffer *res;
815 char str[17];
816 int ret;
817
818 if (h2c->flags & H2_CF_GOAWAY_FAILED)
819 return 1; // claim that it worked
820
821 if (h2c_mux_busy(h2c, h2s)) {
822 if (h2s)
823 h2s->flags |= H2_SF_BLK_MBUSY;
824 else
825 h2c->flags |= H2_CF_DEM_MBUSY;
826 return 0;
827 }
828
829 res = h2_get_mbuf(h2c);
830 if (!res) {
831 h2c->flags |= H2_CF_MUX_MALLOC;
832 if (h2s)
833 h2s->flags |= H2_SF_BLK_MROOM;
834 else
835 h2c->flags |= H2_CF_DEM_MROOM;
836 return 0;
837 }
838
839 /* len: 8, type: 7, flags: none, sid: 0 */
840 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
841
842 if (h2c->last_sid < 0)
843 h2c->last_sid = h2c->max_id;
844
845 write_n32(str + 9, h2c->last_sid);
846 write_n32(str + 13, h2c->errcode);
847 ret = bo_istput(res, ist2(str, 17));
848 if (unlikely(ret <= 0)) {
849 if (!ret) {
850 h2c->flags |= H2_CF_MUX_MFULL;
851 if (h2s)
852 h2s->flags |= H2_SF_BLK_MROOM;
853 else
854 h2c->flags |= H2_CF_DEM_MROOM;
855 return 0;
856 }
857 else {
858 /* we cannot report this error using GOAWAY, so we mark
859 * it and claim a success.
860 */
861 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
862 h2c->flags |= H2_CF_GOAWAY_FAILED;
863 return 1;
864 }
865 }
866 h2c->flags |= H2_CF_GOAWAY_SENT;
867 return ret;
868}
869
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100870/* Try to send an RST_STREAM frame on the connection for the indicated stream
871 * during mux operations. This stream must be valid and cannot be closed
872 * already. h2s->id will be used for the stream ID and h2s->errcode will be
873 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
874 * not yet.
875 *
876 * Returns > 0 on success or zero if nothing was done. In case of lack of room
877 * to write the message, it subscribes the stream to future notifications.
878 */
879static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
880{
881 struct buffer *res;
882 char str[13];
883 int ret;
884
885 if (!h2s || h2s->st == H2_SS_CLOSED)
886 return 1;
887
888 if (h2c_mux_busy(h2c, h2s)) {
889 h2s->flags |= H2_SF_BLK_MBUSY;
890 return 0;
891 }
892
893 res = h2_get_mbuf(h2c);
894 if (!res) {
895 h2c->flags |= H2_CF_MUX_MALLOC;
896 h2s->flags |= H2_SF_BLK_MROOM;
897 return 0;
898 }
899
900 /* len: 4, type: 3, flags: none */
901 memcpy(str, "\x00\x00\x04\x03\x00", 5);
902 write_n32(str + 5, h2s->id);
903 write_n32(str + 9, h2s->errcode);
904 ret = bo_istput(res, ist2(str, 13));
905
906 if (unlikely(ret <= 0)) {
907 if (!ret) {
908 h2c->flags |= H2_CF_MUX_MFULL;
909 h2s->flags |= H2_SF_BLK_MROOM;
910 return 0;
911 }
912 else {
913 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
914 return 0;
915 }
916 }
917
918 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100919 h2c_stream_close(h2c, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100920 return ret;
921}
922
923/* Try to send an RST_STREAM frame on the connection for the stream being
924 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
925 * error code unless the stream's state already is IDLE or CLOSED in which
926 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
927 * it was not yet.
928 *
929 * Returns > 0 on success or zero if nothing was done. In case of lack of room
930 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200931 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100932 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200933 */
934static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
935{
936 struct buffer *res;
937 char str[13];
938 int ret;
939
940 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100941 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200942 return 0;
943 }
944
945 res = h2_get_mbuf(h2c);
946 if (!res) {
947 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100948 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200949 return 0;
950 }
951
952 /* len: 4, type: 3, flags: none */
953 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100954
Willy Tarreau27a84c92017-10-17 08:10:17 +0200955 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100956 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200957 h2s->errcode : H2_ERR_STREAM_CLOSED);
958 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100959
Willy Tarreau27a84c92017-10-17 08:10:17 +0200960 if (unlikely(ret <= 0)) {
961 if (!ret) {
962 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100963 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200964 return 0;
965 }
966 else {
967 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
968 return 0;
969 }
970 }
971
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100972 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200973 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100974 h2c_stream_close(h2c, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100975 }
976
Willy Tarreau27a84c92017-10-17 08:10:17 +0200977 return ret;
978}
979
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100980/* try to send an empty DATA frame with the ES flag set to notify about the
981 * end of stream and match a shutdown(write). If an ES was already sent as
982 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
983 * on success or zero if nothing was done. In case of lack of room to write the
984 * message, it subscribes the requesting stream to future notifications.
985 */
986static int h2_send_empty_data_es(struct h2s *h2s)
987{
988 struct h2c *h2c = h2s->h2c;
989 struct buffer *res;
990 char str[9];
991 int ret;
992
Willy Tarreau721c9742017-11-07 11:05:42 +0100993 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100994 return 1;
995
996 if (h2c_mux_busy(h2c, h2s)) {
997 h2s->flags |= H2_SF_BLK_MBUSY;
998 return 0;
999 }
1000
1001 res = h2_get_mbuf(h2c);
1002 if (!res) {
1003 h2c->flags |= H2_CF_MUX_MALLOC;
1004 h2s->flags |= H2_SF_BLK_MROOM;
1005 return 0;
1006 }
1007
1008 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1009 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1010 write_n32(str + 5, h2s->id);
1011 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001012 if (likely(ret > 0)) {
1013 h2s->flags |= H2_SF_ES_SENT;
1014 }
1015 else if (!ret) {
1016 h2c->flags |= H2_CF_MUX_MFULL;
1017 h2s->flags |= H2_SF_BLK_MROOM;
1018 return 0;
1019 }
1020 else {
1021 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1022 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001023 }
1024 return ret;
1025}
1026
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001027/* wake the streams attached to the connection, whose id is greater than <last>,
1028 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1029 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1030 * stream's state is automatically updated accordingly.
1031 */
1032static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1033{
1034 struct eb32_node *node;
1035 struct h2s *h2s;
1036
1037 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1038 flags |= CS_FL_ERROR;
1039
1040 if (conn_xprt_read0_pending(h2c->conn))
1041 flags |= CS_FL_EOS;
1042
1043 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1044 while (node) {
1045 h2s = container_of(node, struct h2s, by_id);
1046 if (h2s->id <= last)
1047 break;
1048 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001049
1050 if (!h2s->cs) {
1051 /* this stream was already orphaned */
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001052 h2c_stream_close(h2c, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001053 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001054 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001055 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001056 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001057
1058 h2s->cs->flags |= flags;
1059 /* recv is used to force to detect CS_FL_EOS that wake()
1060 * doesn't handle in the stream int code.
1061 */
1062 h2s->cs->data_cb->recv(h2s->cs);
1063 h2s->cs->data_cb->wake(h2s->cs);
1064
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001065 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1066 h2s->st = H2_SS_ERROR;
1067 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1068 h2s->st = H2_SS_HREM;
1069 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001070 h2c_stream_close(h2c, h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001071 }
1072}
1073
Willy Tarreau3421aba2017-07-27 15:41:03 +02001074/* Increase all streams' outgoing window size by the difference passed in
1075 * argument. This is needed upon receipt of the settings frame if the initial
1076 * window size is different. The difference may be negative and the resulting
1077 * window size as well, for the time it takes to receive some window updates.
1078 */
1079static void h2c_update_all_ws(struct h2c *h2c, int diff)
1080{
1081 struct h2s *h2s;
1082 struct eb32_node *node;
1083
1084 if (!diff)
1085 return;
1086
1087 node = eb32_first(&h2c->streams_by_id);
1088 while (node) {
1089 h2s = container_of(node, struct h2s, by_id);
1090 h2s->mws += diff;
1091 node = eb32_next(node);
1092 }
1093}
1094
1095/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1096 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1097 * return an error in h2c. Described in RFC7540#6.5.
1098 */
1099static int h2c_handle_settings(struct h2c *h2c)
1100{
1101 unsigned int offset;
1102 int error;
1103
1104 if (h2c->dff & H2_F_SETTINGS_ACK) {
1105 if (h2c->dfl) {
1106 error = H2_ERR_FRAME_SIZE_ERROR;
1107 goto fail;
1108 }
1109 return 1;
1110 }
1111
1112 if (h2c->dsi != 0) {
1113 error = H2_ERR_PROTOCOL_ERROR;
1114 goto fail;
1115 }
1116
1117 if (h2c->dfl % 6) {
1118 error = H2_ERR_FRAME_SIZE_ERROR;
1119 goto fail;
1120 }
1121
1122 /* that's the limit we can process */
1123 if (h2c->dfl > global.tune.bufsize) {
1124 error = H2_ERR_FRAME_SIZE_ERROR;
1125 goto fail;
1126 }
1127
1128 /* process full frame only */
1129 if (h2c->dbuf->i < h2c->dfl)
1130 return 0;
1131
1132 /* parse the frame */
1133 for (offset = 0; offset < h2c->dfl; offset += 6) {
1134 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1135 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1136
1137 switch (type) {
1138 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1139 /* we need to update all existing streams with the
1140 * difference from the previous iws.
1141 */
1142 if (arg < 0) { // RFC7540#6.5.2
1143 error = H2_ERR_FLOW_CONTROL_ERROR;
1144 goto fail;
1145 }
1146 h2c_update_all_ws(h2c, arg - h2c->miw);
1147 h2c->miw = arg;
1148 break;
1149 case H2_SETTINGS_MAX_FRAME_SIZE:
1150 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1151 error = H2_ERR_PROTOCOL_ERROR;
1152 goto fail;
1153 }
1154 h2c->mfs = arg;
1155 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001156 case H2_SETTINGS_ENABLE_PUSH:
1157 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1158 error = H2_ERR_PROTOCOL_ERROR;
1159 goto fail;
1160 }
1161 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001162 }
1163 }
1164
1165 /* need to ACK this frame now */
1166 h2c->st0 = H2_CS_FRAME_A;
1167 return 1;
1168 fail:
1169 h2c_error(h2c, error);
1170 return 0;
1171}
1172
1173/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1174 * success or one of the h2_status values.
1175 */
1176static int h2c_ack_settings(struct h2c *h2c)
1177{
1178 struct buffer *res;
1179 char str[9];
1180 int ret = -1;
1181
1182 if (h2c_mux_busy(h2c, NULL)) {
1183 h2c->flags |= H2_CF_DEM_MBUSY;
1184 return 0;
1185 }
1186
1187 res = h2_get_mbuf(h2c);
1188 if (!res) {
1189 h2c->flags |= H2_CF_MUX_MALLOC;
1190 h2c->flags |= H2_CF_DEM_MROOM;
1191 return 0;
1192 }
1193
1194 memcpy(str,
1195 "\x00\x00\x00" /* length : 0 (no data) */
1196 "\x04" "\x01" /* type : 4, flags : ACK */
1197 "\x00\x00\x00\x00" /* stream ID */, 9);
1198
1199 ret = bo_istput(res, ist2(str, 9));
1200 if (unlikely(ret <= 0)) {
1201 if (!ret) {
1202 h2c->flags |= H2_CF_MUX_MFULL;
1203 h2c->flags |= H2_CF_DEM_MROOM;
1204 return 0;
1205 }
1206 else {
1207 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1208 return 0;
1209 }
1210 }
1211 return ret;
1212}
1213
Willy Tarreaucf68c782017-10-10 17:11:41 +02001214/* processes a PING frame and schedules an ACK if needed. The caller must pass
1215 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1216 * missing data. It may return an error in h2c.
1217 */
1218static int h2c_handle_ping(struct h2c *h2c)
1219{
1220 /* frame length must be exactly 8 */
1221 if (h2c->dfl != 8) {
1222 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1223 return 0;
1224 }
1225
1226 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001227 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001228 h2c->st0 = H2_CS_FRAME_A;
1229 return 1;
1230}
1231
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001232/* Try to send a window update for stream id <sid> and value <increment>.
1233 * Returns > 0 on success or zero on missing room or failure. It may return an
1234 * error in h2c.
1235 */
1236static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1237{
1238 struct buffer *res;
1239 char str[13];
1240 int ret = -1;
1241
1242 if (h2c_mux_busy(h2c, NULL)) {
1243 h2c->flags |= H2_CF_DEM_MBUSY;
1244 return 0;
1245 }
1246
1247 res = h2_get_mbuf(h2c);
1248 if (!res) {
1249 h2c->flags |= H2_CF_MUX_MALLOC;
1250 h2c->flags |= H2_CF_DEM_MROOM;
1251 return 0;
1252 }
1253
1254 /* length: 4, type: 8, flags: none */
1255 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1256 write_n32(str + 5, sid);
1257 write_n32(str + 9, increment);
1258
1259 ret = bo_istput(res, ist2(str, 13));
1260
1261 if (unlikely(ret <= 0)) {
1262 if (!ret) {
1263 h2c->flags |= H2_CF_MUX_MFULL;
1264 h2c->flags |= H2_CF_DEM_MROOM;
1265 return 0;
1266 }
1267 else {
1268 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1269 return 0;
1270 }
1271 }
1272 return ret;
1273}
1274
1275/* try to send pending window update for the connection. It's safe to call it
1276 * with no pending updates. Returns > 0 on success or zero on missing room or
1277 * failure. It may return an error in h2c.
1278 */
1279static int h2c_send_conn_wu(struct h2c *h2c)
1280{
1281 int ret = 1;
1282
1283 if (h2c->rcvd_c <= 0)
1284 return 1;
1285
1286 /* send WU for the connection */
1287 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1288 if (ret > 0)
1289 h2c->rcvd_c = 0;
1290
1291 return ret;
1292}
1293
1294/* try to send pending window update for the current dmux stream. It's safe to
1295 * call it with no pending updates. Returns > 0 on success or zero on missing
1296 * room or failure. It may return an error in h2c.
1297 */
1298static int h2c_send_strm_wu(struct h2c *h2c)
1299{
1300 int ret = 1;
1301
1302 if (h2c->rcvd_s <= 0)
1303 return 1;
1304
1305 /* send WU for the stream */
1306 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1307 if (ret > 0)
1308 h2c->rcvd_s = 0;
1309
1310 return ret;
1311}
1312
Willy Tarreaucf68c782017-10-10 17:11:41 +02001313/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1314 * success, 0 on missing data or one of the h2_status values.
1315 */
1316static int h2c_ack_ping(struct h2c *h2c)
1317{
1318 struct buffer *res;
1319 char str[17];
1320 int ret = -1;
1321
1322 if (h2c->dbuf->i < 8)
1323 return 0;
1324
1325 if (h2c_mux_busy(h2c, NULL)) {
1326 h2c->flags |= H2_CF_DEM_MBUSY;
1327 return 0;
1328 }
1329
1330 res = h2_get_mbuf(h2c);
1331 if (!res) {
1332 h2c->flags |= H2_CF_MUX_MALLOC;
1333 h2c->flags |= H2_CF_DEM_MROOM;
1334 return 0;
1335 }
1336
1337 memcpy(str,
1338 "\x00\x00\x08" /* length : 8 (same payload) */
1339 "\x06" "\x01" /* type : 6, flags : ACK */
1340 "\x00\x00\x00\x00" /* stream ID */, 9);
1341
1342 /* copy the original payload */
1343 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1344
1345 ret = bo_istput(res, ist2(str, 17));
1346 if (unlikely(ret <= 0)) {
1347 if (!ret) {
1348 h2c->flags |= H2_CF_MUX_MFULL;
1349 h2c->flags |= H2_CF_DEM_MROOM;
1350 return 0;
1351 }
1352 else {
1353 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1354 return 0;
1355 }
1356 }
1357 return ret;
1358}
1359
Willy Tarreau26f95952017-07-27 17:18:30 +02001360/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1361 * Returns > 0 on success or zero on missing data. It may return an error in
1362 * h2c or h2s. Described in RFC7540#6.9.
1363 */
1364static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1365{
1366 int32_t inc;
1367 int error;
1368
1369 if (h2c->dfl != 4) {
1370 error = H2_ERR_FRAME_SIZE_ERROR;
1371 goto conn_err;
1372 }
1373
1374 /* process full frame only */
1375 if (h2c->dbuf->i < h2c->dfl)
1376 return 0;
1377
1378 inc = h2_get_n32(h2c->dbuf, 0);
1379
1380 if (h2c->dsi != 0) {
1381 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001382
1383 /* it's not an error to receive WU on a closed stream */
1384 if (h2s->st == H2_SS_CLOSED)
1385 return 1;
1386
1387 if (!inc) {
1388 error = H2_ERR_PROTOCOL_ERROR;
1389 goto strm_err;
1390 }
1391
1392 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1393 error = H2_ERR_FLOW_CONTROL_ERROR;
1394 goto strm_err;
1395 }
1396
1397 h2s->mws += inc;
1398 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1399 h2s->flags &= ~H2_SF_BLK_SFCTL;
1400 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1401 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1402 /* This stream wanted to send but could not due to its
1403 * own flow control. We can put it back into the send
1404 * list now, it will be handled upon next send() call.
1405 */
1406 LIST_ADDQ(&h2c->send_list, &h2s->list);
1407 }
1408 }
1409 }
1410 else {
1411 /* connection window update */
1412 if (!inc) {
1413 error = H2_ERR_PROTOCOL_ERROR;
1414 goto conn_err;
1415 }
1416
1417 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1418 error = H2_ERR_FLOW_CONTROL_ERROR;
1419 goto conn_err;
1420 }
1421
1422 h2c->mws += inc;
1423 }
1424
1425 return 1;
1426
1427 conn_err:
1428 h2c_error(h2c, error);
1429 return 0;
1430
1431 strm_err:
1432 if (h2s) {
1433 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001434 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001435 }
1436 else
1437 h2c_error(h2c, error);
1438 return 0;
1439}
1440
Willy Tarreaue96b0922017-10-30 00:28:29 +01001441/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1442 * the last ID. Returns > 0 on success or zero on missing data. It may return
1443 * an error in h2c. Described in RFC7540#6.8.
1444 */
1445static int h2c_handle_goaway(struct h2c *h2c)
1446{
1447 int error;
1448 int last;
1449
1450 if (h2c->dsi != 0) {
1451 error = H2_ERR_PROTOCOL_ERROR;
1452 goto conn_err;
1453 }
1454
1455 if (h2c->dfl < 8) {
1456 error = H2_ERR_FRAME_SIZE_ERROR;
1457 goto conn_err;
1458 }
1459
1460 /* process full frame only */
1461 if (h2c->dbuf->i < h2c->dfl)
1462 return 0;
1463
1464 last = h2_get_n32(h2c->dbuf, 0);
1465 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1466 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001467 if (h2c->last_sid < 0)
1468 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001469 return 1;
1470
1471 conn_err:
1472 h2c_error(h2c, error);
1473 return 0;
1474}
1475
Willy Tarreau92153fc2017-12-03 19:46:19 +01001476/* processes a PRIORITY frame, and either skips it or rejects if it is
1477 * invalid. Returns > 0 on success or zero on missing data. It may return
1478 * an error in h2c. Described in RFC7540#6.3.
1479 */
1480static int h2c_handle_priority(struct h2c *h2c)
1481{
1482 int error;
1483
1484 if (h2c->dsi == 0) {
1485 error = H2_ERR_PROTOCOL_ERROR;
1486 goto conn_err;
1487 }
1488
1489 if (h2c->dfl != 5) {
1490 error = H2_ERR_FRAME_SIZE_ERROR;
1491 goto conn_err;
1492 }
1493
1494 /* process full frame only */
1495 if (h2c->dbuf->i < h2c->dfl)
1496 return 0;
1497
1498 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1499 /* 7540#5.3 : can't depend on itself */
1500 error = H2_ERR_PROTOCOL_ERROR;
1501 goto conn_err;
1502 }
1503 return 1;
1504
1505 conn_err:
1506 h2c_error(h2c, error);
1507 return 0;
1508}
1509
Willy Tarreaucd234e92017-08-18 10:59:39 +02001510/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1511 * Returns > 0 on success or zero on missing data. It may return an error in
1512 * h2c. Described in RFC7540#6.4.
1513 */
1514static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1515{
1516 int error;
1517
1518 if (h2c->dsi == 0) {
1519 error = H2_ERR_PROTOCOL_ERROR;
1520 goto conn_err;
1521 }
1522
Willy Tarreaucd234e92017-08-18 10:59:39 +02001523 if (h2c->dfl != 4) {
1524 error = H2_ERR_FRAME_SIZE_ERROR;
1525 goto conn_err;
1526 }
1527
1528 /* process full frame only */
1529 if (h2c->dbuf->i < h2c->dfl)
1530 return 0;
1531
1532 /* late RST, already handled */
1533 if (h2s->st == H2_SS_CLOSED)
1534 return 1;
1535
1536 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001537 h2c_stream_close(h2c, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001538
1539 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001540 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001541 /* recv is used to force to detect CS_FL_EOS that wake()
1542 * doesn't handle in the stream-int code.
1543 */
1544 h2s->cs->data_cb->recv(h2s->cs);
1545 h2s->cs->data_cb->wake(h2s->cs);
1546 }
1547
1548 h2s->flags |= H2_SF_RST_RCVD;
1549 return 1;
1550
1551 conn_err:
1552 h2c_error(h2c, error);
1553 return 0;
1554}
1555
Willy Tarreau13278b42017-10-13 19:23:14 +02001556/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1557 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1558 * errors here are reported as connection errors since it's impossible to
1559 * recover from such errors after the compression context has been altered.
1560 */
1561static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1562{
1563 int error;
1564
1565 if (!h2c->dfl) {
1566 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1567 goto strm_err;
1568 }
1569
1570 if (!h2c->dbuf->size)
1571 return 0; // empty buffer
1572
1573 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1574 return 0; // incomplete frame
1575
1576 /* now either the frame is complete or the buffer is complete */
1577 if (h2s->st != H2_SS_IDLE) {
1578 /* FIXME: stream already exists, this is only allowed for
1579 * trailers (not supported for now).
1580 */
1581 error = H2_ERR_PROTOCOL_ERROR;
1582 goto conn_err;
1583 }
1584 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1585 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1586 error = H2_ERR_PROTOCOL_ERROR;
1587 goto conn_err;
1588 }
1589
1590 h2s = h2c_stream_new(h2c, h2c->dsi);
1591 if (!h2s) {
1592 error = H2_ERR_INTERNAL_ERROR;
1593 goto conn_err;
1594 }
1595
1596 h2s->st = H2_SS_OPEN;
1597 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1598 h2s->st = H2_SS_HREM;
1599 h2s->flags |= H2_SF_ES_RCVD;
1600 }
1601
1602 /* call the upper layers to process the frame, then let the upper layer
1603 * notify the stream about any change.
1604 */
1605 h2s->cs->data_cb->recv(h2s->cs);
1606
1607 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1608 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1609 error = H2_ERR_INTERNAL_ERROR;
1610 goto conn_err;
1611 }
1612
Willy Tarreau8f650c32017-11-21 19:36:21 +01001613 if (h2c->st0 >= H2_CS_ERROR)
1614 return 0;
1615
Willy Tarreau721c9742017-11-07 11:05:42 +01001616 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001617 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001618 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001619 }
1620 else {
1621 /* update the max stream ID if the request is being processed */
1622 if (h2s->id > h2c->max_id)
1623 h2c->max_id = h2s->id;
1624 }
1625
1626 return 1;
1627
1628 conn_err:
1629 h2c_error(h2c, error);
1630 return 0;
1631
1632 strm_err:
1633 if (h2s) {
1634 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001635 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001636 }
1637 else
1638 h2c_error(h2c, error);
1639 return 0;
1640}
1641
Willy Tarreau454f9052017-10-26 19:40:35 +02001642/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1643 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1644 */
1645static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1646{
1647 int error;
1648
1649 /* note that empty DATA frames are perfectly valid and sometimes used
1650 * to signal an end of stream (with the ES flag).
1651 */
1652
1653 if (!h2c->dbuf->size && h2c->dfl)
1654 return 0; // empty buffer
1655
1656 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1657 return 0; // incomplete frame
1658
1659 /* now either the frame is complete or the buffer is complete */
1660
1661 if (!h2c->dsi) {
1662 /* RFC7540#6.1 */
1663 error = H2_ERR_PROTOCOL_ERROR;
1664 goto conn_err;
1665 }
1666
1667 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1668 /* RFC7540#6.1 */
1669 error = H2_ERR_STREAM_CLOSED;
1670 goto strm_err;
1671 }
1672
Willy Tarreau454f9052017-10-26 19:40:35 +02001673 /* call the upper layers to process the frame, then let the upper layer
1674 * notify the stream about any change.
1675 */
1676 if (!h2s->cs) {
1677 error = H2_ERR_STREAM_CLOSED;
1678 goto strm_err;
1679 }
1680
1681 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001682
Willy Tarreau454f9052017-10-26 19:40:35 +02001683 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1684 /* cs has just been destroyed, we have to kill h2s. */
1685 error = H2_ERR_STREAM_CLOSED;
1686 goto strm_err;
1687 }
1688
Willy Tarreau8f650c32017-11-21 19:36:21 +01001689 if (h2c->st0 >= H2_CS_ERROR)
1690 return 0;
1691
Willy Tarreau721c9742017-11-07 11:05:42 +01001692 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001693 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001694 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001695 }
1696
1697 /* check for completion : the callee will change this to FRAME_A or
1698 * FRAME_H once done.
1699 */
1700 if (h2c->st0 == H2_CS_FRAME_P)
1701 return 0;
1702
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001703
1704 /* last frame */
1705 if (h2c->dff & H2_F_DATA_END_STREAM) {
1706 h2s->st = H2_SS_HREM;
1707 h2s->flags |= H2_SF_ES_RCVD;
1708 }
1709
Willy Tarreau454f9052017-10-26 19:40:35 +02001710 return 1;
1711
1712 conn_err:
1713 h2c_error(h2c, error);
1714 return 0;
1715
1716 strm_err:
1717 if (h2s) {
1718 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001719 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001720 }
1721 else
1722 h2c_error(h2c, error);
1723 return 0;
1724}
1725
Willy Tarreaubc933932017-10-09 16:21:43 +02001726/* process Rx frames to be demultiplexed */
1727static void h2_process_demux(struct h2c *h2c)
1728{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001729 struct h2s *h2s;
1730
Willy Tarreau081d4722017-05-16 21:51:05 +02001731 if (h2c->st0 >= H2_CS_ERROR)
1732 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001733
1734 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1735 if (h2c->st0 == H2_CS_PREFACE) {
1736 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1737 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1738 if (h2c->st0 == H2_CS_ERROR)
1739 h2c->st0 = H2_CS_ERROR2;
1740 goto fail;
1741 }
1742
1743 h2c->max_id = 0;
1744 h2c->st0 = H2_CS_SETTINGS1;
1745 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001746
1747 if (h2c->st0 == H2_CS_SETTINGS1) {
1748 struct h2_fh hdr;
1749
1750 /* ensure that what is pending is a valid SETTINGS frame
1751 * without an ACK.
1752 */
1753 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1754 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1755 if (h2c->st0 == H2_CS_ERROR)
1756 h2c->st0 = H2_CS_ERROR2;
1757 goto fail;
1758 }
1759
1760 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1761 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1762 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1763 h2c->st0 = H2_CS_ERROR2;
1764 goto fail;
1765 }
1766
1767 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1768 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1769 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1770 h2c->st0 = H2_CS_ERROR2;
1771 goto fail;
1772 }
1773
1774 /* that's OK, switch to FRAME_P to process it */
1775 h2c->dfl = hdr.len;
1776 h2c->dsi = hdr.sid;
1777 h2c->dft = hdr.ft;
1778 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001779 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001780 h2c->st0 = H2_CS_FRAME_P;
1781 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001782 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001783
1784 /* process as many incoming frames as possible below */
1785 while (h2c->dbuf->i) {
1786 int ret = 0;
1787
1788 if (h2c->st0 >= H2_CS_ERROR)
1789 break;
1790
1791 if (h2c->st0 == H2_CS_FRAME_H) {
1792 struct h2_fh hdr;
1793
1794 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1795 break;
1796
1797 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1798 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1799 h2c->st0 = H2_CS_ERROR;
1800 break;
1801 }
1802
1803 h2c->dfl = hdr.len;
1804 h2c->dsi = hdr.sid;
1805 h2c->dft = hdr.ft;
1806 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001807 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001808 h2c->st0 = H2_CS_FRAME_P;
1809 h2_skip_frame_hdr(h2c->dbuf);
1810 }
1811
1812 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001813 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001814
Willy Tarreaud7901432017-12-29 11:34:40 +01001815 if (h2c->st0 == H2_CS_FRAME_E)
1816 goto strm_err;
1817
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001818 if (h2s->st == H2_SS_IDLE &&
1819 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1820 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1821 * this state MUST be treated as a connection error
1822 */
1823 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1824 h2c->st0 = H2_CS_ERROR;
1825 break;
1826 }
1827
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001828 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1829 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1830 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1831 * this state MUST be treated as a stream error
1832 */
1833 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001834 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001835 goto strm_err;
1836 }
1837
Willy Tarreauab837502017-12-27 15:07:30 +01001838 /* Below the management of frames received in closed state is a
1839 * bit hackish because the spec makes strong differences between
1840 * streams closed by receiving RST, sending RST, and seeing ES
1841 * in both directions. In addition to this, the creation of a
1842 * new stream reusing the identifier of a closed one will be
1843 * detected here. Given that we cannot keep track of all closed
1844 * streams forever, we consider that unknown closed streams were
1845 * closed on RST received, which allows us to respond with an
1846 * RST without breaking the connection (eg: to abort a transfer).
1847 * Some frames have to be silently ignored as well.
1848 */
1849 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1850 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1851 /* #5.1.1: The identifier of a newly
1852 * established stream MUST be numerically
1853 * greater than all streams that the initiating
1854 * endpoint has opened or reserved. This
1855 * governs streams that are opened using a
1856 * HEADERS frame and streams that are reserved
1857 * using PUSH_PROMISE. An endpoint that
1858 * receives an unexpected stream identifier
1859 * MUST respond with a connection error.
1860 */
1861 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1862 goto strm_err;
1863 }
1864
1865 if (h2s->flags & H2_SF_RST_RCVD) {
1866 /* RFC7540#5.1:closed: an endpoint that
1867 * receives any frame other than PRIORITY after
1868 * receiving a RST_STREAM MUST treat that as a
1869 * stream error of type STREAM_CLOSED.
1870 *
1871 * Note that old streams fall into this category
1872 * and will lead to an RST being sent.
1873 */
1874 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1875 h2c->st0 = H2_CS_FRAME_E;
1876 goto strm_err;
1877 }
1878
1879 /* RFC7540#5.1:closed: if this state is reached as a
1880 * result of sending a RST_STREAM frame, the peer that
1881 * receives the RST_STREAM might have already sent
1882 * frames on the stream that cannot be withdrawn. An
1883 * endpoint MUST ignore frames that it receives on
1884 * closed streams after it has sent a RST_STREAM
1885 * frame. An endpoint MAY choose to limit the period
1886 * over which it ignores frames and treat frames that
1887 * arrive after this time as being in error.
1888 */
1889 if (!(h2s->flags & H2_SF_RST_SENT)) {
1890 /* RFC7540#5.1:closed: any frame other than
1891 * PRIO/WU/RST in this state MUST be treated as
1892 * a connection error
1893 */
1894 if (h2c->dft != H2_FT_RST_STREAM &&
1895 h2c->dft != H2_FT_PRIORITY &&
1896 h2c->dft != H2_FT_WINDOW_UPDATE) {
1897 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1898 goto strm_err;
1899 }
1900 }
1901 }
1902
Willy Tarreauc0da1962017-10-30 18:38:00 +01001903#if 0
1904 // problem below: it is not possible to completely ignore such
1905 // streams as we need to maintain the compression state as well
1906 // and for this we need to completely process these frames (eg:
1907 // HEADERS frames) as well as counting DATA frames to emit
1908 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1909 // This is a typical case of layer violation where the
1910 // transported contents are critical to the connection's
1911 // validity and must be ignored at the same time :-(
1912
1913 /* graceful shutdown, ignore streams whose ID is higher than
1914 * the one advertised in GOAWAY. RFC7540#6.8.
1915 */
1916 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1917 ret = MIN(h2c->dbuf->i, h2c->dfl);
1918 bi_del(h2c->dbuf, ret);
1919 h2c->dfl -= ret;
1920 ret = h2c->dfl == 0;
1921 goto strm_err;
1922 }
1923#endif
1924
Willy Tarreau7e98c052017-10-10 15:56:59 +02001925 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001926 case H2_FT_SETTINGS:
1927 if (h2c->st0 == H2_CS_FRAME_P)
1928 ret = h2c_handle_settings(h2c);
1929
1930 if (h2c->st0 == H2_CS_FRAME_A)
1931 ret = h2c_ack_settings(h2c);
1932 break;
1933
Willy Tarreaucf68c782017-10-10 17:11:41 +02001934 case H2_FT_PING:
1935 if (h2c->st0 == H2_CS_FRAME_P)
1936 ret = h2c_handle_ping(h2c);
1937
1938 if (h2c->st0 == H2_CS_FRAME_A)
1939 ret = h2c_ack_ping(h2c);
1940 break;
1941
Willy Tarreau26f95952017-07-27 17:18:30 +02001942 case H2_FT_WINDOW_UPDATE:
1943 if (h2c->st0 == H2_CS_FRAME_P)
1944 ret = h2c_handle_window_update(h2c, h2s);
1945 break;
1946
Willy Tarreau61290ec2017-10-17 08:19:21 +02001947 case H2_FT_CONTINUATION:
1948 /* we currently don't support CONTINUATION frames since
1949 * we have nowhere to store the partial HEADERS frame.
1950 * Let's abort the stream on an INTERNAL_ERROR here.
1951 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001952 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001953 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001954 h2c->st0 = H2_CS_FRAME_E;
1955 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001956 break;
1957
Willy Tarreau13278b42017-10-13 19:23:14 +02001958 case H2_FT_HEADERS:
1959 if (h2c->st0 == H2_CS_FRAME_P)
1960 ret = h2c_frt_handle_headers(h2c, h2s);
1961 break;
1962
Willy Tarreau454f9052017-10-26 19:40:35 +02001963 case H2_FT_DATA:
1964 if (h2c->st0 == H2_CS_FRAME_P)
1965 ret = h2c_frt_handle_data(h2c, h2s);
1966
1967 if (h2c->st0 == H2_CS_FRAME_A)
1968 ret = h2c_send_strm_wu(h2c);
1969 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001970
Willy Tarreau92153fc2017-12-03 19:46:19 +01001971 case H2_FT_PRIORITY:
1972 if (h2c->st0 == H2_CS_FRAME_P)
1973 ret = h2c_handle_priority(h2c);
1974 break;
1975
Willy Tarreaucd234e92017-08-18 10:59:39 +02001976 case H2_FT_RST_STREAM:
1977 if (h2c->st0 == H2_CS_FRAME_P)
1978 ret = h2c_handle_rst_stream(h2c, h2s);
1979 break;
1980
Willy Tarreaue96b0922017-10-30 00:28:29 +01001981 case H2_FT_GOAWAY:
1982 if (h2c->st0 == H2_CS_FRAME_P)
1983 ret = h2c_handle_goaway(h2c);
1984 break;
1985
Willy Tarreau1c661982017-10-30 13:52:01 +01001986 case H2_FT_PUSH_PROMISE:
1987 /* not permitted here, RFC7540#5.1 */
1988 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001989 break;
1990
1991 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001992 default:
1993 /* drop frames that we ignore. They may be larger than
1994 * the buffer so we drain all of their contents until
1995 * we reach the end.
1996 */
1997 ret = MIN(h2c->dbuf->i, h2c->dfl);
1998 bi_del(h2c->dbuf, ret);
1999 h2c->dfl -= ret;
2000 ret = h2c->dfl == 0;
2001 }
2002
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002003 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002004 /* We may have to send an RST if not done yet */
2005 if (h2s->st == H2_SS_ERROR)
2006 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002007
Willy Tarreaua20a5192017-12-27 11:02:06 +01002008 if (h2c->st0 == H2_CS_FRAME_E)
2009 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002010
Willy Tarreau7e98c052017-10-10 15:56:59 +02002011 /* error or missing data condition met above ? */
2012 if (ret <= 0)
2013 break;
2014
2015 if (h2c->st0 != H2_CS_FRAME_H) {
2016 bi_del(h2c->dbuf, h2c->dfl);
2017 h2c->st0 = H2_CS_FRAME_H;
2018 }
2019 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002020
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002021 if (h2c->rcvd_c > 0 &&
2022 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2023 h2c_send_conn_wu(h2c);
2024
Willy Tarreau52eed752017-09-22 15:05:09 +02002025 fail:
2026 /* we can go here on missing data, blocked response or error */
2027 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002028}
2029
2030/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2031 * the end.
2032 */
2033static int h2_process_mux(struct h2c *h2c)
2034{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002035 struct h2s *h2s, *h2s_back;
2036
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002037 /* start by sending possibly pending window updates */
2038 if (h2c->rcvd_c > 0 &&
2039 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2040 h2c_send_conn_wu(h2c) < 0)
2041 goto fail;
2042
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002043 /* First we always process the flow control list because the streams
2044 * waiting there were already elected for immediate emission but were
2045 * blocked just on this.
2046 */
2047
2048 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2049 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2050 h2c->st0 >= H2_CS_ERROR)
2051 break;
2052
2053 /* In theory it's possible that h2s->cs == NULL here :
2054 * - client sends crap that causes a parse error
2055 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2056 * - RST_STREAM cannot be emitted because mux is busy/full
2057 * - stream gets notified, detaches and quits
2058 * - mux buffer gets ready and wakes pending streams up
2059 * - bam!
2060 */
2061 h2s->flags &= ~H2_SF_BLK_ANY;
2062
2063 if (h2s->cs) {
2064 h2s->cs->data_cb->send(h2s->cs);
2065 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002066 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002067 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002068 }
2069
2070 /* depending on callee's blocking reasons, we may queue in send
2071 * list or completely dequeue.
2072 */
2073 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2074 if (h2s->flags & H2_SF_BLK_ANY) {
2075 LIST_DEL(&h2s->list);
2076 LIST_ADDQ(&h2c->send_list, &h2s->list);
2077 }
2078 else {
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 Tarreau91bfdd72017-12-14 12:00:14 +01002085 h2c_stream_close(h2c, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002086 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002087 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002088 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002089 }
2090 }
2091 }
2092
2093 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2094 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2095 break;
2096
2097 /* In theory it's possible that h2s->cs == NULL here :
2098 * - client sends crap that causes a parse error
2099 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2100 * - RST_STREAM cannot be emitted because mux is busy/full
2101 * - stream gets notified, detaches and quits
2102 * - mux buffer gets ready and wakes pending streams up
2103 * - bam!
2104 */
2105 h2s->flags &= ~H2_SF_BLK_ANY;
2106
2107 if (h2s->cs) {
2108 h2s->cs->data_cb->send(h2s->cs);
2109 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002110 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002111 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002112 }
2113 /* depending on callee's blocking reasons, we may queue in fctl
2114 * list or completely dequeue.
2115 */
2116 if (h2s->flags & H2_SF_BLK_MFCTL) {
2117 /* stream hit the connection's flow control */
2118 LIST_DEL(&h2s->list);
2119 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2120 }
2121 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2122 LIST_DEL(&h2s->list);
2123 LIST_INIT(&h2s->list);
2124 if (h2s->cs)
2125 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002126 else {
2127 /* just sent the last frame for this orphaned stream */
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002128 h2c_stream_close(h2c, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002129 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002130 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002131 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002132 }
2133 }
2134
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002135 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002136 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002137 if (h2c->st0 == H2_CS_ERROR) {
2138 if (h2c->max_id >= 0) {
2139 h2c_send_goaway_error(h2c, NULL);
2140 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2141 return 0;
2142 }
2143
2144 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2145 }
2146 return 1;
2147 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002148 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002149}
2150
Willy Tarreau71681172017-10-23 14:39:06 +02002151
Willy Tarreau62f52692017-10-08 23:01:42 +02002152/*********************************************************/
2153/* functions below are I/O callbacks from the connection */
2154/*********************************************************/
2155
2156/* callback called on recv event by the connection handler */
2157static void h2_recv(struct connection *conn)
2158{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002159 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002160 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002161 int max;
2162
Willy Tarreau315d8072017-12-10 22:17:57 +01002163 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002164 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002165
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002166 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002167 if (!buf) {
2168 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002169 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002170 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002171
Willy Tarreaua2af5122017-10-09 11:56:46 +02002172 /* note: buf->o == 0 */
2173 max = buf->size - buf->i;
Willy Tarreau315d8072017-12-10 22:17:57 +01002174 if (max)
2175 conn->xprt->rcv_buf(conn, buf, max);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002176
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002177 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002178 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002179 return;
2180 }
2181
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002182 if (buf->i == buf->size)
2183 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002184 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002185}
2186
2187/* callback called on send event by the connection handler */
2188static void h2_send(struct connection *conn)
2189{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002190 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002191 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002192
2193 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002194 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002195
2196 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2197 /* a handshake was requested */
2198 return;
2199 }
2200
Willy Tarreaubc933932017-10-09 16:21:43 +02002201 /* This loop is quite simple : it tries to fill as much as it can from
2202 * pending streams into the existing buffer until it's reportedly full
2203 * or the end of send requests is reached. Then it tries to send this
2204 * buffer's contents out, marks it not full if at least one byte could
2205 * be sent, and tries again.
2206 *
2207 * The snd_buf() function normally takes a "flags" argument which may
2208 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2209 * data immediately comes and CO_SFL_STREAMER to indicate that the
2210 * connection is streaming lots of data (used to increase TLS record
2211 * size at the expense of latency). The former can be sent any time
2212 * there's a buffer full flag, as it indicates at least one stream
2213 * attempted to send and failed so there are pending data. An
2214 * alternative would be to set it as long as there's an active stream
2215 * but that would be problematic for ACKs until we have an absolute
2216 * guarantee that all waiters have at least one byte to send. The
2217 * latter should possibly not be set for now.
2218 */
2219
2220 done = 0;
2221 while (!done) {
2222 unsigned int flags = 0;
2223
2224 /* fill as much as we can into the current buffer */
2225 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2226 done = h2_process_mux(h2c);
2227
2228 if (conn->flags & CO_FL_ERROR)
2229 break;
2230
2231 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2232 flags |= CO_SFL_MSG_MORE;
2233
Willy Tarreau319994a2017-11-07 11:03:56 +01002234 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002235 break;
2236
2237 /* wrote at least one byte, the buffer is not full anymore */
2238 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2239 }
2240
Willy Tarreaua2af5122017-10-09 11:56:46 +02002241 if (conn->flags & CO_FL_SOCK_WR_SH) {
2242 /* output closed, nothing to send, clear the buffer to release it */
2243 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002244 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002245}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002246
Willy Tarreau62f52692017-10-08 23:01:42 +02002247/* callback called on any event by the connection handler.
2248 * It applies changes and returns zero, or < 0 if it wants immediate
2249 * destruction of the connection (which normally doesn not happen in h2).
2250 */
2251static int h2_wake(struct connection *conn)
2252{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002253 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002254 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002255
Willy Tarreaud13bf272017-12-14 10:34:52 +01002256 if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
2257 h2_process_demux(h2c);
2258
2259 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
2260 h2c->dbuf->i = 0;
2261
2262 if (h2c->dbuf->i != h2c->dbuf->size)
2263 h2c->flags &= ~H2_CF_DEM_DFULL;
2264 }
2265
Willy Tarreau8ec14062017-12-30 18:08:13 +01002266 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2267 /* frontend is stopping, reload likely in progress, let's try
2268 * to announce a graceful shutdown if not yet done. We don't
2269 * care if it fails, it will be tried again later.
2270 */
2271 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2272 if (h2c->last_sid < 0)
2273 h2c->last_sid = (1U << 31) - 1;
2274 h2c_send_goaway_error(h2c, NULL);
2275 }
2276 }
2277
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002278 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002279 * If we received early data, and the handshake is done, wake
2280 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002281 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002282 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2283 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2284 struct eb32_node *node;
2285 struct h2s *h2s;
2286
2287 h2c->flags |= H2_CF_WAIT_FOR_HS;
2288 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2289
2290 while (node) {
2291 h2s = container_of(node, struct h2s, by_id);
2292 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2293 h2s->cs->data_cb->wake(h2s->cs);
2294 node = eb32_next(node);
2295 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002296 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002297
Willy Tarreau26bd7612017-10-09 16:47:04 +02002298 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002299 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2300 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2301 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002302 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002303
2304 if (eb_is_empty(&h2c->streams_by_id)) {
2305 /* no more stream, kill the connection now */
2306 h2_release(conn);
2307 return -1;
2308 }
2309 else {
2310 /* some streams still there, we need to signal them all and
2311 * wait for their departure.
2312 */
2313 __conn_xprt_stop_recv(conn);
2314 __conn_xprt_stop_send(conn);
2315 return 0;
2316 }
2317 }
2318
2319 if (!h2c->dbuf->i)
2320 h2_release_dbuf(h2c);
2321
2322 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002323 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002324 __conn_xprt_stop_recv(conn);
2325 }
2326 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002327 __conn_xprt_want_recv(conn);
2328 }
2329
2330 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002331 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2332 (h2c->st0 == H2_CS_ERROR ||
2333 h2c->mbuf->o ||
2334 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2335 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002336 __conn_xprt_want_send(conn);
2337 }
2338 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002339 h2_release_mbuf(h2c);
2340 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002341 }
2342
Willy Tarreau3f133572017-10-31 19:21:06 +01002343 if (h2c->task) {
2344 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002345 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002346 task_queue(h2c->task);
2347 }
2348 else
2349 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002350 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002351 return 0;
2352}
2353
Willy Tarreauea392822017-10-31 10:02:25 +01002354/* Connection timeout management. The principle is that if there's no receipt
2355 * nor sending for a certain amount of time, the connection is closed. If the
2356 * MUX buffer still has lying data or is not allocatable, the connection is
2357 * immediately killed. If it's allocatable and empty, we attempt to send a
2358 * GOAWAY frame.
2359 */
2360static struct task *h2_timeout_task(struct task *t)
2361{
2362 struct h2c *h2c = t->context;
2363 int expired = tick_is_expired(t->expire, now_ms);
2364
2365 if (!expired)
2366 return t;
2367
2368 h2c_error(h2c, H2_ERR_NO_ERROR);
2369 h2_wake_some_streams(h2c, 0, 0);
2370
2371 if (h2c->mbuf->o) {
2372 /* don't even try to send a GOAWAY, the buffer is stuck */
2373 h2c->flags |= H2_CF_GOAWAY_FAILED;
2374 }
2375
2376 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002377 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002378 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2379 h2c->flags |= H2_CF_GOAWAY_FAILED;
2380
2381 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2382 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2383
2384 if (!eb_is_empty(&h2c->streams_by_id))
2385 goto wait;
2386
2387 h2_release(h2c->conn);
2388 return NULL;
2389
2390 wait:
2391 /* the streams have been notified, we must let them finish and close */
2392 h2c->task = NULL;
2393 task_delete(t);
2394 task_free(t);
2395 return NULL;
2396}
2397
2398
Willy Tarreau62f52692017-10-08 23:01:42 +02002399/*******************************************/
2400/* functions below are used by the streams */
2401/*******************************************/
2402
2403/*
2404 * Attach a new stream to a connection
2405 * (Used for outgoing connections)
2406 */
2407static struct conn_stream *h2_attach(struct connection *conn)
2408{
2409 return NULL;
2410}
2411
2412/* callback used to update the mux's polling flags after changing a cs' status.
2413 * The caller (cs_update_mux_polling) will take care of propagating any changes
2414 * to the transport layer.
2415 */
2416static void h2_update_poll(struct conn_stream *cs)
2417{
Willy Tarreau1d393222017-10-17 10:26:19 +02002418 struct h2s *h2s = cs->ctx;
2419
2420 if (!h2s)
2421 return;
2422
Willy Tarreaud7739c82017-10-30 15:38:23 +01002423 /* we may unblock a blocked read */
2424
Willy Tarreau315d8072017-12-10 22:17:57 +01002425 if (cs->flags & CS_FL_DATA_RD_ENA) {
2426 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002427 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002428 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002429 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002430 conn_xprt_want_send(cs->conn);
2431 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002432 }
2433
Willy Tarreau1d393222017-10-17 10:26:19 +02002434 /* Note: the stream and stream-int code doesn't allow us to perform a
2435 * synchronous send() here unfortunately, because this code is called
2436 * as si_update() from the process_stream() context. This means that
2437 * we have to queue the current cs and defer its processing after the
2438 * connection's cs list is processed anyway.
2439 */
2440
2441 if (cs->flags & CS_FL_DATA_WR_ENA) {
2442 if (LIST_ISEMPTY(&h2s->list)) {
2443 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2444 !h2s->h2c->mbuf->o && // not yet subscribed
2445 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2446 conn_xprt_want_send(cs->conn);
2447 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2448 }
2449 }
2450 else if (!LIST_ISEMPTY(&h2s->list)) {
2451 LIST_DEL(&h2s->list);
2452 LIST_INIT(&h2s->list);
2453 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2454 }
2455
2456 /* this can happen from within si_chk_snd() */
2457 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2458 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002459}
2460
2461/*
2462 * Detach the stream from the connection and possibly release the connection.
2463 */
2464static void h2_detach(struct conn_stream *cs)
2465{
Willy Tarreau60935142017-10-16 18:11:19 +02002466 struct h2s *h2s = cs->ctx;
2467 struct h2c *h2c;
2468
2469 cs->ctx = NULL;
2470 if (!h2s)
2471 return;
2472
2473 h2c = h2s->h2c;
2474 h2s->cs = NULL;
2475
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002476 /* this stream may be blocked waiting for some data to leave (possibly
2477 * an ES or RST frame), so orphan it in this case.
2478 */
2479 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2480 return;
2481
Willy Tarreau541dd822017-11-23 18:12:50 +01002482 /* the stream could be in the send list */
2483 LIST_DEL(&h2s->list);
2484
Willy Tarreau45f752e2017-10-30 15:44:59 +01002485 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2486 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2487 /* unblock the connection if it was blocked on this
2488 * stream.
2489 */
2490 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2491 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2492 conn_xprt_want_recv(cs->conn);
2493 conn_xprt_want_send(cs->conn);
2494 }
2495
Willy Tarreau60935142017-10-16 18:11:19 +02002496 if (h2s->by_id.node.leaf_p) {
2497 /* h2s still attached to the h2c */
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002498 h2c_stream_close(h2c, h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002499 eb32_delete(&h2s->by_id);
2500
2501 /* We don't want to close right now unless we're removing the
2502 * last stream, and either the connection is in error, or it
2503 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002504 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002505 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002506 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2507 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002508 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002509 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2510 (conn_xprt_read0_pending(h2c->conn) ||
2511 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002512 /* no more stream will come, kill it now */
2513 h2_release(h2c->conn);
2514 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002515 else if (h2c->task) {
2516 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002517 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002518 task_queue(h2c->task);
2519 }
2520 else
2521 h2c->task->expire = TICK_ETERNITY;
2522 }
Willy Tarreau60935142017-10-16 18:11:19 +02002523 }
Willy Tarreaubafbe012017-11-24 17:34:44 +01002524 pool_free(pool_head_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002525}
2526
2527static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2528{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002529 struct h2s *h2s = cs->ctx;
2530
2531 if (!mode)
2532 return;
2533
Willy Tarreau721c9742017-11-07 11:05:42 +01002534 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002535 return;
2536
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002537 /* if no outgoing data was seen on this stream, it means it was
2538 * closed with a "tcp-request content" rule that is normally
2539 * used to kill the connection ASAP (eg: limit abuse). In this
2540 * case we send a goaway to close the connection.
2541 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002542 if (!(h2s->flags & H2_SF_RST_SENT) &&
2543 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2544 return;
2545
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002546 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2547 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2548 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2549 return;
2550
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002551 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2552 conn_xprt_want_send(cs->conn);
2553
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002554 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002555}
2556
2557static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2558{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002559 struct h2s *h2s = cs->ctx;
2560
Willy Tarreau721c9742017-11-07 11:05:42 +01002561 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002562 return;
2563
Willy Tarreau67434202017-11-06 20:20:51 +01002564 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002565 /* we can cleanly close using an empty data frame only after headers */
2566
2567 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2568 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002569 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002570
2571 if (h2s->st == H2_SS_HREM)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002572 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002573 else
2574 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002575 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002576 /* if no outgoing data was seen on this stream, it means it was
2577 * closed with a "tcp-request content" rule that is normally
2578 * used to kill the connection ASAP (eg: limit abuse). In this
2579 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002580 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002581 if (!(h2s->flags & H2_SF_RST_SENT) &&
2582 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2583 return;
2584
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002585 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2586 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002587 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2588 return;
2589
Willy Tarreau91bfdd72017-12-14 12:00:14 +01002590 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002591 }
2592
2593 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2594 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002595}
2596
Willy Tarreau13278b42017-10-13 19:23:14 +02002597/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2598 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2599 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002600 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002601 */
2602static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2603{
2604 struct h2c *h2c = h2s->h2c;
2605 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002606 struct chunk *tmp = get_trash_chunk();
2607 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002608 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002609 int flen = h2c->dfl;
2610 int outlen = 0;
2611 int wrap;
2612 int try;
2613
2614 if (!h2c->dfl) {
2615 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002616 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002617 return 0;
2618 }
2619
Willy Tarreau68472622017-12-11 18:36:37 +01002620 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2621 return 0; // incomplete input frame
2622
Willy Tarreau13278b42017-10-13 19:23:14 +02002623 /* if the input buffer wraps, take a temporary copy of it (rare) */
2624 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2625 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002626 copy = alloc_trash_chunk();
2627 if (!copy) {
2628 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2629 goto fail;
2630 }
2631 memcpy(copy->str, h2c->dbuf->p, wrap);
2632 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2633 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002634 }
2635
2636 /* The padlen is the first byte before data, and the padding appears
2637 * after data. padlen+data+padding are included in flen.
2638 */
2639 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002640 h2c->dpl = *hdrs;
2641 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002642 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2643 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002644 return 0;
2645 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002646 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002647 hdrs += 1; // skip Pad Length
2648 }
2649
2650 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2651 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002652 if (read_n32(hdrs) == h2s->id) {
2653 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2654 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2655 return 0;//goto fail_stream;
2656 }
2657
Willy Tarreau13278b42017-10-13 19:23:14 +02002658 hdrs += 5; // stream dep = 4, weight = 1
2659 flen -= 5;
2660 }
2661
2662 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2663 * don't support this for now and can't even decompress so we have to
2664 * break the connection.
2665 */
2666 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2667 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002668 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002669 }
2670
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002671 /* we can't retry a failed decompression operation so we must be very
2672 * careful not to take any risks. In practice the output buffer is
2673 * always empty except maybe for trailers, so these operations almost
2674 * never happen.
2675 */
2676 if (unlikely(buf->o)) {
2677 /* need to let the output buffer flush and
2678 * mark the buffer for later wake up.
2679 */
2680 goto fail;
2681 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002682
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002683 if (unlikely(buffer_space_wraps(buf))) {
2684 /* it doesn't fit and the buffer is fragmented,
2685 * so let's defragment it and try again.
2686 */
2687 buffer_slow_realign(buf);
2688 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002689
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002690 /* first check if we have some room after p+i */
2691 try = buf->data + buf->size - (buf->p + buf->i);
2692
2693 /* otherwise continue between data and p-o */
2694 if (try <= 0) {
2695 try = buf->p - (buf->data + buf->o);
2696 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002697 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002698 }
2699 if (try > count)
2700 try = count;
2701
2702 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2703 sizeof(list)/sizeof(list[0]), tmp);
2704 if (outlen < 0) {
2705 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2706 goto fail;
2707 }
2708
2709 /* OK now we have our header list in <list> */
2710 outlen = h2_make_h1_request(list, bi_end(buf), try);
2711
2712 if (outlen < 0) {
2713 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2714 goto fail;
2715 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002716
2717 /* now consume the input data */
2718 bi_del(h2c->dbuf, h2c->dfl);
2719 h2c->st0 = H2_CS_FRAME_H;
2720 buf->i += outlen;
2721
2722 /* don't send it before returning data!
2723 * FIXME: should we instead try to send it much later, after the
2724 * response ? This would require that we keep a copy of it in h2s.
2725 */
2726 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2727 h2s->cs->flags |= CS_FL_EOS;
2728 h2s->flags |= H2_SF_ES_RCVD;
2729 }
2730
Willy Tarreau68dd9852017-07-03 14:44:26 +02002731 leave:
2732 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002733 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002734 fail:
2735 outlen = 0;
2736 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002737}
2738
Willy Tarreau454f9052017-10-26 19:40:35 +02002739/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2740 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2741 * in use, a new chunk is emitted for each frame. This is supposed to fit
2742 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2743 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2744 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2745 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002746 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2747 * checked to know if some data remain pending (an empty DATA frame can return
2748 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2749 * connection errors in h2c->errcode. The caller must already have checked the
2750 * frame header and ensured that the frame was complete or the buffer full. It
2751 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002752 */
2753static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2754{
2755 struct h2c *h2c = h2s->h2c;
2756 int block1, block2;
2757 unsigned int flen = h2c->dfl;
Willy Tarreau454f9052017-10-26 19:40:35 +02002758
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002759 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002760 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002761
2762 /* The padlen is the first byte before data, and the padding appears
2763 * after data. padlen+data+padding are included in flen.
2764 */
Willy Tarreau79127812017-12-03 21:06:59 +01002765 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002766 if (h2c->dbuf->i < 1)
2767 return 0;
2768
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002769 h2c->dpl = *(uint8_t *)bi_ptr(h2c->dbuf);
2770 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002771 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2772 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002773 return 0;
2774 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002775
2776 /* skip the padlen byte */
2777 bi_del(h2c->dbuf, 1);
2778 h2c->dfl--;
2779 h2c->rcvd_c++; h2c->rcvd_s++;
2780 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002781 }
2782
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002783 flen = h2c->dfl - h2c->dpl;
2784 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002785 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002786
2787 if (flen > h2c->dbuf->i) {
2788 flen = h2c->dbuf->i;
2789 if (!flen)
2790 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002791 }
2792
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002793 /* does it fit in output buffer or should we wait ? */
2794 if (flen > count) {
2795 flen = count;
2796 if (!flen) {
2797 h2c->flags |= H2_CF_DEM_SFULL;
2798 h2s->cs->flags |= CS_FL_RCV_MORE;
2799 return 0;
2800 }
2801 }
2802
Willy Tarreau454f9052017-10-26 19:40:35 +02002803 /* Block1 is the length of the first block before the buffer wraps,
2804 * block2 is the optional second block to reach the end of the frame.
2805 */
2806 block1 = bi_contig_data(h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002807 if (block1 > flen)
2808 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002809 block2 = flen - block1;
2810
2811 if (block1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002812 bi_putblk(buf, b_ptr(h2c->dbuf, 0), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002813
2814 if (block2)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002815 bi_putblk(buf, b_ptr(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002816
2817 /* now mark the input data as consumed (will be deleted from the buffer
2818 * by the caller when seeing FRAME_A after sending the window update).
2819 */
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002820 bi_del(h2c->dbuf, flen);
2821 h2c->dfl -= flen;
2822 h2c->rcvd_c += flen;
2823 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2824
2825 if (h2c->dfl > h2c->dpl) {
2826 /* more data available, transfer stalled on stream full */
2827 h2c->flags |= H2_CF_DEM_SFULL;
2828 h2s->cs->flags |= CS_FL_RCV_MORE;
2829 return flen;
2830 }
2831
Willy Tarreau4a28da12018-01-04 14:41:00 +01002832 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002833 /* here we're done with the frame, all the payload (except padding) was
2834 * transferred.
2835 */
Willy Tarreau454f9052017-10-26 19:40:35 +02002836 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2837
2838 /* don't send it before returning data!
2839 * FIXME: should we instead try to send it much later, after the
2840 * response ? This would require that we keep a copy of it in h2s.
2841 */
Willy Tarreau79127812017-12-03 21:06:59 +01002842 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002843 h2s->cs->flags |= CS_FL_EOS;
2844 h2s->flags |= H2_SF_ES_RCVD;
2845 }
2846
2847 return flen;
2848}
2849
Willy Tarreau62f52692017-10-08 23:01:42 +02002850/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002851 * Called from the upper layer to get more data, up to <count> bytes. The
2852 * caller is responsible for never asking for more data than what is available
2853 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002854 */
2855static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2856{
Willy Tarreau13278b42017-10-13 19:23:14 +02002857 struct h2s *h2s = cs->ctx;
2858 struct h2c *h2c = h2s->h2c;
2859 int ret = 0;
2860
2861 if (h2c->st0 != H2_CS_FRAME_P)
2862 return 0; // no pre-parsed frame yet
2863
2864 if (h2c->dsi != h2s->id)
2865 return 0; // not for us
2866
2867 if (!h2c->dbuf->size)
2868 return 0; // empty buffer
2869
Willy Tarreau13278b42017-10-13 19:23:14 +02002870 switch (h2c->dft) {
2871 case H2_FT_HEADERS:
2872 ret = h2_frt_decode_headers(h2s, buf, count);
2873 break;
2874
Willy Tarreau454f9052017-10-26 19:40:35 +02002875 case H2_FT_DATA:
2876 ret = h2_frt_transfer_data(h2s, buf, count);
2877 break;
2878
Willy Tarreau13278b42017-10-13 19:23:14 +02002879 default:
2880 ret = 0;
2881 }
2882 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002883}
2884
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002885/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2886 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2887 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2888 * to the number of buffer bytes consumed.
2889 */
2890static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2891{
2892 struct http_hdr list[MAX_HTTP_HDR];
2893 struct h2c *h2c = h2s->h2c;
2894 struct h1m *h1m = &h2s->res;
2895 struct chunk outbuf;
2896 int es_now = 0;
2897 int ret = 0;
2898 int hdr;
2899
2900 if (h2c_mux_busy(h2c, h2s)) {
2901 h2s->flags |= H2_SF_BLK_MBUSY;
2902 return 0;
2903 }
2904
2905 if (!h2_get_mbuf(h2c)) {
2906 h2c->flags |= H2_CF_MUX_MALLOC;
2907 h2s->flags |= H2_SF_BLK_MROOM;
2908 return 0;
2909 }
2910
2911 /* First, try to parse the H1 response and index it into <list>.
2912 * NOTE! Since it comes from haproxy, we *know* that a response header
2913 * block does not wrap and we can safely read it this way without
2914 * having to realign the buffer.
2915 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002916 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002917 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2918 list, sizeof(list)/sizeof(list[0]), h1m);
2919 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002920 /* incomplete or invalid response, this is abnormal coming from
2921 * haproxy and may only result in a bad errorfile or bad Lua code
2922 * so that won't be fixed, raise an error now.
2923 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002924 * FIXME: we should instead add the ability to only return a
2925 * 502 bad gateway. But in theory this is not supposed to
2926 * happen.
2927 */
2928 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2929 ret = 0;
2930 goto end;
2931 }
2932
2933 chunk_reset(&outbuf);
2934
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002935 try_again:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002936 while (1) {
2937 outbuf.str = bo_end(h2c->mbuf);
2938 outbuf.size = bo_contig_space(h2c->mbuf);
2939 outbuf.len = 0;
2940
2941 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2942 break;
2943 realign_again:
2944 buffer_slow_realign(h2c->mbuf);
2945 }
2946
2947 if (outbuf.size < 9) {
2948 h2c->flags |= H2_CF_MUX_MFULL;
2949 h2s->flags |= H2_SF_BLK_MROOM;
2950 ret = 0;
2951 goto end;
2952 }
2953
2954 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2955 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2956 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2957 outbuf.len = 9;
2958
2959 /* encode status, which necessarily is the first one */
2960 if (outbuf.len < outbuf.size && h1m->status == 200)
2961 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2962 else if (outbuf.len < outbuf.size && h1m->status == 304)
2963 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002964 else if (unlikely(list[0].v.len != 3)) {
2965 /* this is an unparsable response */
2966 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2967 ret = 0;
2968 goto end;
2969 }
2970 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002971 /* basic encoding of the status code */
2972 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2973 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2974 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2975 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2976 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2977 }
2978 else {
2979 if (buffer_space_wraps(h2c->mbuf))
2980 goto realign_again;
2981
2982 h2c->flags |= H2_CF_MUX_MFULL;
2983 h2s->flags |= H2_SF_BLK_MROOM;
2984 ret = 0;
2985 goto end;
2986 }
2987
2988 /* encode all headers, stop at empty name */
2989 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002990 /* these ones do not exist in H2 and must be dropped. */
2991 if (isteq(list[hdr].n, ist("connection")) ||
2992 isteq(list[hdr].n, ist("proxy-connection")) ||
2993 isteq(list[hdr].n, ist("keep-alive")) ||
2994 isteq(list[hdr].n, ist("upgrade")) ||
2995 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002996 continue;
2997
2998 if (isteq(list[hdr].n, ist("")))
2999 break; // end
3000
3001 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3002 /* output full */
3003 if (buffer_space_wraps(h2c->mbuf))
3004 goto realign_again;
3005
3006 h2c->flags |= H2_CF_MUX_MFULL;
3007 h2s->flags |= H2_SF_BLK_MROOM;
3008 ret = 0;
3009 goto end;
3010 }
3011 }
3012
3013 /* we may need to add END_STREAM */
3014 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3015 es_now = 1;
3016
3017 /* update the frame's size */
3018 h2_set_frame_size(outbuf.str, outbuf.len - 9);
3019
3020 if (es_now)
3021 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
3022
3023 /* consume incoming H1 response */
3024 bo_del(buf, ret);
3025
3026 /* commit the H2 response */
3027 h2c->mbuf->o += outbuf.len;
3028 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01003029 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003030
3031 /* for now we don't implemented CONTINUATION, so we wait for a
3032 * body or directly end in TRL2.
3033 */
3034 if (es_now) {
3035 h1m->state = HTTP_MSG_DONE;
3036 h2s->flags |= H2_SF_ES_SENT;
3037 if (h2s->st == H2_SS_OPEN)
3038 h2s->st = H2_SS_HLOC;
3039 else
Willy Tarreau91bfdd72017-12-14 12:00:14 +01003040 h2c_stream_close(h2c, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003041 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003042 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003043 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003044 h1m->state = HTTP_MSG_RPBEFORE;
3045 h1m->status = 0;
3046 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003047 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003048 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003049 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003050 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003051
3052 end:
3053 //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));
3054 return ret;
3055}
3056
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003057/* Try to send a DATA frame matching HTTP/1 response present in the response
3058 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
3059 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
3060 * corresponds to the number of buffer bytes consumed.
3061 */
3062static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
3063{
3064 struct h2c *h2c = h2s->h2c;
3065 struct h1m *h1m = &h2s->res;
3066 struct chunk outbuf;
3067 int ret = 0;
3068 int total = 0;
3069 int es_now = 0;
3070 int size = 0;
3071 char *blk1, *blk2;
3072 int len1, len2;
3073
3074 if (h2c_mux_busy(h2c, h2s)) {
3075 h2s->flags |= H2_SF_BLK_MBUSY;
3076 goto end;
3077 }
3078
3079 if (!h2_get_mbuf(h2c)) {
3080 h2c->flags |= H2_CF_MUX_MALLOC;
3081 h2s->flags |= H2_SF_BLK_MROOM;
3082 goto end;
3083 }
3084
3085 new_frame:
3086 if (!buf->o)
3087 goto end;
3088
3089 chunk_reset(&outbuf);
3090
3091 while (1) {
3092 outbuf.str = bo_end(h2c->mbuf);
3093 outbuf.size = bo_contig_space(h2c->mbuf);
3094 outbuf.len = 0;
3095
3096 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
3097 break;
3098 realign_again:
3099 buffer_slow_realign(h2c->mbuf);
3100 }
3101
3102 if (outbuf.size < 9) {
3103 h2c->flags |= H2_CF_MUX_MFULL;
3104 h2s->flags |= H2_SF_BLK_MROOM;
3105 goto end;
3106 }
3107
3108 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3109 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3110 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3111 outbuf.len = 9;
3112
3113 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3114 case 0: /* no content length, read till SHUTW */
3115 size = buf->o;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003116 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003117 break;
3118 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
3119 size = buf->o;
3120 if ((long long)size > h1m->curr_len)
3121 size = h1m->curr_len;
3122 break;
3123 default: /* te:chunked : parse chunks */
3124 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
3125 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
3126 if (!ret)
3127 goto end;
3128
3129 if (ret < 0) {
3130 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3131 h1m->err_pos = ret;
3132 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3133 goto end;
3134 }
3135 bo_del(buf, ret);
3136 total += ret;
3137 h1m->state = HTTP_MSG_CHUNK_SIZE;
3138 }
3139
3140 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3141 unsigned int chunk;
3142
3143 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
3144 if (!ret)
3145 goto end;
3146
3147 if (ret < 0) {
3148 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3149 h1m->err_pos = ret;
3150 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3151 goto end;
3152 }
3153
3154 size = chunk;
3155 h1m->curr_len = chunk;
3156 h1m->body_len += chunk;
3157 bo_del(buf, ret);
3158 total += ret;
3159 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3160 if (!size)
3161 goto send_empty;
3162 }
3163
3164 /* in MSG_DATA state, continue below */
3165 size = h1m->curr_len;
3166 break;
3167 }
3168
3169 /* we have in <size> the exact number of bytes we need to copy from
3170 * the H1 buffer. We need to check this against the connection's and
3171 * the stream's send windows, and to ensure that this fits in the max
3172 * frame size and in the buffer's available space minus 9 bytes (for
3173 * the frame header). The connection's flow control is applied last so
3174 * that we can use a separate list of streams which are immediately
3175 * unblocked on window opening. Note: we don't implement padding.
3176 */
3177
3178 if (size > buf->o)
3179 size = buf->o;
3180
3181 if (size > h2s->mws)
3182 size = h2s->mws;
3183
3184 if (size <= 0) {
3185 h2s->flags |= H2_SF_BLK_SFCTL;
3186 goto end;
3187 }
3188
3189 if (h2c->mfs && size > h2c->mfs)
3190 size = h2c->mfs;
3191
3192 if (size + 9 > outbuf.size) {
3193 /* we have an opportunity for enlarging the too small
3194 * available space, let's try.
3195 */
3196 if (buffer_space_wraps(h2c->mbuf))
3197 goto realign_again;
3198 size = outbuf.size - 9;
3199 }
3200
3201 if (size <= 0) {
3202 h2c->flags |= H2_CF_MUX_MFULL;
3203 h2s->flags |= H2_SF_BLK_MROOM;
3204 goto end;
3205 }
3206
3207 if (size > h2c->mws)
3208 size = h2c->mws;
3209
3210 if (size <= 0) {
3211 h2s->flags |= H2_SF_BLK_MFCTL;
3212 goto end;
3213 }
3214
3215 /* copy whatever we can */
3216 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
3217 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
3218 if (ret == 1)
3219 len2 = 0;
3220
3221 if (!ret || len1 + len2 < size) {
3222 /* FIXME: must normally never happen */
3223 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3224 goto end;
3225 }
3226
3227 /* limit len1/len2 to size */
3228 if (len1 + len2 > size) {
3229 int sub = len1 + len2 - size;
3230
3231 if (len2 > sub)
3232 len2 -= sub;
3233 else {
3234 sub -= len2;
3235 len2 = 0;
3236 len1 -= sub;
3237 }
3238 }
3239
3240 /* now let's copy this this into the output buffer */
3241 memcpy(outbuf.str + 9, blk1, len1);
3242 if (len2)
3243 memcpy(outbuf.str + 9 + len1, blk2, len2);
3244
3245 send_empty:
3246 /* we may need to add END_STREAM */
3247 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3248 * could rely on the MSG_MORE flag as a hint for this ?
3249 */
3250 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3251 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3252 es_now = 1;
3253
3254 /* update the frame's size */
3255 h2_set_frame_size(outbuf.str, size);
3256
3257 if (es_now)
3258 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3259
3260 /* commit the H2 response */
3261 h2c->mbuf->o += size + 9;
3262 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3263
3264 /* consume incoming H1 response */
3265 if (size > 0) {
3266 bo_del(buf, size);
3267 total += size;
3268 h1m->curr_len -= size;
3269 h2s->mws -= size;
3270 h2c->mws -= size;
3271
3272 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3273 h1m->state = HTTP_MSG_CHUNK_CRLF;
3274 goto new_frame;
3275 }
3276 }
3277
3278 if (es_now) {
3279 if (h2s->st == H2_SS_OPEN)
3280 h2s->st = H2_SS_HLOC;
3281 else
Willy Tarreau91bfdd72017-12-14 12:00:14 +01003282 h2c_stream_close(h2c, h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003283
3284 if (!(h1m->flags & H1_MF_CHNK))
3285 h1m->state = HTTP_MSG_DONE;
3286
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003287 h2s->flags |= H2_SF_ES_SENT;
3288 }
3289
3290 end:
3291 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);
3292 return total;
3293}
3294
Willy Tarreau62f52692017-10-08 23:01:42 +02003295/* Called from the upper layer, to send data */
3296static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3297{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003298 struct h2s *h2s = cs->ctx;
3299 int total = 0;
3300
Willy Tarreauc4312d32017-11-07 12:01:53 +01003301 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3302 h2s->flags |= H2_SF_OUTGOING_DATA;
3303
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003304 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3305 if (h2s->res.state < HTTP_MSG_BODY) {
3306 total += h2s_frt_make_resp_headers(h2s, buf);
3307
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003308 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003309 break;
3310
3311 if (h2s->flags & H2_SF_BLK_ANY)
3312 break;
3313 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003314 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3315 total += h2s_frt_make_resp_data(h2s, buf);
3316
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003317 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003318 break;
3319
3320 if (h2s->flags & H2_SF_BLK_ANY)
3321 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003322 }
3323 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3324 /* consume the trailers if any (we don't forward them for now) */
3325 int count = h1_measure_trailers(buf);
3326
3327 if (unlikely(count <= 0)) {
3328 if (count < 0)
3329 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3330 break;
3331 }
3332 total += count;
3333 bo_del(buf, count);
3334 h2s->res.state = HTTP_MSG_DONE;
3335 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003336 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003337 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003338 cs->flags |= CS_FL_ERROR;
3339 break;
3340 }
3341 }
3342
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003343 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003344 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003345 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003346 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01003347 h2c_stream_close(h2s->h2c, h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003348 }
3349
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003350 if (h2s->flags & H2_SF_BLK_SFCTL) {
3351 /* stream flow control, quit the list */
3352 LIST_DEL(&h2s->list);
3353 LIST_INIT(&h2s->list);
3354 }
3355
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003356 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003357}
3358
3359
3360/*******************************************************/
3361/* functions below are dedicated to the config parsers */
3362/*******************************************************/
3363
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003364/* config parser for global "tune.h2.header-table-size" */
3365static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3366 struct proxy *defpx, const char *file, int line,
3367 char **err)
3368{
3369 if (too_many_args(1, args, err, NULL))
3370 return -1;
3371
3372 h2_settings_header_table_size = atoi(args[1]);
3373 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3374 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3375 return -1;
3376 }
3377 return 0;
3378}
Willy Tarreau62f52692017-10-08 23:01:42 +02003379
Willy Tarreaue6baec02017-07-27 11:45:11 +02003380/* config parser for global "tune.h2.initial-window-size" */
3381static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3382 struct proxy *defpx, const char *file, int line,
3383 char **err)
3384{
3385 if (too_many_args(1, args, err, NULL))
3386 return -1;
3387
3388 h2_settings_initial_window_size = atoi(args[1]);
3389 if (h2_settings_initial_window_size < 0) {
3390 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3391 return -1;
3392 }
3393 return 0;
3394}
3395
Willy Tarreau5242ef82017-07-27 11:47:28 +02003396/* config parser for global "tune.h2.max-concurrent-streams" */
3397static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3398 struct proxy *defpx, const char *file, int line,
3399 char **err)
3400{
3401 if (too_many_args(1, args, err, NULL))
3402 return -1;
3403
3404 h2_settings_max_concurrent_streams = atoi(args[1]);
3405 if (h2_settings_max_concurrent_streams < 0) {
3406 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3407 return -1;
3408 }
3409 return 0;
3410}
3411
Willy Tarreau62f52692017-10-08 23:01:42 +02003412
3413/****************************************/
3414/* MUX initialization and instanciation */
3415/***************************************/
3416
3417/* The mux operations */
3418const struct mux_ops h2_ops = {
3419 .init = h2_init,
3420 .recv = h2_recv,
3421 .send = h2_send,
3422 .wake = h2_wake,
3423 .update_poll = h2_update_poll,
3424 .rcv_buf = h2_rcv_buf,
3425 .snd_buf = h2_snd_buf,
3426 .attach = h2_attach,
3427 .detach = h2_detach,
3428 .shutr = h2_shutr,
3429 .shutw = h2_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003430 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003431 .name = "H2",
3432};
3433
3434/* ALPN selection : this mux registers ALPN tolen "h2" */
3435static struct alpn_mux_list alpn_mux_h2 =
3436 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3437
3438/* config keyword parsers */
3439static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003440 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003441 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003442 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003443 { 0, NULL, NULL }
3444}};
3445
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003446static void __h2_deinit(void)
3447{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003448 pool_destroy(pool_head_h2s);
3449 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003450}
3451
Willy Tarreau62f52692017-10-08 23:01:42 +02003452__attribute__((constructor))
3453static void __h2_init(void)
3454{
3455 alpn_register_mux(&alpn_mux_h2);
3456 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003457 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003458 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3459 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003460}