blob: 6857c8d720d9b4da9313ea6c68a4c9459dd1f6c2 [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 */
1162 if (!(h2c->dft & H2_F_PING_ACK))
1163 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);
1402 return 1;
1403
1404 conn_err:
1405 h2c_error(h2c, error);
1406 return 0;
1407}
1408
Willy Tarreaucd234e92017-08-18 10:59:39 +02001409/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1410 * Returns > 0 on success or zero on missing data. It may return an error in
1411 * h2c. Described in RFC7540#6.4.
1412 */
1413static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1414{
1415 int error;
1416
1417 if (h2c->dsi == 0) {
1418 error = H2_ERR_PROTOCOL_ERROR;
1419 goto conn_err;
1420 }
1421
Willy Tarreaucd234e92017-08-18 10:59:39 +02001422 if (h2c->dfl != 4) {
1423 error = H2_ERR_FRAME_SIZE_ERROR;
1424 goto conn_err;
1425 }
1426
1427 /* process full frame only */
1428 if (h2c->dbuf->i < h2c->dfl)
1429 return 0;
1430
1431 /* late RST, already handled */
1432 if (h2s->st == H2_SS_CLOSED)
1433 return 1;
1434
1435 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
1436 h2s->st = H2_SS_CLOSED;
1437
1438 if (h2s->cs) {
1439 h2s->cs->flags |= CS_FL_EOS;
1440 /* recv is used to force to detect CS_FL_EOS that wake()
1441 * doesn't handle in the stream-int code.
1442 */
1443 h2s->cs->data_cb->recv(h2s->cs);
1444 h2s->cs->data_cb->wake(h2s->cs);
1445 }
1446
1447 h2s->flags |= H2_SF_RST_RCVD;
1448 return 1;
1449
1450 conn_err:
1451 h2c_error(h2c, error);
1452 return 0;
1453}
1454
Willy Tarreau13278b42017-10-13 19:23:14 +02001455/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1456 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1457 * errors here are reported as connection errors since it's impossible to
1458 * recover from such errors after the compression context has been altered.
1459 */
1460static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1461{
1462 int error;
1463
1464 if (!h2c->dfl) {
1465 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1466 goto strm_err;
1467 }
1468
1469 if (!h2c->dbuf->size)
1470 return 0; // empty buffer
1471
1472 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1473 return 0; // incomplete frame
1474
1475 /* now either the frame is complete or the buffer is complete */
1476 if (h2s->st != H2_SS_IDLE) {
1477 /* FIXME: stream already exists, this is only allowed for
1478 * trailers (not supported for now).
1479 */
1480 error = H2_ERR_PROTOCOL_ERROR;
1481 goto conn_err;
1482 }
1483 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1484 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1485 error = H2_ERR_PROTOCOL_ERROR;
1486 goto conn_err;
1487 }
1488
1489 h2s = h2c_stream_new(h2c, h2c->dsi);
1490 if (!h2s) {
1491 error = H2_ERR_INTERNAL_ERROR;
1492 goto conn_err;
1493 }
1494
1495 h2s->st = H2_SS_OPEN;
1496 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1497 h2s->st = H2_SS_HREM;
1498 h2s->flags |= H2_SF_ES_RCVD;
1499 }
1500
1501 /* call the upper layers to process the frame, then let the upper layer
1502 * notify the stream about any change.
1503 */
1504 h2s->cs->data_cb->recv(h2s->cs);
1505
1506 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1507 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1508 error = H2_ERR_INTERNAL_ERROR;
1509 goto conn_err;
1510 }
1511
Willy Tarreau8f650c32017-11-21 19:36:21 +01001512 if (h2c->st0 >= H2_CS_ERROR)
1513 return 0;
1514
Willy Tarreau721c9742017-11-07 11:05:42 +01001515 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001516 /* stream error : send RST_STREAM */
1517 h2c->st0 = H2_CS_FRAME_A;
1518 }
1519 else {
1520 /* update the max stream ID if the request is being processed */
1521 if (h2s->id > h2c->max_id)
1522 h2c->max_id = h2s->id;
1523 }
1524
1525 return 1;
1526
1527 conn_err:
1528 h2c_error(h2c, error);
1529 return 0;
1530
1531 strm_err:
1532 if (h2s) {
1533 h2s_error(h2s, error);
1534 h2c->st0 = H2_CS_FRAME_A;
1535 }
1536 else
1537 h2c_error(h2c, error);
1538 return 0;
1539}
1540
Willy Tarreau454f9052017-10-26 19:40:35 +02001541/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1542 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1543 */
1544static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1545{
1546 int error;
1547
1548 /* note that empty DATA frames are perfectly valid and sometimes used
1549 * to signal an end of stream (with the ES flag).
1550 */
1551
1552 if (!h2c->dbuf->size && h2c->dfl)
1553 return 0; // empty buffer
1554
1555 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1556 return 0; // incomplete frame
1557
1558 /* now either the frame is complete or the buffer is complete */
1559
1560 if (!h2c->dsi) {
1561 /* RFC7540#6.1 */
1562 error = H2_ERR_PROTOCOL_ERROR;
1563 goto conn_err;
1564 }
1565
1566 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1567 /* RFC7540#6.1 */
1568 error = H2_ERR_STREAM_CLOSED;
1569 goto strm_err;
1570 }
1571
1572 /* last frame */
1573 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1574 h2s->st = H2_SS_HREM;
1575 h2s->flags |= H2_SF_ES_RCVD;
1576 }
1577
1578 /* call the upper layers to process the frame, then let the upper layer
1579 * notify the stream about any change.
1580 */
1581 if (!h2s->cs) {
1582 error = H2_ERR_STREAM_CLOSED;
1583 goto strm_err;
1584 }
1585
1586 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001587
Willy Tarreau454f9052017-10-26 19:40:35 +02001588 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1589 /* cs has just been destroyed, we have to kill h2s. */
1590 error = H2_ERR_STREAM_CLOSED;
1591 goto strm_err;
1592 }
1593
Willy Tarreau8f650c32017-11-21 19:36:21 +01001594 if (h2c->st0 >= H2_CS_ERROR)
1595 return 0;
1596
Willy Tarreau721c9742017-11-07 11:05:42 +01001597 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001598 /* stream error : send RST_STREAM */
1599 h2c->st0 = H2_CS_FRAME_A;
1600 }
1601
1602 /* check for completion : the callee will change this to FRAME_A or
1603 * FRAME_H once done.
1604 */
1605 if (h2c->st0 == H2_CS_FRAME_P)
1606 return 0;
1607
1608 return 1;
1609
1610 conn_err:
1611 h2c_error(h2c, error);
1612 return 0;
1613
1614 strm_err:
1615 if (h2s) {
1616 h2s_error(h2s, error);
1617 h2c->st0 = H2_CS_FRAME_A;
1618 }
1619 else
1620 h2c_error(h2c, error);
1621 return 0;
1622}
1623
Willy Tarreaubc933932017-10-09 16:21:43 +02001624/* process Rx frames to be demultiplexed */
1625static void h2_process_demux(struct h2c *h2c)
1626{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001627 struct h2s *h2s;
1628
Willy Tarreau081d4722017-05-16 21:51:05 +02001629 if (h2c->st0 >= H2_CS_ERROR)
1630 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001631
1632 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1633 if (h2c->st0 == H2_CS_PREFACE) {
1634 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1635 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1636 if (h2c->st0 == H2_CS_ERROR)
1637 h2c->st0 = H2_CS_ERROR2;
1638 goto fail;
1639 }
1640
1641 h2c->max_id = 0;
1642 h2c->st0 = H2_CS_SETTINGS1;
1643 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001644
1645 if (h2c->st0 == H2_CS_SETTINGS1) {
1646 struct h2_fh hdr;
1647
1648 /* ensure that what is pending is a valid SETTINGS frame
1649 * without an ACK.
1650 */
1651 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1652 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1653 if (h2c->st0 == H2_CS_ERROR)
1654 h2c->st0 = H2_CS_ERROR2;
1655 goto fail;
1656 }
1657
1658 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1659 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1660 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1661 h2c->st0 = H2_CS_ERROR2;
1662 goto fail;
1663 }
1664
1665 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1666 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1667 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1668 h2c->st0 = H2_CS_ERROR2;
1669 goto fail;
1670 }
1671
1672 /* that's OK, switch to FRAME_P to process it */
1673 h2c->dfl = hdr.len;
1674 h2c->dsi = hdr.sid;
1675 h2c->dft = hdr.ft;
1676 h2c->dff = hdr.ff;
1677 h2c->st0 = H2_CS_FRAME_P;
1678 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001679 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001680
1681 /* process as many incoming frames as possible below */
1682 while (h2c->dbuf->i) {
1683 int ret = 0;
1684
1685 if (h2c->st0 >= H2_CS_ERROR)
1686 break;
1687
1688 if (h2c->st0 == H2_CS_FRAME_H) {
1689 struct h2_fh hdr;
1690
1691 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1692 break;
1693
1694 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1695 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1696 h2c->st0 = H2_CS_ERROR;
1697 break;
1698 }
1699
1700 h2c->dfl = hdr.len;
1701 h2c->dsi = hdr.sid;
1702 h2c->dft = hdr.ft;
1703 h2c->dff = hdr.ff;
1704 h2c->st0 = H2_CS_FRAME_P;
1705 h2_skip_frame_hdr(h2c->dbuf);
1706 }
1707
1708 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001709 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001710
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001711 if (h2s->st == H2_SS_IDLE &&
1712 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1713 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1714 * this state MUST be treated as a connection error
1715 */
1716 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1717 h2c->st0 = H2_CS_ERROR;
1718 break;
1719 }
1720
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001721 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1722 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1723 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1724 * this state MUST be treated as a stream error
1725 */
1726 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1727 goto strm_err;
1728 }
1729
Willy Tarreauc0da1962017-10-30 18:38:00 +01001730#if 0
1731 // problem below: it is not possible to completely ignore such
1732 // streams as we need to maintain the compression state as well
1733 // and for this we need to completely process these frames (eg:
1734 // HEADERS frames) as well as counting DATA frames to emit
1735 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1736 // This is a typical case of layer violation where the
1737 // transported contents are critical to the connection's
1738 // validity and must be ignored at the same time :-(
1739
1740 /* graceful shutdown, ignore streams whose ID is higher than
1741 * the one advertised in GOAWAY. RFC7540#6.8.
1742 */
1743 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1744 ret = MIN(h2c->dbuf->i, h2c->dfl);
1745 bi_del(h2c->dbuf, ret);
1746 h2c->dfl -= ret;
1747 ret = h2c->dfl == 0;
1748 goto strm_err;
1749 }
1750#endif
1751
Willy Tarreau7e98c052017-10-10 15:56:59 +02001752 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001753 case H2_FT_SETTINGS:
1754 if (h2c->st0 == H2_CS_FRAME_P)
1755 ret = h2c_handle_settings(h2c);
1756
1757 if (h2c->st0 == H2_CS_FRAME_A)
1758 ret = h2c_ack_settings(h2c);
1759 break;
1760
Willy Tarreaucf68c782017-10-10 17:11:41 +02001761 case H2_FT_PING:
1762 if (h2c->st0 == H2_CS_FRAME_P)
1763 ret = h2c_handle_ping(h2c);
1764
1765 if (h2c->st0 == H2_CS_FRAME_A)
1766 ret = h2c_ack_ping(h2c);
1767 break;
1768
Willy Tarreau26f95952017-07-27 17:18:30 +02001769 case H2_FT_WINDOW_UPDATE:
1770 if (h2c->st0 == H2_CS_FRAME_P)
1771 ret = h2c_handle_window_update(h2c, h2s);
1772 break;
1773
Willy Tarreau61290ec2017-10-17 08:19:21 +02001774 case H2_FT_CONTINUATION:
1775 /* we currently don't support CONTINUATION frames since
1776 * we have nowhere to store the partial HEADERS frame.
1777 * Let's abort the stream on an INTERNAL_ERROR here.
1778 */
1779 if (h2c->st0 == H2_CS_FRAME_P)
1780 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1781 break;
1782
Willy Tarreau13278b42017-10-13 19:23:14 +02001783 case H2_FT_HEADERS:
1784 if (h2c->st0 == H2_CS_FRAME_P)
1785 ret = h2c_frt_handle_headers(h2c, h2s);
1786 break;
1787
Willy Tarreau454f9052017-10-26 19:40:35 +02001788 case H2_FT_DATA:
1789 if (h2c->st0 == H2_CS_FRAME_P)
1790 ret = h2c_frt_handle_data(h2c, h2s);
1791
1792 if (h2c->st0 == H2_CS_FRAME_A)
1793 ret = h2c_send_strm_wu(h2c);
1794 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001795
1796 case H2_FT_RST_STREAM:
1797 if (h2c->st0 == H2_CS_FRAME_P)
1798 ret = h2c_handle_rst_stream(h2c, h2s);
1799 break;
1800
Willy Tarreaue96b0922017-10-30 00:28:29 +01001801 case H2_FT_GOAWAY:
1802 if (h2c->st0 == H2_CS_FRAME_P)
1803 ret = h2c_handle_goaway(h2c);
1804 break;
1805
Willy Tarreau1c661982017-10-30 13:52:01 +01001806 case H2_FT_PUSH_PROMISE:
1807 /* not permitted here, RFC7540#5.1 */
1808 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001809 break;
1810
1811 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001812 default:
1813 /* drop frames that we ignore. They may be larger than
1814 * the buffer so we drain all of their contents until
1815 * we reach the end.
1816 */
1817 ret = MIN(h2c->dbuf->i, h2c->dfl);
1818 bi_del(h2c->dbuf, ret);
1819 h2c->dfl -= ret;
1820 ret = h2c->dfl == 0;
1821 }
1822
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001823 strm_err:
Willy Tarreau27a84c92017-10-17 08:10:17 +02001824 /* RST are sent similarly to frame acks */
1825 if (h2s->st == H2_SS_ERROR) {
1826 if (h2c->st0 == H2_CS_FRAME_P)
1827 h2c->st0 = H2_CS_FRAME_A;
1828
1829 if (h2c->st0 == H2_CS_FRAME_A)
1830 ret = h2c_send_rst_stream(h2c, h2s);
1831 }
1832
Willy Tarreau7e98c052017-10-10 15:56:59 +02001833 /* error or missing data condition met above ? */
1834 if (ret <= 0)
1835 break;
1836
1837 if (h2c->st0 != H2_CS_FRAME_H) {
1838 bi_del(h2c->dbuf, h2c->dfl);
1839 h2c->st0 = H2_CS_FRAME_H;
1840 }
1841 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001842
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001843 if (h2c->rcvd_c > 0 &&
1844 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1845 h2c_send_conn_wu(h2c);
1846
Willy Tarreau52eed752017-09-22 15:05:09 +02001847 fail:
1848 /* we can go here on missing data, blocked response or error */
1849 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001850}
1851
1852/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1853 * the end.
1854 */
1855static int h2_process_mux(struct h2c *h2c)
1856{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001857 struct h2s *h2s, *h2s_back;
1858
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001859 /* start by sending possibly pending window updates */
1860 if (h2c->rcvd_c > 0 &&
1861 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
1862 h2c_send_conn_wu(h2c) < 0)
1863 goto fail;
1864
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001865 /* First we always process the flow control list because the streams
1866 * waiting there were already elected for immediate emission but were
1867 * blocked just on this.
1868 */
1869
1870 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
1871 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
1872 h2c->st0 >= H2_CS_ERROR)
1873 break;
1874
1875 /* In theory it's possible that h2s->cs == NULL here :
1876 * - client sends crap that causes a parse error
1877 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1878 * - RST_STREAM cannot be emitted because mux is busy/full
1879 * - stream gets notified, detaches and quits
1880 * - mux buffer gets ready and wakes pending streams up
1881 * - bam!
1882 */
1883 h2s->flags &= ~H2_SF_BLK_ANY;
1884
1885 if (h2s->cs) {
1886 h2s->cs->data_cb->send(h2s->cs);
1887 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001888 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001889 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001890 }
1891
1892 /* depending on callee's blocking reasons, we may queue in send
1893 * list or completely dequeue.
1894 */
1895 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
1896 if (h2s->flags & H2_SF_BLK_ANY) {
1897 LIST_DEL(&h2s->list);
1898 LIST_ADDQ(&h2c->send_list, &h2s->list);
1899 }
1900 else {
1901 LIST_DEL(&h2s->list);
1902 LIST_INIT(&h2s->list);
1903 if (h2s->cs)
1904 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001905 else {
1906 /* just sent the last frame for this orphaned stream */
1907 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001908 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001909 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001910 }
1911 }
1912 }
1913
1914 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
1915 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
1916 break;
1917
1918 /* In theory it's possible that h2s->cs == NULL here :
1919 * - client sends crap that causes a parse error
1920 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1921 * - RST_STREAM cannot be emitted because mux is busy/full
1922 * - stream gets notified, detaches and quits
1923 * - mux buffer gets ready and wakes pending streams up
1924 * - bam!
1925 */
1926 h2s->flags &= ~H2_SF_BLK_ANY;
1927
1928 if (h2s->cs) {
1929 h2s->cs->data_cb->send(h2s->cs);
1930 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001931 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001932 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001933 }
1934 /* depending on callee's blocking reasons, we may queue in fctl
1935 * list or completely dequeue.
1936 */
1937 if (h2s->flags & H2_SF_BLK_MFCTL) {
1938 /* stream hit the connection's flow control */
1939 LIST_DEL(&h2s->list);
1940 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
1941 }
1942 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
1943 LIST_DEL(&h2s->list);
1944 LIST_INIT(&h2s->list);
1945 if (h2s->cs)
1946 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001947 else {
1948 /* just sent the last frame for this orphaned stream */
1949 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001950 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001951 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001952 }
1953 }
1954
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001955 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01001956 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001957 if (h2c->st0 == H2_CS_ERROR) {
1958 if (h2c->max_id >= 0) {
1959 h2c_send_goaway_error(h2c, NULL);
1960 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
1961 return 0;
1962 }
1963
1964 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
1965 }
1966 return 1;
1967 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001968 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02001969}
1970
Willy Tarreau71681172017-10-23 14:39:06 +02001971
Willy Tarreau62f52692017-10-08 23:01:42 +02001972/*********************************************************/
1973/* functions below are I/O callbacks from the connection */
1974/*********************************************************/
1975
1976/* callback called on recv event by the connection handler */
1977static void h2_recv(struct connection *conn)
1978{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001979 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001980 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001981 int max;
1982
1983 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001984 return;
1985
1986 if (h2c->flags & H2_CF_DEM_BLOCK_ANY)
1987 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001988
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001989 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001990 if (!buf) {
1991 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001992 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001993 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001994
Willy Tarreaua2af5122017-10-09 11:56:46 +02001995 /* note: buf->o == 0 */
1996 max = buf->size - buf->i;
1997 if (!max) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001998 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001999 return;
2000 }
2001
2002 conn->xprt->rcv_buf(conn, buf, max);
2003 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002004 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002005
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002006 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002007 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002008 return;
2009 }
2010
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002011 if (buf->i == buf->size)
2012 h2c->flags |= H2_CF_DEM_DFULL;
2013
Willy Tarreaubc933932017-10-09 16:21:43 +02002014 h2_process_demux(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002015
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002016 /* after streams have been processed, we should have made some room */
Willy Tarreau081d4722017-05-16 21:51:05 +02002017 if (h2c->st0 >= H2_CS_ERROR)
2018 buf->i = 0;
2019
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002020 if (buf->i != buf->size)
2021 h2c->flags &= ~H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002022 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002023}
2024
2025/* callback called on send event by the connection handler */
2026static void h2_send(struct connection *conn)
2027{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002028 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002029 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002030
2031 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002032 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002033
2034 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2035 /* a handshake was requested */
2036 return;
2037 }
2038
Willy Tarreaubc933932017-10-09 16:21:43 +02002039 /* This loop is quite simple : it tries to fill as much as it can from
2040 * pending streams into the existing buffer until it's reportedly full
2041 * or the end of send requests is reached. Then it tries to send this
2042 * buffer's contents out, marks it not full if at least one byte could
2043 * be sent, and tries again.
2044 *
2045 * The snd_buf() function normally takes a "flags" argument which may
2046 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2047 * data immediately comes and CO_SFL_STREAMER to indicate that the
2048 * connection is streaming lots of data (used to increase TLS record
2049 * size at the expense of latency). The former can be sent any time
2050 * there's a buffer full flag, as it indicates at least one stream
2051 * attempted to send and failed so there are pending data. An
2052 * alternative would be to set it as long as there's an active stream
2053 * but that would be problematic for ACKs until we have an absolute
2054 * guarantee that all waiters have at least one byte to send. The
2055 * latter should possibly not be set for now.
2056 */
2057
2058 done = 0;
2059 while (!done) {
2060 unsigned int flags = 0;
2061
2062 /* fill as much as we can into the current buffer */
2063 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2064 done = h2_process_mux(h2c);
2065
2066 if (conn->flags & CO_FL_ERROR)
2067 break;
2068
2069 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2070 flags |= CO_SFL_MSG_MORE;
2071
Willy Tarreau319994a2017-11-07 11:03:56 +01002072 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002073 break;
2074
2075 /* wrote at least one byte, the buffer is not full anymore */
2076 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2077 }
2078
Willy Tarreaua2af5122017-10-09 11:56:46 +02002079 if (conn->flags & CO_FL_SOCK_WR_SH) {
2080 /* output closed, nothing to send, clear the buffer to release it */
2081 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002082 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002083}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002084
Willy Tarreau62f52692017-10-08 23:01:42 +02002085/* callback called on any event by the connection handler.
2086 * It applies changes and returns zero, or < 0 if it wants immediate
2087 * destruction of the connection (which normally doesn not happen in h2).
2088 */
2089static int h2_wake(struct connection *conn)
2090{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002091 struct h2c *h2c = conn->mux_ctx;
2092
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002093 /*
2094 * If we received early data, try to wake any stream, just in case
2095 * at least one of them was waiting for the handshake
2096 */
2097 if ((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA | CO_FL_HANDSHAKE)) ==
2098 CO_FL_EARLY_DATA) {
2099 h2_wake_some_streams(h2c, 0, 0);
2100 conn->flags &= ~CO_FL_EARLY_DATA;
2101 }
Willy Tarreau26bd7612017-10-09 16:47:04 +02002102 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002103 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2104 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2105 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002106 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002107
2108 if (eb_is_empty(&h2c->streams_by_id)) {
2109 /* no more stream, kill the connection now */
2110 h2_release(conn);
2111 return -1;
2112 }
2113 else {
2114 /* some streams still there, we need to signal them all and
2115 * wait for their departure.
2116 */
2117 __conn_xprt_stop_recv(conn);
2118 __conn_xprt_stop_send(conn);
2119 return 0;
2120 }
2121 }
2122
2123 if (!h2c->dbuf->i)
2124 h2_release_dbuf(h2c);
2125
2126 /* stop being notified of incoming data if we can't process them */
2127 if (h2c->st0 >= H2_CS_ERROR ||
2128 (h2c->flags & H2_CF_DEM_BLOCK_ANY) || conn_xprt_read0_pending(conn)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002129 __conn_xprt_stop_recv(conn);
2130 }
2131 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002132 __conn_xprt_want_recv(conn);
2133 }
2134
2135 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002136 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2137 (h2c->st0 == H2_CS_ERROR ||
2138 h2c->mbuf->o ||
2139 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2140 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002141 __conn_xprt_want_send(conn);
2142 }
2143 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002144 h2_release_mbuf(h2c);
2145 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002146 }
2147
Willy Tarreau3f133572017-10-31 19:21:06 +01002148 if (h2c->task) {
2149 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002150 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002151 task_queue(h2c->task);
2152 }
2153 else
2154 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002155 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002156 return 0;
2157}
2158
Willy Tarreauea392822017-10-31 10:02:25 +01002159/* Connection timeout management. The principle is that if there's no receipt
2160 * nor sending for a certain amount of time, the connection is closed. If the
2161 * MUX buffer still has lying data or is not allocatable, the connection is
2162 * immediately killed. If it's allocatable and empty, we attempt to send a
2163 * GOAWAY frame.
2164 */
2165static struct task *h2_timeout_task(struct task *t)
2166{
2167 struct h2c *h2c = t->context;
2168 int expired = tick_is_expired(t->expire, now_ms);
2169
2170 if (!expired)
2171 return t;
2172
2173 h2c_error(h2c, H2_ERR_NO_ERROR);
2174 h2_wake_some_streams(h2c, 0, 0);
2175
2176 if (h2c->mbuf->o) {
2177 /* don't even try to send a GOAWAY, the buffer is stuck */
2178 h2c->flags |= H2_CF_GOAWAY_FAILED;
2179 }
2180
2181 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002182 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002183 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2184 h2c->flags |= H2_CF_GOAWAY_FAILED;
2185
2186 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2187 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2188
2189 if (!eb_is_empty(&h2c->streams_by_id))
2190 goto wait;
2191
2192 h2_release(h2c->conn);
2193 return NULL;
2194
2195 wait:
2196 /* the streams have been notified, we must let them finish and close */
2197 h2c->task = NULL;
2198 task_delete(t);
2199 task_free(t);
2200 return NULL;
2201}
2202
2203
Willy Tarreau62f52692017-10-08 23:01:42 +02002204/*******************************************/
2205/* functions below are used by the streams */
2206/*******************************************/
2207
2208/*
2209 * Attach a new stream to a connection
2210 * (Used for outgoing connections)
2211 */
2212static struct conn_stream *h2_attach(struct connection *conn)
2213{
2214 return NULL;
2215}
2216
2217/* callback used to update the mux's polling flags after changing a cs' status.
2218 * The caller (cs_update_mux_polling) will take care of propagating any changes
2219 * to the transport layer.
2220 */
2221static void h2_update_poll(struct conn_stream *cs)
2222{
Willy Tarreau1d393222017-10-17 10:26:19 +02002223 struct h2s *h2s = cs->ctx;
2224
2225 if (!h2s)
2226 return;
2227
Willy Tarreaud7739c82017-10-30 15:38:23 +01002228 /* we may unblock a blocked read */
2229
2230 if (cs->flags & CS_FL_DATA_RD_ENA &&
2231 h2s->h2c->flags & H2_CF_DEM_SFULL && h2s->h2c->dsi == h2s->id) {
2232 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
2233 conn_xprt_want_recv(cs->conn);
2234 }
2235
Willy Tarreau1d393222017-10-17 10:26:19 +02002236 /* Note: the stream and stream-int code doesn't allow us to perform a
2237 * synchronous send() here unfortunately, because this code is called
2238 * as si_update() from the process_stream() context. This means that
2239 * we have to queue the current cs and defer its processing after the
2240 * connection's cs list is processed anyway.
2241 */
2242
2243 if (cs->flags & CS_FL_DATA_WR_ENA) {
2244 if (LIST_ISEMPTY(&h2s->list)) {
2245 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2246 !h2s->h2c->mbuf->o && // not yet subscribed
2247 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2248 conn_xprt_want_send(cs->conn);
2249 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2250 }
2251 }
2252 else if (!LIST_ISEMPTY(&h2s->list)) {
2253 LIST_DEL(&h2s->list);
2254 LIST_INIT(&h2s->list);
2255 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2256 }
2257
2258 /* this can happen from within si_chk_snd() */
2259 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2260 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002261}
2262
2263/*
2264 * Detach the stream from the connection and possibly release the connection.
2265 */
2266static void h2_detach(struct conn_stream *cs)
2267{
Willy Tarreau60935142017-10-16 18:11:19 +02002268 struct h2s *h2s = cs->ctx;
2269 struct h2c *h2c;
2270
2271 cs->ctx = NULL;
2272 if (!h2s)
2273 return;
2274
2275 h2c = h2s->h2c;
2276 h2s->cs = NULL;
2277
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002278 /* this stream may be blocked waiting for some data to leave (possibly
2279 * an ES or RST frame), so orphan it in this case.
2280 */
2281 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2282 return;
2283
Willy Tarreau541dd822017-11-23 18:12:50 +01002284 /* the stream could be in the send list */
2285 LIST_DEL(&h2s->list);
2286
Willy Tarreau45f752e2017-10-30 15:44:59 +01002287 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2288 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2289 /* unblock the connection if it was blocked on this
2290 * stream.
2291 */
2292 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2293 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2294 conn_xprt_want_recv(cs->conn);
2295 conn_xprt_want_send(cs->conn);
2296 }
2297
Willy Tarreau60935142017-10-16 18:11:19 +02002298 if (h2s->by_id.node.leaf_p) {
2299 /* h2s still attached to the h2c */
2300 eb32_delete(&h2s->by_id);
2301
2302 /* We don't want to close right now unless we're removing the
2303 * last stream, and either the connection is in error, or it
2304 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002305 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002306 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002307 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2308 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002309 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002310 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2311 (conn_xprt_read0_pending(h2c->conn) ||
2312 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002313 /* no more stream will come, kill it now */
2314 h2_release(h2c->conn);
2315 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002316 else if (h2c->task) {
2317 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002318 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002319 task_queue(h2c->task);
2320 }
2321 else
2322 h2c->task->expire = TICK_ETERNITY;
2323 }
Willy Tarreau60935142017-10-16 18:11:19 +02002324 }
Willy Tarreaubafbe012017-11-24 17:34:44 +01002325 pool_free(pool_head_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002326}
2327
2328static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2329{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002330 struct h2s *h2s = cs->ctx;
2331
2332 if (!mode)
2333 return;
2334
Willy Tarreau721c9742017-11-07 11:05:42 +01002335 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002336 return;
2337
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002338 /* if no outgoing data was seen on this stream, it means it was
2339 * closed with a "tcp-request content" rule that is normally
2340 * used to kill the connection ASAP (eg: limit abuse). In this
2341 * case we send a goaway to close the connection.
2342 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002343 if (!(h2s->flags & H2_SF_RST_SENT) &&
2344 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2345 return;
2346
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002347 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2348 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2349 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2350 return;
2351
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002352 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2353 conn_xprt_want_send(cs->conn);
2354
2355 h2s->st = H2_SS_CLOSED;
Willy Tarreau62f52692017-10-08 23:01:42 +02002356}
2357
2358static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2359{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002360 struct h2s *h2s = cs->ctx;
2361
Willy Tarreau721c9742017-11-07 11:05:42 +01002362 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002363 return;
2364
Willy Tarreau67434202017-11-06 20:20:51 +01002365 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002366 /* we can cleanly close using an empty data frame only after headers */
2367
2368 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2369 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002370 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002371
2372 if (h2s->st == H2_SS_HREM)
2373 h2s->st = H2_SS_CLOSED;
2374 else
2375 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002376 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002377 /* if no outgoing data was seen on this stream, it means it was
2378 * closed with a "tcp-request content" rule that is normally
2379 * used to kill the connection ASAP (eg: limit abuse). In this
2380 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002381 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002382 if (!(h2s->flags & H2_SF_RST_SENT) &&
2383 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2384 return;
2385
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002386 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2387 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002388 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2389 return;
2390
Willy Tarreau58e32082017-11-07 14:41:09 +01002391 h2s->st = H2_SS_CLOSED;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002392 }
2393
2394 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2395 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002396}
2397
Willy Tarreau13278b42017-10-13 19:23:14 +02002398/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2399 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2400 * proceed. Stream errors are reported in h2s->errcode and connection errors
2401 * in h2c->errcode. The caller must already have checked the frame header and
2402 * ensured that the frame was complete or the buffer full.
2403 */
2404static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2405{
2406 struct h2c *h2c = h2s->h2c;
2407 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002408 struct chunk *tmp = get_trash_chunk();
2409 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002410 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002411 int flen = h2c->dfl;
2412 int outlen = 0;
2413 int wrap;
2414 int try;
2415
2416 if (!h2c->dfl) {
2417 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
2418 return 0;
2419 }
2420
2421 /* if the input buffer wraps, take a temporary copy of it (rare) */
2422 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2423 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002424 copy = alloc_trash_chunk();
2425 if (!copy) {
2426 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2427 goto fail;
2428 }
2429 memcpy(copy->str, h2c->dbuf->p, wrap);
2430 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2431 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002432 }
2433
2434 /* The padlen is the first byte before data, and the padding appears
2435 * after data. padlen+data+padding are included in flen.
2436 */
2437 if (h2c->dff & H2_F_HEADERS_PADDED) {
2438 if (*hdrs >= flen) {
2439 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2440 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002441 return 0;
2442 }
2443 flen -= *hdrs + 1;
2444 hdrs += 1; // skip Pad Length
2445 }
2446
2447 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2448 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
2449 hdrs += 5; // stream dep = 4, weight = 1
2450 flen -= 5;
2451 }
2452
2453 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2454 * don't support this for now and can't even decompress so we have to
2455 * break the connection.
2456 */
2457 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2458 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002459 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002460 }
2461
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002462 /* we can't retry a failed decompression operation so we must be very
2463 * careful not to take any risks. In practice the output buffer is
2464 * always empty except maybe for trailers, so these operations almost
2465 * never happen.
2466 */
2467 if (unlikely(buf->o)) {
2468 /* need to let the output buffer flush and
2469 * mark the buffer for later wake up.
2470 */
2471 goto fail;
2472 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002473
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002474 if (unlikely(buffer_space_wraps(buf))) {
2475 /* it doesn't fit and the buffer is fragmented,
2476 * so let's defragment it and try again.
2477 */
2478 buffer_slow_realign(buf);
2479 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002480
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002481 /* first check if we have some room after p+i */
2482 try = buf->data + buf->size - (buf->p + buf->i);
2483
2484 /* otherwise continue between data and p-o */
2485 if (try <= 0) {
2486 try = buf->p - (buf->data + buf->o);
2487 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002488 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002489 }
2490 if (try > count)
2491 try = count;
2492
2493 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2494 sizeof(list)/sizeof(list[0]), tmp);
2495 if (outlen < 0) {
2496 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2497 goto fail;
2498 }
2499
2500 /* OK now we have our header list in <list> */
2501 outlen = h2_make_h1_request(list, bi_end(buf), try);
2502
2503 if (outlen < 0) {
2504 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2505 goto fail;
2506 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002507
2508 /* now consume the input data */
2509 bi_del(h2c->dbuf, h2c->dfl);
2510 h2c->st0 = H2_CS_FRAME_H;
2511 buf->i += outlen;
2512
2513 /* don't send it before returning data!
2514 * FIXME: should we instead try to send it much later, after the
2515 * response ? This would require that we keep a copy of it in h2s.
2516 */
2517 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2518 h2s->cs->flags |= CS_FL_EOS;
2519 h2s->flags |= H2_SF_ES_RCVD;
2520 }
2521
Willy Tarreau68dd9852017-07-03 14:44:26 +02002522 leave:
2523 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002524 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002525 fail:
2526 outlen = 0;
2527 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002528}
2529
Willy Tarreau454f9052017-10-26 19:40:35 +02002530/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2531 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2532 * in use, a new chunk is emitted for each frame. This is supposed to fit
2533 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2534 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2535 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2536 * parser state is automatically updated. Returns the number of bytes emitted
2537 * if > 0, or 0 if it couldn't proceed. Stream errors are reported in
2538 * h2s->errcode and connection errors in h2c->errcode. The caller must already
2539 * have checked the frame header and ensured that the frame was complete or the
2540 * buffer full. It changes the frame state to FRAME_A once done.
2541 */
2542static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2543{
2544 struct h2c *h2c = h2s->h2c;
2545 int block1, block2;
2546 unsigned int flen = h2c->dfl;
2547 unsigned int padlen = 0;
2548 int offset = 0;
2549
2550 if (h2c->dbuf->i < flen)
2551 return 0;
2552
2553 /* The padlen is the first byte before data, and the padding appears
2554 * after data. padlen+data+padding are included in flen.
2555 */
2556 if (h2c->dff & H2_F_HEADERS_PADDED) {
2557 padlen = *(uint8_t *)bi_ptr(h2c->dbuf);
2558 if (padlen >= flen) {
2559 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2560 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002561 return 0;
2562 }
2563 flen -= padlen + 1;
2564 offset = 1; // skip Pad Length
2565 }
2566
2567 /* does it fit in output buffer or should we wait ? */
2568 if (buf->i + buf->o + flen > buf->size) {
2569 h2c->flags |= H2_CF_DEM_SFULL;
2570 return 0;
2571 }
2572
2573 /* Block1 is the length of the first block before the buffer wraps,
2574 * block2 is the optional second block to reach the end of the frame.
2575 */
2576 block1 = bi_contig_data(h2c->dbuf);
2577 if (block1 > offset + flen)
2578 block1 = offset + flen;
2579 block1 -= offset; // skip Pad Length
2580 block2 = flen - block1;
2581
2582 if (block1)
2583 bi_putblk(buf, b_ptr(h2c->dbuf, offset), block1);
2584
2585 if (block2)
2586 bi_putblk(buf, b_ptr(h2c->dbuf, offset + block1), block2);
2587
2588 /* now mark the input data as consumed (will be deleted from the buffer
2589 * by the caller when seeing FRAME_A after sending the window update).
2590 */
2591 h2c->rcvd_c += h2c->dfl;
2592 h2c->rcvd_s += h2c->dfl; // warning, this can also affect the closed streams!
2593 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2594
2595 /* don't send it before returning data!
2596 * FIXME: should we instead try to send it much later, after the
2597 * response ? This would require that we keep a copy of it in h2s.
2598 */
2599 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2600 h2s->cs->flags |= CS_FL_EOS;
2601 h2s->flags |= H2_SF_ES_RCVD;
2602 }
2603
2604 return flen;
2605}
2606
Willy Tarreau62f52692017-10-08 23:01:42 +02002607/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002608 * Called from the upper layer to get more data, up to <count> bytes. The
2609 * caller is responsible for never asking for more data than what is available
2610 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002611 */
2612static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2613{
Willy Tarreau13278b42017-10-13 19:23:14 +02002614 struct h2s *h2s = cs->ctx;
2615 struct h2c *h2c = h2s->h2c;
2616 int ret = 0;
2617
2618 if (h2c->st0 != H2_CS_FRAME_P)
2619 return 0; // no pre-parsed frame yet
2620
2621 if (h2c->dsi != h2s->id)
2622 return 0; // not for us
2623
2624 if (!h2c->dbuf->size)
2625 return 0; // empty buffer
2626
2627 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2628 return 0; // incomplete input frame
2629
2630 switch (h2c->dft) {
2631 case H2_FT_HEADERS:
2632 ret = h2_frt_decode_headers(h2s, buf, count);
2633 break;
2634
Willy Tarreau454f9052017-10-26 19:40:35 +02002635 case H2_FT_DATA:
2636 ret = h2_frt_transfer_data(h2s, buf, count);
2637 break;
2638
Willy Tarreau13278b42017-10-13 19:23:14 +02002639 default:
2640 ret = 0;
2641 }
2642 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002643}
2644
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002645/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2646 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2647 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2648 * to the number of buffer bytes consumed.
2649 */
2650static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2651{
2652 struct http_hdr list[MAX_HTTP_HDR];
2653 struct h2c *h2c = h2s->h2c;
2654 struct h1m *h1m = &h2s->res;
2655 struct chunk outbuf;
2656 int es_now = 0;
2657 int ret = 0;
2658 int hdr;
2659
2660 if (h2c_mux_busy(h2c, h2s)) {
2661 h2s->flags |= H2_SF_BLK_MBUSY;
2662 return 0;
2663 }
2664
2665 if (!h2_get_mbuf(h2c)) {
2666 h2c->flags |= H2_CF_MUX_MALLOC;
2667 h2s->flags |= H2_SF_BLK_MROOM;
2668 return 0;
2669 }
2670
2671 /* First, try to parse the H1 response and index it into <list>.
2672 * NOTE! Since it comes from haproxy, we *know* that a response header
2673 * block does not wrap and we can safely read it this way without
2674 * having to realign the buffer.
2675 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002676 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002677 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2678 list, sizeof(list)/sizeof(list[0]), h1m);
2679 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002680 /* incomplete or invalid response, this is abnormal coming from
2681 * haproxy and may only result in a bad errorfile or bad Lua code
2682 * so that won't be fixed, raise an error now.
2683 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002684 * FIXME: we should instead add the ability to only return a
2685 * 502 bad gateway. But in theory this is not supposed to
2686 * happen.
2687 */
2688 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2689 ret = 0;
2690 goto end;
2691 }
2692
2693 chunk_reset(&outbuf);
2694
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002695 try_again:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002696 while (1) {
2697 outbuf.str = bo_end(h2c->mbuf);
2698 outbuf.size = bo_contig_space(h2c->mbuf);
2699 outbuf.len = 0;
2700
2701 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2702 break;
2703 realign_again:
2704 buffer_slow_realign(h2c->mbuf);
2705 }
2706
2707 if (outbuf.size < 9) {
2708 h2c->flags |= H2_CF_MUX_MFULL;
2709 h2s->flags |= H2_SF_BLK_MROOM;
2710 ret = 0;
2711 goto end;
2712 }
2713
2714 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2715 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2716 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2717 outbuf.len = 9;
2718
2719 /* encode status, which necessarily is the first one */
2720 if (outbuf.len < outbuf.size && h1m->status == 200)
2721 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2722 else if (outbuf.len < outbuf.size && h1m->status == 304)
2723 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002724 else if (unlikely(list[0].v.len != 3)) {
2725 /* this is an unparsable response */
2726 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2727 ret = 0;
2728 goto end;
2729 }
2730 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002731 /* basic encoding of the status code */
2732 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2733 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2734 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2735 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2736 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2737 }
2738 else {
2739 if (buffer_space_wraps(h2c->mbuf))
2740 goto realign_again;
2741
2742 h2c->flags |= H2_CF_MUX_MFULL;
2743 h2s->flags |= H2_SF_BLK_MROOM;
2744 ret = 0;
2745 goto end;
2746 }
2747
2748 /* encode all headers, stop at empty name */
2749 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002750 /* these ones do not exist in H2 and must be dropped. */
2751 if (isteq(list[hdr].n, ist("connection")) ||
2752 isteq(list[hdr].n, ist("proxy-connection")) ||
2753 isteq(list[hdr].n, ist("keep-alive")) ||
2754 isteq(list[hdr].n, ist("upgrade")) ||
2755 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002756 continue;
2757
2758 if (isteq(list[hdr].n, ist("")))
2759 break; // end
2760
2761 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2762 /* output full */
2763 if (buffer_space_wraps(h2c->mbuf))
2764 goto realign_again;
2765
2766 h2c->flags |= H2_CF_MUX_MFULL;
2767 h2s->flags |= H2_SF_BLK_MROOM;
2768 ret = 0;
2769 goto end;
2770 }
2771 }
2772
2773 /* we may need to add END_STREAM */
2774 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2775 es_now = 1;
2776
2777 /* update the frame's size */
2778 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2779
2780 if (es_now)
2781 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2782
2783 /* consume incoming H1 response */
2784 bo_del(buf, ret);
2785
2786 /* commit the H2 response */
2787 h2c->mbuf->o += outbuf.len;
2788 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01002789 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002790
2791 /* for now we don't implemented CONTINUATION, so we wait for a
2792 * body or directly end in TRL2.
2793 */
2794 if (es_now) {
2795 h1m->state = HTTP_MSG_DONE;
2796 h2s->flags |= H2_SF_ES_SENT;
2797 if (h2s->st == H2_SS_OPEN)
2798 h2s->st = H2_SS_HLOC;
2799 else
2800 h2s->st = H2_SS_CLOSED;
2801 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01002802 else if (h1m->status >= 100 && h1m->status < 200) {
2803 h1m->state = HTTP_MSG_RPBEFORE;
2804 h1m->status = 0;
2805 h1m->flags = 0;
2806 goto next_header_block;
2807 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002808 else
2809 h1m->state = (h1m->flags & H1_MF_CLEN) ? HTTP_MSG_BODY : HTTP_MSG_CHUNK_SIZE;
2810
2811 end:
2812 //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));
2813 return ret;
2814}
2815
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002816/* Try to send a DATA frame matching HTTP/1 response present in the response
2817 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
2818 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
2819 * corresponds to the number of buffer bytes consumed.
2820 */
2821static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
2822{
2823 struct h2c *h2c = h2s->h2c;
2824 struct h1m *h1m = &h2s->res;
2825 struct chunk outbuf;
2826 int ret = 0;
2827 int total = 0;
2828 int es_now = 0;
2829 int size = 0;
2830 char *blk1, *blk2;
2831 int len1, len2;
2832
2833 if (h2c_mux_busy(h2c, h2s)) {
2834 h2s->flags |= H2_SF_BLK_MBUSY;
2835 goto end;
2836 }
2837
2838 if (!h2_get_mbuf(h2c)) {
2839 h2c->flags |= H2_CF_MUX_MALLOC;
2840 h2s->flags |= H2_SF_BLK_MROOM;
2841 goto end;
2842 }
2843
2844 new_frame:
2845 if (!buf->o)
2846 goto end;
2847
2848 chunk_reset(&outbuf);
2849
2850 while (1) {
2851 outbuf.str = bo_end(h2c->mbuf);
2852 outbuf.size = bo_contig_space(h2c->mbuf);
2853 outbuf.len = 0;
2854
2855 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2856 break;
2857 realign_again:
2858 buffer_slow_realign(h2c->mbuf);
2859 }
2860
2861 if (outbuf.size < 9) {
2862 h2c->flags |= H2_CF_MUX_MFULL;
2863 h2s->flags |= H2_SF_BLK_MROOM;
2864 goto end;
2865 }
2866
2867 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
2868 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
2869 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2870 outbuf.len = 9;
2871
2872 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
2873 case 0: /* no content length, read till SHUTW */
2874 size = buf->o;
2875 break;
2876 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
2877 size = buf->o;
2878 if ((long long)size > h1m->curr_len)
2879 size = h1m->curr_len;
2880 break;
2881 default: /* te:chunked : parse chunks */
2882 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
2883 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
2884 if (!ret)
2885 goto end;
2886
2887 if (ret < 0) {
2888 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2889 h1m->err_pos = ret;
2890 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2891 goto end;
2892 }
2893 bo_del(buf, ret);
2894 total += ret;
2895 h1m->state = HTTP_MSG_CHUNK_SIZE;
2896 }
2897
2898 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
2899 unsigned int chunk;
2900
2901 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
2902 if (!ret)
2903 goto end;
2904
2905 if (ret < 0) {
2906 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2907 h1m->err_pos = ret;
2908 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2909 goto end;
2910 }
2911
2912 size = chunk;
2913 h1m->curr_len = chunk;
2914 h1m->body_len += chunk;
2915 bo_del(buf, ret);
2916 total += ret;
2917 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
2918 if (!size)
2919 goto send_empty;
2920 }
2921
2922 /* in MSG_DATA state, continue below */
2923 size = h1m->curr_len;
2924 break;
2925 }
2926
2927 /* we have in <size> the exact number of bytes we need to copy from
2928 * the H1 buffer. We need to check this against the connection's and
2929 * the stream's send windows, and to ensure that this fits in the max
2930 * frame size and in the buffer's available space minus 9 bytes (for
2931 * the frame header). The connection's flow control is applied last so
2932 * that we can use a separate list of streams which are immediately
2933 * unblocked on window opening. Note: we don't implement padding.
2934 */
2935
2936 if (size > buf->o)
2937 size = buf->o;
2938
2939 if (size > h2s->mws)
2940 size = h2s->mws;
2941
2942 if (size <= 0) {
2943 h2s->flags |= H2_SF_BLK_SFCTL;
2944 goto end;
2945 }
2946
2947 if (h2c->mfs && size > h2c->mfs)
2948 size = h2c->mfs;
2949
2950 if (size + 9 > outbuf.size) {
2951 /* we have an opportunity for enlarging the too small
2952 * available space, let's try.
2953 */
2954 if (buffer_space_wraps(h2c->mbuf))
2955 goto realign_again;
2956 size = outbuf.size - 9;
2957 }
2958
2959 if (size <= 0) {
2960 h2c->flags |= H2_CF_MUX_MFULL;
2961 h2s->flags |= H2_SF_BLK_MROOM;
2962 goto end;
2963 }
2964
2965 if (size > h2c->mws)
2966 size = h2c->mws;
2967
2968 if (size <= 0) {
2969 h2s->flags |= H2_SF_BLK_MFCTL;
2970 goto end;
2971 }
2972
2973 /* copy whatever we can */
2974 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
2975 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
2976 if (ret == 1)
2977 len2 = 0;
2978
2979 if (!ret || len1 + len2 < size) {
2980 /* FIXME: must normally never happen */
2981 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2982 goto end;
2983 }
2984
2985 /* limit len1/len2 to size */
2986 if (len1 + len2 > size) {
2987 int sub = len1 + len2 - size;
2988
2989 if (len2 > sub)
2990 len2 -= sub;
2991 else {
2992 sub -= len2;
2993 len2 = 0;
2994 len1 -= sub;
2995 }
2996 }
2997
2998 /* now let's copy this this into the output buffer */
2999 memcpy(outbuf.str + 9, blk1, len1);
3000 if (len2)
3001 memcpy(outbuf.str + 9 + len1, blk2, len2);
3002
3003 send_empty:
3004 /* we may need to add END_STREAM */
3005 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3006 * could rely on the MSG_MORE flag as a hint for this ?
3007 */
3008 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3009 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3010 es_now = 1;
3011
3012 /* update the frame's size */
3013 h2_set_frame_size(outbuf.str, size);
3014
3015 if (es_now)
3016 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3017
3018 /* commit the H2 response */
3019 h2c->mbuf->o += size + 9;
3020 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3021
3022 /* consume incoming H1 response */
3023 if (size > 0) {
3024 bo_del(buf, size);
3025 total += size;
3026 h1m->curr_len -= size;
3027 h2s->mws -= size;
3028 h2c->mws -= size;
3029
3030 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3031 h1m->state = HTTP_MSG_CHUNK_CRLF;
3032 goto new_frame;
3033 }
3034 }
3035
3036 if (es_now) {
3037 if (h2s->st == H2_SS_OPEN)
3038 h2s->st = H2_SS_HLOC;
3039 else
3040 h2s->st = H2_SS_CLOSED;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003041
3042 if (!(h1m->flags & H1_MF_CHNK))
3043 h1m->state = HTTP_MSG_DONE;
3044
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003045 h2s->flags |= H2_SF_ES_SENT;
3046 }
3047
3048 end:
3049 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);
3050 return total;
3051}
3052
Willy Tarreau62f52692017-10-08 23:01:42 +02003053/* Called from the upper layer, to send data */
3054static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3055{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003056 struct h2s *h2s = cs->ctx;
3057 int total = 0;
3058
Willy Tarreauc4312d32017-11-07 12:01:53 +01003059 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3060 h2s->flags |= H2_SF_OUTGOING_DATA;
3061
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003062 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3063 if (h2s->res.state < HTTP_MSG_BODY) {
3064 total += h2s_frt_make_resp_headers(h2s, buf);
3065
3066 if (h2s->st == H2_SS_ERROR)
3067 break;
3068
3069 if (h2s->flags & H2_SF_BLK_ANY)
3070 break;
3071 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003072 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3073 total += h2s_frt_make_resp_data(h2s, buf);
3074
3075 if (h2s->st == H2_SS_ERROR)
3076 break;
3077
3078 if (h2s->flags & H2_SF_BLK_ANY)
3079 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003080 }
3081 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3082 /* consume the trailers if any (we don't forward them for now) */
3083 int count = h1_measure_trailers(buf);
3084
3085 if (unlikely(count <= 0)) {
3086 if (count < 0)
3087 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3088 break;
3089 }
3090 total += count;
3091 bo_del(buf, count);
3092 h2s->res.state = HTTP_MSG_DONE;
3093 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003094 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003095 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003096 cs->flags |= CS_FL_ERROR;
3097 break;
3098 }
3099 }
3100
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003101 /* RST are sent similarly to frame acks */
3102 if (h2s->st == H2_SS_ERROR) {
3103 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003104 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003105 h2s->st = H2_SS_CLOSED;
3106 }
3107
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003108 if (h2s->flags & H2_SF_BLK_SFCTL) {
3109 /* stream flow control, quit the list */
3110 LIST_DEL(&h2s->list);
3111 LIST_INIT(&h2s->list);
3112 }
3113
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003114 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003115}
3116
3117
3118/*******************************************************/
3119/* functions below are dedicated to the config parsers */
3120/*******************************************************/
3121
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003122/* config parser for global "tune.h2.header-table-size" */
3123static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3124 struct proxy *defpx, const char *file, int line,
3125 char **err)
3126{
3127 if (too_many_args(1, args, err, NULL))
3128 return -1;
3129
3130 h2_settings_header_table_size = atoi(args[1]);
3131 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3132 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3133 return -1;
3134 }
3135 return 0;
3136}
Willy Tarreau62f52692017-10-08 23:01:42 +02003137
Willy Tarreaue6baec02017-07-27 11:45:11 +02003138/* config parser for global "tune.h2.initial-window-size" */
3139static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3140 struct proxy *defpx, const char *file, int line,
3141 char **err)
3142{
3143 if (too_many_args(1, args, err, NULL))
3144 return -1;
3145
3146 h2_settings_initial_window_size = atoi(args[1]);
3147 if (h2_settings_initial_window_size < 0) {
3148 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3149 return -1;
3150 }
3151 return 0;
3152}
3153
Willy Tarreau5242ef82017-07-27 11:47:28 +02003154/* config parser for global "tune.h2.max-concurrent-streams" */
3155static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3156 struct proxy *defpx, const char *file, int line,
3157 char **err)
3158{
3159 if (too_many_args(1, args, err, NULL))
3160 return -1;
3161
3162 h2_settings_max_concurrent_streams = atoi(args[1]);
3163 if (h2_settings_max_concurrent_streams < 0) {
3164 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3165 return -1;
3166 }
3167 return 0;
3168}
3169
Willy Tarreau62f52692017-10-08 23:01:42 +02003170
3171/****************************************/
3172/* MUX initialization and instanciation */
3173/***************************************/
3174
3175/* The mux operations */
3176const struct mux_ops h2_ops = {
3177 .init = h2_init,
3178 .recv = h2_recv,
3179 .send = h2_send,
3180 .wake = h2_wake,
3181 .update_poll = h2_update_poll,
3182 .rcv_buf = h2_rcv_buf,
3183 .snd_buf = h2_snd_buf,
3184 .attach = h2_attach,
3185 .detach = h2_detach,
3186 .shutr = h2_shutr,
3187 .shutw = h2_shutw,
Willy Tarreau62f52692017-10-08 23:01:42 +02003188 .name = "H2",
3189};
3190
3191/* ALPN selection : this mux registers ALPN tolen "h2" */
3192static struct alpn_mux_list alpn_mux_h2 =
3193 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3194
3195/* config keyword parsers */
3196static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003197 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003198 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003199 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003200 { 0, NULL, NULL }
3201}};
3202
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003203static void __h2_deinit(void)
3204{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003205 pool_destroy(pool_head_h2s);
3206 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003207}
3208
Willy Tarreau62f52692017-10-08 23:01:42 +02003209__attribute__((constructor))
3210static void __h2_init(void)
3211{
3212 alpn_register_mux(&alpn_mux_h2);
3213 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003214 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003215 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3216 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003217}