blob: 7b2f5381893e6c4a823cc608963c404d51431617 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau35dbd5d2017-09-22 09:13:49 +020020#include <proto/applet.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020021#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020022#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010024#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020025#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026
27
Willy Tarreau2a856182017-05-16 15:20:39 +020028/* dummy streams returned for idle and closed states */
29static const struct h2s *h2_closed_stream;
30static const struct h2s *h2_idle_stream;
31
Willy Tarreau5ab6b572017-09-22 08:05:00 +020032/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010033static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020034/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010035static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020036
37/* Connection flags (32 bit), in h2c->flags */
38#define H2_CF_NONE 0x00000000
39
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020040/* Flags indicating why writing to the mux is blocked. */
41#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
42#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
43#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
44
Willy Tarreau315d8072017-12-10 22:17:57 +010045/* Flags indicating why writing to the demux is blocked.
46 * The first two ones directly affect the ability for the mux to receive data
47 * from the connection. The other ones affect the mux's ability to demux
48 * received data.
49 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020050#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
51#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010052
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020053#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
54#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
55#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
56#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010057#define H2_CF_DEM_BLOCK_ANY 0x000000F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
60#define H2_CF_GOAWAY_SENT 0x00000100 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00000200 // a GOAWAY frame failed to be sent
Olivier Houchard6fa63d92017-11-27 18:41:32 +010062#define H2_CF_WAIT_FOR_HS 0x00000400 // We did check that at least a stream was waiting for handshake
Willy Tarreau081d4722017-05-16 21:51:05 +020063
64
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
93 struct buffer *dbuf; /* demux buffer */
94
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
104 struct buffer *mbuf; /* mux buffer */
105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
117 /* 32 bit hole here */
Willy Tarreauea392822017-10-31 10:02:25 +0100118 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119 struct eb_root streams_by_id; /* all active streams by their ID */
120 struct list send_list; /* list of blocked streams requesting to send */
121 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200123};
124
Willy Tarreau18312642017-10-11 07:57:07 +0200125/* H2 stream state, in h2s->st */
126enum h2_ss {
127 H2_SS_IDLE = 0, // idle
128 H2_SS_RLOC, // reserved(local)
129 H2_SS_RREM, // reserved(remote)
130 H2_SS_OPEN, // open
131 H2_SS_HREM, // half-closed(remote)
132 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200133 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200134 H2_SS_CLOSED, // closed
135 H2_SS_ENTRIES // must be last
136} __attribute__((packed));
137
138/* HTTP/2 stream flags (32 bit), in h2s->flags */
139#define H2_SF_NONE 0x00000000
140#define H2_SF_ES_RCVD 0x00000001
141#define H2_SF_ES_SENT 0x00000002
142
143#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
144#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
145
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200146/* stream flags indicating the reason the stream is blocked */
147#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
148#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
149#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
150#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
151#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
152
Willy Tarreau454f9052017-10-26 19:40:35 +0200153/* stream flags indicating how data is supposed to be sent */
154#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
155#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
156
157/* step we're currently in when sending chunks. This is needed because we may
158 * have to transfer chunks as large as a full buffer so there's no room left
159 * for size nor crlf around.
160 */
161#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
162#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
163#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
164
165#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
166
Willy Tarreau67434202017-11-06 20:20:51 +0100167#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100168#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100169
Willy Tarreau18312642017-10-11 07:57:07 +0200170/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
171 * it is being processed in the internal HTTP representation (H1 for now).
172 */
173struct h2s {
174 struct conn_stream *cs;
175 struct h2c *h2c;
176 struct h1m req, res; /* request and response parser state for H1 */
177 struct eb32_node by_id; /* place in h2c's streams_by_id */
178 struct list list; /* position in active/blocked lists if blocked>0 */
179 int32_t id; /* stream ID */
180 uint32_t flags; /* H2_SF_* */
181 int mws; /* mux window size for this stream */
182 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
183 enum h2_ss st;
184};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200185
Willy Tarreauc6405142017-09-21 20:23:50 +0200186/* descriptor for an h2 frame header */
187struct h2_fh {
188 uint32_t len; /* length, host order, 24 bits */
189 uint32_t sid; /* stream id, host order, 31 bits */
190 uint8_t ft; /* frame type */
191 uint8_t ff; /* frame flags */
192};
193
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200194/* a few settings from the global section */
195static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200196static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200197static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200198
Willy Tarreau2a856182017-05-16 15:20:39 +0200199/* a dmumy closed stream */
200static const struct h2s *h2_closed_stream = &(const struct h2s){
201 .cs = NULL,
202 .h2c = NULL,
203 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100204 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100205 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200206 .id = 0,
207};
208
209/* and a dummy idle stream for use with any unannounced stream */
210static const struct h2s *h2_idle_stream = &(const struct h2s){
211 .cs = NULL,
212 .h2c = NULL,
213 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100214 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200215 .id = 0,
216};
217
Willy Tarreauea392822017-10-31 10:02:25 +0100218static struct task *h2_timeout_task(struct task *t);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200219
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200220/*****************************************************/
221/* functions below are for dynamic buffer management */
222/*****************************************************/
223
Willy Tarreau315d8072017-12-10 22:17:57 +0100224/* indicates whether or not the we may call the h2_recv() function to attempt
225 * to receive data into the buffer and/or demux pending data. The condition is
226 * a bit complex due to some API limits for now. The rules are the following :
227 * - if an error or a shutdown was detected on the connection and the buffer
228 * is empty, we must not attempt to receive
229 * - if the demux buf failed to be allocated, we must not try to receive and
230 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100231 * - if no flag indicates a blocking condition, we may attempt to receive,
232 * regardless of whether the demux buffer is full or not, so that only
233 * de demux part decides whether or not to block. This is needed because
234 * the connection API indeed prevents us from re-enabling receipt that is
235 * already enabled in a polled state, so we must always immediately stop
236 * as soon as the demux can't proceed so as never to hit an end of read
237 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100238 * - otherwise must may not attempt
239 */
240static inline int h2_recv_allowed(const struct h2c *h2c)
241{
242 if (h2c->dbuf->i == 0 &&
243 (h2c->st0 >= H2_CS_ERROR ||
244 h2c->conn->flags & CO_FL_ERROR ||
245 conn_xprt_read0_pending(h2c->conn)))
246 return 0;
247
248 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100249 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100250 return 1;
251
252 return 0;
253}
254
Willy Tarreau44e973f2018-03-01 17:49:30 +0100255/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
256 * flags are used to figure what buffer was requested. It returns 1 if the
257 * allocation succeeds, in which case the connection is woken up, or 0 if it's
258 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200259 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100260static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200261{
262 struct h2c *h2c = target;
263
Willy Tarreau44e973f2018-03-01 17:49:30 +0100264 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200265 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100266 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200267 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200268 return 1;
269 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200270
Willy Tarreau44e973f2018-03-01 17:49:30 +0100271 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
272 h2c->flags &= ~H2_CF_MUX_MALLOC;
273 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
274 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200275
276 if (h2c->flags & H2_CF_DEM_MROOM) {
277 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100278 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200279 conn_xprt_want_recv(h2c->conn);
280 }
Willy Tarreau14398122017-09-22 14:26:04 +0200281 return 1;
282 }
283 return 0;
284}
285
Willy Tarreau44e973f2018-03-01 17:49:30 +0100286static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer **bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200287{
288 struct buffer *buf = NULL;
289
Willy Tarreau44e973f2018-03-01 17:49:30 +0100290 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
291 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
292 h2c->buf_wait.target = h2c;
293 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100294 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100295 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100296 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200297 __conn_xprt_stop_recv(h2c->conn);
298 }
299 return buf;
300}
301
Willy Tarreau44e973f2018-03-01 17:49:30 +0100302static inline void h2_release_buf(struct h2c *h2c, struct buffer **bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200303{
Willy Tarreau44e973f2018-03-01 17:49:30 +0100304 if ((*bptr)->size) {
305 b_free(bptr);
306 offer_buffers(h2c->buf_wait.target,
Willy Tarreau14398122017-09-22 14:26:04 +0200307 tasks_run_queue + applets_active_queue);
308 }
309}
310
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200311
Willy Tarreau62f52692017-10-08 23:01:42 +0200312/*****************************************************************/
313/* functions below are dedicated to the mux setup and management */
314/*****************************************************************/
315
Willy Tarreau32218eb2017-09-22 08:07:25 +0200316/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
317static int h2c_frt_init(struct connection *conn)
318{
319 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100320 struct task *t = NULL;
321 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200322
Willy Tarreaubafbe012017-11-24 17:34:44 +0100323 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200324 if (!h2c)
325 goto fail;
326
Willy Tarreau3f133572017-10-31 19:21:06 +0100327
Willy Tarreau599391a2017-11-24 10:16:00 +0100328 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
329 if (tick_isset(sess->fe->timeout.clientfin))
330 h2c->shut_timeout = sess->fe->timeout.clientfin;
331
Willy Tarreau33400292017-11-05 11:23:40 +0100332 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100333 if (tick_isset(h2c->timeout)) {
334 t = task_new(tid_bit);
335 if (!t)
336 goto fail;
337
338 h2c->task = t;
339 t->process = h2_timeout_task;
340 t->context = h2c;
341 t->expire = tick_add(now_ms, h2c->timeout);
342 }
Willy Tarreauea392822017-10-31 10:02:25 +0100343
Willy Tarreau32218eb2017-09-22 08:07:25 +0200344 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
345 if (!h2c->ddht)
346 goto fail;
347
348 /* Initialise the context. */
349 h2c->st0 = H2_CS_PREFACE;
350 h2c->conn = conn;
351 h2c->max_id = -1;
352 h2c->errcode = H2_ERR_NO_ERROR;
353 h2c->flags = H2_CF_NONE;
354 h2c->rcvd_c = 0;
355 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100356 h2c->nb_streams = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200357
358 h2c->dbuf = &buf_empty;
359 h2c->dsi = -1;
360 h2c->msi = -1;
361 h2c->last_sid = -1;
362
363 h2c->mbuf = &buf_empty;
364 h2c->miw = 65535; /* mux initial window size */
365 h2c->mws = 65535; /* mux window size */
366 h2c->mfs = 16384; /* initial max frame size */
367 h2c->streams_by_id = EB_ROOT_UNIQUE;
368 LIST_INIT(&h2c->send_list);
369 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100370 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200371 conn->mux_ctx = h2c;
372
Willy Tarreau3f133572017-10-31 19:21:06 +0100373 if (t)
374 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200375 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100376
Willy Tarreau32218eb2017-09-22 08:07:25 +0200377 /* mux->wake will be called soon to complete the operation */
378 return 0;
379 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100380 if (t)
381 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100382 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200383 return -1;
384}
385
Willy Tarreau62f52692017-10-08 23:01:42 +0200386/* Initialize the mux once it's attached. For outgoing connections, the context
387 * is already initialized before installing the mux, so we detect incoming
388 * connections from the fact that the context is still NULL. Returns < 0 on
389 * error.
390 */
391static int h2_init(struct connection *conn)
392{
393 if (conn->mux_ctx) {
394 /* we don't support outgoing connections for now */
395 return -1;
396 }
397
Willy Tarreau32218eb2017-09-22 08:07:25 +0200398 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200399}
400
Willy Tarreau2373acc2017-10-12 17:35:14 +0200401/* returns the stream associated with id <id> or NULL if not found */
402static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
403{
404 struct eb32_node *node;
405
Willy Tarreau2a856182017-05-16 15:20:39 +0200406 if (id > h2c->max_id)
407 return (struct h2s *)h2_idle_stream;
408
Willy Tarreau2373acc2017-10-12 17:35:14 +0200409 node = eb32_lookup(&h2c->streams_by_id, id);
410 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200411 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200412
413 return container_of(node, struct h2s, by_id);
414}
415
Willy Tarreau62f52692017-10-08 23:01:42 +0200416/* release function for a connection. This one should be called to free all
417 * resources allocated to the mux.
418 */
419static void h2_release(struct connection *conn)
420{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200421 struct h2c *h2c = conn->mux_ctx;
422
423 LIST_DEL(&conn->list);
424
425 if (h2c) {
426 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200427
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100428 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100429 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100430 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200431
Willy Tarreau44e973f2018-03-01 17:49:30 +0100432 h2_release_buf(h2c, &h2c->dbuf);
433 h2_release_buf(h2c, &h2c->mbuf);
434
Willy Tarreauea392822017-10-31 10:02:25 +0100435 if (h2c->task) {
436 task_delete(h2c->task);
437 task_free(h2c->task);
438 h2c->task = NULL;
439 }
440
Willy Tarreaubafbe012017-11-24 17:34:44 +0100441 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200442 }
443
444 conn->mux = NULL;
445 conn->mux_ctx = NULL;
446
447 conn_stop_tracking(conn);
448 conn_full_close(conn);
449 if (conn->destroy_cb)
450 conn->destroy_cb(conn);
451 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200452}
453
454
Willy Tarreau71681172017-10-23 14:39:06 +0200455/******************************************************/
456/* functions below are for the H2 protocol processing */
457/******************************************************/
458
459/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100460static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200461{
462 return h2s ? h2s->id : 0;
463}
464
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200465/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100466static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200467{
468 if (h2c->msi < 0)
469 return 0;
470
471 if (h2c->msi == h2s_id(h2s))
472 return 0;
473
474 return 1;
475}
476
Willy Tarreau741d6df2017-10-17 08:00:59 +0200477/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100478static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200479{
480 h2c->errcode = err;
481 h2c->st0 = H2_CS_ERROR;
482}
483
Willy Tarreau2e43f082017-10-17 08:03:59 +0200484/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100485static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200486{
487 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
488 h2s->errcode = err;
489 h2s->st = H2_SS_ERROR;
490 if (h2s->cs)
491 h2s->cs->flags |= CS_FL_ERROR;
492 }
493}
494
Willy Tarreaue4820742017-07-27 13:37:23 +0200495/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100496static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200497{
498 uint8_t *out = frame;
499
500 *out = len >> 16;
501 write_n16(out + 1, len);
502}
503
Willy Tarreau54c15062017-10-10 17:10:03 +0200504/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
505 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
506 * the caller's responsibility to verify that there are at least <bytes> bytes
507 * available in the buffer's input prior to calling this function.
508 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100509static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200510 const struct buffer *b, int o)
511{
512 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
513}
514
Willy Tarreau1f094672017-11-20 21:27:45 +0100515static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200516{
517 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
518}
519
Willy Tarreau1f094672017-11-20 21:27:45 +0100520static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200521{
522 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
523}
524
Willy Tarreau1f094672017-11-20 21:27:45 +0100525static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200526{
527 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
528}
529
530
Willy Tarreau715d5312017-07-11 15:20:24 +0200531/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
532 * is not obvious. It turns out that H2 headers are neither aligned nor do they
533 * use regular sizes. And to add to the trouble, the buffer may wrap so each
534 * byte read must be checked. The header is formed like this :
535 *
536 * b0 b1 b2 b3 b4 b5..b8
537 * +----------+---------+--------+----+----+----------------------+
538 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
539 * +----------+---------+--------+----+----+----------------------+
540 *
541 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
542 * we get the sid properly aligned and ordered, and 16 bits of len properly
543 * ordered as well. The type and flags can be extracted using bit shifts from
544 * the word, and only one extra read is needed to fetch len[16:23].
545 * Returns zero if some bytes are missing, otherwise non-zero on success.
546 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100547static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200548{
549 uint64_t w;
550
551 if (b->i < 9)
552 return 0;
553
554 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
555 h->len = *b->p << 16;
556 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
557 h->ff = w >> 32;
558 h->ft = w >> 40;
559 h->len += w >> 48;
560 return 1;
561}
562
563/* skip the next 9 bytes corresponding to the frame header possibly parsed by
564 * h2_peek_frame_hdr() above.
565 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100566static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200567{
568 bi_del(b, 9);
569}
570
571/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100572static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200573{
574 int ret;
575
576 ret = h2_peek_frame_hdr(b, h);
577 if (ret > 0)
578 h2_skip_frame_hdr(b);
579 return ret;
580}
581
Willy Tarreau00dd0782018-03-01 16:31:34 +0100582/* marks stream <h2s> as CLOSED and decrement the number of active streams for
583 * its connection if the stream was not yet closed. Please use this exclusively
584 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100585 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100586static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100587{
588 if (h2s->st != H2_SS_CLOSED)
589 h2s->h2c->nb_streams--;
590 h2s->st = H2_SS_CLOSED;
591}
592
Willy Tarreau0a10de62018-03-01 16:27:53 +0100593/* detaches an H2 stream from its H2C. */
594static void h2s_detach(struct h2s *h2s)
595{
596 h2s_close(h2s);
597 eb32_delete(&h2s->by_id);
598}
599
600/* releases an H2 stream back to the pool, and detaches it from the h2c. */
601static void h2s_free(struct h2s *h2s)
602{
603 pool_free(pool_head_h2s, h2s);
604}
605
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200606/* creates a new stream <id> on the h2c connection and returns it, or NULL in
607 * case of memory allocation error.
608 */
609static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
610{
611 struct conn_stream *cs;
612 struct h2s *h2s;
613
Willy Tarreaubafbe012017-11-24 17:34:44 +0100614 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200615 if (!h2s)
616 goto out;
617
618 h2s->h2c = h2c;
619 h2s->mws = h2c->miw;
620 h2s->flags = H2_SF_NONE;
621 h2s->errcode = H2_ERR_NO_ERROR;
622 h2s->st = H2_SS_IDLE;
623 h1m_init(&h2s->req);
624 h1m_init(&h2s->res);
625 h2s->by_id.key = h2s->id = id;
626 h2c->max_id = id;
627 LIST_INIT(&h2s->list);
628
629 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100630 h2c->nb_streams++;
631 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
632 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200633
634 cs = cs_new(h2c->conn);
635 if (!cs)
636 goto out_close;
637
638 h2s->cs = cs;
639 cs->ctx = h2s;
640
641 if (stream_create_from_cs(cs) < 0)
642 goto out_free_cs;
643
644 /* OK done, the stream lives its own life now */
645 return h2s;
646
647 out_free_cs:
648 cs_free(cs);
649 out_close:
Willy Tarreau0a10de62018-03-01 16:27:53 +0100650 h2s_detach(h2s);
651 h2s_free(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200652 h2s = NULL;
653 out:
654 return h2s;
655}
656
Willy Tarreaube5b7152017-09-25 16:25:39 +0200657/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
658 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
659 * the various settings codes.
660 */
661static int h2c_snd_settings(struct h2c *h2c)
662{
663 struct buffer *res;
664 char buf_data[100]; // enough for 15 settings
665 struct chunk buf;
666 int ret;
667
668 if (h2c_mux_busy(h2c, NULL)) {
669 h2c->flags |= H2_CF_DEM_MBUSY;
670 return 0;
671 }
672
Willy Tarreau44e973f2018-03-01 17:49:30 +0100673 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200674 if (!res) {
675 h2c->flags |= H2_CF_MUX_MALLOC;
676 h2c->flags |= H2_CF_DEM_MROOM;
677 return 0;
678 }
679
680 chunk_init(&buf, buf_data, sizeof(buf_data));
681 chunk_memcpy(&buf,
682 "\x00\x00\x00" /* length : 0 for now */
683 "\x04\x00" /* type : 4 (settings), flags : 0 */
684 "\x00\x00\x00\x00", /* stream ID : 0 */
685 9);
686
687 if (h2_settings_header_table_size != 4096) {
688 char str[6] = "\x00\x01"; /* header_table_size */
689
690 write_n32(str + 2, h2_settings_header_table_size);
691 chunk_memcat(&buf, str, 6);
692 }
693
694 if (h2_settings_initial_window_size != 65535) {
695 char str[6] = "\x00\x04"; /* initial_window_size */
696
697 write_n32(str + 2, h2_settings_initial_window_size);
698 chunk_memcat(&buf, str, 6);
699 }
700
701 if (h2_settings_max_concurrent_streams != 0) {
702 char str[6] = "\x00\x03"; /* max_concurrent_streams */
703
704 /* Note: 0 means "unlimited" for haproxy's config but not for
705 * the protocol, so never send this value!
706 */
707 write_n32(str + 2, h2_settings_max_concurrent_streams);
708 chunk_memcat(&buf, str, 6);
709 }
710
711 if (global.tune.bufsize != 16384) {
712 char str[6] = "\x00\x05"; /* max_frame_size */
713
714 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
715 * match bufsize - rewrite size, but at the moment it seems
716 * that clients don't take care of it.
717 */
718 write_n32(str + 2, global.tune.bufsize);
719 chunk_memcat(&buf, str, 6);
720 }
721
722 h2_set_frame_size(buf.str, buf.len - 9);
723 ret = bo_istput(res, ist2(buf.str, buf.len));
724 if (unlikely(ret <= 0)) {
725 if (!ret) {
726 h2c->flags |= H2_CF_MUX_MFULL;
727 h2c->flags |= H2_CF_DEM_MROOM;
728 return 0;
729 }
730 else {
731 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
732 return 0;
733 }
734 }
735 return ret;
736}
737
Willy Tarreau52eed752017-09-22 15:05:09 +0200738/* Try to receive a connection preface, then upon success try to send our
739 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
740 * missing data. It may return an error in h2c.
741 */
742static int h2c_frt_recv_preface(struct h2c *h2c)
743{
744 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200745 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200746
747 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
748
749 if (unlikely(ret1 <= 0)) {
750 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
751 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
752 return 0;
753 }
754
Willy Tarreaube5b7152017-09-25 16:25:39 +0200755 ret2 = h2c_snd_settings(h2c);
756 if (ret2 > 0)
757 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200758
Willy Tarreaube5b7152017-09-25 16:25:39 +0200759 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200760}
761
Willy Tarreau081d4722017-05-16 21:51:05 +0200762/* try to send a GOAWAY frame on the connection to report an error or a graceful
763 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
764 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
765 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
766 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
767 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
768 * on unrecoverable failure. It will not attempt to send one again in this last
769 * case so that it is safe to use h2c_error() to report such errors.
770 */
771static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
772{
773 struct buffer *res;
774 char str[17];
775 int ret;
776
777 if (h2c->flags & H2_CF_GOAWAY_FAILED)
778 return 1; // claim that it worked
779
780 if (h2c_mux_busy(h2c, h2s)) {
781 if (h2s)
782 h2s->flags |= H2_SF_BLK_MBUSY;
783 else
784 h2c->flags |= H2_CF_DEM_MBUSY;
785 return 0;
786 }
787
Willy Tarreau44e973f2018-03-01 17:49:30 +0100788 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200789 if (!res) {
790 h2c->flags |= H2_CF_MUX_MALLOC;
791 if (h2s)
792 h2s->flags |= H2_SF_BLK_MROOM;
793 else
794 h2c->flags |= H2_CF_DEM_MROOM;
795 return 0;
796 }
797
798 /* len: 8, type: 7, flags: none, sid: 0 */
799 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
800
801 if (h2c->last_sid < 0)
802 h2c->last_sid = h2c->max_id;
803
804 write_n32(str + 9, h2c->last_sid);
805 write_n32(str + 13, h2c->errcode);
806 ret = bo_istput(res, ist2(str, 17));
807 if (unlikely(ret <= 0)) {
808 if (!ret) {
809 h2c->flags |= H2_CF_MUX_MFULL;
810 if (h2s)
811 h2s->flags |= H2_SF_BLK_MROOM;
812 else
813 h2c->flags |= H2_CF_DEM_MROOM;
814 return 0;
815 }
816 else {
817 /* we cannot report this error using GOAWAY, so we mark
818 * it and claim a success.
819 */
820 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
821 h2c->flags |= H2_CF_GOAWAY_FAILED;
822 return 1;
823 }
824 }
825 h2c->flags |= H2_CF_GOAWAY_SENT;
826 return ret;
827}
828
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100829/* Try to send an RST_STREAM frame on the connection for the indicated stream
830 * during mux operations. This stream must be valid and cannot be closed
831 * already. h2s->id will be used for the stream ID and h2s->errcode will be
832 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
833 * not yet.
834 *
835 * Returns > 0 on success or zero if nothing was done. In case of lack of room
836 * to write the message, it subscribes the stream to future notifications.
837 */
838static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
839{
840 struct buffer *res;
841 char str[13];
842 int ret;
843
844 if (!h2s || h2s->st == H2_SS_CLOSED)
845 return 1;
846
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100847 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
848 * RST_STREAM in response to a RST_STREAM frame.
849 */
850 if (h2c->dft == H2_FT_RST_STREAM) {
851 ret = 1;
852 goto ignore;
853 }
854
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100855 if (h2c_mux_busy(h2c, h2s)) {
856 h2s->flags |= H2_SF_BLK_MBUSY;
857 return 0;
858 }
859
Willy Tarreau44e973f2018-03-01 17:49:30 +0100860 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100861 if (!res) {
862 h2c->flags |= H2_CF_MUX_MALLOC;
863 h2s->flags |= H2_SF_BLK_MROOM;
864 return 0;
865 }
866
867 /* len: 4, type: 3, flags: none */
868 memcpy(str, "\x00\x00\x04\x03\x00", 5);
869 write_n32(str + 5, h2s->id);
870 write_n32(str + 9, h2s->errcode);
871 ret = bo_istput(res, ist2(str, 13));
872
873 if (unlikely(ret <= 0)) {
874 if (!ret) {
875 h2c->flags |= H2_CF_MUX_MFULL;
876 h2s->flags |= H2_SF_BLK_MROOM;
877 return 0;
878 }
879 else {
880 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
881 return 0;
882 }
883 }
884
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100885 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100886 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100887 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100888 return ret;
889}
890
891/* Try to send an RST_STREAM frame on the connection for the stream being
892 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
893 * error code unless the stream's state already is IDLE or CLOSED in which
894 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
895 * it was not yet.
896 *
897 * Returns > 0 on success or zero if nothing was done. In case of lack of room
898 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200899 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100900 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200901 */
902static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
903{
904 struct buffer *res;
905 char str[13];
906 int ret;
907
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100908 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
909 * RST_STREAM in response to a RST_STREAM frame.
910 */
911 if (h2c->dft == H2_FT_RST_STREAM) {
912 ret = 1;
913 goto ignore;
914 }
915
Willy Tarreau27a84c92017-10-17 08:10:17 +0200916 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100917 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200918 return 0;
919 }
920
Willy Tarreau44e973f2018-03-01 17:49:30 +0100921 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200922 if (!res) {
923 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100924 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200925 return 0;
926 }
927
928 /* len: 4, type: 3, flags: none */
929 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100930
Willy Tarreau27a84c92017-10-17 08:10:17 +0200931 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100932 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200933 h2s->errcode : H2_ERR_STREAM_CLOSED);
934 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100935
Willy Tarreau27a84c92017-10-17 08:10:17 +0200936 if (unlikely(ret <= 0)) {
937 if (!ret) {
938 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100939 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200940 return 0;
941 }
942 else {
943 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
944 return 0;
945 }
946 }
947
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100948 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100949 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200950 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100951 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100952 }
953
Willy Tarreau27a84c92017-10-17 08:10:17 +0200954 return ret;
955}
956
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100957/* try to send an empty DATA frame with the ES flag set to notify about the
958 * end of stream and match a shutdown(write). If an ES was already sent as
959 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
960 * on success or zero if nothing was done. In case of lack of room to write the
961 * message, it subscribes the requesting stream to future notifications.
962 */
963static int h2_send_empty_data_es(struct h2s *h2s)
964{
965 struct h2c *h2c = h2s->h2c;
966 struct buffer *res;
967 char str[9];
968 int ret;
969
Willy Tarreau721c9742017-11-07 11:05:42 +0100970 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100971 return 1;
972
973 if (h2c_mux_busy(h2c, h2s)) {
974 h2s->flags |= H2_SF_BLK_MBUSY;
975 return 0;
976 }
977
Willy Tarreau44e973f2018-03-01 17:49:30 +0100978 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100979 if (!res) {
980 h2c->flags |= H2_CF_MUX_MALLOC;
981 h2s->flags |= H2_SF_BLK_MROOM;
982 return 0;
983 }
984
985 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
986 memcpy(str, "\x00\x00\x00\x00\x01", 5);
987 write_n32(str + 5, h2s->id);
988 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100989 if (likely(ret > 0)) {
990 h2s->flags |= H2_SF_ES_SENT;
991 }
992 else if (!ret) {
993 h2c->flags |= H2_CF_MUX_MFULL;
994 h2s->flags |= H2_SF_BLK_MROOM;
995 return 0;
996 }
997 else {
998 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
999 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001000 }
1001 return ret;
1002}
1003
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001004/* wake the streams attached to the connection, whose id is greater than <last>,
1005 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1006 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1007 * stream's state is automatically updated accordingly.
1008 */
1009static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1010{
1011 struct eb32_node *node;
1012 struct h2s *h2s;
1013
1014 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1015 flags |= CS_FL_ERROR;
1016
1017 if (conn_xprt_read0_pending(h2c->conn))
1018 flags |= CS_FL_EOS;
1019
1020 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1021 while (node) {
1022 h2s = container_of(node, struct h2s, by_id);
1023 if (h2s->id <= last)
1024 break;
1025 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001026
1027 if (!h2s->cs) {
1028 /* this stream was already orphaned */
Willy Tarreau0a10de62018-03-01 16:27:53 +01001029 h2s_detach(h2s);
1030 h2s_free(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001031 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001032 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001033
1034 h2s->cs->flags |= flags;
1035 /* recv is used to force to detect CS_FL_EOS that wake()
1036 * doesn't handle in the stream int code.
1037 */
1038 h2s->cs->data_cb->recv(h2s->cs);
1039 h2s->cs->data_cb->wake(h2s->cs);
1040
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001041 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1042 h2s->st = H2_SS_ERROR;
1043 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1044 h2s->st = H2_SS_HREM;
1045 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001046 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001047 }
1048}
1049
Willy Tarreau3421aba2017-07-27 15:41:03 +02001050/* Increase all streams' outgoing window size by the difference passed in
1051 * argument. This is needed upon receipt of the settings frame if the initial
1052 * window size is different. The difference may be negative and the resulting
1053 * window size as well, for the time it takes to receive some window updates.
1054 */
1055static void h2c_update_all_ws(struct h2c *h2c, int diff)
1056{
1057 struct h2s *h2s;
1058 struct eb32_node *node;
1059
1060 if (!diff)
1061 return;
1062
1063 node = eb32_first(&h2c->streams_by_id);
1064 while (node) {
1065 h2s = container_of(node, struct h2s, by_id);
1066 h2s->mws += diff;
1067 node = eb32_next(node);
1068 }
1069}
1070
1071/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1072 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1073 * return an error in h2c. Described in RFC7540#6.5.
1074 */
1075static int h2c_handle_settings(struct h2c *h2c)
1076{
1077 unsigned int offset;
1078 int error;
1079
1080 if (h2c->dff & H2_F_SETTINGS_ACK) {
1081 if (h2c->dfl) {
1082 error = H2_ERR_FRAME_SIZE_ERROR;
1083 goto fail;
1084 }
1085 return 1;
1086 }
1087
1088 if (h2c->dsi != 0) {
1089 error = H2_ERR_PROTOCOL_ERROR;
1090 goto fail;
1091 }
1092
1093 if (h2c->dfl % 6) {
1094 error = H2_ERR_FRAME_SIZE_ERROR;
1095 goto fail;
1096 }
1097
1098 /* that's the limit we can process */
1099 if (h2c->dfl > global.tune.bufsize) {
1100 error = H2_ERR_FRAME_SIZE_ERROR;
1101 goto fail;
1102 }
1103
1104 /* process full frame only */
1105 if (h2c->dbuf->i < h2c->dfl)
1106 return 0;
1107
1108 /* parse the frame */
1109 for (offset = 0; offset < h2c->dfl; offset += 6) {
1110 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1111 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1112
1113 switch (type) {
1114 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1115 /* we need to update all existing streams with the
1116 * difference from the previous iws.
1117 */
1118 if (arg < 0) { // RFC7540#6.5.2
1119 error = H2_ERR_FLOW_CONTROL_ERROR;
1120 goto fail;
1121 }
1122 h2c_update_all_ws(h2c, arg - h2c->miw);
1123 h2c->miw = arg;
1124 break;
1125 case H2_SETTINGS_MAX_FRAME_SIZE:
1126 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1127 error = H2_ERR_PROTOCOL_ERROR;
1128 goto fail;
1129 }
1130 h2c->mfs = arg;
1131 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001132 case H2_SETTINGS_ENABLE_PUSH:
1133 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1134 error = H2_ERR_PROTOCOL_ERROR;
1135 goto fail;
1136 }
1137 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001138 }
1139 }
1140
1141 /* need to ACK this frame now */
1142 h2c->st0 = H2_CS_FRAME_A;
1143 return 1;
1144 fail:
1145 h2c_error(h2c, error);
1146 return 0;
1147}
1148
1149/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1150 * success or one of the h2_status values.
1151 */
1152static int h2c_ack_settings(struct h2c *h2c)
1153{
1154 struct buffer *res;
1155 char str[9];
1156 int ret = -1;
1157
1158 if (h2c_mux_busy(h2c, NULL)) {
1159 h2c->flags |= H2_CF_DEM_MBUSY;
1160 return 0;
1161 }
1162
Willy Tarreau44e973f2018-03-01 17:49:30 +01001163 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001164 if (!res) {
1165 h2c->flags |= H2_CF_MUX_MALLOC;
1166 h2c->flags |= H2_CF_DEM_MROOM;
1167 return 0;
1168 }
1169
1170 memcpy(str,
1171 "\x00\x00\x00" /* length : 0 (no data) */
1172 "\x04" "\x01" /* type : 4, flags : ACK */
1173 "\x00\x00\x00\x00" /* stream ID */, 9);
1174
1175 ret = bo_istput(res, ist2(str, 9));
1176 if (unlikely(ret <= 0)) {
1177 if (!ret) {
1178 h2c->flags |= H2_CF_MUX_MFULL;
1179 h2c->flags |= H2_CF_DEM_MROOM;
1180 return 0;
1181 }
1182 else {
1183 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1184 return 0;
1185 }
1186 }
1187 return ret;
1188}
1189
Willy Tarreaucf68c782017-10-10 17:11:41 +02001190/* processes a PING frame and schedules an ACK if needed. The caller must pass
1191 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1192 * missing data. It may return an error in h2c.
1193 */
1194static int h2c_handle_ping(struct h2c *h2c)
1195{
1196 /* frame length must be exactly 8 */
1197 if (h2c->dfl != 8) {
1198 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1199 return 0;
1200 }
1201
1202 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001203 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001204 h2c->st0 = H2_CS_FRAME_A;
1205 return 1;
1206}
1207
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001208/* Try to send a window update for stream id <sid> and value <increment>.
1209 * Returns > 0 on success or zero on missing room or failure. It may return an
1210 * error in h2c.
1211 */
1212static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1213{
1214 struct buffer *res;
1215 char str[13];
1216 int ret = -1;
1217
1218 if (h2c_mux_busy(h2c, NULL)) {
1219 h2c->flags |= H2_CF_DEM_MBUSY;
1220 return 0;
1221 }
1222
Willy Tarreau44e973f2018-03-01 17:49:30 +01001223 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001224 if (!res) {
1225 h2c->flags |= H2_CF_MUX_MALLOC;
1226 h2c->flags |= H2_CF_DEM_MROOM;
1227 return 0;
1228 }
1229
1230 /* length: 4, type: 8, flags: none */
1231 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1232 write_n32(str + 5, sid);
1233 write_n32(str + 9, increment);
1234
1235 ret = bo_istput(res, ist2(str, 13));
1236
1237 if (unlikely(ret <= 0)) {
1238 if (!ret) {
1239 h2c->flags |= H2_CF_MUX_MFULL;
1240 h2c->flags |= H2_CF_DEM_MROOM;
1241 return 0;
1242 }
1243 else {
1244 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1245 return 0;
1246 }
1247 }
1248 return ret;
1249}
1250
1251/* try to send pending window update for the connection. It's safe to call it
1252 * with no pending updates. Returns > 0 on success or zero on missing room or
1253 * failure. It may return an error in h2c.
1254 */
1255static int h2c_send_conn_wu(struct h2c *h2c)
1256{
1257 int ret = 1;
1258
1259 if (h2c->rcvd_c <= 0)
1260 return 1;
1261
1262 /* send WU for the connection */
1263 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1264 if (ret > 0)
1265 h2c->rcvd_c = 0;
1266
1267 return ret;
1268}
1269
1270/* try to send pending window update for the current dmux stream. It's safe to
1271 * call it with no pending updates. Returns > 0 on success or zero on missing
1272 * room or failure. It may return an error in h2c.
1273 */
1274static int h2c_send_strm_wu(struct h2c *h2c)
1275{
1276 int ret = 1;
1277
1278 if (h2c->rcvd_s <= 0)
1279 return 1;
1280
1281 /* send WU for the stream */
1282 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1283 if (ret > 0)
1284 h2c->rcvd_s = 0;
1285
1286 return ret;
1287}
1288
Willy Tarreaucf68c782017-10-10 17:11:41 +02001289/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1290 * success, 0 on missing data or one of the h2_status values.
1291 */
1292static int h2c_ack_ping(struct h2c *h2c)
1293{
1294 struct buffer *res;
1295 char str[17];
1296 int ret = -1;
1297
1298 if (h2c->dbuf->i < 8)
1299 return 0;
1300
1301 if (h2c_mux_busy(h2c, NULL)) {
1302 h2c->flags |= H2_CF_DEM_MBUSY;
1303 return 0;
1304 }
1305
Willy Tarreau44e973f2018-03-01 17:49:30 +01001306 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001307 if (!res) {
1308 h2c->flags |= H2_CF_MUX_MALLOC;
1309 h2c->flags |= H2_CF_DEM_MROOM;
1310 return 0;
1311 }
1312
1313 memcpy(str,
1314 "\x00\x00\x08" /* length : 8 (same payload) */
1315 "\x06" "\x01" /* type : 6, flags : ACK */
1316 "\x00\x00\x00\x00" /* stream ID */, 9);
1317
1318 /* copy the original payload */
1319 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1320
1321 ret = bo_istput(res, ist2(str, 17));
1322 if (unlikely(ret <= 0)) {
1323 if (!ret) {
1324 h2c->flags |= H2_CF_MUX_MFULL;
1325 h2c->flags |= H2_CF_DEM_MROOM;
1326 return 0;
1327 }
1328 else {
1329 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1330 return 0;
1331 }
1332 }
1333 return ret;
1334}
1335
Willy Tarreau26f95952017-07-27 17:18:30 +02001336/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1337 * Returns > 0 on success or zero on missing data. It may return an error in
1338 * h2c or h2s. Described in RFC7540#6.9.
1339 */
1340static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1341{
1342 int32_t inc;
1343 int error;
1344
1345 if (h2c->dfl != 4) {
1346 error = H2_ERR_FRAME_SIZE_ERROR;
1347 goto conn_err;
1348 }
1349
1350 /* process full frame only */
1351 if (h2c->dbuf->i < h2c->dfl)
1352 return 0;
1353
1354 inc = h2_get_n32(h2c->dbuf, 0);
1355
1356 if (h2c->dsi != 0) {
1357 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001358
1359 /* it's not an error to receive WU on a closed stream */
1360 if (h2s->st == H2_SS_CLOSED)
1361 return 1;
1362
1363 if (!inc) {
1364 error = H2_ERR_PROTOCOL_ERROR;
1365 goto strm_err;
1366 }
1367
1368 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1369 error = H2_ERR_FLOW_CONTROL_ERROR;
1370 goto strm_err;
1371 }
1372
1373 h2s->mws += inc;
1374 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1375 h2s->flags &= ~H2_SF_BLK_SFCTL;
1376 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1377 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1378 /* This stream wanted to send but could not due to its
1379 * own flow control. We can put it back into the send
1380 * list now, it will be handled upon next send() call.
1381 */
1382 LIST_ADDQ(&h2c->send_list, &h2s->list);
1383 }
1384 }
1385 }
1386 else {
1387 /* connection window update */
1388 if (!inc) {
1389 error = H2_ERR_PROTOCOL_ERROR;
1390 goto conn_err;
1391 }
1392
1393 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1394 error = H2_ERR_FLOW_CONTROL_ERROR;
1395 goto conn_err;
1396 }
1397
1398 h2c->mws += inc;
1399 }
1400
1401 return 1;
1402
1403 conn_err:
1404 h2c_error(h2c, error);
1405 return 0;
1406
1407 strm_err:
1408 if (h2s) {
1409 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001410 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001411 }
1412 else
1413 h2c_error(h2c, error);
1414 return 0;
1415}
1416
Willy Tarreaue96b0922017-10-30 00:28:29 +01001417/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1418 * the last ID. Returns > 0 on success or zero on missing data. It may return
1419 * an error in h2c. Described in RFC7540#6.8.
1420 */
1421static int h2c_handle_goaway(struct h2c *h2c)
1422{
1423 int error;
1424 int last;
1425
1426 if (h2c->dsi != 0) {
1427 error = H2_ERR_PROTOCOL_ERROR;
1428 goto conn_err;
1429 }
1430
1431 if (h2c->dfl < 8) {
1432 error = H2_ERR_FRAME_SIZE_ERROR;
1433 goto conn_err;
1434 }
1435
1436 /* process full frame only */
1437 if (h2c->dbuf->i < h2c->dfl)
1438 return 0;
1439
1440 last = h2_get_n32(h2c->dbuf, 0);
1441 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1442 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001443 if (h2c->last_sid < 0)
1444 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001445 return 1;
1446
1447 conn_err:
1448 h2c_error(h2c, error);
1449 return 0;
1450}
1451
Willy Tarreau92153fc2017-12-03 19:46:19 +01001452/* processes a PRIORITY frame, and either skips it or rejects if it is
1453 * invalid. Returns > 0 on success or zero on missing data. It may return
1454 * an error in h2c. Described in RFC7540#6.3.
1455 */
1456static int h2c_handle_priority(struct h2c *h2c)
1457{
1458 int error;
1459
1460 if (h2c->dsi == 0) {
1461 error = H2_ERR_PROTOCOL_ERROR;
1462 goto conn_err;
1463 }
1464
1465 if (h2c->dfl != 5) {
1466 error = H2_ERR_FRAME_SIZE_ERROR;
1467 goto conn_err;
1468 }
1469
1470 /* process full frame only */
1471 if (h2c->dbuf->i < h2c->dfl)
1472 return 0;
1473
1474 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1475 /* 7540#5.3 : can't depend on itself */
1476 error = H2_ERR_PROTOCOL_ERROR;
1477 goto conn_err;
1478 }
1479 return 1;
1480
1481 conn_err:
1482 h2c_error(h2c, error);
1483 return 0;
1484}
1485
Willy Tarreaucd234e92017-08-18 10:59:39 +02001486/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1487 * Returns > 0 on success or zero on missing data. It may return an error in
1488 * h2c. Described in RFC7540#6.4.
1489 */
1490static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1491{
1492 int error;
1493
1494 if (h2c->dsi == 0) {
1495 error = H2_ERR_PROTOCOL_ERROR;
1496 goto conn_err;
1497 }
1498
Willy Tarreaucd234e92017-08-18 10:59:39 +02001499 if (h2c->dfl != 4) {
1500 error = H2_ERR_FRAME_SIZE_ERROR;
1501 goto conn_err;
1502 }
1503
1504 /* process full frame only */
1505 if (h2c->dbuf->i < h2c->dfl)
1506 return 0;
1507
1508 /* late RST, already handled */
1509 if (h2s->st == H2_SS_CLOSED)
1510 return 1;
1511
1512 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001513 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001514
1515 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001516 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001517 /* recv is used to force to detect CS_FL_EOS that wake()
1518 * doesn't handle in the stream-int code.
1519 */
1520 h2s->cs->data_cb->recv(h2s->cs);
1521 h2s->cs->data_cb->wake(h2s->cs);
1522 }
1523
1524 h2s->flags |= H2_SF_RST_RCVD;
1525 return 1;
1526
1527 conn_err:
1528 h2c_error(h2c, error);
1529 return 0;
1530}
1531
Willy Tarreau13278b42017-10-13 19:23:14 +02001532/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1533 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1534 * errors here are reported as connection errors since it's impossible to
1535 * recover from such errors after the compression context has been altered.
1536 */
1537static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1538{
1539 int error;
1540
1541 if (!h2c->dfl) {
1542 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1543 goto strm_err;
1544 }
1545
1546 if (!h2c->dbuf->size)
1547 return 0; // empty buffer
1548
1549 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1550 return 0; // incomplete frame
1551
1552 /* now either the frame is complete or the buffer is complete */
1553 if (h2s->st != H2_SS_IDLE) {
1554 /* FIXME: stream already exists, this is only allowed for
1555 * trailers (not supported for now).
1556 */
1557 error = H2_ERR_PROTOCOL_ERROR;
1558 goto conn_err;
1559 }
1560 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1561 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1562 error = H2_ERR_PROTOCOL_ERROR;
1563 goto conn_err;
1564 }
1565
1566 h2s = h2c_stream_new(h2c, h2c->dsi);
1567 if (!h2s) {
1568 error = H2_ERR_INTERNAL_ERROR;
1569 goto conn_err;
1570 }
1571
1572 h2s->st = H2_SS_OPEN;
1573 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1574 h2s->st = H2_SS_HREM;
1575 h2s->flags |= H2_SF_ES_RCVD;
1576 }
1577
1578 /* call the upper layers to process the frame, then let the upper layer
1579 * notify the stream about any change.
1580 */
1581 h2s->cs->data_cb->recv(h2s->cs);
1582
1583 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1584 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1585 error = H2_ERR_INTERNAL_ERROR;
1586 goto conn_err;
1587 }
1588
Willy Tarreau8f650c32017-11-21 19:36:21 +01001589 if (h2c->st0 >= H2_CS_ERROR)
1590 return 0;
1591
Willy Tarreau721c9742017-11-07 11:05:42 +01001592 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001593 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001594 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001595 }
1596 else {
1597 /* update the max stream ID if the request is being processed */
1598 if (h2s->id > h2c->max_id)
1599 h2c->max_id = h2s->id;
1600 }
1601
1602 return 1;
1603
1604 conn_err:
1605 h2c_error(h2c, error);
1606 return 0;
1607
1608 strm_err:
1609 if (h2s) {
1610 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001611 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001612 }
1613 else
1614 h2c_error(h2c, error);
1615 return 0;
1616}
1617
Willy Tarreau454f9052017-10-26 19:40:35 +02001618/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1619 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1620 */
1621static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1622{
1623 int error;
1624
1625 /* note that empty DATA frames are perfectly valid and sometimes used
1626 * to signal an end of stream (with the ES flag).
1627 */
1628
1629 if (!h2c->dbuf->size && h2c->dfl)
1630 return 0; // empty buffer
1631
1632 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1633 return 0; // incomplete frame
1634
1635 /* now either the frame is complete or the buffer is complete */
1636
1637 if (!h2c->dsi) {
1638 /* RFC7540#6.1 */
1639 error = H2_ERR_PROTOCOL_ERROR;
1640 goto conn_err;
1641 }
1642
1643 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1644 /* RFC7540#6.1 */
1645 error = H2_ERR_STREAM_CLOSED;
1646 goto strm_err;
1647 }
1648
Willy Tarreau454f9052017-10-26 19:40:35 +02001649 /* call the upper layers to process the frame, then let the upper layer
1650 * notify the stream about any change.
1651 */
1652 if (!h2s->cs) {
1653 error = H2_ERR_STREAM_CLOSED;
1654 goto strm_err;
1655 }
1656
1657 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001658
Willy Tarreau454f9052017-10-26 19:40:35 +02001659 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1660 /* cs has just been destroyed, we have to kill h2s. */
1661 error = H2_ERR_STREAM_CLOSED;
1662 goto strm_err;
1663 }
1664
Willy Tarreau8f650c32017-11-21 19:36:21 +01001665 if (h2c->st0 >= H2_CS_ERROR)
1666 return 0;
1667
Willy Tarreau721c9742017-11-07 11:05:42 +01001668 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001669 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001670 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001671 }
1672
1673 /* check for completion : the callee will change this to FRAME_A or
1674 * FRAME_H once done.
1675 */
1676 if (h2c->st0 == H2_CS_FRAME_P)
1677 return 0;
1678
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001679
1680 /* last frame */
1681 if (h2c->dff & H2_F_DATA_END_STREAM) {
1682 h2s->st = H2_SS_HREM;
1683 h2s->flags |= H2_SF_ES_RCVD;
1684 }
1685
Willy Tarreau454f9052017-10-26 19:40:35 +02001686 return 1;
1687
1688 conn_err:
1689 h2c_error(h2c, error);
1690 return 0;
1691
1692 strm_err:
1693 if (h2s) {
1694 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001695 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001696 }
1697 else
1698 h2c_error(h2c, error);
1699 return 0;
1700}
1701
Willy Tarreaubc933932017-10-09 16:21:43 +02001702/* process Rx frames to be demultiplexed */
1703static void h2_process_demux(struct h2c *h2c)
1704{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001705 struct h2s *h2s;
1706
Willy Tarreau081d4722017-05-16 21:51:05 +02001707 if (h2c->st0 >= H2_CS_ERROR)
1708 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001709
1710 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1711 if (h2c->st0 == H2_CS_PREFACE) {
1712 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1713 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1714 if (h2c->st0 == H2_CS_ERROR)
1715 h2c->st0 = H2_CS_ERROR2;
1716 goto fail;
1717 }
1718
1719 h2c->max_id = 0;
1720 h2c->st0 = H2_CS_SETTINGS1;
1721 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001722
1723 if (h2c->st0 == H2_CS_SETTINGS1) {
1724 struct h2_fh hdr;
1725
1726 /* ensure that what is pending is a valid SETTINGS frame
1727 * without an ACK.
1728 */
1729 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1730 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1731 if (h2c->st0 == H2_CS_ERROR)
1732 h2c->st0 = H2_CS_ERROR2;
1733 goto fail;
1734 }
1735
1736 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1737 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1738 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1739 h2c->st0 = H2_CS_ERROR2;
1740 goto fail;
1741 }
1742
1743 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1744 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1745 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1746 h2c->st0 = H2_CS_ERROR2;
1747 goto fail;
1748 }
1749
1750 /* that's OK, switch to FRAME_P to process it */
1751 h2c->dfl = hdr.len;
1752 h2c->dsi = hdr.sid;
1753 h2c->dft = hdr.ft;
1754 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001755 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001756 h2c->st0 = H2_CS_FRAME_P;
1757 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001758 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001759
1760 /* process as many incoming frames as possible below */
1761 while (h2c->dbuf->i) {
1762 int ret = 0;
1763
1764 if (h2c->st0 >= H2_CS_ERROR)
1765 break;
1766
1767 if (h2c->st0 == H2_CS_FRAME_H) {
1768 struct h2_fh hdr;
1769
1770 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1771 break;
1772
1773 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1774 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1775 h2c->st0 = H2_CS_ERROR;
1776 break;
1777 }
1778
1779 h2c->dfl = hdr.len;
1780 h2c->dsi = hdr.sid;
1781 h2c->dft = hdr.ft;
1782 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001783 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001784 h2c->st0 = H2_CS_FRAME_P;
1785 h2_skip_frame_hdr(h2c->dbuf);
1786 }
1787
1788 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001789 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001790
Willy Tarreaud7901432017-12-29 11:34:40 +01001791 if (h2c->st0 == H2_CS_FRAME_E)
1792 goto strm_err;
1793
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001794 if (h2s->st == H2_SS_IDLE &&
1795 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1796 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1797 * this state MUST be treated as a connection error
1798 */
1799 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1800 h2c->st0 = H2_CS_ERROR;
1801 break;
1802 }
1803
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001804 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1805 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1806 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1807 * this state MUST be treated as a stream error
1808 */
1809 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001810 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001811 goto strm_err;
1812 }
1813
Willy Tarreauab837502017-12-27 15:07:30 +01001814 /* Below the management of frames received in closed state is a
1815 * bit hackish because the spec makes strong differences between
1816 * streams closed by receiving RST, sending RST, and seeing ES
1817 * in both directions. In addition to this, the creation of a
1818 * new stream reusing the identifier of a closed one will be
1819 * detected here. Given that we cannot keep track of all closed
1820 * streams forever, we consider that unknown closed streams were
1821 * closed on RST received, which allows us to respond with an
1822 * RST without breaking the connection (eg: to abort a transfer).
1823 * Some frames have to be silently ignored as well.
1824 */
1825 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1826 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1827 /* #5.1.1: The identifier of a newly
1828 * established stream MUST be numerically
1829 * greater than all streams that the initiating
1830 * endpoint has opened or reserved. This
1831 * governs streams that are opened using a
1832 * HEADERS frame and streams that are reserved
1833 * using PUSH_PROMISE. An endpoint that
1834 * receives an unexpected stream identifier
1835 * MUST respond with a connection error.
1836 */
1837 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1838 goto strm_err;
1839 }
1840
1841 if (h2s->flags & H2_SF_RST_RCVD) {
1842 /* RFC7540#5.1:closed: an endpoint that
1843 * receives any frame other than PRIORITY after
1844 * receiving a RST_STREAM MUST treat that as a
1845 * stream error of type STREAM_CLOSED.
1846 *
1847 * Note that old streams fall into this category
1848 * and will lead to an RST being sent.
1849 */
1850 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1851 h2c->st0 = H2_CS_FRAME_E;
1852 goto strm_err;
1853 }
1854
1855 /* RFC7540#5.1:closed: if this state is reached as a
1856 * result of sending a RST_STREAM frame, the peer that
1857 * receives the RST_STREAM might have already sent
1858 * frames on the stream that cannot be withdrawn. An
1859 * endpoint MUST ignore frames that it receives on
1860 * closed streams after it has sent a RST_STREAM
1861 * frame. An endpoint MAY choose to limit the period
1862 * over which it ignores frames and treat frames that
1863 * arrive after this time as being in error.
1864 */
1865 if (!(h2s->flags & H2_SF_RST_SENT)) {
1866 /* RFC7540#5.1:closed: any frame other than
1867 * PRIO/WU/RST in this state MUST be treated as
1868 * a connection error
1869 */
1870 if (h2c->dft != H2_FT_RST_STREAM &&
1871 h2c->dft != H2_FT_PRIORITY &&
1872 h2c->dft != H2_FT_WINDOW_UPDATE) {
1873 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1874 goto strm_err;
1875 }
1876 }
1877 }
1878
Willy Tarreauc0da1962017-10-30 18:38:00 +01001879#if 0
1880 // problem below: it is not possible to completely ignore such
1881 // streams as we need to maintain the compression state as well
1882 // and for this we need to completely process these frames (eg:
1883 // HEADERS frames) as well as counting DATA frames to emit
1884 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1885 // This is a typical case of layer violation where the
1886 // transported contents are critical to the connection's
1887 // validity and must be ignored at the same time :-(
1888
1889 /* graceful shutdown, ignore streams whose ID is higher than
1890 * the one advertised in GOAWAY. RFC7540#6.8.
1891 */
1892 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1893 ret = MIN(h2c->dbuf->i, h2c->dfl);
1894 bi_del(h2c->dbuf, ret);
1895 h2c->dfl -= ret;
1896 ret = h2c->dfl == 0;
1897 goto strm_err;
1898 }
1899#endif
1900
Willy Tarreau7e98c052017-10-10 15:56:59 +02001901 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001902 case H2_FT_SETTINGS:
1903 if (h2c->st0 == H2_CS_FRAME_P)
1904 ret = h2c_handle_settings(h2c);
1905
1906 if (h2c->st0 == H2_CS_FRAME_A)
1907 ret = h2c_ack_settings(h2c);
1908 break;
1909
Willy Tarreaucf68c782017-10-10 17:11:41 +02001910 case H2_FT_PING:
1911 if (h2c->st0 == H2_CS_FRAME_P)
1912 ret = h2c_handle_ping(h2c);
1913
1914 if (h2c->st0 == H2_CS_FRAME_A)
1915 ret = h2c_ack_ping(h2c);
1916 break;
1917
Willy Tarreau26f95952017-07-27 17:18:30 +02001918 case H2_FT_WINDOW_UPDATE:
1919 if (h2c->st0 == H2_CS_FRAME_P)
1920 ret = h2c_handle_window_update(h2c, h2s);
1921 break;
1922
Willy Tarreau61290ec2017-10-17 08:19:21 +02001923 case H2_FT_CONTINUATION:
1924 /* we currently don't support CONTINUATION frames since
1925 * we have nowhere to store the partial HEADERS frame.
1926 * Let's abort the stream on an INTERNAL_ERROR here.
1927 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001928 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001929 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001930 h2c->st0 = H2_CS_FRAME_E;
1931 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001932 break;
1933
Willy Tarreau13278b42017-10-13 19:23:14 +02001934 case H2_FT_HEADERS:
1935 if (h2c->st0 == H2_CS_FRAME_P)
1936 ret = h2c_frt_handle_headers(h2c, h2s);
1937 break;
1938
Willy Tarreau454f9052017-10-26 19:40:35 +02001939 case H2_FT_DATA:
1940 if (h2c->st0 == H2_CS_FRAME_P)
1941 ret = h2c_frt_handle_data(h2c, h2s);
1942
1943 if (h2c->st0 == H2_CS_FRAME_A)
1944 ret = h2c_send_strm_wu(h2c);
1945 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001946
Willy Tarreau92153fc2017-12-03 19:46:19 +01001947 case H2_FT_PRIORITY:
1948 if (h2c->st0 == H2_CS_FRAME_P)
1949 ret = h2c_handle_priority(h2c);
1950 break;
1951
Willy Tarreaucd234e92017-08-18 10:59:39 +02001952 case H2_FT_RST_STREAM:
1953 if (h2c->st0 == H2_CS_FRAME_P)
1954 ret = h2c_handle_rst_stream(h2c, h2s);
1955 break;
1956
Willy Tarreaue96b0922017-10-30 00:28:29 +01001957 case H2_FT_GOAWAY:
1958 if (h2c->st0 == H2_CS_FRAME_P)
1959 ret = h2c_handle_goaway(h2c);
1960 break;
1961
Willy Tarreau1c661982017-10-30 13:52:01 +01001962 case H2_FT_PUSH_PROMISE:
1963 /* not permitted here, RFC7540#5.1 */
1964 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001965 break;
1966
1967 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001968 default:
1969 /* drop frames that we ignore. They may be larger than
1970 * the buffer so we drain all of their contents until
1971 * we reach the end.
1972 */
1973 ret = MIN(h2c->dbuf->i, h2c->dfl);
1974 bi_del(h2c->dbuf, ret);
1975 h2c->dfl -= ret;
1976 ret = h2c->dfl == 0;
1977 }
1978
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001979 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01001980 /* We may have to send an RST if not done yet */
1981 if (h2s->st == H2_SS_ERROR)
1982 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001983
Willy Tarreaua20a5192017-12-27 11:02:06 +01001984 if (h2c->st0 == H2_CS_FRAME_E)
1985 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001986
Willy Tarreau7e98c052017-10-10 15:56:59 +02001987 /* error or missing data condition met above ? */
1988 if (ret <= 0)
1989 break;
1990
1991 if (h2c->st0 != H2_CS_FRAME_H) {
1992 bi_del(h2c->dbuf, h2c->dfl);
1993 h2c->st0 = H2_CS_FRAME_H;
1994 }
1995 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001996
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001997 if (h2c->rcvd_c > 0 &&
1998 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1999 h2c_send_conn_wu(h2c);
2000
Willy Tarreau52eed752017-09-22 15:05:09 +02002001 fail:
2002 /* we can go here on missing data, blocked response or error */
2003 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002004}
2005
2006/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2007 * the end.
2008 */
2009static int h2_process_mux(struct h2c *h2c)
2010{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002011 struct h2s *h2s, *h2s_back;
2012
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002013 /* start by sending possibly pending window updates */
2014 if (h2c->rcvd_c > 0 &&
2015 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2016 h2c_send_conn_wu(h2c) < 0)
2017 goto fail;
2018
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002019 /* First we always process the flow control list because the streams
2020 * waiting there were already elected for immediate emission but were
2021 * blocked just on this.
2022 */
2023
2024 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2025 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2026 h2c->st0 >= H2_CS_ERROR)
2027 break;
2028
2029 /* In theory it's possible that h2s->cs == NULL here :
2030 * - client sends crap that causes a parse error
2031 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2032 * - RST_STREAM cannot be emitted because mux is busy/full
2033 * - stream gets notified, detaches and quits
2034 * - mux buffer gets ready and wakes pending streams up
2035 * - bam!
2036 */
2037 h2s->flags &= ~H2_SF_BLK_ANY;
2038
2039 if (h2s->cs) {
2040 h2s->cs->data_cb->send(h2s->cs);
2041 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002042 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002043 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002044 }
2045
2046 /* depending on callee's blocking reasons, we may queue in send
2047 * list or completely dequeue.
2048 */
2049 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2050 if (h2s->flags & H2_SF_BLK_ANY) {
2051 LIST_DEL(&h2s->list);
2052 LIST_ADDQ(&h2c->send_list, &h2s->list);
2053 }
2054 else {
2055 LIST_DEL(&h2s->list);
2056 LIST_INIT(&h2s->list);
2057 if (h2s->cs)
2058 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002059 else {
2060 /* just sent the last frame for this orphaned stream */
Willy Tarreau0a10de62018-03-01 16:27:53 +01002061 h2s_detach(h2s);
2062 h2s_free(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002063 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002064 }
2065 }
2066 }
2067
2068 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2069 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2070 break;
2071
2072 /* In theory it's possible that h2s->cs == NULL here :
2073 * - client sends crap that causes a parse error
2074 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2075 * - RST_STREAM cannot be emitted because mux is busy/full
2076 * - stream gets notified, detaches and quits
2077 * - mux buffer gets ready and wakes pending streams up
2078 * - bam!
2079 */
2080 h2s->flags &= ~H2_SF_BLK_ANY;
2081
2082 if (h2s->cs) {
2083 h2s->cs->data_cb->send(h2s->cs);
2084 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002085 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002086 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002087 }
2088 /* depending on callee's blocking reasons, we may queue in fctl
2089 * list or completely dequeue.
2090 */
2091 if (h2s->flags & H2_SF_BLK_MFCTL) {
2092 /* stream hit the connection's flow control */
2093 LIST_DEL(&h2s->list);
2094 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2095 }
2096 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2097 LIST_DEL(&h2s->list);
2098 LIST_INIT(&h2s->list);
2099 if (h2s->cs)
2100 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002101 else {
2102 /* just sent the last frame for this orphaned stream */
Willy Tarreau0a10de62018-03-01 16:27:53 +01002103 h2s_detach(h2s);
2104 h2s_free(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002105 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002106 }
2107 }
2108
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002109 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002110 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002111 if (h2c->st0 == H2_CS_ERROR) {
2112 if (h2c->max_id >= 0) {
2113 h2c_send_goaway_error(h2c, NULL);
2114 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2115 return 0;
2116 }
2117
2118 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2119 }
2120 return 1;
2121 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002122 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002123}
2124
Willy Tarreau71681172017-10-23 14:39:06 +02002125
Willy Tarreau62f52692017-10-08 23:01:42 +02002126/*********************************************************/
2127/* functions below are I/O callbacks from the connection */
2128/*********************************************************/
2129
2130/* callback called on recv event by the connection handler */
2131static void h2_recv(struct connection *conn)
2132{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002133 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002134 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002135 int max;
2136
Willy Tarreau315d8072017-12-10 22:17:57 +01002137 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002138 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002139
Willy Tarreau44e973f2018-03-01 17:49:30 +01002140 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002141 if (!buf) {
2142 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002143 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002144 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002145
Willy Tarreaua2af5122017-10-09 11:56:46 +02002146 /* note: buf->o == 0 */
2147 max = buf->size - buf->i;
Willy Tarreau315d8072017-12-10 22:17:57 +01002148 if (max)
2149 conn->xprt->rcv_buf(conn, buf, max);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002150
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002151 if (!buf->i) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002152 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002153 return;
2154 }
2155
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002156 if (buf->i == buf->size)
2157 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002158 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002159}
2160
2161/* callback called on send event by the connection handler */
2162static void h2_send(struct connection *conn)
2163{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002164 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002165 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002166
2167 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002168 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002169
2170 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2171 /* a handshake was requested */
2172 return;
2173 }
2174
Willy Tarreaubc933932017-10-09 16:21:43 +02002175 /* This loop is quite simple : it tries to fill as much as it can from
2176 * pending streams into the existing buffer until it's reportedly full
2177 * or the end of send requests is reached. Then it tries to send this
2178 * buffer's contents out, marks it not full if at least one byte could
2179 * be sent, and tries again.
2180 *
2181 * The snd_buf() function normally takes a "flags" argument which may
2182 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2183 * data immediately comes and CO_SFL_STREAMER to indicate that the
2184 * connection is streaming lots of data (used to increase TLS record
2185 * size at the expense of latency). The former can be sent any time
2186 * there's a buffer full flag, as it indicates at least one stream
2187 * attempted to send and failed so there are pending data. An
2188 * alternative would be to set it as long as there's an active stream
2189 * but that would be problematic for ACKs until we have an absolute
2190 * guarantee that all waiters have at least one byte to send. The
2191 * latter should possibly not be set for now.
2192 */
2193
2194 done = 0;
2195 while (!done) {
2196 unsigned int flags = 0;
2197
2198 /* fill as much as we can into the current buffer */
2199 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2200 done = h2_process_mux(h2c);
2201
2202 if (conn->flags & CO_FL_ERROR)
2203 break;
2204
2205 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2206 flags |= CO_SFL_MSG_MORE;
2207
Willy Tarreau319994a2017-11-07 11:03:56 +01002208 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002209 break;
2210
2211 /* wrote at least one byte, the buffer is not full anymore */
2212 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2213 }
2214
Willy Tarreaua2af5122017-10-09 11:56:46 +02002215 if (conn->flags & CO_FL_SOCK_WR_SH) {
2216 /* output closed, nothing to send, clear the buffer to release it */
2217 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002218 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002219}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002220
Willy Tarreau62f52692017-10-08 23:01:42 +02002221/* callback called on any event by the connection handler.
2222 * It applies changes and returns zero, or < 0 if it wants immediate
2223 * destruction of the connection (which normally doesn not happen in h2).
2224 */
2225static int h2_wake(struct connection *conn)
2226{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002227 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002228 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002229
Willy Tarreaud13bf272017-12-14 10:34:52 +01002230 if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
2231 h2_process_demux(h2c);
2232
2233 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
2234 h2c->dbuf->i = 0;
2235
2236 if (h2c->dbuf->i != h2c->dbuf->size)
2237 h2c->flags &= ~H2_CF_DEM_DFULL;
2238 }
2239
Willy Tarreau8ec14062017-12-30 18:08:13 +01002240 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2241 /* frontend is stopping, reload likely in progress, let's try
2242 * to announce a graceful shutdown if not yet done. We don't
2243 * care if it fails, it will be tried again later.
2244 */
2245 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2246 if (h2c->last_sid < 0)
2247 h2c->last_sid = (1U << 31) - 1;
2248 h2c_send_goaway_error(h2c, NULL);
2249 }
2250 }
2251
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002252 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002253 * If we received early data, and the handshake is done, wake
2254 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002255 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002256 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2257 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2258 struct eb32_node *node;
2259 struct h2s *h2s;
2260
2261 h2c->flags |= H2_CF_WAIT_FOR_HS;
2262 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2263
2264 while (node) {
2265 h2s = container_of(node, struct h2s, by_id);
2266 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2267 h2s->cs->data_cb->wake(h2s->cs);
2268 node = eb32_next(node);
2269 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002270 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002271
Willy Tarreau26bd7612017-10-09 16:47:04 +02002272 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002273 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2274 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2275 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002276 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002277
2278 if (eb_is_empty(&h2c->streams_by_id)) {
2279 /* no more stream, kill the connection now */
2280 h2_release(conn);
2281 return -1;
2282 }
2283 else {
2284 /* some streams still there, we need to signal them all and
2285 * wait for their departure.
2286 */
2287 __conn_xprt_stop_recv(conn);
2288 __conn_xprt_stop_send(conn);
2289 return 0;
2290 }
2291 }
2292
2293 if (!h2c->dbuf->i)
Willy Tarreau44e973f2018-03-01 17:49:30 +01002294 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002295
2296 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002297 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002298 __conn_xprt_stop_recv(conn);
2299 }
2300 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002301 __conn_xprt_want_recv(conn);
2302 }
2303
2304 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002305 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2306 (h2c->st0 == H2_CS_ERROR ||
2307 h2c->mbuf->o ||
2308 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2309 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002310 __conn_xprt_want_send(conn);
2311 }
2312 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002313 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002314 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002315 }
2316
Willy Tarreau3f133572017-10-31 19:21:06 +01002317 if (h2c->task) {
Willy Tarreau84b118f2018-03-05 16:10:54 +01002318 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002319 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002320 task_queue(h2c->task);
2321 }
2322 else
2323 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002324 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002325 return 0;
2326}
2327
Willy Tarreauea392822017-10-31 10:02:25 +01002328/* Connection timeout management. The principle is that if there's no receipt
2329 * nor sending for a certain amount of time, the connection is closed. If the
2330 * MUX buffer still has lying data or is not allocatable, the connection is
2331 * immediately killed. If it's allocatable and empty, we attempt to send a
2332 * GOAWAY frame.
2333 */
2334static struct task *h2_timeout_task(struct task *t)
2335{
2336 struct h2c *h2c = t->context;
2337 int expired = tick_is_expired(t->expire, now_ms);
2338
2339 if (!expired)
2340 return t;
2341
2342 h2c_error(h2c, H2_ERR_NO_ERROR);
2343 h2_wake_some_streams(h2c, 0, 0);
2344
2345 if (h2c->mbuf->o) {
2346 /* don't even try to send a GOAWAY, the buffer is stuck */
2347 h2c->flags |= H2_CF_GOAWAY_FAILED;
2348 }
2349
2350 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002351 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002352 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2353 h2c->flags |= H2_CF_GOAWAY_FAILED;
2354
2355 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2356 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2357
2358 if (!eb_is_empty(&h2c->streams_by_id))
2359 goto wait;
2360
2361 h2_release(h2c->conn);
2362 return NULL;
2363
2364 wait:
2365 /* the streams have been notified, we must let them finish and close */
2366 h2c->task = NULL;
2367 task_delete(t);
2368 task_free(t);
2369 return NULL;
2370}
2371
2372
Willy Tarreau62f52692017-10-08 23:01:42 +02002373/*******************************************/
2374/* functions below are used by the streams */
2375/*******************************************/
2376
2377/*
2378 * Attach a new stream to a connection
2379 * (Used for outgoing connections)
2380 */
2381static struct conn_stream *h2_attach(struct connection *conn)
2382{
2383 return NULL;
2384}
2385
2386/* callback used to update the mux's polling flags after changing a cs' status.
2387 * The caller (cs_update_mux_polling) will take care of propagating any changes
2388 * to the transport layer.
2389 */
2390static void h2_update_poll(struct conn_stream *cs)
2391{
Willy Tarreau1d393222017-10-17 10:26:19 +02002392 struct h2s *h2s = cs->ctx;
2393
2394 if (!h2s)
2395 return;
2396
Willy Tarreaud7739c82017-10-30 15:38:23 +01002397 /* we may unblock a blocked read */
2398
Willy Tarreau315d8072017-12-10 22:17:57 +01002399 if (cs->flags & CS_FL_DATA_RD_ENA) {
2400 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002401 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002402 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002403 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002404 conn_xprt_want_send(cs->conn);
2405 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002406 }
2407
Willy Tarreau1d393222017-10-17 10:26:19 +02002408 /* Note: the stream and stream-int code doesn't allow us to perform a
2409 * synchronous send() here unfortunately, because this code is called
2410 * as si_update() from the process_stream() context. This means that
2411 * we have to queue the current cs and defer its processing after the
2412 * connection's cs list is processed anyway.
2413 */
2414
2415 if (cs->flags & CS_FL_DATA_WR_ENA) {
2416 if (LIST_ISEMPTY(&h2s->list)) {
2417 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2418 !h2s->h2c->mbuf->o && // not yet subscribed
2419 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2420 conn_xprt_want_send(cs->conn);
2421 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2422 }
2423 }
2424 else if (!LIST_ISEMPTY(&h2s->list)) {
2425 LIST_DEL(&h2s->list);
2426 LIST_INIT(&h2s->list);
2427 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2428 }
2429
2430 /* this can happen from within si_chk_snd() */
2431 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2432 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002433}
2434
2435/*
2436 * Detach the stream from the connection and possibly release the connection.
2437 */
2438static void h2_detach(struct conn_stream *cs)
2439{
Willy Tarreau60935142017-10-16 18:11:19 +02002440 struct h2s *h2s = cs->ctx;
2441 struct h2c *h2c;
2442
2443 cs->ctx = NULL;
2444 if (!h2s)
2445 return;
2446
2447 h2c = h2s->h2c;
2448 h2s->cs = NULL;
2449
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002450 /* this stream may be blocked waiting for some data to leave (possibly
2451 * an ES or RST frame), so orphan it in this case.
2452 */
2453 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2454 return;
2455
Willy Tarreau541dd822017-11-23 18:12:50 +01002456 /* the stream could be in the send list */
2457 LIST_DEL(&h2s->list);
2458
Willy Tarreau45f752e2017-10-30 15:44:59 +01002459 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2460 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2461 /* unblock the connection if it was blocked on this
2462 * stream.
2463 */
2464 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2465 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2466 conn_xprt_want_recv(cs->conn);
2467 conn_xprt_want_send(cs->conn);
2468 }
2469
Willy Tarreau60935142017-10-16 18:11:19 +02002470 if (h2s->by_id.node.leaf_p) {
2471 /* h2s still attached to the h2c */
Willy Tarreau0a10de62018-03-01 16:27:53 +01002472 h2s_detach(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002473
2474 /* We don't want to close right now unless we're removing the
2475 * last stream, and either the connection is in error, or it
2476 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002477 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002478 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002479 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2480 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002481 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002482 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2483 (conn_xprt_read0_pending(h2c->conn) ||
2484 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002485 /* no more stream will come, kill it now */
2486 h2_release(h2c->conn);
2487 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002488 else if (h2c->task) {
Willy Tarreau84b118f2018-03-05 16:10:54 +01002489 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002490 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002491 task_queue(h2c->task);
2492 }
2493 else
2494 h2c->task->expire = TICK_ETERNITY;
2495 }
Willy Tarreau60935142017-10-16 18:11:19 +02002496 }
Willy Tarreau0a10de62018-03-01 16:27:53 +01002497 h2s_free(h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002498}
2499
2500static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2501{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002502 struct h2s *h2s = cs->ctx;
2503
2504 if (!mode)
2505 return;
2506
Willy Tarreau721c9742017-11-07 11:05:42 +01002507 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002508 return;
2509
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002510 /* if no outgoing data was seen on this stream, it means it was
2511 * closed with a "tcp-request content" rule that is normally
2512 * used to kill the connection ASAP (eg: limit abuse). In this
2513 * case we send a goaway to close the connection.
2514 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002515 if (!(h2s->flags & H2_SF_RST_SENT) &&
2516 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2517 return;
2518
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002519 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2520 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2521 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2522 return;
2523
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002524 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2525 conn_xprt_want_send(cs->conn);
2526
Willy Tarreau00dd0782018-03-01 16:31:34 +01002527 h2s_close(h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002528}
2529
2530static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2531{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002532 struct h2s *h2s = cs->ctx;
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 Tarreau67434202017-11-06 20:20:51 +01002537 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002538 /* we can cleanly close using an empty data frame only after headers */
2539
2540 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2541 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002542 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002543
2544 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002545 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002546 else
2547 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002548 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002549 /* if no outgoing data was seen on this stream, it means it was
2550 * closed with a "tcp-request content" rule that is normally
2551 * used to kill the connection ASAP (eg: limit abuse). In this
2552 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002553 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002554 if (!(h2s->flags & H2_SF_RST_SENT) &&
2555 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2556 return;
2557
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002558 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2559 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002560 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2561 return;
2562
Willy Tarreau00dd0782018-03-01 16:31:34 +01002563 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002564 }
2565
2566 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2567 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002568}
2569
Willy Tarreau13278b42017-10-13 19:23:14 +02002570/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2571 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2572 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002573 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002574 */
2575static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2576{
2577 struct h2c *h2c = h2s->h2c;
2578 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002579 struct chunk *tmp = get_trash_chunk();
2580 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002581 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002582 int flen = h2c->dfl;
2583 int outlen = 0;
2584 int wrap;
2585 int try;
2586
2587 if (!h2c->dfl) {
2588 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002589 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002590 return 0;
2591 }
2592
Willy Tarreau68472622017-12-11 18:36:37 +01002593 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2594 return 0; // incomplete input frame
2595
Willy Tarreau13278b42017-10-13 19:23:14 +02002596 /* if the input buffer wraps, take a temporary copy of it (rare) */
2597 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2598 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002599 copy = alloc_trash_chunk();
2600 if (!copy) {
2601 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2602 goto fail;
2603 }
2604 memcpy(copy->str, h2c->dbuf->p, wrap);
2605 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2606 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002607 }
2608
2609 /* The padlen is the first byte before data, and the padding appears
2610 * after data. padlen+data+padding are included in flen.
2611 */
2612 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002613 h2c->dpl = *hdrs;
2614 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002615 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2616 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002617 return 0;
2618 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002619 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002620 hdrs += 1; // skip Pad Length
2621 }
2622
2623 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2624 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002625 if (read_n32(hdrs) == h2s->id) {
2626 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2627 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2628 return 0;//goto fail_stream;
2629 }
2630
Willy Tarreau13278b42017-10-13 19:23:14 +02002631 hdrs += 5; // stream dep = 4, weight = 1
2632 flen -= 5;
2633 }
2634
2635 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2636 * don't support this for now and can't even decompress so we have to
2637 * break the connection.
2638 */
2639 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2640 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002641 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002642 }
2643
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002644 /* we can't retry a failed decompression operation so we must be very
2645 * careful not to take any risks. In practice the output buffer is
2646 * always empty except maybe for trailers, so these operations almost
2647 * never happen.
2648 */
2649 if (unlikely(buf->o)) {
2650 /* need to let the output buffer flush and
2651 * mark the buffer for later wake up.
2652 */
2653 goto fail;
2654 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002655
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002656 if (unlikely(buffer_space_wraps(buf))) {
2657 /* it doesn't fit and the buffer is fragmented,
2658 * so let's defragment it and try again.
2659 */
2660 buffer_slow_realign(buf);
2661 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002662
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002663 /* first check if we have some room after p+i */
2664 try = buf->data + buf->size - (buf->p + buf->i);
2665
2666 /* otherwise continue between data and p-o */
2667 if (try <= 0) {
2668 try = buf->p - (buf->data + buf->o);
2669 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002670 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002671 }
2672 if (try > count)
2673 try = count;
2674
2675 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2676 sizeof(list)/sizeof(list[0]), tmp);
2677 if (outlen < 0) {
2678 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2679 goto fail;
2680 }
2681
2682 /* OK now we have our header list in <list> */
2683 outlen = h2_make_h1_request(list, bi_end(buf), try);
2684
2685 if (outlen < 0) {
2686 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2687 goto fail;
2688 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002689
2690 /* now consume the input data */
2691 bi_del(h2c->dbuf, h2c->dfl);
2692 h2c->st0 = H2_CS_FRAME_H;
2693 buf->i += outlen;
2694
2695 /* don't send it before returning data!
2696 * FIXME: should we instead try to send it much later, after the
2697 * response ? This would require that we keep a copy of it in h2s.
2698 */
2699 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2700 h2s->cs->flags |= CS_FL_EOS;
2701 h2s->flags |= H2_SF_ES_RCVD;
2702 }
2703
Willy Tarreau68dd9852017-07-03 14:44:26 +02002704 leave:
2705 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002706 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002707 fail:
2708 outlen = 0;
2709 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002710}
2711
Willy Tarreau454f9052017-10-26 19:40:35 +02002712/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2713 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2714 * in use, a new chunk is emitted for each frame. This is supposed to fit
2715 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2716 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2717 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2718 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002719 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2720 * checked to know if some data remain pending (an empty DATA frame can return
2721 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2722 * connection errors in h2c->errcode. The caller must already have checked the
2723 * frame header and ensured that the frame was complete or the buffer full. It
2724 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002725 */
2726static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2727{
2728 struct h2c *h2c = h2s->h2c;
2729 int block1, block2;
2730 unsigned int flen = h2c->dfl;
Willy Tarreau454f9052017-10-26 19:40:35 +02002731
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002732 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002733 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002734
2735 /* The padlen is the first byte before data, and the padding appears
2736 * after data. padlen+data+padding are included in flen.
2737 */
Willy Tarreau79127812017-12-03 21:06:59 +01002738 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002739 if (h2c->dbuf->i < 1)
2740 return 0;
2741
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002742 h2c->dpl = *(uint8_t *)bi_ptr(h2c->dbuf);
2743 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002744 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2745 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002746 return 0;
2747 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002748
2749 /* skip the padlen byte */
2750 bi_del(h2c->dbuf, 1);
2751 h2c->dfl--;
2752 h2c->rcvd_c++; h2c->rcvd_s++;
2753 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002754 }
2755
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002756 flen = h2c->dfl - h2c->dpl;
2757 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002758 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002759
2760 if (flen > h2c->dbuf->i) {
2761 flen = h2c->dbuf->i;
2762 if (!flen)
2763 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002764 }
2765
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002766 /* does it fit in output buffer or should we wait ? */
2767 if (flen > count) {
2768 flen = count;
2769 if (!flen) {
2770 h2c->flags |= H2_CF_DEM_SFULL;
2771 h2s->cs->flags |= CS_FL_RCV_MORE;
2772 return 0;
2773 }
2774 }
2775
Willy Tarreau454f9052017-10-26 19:40:35 +02002776 /* Block1 is the length of the first block before the buffer wraps,
2777 * block2 is the optional second block to reach the end of the frame.
2778 */
2779 block1 = bi_contig_data(h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002780 if (block1 > flen)
2781 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002782 block2 = flen - block1;
2783
2784 if (block1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002785 bi_putblk(buf, b_ptr(h2c->dbuf, 0), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002786
2787 if (block2)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002788 bi_putblk(buf, b_ptr(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002789
2790 /* now mark the input data as consumed (will be deleted from the buffer
2791 * by the caller when seeing FRAME_A after sending the window update).
2792 */
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002793 bi_del(h2c->dbuf, flen);
2794 h2c->dfl -= flen;
2795 h2c->rcvd_c += flen;
2796 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2797
2798 if (h2c->dfl > h2c->dpl) {
2799 /* more data available, transfer stalled on stream full */
2800 h2c->flags |= H2_CF_DEM_SFULL;
2801 h2s->cs->flags |= CS_FL_RCV_MORE;
2802 return flen;
2803 }
2804
Willy Tarreau4a28da12018-01-04 14:41:00 +01002805 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002806 /* here we're done with the frame, all the payload (except padding) was
2807 * transferred.
2808 */
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002809 h2c->rcvd_c += h2c->dpl;
2810 h2c->rcvd_s += h2c->dpl;
2811 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002812 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2813
2814 /* don't send it before returning data!
2815 * FIXME: should we instead try to send it much later, after the
2816 * response ? This would require that we keep a copy of it in h2s.
2817 */
Willy Tarreau79127812017-12-03 21:06:59 +01002818 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002819 h2s->cs->flags |= CS_FL_EOS;
2820 h2s->flags |= H2_SF_ES_RCVD;
2821 }
2822
2823 return flen;
2824}
2825
Willy Tarreau62f52692017-10-08 23:01:42 +02002826/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002827 * Called from the upper layer to get more data, up to <count> bytes. The
2828 * caller is responsible for never asking for more data than what is available
2829 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002830 */
2831static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2832{
Willy Tarreau13278b42017-10-13 19:23:14 +02002833 struct h2s *h2s = cs->ctx;
2834 struct h2c *h2c = h2s->h2c;
2835 int ret = 0;
2836
2837 if (h2c->st0 != H2_CS_FRAME_P)
2838 return 0; // no pre-parsed frame yet
2839
2840 if (h2c->dsi != h2s->id)
2841 return 0; // not for us
2842
2843 if (!h2c->dbuf->size)
2844 return 0; // empty buffer
2845
Willy Tarreau13278b42017-10-13 19:23:14 +02002846 switch (h2c->dft) {
2847 case H2_FT_HEADERS:
2848 ret = h2_frt_decode_headers(h2s, buf, count);
2849 break;
2850
Willy Tarreau454f9052017-10-26 19:40:35 +02002851 case H2_FT_DATA:
2852 ret = h2_frt_transfer_data(h2s, buf, count);
2853 break;
2854
Willy Tarreau13278b42017-10-13 19:23:14 +02002855 default:
2856 ret = 0;
2857 }
2858 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002859}
2860
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002861/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2862 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2863 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2864 * to the number of buffer bytes consumed.
2865 */
2866static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2867{
2868 struct http_hdr list[MAX_HTTP_HDR];
2869 struct h2c *h2c = h2s->h2c;
2870 struct h1m *h1m = &h2s->res;
2871 struct chunk outbuf;
2872 int es_now = 0;
2873 int ret = 0;
2874 int hdr;
2875
2876 if (h2c_mux_busy(h2c, h2s)) {
2877 h2s->flags |= H2_SF_BLK_MBUSY;
2878 return 0;
2879 }
2880
Willy Tarreau44e973f2018-03-01 17:49:30 +01002881 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002882 h2c->flags |= H2_CF_MUX_MALLOC;
2883 h2s->flags |= H2_SF_BLK_MROOM;
2884 return 0;
2885 }
2886
2887 /* First, try to parse the H1 response and index it into <list>.
2888 * NOTE! Since it comes from haproxy, we *know* that a response header
2889 * block does not wrap and we can safely read it this way without
2890 * having to realign the buffer.
2891 */
2892 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2893 list, sizeof(list)/sizeof(list[0]), h1m);
2894 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002895 /* incomplete or invalid response, this is abnormal coming from
2896 * haproxy and may only result in a bad errorfile or bad Lua code
2897 * so that won't be fixed, raise an error now.
2898 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002899 * FIXME: we should instead add the ability to only return a
2900 * 502 bad gateway. But in theory this is not supposed to
2901 * happen.
2902 */
2903 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2904 ret = 0;
2905 goto end;
2906 }
2907
2908 chunk_reset(&outbuf);
2909
2910 while (1) {
2911 outbuf.str = bo_end(h2c->mbuf);
2912 outbuf.size = bo_contig_space(h2c->mbuf);
2913 outbuf.len = 0;
2914
2915 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2916 break;
2917 realign_again:
2918 buffer_slow_realign(h2c->mbuf);
2919 }
2920
2921 if (outbuf.size < 9) {
2922 h2c->flags |= H2_CF_MUX_MFULL;
2923 h2s->flags |= H2_SF_BLK_MROOM;
2924 ret = 0;
2925 goto end;
2926 }
2927
2928 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2929 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2930 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2931 outbuf.len = 9;
2932
2933 /* encode status, which necessarily is the first one */
2934 if (outbuf.len < outbuf.size && h1m->status == 200)
2935 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2936 else if (outbuf.len < outbuf.size && h1m->status == 304)
2937 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002938 else if (unlikely(list[0].v.len != 3)) {
2939 /* this is an unparsable response */
2940 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2941 ret = 0;
2942 goto end;
2943 }
2944 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002945 /* basic encoding of the status code */
2946 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2947 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2948 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2949 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2950 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2951 }
2952 else {
2953 if (buffer_space_wraps(h2c->mbuf))
2954 goto realign_again;
2955
2956 h2c->flags |= H2_CF_MUX_MFULL;
2957 h2s->flags |= H2_SF_BLK_MROOM;
2958 ret = 0;
2959 goto end;
2960 }
2961
2962 /* encode all headers, stop at empty name */
2963 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002964 /* these ones do not exist in H2 and must be dropped. */
2965 if (isteq(list[hdr].n, ist("connection")) ||
2966 isteq(list[hdr].n, ist("proxy-connection")) ||
2967 isteq(list[hdr].n, ist("keep-alive")) ||
2968 isteq(list[hdr].n, ist("upgrade")) ||
2969 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002970 continue;
2971
2972 if (isteq(list[hdr].n, ist("")))
2973 break; // end
2974
2975 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2976 /* output full */
2977 if (buffer_space_wraps(h2c->mbuf))
2978 goto realign_again;
2979
2980 h2c->flags |= H2_CF_MUX_MFULL;
2981 h2s->flags |= H2_SF_BLK_MROOM;
2982 ret = 0;
2983 goto end;
2984 }
2985 }
2986
2987 /* we may need to add END_STREAM */
2988 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2989 es_now = 1;
2990
2991 /* update the frame's size */
2992 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2993
2994 if (es_now)
2995 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2996
2997 /* consume incoming H1 response */
2998 bo_del(buf, ret);
2999
3000 /* commit the H2 response */
3001 h2c->mbuf->o += outbuf.len;
3002 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01003003 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003004
3005 /* for now we don't implemented CONTINUATION, so we wait for a
3006 * body or directly end in TRL2.
3007 */
3008 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003009 // trim any possibly pending data (eg: inconsistent content-length)
3010 bo_del(buf, buf->o);
3011
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003012 h1m->state = HTTP_MSG_DONE;
3013 h2s->flags |= H2_SF_ES_SENT;
3014 if (h2s->st == H2_SS_OPEN)
3015 h2s->st = H2_SS_HLOC;
3016 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003017 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003018 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003019 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003020 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003021 h1m->state = HTTP_MSG_RPBEFORE;
3022 h1m->status = 0;
3023 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003024 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003025 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003026 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003027 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003028
3029 end:
3030 //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));
3031 return ret;
3032}
3033
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003034/* Try to send a DATA frame matching HTTP/1 response present in the response
3035 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
3036 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
3037 * corresponds to the number of buffer bytes consumed.
3038 */
3039static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
3040{
3041 struct h2c *h2c = h2s->h2c;
3042 struct h1m *h1m = &h2s->res;
3043 struct chunk outbuf;
3044 int ret = 0;
3045 int total = 0;
3046 int es_now = 0;
3047 int size = 0;
3048 char *blk1, *blk2;
3049 int len1, len2;
3050
3051 if (h2c_mux_busy(h2c, h2s)) {
3052 h2s->flags |= H2_SF_BLK_MBUSY;
3053 goto end;
3054 }
3055
Willy Tarreau44e973f2018-03-01 17:49:30 +01003056 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003057 h2c->flags |= H2_CF_MUX_MALLOC;
3058 h2s->flags |= H2_SF_BLK_MROOM;
3059 goto end;
3060 }
3061
3062 new_frame:
3063 if (!buf->o)
3064 goto end;
3065
3066 chunk_reset(&outbuf);
3067
3068 while (1) {
3069 outbuf.str = bo_end(h2c->mbuf);
3070 outbuf.size = bo_contig_space(h2c->mbuf);
3071 outbuf.len = 0;
3072
3073 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
3074 break;
3075 realign_again:
3076 buffer_slow_realign(h2c->mbuf);
3077 }
3078
3079 if (outbuf.size < 9) {
3080 h2c->flags |= H2_CF_MUX_MFULL;
3081 h2s->flags |= H2_SF_BLK_MROOM;
3082 goto end;
3083 }
3084
3085 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3086 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3087 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3088 outbuf.len = 9;
3089
3090 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3091 case 0: /* no content length, read till SHUTW */
3092 size = buf->o;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003093 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003094 break;
3095 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
3096 size = buf->o;
3097 if ((long long)size > h1m->curr_len)
3098 size = h1m->curr_len;
3099 break;
3100 default: /* te:chunked : parse chunks */
3101 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
3102 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
3103 if (!ret)
3104 goto end;
3105
3106 if (ret < 0) {
3107 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3108 h1m->err_pos = ret;
3109 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3110 goto end;
3111 }
3112 bo_del(buf, ret);
3113 total += ret;
3114 h1m->state = HTTP_MSG_CHUNK_SIZE;
3115 }
3116
3117 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3118 unsigned int chunk;
3119
3120 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
3121 if (!ret)
3122 goto end;
3123
3124 if (ret < 0) {
3125 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3126 h1m->err_pos = ret;
3127 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3128 goto end;
3129 }
3130
3131 size = chunk;
3132 h1m->curr_len = chunk;
3133 h1m->body_len += chunk;
3134 bo_del(buf, ret);
3135 total += ret;
3136 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3137 if (!size)
3138 goto send_empty;
3139 }
3140
3141 /* in MSG_DATA state, continue below */
3142 size = h1m->curr_len;
3143 break;
3144 }
3145
3146 /* we have in <size> the exact number of bytes we need to copy from
3147 * the H1 buffer. We need to check this against the connection's and
3148 * the stream's send windows, and to ensure that this fits in the max
3149 * frame size and in the buffer's available space minus 9 bytes (for
3150 * the frame header). The connection's flow control is applied last so
3151 * that we can use a separate list of streams which are immediately
3152 * unblocked on window opening. Note: we don't implement padding.
3153 */
3154
3155 if (size > buf->o)
3156 size = buf->o;
3157
3158 if (size > h2s->mws)
3159 size = h2s->mws;
3160
3161 if (size <= 0) {
3162 h2s->flags |= H2_SF_BLK_SFCTL;
3163 goto end;
3164 }
3165
3166 if (h2c->mfs && size > h2c->mfs)
3167 size = h2c->mfs;
3168
3169 if (size + 9 > outbuf.size) {
3170 /* we have an opportunity for enlarging the too small
3171 * available space, let's try.
3172 */
3173 if (buffer_space_wraps(h2c->mbuf))
3174 goto realign_again;
3175 size = outbuf.size - 9;
3176 }
3177
3178 if (size <= 0) {
3179 h2c->flags |= H2_CF_MUX_MFULL;
3180 h2s->flags |= H2_SF_BLK_MROOM;
3181 goto end;
3182 }
3183
3184 if (size > h2c->mws)
3185 size = h2c->mws;
3186
3187 if (size <= 0) {
3188 h2s->flags |= H2_SF_BLK_MFCTL;
3189 goto end;
3190 }
3191
3192 /* copy whatever we can */
3193 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
3194 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
3195 if (ret == 1)
3196 len2 = 0;
3197
3198 if (!ret || len1 + len2 < size) {
3199 /* FIXME: must normally never happen */
3200 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3201 goto end;
3202 }
3203
3204 /* limit len1/len2 to size */
3205 if (len1 + len2 > size) {
3206 int sub = len1 + len2 - size;
3207
3208 if (len2 > sub)
3209 len2 -= sub;
3210 else {
3211 sub -= len2;
3212 len2 = 0;
3213 len1 -= sub;
3214 }
3215 }
3216
3217 /* now let's copy this this into the output buffer */
3218 memcpy(outbuf.str + 9, blk1, len1);
3219 if (len2)
3220 memcpy(outbuf.str + 9 + len1, blk2, len2);
3221
3222 send_empty:
3223 /* we may need to add END_STREAM */
3224 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3225 * could rely on the MSG_MORE flag as a hint for this ?
3226 */
3227 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3228 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3229 es_now = 1;
3230
3231 /* update the frame's size */
3232 h2_set_frame_size(outbuf.str, size);
3233
3234 if (es_now)
3235 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3236
3237 /* commit the H2 response */
3238 h2c->mbuf->o += size + 9;
3239 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3240
3241 /* consume incoming H1 response */
3242 if (size > 0) {
3243 bo_del(buf, size);
3244 total += size;
3245 h1m->curr_len -= size;
3246 h2s->mws -= size;
3247 h2c->mws -= size;
3248
3249 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3250 h1m->state = HTTP_MSG_CHUNK_CRLF;
3251 goto new_frame;
3252 }
3253 }
3254
3255 if (es_now) {
3256 if (h2s->st == H2_SS_OPEN)
3257 h2s->st = H2_SS_HLOC;
3258 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003259 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003260
Willy Tarreau35a62702018-02-27 15:37:25 +01003261 if (!(h1m->flags & H1_MF_CHNK)) {
3262 // trim any possibly pending data (eg: inconsistent content-length)
3263 bo_del(buf, buf->o);
3264
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003265 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003266 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003267
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003268 h2s->flags |= H2_SF_ES_SENT;
3269 }
3270
3271 end:
3272 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);
3273 return total;
3274}
3275
Willy Tarreau62f52692017-10-08 23:01:42 +02003276/* Called from the upper layer, to send data */
3277static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3278{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003279 struct h2s *h2s = cs->ctx;
3280 int total = 0;
3281
Willy Tarreauc4312d32017-11-07 12:01:53 +01003282 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3283 h2s->flags |= H2_SF_OUTGOING_DATA;
3284
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003285 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3286 if (h2s->res.state < HTTP_MSG_BODY) {
3287 total += h2s_frt_make_resp_headers(h2s, buf);
3288
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003289 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003290 break;
3291
3292 if (h2s->flags & H2_SF_BLK_ANY)
3293 break;
3294 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003295 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3296 total += h2s_frt_make_resp_data(h2s, buf);
3297
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003298 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003299 break;
3300
3301 if (h2s->flags & H2_SF_BLK_ANY)
3302 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003303 }
3304 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3305 /* consume the trailers if any (we don't forward them for now) */
3306 int count = h1_measure_trailers(buf);
3307
3308 if (unlikely(count <= 0)) {
3309 if (count < 0)
3310 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3311 break;
3312 }
3313 total += count;
3314 bo_del(buf, count);
Willy Tarreau35a62702018-02-27 15:37:25 +01003315
3316 // trim any possibly pending data (eg: extra CR-LF, ...)
3317 bo_del(buf, buf->o);
3318
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003319 h2s->res.state = HTTP_MSG_DONE;
3320 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003321 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003322 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003323 cs->flags |= CS_FL_ERROR;
3324 break;
3325 }
3326 }
3327
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003328 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003329 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003330 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003331 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003332 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003333 }
3334
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003335 if (h2s->flags & H2_SF_BLK_SFCTL) {
3336 /* stream flow control, quit the list */
3337 LIST_DEL(&h2s->list);
3338 LIST_INIT(&h2s->list);
3339 }
3340
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003341 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003342}
3343
3344
3345/*******************************************************/
3346/* functions below are dedicated to the config parsers */
3347/*******************************************************/
3348
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003349/* config parser for global "tune.h2.header-table-size" */
3350static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3351 struct proxy *defpx, const char *file, int line,
3352 char **err)
3353{
3354 if (too_many_args(1, args, err, NULL))
3355 return -1;
3356
3357 h2_settings_header_table_size = atoi(args[1]);
3358 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3359 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3360 return -1;
3361 }
3362 return 0;
3363}
Willy Tarreau62f52692017-10-08 23:01:42 +02003364
Willy Tarreaue6baec02017-07-27 11:45:11 +02003365/* config parser for global "tune.h2.initial-window-size" */
3366static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3367 struct proxy *defpx, const char *file, int line,
3368 char **err)
3369{
3370 if (too_many_args(1, args, err, NULL))
3371 return -1;
3372
3373 h2_settings_initial_window_size = atoi(args[1]);
3374 if (h2_settings_initial_window_size < 0) {
3375 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3376 return -1;
3377 }
3378 return 0;
3379}
3380
Willy Tarreau5242ef82017-07-27 11:47:28 +02003381/* config parser for global "tune.h2.max-concurrent-streams" */
3382static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3383 struct proxy *defpx, const char *file, int line,
3384 char **err)
3385{
3386 if (too_many_args(1, args, err, NULL))
3387 return -1;
3388
3389 h2_settings_max_concurrent_streams = atoi(args[1]);
3390 if (h2_settings_max_concurrent_streams < 0) {
3391 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3392 return -1;
3393 }
3394 return 0;
3395}
3396
Willy Tarreau62f52692017-10-08 23:01:42 +02003397
3398/****************************************/
3399/* MUX initialization and instanciation */
3400/***************************************/
3401
3402/* The mux operations */
3403const struct mux_ops h2_ops = {
3404 .init = h2_init,
3405 .recv = h2_recv,
3406 .send = h2_send,
3407 .wake = h2_wake,
3408 .update_poll = h2_update_poll,
3409 .rcv_buf = h2_rcv_buf,
3410 .snd_buf = h2_snd_buf,
3411 .attach = h2_attach,
3412 .detach = h2_detach,
3413 .shutr = h2_shutr,
3414 .shutw = h2_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003415 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003416 .name = "H2",
3417};
3418
3419/* ALPN selection : this mux registers ALPN tolen "h2" */
3420static struct alpn_mux_list alpn_mux_h2 =
3421 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3422
3423/* config keyword parsers */
3424static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003425 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003426 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003427 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003428 { 0, NULL, NULL }
3429}};
3430
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003431static void __h2_deinit(void)
3432{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003433 pool_destroy(pool_head_h2s);
3434 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003435}
3436
Willy Tarreau62f52692017-10-08 23:01:42 +02003437__attribute__((constructor))
3438static void __h2_init(void)
3439{
3440 alpn_register_mux(&alpn_mux_h2);
3441 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003442 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003443 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3444 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003445}