blob: 13d1bb316446b386274ccf282c140fb50bd955fb [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 */
33static struct pool_head *pool2_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020034/* the h2s stream pool */
35static struct pool_head *pool2_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 */
107 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200108 struct eb_root streams_by_id; /* all active streams by their ID */
109 struct list send_list; /* list of blocked streams requesting to send */
110 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200111 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +0200112 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200113};
114
Willy Tarreau18312642017-10-11 07:57:07 +0200115/* H2 stream state, in h2s->st */
116enum h2_ss {
117 H2_SS_IDLE = 0, // idle
118 H2_SS_RLOC, // reserved(local)
119 H2_SS_RREM, // reserved(remote)
120 H2_SS_OPEN, // open
121 H2_SS_HREM, // half-closed(remote)
122 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200123 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200124 H2_SS_CLOSED, // closed
125 H2_SS_ENTRIES // must be last
126} __attribute__((packed));
127
128/* HTTP/2 stream flags (32 bit), in h2s->flags */
129#define H2_SF_NONE 0x00000000
130#define H2_SF_ES_RCVD 0x00000001
131#define H2_SF_ES_SENT 0x00000002
132
133#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
134#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
135
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200136/* stream flags indicating the reason the stream is blocked */
137#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
138#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
139#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
140#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
141#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
142
Willy Tarreau454f9052017-10-26 19:40:35 +0200143/* stream flags indicating how data is supposed to be sent */
144#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
145#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
146
147/* step we're currently in when sending chunks. This is needed because we may
148 * have to transfer chunks as large as a full buffer so there's no room left
149 * for size nor crlf around.
150 */
151#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
152#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
153#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
154
155#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
156
Willy Tarreau67434202017-11-06 20:20:51 +0100157#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100158#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100159
Willy Tarreau18312642017-10-11 07:57:07 +0200160/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
161 * it is being processed in the internal HTTP representation (H1 for now).
162 */
163struct h2s {
164 struct conn_stream *cs;
165 struct h2c *h2c;
166 struct h1m req, res; /* request and response parser state for H1 */
167 struct eb32_node by_id; /* place in h2c's streams_by_id */
168 struct list list; /* position in active/blocked lists if blocked>0 */
169 int32_t id; /* stream ID */
170 uint32_t flags; /* H2_SF_* */
171 int mws; /* mux window size for this stream */
172 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
173 enum h2_ss st;
174};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200175
Willy Tarreauc6405142017-09-21 20:23:50 +0200176/* descriptor for an h2 frame header */
177struct h2_fh {
178 uint32_t len; /* length, host order, 24 bits */
179 uint32_t sid; /* stream id, host order, 31 bits */
180 uint8_t ft; /* frame type */
181 uint8_t ff; /* frame flags */
182};
183
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200184/* a few settings from the global section */
185static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200186static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200187static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200188
Willy Tarreau2a856182017-05-16 15:20:39 +0200189/* a dmumy closed stream */
190static const struct h2s *h2_closed_stream = &(const struct h2s){
191 .cs = NULL,
192 .h2c = NULL,
193 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100194 .errcode = H2_ERR_STREAM_CLOSED,
195 .flags = H2_SF_RST_SENT,
Willy Tarreau2a856182017-05-16 15:20:39 +0200196 .id = 0,
197};
198
199/* and a dummy idle stream for use with any unannounced stream */
200static const struct h2s *h2_idle_stream = &(const struct h2s){
201 .cs = NULL,
202 .h2c = NULL,
203 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100204 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200205 .id = 0,
206};
207
Willy Tarreauea392822017-10-31 10:02:25 +0100208static struct task *h2_timeout_task(struct task *t);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200209
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200210/*****************************************************/
211/* functions below are for dynamic buffer management */
212/*****************************************************/
213
214/* re-enables receiving on mux <target> after a buffer was allocated. It returns
215 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
216 * if it's impossible to wake up and we prefer to be woken up later.
217 */
218static int h2_dbuf_available(void *target)
219{
220 struct h2c *h2c = target;
221
222 /* take the buffer now as we'll get scheduled waiting for ->wake() */
223 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200224 h2c->flags &= ~H2_CF_DEM_DALLOC;
225 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
226 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200227 return 1;
228 }
229 return 0;
230}
231
232static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
233{
234 struct buffer *buf = NULL;
235
236 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
237 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
238 h2c->dbuf_wait.target = h2c->conn;
239 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100240 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200241 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100242 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200243 __conn_xprt_stop_recv(h2c->conn);
244 }
245 return buf;
246}
247
248static inline void h2_release_dbuf(struct h2c *h2c)
249{
250 if (h2c->dbuf->size) {
251 b_free(&h2c->dbuf);
252 offer_buffers(h2c->dbuf_wait.target,
253 tasks_run_queue + applets_active_queue);
254 }
255}
256
Willy Tarreau14398122017-09-22 14:26:04 +0200257/* re-enables sending on mux <target> after a buffer was allocated. It returns
258 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
259 * if it's impossible to wake up and we prefer to be woken up later.
260 */
261static int h2_mbuf_available(void *target)
262{
263 struct h2c *h2c = target;
264
265 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
266 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200267 if (h2c->flags & H2_CF_MUX_MALLOC) {
268 h2c->flags &= ~H2_CF_MUX_MALLOC;
269 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
270 conn_xprt_want_send(h2c->conn);
271 }
272
273 if (h2c->flags & H2_CF_DEM_MROOM) {
274 h2c->flags &= ~H2_CF_DEM_MROOM;
275 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
276 conn_xprt_want_recv(h2c->conn);
277 }
278
Willy Tarreau14398122017-09-22 14:26:04 +0200279 /* FIXME: we should in fact call something like h2_update_poll()
280 * now to recompte the polling. For now it will be enough like
281 * this.
282 */
Willy Tarreau14398122017-09-22 14:26:04 +0200283 return 1;
284 }
285 return 0;
286}
287
288static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
289{
290 struct buffer *buf = NULL;
291
292 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
293 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
294 h2c->mbuf_wait.target = h2c;
295 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100296 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200297 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100298 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200299
300 /* FIXME: we should in fact only block the direction being
301 * currently used. For now it will be enough like this.
302 */
303 __conn_xprt_stop_send(h2c->conn);
304 __conn_xprt_stop_recv(h2c->conn);
305 }
306 return buf;
307}
308
309static inline void h2_release_mbuf(struct h2c *h2c)
310{
311 if (h2c->mbuf->size) {
312 b_free(&h2c->mbuf);
313 offer_buffers(h2c->mbuf_wait.target,
314 tasks_run_queue + applets_active_queue);
315 }
316}
317
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200318
Willy Tarreau62f52692017-10-08 23:01:42 +0200319/*****************************************************************/
320/* functions below are dedicated to the mux setup and management */
321/*****************************************************************/
322
Willy Tarreau32218eb2017-09-22 08:07:25 +0200323/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
324static int h2c_frt_init(struct connection *conn)
325{
326 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100327 struct task *t = NULL;
328 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200329
330 h2c = pool_alloc2(pool2_h2c);
331 if (!h2c)
332 goto fail;
333
Willy Tarreau3f133572017-10-31 19:21:06 +0100334
335 h2c->timeout = sess->fe->timeout.client;
Willy Tarreau33400292017-11-05 11:23:40 +0100336 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100337 if (tick_isset(h2c->timeout)) {
338 t = task_new(tid_bit);
339 if (!t)
340 goto fail;
341
342 h2c->task = t;
343 t->process = h2_timeout_task;
344 t->context = h2c;
345 t->expire = tick_add(now_ms, h2c->timeout);
346 }
Willy Tarreauea392822017-10-31 10:02:25 +0100347
Willy Tarreau32218eb2017-09-22 08:07:25 +0200348 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
349 if (!h2c->ddht)
350 goto fail;
351
352 /* Initialise the context. */
353 h2c->st0 = H2_CS_PREFACE;
354 h2c->conn = conn;
355 h2c->max_id = -1;
356 h2c->errcode = H2_ERR_NO_ERROR;
357 h2c->flags = H2_CF_NONE;
358 h2c->rcvd_c = 0;
359 h2c->rcvd_s = 0;
360
361 h2c->dbuf = &buf_empty;
362 h2c->dsi = -1;
363 h2c->msi = -1;
364 h2c->last_sid = -1;
365
366 h2c->mbuf = &buf_empty;
367 h2c->miw = 65535; /* mux initial window size */
368 h2c->mws = 65535; /* mux window size */
369 h2c->mfs = 16384; /* initial max frame size */
370 h2c->streams_by_id = EB_ROOT_UNIQUE;
371 LIST_INIT(&h2c->send_list);
372 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200373 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200374 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200375 conn->mux_ctx = h2c;
376
Willy Tarreau3f133572017-10-31 19:21:06 +0100377 if (t)
378 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200379 conn_xprt_want_recv(conn);
Willy Tarreauea392822017-10-31 10:02:25 +0100380
Willy Tarreau32218eb2017-09-22 08:07:25 +0200381 /* mux->wake will be called soon to complete the operation */
382 return 0;
383 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100384 if (t)
385 task_free(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200386 pool_free2(pool2_h2c, h2c);
387 return -1;
388}
389
Willy Tarreau62f52692017-10-08 23:01:42 +0200390/* Initialize the mux once it's attached. For outgoing connections, the context
391 * is already initialized before installing the mux, so we detect incoming
392 * connections from the fact that the context is still NULL. Returns < 0 on
393 * error.
394 */
395static int h2_init(struct connection *conn)
396{
397 if (conn->mux_ctx) {
398 /* we don't support outgoing connections for now */
399 return -1;
400 }
401
Willy Tarreau32218eb2017-09-22 08:07:25 +0200402 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200403}
404
Willy Tarreau2373acc2017-10-12 17:35:14 +0200405/* returns the stream associated with id <id> or NULL if not found */
406static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
407{
408 struct eb32_node *node;
409
Willy Tarreau2a856182017-05-16 15:20:39 +0200410 if (id > h2c->max_id)
411 return (struct h2s *)h2_idle_stream;
412
Willy Tarreau2373acc2017-10-12 17:35:14 +0200413 node = eb32_lookup(&h2c->streams_by_id, id);
414 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200415 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200416
417 return container_of(node, struct h2s, by_id);
418}
419
Willy Tarreau62f52692017-10-08 23:01:42 +0200420/* release function for a connection. This one should be called to free all
421 * resources allocated to the mux.
422 */
423static void h2_release(struct connection *conn)
424{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200425 struct h2c *h2c = conn->mux_ctx;
426
427 LIST_DEL(&conn->list);
428
429 if (h2c) {
430 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200431 h2_release_dbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100432 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200433 LIST_DEL(&h2c->dbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100434 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200435
436 h2_release_mbuf(h2c);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100437 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200438 LIST_DEL(&h2c->mbuf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100439 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200440
Willy Tarreauea392822017-10-31 10:02:25 +0100441 if (h2c->task) {
442 task_delete(h2c->task);
443 task_free(h2c->task);
444 h2c->task = NULL;
445 }
446
Willy Tarreau32218eb2017-09-22 08:07:25 +0200447 pool_free2(pool2_h2c, h2c);
448 }
449
450 conn->mux = NULL;
451 conn->mux_ctx = NULL;
452
453 conn_stop_tracking(conn);
454 conn_full_close(conn);
455 if (conn->destroy_cb)
456 conn->destroy_cb(conn);
457 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200458}
459
460
Willy Tarreau71681172017-10-23 14:39:06 +0200461/******************************************************/
462/* functions below are for the H2 protocol processing */
463/******************************************************/
464
465/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100466static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200467{
468 return h2s ? h2s->id : 0;
469}
470
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200471/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100472static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200473{
474 if (h2c->msi < 0)
475 return 0;
476
477 if (h2c->msi == h2s_id(h2s))
478 return 0;
479
480 return 1;
481}
482
Willy Tarreau741d6df2017-10-17 08:00:59 +0200483/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100484static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200485{
486 h2c->errcode = err;
487 h2c->st0 = H2_CS_ERROR;
488}
489
Willy Tarreau2e43f082017-10-17 08:03:59 +0200490/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100491static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200492{
493 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
494 h2s->errcode = err;
495 h2s->st = H2_SS_ERROR;
496 if (h2s->cs)
497 h2s->cs->flags |= CS_FL_ERROR;
498 }
499}
500
Willy Tarreaue4820742017-07-27 13:37:23 +0200501/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100502static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200503{
504 uint8_t *out = frame;
505
506 *out = len >> 16;
507 write_n16(out + 1, len);
508}
509
Willy Tarreau54c15062017-10-10 17:10:03 +0200510/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
511 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
512 * the caller's responsibility to verify that there are at least <bytes> bytes
513 * available in the buffer's input prior to calling this function.
514 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100515static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200516 const struct buffer *b, int o)
517{
518 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
519}
520
Willy Tarreau1f094672017-11-20 21:27:45 +0100521static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200522{
523 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
524}
525
Willy Tarreau1f094672017-11-20 21:27:45 +0100526static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200527{
528 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
529}
530
Willy Tarreau1f094672017-11-20 21:27:45 +0100531static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200532{
533 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
534}
535
536
Willy Tarreau715d5312017-07-11 15:20:24 +0200537/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
538 * is not obvious. It turns out that H2 headers are neither aligned nor do they
539 * use regular sizes. And to add to the trouble, the buffer may wrap so each
540 * byte read must be checked. The header is formed like this :
541 *
542 * b0 b1 b2 b3 b4 b5..b8
543 * +----------+---------+--------+----+----+----------------------+
544 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
545 * +----------+---------+--------+----+----+----------------------+
546 *
547 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
548 * we get the sid properly aligned and ordered, and 16 bits of len properly
549 * ordered as well. The type and flags can be extracted using bit shifts from
550 * the word, and only one extra read is needed to fetch len[16:23].
551 * Returns zero if some bytes are missing, otherwise non-zero on success.
552 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100553static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200554{
555 uint64_t w;
556
557 if (b->i < 9)
558 return 0;
559
560 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
561 h->len = *b->p << 16;
562 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
563 h->ff = w >> 32;
564 h->ft = w >> 40;
565 h->len += w >> 48;
566 return 1;
567}
568
569/* skip the next 9 bytes corresponding to the frame header possibly parsed by
570 * h2_peek_frame_hdr() above.
571 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100572static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200573{
574 bi_del(b, 9);
575}
576
577/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100578static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200579{
580 int ret;
581
582 ret = h2_peek_frame_hdr(b, h);
583 if (ret > 0)
584 h2_skip_frame_hdr(b);
585 return ret;
586}
587
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200588/* creates a new stream <id> on the h2c connection and returns it, or NULL in
589 * case of memory allocation error.
590 */
591static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
592{
593 struct conn_stream *cs;
594 struct h2s *h2s;
595
596 h2s = pool_alloc2(pool2_h2s);
597 if (!h2s)
598 goto out;
599
600 h2s->h2c = h2c;
601 h2s->mws = h2c->miw;
602 h2s->flags = H2_SF_NONE;
603 h2s->errcode = H2_ERR_NO_ERROR;
604 h2s->st = H2_SS_IDLE;
605 h1m_init(&h2s->req);
606 h1m_init(&h2s->res);
607 h2s->by_id.key = h2s->id = id;
608 h2c->max_id = id;
609 LIST_INIT(&h2s->list);
610
611 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
612
613 cs = cs_new(h2c->conn);
614 if (!cs)
615 goto out_close;
616
617 h2s->cs = cs;
618 cs->ctx = h2s;
619
620 if (stream_create_from_cs(cs) < 0)
621 goto out_free_cs;
622
623 /* OK done, the stream lives its own life now */
624 return h2s;
625
626 out_free_cs:
627 cs_free(cs);
628 out_close:
629 eb32_delete(&h2s->by_id);
630 pool_free2(pool2_h2s, h2s);
631 h2s = NULL;
632 out:
633 return h2s;
634}
635
Willy Tarreaube5b7152017-09-25 16:25:39 +0200636/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
637 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
638 * the various settings codes.
639 */
640static int h2c_snd_settings(struct h2c *h2c)
641{
642 struct buffer *res;
643 char buf_data[100]; // enough for 15 settings
644 struct chunk buf;
645 int ret;
646
647 if (h2c_mux_busy(h2c, NULL)) {
648 h2c->flags |= H2_CF_DEM_MBUSY;
649 return 0;
650 }
651
652 res = h2_get_mbuf(h2c);
653 if (!res) {
654 h2c->flags |= H2_CF_MUX_MALLOC;
655 h2c->flags |= H2_CF_DEM_MROOM;
656 return 0;
657 }
658
659 chunk_init(&buf, buf_data, sizeof(buf_data));
660 chunk_memcpy(&buf,
661 "\x00\x00\x00" /* length : 0 for now */
662 "\x04\x00" /* type : 4 (settings), flags : 0 */
663 "\x00\x00\x00\x00", /* stream ID : 0 */
664 9);
665
666 if (h2_settings_header_table_size != 4096) {
667 char str[6] = "\x00\x01"; /* header_table_size */
668
669 write_n32(str + 2, h2_settings_header_table_size);
670 chunk_memcat(&buf, str, 6);
671 }
672
673 if (h2_settings_initial_window_size != 65535) {
674 char str[6] = "\x00\x04"; /* initial_window_size */
675
676 write_n32(str + 2, h2_settings_initial_window_size);
677 chunk_memcat(&buf, str, 6);
678 }
679
680 if (h2_settings_max_concurrent_streams != 0) {
681 char str[6] = "\x00\x03"; /* max_concurrent_streams */
682
683 /* Note: 0 means "unlimited" for haproxy's config but not for
684 * the protocol, so never send this value!
685 */
686 write_n32(str + 2, h2_settings_max_concurrent_streams);
687 chunk_memcat(&buf, str, 6);
688 }
689
690 if (global.tune.bufsize != 16384) {
691 char str[6] = "\x00\x05"; /* max_frame_size */
692
693 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
694 * match bufsize - rewrite size, but at the moment it seems
695 * that clients don't take care of it.
696 */
697 write_n32(str + 2, global.tune.bufsize);
698 chunk_memcat(&buf, str, 6);
699 }
700
701 h2_set_frame_size(buf.str, buf.len - 9);
702 ret = bo_istput(res, ist2(buf.str, buf.len));
703 if (unlikely(ret <= 0)) {
704 if (!ret) {
705 h2c->flags |= H2_CF_MUX_MFULL;
706 h2c->flags |= H2_CF_DEM_MROOM;
707 return 0;
708 }
709 else {
710 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
711 return 0;
712 }
713 }
714 return ret;
715}
716
Willy Tarreau52eed752017-09-22 15:05:09 +0200717/* Try to receive a connection preface, then upon success try to send our
718 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
719 * missing data. It may return an error in h2c.
720 */
721static int h2c_frt_recv_preface(struct h2c *h2c)
722{
723 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200724 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200725
726 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
727
728 if (unlikely(ret1 <= 0)) {
729 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
730 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
731 return 0;
732 }
733
Willy Tarreaube5b7152017-09-25 16:25:39 +0200734 ret2 = h2c_snd_settings(h2c);
735 if (ret2 > 0)
736 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200737
Willy Tarreaube5b7152017-09-25 16:25:39 +0200738 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200739}
740
Willy Tarreau081d4722017-05-16 21:51:05 +0200741/* try to send a GOAWAY frame on the connection to report an error or a graceful
742 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
743 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
744 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
745 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
746 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
747 * on unrecoverable failure. It will not attempt to send one again in this last
748 * case so that it is safe to use h2c_error() to report such errors.
749 */
750static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
751{
752 struct buffer *res;
753 char str[17];
754 int ret;
755
756 if (h2c->flags & H2_CF_GOAWAY_FAILED)
757 return 1; // claim that it worked
758
759 if (h2c_mux_busy(h2c, h2s)) {
760 if (h2s)
761 h2s->flags |= H2_SF_BLK_MBUSY;
762 else
763 h2c->flags |= H2_CF_DEM_MBUSY;
764 return 0;
765 }
766
767 res = h2_get_mbuf(h2c);
768 if (!res) {
769 h2c->flags |= H2_CF_MUX_MALLOC;
770 if (h2s)
771 h2s->flags |= H2_SF_BLK_MROOM;
772 else
773 h2c->flags |= H2_CF_DEM_MROOM;
774 return 0;
775 }
776
777 /* len: 8, type: 7, flags: none, sid: 0 */
778 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
779
780 if (h2c->last_sid < 0)
781 h2c->last_sid = h2c->max_id;
782
783 write_n32(str + 9, h2c->last_sid);
784 write_n32(str + 13, h2c->errcode);
785 ret = bo_istput(res, ist2(str, 17));
786 if (unlikely(ret <= 0)) {
787 if (!ret) {
788 h2c->flags |= H2_CF_MUX_MFULL;
789 if (h2s)
790 h2s->flags |= H2_SF_BLK_MROOM;
791 else
792 h2c->flags |= H2_CF_DEM_MROOM;
793 return 0;
794 }
795 else {
796 /* we cannot report this error using GOAWAY, so we mark
797 * it and claim a success.
798 */
799 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
800 h2c->flags |= H2_CF_GOAWAY_FAILED;
801 return 1;
802 }
803 }
804 h2c->flags |= H2_CF_GOAWAY_SENT;
805 return ret;
806}
807
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100808/* Try to send an RST_STREAM frame on the connection for the indicated stream
809 * during mux operations. This stream must be valid and cannot be closed
810 * already. h2s->id will be used for the stream ID and h2s->errcode will be
811 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
812 * not yet.
813 *
814 * Returns > 0 on success or zero if nothing was done. In case of lack of room
815 * to write the message, it subscribes the stream to future notifications.
816 */
817static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
818{
819 struct buffer *res;
820 char str[13];
821 int ret;
822
823 if (!h2s || h2s->st == H2_SS_CLOSED)
824 return 1;
825
826 if (h2c_mux_busy(h2c, h2s)) {
827 h2s->flags |= H2_SF_BLK_MBUSY;
828 return 0;
829 }
830
831 res = h2_get_mbuf(h2c);
832 if (!res) {
833 h2c->flags |= H2_CF_MUX_MALLOC;
834 h2s->flags |= H2_SF_BLK_MROOM;
835 return 0;
836 }
837
838 /* len: 4, type: 3, flags: none */
839 memcpy(str, "\x00\x00\x04\x03\x00", 5);
840 write_n32(str + 5, h2s->id);
841 write_n32(str + 9, h2s->errcode);
842 ret = bo_istput(res, ist2(str, 13));
843
844 if (unlikely(ret <= 0)) {
845 if (!ret) {
846 h2c->flags |= H2_CF_MUX_MFULL;
847 h2s->flags |= H2_SF_BLK_MROOM;
848 return 0;
849 }
850 else {
851 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
852 return 0;
853 }
854 }
855
856 h2s->flags |= H2_SF_RST_SENT;
857 h2s->st = H2_SS_CLOSED;
858 return ret;
859}
860
861/* Try to send an RST_STREAM frame on the connection for the stream being
862 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
863 * error code unless the stream's state already is IDLE or CLOSED in which
864 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
865 * it was not yet.
866 *
867 * Returns > 0 on success or zero if nothing was done. In case of lack of room
868 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200869 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100870 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200871 */
872static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
873{
874 struct buffer *res;
875 char str[13];
876 int ret;
877
878 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100879 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200880 return 0;
881 }
882
883 res = h2_get_mbuf(h2c);
884 if (!res) {
885 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100886 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200887 return 0;
888 }
889
890 /* len: 4, type: 3, flags: none */
891 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100892
Willy Tarreau27a84c92017-10-17 08:10:17 +0200893 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +0100894 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +0200895 h2s->errcode : H2_ERR_STREAM_CLOSED);
896 ret = bo_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100897
Willy Tarreau27a84c92017-10-17 08:10:17 +0200898 if (unlikely(ret <= 0)) {
899 if (!ret) {
900 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100901 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +0200902 return 0;
903 }
904 else {
905 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
906 return 0;
907 }
908 }
909
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100910 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +0200911 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100912 h2s->st = H2_SS_CLOSED;
913 }
914
Willy Tarreau27a84c92017-10-17 08:10:17 +0200915 return ret;
916}
917
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100918/* try to send an empty DATA frame with the ES flag set to notify about the
919 * end of stream and match a shutdown(write). If an ES was already sent as
920 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
921 * on success or zero if nothing was done. In case of lack of room to write the
922 * message, it subscribes the requesting stream to future notifications.
923 */
924static int h2_send_empty_data_es(struct h2s *h2s)
925{
926 struct h2c *h2c = h2s->h2c;
927 struct buffer *res;
928 char str[9];
929 int ret;
930
Willy Tarreau721c9742017-11-07 11:05:42 +0100931 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100932 return 1;
933
934 if (h2c_mux_busy(h2c, h2s)) {
935 h2s->flags |= H2_SF_BLK_MBUSY;
936 return 0;
937 }
938
939 res = h2_get_mbuf(h2c);
940 if (!res) {
941 h2c->flags |= H2_CF_MUX_MALLOC;
942 h2s->flags |= H2_SF_BLK_MROOM;
943 return 0;
944 }
945
946 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
947 memcpy(str, "\x00\x00\x00\x00\x01", 5);
948 write_n32(str + 5, h2s->id);
949 ret = bo_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +0100950 if (likely(ret > 0)) {
951 h2s->flags |= H2_SF_ES_SENT;
952 }
953 else if (!ret) {
954 h2c->flags |= H2_CF_MUX_MFULL;
955 h2s->flags |= H2_SF_BLK_MROOM;
956 return 0;
957 }
958 else {
959 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
960 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100961 }
962 return ret;
963}
964
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100965/* wake the streams attached to the connection, whose id is greater than <last>,
966 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
967 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
968 * stream's state is automatically updated accordingly.
969 */
970static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
971{
972 struct eb32_node *node;
973 struct h2s *h2s;
974
975 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
976 flags |= CS_FL_ERROR;
977
978 if (conn_xprt_read0_pending(h2c->conn))
979 flags |= CS_FL_EOS;
980
981 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
982 while (node) {
983 h2s = container_of(node, struct h2s, by_id);
984 if (h2s->id <= last)
985 break;
986 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +0100987
988 if (!h2s->cs) {
989 /* this stream was already orphaned */
990 eb32_delete(&h2s->by_id);
991 pool_free2(pool2_h2s, h2s);
992 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100993 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +0100994
995 h2s->cs->flags |= flags;
996 /* recv is used to force to detect CS_FL_EOS that wake()
997 * doesn't handle in the stream int code.
998 */
999 h2s->cs->data_cb->recv(h2s->cs);
1000 h2s->cs->data_cb->wake(h2s->cs);
1001
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001002 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1003 h2s->st = H2_SS_ERROR;
1004 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
1005 h2s->st = H2_SS_HREM;
1006 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
1007 h2s->st = H2_SS_CLOSED;
1008 }
1009}
1010
Willy Tarreau3421aba2017-07-27 15:41:03 +02001011/* Increase all streams' outgoing window size by the difference passed in
1012 * argument. This is needed upon receipt of the settings frame if the initial
1013 * window size is different. The difference may be negative and the resulting
1014 * window size as well, for the time it takes to receive some window updates.
1015 */
1016static void h2c_update_all_ws(struct h2c *h2c, int diff)
1017{
1018 struct h2s *h2s;
1019 struct eb32_node *node;
1020
1021 if (!diff)
1022 return;
1023
1024 node = eb32_first(&h2c->streams_by_id);
1025 while (node) {
1026 h2s = container_of(node, struct h2s, by_id);
1027 h2s->mws += diff;
1028 node = eb32_next(node);
1029 }
1030}
1031
1032/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1033 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1034 * return an error in h2c. Described in RFC7540#6.5.
1035 */
1036static int h2c_handle_settings(struct h2c *h2c)
1037{
1038 unsigned int offset;
1039 int error;
1040
1041 if (h2c->dff & H2_F_SETTINGS_ACK) {
1042 if (h2c->dfl) {
1043 error = H2_ERR_FRAME_SIZE_ERROR;
1044 goto fail;
1045 }
1046 return 1;
1047 }
1048
1049 if (h2c->dsi != 0) {
1050 error = H2_ERR_PROTOCOL_ERROR;
1051 goto fail;
1052 }
1053
1054 if (h2c->dfl % 6) {
1055 error = H2_ERR_FRAME_SIZE_ERROR;
1056 goto fail;
1057 }
1058
1059 /* that's the limit we can process */
1060 if (h2c->dfl > global.tune.bufsize) {
1061 error = H2_ERR_FRAME_SIZE_ERROR;
1062 goto fail;
1063 }
1064
1065 /* process full frame only */
1066 if (h2c->dbuf->i < h2c->dfl)
1067 return 0;
1068
1069 /* parse the frame */
1070 for (offset = 0; offset < h2c->dfl; offset += 6) {
1071 uint16_t type = h2_get_n16(h2c->dbuf, offset);
1072 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
1073
1074 switch (type) {
1075 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1076 /* we need to update all existing streams with the
1077 * difference from the previous iws.
1078 */
1079 if (arg < 0) { // RFC7540#6.5.2
1080 error = H2_ERR_FLOW_CONTROL_ERROR;
1081 goto fail;
1082 }
1083 h2c_update_all_ws(h2c, arg - h2c->miw);
1084 h2c->miw = arg;
1085 break;
1086 case H2_SETTINGS_MAX_FRAME_SIZE:
1087 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1088 error = H2_ERR_PROTOCOL_ERROR;
1089 goto fail;
1090 }
1091 h2c->mfs = arg;
1092 break;
1093 }
1094 }
1095
1096 /* need to ACK this frame now */
1097 h2c->st0 = H2_CS_FRAME_A;
1098 return 1;
1099 fail:
1100 h2c_error(h2c, error);
1101 return 0;
1102}
1103
1104/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1105 * success or one of the h2_status values.
1106 */
1107static int h2c_ack_settings(struct h2c *h2c)
1108{
1109 struct buffer *res;
1110 char str[9];
1111 int ret = -1;
1112
1113 if (h2c_mux_busy(h2c, NULL)) {
1114 h2c->flags |= H2_CF_DEM_MBUSY;
1115 return 0;
1116 }
1117
1118 res = h2_get_mbuf(h2c);
1119 if (!res) {
1120 h2c->flags |= H2_CF_MUX_MALLOC;
1121 h2c->flags |= H2_CF_DEM_MROOM;
1122 return 0;
1123 }
1124
1125 memcpy(str,
1126 "\x00\x00\x00" /* length : 0 (no data) */
1127 "\x04" "\x01" /* type : 4, flags : ACK */
1128 "\x00\x00\x00\x00" /* stream ID */, 9);
1129
1130 ret = bo_istput(res, ist2(str, 9));
1131 if (unlikely(ret <= 0)) {
1132 if (!ret) {
1133 h2c->flags |= H2_CF_MUX_MFULL;
1134 h2c->flags |= H2_CF_DEM_MROOM;
1135 return 0;
1136 }
1137 else {
1138 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1139 return 0;
1140 }
1141 }
1142 return ret;
1143}
1144
Willy Tarreaucf68c782017-10-10 17:11:41 +02001145/* processes a PING frame and schedules an ACK if needed. The caller must pass
1146 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1147 * missing data. It may return an error in h2c.
1148 */
1149static int h2c_handle_ping(struct h2c *h2c)
1150{
1151 /* frame length must be exactly 8 */
1152 if (h2c->dfl != 8) {
1153 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1154 return 0;
1155 }
1156
1157 /* schedule a response */
1158 if (!(h2c->dft & H2_F_PING_ACK))
1159 h2c->st0 = H2_CS_FRAME_A;
1160 return 1;
1161}
1162
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001163/* Try to send a window update for stream id <sid> and value <increment>.
1164 * Returns > 0 on success or zero on missing room or failure. It may return an
1165 * error in h2c.
1166 */
1167static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1168{
1169 struct buffer *res;
1170 char str[13];
1171 int ret = -1;
1172
1173 if (h2c_mux_busy(h2c, NULL)) {
1174 h2c->flags |= H2_CF_DEM_MBUSY;
1175 return 0;
1176 }
1177
1178 res = h2_get_mbuf(h2c);
1179 if (!res) {
1180 h2c->flags |= H2_CF_MUX_MALLOC;
1181 h2c->flags |= H2_CF_DEM_MROOM;
1182 return 0;
1183 }
1184
1185 /* length: 4, type: 8, flags: none */
1186 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1187 write_n32(str + 5, sid);
1188 write_n32(str + 9, increment);
1189
1190 ret = bo_istput(res, ist2(str, 13));
1191
1192 if (unlikely(ret <= 0)) {
1193 if (!ret) {
1194 h2c->flags |= H2_CF_MUX_MFULL;
1195 h2c->flags |= H2_CF_DEM_MROOM;
1196 return 0;
1197 }
1198 else {
1199 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1200 return 0;
1201 }
1202 }
1203 return ret;
1204}
1205
1206/* try to send pending window update for the connection. It's safe to call it
1207 * with no pending updates. Returns > 0 on success or zero on missing room or
1208 * failure. It may return an error in h2c.
1209 */
1210static int h2c_send_conn_wu(struct h2c *h2c)
1211{
1212 int ret = 1;
1213
1214 if (h2c->rcvd_c <= 0)
1215 return 1;
1216
1217 /* send WU for the connection */
1218 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1219 if (ret > 0)
1220 h2c->rcvd_c = 0;
1221
1222 return ret;
1223}
1224
1225/* try to send pending window update for the current dmux stream. It's safe to
1226 * call it with no pending updates. Returns > 0 on success or zero on missing
1227 * room or failure. It may return an error in h2c.
1228 */
1229static int h2c_send_strm_wu(struct h2c *h2c)
1230{
1231 int ret = 1;
1232
1233 if (h2c->rcvd_s <= 0)
1234 return 1;
1235
1236 /* send WU for the stream */
1237 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1238 if (ret > 0)
1239 h2c->rcvd_s = 0;
1240
1241 return ret;
1242}
1243
Willy Tarreaucf68c782017-10-10 17:11:41 +02001244/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1245 * success, 0 on missing data or one of the h2_status values.
1246 */
1247static int h2c_ack_ping(struct h2c *h2c)
1248{
1249 struct buffer *res;
1250 char str[17];
1251 int ret = -1;
1252
1253 if (h2c->dbuf->i < 8)
1254 return 0;
1255
1256 if (h2c_mux_busy(h2c, NULL)) {
1257 h2c->flags |= H2_CF_DEM_MBUSY;
1258 return 0;
1259 }
1260
1261 res = h2_get_mbuf(h2c);
1262 if (!res) {
1263 h2c->flags |= H2_CF_MUX_MALLOC;
1264 h2c->flags |= H2_CF_DEM_MROOM;
1265 return 0;
1266 }
1267
1268 memcpy(str,
1269 "\x00\x00\x08" /* length : 8 (same payload) */
1270 "\x06" "\x01" /* type : 6, flags : ACK */
1271 "\x00\x00\x00\x00" /* stream ID */, 9);
1272
1273 /* copy the original payload */
1274 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1275
1276 ret = bo_istput(res, ist2(str, 17));
1277 if (unlikely(ret <= 0)) {
1278 if (!ret) {
1279 h2c->flags |= H2_CF_MUX_MFULL;
1280 h2c->flags |= H2_CF_DEM_MROOM;
1281 return 0;
1282 }
1283 else {
1284 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1285 return 0;
1286 }
1287 }
1288 return ret;
1289}
1290
Willy Tarreau26f95952017-07-27 17:18:30 +02001291/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1292 * Returns > 0 on success or zero on missing data. It may return an error in
1293 * h2c or h2s. Described in RFC7540#6.9.
1294 */
1295static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1296{
1297 int32_t inc;
1298 int error;
1299
1300 if (h2c->dfl != 4) {
1301 error = H2_ERR_FRAME_SIZE_ERROR;
1302 goto conn_err;
1303 }
1304
1305 /* process full frame only */
1306 if (h2c->dbuf->i < h2c->dfl)
1307 return 0;
1308
1309 inc = h2_get_n32(h2c->dbuf, 0);
1310
1311 if (h2c->dsi != 0) {
1312 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001313
1314 /* it's not an error to receive WU on a closed stream */
1315 if (h2s->st == H2_SS_CLOSED)
1316 return 1;
1317
1318 if (!inc) {
1319 error = H2_ERR_PROTOCOL_ERROR;
1320 goto strm_err;
1321 }
1322
1323 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1324 error = H2_ERR_FLOW_CONTROL_ERROR;
1325 goto strm_err;
1326 }
1327
1328 h2s->mws += inc;
1329 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1330 h2s->flags &= ~H2_SF_BLK_SFCTL;
1331 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1332 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1333 /* This stream wanted to send but could not due to its
1334 * own flow control. We can put it back into the send
1335 * list now, it will be handled upon next send() call.
1336 */
1337 LIST_ADDQ(&h2c->send_list, &h2s->list);
1338 }
1339 }
1340 }
1341 else {
1342 /* connection window update */
1343 if (!inc) {
1344 error = H2_ERR_PROTOCOL_ERROR;
1345 goto conn_err;
1346 }
1347
1348 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1349 error = H2_ERR_FLOW_CONTROL_ERROR;
1350 goto conn_err;
1351 }
1352
1353 h2c->mws += inc;
1354 }
1355
1356 return 1;
1357
1358 conn_err:
1359 h2c_error(h2c, error);
1360 return 0;
1361
1362 strm_err:
1363 if (h2s) {
1364 h2s_error(h2s, error);
1365 h2c->st0 = H2_CS_FRAME_A;
1366 }
1367 else
1368 h2c_error(h2c, error);
1369 return 0;
1370}
1371
Willy Tarreaue96b0922017-10-30 00:28:29 +01001372/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1373 * the last ID. Returns > 0 on success or zero on missing data. It may return
1374 * an error in h2c. Described in RFC7540#6.8.
1375 */
1376static int h2c_handle_goaway(struct h2c *h2c)
1377{
1378 int error;
1379 int last;
1380
1381 if (h2c->dsi != 0) {
1382 error = H2_ERR_PROTOCOL_ERROR;
1383 goto conn_err;
1384 }
1385
1386 if (h2c->dfl < 8) {
1387 error = H2_ERR_FRAME_SIZE_ERROR;
1388 goto conn_err;
1389 }
1390
1391 /* process full frame only */
1392 if (h2c->dbuf->i < h2c->dfl)
1393 return 0;
1394
1395 last = h2_get_n32(h2c->dbuf, 0);
1396 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1397 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
1398 return 1;
1399
1400 conn_err:
1401 h2c_error(h2c, error);
1402 return 0;
1403}
1404
Willy Tarreaucd234e92017-08-18 10:59:39 +02001405/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1406 * Returns > 0 on success or zero on missing data. It may return an error in
1407 * h2c. Described in RFC7540#6.4.
1408 */
1409static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1410{
1411 int error;
1412
1413 if (h2c->dsi == 0) {
1414 error = H2_ERR_PROTOCOL_ERROR;
1415 goto conn_err;
1416 }
1417
Willy Tarreaucd234e92017-08-18 10:59:39 +02001418 if (h2c->dfl != 4) {
1419 error = H2_ERR_FRAME_SIZE_ERROR;
1420 goto conn_err;
1421 }
1422
1423 /* process full frame only */
1424 if (h2c->dbuf->i < h2c->dfl)
1425 return 0;
1426
1427 /* late RST, already handled */
1428 if (h2s->st == H2_SS_CLOSED)
1429 return 1;
1430
1431 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
1432 h2s->st = H2_SS_CLOSED;
1433
1434 if (h2s->cs) {
1435 h2s->cs->flags |= CS_FL_EOS;
1436 /* recv is used to force to detect CS_FL_EOS that wake()
1437 * doesn't handle in the stream-int code.
1438 */
1439 h2s->cs->data_cb->recv(h2s->cs);
1440 h2s->cs->data_cb->wake(h2s->cs);
1441 }
1442
1443 h2s->flags |= H2_SF_RST_RCVD;
1444 return 1;
1445
1446 conn_err:
1447 h2c_error(h2c, error);
1448 return 0;
1449}
1450
Willy Tarreau13278b42017-10-13 19:23:14 +02001451/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1452 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1453 * errors here are reported as connection errors since it's impossible to
1454 * recover from such errors after the compression context has been altered.
1455 */
1456static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1457{
1458 int error;
1459
1460 if (!h2c->dfl) {
1461 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1462 goto strm_err;
1463 }
1464
1465 if (!h2c->dbuf->size)
1466 return 0; // empty buffer
1467
1468 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1469 return 0; // incomplete frame
1470
1471 /* now either the frame is complete or the buffer is complete */
1472 if (h2s->st != H2_SS_IDLE) {
1473 /* FIXME: stream already exists, this is only allowed for
1474 * trailers (not supported for now).
1475 */
1476 error = H2_ERR_PROTOCOL_ERROR;
1477 goto conn_err;
1478 }
1479 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1480 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1481 error = H2_ERR_PROTOCOL_ERROR;
1482 goto conn_err;
1483 }
1484
1485 h2s = h2c_stream_new(h2c, h2c->dsi);
1486 if (!h2s) {
1487 error = H2_ERR_INTERNAL_ERROR;
1488 goto conn_err;
1489 }
1490
1491 h2s->st = H2_SS_OPEN;
1492 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1493 h2s->st = H2_SS_HREM;
1494 h2s->flags |= H2_SF_ES_RCVD;
1495 }
1496
1497 /* call the upper layers to process the frame, then let the upper layer
1498 * notify the stream about any change.
1499 */
1500 h2s->cs->data_cb->recv(h2s->cs);
1501
1502 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1503 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1504 error = H2_ERR_INTERNAL_ERROR;
1505 goto conn_err;
1506 }
1507
Willy Tarreau8f650c32017-11-21 19:36:21 +01001508 if (h2c->st0 >= H2_CS_ERROR)
1509 return 0;
1510
Willy Tarreau721c9742017-11-07 11:05:42 +01001511 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001512 /* stream error : send RST_STREAM */
1513 h2c->st0 = H2_CS_FRAME_A;
1514 }
1515 else {
1516 /* update the max stream ID if the request is being processed */
1517 if (h2s->id > h2c->max_id)
1518 h2c->max_id = h2s->id;
1519 }
1520
1521 return 1;
1522
1523 conn_err:
1524 h2c_error(h2c, error);
1525 return 0;
1526
1527 strm_err:
1528 if (h2s) {
1529 h2s_error(h2s, error);
1530 h2c->st0 = H2_CS_FRAME_A;
1531 }
1532 else
1533 h2c_error(h2c, error);
1534 return 0;
1535}
1536
Willy Tarreau454f9052017-10-26 19:40:35 +02001537/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1538 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1539 */
1540static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1541{
1542 int error;
1543
1544 /* note that empty DATA frames are perfectly valid and sometimes used
1545 * to signal an end of stream (with the ES flag).
1546 */
1547
1548 if (!h2c->dbuf->size && h2c->dfl)
1549 return 0; // empty buffer
1550
1551 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1552 return 0; // incomplete frame
1553
1554 /* now either the frame is complete or the buffer is complete */
1555
1556 if (!h2c->dsi) {
1557 /* RFC7540#6.1 */
1558 error = H2_ERR_PROTOCOL_ERROR;
1559 goto conn_err;
1560 }
1561
1562 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1563 /* RFC7540#6.1 */
1564 error = H2_ERR_STREAM_CLOSED;
1565 goto strm_err;
1566 }
1567
1568 /* last frame */
1569 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1570 h2s->st = H2_SS_HREM;
1571 h2s->flags |= H2_SF_ES_RCVD;
1572 }
1573
1574 /* call the upper layers to process the frame, then let the upper layer
1575 * notify the stream about any change.
1576 */
1577 if (!h2s->cs) {
1578 error = H2_ERR_STREAM_CLOSED;
1579 goto strm_err;
1580 }
1581
1582 h2s->cs->data_cb->recv(h2s->cs);
Willy Tarreau8f650c32017-11-21 19:36:21 +01001583
Willy Tarreau454f9052017-10-26 19:40:35 +02001584 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1585 /* cs has just been destroyed, we have to kill h2s. */
1586 error = H2_ERR_STREAM_CLOSED;
1587 goto strm_err;
1588 }
1589
Willy Tarreau8f650c32017-11-21 19:36:21 +01001590 if (h2c->st0 >= H2_CS_ERROR)
1591 return 0;
1592
Willy Tarreau721c9742017-11-07 11:05:42 +01001593 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001594 /* stream error : send RST_STREAM */
1595 h2c->st0 = H2_CS_FRAME_A;
1596 }
1597
1598 /* check for completion : the callee will change this to FRAME_A or
1599 * FRAME_H once done.
1600 */
1601 if (h2c->st0 == H2_CS_FRAME_P)
1602 return 0;
1603
1604 return 1;
1605
1606 conn_err:
1607 h2c_error(h2c, error);
1608 return 0;
1609
1610 strm_err:
1611 if (h2s) {
1612 h2s_error(h2s, error);
1613 h2c->st0 = H2_CS_FRAME_A;
1614 }
1615 else
1616 h2c_error(h2c, error);
1617 return 0;
1618}
1619
Willy Tarreaubc933932017-10-09 16:21:43 +02001620/* process Rx frames to be demultiplexed */
1621static void h2_process_demux(struct h2c *h2c)
1622{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001623 struct h2s *h2s;
1624
Willy Tarreau081d4722017-05-16 21:51:05 +02001625 if (h2c->st0 >= H2_CS_ERROR)
1626 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001627
1628 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1629 if (h2c->st0 == H2_CS_PREFACE) {
1630 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1631 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1632 if (h2c->st0 == H2_CS_ERROR)
1633 h2c->st0 = H2_CS_ERROR2;
1634 goto fail;
1635 }
1636
1637 h2c->max_id = 0;
1638 h2c->st0 = H2_CS_SETTINGS1;
1639 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001640
1641 if (h2c->st0 == H2_CS_SETTINGS1) {
1642 struct h2_fh hdr;
1643
1644 /* ensure that what is pending is a valid SETTINGS frame
1645 * without an ACK.
1646 */
1647 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1648 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1649 if (h2c->st0 == H2_CS_ERROR)
1650 h2c->st0 = H2_CS_ERROR2;
1651 goto fail;
1652 }
1653
1654 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1655 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1656 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1657 h2c->st0 = H2_CS_ERROR2;
1658 goto fail;
1659 }
1660
1661 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1662 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1663 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1664 h2c->st0 = H2_CS_ERROR2;
1665 goto fail;
1666 }
1667
1668 /* that's OK, switch to FRAME_P to process it */
1669 h2c->dfl = hdr.len;
1670 h2c->dsi = hdr.sid;
1671 h2c->dft = hdr.ft;
1672 h2c->dff = hdr.ff;
1673 h2c->st0 = H2_CS_FRAME_P;
1674 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001675 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001676
1677 /* process as many incoming frames as possible below */
1678 while (h2c->dbuf->i) {
1679 int ret = 0;
1680
1681 if (h2c->st0 >= H2_CS_ERROR)
1682 break;
1683
1684 if (h2c->st0 == H2_CS_FRAME_H) {
1685 struct h2_fh hdr;
1686
1687 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1688 break;
1689
1690 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1691 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1692 h2c->st0 = H2_CS_ERROR;
1693 break;
1694 }
1695
1696 h2c->dfl = hdr.len;
1697 h2c->dsi = hdr.sid;
1698 h2c->dft = hdr.ft;
1699 h2c->dff = hdr.ff;
1700 h2c->st0 = H2_CS_FRAME_P;
1701 h2_skip_frame_hdr(h2c->dbuf);
1702 }
1703
1704 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001705 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001706
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001707 if (h2s->st == H2_SS_IDLE &&
1708 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1709 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1710 * this state MUST be treated as a connection error
1711 */
1712 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1713 h2c->st0 = H2_CS_ERROR;
1714 break;
1715 }
1716
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001717 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1718 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1719 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1720 * this state MUST be treated as a stream error
1721 */
1722 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1723 goto strm_err;
1724 }
1725
Willy Tarreauc0da1962017-10-30 18:38:00 +01001726#if 0
1727 // problem below: it is not possible to completely ignore such
1728 // streams as we need to maintain the compression state as well
1729 // and for this we need to completely process these frames (eg:
1730 // HEADERS frames) as well as counting DATA frames to emit
1731 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1732 // This is a typical case of layer violation where the
1733 // transported contents are critical to the connection's
1734 // validity and must be ignored at the same time :-(
1735
1736 /* graceful shutdown, ignore streams whose ID is higher than
1737 * the one advertised in GOAWAY. RFC7540#6.8.
1738 */
1739 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1740 ret = MIN(h2c->dbuf->i, h2c->dfl);
1741 bi_del(h2c->dbuf, ret);
1742 h2c->dfl -= ret;
1743 ret = h2c->dfl == 0;
1744 goto strm_err;
1745 }
1746#endif
1747
Willy Tarreau7e98c052017-10-10 15:56:59 +02001748 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001749 case H2_FT_SETTINGS:
1750 if (h2c->st0 == H2_CS_FRAME_P)
1751 ret = h2c_handle_settings(h2c);
1752
1753 if (h2c->st0 == H2_CS_FRAME_A)
1754 ret = h2c_ack_settings(h2c);
1755 break;
1756
Willy Tarreaucf68c782017-10-10 17:11:41 +02001757 case H2_FT_PING:
1758 if (h2c->st0 == H2_CS_FRAME_P)
1759 ret = h2c_handle_ping(h2c);
1760
1761 if (h2c->st0 == H2_CS_FRAME_A)
1762 ret = h2c_ack_ping(h2c);
1763 break;
1764
Willy Tarreau26f95952017-07-27 17:18:30 +02001765 case H2_FT_WINDOW_UPDATE:
1766 if (h2c->st0 == H2_CS_FRAME_P)
1767 ret = h2c_handle_window_update(h2c, h2s);
1768 break;
1769
Willy Tarreau61290ec2017-10-17 08:19:21 +02001770 case H2_FT_CONTINUATION:
1771 /* we currently don't support CONTINUATION frames since
1772 * we have nowhere to store the partial HEADERS frame.
1773 * Let's abort the stream on an INTERNAL_ERROR here.
1774 */
1775 if (h2c->st0 == H2_CS_FRAME_P)
1776 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1777 break;
1778
Willy Tarreau13278b42017-10-13 19:23:14 +02001779 case H2_FT_HEADERS:
1780 if (h2c->st0 == H2_CS_FRAME_P)
1781 ret = h2c_frt_handle_headers(h2c, h2s);
1782 break;
1783
Willy Tarreau454f9052017-10-26 19:40:35 +02001784 case H2_FT_DATA:
1785 if (h2c->st0 == H2_CS_FRAME_P)
1786 ret = h2c_frt_handle_data(h2c, h2s);
1787
1788 if (h2c->st0 == H2_CS_FRAME_A)
1789 ret = h2c_send_strm_wu(h2c);
1790 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001791
1792 case H2_FT_RST_STREAM:
1793 if (h2c->st0 == H2_CS_FRAME_P)
1794 ret = h2c_handle_rst_stream(h2c, h2s);
1795 break;
1796
Willy Tarreaue96b0922017-10-30 00:28:29 +01001797 case H2_FT_GOAWAY:
1798 if (h2c->st0 == H2_CS_FRAME_P)
1799 ret = h2c_handle_goaway(h2c);
1800 break;
1801
Willy Tarreau1c661982017-10-30 13:52:01 +01001802 case H2_FT_PUSH_PROMISE:
1803 /* not permitted here, RFC7540#5.1 */
1804 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau1c661982017-10-30 13:52:01 +01001805 break;
1806
1807 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001808 default:
1809 /* drop frames that we ignore. They may be larger than
1810 * the buffer so we drain all of their contents until
1811 * we reach the end.
1812 */
1813 ret = MIN(h2c->dbuf->i, h2c->dfl);
1814 bi_del(h2c->dbuf, ret);
1815 h2c->dfl -= ret;
1816 ret = h2c->dfl == 0;
1817 }
1818
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001819 strm_err:
Willy Tarreau27a84c92017-10-17 08:10:17 +02001820 /* RST are sent similarly to frame acks */
1821 if (h2s->st == H2_SS_ERROR) {
1822 if (h2c->st0 == H2_CS_FRAME_P)
1823 h2c->st0 = H2_CS_FRAME_A;
1824
1825 if (h2c->st0 == H2_CS_FRAME_A)
1826 ret = h2c_send_rst_stream(h2c, h2s);
1827 }
1828
Willy Tarreau7e98c052017-10-10 15:56:59 +02001829 /* error or missing data condition met above ? */
1830 if (ret <= 0)
1831 break;
1832
1833 if (h2c->st0 != H2_CS_FRAME_H) {
1834 bi_del(h2c->dbuf, h2c->dfl);
1835 h2c->st0 = H2_CS_FRAME_H;
1836 }
1837 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001838
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001839 if (h2c->rcvd_c > 0 &&
1840 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1841 h2c_send_conn_wu(h2c);
1842
Willy Tarreau52eed752017-09-22 15:05:09 +02001843 fail:
1844 /* we can go here on missing data, blocked response or error */
1845 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001846}
1847
1848/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1849 * the end.
1850 */
1851static int h2_process_mux(struct h2c *h2c)
1852{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001853 struct h2s *h2s, *h2s_back;
1854
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001855 /* start by sending possibly pending window updates */
1856 if (h2c->rcvd_c > 0 &&
1857 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
1858 h2c_send_conn_wu(h2c) < 0)
1859 goto fail;
1860
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001861 /* First we always process the flow control list because the streams
1862 * waiting there were already elected for immediate emission but were
1863 * blocked just on this.
1864 */
1865
1866 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
1867 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
1868 h2c->st0 >= H2_CS_ERROR)
1869 break;
1870
1871 /* In theory it's possible that h2s->cs == NULL here :
1872 * - client sends crap that causes a parse error
1873 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1874 * - RST_STREAM cannot be emitted because mux is busy/full
1875 * - stream gets notified, detaches and quits
1876 * - mux buffer gets ready and wakes pending streams up
1877 * - bam!
1878 */
1879 h2s->flags &= ~H2_SF_BLK_ANY;
1880
1881 if (h2s->cs) {
1882 h2s->cs->data_cb->send(h2s->cs);
1883 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001884 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001885 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001886 }
1887
1888 /* depending on callee's blocking reasons, we may queue in send
1889 * list or completely dequeue.
1890 */
1891 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
1892 if (h2s->flags & H2_SF_BLK_ANY) {
1893 LIST_DEL(&h2s->list);
1894 LIST_ADDQ(&h2c->send_list, &h2s->list);
1895 }
1896 else {
1897 LIST_DEL(&h2s->list);
1898 LIST_INIT(&h2s->list);
1899 if (h2s->cs)
1900 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001901 else {
1902 /* just sent the last frame for this orphaned stream */
1903 eb32_delete(&h2s->by_id);
1904 pool_free2(pool2_h2s, h2s);
1905 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001906 }
1907 }
1908 }
1909
1910 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
1911 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
1912 break;
1913
1914 /* In theory it's possible that h2s->cs == NULL here :
1915 * - client sends crap that causes a parse error
1916 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1917 * - RST_STREAM cannot be emitted because mux is busy/full
1918 * - stream gets notified, detaches and quits
1919 * - mux buffer gets ready and wakes pending streams up
1920 * - bam!
1921 */
1922 h2s->flags &= ~H2_SF_BLK_ANY;
1923
1924 if (h2s->cs) {
1925 h2s->cs->data_cb->send(h2s->cs);
1926 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001927 } else {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001928 h2s_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001929 }
1930 /* depending on callee's blocking reasons, we may queue in fctl
1931 * list or completely dequeue.
1932 */
1933 if (h2s->flags & H2_SF_BLK_MFCTL) {
1934 /* stream hit the connection's flow control */
1935 LIST_DEL(&h2s->list);
1936 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
1937 }
1938 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
1939 LIST_DEL(&h2s->list);
1940 LIST_INIT(&h2s->list);
1941 if (h2s->cs)
1942 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001943 else {
1944 /* just sent the last frame for this orphaned stream */
1945 eb32_delete(&h2s->by_id);
1946 pool_free2(pool2_h2s, h2s);
1947 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001948 }
1949 }
1950
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001951 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01001952 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001953 if (h2c->st0 == H2_CS_ERROR) {
1954 if (h2c->max_id >= 0) {
1955 h2c_send_goaway_error(h2c, NULL);
1956 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
1957 return 0;
1958 }
1959
1960 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
1961 }
1962 return 1;
1963 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001964 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02001965}
1966
Willy Tarreau71681172017-10-23 14:39:06 +02001967
Willy Tarreau62f52692017-10-08 23:01:42 +02001968/*********************************************************/
1969/* functions below are I/O callbacks from the connection */
1970/*********************************************************/
1971
1972/* callback called on recv event by the connection handler */
1973static void h2_recv(struct connection *conn)
1974{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001975 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001976 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001977 int max;
1978
1979 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001980 return;
1981
1982 if (h2c->flags & H2_CF_DEM_BLOCK_ANY)
1983 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001984
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001985 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001986 if (!buf) {
1987 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001988 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001989 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001990
Willy Tarreaua2af5122017-10-09 11:56:46 +02001991 /* note: buf->o == 0 */
1992 max = buf->size - buf->i;
1993 if (!max) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001994 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001995 return;
1996 }
1997
1998 conn->xprt->rcv_buf(conn, buf, max);
1999 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002000 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002001
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002002 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002003 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002004 return;
2005 }
2006
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002007 if (buf->i == buf->size)
2008 h2c->flags |= H2_CF_DEM_DFULL;
2009
Willy Tarreaubc933932017-10-09 16:21:43 +02002010 h2_process_demux(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002011
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002012 /* after streams have been processed, we should have made some room */
Willy Tarreau081d4722017-05-16 21:51:05 +02002013 if (h2c->st0 >= H2_CS_ERROR)
2014 buf->i = 0;
2015
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002016 if (buf->i != buf->size)
2017 h2c->flags &= ~H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002018 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02002019}
2020
2021/* callback called on send event by the connection handler */
2022static void h2_send(struct connection *conn)
2023{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002024 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02002025 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002026
2027 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002028 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002029
2030 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2031 /* a handshake was requested */
2032 return;
2033 }
2034
Willy Tarreaubc933932017-10-09 16:21:43 +02002035 /* This loop is quite simple : it tries to fill as much as it can from
2036 * pending streams into the existing buffer until it's reportedly full
2037 * or the end of send requests is reached. Then it tries to send this
2038 * buffer's contents out, marks it not full if at least one byte could
2039 * be sent, and tries again.
2040 *
2041 * The snd_buf() function normally takes a "flags" argument which may
2042 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2043 * data immediately comes and CO_SFL_STREAMER to indicate that the
2044 * connection is streaming lots of data (used to increase TLS record
2045 * size at the expense of latency). The former can be sent any time
2046 * there's a buffer full flag, as it indicates at least one stream
2047 * attempted to send and failed so there are pending data. An
2048 * alternative would be to set it as long as there's an active stream
2049 * but that would be problematic for ACKs until we have an absolute
2050 * guarantee that all waiters have at least one byte to send. The
2051 * latter should possibly not be set for now.
2052 */
2053
2054 done = 0;
2055 while (!done) {
2056 unsigned int flags = 0;
2057
2058 /* fill as much as we can into the current buffer */
2059 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2060 done = h2_process_mux(h2c);
2061
2062 if (conn->flags & CO_FL_ERROR)
2063 break;
2064
2065 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2066 flags |= CO_SFL_MSG_MORE;
2067
Willy Tarreau319994a2017-11-07 11:03:56 +01002068 if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
Willy Tarreaubc933932017-10-09 16:21:43 +02002069 break;
2070
2071 /* wrote at least one byte, the buffer is not full anymore */
2072 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2073 }
2074
Willy Tarreaua2af5122017-10-09 11:56:46 +02002075 if (conn->flags & CO_FL_SOCK_WR_SH) {
2076 /* output closed, nothing to send, clear the buffer to release it */
2077 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002078 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002079}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002080
Willy Tarreau62f52692017-10-08 23:01:42 +02002081/* callback called on any event by the connection handler.
2082 * It applies changes and returns zero, or < 0 if it wants immediate
2083 * destruction of the connection (which normally doesn not happen in h2).
2084 */
2085static int h2_wake(struct connection *conn)
2086{
Willy Tarreaua2af5122017-10-09 11:56:46 +02002087 struct h2c *h2c = conn->mux_ctx;
2088
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002089 /*
2090 * If we received early data, try to wake any stream, just in case
2091 * at least one of them was waiting for the handshake
2092 */
2093 if ((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA | CO_FL_HANDSHAKE)) ==
2094 CO_FL_EARLY_DATA) {
2095 h2_wake_some_streams(h2c, 0, 0);
2096 conn->flags &= ~CO_FL_EARLY_DATA;
2097 }
Willy Tarreau26bd7612017-10-09 16:47:04 +02002098 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002099 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2100 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2101 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002102 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002103
2104 if (eb_is_empty(&h2c->streams_by_id)) {
2105 /* no more stream, kill the connection now */
2106 h2_release(conn);
2107 return -1;
2108 }
2109 else {
2110 /* some streams still there, we need to signal them all and
2111 * wait for their departure.
2112 */
2113 __conn_xprt_stop_recv(conn);
2114 __conn_xprt_stop_send(conn);
2115 return 0;
2116 }
2117 }
2118
2119 if (!h2c->dbuf->i)
2120 h2_release_dbuf(h2c);
2121
2122 /* stop being notified of incoming data if we can't process them */
2123 if (h2c->st0 >= H2_CS_ERROR ||
2124 (h2c->flags & H2_CF_DEM_BLOCK_ANY) || conn_xprt_read0_pending(conn)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002125 __conn_xprt_stop_recv(conn);
2126 }
2127 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002128 __conn_xprt_want_recv(conn);
2129 }
2130
2131 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002132 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2133 (h2c->st0 == H2_CS_ERROR ||
2134 h2c->mbuf->o ||
2135 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2136 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002137 __conn_xprt_want_send(conn);
2138 }
2139 else {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002140 h2_release_mbuf(h2c);
2141 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002142 }
2143
Willy Tarreau3f133572017-10-31 19:21:06 +01002144 if (h2c->task) {
2145 if (eb_is_empty(&h2c->streams_by_id)) {
2146 h2c->task->expire = tick_add(now_ms, h2c->timeout);
2147 task_queue(h2c->task);
2148 }
2149 else
2150 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002151 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002152 return 0;
2153}
2154
Willy Tarreauea392822017-10-31 10:02:25 +01002155/* Connection timeout management. The principle is that if there's no receipt
2156 * nor sending for a certain amount of time, the connection is closed. If the
2157 * MUX buffer still has lying data or is not allocatable, the connection is
2158 * immediately killed. If it's allocatable and empty, we attempt to send a
2159 * GOAWAY frame.
2160 */
2161static struct task *h2_timeout_task(struct task *t)
2162{
2163 struct h2c *h2c = t->context;
2164 int expired = tick_is_expired(t->expire, now_ms);
2165
2166 if (!expired)
2167 return t;
2168
2169 h2c_error(h2c, H2_ERR_NO_ERROR);
2170 h2_wake_some_streams(h2c, 0, 0);
2171
2172 if (h2c->mbuf->o) {
2173 /* don't even try to send a GOAWAY, the buffer is stuck */
2174 h2c->flags |= H2_CF_GOAWAY_FAILED;
2175 }
2176
2177 /* try to send but no need to insist */
2178 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2179 h2c->flags |= H2_CF_GOAWAY_FAILED;
2180
2181 if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn))
2182 h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0);
2183
2184 if (!eb_is_empty(&h2c->streams_by_id))
2185 goto wait;
2186
2187 h2_release(h2c->conn);
2188 return NULL;
2189
2190 wait:
2191 /* the streams have been notified, we must let them finish and close */
2192 h2c->task = NULL;
2193 task_delete(t);
2194 task_free(t);
2195 return NULL;
2196}
2197
2198
Willy Tarreau62f52692017-10-08 23:01:42 +02002199/*******************************************/
2200/* functions below are used by the streams */
2201/*******************************************/
2202
2203/*
2204 * Attach a new stream to a connection
2205 * (Used for outgoing connections)
2206 */
2207static struct conn_stream *h2_attach(struct connection *conn)
2208{
2209 return NULL;
2210}
2211
2212/* callback used to update the mux's polling flags after changing a cs' status.
2213 * The caller (cs_update_mux_polling) will take care of propagating any changes
2214 * to the transport layer.
2215 */
2216static void h2_update_poll(struct conn_stream *cs)
2217{
Willy Tarreau1d393222017-10-17 10:26:19 +02002218 struct h2s *h2s = cs->ctx;
2219
2220 if (!h2s)
2221 return;
2222
Willy Tarreaud7739c82017-10-30 15:38:23 +01002223 /* we may unblock a blocked read */
2224
2225 if (cs->flags & CS_FL_DATA_RD_ENA &&
2226 h2s->h2c->flags & H2_CF_DEM_SFULL && h2s->h2c->dsi == h2s->id) {
2227 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
2228 conn_xprt_want_recv(cs->conn);
2229 }
2230
Willy Tarreau1d393222017-10-17 10:26:19 +02002231 /* Note: the stream and stream-int code doesn't allow us to perform a
2232 * synchronous send() here unfortunately, because this code is called
2233 * as si_update() from the process_stream() context. This means that
2234 * we have to queue the current cs and defer its processing after the
2235 * connection's cs list is processed anyway.
2236 */
2237
2238 if (cs->flags & CS_FL_DATA_WR_ENA) {
2239 if (LIST_ISEMPTY(&h2s->list)) {
2240 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2241 !h2s->h2c->mbuf->o && // not yet subscribed
2242 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2243 conn_xprt_want_send(cs->conn);
2244 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2245 }
2246 }
2247 else if (!LIST_ISEMPTY(&h2s->list)) {
2248 LIST_DEL(&h2s->list);
2249 LIST_INIT(&h2s->list);
2250 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2251 }
2252
2253 /* this can happen from within si_chk_snd() */
2254 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2255 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002256}
2257
2258/*
2259 * Detach the stream from the connection and possibly release the connection.
2260 */
2261static void h2_detach(struct conn_stream *cs)
2262{
Willy Tarreau60935142017-10-16 18:11:19 +02002263 struct h2s *h2s = cs->ctx;
2264 struct h2c *h2c;
2265
2266 cs->ctx = NULL;
2267 if (!h2s)
2268 return;
2269
2270 h2c = h2s->h2c;
2271 h2s->cs = NULL;
2272
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002273 /* this stream may be blocked waiting for some data to leave (possibly
2274 * an ES or RST frame), so orphan it in this case.
2275 */
2276 if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))
2277 return;
2278
Willy Tarreau541dd822017-11-23 18:12:50 +01002279 /* the stream could be in the send list */
2280 LIST_DEL(&h2s->list);
2281
Willy Tarreau45f752e2017-10-30 15:44:59 +01002282 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2283 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2284 /* unblock the connection if it was blocked on this
2285 * stream.
2286 */
2287 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2288 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2289 conn_xprt_want_recv(cs->conn);
2290 conn_xprt_want_send(cs->conn);
2291 }
2292
Willy Tarreau60935142017-10-16 18:11:19 +02002293 if (h2s->by_id.node.leaf_p) {
2294 /* h2s still attached to the h2c */
2295 eb32_delete(&h2s->by_id);
2296
2297 /* We don't want to close right now unless we're removing the
2298 * last stream, and either the connection is in error, or it
2299 * reached the ID already specified in a GOAWAY frame received
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002300 * or sent (as seen by last_sid >= 0).
Willy Tarreau60935142017-10-16 18:11:19 +02002301 */
Willy Tarreau83906c22017-11-07 11:48:46 +01002302 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2303 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau60935142017-10-16 18:11:19 +02002304 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreau83906c22017-11-07 11:48:46 +01002305 (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */
2306 (conn_xprt_read0_pending(h2c->conn) ||
2307 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
Willy Tarreau60935142017-10-16 18:11:19 +02002308 /* no more stream will come, kill it now */
2309 h2_release(h2c->conn);
2310 }
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002311 else if (h2c->task) {
2312 if (eb_is_empty(&h2c->streams_by_id)) {
2313 h2c->task->expire = tick_add(now_ms, h2c->timeout);
2314 task_queue(h2c->task);
2315 }
2316 else
2317 h2c->task->expire = TICK_ETERNITY;
2318 }
Willy Tarreau60935142017-10-16 18:11:19 +02002319 }
2320 pool_free2(pool2_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002321}
2322
2323static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2324{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002325 struct h2s *h2s = cs->ctx;
2326
2327 if (!mode)
2328 return;
2329
Willy Tarreau721c9742017-11-07 11:05:42 +01002330 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002331 return;
2332
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002333 /* if no outgoing data was seen on this stream, it means it was
2334 * closed with a "tcp-request content" rule that is normally
2335 * used to kill the connection ASAP (eg: limit abuse). In this
2336 * case we send a goaway to close the connection.
2337 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002338 if (!(h2s->flags & H2_SF_RST_SENT) &&
2339 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2340 return;
2341
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002342 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2343 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
2344 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2345 return;
2346
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002347 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2348 conn_xprt_want_send(cs->conn);
2349
2350 h2s->st = H2_SS_CLOSED;
Willy Tarreau62f52692017-10-08 23:01:42 +02002351}
2352
2353static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2354{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002355 struct h2s *h2s = cs->ctx;
2356
Willy Tarreau721c9742017-11-07 11:05:42 +01002357 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002358 return;
2359
Willy Tarreau67434202017-11-06 20:20:51 +01002360 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002361 /* we can cleanly close using an empty data frame only after headers */
2362
2363 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2364 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002365 return;
Willy Tarreau58e32082017-11-07 14:41:09 +01002366
2367 if (h2s->st == H2_SS_HREM)
2368 h2s->st = H2_SS_CLOSED;
2369 else
2370 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002371 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002372 /* if no outgoing data was seen on this stream, it means it was
2373 * closed with a "tcp-request content" rule that is normally
2374 * used to kill the connection ASAP (eg: limit abuse). In this
2375 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002376 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002377 if (!(h2s->flags & H2_SF_RST_SENT) &&
2378 h2s_send_rst_stream(h2s->h2c, h2s) <= 0)
2379 return;
2380
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002381 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2382 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Willy Tarreaua1349f02017-10-31 07:41:55 +01002383 h2c_send_goaway_error(h2s->h2c, h2s) <= 0)
2384 return;
2385
Willy Tarreau58e32082017-11-07 14:41:09 +01002386 h2s->st = H2_SS_CLOSED;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002387 }
2388
2389 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2390 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002391}
2392
Willy Tarreau13278b42017-10-13 19:23:14 +02002393/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2394 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2395 * proceed. Stream errors are reported in h2s->errcode and connection errors
2396 * in h2c->errcode. The caller must already have checked the frame header and
2397 * ensured that the frame was complete or the buffer full.
2398 */
2399static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2400{
2401 struct h2c *h2c = h2s->h2c;
2402 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002403 struct chunk *tmp = get_trash_chunk();
2404 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau68dd9852017-07-03 14:44:26 +02002405 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002406 int flen = h2c->dfl;
2407 int outlen = 0;
2408 int wrap;
2409 int try;
2410
2411 if (!h2c->dfl) {
2412 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
2413 return 0;
2414 }
2415
2416 /* if the input buffer wraps, take a temporary copy of it (rare) */
2417 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2418 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002419 copy = alloc_trash_chunk();
2420 if (!copy) {
2421 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2422 goto fail;
2423 }
2424 memcpy(copy->str, h2c->dbuf->p, wrap);
2425 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2426 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002427 }
2428
2429 /* The padlen is the first byte before data, and the padding appears
2430 * after data. padlen+data+padding are included in flen.
2431 */
2432 if (h2c->dff & H2_F_HEADERS_PADDED) {
2433 if (*hdrs >= flen) {
2434 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2435 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau13278b42017-10-13 19:23:14 +02002436 return 0;
2437 }
2438 flen -= *hdrs + 1;
2439 hdrs += 1; // skip Pad Length
2440 }
2441
2442 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2443 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
2444 hdrs += 5; // stream dep = 4, weight = 1
2445 flen -= 5;
2446 }
2447
2448 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2449 * don't support this for now and can't even decompress so we have to
2450 * break the connection.
2451 */
2452 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2453 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002454 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002455 }
2456
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002457 /* we can't retry a failed decompression operation so we must be very
2458 * careful not to take any risks. In practice the output buffer is
2459 * always empty except maybe for trailers, so these operations almost
2460 * never happen.
2461 */
2462 if (unlikely(buf->o)) {
2463 /* need to let the output buffer flush and
2464 * mark the buffer for later wake up.
2465 */
2466 goto fail;
2467 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002468
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002469 if (unlikely(buffer_space_wraps(buf))) {
2470 /* it doesn't fit and the buffer is fragmented,
2471 * so let's defragment it and try again.
2472 */
2473 buffer_slow_realign(buf);
2474 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002475
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002476 /* first check if we have some room after p+i */
2477 try = buf->data + buf->size - (buf->p + buf->i);
2478
2479 /* otherwise continue between data and p-o */
2480 if (try <= 0) {
2481 try = buf->p - (buf->data + buf->o);
2482 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002483 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002484 }
2485 if (try > count)
2486 try = count;
2487
2488 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2489 sizeof(list)/sizeof(list[0]), tmp);
2490 if (outlen < 0) {
2491 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2492 goto fail;
2493 }
2494
2495 /* OK now we have our header list in <list> */
2496 outlen = h2_make_h1_request(list, bi_end(buf), try);
2497
2498 if (outlen < 0) {
2499 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2500 goto fail;
2501 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002502
2503 /* now consume the input data */
2504 bi_del(h2c->dbuf, h2c->dfl);
2505 h2c->st0 = H2_CS_FRAME_H;
2506 buf->i += outlen;
2507
2508 /* don't send it before returning data!
2509 * FIXME: should we instead try to send it much later, after the
2510 * response ? This would require that we keep a copy of it in h2s.
2511 */
2512 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2513 h2s->cs->flags |= CS_FL_EOS;
2514 h2s->flags |= H2_SF_ES_RCVD;
2515 }
2516
Willy Tarreau68dd9852017-07-03 14:44:26 +02002517 leave:
2518 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002519 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002520 fail:
2521 outlen = 0;
2522 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002523}
2524
Willy Tarreau454f9052017-10-26 19:40:35 +02002525/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2526 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2527 * in use, a new chunk is emitted for each frame. This is supposed to fit
2528 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2529 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2530 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2531 * parser state is automatically updated. Returns the number of bytes emitted
2532 * if > 0, or 0 if it couldn't proceed. Stream errors are reported in
2533 * h2s->errcode and connection errors in h2c->errcode. The caller must already
2534 * have checked the frame header and ensured that the frame was complete or the
2535 * buffer full. It changes the frame state to FRAME_A once done.
2536 */
2537static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2538{
2539 struct h2c *h2c = h2s->h2c;
2540 int block1, block2;
2541 unsigned int flen = h2c->dfl;
2542 unsigned int padlen = 0;
2543 int offset = 0;
2544
2545 if (h2c->dbuf->i < flen)
2546 return 0;
2547
2548 /* The padlen is the first byte before data, and the padding appears
2549 * after data. padlen+data+padding are included in flen.
2550 */
2551 if (h2c->dff & H2_F_HEADERS_PADDED) {
2552 padlen = *(uint8_t *)bi_ptr(h2c->dbuf);
2553 if (padlen >= flen) {
2554 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2555 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002556 return 0;
2557 }
2558 flen -= padlen + 1;
2559 offset = 1; // skip Pad Length
2560 }
2561
2562 /* does it fit in output buffer or should we wait ? */
2563 if (buf->i + buf->o + flen > buf->size) {
2564 h2c->flags |= H2_CF_DEM_SFULL;
2565 return 0;
2566 }
2567
2568 /* Block1 is the length of the first block before the buffer wraps,
2569 * block2 is the optional second block to reach the end of the frame.
2570 */
2571 block1 = bi_contig_data(h2c->dbuf);
2572 if (block1 > offset + flen)
2573 block1 = offset + flen;
2574 block1 -= offset; // skip Pad Length
2575 block2 = flen - block1;
2576
2577 if (block1)
2578 bi_putblk(buf, b_ptr(h2c->dbuf, offset), block1);
2579
2580 if (block2)
2581 bi_putblk(buf, b_ptr(h2c->dbuf, offset + block1), block2);
2582
2583 /* now mark the input data as consumed (will be deleted from the buffer
2584 * by the caller when seeing FRAME_A after sending the window update).
2585 */
2586 h2c->rcvd_c += h2c->dfl;
2587 h2c->rcvd_s += h2c->dfl; // warning, this can also affect the closed streams!
2588 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2589
2590 /* don't send it before returning data!
2591 * FIXME: should we instead try to send it much later, after the
2592 * response ? This would require that we keep a copy of it in h2s.
2593 */
2594 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2595 h2s->cs->flags |= CS_FL_EOS;
2596 h2s->flags |= H2_SF_ES_RCVD;
2597 }
2598
2599 return flen;
2600}
2601
Willy Tarreau62f52692017-10-08 23:01:42 +02002602/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002603 * Called from the upper layer to get more data, up to <count> bytes. The
2604 * caller is responsible for never asking for more data than what is available
2605 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002606 */
2607static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2608{
Willy Tarreau13278b42017-10-13 19:23:14 +02002609 struct h2s *h2s = cs->ctx;
2610 struct h2c *h2c = h2s->h2c;
2611 int ret = 0;
2612
2613 if (h2c->st0 != H2_CS_FRAME_P)
2614 return 0; // no pre-parsed frame yet
2615
2616 if (h2c->dsi != h2s->id)
2617 return 0; // not for us
2618
2619 if (!h2c->dbuf->size)
2620 return 0; // empty buffer
2621
2622 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2623 return 0; // incomplete input frame
2624
2625 switch (h2c->dft) {
2626 case H2_FT_HEADERS:
2627 ret = h2_frt_decode_headers(h2s, buf, count);
2628 break;
2629
Willy Tarreau454f9052017-10-26 19:40:35 +02002630 case H2_FT_DATA:
2631 ret = h2_frt_transfer_data(h2s, buf, count);
2632 break;
2633
Willy Tarreau13278b42017-10-13 19:23:14 +02002634 default:
2635 ret = 0;
2636 }
2637 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002638}
2639
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002640/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2641 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2642 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2643 * to the number of buffer bytes consumed.
2644 */
2645static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2646{
2647 struct http_hdr list[MAX_HTTP_HDR];
2648 struct h2c *h2c = h2s->h2c;
2649 struct h1m *h1m = &h2s->res;
2650 struct chunk outbuf;
2651 int es_now = 0;
2652 int ret = 0;
2653 int hdr;
2654
2655 if (h2c_mux_busy(h2c, h2s)) {
2656 h2s->flags |= H2_SF_BLK_MBUSY;
2657 return 0;
2658 }
2659
2660 if (!h2_get_mbuf(h2c)) {
2661 h2c->flags |= H2_CF_MUX_MALLOC;
2662 h2s->flags |= H2_SF_BLK_MROOM;
2663 return 0;
2664 }
2665
2666 /* First, try to parse the H1 response and index it into <list>.
2667 * NOTE! Since it comes from haproxy, we *know* that a response header
2668 * block does not wrap and we can safely read it this way without
2669 * having to realign the buffer.
2670 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002671 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002672 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2673 list, sizeof(list)/sizeof(list[0]), h1m);
2674 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01002675 /* incomplete or invalid response, this is abnormal coming from
2676 * haproxy and may only result in a bad errorfile or bad Lua code
2677 * so that won't be fixed, raise an error now.
2678 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002679 * FIXME: we should instead add the ability to only return a
2680 * 502 bad gateway. But in theory this is not supposed to
2681 * happen.
2682 */
2683 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2684 ret = 0;
2685 goto end;
2686 }
2687
2688 chunk_reset(&outbuf);
2689
Willy Tarreauaf1e4f52017-10-30 21:54:49 +01002690 try_again:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002691 while (1) {
2692 outbuf.str = bo_end(h2c->mbuf);
2693 outbuf.size = bo_contig_space(h2c->mbuf);
2694 outbuf.len = 0;
2695
2696 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2697 break;
2698 realign_again:
2699 buffer_slow_realign(h2c->mbuf);
2700 }
2701
2702 if (outbuf.size < 9) {
2703 h2c->flags |= H2_CF_MUX_MFULL;
2704 h2s->flags |= H2_SF_BLK_MROOM;
2705 ret = 0;
2706 goto end;
2707 }
2708
2709 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2710 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2711 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2712 outbuf.len = 9;
2713
2714 /* encode status, which necessarily is the first one */
2715 if (outbuf.len < outbuf.size && h1m->status == 200)
2716 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2717 else if (outbuf.len < outbuf.size && h1m->status == 304)
2718 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01002719 else if (unlikely(list[0].v.len != 3)) {
2720 /* this is an unparsable response */
2721 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2722 ret = 0;
2723 goto end;
2724 }
2725 else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002726 /* basic encoding of the status code */
2727 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2728 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2729 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2730 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2731 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2732 }
2733 else {
2734 if (buffer_space_wraps(h2c->mbuf))
2735 goto realign_again;
2736
2737 h2c->flags |= H2_CF_MUX_MFULL;
2738 h2s->flags |= H2_SF_BLK_MROOM;
2739 ret = 0;
2740 goto end;
2741 }
2742
2743 /* encode all headers, stop at empty name */
2744 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01002745 /* these ones do not exist in H2 and must be dropped. */
2746 if (isteq(list[hdr].n, ist("connection")) ||
2747 isteq(list[hdr].n, ist("proxy-connection")) ||
2748 isteq(list[hdr].n, ist("keep-alive")) ||
2749 isteq(list[hdr].n, ist("upgrade")) ||
2750 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002751 continue;
2752
2753 if (isteq(list[hdr].n, ist("")))
2754 break; // end
2755
2756 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2757 /* output full */
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
2768 /* we may need to add END_STREAM */
2769 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2770 es_now = 1;
2771
2772 /* update the frame's size */
2773 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2774
2775 if (es_now)
2776 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2777
2778 /* consume incoming H1 response */
2779 bo_del(buf, ret);
2780
2781 /* commit the H2 response */
2782 h2c->mbuf->o += outbuf.len;
2783 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
Willy Tarreau67434202017-11-06 20:20:51 +01002784 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002785
2786 /* for now we don't implemented CONTINUATION, so we wait for a
2787 * body or directly end in TRL2.
2788 */
2789 if (es_now) {
2790 h1m->state = HTTP_MSG_DONE;
2791 h2s->flags |= H2_SF_ES_SENT;
2792 if (h2s->st == H2_SS_OPEN)
2793 h2s->st = H2_SS_HLOC;
2794 else
2795 h2s->st = H2_SS_CLOSED;
2796 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01002797 else if (h1m->status >= 100 && h1m->status < 200) {
2798 h1m->state = HTTP_MSG_RPBEFORE;
2799 h1m->status = 0;
2800 h1m->flags = 0;
2801 goto next_header_block;
2802 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002803 else
2804 h1m->state = (h1m->flags & H1_MF_CLEN) ? HTTP_MSG_BODY : HTTP_MSG_CHUNK_SIZE;
2805
2806 end:
2807 //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));
2808 return ret;
2809}
2810
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002811/* Try to send a DATA frame matching HTTP/1 response present in the response
2812 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
2813 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
2814 * corresponds to the number of buffer bytes consumed.
2815 */
2816static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
2817{
2818 struct h2c *h2c = h2s->h2c;
2819 struct h1m *h1m = &h2s->res;
2820 struct chunk outbuf;
2821 int ret = 0;
2822 int total = 0;
2823 int es_now = 0;
2824 int size = 0;
2825 char *blk1, *blk2;
2826 int len1, len2;
2827
2828 if (h2c_mux_busy(h2c, h2s)) {
2829 h2s->flags |= H2_SF_BLK_MBUSY;
2830 goto end;
2831 }
2832
2833 if (!h2_get_mbuf(h2c)) {
2834 h2c->flags |= H2_CF_MUX_MALLOC;
2835 h2s->flags |= H2_SF_BLK_MROOM;
2836 goto end;
2837 }
2838
2839 new_frame:
2840 if (!buf->o)
2841 goto end;
2842
2843 chunk_reset(&outbuf);
2844
2845 while (1) {
2846 outbuf.str = bo_end(h2c->mbuf);
2847 outbuf.size = bo_contig_space(h2c->mbuf);
2848 outbuf.len = 0;
2849
2850 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2851 break;
2852 realign_again:
2853 buffer_slow_realign(h2c->mbuf);
2854 }
2855
2856 if (outbuf.size < 9) {
2857 h2c->flags |= H2_CF_MUX_MFULL;
2858 h2s->flags |= H2_SF_BLK_MROOM;
2859 goto end;
2860 }
2861
2862 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
2863 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
2864 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2865 outbuf.len = 9;
2866
2867 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
2868 case 0: /* no content length, read till SHUTW */
2869 size = buf->o;
2870 break;
2871 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
2872 size = buf->o;
2873 if ((long long)size > h1m->curr_len)
2874 size = h1m->curr_len;
2875 break;
2876 default: /* te:chunked : parse chunks */
2877 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
2878 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
2879 if (!ret)
2880 goto end;
2881
2882 if (ret < 0) {
2883 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2884 h1m->err_pos = ret;
2885 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2886 goto end;
2887 }
2888 bo_del(buf, ret);
2889 total += ret;
2890 h1m->state = HTTP_MSG_CHUNK_SIZE;
2891 }
2892
2893 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
2894 unsigned int chunk;
2895
2896 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
2897 if (!ret)
2898 goto end;
2899
2900 if (ret < 0) {
2901 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2902 h1m->err_pos = ret;
2903 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2904 goto end;
2905 }
2906
2907 size = chunk;
2908 h1m->curr_len = chunk;
2909 h1m->body_len += chunk;
2910 bo_del(buf, ret);
2911 total += ret;
2912 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
2913 if (!size)
2914 goto send_empty;
2915 }
2916
2917 /* in MSG_DATA state, continue below */
2918 size = h1m->curr_len;
2919 break;
2920 }
2921
2922 /* we have in <size> the exact number of bytes we need to copy from
2923 * the H1 buffer. We need to check this against the connection's and
2924 * the stream's send windows, and to ensure that this fits in the max
2925 * frame size and in the buffer's available space minus 9 bytes (for
2926 * the frame header). The connection's flow control is applied last so
2927 * that we can use a separate list of streams which are immediately
2928 * unblocked on window opening. Note: we don't implement padding.
2929 */
2930
2931 if (size > buf->o)
2932 size = buf->o;
2933
2934 if (size > h2s->mws)
2935 size = h2s->mws;
2936
2937 if (size <= 0) {
2938 h2s->flags |= H2_SF_BLK_SFCTL;
2939 goto end;
2940 }
2941
2942 if (h2c->mfs && size > h2c->mfs)
2943 size = h2c->mfs;
2944
2945 if (size + 9 > outbuf.size) {
2946 /* we have an opportunity for enlarging the too small
2947 * available space, let's try.
2948 */
2949 if (buffer_space_wraps(h2c->mbuf))
2950 goto realign_again;
2951 size = outbuf.size - 9;
2952 }
2953
2954 if (size <= 0) {
2955 h2c->flags |= H2_CF_MUX_MFULL;
2956 h2s->flags |= H2_SF_BLK_MROOM;
2957 goto end;
2958 }
2959
2960 if (size > h2c->mws)
2961 size = h2c->mws;
2962
2963 if (size <= 0) {
2964 h2s->flags |= H2_SF_BLK_MFCTL;
2965 goto end;
2966 }
2967
2968 /* copy whatever we can */
2969 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
2970 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
2971 if (ret == 1)
2972 len2 = 0;
2973
2974 if (!ret || len1 + len2 < size) {
2975 /* FIXME: must normally never happen */
2976 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2977 goto end;
2978 }
2979
2980 /* limit len1/len2 to size */
2981 if (len1 + len2 > size) {
2982 int sub = len1 + len2 - size;
2983
2984 if (len2 > sub)
2985 len2 -= sub;
2986 else {
2987 sub -= len2;
2988 len2 = 0;
2989 len1 -= sub;
2990 }
2991 }
2992
2993 /* now let's copy this this into the output buffer */
2994 memcpy(outbuf.str + 9, blk1, len1);
2995 if (len2)
2996 memcpy(outbuf.str + 9 + len1, blk2, len2);
2997
2998 send_empty:
2999 /* we may need to add END_STREAM */
3000 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3001 * could rely on the MSG_MORE flag as a hint for this ?
3002 */
3003 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
3004 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
3005 es_now = 1;
3006
3007 /* update the frame's size */
3008 h2_set_frame_size(outbuf.str, size);
3009
3010 if (es_now)
3011 outbuf.str[4] |= H2_F_DATA_END_STREAM;
3012
3013 /* commit the H2 response */
3014 h2c->mbuf->o += size + 9;
3015 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
3016
3017 /* consume incoming H1 response */
3018 if (size > 0) {
3019 bo_del(buf, size);
3020 total += size;
3021 h1m->curr_len -= size;
3022 h2s->mws -= size;
3023 h2c->mws -= size;
3024
3025 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
3026 h1m->state = HTTP_MSG_CHUNK_CRLF;
3027 goto new_frame;
3028 }
3029 }
3030
3031 if (es_now) {
3032 if (h2s->st == H2_SS_OPEN)
3033 h2s->st = H2_SS_HLOC;
3034 else
3035 h2s->st = H2_SS_CLOSED;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003036
3037 if (!(h1m->flags & H1_MF_CHNK))
3038 h1m->state = HTTP_MSG_DONE;
3039
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003040 h2s->flags |= H2_SF_ES_SENT;
3041 }
3042
3043 end:
3044 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);
3045 return total;
3046}
3047
Willy Tarreau62f52692017-10-08 23:01:42 +02003048/* Called from the upper layer, to send data */
3049static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
3050{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003051 struct h2s *h2s = cs->ctx;
3052 int total = 0;
3053
Willy Tarreauc4312d32017-11-07 12:01:53 +01003054 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o)
3055 h2s->flags |= H2_SF_OUTGOING_DATA;
3056
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003057 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
3058 if (h2s->res.state < HTTP_MSG_BODY) {
3059 total += h2s_frt_make_resp_headers(h2s, buf);
3060
3061 if (h2s->st == H2_SS_ERROR)
3062 break;
3063
3064 if (h2s->flags & H2_SF_BLK_ANY)
3065 break;
3066 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003067 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
3068 total += h2s_frt_make_resp_data(h2s, buf);
3069
3070 if (h2s->st == H2_SS_ERROR)
3071 break;
3072
3073 if (h2s->flags & H2_SF_BLK_ANY)
3074 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003075 }
3076 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
3077 /* consume the trailers if any (we don't forward them for now) */
3078 int count = h1_measure_trailers(buf);
3079
3080 if (unlikely(count <= 0)) {
3081 if (count < 0)
3082 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3083 break;
3084 }
3085 total += count;
3086 bo_del(buf, count);
3087 h2s->res.state = HTTP_MSG_DONE;
3088 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003089 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003090 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003091 cs->flags |= CS_FL_ERROR;
3092 break;
3093 }
3094 }
3095
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003096 /* RST are sent similarly to frame acks */
3097 if (h2s->st == H2_SS_ERROR) {
3098 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003099 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003100 h2s->st = H2_SS_CLOSED;
3101 }
3102
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003103 if (h2s->flags & H2_SF_BLK_SFCTL) {
3104 /* stream flow control, quit the list */
3105 LIST_DEL(&h2s->list);
3106 LIST_INIT(&h2s->list);
3107 }
3108
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003109 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003110}
3111
3112
3113/*******************************************************/
3114/* functions below are dedicated to the config parsers */
3115/*******************************************************/
3116
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003117/* config parser for global "tune.h2.header-table-size" */
3118static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3119 struct proxy *defpx, const char *file, int line,
3120 char **err)
3121{
3122 if (too_many_args(1, args, err, NULL))
3123 return -1;
3124
3125 h2_settings_header_table_size = atoi(args[1]);
3126 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3127 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3128 return -1;
3129 }
3130 return 0;
3131}
Willy Tarreau62f52692017-10-08 23:01:42 +02003132
Willy Tarreaue6baec02017-07-27 11:45:11 +02003133/* config parser for global "tune.h2.initial-window-size" */
3134static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3135 struct proxy *defpx, const char *file, int line,
3136 char **err)
3137{
3138 if (too_many_args(1, args, err, NULL))
3139 return -1;
3140
3141 h2_settings_initial_window_size = atoi(args[1]);
3142 if (h2_settings_initial_window_size < 0) {
3143 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3144 return -1;
3145 }
3146 return 0;
3147}
3148
Willy Tarreau5242ef82017-07-27 11:47:28 +02003149/* config parser for global "tune.h2.max-concurrent-streams" */
3150static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3151 struct proxy *defpx, const char *file, int line,
3152 char **err)
3153{
3154 if (too_many_args(1, args, err, NULL))
3155 return -1;
3156
3157 h2_settings_max_concurrent_streams = atoi(args[1]);
3158 if (h2_settings_max_concurrent_streams < 0) {
3159 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3160 return -1;
3161 }
3162 return 0;
3163}
3164
Willy Tarreau62f52692017-10-08 23:01:42 +02003165
3166/****************************************/
3167/* MUX initialization and instanciation */
3168/***************************************/
3169
3170/* The mux operations */
3171const struct mux_ops h2_ops = {
3172 .init = h2_init,
3173 .recv = h2_recv,
3174 .send = h2_send,
3175 .wake = h2_wake,
3176 .update_poll = h2_update_poll,
3177 .rcv_buf = h2_rcv_buf,
3178 .snd_buf = h2_snd_buf,
3179 .attach = h2_attach,
3180 .detach = h2_detach,
3181 .shutr = h2_shutr,
3182 .shutw = h2_shutw,
Willy Tarreau62f52692017-10-08 23:01:42 +02003183 .name = "H2",
3184};
3185
3186/* ALPN selection : this mux registers ALPN tolen "h2" */
3187static struct alpn_mux_list alpn_mux_h2 =
3188 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
3189
3190/* config keyword parsers */
3191static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003192 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003193 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003194 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003195 { 0, NULL, NULL }
3196}};
3197
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003198static void __h2_deinit(void)
3199{
Willy Tarreau18312642017-10-11 07:57:07 +02003200 pool_destroy2(pool2_h2s);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003201 pool_destroy2(pool2_h2c);
3202}
3203
Willy Tarreau62f52692017-10-08 23:01:42 +02003204__attribute__((constructor))
3205static void __h2_init(void)
3206{
3207 alpn_register_mux(&alpn_mux_h2);
3208 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003209 hap_register_post_deinit(__h2_deinit);
3210 pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
Willy Tarreau18312642017-10-11 07:57:07 +02003211 pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003212}