blob: 03f6e0e41be83110c6b11591cc6a871bff2aba91 [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) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200436 h2c->task->context = NULL;
437 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100438 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 Tarreau71049cc2018-03-28 13:56:39 +0200593/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
594static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100595{
596 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200597 LIST_DEL(&h2s->list);
598 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100599 eb32_delete(&h2s->by_id);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100600 pool_free(pool_head_h2s, h2s);
601}
602
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200603/* creates a new stream <id> on the h2c connection and returns it, or NULL in
604 * case of memory allocation error.
605 */
606static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
607{
608 struct conn_stream *cs;
609 struct h2s *h2s;
610
Willy Tarreaubafbe012017-11-24 17:34:44 +0100611 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200612 if (!h2s)
613 goto out;
614
615 h2s->h2c = h2c;
616 h2s->mws = h2c->miw;
617 h2s->flags = H2_SF_NONE;
618 h2s->errcode = H2_ERR_NO_ERROR;
619 h2s->st = H2_SS_IDLE;
620 h1m_init(&h2s->req);
621 h1m_init(&h2s->res);
622 h2s->by_id.key = h2s->id = id;
623 h2c->max_id = id;
624 LIST_INIT(&h2s->list);
625
626 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100627 h2c->nb_streams++;
628 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
629 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200630
631 cs = cs_new(h2c->conn);
632 if (!cs)
633 goto out_close;
634
635 h2s->cs = cs;
636 cs->ctx = h2s;
637
638 if (stream_create_from_cs(cs) < 0)
639 goto out_free_cs;
640
641 /* OK done, the stream lives its own life now */
642 return h2s;
643
644 out_free_cs:
645 cs_free(cs);
646 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200647 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200648 h2s = NULL;
649 out:
650 return h2s;
651}
652
Willy Tarreaube5b7152017-09-25 16:25:39 +0200653/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
654 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
655 * the various settings codes.
656 */
657static int h2c_snd_settings(struct h2c *h2c)
658{
659 struct buffer *res;
660 char buf_data[100]; // enough for 15 settings
661 struct chunk buf;
662 int ret;
663
664 if (h2c_mux_busy(h2c, NULL)) {
665 h2c->flags |= H2_CF_DEM_MBUSY;
666 return 0;
667 }
668
Willy Tarreau44e973f2018-03-01 17:49:30 +0100669 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200670 if (!res) {
671 h2c->flags |= H2_CF_MUX_MALLOC;
672 h2c->flags |= H2_CF_DEM_MROOM;
673 return 0;
674 }
675
676 chunk_init(&buf, buf_data, sizeof(buf_data));
677 chunk_memcpy(&buf,
678 "\x00\x00\x00" /* length : 0 for now */
679 "\x04\x00" /* type : 4 (settings), flags : 0 */
680 "\x00\x00\x00\x00", /* stream ID : 0 */
681 9);
682
683 if (h2_settings_header_table_size != 4096) {
684 char str[6] = "\x00\x01"; /* header_table_size */
685
686 write_n32(str + 2, h2_settings_header_table_size);
687 chunk_memcat(&buf, str, 6);
688 }
689
690 if (h2_settings_initial_window_size != 65535) {
691 char str[6] = "\x00\x04"; /* initial_window_size */
692
693 write_n32(str + 2, h2_settings_initial_window_size);
694 chunk_memcat(&buf, str, 6);
695 }
696
697 if (h2_settings_max_concurrent_streams != 0) {
698 char str[6] = "\x00\x03"; /* max_concurrent_streams */
699
700 /* Note: 0 means "unlimited" for haproxy's config but not for
701 * the protocol, so never send this value!
702 */
703 write_n32(str + 2, h2_settings_max_concurrent_streams);
704 chunk_memcat(&buf, str, 6);
705 }
706
707 if (global.tune.bufsize != 16384) {
708 char str[6] = "\x00\x05"; /* max_frame_size */
709
710 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
711 * match bufsize - rewrite size, but at the moment it seems
712 * that clients don't take care of it.
713 */
714 write_n32(str + 2, global.tune.bufsize);
715 chunk_memcat(&buf, str, 6);
716 }
717
718 h2_set_frame_size(buf.str, buf.len - 9);
719 ret = bo_istput(res, ist2(buf.str, buf.len));
720 if (unlikely(ret <= 0)) {
721 if (!ret) {
722 h2c->flags |= H2_CF_MUX_MFULL;
723 h2c->flags |= H2_CF_DEM_MROOM;
724 return 0;
725 }
726 else {
727 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
728 return 0;
729 }
730 }
731 return ret;
732}
733
Willy Tarreau52eed752017-09-22 15:05:09 +0200734/* Try to receive a connection preface, then upon success try to send our
735 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
736 * missing data. It may return an error in h2c.
737 */
738static int h2c_frt_recv_preface(struct h2c *h2c)
739{
740 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200741 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200742
743 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
744
745 if (unlikely(ret1 <= 0)) {
746 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
747 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
748 return 0;
749 }
750
Willy Tarreaube5b7152017-09-25 16:25:39 +0200751 ret2 = h2c_snd_settings(h2c);
752 if (ret2 > 0)
753 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200754
Willy Tarreaube5b7152017-09-25 16:25:39 +0200755 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200756}
757
Willy Tarreau081d4722017-05-16 21:51:05 +0200758/* try to send a GOAWAY frame on the connection to report an error or a graceful
759 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
760 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
761 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
762 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
763 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
764 * on unrecoverable failure. It will not attempt to send one again in this last
765 * case so that it is safe to use h2c_error() to report such errors.
766 */
767static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
768{
769 struct buffer *res;
770 char str[17];
771 int ret;
772
773 if (h2c->flags & H2_CF_GOAWAY_FAILED)
774 return 1; // claim that it worked
775
776 if (h2c_mux_busy(h2c, h2s)) {
777 if (h2s)
778 h2s->flags |= H2_SF_BLK_MBUSY;
779 else
780 h2c->flags |= H2_CF_DEM_MBUSY;
781 return 0;
782 }
783
Willy Tarreau44e973f2018-03-01 17:49:30 +0100784 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200785 if (!res) {
786 h2c->flags |= H2_CF_MUX_MALLOC;
787 if (h2s)
788 h2s->flags |= H2_SF_BLK_MROOM;
789 else
790 h2c->flags |= H2_CF_DEM_MROOM;
791 return 0;
792 }
793
794 /* len: 8, type: 7, flags: none, sid: 0 */
795 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
796
797 if (h2c->last_sid < 0)
798 h2c->last_sid = h2c->max_id;
799
800 write_n32(str + 9, h2c->last_sid);
801 write_n32(str + 13, h2c->errcode);
802 ret = bo_istput(res, ist2(str, 17));
803 if (unlikely(ret <= 0)) {
804 if (!ret) {
805 h2c->flags |= H2_CF_MUX_MFULL;
806 if (h2s)
807 h2s->flags |= H2_SF_BLK_MROOM;
808 else
809 h2c->flags |= H2_CF_DEM_MROOM;
810 return 0;
811 }
812 else {
813 /* we cannot report this error using GOAWAY, so we mark
814 * it and claim a success.
815 */
816 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
817 h2c->flags |= H2_CF_GOAWAY_FAILED;
818 return 1;
819 }
820 }
821 h2c->flags |= H2_CF_GOAWAY_SENT;
822 return ret;
823}
824
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100825/* Try to send an RST_STREAM frame on the connection for the indicated stream
826 * during mux operations. This stream must be valid and cannot be closed
827 * already. h2s->id will be used for the stream ID and h2s->errcode will be
828 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
829 * not yet.
830 *
831 * Returns > 0 on success or zero if nothing was done. In case of lack of room
832 * to write the message, it subscribes the stream to future notifications.
833 */
834static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
835{
836 struct buffer *res;
837 char str[13];
838 int ret;
839
840 if (!h2s || h2s->st == H2_SS_CLOSED)
841 return 1;
842
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100843 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
844 * RST_STREAM in response to a RST_STREAM frame.
845 */
846 if (h2c->dft == H2_FT_RST_STREAM) {
847 ret = 1;
848 goto ignore;
849 }
850
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100851 if (h2c_mux_busy(h2c, h2s)) {
852 h2s->flags |= H2_SF_BLK_MBUSY;
853 return 0;
854 }
855
Willy Tarreau44e973f2018-03-01 17:49:30 +0100856 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100857 if (!res) {
858 h2c->flags |= H2_CF_MUX_MALLOC;
859 h2s->flags |= H2_SF_BLK_MROOM;
860 return 0;
861 }
862
863 /* len: 4, type: 3, flags: none */
864 memcpy(str, "\x00\x00\x04\x03\x00", 5);
865 write_n32(str + 5, h2s->id);
866 write_n32(str + 9, h2s->errcode);
867 ret = bo_istput(res, ist2(str, 13));
868
869 if (unlikely(ret <= 0)) {
870 if (!ret) {
871 h2c->flags |= H2_CF_MUX_MFULL;
872 h2s->flags |= H2_SF_BLK_MROOM;
873 return 0;
874 }
875 else {
876 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
877 return 0;
878 }
879 }
880
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100881 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100882 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100883 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100884 return ret;
885}
886
887/* Try to send an RST_STREAM frame on the connection for the stream being
888 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
889 * error code unless the stream's state already is IDLE or CLOSED in which
890 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
891 * it was not yet.
892 *
893 * Returns > 0 on success or zero if nothing was done. In case of lack of room
894 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200895 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100896 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200897 */
898static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
899{
900 struct buffer *res;
901 char str[13];
902 int ret;
903
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100904 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
905 * RST_STREAM in response to a RST_STREAM frame.
906 */
907 if (h2c->dft == H2_FT_RST_STREAM) {
908 ret = 1;
909 goto ignore;
910 }
911
Willy Tarreau27a84c92017-10-17 08:10:17 +0200912 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100913 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200914 return 0;
915 }
916
Willy Tarreau44e973f2018-03-01 17:49:30 +0100917 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200918 if (!res) {
919 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100920 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200921 return 0;
922 }
923
924 /* len: 4, type: 3, flags: none */
925 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100926
Willy Tarreau27a84c92017-10-17 08:10:17 +0200927 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100928 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200929 h2s->errcode : H2_ERR_STREAM_CLOSED);
930 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100931
Willy Tarreau27a84c92017-10-17 08:10:17 +0200932 if (unlikely(ret <= 0)) {
933 if (!ret) {
934 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100935 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200936 return 0;
937 }
938 else {
939 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
940 return 0;
941 }
942 }
943
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100944 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100945 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200946 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100947 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100948 }
949
Willy Tarreau27a84c92017-10-17 08:10:17 +0200950 return ret;
951}
952
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100953/* try to send an empty DATA frame with the ES flag set to notify about the
954 * end of stream and match a shutdown(write). If an ES was already sent as
955 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
956 * on success or zero if nothing was done. In case of lack of room to write the
957 * message, it subscribes the requesting stream to future notifications.
958 */
959static int h2_send_empty_data_es(struct h2s *h2s)
960{
961 struct h2c *h2c = h2s->h2c;
962 struct buffer *res;
963 char str[9];
964 int ret;
965
Willy Tarreau721c9742017-11-07 11:05:42 +0100966 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100967 return 1;
968
969 if (h2c_mux_busy(h2c, h2s)) {
970 h2s->flags |= H2_SF_BLK_MBUSY;
971 return 0;
972 }
973
Willy Tarreau44e973f2018-03-01 17:49:30 +0100974 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100975 if (!res) {
976 h2c->flags |= H2_CF_MUX_MALLOC;
977 h2s->flags |= H2_SF_BLK_MROOM;
978 return 0;
979 }
980
981 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
982 memcpy(str, "\x00\x00\x00\x00\x01", 5);
983 write_n32(str + 5, h2s->id);
984 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100985 if (likely(ret > 0)) {
986 h2s->flags |= H2_SF_ES_SENT;
987 }
988 else if (!ret) {
989 h2c->flags |= H2_CF_MUX_MFULL;
990 h2s->flags |= H2_SF_BLK_MROOM;
991 return 0;
992 }
993 else {
994 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
995 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100996 }
997 return ret;
998}
999
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001000/* wake the streams attached to the connection, whose id is greater than <last>,
1001 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1002 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1003 * stream's state is automatically updated accordingly.
1004 */
1005static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1006{
1007 struct eb32_node *node;
1008 struct h2s *h2s;
1009
1010 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1011 flags |= CS_FL_ERROR;
1012
1013 if (conn_xprt_read0_pending(h2c->conn))
1014 flags |= CS_FL_EOS;
1015
1016 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1017 while (node) {
1018 h2s = container_of(node, struct h2s, by_id);
1019 if (h2s->id <= last)
1020 break;
1021 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001022
1023 if (!h2s->cs) {
1024 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001025 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001026 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001027 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001028
1029 h2s->cs->flags |= flags;
1030 /* recv is used to force to detect CS_FL_EOS that wake()
1031 * doesn't handle in the stream int code.
1032 */
1033 h2s->cs->data_cb->recv(h2s->cs);
1034 h2s->cs->data_cb->wake(h2s->cs);
1035
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001036 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1037 h2s->st = H2_SS_ERROR;
1038 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1039 h2s->st = H2_SS_HREM;
1040 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001041 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001042 }
1043}
1044
Willy Tarreau3421aba2017-07-27 15:41:03 +02001045/* Increase all streams' outgoing window size by the difference passed in
1046 * argument. This is needed upon receipt of the settings frame if the initial
1047 * window size is different. The difference may be negative and the resulting
1048 * window size as well, for the time it takes to receive some window updates.
1049 */
1050static void h2c_update_all_ws(struct h2c *h2c, int diff)
1051{
1052 struct h2s *h2s;
1053 struct eb32_node *node;
1054
1055 if (!diff)
1056 return;
1057
1058 node = eb32_first(&h2c->streams_by_id);
1059 while (node) {
1060 h2s = container_of(node, struct h2s, by_id);
1061 h2s->mws += diff;
1062 node = eb32_next(node);
1063 }
1064}
1065
1066/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1067 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1068 * return an error in h2c. Described in RFC7540#6.5.
1069 */
1070static int h2c_handle_settings(struct h2c *h2c)
1071{
1072 unsigned int offset;
1073 int error;
1074
1075 if (h2c->dff & H2_F_SETTINGS_ACK) {
1076 if (h2c->dfl) {
1077 error = H2_ERR_FRAME_SIZE_ERROR;
1078 goto fail;
1079 }
1080 return 1;
1081 }
1082
1083 if (h2c->dsi != 0) {
1084 error = H2_ERR_PROTOCOL_ERROR;
1085 goto fail;
1086 }
1087
1088 if (h2c->dfl % 6) {
1089 error = H2_ERR_FRAME_SIZE_ERROR;
1090 goto fail;
1091 }
1092
1093 /* that's the limit we can process */
1094 if (h2c->dfl > global.tune.bufsize) {
1095 error = H2_ERR_FRAME_SIZE_ERROR;
1096 goto fail;
1097 }
1098
1099 /* process full frame only */
1100 if (h2c->dbuf->i < h2c->dfl)
1101 return 0;
1102
1103 /* parse the frame */
1104 for (offset = 0; offset < h2c->dfl; offset += 6) {
1105 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1106 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1107
1108 switch (type) {
1109 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1110 /* we need to update all existing streams with the
1111 * difference from the previous iws.
1112 */
1113 if (arg < 0) { // RFC7540#6.5.2
1114 error = H2_ERR_FLOW_CONTROL_ERROR;
1115 goto fail;
1116 }
1117 h2c_update_all_ws(h2c, arg - h2c->miw);
1118 h2c->miw = arg;
1119 break;
1120 case H2_SETTINGS_MAX_FRAME_SIZE:
1121 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1122 error = H2_ERR_PROTOCOL_ERROR;
1123 goto fail;
1124 }
1125 h2c->mfs = arg;
1126 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001127 case H2_SETTINGS_ENABLE_PUSH:
1128 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1129 error = H2_ERR_PROTOCOL_ERROR;
1130 goto fail;
1131 }
1132 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001133 }
1134 }
1135
1136 /* need to ACK this frame now */
1137 h2c->st0 = H2_CS_FRAME_A;
1138 return 1;
1139 fail:
1140 h2c_error(h2c, error);
1141 return 0;
1142}
1143
1144/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1145 * success or one of the h2_status values.
1146 */
1147static int h2c_ack_settings(struct h2c *h2c)
1148{
1149 struct buffer *res;
1150 char str[9];
1151 int ret = -1;
1152
1153 if (h2c_mux_busy(h2c, NULL)) {
1154 h2c->flags |= H2_CF_DEM_MBUSY;
1155 return 0;
1156 }
1157
Willy Tarreau44e973f2018-03-01 17:49:30 +01001158 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001159 if (!res) {
1160 h2c->flags |= H2_CF_MUX_MALLOC;
1161 h2c->flags |= H2_CF_DEM_MROOM;
1162 return 0;
1163 }
1164
1165 memcpy(str,
1166 "\x00\x00\x00" /* length : 0 (no data) */
1167 "\x04" "\x01" /* type : 4, flags : ACK */
1168 "\x00\x00\x00\x00" /* stream ID */, 9);
1169
1170 ret = bo_istput(res, ist2(str, 9));
1171 if (unlikely(ret <= 0)) {
1172 if (!ret) {
1173 h2c->flags |= H2_CF_MUX_MFULL;
1174 h2c->flags |= H2_CF_DEM_MROOM;
1175 return 0;
1176 }
1177 else {
1178 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1179 return 0;
1180 }
1181 }
1182 return ret;
1183}
1184
Willy Tarreaucf68c782017-10-10 17:11:41 +02001185/* processes a PING frame and schedules an ACK if needed. The caller must pass
1186 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1187 * missing data. It may return an error in h2c.
1188 */
1189static int h2c_handle_ping(struct h2c *h2c)
1190{
1191 /* frame length must be exactly 8 */
1192 if (h2c->dfl != 8) {
1193 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1194 return 0;
1195 }
1196
1197 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001198 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001199 h2c->st0 = H2_CS_FRAME_A;
1200 return 1;
1201}
1202
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001203/* Try to send a window update for stream id <sid> and value <increment>.
1204 * Returns > 0 on success or zero on missing room or failure. It may return an
1205 * error in h2c.
1206 */
1207static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1208{
1209 struct buffer *res;
1210 char str[13];
1211 int ret = -1;
1212
1213 if (h2c_mux_busy(h2c, NULL)) {
1214 h2c->flags |= H2_CF_DEM_MBUSY;
1215 return 0;
1216 }
1217
Willy Tarreau44e973f2018-03-01 17:49:30 +01001218 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001219 if (!res) {
1220 h2c->flags |= H2_CF_MUX_MALLOC;
1221 h2c->flags |= H2_CF_DEM_MROOM;
1222 return 0;
1223 }
1224
1225 /* length: 4, type: 8, flags: none */
1226 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1227 write_n32(str + 5, sid);
1228 write_n32(str + 9, increment);
1229
1230 ret = bo_istput(res, ist2(str, 13));
1231
1232 if (unlikely(ret <= 0)) {
1233 if (!ret) {
1234 h2c->flags |= H2_CF_MUX_MFULL;
1235 h2c->flags |= H2_CF_DEM_MROOM;
1236 return 0;
1237 }
1238 else {
1239 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1240 return 0;
1241 }
1242 }
1243 return ret;
1244}
1245
1246/* try to send pending window update for the connection. It's safe to call it
1247 * with no pending updates. Returns > 0 on success or zero on missing room or
1248 * failure. It may return an error in h2c.
1249 */
1250static int h2c_send_conn_wu(struct h2c *h2c)
1251{
1252 int ret = 1;
1253
1254 if (h2c->rcvd_c <= 0)
1255 return 1;
1256
1257 /* send WU for the connection */
1258 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1259 if (ret > 0)
1260 h2c->rcvd_c = 0;
1261
1262 return ret;
1263}
1264
1265/* try to send pending window update for the current dmux stream. It's safe to
1266 * call it with no pending updates. Returns > 0 on success or zero on missing
1267 * room or failure. It may return an error in h2c.
1268 */
1269static int h2c_send_strm_wu(struct h2c *h2c)
1270{
1271 int ret = 1;
1272
1273 if (h2c->rcvd_s <= 0)
1274 return 1;
1275
1276 /* send WU for the stream */
1277 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1278 if (ret > 0)
1279 h2c->rcvd_s = 0;
1280
1281 return ret;
1282}
1283
Willy Tarreaucf68c782017-10-10 17:11:41 +02001284/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1285 * success, 0 on missing data or one of the h2_status values.
1286 */
1287static int h2c_ack_ping(struct h2c *h2c)
1288{
1289 struct buffer *res;
1290 char str[17];
1291 int ret = -1;
1292
1293 if (h2c->dbuf->i < 8)
1294 return 0;
1295
1296 if (h2c_mux_busy(h2c, NULL)) {
1297 h2c->flags |= H2_CF_DEM_MBUSY;
1298 return 0;
1299 }
1300
Willy Tarreau44e973f2018-03-01 17:49:30 +01001301 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001302 if (!res) {
1303 h2c->flags |= H2_CF_MUX_MALLOC;
1304 h2c->flags |= H2_CF_DEM_MROOM;
1305 return 0;
1306 }
1307
1308 memcpy(str,
1309 "\x00\x00\x08" /* length : 8 (same payload) */
1310 "\x06" "\x01" /* type : 6, flags : ACK */
1311 "\x00\x00\x00\x00" /* stream ID */, 9);
1312
1313 /* copy the original payload */
1314 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1315
1316 ret = bo_istput(res, ist2(str, 17));
1317 if (unlikely(ret <= 0)) {
1318 if (!ret) {
1319 h2c->flags |= H2_CF_MUX_MFULL;
1320 h2c->flags |= H2_CF_DEM_MROOM;
1321 return 0;
1322 }
1323 else {
1324 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1325 return 0;
1326 }
1327 }
1328 return ret;
1329}
1330
Willy Tarreau26f95952017-07-27 17:18:30 +02001331/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1332 * Returns > 0 on success or zero on missing data. It may return an error in
1333 * h2c or h2s. Described in RFC7540#6.9.
1334 */
1335static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1336{
1337 int32_t inc;
1338 int error;
1339
1340 if (h2c->dfl != 4) {
1341 error = H2_ERR_FRAME_SIZE_ERROR;
1342 goto conn_err;
1343 }
1344
1345 /* process full frame only */
1346 if (h2c->dbuf->i < h2c->dfl)
1347 return 0;
1348
1349 inc = h2_get_n32(h2c->dbuf, 0);
1350
1351 if (h2c->dsi != 0) {
1352 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001353
1354 /* it's not an error to receive WU on a closed stream */
1355 if (h2s->st == H2_SS_CLOSED)
1356 return 1;
1357
1358 if (!inc) {
1359 error = H2_ERR_PROTOCOL_ERROR;
1360 goto strm_err;
1361 }
1362
1363 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1364 error = H2_ERR_FLOW_CONTROL_ERROR;
1365 goto strm_err;
1366 }
1367
1368 h2s->mws += inc;
1369 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1370 h2s->flags &= ~H2_SF_BLK_SFCTL;
1371 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1372 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1373 /* This stream wanted to send but could not due to its
1374 * own flow control. We can put it back into the send
1375 * list now, it will be handled upon next send() call.
1376 */
1377 LIST_ADDQ(&h2c->send_list, &h2s->list);
1378 }
1379 }
1380 }
1381 else {
1382 /* connection window update */
1383 if (!inc) {
1384 error = H2_ERR_PROTOCOL_ERROR;
1385 goto conn_err;
1386 }
1387
1388 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1389 error = H2_ERR_FLOW_CONTROL_ERROR;
1390 goto conn_err;
1391 }
1392
1393 h2c->mws += inc;
1394 }
1395
1396 return 1;
1397
1398 conn_err:
1399 h2c_error(h2c, error);
1400 return 0;
1401
1402 strm_err:
1403 if (h2s) {
1404 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001405 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001406 }
1407 else
1408 h2c_error(h2c, error);
1409 return 0;
1410}
1411
Willy Tarreaue96b0922017-10-30 00:28:29 +01001412/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1413 * the last ID. Returns > 0 on success or zero on missing data. It may return
1414 * an error in h2c. Described in RFC7540#6.8.
1415 */
1416static int h2c_handle_goaway(struct h2c *h2c)
1417{
1418 int error;
1419 int last;
1420
1421 if (h2c->dsi != 0) {
1422 error = H2_ERR_PROTOCOL_ERROR;
1423 goto conn_err;
1424 }
1425
1426 if (h2c->dfl < 8) {
1427 error = H2_ERR_FRAME_SIZE_ERROR;
1428 goto conn_err;
1429 }
1430
1431 /* process full frame only */
1432 if (h2c->dbuf->i < h2c->dfl)
1433 return 0;
1434
1435 last = h2_get_n32(h2c->dbuf, 0);
1436 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1437 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001438 if (h2c->last_sid < 0)
1439 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001440 return 1;
1441
1442 conn_err:
1443 h2c_error(h2c, error);
1444 return 0;
1445}
1446
Willy Tarreau92153fc2017-12-03 19:46:19 +01001447/* processes a PRIORITY frame, and either skips it or rejects if it is
1448 * invalid. Returns > 0 on success or zero on missing data. It may return
1449 * an error in h2c. Described in RFC7540#6.3.
1450 */
1451static int h2c_handle_priority(struct h2c *h2c)
1452{
1453 int error;
1454
1455 if (h2c->dsi == 0) {
1456 error = H2_ERR_PROTOCOL_ERROR;
1457 goto conn_err;
1458 }
1459
1460 if (h2c->dfl != 5) {
1461 error = H2_ERR_FRAME_SIZE_ERROR;
1462 goto conn_err;
1463 }
1464
1465 /* process full frame only */
1466 if (h2c->dbuf->i < h2c->dfl)
1467 return 0;
1468
1469 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1470 /* 7540#5.3 : can't depend on itself */
1471 error = H2_ERR_PROTOCOL_ERROR;
1472 goto conn_err;
1473 }
1474 return 1;
1475
1476 conn_err:
1477 h2c_error(h2c, error);
1478 return 0;
1479}
1480
Willy Tarreaucd234e92017-08-18 10:59:39 +02001481/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1482 * Returns > 0 on success or zero on missing data. It may return an error in
1483 * h2c. Described in RFC7540#6.4.
1484 */
1485static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1486{
1487 int error;
1488
1489 if (h2c->dsi == 0) {
1490 error = H2_ERR_PROTOCOL_ERROR;
1491 goto conn_err;
1492 }
1493
Willy Tarreaucd234e92017-08-18 10:59:39 +02001494 if (h2c->dfl != 4) {
1495 error = H2_ERR_FRAME_SIZE_ERROR;
1496 goto conn_err;
1497 }
1498
1499 /* process full frame only */
1500 if (h2c->dbuf->i < h2c->dfl)
1501 return 0;
1502
1503 /* late RST, already handled */
1504 if (h2s->st == H2_SS_CLOSED)
1505 return 1;
1506
1507 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001508 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001509
1510 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001511 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001512 /* recv is used to force to detect CS_FL_EOS that wake()
1513 * doesn't handle in the stream-int code.
1514 */
1515 h2s->cs->data_cb->recv(h2s->cs);
1516 h2s->cs->data_cb->wake(h2s->cs);
1517 }
1518
1519 h2s->flags |= H2_SF_RST_RCVD;
1520 return 1;
1521
1522 conn_err:
1523 h2c_error(h2c, error);
1524 return 0;
1525}
1526
Willy Tarreau13278b42017-10-13 19:23:14 +02001527/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1528 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1529 * errors here are reported as connection errors since it's impossible to
1530 * recover from such errors after the compression context has been altered.
1531 */
1532static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1533{
1534 int error;
1535
1536 if (!h2c->dfl) {
1537 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1538 goto strm_err;
1539 }
1540
1541 if (!h2c->dbuf->size)
1542 return 0; // empty buffer
1543
1544 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1545 return 0; // incomplete frame
1546
1547 /* now either the frame is complete or the buffer is complete */
1548 if (h2s->st != H2_SS_IDLE) {
1549 /* FIXME: stream already exists, this is only allowed for
1550 * trailers (not supported for now).
1551 */
1552 error = H2_ERR_PROTOCOL_ERROR;
1553 goto conn_err;
1554 }
1555 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1556 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1557 error = H2_ERR_PROTOCOL_ERROR;
1558 goto conn_err;
1559 }
1560
1561 h2s = h2c_stream_new(h2c, h2c->dsi);
1562 if (!h2s) {
1563 error = H2_ERR_INTERNAL_ERROR;
1564 goto conn_err;
1565 }
1566
1567 h2s->st = H2_SS_OPEN;
1568 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1569 h2s->st = H2_SS_HREM;
1570 h2s->flags |= H2_SF_ES_RCVD;
1571 }
1572
1573 /* call the upper layers to process the frame, then let the upper layer
1574 * notify the stream about any change.
1575 */
1576 h2s->cs->data_cb->recv(h2s->cs);
1577
1578 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1579 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1580 error = H2_ERR_INTERNAL_ERROR;
1581 goto conn_err;
1582 }
1583
Willy Tarreau8f650c32017-11-21 19:36:21 +01001584 if (h2c->st0 >= H2_CS_ERROR)
1585 return 0;
1586
Willy Tarreau721c9742017-11-07 11:05:42 +01001587 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001588 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001589 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001590 }
1591 else {
1592 /* update the max stream ID if the request is being processed */
1593 if (h2s->id > h2c->max_id)
1594 h2c->max_id = h2s->id;
1595 }
1596
1597 return 1;
1598
1599 conn_err:
1600 h2c_error(h2c, error);
1601 return 0;
1602
1603 strm_err:
1604 if (h2s) {
1605 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001606 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001607 }
1608 else
1609 h2c_error(h2c, error);
1610 return 0;
1611}
1612
Willy Tarreau454f9052017-10-26 19:40:35 +02001613/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1614 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1615 */
1616static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1617{
1618 int error;
1619
1620 /* note that empty DATA frames are perfectly valid and sometimes used
1621 * to signal an end of stream (with the ES flag).
1622 */
1623
1624 if (!h2c->dbuf->size && h2c->dfl)
1625 return 0; // empty buffer
1626
1627 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1628 return 0; // incomplete frame
1629
1630 /* now either the frame is complete or the buffer is complete */
1631
1632 if (!h2c->dsi) {
1633 /* RFC7540#6.1 */
1634 error = H2_ERR_PROTOCOL_ERROR;
1635 goto conn_err;
1636 }
1637
1638 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1639 /* RFC7540#6.1 */
1640 error = H2_ERR_STREAM_CLOSED;
1641 goto strm_err;
1642 }
1643
Willy Tarreau454f9052017-10-26 19:40:35 +02001644 /* call the upper layers to process the frame, then let the upper layer
1645 * notify the stream about any change.
1646 */
1647 if (!h2s->cs) {
1648 error = H2_ERR_STREAM_CLOSED;
1649 goto strm_err;
1650 }
1651
1652 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001653
Willy Tarreau454f9052017-10-26 19:40:35 +02001654 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1655 /* cs has just been destroyed, we have to kill h2s. */
1656 error = H2_ERR_STREAM_CLOSED;
1657 goto strm_err;
1658 }
1659
Willy Tarreau8f650c32017-11-21 19:36:21 +01001660 if (h2c->st0 >= H2_CS_ERROR)
1661 return 0;
1662
Willy Tarreau721c9742017-11-07 11:05:42 +01001663 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001664 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001665 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001666 }
1667
1668 /* check for completion : the callee will change this to FRAME_A or
1669 * FRAME_H once done.
1670 */
1671 if (h2c->st0 == H2_CS_FRAME_P)
1672 return 0;
1673
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001674
1675 /* last frame */
1676 if (h2c->dff & H2_F_DATA_END_STREAM) {
1677 h2s->st = H2_SS_HREM;
1678 h2s->flags |= H2_SF_ES_RCVD;
1679 }
1680
Willy Tarreau454f9052017-10-26 19:40:35 +02001681 return 1;
1682
1683 conn_err:
1684 h2c_error(h2c, error);
1685 return 0;
1686
1687 strm_err:
1688 if (h2s) {
1689 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001690 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001691 }
1692 else
1693 h2c_error(h2c, error);
1694 return 0;
1695}
1696
Willy Tarreaubc933932017-10-09 16:21:43 +02001697/* process Rx frames to be demultiplexed */
1698static void h2_process_demux(struct h2c *h2c)
1699{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001700 struct h2s *h2s;
1701
Willy Tarreau081d4722017-05-16 21:51:05 +02001702 if (h2c->st0 >= H2_CS_ERROR)
1703 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001704
1705 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1706 if (h2c->st0 == H2_CS_PREFACE) {
1707 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1708 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1709 if (h2c->st0 == H2_CS_ERROR)
1710 h2c->st0 = H2_CS_ERROR2;
1711 goto fail;
1712 }
1713
1714 h2c->max_id = 0;
1715 h2c->st0 = H2_CS_SETTINGS1;
1716 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001717
1718 if (h2c->st0 == H2_CS_SETTINGS1) {
1719 struct h2_fh hdr;
1720
1721 /* ensure that what is pending is a valid SETTINGS frame
1722 * without an ACK.
1723 */
1724 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1725 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1726 if (h2c->st0 == H2_CS_ERROR)
1727 h2c->st0 = H2_CS_ERROR2;
1728 goto fail;
1729 }
1730
1731 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1732 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1733 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1734 h2c->st0 = H2_CS_ERROR2;
1735 goto fail;
1736 }
1737
1738 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1739 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1740 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1741 h2c->st0 = H2_CS_ERROR2;
1742 goto fail;
1743 }
1744
1745 /* that's OK, switch to FRAME_P to process it */
1746 h2c->dfl = hdr.len;
1747 h2c->dsi = hdr.sid;
1748 h2c->dft = hdr.ft;
1749 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001750 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001751 h2c->st0 = H2_CS_FRAME_P;
1752 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001753 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001754
1755 /* process as many incoming frames as possible below */
1756 while (h2c->dbuf->i) {
1757 int ret = 0;
1758
1759 if (h2c->st0 >= H2_CS_ERROR)
1760 break;
1761
1762 if (h2c->st0 == H2_CS_FRAME_H) {
1763 struct h2_fh hdr;
1764
1765 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1766 break;
1767
1768 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1769 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1770 h2c->st0 = H2_CS_ERROR;
1771 break;
1772 }
1773
1774 h2c->dfl = hdr.len;
1775 h2c->dsi = hdr.sid;
1776 h2c->dft = hdr.ft;
1777 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001778 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001779 h2c->st0 = H2_CS_FRAME_P;
1780 h2_skip_frame_hdr(h2c->dbuf);
1781 }
1782
1783 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001784 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001785
Willy Tarreaud7901432017-12-29 11:34:40 +01001786 if (h2c->st0 == H2_CS_FRAME_E)
1787 goto strm_err;
1788
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001789 if (h2s->st == H2_SS_IDLE &&
1790 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1791 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1792 * this state MUST be treated as a connection error
1793 */
1794 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1795 h2c->st0 = H2_CS_ERROR;
1796 break;
1797 }
1798
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001799 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1800 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1801 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1802 * this state MUST be treated as a stream error
1803 */
1804 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001805 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001806 goto strm_err;
1807 }
1808
Willy Tarreauab837502017-12-27 15:07:30 +01001809 /* Below the management of frames received in closed state is a
1810 * bit hackish because the spec makes strong differences between
1811 * streams closed by receiving RST, sending RST, and seeing ES
1812 * in both directions. In addition to this, the creation of a
1813 * new stream reusing the identifier of a closed one will be
1814 * detected here. Given that we cannot keep track of all closed
1815 * streams forever, we consider that unknown closed streams were
1816 * closed on RST received, which allows us to respond with an
1817 * RST without breaking the connection (eg: to abort a transfer).
1818 * Some frames have to be silently ignored as well.
1819 */
1820 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1821 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1822 /* #5.1.1: The identifier of a newly
1823 * established stream MUST be numerically
1824 * greater than all streams that the initiating
1825 * endpoint has opened or reserved. This
1826 * governs streams that are opened using a
1827 * HEADERS frame and streams that are reserved
1828 * using PUSH_PROMISE. An endpoint that
1829 * receives an unexpected stream identifier
1830 * MUST respond with a connection error.
1831 */
1832 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1833 goto strm_err;
1834 }
1835
1836 if (h2s->flags & H2_SF_RST_RCVD) {
1837 /* RFC7540#5.1:closed: an endpoint that
1838 * receives any frame other than PRIORITY after
1839 * receiving a RST_STREAM MUST treat that as a
1840 * stream error of type STREAM_CLOSED.
1841 *
1842 * Note that old streams fall into this category
1843 * and will lead to an RST being sent.
1844 */
1845 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1846 h2c->st0 = H2_CS_FRAME_E;
1847 goto strm_err;
1848 }
1849
1850 /* RFC7540#5.1:closed: if this state is reached as a
1851 * result of sending a RST_STREAM frame, the peer that
1852 * receives the RST_STREAM might have already sent
1853 * frames on the stream that cannot be withdrawn. An
1854 * endpoint MUST ignore frames that it receives on
1855 * closed streams after it has sent a RST_STREAM
1856 * frame. An endpoint MAY choose to limit the period
1857 * over which it ignores frames and treat frames that
1858 * arrive after this time as being in error.
1859 */
1860 if (!(h2s->flags & H2_SF_RST_SENT)) {
1861 /* RFC7540#5.1:closed: any frame other than
1862 * PRIO/WU/RST in this state MUST be treated as
1863 * a connection error
1864 */
1865 if (h2c->dft != H2_FT_RST_STREAM &&
1866 h2c->dft != H2_FT_PRIORITY &&
1867 h2c->dft != H2_FT_WINDOW_UPDATE) {
1868 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1869 goto strm_err;
1870 }
1871 }
1872 }
1873
Willy Tarreauc0da1962017-10-30 18:38:00 +01001874#if 0
1875 // problem below: it is not possible to completely ignore such
1876 // streams as we need to maintain the compression state as well
1877 // and for this we need to completely process these frames (eg:
1878 // HEADERS frames) as well as counting DATA frames to emit
1879 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1880 // This is a typical case of layer violation where the
1881 // transported contents are critical to the connection's
1882 // validity and must be ignored at the same time :-(
1883
1884 /* graceful shutdown, ignore streams whose ID is higher than
1885 * the one advertised in GOAWAY. RFC7540#6.8.
1886 */
1887 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1888 ret = MIN(h2c->dbuf->i, h2c->dfl);
1889 bi_del(h2c->dbuf, ret);
1890 h2c->dfl -= ret;
1891 ret = h2c->dfl == 0;
1892 goto strm_err;
1893 }
1894#endif
1895
Willy Tarreau7e98c052017-10-10 15:56:59 +02001896 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001897 case H2_FT_SETTINGS:
1898 if (h2c->st0 == H2_CS_FRAME_P)
1899 ret = h2c_handle_settings(h2c);
1900
1901 if (h2c->st0 == H2_CS_FRAME_A)
1902 ret = h2c_ack_settings(h2c);
1903 break;
1904
Willy Tarreaucf68c782017-10-10 17:11:41 +02001905 case H2_FT_PING:
1906 if (h2c->st0 == H2_CS_FRAME_P)
1907 ret = h2c_handle_ping(h2c);
1908
1909 if (h2c->st0 == H2_CS_FRAME_A)
1910 ret = h2c_ack_ping(h2c);
1911 break;
1912
Willy Tarreau26f95952017-07-27 17:18:30 +02001913 case H2_FT_WINDOW_UPDATE:
1914 if (h2c->st0 == H2_CS_FRAME_P)
1915 ret = h2c_handle_window_update(h2c, h2s);
1916 break;
1917
Willy Tarreau61290ec2017-10-17 08:19:21 +02001918 case H2_FT_CONTINUATION:
1919 /* we currently don't support CONTINUATION frames since
1920 * we have nowhere to store the partial HEADERS frame.
1921 * Let's abort the stream on an INTERNAL_ERROR here.
1922 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001923 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001924 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001925 h2c->st0 = H2_CS_FRAME_E;
1926 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001927 break;
1928
Willy Tarreau13278b42017-10-13 19:23:14 +02001929 case H2_FT_HEADERS:
1930 if (h2c->st0 == H2_CS_FRAME_P)
1931 ret = h2c_frt_handle_headers(h2c, h2s);
1932 break;
1933
Willy Tarreau454f9052017-10-26 19:40:35 +02001934 case H2_FT_DATA:
1935 if (h2c->st0 == H2_CS_FRAME_P)
1936 ret = h2c_frt_handle_data(h2c, h2s);
1937
1938 if (h2c->st0 == H2_CS_FRAME_A)
1939 ret = h2c_send_strm_wu(h2c);
1940 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001941
Willy Tarreau92153fc2017-12-03 19:46:19 +01001942 case H2_FT_PRIORITY:
1943 if (h2c->st0 == H2_CS_FRAME_P)
1944 ret = h2c_handle_priority(h2c);
1945 break;
1946
Willy Tarreaucd234e92017-08-18 10:59:39 +02001947 case H2_FT_RST_STREAM:
1948 if (h2c->st0 == H2_CS_FRAME_P)
1949 ret = h2c_handle_rst_stream(h2c, h2s);
1950 break;
1951
Willy Tarreaue96b0922017-10-30 00:28:29 +01001952 case H2_FT_GOAWAY:
1953 if (h2c->st0 == H2_CS_FRAME_P)
1954 ret = h2c_handle_goaway(h2c);
1955 break;
1956
Willy Tarreau1c661982017-10-30 13:52:01 +01001957 case H2_FT_PUSH_PROMISE:
1958 /* not permitted here, RFC7540#5.1 */
1959 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001960 break;
1961
1962 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001963 default:
1964 /* drop frames that we ignore. They may be larger than
1965 * the buffer so we drain all of their contents until
1966 * we reach the end.
1967 */
1968 ret = MIN(h2c->dbuf->i, h2c->dfl);
1969 bi_del(h2c->dbuf, ret);
1970 h2c->dfl -= ret;
1971 ret = h2c->dfl == 0;
1972 }
1973
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001974 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01001975 /* We may have to send an RST if not done yet */
1976 if (h2s->st == H2_SS_ERROR)
1977 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001978
Willy Tarreaua20a5192017-12-27 11:02:06 +01001979 if (h2c->st0 == H2_CS_FRAME_E)
1980 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001981
Willy Tarreau7e98c052017-10-10 15:56:59 +02001982 /* error or missing data condition met above ? */
1983 if (ret <= 0)
1984 break;
1985
1986 if (h2c->st0 != H2_CS_FRAME_H) {
1987 bi_del(h2c->dbuf, h2c->dfl);
1988 h2c->st0 = H2_CS_FRAME_H;
1989 }
1990 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001991
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001992 if (h2c->rcvd_c > 0 &&
1993 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1994 h2c_send_conn_wu(h2c);
1995
Willy Tarreau52eed752017-09-22 15:05:09 +02001996 fail:
1997 /* we can go here on missing data, blocked response or error */
1998 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001999}
2000
2001/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2002 * the end.
2003 */
2004static int h2_process_mux(struct h2c *h2c)
2005{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002006 struct h2s *h2s, *h2s_back;
2007
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002008 /* start by sending possibly pending window updates */
2009 if (h2c->rcvd_c > 0 &&
2010 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2011 h2c_send_conn_wu(h2c) < 0)
2012 goto fail;
2013
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002014 /* First we always process the flow control list because the streams
2015 * waiting there were already elected for immediate emission but were
2016 * blocked just on this.
2017 */
2018
2019 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2020 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2021 h2c->st0 >= H2_CS_ERROR)
2022 break;
2023
2024 /* In theory it's possible that h2s->cs == NULL here :
2025 * - client sends crap that causes a parse error
2026 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2027 * - RST_STREAM cannot be emitted because mux is busy/full
2028 * - stream gets notified, detaches and quits
2029 * - mux buffer gets ready and wakes pending streams up
2030 * - bam!
2031 */
2032 h2s->flags &= ~H2_SF_BLK_ANY;
2033
2034 if (h2s->cs) {
2035 h2s->cs->data_cb->send(h2s->cs);
2036 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002037 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002038 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002039 }
2040
2041 /* depending on callee's blocking reasons, we may queue in send
2042 * list or completely dequeue.
2043 */
2044 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2045 if (h2s->flags & H2_SF_BLK_ANY) {
2046 LIST_DEL(&h2s->list);
2047 LIST_ADDQ(&h2c->send_list, &h2s->list);
2048 }
2049 else {
2050 LIST_DEL(&h2s->list);
2051 LIST_INIT(&h2s->list);
2052 if (h2s->cs)
2053 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002054 else {
2055 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002056 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002057 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002058 }
2059 }
2060 }
2061
2062 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2063 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2064 break;
2065
2066 /* In theory it's possible that h2s->cs == NULL here :
2067 * - client sends crap that causes a parse error
2068 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2069 * - RST_STREAM cannot be emitted because mux is busy/full
2070 * - stream gets notified, detaches and quits
2071 * - mux buffer gets ready and wakes pending streams up
2072 * - bam!
2073 */
2074 h2s->flags &= ~H2_SF_BLK_ANY;
2075
2076 if (h2s->cs) {
2077 h2s->cs->data_cb->send(h2s->cs);
2078 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002079 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002080 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002081 }
2082 /* depending on callee's blocking reasons, we may queue in fctl
2083 * list or completely dequeue.
2084 */
2085 if (h2s->flags & H2_SF_BLK_MFCTL) {
2086 /* stream hit the connection's flow control */
2087 LIST_DEL(&h2s->list);
2088 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2089 }
2090 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2091 LIST_DEL(&h2s->list);
2092 LIST_INIT(&h2s->list);
2093 if (h2s->cs)
2094 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002095 else {
2096 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002097 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002098 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002099 }
2100 }
2101
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002102 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002103 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002104 if (h2c->st0 == H2_CS_ERROR) {
2105 if (h2c->max_id >= 0) {
2106 h2c_send_goaway_error(h2c, NULL);
2107 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2108 return 0;
2109 }
2110
2111 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2112 }
2113 return 1;
2114 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002115 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002116}
2117
Willy Tarreau71681172017-10-23 14:39:06 +02002118
Willy Tarreau62f52692017-10-08 23:01:42 +02002119/*********************************************************/
2120/* functions below are I/O callbacks from the connection */
2121/*********************************************************/
2122
2123/* callback called on recv event by the connection handler */
2124static void h2_recv(struct connection *conn)
2125{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002126 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002127 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002128 int max;
2129
Willy Tarreau315d8072017-12-10 22:17:57 +01002130 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002131 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002132
Willy Tarreau44e973f2018-03-01 17:49:30 +01002133 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002134 if (!buf) {
2135 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002136 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002137 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002138
Willy Tarreaua2af5122017-10-09 11:56:46 +02002139 /* note: buf->o == 0 */
2140 max = buf->size - buf->i;
Willy Tarreau315d8072017-12-10 22:17:57 +01002141 if (max)
2142 conn->xprt->rcv_buf(conn, buf, max);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002143
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002144 if (!buf->i) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002145 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002146 return;
2147 }
2148
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002149 if (buf->i == buf->size)
2150 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002151 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002152}
2153
2154/* callback called on send event by the connection handler */
2155static void h2_send(struct connection *conn)
2156{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002157 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002158 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002159
2160 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002161 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002162
2163 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2164 /* a handshake was requested */
2165 return;
2166 }
2167
Willy Tarreaubc933932017-10-09 16:21:43 +02002168 /* This loop is quite simple : it tries to fill as much as it can from
2169 * pending streams into the existing buffer until it's reportedly full
2170 * or the end of send requests is reached. Then it tries to send this
2171 * buffer's contents out, marks it not full if at least one byte could
2172 * be sent, and tries again.
2173 *
2174 * The snd_buf() function normally takes a "flags" argument which may
2175 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2176 * data immediately comes and CO_SFL_STREAMER to indicate that the
2177 * connection is streaming lots of data (used to increase TLS record
2178 * size at the expense of latency). The former can be sent any time
2179 * there's a buffer full flag, as it indicates at least one stream
2180 * attempted to send and failed so there are pending data. An
2181 * alternative would be to set it as long as there's an active stream
2182 * but that would be problematic for ACKs until we have an absolute
2183 * guarantee that all waiters have at least one byte to send. The
2184 * latter should possibly not be set for now.
2185 */
2186
2187 done = 0;
2188 while (!done) {
2189 unsigned int flags = 0;
2190
2191 /* fill as much as we can into the current buffer */
2192 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2193 done = h2_process_mux(h2c);
2194
2195 if (conn->flags & CO_FL_ERROR)
2196 break;
2197
2198 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2199 flags |= CO_SFL_MSG_MORE;
2200
Willy Tarreau319994a2017-11-07 11:03:56 +01002201 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002202 break;
2203
2204 /* wrote at least one byte, the buffer is not full anymore */
2205 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2206 }
2207
Willy Tarreaua2af5122017-10-09 11:56:46 +02002208 if (conn->flags & CO_FL_SOCK_WR_SH) {
2209 /* output closed, nothing to send, clear the buffer to release it */
2210 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002211 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002212}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002213
Willy Tarreau62f52692017-10-08 23:01:42 +02002214/* callback called on any event by the connection handler.
2215 * It applies changes and returns zero, or < 0 if it wants immediate
2216 * destruction of the connection (which normally doesn not happen in h2).
2217 */
2218static int h2_wake(struct connection *conn)
2219{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002220 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002221 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002222
Willy Tarreaud13bf272017-12-14 10:34:52 +01002223 if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
2224 h2_process_demux(h2c);
2225
2226 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
2227 h2c->dbuf->i = 0;
2228
2229 if (h2c->dbuf->i != h2c->dbuf->size)
2230 h2c->flags &= ~H2_CF_DEM_DFULL;
2231 }
2232
Willy Tarreau8ec14062017-12-30 18:08:13 +01002233 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2234 /* frontend is stopping, reload likely in progress, let's try
2235 * to announce a graceful shutdown if not yet done. We don't
2236 * care if it fails, it will be tried again later.
2237 */
2238 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2239 if (h2c->last_sid < 0)
2240 h2c->last_sid = (1U << 31) - 1;
2241 h2c_send_goaway_error(h2c, NULL);
2242 }
2243 }
2244
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002245 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002246 * If we received early data, and the handshake is done, wake
2247 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002248 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002249 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2250 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2251 struct eb32_node *node;
2252 struct h2s *h2s;
2253
2254 h2c->flags |= H2_CF_WAIT_FOR_HS;
2255 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2256
2257 while (node) {
2258 h2s = container_of(node, struct h2s, by_id);
2259 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2260 h2s->cs->data_cb->wake(h2s->cs);
2261 node = eb32_next(node);
2262 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002263 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002264
Willy Tarreau26bd7612017-10-09 16:47:04 +02002265 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002266 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2267 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2268 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002269 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002270
2271 if (eb_is_empty(&h2c->streams_by_id)) {
2272 /* no more stream, kill the connection now */
2273 h2_release(conn);
2274 return -1;
2275 }
2276 else {
2277 /* some streams still there, we need to signal them all and
2278 * wait for their departure.
2279 */
2280 __conn_xprt_stop_recv(conn);
2281 __conn_xprt_stop_send(conn);
2282 return 0;
2283 }
2284 }
2285
2286 if (!h2c->dbuf->i)
Willy Tarreau44e973f2018-03-01 17:49:30 +01002287 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002288
2289 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002290 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002291 __conn_xprt_stop_recv(conn);
2292 }
2293 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002294 __conn_xprt_want_recv(conn);
2295 }
2296
2297 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002298 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2299 (h2c->st0 == H2_CS_ERROR ||
2300 h2c->mbuf->o ||
2301 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2302 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002303 __conn_xprt_want_send(conn);
2304 }
2305 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002306 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002307 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002308 }
2309
Willy Tarreau3f133572017-10-31 19:21:06 +01002310 if (h2c->task) {
Willy Tarreau84b118f2018-03-05 16:10:54 +01002311 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002312 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002313 task_queue(h2c->task);
2314 }
2315 else
2316 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002317 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002318 return 0;
2319}
2320
Willy Tarreauea392822017-10-31 10:02:25 +01002321/* Connection timeout management. The principle is that if there's no receipt
2322 * nor sending for a certain amount of time, the connection is closed. If the
2323 * MUX buffer still has lying data or is not allocatable, the connection is
2324 * immediately killed. If it's allocatable and empty, we attempt to send a
2325 * GOAWAY frame.
2326 */
2327static struct task *h2_timeout_task(struct task *t)
2328{
2329 struct h2c *h2c = t->context;
2330 int expired = tick_is_expired(t->expire, now_ms);
2331
Willy Tarreau0975f112018-03-29 15:22:59 +02002332 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002333 return t;
2334
Willy Tarreau0975f112018-03-29 15:22:59 +02002335 task_delete(t);
2336 task_free(t);
2337
2338 if (!h2c) {
2339 /* resources were already deleted */
2340 return NULL;
2341 }
2342
2343 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002344 h2c_error(h2c, H2_ERR_NO_ERROR);
2345 h2_wake_some_streams(h2c, 0, 0);
2346
2347 if (h2c->mbuf->o) {
2348 /* don't even try to send a GOAWAY, the buffer is stuck */
2349 h2c->flags |= H2_CF_GOAWAY_FAILED;
2350 }
2351
2352 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002353 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002354 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2355 h2c->flags |= H2_CF_GOAWAY_FAILED;
2356
2357 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2358 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2359
Willy Tarreau0975f112018-03-29 15:22:59 +02002360 /* either we can release everything now or it will be done later once
2361 * the last stream closes.
2362 */
2363 if (eb_is_empty(&h2c->streams_by_id))
2364 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002365
Willy Tarreauea392822017-10-31 10:02:25 +01002366 return NULL;
2367}
2368
2369
Willy Tarreau62f52692017-10-08 23:01:42 +02002370/*******************************************/
2371/* functions below are used by the streams */
2372/*******************************************/
2373
2374/*
2375 * Attach a new stream to a connection
2376 * (Used for outgoing connections)
2377 */
2378static struct conn_stream *h2_attach(struct connection *conn)
2379{
2380 return NULL;
2381}
2382
2383/* callback used to update the mux's polling flags after changing a cs' status.
2384 * The caller (cs_update_mux_polling) will take care of propagating any changes
2385 * to the transport layer.
2386 */
2387static void h2_update_poll(struct conn_stream *cs)
2388{
Willy Tarreau1d393222017-10-17 10:26:19 +02002389 struct h2s *h2s = cs->ctx;
2390
2391 if (!h2s)
2392 return;
2393
Willy Tarreaud7739c82017-10-30 15:38:23 +01002394 /* we may unblock a blocked read */
2395
Willy Tarreau315d8072017-12-10 22:17:57 +01002396 if (cs->flags & CS_FL_DATA_RD_ENA) {
2397 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002398 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002399 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002400 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002401 conn_xprt_want_send(cs->conn);
2402 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002403 }
2404
Willy Tarreau1d393222017-10-17 10:26:19 +02002405 /* Note: the stream and stream-int code doesn't allow us to perform a
2406 * synchronous send() here unfortunately, because this code is called
2407 * as si_update() from the process_stream() context. This means that
2408 * we have to queue the current cs and defer its processing after the
2409 * connection's cs list is processed anyway.
2410 */
2411
2412 if (cs->flags & CS_FL_DATA_WR_ENA) {
2413 if (LIST_ISEMPTY(&h2s->list)) {
2414 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2415 !h2s->h2c->mbuf->o && // not yet subscribed
2416 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2417 conn_xprt_want_send(cs->conn);
2418 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2419 }
2420 }
2421 else if (!LIST_ISEMPTY(&h2s->list)) {
2422 LIST_DEL(&h2s->list);
2423 LIST_INIT(&h2s->list);
2424 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2425 }
2426
2427 /* this can happen from within si_chk_snd() */
2428 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2429 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002430}
2431
2432/*
2433 * Detach the stream from the connection and possibly release the connection.
2434 */
2435static void h2_detach(struct conn_stream *cs)
2436{
Willy Tarreau60935142017-10-16 18:11:19 +02002437 struct h2s *h2s = cs->ctx;
2438 struct h2c *h2c;
2439
2440 cs->ctx = NULL;
2441 if (!h2s)
2442 return;
2443
2444 h2c = h2s->h2c;
2445 h2s->cs = NULL;
2446
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002447 /* this stream may be blocked waiting for some data to leave (possibly
2448 * an ES or RST frame), so orphan it in this case.
2449 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002450 if (!(cs->conn->flags & CO_FL_ERROR) &&
2451 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002452 return;
2453
Willy Tarreau45f752e2017-10-30 15:44:59 +01002454 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2455 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2456 /* unblock the connection if it was blocked on this
2457 * stream.
2458 */
2459 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2460 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2461 conn_xprt_want_recv(cs->conn);
2462 conn_xprt_want_send(cs->conn);
2463 }
2464
Willy Tarreau71049cc2018-03-28 13:56:39 +02002465 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002466
Willy Tarreaue323f342018-03-28 13:51:45 +02002467 /* We don't want to close right now unless we're removing the
2468 * last stream, and either the connection is in error, or it
2469 * reached the ID already specified in a GOAWAY frame received
2470 * or sent (as seen by last_sid >= 0).
2471 */
2472 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2473 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
2474 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2475 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2476 (conn_xprt_read0_pending(h2c->conn) ||
2477 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2478 /* no more stream will come, kill it now */
2479 h2_release(h2c->conn);
2480 }
2481 else if (h2c->task) {
2482 if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) {
2483 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2484 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002485 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002486 else
2487 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002488 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002489}
2490
2491static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2492{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002493 struct h2s *h2s = cs->ctx;
2494
2495 if (!mode)
2496 return;
2497
Willy Tarreau721c9742017-11-07 11:05:42 +01002498 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002499 return;
2500
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002501 /* if no outgoing data was seen on this stream, it means it was
2502 * closed with a "tcp-request content" rule that is normally
2503 * used to kill the connection ASAP (eg: limit abuse). In this
2504 * case we send a goaway to close the connection.
2505 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002506 if (!(h2s->flags & H2_SF_RST_SENT) &&
2507 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2508 return;
2509
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002510 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2511 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2512 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2513 return;
2514
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002515 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2516 conn_xprt_want_send(cs->conn);
2517
Willy Tarreau00dd0782018-03-01 16:31:34 +01002518 h2s_close(h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002519}
2520
2521static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2522{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002523 struct h2s *h2s = cs->ctx;
2524
Willy Tarreau721c9742017-11-07 11:05:42 +01002525 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002526 return;
2527
Willy Tarreau67434202017-11-06 20:20:51 +01002528 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002529 /* we can cleanly close using an empty data frame only after headers */
2530
2531 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2532 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002533 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002534
2535 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002536 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002537 else
2538 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002539 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002540 /* if no outgoing data was seen on this stream, it means it was
2541 * closed with a "tcp-request content" rule that is normally
2542 * used to kill the connection ASAP (eg: limit abuse). In this
2543 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002544 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002545 if (!(h2s->flags & H2_SF_RST_SENT) &&
2546 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2547 return;
2548
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002549 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2550 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002551 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2552 return;
2553
Willy Tarreau00dd0782018-03-01 16:31:34 +01002554 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002555 }
2556
2557 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2558 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002559}
2560
Willy Tarreau13278b42017-10-13 19:23:14 +02002561/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2562 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2563 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002564 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002565 */
2566static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2567{
2568 struct h2c *h2c = h2s->h2c;
2569 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002570 struct chunk *tmp = get_trash_chunk();
2571 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002572 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002573 int flen = h2c->dfl;
2574 int outlen = 0;
2575 int wrap;
2576 int try;
2577
2578 if (!h2c->dfl) {
2579 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002580 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002581 return 0;
2582 }
2583
Willy Tarreau68472622017-12-11 18:36:37 +01002584 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2585 return 0; // incomplete input frame
2586
Willy Tarreau13278b42017-10-13 19:23:14 +02002587 /* if the input buffer wraps, take a temporary copy of it (rare) */
2588 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2589 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002590 copy = alloc_trash_chunk();
2591 if (!copy) {
2592 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2593 goto fail;
2594 }
2595 memcpy(copy->str, h2c->dbuf->p, wrap);
2596 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2597 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002598 }
2599
2600 /* The padlen is the first byte before data, and the padding appears
2601 * after data. padlen+data+padding are included in flen.
2602 */
2603 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002604 h2c->dpl = *hdrs;
2605 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002606 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2607 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002608 return 0;
2609 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002610 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002611 hdrs += 1; // skip Pad Length
2612 }
2613
2614 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2615 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002616 if (read_n32(hdrs) == h2s->id) {
2617 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2618 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2619 return 0;//goto fail_stream;
2620 }
2621
Willy Tarreau13278b42017-10-13 19:23:14 +02002622 hdrs += 5; // stream dep = 4, weight = 1
2623 flen -= 5;
2624 }
2625
2626 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2627 * don't support this for now and can't even decompress so we have to
2628 * break the connection.
2629 */
2630 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2631 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002632 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002633 }
2634
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002635 /* we can't retry a failed decompression operation so we must be very
2636 * careful not to take any risks. In practice the output buffer is
2637 * always empty except maybe for trailers, so these operations almost
2638 * never happen.
2639 */
2640 if (unlikely(buf->o)) {
2641 /* need to let the output buffer flush and
2642 * mark the buffer for later wake up.
2643 */
2644 goto fail;
2645 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002646
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002647 if (unlikely(buffer_space_wraps(buf))) {
2648 /* it doesn't fit and the buffer is fragmented,
2649 * so let's defragment it and try again.
2650 */
2651 buffer_slow_realign(buf);
2652 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002653
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002654 /* first check if we have some room after p+i */
2655 try = buf->data + buf->size - (buf->p + buf->i);
2656
2657 /* otherwise continue between data and p-o */
2658 if (try <= 0) {
2659 try = buf->p - (buf->data + buf->o);
2660 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002661 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002662 }
2663 if (try > count)
2664 try = count;
2665
2666 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2667 sizeof(list)/sizeof(list[0]), tmp);
2668 if (outlen < 0) {
2669 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2670 goto fail;
2671 }
2672
2673 /* OK now we have our header list in <list> */
2674 outlen = h2_make_h1_request(list, bi_end(buf), try);
2675
2676 if (outlen < 0) {
2677 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2678 goto fail;
2679 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002680
2681 /* now consume the input data */
2682 bi_del(h2c->dbuf, h2c->dfl);
2683 h2c->st0 = H2_CS_FRAME_H;
2684 buf->i += outlen;
2685
2686 /* don't send it before returning data!
2687 * FIXME: should we instead try to send it much later, after the
2688 * response ? This would require that we keep a copy of it in h2s.
2689 */
2690 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2691 h2s->cs->flags |= CS_FL_EOS;
2692 h2s->flags |= H2_SF_ES_RCVD;
2693 }
2694
Willy Tarreau68dd9852017-07-03 14:44:26 +02002695 leave:
2696 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002697 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002698 fail:
2699 outlen = 0;
2700 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002701}
2702
Willy Tarreau454f9052017-10-26 19:40:35 +02002703/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2704 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2705 * in use, a new chunk is emitted for each frame. This is supposed to fit
2706 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2707 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2708 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2709 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002710 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2711 * checked to know if some data remain pending (an empty DATA frame can return
2712 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2713 * connection errors in h2c->errcode. The caller must already have checked the
2714 * frame header and ensured that the frame was complete or the buffer full. It
2715 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002716 */
2717static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2718{
2719 struct h2c *h2c = h2s->h2c;
2720 int block1, block2;
2721 unsigned int flen = h2c->dfl;
Willy Tarreau454f9052017-10-26 19:40:35 +02002722
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002723 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002724 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002725
2726 /* The padlen is the first byte before data, and the padding appears
2727 * after data. padlen+data+padding are included in flen.
2728 */
Willy Tarreau79127812017-12-03 21:06:59 +01002729 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002730 if (h2c->dbuf->i < 1)
2731 return 0;
2732
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002733 h2c->dpl = *(uint8_t *)bi_ptr(h2c->dbuf);
2734 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002735 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2736 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002737 return 0;
2738 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002739
2740 /* skip the padlen byte */
2741 bi_del(h2c->dbuf, 1);
2742 h2c->dfl--;
2743 h2c->rcvd_c++; h2c->rcvd_s++;
2744 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002745 }
2746
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002747 flen = h2c->dfl - h2c->dpl;
2748 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002749 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002750
2751 if (flen > h2c->dbuf->i) {
2752 flen = h2c->dbuf->i;
2753 if (!flen)
2754 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002755 }
2756
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002757 /* does it fit in output buffer or should we wait ? */
2758 if (flen > count) {
2759 flen = count;
2760 if (!flen) {
2761 h2c->flags |= H2_CF_DEM_SFULL;
2762 h2s->cs->flags |= CS_FL_RCV_MORE;
2763 return 0;
2764 }
2765 }
2766
Willy Tarreau454f9052017-10-26 19:40:35 +02002767 /* Block1 is the length of the first block before the buffer wraps,
2768 * block2 is the optional second block to reach the end of the frame.
2769 */
2770 block1 = bi_contig_data(h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002771 if (block1 > flen)
2772 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002773 block2 = flen - block1;
2774
2775 if (block1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002776 bi_putblk(buf, b_ptr(h2c->dbuf, 0), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002777
2778 if (block2)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002779 bi_putblk(buf, b_ptr(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002780
2781 /* now mark the input data as consumed (will be deleted from the buffer
2782 * by the caller when seeing FRAME_A after sending the window update).
2783 */
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002784 bi_del(h2c->dbuf, flen);
2785 h2c->dfl -= flen;
2786 h2c->rcvd_c += flen;
2787 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2788
2789 if (h2c->dfl > h2c->dpl) {
2790 /* more data available, transfer stalled on stream full */
2791 h2c->flags |= H2_CF_DEM_SFULL;
2792 h2s->cs->flags |= CS_FL_RCV_MORE;
2793 return flen;
2794 }
2795
Willy Tarreau4a28da12018-01-04 14:41:00 +01002796 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002797 /* here we're done with the frame, all the payload (except padding) was
2798 * transferred.
2799 */
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002800 h2c->rcvd_c += h2c->dpl;
2801 h2c->rcvd_s += h2c->dpl;
2802 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002803 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2804
2805 /* don't send it before returning data!
2806 * FIXME: should we instead try to send it much later, after the
2807 * response ? This would require that we keep a copy of it in h2s.
2808 */
Willy Tarreau79127812017-12-03 21:06:59 +01002809 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002810 h2s->cs->flags |= CS_FL_EOS;
2811 h2s->flags |= H2_SF_ES_RCVD;
2812 }
2813
2814 return flen;
2815}
2816
Willy Tarreau62f52692017-10-08 23:01:42 +02002817/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002818 * Called from the upper layer to get more data, up to <count> bytes. The
2819 * caller is responsible for never asking for more data than what is available
2820 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002821 */
2822static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2823{
Willy Tarreau13278b42017-10-13 19:23:14 +02002824 struct h2s *h2s = cs->ctx;
2825 struct h2c *h2c = h2s->h2c;
2826 int ret = 0;
2827
2828 if (h2c->st0 != H2_CS_FRAME_P)
2829 return 0; // no pre-parsed frame yet
2830
2831 if (h2c->dsi != h2s->id)
2832 return 0; // not for us
2833
2834 if (!h2c->dbuf->size)
2835 return 0; // empty buffer
2836
Willy Tarreau13278b42017-10-13 19:23:14 +02002837 switch (h2c->dft) {
2838 case H2_FT_HEADERS:
2839 ret = h2_frt_decode_headers(h2s, buf, count);
2840 break;
2841
Willy Tarreau454f9052017-10-26 19:40:35 +02002842 case H2_FT_DATA:
2843 ret = h2_frt_transfer_data(h2s, buf, count);
2844 break;
2845
Willy Tarreau13278b42017-10-13 19:23:14 +02002846 default:
2847 ret = 0;
2848 }
2849 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002850}
2851
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002852/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2853 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2854 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2855 * to the number of buffer bytes consumed.
2856 */
2857static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2858{
2859 struct http_hdr list[MAX_HTTP_HDR];
2860 struct h2c *h2c = h2s->h2c;
2861 struct h1m *h1m = &h2s->res;
2862 struct chunk outbuf;
2863 int es_now = 0;
2864 int ret = 0;
2865 int hdr;
2866
2867 if (h2c_mux_busy(h2c, h2s)) {
2868 h2s->flags |= H2_SF_BLK_MBUSY;
2869 return 0;
2870 }
2871
Willy Tarreau44e973f2018-03-01 17:49:30 +01002872 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002873 h2c->flags |= H2_CF_MUX_MALLOC;
2874 h2s->flags |= H2_SF_BLK_MROOM;
2875 return 0;
2876 }
2877
2878 /* First, try to parse the H1 response and index it into <list>.
2879 * NOTE! Since it comes from haproxy, we *know* that a response header
2880 * block does not wrap and we can safely read it this way without
2881 * having to realign the buffer.
2882 */
2883 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2884 list, sizeof(list)/sizeof(list[0]), h1m);
2885 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002886 /* incomplete or invalid response, this is abnormal coming from
2887 * haproxy and may only result in a bad errorfile or bad Lua code
2888 * so that won't be fixed, raise an error now.
2889 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002890 * FIXME: we should instead add the ability to only return a
2891 * 502 bad gateway. But in theory this is not supposed to
2892 * happen.
2893 */
2894 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2895 ret = 0;
2896 goto end;
2897 }
2898
2899 chunk_reset(&outbuf);
2900
2901 while (1) {
2902 outbuf.str = bo_end(h2c->mbuf);
2903 outbuf.size = bo_contig_space(h2c->mbuf);
2904 outbuf.len = 0;
2905
2906 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2907 break;
2908 realign_again:
2909 buffer_slow_realign(h2c->mbuf);
2910 }
2911
2912 if (outbuf.size < 9) {
2913 h2c->flags |= H2_CF_MUX_MFULL;
2914 h2s->flags |= H2_SF_BLK_MROOM;
2915 ret = 0;
2916 goto end;
2917 }
2918
2919 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2920 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2921 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2922 outbuf.len = 9;
2923
2924 /* encode status, which necessarily is the first one */
2925 if (outbuf.len < outbuf.size && h1m->status == 200)
2926 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2927 else if (outbuf.len < outbuf.size && h1m->status == 304)
2928 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002929 else if (unlikely(list[0].v.len != 3)) {
2930 /* this is an unparsable response */
2931 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2932 ret = 0;
2933 goto end;
2934 }
2935 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002936 /* basic encoding of the status code */
2937 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2938 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2939 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2940 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2941 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2942 }
2943 else {
2944 if (buffer_space_wraps(h2c->mbuf))
2945 goto realign_again;
2946
2947 h2c->flags |= H2_CF_MUX_MFULL;
2948 h2s->flags |= H2_SF_BLK_MROOM;
2949 ret = 0;
2950 goto end;
2951 }
2952
2953 /* encode all headers, stop at empty name */
2954 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002955 /* these ones do not exist in H2 and must be dropped. */
2956 if (isteq(list[hdr].n, ist("connection")) ||
2957 isteq(list[hdr].n, ist("proxy-connection")) ||
2958 isteq(list[hdr].n, ist("keep-alive")) ||
2959 isteq(list[hdr].n, ist("upgrade")) ||
2960 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002961 continue;
2962
2963 if (isteq(list[hdr].n, ist("")))
2964 break; // end
2965
2966 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2967 /* output full */
2968 if (buffer_space_wraps(h2c->mbuf))
2969 goto realign_again;
2970
2971 h2c->flags |= H2_CF_MUX_MFULL;
2972 h2s->flags |= H2_SF_BLK_MROOM;
2973 ret = 0;
2974 goto end;
2975 }
2976 }
2977
2978 /* we may need to add END_STREAM */
2979 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2980 es_now = 1;
2981
2982 /* update the frame's size */
2983 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2984
2985 if (es_now)
2986 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2987
2988 /* consume incoming H1 response */
2989 bo_del(buf, ret);
2990
2991 /* commit the H2 response */
2992 h2c->mbuf->o += outbuf.len;
2993 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01002994 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002995
2996 /* for now we don't implemented CONTINUATION, so we wait for a
2997 * body or directly end in TRL2.
2998 */
2999 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003000 // trim any possibly pending data (eg: inconsistent content-length)
3001 bo_del(buf, buf->o);
3002
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003003 h1m->state = HTTP_MSG_DONE;
3004 h2s->flags |= H2_SF_ES_SENT;
3005 if (h2s->st == H2_SS_OPEN)
3006 h2s->st = H2_SS_HLOC;
3007 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003008 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003009 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003010 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003011 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003012 h1m->state = HTTP_MSG_RPBEFORE;
3013 h1m->status = 0;
3014 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003015 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003016 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003017 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003018 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003019
3020 end:
3021 //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));
3022 return ret;
3023}
3024
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003025/* Try to send a DATA frame matching HTTP/1 response present in the response
3026 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
3027 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
3028 * corresponds to the number of buffer bytes consumed.
3029 */
3030static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
3031{
3032 struct h2c *h2c = h2s->h2c;
3033 struct h1m *h1m = &h2s->res;
3034 struct chunk outbuf;
3035 int ret = 0;
3036 int total = 0;
3037 int es_now = 0;
3038 int size = 0;
3039 char *blk1, *blk2;
3040 int len1, len2;
3041
3042 if (h2c_mux_busy(h2c, h2s)) {
3043 h2s->flags |= H2_SF_BLK_MBUSY;
3044 goto end;
3045 }
3046
Willy Tarreau44e973f2018-03-01 17:49:30 +01003047 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003048 h2c->flags |= H2_CF_MUX_MALLOC;
3049 h2s->flags |= H2_SF_BLK_MROOM;
3050 goto end;
3051 }
3052
3053 new_frame:
3054 if (!buf->o)
3055 goto end;
3056
3057 chunk_reset(&outbuf);
3058
3059 while (1) {
3060 outbuf.str = bo_end(h2c->mbuf);
3061 outbuf.size = bo_contig_space(h2c->mbuf);
3062 outbuf.len = 0;
3063
3064 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
3065 break;
3066 realign_again:
3067 buffer_slow_realign(h2c->mbuf);
3068 }
3069
3070 if (outbuf.size < 9) {
3071 h2c->flags |= H2_CF_MUX_MFULL;
3072 h2s->flags |= H2_SF_BLK_MROOM;
3073 goto end;
3074 }
3075
3076 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3077 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3078 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3079 outbuf.len = 9;
3080
3081 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3082 case 0: /* no content length, read till SHUTW */
3083 size = buf->o;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003084 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003085 break;
3086 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
3087 size = buf->o;
3088 if ((long long)size > h1m->curr_len)
3089 size = h1m->curr_len;
3090 break;
3091 default: /* te:chunked : parse chunks */
3092 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
3093 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
3094 if (!ret)
3095 goto end;
3096
3097 if (ret < 0) {
3098 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3099 h1m->err_pos = ret;
3100 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3101 goto end;
3102 }
3103 bo_del(buf, ret);
3104 total += ret;
3105 h1m->state = HTTP_MSG_CHUNK_SIZE;
3106 }
3107
3108 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3109 unsigned int chunk;
3110
3111 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
3112 if (!ret)
3113 goto end;
3114
3115 if (ret < 0) {
3116 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3117 h1m->err_pos = ret;
3118 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3119 goto end;
3120 }
3121
3122 size = chunk;
3123 h1m->curr_len = chunk;
3124 h1m->body_len += chunk;
3125 bo_del(buf, ret);
3126 total += ret;
3127 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3128 if (!size)
3129 goto send_empty;
3130 }
3131
3132 /* in MSG_DATA state, continue below */
3133 size = h1m->curr_len;
3134 break;
3135 }
3136
3137 /* we have in <size> the exact number of bytes we need to copy from
3138 * the H1 buffer. We need to check this against the connection's and
3139 * the stream's send windows, and to ensure that this fits in the max
3140 * frame size and in the buffer's available space minus 9 bytes (for
3141 * the frame header). The connection's flow control is applied last so
3142 * that we can use a separate list of streams which are immediately
3143 * unblocked on window opening. Note: we don't implement padding.
3144 */
3145
3146 if (size > buf->o)
3147 size = buf->o;
3148
3149 if (size > h2s->mws)
3150 size = h2s->mws;
3151
3152 if (size <= 0) {
3153 h2s->flags |= H2_SF_BLK_SFCTL;
3154 goto end;
3155 }
3156
3157 if (h2c->mfs && size > h2c->mfs)
3158 size = h2c->mfs;
3159
3160 if (size + 9 > outbuf.size) {
3161 /* we have an opportunity for enlarging the too small
3162 * available space, let's try.
3163 */
3164 if (buffer_space_wraps(h2c->mbuf))
3165 goto realign_again;
3166 size = outbuf.size - 9;
3167 }
3168
3169 if (size <= 0) {
3170 h2c->flags |= H2_CF_MUX_MFULL;
3171 h2s->flags |= H2_SF_BLK_MROOM;
3172 goto end;
3173 }
3174
3175 if (size > h2c->mws)
3176 size = h2c->mws;
3177
3178 if (size <= 0) {
3179 h2s->flags |= H2_SF_BLK_MFCTL;
3180 goto end;
3181 }
3182
3183 /* copy whatever we can */
3184 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
3185 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
3186 if (ret == 1)
3187 len2 = 0;
3188
3189 if (!ret || len1 + len2 < size) {
3190 /* FIXME: must normally never happen */
3191 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3192 goto end;
3193 }
3194
3195 /* limit len1/len2 to size */
3196 if (len1 + len2 > size) {
3197 int sub = len1 + len2 - size;
3198
3199 if (len2 > sub)
3200 len2 -= sub;
3201 else {
3202 sub -= len2;
3203 len2 = 0;
3204 len1 -= sub;
3205 }
3206 }
3207
3208 /* now let's copy this this into the output buffer */
3209 memcpy(outbuf.str + 9, blk1, len1);
3210 if (len2)
3211 memcpy(outbuf.str + 9 + len1, blk2, len2);
3212
3213 send_empty:
3214 /* we may need to add END_STREAM */
3215 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3216 * could rely on the MSG_MORE flag as a hint for this ?
3217 */
3218 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3219 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3220 es_now = 1;
3221
3222 /* update the frame's size */
3223 h2_set_frame_size(outbuf.str, size);
3224
3225 if (es_now)
3226 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3227
3228 /* commit the H2 response */
3229 h2c->mbuf->o += size + 9;
3230 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3231
3232 /* consume incoming H1 response */
3233 if (size > 0) {
3234 bo_del(buf, size);
3235 total += size;
3236 h1m->curr_len -= size;
3237 h2s->mws -= size;
3238 h2c->mws -= size;
3239
3240 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3241 h1m->state = HTTP_MSG_CHUNK_CRLF;
3242 goto new_frame;
3243 }
3244 }
3245
3246 if (es_now) {
3247 if (h2s->st == H2_SS_OPEN)
3248 h2s->st = H2_SS_HLOC;
3249 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003250 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003251
Willy Tarreau35a62702018-02-27 15:37:25 +01003252 if (!(h1m->flags & H1_MF_CHNK)) {
3253 // trim any possibly pending data (eg: inconsistent content-length)
3254 bo_del(buf, buf->o);
3255
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003256 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003257 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003258
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003259 h2s->flags |= H2_SF_ES_SENT;
3260 }
3261
3262 end:
3263 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);
3264 return total;
3265}
3266
Willy Tarreau62f52692017-10-08 23:01:42 +02003267/* Called from the upper layer, to send data */
3268static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3269{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003270 struct h2s *h2s = cs->ctx;
3271 int total = 0;
3272
Willy Tarreauc4312d32017-11-07 12:01:53 +01003273 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3274 h2s->flags |= H2_SF_OUTGOING_DATA;
3275
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003276 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3277 if (h2s->res.state < HTTP_MSG_BODY) {
3278 total += h2s_frt_make_resp_headers(h2s, buf);
3279
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003280 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003281 break;
3282
3283 if (h2s->flags & H2_SF_BLK_ANY)
3284 break;
3285 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003286 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3287 total += h2s_frt_make_resp_data(h2s, buf);
3288
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003289 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003290 break;
3291
3292 if (h2s->flags & H2_SF_BLK_ANY)
3293 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003294 }
3295 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3296 /* consume the trailers if any (we don't forward them for now) */
3297 int count = h1_measure_trailers(buf);
3298
3299 if (unlikely(count <= 0)) {
3300 if (count < 0)
3301 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3302 break;
3303 }
3304 total += count;
3305 bo_del(buf, count);
Willy Tarreau35a62702018-02-27 15:37:25 +01003306
3307 // trim any possibly pending data (eg: extra CR-LF, ...)
3308 bo_del(buf, buf->o);
3309
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003310 h2s->res.state = HTTP_MSG_DONE;
3311 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003312 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003313 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003314 cs->flags |= CS_FL_ERROR;
3315 break;
3316 }
3317 }
3318
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003319 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003320 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003321 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003322 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003323 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003324 }
3325
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003326 if (h2s->flags & H2_SF_BLK_SFCTL) {
3327 /* stream flow control, quit the list */
3328 LIST_DEL(&h2s->list);
3329 LIST_INIT(&h2s->list);
3330 }
3331
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003332 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003333}
3334
3335
3336/*******************************************************/
3337/* functions below are dedicated to the config parsers */
3338/*******************************************************/
3339
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003340/* config parser for global "tune.h2.header-table-size" */
3341static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3342 struct proxy *defpx, const char *file, int line,
3343 char **err)
3344{
3345 if (too_many_args(1, args, err, NULL))
3346 return -1;
3347
3348 h2_settings_header_table_size = atoi(args[1]);
3349 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3350 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3351 return -1;
3352 }
3353 return 0;
3354}
Willy Tarreau62f52692017-10-08 23:01:42 +02003355
Willy Tarreaue6baec02017-07-27 11:45:11 +02003356/* config parser for global "tune.h2.initial-window-size" */
3357static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3358 struct proxy *defpx, const char *file, int line,
3359 char **err)
3360{
3361 if (too_many_args(1, args, err, NULL))
3362 return -1;
3363
3364 h2_settings_initial_window_size = atoi(args[1]);
3365 if (h2_settings_initial_window_size < 0) {
3366 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3367 return -1;
3368 }
3369 return 0;
3370}
3371
Willy Tarreau5242ef82017-07-27 11:47:28 +02003372/* config parser for global "tune.h2.max-concurrent-streams" */
3373static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3374 struct proxy *defpx, const char *file, int line,
3375 char **err)
3376{
3377 if (too_many_args(1, args, err, NULL))
3378 return -1;
3379
3380 h2_settings_max_concurrent_streams = atoi(args[1]);
3381 if (h2_settings_max_concurrent_streams < 0) {
3382 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3383 return -1;
3384 }
3385 return 0;
3386}
3387
Willy Tarreau62f52692017-10-08 23:01:42 +02003388
3389/****************************************/
3390/* MUX initialization and instanciation */
3391/***************************************/
3392
3393/* The mux operations */
3394const struct mux_ops h2_ops = {
3395 .init = h2_init,
3396 .recv = h2_recv,
3397 .send = h2_send,
3398 .wake = h2_wake,
3399 .update_poll = h2_update_poll,
3400 .rcv_buf = h2_rcv_buf,
3401 .snd_buf = h2_snd_buf,
3402 .attach = h2_attach,
3403 .detach = h2_detach,
3404 .shutr = h2_shutr,
3405 .shutw = h2_shutw,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003406 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003407 .name = "H2",
3408};
3409
3410/* ALPN selection : this mux registers ALPN tolen "h2" */
3411static struct alpn_mux_list alpn_mux_h2 =
3412 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3413
3414/* config keyword parsers */
3415static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003416 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003417 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003418 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003419 { 0, NULL, NULL }
3420}};
3421
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003422static void __h2_deinit(void)
3423{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003424 pool_destroy(pool_head_h2s);
3425 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003426}
3427
Willy Tarreau62f52692017-10-08 23:01:42 +02003428__attribute__((constructor))
3429static void __h2_init(void)
3430{
3431 alpn_register_mux(&alpn_mux_h2);
3432 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003433 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003434 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3435 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003436}