blob: bf080054e6b6b832cc9ad7c35c130204e69dd622 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau35dbd5d2017-09-22 09:13:49 +020020#include <proto/applet.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020021#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020022#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010024#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020025#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026
27
Willy Tarreau2a856182017-05-16 15:20:39 +020028/* dummy streams returned for idle and closed states */
29static const struct h2s *h2_closed_stream;
30static const struct h2s *h2_idle_stream;
31
Willy Tarreau5ab6b572017-09-22 08:05:00 +020032/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010033static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020034/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010035static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020036
37/* Connection flags (32 bit), in h2c->flags */
38#define H2_CF_NONE 0x00000000
39
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020040/* Flags indicating why writing to the mux is blocked. */
41#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
42#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
43#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
44
45/* Flags indicating why writing to the demux is blocked. */
46#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
47#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
48#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
49#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
50#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
51#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
52#define H2_CF_DEM_BLOCK_ANY 0x000000FC // aggregate of the demux flags above
53
Willy Tarreau081d4722017-05-16 21:51:05 +020054/* other flags */
55#define H2_CF_GOAWAY_SENT 0x00000100 // a GOAWAY frame was successfully sent
56#define H2_CF_GOAWAY_FAILED 0x00000200 // a GOAWAY frame failed to be sent
57
58
Willy Tarreau5ab6b572017-09-22 08:05:00 +020059/* H2 connection state, in h2c->st0 */
60enum h2_cs {
61 H2_CS_PREFACE, // init done, waiting for connection preface
62 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
63 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
64 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
65 H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame
66 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
67 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
68 H2_CS_ENTRIES // must be last
69} __attribute__((packed));
70
71/* H2 connection descriptor */
72struct h2c {
73 struct connection *conn;
74
75 enum h2_cs st0; /* mux state */
76 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
77
78 /* 16 bit hole here */
79 uint32_t flags; /* connection flags: H2_CF_* */
80 int32_t max_id; /* highest ID known on this connection, <0 before preface */
81 uint32_t rcvd_c; /* newly received data to ACK for the connection */
82 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
83
84 /* states for the demux direction */
85 struct hpack_dht *ddht; /* demux dynamic header table */
86 struct buffer *dbuf; /* demux buffer */
87
88 int32_t dsi; /* demux stream ID (<0 = idle) */
89 int32_t dfl; /* demux frame length (if dsi >= 0) */
90 int8_t dft; /* demux frame type (if dsi >= 0) */
91 int8_t dff; /* demux frame flags (if dsi >= 0) */
92 /* 16 bit hole here */
93 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
94
95 /* states for the mux direction */
96 struct buffer *mbuf; /* mux buffer */
97 int32_t msi; /* mux stream ID (<0 = idle) */
98 int32_t mfl; /* mux frame length (if dsi >= 0) */
99 int8_t mft; /* mux frame type (if dsi >= 0) */
100 int8_t mff; /* mux frame flags (if dsi >= 0) */
101 /* 16 bit hole here */
102 int32_t miw; /* mux initial window size for all new streams */
103 int32_t mws; /* mux window size. Can be negative. */
104 int32_t mfs; /* mux's max frame size */
105
Willy Tarreauea392822017-10-31 10:02:25 +0100106 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100107 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreauea392822017-10-31 10:02:25 +0100108 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 struct eb_root streams_by_id; /* all active streams by their ID */
110 struct list send_list; /* list of blocked streams requesting to send */
111 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200112 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +0200113 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200114};
115
Willy Tarreau18312642017-10-11 07:57:07 +0200116/* H2 stream state, in h2s->st */
117enum h2_ss {
118 H2_SS_IDLE = 0, // idle
119 H2_SS_RLOC, // reserved(local)
120 H2_SS_RREM, // reserved(remote)
121 H2_SS_OPEN, // open
122 H2_SS_HREM, // half-closed(remote)
123 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200124 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200125 H2_SS_CLOSED, // closed
126 H2_SS_ENTRIES // must be last
127} __attribute__((packed));
128
129/* HTTP/2 stream flags (32 bit), in h2s->flags */
130#define H2_SF_NONE 0x00000000
131#define H2_SF_ES_RCVD 0x00000001
132#define H2_SF_ES_SENT 0x00000002
133
134#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
135#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
136
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200137/* stream flags indicating the reason the stream is blocked */
138#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
139#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
140#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
141#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
142#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
143
Willy Tarreau454f9052017-10-26 19:40:35 +0200144/* stream flags indicating how data is supposed to be sent */
145#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
146#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
147
148/* step we're currently in when sending chunks. This is needed because we may
149 * have to transfer chunks as large as a full buffer so there's no room left
150 * for size nor crlf around.
151 */
152#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
153#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
154#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
155
156#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
157
Willy Tarreau67434202017-11-06 20:20:51 +0100158#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100159#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100160
Willy Tarreau18312642017-10-11 07:57:07 +0200161/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
162 * it is being processed in the internal HTTP representation (H1 for now).
163 */
164struct h2s {
165 struct conn_stream *cs;
166 struct h2c *h2c;
167 struct h1m req, res; /* request and response parser state for H1 */
168 struct eb32_node by_id; /* place in h2c's streams_by_id */
169 struct list list; /* position in active/blocked lists if blocked>0 */
170 int32_t id; /* stream ID */
171 uint32_t flags; /* H2_SF_* */
172 int mws; /* mux window size for this stream */
173 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
174 enum h2_ss st;
175};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200176
Willy Tarreauc6405142017-09-21 20:23:50 +0200177/* descriptor for an h2 frame header */
178struct h2_fh {
179 uint32_t len; /* length, host order, 24 bits */
180 uint32_t sid; /* stream id, host order, 31 bits */
181 uint8_t ft; /* frame type */
182 uint8_t ff; /* frame flags */
183};
184
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200185/* a few settings from the global section */
186static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200187static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200188static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200189
Willy Tarreau2a856182017-05-16 15:20:39 +0200190/* a dmumy closed stream */
191static const struct h2s *h2_closed_stream = &(const struct h2s){
192 .cs = NULL,
193 .h2c = NULL,
194 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100195 .errcode = H2_ERR_STREAM_CLOSED,
196 .flags = H2_SF_RST_SENT,
Willy Tarreau2a856182017-05-16 15:20:39 +0200197 .id = 0,
198};
199
200/* and a dummy idle stream for use with any unannounced stream */
201static const struct h2s *h2_idle_stream = &(const struct h2s){
202 .cs = NULL,
203 .h2c = NULL,
204 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100205 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200206 .id = 0,
207};
208
Willy Tarreauea392822017-10-31 10:02:25 +0100209static struct task *h2_timeout_task(struct task *t);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200210
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200211/*****************************************************/
212/* functions below are for dynamic buffer management */
213/*****************************************************/
214
215/* re-enables receiving on mux <target> after a buffer was allocated. It returns
216 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
217 * if it's impossible to wake up and we prefer to be woken up later.
218 */
219static int h2_dbuf_available(void *target)
220{
221 struct h2c *h2c = target;
222
223 /* take the buffer now as we'll get scheduled waiting for ->wake() */
224 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200225 h2c->flags &= ~H2_CF_DEM_DALLOC;
226 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
227 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200228 return 1;
229 }
230 return 0;
231}
232
233static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
234{
235 struct buffer *buf = NULL;
236
237 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
238 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
239 h2c->dbuf_wait.target = h2c->conn;
240 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100241 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200242 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100243 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200244 __conn_xprt_stop_recv(h2c->conn);
245 }
246 return buf;
247}
248
249static inline void h2_release_dbuf(struct h2c *h2c)
250{
251 if (h2c->dbuf->size) {
252 b_free(&h2c->dbuf);
253 offer_buffers(h2c->dbuf_wait.target,
254 tasks_run_queue + applets_active_queue);
255 }
256}
257
Willy Tarreau14398122017-09-22 14:26:04 +0200258/* re-enables sending on mux <target> after a buffer was allocated. It returns
259 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
260 * if it's impossible to wake up and we prefer to be woken up later.
261 */
262static int h2_mbuf_available(void *target)
263{
264 struct h2c *h2c = target;
265
266 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
267 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200268 if (h2c->flags & H2_CF_MUX_MALLOC) {
269 h2c->flags &= ~H2_CF_MUX_MALLOC;
270 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
271 conn_xprt_want_send(h2c->conn);
272 }
273
274 if (h2c->flags & H2_CF_DEM_MROOM) {
275 h2c->flags &= ~H2_CF_DEM_MROOM;
276 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
277 conn_xprt_want_recv(h2c->conn);
278 }
279
Willy Tarreau14398122017-09-22 14:26:04 +0200280 /* FIXME: we should in fact call something like h2_update_poll()
281 * now to recompte the polling. For now it will be enough like
282 * this.
283 */
Willy Tarreau14398122017-09-22 14:26:04 +0200284 return 1;
285 }
286 return 0;
287}
288
289static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
290{
291 struct buffer *buf = NULL;
292
293 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
294 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
295 h2c->mbuf_wait.target = h2c;
296 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100297 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200298 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100299 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200300
301 /* FIXME: we should in fact only block the direction being
302 * currently used. For now it will be enough like this.
303 */
304 __conn_xprt_stop_send(h2c->conn);
305 __conn_xprt_stop_recv(h2c->conn);
306 }
307 return buf;
308}
309
310static inline void h2_release_mbuf(struct h2c *h2c)
311{
312 if (h2c->mbuf->size) {
313 b_free(&h2c->mbuf);
314 offer_buffers(h2c->mbuf_wait.target,
315 tasks_run_queue + applets_active_queue);
316 }
317}
318
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200319
Willy Tarreau62f52692017-10-08 23:01:42 +0200320/*****************************************************************/
321/* functions below are dedicated to the mux setup and management */
322/*****************************************************************/
323
Willy Tarreau32218eb2017-09-22 08:07:25 +0200324/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
325static int h2c_frt_init(struct connection *conn)
326{
327 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100328 struct task *t = NULL;
329 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200330
Willy Tarreaubafbe012017-11-24 17:34:44 +0100331 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200332 if (!h2c)
333 goto fail;
334
Willy Tarreau3f133572017-10-31 19:21:06 +0100335
Willy Tarreau599391a2017-11-24 10:16:00 +0100336 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
337 if (tick_isset(sess->fe->timeout.clientfin))
338 h2c->shut_timeout = sess->fe->timeout.clientfin;
339
Willy Tarreau33400292017-11-05 11:23:40 +0100340 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100341 if (tick_isset(h2c->timeout)) {
342 t = task_new(tid_bit);
343 if (!t)
344 goto fail;
345
346 h2c->task = t;
347 t->process = h2_timeout_task;
348 t->context = h2c;
349 t->expire = tick_add(now_ms, h2c->timeout);
350 }
Willy Tarreauea392822017-10-31 10:02:25 +0100351
Willy Tarreau32218eb2017-09-22 08:07:25 +0200352 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
353 if (!h2c->ddht)
354 goto fail;
355
356 /* Initialise the context. */
357 h2c->st0 = H2_CS_PREFACE;
358 h2c->conn = conn;
359 h2c->max_id = -1;
360 h2c->errcode = H2_ERR_NO_ERROR;
361 h2c->flags = H2_CF_NONE;
362 h2c->rcvd_c = 0;
363 h2c->rcvd_s = 0;
364
365 h2c->dbuf = &buf_empty;
366 h2c->dsi = -1;
367 h2c->msi = -1;
368 h2c->last_sid = -1;
369
370 h2c->mbuf = &buf_empty;
371 h2c->miw = 65535; /* mux initial window size */
372 h2c->mws = 65535; /* mux window size */
373 h2c->mfs = 16384; /* initial max frame size */
374 h2c->streams_by_id = EB_ROOT_UNIQUE;
375 LIST_INIT(&h2c->send_list);
376 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200377 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200378 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200379 conn->mux_ctx = h2c;
380
Willy Tarreau3f133572017-10-31 19:21:06 +0100381 if (t)
382 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200383 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100384
Willy Tarreau32218eb2017-09-22 08:07:25 +0200385 /* mux->wake will be called soon to complete the operation */
386 return 0;
387 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100388 if (t)
389 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100390 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200391 return -1;
392}
393
Willy Tarreau62f52692017-10-08 23:01:42 +0200394/* Initialize the mux once it's attached. For outgoing connections, the context
395 * is already initialized before installing the mux, so we detect incoming
396 * connections from the fact that the context is still NULL. Returns < 0 on
397 * error.
398 */
399static int h2_init(struct connection *conn)
400{
401 if (conn->mux_ctx) {
402 /* we don't support outgoing connections for now */
403 return -1;
404 }
405
Willy Tarreau32218eb2017-09-22 08:07:25 +0200406 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200407}
408
Willy Tarreau2373acc2017-10-12 17:35:14 +0200409/* returns the stream associated with id <id> or NULL if not found */
410static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
411{
412 struct eb32_node *node;
413
Willy Tarreau2a856182017-05-16 15:20:39 +0200414 if (id > h2c->max_id)
415 return (struct h2s *)h2_idle_stream;
416
Willy Tarreau2373acc2017-10-12 17:35:14 +0200417 node = eb32_lookup(&h2c->streams_by_id, id);
418 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200419 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200420
421 return container_of(node, struct h2s, by_id);
422}
423
Willy Tarreau62f52692017-10-08 23:01:42 +0200424/* release function for a connection. This one should be called to free all
425 * resources allocated to the mux.
426 */
427static void h2_release(struct connection *conn)
428{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200429 struct h2c *h2c = conn->mux_ctx;
430
431 LIST_DEL(&conn->list);
432
433 if (h2c) {
434 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200435 h2_release_dbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100436 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200437 LIST_DEL(&h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100438 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200439
440 h2_release_mbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100441 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200442 LIST_DEL(&h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100443 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200444
Willy Tarreauea392822017-10-31 10:02:25 +0100445 if (h2c->task) {
446 task_delete(h2c->task);
447 task_free(h2c->task);
448 h2c->task = NULL;
449 }
450
Willy Tarreaubafbe012017-11-24 17:34:44 +0100451 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200452 }
453
454 conn->mux = NULL;
455 conn->mux_ctx = NULL;
456
457 conn_stop_tracking(conn);
458 conn_full_close(conn);
459 if (conn->destroy_cb)
460 conn->destroy_cb(conn);
461 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200462}
463
464
Willy Tarreau71681172017-10-23 14:39:06 +0200465/******************************************************/
466/* functions below are for the H2 protocol processing */
467/******************************************************/
468
469/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100470static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200471{
472 return h2s ? h2s->id : 0;
473}
474
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200475/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100476static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200477{
478 if (h2c->msi < 0)
479 return 0;
480
481 if (h2c->msi == h2s_id(h2s))
482 return 0;
483
484 return 1;
485}
486
Willy Tarreau741d6df2017-10-17 08:00:59 +0200487/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100488static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200489{
490 h2c->errcode = err;
491 h2c->st0 = H2_CS_ERROR;
492}
493
Willy Tarreau2e43f082017-10-17 08:03:59 +0200494/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100495static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200496{
497 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
498 h2s->errcode = err;
499 h2s->st = H2_SS_ERROR;
500 if (h2s->cs)
501 h2s->cs->flags |= CS_FL_ERROR;
502 }
503}
504
Willy Tarreaue4820742017-07-27 13:37:23 +0200505/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100506static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200507{
508 uint8_t *out = frame;
509
510 *out = len >> 16;
511 write_n16(out + 1, len);
512}
513
Willy Tarreau54c15062017-10-10 17:10:03 +0200514/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
515 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
516 * the caller's responsibility to verify that there are at least <bytes> bytes
517 * available in the buffer's input prior to calling this function.
518 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100519static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200520 const struct buffer *b, int o)
521{
522 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
523}
524
Willy Tarreau1f094672017-11-20 21:27:45 +0100525static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200526{
527 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
528}
529
Willy Tarreau1f094672017-11-20 21:27:45 +0100530static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200531{
532 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
533}
534
Willy Tarreau1f094672017-11-20 21:27:45 +0100535static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200536{
537 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
538}
539
540
Willy Tarreau715d5312017-07-11 15:20:24 +0200541/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
542 * is not obvious. It turns out that H2 headers are neither aligned nor do they
543 * use regular sizes. And to add to the trouble, the buffer may wrap so each
544 * byte read must be checked. The header is formed like this :
545 *
546 * b0 b1 b2 b3 b4 b5..b8
547 * +----------+---------+--------+----+----+----------------------+
548 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
549 * +----------+---------+--------+----+----+----------------------+
550 *
551 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
552 * we get the sid properly aligned and ordered, and 16 bits of len properly
553 * ordered as well. The type and flags can be extracted using bit shifts from
554 * the word, and only one extra read is needed to fetch len[16:23].
555 * Returns zero if some bytes are missing, otherwise non-zero on success.
556 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100557static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200558{
559 uint64_t w;
560
561 if (b->i < 9)
562 return 0;
563
564 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
565 h->len = *b->p << 16;
566 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
567 h->ff = w >> 32;
568 h->ft = w >> 40;
569 h->len += w >> 48;
570 return 1;
571}
572
573/* skip the next 9 bytes corresponding to the frame header possibly parsed by
574 * h2_peek_frame_hdr() above.
575 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100576static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200577{
578 bi_del(b, 9);
579}
580
581/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100582static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200583{
584 int ret;
585
586 ret = h2_peek_frame_hdr(b, h);
587 if (ret > 0)
588 h2_skip_frame_hdr(b);
589 return ret;
590}
591
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200592/* creates a new stream <id> on the h2c connection and returns it, or NULL in
593 * case of memory allocation error.
594 */
595static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
596{
597 struct conn_stream *cs;
598 struct h2s *h2s;
599
Willy Tarreaubafbe012017-11-24 17:34:44 +0100600 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200601 if (!h2s)
602 goto out;
603
604 h2s->h2c = h2c;
605 h2s->mws = h2c->miw;
606 h2s->flags = H2_SF_NONE;
607 h2s->errcode = H2_ERR_NO_ERROR;
608 h2s->st = H2_SS_IDLE;
609 h1m_init(&h2s->req);
610 h1m_init(&h2s->res);
611 h2s->by_id.key = h2s->id = id;
612 h2c->max_id = id;
613 LIST_INIT(&h2s->list);
614
615 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
616
617 cs = cs_new(h2c->conn);
618 if (!cs)
619 goto out_close;
620
621 h2s->cs = cs;
622 cs->ctx = h2s;
623
624 if (stream_create_from_cs(cs) < 0)
625 goto out_free_cs;
626
627 /* OK done, the stream lives its own life now */
628 return h2s;
629
630 out_free_cs:
631 cs_free(cs);
632 out_close:
633 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100634 pool_free(pool_head_h2s, h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200635 h2s = NULL;
636 out:
637 return h2s;
638}
639
Willy Tarreaube5b7152017-09-25 16:25:39 +0200640/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
641 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
642 * the various settings codes.
643 */
644static int h2c_snd_settings(struct h2c *h2c)
645{
646 struct buffer *res;
647 char buf_data[100]; // enough for 15 settings
648 struct chunk buf;
649 int ret;
650
651 if (h2c_mux_busy(h2c, NULL)) {
652 h2c->flags |= H2_CF_DEM_MBUSY;
653 return 0;
654 }
655
656 res = h2_get_mbuf(h2c);
657 if (!res) {
658 h2c->flags |= H2_CF_MUX_MALLOC;
659 h2c->flags |= H2_CF_DEM_MROOM;
660 return 0;
661 }
662
663 chunk_init(&buf, buf_data, sizeof(buf_data));
664 chunk_memcpy(&buf,
665 "\x00\x00\x00" /* length : 0 for now */
666 "\x04\x00" /* type : 4 (settings), flags : 0 */
667 "\x00\x00\x00\x00", /* stream ID : 0 */
668 9);
669
670 if (h2_settings_header_table_size != 4096) {
671 char str[6] = "\x00\x01"; /* header_table_size */
672
673 write_n32(str + 2, h2_settings_header_table_size);
674 chunk_memcat(&buf, str, 6);
675 }
676
677 if (h2_settings_initial_window_size != 65535) {
678 char str[6] = "\x00\x04"; /* initial_window_size */
679
680 write_n32(str + 2, h2_settings_initial_window_size);
681 chunk_memcat(&buf, str, 6);
682 }
683
684 if (h2_settings_max_concurrent_streams != 0) {
685 char str[6] = "\x00\x03"; /* max_concurrent_streams */
686
687 /* Note: 0 means "unlimited" for haproxy's config but not for
688 * the protocol, so never send this value!
689 */
690 write_n32(str + 2, h2_settings_max_concurrent_streams);
691 chunk_memcat(&buf, str, 6);
692 }
693
694 if (global.tune.bufsize != 16384) {
695 char str[6] = "\x00\x05"; /* max_frame_size */
696
697 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
698 * match bufsize - rewrite size, but at the moment it seems
699 * that clients don't take care of it.
700 */
701 write_n32(str + 2, global.tune.bufsize);
702 chunk_memcat(&buf, str, 6);
703 }
704
705 h2_set_frame_size(buf.str, buf.len - 9);
706 ret = bo_istput(res, ist2(buf.str, buf.len));
707 if (unlikely(ret <= 0)) {
708 if (!ret) {
709 h2c->flags |= H2_CF_MUX_MFULL;
710 h2c->flags |= H2_CF_DEM_MROOM;
711 return 0;
712 }
713 else {
714 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
715 return 0;
716 }
717 }
718 return ret;
719}
720
Willy Tarreau52eed752017-09-22 15:05:09 +0200721/* Try to receive a connection preface, then upon success try to send our
722 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
723 * missing data. It may return an error in h2c.
724 */
725static int h2c_frt_recv_preface(struct h2c *h2c)
726{
727 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200728 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200729
730 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
731
732 if (unlikely(ret1 <= 0)) {
733 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
734 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
735 return 0;
736 }
737
Willy Tarreaube5b7152017-09-25 16:25:39 +0200738 ret2 = h2c_snd_settings(h2c);
739 if (ret2 > 0)
740 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200741
Willy Tarreaube5b7152017-09-25 16:25:39 +0200742 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200743}
744
Willy Tarreau081d4722017-05-16 21:51:05 +0200745/* try to send a GOAWAY frame on the connection to report an error or a graceful
746 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
747 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
748 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
749 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
750 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
751 * on unrecoverable failure. It will not attempt to send one again in this last
752 * case so that it is safe to use h2c_error() to report such errors.
753 */
754static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
755{
756 struct buffer *res;
757 char str[17];
758 int ret;
759
760 if (h2c->flags & H2_CF_GOAWAY_FAILED)
761 return 1; // claim that it worked
762
763 if (h2c_mux_busy(h2c, h2s)) {
764 if (h2s)
765 h2s->flags |= H2_SF_BLK_MBUSY;
766 else
767 h2c->flags |= H2_CF_DEM_MBUSY;
768 return 0;
769 }
770
771 res = h2_get_mbuf(h2c);
772 if (!res) {
773 h2c->flags |= H2_CF_MUX_MALLOC;
774 if (h2s)
775 h2s->flags |= H2_SF_BLK_MROOM;
776 else
777 h2c->flags |= H2_CF_DEM_MROOM;
778 return 0;
779 }
780
781 /* len: 8, type: 7, flags: none, sid: 0 */
782 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
783
784 if (h2c->last_sid < 0)
785 h2c->last_sid = h2c->max_id;
786
787 write_n32(str + 9, h2c->last_sid);
788 write_n32(str + 13, h2c->errcode);
789 ret = bo_istput(res, ist2(str, 17));
790 if (unlikely(ret <= 0)) {
791 if (!ret) {
792 h2c->flags |= H2_CF_MUX_MFULL;
793 if (h2s)
794 h2s->flags |= H2_SF_BLK_MROOM;
795 else
796 h2c->flags |= H2_CF_DEM_MROOM;
797 return 0;
798 }
799 else {
800 /* we cannot report this error using GOAWAY, so we mark
801 * it and claim a success.
802 */
803 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
804 h2c->flags |= H2_CF_GOAWAY_FAILED;
805 return 1;
806 }
807 }
808 h2c->flags |= H2_CF_GOAWAY_SENT;
809 return ret;
810}
811
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100812/* Try to send an RST_STREAM frame on the connection for the indicated stream
813 * during mux operations. This stream must be valid and cannot be closed
814 * already. h2s->id will be used for the stream ID and h2s->errcode will be
815 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
816 * not yet.
817 *
818 * Returns > 0 on success or zero if nothing was done. In case of lack of room
819 * to write the message, it subscribes the stream to future notifications.
820 */
821static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
822{
823 struct buffer *res;
824 char str[13];
825 int ret;
826
827 if (!h2s || h2s->st == H2_SS_CLOSED)
828 return 1;
829
830 if (h2c_mux_busy(h2c, h2s)) {
831 h2s->flags |= H2_SF_BLK_MBUSY;
832 return 0;
833 }
834
835 res = h2_get_mbuf(h2c);
836 if (!res) {
837 h2c->flags |= H2_CF_MUX_MALLOC;
838 h2s->flags |= H2_SF_BLK_MROOM;
839 return 0;
840 }
841
842 /* len: 4, type: 3, flags: none */
843 memcpy(str, "\x00\x00\x04\x03\x00", 5);
844 write_n32(str + 5, h2s->id);
845 write_n32(str + 9, h2s->errcode);
846 ret = bo_istput(res, ist2(str, 13));
847
848 if (unlikely(ret <= 0)) {
849 if (!ret) {
850 h2c->flags |= H2_CF_MUX_MFULL;
851 h2s->flags |= H2_SF_BLK_MROOM;
852 return 0;
853 }
854 else {
855 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
856 return 0;
857 }
858 }
859
860 h2s->flags |= H2_SF_RST_SENT;
861 h2s->st = H2_SS_CLOSED;
862 return ret;
863}
864
865/* Try to send an RST_STREAM frame on the connection for the stream being
866 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
867 * error code unless the stream's state already is IDLE or CLOSED in which
868 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
869 * it was not yet.
870 *
871 * Returns > 0 on success or zero if nothing was done. In case of lack of room
872 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200873 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100874 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200875 */
876static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
877{
878 struct buffer *res;
879 char str[13];
880 int ret;
881
882 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100883 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200884 return 0;
885 }
886
887 res = h2_get_mbuf(h2c);
888 if (!res) {
889 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100890 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200891 return 0;
892 }
893
894 /* len: 4, type: 3, flags: none */
895 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100896
Willy Tarreau27a84c92017-10-17 08:10:17 +0200897 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100898 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200899 h2s->errcode : H2_ERR_STREAM_CLOSED);
900 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100901
Willy Tarreau27a84c92017-10-17 08:10:17 +0200902 if (unlikely(ret <= 0)) {
903 if (!ret) {
904 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100905 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200906 return 0;
907 }
908 else {
909 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
910 return 0;
911 }
912 }
913
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100914 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200915 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100916 h2s->st = H2_SS_CLOSED;
917 }
918
Willy Tarreau27a84c92017-10-17 08:10:17 +0200919 return ret;
920}
921
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100922/* try to send an empty DATA frame with the ES flag set to notify about the
923 * end of stream and match a shutdown(write). If an ES was already sent as
924 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
925 * on success or zero if nothing was done. In case of lack of room to write the
926 * message, it subscribes the requesting stream to future notifications.
927 */
928static int h2_send_empty_data_es(struct h2s *h2s)
929{
930 struct h2c *h2c = h2s->h2c;
931 struct buffer *res;
932 char str[9];
933 int ret;
934
Willy Tarreau721c9742017-11-07 11:05:42 +0100935 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100936 return 1;
937
938 if (h2c_mux_busy(h2c, h2s)) {
939 h2s->flags |= H2_SF_BLK_MBUSY;
940 return 0;
941 }
942
943 res = h2_get_mbuf(h2c);
944 if (!res) {
945 h2c->flags |= H2_CF_MUX_MALLOC;
946 h2s->flags |= H2_SF_BLK_MROOM;
947 return 0;
948 }
949
950 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
951 memcpy(str, "\x00\x00\x00\x00\x01", 5);
952 write_n32(str + 5, h2s->id);
953 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100954 if (likely(ret > 0)) {
955 h2s->flags |= H2_SF_ES_SENT;
956 }
957 else if (!ret) {
958 h2c->flags |= H2_CF_MUX_MFULL;
959 h2s->flags |= H2_SF_BLK_MROOM;
960 return 0;
961 }
962 else {
963 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
964 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100965 }
966 return ret;
967}
968
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100969/* wake the streams attached to the connection, whose id is greater than <last>,
970 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
971 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
972 * stream's state is automatically updated accordingly.
973 */
974static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
975{
976 struct eb32_node *node;
977 struct h2s *h2s;
978
979 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
980 flags |= CS_FL_ERROR;
981
982 if (conn_xprt_read0_pending(h2c->conn))
983 flags |= CS_FL_EOS;
984
985 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
986 while (node) {
987 h2s = container_of(node, struct h2s, by_id);
988 if (h2s->id <= last)
989 break;
990 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +0100991
992 if (!h2s->cs) {
993 /* this stream was already orphaned */
994 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100995 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +0100996 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100997 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +0100998
999 h2s->cs->flags |= flags;
1000 /* recv is used to force to detect CS_FL_EOS that wake()
1001 * doesn't handle in the stream int code.
1002 */
1003 h2s->cs->data_cb->recv(h2s->cs);
1004 h2s->cs->data_cb->wake(h2s->cs);
1005
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001006 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1007 h2s->st = H2_SS_ERROR;
1008 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1009 h2s->st = H2_SS_HREM;
1010 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
1011 h2s->st = H2_SS_CLOSED;
1012 }
1013}
1014
Willy Tarreau3421aba2017-07-27 15:41:03 +02001015/* Increase all streams' outgoing window size by the difference passed in
1016 * argument. This is needed upon receipt of the settings frame if the initial
1017 * window size is different. The difference may be negative and the resulting
1018 * window size as well, for the time it takes to receive some window updates.
1019 */
1020static void h2c_update_all_ws(struct h2c *h2c, int diff)
1021{
1022 struct h2s *h2s;
1023 struct eb32_node *node;
1024
1025 if (!diff)
1026 return;
1027
1028 node = eb32_first(&h2c->streams_by_id);
1029 while (node) {
1030 h2s = container_of(node, struct h2s, by_id);
1031 h2s->mws += diff;
1032 node = eb32_next(node);
1033 }
1034}
1035
1036/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1037 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1038 * return an error in h2c. Described in RFC7540#6.5.
1039 */
1040static int h2c_handle_settings(struct h2c *h2c)
1041{
1042 unsigned int offset;
1043 int error;
1044
1045 if (h2c->dff & H2_F_SETTINGS_ACK) {
1046 if (h2c->dfl) {
1047 error = H2_ERR_FRAME_SIZE_ERROR;
1048 goto fail;
1049 }
1050 return 1;
1051 }
1052
1053 if (h2c->dsi != 0) {
1054 error = H2_ERR_PROTOCOL_ERROR;
1055 goto fail;
1056 }
1057
1058 if (h2c->dfl % 6) {
1059 error = H2_ERR_FRAME_SIZE_ERROR;
1060 goto fail;
1061 }
1062
1063 /* that's the limit we can process */
1064 if (h2c->dfl > global.tune.bufsize) {
1065 error = H2_ERR_FRAME_SIZE_ERROR;
1066 goto fail;
1067 }
1068
1069 /* process full frame only */
1070 if (h2c->dbuf->i < h2c->dfl)
1071 return 0;
1072
1073 /* parse the frame */
1074 for (offset = 0; offset < h2c->dfl; offset += 6) {
1075 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1076 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1077
1078 switch (type) {
1079 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1080 /* we need to update all existing streams with the
1081 * difference from the previous iws.
1082 */
1083 if (arg < 0) { // RFC7540#6.5.2
1084 error = H2_ERR_FLOW_CONTROL_ERROR;
1085 goto fail;
1086 }
1087 h2c_update_all_ws(h2c, arg - h2c->miw);
1088 h2c->miw = arg;
1089 break;
1090 case H2_SETTINGS_MAX_FRAME_SIZE:
1091 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1092 error = H2_ERR_PROTOCOL_ERROR;
1093 goto fail;
1094 }
1095 h2c->mfs = arg;
1096 break;
1097 }
1098 }
1099
1100 /* need to ACK this frame now */
1101 h2c->st0 = H2_CS_FRAME_A;
1102 return 1;
1103 fail:
1104 h2c_error(h2c, error);
1105 return 0;
1106}
1107
1108/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1109 * success or one of the h2_status values.
1110 */
1111static int h2c_ack_settings(struct h2c *h2c)
1112{
1113 struct buffer *res;
1114 char str[9];
1115 int ret = -1;
1116
1117 if (h2c_mux_busy(h2c, NULL)) {
1118 h2c->flags |= H2_CF_DEM_MBUSY;
1119 return 0;
1120 }
1121
1122 res = h2_get_mbuf(h2c);
1123 if (!res) {
1124 h2c->flags |= H2_CF_MUX_MALLOC;
1125 h2c->flags |= H2_CF_DEM_MROOM;
1126 return 0;
1127 }
1128
1129 memcpy(str,
1130 "\x00\x00\x00" /* length : 0 (no data) */
1131 "\x04" "\x01" /* type : 4, flags : ACK */
1132 "\x00\x00\x00\x00" /* stream ID */, 9);
1133
1134 ret = bo_istput(res, ist2(str, 9));
1135 if (unlikely(ret <= 0)) {
1136 if (!ret) {
1137 h2c->flags |= H2_CF_MUX_MFULL;
1138 h2c->flags |= H2_CF_DEM_MROOM;
1139 return 0;
1140 }
1141 else {
1142 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1143 return 0;
1144 }
1145 }
1146 return ret;
1147}
1148
Willy Tarreaucf68c782017-10-10 17:11:41 +02001149/* processes a PING frame and schedules an ACK if needed. The caller must pass
1150 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1151 * missing data. It may return an error in h2c.
1152 */
1153static int h2c_handle_ping(struct h2c *h2c)
1154{
1155 /* frame length must be exactly 8 */
1156 if (h2c->dfl != 8) {
1157 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1158 return 0;
1159 }
1160
1161 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001162 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001163 h2c->st0 = H2_CS_FRAME_A;
1164 return 1;
1165}
1166
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001167/* Try to send a window update for stream id <sid> and value <increment>.
1168 * Returns > 0 on success or zero on missing room or failure. It may return an
1169 * error in h2c.
1170 */
1171static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1172{
1173 struct buffer *res;
1174 char str[13];
1175 int ret = -1;
1176
1177 if (h2c_mux_busy(h2c, NULL)) {
1178 h2c->flags |= H2_CF_DEM_MBUSY;
1179 return 0;
1180 }
1181
1182 res = h2_get_mbuf(h2c);
1183 if (!res) {
1184 h2c->flags |= H2_CF_MUX_MALLOC;
1185 h2c->flags |= H2_CF_DEM_MROOM;
1186 return 0;
1187 }
1188
1189 /* length: 4, type: 8, flags: none */
1190 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1191 write_n32(str + 5, sid);
1192 write_n32(str + 9, increment);
1193
1194 ret = bo_istput(res, ist2(str, 13));
1195
1196 if (unlikely(ret <= 0)) {
1197 if (!ret) {
1198 h2c->flags |= H2_CF_MUX_MFULL;
1199 h2c->flags |= H2_CF_DEM_MROOM;
1200 return 0;
1201 }
1202 else {
1203 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1204 return 0;
1205 }
1206 }
1207 return ret;
1208}
1209
1210/* try to send pending window update for the connection. It's safe to call it
1211 * with no pending updates. Returns > 0 on success or zero on missing room or
1212 * failure. It may return an error in h2c.
1213 */
1214static int h2c_send_conn_wu(struct h2c *h2c)
1215{
1216 int ret = 1;
1217
1218 if (h2c->rcvd_c <= 0)
1219 return 1;
1220
1221 /* send WU for the connection */
1222 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1223 if (ret > 0)
1224 h2c->rcvd_c = 0;
1225
1226 return ret;
1227}
1228
1229/* try to send pending window update for the current dmux stream. It's safe to
1230 * call it with no pending updates. Returns > 0 on success or zero on missing
1231 * room or failure. It may return an error in h2c.
1232 */
1233static int h2c_send_strm_wu(struct h2c *h2c)
1234{
1235 int ret = 1;
1236
1237 if (h2c->rcvd_s <= 0)
1238 return 1;
1239
1240 /* send WU for the stream */
1241 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1242 if (ret > 0)
1243 h2c->rcvd_s = 0;
1244
1245 return ret;
1246}
1247
Willy Tarreaucf68c782017-10-10 17:11:41 +02001248/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1249 * success, 0 on missing data or one of the h2_status values.
1250 */
1251static int h2c_ack_ping(struct h2c *h2c)
1252{
1253 struct buffer *res;
1254 char str[17];
1255 int ret = -1;
1256
1257 if (h2c->dbuf->i < 8)
1258 return 0;
1259
1260 if (h2c_mux_busy(h2c, NULL)) {
1261 h2c->flags |= H2_CF_DEM_MBUSY;
1262 return 0;
1263 }
1264
1265 res = h2_get_mbuf(h2c);
1266 if (!res) {
1267 h2c->flags |= H2_CF_MUX_MALLOC;
1268 h2c->flags |= H2_CF_DEM_MROOM;
1269 return 0;
1270 }
1271
1272 memcpy(str,
1273 "\x00\x00\x08" /* length : 8 (same payload) */
1274 "\x06" "\x01" /* type : 6, flags : ACK */
1275 "\x00\x00\x00\x00" /* stream ID */, 9);
1276
1277 /* copy the original payload */
1278 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1279
1280 ret = bo_istput(res, ist2(str, 17));
1281 if (unlikely(ret <= 0)) {
1282 if (!ret) {
1283 h2c->flags |= H2_CF_MUX_MFULL;
1284 h2c->flags |= H2_CF_DEM_MROOM;
1285 return 0;
1286 }
1287 else {
1288 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1289 return 0;
1290 }
1291 }
1292 return ret;
1293}
1294
Willy Tarreau26f95952017-07-27 17:18:30 +02001295/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1296 * Returns > 0 on success or zero on missing data. It may return an error in
1297 * h2c or h2s. Described in RFC7540#6.9.
1298 */
1299static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1300{
1301 int32_t inc;
1302 int error;
1303
1304 if (h2c->dfl != 4) {
1305 error = H2_ERR_FRAME_SIZE_ERROR;
1306 goto conn_err;
1307 }
1308
1309 /* process full frame only */
1310 if (h2c->dbuf->i < h2c->dfl)
1311 return 0;
1312
1313 inc = h2_get_n32(h2c->dbuf, 0);
1314
1315 if (h2c->dsi != 0) {
1316 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001317
1318 /* it's not an error to receive WU on a closed stream */
1319 if (h2s->st == H2_SS_CLOSED)
1320 return 1;
1321
1322 if (!inc) {
1323 error = H2_ERR_PROTOCOL_ERROR;
1324 goto strm_err;
1325 }
1326
1327 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1328 error = H2_ERR_FLOW_CONTROL_ERROR;
1329 goto strm_err;
1330 }
1331
1332 h2s->mws += inc;
1333 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1334 h2s->flags &= ~H2_SF_BLK_SFCTL;
1335 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1336 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1337 /* This stream wanted to send but could not due to its
1338 * own flow control. We can put it back into the send
1339 * list now, it will be handled upon next send() call.
1340 */
1341 LIST_ADDQ(&h2c->send_list, &h2s->list);
1342 }
1343 }
1344 }
1345 else {
1346 /* connection window update */
1347 if (!inc) {
1348 error = H2_ERR_PROTOCOL_ERROR;
1349 goto conn_err;
1350 }
1351
1352 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1353 error = H2_ERR_FLOW_CONTROL_ERROR;
1354 goto conn_err;
1355 }
1356
1357 h2c->mws += inc;
1358 }
1359
1360 return 1;
1361
1362 conn_err:
1363 h2c_error(h2c, error);
1364 return 0;
1365
1366 strm_err:
1367 if (h2s) {
1368 h2s_error(h2s, error);
1369 h2c->st0 = H2_CS_FRAME_A;
1370 }
1371 else
1372 h2c_error(h2c, error);
1373 return 0;
1374}
1375
Willy Tarreaue96b0922017-10-30 00:28:29 +01001376/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1377 * the last ID. Returns > 0 on success or zero on missing data. It may return
1378 * an error in h2c. Described in RFC7540#6.8.
1379 */
1380static int h2c_handle_goaway(struct h2c *h2c)
1381{
1382 int error;
1383 int last;
1384
1385 if (h2c->dsi != 0) {
1386 error = H2_ERR_PROTOCOL_ERROR;
1387 goto conn_err;
1388 }
1389
1390 if (h2c->dfl < 8) {
1391 error = H2_ERR_FRAME_SIZE_ERROR;
1392 goto conn_err;
1393 }
1394
1395 /* process full frame only */
1396 if (h2c->dbuf->i < h2c->dfl)
1397 return 0;
1398
1399 last = h2_get_n32(h2c->dbuf, 0);
1400 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1401 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001402 if (h2c->last_sid < 0)
1403 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001404 return 1;
1405
1406 conn_err:
1407 h2c_error(h2c, error);
1408 return 0;
1409}
1410
Willy Tarreaucd234e92017-08-18 10:59:39 +02001411/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1412 * Returns > 0 on success or zero on missing data. It may return an error in
1413 * h2c. Described in RFC7540#6.4.
1414 */
1415static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1416{
1417 int error;
1418
1419 if (h2c->dsi == 0) {
1420 error = H2_ERR_PROTOCOL_ERROR;
1421 goto conn_err;
1422 }
1423
Willy Tarreaucd234e92017-08-18 10:59:39 +02001424 if (h2c->dfl != 4) {
1425 error = H2_ERR_FRAME_SIZE_ERROR;
1426 goto conn_err;
1427 }
1428
1429 /* process full frame only */
1430 if (h2c->dbuf->i < h2c->dfl)
1431 return 0;
1432
1433 /* late RST, already handled */
1434 if (h2s->st == H2_SS_CLOSED)
1435 return 1;
1436
1437 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
1438 h2s->st = H2_SS_CLOSED;
1439
1440 if (h2s->cs) {
1441 h2s->cs->flags |= CS_FL_EOS;
1442 /* recv is used to force to detect CS_FL_EOS that wake()
1443 * doesn't handle in the stream-int code.
1444 */
1445 h2s->cs->data_cb->recv(h2s->cs);
1446 h2s->cs->data_cb->wake(h2s->cs);
1447 }
1448
1449 h2s->flags |= H2_SF_RST_RCVD;
1450 return 1;
1451
1452 conn_err:
1453 h2c_error(h2c, error);
1454 return 0;
1455}
1456
Willy Tarreau13278b42017-10-13 19:23:14 +02001457/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1458 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1459 * errors here are reported as connection errors since it's impossible to
1460 * recover from such errors after the compression context has been altered.
1461 */
1462static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1463{
1464 int error;
1465
1466 if (!h2c->dfl) {
1467 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1468 goto strm_err;
1469 }
1470
1471 if (!h2c->dbuf->size)
1472 return 0; // empty buffer
1473
1474 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1475 return 0; // incomplete frame
1476
1477 /* now either the frame is complete or the buffer is complete */
1478 if (h2s->st != H2_SS_IDLE) {
1479 /* FIXME: stream already exists, this is only allowed for
1480 * trailers (not supported for now).
1481 */
1482 error = H2_ERR_PROTOCOL_ERROR;
1483 goto conn_err;
1484 }
1485 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1486 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1487 error = H2_ERR_PROTOCOL_ERROR;
1488 goto conn_err;
1489 }
1490
1491 h2s = h2c_stream_new(h2c, h2c->dsi);
1492 if (!h2s) {
1493 error = H2_ERR_INTERNAL_ERROR;
1494 goto conn_err;
1495 }
1496
1497 h2s->st = H2_SS_OPEN;
1498 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1499 h2s->st = H2_SS_HREM;
1500 h2s->flags |= H2_SF_ES_RCVD;
1501 }
1502
1503 /* call the upper layers to process the frame, then let the upper layer
1504 * notify the stream about any change.
1505 */
1506 h2s->cs->data_cb->recv(h2s->cs);
1507
1508 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1509 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1510 error = H2_ERR_INTERNAL_ERROR;
1511 goto conn_err;
1512 }
1513
Willy Tarreau8f650c32017-11-21 19:36:21 +01001514 if (h2c->st0 >= H2_CS_ERROR)
1515 return 0;
1516
Willy Tarreau721c9742017-11-07 11:05:42 +01001517 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001518 /* stream error : send RST_STREAM */
1519 h2c->st0 = H2_CS_FRAME_A;
1520 }
1521 else {
1522 /* update the max stream ID if the request is being processed */
1523 if (h2s->id > h2c->max_id)
1524 h2c->max_id = h2s->id;
1525 }
1526
1527 return 1;
1528
1529 conn_err:
1530 h2c_error(h2c, error);
1531 return 0;
1532
1533 strm_err:
1534 if (h2s) {
1535 h2s_error(h2s, error);
1536 h2c->st0 = H2_CS_FRAME_A;
1537 }
1538 else
1539 h2c_error(h2c, error);
1540 return 0;
1541}
1542
Willy Tarreau454f9052017-10-26 19:40:35 +02001543/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1544 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1545 */
1546static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1547{
1548 int error;
1549
1550 /* note that empty DATA frames are perfectly valid and sometimes used
1551 * to signal an end of stream (with the ES flag).
1552 */
1553
1554 if (!h2c->dbuf->size && h2c->dfl)
1555 return 0; // empty buffer
1556
1557 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1558 return 0; // incomplete frame
1559
1560 /* now either the frame is complete or the buffer is complete */
1561
1562 if (!h2c->dsi) {
1563 /* RFC7540#6.1 */
1564 error = H2_ERR_PROTOCOL_ERROR;
1565 goto conn_err;
1566 }
1567
1568 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1569 /* RFC7540#6.1 */
1570 error = H2_ERR_STREAM_CLOSED;
1571 goto strm_err;
1572 }
1573
1574 /* last frame */
1575 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1576 h2s->st = H2_SS_HREM;
1577 h2s->flags |= H2_SF_ES_RCVD;
1578 }
1579
1580 /* call the upper layers to process the frame, then let the upper layer
1581 * notify the stream about any change.
1582 */
1583 if (!h2s->cs) {
1584 error = H2_ERR_STREAM_CLOSED;
1585 goto strm_err;
1586 }
1587
1588 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001589
Willy Tarreau454f9052017-10-26 19:40:35 +02001590 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1591 /* cs has just been destroyed, we have to kill h2s. */
1592 error = H2_ERR_STREAM_CLOSED;
1593 goto strm_err;
1594 }
1595
Willy Tarreau8f650c32017-11-21 19:36:21 +01001596 if (h2c->st0 >= H2_CS_ERROR)
1597 return 0;
1598
Willy Tarreau721c9742017-11-07 11:05:42 +01001599 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001600 /* stream error : send RST_STREAM */
1601 h2c->st0 = H2_CS_FRAME_A;
1602 }
1603
1604 /* check for completion : the callee will change this to FRAME_A or
1605 * FRAME_H once done.
1606 */
1607 if (h2c->st0 == H2_CS_FRAME_P)
1608 return 0;
1609
1610 return 1;
1611
1612 conn_err:
1613 h2c_error(h2c, error);
1614 return 0;
1615
1616 strm_err:
1617 if (h2s) {
1618 h2s_error(h2s, error);
1619 h2c->st0 = H2_CS_FRAME_A;
1620 }
1621 else
1622 h2c_error(h2c, error);
1623 return 0;
1624}
1625
Willy Tarreaubc933932017-10-09 16:21:43 +02001626/* process Rx frames to be demultiplexed */
1627static void h2_process_demux(struct h2c *h2c)
1628{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001629 struct h2s *h2s;
1630
Willy Tarreau081d4722017-05-16 21:51:05 +02001631 if (h2c->st0 >= H2_CS_ERROR)
1632 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001633
1634 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1635 if (h2c->st0 == H2_CS_PREFACE) {
1636 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1637 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1638 if (h2c->st0 == H2_CS_ERROR)
1639 h2c->st0 = H2_CS_ERROR2;
1640 goto fail;
1641 }
1642
1643 h2c->max_id = 0;
1644 h2c->st0 = H2_CS_SETTINGS1;
1645 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001646
1647 if (h2c->st0 == H2_CS_SETTINGS1) {
1648 struct h2_fh hdr;
1649
1650 /* ensure that what is pending is a valid SETTINGS frame
1651 * without an ACK.
1652 */
1653 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1654 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1655 if (h2c->st0 == H2_CS_ERROR)
1656 h2c->st0 = H2_CS_ERROR2;
1657 goto fail;
1658 }
1659
1660 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1661 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1662 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1663 h2c->st0 = H2_CS_ERROR2;
1664 goto fail;
1665 }
1666
1667 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1668 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1669 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1670 h2c->st0 = H2_CS_ERROR2;
1671 goto fail;
1672 }
1673
1674 /* that's OK, switch to FRAME_P to process it */
1675 h2c->dfl = hdr.len;
1676 h2c->dsi = hdr.sid;
1677 h2c->dft = hdr.ft;
1678 h2c->dff = hdr.ff;
1679 h2c->st0 = H2_CS_FRAME_P;
1680 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001681 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001682
1683 /* process as many incoming frames as possible below */
1684 while (h2c->dbuf->i) {
1685 int ret = 0;
1686
1687 if (h2c->st0 >= H2_CS_ERROR)
1688 break;
1689
1690 if (h2c->st0 == H2_CS_FRAME_H) {
1691 struct h2_fh hdr;
1692
1693 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1694 break;
1695
1696 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1697 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1698 h2c->st0 = H2_CS_ERROR;
1699 break;
1700 }
1701
1702 h2c->dfl = hdr.len;
1703 h2c->dsi = hdr.sid;
1704 h2c->dft = hdr.ft;
1705 h2c->dff = hdr.ff;
1706 h2c->st0 = H2_CS_FRAME_P;
1707 h2_skip_frame_hdr(h2c->dbuf);
1708 }
1709
1710 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001711 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001712
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001713 if (h2s->st == H2_SS_IDLE &&
1714 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1715 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1716 * this state MUST be treated as a connection error
1717 */
1718 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1719 h2c->st0 = H2_CS_ERROR;
1720 break;
1721 }
1722
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001723 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1724 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1725 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1726 * this state MUST be treated as a stream error
1727 */
1728 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1729 goto strm_err;
1730 }
1731
Willy Tarreauc0da1962017-10-30 18:38:00 +01001732#if 0
1733 // problem below: it is not possible to completely ignore such
1734 // streams as we need to maintain the compression state as well
1735 // and for this we need to completely process these frames (eg:
1736 // HEADERS frames) as well as counting DATA frames to emit
1737 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1738 // This is a typical case of layer violation where the
1739 // transported contents are critical to the connection's
1740 // validity and must be ignored at the same time :-(
1741
1742 /* graceful shutdown, ignore streams whose ID is higher than
1743 * the one advertised in GOAWAY. RFC7540#6.8.
1744 */
1745 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1746 ret = MIN(h2c->dbuf->i, h2c->dfl);
1747 bi_del(h2c->dbuf, ret);
1748 h2c->dfl -= ret;
1749 ret = h2c->dfl == 0;
1750 goto strm_err;
1751 }
1752#endif
1753
Willy Tarreau7e98c052017-10-10 15:56:59 +02001754 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001755 case H2_FT_SETTINGS:
1756 if (h2c->st0 == H2_CS_FRAME_P)
1757 ret = h2c_handle_settings(h2c);
1758
1759 if (h2c->st0 == H2_CS_FRAME_A)
1760 ret = h2c_ack_settings(h2c);
1761 break;
1762
Willy Tarreaucf68c782017-10-10 17:11:41 +02001763 case H2_FT_PING:
1764 if (h2c->st0 == H2_CS_FRAME_P)
1765 ret = h2c_handle_ping(h2c);
1766
1767 if (h2c->st0 == H2_CS_FRAME_A)
1768 ret = h2c_ack_ping(h2c);
1769 break;
1770
Willy Tarreau26f95952017-07-27 17:18:30 +02001771 case H2_FT_WINDOW_UPDATE:
1772 if (h2c->st0 == H2_CS_FRAME_P)
1773 ret = h2c_handle_window_update(h2c, h2s);
1774 break;
1775
Willy Tarreau61290ec2017-10-17 08:19:21 +02001776 case H2_FT_CONTINUATION:
1777 /* we currently don't support CONTINUATION frames since
1778 * we have nowhere to store the partial HEADERS frame.
1779 * Let's abort the stream on an INTERNAL_ERROR here.
1780 */
1781 if (h2c->st0 == H2_CS_FRAME_P)
1782 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1783 break;
1784
Willy Tarreau13278b42017-10-13 19:23:14 +02001785 case H2_FT_HEADERS:
1786 if (h2c->st0 == H2_CS_FRAME_P)
1787 ret = h2c_frt_handle_headers(h2c, h2s);
1788 break;
1789
Willy Tarreau454f9052017-10-26 19:40:35 +02001790 case H2_FT_DATA:
1791 if (h2c->st0 == H2_CS_FRAME_P)
1792 ret = h2c_frt_handle_data(h2c, h2s);
1793
1794 if (h2c->st0 == H2_CS_FRAME_A)
1795 ret = h2c_send_strm_wu(h2c);
1796 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001797
1798 case H2_FT_RST_STREAM:
1799 if (h2c->st0 == H2_CS_FRAME_P)
1800 ret = h2c_handle_rst_stream(h2c, h2s);
1801 break;
1802
Willy Tarreaue96b0922017-10-30 00:28:29 +01001803 case H2_FT_GOAWAY:
1804 if (h2c->st0 == H2_CS_FRAME_P)
1805 ret = h2c_handle_goaway(h2c);
1806 break;
1807
Willy Tarreau1c661982017-10-30 13:52:01 +01001808 case H2_FT_PUSH_PROMISE:
1809 /* not permitted here, RFC7540#5.1 */
1810 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001811 break;
1812
1813 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001814 default:
1815 /* drop frames that we ignore. They may be larger than
1816 * the buffer so we drain all of their contents until
1817 * we reach the end.
1818 */
1819 ret = MIN(h2c->dbuf->i, h2c->dfl);
1820 bi_del(h2c->dbuf, ret);
1821 h2c->dfl -= ret;
1822 ret = h2c->dfl == 0;
1823 }
1824
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001825 strm_err:
Willy Tarreau27a84c92017-10-17 08:10:17 +02001826 /* RST are sent similarly to frame acks */
1827 if (h2s->st == H2_SS_ERROR) {
1828 if (h2c->st0 == H2_CS_FRAME_P)
1829 h2c->st0 = H2_CS_FRAME_A;
1830
1831 if (h2c->st0 == H2_CS_FRAME_A)
1832 ret = h2c_send_rst_stream(h2c, h2s);
1833 }
1834
Willy Tarreau7e98c052017-10-10 15:56:59 +02001835 /* error or missing data condition met above ? */
1836 if (ret <= 0)
1837 break;
1838
1839 if (h2c->st0 != H2_CS_FRAME_H) {
1840 bi_del(h2c->dbuf, h2c->dfl);
1841 h2c->st0 = H2_CS_FRAME_H;
1842 }
1843 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001844
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001845 if (h2c->rcvd_c > 0 &&
1846 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1847 h2c_send_conn_wu(h2c);
1848
Willy Tarreau52eed752017-09-22 15:05:09 +02001849 fail:
1850 /* we can go here on missing data, blocked response or error */
1851 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001852}
1853
1854/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1855 * the end.
1856 */
1857static int h2_process_mux(struct h2c *h2c)
1858{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001859 struct h2s *h2s, *h2s_back;
1860
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001861 /* start by sending possibly pending window updates */
1862 if (h2c->rcvd_c > 0 &&
1863 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
1864 h2c_send_conn_wu(h2c) < 0)
1865 goto fail;
1866
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001867 /* First we always process the flow control list because the streams
1868 * waiting there were already elected for immediate emission but were
1869 * blocked just on this.
1870 */
1871
1872 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
1873 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
1874 h2c->st0 >= H2_CS_ERROR)
1875 break;
1876
1877 /* In theory it's possible that h2s->cs == NULL here :
1878 * - client sends crap that causes a parse error
1879 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1880 * - RST_STREAM cannot be emitted because mux is busy/full
1881 * - stream gets notified, detaches and quits
1882 * - mux buffer gets ready and wakes pending streams up
1883 * - bam!
1884 */
1885 h2s->flags &= ~H2_SF_BLK_ANY;
1886
1887 if (h2s->cs) {
1888 h2s->cs->data_cb->send(h2s->cs);
1889 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001890 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001891 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001892 }
1893
1894 /* depending on callee's blocking reasons, we may queue in send
1895 * list or completely dequeue.
1896 */
1897 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
1898 if (h2s->flags & H2_SF_BLK_ANY) {
1899 LIST_DEL(&h2s->list);
1900 LIST_ADDQ(&h2c->send_list, &h2s->list);
1901 }
1902 else {
1903 LIST_DEL(&h2s->list);
1904 LIST_INIT(&h2s->list);
1905 if (h2s->cs)
1906 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001907 else {
1908 /* just sent the last frame for this orphaned stream */
1909 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001910 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001911 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001912 }
1913 }
1914 }
1915
1916 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
1917 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
1918 break;
1919
1920 /* In theory it's possible that h2s->cs == NULL here :
1921 * - client sends crap that causes a parse error
1922 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1923 * - RST_STREAM cannot be emitted because mux is busy/full
1924 * - stream gets notified, detaches and quits
1925 * - mux buffer gets ready and wakes pending streams up
1926 * - bam!
1927 */
1928 h2s->flags &= ~H2_SF_BLK_ANY;
1929
1930 if (h2s->cs) {
1931 h2s->cs->data_cb->send(h2s->cs);
1932 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001933 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001934 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001935 }
1936 /* depending on callee's blocking reasons, we may queue in fctl
1937 * list or completely dequeue.
1938 */
1939 if (h2s->flags & H2_SF_BLK_MFCTL) {
1940 /* stream hit the connection's flow control */
1941 LIST_DEL(&h2s->list);
1942 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
1943 }
1944 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
1945 LIST_DEL(&h2s->list);
1946 LIST_INIT(&h2s->list);
1947 if (h2s->cs)
1948 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001949 else {
1950 /* just sent the last frame for this orphaned stream */
1951 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001952 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001953 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001954 }
1955 }
1956
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001957 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01001958 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001959 if (h2c->st0 == H2_CS_ERROR) {
1960 if (h2c->max_id >= 0) {
1961 h2c_send_goaway_error(h2c, NULL);
1962 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
1963 return 0;
1964 }
1965
1966 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
1967 }
1968 return 1;
1969 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001970 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02001971}
1972
Willy Tarreau71681172017-10-23 14:39:06 +02001973
Willy Tarreau62f52692017-10-08 23:01:42 +02001974/*********************************************************/
1975/* functions below are I/O callbacks from the connection */
1976/*********************************************************/
1977
1978/* callback called on recv event by the connection handler */
1979static void h2_recv(struct connection *conn)
1980{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001981 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001982 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001983 int max;
1984
1985 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001986 return;
1987
1988 if (h2c->flags & H2_CF_DEM_BLOCK_ANY)
1989 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001990
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001991 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001992 if (!buf) {
1993 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001994 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001995 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001996
Willy Tarreaua2af5122017-10-09 11:56:46 +02001997 /* note: buf->o == 0 */
1998 max = buf->size - buf->i;
1999 if (!max) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002000 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002001 return;
2002 }
2003
2004 conn->xprt->rcv_buf(conn, buf, max);
2005 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002006 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002007
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002008 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002009 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002010 return;
2011 }
2012
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002013 if (buf->i == buf->size)
2014 h2c->flags |= H2_CF_DEM_DFULL;
2015
Willy Tarreaubc933932017-10-09 16:21:43 +02002016 h2_process_demux(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002017
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002018 /* after streams have been processed, we should have made some room */
Willy Tarreau081d4722017-05-16 21:51:05 +02002019 if (h2c->st0 >= H2_CS_ERROR)
2020 buf->i = 0;
2021
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002022 if (buf->i != buf->size)
2023 h2c->flags &= ~H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002024 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002025}
2026
2027/* callback called on send event by the connection handler */
2028static void h2_send(struct connection *conn)
2029{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002030 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002031 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002032
2033 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002034 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002035
2036 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2037 /* a handshake was requested */
2038 return;
2039 }
2040
Willy Tarreaubc933932017-10-09 16:21:43 +02002041 /* This loop is quite simple : it tries to fill as much as it can from
2042 * pending streams into the existing buffer until it's reportedly full
2043 * or the end of send requests is reached. Then it tries to send this
2044 * buffer's contents out, marks it not full if at least one byte could
2045 * be sent, and tries again.
2046 *
2047 * The snd_buf() function normally takes a "flags" argument which may
2048 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2049 * data immediately comes and CO_SFL_STREAMER to indicate that the
2050 * connection is streaming lots of data (used to increase TLS record
2051 * size at the expense of latency). The former can be sent any time
2052 * there's a buffer full flag, as it indicates at least one stream
2053 * attempted to send and failed so there are pending data. An
2054 * alternative would be to set it as long as there's an active stream
2055 * but that would be problematic for ACKs until we have an absolute
2056 * guarantee that all waiters have at least one byte to send. The
2057 * latter should possibly not be set for now.
2058 */
2059
2060 done = 0;
2061 while (!done) {
2062 unsigned int flags = 0;
2063
2064 /* fill as much as we can into the current buffer */
2065 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2066 done = h2_process_mux(h2c);
2067
2068 if (conn->flags & CO_FL_ERROR)
2069 break;
2070
2071 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2072 flags |= CO_SFL_MSG_MORE;
2073
Willy Tarreau319994a2017-11-07 11:03:56 +01002074 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002075 break;
2076
2077 /* wrote at least one byte, the buffer is not full anymore */
2078 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2079 }
2080
Willy Tarreaua2af5122017-10-09 11:56:46 +02002081 if (conn->flags & CO_FL_SOCK_WR_SH) {
2082 /* output closed, nothing to send, clear the buffer to release it */
2083 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002084 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002085}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002086
Willy Tarreau62f52692017-10-08 23:01:42 +02002087/* callback called on any event by the connection handler.
2088 * It applies changes and returns zero, or < 0 if it wants immediate
2089 * destruction of the connection (which normally doesn not happen in h2).
2090 */
2091static int h2_wake(struct connection *conn)
2092{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002093 struct h2c *h2c = conn->mux_ctx;
2094
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002095 /*
2096 * If we received early data, try to wake any stream, just in case
2097 * at least one of them was waiting for the handshake
2098 */
2099 if ((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA | CO_FL_HANDSHAKE)) ==
2100 CO_FL_EARLY_DATA) {
2101 h2_wake_some_streams(h2c, 0, 0);
2102 conn->flags &= ~CO_FL_EARLY_DATA;
2103 }
Willy Tarreau26bd7612017-10-09 16:47:04 +02002104 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002105 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2106 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2107 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002108 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002109
2110 if (eb_is_empty(&h2c->streams_by_id)) {
2111 /* no more stream, kill the connection now */
2112 h2_release(conn);
2113 return -1;
2114 }
2115 else {
2116 /* some streams still there, we need to signal them all and
2117 * wait for their departure.
2118 */
2119 __conn_xprt_stop_recv(conn);
2120 __conn_xprt_stop_send(conn);
2121 return 0;
2122 }
2123 }
2124
2125 if (!h2c->dbuf->i)
2126 h2_release_dbuf(h2c);
2127
2128 /* stop being notified of incoming data if we can't process them */
2129 if (h2c->st0 >= H2_CS_ERROR ||
2130 (h2c->flags & H2_CF_DEM_BLOCK_ANY) || conn_xprt_read0_pending(conn)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002131 __conn_xprt_stop_recv(conn);
2132 }
2133 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002134 __conn_xprt_want_recv(conn);
2135 }
2136
2137 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002138 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2139 (h2c->st0 == H2_CS_ERROR ||
2140 h2c->mbuf->o ||
2141 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2142 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002143 __conn_xprt_want_send(conn);
2144 }
2145 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002146 h2_release_mbuf(h2c);
2147 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002148 }
2149
Willy Tarreau3f133572017-10-31 19:21:06 +01002150 if (h2c->task) {
2151 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002152 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002153 task_queue(h2c->task);
2154 }
2155 else
2156 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002157 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002158 return 0;
2159}
2160
Willy Tarreauea392822017-10-31 10:02:25 +01002161/* Connection timeout management. The principle is that if there's no receipt
2162 * nor sending for a certain amount of time, the connection is closed. If the
2163 * MUX buffer still has lying data or is not allocatable, the connection is
2164 * immediately killed. If it's allocatable and empty, we attempt to send a
2165 * GOAWAY frame.
2166 */
2167static struct task *h2_timeout_task(struct task *t)
2168{
2169 struct h2c *h2c = t->context;
2170 int expired = tick_is_expired(t->expire, now_ms);
2171
2172 if (!expired)
2173 return t;
2174
2175 h2c_error(h2c, H2_ERR_NO_ERROR);
2176 h2_wake_some_streams(h2c, 0, 0);
2177
2178 if (h2c->mbuf->o) {
2179 /* don't even try to send a GOAWAY, the buffer is stuck */
2180 h2c->flags |= H2_CF_GOAWAY_FAILED;
2181 }
2182
2183 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002184 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002185 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2186 h2c->flags |= H2_CF_GOAWAY_FAILED;
2187
2188 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2189 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2190
2191 if (!eb_is_empty(&h2c->streams_by_id))
2192 goto wait;
2193
2194 h2_release(h2c->conn);
2195 return NULL;
2196
2197 wait:
2198 /* the streams have been notified, we must let them finish and close */
2199 h2c->task = NULL;
2200 task_delete(t);
2201 task_free(t);
2202 return NULL;
2203}
2204
2205
Willy Tarreau62f52692017-10-08 23:01:42 +02002206/*******************************************/
2207/* functions below are used by the streams */
2208/*******************************************/
2209
2210/*
2211 * Attach a new stream to a connection
2212 * (Used for outgoing connections)
2213 */
2214static struct conn_stream *h2_attach(struct connection *conn)
2215{
2216 return NULL;
2217}
2218
2219/* callback used to update the mux's polling flags after changing a cs' status.
2220 * The caller (cs_update_mux_polling) will take care of propagating any changes
2221 * to the transport layer.
2222 */
2223static void h2_update_poll(struct conn_stream *cs)
2224{
Willy Tarreau1d393222017-10-17 10:26:19 +02002225 struct h2s *h2s = cs->ctx;
2226
2227 if (!h2s)
2228 return;
2229
Willy Tarreaud7739c82017-10-30 15:38:23 +01002230 /* we may unblock a blocked read */
2231
2232 if (cs->flags & CS_FL_DATA_RD_ENA &&
2233 h2s->h2c->flags & H2_CF_DEM_SFULL && h2s->h2c->dsi == h2s->id) {
2234 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
2235 conn_xprt_want_recv(cs->conn);
2236 }
2237
Willy Tarreau1d393222017-10-17 10:26:19 +02002238 /* Note: the stream and stream-int code doesn't allow us to perform a
2239 * synchronous send() here unfortunately, because this code is called
2240 * as si_update() from the process_stream() context. This means that
2241 * we have to queue the current cs and defer its processing after the
2242 * connection's cs list is processed anyway.
2243 */
2244
2245 if (cs->flags & CS_FL_DATA_WR_ENA) {
2246 if (LIST_ISEMPTY(&h2s->list)) {
2247 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2248 !h2s->h2c->mbuf->o && // not yet subscribed
2249 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2250 conn_xprt_want_send(cs->conn);
2251 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2252 }
2253 }
2254 else if (!LIST_ISEMPTY(&h2s->list)) {
2255 LIST_DEL(&h2s->list);
2256 LIST_INIT(&h2s->list);
2257 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2258 }
2259
2260 /* this can happen from within si_chk_snd() */
2261 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2262 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002263}
2264
2265/*
2266 * Detach the stream from the connection and possibly release the connection.
2267 */
2268static void h2_detach(struct conn_stream *cs)
2269{
Willy Tarreau60935142017-10-16 18:11:19 +02002270 struct h2s *h2s = cs->ctx;
2271 struct h2c *h2c;
2272
2273 cs->ctx = NULL;
2274 if (!h2s)
2275 return;
2276
2277 h2c = h2s->h2c;
2278 h2s->cs = NULL;
2279
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002280 /* this stream may be blocked waiting for some data to leave (possibly
2281 * an ES or RST frame), so orphan it in this case.
2282 */
2283 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2284 return;
2285
Willy Tarreau541dd822017-11-23 18:12:50 +01002286 /* the stream could be in the send list */
2287 LIST_DEL(&h2s->list);
2288
Willy Tarreau45f752e2017-10-30 15:44:59 +01002289 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2290 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2291 /* unblock the connection if it was blocked on this
2292 * stream.
2293 */
2294 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2295 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2296 conn_xprt_want_recv(cs->conn);
2297 conn_xprt_want_send(cs->conn);
2298 }
2299
Willy Tarreau60935142017-10-16 18:11:19 +02002300 if (h2s->by_id.node.leaf_p) {
2301 /* h2s still attached to the h2c */
2302 eb32_delete(&h2s->by_id);
2303
2304 /* We don't want to close right now unless we're removing the
2305 * last stream, and either the connection is in error, or it
2306 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002307 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002308 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002309 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2310 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002311 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002312 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2313 (conn_xprt_read0_pending(h2c->conn) ||
2314 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002315 /* no more stream will come, kill it now */
2316 h2_release(h2c->conn);
2317 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002318 else if (h2c->task) {
2319 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002320 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002321 task_queue(h2c->task);
2322 }
2323 else
2324 h2c->task->expire = TICK_ETERNITY;
2325 }
Willy Tarreau60935142017-10-16 18:11:19 +02002326 }
Willy Tarreaubafbe012017-11-24 17:34:44 +01002327 pool_free(pool_head_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002328}
2329
2330static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2331{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002332 struct h2s *h2s = cs->ctx;
2333
2334 if (!mode)
2335 return;
2336
Willy Tarreau721c9742017-11-07 11:05:42 +01002337 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002338 return;
2339
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002340 /* if no outgoing data was seen on this stream, it means it was
2341 * closed with a "tcp-request content" rule that is normally
2342 * used to kill the connection ASAP (eg: limit abuse). In this
2343 * case we send a goaway to close the connection.
2344 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002345 if (!(h2s->flags & H2_SF_RST_SENT) &&
2346 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2347 return;
2348
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002349 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2350 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2351 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2352 return;
2353
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002354 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2355 conn_xprt_want_send(cs->conn);
2356
2357 h2s->st = H2_SS_CLOSED;
Willy Tarreau62f52692017-10-08 23:01:42 +02002358}
2359
2360static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2361{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002362 struct h2s *h2s = cs->ctx;
2363
Willy Tarreau721c9742017-11-07 11:05:42 +01002364 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002365 return;
2366
Willy Tarreau67434202017-11-06 20:20:51 +01002367 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002368 /* we can cleanly close using an empty data frame only after headers */
2369
2370 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2371 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002372 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002373
2374 if (h2s->st == H2_SS_HREM)
2375 h2s->st = H2_SS_CLOSED;
2376 else
2377 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002378 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002379 /* if no outgoing data was seen on this stream, it means it was
2380 * closed with a "tcp-request content" rule that is normally
2381 * used to kill the connection ASAP (eg: limit abuse). In this
2382 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002383 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002384 if (!(h2s->flags & H2_SF_RST_SENT) &&
2385 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2386 return;
2387
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002388 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2389 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002390 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2391 return;
2392
Willy Tarreau58e32082017-11-07 14:41:09 +01002393 h2s->st = H2_SS_CLOSED;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002394 }
2395
2396 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2397 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002398}
2399
Willy Tarreau13278b42017-10-13 19:23:14 +02002400/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2401 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2402 * proceed. Stream errors are reported in h2s->errcode and connection errors
2403 * in h2c->errcode. The caller must already have checked the frame header and
2404 * ensured that the frame was complete or the buffer full.
2405 */
2406static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2407{
2408 struct h2c *h2c = h2s->h2c;
2409 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002410 struct chunk *tmp = get_trash_chunk();
2411 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002412 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002413 int flen = h2c->dfl;
2414 int outlen = 0;
2415 int wrap;
2416 int try;
2417
2418 if (!h2c->dfl) {
2419 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
2420 return 0;
2421 }
2422
2423 /* if the input buffer wraps, take a temporary copy of it (rare) */
2424 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2425 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002426 copy = alloc_trash_chunk();
2427 if (!copy) {
2428 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2429 goto fail;
2430 }
2431 memcpy(copy->str, h2c->dbuf->p, wrap);
2432 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2433 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002434 }
2435
2436 /* The padlen is the first byte before data, and the padding appears
2437 * after data. padlen+data+padding are included in flen.
2438 */
2439 if (h2c->dff & H2_F_HEADERS_PADDED) {
2440 if (*hdrs >= flen) {
2441 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2442 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002443 return 0;
2444 }
2445 flen -= *hdrs + 1;
2446 hdrs += 1; // skip Pad Length
2447 }
2448
2449 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2450 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
2451 hdrs += 5; // stream dep = 4, weight = 1
2452 flen -= 5;
2453 }
2454
2455 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2456 * don't support this for now and can't even decompress so we have to
2457 * break the connection.
2458 */
2459 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2460 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002461 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002462 }
2463
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002464 /* we can't retry a failed decompression operation so we must be very
2465 * careful not to take any risks. In practice the output buffer is
2466 * always empty except maybe for trailers, so these operations almost
2467 * never happen.
2468 */
2469 if (unlikely(buf->o)) {
2470 /* need to let the output buffer flush and
2471 * mark the buffer for later wake up.
2472 */
2473 goto fail;
2474 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002475
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002476 if (unlikely(buffer_space_wraps(buf))) {
2477 /* it doesn't fit and the buffer is fragmented,
2478 * so let's defragment it and try again.
2479 */
2480 buffer_slow_realign(buf);
2481 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002482
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002483 /* first check if we have some room after p+i */
2484 try = buf->data + buf->size - (buf->p + buf->i);
2485
2486 /* otherwise continue between data and p-o */
2487 if (try <= 0) {
2488 try = buf->p - (buf->data + buf->o);
2489 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002490 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002491 }
2492 if (try > count)
2493 try = count;
2494
2495 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2496 sizeof(list)/sizeof(list[0]), tmp);
2497 if (outlen < 0) {
2498 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2499 goto fail;
2500 }
2501
2502 /* OK now we have our header list in <list> */
2503 outlen = h2_make_h1_request(list, bi_end(buf), try);
2504
2505 if (outlen < 0) {
2506 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2507 goto fail;
2508 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002509
2510 /* now consume the input data */
2511 bi_del(h2c->dbuf, h2c->dfl);
2512 h2c->st0 = H2_CS_FRAME_H;
2513 buf->i += outlen;
2514
2515 /* don't send it before returning data!
2516 * FIXME: should we instead try to send it much later, after the
2517 * response ? This would require that we keep a copy of it in h2s.
2518 */
2519 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2520 h2s->cs->flags |= CS_FL_EOS;
2521 h2s->flags |= H2_SF_ES_RCVD;
2522 }
2523
Willy Tarreau68dd9852017-07-03 14:44:26 +02002524 leave:
2525 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002526 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002527 fail:
2528 outlen = 0;
2529 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002530}
2531
Willy Tarreau454f9052017-10-26 19:40:35 +02002532/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2533 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2534 * in use, a new chunk is emitted for each frame. This is supposed to fit
2535 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2536 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2537 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2538 * parser state is automatically updated. Returns the number of bytes emitted
2539 * if > 0, or 0 if it couldn't proceed. Stream errors are reported in
2540 * h2s->errcode and connection errors in h2c->errcode. The caller must already
2541 * have checked the frame header and ensured that the frame was complete or the
2542 * buffer full. It changes the frame state to FRAME_A once done.
2543 */
2544static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2545{
2546 struct h2c *h2c = h2s->h2c;
2547 int block1, block2;
2548 unsigned int flen = h2c->dfl;
2549 unsigned int padlen = 0;
2550 int offset = 0;
2551
2552 if (h2c->dbuf->i < flen)
2553 return 0;
2554
2555 /* The padlen is the first byte before data, and the padding appears
2556 * after data. padlen+data+padding are included in flen.
2557 */
2558 if (h2c->dff & H2_F_HEADERS_PADDED) {
2559 padlen = *(uint8_t *)bi_ptr(h2c->dbuf);
2560 if (padlen >= flen) {
2561 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2562 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002563 return 0;
2564 }
2565 flen -= padlen + 1;
2566 offset = 1; // skip Pad Length
2567 }
2568
2569 /* does it fit in output buffer or should we wait ? */
2570 if (buf->i + buf->o + flen > buf->size) {
2571 h2c->flags |= H2_CF_DEM_SFULL;
2572 return 0;
2573 }
2574
2575 /* Block1 is the length of the first block before the buffer wraps,
2576 * block2 is the optional second block to reach the end of the frame.
2577 */
2578 block1 = bi_contig_data(h2c->dbuf);
2579 if (block1 > offset + flen)
2580 block1 = offset + flen;
2581 block1 -= offset; // skip Pad Length
2582 block2 = flen - block1;
2583
2584 if (block1)
2585 bi_putblk(buf, b_ptr(h2c->dbuf, offset), block1);
2586
2587 if (block2)
2588 bi_putblk(buf, b_ptr(h2c->dbuf, offset + block1), block2);
2589
2590 /* now mark the input data as consumed (will be deleted from the buffer
2591 * by the caller when seeing FRAME_A after sending the window update).
2592 */
2593 h2c->rcvd_c += h2c->dfl;
2594 h2c->rcvd_s += h2c->dfl; // warning, this can also affect the closed streams!
2595 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2596
2597 /* don't send it before returning data!
2598 * FIXME: should we instead try to send it much later, after the
2599 * response ? This would require that we keep a copy of it in h2s.
2600 */
2601 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2602 h2s->cs->flags |= CS_FL_EOS;
2603 h2s->flags |= H2_SF_ES_RCVD;
2604 }
2605
2606 return flen;
2607}
2608
Willy Tarreau62f52692017-10-08 23:01:42 +02002609/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002610 * Called from the upper layer to get more data, up to <count> bytes. The
2611 * caller is responsible for never asking for more data than what is available
2612 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002613 */
2614static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2615{
Willy Tarreau13278b42017-10-13 19:23:14 +02002616 struct h2s *h2s = cs->ctx;
2617 struct h2c *h2c = h2s->h2c;
2618 int ret = 0;
2619
2620 if (h2c->st0 != H2_CS_FRAME_P)
2621 return 0; // no pre-parsed frame yet
2622
2623 if (h2c->dsi != h2s->id)
2624 return 0; // not for us
2625
2626 if (!h2c->dbuf->size)
2627 return 0; // empty buffer
2628
2629 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2630 return 0; // incomplete input frame
2631
2632 switch (h2c->dft) {
2633 case H2_FT_HEADERS:
2634 ret = h2_frt_decode_headers(h2s, buf, count);
2635 break;
2636
Willy Tarreau454f9052017-10-26 19:40:35 +02002637 case H2_FT_DATA:
2638 ret = h2_frt_transfer_data(h2s, buf, count);
2639 break;
2640
Willy Tarreau13278b42017-10-13 19:23:14 +02002641 default:
2642 ret = 0;
2643 }
2644 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002645}
2646
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002647/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2648 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2649 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2650 * to the number of buffer bytes consumed.
2651 */
2652static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2653{
2654 struct http_hdr list[MAX_HTTP_HDR];
2655 struct h2c *h2c = h2s->h2c;
2656 struct h1m *h1m = &h2s->res;
2657 struct chunk outbuf;
2658 int es_now = 0;
2659 int ret = 0;
2660 int hdr;
2661
2662 if (h2c_mux_busy(h2c, h2s)) {
2663 h2s->flags |= H2_SF_BLK_MBUSY;
2664 return 0;
2665 }
2666
2667 if (!h2_get_mbuf(h2c)) {
2668 h2c->flags |= H2_CF_MUX_MALLOC;
2669 h2s->flags |= H2_SF_BLK_MROOM;
2670 return 0;
2671 }
2672
2673 /* First, try to parse the H1 response and index it into <list>.
2674 * NOTE! Since it comes from haproxy, we *know* that a response header
2675 * block does not wrap and we can safely read it this way without
2676 * having to realign the buffer.
2677 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002678 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002679 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2680 list, sizeof(list)/sizeof(list[0]), h1m);
2681 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002682 /* incomplete or invalid response, this is abnormal coming from
2683 * haproxy and may only result in a bad errorfile or bad Lua code
2684 * so that won't be fixed, raise an error now.
2685 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002686 * FIXME: we should instead add the ability to only return a
2687 * 502 bad gateway. But in theory this is not supposed to
2688 * happen.
2689 */
2690 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2691 ret = 0;
2692 goto end;
2693 }
2694
2695 chunk_reset(&outbuf);
2696
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002697 try_again:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002698 while (1) {
2699 outbuf.str = bo_end(h2c->mbuf);
2700 outbuf.size = bo_contig_space(h2c->mbuf);
2701 outbuf.len = 0;
2702
2703 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2704 break;
2705 realign_again:
2706 buffer_slow_realign(h2c->mbuf);
2707 }
2708
2709 if (outbuf.size < 9) {
2710 h2c->flags |= H2_CF_MUX_MFULL;
2711 h2s->flags |= H2_SF_BLK_MROOM;
2712 ret = 0;
2713 goto end;
2714 }
2715
2716 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2717 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2718 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2719 outbuf.len = 9;
2720
2721 /* encode status, which necessarily is the first one */
2722 if (outbuf.len < outbuf.size && h1m->status == 200)
2723 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2724 else if (outbuf.len < outbuf.size && h1m->status == 304)
2725 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002726 else if (unlikely(list[0].v.len != 3)) {
2727 /* this is an unparsable response */
2728 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2729 ret = 0;
2730 goto end;
2731 }
2732 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002733 /* basic encoding of the status code */
2734 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2735 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2736 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2737 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2738 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2739 }
2740 else {
2741 if (buffer_space_wraps(h2c->mbuf))
2742 goto realign_again;
2743
2744 h2c->flags |= H2_CF_MUX_MFULL;
2745 h2s->flags |= H2_SF_BLK_MROOM;
2746 ret = 0;
2747 goto end;
2748 }
2749
2750 /* encode all headers, stop at empty name */
2751 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002752 /* these ones do not exist in H2 and must be dropped. */
2753 if (isteq(list[hdr].n, ist("connection")) ||
2754 isteq(list[hdr].n, ist("proxy-connection")) ||
2755 isteq(list[hdr].n, ist("keep-alive")) ||
2756 isteq(list[hdr].n, ist("upgrade")) ||
2757 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002758 continue;
2759
2760 if (isteq(list[hdr].n, ist("")))
2761 break; // end
2762
2763 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2764 /* output full */
2765 if (buffer_space_wraps(h2c->mbuf))
2766 goto realign_again;
2767
2768 h2c->flags |= H2_CF_MUX_MFULL;
2769 h2s->flags |= H2_SF_BLK_MROOM;
2770 ret = 0;
2771 goto end;
2772 }
2773 }
2774
2775 /* we may need to add END_STREAM */
2776 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2777 es_now = 1;
2778
2779 /* update the frame's size */
2780 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2781
2782 if (es_now)
2783 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2784
2785 /* consume incoming H1 response */
2786 bo_del(buf, ret);
2787
2788 /* commit the H2 response */
2789 h2c->mbuf->o += outbuf.len;
2790 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01002791 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002792
2793 /* for now we don't implemented CONTINUATION, so we wait for a
2794 * body or directly end in TRL2.
2795 */
2796 if (es_now) {
2797 h1m->state = HTTP_MSG_DONE;
2798 h2s->flags |= H2_SF_ES_SENT;
2799 if (h2s->st == H2_SS_OPEN)
2800 h2s->st = H2_SS_HLOC;
2801 else
2802 h2s->st = H2_SS_CLOSED;
2803 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01002804 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01002805 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002806 h1m->state = HTTP_MSG_RPBEFORE;
2807 h1m->status = 0;
2808 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01002809 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01002810 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002811 else
2812 h1m->state = (h1m->flags & H1_MF_CLEN) ? HTTP_MSG_BODY : HTTP_MSG_CHUNK_SIZE;
2813
2814 end:
2815 //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));
2816 return ret;
2817}
2818
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002819/* Try to send a DATA frame matching HTTP/1 response present in the response
2820 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
2821 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
2822 * corresponds to the number of buffer bytes consumed.
2823 */
2824static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
2825{
2826 struct h2c *h2c = h2s->h2c;
2827 struct h1m *h1m = &h2s->res;
2828 struct chunk outbuf;
2829 int ret = 0;
2830 int total = 0;
2831 int es_now = 0;
2832 int size = 0;
2833 char *blk1, *blk2;
2834 int len1, len2;
2835
2836 if (h2c_mux_busy(h2c, h2s)) {
2837 h2s->flags |= H2_SF_BLK_MBUSY;
2838 goto end;
2839 }
2840
2841 if (!h2_get_mbuf(h2c)) {
2842 h2c->flags |= H2_CF_MUX_MALLOC;
2843 h2s->flags |= H2_SF_BLK_MROOM;
2844 goto end;
2845 }
2846
2847 new_frame:
2848 if (!buf->o)
2849 goto end;
2850
2851 chunk_reset(&outbuf);
2852
2853 while (1) {
2854 outbuf.str = bo_end(h2c->mbuf);
2855 outbuf.size = bo_contig_space(h2c->mbuf);
2856 outbuf.len = 0;
2857
2858 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2859 break;
2860 realign_again:
2861 buffer_slow_realign(h2c->mbuf);
2862 }
2863
2864 if (outbuf.size < 9) {
2865 h2c->flags |= H2_CF_MUX_MFULL;
2866 h2s->flags |= H2_SF_BLK_MROOM;
2867 goto end;
2868 }
2869
2870 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
2871 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
2872 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2873 outbuf.len = 9;
2874
2875 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
2876 case 0: /* no content length, read till SHUTW */
2877 size = buf->o;
2878 break;
2879 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
2880 size = buf->o;
2881 if ((long long)size > h1m->curr_len)
2882 size = h1m->curr_len;
2883 break;
2884 default: /* te:chunked : parse chunks */
2885 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
2886 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
2887 if (!ret)
2888 goto end;
2889
2890 if (ret < 0) {
2891 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2892 h1m->err_pos = ret;
2893 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2894 goto end;
2895 }
2896 bo_del(buf, ret);
2897 total += ret;
2898 h1m->state = HTTP_MSG_CHUNK_SIZE;
2899 }
2900
2901 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
2902 unsigned int chunk;
2903
2904 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
2905 if (!ret)
2906 goto end;
2907
2908 if (ret < 0) {
2909 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2910 h1m->err_pos = ret;
2911 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2912 goto end;
2913 }
2914
2915 size = chunk;
2916 h1m->curr_len = chunk;
2917 h1m->body_len += chunk;
2918 bo_del(buf, ret);
2919 total += ret;
2920 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
2921 if (!size)
2922 goto send_empty;
2923 }
2924
2925 /* in MSG_DATA state, continue below */
2926 size = h1m->curr_len;
2927 break;
2928 }
2929
2930 /* we have in <size> the exact number of bytes we need to copy from
2931 * the H1 buffer. We need to check this against the connection's and
2932 * the stream's send windows, and to ensure that this fits in the max
2933 * frame size and in the buffer's available space minus 9 bytes (for
2934 * the frame header). The connection's flow control is applied last so
2935 * that we can use a separate list of streams which are immediately
2936 * unblocked on window opening. Note: we don't implement padding.
2937 */
2938
2939 if (size > buf->o)
2940 size = buf->o;
2941
2942 if (size > h2s->mws)
2943 size = h2s->mws;
2944
2945 if (size <= 0) {
2946 h2s->flags |= H2_SF_BLK_SFCTL;
2947 goto end;
2948 }
2949
2950 if (h2c->mfs && size > h2c->mfs)
2951 size = h2c->mfs;
2952
2953 if (size + 9 > outbuf.size) {
2954 /* we have an opportunity for enlarging the too small
2955 * available space, let's try.
2956 */
2957 if (buffer_space_wraps(h2c->mbuf))
2958 goto realign_again;
2959 size = outbuf.size - 9;
2960 }
2961
2962 if (size <= 0) {
2963 h2c->flags |= H2_CF_MUX_MFULL;
2964 h2s->flags |= H2_SF_BLK_MROOM;
2965 goto end;
2966 }
2967
2968 if (size > h2c->mws)
2969 size = h2c->mws;
2970
2971 if (size <= 0) {
2972 h2s->flags |= H2_SF_BLK_MFCTL;
2973 goto end;
2974 }
2975
2976 /* copy whatever we can */
2977 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
2978 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
2979 if (ret == 1)
2980 len2 = 0;
2981
2982 if (!ret || len1 + len2 < size) {
2983 /* FIXME: must normally never happen */
2984 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2985 goto end;
2986 }
2987
2988 /* limit len1/len2 to size */
2989 if (len1 + len2 > size) {
2990 int sub = len1 + len2 - size;
2991
2992 if (len2 > sub)
2993 len2 -= sub;
2994 else {
2995 sub -= len2;
2996 len2 = 0;
2997 len1 -= sub;
2998 }
2999 }
3000
3001 /* now let's copy this this into the output buffer */
3002 memcpy(outbuf.str + 9, blk1, len1);
3003 if (len2)
3004 memcpy(outbuf.str + 9 + len1, blk2, len2);
3005
3006 send_empty:
3007 /* we may need to add END_STREAM */
3008 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3009 * could rely on the MSG_MORE flag as a hint for this ?
3010 */
3011 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3012 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3013 es_now = 1;
3014
3015 /* update the frame's size */
3016 h2_set_frame_size(outbuf.str, size);
3017
3018 if (es_now)
3019 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3020
3021 /* commit the H2 response */
3022 h2c->mbuf->o += size + 9;
3023 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3024
3025 /* consume incoming H1 response */
3026 if (size > 0) {
3027 bo_del(buf, size);
3028 total += size;
3029 h1m->curr_len -= size;
3030 h2s->mws -= size;
3031 h2c->mws -= size;
3032
3033 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3034 h1m->state = HTTP_MSG_CHUNK_CRLF;
3035 goto new_frame;
3036 }
3037 }
3038
3039 if (es_now) {
3040 if (h2s->st == H2_SS_OPEN)
3041 h2s->st = H2_SS_HLOC;
3042 else
3043 h2s->st = H2_SS_CLOSED;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003044
3045 if (!(h1m->flags & H1_MF_CHNK))
3046 h1m->state = HTTP_MSG_DONE;
3047
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003048 h2s->flags |= H2_SF_ES_SENT;
3049 }
3050
3051 end:
3052 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%d in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) buf->o=%d", h2c->st0, h2s->id, size+9, total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, buf->o);
3053 return total;
3054}
3055
Willy Tarreau62f52692017-10-08 23:01:42 +02003056/* Called from the upper layer, to send data */
3057static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3058{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003059 struct h2s *h2s = cs->ctx;
3060 int total = 0;
3061
Willy Tarreauc4312d32017-11-07 12:01:53 +01003062 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3063 h2s->flags |= H2_SF_OUTGOING_DATA;
3064
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003065 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3066 if (h2s->res.state < HTTP_MSG_BODY) {
3067 total += h2s_frt_make_resp_headers(h2s, buf);
3068
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003069 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003070 break;
3071
3072 if (h2s->flags & H2_SF_BLK_ANY)
3073 break;
3074 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003075 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3076 total += h2s_frt_make_resp_data(h2s, buf);
3077
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003078 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003079 break;
3080
3081 if (h2s->flags & H2_SF_BLK_ANY)
3082 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003083 }
3084 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3085 /* consume the trailers if any (we don't forward them for now) */
3086 int count = h1_measure_trailers(buf);
3087
3088 if (unlikely(count <= 0)) {
3089 if (count < 0)
3090 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3091 break;
3092 }
3093 total += count;
3094 bo_del(buf, count);
3095 h2s->res.state = HTTP_MSG_DONE;
3096 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003097 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003098 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003099 cs->flags |= CS_FL_ERROR;
3100 break;
3101 }
3102 }
3103
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003104 /* RST are sent similarly to frame acks */
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003105 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003106 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003107 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003108 h2s->st = H2_SS_CLOSED;
3109 }
3110
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003111 if (h2s->flags & H2_SF_BLK_SFCTL) {
3112 /* stream flow control, quit the list */
3113 LIST_DEL(&h2s->list);
3114 LIST_INIT(&h2s->list);
3115 }
3116
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003117 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003118}
3119
3120
3121/*******************************************************/
3122/* functions below are dedicated to the config parsers */
3123/*******************************************************/
3124
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003125/* config parser for global "tune.h2.header-table-size" */
3126static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3127 struct proxy *defpx, const char *file, int line,
3128 char **err)
3129{
3130 if (too_many_args(1, args, err, NULL))
3131 return -1;
3132
3133 h2_settings_header_table_size = atoi(args[1]);
3134 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3135 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3136 return -1;
3137 }
3138 return 0;
3139}
Willy Tarreau62f52692017-10-08 23:01:42 +02003140
Willy Tarreaue6baec02017-07-27 11:45:11 +02003141/* config parser for global "tune.h2.initial-window-size" */
3142static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3143 struct proxy *defpx, const char *file, int line,
3144 char **err)
3145{
3146 if (too_many_args(1, args, err, NULL))
3147 return -1;
3148
3149 h2_settings_initial_window_size = atoi(args[1]);
3150 if (h2_settings_initial_window_size < 0) {
3151 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3152 return -1;
3153 }
3154 return 0;
3155}
3156
Willy Tarreau5242ef82017-07-27 11:47:28 +02003157/* config parser for global "tune.h2.max-concurrent-streams" */
3158static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3159 struct proxy *defpx, const char *file, int line,
3160 char **err)
3161{
3162 if (too_many_args(1, args, err, NULL))
3163 return -1;
3164
3165 h2_settings_max_concurrent_streams = atoi(args[1]);
3166 if (h2_settings_max_concurrent_streams < 0) {
3167 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3168 return -1;
3169 }
3170 return 0;
3171}
3172
Willy Tarreau62f52692017-10-08 23:01:42 +02003173
3174/****************************************/
3175/* MUX initialization and instanciation */
3176/***************************************/
3177
3178/* The mux operations */
3179const struct mux_ops h2_ops = {
3180 .init = h2_init,
3181 .recv = h2_recv,
3182 .send = h2_send,
3183 .wake = h2_wake,
3184 .update_poll = h2_update_poll,
3185 .rcv_buf = h2_rcv_buf,
3186 .snd_buf = h2_snd_buf,
3187 .attach = h2_attach,
3188 .detach = h2_detach,
3189 .shutr = h2_shutr,
3190 .shutw = h2_shutw,
Willy Tarreau62f52692017-10-08 23:01:42 +02003191 .name = "H2",
3192};
3193
3194/* ALPN selection : this mux registers ALPN tolen "h2" */
3195static struct alpn_mux_list alpn_mux_h2 =
3196 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3197
3198/* config keyword parsers */
3199static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003200 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003201 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003202 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003203 { 0, NULL, NULL }
3204}};
3205
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003206static void __h2_deinit(void)
3207{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003208 pool_destroy(pool_head_h2s);
3209 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003210}
3211
Willy Tarreau62f52692017-10-08 23:01:42 +02003212__attribute__((constructor))
3213static void __h2_init(void)
3214{
3215 alpn_register_mux(&alpn_mux_h2);
3216 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003217 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003218 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3219 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003220}