blob: 46d69bb29eb4c3f926ab1ae8114ac536585af7c2 [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 Tarreau62f52692017-10-08 23:01:42 +020020#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020021#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020022#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010023#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020024#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025
26
Willy Tarreau2a856182017-05-16 15:20:39 +020027/* dummy streams returned for idle and closed states */
28static const struct h2s *h2_closed_stream;
29static const struct h2s *h2_idle_stream;
30
Willy Tarreau5ab6b572017-09-22 08:05:00 +020031/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010032static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020033/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010034static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020035
36/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // 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 */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#define H2_CF_WAIT_FOR_HS 0x00004000 // 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 */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200117 unsigned int nb_cs; /* number of attached conn_streams */
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
Olivier Houchard9f6af332018-05-25 14:04:04 +0200218static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
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{
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200242 if (b_data(h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100243 (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 Tarreauf2101912018-07-19 10:11:38 +0200255/* returns true if the connection has too many conn_streams attached */
256static inline int h2_has_too_many_cs(const struct h2c *h2c)
257{
258 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
259}
260
Willy Tarreau44e973f2018-03-01 17:49:30 +0100261/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
262 * flags are used to figure what buffer was requested. It returns 1 if the
263 * allocation succeeds, in which case the connection is woken up, or 0 if it's
264 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200265 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100266static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200267{
268 struct h2c *h2c = target;
269
Willy Tarreau44e973f2018-03-01 17:49:30 +0100270 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200271 h2c->flags &= ~H2_CF_DEM_DALLOC;
Willy Tarreau315d8072017-12-10 22:17:57 +0100272 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200273 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200274 return 1;
275 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200276
Willy Tarreau44e973f2018-03-01 17:49:30 +0100277 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
278 h2c->flags &= ~H2_CF_MUX_MALLOC;
279 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
280 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200281
282 if (h2c->flags & H2_CF_DEM_MROOM) {
283 h2c->flags &= ~H2_CF_DEM_MROOM;
Willy Tarreau315d8072017-12-10 22:17:57 +0100284 if (h2_recv_allowed(h2c))
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200285 conn_xprt_want_recv(h2c->conn);
286 }
Willy Tarreau14398122017-09-22 14:26:04 +0200287 return 1;
288 }
289 return 0;
290}
291
Willy Tarreau44e973f2018-03-01 17:49:30 +0100292static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer **bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200293{
294 struct buffer *buf = NULL;
295
Willy Tarreau44e973f2018-03-01 17:49:30 +0100296 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
297 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
298 h2c->buf_wait.target = h2c;
299 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100300 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100301 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100302 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200303 __conn_xprt_stop_recv(h2c->conn);
304 }
305 return buf;
306}
307
Willy Tarreau44e973f2018-03-01 17:49:30 +0100308static inline void h2_release_buf(struct h2c *h2c, struct buffer **bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200309{
Willy Tarreau44e973f2018-03-01 17:49:30 +0100310 if ((*bptr)->size) {
311 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200312 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200313 }
314}
315
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200316
Willy Tarreau62f52692017-10-08 23:01:42 +0200317/*****************************************************************/
318/* functions below are dedicated to the mux setup and management */
319/*****************************************************************/
320
Willy Tarreau32218eb2017-09-22 08:07:25 +0200321/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
322static int h2c_frt_init(struct connection *conn)
323{
324 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100325 struct task *t = NULL;
326 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200327
Willy Tarreaubafbe012017-11-24 17:34:44 +0100328 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200329 if (!h2c)
330 goto fail;
331
Willy Tarreau3f133572017-10-31 19:21:06 +0100332
Willy Tarreau599391a2017-11-24 10:16:00 +0100333 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
334 if (tick_isset(sess->fe->timeout.clientfin))
335 h2c->shut_timeout = sess->fe->timeout.clientfin;
336
Willy Tarreau33400292017-11-05 11:23:40 +0100337 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100338 if (tick_isset(h2c->timeout)) {
339 t = task_new(tid_bit);
340 if (!t)
341 goto fail;
342
343 h2c->task = t;
344 t->process = h2_timeout_task;
345 t->context = h2c;
346 t->expire = tick_add(now_ms, h2c->timeout);
347 }
Willy Tarreauea392822017-10-31 10:02:25 +0100348
Willy Tarreau32218eb2017-09-22 08:07:25 +0200349 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
350 if (!h2c->ddht)
351 goto fail;
352
353 /* Initialise the context. */
354 h2c->st0 = H2_CS_PREFACE;
355 h2c->conn = conn;
356 h2c->max_id = -1;
357 h2c->errcode = H2_ERR_NO_ERROR;
358 h2c->flags = H2_CF_NONE;
359 h2c->rcvd_c = 0;
360 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100361 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200362 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200363
364 h2c->dbuf = &buf_empty;
365 h2c->dsi = -1;
366 h2c->msi = -1;
367 h2c->last_sid = -1;
368
369 h2c->mbuf = &buf_empty;
370 h2c->miw = 65535; /* mux initial window size */
371 h2c->mws = 65535; /* mux window size */
372 h2c->mfs = 16384; /* initial max frame size */
373 h2c->streams_by_id = EB_ROOT_UNIQUE;
374 LIST_INIT(&h2c->send_list);
375 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100376 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200377 conn->mux_ctx = h2c;
378
Willy Tarreau3f133572017-10-31 19:21:06 +0100379 if (t)
380 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200381 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100382
Willy Tarreau32218eb2017-09-22 08:07:25 +0200383 /* mux->wake will be called soon to complete the operation */
384 return 0;
385 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100386 if (t)
387 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100388 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200389 return -1;
390}
391
Willy Tarreau62f52692017-10-08 23:01:42 +0200392/* Initialize the mux once it's attached. For outgoing connections, the context
393 * is already initialized before installing the mux, so we detect incoming
394 * connections from the fact that the context is still NULL. Returns < 0 on
395 * error.
396 */
397static int h2_init(struct connection *conn)
398{
399 if (conn->mux_ctx) {
400 /* we don't support outgoing connections for now */
401 return -1;
402 }
403
Willy Tarreau32218eb2017-09-22 08:07:25 +0200404 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200405}
406
Willy Tarreau2373acc2017-10-12 17:35:14 +0200407/* returns the stream associated with id <id> or NULL if not found */
408static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
409{
410 struct eb32_node *node;
411
Willy Tarreau2a856182017-05-16 15:20:39 +0200412 if (id > h2c->max_id)
413 return (struct h2s *)h2_idle_stream;
414
Willy Tarreau2373acc2017-10-12 17:35:14 +0200415 node = eb32_lookup(&h2c->streams_by_id, id);
416 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200417 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200418
419 return container_of(node, struct h2s, by_id);
420}
421
Willy Tarreau62f52692017-10-08 23:01:42 +0200422/* release function for a connection. This one should be called to free all
423 * resources allocated to the mux.
424 */
425static void h2_release(struct connection *conn)
426{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200427 struct h2c *h2c = conn->mux_ctx;
428
429 LIST_DEL(&conn->list);
430
431 if (h2c) {
432 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200433
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100434 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100435 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100436 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200437
Willy Tarreau44e973f2018-03-01 17:49:30 +0100438 h2_release_buf(h2c, &h2c->dbuf);
439 h2_release_buf(h2c, &h2c->mbuf);
440
Willy Tarreauea392822017-10-31 10:02:25 +0100441 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200442 h2c->task->context = NULL;
443 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100444 h2c->task = NULL;
445 }
446
Willy Tarreaubafbe012017-11-24 17:34:44 +0100447 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200448 }
449
450 conn->mux = NULL;
451 conn->mux_ctx = NULL;
452
453 conn_stop_tracking(conn);
454 conn_full_close(conn);
455 if (conn->destroy_cb)
456 conn->destroy_cb(conn);
457 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200458}
459
460
Willy Tarreau71681172017-10-23 14:39:06 +0200461/******************************************************/
462/* functions below are for the H2 protocol processing */
463/******************************************************/
464
465/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100466static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200467{
468 return h2s ? h2s->id : 0;
469}
470
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200471/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100472static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200473{
474 if (h2c->msi < 0)
475 return 0;
476
477 if (h2c->msi == h2s_id(h2s))
478 return 0;
479
480 return 1;
481}
482
Willy Tarreau741d6df2017-10-17 08:00:59 +0200483/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100484static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200485{
486 h2c->errcode = err;
487 h2c->st0 = H2_CS_ERROR;
488}
489
Willy Tarreau2e43f082017-10-17 08:03:59 +0200490/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100491static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200492{
493 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
494 h2s->errcode = err;
495 h2s->st = H2_SS_ERROR;
496 if (h2s->cs)
497 h2s->cs->flags |= CS_FL_ERROR;
498 }
499}
500
Willy Tarreaue4820742017-07-27 13:37:23 +0200501/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100502static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200503{
504 uint8_t *out = frame;
505
506 *out = len >> 16;
507 write_n16(out + 1, len);
508}
509
Willy Tarreau54c15062017-10-10 17:10:03 +0200510/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
511 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
512 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200513 * available in the buffer's input prior to calling this function. The buffer
514 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200515 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100516static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200517 const struct buffer *b, int o)
518{
Willy Tarreau591d4452018-06-15 17:21:00 +0200519 readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200520}
521
Willy Tarreau1f094672017-11-20 21:27:45 +0100522static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200523{
Willy Tarreau591d4452018-06-15 17:21:00 +0200524 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200525}
526
Willy Tarreau1f094672017-11-20 21:27:45 +0100527static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200528{
Willy Tarreau591d4452018-06-15 17:21:00 +0200529 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200530}
531
Willy Tarreau1f094672017-11-20 21:27:45 +0100532static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200533{
Willy Tarreau591d4452018-06-15 17:21:00 +0200534 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200535}
536
537
Willy Tarreau715d5312017-07-11 15:20:24 +0200538/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
539 * is not obvious. It turns out that H2 headers are neither aligned nor do they
540 * use regular sizes. And to add to the trouble, the buffer may wrap so each
541 * byte read must be checked. The header is formed like this :
542 *
543 * b0 b1 b2 b3 b4 b5..b8
544 * +----------+---------+--------+----+----+----------------------+
545 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
546 * +----------+---------+--------+----+----+----------------------+
547 *
548 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
549 * we get the sid properly aligned and ordered, and 16 bits of len properly
550 * ordered as well. The type and flags can be extracted using bit shifts from
551 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200552 * Returns zero if some bytes are missing, otherwise non-zero on success. The
553 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200554 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100555static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200556{
557 uint64_t w;
558
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200559 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200560 return 0;
561
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200562 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200563 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200564 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
565 h->ff = w >> 32;
566 h->ft = w >> 40;
567 h->len += w >> 48;
568 return 1;
569}
570
571/* skip the next 9 bytes corresponding to the frame header possibly parsed by
572 * h2_peek_frame_hdr() above.
573 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100574static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200575{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200576 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200577}
578
579/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100580static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200581{
582 int ret;
583
584 ret = h2_peek_frame_hdr(b, h);
585 if (ret > 0)
586 h2_skip_frame_hdr(b);
587 return ret;
588}
589
Willy Tarreau00dd0782018-03-01 16:31:34 +0100590/* marks stream <h2s> as CLOSED and decrement the number of active streams for
591 * its connection if the stream was not yet closed. Please use this exclusively
592 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100593 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100594static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100595{
596 if (h2s->st != H2_SS_CLOSED)
597 h2s->h2c->nb_streams--;
598 h2s->st = H2_SS_CLOSED;
599}
600
Willy Tarreau71049cc2018-03-28 13:56:39 +0200601/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
602static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100603{
604 h2s_close(h2s);
Willy Tarreau4a333d32018-03-28 11:29:04 +0200605 LIST_DEL(&h2s->list);
606 LIST_INIT(&h2s->list);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100607 eb32_delete(&h2s->by_id);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100608 pool_free(pool_head_h2s, h2s);
609}
610
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200611/* creates a new stream <id> on the h2c connection and returns it, or NULL in
612 * case of memory allocation error.
613 */
614static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
615{
616 struct conn_stream *cs;
617 struct h2s *h2s;
618
Willy Tarreaubafbe012017-11-24 17:34:44 +0100619 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200620 if (!h2s)
621 goto out;
622
623 h2s->h2c = h2c;
624 h2s->mws = h2c->miw;
625 h2s->flags = H2_SF_NONE;
626 h2s->errcode = H2_ERR_NO_ERROR;
627 h2s->st = H2_SS_IDLE;
628 h1m_init(&h2s->req);
629 h1m_init(&h2s->res);
630 h2s->by_id.key = h2s->id = id;
631 h2c->max_id = id;
632 LIST_INIT(&h2s->list);
633
634 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100635 h2c->nb_streams++;
636 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
637 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200638
639 cs = cs_new(h2c->conn);
640 if (!cs)
641 goto out_close;
642
643 h2s->cs = cs;
644 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200645 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200646
647 if (stream_create_from_cs(cs) < 0)
648 goto out_free_cs;
649
650 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200651 if (h2_has_too_many_cs(h2c))
652 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200653 return h2s;
654
655 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200656 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200657 cs_free(cs);
658 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200659 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200660 h2s = NULL;
661 out:
662 return h2s;
663}
664
Willy Tarreaube5b7152017-09-25 16:25:39 +0200665/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
666 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
667 * the various settings codes.
668 */
669static int h2c_snd_settings(struct h2c *h2c)
670{
671 struct buffer *res;
672 char buf_data[100]; // enough for 15 settings
673 struct chunk buf;
674 int ret;
675
676 if (h2c_mux_busy(h2c, NULL)) {
677 h2c->flags |= H2_CF_DEM_MBUSY;
678 return 0;
679 }
680
Willy Tarreau44e973f2018-03-01 17:49:30 +0100681 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200682 if (!res) {
683 h2c->flags |= H2_CF_MUX_MALLOC;
684 h2c->flags |= H2_CF_DEM_MROOM;
685 return 0;
686 }
687
688 chunk_init(&buf, buf_data, sizeof(buf_data));
689 chunk_memcpy(&buf,
690 "\x00\x00\x00" /* length : 0 for now */
691 "\x04\x00" /* type : 4 (settings), flags : 0 */
692 "\x00\x00\x00\x00", /* stream ID : 0 */
693 9);
694
695 if (h2_settings_header_table_size != 4096) {
696 char str[6] = "\x00\x01"; /* header_table_size */
697
698 write_n32(str + 2, h2_settings_header_table_size);
699 chunk_memcat(&buf, str, 6);
700 }
701
702 if (h2_settings_initial_window_size != 65535) {
703 char str[6] = "\x00\x04"; /* initial_window_size */
704
705 write_n32(str + 2, h2_settings_initial_window_size);
706 chunk_memcat(&buf, str, 6);
707 }
708
709 if (h2_settings_max_concurrent_streams != 0) {
710 char str[6] = "\x00\x03"; /* max_concurrent_streams */
711
712 /* Note: 0 means "unlimited" for haproxy's config but not for
713 * the protocol, so never send this value!
714 */
715 write_n32(str + 2, h2_settings_max_concurrent_streams);
716 chunk_memcat(&buf, str, 6);
717 }
718
719 if (global.tune.bufsize != 16384) {
720 char str[6] = "\x00\x05"; /* max_frame_size */
721
722 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
723 * match bufsize - rewrite size, but at the moment it seems
724 * that clients don't take care of it.
725 */
726 write_n32(str + 2, global.tune.bufsize);
727 chunk_memcat(&buf, str, 6);
728 }
729
730 h2_set_frame_size(buf.str, buf.len - 9);
731 ret = bo_istput(res, ist2(buf.str, buf.len));
732 if (unlikely(ret <= 0)) {
733 if (!ret) {
734 h2c->flags |= H2_CF_MUX_MFULL;
735 h2c->flags |= H2_CF_DEM_MROOM;
736 return 0;
737 }
738 else {
739 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
740 return 0;
741 }
742 }
743 return ret;
744}
745
Willy Tarreau52eed752017-09-22 15:05:09 +0200746/* Try to receive a connection preface, then upon success try to send our
747 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
748 * missing data. It may return an error in h2c.
749 */
750static int h2c_frt_recv_preface(struct h2c *h2c)
751{
752 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200753 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200754
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200755 ret1 = b_isteq(h2c->dbuf, 0, b_data(h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200756
757 if (unlikely(ret1 <= 0)) {
758 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
759 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
760 return 0;
761 }
762
Willy Tarreaube5b7152017-09-25 16:25:39 +0200763 ret2 = h2c_snd_settings(h2c);
764 if (ret2 > 0)
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200765 b_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200766
Willy Tarreaube5b7152017-09-25 16:25:39 +0200767 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200768}
769
Willy Tarreau081d4722017-05-16 21:51:05 +0200770/* try to send a GOAWAY frame on the connection to report an error or a graceful
771 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
772 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
773 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
774 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
775 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
776 * on unrecoverable failure. It will not attempt to send one again in this last
777 * case so that it is safe to use h2c_error() to report such errors.
778 */
779static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
780{
781 struct buffer *res;
782 char str[17];
783 int ret;
784
785 if (h2c->flags & H2_CF_GOAWAY_FAILED)
786 return 1; // claim that it worked
787
788 if (h2c_mux_busy(h2c, h2s)) {
789 if (h2s)
790 h2s->flags |= H2_SF_BLK_MBUSY;
791 else
792 h2c->flags |= H2_CF_DEM_MBUSY;
793 return 0;
794 }
795
Willy Tarreau44e973f2018-03-01 17:49:30 +0100796 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200797 if (!res) {
798 h2c->flags |= H2_CF_MUX_MALLOC;
799 if (h2s)
800 h2s->flags |= H2_SF_BLK_MROOM;
801 else
802 h2c->flags |= H2_CF_DEM_MROOM;
803 return 0;
804 }
805
806 /* len: 8, type: 7, flags: none, sid: 0 */
807 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
808
809 if (h2c->last_sid < 0)
810 h2c->last_sid = h2c->max_id;
811
812 write_n32(str + 9, h2c->last_sid);
813 write_n32(str + 13, h2c->errcode);
814 ret = bo_istput(res, ist2(str, 17));
815 if (unlikely(ret <= 0)) {
816 if (!ret) {
817 h2c->flags |= H2_CF_MUX_MFULL;
818 if (h2s)
819 h2s->flags |= H2_SF_BLK_MROOM;
820 else
821 h2c->flags |= H2_CF_DEM_MROOM;
822 return 0;
823 }
824 else {
825 /* we cannot report this error using GOAWAY, so we mark
826 * it and claim a success.
827 */
828 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
829 h2c->flags |= H2_CF_GOAWAY_FAILED;
830 return 1;
831 }
832 }
833 h2c->flags |= H2_CF_GOAWAY_SENT;
834 return ret;
835}
836
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100837/* Try to send an RST_STREAM frame on the connection for the indicated stream
838 * during mux operations. This stream must be valid and cannot be closed
839 * already. h2s->id will be used for the stream ID and h2s->errcode will be
840 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
841 * not yet.
842 *
843 * Returns > 0 on success or zero if nothing was done. In case of lack of room
844 * to write the message, it subscribes the stream to future notifications.
845 */
846static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
847{
848 struct buffer *res;
849 char str[13];
850 int ret;
851
852 if (!h2s || h2s->st == H2_SS_CLOSED)
853 return 1;
854
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100855 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
856 * RST_STREAM in response to a RST_STREAM frame.
857 */
858 if (h2c->dft == H2_FT_RST_STREAM) {
859 ret = 1;
860 goto ignore;
861 }
862
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100863 if (h2c_mux_busy(h2c, h2s)) {
864 h2s->flags |= H2_SF_BLK_MBUSY;
865 return 0;
866 }
867
Willy Tarreau44e973f2018-03-01 17:49:30 +0100868 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100869 if (!res) {
870 h2c->flags |= H2_CF_MUX_MALLOC;
871 h2s->flags |= H2_SF_BLK_MROOM;
872 return 0;
873 }
874
875 /* len: 4, type: 3, flags: none */
876 memcpy(str, "\x00\x00\x04\x03\x00", 5);
877 write_n32(str + 5, h2s->id);
878 write_n32(str + 9, h2s->errcode);
879 ret = bo_istput(res, ist2(str, 13));
880
881 if (unlikely(ret <= 0)) {
882 if (!ret) {
883 h2c->flags |= H2_CF_MUX_MFULL;
884 h2s->flags |= H2_SF_BLK_MROOM;
885 return 0;
886 }
887 else {
888 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
889 return 0;
890 }
891 }
892
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100893 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100894 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100895 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100896 return ret;
897}
898
899/* Try to send an RST_STREAM frame on the connection for the stream being
900 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
901 * error code unless the stream's state already is IDLE or CLOSED in which
902 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
903 * it was not yet.
904 *
905 * Returns > 0 on success or zero if nothing was done. In case of lack of room
906 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200907 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100908 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200909 */
910static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
911{
912 struct buffer *res;
913 char str[13];
914 int ret;
915
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100916 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
917 * RST_STREAM in response to a RST_STREAM frame.
918 */
919 if (h2c->dft == H2_FT_RST_STREAM) {
920 ret = 1;
921 goto ignore;
922 }
923
Willy Tarreau27a84c92017-10-17 08:10:17 +0200924 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100925 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200926 return 0;
927 }
928
Willy Tarreau44e973f2018-03-01 17:49:30 +0100929 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +0200930 if (!res) {
931 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100932 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200933 return 0;
934 }
935
936 /* len: 4, type: 3, flags: none */
937 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100938
Willy Tarreau27a84c92017-10-17 08:10:17 +0200939 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100940 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200941 h2s->errcode : H2_ERR_STREAM_CLOSED);
942 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100943
Willy Tarreau27a84c92017-10-17 08:10:17 +0200944 if (unlikely(ret <= 0)) {
945 if (!ret) {
946 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100947 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200948 return 0;
949 }
950 else {
951 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
952 return 0;
953 }
954 }
955
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100956 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100957 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200958 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100959 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100960 }
961
Willy Tarreau27a84c92017-10-17 08:10:17 +0200962 return ret;
963}
964
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100965/* try to send an empty DATA frame with the ES flag set to notify about the
966 * end of stream and match a shutdown(write). If an ES was already sent as
967 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
968 * on success or zero if nothing was done. In case of lack of room to write the
969 * message, it subscribes the requesting stream to future notifications.
970 */
971static int h2_send_empty_data_es(struct h2s *h2s)
972{
973 struct h2c *h2c = h2s->h2c;
974 struct buffer *res;
975 char str[9];
976 int ret;
977
Willy Tarreau721c9742017-11-07 11:05:42 +0100978 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100979 return 1;
980
981 if (h2c_mux_busy(h2c, h2s)) {
982 h2s->flags |= H2_SF_BLK_MBUSY;
983 return 0;
984 }
985
Willy Tarreau44e973f2018-03-01 17:49:30 +0100986 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100987 if (!res) {
988 h2c->flags |= H2_CF_MUX_MALLOC;
989 h2s->flags |= H2_SF_BLK_MROOM;
990 return 0;
991 }
992
993 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
994 memcpy(str, "\x00\x00\x00\x00\x01", 5);
995 write_n32(str + 5, h2s->id);
996 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100997 if (likely(ret > 0)) {
998 h2s->flags |= H2_SF_ES_SENT;
999 }
1000 else if (!ret) {
1001 h2c->flags |= H2_CF_MUX_MFULL;
1002 h2s->flags |= H2_SF_BLK_MROOM;
1003 return 0;
1004 }
1005 else {
1006 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1007 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001008 }
1009 return ret;
1010}
1011
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001012/* wake the streams attached to the connection, whose id is greater than <last>,
1013 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
1014 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
1015 * stream's state is automatically updated accordingly.
1016 */
1017static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1018{
1019 struct eb32_node *node;
1020 struct h2s *h2s;
1021
1022 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1023 flags |= CS_FL_ERROR;
1024
1025 if (conn_xprt_read0_pending(h2c->conn))
1026 flags |= CS_FL_EOS;
1027
1028 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1029 while (node) {
1030 h2s = container_of(node, struct h2s, by_id);
1031 if (h2s->id <= last)
1032 break;
1033 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001034
1035 if (!h2s->cs) {
1036 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001037 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001038 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001039 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001040
1041 h2s->cs->flags |= flags;
1042 /* recv is used to force to detect CS_FL_EOS that wake()
1043 * doesn't handle in the stream int code.
1044 */
1045 h2s->cs->data_cb->recv(h2s->cs);
1046 h2s->cs->data_cb->wake(h2s->cs);
1047
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001048 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1049 h2s->st = H2_SS_ERROR;
1050 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1051 h2s->st = H2_SS_HREM;
1052 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001053 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001054 }
1055}
1056
Willy Tarreau3421aba2017-07-27 15:41:03 +02001057/* Increase all streams' outgoing window size by the difference passed in
1058 * argument. This is needed upon receipt of the settings frame if the initial
1059 * window size is different. The difference may be negative and the resulting
1060 * window size as well, for the time it takes to receive some window updates.
1061 */
1062static void h2c_update_all_ws(struct h2c *h2c, int diff)
1063{
1064 struct h2s *h2s;
1065 struct eb32_node *node;
1066
1067 if (!diff)
1068 return;
1069
1070 node = eb32_first(&h2c->streams_by_id);
1071 while (node) {
1072 h2s = container_of(node, struct h2s, by_id);
1073 h2s->mws += diff;
1074 node = eb32_next(node);
1075 }
1076}
1077
1078/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1079 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1080 * return an error in h2c. Described in RFC7540#6.5.
1081 */
1082static int h2c_handle_settings(struct h2c *h2c)
1083{
1084 unsigned int offset;
1085 int error;
1086
1087 if (h2c->dff & H2_F_SETTINGS_ACK) {
1088 if (h2c->dfl) {
1089 error = H2_ERR_FRAME_SIZE_ERROR;
1090 goto fail;
1091 }
1092 return 1;
1093 }
1094
1095 if (h2c->dsi != 0) {
1096 error = H2_ERR_PROTOCOL_ERROR;
1097 goto fail;
1098 }
1099
1100 if (h2c->dfl % 6) {
1101 error = H2_ERR_FRAME_SIZE_ERROR;
1102 goto fail;
1103 }
1104
1105 /* that's the limit we can process */
1106 if (h2c->dfl > global.tune.bufsize) {
1107 error = H2_ERR_FRAME_SIZE_ERROR;
1108 goto fail;
1109 }
1110
1111 /* process full frame only */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001112 if (b_data(h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001113 return 0;
1114
1115 /* parse the frame */
1116 for (offset = 0; offset < h2c->dfl; offset += 6) {
1117 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1118 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1119
1120 switch (type) {
1121 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1122 /* we need to update all existing streams with the
1123 * difference from the previous iws.
1124 */
1125 if (arg < 0) { // RFC7540#6.5.2
1126 error = H2_ERR_FLOW_CONTROL_ERROR;
1127 goto fail;
1128 }
1129 h2c_update_all_ws(h2c, arg - h2c->miw);
1130 h2c->miw = arg;
1131 break;
1132 case H2_SETTINGS_MAX_FRAME_SIZE:
1133 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1134 error = H2_ERR_PROTOCOL_ERROR;
1135 goto fail;
1136 }
1137 h2c->mfs = arg;
1138 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001139 case H2_SETTINGS_ENABLE_PUSH:
1140 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1141 error = H2_ERR_PROTOCOL_ERROR;
1142 goto fail;
1143 }
1144 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001145 }
1146 }
1147
1148 /* need to ACK this frame now */
1149 h2c->st0 = H2_CS_FRAME_A;
1150 return 1;
1151 fail:
1152 h2c_error(h2c, error);
1153 return 0;
1154}
1155
1156/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1157 * success or one of the h2_status values.
1158 */
1159static int h2c_ack_settings(struct h2c *h2c)
1160{
1161 struct buffer *res;
1162 char str[9];
1163 int ret = -1;
1164
1165 if (h2c_mux_busy(h2c, NULL)) {
1166 h2c->flags |= H2_CF_DEM_MBUSY;
1167 return 0;
1168 }
1169
Willy Tarreau44e973f2018-03-01 17:49:30 +01001170 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001171 if (!res) {
1172 h2c->flags |= H2_CF_MUX_MALLOC;
1173 h2c->flags |= H2_CF_DEM_MROOM;
1174 return 0;
1175 }
1176
1177 memcpy(str,
1178 "\x00\x00\x00" /* length : 0 (no data) */
1179 "\x04" "\x01" /* type : 4, flags : ACK */
1180 "\x00\x00\x00\x00" /* stream ID */, 9);
1181
1182 ret = bo_istput(res, ist2(str, 9));
1183 if (unlikely(ret <= 0)) {
1184 if (!ret) {
1185 h2c->flags |= H2_CF_MUX_MFULL;
1186 h2c->flags |= H2_CF_DEM_MROOM;
1187 return 0;
1188 }
1189 else {
1190 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1191 return 0;
1192 }
1193 }
1194 return ret;
1195}
1196
Willy Tarreaucf68c782017-10-10 17:11:41 +02001197/* processes a PING frame and schedules an ACK if needed. The caller must pass
1198 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1199 * missing data. It may return an error in h2c.
1200 */
1201static int h2c_handle_ping(struct h2c *h2c)
1202{
1203 /* frame length must be exactly 8 */
1204 if (h2c->dfl != 8) {
1205 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1206 return 0;
1207 }
1208
1209 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001210 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001211 h2c->st0 = H2_CS_FRAME_A;
1212 return 1;
1213}
1214
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001215/* Try to send a window update for stream id <sid> and value <increment>.
1216 * Returns > 0 on success or zero on missing room or failure. It may return an
1217 * error in h2c.
1218 */
1219static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1220{
1221 struct buffer *res;
1222 char str[13];
1223 int ret = -1;
1224
1225 if (h2c_mux_busy(h2c, NULL)) {
1226 h2c->flags |= H2_CF_DEM_MBUSY;
1227 return 0;
1228 }
1229
Willy Tarreau44e973f2018-03-01 17:49:30 +01001230 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001231 if (!res) {
1232 h2c->flags |= H2_CF_MUX_MALLOC;
1233 h2c->flags |= H2_CF_DEM_MROOM;
1234 return 0;
1235 }
1236
1237 /* length: 4, type: 8, flags: none */
1238 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1239 write_n32(str + 5, sid);
1240 write_n32(str + 9, increment);
1241
1242 ret = bo_istput(res, ist2(str, 13));
1243
1244 if (unlikely(ret <= 0)) {
1245 if (!ret) {
1246 h2c->flags |= H2_CF_MUX_MFULL;
1247 h2c->flags |= H2_CF_DEM_MROOM;
1248 return 0;
1249 }
1250 else {
1251 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1252 return 0;
1253 }
1254 }
1255 return ret;
1256}
1257
1258/* try to send pending window update for the connection. It's safe to call it
1259 * with no pending updates. Returns > 0 on success or zero on missing room or
1260 * failure. It may return an error in h2c.
1261 */
1262static int h2c_send_conn_wu(struct h2c *h2c)
1263{
1264 int ret = 1;
1265
1266 if (h2c->rcvd_c <= 0)
1267 return 1;
1268
1269 /* send WU for the connection */
1270 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1271 if (ret > 0)
1272 h2c->rcvd_c = 0;
1273
1274 return ret;
1275}
1276
1277/* try to send pending window update for the current dmux stream. It's safe to
1278 * call it with no pending updates. Returns > 0 on success or zero on missing
1279 * room or failure. It may return an error in h2c.
1280 */
1281static int h2c_send_strm_wu(struct h2c *h2c)
1282{
1283 int ret = 1;
1284
1285 if (h2c->rcvd_s <= 0)
1286 return 1;
1287
1288 /* send WU for the stream */
1289 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1290 if (ret > 0)
1291 h2c->rcvd_s = 0;
1292
1293 return ret;
1294}
1295
Willy Tarreaucf68c782017-10-10 17:11:41 +02001296/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1297 * success, 0 on missing data or one of the h2_status values.
1298 */
1299static int h2c_ack_ping(struct h2c *h2c)
1300{
1301 struct buffer *res;
1302 char str[17];
1303 int ret = -1;
1304
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001305 if (b_data(h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001306 return 0;
1307
1308 if (h2c_mux_busy(h2c, NULL)) {
1309 h2c->flags |= H2_CF_DEM_MBUSY;
1310 return 0;
1311 }
1312
Willy Tarreau44e973f2018-03-01 17:49:30 +01001313 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001314 if (!res) {
1315 h2c->flags |= H2_CF_MUX_MALLOC;
1316 h2c->flags |= H2_CF_DEM_MROOM;
1317 return 0;
1318 }
1319
1320 memcpy(str,
1321 "\x00\x00\x08" /* length : 8 (same payload) */
1322 "\x06" "\x01" /* type : 6, flags : ACK */
1323 "\x00\x00\x00\x00" /* stream ID */, 9);
1324
1325 /* copy the original payload */
1326 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1327
1328 ret = bo_istput(res, ist2(str, 17));
1329 if (unlikely(ret <= 0)) {
1330 if (!ret) {
1331 h2c->flags |= H2_CF_MUX_MFULL;
1332 h2c->flags |= H2_CF_DEM_MROOM;
1333 return 0;
1334 }
1335 else {
1336 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1337 return 0;
1338 }
1339 }
1340 return ret;
1341}
1342
Willy Tarreau26f95952017-07-27 17:18:30 +02001343/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1344 * Returns > 0 on success or zero on missing data. It may return an error in
1345 * h2c or h2s. Described in RFC7540#6.9.
1346 */
1347static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1348{
1349 int32_t inc;
1350 int error;
1351
1352 if (h2c->dfl != 4) {
1353 error = H2_ERR_FRAME_SIZE_ERROR;
1354 goto conn_err;
1355 }
1356
1357 /* process full frame only */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001358 if (b_data(h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001359 return 0;
1360
1361 inc = h2_get_n32(h2c->dbuf, 0);
1362
1363 if (h2c->dsi != 0) {
1364 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001365
1366 /* it's not an error to receive WU on a closed stream */
1367 if (h2s->st == H2_SS_CLOSED)
1368 return 1;
1369
1370 if (!inc) {
1371 error = H2_ERR_PROTOCOL_ERROR;
1372 goto strm_err;
1373 }
1374
1375 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1376 error = H2_ERR_FLOW_CONTROL_ERROR;
1377 goto strm_err;
1378 }
1379
1380 h2s->mws += inc;
1381 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1382 h2s->flags &= ~H2_SF_BLK_SFCTL;
1383 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1384 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1385 /* This stream wanted to send but could not due to its
1386 * own flow control. We can put it back into the send
1387 * list now, it will be handled upon next send() call.
1388 */
1389 LIST_ADDQ(&h2c->send_list, &h2s->list);
1390 }
1391 }
1392 }
1393 else {
1394 /* connection window update */
1395 if (!inc) {
1396 error = H2_ERR_PROTOCOL_ERROR;
1397 goto conn_err;
1398 }
1399
1400 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1401 error = H2_ERR_FLOW_CONTROL_ERROR;
1402 goto conn_err;
1403 }
1404
1405 h2c->mws += inc;
1406 }
1407
1408 return 1;
1409
1410 conn_err:
1411 h2c_error(h2c, error);
1412 return 0;
1413
1414 strm_err:
1415 if (h2s) {
1416 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001417 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001418 }
1419 else
1420 h2c_error(h2c, error);
1421 return 0;
1422}
1423
Willy Tarreaue96b0922017-10-30 00:28:29 +01001424/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1425 * the last ID. Returns > 0 on success or zero on missing data. It may return
1426 * an error in h2c. Described in RFC7540#6.8.
1427 */
1428static int h2c_handle_goaway(struct h2c *h2c)
1429{
1430 int error;
1431 int last;
1432
1433 if (h2c->dsi != 0) {
1434 error = H2_ERR_PROTOCOL_ERROR;
1435 goto conn_err;
1436 }
1437
1438 if (h2c->dfl < 8) {
1439 error = H2_ERR_FRAME_SIZE_ERROR;
1440 goto conn_err;
1441 }
1442
1443 /* process full frame only */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001444 if (b_data(h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001445 return 0;
1446
1447 last = h2_get_n32(h2c->dbuf, 0);
1448 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1449 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001450 if (h2c->last_sid < 0)
1451 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001452 return 1;
1453
1454 conn_err:
1455 h2c_error(h2c, error);
1456 return 0;
1457}
1458
Willy Tarreau92153fc2017-12-03 19:46:19 +01001459/* processes a PRIORITY frame, and either skips it or rejects if it is
1460 * invalid. Returns > 0 on success or zero on missing data. It may return
1461 * an error in h2c. Described in RFC7540#6.3.
1462 */
1463static int h2c_handle_priority(struct h2c *h2c)
1464{
1465 int error;
1466
1467 if (h2c->dsi == 0) {
1468 error = H2_ERR_PROTOCOL_ERROR;
1469 goto conn_err;
1470 }
1471
1472 if (h2c->dfl != 5) {
1473 error = H2_ERR_FRAME_SIZE_ERROR;
1474 goto conn_err;
1475 }
1476
1477 /* process full frame only */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001478 if (b_data(h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001479 return 0;
1480
1481 if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) {
1482 /* 7540#5.3 : can't depend on itself */
1483 error = H2_ERR_PROTOCOL_ERROR;
1484 goto conn_err;
1485 }
1486 return 1;
1487
1488 conn_err:
1489 h2c_error(h2c, error);
1490 return 0;
1491}
1492
Willy Tarreaucd234e92017-08-18 10:59:39 +02001493/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1494 * Returns > 0 on success or zero on missing data. It may return an error in
1495 * h2c. Described in RFC7540#6.4.
1496 */
1497static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1498{
1499 int error;
1500
1501 if (h2c->dsi == 0) {
1502 error = H2_ERR_PROTOCOL_ERROR;
1503 goto conn_err;
1504 }
1505
Willy Tarreaucd234e92017-08-18 10:59:39 +02001506 if (h2c->dfl != 4) {
1507 error = H2_ERR_FRAME_SIZE_ERROR;
1508 goto conn_err;
1509 }
1510
1511 /* process full frame only */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001512 if (b_data(h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001513 return 0;
1514
1515 /* late RST, already handled */
1516 if (h2s->st == H2_SS_CLOSED)
1517 return 1;
1518
1519 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001520 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001521
1522 if (h2s->cs) {
Willy Tarreau2153d3c2017-12-15 11:56:29 +01001523 h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001524 /* recv is used to force to detect CS_FL_EOS that wake()
1525 * doesn't handle in the stream-int code.
1526 */
1527 h2s->cs->data_cb->recv(h2s->cs);
1528 h2s->cs->data_cb->wake(h2s->cs);
1529 }
1530
1531 h2s->flags |= H2_SF_RST_RCVD;
1532 return 1;
1533
1534 conn_err:
1535 h2c_error(h2c, error);
1536 return 0;
1537}
1538
Willy Tarreau13278b42017-10-13 19:23:14 +02001539/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1540 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1541 * errors here are reported as connection errors since it's impossible to
1542 * recover from such errors after the compression context has been altered.
1543 */
1544static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1545{
1546 int error;
1547
1548 if (!h2c->dfl) {
1549 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1550 goto strm_err;
1551 }
1552
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001553 if (!b_size(h2c->dbuf))
Willy Tarreau13278b42017-10-13 19:23:14 +02001554 return 0; // empty buffer
1555
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001556 if (b_data(h2c->dbuf) < h2c->dfl && !b_full(h2c->dbuf))
Willy Tarreau13278b42017-10-13 19:23:14 +02001557 return 0; // incomplete frame
1558
Willy Tarreauf2101912018-07-19 10:11:38 +02001559 if (h2c->flags & H2_CF_DEM_TOOMANY)
1560 return 0; // too many cs still present
1561
Willy Tarreau13278b42017-10-13 19:23:14 +02001562 /* now either the frame is complete or the buffer is complete */
1563 if (h2s->st != H2_SS_IDLE) {
1564 /* FIXME: stream already exists, this is only allowed for
1565 * trailers (not supported for now).
1566 */
1567 error = H2_ERR_PROTOCOL_ERROR;
1568 goto conn_err;
1569 }
1570 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1571 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1572 error = H2_ERR_PROTOCOL_ERROR;
1573 goto conn_err;
1574 }
1575
1576 h2s = h2c_stream_new(h2c, h2c->dsi);
1577 if (!h2s) {
1578 error = H2_ERR_INTERNAL_ERROR;
1579 goto conn_err;
1580 }
1581
1582 h2s->st = H2_SS_OPEN;
1583 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1584 h2s->st = H2_SS_HREM;
1585 h2s->flags |= H2_SF_ES_RCVD;
1586 }
1587
1588 /* call the upper layers to process the frame, then let the upper layer
1589 * notify the stream about any change.
1590 */
1591 h2s->cs->data_cb->recv(h2s->cs);
1592
1593 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1594 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1595 error = H2_ERR_INTERNAL_ERROR;
1596 goto conn_err;
1597 }
1598
Willy Tarreau8f650c32017-11-21 19:36:21 +01001599 if (h2c->st0 >= H2_CS_ERROR)
1600 return 0;
1601
Willy Tarreau721c9742017-11-07 11:05:42 +01001602 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001603 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001604 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001605 }
1606 else {
1607 /* update the max stream ID if the request is being processed */
1608 if (h2s->id > h2c->max_id)
1609 h2c->max_id = h2s->id;
1610 }
1611
1612 return 1;
1613
1614 conn_err:
1615 h2c_error(h2c, error);
1616 return 0;
1617
1618 strm_err:
1619 if (h2s) {
1620 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001621 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001622 }
1623 else
1624 h2c_error(h2c, error);
1625 return 0;
1626}
1627
Willy Tarreau454f9052017-10-26 19:40:35 +02001628/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1629 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1630 */
1631static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1632{
1633 int error;
1634
1635 /* note that empty DATA frames are perfectly valid and sometimes used
1636 * to signal an end of stream (with the ES flag).
1637 */
1638
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001639 if (!b_size(h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001640 return 0; // empty buffer
1641
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001642 if (b_data(h2c->dbuf) < h2c->dfl && !b_full(h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001643 return 0; // incomplete frame
1644
1645 /* now either the frame is complete or the buffer is complete */
1646
1647 if (!h2c->dsi) {
1648 /* RFC7540#6.1 */
1649 error = H2_ERR_PROTOCOL_ERROR;
1650 goto conn_err;
1651 }
1652
1653 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1654 /* RFC7540#6.1 */
1655 error = H2_ERR_STREAM_CLOSED;
1656 goto strm_err;
1657 }
1658
Willy Tarreau454f9052017-10-26 19:40:35 +02001659 /* call the upper layers to process the frame, then let the upper layer
1660 * notify the stream about any change.
1661 */
1662 if (!h2s->cs) {
1663 error = H2_ERR_STREAM_CLOSED;
1664 goto strm_err;
1665 }
1666
1667 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001668
Willy Tarreau454f9052017-10-26 19:40:35 +02001669 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1670 /* cs has just been destroyed, we have to kill h2s. */
1671 error = H2_ERR_STREAM_CLOSED;
1672 goto strm_err;
1673 }
1674
Willy Tarreau8f650c32017-11-21 19:36:21 +01001675 if (h2c->st0 >= H2_CS_ERROR)
1676 return 0;
1677
Willy Tarreau721c9742017-11-07 11:05:42 +01001678 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001679 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001680 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001681 }
1682
1683 /* check for completion : the callee will change this to FRAME_A or
1684 * FRAME_H once done.
1685 */
1686 if (h2c->st0 == H2_CS_FRAME_P)
1687 return 0;
1688
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001689
1690 /* last frame */
1691 if (h2c->dff & H2_F_DATA_END_STREAM) {
1692 h2s->st = H2_SS_HREM;
1693 h2s->flags |= H2_SF_ES_RCVD;
1694 }
1695
Willy Tarreau454f9052017-10-26 19:40:35 +02001696 return 1;
1697
1698 conn_err:
1699 h2c_error(h2c, error);
1700 return 0;
1701
1702 strm_err:
1703 if (h2s) {
1704 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001705 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001706 }
1707 else
1708 h2c_error(h2c, error);
1709 return 0;
1710}
1711
Willy Tarreaubc933932017-10-09 16:21:43 +02001712/* process Rx frames to be demultiplexed */
1713static void h2_process_demux(struct h2c *h2c)
1714{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001715 struct h2s *h2s;
1716
Willy Tarreau081d4722017-05-16 21:51:05 +02001717 if (h2c->st0 >= H2_CS_ERROR)
1718 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001719
1720 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1721 if (h2c->st0 == H2_CS_PREFACE) {
1722 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1723 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1724 if (h2c->st0 == H2_CS_ERROR)
1725 h2c->st0 = H2_CS_ERROR2;
1726 goto fail;
1727 }
1728
1729 h2c->max_id = 0;
1730 h2c->st0 = H2_CS_SETTINGS1;
1731 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001732
1733 if (h2c->st0 == H2_CS_SETTINGS1) {
1734 struct h2_fh hdr;
1735
1736 /* ensure that what is pending is a valid SETTINGS frame
1737 * without an ACK.
1738 */
1739 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1740 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1741 if (h2c->st0 == H2_CS_ERROR)
1742 h2c->st0 = H2_CS_ERROR2;
1743 goto fail;
1744 }
1745
1746 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1747 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1748 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1749 h2c->st0 = H2_CS_ERROR2;
1750 goto fail;
1751 }
1752
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001753 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001754 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1755 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1756 h2c->st0 = H2_CS_ERROR2;
1757 goto fail;
1758 }
1759
1760 /* that's OK, switch to FRAME_P to process it */
1761 h2c->dfl = hdr.len;
1762 h2c->dsi = hdr.sid;
1763 h2c->dft = hdr.ft;
1764 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001765 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001766 h2c->st0 = H2_CS_FRAME_P;
1767 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001768 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001769
1770 /* process as many incoming frames as possible below */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001771 while (b_data(h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001772 int ret = 0;
1773
1774 if (h2c->st0 >= H2_CS_ERROR)
1775 break;
1776
1777 if (h2c->st0 == H2_CS_FRAME_H) {
1778 struct h2_fh hdr;
1779
1780 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1781 break;
1782
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001783 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001784 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1785 h2c->st0 = H2_CS_ERROR;
1786 break;
1787 }
1788
1789 h2c->dfl = hdr.len;
1790 h2c->dsi = hdr.sid;
1791 h2c->dft = hdr.ft;
1792 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001793 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001794 h2c->st0 = H2_CS_FRAME_P;
1795 h2_skip_frame_hdr(h2c->dbuf);
1796 }
1797
1798 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001799 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001800
Willy Tarreaud7901432017-12-29 11:34:40 +01001801 if (h2c->st0 == H2_CS_FRAME_E)
1802 goto strm_err;
1803
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001804 if (h2s->st == H2_SS_IDLE &&
1805 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1806 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1807 * this state MUST be treated as a connection error
1808 */
1809 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1810 h2c->st0 = H2_CS_ERROR;
1811 break;
1812 }
1813
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001814 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1815 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1816 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1817 * this state MUST be treated as a stream error
1818 */
1819 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001820 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001821 goto strm_err;
1822 }
1823
Willy Tarreauab837502017-12-27 15:07:30 +01001824 /* Below the management of frames received in closed state is a
1825 * bit hackish because the spec makes strong differences between
1826 * streams closed by receiving RST, sending RST, and seeing ES
1827 * in both directions. In addition to this, the creation of a
1828 * new stream reusing the identifier of a closed one will be
1829 * detected here. Given that we cannot keep track of all closed
1830 * streams forever, we consider that unknown closed streams were
1831 * closed on RST received, which allows us to respond with an
1832 * RST without breaking the connection (eg: to abort a transfer).
1833 * Some frames have to be silently ignored as well.
1834 */
1835 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1836 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1837 /* #5.1.1: The identifier of a newly
1838 * established stream MUST be numerically
1839 * greater than all streams that the initiating
1840 * endpoint has opened or reserved. This
1841 * governs streams that are opened using a
1842 * HEADERS frame and streams that are reserved
1843 * using PUSH_PROMISE. An endpoint that
1844 * receives an unexpected stream identifier
1845 * MUST respond with a connection error.
1846 */
1847 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1848 goto strm_err;
1849 }
1850
1851 if (h2s->flags & H2_SF_RST_RCVD) {
1852 /* RFC7540#5.1:closed: an endpoint that
1853 * receives any frame other than PRIORITY after
1854 * receiving a RST_STREAM MUST treat that as a
1855 * stream error of type STREAM_CLOSED.
1856 *
1857 * Note that old streams fall into this category
1858 * and will lead to an RST being sent.
1859 */
1860 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1861 h2c->st0 = H2_CS_FRAME_E;
1862 goto strm_err;
1863 }
1864
1865 /* RFC7540#5.1:closed: if this state is reached as a
1866 * result of sending a RST_STREAM frame, the peer that
1867 * receives the RST_STREAM might have already sent
1868 * frames on the stream that cannot be withdrawn. An
1869 * endpoint MUST ignore frames that it receives on
1870 * closed streams after it has sent a RST_STREAM
1871 * frame. An endpoint MAY choose to limit the period
1872 * over which it ignores frames and treat frames that
1873 * arrive after this time as being in error.
1874 */
1875 if (!(h2s->flags & H2_SF_RST_SENT)) {
1876 /* RFC7540#5.1:closed: any frame other than
1877 * PRIO/WU/RST in this state MUST be treated as
1878 * a connection error
1879 */
1880 if (h2c->dft != H2_FT_RST_STREAM &&
1881 h2c->dft != H2_FT_PRIORITY &&
1882 h2c->dft != H2_FT_WINDOW_UPDATE) {
1883 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1884 goto strm_err;
1885 }
1886 }
1887 }
1888
Willy Tarreauc0da1962017-10-30 18:38:00 +01001889#if 0
1890 // problem below: it is not possible to completely ignore such
1891 // streams as we need to maintain the compression state as well
1892 // and for this we need to completely process these frames (eg:
1893 // HEADERS frames) as well as counting DATA frames to emit
1894 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1895 // This is a typical case of layer violation where the
1896 // transported contents are critical to the connection's
1897 // validity and must be ignored at the same time :-(
1898
1899 /* graceful shutdown, ignore streams whose ID is higher than
1900 * the one advertised in GOAWAY. RFC7540#6.8.
1901 */
1902 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001903 ret = MIN(b_data(h2c->dbuf), h2c->dfl);
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001904 b_del(h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01001905 h2c->dfl -= ret;
1906 ret = h2c->dfl == 0;
1907 goto strm_err;
1908 }
1909#endif
1910
Willy Tarreau7e98c052017-10-10 15:56:59 +02001911 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001912 case H2_FT_SETTINGS:
1913 if (h2c->st0 == H2_CS_FRAME_P)
1914 ret = h2c_handle_settings(h2c);
1915
1916 if (h2c->st0 == H2_CS_FRAME_A)
1917 ret = h2c_ack_settings(h2c);
1918 break;
1919
Willy Tarreaucf68c782017-10-10 17:11:41 +02001920 case H2_FT_PING:
1921 if (h2c->st0 == H2_CS_FRAME_P)
1922 ret = h2c_handle_ping(h2c);
1923
1924 if (h2c->st0 == H2_CS_FRAME_A)
1925 ret = h2c_ack_ping(h2c);
1926 break;
1927
Willy Tarreau26f95952017-07-27 17:18:30 +02001928 case H2_FT_WINDOW_UPDATE:
1929 if (h2c->st0 == H2_CS_FRAME_P)
1930 ret = h2c_handle_window_update(h2c, h2s);
1931 break;
1932
Willy Tarreau61290ec2017-10-17 08:19:21 +02001933 case H2_FT_CONTINUATION:
1934 /* we currently don't support CONTINUATION frames since
1935 * we have nowhere to store the partial HEADERS frame.
1936 * Let's abort the stream on an INTERNAL_ERROR here.
1937 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001938 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02001939 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001940 h2c->st0 = H2_CS_FRAME_E;
1941 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02001942 break;
1943
Willy Tarreau13278b42017-10-13 19:23:14 +02001944 case H2_FT_HEADERS:
1945 if (h2c->st0 == H2_CS_FRAME_P)
1946 ret = h2c_frt_handle_headers(h2c, h2s);
1947 break;
1948
Willy Tarreau454f9052017-10-26 19:40:35 +02001949 case H2_FT_DATA:
1950 if (h2c->st0 == H2_CS_FRAME_P)
1951 ret = h2c_frt_handle_data(h2c, h2s);
1952
1953 if (h2c->st0 == H2_CS_FRAME_A)
1954 ret = h2c_send_strm_wu(h2c);
1955 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001956
Willy Tarreau92153fc2017-12-03 19:46:19 +01001957 case H2_FT_PRIORITY:
1958 if (h2c->st0 == H2_CS_FRAME_P)
1959 ret = h2c_handle_priority(h2c);
1960 break;
1961
Willy Tarreaucd234e92017-08-18 10:59:39 +02001962 case H2_FT_RST_STREAM:
1963 if (h2c->st0 == H2_CS_FRAME_P)
1964 ret = h2c_handle_rst_stream(h2c, h2s);
1965 break;
1966
Willy Tarreaue96b0922017-10-30 00:28:29 +01001967 case H2_FT_GOAWAY:
1968 if (h2c->st0 == H2_CS_FRAME_P)
1969 ret = h2c_handle_goaway(h2c);
1970 break;
1971
Willy Tarreau1c661982017-10-30 13:52:01 +01001972 case H2_FT_PUSH_PROMISE:
1973 /* not permitted here, RFC7540#5.1 */
1974 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001975 break;
1976
1977 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001978 default:
1979 /* drop frames that we ignore. They may be larger than
1980 * the buffer so we drain all of their contents until
1981 * we reach the end.
1982 */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02001983 ret = MIN(b_data(h2c->dbuf), h2c->dfl);
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001984 b_del(h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001985 h2c->dfl -= ret;
1986 ret = h2c->dfl == 0;
1987 }
1988
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001989 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01001990 /* We may have to send an RST if not done yet */
1991 if (h2s->st == H2_SS_ERROR)
1992 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001993
Willy Tarreaua20a5192017-12-27 11:02:06 +01001994 if (h2c->st0 == H2_CS_FRAME_E)
1995 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001996
Willy Tarreau7e98c052017-10-10 15:56:59 +02001997 /* error or missing data condition met above ? */
1998 if (ret <= 0)
1999 break;
2000
2001 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02002002 b_del(h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002003 h2c->st0 = H2_CS_FRAME_H;
2004 }
2005 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002006
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002007 if (h2c->rcvd_c > 0 &&
2008 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2009 h2c_send_conn_wu(h2c);
2010
Willy Tarreau52eed752017-09-22 15:05:09 +02002011 fail:
2012 /* we can go here on missing data, blocked response or error */
2013 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002014}
2015
2016/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2017 * the end.
2018 */
2019static int h2_process_mux(struct h2c *h2c)
2020{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002021 struct h2s *h2s, *h2s_back;
2022
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002023 /* start by sending possibly pending window updates */
2024 if (h2c->rcvd_c > 0 &&
2025 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2026 h2c_send_conn_wu(h2c) < 0)
2027 goto fail;
2028
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002029 /* First we always process the flow control list because the streams
2030 * waiting there were already elected for immediate emission but were
2031 * blocked just on this.
2032 */
2033
2034 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
2035 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2036 h2c->st0 >= H2_CS_ERROR)
2037 break;
2038
2039 /* In theory it's possible that h2s->cs == NULL here :
2040 * - client sends crap that causes a parse error
2041 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2042 * - RST_STREAM cannot be emitted because mux is busy/full
2043 * - stream gets notified, detaches and quits
2044 * - mux buffer gets ready and wakes pending streams up
2045 * - bam!
2046 */
2047 h2s->flags &= ~H2_SF_BLK_ANY;
2048
2049 if (h2s->cs) {
2050 h2s->cs->data_cb->send(h2s->cs);
2051 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002052 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002053 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002054 }
2055
2056 /* depending on callee's blocking reasons, we may queue in send
2057 * list or completely dequeue.
2058 */
2059 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
2060 if (h2s->flags & H2_SF_BLK_ANY) {
2061 LIST_DEL(&h2s->list);
2062 LIST_ADDQ(&h2c->send_list, &h2s->list);
2063 }
2064 else {
2065 LIST_DEL(&h2s->list);
2066 LIST_INIT(&h2s->list);
2067 if (h2s->cs)
2068 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002069 else {
2070 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002071 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002072 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002073 }
2074 }
2075 }
2076
2077 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
2078 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2079 break;
2080
2081 /* In theory it's possible that h2s->cs == NULL here :
2082 * - client sends crap that causes a parse error
2083 * - RST_STREAM is produced and CS_FL_ERROR at the same time
2084 * - RST_STREAM cannot be emitted because mux is busy/full
2085 * - stream gets notified, detaches and quits
2086 * - mux buffer gets ready and wakes pending streams up
2087 * - bam!
2088 */
2089 h2s->flags &= ~H2_SF_BLK_ANY;
2090
2091 if (h2s->cs) {
2092 h2s->cs->data_cb->send(h2s->cs);
2093 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002094 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01002095 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002096 }
2097 /* depending on callee's blocking reasons, we may queue in fctl
2098 * list or completely dequeue.
2099 */
2100 if (h2s->flags & H2_SF_BLK_MFCTL) {
2101 /* stream hit the connection's flow control */
2102 LIST_DEL(&h2s->list);
2103 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2104 }
2105 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
2106 LIST_DEL(&h2s->list);
2107 LIST_INIT(&h2s->list);
2108 if (h2s->cs)
2109 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002110 else {
2111 /* just sent the last frame for this orphaned stream */
Willy Tarreau71049cc2018-03-28 13:56:39 +02002112 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002113 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002114 }
2115 }
2116
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002117 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002118 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002119 if (h2c->st0 == H2_CS_ERROR) {
2120 if (h2c->max_id >= 0) {
2121 h2c_send_goaway_error(h2c, NULL);
2122 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2123 return 0;
2124 }
2125
2126 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2127 }
2128 return 1;
2129 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002130 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002131}
2132
Willy Tarreau71681172017-10-23 14:39:06 +02002133
Willy Tarreau62f52692017-10-08 23:01:42 +02002134/*********************************************************/
2135/* functions below are I/O callbacks from the connection */
2136/*********************************************************/
2137
2138/* callback called on recv event by the connection handler */
2139static void h2_recv(struct connection *conn)
2140{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002141 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002142 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002143 int max;
2144
Willy Tarreau315d8072017-12-10 22:17:57 +01002145 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002146 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002147
Willy Tarreau44e973f2018-03-01 17:49:30 +01002148 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002149 if (!buf) {
2150 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002151 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002152 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002153
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002154 max = buf->size - b_data(buf);
Willy Tarreau315d8072017-12-10 22:17:57 +01002155 if (max)
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002156 conn->xprt->rcv_buf(conn, buf, max, 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002157
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002158 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002159 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002160 return;
2161 }
2162
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002163 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002164 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002165 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002166}
2167
2168/* callback called on send event by the connection handler */
2169static void h2_send(struct connection *conn)
2170{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002171 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002172 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002173
2174 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002175 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002176
2177 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2178 /* a handshake was requested */
2179 return;
2180 }
2181
Willy Tarreaubc933932017-10-09 16:21:43 +02002182 /* This loop is quite simple : it tries to fill as much as it can from
2183 * pending streams into the existing buffer until it's reportedly full
2184 * or the end of send requests is reached. Then it tries to send this
2185 * buffer's contents out, marks it not full if at least one byte could
2186 * be sent, and tries again.
2187 *
2188 * The snd_buf() function normally takes a "flags" argument which may
2189 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2190 * data immediately comes and CO_SFL_STREAMER to indicate that the
2191 * connection is streaming lots of data (used to increase TLS record
2192 * size at the expense of latency). The former can be sent any time
2193 * there's a buffer full flag, as it indicates at least one stream
2194 * attempted to send and failed so there are pending data. An
2195 * alternative would be to set it as long as there's an active stream
2196 * but that would be problematic for ACKs until we have an absolute
2197 * guarantee that all waiters have at least one byte to send. The
2198 * latter should possibly not be set for now.
2199 */
2200
2201 done = 0;
2202 while (!done) {
2203 unsigned int flags = 0;
2204
2205 /* fill as much as we can into the current buffer */
2206 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2207 done = h2_process_mux(h2c);
2208
2209 if (conn->flags & CO_FL_ERROR)
2210 break;
2211
2212 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2213 flags |= CO_SFL_MSG_MORE;
2214
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002215 if (b_data(h2c->mbuf)) {
2216 int ret = conn->xprt->snd_buf(conn, h2c->mbuf, b_data(h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002217 if (!ret)
2218 break;
2219 b_del(h2c->mbuf, ret);
2220 b_realign_if_empty(h2c->mbuf);
2221 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002222
2223 /* wrote at least one byte, the buffer is not full anymore */
2224 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2225 }
2226
Willy Tarreaua2af5122017-10-09 11:56:46 +02002227 if (conn->flags & CO_FL_SOCK_WR_SH) {
2228 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002229 b_reset(h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002230 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002231}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002232
Willy Tarreau62f52692017-10-08 23:01:42 +02002233/* callback called on any event by the connection handler.
2234 * It applies changes and returns zero, or < 0 if it wants immediate
2235 * destruction of the connection (which normally doesn not happen in h2).
2236 */
2237static int h2_wake(struct connection *conn)
2238{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002239 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002240 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002241
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002242 if (b_data(h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002243 h2_process_demux(h2c);
2244
2245 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002246 b_reset(h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002247
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002248 if (!b_full(h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002249 h2c->flags &= ~H2_CF_DEM_DFULL;
2250 }
2251
Willy Tarreau8ec14062017-12-30 18:08:13 +01002252 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2253 /* frontend is stopping, reload likely in progress, let's try
2254 * to announce a graceful shutdown if not yet done. We don't
2255 * care if it fails, it will be tried again later.
2256 */
2257 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2258 if (h2c->last_sid < 0)
2259 h2c->last_sid = (1U << 31) - 1;
2260 h2c_send_goaway_error(h2c, NULL);
2261 }
2262 }
2263
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002264 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002265 * If we received early data, and the handshake is done, wake
2266 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002267 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002268 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2269 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2270 struct eb32_node *node;
2271 struct h2s *h2s;
2272
2273 h2c->flags |= H2_CF_WAIT_FOR_HS;
2274 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2275
2276 while (node) {
2277 h2s = container_of(node, struct h2s, by_id);
2278 if (h2s->cs->flags & CS_FL_WAIT_FOR_HS)
2279 h2s->cs->data_cb->wake(h2s->cs);
2280 node = eb32_next(node);
2281 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002282 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002283
Willy Tarreau26bd7612017-10-09 16:47:04 +02002284 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002285 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2286 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2287 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002288 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002289
2290 if (eb_is_empty(&h2c->streams_by_id)) {
2291 /* no more stream, kill the connection now */
2292 h2_release(conn);
2293 return -1;
2294 }
2295 else {
2296 /* some streams still there, we need to signal them all and
2297 * wait for their departure.
2298 */
2299 __conn_xprt_stop_recv(conn);
2300 __conn_xprt_stop_send(conn);
2301 return 0;
2302 }
2303 }
2304
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002305 if (!b_data(h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002306 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002307
2308 /* stop being notified of incoming data if we can't process them */
Willy Tarreau315d8072017-12-10 22:17:57 +01002309 if (!h2_recv_allowed(h2c)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002310 __conn_xprt_stop_recv(conn);
2311 }
2312 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002313 __conn_xprt_want_recv(conn);
2314 }
2315
2316 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002317 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2318 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002319 b_data(h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002320 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2321 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002322 __conn_xprt_want_send(conn);
2323 }
2324 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002325 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002326 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002327 }
2328
Willy Tarreau3f133572017-10-31 19:21:06 +01002329 if (h2c->task) {
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002330 if (eb_is_empty(&h2c->streams_by_id) || b_data(h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002331 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002332 task_queue(h2c->task);
2333 }
2334 else
2335 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002336 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002337 return 0;
2338}
2339
Willy Tarreauea392822017-10-31 10:02:25 +01002340/* Connection timeout management. The principle is that if there's no receipt
2341 * nor sending for a certain amount of time, the connection is closed. If the
2342 * MUX buffer still has lying data or is not allocatable, the connection is
2343 * immediately killed. If it's allocatable and empty, we attempt to send a
2344 * GOAWAY frame.
2345 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002346static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002347{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002348 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002349 int expired = tick_is_expired(t->expire, now_ms);
2350
Willy Tarreau0975f112018-03-29 15:22:59 +02002351 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002352 return t;
2353
Willy Tarreau0975f112018-03-29 15:22:59 +02002354 task_delete(t);
2355 task_free(t);
2356
2357 if (!h2c) {
2358 /* resources were already deleted */
2359 return NULL;
2360 }
2361
2362 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002363 h2c_error(h2c, H2_ERR_NO_ERROR);
2364 h2_wake_some_streams(h2c, 0, 0);
2365
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002366 if (b_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002367 /* don't even try to send a GOAWAY, the buffer is stuck */
2368 h2c->flags |= H2_CF_GOAWAY_FAILED;
2369 }
2370
2371 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002372 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002373 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2374 h2c->flags |= H2_CF_GOAWAY_FAILED;
2375
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002376 if (b_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2377 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, b_data(h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002378 if (ret > 0) {
2379 b_del(h2c->mbuf, ret);
2380 b_realign_if_empty(h2c->mbuf);
2381 }
2382 }
Willy Tarreauea392822017-10-31 10:02:25 +01002383
Willy Tarreau0975f112018-03-29 15:22:59 +02002384 /* either we can release everything now or it will be done later once
2385 * the last stream closes.
2386 */
2387 if (eb_is_empty(&h2c->streams_by_id))
2388 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002389
Willy Tarreauea392822017-10-31 10:02:25 +01002390 return NULL;
2391}
2392
2393
Willy Tarreau62f52692017-10-08 23:01:42 +02002394/*******************************************/
2395/* functions below are used by the streams */
2396/*******************************************/
2397
2398/*
2399 * Attach a new stream to a connection
2400 * (Used for outgoing connections)
2401 */
2402static struct conn_stream *h2_attach(struct connection *conn)
2403{
2404 return NULL;
2405}
2406
2407/* callback used to update the mux's polling flags after changing a cs' status.
2408 * The caller (cs_update_mux_polling) will take care of propagating any changes
2409 * to the transport layer.
2410 */
2411static void h2_update_poll(struct conn_stream *cs)
2412{
Willy Tarreau1d393222017-10-17 10:26:19 +02002413 struct h2s *h2s = cs->ctx;
2414
2415 if (!h2s)
2416 return;
2417
Willy Tarreaud7739c82017-10-30 15:38:23 +01002418 /* we may unblock a blocked read */
2419
Willy Tarreau315d8072017-12-10 22:17:57 +01002420 if (cs->flags & CS_FL_DATA_RD_ENA) {
2421 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002422 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002423 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002424 conn_xprt_want_recv(cs->conn);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002425 conn_xprt_want_send(cs->conn);
2426 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002427 }
2428
Willy Tarreau1d393222017-10-17 10:26:19 +02002429 /* Note: the stream and stream-int code doesn't allow us to perform a
2430 * synchronous send() here unfortunately, because this code is called
2431 * as si_update() from the process_stream() context. This means that
2432 * we have to queue the current cs and defer its processing after the
2433 * connection's cs list is processed anyway.
2434 */
2435
2436 if (cs->flags & CS_FL_DATA_WR_ENA) {
2437 if (LIST_ISEMPTY(&h2s->list)) {
2438 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002439 !b_data(h2s->h2c->mbuf) && // not yet subscribed
Willy Tarreau1d393222017-10-17 10:26:19 +02002440 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2441 conn_xprt_want_send(cs->conn);
2442 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2443 }
2444 }
2445 else if (!LIST_ISEMPTY(&h2s->list)) {
2446 LIST_DEL(&h2s->list);
2447 LIST_INIT(&h2s->list);
2448 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2449 }
2450
2451 /* this can happen from within si_chk_snd() */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002452 if (b_data(h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002453 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002454}
2455
2456/*
2457 * Detach the stream from the connection and possibly release the connection.
2458 */
2459static void h2_detach(struct conn_stream *cs)
2460{
Willy Tarreau60935142017-10-16 18:11:19 +02002461 struct h2s *h2s = cs->ctx;
2462 struct h2c *h2c;
2463
2464 cs->ctx = NULL;
2465 if (!h2s)
2466 return;
2467
2468 h2c = h2s->h2c;
2469 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002470 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002471 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2472 !h2_has_too_many_cs(h2c)) {
2473 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2474 if (h2_recv_allowed(h2c)) {
2475 __conn_xprt_want_recv(h2c->conn);
2476 conn_xprt_want_send(h2c->conn);
2477 }
2478 }
Willy Tarreau60935142017-10-16 18:11:19 +02002479
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002480 /* this stream may be blocked waiting for some data to leave (possibly
2481 * an ES or RST frame), so orphan it in this case.
2482 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002483 if (!(cs->conn->flags & CO_FL_ERROR) &&
2484 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002485 return;
2486
Willy Tarreau45f752e2017-10-30 15:44:59 +01002487 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2488 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2489 /* unblock the connection if it was blocked on this
2490 * stream.
2491 */
2492 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2493 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2494 conn_xprt_want_recv(cs->conn);
2495 conn_xprt_want_send(cs->conn);
2496 }
2497
Willy Tarreau71049cc2018-03-28 13:56:39 +02002498 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002499
Willy Tarreaue323f342018-03-28 13:51:45 +02002500 /* We don't want to close right now unless we're removing the
2501 * last stream, and either the connection is in error, or it
2502 * reached the ID already specified in a GOAWAY frame received
2503 * or sent (as seen by last_sid >= 0).
2504 */
2505 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2506 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002507 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002508 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002509 (!b_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002510 (conn_xprt_read0_pending(h2c->conn) ||
2511 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2512 /* no more stream will come, kill it now */
2513 h2_release(h2c->conn);
2514 }
2515 else if (h2c->task) {
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002516 if (eb_is_empty(&h2c->streams_by_id) || b_data(h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002517 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2518 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002519 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002520 else
2521 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002522 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002523}
2524
2525static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2526{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002527 struct h2s *h2s = cs->ctx;
2528
2529 if (!mode)
2530 return;
2531
Willy Tarreau721c9742017-11-07 11:05:42 +01002532 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002533 return;
2534
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002535 /* if no outgoing data was seen on this stream, it means it was
2536 * closed with a "tcp-request content" rule that is normally
2537 * used to kill the connection ASAP (eg: limit abuse). In this
2538 * case we send a goaway to close the connection.
2539 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002540 if (!(h2s->flags & H2_SF_RST_SENT) &&
2541 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002542 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002543
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002544 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2545 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2546 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002547 goto add_to_list;
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002548
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002549 if (b_data(h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002550 conn_xprt_want_send(cs->conn);
2551
Willy Tarreau00dd0782018-03-01 16:31:34 +01002552 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002553
2554 add_to_list:
2555 if (LIST_ISEMPTY(&h2s->list)) {
2556 if (h2s->flags & H2_SF_BLK_MFCTL)
2557 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2558 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2559 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2560 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002561}
2562
2563static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2564{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002565 struct h2s *h2s = cs->ctx;
2566
Willy Tarreau721c9742017-11-07 11:05:42 +01002567 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002568 return;
2569
Willy Tarreau67434202017-11-06 20:20:51 +01002570 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002571 /* we can cleanly close using an empty data frame only after headers */
2572
2573 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2574 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002575 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002576
2577 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002578 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002579 else
2580 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002581 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002582 /* if no outgoing data was seen on this stream, it means it was
2583 * closed with a "tcp-request content" rule that is normally
2584 * used to kill the connection ASAP (eg: limit abuse). In this
2585 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002586 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002587 if (!(h2s->flags & H2_SF_RST_SENT) &&
2588 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002589 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002590
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002591 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2592 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002593 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002594 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002595
Willy Tarreau00dd0782018-03-01 16:31:34 +01002596 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002597 }
2598
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002599 if (b_data(h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002600 conn_xprt_want_send(cs->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002601
2602 add_to_list:
2603 if (LIST_ISEMPTY(&h2s->list)) {
2604 if (h2s->flags & H2_SF_BLK_MFCTL)
2605 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
2606 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
2607 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2608 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002609}
2610
Willy Tarreau13278b42017-10-13 19:23:14 +02002611/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2612 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2613 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002614 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002615 */
Willy Tarreau337ea572018-06-19 06:23:38 +02002616static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count, int flags)
Willy Tarreau13278b42017-10-13 19:23:14 +02002617{
2618 struct h2c *h2c = h2s->h2c;
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002619 const uint8_t *hdrs = (uint8_t *)b_head(h2c->dbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002620 struct chunk *tmp = get_trash_chunk();
2621 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002622 struct chunk *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002623 unsigned int msgf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002624 int flen = h2c->dfl;
2625 int outlen = 0;
2626 int wrap;
2627 int try;
2628
2629 if (!h2c->dfl) {
2630 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002631 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002632 return 0;
2633 }
2634
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002635 if (b_data(h2c->dbuf) < h2c->dfl && !b_full(h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002636 return 0; // incomplete input frame
2637
Willy Tarreau13278b42017-10-13 19:23:14 +02002638 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002639 wrap = b_wrap(h2c->dbuf) - b_head(h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002640 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002641 copy = alloc_trash_chunk();
2642 if (!copy) {
2643 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2644 goto fail;
2645 }
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002646 memcpy(copy->str, b_head(h2c->dbuf), wrap);
2647 memcpy(copy->str + wrap, b_orig(h2c->dbuf), h2c->dfl - wrap);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002648 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002649 }
2650
2651 /* The padlen is the first byte before data, and the padding appears
2652 * after data. padlen+data+padding are included in flen.
2653 */
2654 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002655 h2c->dpl = *hdrs;
2656 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002657 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2658 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002659 return 0;
2660 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002661 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002662 hdrs += 1; // skip Pad Length
2663 }
2664
2665 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2666 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002667 if (read_n32(hdrs) == h2s->id) {
2668 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2669 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2670 return 0;//goto fail_stream;
2671 }
2672
Willy Tarreau13278b42017-10-13 19:23:14 +02002673 hdrs += 5; // stream dep = 4, weight = 1
2674 flen -= 5;
2675 }
2676
2677 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2678 * don't support this for now and can't even decompress so we have to
2679 * break the connection.
2680 */
2681 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2682 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002683 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002684 }
2685
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002686 /* we can't retry a failed decompression operation so we must be very
2687 * careful not to take any risks. In practice the output buffer is
2688 * always empty except maybe for trailers, so these operations almost
2689 * never happen.
2690 */
Willy Tarreau337ea572018-06-19 06:23:38 +02002691 if (flags & CO_RFL_BUF_WET) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002692 /* need to let the output buffer flush and
2693 * mark the buffer for later wake up.
2694 */
2695 goto fail;
2696 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002697
Willy Tarreauaa7af722018-07-12 10:33:12 +02002698 if (unlikely(b_space_wraps(buf))) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002699 /* it doesn't fit and the buffer is fragmented,
2700 * so let's defragment it and try again.
2701 */
Willy Tarreau0db4d102018-07-12 11:00:01 +02002702 b_slow_realign(buf, trash.str, 0);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002703 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002704
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002705 try = b_contig_space(buf);
2706 if (!try)
2707 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002708
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002709 if (try > count)
2710 try = count;
2711
2712 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2713 sizeof(list)/sizeof(list[0]), tmp);
2714 if (outlen < 0) {
2715 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2716 goto fail;
2717 }
2718
2719 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002720 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau8f9c72d2018-06-07 18:46:28 +02002721 outlen = h2_make_h1_request(list, b_tail(buf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002722
2723 if (outlen < 0) {
2724 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2725 goto fail;
2726 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002727
Willy Tarreau174b06a2018-04-25 18:13:58 +02002728 if (msgf & H2_MSGF_BODY) {
2729 /* a payload is present */
2730 if (msgf & H2_MSGF_BODY_CL)
2731 h2s->flags |= H2_SF_DATA_CLEN;
2732 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2733 h2s->flags |= H2_SF_DATA_CHNK;
2734 }
2735
Willy Tarreau13278b42017-10-13 19:23:14 +02002736 /* now consume the input data */
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02002737 b_del(h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002738 h2c->st0 = H2_CS_FRAME_H;
Olivier Houchardacd14032018-06-28 18:17:23 +02002739 b_add(buf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002740
2741 /* don't send it before returning data!
2742 * FIXME: should we instead try to send it much later, after the
2743 * response ? This would require that we keep a copy of it in h2s.
2744 */
2745 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2746 h2s->cs->flags |= CS_FL_EOS;
2747 h2s->flags |= H2_SF_ES_RCVD;
2748 }
2749
Willy Tarreau68dd9852017-07-03 14:44:26 +02002750 leave:
2751 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002752 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002753 fail:
2754 outlen = 0;
2755 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002756}
2757
Willy Tarreau454f9052017-10-26 19:40:35 +02002758/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2759 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2760 * in use, a new chunk is emitted for each frame. This is supposed to fit
2761 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2762 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2763 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2764 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002765 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2766 * checked to know if some data remain pending (an empty DATA frame can return
2767 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2768 * connection errors in h2c->errcode. The caller must already have checked the
2769 * frame header and ensured that the frame was complete or the buffer full. It
2770 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002771 */
Willy Tarreau337ea572018-06-19 06:23:38 +02002772static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count, int flags)
Willy Tarreau454f9052017-10-26 19:40:35 +02002773{
2774 struct h2c *h2c = h2s->h2c;
2775 int block1, block2;
2776 unsigned int flen = h2c->dfl;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002777 unsigned int chklen = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002778
Willy Tarreauc9ede6c2017-12-10 21:28:43 +01002779 h2s->cs->flags &= ~CS_FL_RCV_MORE;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002780 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002781
2782 /* The padlen is the first byte before data, and the padding appears
2783 * after data. padlen+data+padding are included in flen.
2784 */
Willy Tarreau79127812017-12-03 21:06:59 +01002785 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002786 if (b_data(h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002787 return 0;
2788
Willy Tarreaudda2e412018-06-07 18:08:04 +02002789 h2c->dpl = *(uint8_t *)b_head(h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002790 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002791 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2792 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002793 return 0;
2794 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002795
2796 /* skip the padlen byte */
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02002797 b_del(h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002798 h2c->dfl--;
2799 h2c->rcvd_c++; h2c->rcvd_s++;
2800 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002801 }
2802
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002803 flen = h2c->dfl - h2c->dpl;
2804 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002805 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002806
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002807 if (flen > b_data(h2c->dbuf)) {
2808 flen = b_data(h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002809 if (!flen)
2810 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002811 }
2812
Willy Tarreaueba10f22018-04-25 20:44:22 +02002813 /* chunked-encoding requires more room */
2814 if (h2s->flags & H2_SF_DATA_CHNK) {
2815 chklen = MIN(flen, count);
2816 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2817 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2818 (chklen < 1048576) ? 4 : 8;
2819 chklen += 4; // CRLF, CRLF
2820 }
2821
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002822 /* does it fit in output buffer or should we wait ? */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002823 if (flen + chklen > count) {
2824 if (chklen >= count)
2825 goto full;
2826 flen = count - chklen;
2827 }
2828
2829 if (h2s->flags & H2_SF_DATA_CHNK) {
2830 /* emit the chunk size */
2831 unsigned int chksz = flen;
2832 char str[10];
2833 char *beg;
2834
2835 beg = str + sizeof(str);
2836 *--beg = '\n';
2837 *--beg = '\r';
2838 do {
2839 *--beg = hextab[chksz & 0xF];
2840 } while (chksz >>= 4);
2841 bi_putblk(buf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002842 }
2843
Willy Tarreau454f9052017-10-26 19:40:35 +02002844 /* Block1 is the length of the first block before the buffer wraps,
2845 * block2 is the optional second block to reach the end of the frame.
2846 */
Willy Tarreau7194d3c2018-06-06 16:55:45 +02002847 block1 = b_contig_data(h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002848 if (block1 > flen)
2849 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002850 block2 = flen - block1;
2851
2852 if (block1)
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02002853 bi_putblk(buf, b_head(h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02002854
2855 if (block2)
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02002856 bi_putblk(buf, b_peek(h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02002857
Willy Tarreaueba10f22018-04-25 20:44:22 +02002858 if (h2s->flags & H2_SF_DATA_CHNK) {
2859 /* emit the CRLF */
2860 bi_putblk(buf, "\r\n", 2);
2861 }
2862
Willy Tarreau454f9052017-10-26 19:40:35 +02002863 /* now mark the input data as consumed (will be deleted from the buffer
2864 * by the caller when seeing FRAME_A after sending the window update).
2865 */
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02002866 b_del(h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002867 h2c->dfl -= flen;
2868 h2c->rcvd_c += flen;
2869 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
2870
2871 if (h2c->dfl > h2c->dpl) {
2872 /* more data available, transfer stalled on stream full */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002873 goto more;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002874 }
2875
Willy Tarreau4a28da12018-01-04 14:41:00 +01002876 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002877 /* here we're done with the frame, all the payload (except padding) was
2878 * transferred.
2879 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02002880
2881 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
2882 /* emit the trailing 0 CRLF CRLF */
2883 if (count < 5)
2884 goto more;
2885 chklen += 5;
2886 bi_putblk(buf, "0\r\n\r\n", 5);
2887 }
2888
Willy Tarreaud1023bb2018-03-22 16:53:12 +01002889 h2c->rcvd_c += h2c->dpl;
2890 h2c->rcvd_s += h2c->dpl;
2891 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02002892 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2893
2894 /* don't send it before returning data!
2895 * FIXME: should we instead try to send it much later, after the
2896 * response ? This would require that we keep a copy of it in h2s.
2897 */
Willy Tarreau79127812017-12-03 21:06:59 +01002898 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002899 h2s->cs->flags |= CS_FL_EOS;
2900 h2s->flags |= H2_SF_ES_RCVD;
2901 }
2902
Willy Tarreaueba10f22018-04-25 20:44:22 +02002903 return flen + chklen;
2904 full:
2905 flen = chklen = 0;
2906 more:
2907 h2c->flags |= H2_CF_DEM_SFULL;
2908 h2s->cs->flags |= CS_FL_RCV_MORE;
2909 return flen + chklen;
Willy Tarreau454f9052017-10-26 19:40:35 +02002910}
2911
Willy Tarreau62f52692017-10-08 23:01:42 +02002912/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002913 * Called from the upper layer to get more data, up to <count> bytes. The
2914 * caller is responsible for never asking for more data than what is available
2915 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002916 */
Willy Tarreau7f3225f2018-06-19 06:15:17 +02002917static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02002918{
Willy Tarreau13278b42017-10-13 19:23:14 +02002919 struct h2s *h2s = cs->ctx;
2920 struct h2c *h2c = h2s->h2c;
Willy Tarreaud9cf5402018-07-18 11:29:06 +02002921 size_t ret = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002922
2923 if (h2c->st0 != H2_CS_FRAME_P)
2924 return 0; // no pre-parsed frame yet
2925
2926 if (h2c->dsi != h2s->id)
2927 return 0; // not for us
2928
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002929 if (!b_size(h2c->dbuf))
Willy Tarreau13278b42017-10-13 19:23:14 +02002930 return 0; // empty buffer
2931
Willy Tarreau13278b42017-10-13 19:23:14 +02002932 switch (h2c->dft) {
2933 case H2_FT_HEADERS:
Willy Tarreau337ea572018-06-19 06:23:38 +02002934 ret = h2_frt_decode_headers(h2s, buf, count, flags);
Willy Tarreau13278b42017-10-13 19:23:14 +02002935 break;
2936
Willy Tarreau454f9052017-10-26 19:40:35 +02002937 case H2_FT_DATA:
Willy Tarreau337ea572018-06-19 06:23:38 +02002938 ret = h2_frt_transfer_data(h2s, buf, count, flags);
Willy Tarreau454f9052017-10-26 19:40:35 +02002939 break;
2940
Willy Tarreau13278b42017-10-13 19:23:14 +02002941 default:
2942 ret = 0;
2943 }
2944 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002945}
2946
Willy Tarreau5dd17352018-06-14 13:33:30 +02002947/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
2948 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
2949 * number of bytes sent. The caller must check the stream's status to detect
2950 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002951 */
Willy Tarreau206ba832018-06-14 15:27:31 +02002952static size_t h2s_frt_make_resp_headers(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002953{
2954 struct http_hdr list[MAX_HTTP_HDR];
2955 struct h2c *h2c = h2s->h2c;
2956 struct h1m *h1m = &h2s->res;
2957 struct chunk outbuf;
2958 int es_now = 0;
2959 int ret = 0;
2960 int hdr;
2961
2962 if (h2c_mux_busy(h2c, h2s)) {
2963 h2s->flags |= H2_SF_BLK_MBUSY;
2964 return 0;
2965 }
2966
Willy Tarreau44e973f2018-03-01 17:49:30 +01002967 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002968 h2c->flags |= H2_CF_MUX_MALLOC;
2969 h2s->flags |= H2_SF_BLK_MROOM;
2970 return 0;
2971 }
2972
2973 /* First, try to parse the H1 response and index it into <list>.
2974 * NOTE! Since it comes from haproxy, we *know* that a response header
2975 * block does not wrap and we can safely read it this way without
2976 * having to realign the buffer.
2977 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02002978 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002979 list, sizeof(list)/sizeof(list[0]), h1m);
2980 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002981 /* incomplete or invalid response, this is abnormal coming from
2982 * haproxy and may only result in a bad errorfile or bad Lua code
2983 * so that won't be fixed, raise an error now.
2984 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002985 * FIXME: we should instead add the ability to only return a
2986 * 502 bad gateway. But in theory this is not supposed to
2987 * happen.
2988 */
2989 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2990 ret = 0;
2991 goto end;
2992 }
2993
2994 chunk_reset(&outbuf);
2995
2996 while (1) {
Willy Tarreau41e38ac2018-06-07 18:22:26 +02002997 outbuf.str = b_tail(h2c->mbuf);
Willy Tarreaue4d5a032018-06-07 18:58:07 +02002998 outbuf.size = b_contig_space(h2c->mbuf);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002999 outbuf.len = 0;
3000
Willy Tarreauaa7af722018-07-12 10:33:12 +02003001 if (outbuf.size >= 9 || !b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003002 break;
3003 realign_again:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003004 b_slow_realign(h2c->mbuf, trash.str, b_data(h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003005 }
3006
3007 if (outbuf.size < 9) {
3008 h2c->flags |= H2_CF_MUX_MFULL;
3009 h2s->flags |= H2_SF_BLK_MROOM;
3010 ret = 0;
3011 goto end;
3012 }
3013
3014 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3015 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
3016 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3017 outbuf.len = 9;
3018
3019 /* encode status, which necessarily is the first one */
3020 if (outbuf.len < outbuf.size && h1m->status == 200)
3021 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
3022 else if (outbuf.len < outbuf.size && h1m->status == 304)
3023 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003024 else if (unlikely(list[0].v.len != 3)) {
3025 /* this is an unparsable response */
3026 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3027 ret = 0;
3028 goto end;
3029 }
3030 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003031 /* basic encoding of the status code */
3032 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
3033 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
3034 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
3035 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
3036 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
3037 }
3038 else {
Willy Tarreauaa7af722018-07-12 10:33:12 +02003039 if (b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003040 goto realign_again;
3041
3042 h2c->flags |= H2_CF_MUX_MFULL;
3043 h2s->flags |= H2_SF_BLK_MROOM;
3044 ret = 0;
3045 goto end;
3046 }
3047
3048 /* encode all headers, stop at empty name */
3049 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003050 /* these ones do not exist in H2 and must be dropped. */
3051 if (isteq(list[hdr].n, ist("connection")) ||
3052 isteq(list[hdr].n, ist("proxy-connection")) ||
3053 isteq(list[hdr].n, ist("keep-alive")) ||
3054 isteq(list[hdr].n, ist("upgrade")) ||
3055 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003056 continue;
3057
3058 if (isteq(list[hdr].n, ist("")))
3059 break; // end
3060
3061 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3062 /* output full */
Willy Tarreauaa7af722018-07-12 10:33:12 +02003063 if (b_space_wraps(h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003064 goto realign_again;
3065
3066 h2c->flags |= H2_CF_MUX_MFULL;
3067 h2s->flags |= H2_SF_BLK_MROOM;
3068 ret = 0;
3069 goto end;
3070 }
3071 }
3072
3073 /* we may need to add END_STREAM */
3074 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3075 es_now = 1;
3076
3077 /* update the frame's size */
3078 h2_set_frame_size(outbuf.str, outbuf.len - 9);
3079
3080 if (es_now)
3081 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
3082
3083 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003084 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003085
3086 /* commit the H2 response */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003087 b_add(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01003088 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003089
3090 /* for now we don't implemented CONTINUATION, so we wait for a
3091 * body or directly end in TRL2.
3092 */
3093 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003094 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003095 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003096
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003097 h1m->state = HTTP_MSG_DONE;
3098 h2s->flags |= H2_SF_ES_SENT;
3099 if (h2s->st == H2_SS_OPEN)
3100 h2s->st = H2_SS_HLOC;
3101 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003102 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003103 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01003104 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003105 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01003106 h1m->state = HTTP_MSG_RPBEFORE;
3107 h1m->status = 0;
3108 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01003109 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003110 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003111 else
Willy Tarreau13e4e942017-12-14 10:55:21 +01003112 h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003113
3114 end:
3115 //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));
3116 return ret;
3117}
3118
Willy Tarreau5dd17352018-06-14 13:33:30 +02003119/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3120 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3121 * the number of bytes sent. The caller must check the stream's status to
3122 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003123 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003124static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003125{
3126 struct h2c *h2c = h2s->h2c;
3127 struct h1m *h1m = &h2s->res;
3128 struct chunk outbuf;
3129 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003130 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003131 int es_now = 0;
3132 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003133 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003134 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003135
3136 if (h2c_mux_busy(h2c, h2s)) {
3137 h2s->flags |= H2_SF_BLK_MBUSY;
3138 goto end;
3139 }
3140
Willy Tarreau44e973f2018-03-01 17:49:30 +01003141 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003142 h2c->flags |= H2_CF_MUX_MALLOC;
3143 h2s->flags |= H2_SF_BLK_MROOM;
3144 goto end;
3145 }
3146
3147 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003148 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003149 goto end;
3150
3151 chunk_reset(&outbuf);
3152
3153 while (1) {
Willy Tarreau41e38ac2018-06-07 18:22:26 +02003154 outbuf.str = b_tail(h2c->mbuf);
Willy Tarreaue4d5a032018-06-07 18:58:07 +02003155 outbuf.size = b_contig_space(h2c->mbuf);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003156 outbuf.len = 0;
3157
Willy Tarreauaa7af722018-07-12 10:33:12 +02003158 if (outbuf.size >= 9 || !b_space_wraps(h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003159 break;
3160 realign_again:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003161 b_slow_realign(h2c->mbuf, trash.str, b_data(h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003162 }
3163
3164 if (outbuf.size < 9) {
3165 h2c->flags |= H2_CF_MUX_MFULL;
3166 h2s->flags |= H2_SF_BLK_MROOM;
3167 goto end;
3168 }
3169
3170 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
3171 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
3172 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
3173 outbuf.len = 9;
3174
3175 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3176 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003177 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003178 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003179 break;
3180 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003181 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003182 if ((long long)size > h1m->curr_len)
3183 size = h1m->curr_len;
3184 break;
3185 default: /* te:chunked : parse chunks */
3186 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003187 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003188 if (!ret)
3189 goto end;
3190
3191 if (ret < 0) {
3192 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3193 h1m->err_pos = ret;
3194 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3195 goto end;
3196 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003197 max -= ret;
3198 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003199 total += ret;
3200 h1m->state = HTTP_MSG_CHUNK_SIZE;
3201 }
3202
3203 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
3204 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003205 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003206 if (!ret)
3207 goto end;
3208
3209 if (ret < 0) {
3210 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
3211 h1m->err_pos = ret;
3212 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3213 goto end;
3214 }
3215
3216 size = chunk;
3217 h1m->curr_len = chunk;
3218 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003219 max -= ret;
3220 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003221 total += ret;
3222 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
3223 if (!size)
3224 goto send_empty;
3225 }
3226
3227 /* in MSG_DATA state, continue below */
3228 size = h1m->curr_len;
3229 break;
3230 }
3231
3232 /* we have in <size> the exact number of bytes we need to copy from
3233 * the H1 buffer. We need to check this against the connection's and
3234 * the stream's send windows, and to ensure that this fits in the max
3235 * frame size and in the buffer's available space minus 9 bytes (for
3236 * the frame header). The connection's flow control is applied last so
3237 * that we can use a separate list of streams which are immediately
3238 * unblocked on window opening. Note: we don't implement padding.
3239 */
3240
Willy Tarreau5dd17352018-06-14 13:33:30 +02003241 if (size > max)
3242 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003243
3244 if (size > h2s->mws)
3245 size = h2s->mws;
3246
3247 if (size <= 0) {
3248 h2s->flags |= H2_SF_BLK_SFCTL;
3249 goto end;
3250 }
3251
3252 if (h2c->mfs && size > h2c->mfs)
3253 size = h2c->mfs;
3254
3255 if (size + 9 > outbuf.size) {
3256 /* we have an opportunity for enlarging the too small
3257 * available space, let's try.
3258 */
Willy Tarreauaa7af722018-07-12 10:33:12 +02003259 if (b_space_wraps(h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003260 goto realign_again;
3261 size = outbuf.size - 9;
3262 }
3263
3264 if (size <= 0) {
3265 h2c->flags |= H2_CF_MUX_MFULL;
3266 h2s->flags |= H2_SF_BLK_MROOM;
3267 goto end;
3268 }
3269
3270 if (size > h2c->mws)
3271 size = h2c->mws;
3272
3273 if (size <= 0) {
3274 h2s->flags |= H2_SF_BLK_MFCTL;
3275 goto end;
3276 }
3277
3278 /* copy whatever we can */
3279 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003280 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003281 if (ret == 1)
3282 len2 = 0;
3283
3284 if (!ret || len1 + len2 < size) {
3285 /* FIXME: must normally never happen */
3286 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3287 goto end;
3288 }
3289
3290 /* limit len1/len2 to size */
3291 if (len1 + len2 > size) {
3292 int sub = len1 + len2 - size;
3293
3294 if (len2 > sub)
3295 len2 -= sub;
3296 else {
3297 sub -= len2;
3298 len2 = 0;
3299 len1 -= sub;
3300 }
3301 }
3302
3303 /* now let's copy this this into the output buffer */
3304 memcpy(outbuf.str + 9, blk1, len1);
3305 if (len2)
3306 memcpy(outbuf.str + 9 + len1, blk2, len2);
3307
3308 send_empty:
3309 /* we may need to add END_STREAM */
3310 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3311 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003312 *
3313 * FIXME: what we do here is not correct because we send end_stream
3314 * before knowing if we'll have to send a HEADERS frame for the
3315 * trailers. More importantly we're not consuming the trailing CRLF
3316 * after the end of trailers, so it will be left to the caller to
3317 * eat it. The right way to do it would be to measure trailers here
3318 * and to send ES only if there are no trailers.
3319 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003320 */
3321 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3322 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3323 es_now = 1;
3324
3325 /* update the frame's size */
3326 h2_set_frame_size(outbuf.str, size);
3327
3328 if (es_now)
3329 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3330
3331 /* commit the H2 response */
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003332 b_add(h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003333
3334 /* consume incoming H1 response */
3335 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003336 max -= size;
3337 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003338 total += size;
3339 h1m->curr_len -= size;
3340 h2s->mws -= size;
3341 h2c->mws -= size;
3342
3343 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3344 h1m->state = HTTP_MSG_CHUNK_CRLF;
3345 goto new_frame;
3346 }
3347 }
3348
3349 if (es_now) {
3350 if (h2s->st == H2_SS_OPEN)
3351 h2s->st = H2_SS_HLOC;
3352 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003353 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003354
Willy Tarreau35a62702018-02-27 15:37:25 +01003355 if (!(h1m->flags & H1_MF_CHNK)) {
3356 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003357 total += max;
3358 ofs += max;
3359 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003360
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003361 h1m->state = HTTP_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003362 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003363
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003364 h2s->flags |= H2_SF_ES_SENT;
3365 }
3366
3367 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003368 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003369 return total;
3370}
3371
Willy Tarreau62f52692017-10-08 23:01:42 +02003372/* Called from the upper layer, to send data */
Willy Tarreaudeccd112018-06-14 18:38:55 +02003373static size_t h2_snd_buf(struct conn_stream *cs, const struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003374{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003375 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003376 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003377 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003378
Willy Tarreau0bad0432018-06-14 16:54:01 +02003379 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003380 h2s->flags |= H2_SF_OUTGOING_DATA;
3381
Willy Tarreau0bad0432018-06-14 16:54:01 +02003382 while (h2s->res.state < HTTP_MSG_DONE && count) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003383 if (h2s->res.state < HTTP_MSG_BODY) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003384 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003385 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003386 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003387 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003388 }
3389 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3390 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003391 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003392
Willy Tarreau5dd17352018-06-14 13:33:30 +02003393 if (unlikely((int)ret <= 0)) {
3394 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003395 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3396 break;
3397 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003398 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003399 total += count;
3400 count = 0;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003401 h2s->res.state = HTTP_MSG_DONE;
3402 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003403 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003404 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003405 cs->flags |= CS_FL_ERROR;
3406 break;
3407 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003408
3409 total += ret;
3410 count -= ret;
3411
3412 if (h2s->st >= H2_SS_ERROR)
3413 break;
3414
3415 if (h2s->flags & H2_SF_BLK_ANY)
3416 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003417 }
3418
Willy Tarreau00610962018-07-19 10:58:28 +02003419 if (h2s->st >= H2_SS_ERROR) {
3420 /* trim any possibly pending data after we close (extra CR-LF,
3421 * unprocessed trailers, abnormal extra data, ...)
3422 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003423 total += count;
3424 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003425 }
3426
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003427 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003428 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003429 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003430 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003431 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003432 }
3433
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003434 if (h2s->flags & H2_SF_BLK_SFCTL) {
3435 /* stream flow control, quit the list */
3436 LIST_DEL(&h2s->list);
3437 LIST_INIT(&h2s->list);
3438 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003439 else if (LIST_ISEMPTY(&h2s->list)) {
3440 if (h2s->flags & H2_SF_BLK_MFCTL)
3441 LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list);
3442 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
3443 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
3444 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003445
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003446 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003447}
3448
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003449/* for debugging with CLI's "show fd" command */
3450static void h2_show_fd(struct chunk *msg, struct connection *conn)
3451{
3452 struct h2c *h2c = conn->mux_ctx;
3453 struct h2s *h2s;
3454 struct eb32_node *node;
3455 int fctl_cnt = 0;
3456 int send_cnt = 0;
3457 int tree_cnt = 0;
3458 int orph_cnt = 0;
3459
3460 if (!h2c)
3461 return;
3462
3463 list_for_each_entry(h2s, &h2c->fctl_list, list)
3464 fctl_cnt++;
3465
3466 list_for_each_entry(h2s, &h2c->send_list, list)
3467 send_cnt++;
3468
3469 node = eb32_first(&h2c->streams_by_id);
3470 while (node) {
3471 h2s = container_of(node, struct h2s, by_id);
3472 tree_cnt++;
3473 if (!h2s->cs)
3474 orph_cnt++;
3475 node = eb32_next(node);
3476 }
3477
Willy Tarreauc65edac2018-07-19 10:54:43 +02003478 chunk_appendf(msg, " st0=%d flg=0x%08x nbst=%u nbcs=%u fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003479 h2c->st0, h2c->flags, h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt, (unsigned int)b_data(h2c->dbuf), (unsigned int)b_size(h2c->dbuf), (unsigned int)b_data(h2c->mbuf), (unsigned int)b_size(h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003480}
Willy Tarreau62f52692017-10-08 23:01:42 +02003481
3482/*******************************************************/
3483/* functions below are dedicated to the config parsers */
3484/*******************************************************/
3485
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003486/* config parser for global "tune.h2.header-table-size" */
3487static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3488 struct proxy *defpx, const char *file, int line,
3489 char **err)
3490{
3491 if (too_many_args(1, args, err, NULL))
3492 return -1;
3493
3494 h2_settings_header_table_size = atoi(args[1]);
3495 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3496 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3497 return -1;
3498 }
3499 return 0;
3500}
Willy Tarreau62f52692017-10-08 23:01:42 +02003501
Willy Tarreaue6baec02017-07-27 11:45:11 +02003502/* config parser for global "tune.h2.initial-window-size" */
3503static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3504 struct proxy *defpx, const char *file, int line,
3505 char **err)
3506{
3507 if (too_many_args(1, args, err, NULL))
3508 return -1;
3509
3510 h2_settings_initial_window_size = atoi(args[1]);
3511 if (h2_settings_initial_window_size < 0) {
3512 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3513 return -1;
3514 }
3515 return 0;
3516}
3517
Willy Tarreau5242ef82017-07-27 11:47:28 +02003518/* config parser for global "tune.h2.max-concurrent-streams" */
3519static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3520 struct proxy *defpx, const char *file, int line,
3521 char **err)
3522{
3523 if (too_many_args(1, args, err, NULL))
3524 return -1;
3525
3526 h2_settings_max_concurrent_streams = atoi(args[1]);
3527 if (h2_settings_max_concurrent_streams < 0) {
3528 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3529 return -1;
3530 }
3531 return 0;
3532}
3533
Willy Tarreau62f52692017-10-08 23:01:42 +02003534
3535/****************************************/
3536/* MUX initialization and instanciation */
3537/***************************************/
3538
3539/* The mux operations */
3540const struct mux_ops h2_ops = {
3541 .init = h2_init,
3542 .recv = h2_recv,
3543 .send = h2_send,
3544 .wake = h2_wake,
3545 .update_poll = h2_update_poll,
3546 .rcv_buf = h2_rcv_buf,
3547 .snd_buf = h2_snd_buf,
3548 .attach = h2_attach,
3549 .detach = h2_detach,
3550 .shutr = h2_shutr,
3551 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003552 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003553 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003554 .name = "H2",
3555};
3556
3557/* ALPN selection : this mux registers ALPN tolen "h2" */
3558static struct alpn_mux_list alpn_mux_h2 =
3559 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3560
3561/* config keyword parsers */
3562static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003563 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003564 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003565 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003566 { 0, NULL, NULL }
3567}};
3568
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003569static void __h2_deinit(void)
3570{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003571 pool_destroy(pool_head_h2s);
3572 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003573}
3574
Willy Tarreau62f52692017-10-08 23:01:42 +02003575__attribute__((constructor))
3576static void __h2_init(void)
3577{
3578 alpn_register_mux(&alpn_mux_h2);
3579 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003580 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003581 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3582 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003583}