blob: 771a3e6022f4061756a87ae5b90f392b5ed0ec6b [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 Tarreau49745612017-12-03 18:56:02 +0100108 unsigned int nb_streams; /* number of streams in the tree */
109 /* 32 bit hole here */
Willy Tarreauea392822017-10-31 10:02:25 +0100110 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200111 struct eb_root streams_by_id; /* all active streams by their ID */
112 struct list send_list; /* list of blocked streams requesting to send */
113 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200114 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +0200115 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200116};
117
Willy Tarreau18312642017-10-11 07:57:07 +0200118/* H2 stream state, in h2s->st */
119enum h2_ss {
120 H2_SS_IDLE = 0, // idle
121 H2_SS_RLOC, // reserved(local)
122 H2_SS_RREM, // reserved(remote)
123 H2_SS_OPEN, // open
124 H2_SS_HREM, // half-closed(remote)
125 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200126 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200127 H2_SS_CLOSED, // closed
128 H2_SS_ENTRIES // must be last
129} __attribute__((packed));
130
131/* HTTP/2 stream flags (32 bit), in h2s->flags */
132#define H2_SF_NONE 0x00000000
133#define H2_SF_ES_RCVD 0x00000001
134#define H2_SF_ES_SENT 0x00000002
135
136#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
137#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
138
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200139/* stream flags indicating the reason the stream is blocked */
140#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
141#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
142#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
143#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
144#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
145
Willy Tarreau454f9052017-10-26 19:40:35 +0200146/* stream flags indicating how data is supposed to be sent */
147#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
148#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
149
150/* step we're currently in when sending chunks. This is needed because we may
151 * have to transfer chunks as large as a full buffer so there's no room left
152 * for size nor crlf around.
153 */
154#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
155#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
156#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
157
158#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
159
Willy Tarreau67434202017-11-06 20:20:51 +0100160#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100161#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100162
Willy Tarreau18312642017-10-11 07:57:07 +0200163/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
164 * it is being processed in the internal HTTP representation (H1 for now).
165 */
166struct h2s {
167 struct conn_stream *cs;
168 struct h2c *h2c;
169 struct h1m req, res; /* request and response parser state for H1 */
170 struct eb32_node by_id; /* place in h2c's streams_by_id */
171 struct list list; /* position in active/blocked lists if blocked>0 */
172 int32_t id; /* stream ID */
173 uint32_t flags; /* H2_SF_* */
174 int mws; /* mux window size for this stream */
175 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
176 enum h2_ss st;
177};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200178
Willy Tarreauc6405142017-09-21 20:23:50 +0200179/* descriptor for an h2 frame header */
180struct h2_fh {
181 uint32_t len; /* length, host order, 24 bits */
182 uint32_t sid; /* stream id, host order, 31 bits */
183 uint8_t ft; /* frame type */
184 uint8_t ff; /* frame flags */
185};
186
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200187/* a few settings from the global section */
188static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200189static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200190static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200191
Willy Tarreau2a856182017-05-16 15:20:39 +0200192/* a dmumy closed stream */
193static const struct h2s *h2_closed_stream = &(const struct h2s){
194 .cs = NULL,
195 .h2c = NULL,
196 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100197 .errcode = H2_ERR_STREAM_CLOSED,
198 .flags = H2_SF_RST_SENT,
Willy Tarreau2a856182017-05-16 15:20:39 +0200199 .id = 0,
200};
201
202/* and a dummy idle stream for use with any unannounced stream */
203static const struct h2s *h2_idle_stream = &(const struct h2s){
204 .cs = NULL,
205 .h2c = NULL,
206 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100207 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200208 .id = 0,
209};
210
Willy Tarreauea392822017-10-31 10:02:25 +0100211static struct task *h2_timeout_task(struct task *t);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200212
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200213/*****************************************************/
214/* functions below are for dynamic buffer management */
215/*****************************************************/
216
217/* re-enables receiving on mux <target> after a buffer was allocated. It returns
218 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
219 * if it's impossible to wake up and we prefer to be woken up later.
220 */
221static int h2_dbuf_available(void *target)
222{
223 struct h2c *h2c = target;
224
225 /* take the buffer now as we'll get scheduled waiting for ->wake() */
226 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200227 h2c->flags &= ~H2_CF_DEM_DALLOC;
228 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
229 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200230 return 1;
231 }
232 return 0;
233}
234
235static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
236{
237 struct buffer *buf = NULL;
238
239 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
240 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
241 h2c->dbuf_wait.target = h2c->conn;
242 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100243 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200244 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100245 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200246 __conn_xprt_stop_recv(h2c->conn);
247 }
248 return buf;
249}
250
251static inline void h2_release_dbuf(struct h2c *h2c)
252{
253 if (h2c->dbuf->size) {
254 b_free(&h2c->dbuf);
255 offer_buffers(h2c->dbuf_wait.target,
256 tasks_run_queue + applets_active_queue);
257 }
258}
259
Willy Tarreau14398122017-09-22 14:26:04 +0200260/* re-enables sending on mux <target> after a buffer was allocated. It returns
261 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
262 * if it's impossible to wake up and we prefer to be woken up later.
263 */
264static int h2_mbuf_available(void *target)
265{
266 struct h2c *h2c = target;
267
268 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
269 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200270 if (h2c->flags & H2_CF_MUX_MALLOC) {
271 h2c->flags &= ~H2_CF_MUX_MALLOC;
272 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
273 conn_xprt_want_send(h2c->conn);
274 }
275
276 if (h2c->flags & H2_CF_DEM_MROOM) {
277 h2c->flags &= ~H2_CF_DEM_MROOM;
278 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
279 conn_xprt_want_recv(h2c->conn);
280 }
281
Willy Tarreau14398122017-09-22 14:26:04 +0200282 /* FIXME: we should in fact call something like h2_update_poll()
283 * now to recompte the polling. For now it will be enough like
284 * this.
285 */
Willy Tarreau14398122017-09-22 14:26:04 +0200286 return 1;
287 }
288 return 0;
289}
290
291static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
292{
293 struct buffer *buf = NULL;
294
295 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
296 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
297 h2c->mbuf_wait.target = h2c;
298 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100299 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200300 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100301 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200302
303 /* FIXME: we should in fact only block the direction being
304 * currently used. For now it will be enough like this.
305 */
306 __conn_xprt_stop_send(h2c->conn);
307 __conn_xprt_stop_recv(h2c->conn);
308 }
309 return buf;
310}
311
312static inline void h2_release_mbuf(struct h2c *h2c)
313{
314 if (h2c->mbuf->size) {
315 b_free(&h2c->mbuf);
316 offer_buffers(h2c->mbuf_wait.target,
317 tasks_run_queue + applets_active_queue);
318 }
319}
320
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200321
Willy Tarreau62f52692017-10-08 23:01:42 +0200322/*****************************************************************/
323/* functions below are dedicated to the mux setup and management */
324/*****************************************************************/
325
Willy Tarreau32218eb2017-09-22 08:07:25 +0200326/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
327static int h2c_frt_init(struct connection *conn)
328{
329 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100330 struct task *t = NULL;
331 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200332
Willy Tarreaubafbe012017-11-24 17:34:44 +0100333 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200334 if (!h2c)
335 goto fail;
336
Willy Tarreau3f133572017-10-31 19:21:06 +0100337
Willy Tarreau599391a2017-11-24 10:16:00 +0100338 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
339 if (tick_isset(sess->fe->timeout.clientfin))
340 h2c->shut_timeout = sess->fe->timeout.clientfin;
341
Willy Tarreau33400292017-11-05 11:23:40 +0100342 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100343 if (tick_isset(h2c->timeout)) {
344 t = task_new(tid_bit);
345 if (!t)
346 goto fail;
347
348 h2c->task = t;
349 t->process = h2_timeout_task;
350 t->context = h2c;
351 t->expire = tick_add(now_ms, h2c->timeout);
352 }
Willy Tarreauea392822017-10-31 10:02:25 +0100353
Willy Tarreau32218eb2017-09-22 08:07:25 +0200354 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
355 if (!h2c->ddht)
356 goto fail;
357
358 /* Initialise the context. */
359 h2c->st0 = H2_CS_PREFACE;
360 h2c->conn = conn;
361 h2c->max_id = -1;
362 h2c->errcode = H2_ERR_NO_ERROR;
363 h2c->flags = H2_CF_NONE;
364 h2c->rcvd_c = 0;
365 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100366 h2c->nb_streams = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200367
368 h2c->dbuf = &buf_empty;
369 h2c->dsi = -1;
370 h2c->msi = -1;
371 h2c->last_sid = -1;
372
373 h2c->mbuf = &buf_empty;
374 h2c->miw = 65535; /* mux initial window size */
375 h2c->mws = 65535; /* mux window size */
376 h2c->mfs = 16384; /* initial max frame size */
377 h2c->streams_by_id = EB_ROOT_UNIQUE;
378 LIST_INIT(&h2c->send_list);
379 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200380 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200381 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200382 conn->mux_ctx = h2c;
383
Willy Tarreau3f133572017-10-31 19:21:06 +0100384 if (t)
385 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200386 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100387
Willy Tarreau32218eb2017-09-22 08:07:25 +0200388 /* mux->wake will be called soon to complete the operation */
389 return 0;
390 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100391 if (t)
392 task_free(t);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100393 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200394 return -1;
395}
396
Willy Tarreau62f52692017-10-08 23:01:42 +0200397/* Initialize the mux once it's attached. For outgoing connections, the context
398 * is already initialized before installing the mux, so we detect incoming
399 * connections from the fact that the context is still NULL. Returns < 0 on
400 * error.
401 */
402static int h2_init(struct connection *conn)
403{
404 if (conn->mux_ctx) {
405 /* we don't support outgoing connections for now */
406 return -1;
407 }
408
Willy Tarreau32218eb2017-09-22 08:07:25 +0200409 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200410}
411
Willy Tarreau2373acc2017-10-12 17:35:14 +0200412/* returns the stream associated with id <id> or NULL if not found */
413static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
414{
415 struct eb32_node *node;
416
Willy Tarreau2a856182017-05-16 15:20:39 +0200417 if (id > h2c->max_id)
418 return (struct h2s *)h2_idle_stream;
419
Willy Tarreau2373acc2017-10-12 17:35:14 +0200420 node = eb32_lookup(&h2c->streams_by_id, id);
421 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200422 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200423
424 return container_of(node, struct h2s, by_id);
425}
426
Willy Tarreau62f52692017-10-08 23:01:42 +0200427/* release function for a connection. This one should be called to free all
428 * resources allocated to the mux.
429 */
430static void h2_release(struct connection *conn)
431{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200432 struct h2c *h2c = conn->mux_ctx;
433
434 LIST_DEL(&conn->list);
435
436 if (h2c) {
437 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200438 h2_release_dbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100439 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200440 LIST_DEL(&h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100441 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200442
443 h2_release_mbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100444 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200445 LIST_DEL(&h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100446 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200447
Willy Tarreauea392822017-10-31 10:02:25 +0100448 if (h2c->task) {
449 task_delete(h2c->task);
450 task_free(h2c->task);
451 h2c->task = NULL;
452 }
453
Willy Tarreaubafbe012017-11-24 17:34:44 +0100454 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200455 }
456
457 conn->mux = NULL;
458 conn->mux_ctx = NULL;
459
460 conn_stop_tracking(conn);
461 conn_full_close(conn);
462 if (conn->destroy_cb)
463 conn->destroy_cb(conn);
464 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200465}
466
467
Willy Tarreau71681172017-10-23 14:39:06 +0200468/******************************************************/
469/* functions below are for the H2 protocol processing */
470/******************************************************/
471
472/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100473static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200474{
475 return h2s ? h2s->id : 0;
476}
477
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200478/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100479static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200480{
481 if (h2c->msi < 0)
482 return 0;
483
484 if (h2c->msi == h2s_id(h2s))
485 return 0;
486
487 return 1;
488}
489
Willy Tarreau741d6df2017-10-17 08:00:59 +0200490/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100491static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200492{
493 h2c->errcode = err;
494 h2c->st0 = H2_CS_ERROR;
495}
496
Willy Tarreau2e43f082017-10-17 08:03:59 +0200497/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100498static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200499{
500 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
501 h2s->errcode = err;
502 h2s->st = H2_SS_ERROR;
503 if (h2s->cs)
504 h2s->cs->flags |= CS_FL_ERROR;
505 }
506}
507
Willy Tarreaue4820742017-07-27 13:37:23 +0200508/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100509static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200510{
511 uint8_t *out = frame;
512
513 *out = len >> 16;
514 write_n16(out + 1, len);
515}
516
Willy Tarreau54c15062017-10-10 17:10:03 +0200517/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
518 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
519 * the caller's responsibility to verify that there are at least <bytes> bytes
520 * available in the buffer's input prior to calling this function.
521 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100522static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200523 const struct buffer *b, int o)
524{
525 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
526}
527
Willy Tarreau1f094672017-11-20 21:27:45 +0100528static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200529{
530 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
531}
532
Willy Tarreau1f094672017-11-20 21:27:45 +0100533static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200534{
535 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
536}
537
Willy Tarreau1f094672017-11-20 21:27:45 +0100538static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200539{
540 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
541}
542
543
Willy Tarreau715d5312017-07-11 15:20:24 +0200544/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
545 * is not obvious. It turns out that H2 headers are neither aligned nor do they
546 * use regular sizes. And to add to the trouble, the buffer may wrap so each
547 * byte read must be checked. The header is formed like this :
548 *
549 * b0 b1 b2 b3 b4 b5..b8
550 * +----------+---------+--------+----+----+----------------------+
551 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
552 * +----------+---------+--------+----+----+----------------------+
553 *
554 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
555 * we get the sid properly aligned and ordered, and 16 bits of len properly
556 * ordered as well. The type and flags can be extracted using bit shifts from
557 * the word, and only one extra read is needed to fetch len[16:23].
558 * Returns zero if some bytes are missing, otherwise non-zero on success.
559 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100560static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200561{
562 uint64_t w;
563
564 if (b->i < 9)
565 return 0;
566
567 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
568 h->len = *b->p << 16;
569 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
570 h->ff = w >> 32;
571 h->ft = w >> 40;
572 h->len += w >> 48;
573 return 1;
574}
575
576/* skip the next 9 bytes corresponding to the frame header possibly parsed by
577 * h2_peek_frame_hdr() above.
578 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100579static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200580{
581 bi_del(b, 9);
582}
583
584/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100585static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200586{
587 int ret;
588
589 ret = h2_peek_frame_hdr(b, h);
590 if (ret > 0)
591 h2_skip_frame_hdr(b);
592 return ret;
593}
594
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200595/* creates a new stream <id> on the h2c connection and returns it, or NULL in
596 * case of memory allocation error.
597 */
598static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
599{
600 struct conn_stream *cs;
601 struct h2s *h2s;
602
Willy Tarreaubafbe012017-11-24 17:34:44 +0100603 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200604 if (!h2s)
605 goto out;
606
607 h2s->h2c = h2c;
608 h2s->mws = h2c->miw;
609 h2s->flags = H2_SF_NONE;
610 h2s->errcode = H2_ERR_NO_ERROR;
611 h2s->st = H2_SS_IDLE;
612 h1m_init(&h2s->req);
613 h1m_init(&h2s->res);
614 h2s->by_id.key = h2s->id = id;
615 h2c->max_id = id;
616 LIST_INIT(&h2s->list);
617
618 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100619 h2c->nb_streams++;
620 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
621 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200622
623 cs = cs_new(h2c->conn);
624 if (!cs)
625 goto out_close;
626
627 h2s->cs = cs;
628 cs->ctx = h2s;
629
630 if (stream_create_from_cs(cs) < 0)
631 goto out_free_cs;
632
633 /* OK done, the stream lives its own life now */
634 return h2s;
635
636 out_free_cs:
637 cs_free(cs);
638 out_close:
Willy Tarreau49745612017-12-03 18:56:02 +0100639 h2c->nb_streams--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200640 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100641 pool_free(pool_head_h2s, h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200642 h2s = NULL;
643 out:
644 return h2s;
645}
646
Willy Tarreaube5b7152017-09-25 16:25:39 +0200647/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
648 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
649 * the various settings codes.
650 */
651static int h2c_snd_settings(struct h2c *h2c)
652{
653 struct buffer *res;
654 char buf_data[100]; // enough for 15 settings
655 struct chunk buf;
656 int ret;
657
658 if (h2c_mux_busy(h2c, NULL)) {
659 h2c->flags |= H2_CF_DEM_MBUSY;
660 return 0;
661 }
662
663 res = h2_get_mbuf(h2c);
664 if (!res) {
665 h2c->flags |= H2_CF_MUX_MALLOC;
666 h2c->flags |= H2_CF_DEM_MROOM;
667 return 0;
668 }
669
670 chunk_init(&buf, buf_data, sizeof(buf_data));
671 chunk_memcpy(&buf,
672 "\x00\x00\x00" /* length : 0 for now */
673 "\x04\x00" /* type : 4 (settings), flags : 0 */
674 "\x00\x00\x00\x00", /* stream ID : 0 */
675 9);
676
677 if (h2_settings_header_table_size != 4096) {
678 char str[6] = "\x00\x01"; /* header_table_size */
679
680 write_n32(str + 2, h2_settings_header_table_size);
681 chunk_memcat(&buf, str, 6);
682 }
683
684 if (h2_settings_initial_window_size != 65535) {
685 char str[6] = "\x00\x04"; /* initial_window_size */
686
687 write_n32(str + 2, h2_settings_initial_window_size);
688 chunk_memcat(&buf, str, 6);
689 }
690
691 if (h2_settings_max_concurrent_streams != 0) {
692 char str[6] = "\x00\x03"; /* max_concurrent_streams */
693
694 /* Note: 0 means "unlimited" for haproxy's config but not for
695 * the protocol, so never send this value!
696 */
697 write_n32(str + 2, h2_settings_max_concurrent_streams);
698 chunk_memcat(&buf, str, 6);
699 }
700
701 if (global.tune.bufsize != 16384) {
702 char str[6] = "\x00\x05"; /* max_frame_size */
703
704 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
705 * match bufsize - rewrite size, but at the moment it seems
706 * that clients don't take care of it.
707 */
708 write_n32(str + 2, global.tune.bufsize);
709 chunk_memcat(&buf, str, 6);
710 }
711
712 h2_set_frame_size(buf.str, buf.len - 9);
713 ret = bo_istput(res, ist2(buf.str, buf.len));
714 if (unlikely(ret <= 0)) {
715 if (!ret) {
716 h2c->flags |= H2_CF_MUX_MFULL;
717 h2c->flags |= H2_CF_DEM_MROOM;
718 return 0;
719 }
720 else {
721 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
722 return 0;
723 }
724 }
725 return ret;
726}
727
Willy Tarreau52eed752017-09-22 15:05:09 +0200728/* Try to receive a connection preface, then upon success try to send our
729 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
730 * missing data. It may return an error in h2c.
731 */
732static int h2c_frt_recv_preface(struct h2c *h2c)
733{
734 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200735 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200736
737 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
738
739 if (unlikely(ret1 <= 0)) {
740 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
741 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
742 return 0;
743 }
744
Willy Tarreaube5b7152017-09-25 16:25:39 +0200745 ret2 = h2c_snd_settings(h2c);
746 if (ret2 > 0)
747 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200748
Willy Tarreaube5b7152017-09-25 16:25:39 +0200749 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200750}
751
Willy Tarreau081d4722017-05-16 21:51:05 +0200752/* try to send a GOAWAY frame on the connection to report an error or a graceful
753 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
754 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
755 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
756 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
757 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
758 * on unrecoverable failure. It will not attempt to send one again in this last
759 * case so that it is safe to use h2c_error() to report such errors.
760 */
761static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
762{
763 struct buffer *res;
764 char str[17];
765 int ret;
766
767 if (h2c->flags & H2_CF_GOAWAY_FAILED)
768 return 1; // claim that it worked
769
770 if (h2c_mux_busy(h2c, h2s)) {
771 if (h2s)
772 h2s->flags |= H2_SF_BLK_MBUSY;
773 else
774 h2c->flags |= H2_CF_DEM_MBUSY;
775 return 0;
776 }
777
778 res = h2_get_mbuf(h2c);
779 if (!res) {
780 h2c->flags |= H2_CF_MUX_MALLOC;
781 if (h2s)
782 h2s->flags |= H2_SF_BLK_MROOM;
783 else
784 h2c->flags |= H2_CF_DEM_MROOM;
785 return 0;
786 }
787
788 /* len: 8, type: 7, flags: none, sid: 0 */
789 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
790
791 if (h2c->last_sid < 0)
792 h2c->last_sid = h2c->max_id;
793
794 write_n32(str + 9, h2c->last_sid);
795 write_n32(str + 13, h2c->errcode);
796 ret = bo_istput(res, ist2(str, 17));
797 if (unlikely(ret <= 0)) {
798 if (!ret) {
799 h2c->flags |= H2_CF_MUX_MFULL;
800 if (h2s)
801 h2s->flags |= H2_SF_BLK_MROOM;
802 else
803 h2c->flags |= H2_CF_DEM_MROOM;
804 return 0;
805 }
806 else {
807 /* we cannot report this error using GOAWAY, so we mark
808 * it and claim a success.
809 */
810 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
811 h2c->flags |= H2_CF_GOAWAY_FAILED;
812 return 1;
813 }
814 }
815 h2c->flags |= H2_CF_GOAWAY_SENT;
816 return ret;
817}
818
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100819/* Try to send an RST_STREAM frame on the connection for the indicated stream
820 * during mux operations. This stream must be valid and cannot be closed
821 * already. h2s->id will be used for the stream ID and h2s->errcode will be
822 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
823 * not yet.
824 *
825 * Returns > 0 on success or zero if nothing was done. In case of lack of room
826 * to write the message, it subscribes the stream to future notifications.
827 */
828static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
829{
830 struct buffer *res;
831 char str[13];
832 int ret;
833
834 if (!h2s || h2s->st == H2_SS_CLOSED)
835 return 1;
836
837 if (h2c_mux_busy(h2c, h2s)) {
838 h2s->flags |= H2_SF_BLK_MBUSY;
839 return 0;
840 }
841
842 res = h2_get_mbuf(h2c);
843 if (!res) {
844 h2c->flags |= H2_CF_MUX_MALLOC;
845 h2s->flags |= H2_SF_BLK_MROOM;
846 return 0;
847 }
848
849 /* len: 4, type: 3, flags: none */
850 memcpy(str, "\x00\x00\x04\x03\x00", 5);
851 write_n32(str + 5, h2s->id);
852 write_n32(str + 9, h2s->errcode);
853 ret = bo_istput(res, ist2(str, 13));
854
855 if (unlikely(ret <= 0)) {
856 if (!ret) {
857 h2c->flags |= H2_CF_MUX_MFULL;
858 h2s->flags |= H2_SF_BLK_MROOM;
859 return 0;
860 }
861 else {
862 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
863 return 0;
864 }
865 }
866
867 h2s->flags |= H2_SF_RST_SENT;
868 h2s->st = H2_SS_CLOSED;
869 return ret;
870}
871
872/* Try to send an RST_STREAM frame on the connection for the stream being
873 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
874 * error code unless the stream's state already is IDLE or CLOSED in which
875 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
876 * it was not yet.
877 *
878 * Returns > 0 on success or zero if nothing was done. In case of lack of room
879 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200880 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100881 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200882 */
883static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
884{
885 struct buffer *res;
886 char str[13];
887 int ret;
888
889 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100890 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200891 return 0;
892 }
893
894 res = h2_get_mbuf(h2c);
895 if (!res) {
896 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100897 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200898 return 0;
899 }
900
901 /* len: 4, type: 3, flags: none */
902 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100903
Willy Tarreau27a84c92017-10-17 08:10:17 +0200904 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100905 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200906 h2s->errcode : H2_ERR_STREAM_CLOSED);
907 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100908
Willy Tarreau27a84c92017-10-17 08:10:17 +0200909 if (unlikely(ret <= 0)) {
910 if (!ret) {
911 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100912 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200913 return 0;
914 }
915 else {
916 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
917 return 0;
918 }
919 }
920
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100921 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200922 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100923 h2s->st = H2_SS_CLOSED;
924 }
925
Willy Tarreau27a84c92017-10-17 08:10:17 +0200926 return ret;
927}
928
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100929/* try to send an empty DATA frame with the ES flag set to notify about the
930 * end of stream and match a shutdown(write). If an ES was already sent as
931 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
932 * on success or zero if nothing was done. In case of lack of room to write the
933 * message, it subscribes the requesting stream to future notifications.
934 */
935static int h2_send_empty_data_es(struct h2s *h2s)
936{
937 struct h2c *h2c = h2s->h2c;
938 struct buffer *res;
939 char str[9];
940 int ret;
941
Willy Tarreau721c9742017-11-07 11:05:42 +0100942 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100943 return 1;
944
945 if (h2c_mux_busy(h2c, h2s)) {
946 h2s->flags |= H2_SF_BLK_MBUSY;
947 return 0;
948 }
949
950 res = h2_get_mbuf(h2c);
951 if (!res) {
952 h2c->flags |= H2_CF_MUX_MALLOC;
953 h2s->flags |= H2_SF_BLK_MROOM;
954 return 0;
955 }
956
957 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
958 memcpy(str, "\x00\x00\x00\x00\x01", 5);
959 write_n32(str + 5, h2s->id);
960 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100961 if (likely(ret > 0)) {
962 h2s->flags |= H2_SF_ES_SENT;
963 }
964 else if (!ret) {
965 h2c->flags |= H2_CF_MUX_MFULL;
966 h2s->flags |= H2_SF_BLK_MROOM;
967 return 0;
968 }
969 else {
970 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
971 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100972 }
973 return ret;
974}
975
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100976/* wake the streams attached to the connection, whose id is greater than <last>,
977 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
978 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
979 * stream's state is automatically updated accordingly.
980 */
981static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
982{
983 struct eb32_node *node;
984 struct h2s *h2s;
985
986 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
987 flags |= CS_FL_ERROR;
988
989 if (conn_xprt_read0_pending(h2c->conn))
990 flags |= CS_FL_EOS;
991
992 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
993 while (node) {
994 h2s = container_of(node, struct h2s, by_id);
995 if (h2s->id <= last)
996 break;
997 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +0100998
999 if (!h2s->cs) {
1000 /* this stream was already orphaned */
Willy Tarreau49745612017-12-03 18:56:02 +01001001 h2c->nb_streams--;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001002 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001003 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001004 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001005 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001006
1007 h2s->cs->flags |= flags;
1008 /* recv is used to force to detect CS_FL_EOS that wake()
1009 * doesn't handle in the stream int code.
1010 */
1011 h2s->cs->data_cb->recv(h2s->cs);
1012 h2s->cs->data_cb->wake(h2s->cs);
1013
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001014 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1015 h2s->st = H2_SS_ERROR;
1016 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1017 h2s->st = H2_SS_HREM;
1018 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
1019 h2s->st = H2_SS_CLOSED;
1020 }
1021}
1022
Willy Tarreau3421aba2017-07-27 15:41:03 +02001023/* Increase all streams' outgoing window size by the difference passed in
1024 * argument. This is needed upon receipt of the settings frame if the initial
1025 * window size is different. The difference may be negative and the resulting
1026 * window size as well, for the time it takes to receive some window updates.
1027 */
1028static void h2c_update_all_ws(struct h2c *h2c, int diff)
1029{
1030 struct h2s *h2s;
1031 struct eb32_node *node;
1032
1033 if (!diff)
1034 return;
1035
1036 node = eb32_first(&h2c->streams_by_id);
1037 while (node) {
1038 h2s = container_of(node, struct h2s, by_id);
1039 h2s->mws += diff;
1040 node = eb32_next(node);
1041 }
1042}
1043
1044/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1045 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1046 * return an error in h2c. Described in RFC7540#6.5.
1047 */
1048static int h2c_handle_settings(struct h2c *h2c)
1049{
1050 unsigned int offset;
1051 int error;
1052
1053 if (h2c->dff & H2_F_SETTINGS_ACK) {
1054 if (h2c->dfl) {
1055 error = H2_ERR_FRAME_SIZE_ERROR;
1056 goto fail;
1057 }
1058 return 1;
1059 }
1060
1061 if (h2c->dsi != 0) {
1062 error = H2_ERR_PROTOCOL_ERROR;
1063 goto fail;
1064 }
1065
1066 if (h2c->dfl % 6) {
1067 error = H2_ERR_FRAME_SIZE_ERROR;
1068 goto fail;
1069 }
1070
1071 /* that's the limit we can process */
1072 if (h2c->dfl > global.tune.bufsize) {
1073 error = H2_ERR_FRAME_SIZE_ERROR;
1074 goto fail;
1075 }
1076
1077 /* process full frame only */
1078 if (h2c->dbuf->i < h2c->dfl)
1079 return 0;
1080
1081 /* parse the frame */
1082 for (offset = 0; offset < h2c->dfl; offset += 6) {
1083 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1084 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1085
1086 switch (type) {
1087 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1088 /* we need to update all existing streams with the
1089 * difference from the previous iws.
1090 */
1091 if (arg < 0) { // RFC7540#6.5.2
1092 error = H2_ERR_FLOW_CONTROL_ERROR;
1093 goto fail;
1094 }
1095 h2c_update_all_ws(h2c, arg - h2c->miw);
1096 h2c->miw = arg;
1097 break;
1098 case H2_SETTINGS_MAX_FRAME_SIZE:
1099 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1100 error = H2_ERR_PROTOCOL_ERROR;
1101 goto fail;
1102 }
1103 h2c->mfs = arg;
1104 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001105 case H2_SETTINGS_ENABLE_PUSH:
1106 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1107 error = H2_ERR_PROTOCOL_ERROR;
1108 goto fail;
1109 }
1110 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001111 }
1112 }
1113
1114 /* need to ACK this frame now */
1115 h2c->st0 = H2_CS_FRAME_A;
1116 return 1;
1117 fail:
1118 h2c_error(h2c, error);
1119 return 0;
1120}
1121
1122/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1123 * success or one of the h2_status values.
1124 */
1125static int h2c_ack_settings(struct h2c *h2c)
1126{
1127 struct buffer *res;
1128 char str[9];
1129 int ret = -1;
1130
1131 if (h2c_mux_busy(h2c, NULL)) {
1132 h2c->flags |= H2_CF_DEM_MBUSY;
1133 return 0;
1134 }
1135
1136 res = h2_get_mbuf(h2c);
1137 if (!res) {
1138 h2c->flags |= H2_CF_MUX_MALLOC;
1139 h2c->flags |= H2_CF_DEM_MROOM;
1140 return 0;
1141 }
1142
1143 memcpy(str,
1144 "\x00\x00\x00" /* length : 0 (no data) */
1145 "\x04" "\x01" /* type : 4, flags : ACK */
1146 "\x00\x00\x00\x00" /* stream ID */, 9);
1147
1148 ret = bo_istput(res, ist2(str, 9));
1149 if (unlikely(ret <= 0)) {
1150 if (!ret) {
1151 h2c->flags |= H2_CF_MUX_MFULL;
1152 h2c->flags |= H2_CF_DEM_MROOM;
1153 return 0;
1154 }
1155 else {
1156 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1157 return 0;
1158 }
1159 }
1160 return ret;
1161}
1162
Willy Tarreaucf68c782017-10-10 17:11:41 +02001163/* processes a PING frame and schedules an ACK if needed. The caller must pass
1164 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1165 * missing data. It may return an error in h2c.
1166 */
1167static int h2c_handle_ping(struct h2c *h2c)
1168{
1169 /* frame length must be exactly 8 */
1170 if (h2c->dfl != 8) {
1171 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1172 return 0;
1173 }
1174
1175 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001176 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001177 h2c->st0 = H2_CS_FRAME_A;
1178 return 1;
1179}
1180
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001181/* Try to send a window update for stream id <sid> and value <increment>.
1182 * Returns > 0 on success or zero on missing room or failure. It may return an
1183 * error in h2c.
1184 */
1185static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1186{
1187 struct buffer *res;
1188 char str[13];
1189 int ret = -1;
1190
1191 if (h2c_mux_busy(h2c, NULL)) {
1192 h2c->flags |= H2_CF_DEM_MBUSY;
1193 return 0;
1194 }
1195
1196 res = h2_get_mbuf(h2c);
1197 if (!res) {
1198 h2c->flags |= H2_CF_MUX_MALLOC;
1199 h2c->flags |= H2_CF_DEM_MROOM;
1200 return 0;
1201 }
1202
1203 /* length: 4, type: 8, flags: none */
1204 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1205 write_n32(str + 5, sid);
1206 write_n32(str + 9, increment);
1207
1208 ret = bo_istput(res, ist2(str, 13));
1209
1210 if (unlikely(ret <= 0)) {
1211 if (!ret) {
1212 h2c->flags |= H2_CF_MUX_MFULL;
1213 h2c->flags |= H2_CF_DEM_MROOM;
1214 return 0;
1215 }
1216 else {
1217 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1218 return 0;
1219 }
1220 }
1221 return ret;
1222}
1223
1224/* try to send pending window update for the connection. It's safe to call it
1225 * with no pending updates. Returns > 0 on success or zero on missing room or
1226 * failure. It may return an error in h2c.
1227 */
1228static int h2c_send_conn_wu(struct h2c *h2c)
1229{
1230 int ret = 1;
1231
1232 if (h2c->rcvd_c <= 0)
1233 return 1;
1234
1235 /* send WU for the connection */
1236 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1237 if (ret > 0)
1238 h2c->rcvd_c = 0;
1239
1240 return ret;
1241}
1242
1243/* try to send pending window update for the current dmux stream. It's safe to
1244 * call it with no pending updates. Returns > 0 on success or zero on missing
1245 * room or failure. It may return an error in h2c.
1246 */
1247static int h2c_send_strm_wu(struct h2c *h2c)
1248{
1249 int ret = 1;
1250
1251 if (h2c->rcvd_s <= 0)
1252 return 1;
1253
1254 /* send WU for the stream */
1255 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1256 if (ret > 0)
1257 h2c->rcvd_s = 0;
1258
1259 return ret;
1260}
1261
Willy Tarreaucf68c782017-10-10 17:11:41 +02001262/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1263 * success, 0 on missing data or one of the h2_status values.
1264 */
1265static int h2c_ack_ping(struct h2c *h2c)
1266{
1267 struct buffer *res;
1268 char str[17];
1269 int ret = -1;
1270
1271 if (h2c->dbuf->i < 8)
1272 return 0;
1273
1274 if (h2c_mux_busy(h2c, NULL)) {
1275 h2c->flags |= H2_CF_DEM_MBUSY;
1276 return 0;
1277 }
1278
1279 res = h2_get_mbuf(h2c);
1280 if (!res) {
1281 h2c->flags |= H2_CF_MUX_MALLOC;
1282 h2c->flags |= H2_CF_DEM_MROOM;
1283 return 0;
1284 }
1285
1286 memcpy(str,
1287 "\x00\x00\x08" /* length : 8 (same payload) */
1288 "\x06" "\x01" /* type : 6, flags : ACK */
1289 "\x00\x00\x00\x00" /* stream ID */, 9);
1290
1291 /* copy the original payload */
1292 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1293
1294 ret = bo_istput(res, ist2(str, 17));
1295 if (unlikely(ret <= 0)) {
1296 if (!ret) {
1297 h2c->flags |= H2_CF_MUX_MFULL;
1298 h2c->flags |= H2_CF_DEM_MROOM;
1299 return 0;
1300 }
1301 else {
1302 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1303 return 0;
1304 }
1305 }
1306 return ret;
1307}
1308
Willy Tarreau26f95952017-07-27 17:18:30 +02001309/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1310 * Returns > 0 on success or zero on missing data. It may return an error in
1311 * h2c or h2s. Described in RFC7540#6.9.
1312 */
1313static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1314{
1315 int32_t inc;
1316 int error;
1317
1318 if (h2c->dfl != 4) {
1319 error = H2_ERR_FRAME_SIZE_ERROR;
1320 goto conn_err;
1321 }
1322
1323 /* process full frame only */
1324 if (h2c->dbuf->i < h2c->dfl)
1325 return 0;
1326
1327 inc = h2_get_n32(h2c->dbuf, 0);
1328
1329 if (h2c->dsi != 0) {
1330 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001331
1332 /* it's not an error to receive WU on a closed stream */
1333 if (h2s->st == H2_SS_CLOSED)
1334 return 1;
1335
1336 if (!inc) {
1337 error = H2_ERR_PROTOCOL_ERROR;
1338 goto strm_err;
1339 }
1340
1341 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1342 error = H2_ERR_FLOW_CONTROL_ERROR;
1343 goto strm_err;
1344 }
1345
1346 h2s->mws += inc;
1347 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1348 h2s->flags &= ~H2_SF_BLK_SFCTL;
1349 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1350 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1351 /* This stream wanted to send but could not due to its
1352 * own flow control. We can put it back into the send
1353 * list now, it will be handled upon next send() call.
1354 */
1355 LIST_ADDQ(&h2c->send_list, &h2s->list);
1356 }
1357 }
1358 }
1359 else {
1360 /* connection window update */
1361 if (!inc) {
1362 error = H2_ERR_PROTOCOL_ERROR;
1363 goto conn_err;
1364 }
1365
1366 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1367 error = H2_ERR_FLOW_CONTROL_ERROR;
1368 goto conn_err;
1369 }
1370
1371 h2c->mws += inc;
1372 }
1373
1374 return 1;
1375
1376 conn_err:
1377 h2c_error(h2c, error);
1378 return 0;
1379
1380 strm_err:
1381 if (h2s) {
1382 h2s_error(h2s, error);
1383 h2c->st0 = H2_CS_FRAME_A;
1384 }
1385 else
1386 h2c_error(h2c, error);
1387 return 0;
1388}
1389
Willy Tarreaue96b0922017-10-30 00:28:29 +01001390/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1391 * the last ID. Returns > 0 on success or zero on missing data. It may return
1392 * an error in h2c. Described in RFC7540#6.8.
1393 */
1394static int h2c_handle_goaway(struct h2c *h2c)
1395{
1396 int error;
1397 int last;
1398
1399 if (h2c->dsi != 0) {
1400 error = H2_ERR_PROTOCOL_ERROR;
1401 goto conn_err;
1402 }
1403
1404 if (h2c->dfl < 8) {
1405 error = H2_ERR_FRAME_SIZE_ERROR;
1406 goto conn_err;
1407 }
1408
1409 /* process full frame only */
1410 if (h2c->dbuf->i < h2c->dfl)
1411 return 0;
1412
1413 last = h2_get_n32(h2c->dbuf, 0);
1414 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1415 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001416 if (h2c->last_sid < 0)
1417 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001418 return 1;
1419
1420 conn_err:
1421 h2c_error(h2c, error);
1422 return 0;
1423}
1424
Willy Tarreaucd234e92017-08-18 10:59:39 +02001425/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1426 * Returns > 0 on success or zero on missing data. It may return an error in
1427 * h2c. Described in RFC7540#6.4.
1428 */
1429static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1430{
1431 int error;
1432
1433 if (h2c->dsi == 0) {
1434 error = H2_ERR_PROTOCOL_ERROR;
1435 goto conn_err;
1436 }
1437
Willy Tarreaucd234e92017-08-18 10:59:39 +02001438 if (h2c->dfl != 4) {
1439 error = H2_ERR_FRAME_SIZE_ERROR;
1440 goto conn_err;
1441 }
1442
1443 /* process full frame only */
1444 if (h2c->dbuf->i < h2c->dfl)
1445 return 0;
1446
1447 /* late RST, already handled */
1448 if (h2s->st == H2_SS_CLOSED)
1449 return 1;
1450
1451 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
1452 h2s->st = H2_SS_CLOSED;
1453
1454 if (h2s->cs) {
1455 h2s->cs->flags |= CS_FL_EOS;
1456 /* recv is used to force to detect CS_FL_EOS that wake()
1457 * doesn't handle in the stream-int code.
1458 */
1459 h2s->cs->data_cb->recv(h2s->cs);
1460 h2s->cs->data_cb->wake(h2s->cs);
1461 }
1462
1463 h2s->flags |= H2_SF_RST_RCVD;
1464 return 1;
1465
1466 conn_err:
1467 h2c_error(h2c, error);
1468 return 0;
1469}
1470
Willy Tarreau13278b42017-10-13 19:23:14 +02001471/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1472 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1473 * errors here are reported as connection errors since it's impossible to
1474 * recover from such errors after the compression context has been altered.
1475 */
1476static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1477{
1478 int error;
1479
1480 if (!h2c->dfl) {
1481 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1482 goto strm_err;
1483 }
1484
1485 if (!h2c->dbuf->size)
1486 return 0; // empty buffer
1487
1488 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1489 return 0; // incomplete frame
1490
1491 /* now either the frame is complete or the buffer is complete */
1492 if (h2s->st != H2_SS_IDLE) {
1493 /* FIXME: stream already exists, this is only allowed for
1494 * trailers (not supported for now).
1495 */
1496 error = H2_ERR_PROTOCOL_ERROR;
1497 goto conn_err;
1498 }
1499 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1500 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1501 error = H2_ERR_PROTOCOL_ERROR;
1502 goto conn_err;
1503 }
1504
1505 h2s = h2c_stream_new(h2c, h2c->dsi);
1506 if (!h2s) {
1507 error = H2_ERR_INTERNAL_ERROR;
1508 goto conn_err;
1509 }
1510
1511 h2s->st = H2_SS_OPEN;
1512 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1513 h2s->st = H2_SS_HREM;
1514 h2s->flags |= H2_SF_ES_RCVD;
1515 }
1516
1517 /* call the upper layers to process the frame, then let the upper layer
1518 * notify the stream about any change.
1519 */
1520 h2s->cs->data_cb->recv(h2s->cs);
1521
1522 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1523 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1524 error = H2_ERR_INTERNAL_ERROR;
1525 goto conn_err;
1526 }
1527
Willy Tarreau8f650c32017-11-21 19:36:21 +01001528 if (h2c->st0 >= H2_CS_ERROR)
1529 return 0;
1530
Willy Tarreau721c9742017-11-07 11:05:42 +01001531 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001532 /* stream error : send RST_STREAM */
1533 h2c->st0 = H2_CS_FRAME_A;
1534 }
1535 else {
1536 /* update the max stream ID if the request is being processed */
1537 if (h2s->id > h2c->max_id)
1538 h2c->max_id = h2s->id;
1539 }
1540
1541 return 1;
1542
1543 conn_err:
1544 h2c_error(h2c, error);
1545 return 0;
1546
1547 strm_err:
1548 if (h2s) {
1549 h2s_error(h2s, error);
1550 h2c->st0 = H2_CS_FRAME_A;
1551 }
1552 else
1553 h2c_error(h2c, error);
1554 return 0;
1555}
1556
Willy Tarreau454f9052017-10-26 19:40:35 +02001557/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1558 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1559 */
1560static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1561{
1562 int error;
1563
1564 /* note that empty DATA frames are perfectly valid and sometimes used
1565 * to signal an end of stream (with the ES flag).
1566 */
1567
1568 if (!h2c->dbuf->size && h2c->dfl)
1569 return 0; // empty buffer
1570
1571 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1572 return 0; // incomplete frame
1573
1574 /* now either the frame is complete or the buffer is complete */
1575
1576 if (!h2c->dsi) {
1577 /* RFC7540#6.1 */
1578 error = H2_ERR_PROTOCOL_ERROR;
1579 goto conn_err;
1580 }
1581
1582 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1583 /* RFC7540#6.1 */
1584 error = H2_ERR_STREAM_CLOSED;
1585 goto strm_err;
1586 }
1587
1588 /* last frame */
1589 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1590 h2s->st = H2_SS_HREM;
1591 h2s->flags |= H2_SF_ES_RCVD;
1592 }
1593
1594 /* call the upper layers to process the frame, then let the upper layer
1595 * notify the stream about any change.
1596 */
1597 if (!h2s->cs) {
1598 error = H2_ERR_STREAM_CLOSED;
1599 goto strm_err;
1600 }
1601
1602 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001603
Willy Tarreau454f9052017-10-26 19:40:35 +02001604 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1605 /* cs has just been destroyed, we have to kill h2s. */
1606 error = H2_ERR_STREAM_CLOSED;
1607 goto strm_err;
1608 }
1609
Willy Tarreau8f650c32017-11-21 19:36:21 +01001610 if (h2c->st0 >= H2_CS_ERROR)
1611 return 0;
1612
Willy Tarreau721c9742017-11-07 11:05:42 +01001613 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001614 /* stream error : send RST_STREAM */
1615 h2c->st0 = H2_CS_FRAME_A;
1616 }
1617
1618 /* check for completion : the callee will change this to FRAME_A or
1619 * FRAME_H once done.
1620 */
1621 if (h2c->st0 == H2_CS_FRAME_P)
1622 return 0;
1623
1624 return 1;
1625
1626 conn_err:
1627 h2c_error(h2c, error);
1628 return 0;
1629
1630 strm_err:
1631 if (h2s) {
1632 h2s_error(h2s, error);
1633 h2c->st0 = H2_CS_FRAME_A;
1634 }
1635 else
1636 h2c_error(h2c, error);
1637 return 0;
1638}
1639
Willy Tarreaubc933932017-10-09 16:21:43 +02001640/* process Rx frames to be demultiplexed */
1641static void h2_process_demux(struct h2c *h2c)
1642{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001643 struct h2s *h2s;
1644
Willy Tarreau081d4722017-05-16 21:51:05 +02001645 if (h2c->st0 >= H2_CS_ERROR)
1646 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001647
1648 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1649 if (h2c->st0 == H2_CS_PREFACE) {
1650 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1651 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1652 if (h2c->st0 == H2_CS_ERROR)
1653 h2c->st0 = H2_CS_ERROR2;
1654 goto fail;
1655 }
1656
1657 h2c->max_id = 0;
1658 h2c->st0 = H2_CS_SETTINGS1;
1659 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001660
1661 if (h2c->st0 == H2_CS_SETTINGS1) {
1662 struct h2_fh hdr;
1663
1664 /* ensure that what is pending is a valid SETTINGS frame
1665 * without an ACK.
1666 */
1667 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1668 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1669 if (h2c->st0 == H2_CS_ERROR)
1670 h2c->st0 = H2_CS_ERROR2;
1671 goto fail;
1672 }
1673
1674 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1675 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1676 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1677 h2c->st0 = H2_CS_ERROR2;
1678 goto fail;
1679 }
1680
1681 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1682 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1683 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1684 h2c->st0 = H2_CS_ERROR2;
1685 goto fail;
1686 }
1687
1688 /* that's OK, switch to FRAME_P to process it */
1689 h2c->dfl = hdr.len;
1690 h2c->dsi = hdr.sid;
1691 h2c->dft = hdr.ft;
1692 h2c->dff = hdr.ff;
1693 h2c->st0 = H2_CS_FRAME_P;
1694 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001695 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001696
1697 /* process as many incoming frames as possible below */
1698 while (h2c->dbuf->i) {
1699 int ret = 0;
1700
1701 if (h2c->st0 >= H2_CS_ERROR)
1702 break;
1703
1704 if (h2c->st0 == H2_CS_FRAME_H) {
1705 struct h2_fh hdr;
1706
1707 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1708 break;
1709
1710 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1711 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1712 h2c->st0 = H2_CS_ERROR;
1713 break;
1714 }
1715
1716 h2c->dfl = hdr.len;
1717 h2c->dsi = hdr.sid;
1718 h2c->dft = hdr.ft;
1719 h2c->dff = hdr.ff;
1720 h2c->st0 = H2_CS_FRAME_P;
1721 h2_skip_frame_hdr(h2c->dbuf);
1722 }
1723
1724 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001725 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001726
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001727 if (h2s->st == H2_SS_IDLE &&
1728 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1729 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1730 * this state MUST be treated as a connection error
1731 */
1732 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1733 h2c->st0 = H2_CS_ERROR;
1734 break;
1735 }
1736
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001737 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1738 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1739 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1740 * this state MUST be treated as a stream error
1741 */
1742 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1743 goto strm_err;
1744 }
1745
Willy Tarreauc0da1962017-10-30 18:38:00 +01001746#if 0
1747 // problem below: it is not possible to completely ignore such
1748 // streams as we need to maintain the compression state as well
1749 // and for this we need to completely process these frames (eg:
1750 // HEADERS frames) as well as counting DATA frames to emit
1751 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1752 // This is a typical case of layer violation where the
1753 // transported contents are critical to the connection's
1754 // validity and must be ignored at the same time :-(
1755
1756 /* graceful shutdown, ignore streams whose ID is higher than
1757 * the one advertised in GOAWAY. RFC7540#6.8.
1758 */
1759 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1760 ret = MIN(h2c->dbuf->i, h2c->dfl);
1761 bi_del(h2c->dbuf, ret);
1762 h2c->dfl -= ret;
1763 ret = h2c->dfl == 0;
1764 goto strm_err;
1765 }
1766#endif
1767
Willy Tarreau7e98c052017-10-10 15:56:59 +02001768 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001769 case H2_FT_SETTINGS:
1770 if (h2c->st0 == H2_CS_FRAME_P)
1771 ret = h2c_handle_settings(h2c);
1772
1773 if (h2c->st0 == H2_CS_FRAME_A)
1774 ret = h2c_ack_settings(h2c);
1775 break;
1776
Willy Tarreaucf68c782017-10-10 17:11:41 +02001777 case H2_FT_PING:
1778 if (h2c->st0 == H2_CS_FRAME_P)
1779 ret = h2c_handle_ping(h2c);
1780
1781 if (h2c->st0 == H2_CS_FRAME_A)
1782 ret = h2c_ack_ping(h2c);
1783 break;
1784
Willy Tarreau26f95952017-07-27 17:18:30 +02001785 case H2_FT_WINDOW_UPDATE:
1786 if (h2c->st0 == H2_CS_FRAME_P)
1787 ret = h2c_handle_window_update(h2c, h2s);
1788 break;
1789
Willy Tarreau61290ec2017-10-17 08:19:21 +02001790 case H2_FT_CONTINUATION:
1791 /* we currently don't support CONTINUATION frames since
1792 * we have nowhere to store the partial HEADERS frame.
1793 * Let's abort the stream on an INTERNAL_ERROR here.
1794 */
1795 if (h2c->st0 == H2_CS_FRAME_P)
1796 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1797 break;
1798
Willy Tarreau13278b42017-10-13 19:23:14 +02001799 case H2_FT_HEADERS:
1800 if (h2c->st0 == H2_CS_FRAME_P)
1801 ret = h2c_frt_handle_headers(h2c, h2s);
1802 break;
1803
Willy Tarreau454f9052017-10-26 19:40:35 +02001804 case H2_FT_DATA:
1805 if (h2c->st0 == H2_CS_FRAME_P)
1806 ret = h2c_frt_handle_data(h2c, h2s);
1807
1808 if (h2c->st0 == H2_CS_FRAME_A)
1809 ret = h2c_send_strm_wu(h2c);
1810 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001811
1812 case H2_FT_RST_STREAM:
1813 if (h2c->st0 == H2_CS_FRAME_P)
1814 ret = h2c_handle_rst_stream(h2c, h2s);
1815 break;
1816
Willy Tarreaue96b0922017-10-30 00:28:29 +01001817 case H2_FT_GOAWAY:
1818 if (h2c->st0 == H2_CS_FRAME_P)
1819 ret = h2c_handle_goaway(h2c);
1820 break;
1821
Willy Tarreau1c661982017-10-30 13:52:01 +01001822 case H2_FT_PUSH_PROMISE:
1823 /* not permitted here, RFC7540#5.1 */
1824 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001825 break;
1826
1827 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001828 default:
1829 /* drop frames that we ignore. They may be larger than
1830 * the buffer so we drain all of their contents until
1831 * we reach the end.
1832 */
1833 ret = MIN(h2c->dbuf->i, h2c->dfl);
1834 bi_del(h2c->dbuf, ret);
1835 h2c->dfl -= ret;
1836 ret = h2c->dfl == 0;
1837 }
1838
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001839 strm_err:
Willy Tarreau27a84c92017-10-17 08:10:17 +02001840 /* RST are sent similarly to frame acks */
1841 if (h2s->st == H2_SS_ERROR) {
1842 if (h2c->st0 == H2_CS_FRAME_P)
1843 h2c->st0 = H2_CS_FRAME_A;
1844
1845 if (h2c->st0 == H2_CS_FRAME_A)
1846 ret = h2c_send_rst_stream(h2c, h2s);
1847 }
1848
Willy Tarreau7e98c052017-10-10 15:56:59 +02001849 /* error or missing data condition met above ? */
1850 if (ret <= 0)
1851 break;
1852
1853 if (h2c->st0 != H2_CS_FRAME_H) {
1854 bi_del(h2c->dbuf, h2c->dfl);
1855 h2c->st0 = H2_CS_FRAME_H;
1856 }
1857 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001858
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001859 if (h2c->rcvd_c > 0 &&
1860 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1861 h2c_send_conn_wu(h2c);
1862
Willy Tarreau52eed752017-09-22 15:05:09 +02001863 fail:
1864 /* we can go here on missing data, blocked response or error */
1865 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001866}
1867
1868/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1869 * the end.
1870 */
1871static int h2_process_mux(struct h2c *h2c)
1872{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001873 struct h2s *h2s, *h2s_back;
1874
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001875 /* start by sending possibly pending window updates */
1876 if (h2c->rcvd_c > 0 &&
1877 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
1878 h2c_send_conn_wu(h2c) < 0)
1879 goto fail;
1880
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001881 /* First we always process the flow control list because the streams
1882 * waiting there were already elected for immediate emission but were
1883 * blocked just on this.
1884 */
1885
1886 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
1887 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
1888 h2c->st0 >= H2_CS_ERROR)
1889 break;
1890
1891 /* In theory it's possible that h2s->cs == NULL here :
1892 * - client sends crap that causes a parse error
1893 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1894 * - RST_STREAM cannot be emitted because mux is busy/full
1895 * - stream gets notified, detaches and quits
1896 * - mux buffer gets ready and wakes pending streams up
1897 * - bam!
1898 */
1899 h2s->flags &= ~H2_SF_BLK_ANY;
1900
1901 if (h2s->cs) {
1902 h2s->cs->data_cb->send(h2s->cs);
1903 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001904 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001905 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001906 }
1907
1908 /* depending on callee's blocking reasons, we may queue in send
1909 * list or completely dequeue.
1910 */
1911 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
1912 if (h2s->flags & H2_SF_BLK_ANY) {
1913 LIST_DEL(&h2s->list);
1914 LIST_ADDQ(&h2c->send_list, &h2s->list);
1915 }
1916 else {
1917 LIST_DEL(&h2s->list);
1918 LIST_INIT(&h2s->list);
1919 if (h2s->cs)
1920 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001921 else {
1922 /* just sent the last frame for this orphaned stream */
Willy Tarreau49745612017-12-03 18:56:02 +01001923 h2c->nb_streams--;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001924 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001925 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001926 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001927 }
1928 }
1929 }
1930
1931 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
1932 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
1933 break;
1934
1935 /* In theory it's possible that h2s->cs == NULL here :
1936 * - client sends crap that causes a parse error
1937 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1938 * - RST_STREAM cannot be emitted because mux is busy/full
1939 * - stream gets notified, detaches and quits
1940 * - mux buffer gets ready and wakes pending streams up
1941 * - bam!
1942 */
1943 h2s->flags &= ~H2_SF_BLK_ANY;
1944
1945 if (h2s->cs) {
1946 h2s->cs->data_cb->send(h2s->cs);
1947 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001948 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001949 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001950 }
1951 /* depending on callee's blocking reasons, we may queue in fctl
1952 * list or completely dequeue.
1953 */
1954 if (h2s->flags & H2_SF_BLK_MFCTL) {
1955 /* stream hit the connection's flow control */
1956 LIST_DEL(&h2s->list);
1957 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
1958 }
1959 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
1960 LIST_DEL(&h2s->list);
1961 LIST_INIT(&h2s->list);
1962 if (h2s->cs)
1963 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001964 else {
1965 /* just sent the last frame for this orphaned stream */
Willy Tarreau49745612017-12-03 18:56:02 +01001966 h2c->nb_streams--;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001967 eb32_delete(&h2s->by_id);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001968 pool_free(pool_head_h2s, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001969 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001970 }
1971 }
1972
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001973 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01001974 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001975 if (h2c->st0 == H2_CS_ERROR) {
1976 if (h2c->max_id >= 0) {
1977 h2c_send_goaway_error(h2c, NULL);
1978 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
1979 return 0;
1980 }
1981
1982 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
1983 }
1984 return 1;
1985 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001986 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02001987}
1988
Willy Tarreau71681172017-10-23 14:39:06 +02001989
Willy Tarreau62f52692017-10-08 23:01:42 +02001990/*********************************************************/
1991/* functions below are I/O callbacks from the connection */
1992/*********************************************************/
1993
1994/* callback called on recv event by the connection handler */
1995static void h2_recv(struct connection *conn)
1996{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001997 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001998 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001999 int max;
2000
2001 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002002 return;
2003
2004 if (h2c->flags & H2_CF_DEM_BLOCK_ANY)
2005 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002006
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002007 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002008 if (!buf) {
2009 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002010 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002011 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002012
Willy Tarreaua2af5122017-10-09 11:56:46 +02002013 /* note: buf->o == 0 */
2014 max = buf->size - buf->i;
2015 if (!max) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002016 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002017 return;
2018 }
2019
2020 conn->xprt->rcv_buf(conn, buf, max);
2021 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002022 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002023
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002024 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002025 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002026 return;
2027 }
2028
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002029 if (buf->i == buf->size)
2030 h2c->flags |= H2_CF_DEM_DFULL;
2031
Willy Tarreaubc933932017-10-09 16:21:43 +02002032 h2_process_demux(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002033
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002034 /* after streams have been processed, we should have made some room */
Willy Tarreau081d4722017-05-16 21:51:05 +02002035 if (h2c->st0 >= H2_CS_ERROR)
2036 buf->i = 0;
2037
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002038 if (buf->i != buf->size)
2039 h2c->flags &= ~H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002040 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002041}
2042
2043/* callback called on send event by the connection handler */
2044static void h2_send(struct connection *conn)
2045{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002046 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002047 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002048
2049 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002050 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002051
2052 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2053 /* a handshake was requested */
2054 return;
2055 }
2056
Willy Tarreaubc933932017-10-09 16:21:43 +02002057 /* This loop is quite simple : it tries to fill as much as it can from
2058 * pending streams into the existing buffer until it's reportedly full
2059 * or the end of send requests is reached. Then it tries to send this
2060 * buffer's contents out, marks it not full if at least one byte could
2061 * be sent, and tries again.
2062 *
2063 * The snd_buf() function normally takes a "flags" argument which may
2064 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2065 * data immediately comes and CO_SFL_STREAMER to indicate that the
2066 * connection is streaming lots of data (used to increase TLS record
2067 * size at the expense of latency). The former can be sent any time
2068 * there's a buffer full flag, as it indicates at least one stream
2069 * attempted to send and failed so there are pending data. An
2070 * alternative would be to set it as long as there's an active stream
2071 * but that would be problematic for ACKs until we have an absolute
2072 * guarantee that all waiters have at least one byte to send. The
2073 * latter should possibly not be set for now.
2074 */
2075
2076 done = 0;
2077 while (!done) {
2078 unsigned int flags = 0;
2079
2080 /* fill as much as we can into the current buffer */
2081 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2082 done = h2_process_mux(h2c);
2083
2084 if (conn->flags & CO_FL_ERROR)
2085 break;
2086
2087 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2088 flags |= CO_SFL_MSG_MORE;
2089
Willy Tarreau319994a2017-11-07 11:03:56 +01002090 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002091 break;
2092
2093 /* wrote at least one byte, the buffer is not full anymore */
2094 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2095 }
2096
Willy Tarreaua2af5122017-10-09 11:56:46 +02002097 if (conn->flags & CO_FL_SOCK_WR_SH) {
2098 /* output closed, nothing to send, clear the buffer to release it */
2099 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002100 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002101}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002102
Willy Tarreau62f52692017-10-08 23:01:42 +02002103/* callback called on any event by the connection handler.
2104 * It applies changes and returns zero, or < 0 if it wants immediate
2105 * destruction of the connection (which normally doesn not happen in h2).
2106 */
2107static int h2_wake(struct connection *conn)
2108{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002109 struct h2c *h2c = conn->mux_ctx;
2110
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002111 /*
2112 * If we received early data, try to wake any stream, just in case
2113 * at least one of them was waiting for the handshake
2114 */
2115 if ((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA | CO_FL_HANDSHAKE)) ==
2116 CO_FL_EARLY_DATA) {
2117 h2_wake_some_streams(h2c, 0, 0);
2118 conn->flags &= ~CO_FL_EARLY_DATA;
2119 }
Willy Tarreau26bd7612017-10-09 16:47:04 +02002120 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002121 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2122 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2123 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002124 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002125
2126 if (eb_is_empty(&h2c->streams_by_id)) {
2127 /* no more stream, kill the connection now */
2128 h2_release(conn);
2129 return -1;
2130 }
2131 else {
2132 /* some streams still there, we need to signal them all and
2133 * wait for their departure.
2134 */
2135 __conn_xprt_stop_recv(conn);
2136 __conn_xprt_stop_send(conn);
2137 return 0;
2138 }
2139 }
2140
2141 if (!h2c->dbuf->i)
2142 h2_release_dbuf(h2c);
2143
2144 /* stop being notified of incoming data if we can't process them */
2145 if (h2c->st0 >= H2_CS_ERROR ||
2146 (h2c->flags & H2_CF_DEM_BLOCK_ANY) || conn_xprt_read0_pending(conn)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002147 __conn_xprt_stop_recv(conn);
2148 }
2149 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002150 __conn_xprt_want_recv(conn);
2151 }
2152
2153 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002154 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2155 (h2c->st0 == H2_CS_ERROR ||
2156 h2c->mbuf->o ||
2157 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2158 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002159 __conn_xprt_want_send(conn);
2160 }
2161 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002162 h2_release_mbuf(h2c);
2163 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002164 }
2165
Willy Tarreau3f133572017-10-31 19:21:06 +01002166 if (h2c->task) {
2167 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002168 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002169 task_queue(h2c->task);
2170 }
2171 else
2172 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002173 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002174 return 0;
2175}
2176
Willy Tarreauea392822017-10-31 10:02:25 +01002177/* Connection timeout management. The principle is that if there's no receipt
2178 * nor sending for a certain amount of time, the connection is closed. If the
2179 * MUX buffer still has lying data or is not allocatable, the connection is
2180 * immediately killed. If it's allocatable and empty, we attempt to send a
2181 * GOAWAY frame.
2182 */
2183static struct task *h2_timeout_task(struct task *t)
2184{
2185 struct h2c *h2c = t->context;
2186 int expired = tick_is_expired(t->expire, now_ms);
2187
2188 if (!expired)
2189 return t;
2190
2191 h2c_error(h2c, H2_ERR_NO_ERROR);
2192 h2_wake_some_streams(h2c, 0, 0);
2193
2194 if (h2c->mbuf->o) {
2195 /* don't even try to send a GOAWAY, the buffer is stuck */
2196 h2c->flags |= H2_CF_GOAWAY_FAILED;
2197 }
2198
2199 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002200 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002201 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2202 h2c->flags |= H2_CF_GOAWAY_FAILED;
2203
2204 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2205 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2206
2207 if (!eb_is_empty(&h2c->streams_by_id))
2208 goto wait;
2209
2210 h2_release(h2c->conn);
2211 return NULL;
2212
2213 wait:
2214 /* the streams have been notified, we must let them finish and close */
2215 h2c->task = NULL;
2216 task_delete(t);
2217 task_free(t);
2218 return NULL;
2219}
2220
2221
Willy Tarreau62f52692017-10-08 23:01:42 +02002222/*******************************************/
2223/* functions below are used by the streams */
2224/*******************************************/
2225
2226/*
2227 * Attach a new stream to a connection
2228 * (Used for outgoing connections)
2229 */
2230static struct conn_stream *h2_attach(struct connection *conn)
2231{
2232 return NULL;
2233}
2234
2235/* callback used to update the mux's polling flags after changing a cs' status.
2236 * The caller (cs_update_mux_polling) will take care of propagating any changes
2237 * to the transport layer.
2238 */
2239static void h2_update_poll(struct conn_stream *cs)
2240{
Willy Tarreau1d393222017-10-17 10:26:19 +02002241 struct h2s *h2s = cs->ctx;
2242
2243 if (!h2s)
2244 return;
2245
Willy Tarreaud7739c82017-10-30 15:38:23 +01002246 /* we may unblock a blocked read */
2247
2248 if (cs->flags & CS_FL_DATA_RD_ENA &&
2249 h2s->h2c->flags & H2_CF_DEM_SFULL && h2s->h2c->dsi == h2s->id) {
2250 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
2251 conn_xprt_want_recv(cs->conn);
2252 }
2253
Willy Tarreau1d393222017-10-17 10:26:19 +02002254 /* Note: the stream and stream-int code doesn't allow us to perform a
2255 * synchronous send() here unfortunately, because this code is called
2256 * as si_update() from the process_stream() context. This means that
2257 * we have to queue the current cs and defer its processing after the
2258 * connection's cs list is processed anyway.
2259 */
2260
2261 if (cs->flags & CS_FL_DATA_WR_ENA) {
2262 if (LIST_ISEMPTY(&h2s->list)) {
2263 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2264 !h2s->h2c->mbuf->o && // not yet subscribed
2265 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2266 conn_xprt_want_send(cs->conn);
2267 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2268 }
2269 }
2270 else if (!LIST_ISEMPTY(&h2s->list)) {
2271 LIST_DEL(&h2s->list);
2272 LIST_INIT(&h2s->list);
2273 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2274 }
2275
2276 /* this can happen from within si_chk_snd() */
2277 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2278 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002279}
2280
2281/*
2282 * Detach the stream from the connection and possibly release the connection.
2283 */
2284static void h2_detach(struct conn_stream *cs)
2285{
Willy Tarreau60935142017-10-16 18:11:19 +02002286 struct h2s *h2s = cs->ctx;
2287 struct h2c *h2c;
2288
2289 cs->ctx = NULL;
2290 if (!h2s)
2291 return;
2292
2293 h2c = h2s->h2c;
2294 h2s->cs = NULL;
2295
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002296 /* this stream may be blocked waiting for some data to leave (possibly
2297 * an ES or RST frame), so orphan it in this case.
2298 */
2299 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2300 return;
2301
Willy Tarreau541dd822017-11-23 18:12:50 +01002302 /* the stream could be in the send list */
2303 LIST_DEL(&h2s->list);
2304
Willy Tarreau45f752e2017-10-30 15:44:59 +01002305 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2306 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2307 /* unblock the connection if it was blocked on this
2308 * stream.
2309 */
2310 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2311 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2312 conn_xprt_want_recv(cs->conn);
2313 conn_xprt_want_send(cs->conn);
2314 }
2315
Willy Tarreau60935142017-10-16 18:11:19 +02002316 if (h2s->by_id.node.leaf_p) {
2317 /* h2s still attached to the h2c */
Willy Tarreau49745612017-12-03 18:56:02 +01002318 h2c->nb_streams--;
Willy Tarreau60935142017-10-16 18:11:19 +02002319 eb32_delete(&h2s->by_id);
2320
2321 /* We don't want to close right now unless we're removing the
2322 * last stream, and either the connection is in error, or it
2323 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002324 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002325 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002326 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2327 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002328 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002329 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2330 (conn_xprt_read0_pending(h2c->conn) ||
2331 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002332 /* no more stream will come, kill it now */
2333 h2_release(h2c->conn);
2334 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002335 else if (h2c->task) {
2336 if (eb_is_empty(&h2c->streams_by_id)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002337 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002338 task_queue(h2c->task);
2339 }
2340 else
2341 h2c->task->expire = TICK_ETERNITY;
2342 }
Willy Tarreau60935142017-10-16 18:11:19 +02002343 }
Willy Tarreaubafbe012017-11-24 17:34:44 +01002344 pool_free(pool_head_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002345}
2346
2347static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2348{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002349 struct h2s *h2s = cs->ctx;
2350
2351 if (!mode)
2352 return;
2353
Willy Tarreau721c9742017-11-07 11:05:42 +01002354 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002355 return;
2356
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002357 /* if no outgoing data was seen on this stream, it means it was
2358 * closed with a "tcp-request content" rule that is normally
2359 * used to kill the connection ASAP (eg: limit abuse). In this
2360 * case we send a goaway to close the connection.
2361 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002362 if (!(h2s->flags & H2_SF_RST_SENT) &&
2363 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2364 return;
2365
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002366 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2367 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2368 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2369 return;
2370
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002371 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2372 conn_xprt_want_send(cs->conn);
2373
2374 h2s->st = H2_SS_CLOSED;
Willy Tarreau62f52692017-10-08 23:01:42 +02002375}
2376
2377static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2378{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002379 struct h2s *h2s = cs->ctx;
2380
Willy Tarreau721c9742017-11-07 11:05:42 +01002381 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002382 return;
2383
Willy Tarreau67434202017-11-06 20:20:51 +01002384 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002385 /* we can cleanly close using an empty data frame only after headers */
2386
2387 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2388 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002389 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002390
2391 if (h2s->st == H2_SS_HREM)
2392 h2s->st = H2_SS_CLOSED;
2393 else
2394 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002395 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002396 /* if no outgoing data was seen on this stream, it means it was
2397 * closed with a "tcp-request content" rule that is normally
2398 * used to kill the connection ASAP (eg: limit abuse). In this
2399 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002400 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002401 if (!(h2s->flags & H2_SF_RST_SENT) &&
2402 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2403 return;
2404
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002405 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2406 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002407 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2408 return;
2409
Willy Tarreau58e32082017-11-07 14:41:09 +01002410 h2s->st = H2_SS_CLOSED;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002411 }
2412
2413 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2414 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002415}
2416
Willy Tarreau13278b42017-10-13 19:23:14 +02002417/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2418 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2419 * proceed. Stream errors are reported in h2s->errcode and connection errors
2420 * in h2c->errcode. The caller must already have checked the frame header and
2421 * ensured that the frame was complete or the buffer full.
2422 */
2423static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2424{
2425 struct h2c *h2c = h2s->h2c;
2426 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002427 struct chunk *tmp = get_trash_chunk();
2428 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002429 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002430 int flen = h2c->dfl;
2431 int outlen = 0;
2432 int wrap;
2433 int try;
2434
2435 if (!h2c->dfl) {
2436 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
2437 return 0;
2438 }
2439
2440 /* if the input buffer wraps, take a temporary copy of it (rare) */
2441 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2442 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002443 copy = alloc_trash_chunk();
2444 if (!copy) {
2445 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2446 goto fail;
2447 }
2448 memcpy(copy->str, h2c->dbuf->p, wrap);
2449 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2450 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002451 }
2452
2453 /* The padlen is the first byte before data, and the padding appears
2454 * after data. padlen+data+padding are included in flen.
2455 */
2456 if (h2c->dff & H2_F_HEADERS_PADDED) {
2457 if (*hdrs >= flen) {
2458 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2459 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002460 return 0;
2461 }
2462 flen -= *hdrs + 1;
2463 hdrs += 1; // skip Pad Length
2464 }
2465
2466 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2467 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
2468 hdrs += 5; // stream dep = 4, weight = 1
2469 flen -= 5;
2470 }
2471
2472 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2473 * don't support this for now and can't even decompress so we have to
2474 * break the connection.
2475 */
2476 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2477 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002478 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002479 }
2480
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002481 /* we can't retry a failed decompression operation so we must be very
2482 * careful not to take any risks. In practice the output buffer is
2483 * always empty except maybe for trailers, so these operations almost
2484 * never happen.
2485 */
2486 if (unlikely(buf->o)) {
2487 /* need to let the output buffer flush and
2488 * mark the buffer for later wake up.
2489 */
2490 goto fail;
2491 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002492
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002493 if (unlikely(buffer_space_wraps(buf))) {
2494 /* it doesn't fit and the buffer is fragmented,
2495 * so let's defragment it and try again.
2496 */
2497 buffer_slow_realign(buf);
2498 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002499
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002500 /* first check if we have some room after p+i */
2501 try = buf->data + buf->size - (buf->p + buf->i);
2502
2503 /* otherwise continue between data and p-o */
2504 if (try <= 0) {
2505 try = buf->p - (buf->data + buf->o);
2506 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002507 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002508 }
2509 if (try > count)
2510 try = count;
2511
2512 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2513 sizeof(list)/sizeof(list[0]), tmp);
2514 if (outlen < 0) {
2515 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2516 goto fail;
2517 }
2518
2519 /* OK now we have our header list in <list> */
2520 outlen = h2_make_h1_request(list, bi_end(buf), try);
2521
2522 if (outlen < 0) {
2523 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2524 goto fail;
2525 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002526
2527 /* now consume the input data */
2528 bi_del(h2c->dbuf, h2c->dfl);
2529 h2c->st0 = H2_CS_FRAME_H;
2530 buf->i += outlen;
2531
2532 /* don't send it before returning data!
2533 * FIXME: should we instead try to send it much later, after the
2534 * response ? This would require that we keep a copy of it in h2s.
2535 */
2536 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2537 h2s->cs->flags |= CS_FL_EOS;
2538 h2s->flags |= H2_SF_ES_RCVD;
2539 }
2540
Willy Tarreau68dd9852017-07-03 14:44:26 +02002541 leave:
2542 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002543 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002544 fail:
2545 outlen = 0;
2546 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002547}
2548
Willy Tarreau454f9052017-10-26 19:40:35 +02002549/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2550 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2551 * in use, a new chunk is emitted for each frame. This is supposed to fit
2552 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2553 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2554 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2555 * parser state is automatically updated. Returns the number of bytes emitted
2556 * if > 0, or 0 if it couldn't proceed. Stream errors are reported in
2557 * h2s->errcode and connection errors in h2c->errcode. The caller must already
2558 * have checked the frame header and ensured that the frame was complete or the
2559 * buffer full. It changes the frame state to FRAME_A once done.
2560 */
2561static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2562{
2563 struct h2c *h2c = h2s->h2c;
2564 int block1, block2;
2565 unsigned int flen = h2c->dfl;
2566 unsigned int padlen = 0;
2567 int offset = 0;
2568
2569 if (h2c->dbuf->i < flen)
2570 return 0;
2571
2572 /* The padlen is the first byte before data, and the padding appears
2573 * after data. padlen+data+padding are included in flen.
2574 */
2575 if (h2c->dff & H2_F_HEADERS_PADDED) {
2576 padlen = *(uint8_t *)bi_ptr(h2c->dbuf);
2577 if (padlen >= flen) {
2578 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2579 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002580 return 0;
2581 }
2582 flen -= padlen + 1;
2583 offset = 1; // skip Pad Length
2584 }
2585
2586 /* does it fit in output buffer or should we wait ? */
2587 if (buf->i + buf->o + flen > buf->size) {
2588 h2c->flags |= H2_CF_DEM_SFULL;
2589 return 0;
2590 }
2591
2592 /* Block1 is the length of the first block before the buffer wraps,
2593 * block2 is the optional second block to reach the end of the frame.
2594 */
2595 block1 = bi_contig_data(h2c->dbuf);
2596 if (block1 > offset + flen)
2597 block1 = offset + flen;
2598 block1 -= offset; // skip Pad Length
2599 block2 = flen - block1;
2600
2601 if (block1)
2602 bi_putblk(buf, b_ptr(h2c->dbuf, offset), block1);
2603
2604 if (block2)
2605 bi_putblk(buf, b_ptr(h2c->dbuf, offset + block1), block2);
2606
2607 /* now mark the input data as consumed (will be deleted from the buffer
2608 * by the caller when seeing FRAME_A after sending the window update).
2609 */
2610 h2c->rcvd_c += h2c->dfl;
2611 h2c->rcvd_s += h2c->dfl; // warning, this can also affect the closed streams!
2612 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2613
2614 /* don't send it before returning data!
2615 * FIXME: should we instead try to send it much later, after the
2616 * response ? This would require that we keep a copy of it in h2s.
2617 */
2618 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2619 h2s->cs->flags |= CS_FL_EOS;
2620 h2s->flags |= H2_SF_ES_RCVD;
2621 }
2622
2623 return flen;
2624}
2625
Willy Tarreau62f52692017-10-08 23:01:42 +02002626/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002627 * Called from the upper layer to get more data, up to <count> bytes. The
2628 * caller is responsible for never asking for more data than what is available
2629 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002630 */
2631static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2632{
Willy Tarreau13278b42017-10-13 19:23:14 +02002633 struct h2s *h2s = cs->ctx;
2634 struct h2c *h2c = h2s->h2c;
2635 int ret = 0;
2636
2637 if (h2c->st0 != H2_CS_FRAME_P)
2638 return 0; // no pre-parsed frame yet
2639
2640 if (h2c->dsi != h2s->id)
2641 return 0; // not for us
2642
2643 if (!h2c->dbuf->size)
2644 return 0; // empty buffer
2645
2646 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2647 return 0; // incomplete input frame
2648
2649 switch (h2c->dft) {
2650 case H2_FT_HEADERS:
2651 ret = h2_frt_decode_headers(h2s, buf, count);
2652 break;
2653
Willy Tarreau454f9052017-10-26 19:40:35 +02002654 case H2_FT_DATA:
2655 ret = h2_frt_transfer_data(h2s, buf, count);
2656 break;
2657
Willy Tarreau13278b42017-10-13 19:23:14 +02002658 default:
2659 ret = 0;
2660 }
2661 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002662}
2663
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002664/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2665 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2666 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2667 * to the number of buffer bytes consumed.
2668 */
2669static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2670{
2671 struct http_hdr list[MAX_HTTP_HDR];
2672 struct h2c *h2c = h2s->h2c;
2673 struct h1m *h1m = &h2s->res;
2674 struct chunk outbuf;
2675 int es_now = 0;
2676 int ret = 0;
2677 int hdr;
2678
2679 if (h2c_mux_busy(h2c, h2s)) {
2680 h2s->flags |= H2_SF_BLK_MBUSY;
2681 return 0;
2682 }
2683
2684 if (!h2_get_mbuf(h2c)) {
2685 h2c->flags |= H2_CF_MUX_MALLOC;
2686 h2s->flags |= H2_SF_BLK_MROOM;
2687 return 0;
2688 }
2689
2690 /* First, try to parse the H1 response and index it into <list>.
2691 * NOTE! Since it comes from haproxy, we *know* that a response header
2692 * block does not wrap and we can safely read it this way without
2693 * having to realign the buffer.
2694 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002695 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002696 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2697 list, sizeof(list)/sizeof(list[0]), h1m);
2698 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002699 /* incomplete or invalid response, this is abnormal coming from
2700 * haproxy and may only result in a bad errorfile or bad Lua code
2701 * so that won't be fixed, raise an error now.
2702 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002703 * FIXME: we should instead add the ability to only return a
2704 * 502 bad gateway. But in theory this is not supposed to
2705 * happen.
2706 */
2707 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2708 ret = 0;
2709 goto end;
2710 }
2711
2712 chunk_reset(&outbuf);
2713
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002714 try_again:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002715 while (1) {
2716 outbuf.str = bo_end(h2c->mbuf);
2717 outbuf.size = bo_contig_space(h2c->mbuf);
2718 outbuf.len = 0;
2719
2720 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2721 break;
2722 realign_again:
2723 buffer_slow_realign(h2c->mbuf);
2724 }
2725
2726 if (outbuf.size < 9) {
2727 h2c->flags |= H2_CF_MUX_MFULL;
2728 h2s->flags |= H2_SF_BLK_MROOM;
2729 ret = 0;
2730 goto end;
2731 }
2732
2733 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2734 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2735 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2736 outbuf.len = 9;
2737
2738 /* encode status, which necessarily is the first one */
2739 if (outbuf.len < outbuf.size && h1m->status == 200)
2740 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2741 else if (outbuf.len < outbuf.size && h1m->status == 304)
2742 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002743 else if (unlikely(list[0].v.len != 3)) {
2744 /* this is an unparsable response */
2745 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2746 ret = 0;
2747 goto end;
2748 }
2749 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002750 /* basic encoding of the status code */
2751 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2752 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2753 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2754 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2755 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2756 }
2757 else {
2758 if (buffer_space_wraps(h2c->mbuf))
2759 goto realign_again;
2760
2761 h2c->flags |= H2_CF_MUX_MFULL;
2762 h2s->flags |= H2_SF_BLK_MROOM;
2763 ret = 0;
2764 goto end;
2765 }
2766
2767 /* encode all headers, stop at empty name */
2768 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002769 /* these ones do not exist in H2 and must be dropped. */
2770 if (isteq(list[hdr].n, ist("connection")) ||
2771 isteq(list[hdr].n, ist("proxy-connection")) ||
2772 isteq(list[hdr].n, ist("keep-alive")) ||
2773 isteq(list[hdr].n, ist("upgrade")) ||
2774 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002775 continue;
2776
2777 if (isteq(list[hdr].n, ist("")))
2778 break; // end
2779
2780 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2781 /* output full */
2782 if (buffer_space_wraps(h2c->mbuf))
2783 goto realign_again;
2784
2785 h2c->flags |= H2_CF_MUX_MFULL;
2786 h2s->flags |= H2_SF_BLK_MROOM;
2787 ret = 0;
2788 goto end;
2789 }
2790 }
2791
2792 /* we may need to add END_STREAM */
2793 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2794 es_now = 1;
2795
2796 /* update the frame's size */
2797 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2798
2799 if (es_now)
2800 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2801
2802 /* consume incoming H1 response */
2803 bo_del(buf, ret);
2804
2805 /* commit the H2 response */
2806 h2c->mbuf->o += outbuf.len;
2807 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01002808 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002809
2810 /* for now we don't implemented CONTINUATION, so we wait for a
2811 * body or directly end in TRL2.
2812 */
2813 if (es_now) {
2814 h1m->state = HTTP_MSG_DONE;
2815 h2s->flags |= H2_SF_ES_SENT;
2816 if (h2s->st == H2_SS_OPEN)
2817 h2s->st = H2_SS_HLOC;
2818 else
2819 h2s->st = H2_SS_CLOSED;
2820 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01002821 else if (h1m->status >= 100 && h1m->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01002822 /* we'll let the caller check if it has more headers to send */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002823 h1m->state = HTTP_MSG_RPBEFORE;
2824 h1m->status = 0;
2825 h1m->flags = 0;
Willy Tarreau87285592017-11-29 15:41:32 +01002826 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01002827 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002828 else
2829 h1m->state = (h1m->flags & H1_MF_CLEN) ? HTTP_MSG_BODY : HTTP_MSG_CHUNK_SIZE;
2830
2831 end:
2832 //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));
2833 return ret;
2834}
2835
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002836/* Try to send a DATA frame matching HTTP/1 response present in the response
2837 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
2838 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
2839 * corresponds to the number of buffer bytes consumed.
2840 */
2841static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
2842{
2843 struct h2c *h2c = h2s->h2c;
2844 struct h1m *h1m = &h2s->res;
2845 struct chunk outbuf;
2846 int ret = 0;
2847 int total = 0;
2848 int es_now = 0;
2849 int size = 0;
2850 char *blk1, *blk2;
2851 int len1, len2;
2852
2853 if (h2c_mux_busy(h2c, h2s)) {
2854 h2s->flags |= H2_SF_BLK_MBUSY;
2855 goto end;
2856 }
2857
2858 if (!h2_get_mbuf(h2c)) {
2859 h2c->flags |= H2_CF_MUX_MALLOC;
2860 h2s->flags |= H2_SF_BLK_MROOM;
2861 goto end;
2862 }
2863
2864 new_frame:
2865 if (!buf->o)
2866 goto end;
2867
2868 chunk_reset(&outbuf);
2869
2870 while (1) {
2871 outbuf.str = bo_end(h2c->mbuf);
2872 outbuf.size = bo_contig_space(h2c->mbuf);
2873 outbuf.len = 0;
2874
2875 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2876 break;
2877 realign_again:
2878 buffer_slow_realign(h2c->mbuf);
2879 }
2880
2881 if (outbuf.size < 9) {
2882 h2c->flags |= H2_CF_MUX_MFULL;
2883 h2s->flags |= H2_SF_BLK_MROOM;
2884 goto end;
2885 }
2886
2887 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
2888 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
2889 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2890 outbuf.len = 9;
2891
2892 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
2893 case 0: /* no content length, read till SHUTW */
2894 size = buf->o;
2895 break;
2896 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
2897 size = buf->o;
2898 if ((long long)size > h1m->curr_len)
2899 size = h1m->curr_len;
2900 break;
2901 default: /* te:chunked : parse chunks */
2902 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
2903 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
2904 if (!ret)
2905 goto end;
2906
2907 if (ret < 0) {
2908 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2909 h1m->err_pos = ret;
2910 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2911 goto end;
2912 }
2913 bo_del(buf, ret);
2914 total += ret;
2915 h1m->state = HTTP_MSG_CHUNK_SIZE;
2916 }
2917
2918 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
2919 unsigned int chunk;
2920
2921 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
2922 if (!ret)
2923 goto end;
2924
2925 if (ret < 0) {
2926 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2927 h1m->err_pos = ret;
2928 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2929 goto end;
2930 }
2931
2932 size = chunk;
2933 h1m->curr_len = chunk;
2934 h1m->body_len += chunk;
2935 bo_del(buf, ret);
2936 total += ret;
2937 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
2938 if (!size)
2939 goto send_empty;
2940 }
2941
2942 /* in MSG_DATA state, continue below */
2943 size = h1m->curr_len;
2944 break;
2945 }
2946
2947 /* we have in <size> the exact number of bytes we need to copy from
2948 * the H1 buffer. We need to check this against the connection's and
2949 * the stream's send windows, and to ensure that this fits in the max
2950 * frame size and in the buffer's available space minus 9 bytes (for
2951 * the frame header). The connection's flow control is applied last so
2952 * that we can use a separate list of streams which are immediately
2953 * unblocked on window opening. Note: we don't implement padding.
2954 */
2955
2956 if (size > buf->o)
2957 size = buf->o;
2958
2959 if (size > h2s->mws)
2960 size = h2s->mws;
2961
2962 if (size <= 0) {
2963 h2s->flags |= H2_SF_BLK_SFCTL;
2964 goto end;
2965 }
2966
2967 if (h2c->mfs && size > h2c->mfs)
2968 size = h2c->mfs;
2969
2970 if (size + 9 > outbuf.size) {
2971 /* we have an opportunity for enlarging the too small
2972 * available space, let's try.
2973 */
2974 if (buffer_space_wraps(h2c->mbuf))
2975 goto realign_again;
2976 size = outbuf.size - 9;
2977 }
2978
2979 if (size <= 0) {
2980 h2c->flags |= H2_CF_MUX_MFULL;
2981 h2s->flags |= H2_SF_BLK_MROOM;
2982 goto end;
2983 }
2984
2985 if (size > h2c->mws)
2986 size = h2c->mws;
2987
2988 if (size <= 0) {
2989 h2s->flags |= H2_SF_BLK_MFCTL;
2990 goto end;
2991 }
2992
2993 /* copy whatever we can */
2994 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
2995 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
2996 if (ret == 1)
2997 len2 = 0;
2998
2999 if (!ret || len1 + len2 < size) {
3000 /* FIXME: must normally never happen */
3001 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3002 goto end;
3003 }
3004
3005 /* limit len1/len2 to size */
3006 if (len1 + len2 > size) {
3007 int sub = len1 + len2 - size;
3008
3009 if (len2 > sub)
3010 len2 -= sub;
3011 else {
3012 sub -= len2;
3013 len2 = 0;
3014 len1 -= sub;
3015 }
3016 }
3017
3018 /* now let's copy this this into the output buffer */
3019 memcpy(outbuf.str + 9, blk1, len1);
3020 if (len2)
3021 memcpy(outbuf.str + 9 + len1, blk2, len2);
3022
3023 send_empty:
3024 /* we may need to add END_STREAM */
3025 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3026 * could rely on the MSG_MORE flag as a hint for this ?
3027 */
3028 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3029 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3030 es_now = 1;
3031
3032 /* update the frame's size */
3033 h2_set_frame_size(outbuf.str, size);
3034
3035 if (es_now)
3036 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3037
3038 /* commit the H2 response */
3039 h2c->mbuf->o += size + 9;
3040 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3041
3042 /* consume incoming H1 response */
3043 if (size > 0) {
3044 bo_del(buf, size);
3045 total += size;
3046 h1m->curr_len -= size;
3047 h2s->mws -= size;
3048 h2c->mws -= size;
3049
3050 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3051 h1m->state = HTTP_MSG_CHUNK_CRLF;
3052 goto new_frame;
3053 }
3054 }
3055
3056 if (es_now) {
3057 if (h2s->st == H2_SS_OPEN)
3058 h2s->st = H2_SS_HLOC;
3059 else
3060 h2s->st = H2_SS_CLOSED;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003061
3062 if (!(h1m->flags & H1_MF_CHNK))
3063 h1m->state = HTTP_MSG_DONE;
3064
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003065 h2s->flags |= H2_SF_ES_SENT;
3066 }
3067
3068 end:
3069 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);
3070 return total;
3071}
3072
Willy Tarreau62f52692017-10-08 23:01:42 +02003073/* Called from the upper layer, to send data */
3074static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3075{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003076 struct h2s *h2s = cs->ctx;
3077 int total = 0;
3078
Willy Tarreauc4312d32017-11-07 12:01:53 +01003079 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3080 h2s->flags |= H2_SF_OUTGOING_DATA;
3081
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003082 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3083 if (h2s->res.state < HTTP_MSG_BODY) {
3084 total += h2s_frt_make_resp_headers(h2s, buf);
3085
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003086 if (h2s->st >= H2_SS_ERROR)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003087 break;
3088
3089 if (h2s->flags & H2_SF_BLK_ANY)
3090 break;
3091 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003092 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3093 total += h2s_frt_make_resp_data(h2s, buf);
3094
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003095 if (h2s->st >= H2_SS_ERROR)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003096 break;
3097
3098 if (h2s->flags & H2_SF_BLK_ANY)
3099 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003100 }
3101 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3102 /* consume the trailers if any (we don't forward them for now) */
3103 int count = h1_measure_trailers(buf);
3104
3105 if (unlikely(count <= 0)) {
3106 if (count < 0)
3107 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3108 break;
3109 }
3110 total += count;
3111 bo_del(buf, count);
3112 h2s->res.state = HTTP_MSG_DONE;
3113 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003114 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003115 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003116 cs->flags |= CS_FL_ERROR;
3117 break;
3118 }
3119 }
3120
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003121 /* RST are sent similarly to frame acks */
Willy Tarreau9470d2c2017-12-03 10:42:59 +01003122 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003123 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003124 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003125 h2s->st = H2_SS_CLOSED;
3126 }
3127
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003128 if (h2s->flags & H2_SF_BLK_SFCTL) {
3129 /* stream flow control, quit the list */
3130 LIST_DEL(&h2s->list);
3131 LIST_INIT(&h2s->list);
3132 }
3133
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003134 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003135}
3136
3137
3138/*******************************************************/
3139/* functions below are dedicated to the config parsers */
3140/*******************************************************/
3141
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003142/* config parser for global "tune.h2.header-table-size" */
3143static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3144 struct proxy *defpx, const char *file, int line,
3145 char **err)
3146{
3147 if (too_many_args(1, args, err, NULL))
3148 return -1;
3149
3150 h2_settings_header_table_size = atoi(args[1]);
3151 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3152 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3153 return -1;
3154 }
3155 return 0;
3156}
Willy Tarreau62f52692017-10-08 23:01:42 +02003157
Willy Tarreaue6baec02017-07-27 11:45:11 +02003158/* config parser for global "tune.h2.initial-window-size" */
3159static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3160 struct proxy *defpx, const char *file, int line,
3161 char **err)
3162{
3163 if (too_many_args(1, args, err, NULL))
3164 return -1;
3165
3166 h2_settings_initial_window_size = atoi(args[1]);
3167 if (h2_settings_initial_window_size < 0) {
3168 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3169 return -1;
3170 }
3171 return 0;
3172}
3173
Willy Tarreau5242ef82017-07-27 11:47:28 +02003174/* config parser for global "tune.h2.max-concurrent-streams" */
3175static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3176 struct proxy *defpx, const char *file, int line,
3177 char **err)
3178{
3179 if (too_many_args(1, args, err, NULL))
3180 return -1;
3181
3182 h2_settings_max_concurrent_streams = atoi(args[1]);
3183 if (h2_settings_max_concurrent_streams < 0) {
3184 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3185 return -1;
3186 }
3187 return 0;
3188}
3189
Willy Tarreau62f52692017-10-08 23:01:42 +02003190
3191/****************************************/
3192/* MUX initialization and instanciation */
3193/***************************************/
3194
3195/* The mux operations */
3196const struct mux_ops h2_ops = {
3197 .init = h2_init,
3198 .recv = h2_recv,
3199 .send = h2_send,
3200 .wake = h2_wake,
3201 .update_poll = h2_update_poll,
3202 .rcv_buf = h2_rcv_buf,
3203 .snd_buf = h2_snd_buf,
3204 .attach = h2_attach,
3205 .detach = h2_detach,
3206 .shutr = h2_shutr,
3207 .shutw = h2_shutw,
Willy Tarreau62f52692017-10-08 23:01:42 +02003208 .name = "H2",
3209};
3210
3211/* ALPN selection : this mux registers ALPN tolen "h2" */
3212static struct alpn_mux_list alpn_mux_h2 =
3213 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3214
3215/* config keyword parsers */
3216static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003217 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003218 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003219 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003220 { 0, NULL, NULL }
3221}};
3222
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003223static void __h2_deinit(void)
3224{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003225 pool_destroy(pool_head_h2s);
3226 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003227}
3228
Willy Tarreau62f52692017-10-08 23:01:42 +02003229__attribute__((constructor))
3230static void __h2_init(void)
3231{
3232 alpn_register_mux(&alpn_mux_h2);
3233 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003234 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003235 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3236 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003237}