blob: bae6847eadc98f63523b470886ee2cfedffbb7fb [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 Tarreau5ab6b572017-09-22 08:05:00 +020024#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025
26
Willy Tarreau2a856182017-05-16 15:20:39 +020027/* dummy streams returned for idle and closed states */
28static const struct h2s *h2_closed_stream;
29static const struct h2s *h2_idle_stream;
30
Willy Tarreau5ab6b572017-09-22 08:05:00 +020031/* the h2c connection pool */
32static struct pool_head *pool2_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020033/* the h2s stream pool */
34static struct pool_head *pool2_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020035
36/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
44/* Flags indicating why writing to the demux is blocked. */
45#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
46#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
47#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
48#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
49#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
50#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
51#define H2_CF_DEM_BLOCK_ANY 0x000000FC // aggregate of the demux flags above
52
Willy Tarreau081d4722017-05-16 21:51:05 +020053/* other flags */
54#define H2_CF_GOAWAY_SENT 0x00000100 // a GOAWAY frame was successfully sent
55#define H2_CF_GOAWAY_FAILED 0x00000200 // a GOAWAY frame failed to be sent
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020056#define H2_CF_HEADERS_SENT 0x00000400 // a HEADERS frame was sent
Willy Tarreau081d4722017-05-16 21:51:05 +020057
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
106 struct eb_root streams_by_id; /* all active streams by their ID */
107 struct list send_list; /* list of blocked streams requesting to send */
108 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200109 struct buffer_wait dbuf_wait; /* wait list for demux buffer allocation */
Willy Tarreau14398122017-09-22 14:26:04 +0200110 struct buffer_wait mbuf_wait; /* wait list for mux buffer allocation */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200111};
112
Willy Tarreau18312642017-10-11 07:57:07 +0200113/* H2 stream state, in h2s->st */
114enum h2_ss {
115 H2_SS_IDLE = 0, // idle
116 H2_SS_RLOC, // reserved(local)
117 H2_SS_RREM, // reserved(remote)
118 H2_SS_OPEN, // open
119 H2_SS_HREM, // half-closed(remote)
120 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200121 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
122 H2_SS_RESET, // closed after sending RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200123 H2_SS_CLOSED, // closed
124 H2_SS_ENTRIES // must be last
125} __attribute__((packed));
126
127/* HTTP/2 stream flags (32 bit), in h2s->flags */
128#define H2_SF_NONE 0x00000000
129#define H2_SF_ES_RCVD 0x00000001
130#define H2_SF_ES_SENT 0x00000002
131
132#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
133#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
134
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200135/* stream flags indicating the reason the stream is blocked */
136#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
137#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
138#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
139#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
140#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
141
Willy Tarreau454f9052017-10-26 19:40:35 +0200142/* stream flags indicating how data is supposed to be sent */
143#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
144#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
145
146/* step we're currently in when sending chunks. This is needed because we may
147 * have to transfer chunks as large as a full buffer so there's no room left
148 * for size nor crlf around.
149 */
150#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
151#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
152#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
153
154#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
155
Willy Tarreau18312642017-10-11 07:57:07 +0200156/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
157 * it is being processed in the internal HTTP representation (H1 for now).
158 */
159struct h2s {
160 struct conn_stream *cs;
161 struct h2c *h2c;
162 struct h1m req, res; /* request and response parser state for H1 */
163 struct eb32_node by_id; /* place in h2c's streams_by_id */
164 struct list list; /* position in active/blocked lists if blocked>0 */
165 int32_t id; /* stream ID */
166 uint32_t flags; /* H2_SF_* */
167 int mws; /* mux window size for this stream */
168 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
169 enum h2_ss st;
170};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200171
Willy Tarreauc6405142017-09-21 20:23:50 +0200172/* descriptor for an h2 frame header */
173struct h2_fh {
174 uint32_t len; /* length, host order, 24 bits */
175 uint32_t sid; /* stream id, host order, 31 bits */
176 uint8_t ft; /* frame type */
177 uint8_t ff; /* frame flags */
178};
179
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200180/* a few settings from the global section */
181static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200182static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200183static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200184
Willy Tarreau2a856182017-05-16 15:20:39 +0200185/* a dmumy closed stream */
186static const struct h2s *h2_closed_stream = &(const struct h2s){
187 .cs = NULL,
188 .h2c = NULL,
189 .st = H2_SS_CLOSED,
190 .id = 0,
191};
192
193/* and a dummy idle stream for use with any unannounced stream */
194static const struct h2s *h2_idle_stream = &(const struct h2s){
195 .cs = NULL,
196 .h2c = NULL,
197 .st = H2_SS_IDLE,
198 .id = 0,
199};
200
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200201
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200202/*****************************************************/
203/* functions below are for dynamic buffer management */
204/*****************************************************/
205
206/* re-enables receiving on mux <target> after a buffer was allocated. It returns
207 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
208 * if it's impossible to wake up and we prefer to be woken up later.
209 */
210static int h2_dbuf_available(void *target)
211{
212 struct h2c *h2c = target;
213
214 /* take the buffer now as we'll get scheduled waiting for ->wake() */
215 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200216 h2c->flags &= ~H2_CF_DEM_DALLOC;
217 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
218 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200219 return 1;
220 }
221 return 0;
222}
223
224static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
225{
226 struct buffer *buf = NULL;
227
228 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
229 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
230 h2c->dbuf_wait.target = h2c->conn;
231 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
232 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
233 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
234 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
235 __conn_xprt_stop_recv(h2c->conn);
236 }
237 return buf;
238}
239
240static inline void h2_release_dbuf(struct h2c *h2c)
241{
242 if (h2c->dbuf->size) {
243 b_free(&h2c->dbuf);
244 offer_buffers(h2c->dbuf_wait.target,
245 tasks_run_queue + applets_active_queue);
246 }
247}
248
Willy Tarreau14398122017-09-22 14:26:04 +0200249/* re-enables sending on mux <target> after a buffer was allocated. It returns
250 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
251 * if it's impossible to wake up and we prefer to be woken up later.
252 */
253static int h2_mbuf_available(void *target)
254{
255 struct h2c *h2c = target;
256
257 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
258 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200259 if (h2c->flags & H2_CF_MUX_MALLOC) {
260 h2c->flags &= ~H2_CF_MUX_MALLOC;
261 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
262 conn_xprt_want_send(h2c->conn);
263 }
264
265 if (h2c->flags & H2_CF_DEM_MROOM) {
266 h2c->flags &= ~H2_CF_DEM_MROOM;
267 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
268 conn_xprt_want_recv(h2c->conn);
269 }
270
Willy Tarreau14398122017-09-22 14:26:04 +0200271 /* FIXME: we should in fact call something like h2_update_poll()
272 * now to recompte the polling. For now it will be enough like
273 * this.
274 */
Willy Tarreau14398122017-09-22 14:26:04 +0200275 return 1;
276 }
277 return 0;
278}
279
280static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
281{
282 struct buffer *buf = NULL;
283
284 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
285 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
286 h2c->mbuf_wait.target = h2c;
287 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
288 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
289 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
290 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
291
292 /* FIXME: we should in fact only block the direction being
293 * currently used. For now it will be enough like this.
294 */
295 __conn_xprt_stop_send(h2c->conn);
296 __conn_xprt_stop_recv(h2c->conn);
297 }
298 return buf;
299}
300
301static inline void h2_release_mbuf(struct h2c *h2c)
302{
303 if (h2c->mbuf->size) {
304 b_free(&h2c->mbuf);
305 offer_buffers(h2c->mbuf_wait.target,
306 tasks_run_queue + applets_active_queue);
307 }
308}
309
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200310
Willy Tarreau62f52692017-10-08 23:01:42 +0200311/*****************************************************************/
312/* functions below are dedicated to the mux setup and management */
313/*****************************************************************/
314
Willy Tarreau32218eb2017-09-22 08:07:25 +0200315/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
316static int h2c_frt_init(struct connection *conn)
317{
318 struct h2c *h2c;
319
320 h2c = pool_alloc2(pool2_h2c);
321 if (!h2c)
322 goto fail;
323
324 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
325 if (!h2c->ddht)
326 goto fail;
327
328 /* Initialise the context. */
329 h2c->st0 = H2_CS_PREFACE;
330 h2c->conn = conn;
331 h2c->max_id = -1;
332 h2c->errcode = H2_ERR_NO_ERROR;
333 h2c->flags = H2_CF_NONE;
334 h2c->rcvd_c = 0;
335 h2c->rcvd_s = 0;
336
337 h2c->dbuf = &buf_empty;
338 h2c->dsi = -1;
339 h2c->msi = -1;
340 h2c->last_sid = -1;
341
342 h2c->mbuf = &buf_empty;
343 h2c->miw = 65535; /* mux initial window size */
344 h2c->mws = 65535; /* mux window size */
345 h2c->mfs = 16384; /* initial max frame size */
346 h2c->streams_by_id = EB_ROOT_UNIQUE;
347 LIST_INIT(&h2c->send_list);
348 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200349 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200350 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200351 conn->mux_ctx = h2c;
352
353 conn_xprt_want_recv(conn);
354 /* mux->wake will be called soon to complete the operation */
355 return 0;
356 fail:
357 pool_free2(pool2_h2c, h2c);
358 return -1;
359}
360
Willy Tarreau62f52692017-10-08 23:01:42 +0200361/* Initialize the mux once it's attached. For outgoing connections, the context
362 * is already initialized before installing the mux, so we detect incoming
363 * connections from the fact that the context is still NULL. Returns < 0 on
364 * error.
365 */
366static int h2_init(struct connection *conn)
367{
368 if (conn->mux_ctx) {
369 /* we don't support outgoing connections for now */
370 return -1;
371 }
372
Willy Tarreau32218eb2017-09-22 08:07:25 +0200373 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200374}
375
Willy Tarreau2373acc2017-10-12 17:35:14 +0200376/* returns the stream associated with id <id> or NULL if not found */
377static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
378{
379 struct eb32_node *node;
380
Willy Tarreau2a856182017-05-16 15:20:39 +0200381 if (id > h2c->max_id)
382 return (struct h2s *)h2_idle_stream;
383
Willy Tarreau2373acc2017-10-12 17:35:14 +0200384 node = eb32_lookup(&h2c->streams_by_id, id);
385 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200386 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200387
388 return container_of(node, struct h2s, by_id);
389}
390
Willy Tarreau62f52692017-10-08 23:01:42 +0200391/* release function for a connection. This one should be called to free all
392 * resources allocated to the mux.
393 */
394static void h2_release(struct connection *conn)
395{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200396 struct h2c *h2c = conn->mux_ctx;
397
398 LIST_DEL(&conn->list);
399
400 if (h2c) {
401 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200402 h2_release_dbuf(h2c);
403 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
404 LIST_DEL(&h2c->dbuf_wait.list);
405 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200406
407 h2_release_mbuf(h2c);
408 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
409 LIST_DEL(&h2c->mbuf_wait.list);
410 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
411
Willy Tarreau32218eb2017-09-22 08:07:25 +0200412 pool_free2(pool2_h2c, h2c);
413 }
414
415 conn->mux = NULL;
416 conn->mux_ctx = NULL;
417
418 conn_stop_tracking(conn);
419 conn_full_close(conn);
420 if (conn->destroy_cb)
421 conn->destroy_cb(conn);
422 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200423}
424
425
Willy Tarreau71681172017-10-23 14:39:06 +0200426/******************************************************/
427/* functions below are for the H2 protocol processing */
428/******************************************************/
429
430/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
431static inline int h2s_id(const struct h2s *h2s)
432{
433 return h2s ? h2s->id : 0;
434}
435
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200436/* returns true of the mux is currently busy as seen from stream <h2s> */
437static inline int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
438{
439 if (h2c->msi < 0)
440 return 0;
441
442 if (h2c->msi == h2s_id(h2s))
443 return 0;
444
445 return 1;
446}
447
Willy Tarreau741d6df2017-10-17 08:00:59 +0200448/* marks an error on the connection */
449static inline void h2c_error(struct h2c *h2c, enum h2_err err)
450{
451 h2c->errcode = err;
452 h2c->st0 = H2_CS_ERROR;
453}
454
Willy Tarreau2e43f082017-10-17 08:03:59 +0200455/* marks an error on the stream */
456static inline void h2s_error(struct h2s *h2s, enum h2_err err)
457{
458 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
459 h2s->errcode = err;
460 h2s->st = H2_SS_ERROR;
461 if (h2s->cs)
462 h2s->cs->flags |= CS_FL_ERROR;
463 }
464}
465
Willy Tarreaue4820742017-07-27 13:37:23 +0200466/* writes the 24-bit frame size <len> at address <frame> */
467static inline void h2_set_frame_size(void *frame, uint32_t len)
468{
469 uint8_t *out = frame;
470
471 *out = len >> 16;
472 write_n16(out + 1, len);
473}
474
Willy Tarreau54c15062017-10-10 17:10:03 +0200475/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
476 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
477 * the caller's responsibility to verify that there are at least <bytes> bytes
478 * available in the buffer's input prior to calling this function.
479 */
480static inline void h2_get_buf_bytes(void *dst, size_t bytes,
481 const struct buffer *b, int o)
482{
483 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
484}
485
486static inline uint16_t h2_get_n16(const struct buffer *b, int o)
487{
488 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
489}
490
491static inline uint32_t h2_get_n32(const struct buffer *b, int o)
492{
493 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
494}
495
496static inline uint64_t h2_get_n64(const struct buffer *b, int o)
497{
498 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
499}
500
501
Willy Tarreau715d5312017-07-11 15:20:24 +0200502/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
503 * is not obvious. It turns out that H2 headers are neither aligned nor do they
504 * use regular sizes. And to add to the trouble, the buffer may wrap so each
505 * byte read must be checked. The header is formed like this :
506 *
507 * b0 b1 b2 b3 b4 b5..b8
508 * +----------+---------+--------+----+----+----------------------+
509 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
510 * +----------+---------+--------+----+----+----------------------+
511 *
512 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
513 * we get the sid properly aligned and ordered, and 16 bits of len properly
514 * ordered as well. The type and flags can be extracted using bit shifts from
515 * the word, and only one extra read is needed to fetch len[16:23].
516 * Returns zero if some bytes are missing, otherwise non-zero on success.
517 */
518static int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
519{
520 uint64_t w;
521
522 if (b->i < 9)
523 return 0;
524
525 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
526 h->len = *b->p << 16;
527 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
528 h->ff = w >> 32;
529 h->ft = w >> 40;
530 h->len += w >> 48;
531 return 1;
532}
533
534/* skip the next 9 bytes corresponding to the frame header possibly parsed by
535 * h2_peek_frame_hdr() above.
536 */
537static inline void h2_skip_frame_hdr(struct buffer *b)
538{
539 bi_del(b, 9);
540}
541
542/* same as above, automatically advances the buffer on success */
543static inline int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
544{
545 int ret;
546
547 ret = h2_peek_frame_hdr(b, h);
548 if (ret > 0)
549 h2_skip_frame_hdr(b);
550 return ret;
551}
552
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200553/* creates a new stream <id> on the h2c connection and returns it, or NULL in
554 * case of memory allocation error.
555 */
556static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
557{
558 struct conn_stream *cs;
559 struct h2s *h2s;
560
561 h2s = pool_alloc2(pool2_h2s);
562 if (!h2s)
563 goto out;
564
565 h2s->h2c = h2c;
566 h2s->mws = h2c->miw;
567 h2s->flags = H2_SF_NONE;
568 h2s->errcode = H2_ERR_NO_ERROR;
569 h2s->st = H2_SS_IDLE;
570 h1m_init(&h2s->req);
571 h1m_init(&h2s->res);
572 h2s->by_id.key = h2s->id = id;
573 h2c->max_id = id;
574 LIST_INIT(&h2s->list);
575
576 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
577
578 cs = cs_new(h2c->conn);
579 if (!cs)
580 goto out_close;
581
582 h2s->cs = cs;
583 cs->ctx = h2s;
584
585 if (stream_create_from_cs(cs) < 0)
586 goto out_free_cs;
587
588 /* OK done, the stream lives its own life now */
589 return h2s;
590
591 out_free_cs:
592 cs_free(cs);
593 out_close:
594 eb32_delete(&h2s->by_id);
595 pool_free2(pool2_h2s, h2s);
596 h2s = NULL;
597 out:
598 return h2s;
599}
600
Willy Tarreaube5b7152017-09-25 16:25:39 +0200601/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
602 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
603 * the various settings codes.
604 */
605static int h2c_snd_settings(struct h2c *h2c)
606{
607 struct buffer *res;
608 char buf_data[100]; // enough for 15 settings
609 struct chunk buf;
610 int ret;
611
612 if (h2c_mux_busy(h2c, NULL)) {
613 h2c->flags |= H2_CF_DEM_MBUSY;
614 return 0;
615 }
616
617 res = h2_get_mbuf(h2c);
618 if (!res) {
619 h2c->flags |= H2_CF_MUX_MALLOC;
620 h2c->flags |= H2_CF_DEM_MROOM;
621 return 0;
622 }
623
624 chunk_init(&buf, buf_data, sizeof(buf_data));
625 chunk_memcpy(&buf,
626 "\x00\x00\x00" /* length : 0 for now */
627 "\x04\x00" /* type : 4 (settings), flags : 0 */
628 "\x00\x00\x00\x00", /* stream ID : 0 */
629 9);
630
631 if (h2_settings_header_table_size != 4096) {
632 char str[6] = "\x00\x01"; /* header_table_size */
633
634 write_n32(str + 2, h2_settings_header_table_size);
635 chunk_memcat(&buf, str, 6);
636 }
637
638 if (h2_settings_initial_window_size != 65535) {
639 char str[6] = "\x00\x04"; /* initial_window_size */
640
641 write_n32(str + 2, h2_settings_initial_window_size);
642 chunk_memcat(&buf, str, 6);
643 }
644
645 if (h2_settings_max_concurrent_streams != 0) {
646 char str[6] = "\x00\x03"; /* max_concurrent_streams */
647
648 /* Note: 0 means "unlimited" for haproxy's config but not for
649 * the protocol, so never send this value!
650 */
651 write_n32(str + 2, h2_settings_max_concurrent_streams);
652 chunk_memcat(&buf, str, 6);
653 }
654
655 if (global.tune.bufsize != 16384) {
656 char str[6] = "\x00\x05"; /* max_frame_size */
657
658 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
659 * match bufsize - rewrite size, but at the moment it seems
660 * that clients don't take care of it.
661 */
662 write_n32(str + 2, global.tune.bufsize);
663 chunk_memcat(&buf, str, 6);
664 }
665
666 h2_set_frame_size(buf.str, buf.len - 9);
667 ret = bo_istput(res, ist2(buf.str, buf.len));
668 if (unlikely(ret <= 0)) {
669 if (!ret) {
670 h2c->flags |= H2_CF_MUX_MFULL;
671 h2c->flags |= H2_CF_DEM_MROOM;
672 return 0;
673 }
674 else {
675 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
676 return 0;
677 }
678 }
679 return ret;
680}
681
Willy Tarreau52eed752017-09-22 15:05:09 +0200682/* Try to receive a connection preface, then upon success try to send our
683 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
684 * missing data. It may return an error in h2c.
685 */
686static int h2c_frt_recv_preface(struct h2c *h2c)
687{
688 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200689 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200690
691 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
692
693 if (unlikely(ret1 <= 0)) {
694 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
695 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
696 return 0;
697 }
698
Willy Tarreaube5b7152017-09-25 16:25:39 +0200699 ret2 = h2c_snd_settings(h2c);
700 if (ret2 > 0)
701 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200702
Willy Tarreaube5b7152017-09-25 16:25:39 +0200703 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200704}
705
Willy Tarreau081d4722017-05-16 21:51:05 +0200706/* try to send a GOAWAY frame on the connection to report an error or a graceful
707 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
708 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
709 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
710 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
711 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
712 * on unrecoverable failure. It will not attempt to send one again in this last
713 * case so that it is safe to use h2c_error() to report such errors.
714 */
715static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
716{
717 struct buffer *res;
718 char str[17];
719 int ret;
720
721 if (h2c->flags & H2_CF_GOAWAY_FAILED)
722 return 1; // claim that it worked
723
724 if (h2c_mux_busy(h2c, h2s)) {
725 if (h2s)
726 h2s->flags |= H2_SF_BLK_MBUSY;
727 else
728 h2c->flags |= H2_CF_DEM_MBUSY;
729 return 0;
730 }
731
732 res = h2_get_mbuf(h2c);
733 if (!res) {
734 h2c->flags |= H2_CF_MUX_MALLOC;
735 if (h2s)
736 h2s->flags |= H2_SF_BLK_MROOM;
737 else
738 h2c->flags |= H2_CF_DEM_MROOM;
739 return 0;
740 }
741
742 /* len: 8, type: 7, flags: none, sid: 0 */
743 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
744
745 if (h2c->last_sid < 0)
746 h2c->last_sid = h2c->max_id;
747
748 write_n32(str + 9, h2c->last_sid);
749 write_n32(str + 13, h2c->errcode);
750 ret = bo_istput(res, ist2(str, 17));
751 if (unlikely(ret <= 0)) {
752 if (!ret) {
753 h2c->flags |= H2_CF_MUX_MFULL;
754 if (h2s)
755 h2s->flags |= H2_SF_BLK_MROOM;
756 else
757 h2c->flags |= H2_CF_DEM_MROOM;
758 return 0;
759 }
760 else {
761 /* we cannot report this error using GOAWAY, so we mark
762 * it and claim a success.
763 */
764 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
765 h2c->flags |= H2_CF_GOAWAY_FAILED;
766 return 1;
767 }
768 }
769 h2c->flags |= H2_CF_GOAWAY_SENT;
770 return ret;
771}
772
Willy Tarreau27a84c92017-10-17 08:10:17 +0200773/* try to send an RST_STREAM frame on the connection for the current demuxed
774 * stream to report an error, with h2s->errcode as the error code. Returns > 0
775 * on success or zero if nothing was done. It uses h2c->dsi as the stream ID
776 * and h2s->errcode for the error code. In case of lack of room to write the
777 * message, it subscribes the requester (either <h2s> or <h2c>) to future
778 * notifications. It's worth mentionning that an RST may even be sent for a
779 * closed stream with error 0 in this case.
780 */
781static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
782{
783 struct buffer *res;
784 char str[13];
785 int ret;
786
787 if (h2c_mux_busy(h2c, h2s)) {
788 if (h2s)
789 h2s->flags |= H2_SF_BLK_MBUSY;
790 else
791 h2c->flags |= H2_CF_DEM_MBUSY;
792 return 0;
793 }
794
795 res = h2_get_mbuf(h2c);
796 if (!res) {
797 h2c->flags |= H2_CF_MUX_MALLOC;
798 if (h2s)
799 h2s->flags |= H2_SF_BLK_MROOM;
800 else
801 h2c->flags |= H2_CF_DEM_MROOM;
802 return 0;
803 }
804
805 /* len: 4, type: 3, flags: none */
806 memcpy(str, "\x00\x00\x04\x03\x00", 5);
807 write_n32(str + 5, h2c->dsi);
808 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_RESET) ?
809 h2s->errcode : H2_ERR_STREAM_CLOSED);
810 ret = bo_istput(res, ist2(str, 13));
811 if (unlikely(ret <= 0)) {
812 if (!ret) {
813 h2c->flags |= H2_CF_MUX_MFULL;
814 if (h2s)
815 h2s->flags |= H2_SF_BLK_MROOM;
816 else
817 h2c->flags |= H2_CF_DEM_MROOM;
818 return 0;
819 }
820 else {
821 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
822 return 0;
823 }
824 }
825
826 if (h2s)
827 h2s->flags |= H2_SF_RST_SENT;
828 return ret;
829}
830
Willy Tarreauc7576ea2017-10-29 22:00:09 +0100831/* try to send an empty DATA frame with the ES flag set to notify about the
832 * end of stream and match a shutdown(write). If an ES was already sent as
833 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
834 * on success or zero if nothing was done. In case of lack of room to write the
835 * message, it subscribes the requesting stream to future notifications.
836 */
837static int h2_send_empty_data_es(struct h2s *h2s)
838{
839 struct h2c *h2c = h2s->h2c;
840 struct buffer *res;
841 char str[9];
842 int ret;
843
844 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR ||
845 h2s->st == H2_SS_RESET || h2s->st == H2_SS_CLOSED)
846 return 1;
847
848 if (h2c_mux_busy(h2c, h2s)) {
849 h2s->flags |= H2_SF_BLK_MBUSY;
850 return 0;
851 }
852
853 res = h2_get_mbuf(h2c);
854 if (!res) {
855 h2c->flags |= H2_CF_MUX_MALLOC;
856 h2s->flags |= H2_SF_BLK_MROOM;
857 return 0;
858 }
859
860 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
861 memcpy(str, "\x00\x00\x00\x00\x01", 5);
862 write_n32(str + 5, h2s->id);
863 ret = bo_istput(res, ist2(str, 9));
864 if (unlikely(ret <= 0)) {
865 if (!ret) {
866 h2c->flags |= H2_CF_MUX_MFULL;
867 h2s->flags |= H2_SF_BLK_MROOM;
868 return 0;
869 }
870 else {
871 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
872 return 0;
873 }
874 }
875 return ret;
876}
877
Willy Tarreau23b92aa2017-10-30 00:26:54 +0100878/* wake the streams attached to the connection, whose id is greater than <last>,
879 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
880 * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The
881 * stream's state is automatically updated accordingly.
882 */
883static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
884{
885 struct eb32_node *node;
886 struct h2s *h2s;
887
888 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
889 flags |= CS_FL_ERROR;
890
891 if (conn_xprt_read0_pending(h2c->conn))
892 flags |= CS_FL_EOS;
893
894 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
895 while (node) {
896 h2s = container_of(node, struct h2s, by_id);
897 if (h2s->id <= last)
898 break;
899 node = eb32_next(node);
900 if (h2s->cs) {
901 h2s->cs->flags |= flags;
902 /* recv is used to force to detect CS_FL_EOS that wake()
903 * doesn't handle in the stream int code.
904 */
905 h2s->cs->data_cb->recv(h2s->cs);
906 h2s->cs->data_cb->wake(h2s->cs);
907 }
908 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
909 h2s->st = H2_SS_ERROR;
910 else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN)
911 h2s->st = H2_SS_HREM;
912 else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC)
913 h2s->st = H2_SS_CLOSED;
914 }
915}
916
Willy Tarreau3421aba2017-07-27 15:41:03 +0200917/* Increase all streams' outgoing window size by the difference passed in
918 * argument. This is needed upon receipt of the settings frame if the initial
919 * window size is different. The difference may be negative and the resulting
920 * window size as well, for the time it takes to receive some window updates.
921 */
922static void h2c_update_all_ws(struct h2c *h2c, int diff)
923{
924 struct h2s *h2s;
925 struct eb32_node *node;
926
927 if (!diff)
928 return;
929
930 node = eb32_first(&h2c->streams_by_id);
931 while (node) {
932 h2s = container_of(node, struct h2s, by_id);
933 h2s->mws += diff;
934 node = eb32_next(node);
935 }
936}
937
938/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
939 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
940 * return an error in h2c. Described in RFC7540#6.5.
941 */
942static int h2c_handle_settings(struct h2c *h2c)
943{
944 unsigned int offset;
945 int error;
946
947 if (h2c->dff & H2_F_SETTINGS_ACK) {
948 if (h2c->dfl) {
949 error = H2_ERR_FRAME_SIZE_ERROR;
950 goto fail;
951 }
952 return 1;
953 }
954
955 if (h2c->dsi != 0) {
956 error = H2_ERR_PROTOCOL_ERROR;
957 goto fail;
958 }
959
960 if (h2c->dfl % 6) {
961 error = H2_ERR_FRAME_SIZE_ERROR;
962 goto fail;
963 }
964
965 /* that's the limit we can process */
966 if (h2c->dfl > global.tune.bufsize) {
967 error = H2_ERR_FRAME_SIZE_ERROR;
968 goto fail;
969 }
970
971 /* process full frame only */
972 if (h2c->dbuf->i < h2c->dfl)
973 return 0;
974
975 /* parse the frame */
976 for (offset = 0; offset < h2c->dfl; offset += 6) {
977 uint16_t type = h2_get_n16(h2c->dbuf, offset);
978 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
979
980 switch (type) {
981 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
982 /* we need to update all existing streams with the
983 * difference from the previous iws.
984 */
985 if (arg < 0) { // RFC7540#6.5.2
986 error = H2_ERR_FLOW_CONTROL_ERROR;
987 goto fail;
988 }
989 h2c_update_all_ws(h2c, arg - h2c->miw);
990 h2c->miw = arg;
991 break;
992 case H2_SETTINGS_MAX_FRAME_SIZE:
993 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
994 error = H2_ERR_PROTOCOL_ERROR;
995 goto fail;
996 }
997 h2c->mfs = arg;
998 break;
999 }
1000 }
1001
1002 /* need to ACK this frame now */
1003 h2c->st0 = H2_CS_FRAME_A;
1004 return 1;
1005 fail:
1006 h2c_error(h2c, error);
1007 return 0;
1008}
1009
1010/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1011 * success or one of the h2_status values.
1012 */
1013static int h2c_ack_settings(struct h2c *h2c)
1014{
1015 struct buffer *res;
1016 char str[9];
1017 int ret = -1;
1018
1019 if (h2c_mux_busy(h2c, NULL)) {
1020 h2c->flags |= H2_CF_DEM_MBUSY;
1021 return 0;
1022 }
1023
1024 res = h2_get_mbuf(h2c);
1025 if (!res) {
1026 h2c->flags |= H2_CF_MUX_MALLOC;
1027 h2c->flags |= H2_CF_DEM_MROOM;
1028 return 0;
1029 }
1030
1031 memcpy(str,
1032 "\x00\x00\x00" /* length : 0 (no data) */
1033 "\x04" "\x01" /* type : 4, flags : ACK */
1034 "\x00\x00\x00\x00" /* stream ID */, 9);
1035
1036 ret = bo_istput(res, ist2(str, 9));
1037 if (unlikely(ret <= 0)) {
1038 if (!ret) {
1039 h2c->flags |= H2_CF_MUX_MFULL;
1040 h2c->flags |= H2_CF_DEM_MROOM;
1041 return 0;
1042 }
1043 else {
1044 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1045 return 0;
1046 }
1047 }
1048 return ret;
1049}
1050
Willy Tarreaucf68c782017-10-10 17:11:41 +02001051/* processes a PING frame and schedules an ACK if needed. The caller must pass
1052 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1053 * missing data. It may return an error in h2c.
1054 */
1055static int h2c_handle_ping(struct h2c *h2c)
1056{
1057 /* frame length must be exactly 8 */
1058 if (h2c->dfl != 8) {
1059 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1060 return 0;
1061 }
1062
1063 /* schedule a response */
1064 if (!(h2c->dft & H2_F_PING_ACK))
1065 h2c->st0 = H2_CS_FRAME_A;
1066 return 1;
1067}
1068
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001069/* Try to send a window update for stream id <sid> and value <increment>.
1070 * Returns > 0 on success or zero on missing room or failure. It may return an
1071 * error in h2c.
1072 */
1073static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1074{
1075 struct buffer *res;
1076 char str[13];
1077 int ret = -1;
1078
1079 if (h2c_mux_busy(h2c, NULL)) {
1080 h2c->flags |= H2_CF_DEM_MBUSY;
1081 return 0;
1082 }
1083
1084 res = h2_get_mbuf(h2c);
1085 if (!res) {
1086 h2c->flags |= H2_CF_MUX_MALLOC;
1087 h2c->flags |= H2_CF_DEM_MROOM;
1088 return 0;
1089 }
1090
1091 /* length: 4, type: 8, flags: none */
1092 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1093 write_n32(str + 5, sid);
1094 write_n32(str + 9, increment);
1095
1096 ret = bo_istput(res, ist2(str, 13));
1097
1098 if (unlikely(ret <= 0)) {
1099 if (!ret) {
1100 h2c->flags |= H2_CF_MUX_MFULL;
1101 h2c->flags |= H2_CF_DEM_MROOM;
1102 return 0;
1103 }
1104 else {
1105 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1106 return 0;
1107 }
1108 }
1109 return ret;
1110}
1111
1112/* try to send pending window update for the connection. It's safe to call it
1113 * with no pending updates. Returns > 0 on success or zero on missing room or
1114 * failure. It may return an error in h2c.
1115 */
1116static int h2c_send_conn_wu(struct h2c *h2c)
1117{
1118 int ret = 1;
1119
1120 if (h2c->rcvd_c <= 0)
1121 return 1;
1122
1123 /* send WU for the connection */
1124 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1125 if (ret > 0)
1126 h2c->rcvd_c = 0;
1127
1128 return ret;
1129}
1130
1131/* try to send pending window update for the current dmux stream. It's safe to
1132 * call it with no pending updates. Returns > 0 on success or zero on missing
1133 * room or failure. It may return an error in h2c.
1134 */
1135static int h2c_send_strm_wu(struct h2c *h2c)
1136{
1137 int ret = 1;
1138
1139 if (h2c->rcvd_s <= 0)
1140 return 1;
1141
1142 /* send WU for the stream */
1143 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1144 if (ret > 0)
1145 h2c->rcvd_s = 0;
1146
1147 return ret;
1148}
1149
Willy Tarreaucf68c782017-10-10 17:11:41 +02001150/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1151 * success, 0 on missing data or one of the h2_status values.
1152 */
1153static int h2c_ack_ping(struct h2c *h2c)
1154{
1155 struct buffer *res;
1156 char str[17];
1157 int ret = -1;
1158
1159 if (h2c->dbuf->i < 8)
1160 return 0;
1161
1162 if (h2c_mux_busy(h2c, NULL)) {
1163 h2c->flags |= H2_CF_DEM_MBUSY;
1164 return 0;
1165 }
1166
1167 res = h2_get_mbuf(h2c);
1168 if (!res) {
1169 h2c->flags |= H2_CF_MUX_MALLOC;
1170 h2c->flags |= H2_CF_DEM_MROOM;
1171 return 0;
1172 }
1173
1174 memcpy(str,
1175 "\x00\x00\x08" /* length : 8 (same payload) */
1176 "\x06" "\x01" /* type : 6, flags : ACK */
1177 "\x00\x00\x00\x00" /* stream ID */, 9);
1178
1179 /* copy the original payload */
1180 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1181
1182 ret = bo_istput(res, ist2(str, 17));
1183 if (unlikely(ret <= 0)) {
1184 if (!ret) {
1185 h2c->flags |= H2_CF_MUX_MFULL;
1186 h2c->flags |= H2_CF_DEM_MROOM;
1187 return 0;
1188 }
1189 else {
1190 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1191 return 0;
1192 }
1193 }
1194 return ret;
1195}
1196
Willy Tarreau26f95952017-07-27 17:18:30 +02001197/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1198 * Returns > 0 on success or zero on missing data. It may return an error in
1199 * h2c or h2s. Described in RFC7540#6.9.
1200 */
1201static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1202{
1203 int32_t inc;
1204 int error;
1205
1206 if (h2c->dfl != 4) {
1207 error = H2_ERR_FRAME_SIZE_ERROR;
1208 goto conn_err;
1209 }
1210
1211 /* process full frame only */
1212 if (h2c->dbuf->i < h2c->dfl)
1213 return 0;
1214
1215 inc = h2_get_n32(h2c->dbuf, 0);
1216
1217 if (h2c->dsi != 0) {
1218 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001219
1220 /* it's not an error to receive WU on a closed stream */
1221 if (h2s->st == H2_SS_CLOSED)
1222 return 1;
1223
1224 if (!inc) {
1225 error = H2_ERR_PROTOCOL_ERROR;
1226 goto strm_err;
1227 }
1228
1229 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1230 error = H2_ERR_FLOW_CONTROL_ERROR;
1231 goto strm_err;
1232 }
1233
1234 h2s->mws += inc;
1235 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1236 h2s->flags &= ~H2_SF_BLK_SFCTL;
1237 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1238 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1239 /* This stream wanted to send but could not due to its
1240 * own flow control. We can put it back into the send
1241 * list now, it will be handled upon next send() call.
1242 */
1243 LIST_ADDQ(&h2c->send_list, &h2s->list);
1244 }
1245 }
1246 }
1247 else {
1248 /* connection window update */
1249 if (!inc) {
1250 error = H2_ERR_PROTOCOL_ERROR;
1251 goto conn_err;
1252 }
1253
1254 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1255 error = H2_ERR_FLOW_CONTROL_ERROR;
1256 goto conn_err;
1257 }
1258
1259 h2c->mws += inc;
1260 }
1261
1262 return 1;
1263
1264 conn_err:
1265 h2c_error(h2c, error);
1266 return 0;
1267
1268 strm_err:
1269 if (h2s) {
1270 h2s_error(h2s, error);
1271 h2c->st0 = H2_CS_FRAME_A;
1272 }
1273 else
1274 h2c_error(h2c, error);
1275 return 0;
1276}
1277
Willy Tarreaue96b0922017-10-30 00:28:29 +01001278/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1279 * the last ID. Returns > 0 on success or zero on missing data. It may return
1280 * an error in h2c. Described in RFC7540#6.8.
1281 */
1282static int h2c_handle_goaway(struct h2c *h2c)
1283{
1284 int error;
1285 int last;
1286
1287 if (h2c->dsi != 0) {
1288 error = H2_ERR_PROTOCOL_ERROR;
1289 goto conn_err;
1290 }
1291
1292 if (h2c->dfl < 8) {
1293 error = H2_ERR_FRAME_SIZE_ERROR;
1294 goto conn_err;
1295 }
1296
1297 /* process full frame only */
1298 if (h2c->dbuf->i < h2c->dfl)
1299 return 0;
1300
1301 last = h2_get_n32(h2c->dbuf, 0);
1302 h2c->errcode = h2_get_n32(h2c->dbuf, 4);
1303 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
1304 return 1;
1305
1306 conn_err:
1307 h2c_error(h2c, error);
1308 return 0;
1309}
1310
Willy Tarreaucd234e92017-08-18 10:59:39 +02001311/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1312 * Returns > 0 on success or zero on missing data. It may return an error in
1313 * h2c. Described in RFC7540#6.4.
1314 */
1315static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1316{
1317 int error;
1318
1319 if (h2c->dsi == 0) {
1320 error = H2_ERR_PROTOCOL_ERROR;
1321 goto conn_err;
1322 }
1323
Willy Tarreaucd234e92017-08-18 10:59:39 +02001324 if (h2c->dfl != 4) {
1325 error = H2_ERR_FRAME_SIZE_ERROR;
1326 goto conn_err;
1327 }
1328
1329 /* process full frame only */
1330 if (h2c->dbuf->i < h2c->dfl)
1331 return 0;
1332
1333 /* late RST, already handled */
1334 if (h2s->st == H2_SS_CLOSED)
1335 return 1;
1336
1337 h2s->errcode = h2_get_n32(h2c->dbuf, 0);
1338 h2s->st = H2_SS_CLOSED;
1339
1340 if (h2s->cs) {
1341 h2s->cs->flags |= CS_FL_EOS;
1342 /* recv is used to force to detect CS_FL_EOS that wake()
1343 * doesn't handle in the stream-int code.
1344 */
1345 h2s->cs->data_cb->recv(h2s->cs);
1346 h2s->cs->data_cb->wake(h2s->cs);
1347 }
1348
1349 h2s->flags |= H2_SF_RST_RCVD;
1350 return 1;
1351
1352 conn_err:
1353 h2c_error(h2c, error);
1354 return 0;
1355}
1356
Willy Tarreau13278b42017-10-13 19:23:14 +02001357/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1358 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1359 * errors here are reported as connection errors since it's impossible to
1360 * recover from such errors after the compression context has been altered.
1361 */
1362static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1363{
1364 int error;
1365
1366 if (!h2c->dfl) {
1367 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1368 goto strm_err;
1369 }
1370
1371 if (!h2c->dbuf->size)
1372 return 0; // empty buffer
1373
1374 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1375 return 0; // incomplete frame
1376
1377 /* now either the frame is complete or the buffer is complete */
1378 if (h2s->st != H2_SS_IDLE) {
1379 /* FIXME: stream already exists, this is only allowed for
1380 * trailers (not supported for now).
1381 */
1382 error = H2_ERR_PROTOCOL_ERROR;
1383 goto conn_err;
1384 }
1385 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1386 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1387 error = H2_ERR_PROTOCOL_ERROR;
1388 goto conn_err;
1389 }
1390
1391 h2s = h2c_stream_new(h2c, h2c->dsi);
1392 if (!h2s) {
1393 error = H2_ERR_INTERNAL_ERROR;
1394 goto conn_err;
1395 }
1396
1397 h2s->st = H2_SS_OPEN;
1398 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1399 h2s->st = H2_SS_HREM;
1400 h2s->flags |= H2_SF_ES_RCVD;
1401 }
1402
1403 /* call the upper layers to process the frame, then let the upper layer
1404 * notify the stream about any change.
1405 */
1406 h2s->cs->data_cb->recv(h2s->cs);
1407
1408 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1409 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1410 error = H2_ERR_INTERNAL_ERROR;
1411 goto conn_err;
1412 }
1413
1414 if (h2s->st >= H2_SS_RESET) {
1415 /* stream error : send RST_STREAM */
1416 h2c->st0 = H2_CS_FRAME_A;
1417 }
1418 else {
1419 /* update the max stream ID if the request is being processed */
1420 if (h2s->id > h2c->max_id)
1421 h2c->max_id = h2s->id;
1422 }
1423
1424 return 1;
1425
1426 conn_err:
1427 h2c_error(h2c, error);
1428 return 0;
1429
1430 strm_err:
1431 if (h2s) {
1432 h2s_error(h2s, error);
1433 h2c->st0 = H2_CS_FRAME_A;
1434 }
1435 else
1436 h2c_error(h2c, error);
1437 return 0;
1438}
1439
Willy Tarreau454f9052017-10-26 19:40:35 +02001440/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1441 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1442 */
1443static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1444{
1445 int error;
1446
1447 /* note that empty DATA frames are perfectly valid and sometimes used
1448 * to signal an end of stream (with the ES flag).
1449 */
1450
1451 if (!h2c->dbuf->size && h2c->dfl)
1452 return 0; // empty buffer
1453
1454 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1455 return 0; // incomplete frame
1456
1457 /* now either the frame is complete or the buffer is complete */
1458
1459 if (!h2c->dsi) {
1460 /* RFC7540#6.1 */
1461 error = H2_ERR_PROTOCOL_ERROR;
1462 goto conn_err;
1463 }
1464
1465 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1466 /* RFC7540#6.1 */
1467 error = H2_ERR_STREAM_CLOSED;
1468 goto strm_err;
1469 }
1470
1471 /* last frame */
1472 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1473 h2s->st = H2_SS_HREM;
1474 h2s->flags |= H2_SF_ES_RCVD;
1475 }
1476
1477 /* call the upper layers to process the frame, then let the upper layer
1478 * notify the stream about any change.
1479 */
1480 if (!h2s->cs) {
1481 error = H2_ERR_STREAM_CLOSED;
1482 goto strm_err;
1483 }
1484
1485 h2s->cs->data_cb->recv(h2s->cs);
1486 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1487 /* cs has just been destroyed, we have to kill h2s. */
1488 error = H2_ERR_STREAM_CLOSED;
1489 goto strm_err;
1490 }
1491
1492 if (h2s->st >= H2_SS_RESET) {
1493 /* stream error : send RST_STREAM */
1494 h2c->st0 = H2_CS_FRAME_A;
1495 }
1496
1497 /* check for completion : the callee will change this to FRAME_A or
1498 * FRAME_H once done.
1499 */
1500 if (h2c->st0 == H2_CS_FRAME_P)
1501 return 0;
1502
1503 return 1;
1504
1505 conn_err:
1506 h2c_error(h2c, error);
1507 return 0;
1508
1509 strm_err:
1510 if (h2s) {
1511 h2s_error(h2s, error);
1512 h2c->st0 = H2_CS_FRAME_A;
1513 }
1514 else
1515 h2c_error(h2c, error);
1516 return 0;
1517}
1518
Willy Tarreaubc933932017-10-09 16:21:43 +02001519/* process Rx frames to be demultiplexed */
1520static void h2_process_demux(struct h2c *h2c)
1521{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001522 struct h2s *h2s;
1523
Willy Tarreau081d4722017-05-16 21:51:05 +02001524 if (h2c->st0 >= H2_CS_ERROR)
1525 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001526
1527 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1528 if (h2c->st0 == H2_CS_PREFACE) {
1529 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1530 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1531 if (h2c->st0 == H2_CS_ERROR)
1532 h2c->st0 = H2_CS_ERROR2;
1533 goto fail;
1534 }
1535
1536 h2c->max_id = 0;
1537 h2c->st0 = H2_CS_SETTINGS1;
1538 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001539
1540 if (h2c->st0 == H2_CS_SETTINGS1) {
1541 struct h2_fh hdr;
1542
1543 /* ensure that what is pending is a valid SETTINGS frame
1544 * without an ACK.
1545 */
1546 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1547 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1548 if (h2c->st0 == H2_CS_ERROR)
1549 h2c->st0 = H2_CS_ERROR2;
1550 goto fail;
1551 }
1552
1553 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1554 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1555 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1556 h2c->st0 = H2_CS_ERROR2;
1557 goto fail;
1558 }
1559
1560 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1561 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1562 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1563 h2c->st0 = H2_CS_ERROR2;
1564 goto fail;
1565 }
1566
1567 /* that's OK, switch to FRAME_P to process it */
1568 h2c->dfl = hdr.len;
1569 h2c->dsi = hdr.sid;
1570 h2c->dft = hdr.ft;
1571 h2c->dff = hdr.ff;
1572 h2c->st0 = H2_CS_FRAME_P;
1573 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001574 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001575
1576 /* process as many incoming frames as possible below */
1577 while (h2c->dbuf->i) {
1578 int ret = 0;
1579
1580 if (h2c->st0 >= H2_CS_ERROR)
1581 break;
1582
1583 if (h2c->st0 == H2_CS_FRAME_H) {
1584 struct h2_fh hdr;
1585
1586 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1587 break;
1588
1589 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1590 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1591 h2c->st0 = H2_CS_ERROR;
1592 break;
1593 }
1594
1595 h2c->dfl = hdr.len;
1596 h2c->dsi = hdr.sid;
1597 h2c->dft = hdr.ft;
1598 h2c->dff = hdr.ff;
1599 h2c->st0 = H2_CS_FRAME_P;
1600 h2_skip_frame_hdr(h2c->dbuf);
1601 }
1602
1603 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001604 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001605
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001606 if (h2s->st == H2_SS_IDLE &&
1607 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1608 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1609 * this state MUST be treated as a connection error
1610 */
1611 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1612 h2c->st0 = H2_CS_ERROR;
1613 break;
1614 }
1615
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001616 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1617 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1618 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1619 * this state MUST be treated as a stream error
1620 */
1621 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1622 goto strm_err;
1623 }
1624
Willy Tarreauc0da1962017-10-30 18:38:00 +01001625#if 0
1626 // problem below: it is not possible to completely ignore such
1627 // streams as we need to maintain the compression state as well
1628 // and for this we need to completely process these frames (eg:
1629 // HEADERS frames) as well as counting DATA frames to emit
1630 // proper WINDOW UPDATES and ensure the connection doesn't stall.
1631 // This is a typical case of layer violation where the
1632 // transported contents are critical to the connection's
1633 // validity and must be ignored at the same time :-(
1634
1635 /* graceful shutdown, ignore streams whose ID is higher than
1636 * the one advertised in GOAWAY. RFC7540#6.8.
1637 */
1638 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
1639 ret = MIN(h2c->dbuf->i, h2c->dfl);
1640 bi_del(h2c->dbuf, ret);
1641 h2c->dfl -= ret;
1642 ret = h2c->dfl == 0;
1643 goto strm_err;
1644 }
1645#endif
1646
Willy Tarreau7e98c052017-10-10 15:56:59 +02001647 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001648 case H2_FT_SETTINGS:
1649 if (h2c->st0 == H2_CS_FRAME_P)
1650 ret = h2c_handle_settings(h2c);
1651
1652 if (h2c->st0 == H2_CS_FRAME_A)
1653 ret = h2c_ack_settings(h2c);
1654 break;
1655
Willy Tarreaucf68c782017-10-10 17:11:41 +02001656 case H2_FT_PING:
1657 if (h2c->st0 == H2_CS_FRAME_P)
1658 ret = h2c_handle_ping(h2c);
1659
1660 if (h2c->st0 == H2_CS_FRAME_A)
1661 ret = h2c_ack_ping(h2c);
1662 break;
1663
Willy Tarreau26f95952017-07-27 17:18:30 +02001664 case H2_FT_WINDOW_UPDATE:
1665 if (h2c->st0 == H2_CS_FRAME_P)
1666 ret = h2c_handle_window_update(h2c, h2s);
1667 break;
1668
Willy Tarreau61290ec2017-10-17 08:19:21 +02001669 case H2_FT_CONTINUATION:
1670 /* we currently don't support CONTINUATION frames since
1671 * we have nowhere to store the partial HEADERS frame.
1672 * Let's abort the stream on an INTERNAL_ERROR here.
1673 */
1674 if (h2c->st0 == H2_CS_FRAME_P)
1675 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1676 break;
1677
Willy Tarreau13278b42017-10-13 19:23:14 +02001678 case H2_FT_HEADERS:
1679 if (h2c->st0 == H2_CS_FRAME_P)
1680 ret = h2c_frt_handle_headers(h2c, h2s);
1681 break;
1682
Willy Tarreau454f9052017-10-26 19:40:35 +02001683 case H2_FT_DATA:
1684 if (h2c->st0 == H2_CS_FRAME_P)
1685 ret = h2c_frt_handle_data(h2c, h2s);
1686
1687 if (h2c->st0 == H2_CS_FRAME_A)
1688 ret = h2c_send_strm_wu(h2c);
1689 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001690
1691 case H2_FT_RST_STREAM:
1692 if (h2c->st0 == H2_CS_FRAME_P)
1693 ret = h2c_handle_rst_stream(h2c, h2s);
1694 break;
1695
Willy Tarreaue96b0922017-10-30 00:28:29 +01001696 case H2_FT_GOAWAY:
1697 if (h2c->st0 == H2_CS_FRAME_P)
1698 ret = h2c_handle_goaway(h2c);
1699 break;
1700
Willy Tarreau1c661982017-10-30 13:52:01 +01001701 case H2_FT_PUSH_PROMISE:
1702 /* not permitted here, RFC7540#5.1 */
1703 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1704 h2c->st0 = H2_SS_ERROR;
1705 break;
1706
1707 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02001708 default:
1709 /* drop frames that we ignore. They may be larger than
1710 * the buffer so we drain all of their contents until
1711 * we reach the end.
1712 */
1713 ret = MIN(h2c->dbuf->i, h2c->dfl);
1714 bi_del(h2c->dbuf, ret);
1715 h2c->dfl -= ret;
1716 ret = h2c->dfl == 0;
1717 }
1718
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001719 strm_err:
Willy Tarreau27a84c92017-10-17 08:10:17 +02001720 /* RST are sent similarly to frame acks */
1721 if (h2s->st == H2_SS_ERROR) {
1722 if (h2c->st0 == H2_CS_FRAME_P)
1723 h2c->st0 = H2_CS_FRAME_A;
1724
1725 if (h2c->st0 == H2_CS_FRAME_A)
1726 ret = h2c_send_rst_stream(h2c, h2s);
1727 }
1728
Willy Tarreau7e98c052017-10-10 15:56:59 +02001729 /* error or missing data condition met above ? */
1730 if (ret <= 0)
1731 break;
1732
1733 if (h2c->st0 != H2_CS_FRAME_H) {
1734 bi_del(h2c->dbuf, h2c->dfl);
1735 h2c->st0 = H2_CS_FRAME_H;
1736 }
1737 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001738
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001739 if (h2c->rcvd_c > 0 &&
1740 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
1741 h2c_send_conn_wu(h2c);
1742
Willy Tarreau52eed752017-09-22 15:05:09 +02001743 fail:
1744 /* we can go here on missing data, blocked response or error */
1745 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001746}
1747
1748/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1749 * the end.
1750 */
1751static int h2_process_mux(struct h2c *h2c)
1752{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001753 struct h2s *h2s, *h2s_back;
1754
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001755 /* start by sending possibly pending window updates */
1756 if (h2c->rcvd_c > 0 &&
1757 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
1758 h2c_send_conn_wu(h2c) < 0)
1759 goto fail;
1760
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001761 /* First we always process the flow control list because the streams
1762 * waiting there were already elected for immediate emission but were
1763 * blocked just on this.
1764 */
1765
1766 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
1767 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
1768 h2c->st0 >= H2_CS_ERROR)
1769 break;
1770
1771 /* In theory it's possible that h2s->cs == NULL here :
1772 * - client sends crap that causes a parse error
1773 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1774 * - RST_STREAM cannot be emitted because mux is busy/full
1775 * - stream gets notified, detaches and quits
1776 * - mux buffer gets ready and wakes pending streams up
1777 * - bam!
1778 */
1779 h2s->flags &= ~H2_SF_BLK_ANY;
1780
1781 if (h2s->cs) {
1782 h2s->cs->data_cb->send(h2s->cs);
1783 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001784 } else {
1785 h2c_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001786 }
1787
1788 /* depending on callee's blocking reasons, we may queue in send
1789 * list or completely dequeue.
1790 */
1791 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
1792 if (h2s->flags & H2_SF_BLK_ANY) {
1793 LIST_DEL(&h2s->list);
1794 LIST_ADDQ(&h2c->send_list, &h2s->list);
1795 }
1796 else {
1797 LIST_DEL(&h2s->list);
1798 LIST_INIT(&h2s->list);
1799 if (h2s->cs)
1800 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
1801 }
1802 }
1803 }
1804
1805 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
1806 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
1807 break;
1808
1809 /* In theory it's possible that h2s->cs == NULL here :
1810 * - client sends crap that causes a parse error
1811 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1812 * - RST_STREAM cannot be emitted because mux is busy/full
1813 * - stream gets notified, detaches and quits
1814 * - mux buffer gets ready and wakes pending streams up
1815 * - bam!
1816 */
1817 h2s->flags &= ~H2_SF_BLK_ANY;
1818
1819 if (h2s->cs) {
1820 h2s->cs->data_cb->send(h2s->cs);
1821 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001822 } else {
1823 h2c_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001824 }
1825 /* depending on callee's blocking reasons, we may queue in fctl
1826 * list or completely dequeue.
1827 */
1828 if (h2s->flags & H2_SF_BLK_MFCTL) {
1829 /* stream hit the connection's flow control */
1830 LIST_DEL(&h2s->list);
1831 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
1832 }
1833 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
1834 LIST_DEL(&h2s->list);
1835 LIST_INIT(&h2s->list);
1836 if (h2s->cs)
1837 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
1838 }
1839 }
1840
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001841 fail:
Willy Tarreau081d4722017-05-16 21:51:05 +02001842 if (unlikely(h2c->st0 > H2_CS_ERROR)) {
1843 if (h2c->st0 == H2_CS_ERROR) {
1844 if (h2c->max_id >= 0) {
1845 h2c_send_goaway_error(h2c, NULL);
1846 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
1847 return 0;
1848 }
1849
1850 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
1851 }
1852 return 1;
1853 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001854 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02001855}
1856
Willy Tarreau71681172017-10-23 14:39:06 +02001857
Willy Tarreau62f52692017-10-08 23:01:42 +02001858/*********************************************************/
1859/* functions below are I/O callbacks from the connection */
1860/*********************************************************/
1861
1862/* callback called on recv event by the connection handler */
1863static void h2_recv(struct connection *conn)
1864{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001865 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001866 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001867 int max;
1868
1869 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001870 return;
1871
1872 if (h2c->flags & H2_CF_DEM_BLOCK_ANY)
1873 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001874
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001875 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001876 if (!buf) {
1877 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001878 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001879 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001880
Willy Tarreaua2af5122017-10-09 11:56:46 +02001881 /* note: buf->o == 0 */
1882 max = buf->size - buf->i;
1883 if (!max) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001884 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001885 return;
1886 }
1887
1888 conn->xprt->rcv_buf(conn, buf, max);
1889 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001890 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001891
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001892 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001893 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02001894 return;
1895 }
1896
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001897 if (buf->i == buf->size)
1898 h2c->flags |= H2_CF_DEM_DFULL;
1899
Willy Tarreaubc933932017-10-09 16:21:43 +02001900 h2_process_demux(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02001901
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001902 /* after streams have been processed, we should have made some room */
Willy Tarreau081d4722017-05-16 21:51:05 +02001903 if (h2c->st0 >= H2_CS_ERROR)
1904 buf->i = 0;
1905
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001906 if (buf->i != buf->size)
1907 h2c->flags &= ~H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001908 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02001909}
1910
1911/* callback called on send event by the connection handler */
1912static void h2_send(struct connection *conn)
1913{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001914 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02001915 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001916
1917 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001918 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001919
1920 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
1921 /* a handshake was requested */
1922 return;
1923 }
1924
Willy Tarreaubc933932017-10-09 16:21:43 +02001925 /* This loop is quite simple : it tries to fill as much as it can from
1926 * pending streams into the existing buffer until it's reportedly full
1927 * or the end of send requests is reached. Then it tries to send this
1928 * buffer's contents out, marks it not full if at least one byte could
1929 * be sent, and tries again.
1930 *
1931 * The snd_buf() function normally takes a "flags" argument which may
1932 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
1933 * data immediately comes and CO_SFL_STREAMER to indicate that the
1934 * connection is streaming lots of data (used to increase TLS record
1935 * size at the expense of latency). The former can be sent any time
1936 * there's a buffer full flag, as it indicates at least one stream
1937 * attempted to send and failed so there are pending data. An
1938 * alternative would be to set it as long as there's an active stream
1939 * but that would be problematic for ACKs until we have an absolute
1940 * guarantee that all waiters have at least one byte to send. The
1941 * latter should possibly not be set for now.
1942 */
1943
1944 done = 0;
1945 while (!done) {
1946 unsigned int flags = 0;
1947
1948 /* fill as much as we can into the current buffer */
1949 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
1950 done = h2_process_mux(h2c);
1951
1952 if (conn->flags & CO_FL_ERROR)
1953 break;
1954
1955 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
1956 flags |= CO_SFL_MSG_MORE;
1957
1958 if (conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
1959 break;
1960
1961 /* wrote at least one byte, the buffer is not full anymore */
1962 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
1963 }
1964
Willy Tarreaua2af5122017-10-09 11:56:46 +02001965 if (conn->flags & CO_FL_SOCK_WR_SH) {
1966 /* output closed, nothing to send, clear the buffer to release it */
1967 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001968 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001969}
Willy Tarreaua2af5122017-10-09 11:56:46 +02001970
Willy Tarreau62f52692017-10-08 23:01:42 +02001971/* callback called on any event by the connection handler.
1972 * It applies changes and returns zero, or < 0 if it wants immediate
1973 * destruction of the connection (which normally doesn not happen in h2).
1974 */
1975static int h2_wake(struct connection *conn)
1976{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001977 struct h2c *h2c = conn->mux_ctx;
1978
Willy Tarreau26bd7612017-10-09 16:47:04 +02001979 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01001980 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
1981 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
1982 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001983 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001984
1985 if (eb_is_empty(&h2c->streams_by_id)) {
1986 /* no more stream, kill the connection now */
1987 h2_release(conn);
1988 return -1;
1989 }
1990 else {
1991 /* some streams still there, we need to signal them all and
1992 * wait for their departure.
1993 */
1994 __conn_xprt_stop_recv(conn);
1995 __conn_xprt_stop_send(conn);
1996 return 0;
1997 }
1998 }
1999
2000 if (!h2c->dbuf->i)
2001 h2_release_dbuf(h2c);
2002
2003 /* stop being notified of incoming data if we can't process them */
2004 if (h2c->st0 >= H2_CS_ERROR ||
2005 (h2c->flags & H2_CF_DEM_BLOCK_ANY) || conn_xprt_read0_pending(conn)) {
2006 /* FIXME: we should clear a read timeout here */
2007 __conn_xprt_stop_recv(conn);
2008 }
2009 else {
2010 /* FIXME: we should (re-)arm a read timeout here */
2011 __conn_xprt_want_recv(conn);
2012 }
2013
2014 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002015 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
2016 (h2c->st0 == H2_CS_ERROR ||
2017 h2c->mbuf->o ||
2018 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2019 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002020 /* FIXME: we should (re-)arm a send timeout here */
2021 __conn_xprt_want_send(conn);
2022 }
2023 else {
2024 /* FIXME: we should clear a send timeout here */
2025 h2_release_mbuf(h2c);
2026 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002027 }
2028
Willy Tarreau62f52692017-10-08 23:01:42 +02002029 return 0;
2030}
2031
2032/*******************************************/
2033/* functions below are used by the streams */
2034/*******************************************/
2035
2036/*
2037 * Attach a new stream to a connection
2038 * (Used for outgoing connections)
2039 */
2040static struct conn_stream *h2_attach(struct connection *conn)
2041{
2042 return NULL;
2043}
2044
2045/* callback used to update the mux's polling flags after changing a cs' status.
2046 * The caller (cs_update_mux_polling) will take care of propagating any changes
2047 * to the transport layer.
2048 */
2049static void h2_update_poll(struct conn_stream *cs)
2050{
Willy Tarreau1d393222017-10-17 10:26:19 +02002051 struct h2s *h2s = cs->ctx;
2052
2053 if (!h2s)
2054 return;
2055
Willy Tarreaud7739c82017-10-30 15:38:23 +01002056 /* we may unblock a blocked read */
2057
2058 if (cs->flags & CS_FL_DATA_RD_ENA &&
2059 h2s->h2c->flags & H2_CF_DEM_SFULL && h2s->h2c->dsi == h2s->id) {
2060 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
2061 conn_xprt_want_recv(cs->conn);
2062 }
2063
Willy Tarreau1d393222017-10-17 10:26:19 +02002064 /* Note: the stream and stream-int code doesn't allow us to perform a
2065 * synchronous send() here unfortunately, because this code is called
2066 * as si_update() from the process_stream() context. This means that
2067 * we have to queue the current cs and defer its processing after the
2068 * connection's cs list is processed anyway.
2069 */
2070
2071 if (cs->flags & CS_FL_DATA_WR_ENA) {
2072 if (LIST_ISEMPTY(&h2s->list)) {
2073 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
2074 !h2s->h2c->mbuf->o && // not yet subscribed
2075 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2076 conn_xprt_want_send(cs->conn);
2077 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
2078 }
2079 }
2080 else if (!LIST_ISEMPTY(&h2s->list)) {
2081 LIST_DEL(&h2s->list);
2082 LIST_INIT(&h2s->list);
2083 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
2084 }
2085
2086 /* this can happen from within si_chk_snd() */
2087 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2088 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002089}
2090
2091/*
2092 * Detach the stream from the connection and possibly release the connection.
2093 */
2094static void h2_detach(struct conn_stream *cs)
2095{
Willy Tarreau60935142017-10-16 18:11:19 +02002096 struct h2s *h2s = cs->ctx;
2097 struct h2c *h2c;
2098
2099 cs->ctx = NULL;
2100 if (!h2s)
2101 return;
2102
2103 h2c = h2s->h2c;
2104 h2s->cs = NULL;
2105
Willy Tarreau45f752e2017-10-30 15:44:59 +01002106 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2107 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2108 /* unblock the connection if it was blocked on this
2109 * stream.
2110 */
2111 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2112 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2113 conn_xprt_want_recv(cs->conn);
2114 conn_xprt_want_send(cs->conn);
2115 }
2116
Willy Tarreau60935142017-10-16 18:11:19 +02002117 if (h2s->by_id.node.leaf_p) {
2118 /* h2s still attached to the h2c */
2119 eb32_delete(&h2s->by_id);
2120
2121 /* We don't want to close right now unless we're removing the
2122 * last stream, and either the connection is in error, or it
2123 * reached the ID already specified in a GOAWAY frame received
2124 * or sent (as seen by last_sid >= 0). A timer should be armed
2125 * to kill the connection after some idle time though.
2126 */
2127 if (eb_is_empty(&h2c->streams_by_id) &&
2128 (conn_xprt_read0_pending(h2c->conn) ||
2129 (h2c->conn->flags & CO_FL_ERROR) ||
2130 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2131 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))) {
2132 /* no more stream will come, kill it now */
2133 h2_release(h2c->conn);
2134 }
2135 }
2136 pool_free2(pool2_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02002137}
2138
2139static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2140{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002141 struct h2s *h2s = cs->ctx;
2142
2143 if (!mode)
2144 return;
2145
2146 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR ||
2147 h2s->st == H2_SS_RESET || h2s->st == H2_SS_CLOSED)
2148 return;
2149
2150 if (h2c_send_rst_stream(h2s->h2c, h2s) <= 0)
2151 return;
2152
2153 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2154 conn_xprt_want_send(cs->conn);
2155
2156 h2s->st = H2_SS_CLOSED;
Willy Tarreau62f52692017-10-08 23:01:42 +02002157}
2158
2159static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2160{
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002161 struct h2s *h2s = cs->ctx;
2162
2163 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR ||
2164 h2s->st == H2_SS_RESET || h2s->st == H2_SS_CLOSED)
2165 return;
2166
2167 if (h2s->h2c->flags & H2_CF_HEADERS_SENT) {
2168 if (h2_send_empty_data_es(h2s) <= 0)
2169 return;
2170 } else {
2171 if (h2c_send_rst_stream(h2s->h2c, h2s) <= 0)
2172 return;
2173 }
2174
2175 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
2176 conn_xprt_want_send(cs->conn);
2177
2178 if (h2s->st == H2_SS_OPEN && !(h2s->flags & H2_SF_RST_SENT))
2179 h2s->st = H2_SS_HLOC;
2180 else
2181 h2s->st = H2_SS_CLOSED;
2182
Willy Tarreau62f52692017-10-08 23:01:42 +02002183}
2184
Willy Tarreau13278b42017-10-13 19:23:14 +02002185/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2186 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2187 * proceed. Stream errors are reported in h2s->errcode and connection errors
2188 * in h2c->errcode. The caller must already have checked the frame header and
2189 * ensured that the frame was complete or the buffer full.
2190 */
2191static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
2192{
2193 struct h2c *h2c = h2s->h2c;
2194 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002195 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002196 int flen = h2c->dfl;
2197 int outlen = 0;
2198 int wrap;
2199 int try;
2200
2201 if (!h2c->dfl) {
2202 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
2203 return 0;
2204 }
2205
2206 /* if the input buffer wraps, take a temporary copy of it (rare) */
2207 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
2208 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002209 copy = alloc_trash_chunk();
2210 if (!copy) {
2211 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2212 goto fail;
2213 }
2214 memcpy(copy->str, h2c->dbuf->p, wrap);
2215 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
2216 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02002217 }
2218
2219 /* The padlen is the first byte before data, and the padding appears
2220 * after data. padlen+data+padding are included in flen.
2221 */
2222 if (h2c->dff & H2_F_HEADERS_PADDED) {
2223 if (*hdrs >= flen) {
2224 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2225 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2226 h2c->st0 = H2_SS_ERROR;
2227 return 0;
2228 }
2229 flen -= *hdrs + 1;
2230 hdrs += 1; // skip Pad Length
2231 }
2232
2233 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2234 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
2235 hdrs += 5; // stream dep = 4, weight = 1
2236 flen -= 5;
2237 }
2238
2239 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2240 * don't support this for now and can't even decompress so we have to
2241 * break the connection.
2242 */
2243 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2244 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002245 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002246 }
2247
2248 do {
2249 /* first check if we have some room after p+i */
2250 try = buf->data + buf->size - (buf->p + buf->i);
2251
2252 /* otherwise continue between data and p-o */
2253 if (try <= 0) {
2254 try = buf->p - (buf->data + buf->o);
2255 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02002256 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002257 }
2258 if (try > count)
2259 try = count;
2260
2261 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, bi_end(buf), try);
2262 if (outlen == -HPACK_ERR_TOO_LARGE) {
2263 if (buffer_space_wraps(buf)) {
2264 /* it doesn't fit and the buffer is fragmented,
2265 * so let's defragment it and try again.
2266 */
2267 buffer_slow_realign(buf);
2268 }
2269 else if (buf->o) {
2270 /* need to let the output buffer flush and
2271 * mark the buffer for later wake up.
2272 */
Willy Tarreau68dd9852017-07-03 14:44:26 +02002273 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002274 }
2275 else {
2276 /* no other way around */
2277 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002278 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002279 }
2280 }
2281 else if (outlen < 0) {
2282 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002283 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002284 }
2285 } while (outlen < 0);
2286
2287 /* now consume the input data */
2288 bi_del(h2c->dbuf, h2c->dfl);
2289 h2c->st0 = H2_CS_FRAME_H;
2290 buf->i += outlen;
2291
2292 /* don't send it before returning data!
2293 * FIXME: should we instead try to send it much later, after the
2294 * response ? This would require that we keep a copy of it in h2s.
2295 */
2296 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2297 h2s->cs->flags |= CS_FL_EOS;
2298 h2s->flags |= H2_SF_ES_RCVD;
2299 }
2300
Willy Tarreau68dd9852017-07-03 14:44:26 +02002301 leave:
2302 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002303 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002304 fail:
2305 outlen = 0;
2306 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002307}
2308
Willy Tarreau454f9052017-10-26 19:40:35 +02002309/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2310 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2311 * in use, a new chunk is emitted for each frame. This is supposed to fit
2312 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2313 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2314 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2315 * parser state is automatically updated. Returns the number of bytes emitted
2316 * if > 0, or 0 if it couldn't proceed. Stream errors are reported in
2317 * h2s->errcode and connection errors in h2c->errcode. The caller must already
2318 * have checked the frame header and ensured that the frame was complete or the
2319 * buffer full. It changes the frame state to FRAME_A once done.
2320 */
2321static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count)
2322{
2323 struct h2c *h2c = h2s->h2c;
2324 int block1, block2;
2325 unsigned int flen = h2c->dfl;
2326 unsigned int padlen = 0;
2327 int offset = 0;
2328
2329 if (h2c->dbuf->i < flen)
2330 return 0;
2331
2332 /* The padlen is the first byte before data, and the padding appears
2333 * after data. padlen+data+padding are included in flen.
2334 */
2335 if (h2c->dff & H2_F_HEADERS_PADDED) {
2336 padlen = *(uint8_t *)bi_ptr(h2c->dbuf);
2337 if (padlen >= flen) {
2338 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2339 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2340 h2c->st0 = H2_SS_ERROR;
2341 return 0;
2342 }
2343 flen -= padlen + 1;
2344 offset = 1; // skip Pad Length
2345 }
2346
2347 /* does it fit in output buffer or should we wait ? */
2348 if (buf->i + buf->o + flen > buf->size) {
2349 h2c->flags |= H2_CF_DEM_SFULL;
2350 return 0;
2351 }
2352
2353 /* Block1 is the length of the first block before the buffer wraps,
2354 * block2 is the optional second block to reach the end of the frame.
2355 */
2356 block1 = bi_contig_data(h2c->dbuf);
2357 if (block1 > offset + flen)
2358 block1 = offset + flen;
2359 block1 -= offset; // skip Pad Length
2360 block2 = flen - block1;
2361
2362 if (block1)
2363 bi_putblk(buf, b_ptr(h2c->dbuf, offset), block1);
2364
2365 if (block2)
2366 bi_putblk(buf, b_ptr(h2c->dbuf, offset + block1), block2);
2367
2368 /* now mark the input data as consumed (will be deleted from the buffer
2369 * by the caller when seeing FRAME_A after sending the window update).
2370 */
2371 h2c->rcvd_c += h2c->dfl;
2372 h2c->rcvd_s += h2c->dfl; // warning, this can also affect the closed streams!
2373 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
2374
2375 /* don't send it before returning data!
2376 * FIXME: should we instead try to send it much later, after the
2377 * response ? This would require that we keep a copy of it in h2s.
2378 */
2379 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2380 h2s->cs->flags |= CS_FL_EOS;
2381 h2s->flags |= H2_SF_ES_RCVD;
2382 }
2383
2384 return flen;
2385}
2386
Willy Tarreau62f52692017-10-08 23:01:42 +02002387/*
Willy Tarreau13278b42017-10-13 19:23:14 +02002388 * Called from the upper layer to get more data, up to <count> bytes. The
2389 * caller is responsible for never asking for more data than what is available
2390 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02002391 */
2392static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
2393{
Willy Tarreau13278b42017-10-13 19:23:14 +02002394 struct h2s *h2s = cs->ctx;
2395 struct h2c *h2c = h2s->h2c;
2396 int ret = 0;
2397
2398 if (h2c->st0 != H2_CS_FRAME_P)
2399 return 0; // no pre-parsed frame yet
2400
2401 if (h2c->dsi != h2s->id)
2402 return 0; // not for us
2403
2404 if (!h2c->dbuf->size)
2405 return 0; // empty buffer
2406
2407 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
2408 return 0; // incomplete input frame
2409
2410 switch (h2c->dft) {
2411 case H2_FT_HEADERS:
2412 ret = h2_frt_decode_headers(h2s, buf, count);
2413 break;
2414
Willy Tarreau454f9052017-10-26 19:40:35 +02002415 case H2_FT_DATA:
2416 ret = h2_frt_transfer_data(h2s, buf, count);
2417 break;
2418
Willy Tarreau13278b42017-10-13 19:23:14 +02002419 default:
2420 ret = 0;
2421 }
2422 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02002423}
2424
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002425/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
2426 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
2427 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
2428 * to the number of buffer bytes consumed.
2429 */
2430static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
2431{
2432 struct http_hdr list[MAX_HTTP_HDR];
2433 struct h2c *h2c = h2s->h2c;
2434 struct h1m *h1m = &h2s->res;
2435 struct chunk outbuf;
2436 int es_now = 0;
2437 int ret = 0;
2438 int hdr;
2439
2440 if (h2c_mux_busy(h2c, h2s)) {
2441 h2s->flags |= H2_SF_BLK_MBUSY;
2442 return 0;
2443 }
2444
2445 if (!h2_get_mbuf(h2c)) {
2446 h2c->flags |= H2_CF_MUX_MALLOC;
2447 h2s->flags |= H2_SF_BLK_MROOM;
2448 return 0;
2449 }
2450
2451 /* First, try to parse the H1 response and index it into <list>.
2452 * NOTE! Since it comes from haproxy, we *know* that a response header
2453 * block does not wrap and we can safely read it this way without
2454 * having to realign the buffer.
2455 */
Willy Tarreauc199faf2017-10-31 08:35:27 +01002456 next_header_block:
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002457 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
2458 list, sizeof(list)/sizeof(list[0]), h1m);
2459 if (ret <= 0) {
2460 if (!ret)
2461 goto end; // missing input
2462
2463 /* Impossible to index the response.
2464 * FIXME: we should instead add the ability to only return a
2465 * 502 bad gateway. But in theory this is not supposed to
2466 * happen.
2467 */
2468 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2469 ret = 0;
2470 goto end;
2471 }
2472
2473 chunk_reset(&outbuf);
2474
2475 while (1) {
2476 outbuf.str = bo_end(h2c->mbuf);
2477 outbuf.size = bo_contig_space(h2c->mbuf);
2478 outbuf.len = 0;
2479
2480 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2481 break;
2482 realign_again:
2483 buffer_slow_realign(h2c->mbuf);
2484 }
2485
2486 if (outbuf.size < 9) {
2487 h2c->flags |= H2_CF_MUX_MFULL;
2488 h2s->flags |= H2_SF_BLK_MROOM;
2489 ret = 0;
2490 goto end;
2491 }
2492
2493 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
2494 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
2495 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2496 outbuf.len = 9;
2497
2498 /* encode status, which necessarily is the first one */
2499 if (outbuf.len < outbuf.size && h1m->status == 200)
2500 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
2501 else if (outbuf.len < outbuf.size && h1m->status == 304)
2502 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
2503 else if (list[0].v.len == 3 && outbuf.len + 2 + 3 <= outbuf.size) {
2504 /* basic encoding of the status code */
2505 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2506 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2507 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2508 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2509 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2510 }
2511 else {
2512 if (buffer_space_wraps(h2c->mbuf))
2513 goto realign_again;
2514
2515 h2c->flags |= H2_CF_MUX_MFULL;
2516 h2s->flags |= H2_SF_BLK_MROOM;
2517 ret = 0;
2518 goto end;
2519 }
2520
2521 /* encode all headers, stop at empty name */
2522 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
2523 /* these ones do not exist in H2 and must be dropped */
2524 if (isteq(list[hdr].n, ist("connection")) ||
2525 isteq(list[hdr].n, ist("proxy-connection")) ||
2526 isteq(list[hdr].n, ist("keep-alive")) ||
2527 isteq(list[hdr].n, ist("upgrade")) ||
2528 isteq(list[hdr].n, ist("transfer-encoding")))
2529 continue;
2530
2531 if (isteq(list[hdr].n, ist("")))
2532 break; // end
2533
2534 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2535 /* output full */
2536 if (buffer_space_wraps(h2c->mbuf))
2537 goto realign_again;
2538
2539 h2c->flags |= H2_CF_MUX_MFULL;
2540 h2s->flags |= H2_SF_BLK_MROOM;
2541 ret = 0;
2542 goto end;
2543 }
2544 }
2545
2546 /* we may need to add END_STREAM */
2547 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2548 es_now = 1;
2549
2550 /* update the frame's size */
2551 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2552
2553 if (es_now)
2554 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2555
2556 /* consume incoming H1 response */
2557 bo_del(buf, ret);
2558
2559 /* commit the H2 response */
2560 h2c->mbuf->o += outbuf.len;
2561 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
2562 h2c->flags |= H2_CF_HEADERS_SENT;
2563
2564 /* for now we don't implemented CONTINUATION, so we wait for a
2565 * body or directly end in TRL2.
2566 */
2567 if (es_now) {
2568 h1m->state = HTTP_MSG_DONE;
2569 h2s->flags |= H2_SF_ES_SENT;
2570 if (h2s->st == H2_SS_OPEN)
2571 h2s->st = H2_SS_HLOC;
2572 else
2573 h2s->st = H2_SS_CLOSED;
2574 }
Willy Tarreauc199faf2017-10-31 08:35:27 +01002575 else if (h1m->status >= 100 && h1m->status < 200) {
2576 h1m->state = HTTP_MSG_RPBEFORE;
2577 h1m->status = 0;
2578 h1m->flags = 0;
2579 goto next_header_block;
2580 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002581 else
2582 h1m->state = (h1m->flags & H1_MF_CLEN) ? HTTP_MSG_BODY : HTTP_MSG_CHUNK_SIZE;
2583
2584 end:
2585 //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));
2586 return ret;
2587}
2588
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002589/* Try to send a DATA frame matching HTTP/1 response present in the response
2590 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
2591 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
2592 * corresponds to the number of buffer bytes consumed.
2593 */
2594static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
2595{
2596 struct h2c *h2c = h2s->h2c;
2597 struct h1m *h1m = &h2s->res;
2598 struct chunk outbuf;
2599 int ret = 0;
2600 int total = 0;
2601 int es_now = 0;
2602 int size = 0;
2603 char *blk1, *blk2;
2604 int len1, len2;
2605
2606 if (h2c_mux_busy(h2c, h2s)) {
2607 h2s->flags |= H2_SF_BLK_MBUSY;
2608 goto end;
2609 }
2610
2611 if (!h2_get_mbuf(h2c)) {
2612 h2c->flags |= H2_CF_MUX_MALLOC;
2613 h2s->flags |= H2_SF_BLK_MROOM;
2614 goto end;
2615 }
2616
2617 new_frame:
2618 if (!buf->o)
2619 goto end;
2620
2621 chunk_reset(&outbuf);
2622
2623 while (1) {
2624 outbuf.str = bo_end(h2c->mbuf);
2625 outbuf.size = bo_contig_space(h2c->mbuf);
2626 outbuf.len = 0;
2627
2628 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2629 break;
2630 realign_again:
2631 buffer_slow_realign(h2c->mbuf);
2632 }
2633
2634 if (outbuf.size < 9) {
2635 h2c->flags |= H2_CF_MUX_MFULL;
2636 h2s->flags |= H2_SF_BLK_MROOM;
2637 goto end;
2638 }
2639
2640 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
2641 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
2642 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2643 outbuf.len = 9;
2644
2645 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
2646 case 0: /* no content length, read till SHUTW */
2647 size = buf->o;
2648 break;
2649 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
2650 size = buf->o;
2651 if ((long long)size > h1m->curr_len)
2652 size = h1m->curr_len;
2653 break;
2654 default: /* te:chunked : parse chunks */
2655 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
2656 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
2657 if (!ret)
2658 goto end;
2659
2660 if (ret < 0) {
2661 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2662 h1m->err_pos = ret;
2663 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2664 goto end;
2665 }
2666 bo_del(buf, ret);
2667 total += ret;
2668 h1m->state = HTTP_MSG_CHUNK_SIZE;
2669 }
2670
2671 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
2672 unsigned int chunk;
2673
2674 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
2675 if (!ret)
2676 goto end;
2677
2678 if (ret < 0) {
2679 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2680 h1m->err_pos = ret;
2681 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2682 goto end;
2683 }
2684
2685 size = chunk;
2686 h1m->curr_len = chunk;
2687 h1m->body_len += chunk;
2688 bo_del(buf, ret);
2689 total += ret;
2690 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
2691 if (!size)
2692 goto send_empty;
2693 }
2694
2695 /* in MSG_DATA state, continue below */
2696 size = h1m->curr_len;
2697 break;
2698 }
2699
2700 /* we have in <size> the exact number of bytes we need to copy from
2701 * the H1 buffer. We need to check this against the connection's and
2702 * the stream's send windows, and to ensure that this fits in the max
2703 * frame size and in the buffer's available space minus 9 bytes (for
2704 * the frame header). The connection's flow control is applied last so
2705 * that we can use a separate list of streams which are immediately
2706 * unblocked on window opening. Note: we don't implement padding.
2707 */
2708
2709 if (size > buf->o)
2710 size = buf->o;
2711
2712 if (size > h2s->mws)
2713 size = h2s->mws;
2714
2715 if (size <= 0) {
2716 h2s->flags |= H2_SF_BLK_SFCTL;
2717 goto end;
2718 }
2719
2720 if (h2c->mfs && size > h2c->mfs)
2721 size = h2c->mfs;
2722
2723 if (size + 9 > outbuf.size) {
2724 /* we have an opportunity for enlarging the too small
2725 * available space, let's try.
2726 */
2727 if (buffer_space_wraps(h2c->mbuf))
2728 goto realign_again;
2729 size = outbuf.size - 9;
2730 }
2731
2732 if (size <= 0) {
2733 h2c->flags |= H2_CF_MUX_MFULL;
2734 h2s->flags |= H2_SF_BLK_MROOM;
2735 goto end;
2736 }
2737
2738 if (size > h2c->mws)
2739 size = h2c->mws;
2740
2741 if (size <= 0) {
2742 h2s->flags |= H2_SF_BLK_MFCTL;
2743 goto end;
2744 }
2745
2746 /* copy whatever we can */
2747 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
2748 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
2749 if (ret == 1)
2750 len2 = 0;
2751
2752 if (!ret || len1 + len2 < size) {
2753 /* FIXME: must normally never happen */
2754 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2755 goto end;
2756 }
2757
2758 /* limit len1/len2 to size */
2759 if (len1 + len2 > size) {
2760 int sub = len1 + len2 - size;
2761
2762 if (len2 > sub)
2763 len2 -= sub;
2764 else {
2765 sub -= len2;
2766 len2 = 0;
2767 len1 -= sub;
2768 }
2769 }
2770
2771 /* now let's copy this this into the output buffer */
2772 memcpy(outbuf.str + 9, blk1, len1);
2773 if (len2)
2774 memcpy(outbuf.str + 9 + len1, blk2, len2);
2775
2776 send_empty:
2777 /* we may need to add END_STREAM */
2778 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
2779 * could rely on the MSG_MORE flag as a hint for this ?
2780 */
2781 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
2782 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
2783 es_now = 1;
2784
2785 /* update the frame's size */
2786 h2_set_frame_size(outbuf.str, size);
2787
2788 if (es_now)
2789 outbuf.str[4] |= H2_F_DATA_END_STREAM;
2790
2791 /* commit the H2 response */
2792 h2c->mbuf->o += size + 9;
2793 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
2794
2795 /* consume incoming H1 response */
2796 if (size > 0) {
2797 bo_del(buf, size);
2798 total += size;
2799 h1m->curr_len -= size;
2800 h2s->mws -= size;
2801 h2c->mws -= size;
2802
2803 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
2804 h1m->state = HTTP_MSG_CHUNK_CRLF;
2805 goto new_frame;
2806 }
2807 }
2808
2809 if (es_now) {
2810 if (h2s->st == H2_SS_OPEN)
2811 h2s->st = H2_SS_HLOC;
2812 else
2813 h2s->st = H2_SS_CLOSED;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01002814
2815 if (!(h1m->flags & H1_MF_CHNK))
2816 h1m->state = HTTP_MSG_DONE;
2817
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002818 h2s->flags |= H2_SF_ES_SENT;
2819 }
2820
2821 end:
2822 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);
2823 return total;
2824}
2825
Willy Tarreau62f52692017-10-08 23:01:42 +02002826/* Called from the upper layer, to send data */
2827static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
2828{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002829 struct h2s *h2s = cs->ctx;
2830 int total = 0;
2831
2832 //fprintf(stderr, "cs=%p h2s=%p rqst=%d rsst=%d\n", cs, h2s, h2s->req.state, h2s->res.state);
2833 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
2834 if (h2s->res.state < HTTP_MSG_BODY) {
2835 total += h2s_frt_make_resp_headers(h2s, buf);
2836
2837 if (h2s->st == H2_SS_ERROR)
2838 break;
2839
2840 if (h2s->flags & H2_SF_BLK_ANY)
2841 break;
2842 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002843 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
2844 total += h2s_frt_make_resp_data(h2s, buf);
2845
2846 if (h2s->st == H2_SS_ERROR)
2847 break;
2848
2849 if (h2s->flags & H2_SF_BLK_ANY)
2850 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01002851 }
2852 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
2853 /* consume the trailers if any (we don't forward them for now) */
2854 int count = h1_measure_trailers(buf);
2855
2856 if (unlikely(count <= 0)) {
2857 if (count < 0)
2858 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2859 break;
2860 }
2861 total += count;
2862 bo_del(buf, count);
2863 h2s->res.state = HTTP_MSG_DONE;
2864 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002865 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002866 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002867 cs->flags |= CS_FL_ERROR;
2868 break;
2869 }
2870 }
2871
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002872 if (h2s->flags & H2_SF_BLK_SFCTL) {
2873 /* stream flow control, quit the list */
2874 LIST_DEL(&h2s->list);
2875 LIST_INIT(&h2s->list);
2876 }
2877
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002878 if (h2s->st == H2_SS_ERROR)
2879 cs->flags |= CS_FL_ERROR;
2880
2881 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02002882}
2883
2884
2885/*******************************************************/
2886/* functions below are dedicated to the config parsers */
2887/*******************************************************/
2888
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02002889/* config parser for global "tune.h2.header-table-size" */
2890static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
2891 struct proxy *defpx, const char *file, int line,
2892 char **err)
2893{
2894 if (too_many_args(1, args, err, NULL))
2895 return -1;
2896
2897 h2_settings_header_table_size = atoi(args[1]);
2898 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
2899 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
2900 return -1;
2901 }
2902 return 0;
2903}
Willy Tarreau62f52692017-10-08 23:01:42 +02002904
Willy Tarreaue6baec02017-07-27 11:45:11 +02002905/* config parser for global "tune.h2.initial-window-size" */
2906static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
2907 struct proxy *defpx, const char *file, int line,
2908 char **err)
2909{
2910 if (too_many_args(1, args, err, NULL))
2911 return -1;
2912
2913 h2_settings_initial_window_size = atoi(args[1]);
2914 if (h2_settings_initial_window_size < 0) {
2915 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
2916 return -1;
2917 }
2918 return 0;
2919}
2920
Willy Tarreau5242ef82017-07-27 11:47:28 +02002921/* config parser for global "tune.h2.max-concurrent-streams" */
2922static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
2923 struct proxy *defpx, const char *file, int line,
2924 char **err)
2925{
2926 if (too_many_args(1, args, err, NULL))
2927 return -1;
2928
2929 h2_settings_max_concurrent_streams = atoi(args[1]);
2930 if (h2_settings_max_concurrent_streams < 0) {
2931 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
2932 return -1;
2933 }
2934 return 0;
2935}
2936
Willy Tarreau62f52692017-10-08 23:01:42 +02002937
2938/****************************************/
2939/* MUX initialization and instanciation */
2940/***************************************/
2941
2942/* The mux operations */
2943const struct mux_ops h2_ops = {
2944 .init = h2_init,
2945 .recv = h2_recv,
2946 .send = h2_send,
2947 .wake = h2_wake,
2948 .update_poll = h2_update_poll,
2949 .rcv_buf = h2_rcv_buf,
2950 .snd_buf = h2_snd_buf,
2951 .attach = h2_attach,
2952 .detach = h2_detach,
2953 .shutr = h2_shutr,
2954 .shutw = h2_shutw,
2955 .release = h2_release,
2956 .name = "H2",
2957};
2958
2959/* ALPN selection : this mux registers ALPN tolen "h2" */
2960static struct alpn_mux_list alpn_mux_h2 =
2961 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
2962
2963/* config keyword parsers */
2964static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02002965 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02002966 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02002967 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02002968 { 0, NULL, NULL }
2969}};
2970
Willy Tarreau5ab6b572017-09-22 08:05:00 +02002971static void __h2_deinit(void)
2972{
Willy Tarreau18312642017-10-11 07:57:07 +02002973 pool_destroy2(pool2_h2s);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02002974 pool_destroy2(pool2_h2c);
2975}
2976
Willy Tarreau62f52692017-10-08 23:01:42 +02002977__attribute__((constructor))
2978static void __h2_init(void)
2979{
2980 alpn_register_mux(&alpn_mux_h2);
2981 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02002982 hap_register_post_deinit(__h2_deinit);
2983 pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
Willy Tarreau18312642017-10-11 07:57:07 +02002984 pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02002985}