blob: e0e2ebe57b422299f2c4c1ca7a980259cd3b1e07 [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 Tarreau18312642017-10-11 07:57:07 +0200142/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
143 * it is being processed in the internal HTTP representation (H1 for now).
144 */
145struct h2s {
146 struct conn_stream *cs;
147 struct h2c *h2c;
148 struct h1m req, res; /* request and response parser state for H1 */
149 struct eb32_node by_id; /* place in h2c's streams_by_id */
150 struct list list; /* position in active/blocked lists if blocked>0 */
151 int32_t id; /* stream ID */
152 uint32_t flags; /* H2_SF_* */
153 int mws; /* mux window size for this stream */
154 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
155 enum h2_ss st;
156};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200157
Willy Tarreauc6405142017-09-21 20:23:50 +0200158/* descriptor for an h2 frame header */
159struct h2_fh {
160 uint32_t len; /* length, host order, 24 bits */
161 uint32_t sid; /* stream id, host order, 31 bits */
162 uint8_t ft; /* frame type */
163 uint8_t ff; /* frame flags */
164};
165
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200166/* a few settings from the global section */
167static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200168static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200169static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200170
Willy Tarreau2a856182017-05-16 15:20:39 +0200171/* a dmumy closed stream */
172static const struct h2s *h2_closed_stream = &(const struct h2s){
173 .cs = NULL,
174 .h2c = NULL,
175 .st = H2_SS_CLOSED,
176 .id = 0,
177};
178
179/* and a dummy idle stream for use with any unannounced stream */
180static const struct h2s *h2_idle_stream = &(const struct h2s){
181 .cs = NULL,
182 .h2c = NULL,
183 .st = H2_SS_IDLE,
184 .id = 0,
185};
186
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200187
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200188/*****************************************************/
189/* functions below are for dynamic buffer management */
190/*****************************************************/
191
192/* re-enables receiving on mux <target> after a buffer was allocated. It returns
193 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
194 * if it's impossible to wake up and we prefer to be woken up later.
195 */
196static int h2_dbuf_available(void *target)
197{
198 struct h2c *h2c = target;
199
200 /* take the buffer now as we'll get scheduled waiting for ->wake() */
201 if (b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200202 h2c->flags &= ~H2_CF_DEM_DALLOC;
203 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
204 conn_xprt_want_recv(h2c->conn);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200205 return 1;
206 }
207 return 0;
208}
209
210static inline struct buffer *h2_get_dbuf(struct h2c *h2c)
211{
212 struct buffer *buf = NULL;
213
214 if (likely(LIST_ISEMPTY(&h2c->dbuf_wait.list)) &&
215 unlikely((buf = b_alloc_margin(&h2c->dbuf, 0)) == NULL)) {
216 h2c->dbuf_wait.target = h2c->conn;
217 h2c->dbuf_wait.wakeup_cb = h2_dbuf_available;
218 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
219 LIST_ADDQ(&buffer_wq, &h2c->dbuf_wait.list);
220 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
221 __conn_xprt_stop_recv(h2c->conn);
222 }
223 return buf;
224}
225
226static inline void h2_release_dbuf(struct h2c *h2c)
227{
228 if (h2c->dbuf->size) {
229 b_free(&h2c->dbuf);
230 offer_buffers(h2c->dbuf_wait.target,
231 tasks_run_queue + applets_active_queue);
232 }
233}
234
Willy Tarreau14398122017-09-22 14:26:04 +0200235/* re-enables sending on mux <target> after a buffer was allocated. It returns
236 * 1 if the allocation succeeds, in which case the connection is woken up, or 0
237 * if it's impossible to wake up and we prefer to be woken up later.
238 */
239static int h2_mbuf_available(void *target)
240{
241 struct h2c *h2c = target;
242
243 /* take the buffer now as we'll get scheduled waiting for ->wake(). */
244 if (b_alloc_margin(&h2c->mbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200245 if (h2c->flags & H2_CF_MUX_MALLOC) {
246 h2c->flags &= ~H2_CF_MUX_MALLOC;
247 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
248 conn_xprt_want_send(h2c->conn);
249 }
250
251 if (h2c->flags & H2_CF_DEM_MROOM) {
252 h2c->flags &= ~H2_CF_DEM_MROOM;
253 if (!(h2c->flags & H2_CF_DEM_BLOCK_ANY))
254 conn_xprt_want_recv(h2c->conn);
255 }
256
Willy Tarreau14398122017-09-22 14:26:04 +0200257 /* FIXME: we should in fact call something like h2_update_poll()
258 * now to recompte the polling. For now it will be enough like
259 * this.
260 */
Willy Tarreau14398122017-09-22 14:26:04 +0200261 return 1;
262 }
263 return 0;
264}
265
266static inline struct buffer *h2_get_mbuf(struct h2c *h2c)
267{
268 struct buffer *buf = NULL;
269
270 if (likely(LIST_ISEMPTY(&h2c->mbuf_wait.list)) &&
271 unlikely((buf = b_alloc_margin(&h2c->mbuf, 0)) == NULL)) {
272 h2c->mbuf_wait.target = h2c;
273 h2c->mbuf_wait.wakeup_cb = h2_mbuf_available;
274 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
275 LIST_ADDQ(&buffer_wq, &h2c->mbuf_wait.list);
276 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
277
278 /* FIXME: we should in fact only block the direction being
279 * currently used. For now it will be enough like this.
280 */
281 __conn_xprt_stop_send(h2c->conn);
282 __conn_xprt_stop_recv(h2c->conn);
283 }
284 return buf;
285}
286
287static inline void h2_release_mbuf(struct h2c *h2c)
288{
289 if (h2c->mbuf->size) {
290 b_free(&h2c->mbuf);
291 offer_buffers(h2c->mbuf_wait.target,
292 tasks_run_queue + applets_active_queue);
293 }
294}
295
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200296
Willy Tarreau62f52692017-10-08 23:01:42 +0200297/*****************************************************************/
298/* functions below are dedicated to the mux setup and management */
299/*****************************************************************/
300
Willy Tarreau32218eb2017-09-22 08:07:25 +0200301/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
302static int h2c_frt_init(struct connection *conn)
303{
304 struct h2c *h2c;
305
306 h2c = pool_alloc2(pool2_h2c);
307 if (!h2c)
308 goto fail;
309
310 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
311 if (!h2c->ddht)
312 goto fail;
313
314 /* Initialise the context. */
315 h2c->st0 = H2_CS_PREFACE;
316 h2c->conn = conn;
317 h2c->max_id = -1;
318 h2c->errcode = H2_ERR_NO_ERROR;
319 h2c->flags = H2_CF_NONE;
320 h2c->rcvd_c = 0;
321 h2c->rcvd_s = 0;
322
323 h2c->dbuf = &buf_empty;
324 h2c->dsi = -1;
325 h2c->msi = -1;
326 h2c->last_sid = -1;
327
328 h2c->mbuf = &buf_empty;
329 h2c->miw = 65535; /* mux initial window size */
330 h2c->mws = 65535; /* mux window size */
331 h2c->mfs = 16384; /* initial max frame size */
332 h2c->streams_by_id = EB_ROOT_UNIQUE;
333 LIST_INIT(&h2c->send_list);
334 LIST_INIT(&h2c->fctl_list);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200335 LIST_INIT(&h2c->dbuf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200336 LIST_INIT(&h2c->mbuf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200337 conn->mux_ctx = h2c;
338
339 conn_xprt_want_recv(conn);
340 /* mux->wake will be called soon to complete the operation */
341 return 0;
342 fail:
343 pool_free2(pool2_h2c, h2c);
344 return -1;
345}
346
Willy Tarreau62f52692017-10-08 23:01:42 +0200347/* Initialize the mux once it's attached. For outgoing connections, the context
348 * is already initialized before installing the mux, so we detect incoming
349 * connections from the fact that the context is still NULL. Returns < 0 on
350 * error.
351 */
352static int h2_init(struct connection *conn)
353{
354 if (conn->mux_ctx) {
355 /* we don't support outgoing connections for now */
356 return -1;
357 }
358
Willy Tarreau32218eb2017-09-22 08:07:25 +0200359 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200360}
361
Willy Tarreau2373acc2017-10-12 17:35:14 +0200362/* returns the stream associated with id <id> or NULL if not found */
363static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
364{
365 struct eb32_node *node;
366
Willy Tarreau2a856182017-05-16 15:20:39 +0200367 if (id > h2c->max_id)
368 return (struct h2s *)h2_idle_stream;
369
Willy Tarreau2373acc2017-10-12 17:35:14 +0200370 node = eb32_lookup(&h2c->streams_by_id, id);
371 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200372 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200373
374 return container_of(node, struct h2s, by_id);
375}
376
Willy Tarreau62f52692017-10-08 23:01:42 +0200377/* release function for a connection. This one should be called to free all
378 * resources allocated to the mux.
379 */
380static void h2_release(struct connection *conn)
381{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200382 struct h2c *h2c = conn->mux_ctx;
383
384 LIST_DEL(&conn->list);
385
386 if (h2c) {
387 hpack_dht_free(h2c->ddht);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200388 h2_release_dbuf(h2c);
389 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
390 LIST_DEL(&h2c->dbuf_wait.list);
391 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200392
393 h2_release_mbuf(h2c);
394 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
395 LIST_DEL(&h2c->mbuf_wait.list);
396 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
397
Willy Tarreau32218eb2017-09-22 08:07:25 +0200398 pool_free2(pool2_h2c, h2c);
399 }
400
401 conn->mux = NULL;
402 conn->mux_ctx = NULL;
403
404 conn_stop_tracking(conn);
405 conn_full_close(conn);
406 if (conn->destroy_cb)
407 conn->destroy_cb(conn);
408 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200409}
410
411
Willy Tarreau71681172017-10-23 14:39:06 +0200412/******************************************************/
413/* functions below are for the H2 protocol processing */
414/******************************************************/
415
416/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
417static inline int h2s_id(const struct h2s *h2s)
418{
419 return h2s ? h2s->id : 0;
420}
421
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200422/* returns true of the mux is currently busy as seen from stream <h2s> */
423static inline int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
424{
425 if (h2c->msi < 0)
426 return 0;
427
428 if (h2c->msi == h2s_id(h2s))
429 return 0;
430
431 return 1;
432}
433
Willy Tarreau741d6df2017-10-17 08:00:59 +0200434/* marks an error on the connection */
435static inline void h2c_error(struct h2c *h2c, enum h2_err err)
436{
437 h2c->errcode = err;
438 h2c->st0 = H2_CS_ERROR;
439}
440
Willy Tarreau2e43f082017-10-17 08:03:59 +0200441/* marks an error on the stream */
442static inline void h2s_error(struct h2s *h2s, enum h2_err err)
443{
444 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
445 h2s->errcode = err;
446 h2s->st = H2_SS_ERROR;
447 if (h2s->cs)
448 h2s->cs->flags |= CS_FL_ERROR;
449 }
450}
451
Willy Tarreaue4820742017-07-27 13:37:23 +0200452/* writes the 24-bit frame size <len> at address <frame> */
453static inline void h2_set_frame_size(void *frame, uint32_t len)
454{
455 uint8_t *out = frame;
456
457 *out = len >> 16;
458 write_n16(out + 1, len);
459}
460
Willy Tarreau54c15062017-10-10 17:10:03 +0200461/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
462 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
463 * the caller's responsibility to verify that there are at least <bytes> bytes
464 * available in the buffer's input prior to calling this function.
465 */
466static inline void h2_get_buf_bytes(void *dst, size_t bytes,
467 const struct buffer *b, int o)
468{
469 readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
470}
471
472static inline uint16_t h2_get_n16(const struct buffer *b, int o)
473{
474 return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
475}
476
477static inline uint32_t h2_get_n32(const struct buffer *b, int o)
478{
479 return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
480}
481
482static inline uint64_t h2_get_n64(const struct buffer *b, int o)
483{
484 return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data);
485}
486
487
Willy Tarreau715d5312017-07-11 15:20:24 +0200488/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
489 * is not obvious. It turns out that H2 headers are neither aligned nor do they
490 * use regular sizes. And to add to the trouble, the buffer may wrap so each
491 * byte read must be checked. The header is formed like this :
492 *
493 * b0 b1 b2 b3 b4 b5..b8
494 * +----------+---------+--------+----+----+----------------------+
495 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
496 * +----------+---------+--------+----+----+----------------------+
497 *
498 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
499 * we get the sid properly aligned and ordered, and 16 bits of len properly
500 * ordered as well. The type and flags can be extracted using bit shifts from
501 * the word, and only one extra read is needed to fetch len[16:23].
502 * Returns zero if some bytes are missing, otherwise non-zero on success.
503 */
504static int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
505{
506 uint64_t w;
507
508 if (b->i < 9)
509 return 0;
510
511 w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data);
512 h->len = *b->p << 16;
513 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
514 h->ff = w >> 32;
515 h->ft = w >> 40;
516 h->len += w >> 48;
517 return 1;
518}
519
520/* skip the next 9 bytes corresponding to the frame header possibly parsed by
521 * h2_peek_frame_hdr() above.
522 */
523static inline void h2_skip_frame_hdr(struct buffer *b)
524{
525 bi_del(b, 9);
526}
527
528/* same as above, automatically advances the buffer on success */
529static inline int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
530{
531 int ret;
532
533 ret = h2_peek_frame_hdr(b, h);
534 if (ret > 0)
535 h2_skip_frame_hdr(b);
536 return ret;
537}
538
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200539/* creates a new stream <id> on the h2c connection and returns it, or NULL in
540 * case of memory allocation error.
541 */
542static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
543{
544 struct conn_stream *cs;
545 struct h2s *h2s;
546
547 h2s = pool_alloc2(pool2_h2s);
548 if (!h2s)
549 goto out;
550
551 h2s->h2c = h2c;
552 h2s->mws = h2c->miw;
553 h2s->flags = H2_SF_NONE;
554 h2s->errcode = H2_ERR_NO_ERROR;
555 h2s->st = H2_SS_IDLE;
556 h1m_init(&h2s->req);
557 h1m_init(&h2s->res);
558 h2s->by_id.key = h2s->id = id;
559 h2c->max_id = id;
560 LIST_INIT(&h2s->list);
561
562 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
563
564 cs = cs_new(h2c->conn);
565 if (!cs)
566 goto out_close;
567
568 h2s->cs = cs;
569 cs->ctx = h2s;
570
571 if (stream_create_from_cs(cs) < 0)
572 goto out_free_cs;
573
574 /* OK done, the stream lives its own life now */
575 return h2s;
576
577 out_free_cs:
578 cs_free(cs);
579 out_close:
580 eb32_delete(&h2s->by_id);
581 pool_free2(pool2_h2s, h2s);
582 h2s = NULL;
583 out:
584 return h2s;
585}
586
Willy Tarreaube5b7152017-09-25 16:25:39 +0200587/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
588 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
589 * the various settings codes.
590 */
591static int h2c_snd_settings(struct h2c *h2c)
592{
593 struct buffer *res;
594 char buf_data[100]; // enough for 15 settings
595 struct chunk buf;
596 int ret;
597
598 if (h2c_mux_busy(h2c, NULL)) {
599 h2c->flags |= H2_CF_DEM_MBUSY;
600 return 0;
601 }
602
603 res = h2_get_mbuf(h2c);
604 if (!res) {
605 h2c->flags |= H2_CF_MUX_MALLOC;
606 h2c->flags |= H2_CF_DEM_MROOM;
607 return 0;
608 }
609
610 chunk_init(&buf, buf_data, sizeof(buf_data));
611 chunk_memcpy(&buf,
612 "\x00\x00\x00" /* length : 0 for now */
613 "\x04\x00" /* type : 4 (settings), flags : 0 */
614 "\x00\x00\x00\x00", /* stream ID : 0 */
615 9);
616
617 if (h2_settings_header_table_size != 4096) {
618 char str[6] = "\x00\x01"; /* header_table_size */
619
620 write_n32(str + 2, h2_settings_header_table_size);
621 chunk_memcat(&buf, str, 6);
622 }
623
624 if (h2_settings_initial_window_size != 65535) {
625 char str[6] = "\x00\x04"; /* initial_window_size */
626
627 write_n32(str + 2, h2_settings_initial_window_size);
628 chunk_memcat(&buf, str, 6);
629 }
630
631 if (h2_settings_max_concurrent_streams != 0) {
632 char str[6] = "\x00\x03"; /* max_concurrent_streams */
633
634 /* Note: 0 means "unlimited" for haproxy's config but not for
635 * the protocol, so never send this value!
636 */
637 write_n32(str + 2, h2_settings_max_concurrent_streams);
638 chunk_memcat(&buf, str, 6);
639 }
640
641 if (global.tune.bufsize != 16384) {
642 char str[6] = "\x00\x05"; /* max_frame_size */
643
644 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
645 * match bufsize - rewrite size, but at the moment it seems
646 * that clients don't take care of it.
647 */
648 write_n32(str + 2, global.tune.bufsize);
649 chunk_memcat(&buf, str, 6);
650 }
651
652 h2_set_frame_size(buf.str, buf.len - 9);
653 ret = bo_istput(res, ist2(buf.str, buf.len));
654 if (unlikely(ret <= 0)) {
655 if (!ret) {
656 h2c->flags |= H2_CF_MUX_MFULL;
657 h2c->flags |= H2_CF_DEM_MROOM;
658 return 0;
659 }
660 else {
661 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
662 return 0;
663 }
664 }
665 return ret;
666}
667
Willy Tarreau52eed752017-09-22 15:05:09 +0200668/* Try to receive a connection preface, then upon success try to send our
669 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
670 * missing data. It may return an error in h2c.
671 */
672static int h2c_frt_recv_preface(struct h2c *h2c)
673{
674 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200675 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200676
677 ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE));
678
679 if (unlikely(ret1 <= 0)) {
680 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
681 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
682 return 0;
683 }
684
Willy Tarreaube5b7152017-09-25 16:25:39 +0200685 ret2 = h2c_snd_settings(h2c);
686 if (ret2 > 0)
687 bi_del(h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200688
Willy Tarreaube5b7152017-09-25 16:25:39 +0200689 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200690}
691
Willy Tarreau081d4722017-05-16 21:51:05 +0200692/* try to send a GOAWAY frame on the connection to report an error or a graceful
693 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
694 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
695 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
696 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
697 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
698 * on unrecoverable failure. It will not attempt to send one again in this last
699 * case so that it is safe to use h2c_error() to report such errors.
700 */
701static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
702{
703 struct buffer *res;
704 char str[17];
705 int ret;
706
707 if (h2c->flags & H2_CF_GOAWAY_FAILED)
708 return 1; // claim that it worked
709
710 if (h2c_mux_busy(h2c, h2s)) {
711 if (h2s)
712 h2s->flags |= H2_SF_BLK_MBUSY;
713 else
714 h2c->flags |= H2_CF_DEM_MBUSY;
715 return 0;
716 }
717
718 res = h2_get_mbuf(h2c);
719 if (!res) {
720 h2c->flags |= H2_CF_MUX_MALLOC;
721 if (h2s)
722 h2s->flags |= H2_SF_BLK_MROOM;
723 else
724 h2c->flags |= H2_CF_DEM_MROOM;
725 return 0;
726 }
727
728 /* len: 8, type: 7, flags: none, sid: 0 */
729 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
730
731 if (h2c->last_sid < 0)
732 h2c->last_sid = h2c->max_id;
733
734 write_n32(str + 9, h2c->last_sid);
735 write_n32(str + 13, h2c->errcode);
736 ret = bo_istput(res, ist2(str, 17));
737 if (unlikely(ret <= 0)) {
738 if (!ret) {
739 h2c->flags |= H2_CF_MUX_MFULL;
740 if (h2s)
741 h2s->flags |= H2_SF_BLK_MROOM;
742 else
743 h2c->flags |= H2_CF_DEM_MROOM;
744 return 0;
745 }
746 else {
747 /* we cannot report this error using GOAWAY, so we mark
748 * it and claim a success.
749 */
750 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
751 h2c->flags |= H2_CF_GOAWAY_FAILED;
752 return 1;
753 }
754 }
755 h2c->flags |= H2_CF_GOAWAY_SENT;
756 return ret;
757}
758
Willy Tarreau27a84c92017-10-17 08:10:17 +0200759/* try to send an RST_STREAM frame on the connection for the current demuxed
760 * stream to report an error, with h2s->errcode as the error code. Returns > 0
761 * on success or zero if nothing was done. It uses h2c->dsi as the stream ID
762 * and h2s->errcode for the error code. In case of lack of room to write the
763 * message, it subscribes the requester (either <h2s> or <h2c>) to future
764 * notifications. It's worth mentionning that an RST may even be sent for a
765 * closed stream with error 0 in this case.
766 */
767static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
768{
769 struct buffer *res;
770 char str[13];
771 int ret;
772
773 if (h2c_mux_busy(h2c, h2s)) {
774 if (h2s)
775 h2s->flags |= H2_SF_BLK_MBUSY;
776 else
777 h2c->flags |= H2_CF_DEM_MBUSY;
778 return 0;
779 }
780
781 res = h2_get_mbuf(h2c);
782 if (!res) {
783 h2c->flags |= H2_CF_MUX_MALLOC;
784 if (h2s)
785 h2s->flags |= H2_SF_BLK_MROOM;
786 else
787 h2c->flags |= H2_CF_DEM_MROOM;
788 return 0;
789 }
790
791 /* len: 4, type: 3, flags: none */
792 memcpy(str, "\x00\x00\x04\x03\x00", 5);
793 write_n32(str + 5, h2c->dsi);
794 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_RESET) ?
795 h2s->errcode : H2_ERR_STREAM_CLOSED);
796 ret = bo_istput(res, ist2(str, 13));
797 if (unlikely(ret <= 0)) {
798 if (!ret) {
799 h2c->flags |= H2_CF_MUX_MFULL;
800 if (h2s)
801 h2s->flags |= H2_SF_BLK_MROOM;
802 else
803 h2c->flags |= H2_CF_DEM_MROOM;
804 return 0;
805 }
806 else {
807 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
808 return 0;
809 }
810 }
811
812 if (h2s)
813 h2s->flags |= H2_SF_RST_SENT;
814 return ret;
815}
816
Willy Tarreau3421aba2017-07-27 15:41:03 +0200817/* Increase all streams' outgoing window size by the difference passed in
818 * argument. This is needed upon receipt of the settings frame if the initial
819 * window size is different. The difference may be negative and the resulting
820 * window size as well, for the time it takes to receive some window updates.
821 */
822static void h2c_update_all_ws(struct h2c *h2c, int diff)
823{
824 struct h2s *h2s;
825 struct eb32_node *node;
826
827 if (!diff)
828 return;
829
830 node = eb32_first(&h2c->streams_by_id);
831 while (node) {
832 h2s = container_of(node, struct h2s, by_id);
833 h2s->mws += diff;
834 node = eb32_next(node);
835 }
836}
837
838/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
839 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
840 * return an error in h2c. Described in RFC7540#6.5.
841 */
842static int h2c_handle_settings(struct h2c *h2c)
843{
844 unsigned int offset;
845 int error;
846
847 if (h2c->dff & H2_F_SETTINGS_ACK) {
848 if (h2c->dfl) {
849 error = H2_ERR_FRAME_SIZE_ERROR;
850 goto fail;
851 }
852 return 1;
853 }
854
855 if (h2c->dsi != 0) {
856 error = H2_ERR_PROTOCOL_ERROR;
857 goto fail;
858 }
859
860 if (h2c->dfl % 6) {
861 error = H2_ERR_FRAME_SIZE_ERROR;
862 goto fail;
863 }
864
865 /* that's the limit we can process */
866 if (h2c->dfl > global.tune.bufsize) {
867 error = H2_ERR_FRAME_SIZE_ERROR;
868 goto fail;
869 }
870
871 /* process full frame only */
872 if (h2c->dbuf->i < h2c->dfl)
873 return 0;
874
875 /* parse the frame */
876 for (offset = 0; offset < h2c->dfl; offset += 6) {
877 uint16_t type = h2_get_n16(h2c->dbuf, offset);
878 int32_t arg = h2_get_n32(h2c->dbuf, offset + 2);
879
880 switch (type) {
881 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
882 /* we need to update all existing streams with the
883 * difference from the previous iws.
884 */
885 if (arg < 0) { // RFC7540#6.5.2
886 error = H2_ERR_FLOW_CONTROL_ERROR;
887 goto fail;
888 }
889 h2c_update_all_ws(h2c, arg - h2c->miw);
890 h2c->miw = arg;
891 break;
892 case H2_SETTINGS_MAX_FRAME_SIZE:
893 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
894 error = H2_ERR_PROTOCOL_ERROR;
895 goto fail;
896 }
897 h2c->mfs = arg;
898 break;
899 }
900 }
901
902 /* need to ACK this frame now */
903 h2c->st0 = H2_CS_FRAME_A;
904 return 1;
905 fail:
906 h2c_error(h2c, error);
907 return 0;
908}
909
910/* try to send an ACK for a settings frame on the connection. Returns > 0 on
911 * success or one of the h2_status values.
912 */
913static int h2c_ack_settings(struct h2c *h2c)
914{
915 struct buffer *res;
916 char str[9];
917 int ret = -1;
918
919 if (h2c_mux_busy(h2c, NULL)) {
920 h2c->flags |= H2_CF_DEM_MBUSY;
921 return 0;
922 }
923
924 res = h2_get_mbuf(h2c);
925 if (!res) {
926 h2c->flags |= H2_CF_MUX_MALLOC;
927 h2c->flags |= H2_CF_DEM_MROOM;
928 return 0;
929 }
930
931 memcpy(str,
932 "\x00\x00\x00" /* length : 0 (no data) */
933 "\x04" "\x01" /* type : 4, flags : ACK */
934 "\x00\x00\x00\x00" /* stream ID */, 9);
935
936 ret = bo_istput(res, ist2(str, 9));
937 if (unlikely(ret <= 0)) {
938 if (!ret) {
939 h2c->flags |= H2_CF_MUX_MFULL;
940 h2c->flags |= H2_CF_DEM_MROOM;
941 return 0;
942 }
943 else {
944 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
945 return 0;
946 }
947 }
948 return ret;
949}
950
Willy Tarreaucf68c782017-10-10 17:11:41 +0200951/* processes a PING frame and schedules an ACK if needed. The caller must pass
952 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
953 * missing data. It may return an error in h2c.
954 */
955static int h2c_handle_ping(struct h2c *h2c)
956{
957 /* frame length must be exactly 8 */
958 if (h2c->dfl != 8) {
959 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
960 return 0;
961 }
962
963 /* schedule a response */
964 if (!(h2c->dft & H2_F_PING_ACK))
965 h2c->st0 = H2_CS_FRAME_A;
966 return 1;
967}
968
969/* try to send an ACK for a ping frame on the connection. Returns > 0 on
970 * success, 0 on missing data or one of the h2_status values.
971 */
972static int h2c_ack_ping(struct h2c *h2c)
973{
974 struct buffer *res;
975 char str[17];
976 int ret = -1;
977
978 if (h2c->dbuf->i < 8)
979 return 0;
980
981 if (h2c_mux_busy(h2c, NULL)) {
982 h2c->flags |= H2_CF_DEM_MBUSY;
983 return 0;
984 }
985
986 res = h2_get_mbuf(h2c);
987 if (!res) {
988 h2c->flags |= H2_CF_MUX_MALLOC;
989 h2c->flags |= H2_CF_DEM_MROOM;
990 return 0;
991 }
992
993 memcpy(str,
994 "\x00\x00\x08" /* length : 8 (same payload) */
995 "\x06" "\x01" /* type : 6, flags : ACK */
996 "\x00\x00\x00\x00" /* stream ID */, 9);
997
998 /* copy the original payload */
999 h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0);
1000
1001 ret = bo_istput(res, ist2(str, 17));
1002 if (unlikely(ret <= 0)) {
1003 if (!ret) {
1004 h2c->flags |= H2_CF_MUX_MFULL;
1005 h2c->flags |= H2_CF_DEM_MROOM;
1006 return 0;
1007 }
1008 else {
1009 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1010 return 0;
1011 }
1012 }
1013 return ret;
1014}
1015
Willy Tarreau26f95952017-07-27 17:18:30 +02001016/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1017 * Returns > 0 on success or zero on missing data. It may return an error in
1018 * h2c or h2s. Described in RFC7540#6.9.
1019 */
1020static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1021{
1022 int32_t inc;
1023 int error;
1024
1025 if (h2c->dfl != 4) {
1026 error = H2_ERR_FRAME_SIZE_ERROR;
1027 goto conn_err;
1028 }
1029
1030 /* process full frame only */
1031 if (h2c->dbuf->i < h2c->dfl)
1032 return 0;
1033
1034 inc = h2_get_n32(h2c->dbuf, 0);
1035
1036 if (h2c->dsi != 0) {
1037 /* stream window update */
1038 if (h2s->st == H2_SS_IDLE) {
1039 error = H2_ERR_PROTOCOL_ERROR;
1040 goto conn_err;
1041 }
1042
1043 /* it's not an error to receive WU on a closed stream */
1044 if (h2s->st == H2_SS_CLOSED)
1045 return 1;
1046
1047 if (!inc) {
1048 error = H2_ERR_PROTOCOL_ERROR;
1049 goto strm_err;
1050 }
1051
1052 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1053 error = H2_ERR_FLOW_CONTROL_ERROR;
1054 goto strm_err;
1055 }
1056
1057 h2s->mws += inc;
1058 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1059 h2s->flags &= ~H2_SF_BLK_SFCTL;
1060 if (h2s->cs && LIST_ISEMPTY(&h2s->list) &&
1061 (h2s->cs->flags & CS_FL_DATA_WR_ENA)) {
1062 /* This stream wanted to send but could not due to its
1063 * own flow control. We can put it back into the send
1064 * list now, it will be handled upon next send() call.
1065 */
1066 LIST_ADDQ(&h2c->send_list, &h2s->list);
1067 }
1068 }
1069 }
1070 else {
1071 /* connection window update */
1072 if (!inc) {
1073 error = H2_ERR_PROTOCOL_ERROR;
1074 goto conn_err;
1075 }
1076
1077 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1078 error = H2_ERR_FLOW_CONTROL_ERROR;
1079 goto conn_err;
1080 }
1081
1082 h2c->mws += inc;
1083 }
1084
1085 return 1;
1086
1087 conn_err:
1088 h2c_error(h2c, error);
1089 return 0;
1090
1091 strm_err:
1092 if (h2s) {
1093 h2s_error(h2s, error);
1094 h2c->st0 = H2_CS_FRAME_A;
1095 }
1096 else
1097 h2c_error(h2c, error);
1098 return 0;
1099}
1100
Willy Tarreau13278b42017-10-13 19:23:14 +02001101/* processes a HEADERS frame. Returns > 0 on success or zero on missing data.
1102 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1103 * errors here are reported as connection errors since it's impossible to
1104 * recover from such errors after the compression context has been altered.
1105 */
1106static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
1107{
1108 int error;
1109
1110 if (!h2c->dfl) {
1111 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1112 goto strm_err;
1113 }
1114
1115 if (!h2c->dbuf->size)
1116 return 0; // empty buffer
1117
1118 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1119 return 0; // incomplete frame
1120
1121 /* now either the frame is complete or the buffer is complete */
1122 if (h2s->st != H2_SS_IDLE) {
1123 /* FIXME: stream already exists, this is only allowed for
1124 * trailers (not supported for now).
1125 */
1126 error = H2_ERR_PROTOCOL_ERROR;
1127 goto conn_err;
1128 }
1129 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1130 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1131 error = H2_ERR_PROTOCOL_ERROR;
1132 goto conn_err;
1133 }
1134
1135 h2s = h2c_stream_new(h2c, h2c->dsi);
1136 if (!h2s) {
1137 error = H2_ERR_INTERNAL_ERROR;
1138 goto conn_err;
1139 }
1140
1141 h2s->st = H2_SS_OPEN;
1142 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1143 h2s->st = H2_SS_HREM;
1144 h2s->flags |= H2_SF_ES_RCVD;
1145 }
1146
1147 /* call the upper layers to process the frame, then let the upper layer
1148 * notify the stream about any change.
1149 */
1150 h2s->cs->data_cb->recv(h2s->cs);
1151
1152 if (h2s->cs->data_cb->wake(h2s->cs) < 0) {
1153 /* FIXME: cs has already been destroyed, but we have to kill h2s. */
1154 error = H2_ERR_INTERNAL_ERROR;
1155 goto conn_err;
1156 }
1157
1158 if (h2s->st >= H2_SS_RESET) {
1159 /* stream error : send RST_STREAM */
1160 h2c->st0 = H2_CS_FRAME_A;
1161 }
1162 else {
1163 /* update the max stream ID if the request is being processed */
1164 if (h2s->id > h2c->max_id)
1165 h2c->max_id = h2s->id;
1166 }
1167
1168 return 1;
1169
1170 conn_err:
1171 h2c_error(h2c, error);
1172 return 0;
1173
1174 strm_err:
1175 if (h2s) {
1176 h2s_error(h2s, error);
1177 h2c->st0 = H2_CS_FRAME_A;
1178 }
1179 else
1180 h2c_error(h2c, error);
1181 return 0;
1182}
1183
Willy Tarreaubc933932017-10-09 16:21:43 +02001184/* process Rx frames to be demultiplexed */
1185static void h2_process_demux(struct h2c *h2c)
1186{
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001187 struct h2s *h2s;
1188
Willy Tarreau081d4722017-05-16 21:51:05 +02001189 if (h2c->st0 >= H2_CS_ERROR)
1190 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001191
1192 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1193 if (h2c->st0 == H2_CS_PREFACE) {
1194 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1195 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1196 if (h2c->st0 == H2_CS_ERROR)
1197 h2c->st0 = H2_CS_ERROR2;
1198 goto fail;
1199 }
1200
1201 h2c->max_id = 0;
1202 h2c->st0 = H2_CS_SETTINGS1;
1203 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001204
1205 if (h2c->st0 == H2_CS_SETTINGS1) {
1206 struct h2_fh hdr;
1207
1208 /* ensure that what is pending is a valid SETTINGS frame
1209 * without an ACK.
1210 */
1211 if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) {
1212 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1213 if (h2c->st0 == H2_CS_ERROR)
1214 h2c->st0 = H2_CS_ERROR2;
1215 goto fail;
1216 }
1217
1218 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1219 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1220 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1221 h2c->st0 = H2_CS_ERROR2;
1222 goto fail;
1223 }
1224
1225 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1226 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1227 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1228 h2c->st0 = H2_CS_ERROR2;
1229 goto fail;
1230 }
1231
1232 /* that's OK, switch to FRAME_P to process it */
1233 h2c->dfl = hdr.len;
1234 h2c->dsi = hdr.sid;
1235 h2c->dft = hdr.ft;
1236 h2c->dff = hdr.ff;
1237 h2c->st0 = H2_CS_FRAME_P;
1238 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001239 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001240
1241 /* process as many incoming frames as possible below */
1242 while (h2c->dbuf->i) {
1243 int ret = 0;
1244
1245 if (h2c->st0 >= H2_CS_ERROR)
1246 break;
1247
1248 if (h2c->st0 == H2_CS_FRAME_H) {
1249 struct h2_fh hdr;
1250
1251 if (!h2_peek_frame_hdr(h2c->dbuf, &hdr))
1252 break;
1253
1254 if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) {
1255 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1256 h2c->st0 = H2_CS_ERROR;
1257 break;
1258 }
1259
1260 h2c->dfl = hdr.len;
1261 h2c->dsi = hdr.sid;
1262 h2c->dft = hdr.ft;
1263 h2c->dff = hdr.ff;
1264 h2c->st0 = H2_CS_FRAME_P;
1265 h2_skip_frame_hdr(h2c->dbuf);
1266 }
1267
1268 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001269 h2s = h2c_st_by_id(h2c, h2c->dsi);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001270
1271 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001272 case H2_FT_SETTINGS:
1273 if (h2c->st0 == H2_CS_FRAME_P)
1274 ret = h2c_handle_settings(h2c);
1275
1276 if (h2c->st0 == H2_CS_FRAME_A)
1277 ret = h2c_ack_settings(h2c);
1278 break;
1279
Willy Tarreaucf68c782017-10-10 17:11:41 +02001280 case H2_FT_PING:
1281 if (h2c->st0 == H2_CS_FRAME_P)
1282 ret = h2c_handle_ping(h2c);
1283
1284 if (h2c->st0 == H2_CS_FRAME_A)
1285 ret = h2c_ack_ping(h2c);
1286 break;
1287
Willy Tarreau26f95952017-07-27 17:18:30 +02001288 case H2_FT_WINDOW_UPDATE:
1289 if (h2c->st0 == H2_CS_FRAME_P)
1290 ret = h2c_handle_window_update(h2c, h2s);
1291 break;
1292
Willy Tarreau61290ec2017-10-17 08:19:21 +02001293 case H2_FT_CONTINUATION:
1294 /* we currently don't support CONTINUATION frames since
1295 * we have nowhere to store the partial HEADERS frame.
1296 * Let's abort the stream on an INTERNAL_ERROR here.
1297 */
1298 if (h2c->st0 == H2_CS_FRAME_P)
1299 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1300 break;
1301
Willy Tarreau13278b42017-10-13 19:23:14 +02001302 case H2_FT_HEADERS:
1303 if (h2c->st0 == H2_CS_FRAME_P)
1304 ret = h2c_frt_handle_headers(h2c, h2s);
1305 break;
1306
Willy Tarreau7e98c052017-10-10 15:56:59 +02001307 /* FIXME: implement all supported frame types here */
1308 default:
1309 /* drop frames that we ignore. They may be larger than
1310 * the buffer so we drain all of their contents until
1311 * we reach the end.
1312 */
1313 ret = MIN(h2c->dbuf->i, h2c->dfl);
1314 bi_del(h2c->dbuf, ret);
1315 h2c->dfl -= ret;
1316 ret = h2c->dfl == 0;
1317 }
1318
Willy Tarreau27a84c92017-10-17 08:10:17 +02001319 /* RST are sent similarly to frame acks */
1320 if (h2s->st == H2_SS_ERROR) {
1321 if (h2c->st0 == H2_CS_FRAME_P)
1322 h2c->st0 = H2_CS_FRAME_A;
1323
1324 if (h2c->st0 == H2_CS_FRAME_A)
1325 ret = h2c_send_rst_stream(h2c, h2s);
1326 }
1327
Willy Tarreau7e98c052017-10-10 15:56:59 +02001328 /* error or missing data condition met above ? */
1329 if (ret <= 0)
1330 break;
1331
1332 if (h2c->st0 != H2_CS_FRAME_H) {
1333 bi_del(h2c->dbuf, h2c->dfl);
1334 h2c->st0 = H2_CS_FRAME_H;
1335 }
1336 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001337
1338 fail:
1339 /* we can go here on missing data, blocked response or error */
1340 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02001341}
1342
1343/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
1344 * the end.
1345 */
1346static int h2_process_mux(struct h2c *h2c)
1347{
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001348 struct h2s *h2s, *h2s_back;
1349
1350 /* First we always process the flow control list because the streams
1351 * waiting there were already elected for immediate emission but were
1352 * blocked just on this.
1353 */
1354
1355 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
1356 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
1357 h2c->st0 >= H2_CS_ERROR)
1358 break;
1359
1360 /* In theory it's possible that h2s->cs == NULL here :
1361 * - client sends crap that causes a parse error
1362 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1363 * - RST_STREAM cannot be emitted because mux is busy/full
1364 * - stream gets notified, detaches and quits
1365 * - mux buffer gets ready and wakes pending streams up
1366 * - bam!
1367 */
1368 h2s->flags &= ~H2_SF_BLK_ANY;
1369
1370 if (h2s->cs) {
1371 h2s->cs->data_cb->send(h2s->cs);
1372 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001373 } else {
1374 h2c_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001375 }
1376
1377 /* depending on callee's blocking reasons, we may queue in send
1378 * list or completely dequeue.
1379 */
1380 if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) {
1381 if (h2s->flags & H2_SF_BLK_ANY) {
1382 LIST_DEL(&h2s->list);
1383 LIST_ADDQ(&h2c->send_list, &h2s->list);
1384 }
1385 else {
1386 LIST_DEL(&h2s->list);
1387 LIST_INIT(&h2s->list);
1388 if (h2s->cs)
1389 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
1390 }
1391 }
1392 }
1393
1394 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
1395 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
1396 break;
1397
1398 /* In theory it's possible that h2s->cs == NULL here :
1399 * - client sends crap that causes a parse error
1400 * - RST_STREAM is produced and CS_FL_ERROR at the same time
1401 * - RST_STREAM cannot be emitted because mux is busy/full
1402 * - stream gets notified, detaches and quits
1403 * - mux buffer gets ready and wakes pending streams up
1404 * - bam!
1405 */
1406 h2s->flags &= ~H2_SF_BLK_ANY;
1407
1408 if (h2s->cs) {
1409 h2s->cs->data_cb->send(h2s->cs);
1410 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001411 } else {
1412 h2c_send_rst_stream(h2c, h2s);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001413 }
1414 /* depending on callee's blocking reasons, we may queue in fctl
1415 * list or completely dequeue.
1416 */
1417 if (h2s->flags & H2_SF_BLK_MFCTL) {
1418 /* stream hit the connection's flow control */
1419 LIST_DEL(&h2s->list);
1420 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
1421 }
1422 else if (!(h2s->flags & H2_SF_BLK_ANY)) {
1423 LIST_DEL(&h2s->list);
1424 LIST_INIT(&h2s->list);
1425 if (h2s->cs)
1426 h2s->cs->flags &= ~CS_FL_DATA_WR_ENA;
1427 }
1428 }
1429
Willy Tarreau081d4722017-05-16 21:51:05 +02001430 if (unlikely(h2c->st0 > H2_CS_ERROR)) {
1431 if (h2c->st0 == H2_CS_ERROR) {
1432 if (h2c->max_id >= 0) {
1433 h2c_send_goaway_error(h2c, NULL);
1434 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
1435 return 0;
1436 }
1437
1438 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
1439 }
1440 return 1;
1441 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02001442 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02001443}
1444
Willy Tarreau71681172017-10-23 14:39:06 +02001445
Willy Tarreau62f52692017-10-08 23:01:42 +02001446/*********************************************************/
1447/* functions below are I/O callbacks from the connection */
1448/*********************************************************/
1449
1450/* callback called on recv event by the connection handler */
1451static void h2_recv(struct connection *conn)
1452{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001453 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001454 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001455 int max;
1456
1457 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001458 return;
1459
1460 if (h2c->flags & H2_CF_DEM_BLOCK_ANY)
1461 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001462
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001463 buf = h2_get_dbuf(h2c);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001464 if (!buf) {
1465 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001466 return;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02001467 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001468
Willy Tarreaua2af5122017-10-09 11:56:46 +02001469 /* note: buf->o == 0 */
1470 max = buf->size - buf->i;
1471 if (!max) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001472 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001473 return;
1474 }
1475
1476 conn->xprt->rcv_buf(conn, buf, max);
1477 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001478 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001479
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001480 if (!buf->i) {
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02001481 h2_release_dbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02001482 return;
1483 }
1484
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001485 if (buf->i == buf->size)
1486 h2c->flags |= H2_CF_DEM_DFULL;
1487
Willy Tarreaubc933932017-10-09 16:21:43 +02001488 h2_process_demux(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02001489
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001490 /* after streams have been processed, we should have made some room */
Willy Tarreau081d4722017-05-16 21:51:05 +02001491 if (h2c->st0 >= H2_CS_ERROR)
1492 buf->i = 0;
1493
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001494 if (buf->i != buf->size)
1495 h2c->flags &= ~H2_CF_DEM_DFULL;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001496 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02001497}
1498
1499/* callback called on send event by the connection handler */
1500static void h2_send(struct connection *conn)
1501{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001502 struct h2c *h2c = conn->mux_ctx;
Willy Tarreaubc933932017-10-09 16:21:43 +02001503 int done;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001504
1505 if (conn->flags & CO_FL_ERROR)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001506 return;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001507
1508 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
1509 /* a handshake was requested */
1510 return;
1511 }
1512
Willy Tarreaubc933932017-10-09 16:21:43 +02001513 /* This loop is quite simple : it tries to fill as much as it can from
1514 * pending streams into the existing buffer until it's reportedly full
1515 * or the end of send requests is reached. Then it tries to send this
1516 * buffer's contents out, marks it not full if at least one byte could
1517 * be sent, and tries again.
1518 *
1519 * The snd_buf() function normally takes a "flags" argument which may
1520 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
1521 * data immediately comes and CO_SFL_STREAMER to indicate that the
1522 * connection is streaming lots of data (used to increase TLS record
1523 * size at the expense of latency). The former can be sent any time
1524 * there's a buffer full flag, as it indicates at least one stream
1525 * attempted to send and failed so there are pending data. An
1526 * alternative would be to set it as long as there's an active stream
1527 * but that would be problematic for ACKs until we have an absolute
1528 * guarantee that all waiters have at least one byte to send. The
1529 * latter should possibly not be set for now.
1530 */
1531
1532 done = 0;
1533 while (!done) {
1534 unsigned int flags = 0;
1535
1536 /* fill as much as we can into the current buffer */
1537 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
1538 done = h2_process_mux(h2c);
1539
1540 if (conn->flags & CO_FL_ERROR)
1541 break;
1542
1543 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
1544 flags |= CO_SFL_MSG_MORE;
1545
1546 if (conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0)
1547 break;
1548
1549 /* wrote at least one byte, the buffer is not full anymore */
1550 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
1551 }
1552
Willy Tarreaua2af5122017-10-09 11:56:46 +02001553 if (conn->flags & CO_FL_SOCK_WR_SH) {
1554 /* output closed, nothing to send, clear the buffer to release it */
1555 h2c->mbuf->o = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001556 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001557}
Willy Tarreaua2af5122017-10-09 11:56:46 +02001558
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001559/* call the wake up function of all streams attached to the connection */
1560static void h2_wake_all_streams(struct h2c *h2c)
1561{
1562 struct eb32_node *node;
1563 struct h2s *h2s;
1564 unsigned int flags = 0;
Willy Tarreau14398122017-09-22 14:26:04 +02001565
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001566 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1567 flags |= CS_FL_ERROR;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001568
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001569 if (conn_xprt_read0_pending(h2c->conn))
1570 flags |= CS_FL_EOS;
Willy Tarreaua2af5122017-10-09 11:56:46 +02001571
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001572 node = eb32_first(&h2c->streams_by_id);
1573 while (node) {
1574 h2s = container_of(node, struct h2s, by_id);
1575 node = eb32_next(node);
1576 if (h2s->cs) {
1577 h2s->cs->flags |= flags;
1578 /* recv is used to force to detect CS_FL_EOS that wake()
1579 * doesn't handle in the stream int code.
1580 */
1581 h2s->cs->data_cb->recv(h2s->cs);
1582 h2s->cs->data_cb->wake(h2s->cs);
1583 }
1584 }
Willy Tarreau62f52692017-10-08 23:01:42 +02001585}
1586
1587/* callback called on any event by the connection handler.
1588 * It applies changes and returns zero, or < 0 if it wants immediate
1589 * destruction of the connection (which normally doesn not happen in h2).
1590 */
1591static int h2_wake(struct connection *conn)
1592{
Willy Tarreaua2af5122017-10-09 11:56:46 +02001593 struct h2c *h2c = conn->mux_ctx;
1594
Willy Tarreau26bd7612017-10-09 16:47:04 +02001595 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01001596 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
1597 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
1598 h2c->max_id >= h2c->last_sid)) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001599 h2_wake_all_streams(h2c);
1600
1601 if (eb_is_empty(&h2c->streams_by_id)) {
1602 /* no more stream, kill the connection now */
1603 h2_release(conn);
1604 return -1;
1605 }
1606 else {
1607 /* some streams still there, we need to signal them all and
1608 * wait for their departure.
1609 */
1610 __conn_xprt_stop_recv(conn);
1611 __conn_xprt_stop_send(conn);
1612 return 0;
1613 }
1614 }
1615
1616 if (!h2c->dbuf->i)
1617 h2_release_dbuf(h2c);
1618
1619 /* stop being notified of incoming data if we can't process them */
1620 if (h2c->st0 >= H2_CS_ERROR ||
1621 (h2c->flags & H2_CF_DEM_BLOCK_ANY) || conn_xprt_read0_pending(conn)) {
1622 /* FIXME: we should clear a read timeout here */
1623 __conn_xprt_stop_recv(conn);
1624 }
1625 else {
1626 /* FIXME: we should (re-)arm a read timeout here */
1627 __conn_xprt_want_recv(conn);
1628 }
1629
1630 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02001631 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
1632 (h2c->st0 == H2_CS_ERROR ||
1633 h2c->mbuf->o ||
1634 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
1635 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02001636 /* FIXME: we should (re-)arm a send timeout here */
1637 __conn_xprt_want_send(conn);
1638 }
1639 else {
1640 /* FIXME: we should clear a send timeout here */
1641 h2_release_mbuf(h2c);
1642 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02001643 }
1644
Willy Tarreau62f52692017-10-08 23:01:42 +02001645 return 0;
1646}
1647
1648/*******************************************/
1649/* functions below are used by the streams */
1650/*******************************************/
1651
1652/*
1653 * Attach a new stream to a connection
1654 * (Used for outgoing connections)
1655 */
1656static struct conn_stream *h2_attach(struct connection *conn)
1657{
1658 return NULL;
1659}
1660
1661/* callback used to update the mux's polling flags after changing a cs' status.
1662 * The caller (cs_update_mux_polling) will take care of propagating any changes
1663 * to the transport layer.
1664 */
1665static void h2_update_poll(struct conn_stream *cs)
1666{
Willy Tarreau1d393222017-10-17 10:26:19 +02001667 struct h2s *h2s = cs->ctx;
1668
1669 if (!h2s)
1670 return;
1671
Willy Tarreaud7739c82017-10-30 15:38:23 +01001672 /* we may unblock a blocked read */
1673
1674 if (cs->flags & CS_FL_DATA_RD_ENA &&
1675 h2s->h2c->flags & H2_CF_DEM_SFULL && h2s->h2c->dsi == h2s->id) {
1676 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
1677 conn_xprt_want_recv(cs->conn);
1678 }
1679
Willy Tarreau1d393222017-10-17 10:26:19 +02001680 /* Note: the stream and stream-int code doesn't allow us to perform a
1681 * synchronous send() here unfortunately, because this code is called
1682 * as si_update() from the process_stream() context. This means that
1683 * we have to queue the current cs and defer its processing after the
1684 * connection's cs list is processed anyway.
1685 */
1686
1687 if (cs->flags & CS_FL_DATA_WR_ENA) {
1688 if (LIST_ISEMPTY(&h2s->list)) {
1689 if (LIST_ISEMPTY(&h2s->h2c->send_list) &&
1690 !h2s->h2c->mbuf->o && // not yet subscribed
1691 !(cs->conn->flags & CO_FL_SOCK_WR_SH))
1692 conn_xprt_want_send(cs->conn);
1693 LIST_ADDQ(&h2s->h2c->send_list, &h2s->list);
1694 }
1695 }
1696 else if (!LIST_ISEMPTY(&h2s->list)) {
1697 LIST_DEL(&h2s->list);
1698 LIST_INIT(&h2s->list);
1699 h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL);
1700 }
1701
1702 /* this can happen from within si_chk_snd() */
1703 if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
1704 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02001705}
1706
1707/*
1708 * Detach the stream from the connection and possibly release the connection.
1709 */
1710static void h2_detach(struct conn_stream *cs)
1711{
Willy Tarreau60935142017-10-16 18:11:19 +02001712 struct h2s *h2s = cs->ctx;
1713 struct h2c *h2c;
1714
1715 cs->ctx = NULL;
1716 if (!h2s)
1717 return;
1718
1719 h2c = h2s->h2c;
1720 h2s->cs = NULL;
1721
Willy Tarreau45f752e2017-10-30 15:44:59 +01001722 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
1723 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
1724 /* unblock the connection if it was blocked on this
1725 * stream.
1726 */
1727 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
1728 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
1729 conn_xprt_want_recv(cs->conn);
1730 conn_xprt_want_send(cs->conn);
1731 }
1732
Willy Tarreau60935142017-10-16 18:11:19 +02001733 if (h2s->by_id.node.leaf_p) {
1734 /* h2s still attached to the h2c */
1735 eb32_delete(&h2s->by_id);
1736
1737 /* We don't want to close right now unless we're removing the
1738 * last stream, and either the connection is in error, or it
1739 * reached the ID already specified in a GOAWAY frame received
1740 * or sent (as seen by last_sid >= 0). A timer should be armed
1741 * to kill the connection after some idle time though.
1742 */
1743 if (eb_is_empty(&h2c->streams_by_id) &&
1744 (conn_xprt_read0_pending(h2c->conn) ||
1745 (h2c->conn->flags & CO_FL_ERROR) ||
1746 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
1747 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))) {
1748 /* no more stream will come, kill it now */
1749 h2_release(h2c->conn);
1750 }
1751 }
1752 pool_free2(pool2_h2s, h2s);
Willy Tarreau62f52692017-10-08 23:01:42 +02001753}
1754
1755static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
1756{
1757}
1758
1759static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
1760{
1761}
1762
Willy Tarreau13278b42017-10-13 19:23:14 +02001763/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
1764 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
1765 * proceed. Stream errors are reported in h2s->errcode and connection errors
1766 * in h2c->errcode. The caller must already have checked the frame header and
1767 * ensured that the frame was complete or the buffer full.
1768 */
1769static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count)
1770{
1771 struct h2c *h2c = h2s->h2c;
1772 const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p;
Willy Tarreau68dd9852017-07-03 14:44:26 +02001773 struct chunk *copy = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001774 int flen = h2c->dfl;
1775 int outlen = 0;
1776 int wrap;
1777 int try;
1778
1779 if (!h2c->dfl) {
1780 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
1781 return 0;
1782 }
1783
1784 /* if the input buffer wraps, take a temporary copy of it (rare) */
1785 wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p;
1786 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02001787 copy = alloc_trash_chunk();
1788 if (!copy) {
1789 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1790 goto fail;
1791 }
1792 memcpy(copy->str, h2c->dbuf->p, wrap);
1793 memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap);
1794 hdrs = (uint8_t *)copy->str;
Willy Tarreau13278b42017-10-13 19:23:14 +02001795 }
1796
1797 /* The padlen is the first byte before data, and the padding appears
1798 * after data. padlen+data+padding are included in flen.
1799 */
1800 if (h2c->dff & H2_F_HEADERS_PADDED) {
1801 if (*hdrs >= flen) {
1802 /* RFC7540#6.2 : pad length = length of frame payload or greater */
1803 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1804 h2c->st0 = H2_SS_ERROR;
1805 return 0;
1806 }
1807 flen -= *hdrs + 1;
1808 hdrs += 1; // skip Pad Length
1809 }
1810
1811 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
1812 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
1813 hdrs += 5; // stream dep = 4, weight = 1
1814 flen -= 5;
1815 }
1816
1817 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
1818 * don't support this for now and can't even decompress so we have to
1819 * break the connection.
1820 */
1821 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
1822 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02001823 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02001824 }
1825
1826 do {
1827 /* first check if we have some room after p+i */
1828 try = buf->data + buf->size - (buf->p + buf->i);
1829
1830 /* otherwise continue between data and p-o */
1831 if (try <= 0) {
1832 try = buf->p - (buf->data + buf->o);
1833 if (try <= 0)
Willy Tarreau68dd9852017-07-03 14:44:26 +02001834 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02001835 }
1836 if (try > count)
1837 try = count;
1838
1839 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, bi_end(buf), try);
1840 if (outlen == -HPACK_ERR_TOO_LARGE) {
1841 if (buffer_space_wraps(buf)) {
1842 /* it doesn't fit and the buffer is fragmented,
1843 * so let's defragment it and try again.
1844 */
1845 buffer_slow_realign(buf);
1846 }
1847 else if (buf->o) {
1848 /* need to let the output buffer flush and
1849 * mark the buffer for later wake up.
1850 */
Willy Tarreau68dd9852017-07-03 14:44:26 +02001851 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02001852 }
1853 else {
1854 /* no other way around */
1855 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02001856 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02001857 }
1858 }
1859 else if (outlen < 0) {
1860 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02001861 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02001862 }
1863 } while (outlen < 0);
1864
1865 /* now consume the input data */
1866 bi_del(h2c->dbuf, h2c->dfl);
1867 h2c->st0 = H2_CS_FRAME_H;
1868 buf->i += outlen;
1869
1870 /* don't send it before returning data!
1871 * FIXME: should we instead try to send it much later, after the
1872 * response ? This would require that we keep a copy of it in h2s.
1873 */
1874 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1875 h2s->cs->flags |= CS_FL_EOS;
1876 h2s->flags |= H2_SF_ES_RCVD;
1877 }
1878
Willy Tarreau68dd9852017-07-03 14:44:26 +02001879 leave:
1880 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02001881 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02001882 fail:
1883 outlen = 0;
1884 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02001885}
1886
Willy Tarreau62f52692017-10-08 23:01:42 +02001887/*
Willy Tarreau13278b42017-10-13 19:23:14 +02001888 * Called from the upper layer to get more data, up to <count> bytes. The
1889 * caller is responsible for never asking for more data than what is available
1890 * in the buffer.
Willy Tarreau62f52692017-10-08 23:01:42 +02001891 */
1892static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count)
1893{
Willy Tarreau13278b42017-10-13 19:23:14 +02001894 struct h2s *h2s = cs->ctx;
1895 struct h2c *h2c = h2s->h2c;
1896 int ret = 0;
1897
1898 if (h2c->st0 != H2_CS_FRAME_P)
1899 return 0; // no pre-parsed frame yet
1900
1901 if (h2c->dsi != h2s->id)
1902 return 0; // not for us
1903
1904 if (!h2c->dbuf->size)
1905 return 0; // empty buffer
1906
1907 if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size)
1908 return 0; // incomplete input frame
1909
1910 switch (h2c->dft) {
1911 case H2_FT_HEADERS:
1912 ret = h2_frt_decode_headers(h2s, buf, count);
1913 break;
1914
1915 default:
1916 ret = 0;
1917 }
1918 return ret;
Willy Tarreau62f52692017-10-08 23:01:42 +02001919}
1920
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02001921/* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf>
1922 * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of
1923 * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds
1924 * to the number of buffer bytes consumed.
1925 */
1926static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf)
1927{
1928 struct http_hdr list[MAX_HTTP_HDR];
1929 struct h2c *h2c = h2s->h2c;
1930 struct h1m *h1m = &h2s->res;
1931 struct chunk outbuf;
1932 int es_now = 0;
1933 int ret = 0;
1934 int hdr;
1935
1936 if (h2c_mux_busy(h2c, h2s)) {
1937 h2s->flags |= H2_SF_BLK_MBUSY;
1938 return 0;
1939 }
1940
1941 if (!h2_get_mbuf(h2c)) {
1942 h2c->flags |= H2_CF_MUX_MALLOC;
1943 h2s->flags |= H2_SF_BLK_MROOM;
1944 return 0;
1945 }
1946
1947 /* First, try to parse the H1 response and index it into <list>.
1948 * NOTE! Since it comes from haproxy, we *know* that a response header
1949 * block does not wrap and we can safely read it this way without
1950 * having to realign the buffer.
1951 */
1952 ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o,
1953 list, sizeof(list)/sizeof(list[0]), h1m);
1954 if (ret <= 0) {
1955 if (!ret)
1956 goto end; // missing input
1957
1958 /* Impossible to index the response.
1959 * FIXME: we should instead add the ability to only return a
1960 * 502 bad gateway. But in theory this is not supposed to
1961 * happen.
1962 */
1963 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1964 ret = 0;
1965 goto end;
1966 }
1967
1968 chunk_reset(&outbuf);
1969
1970 while (1) {
1971 outbuf.str = bo_end(h2c->mbuf);
1972 outbuf.size = bo_contig_space(h2c->mbuf);
1973 outbuf.len = 0;
1974
1975 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
1976 break;
1977 realign_again:
1978 buffer_slow_realign(h2c->mbuf);
1979 }
1980
1981 if (outbuf.size < 9) {
1982 h2c->flags |= H2_CF_MUX_MFULL;
1983 h2s->flags |= H2_SF_BLK_MROOM;
1984 ret = 0;
1985 goto end;
1986 }
1987
1988 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
1989 memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5);
1990 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
1991 outbuf.len = 9;
1992
1993 /* encode status, which necessarily is the first one */
1994 if (outbuf.len < outbuf.size && h1m->status == 200)
1995 outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200")
1996 else if (outbuf.len < outbuf.size && h1m->status == 304)
1997 outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304")
1998 else if (list[0].v.len == 3 && outbuf.len + 2 + 3 <= outbuf.size) {
1999 /* basic encoding of the status code */
2000 outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8)
2001 outbuf.str[outbuf.len++] = 0x03; // 3 bytes status
2002 outbuf.str[outbuf.len++] = list[0].v.ptr[0];
2003 outbuf.str[outbuf.len++] = list[0].v.ptr[1];
2004 outbuf.str[outbuf.len++] = list[0].v.ptr[2];
2005 }
2006 else {
2007 if (buffer_space_wraps(h2c->mbuf))
2008 goto realign_again;
2009
2010 h2c->flags |= H2_CF_MUX_MFULL;
2011 h2s->flags |= H2_SF_BLK_MROOM;
2012 ret = 0;
2013 goto end;
2014 }
2015
2016 /* encode all headers, stop at empty name */
2017 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
2018 /* these ones do not exist in H2 and must be dropped */
2019 if (isteq(list[hdr].n, ist("connection")) ||
2020 isteq(list[hdr].n, ist("proxy-connection")) ||
2021 isteq(list[hdr].n, ist("keep-alive")) ||
2022 isteq(list[hdr].n, ist("upgrade")) ||
2023 isteq(list[hdr].n, ist("transfer-encoding")))
2024 continue;
2025
2026 if (isteq(list[hdr].n, ist("")))
2027 break; // end
2028
2029 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
2030 /* output full */
2031 if (buffer_space_wraps(h2c->mbuf))
2032 goto realign_again;
2033
2034 h2c->flags |= H2_CF_MUX_MFULL;
2035 h2s->flags |= H2_SF_BLK_MROOM;
2036 ret = 0;
2037 goto end;
2038 }
2039 }
2040
2041 /* we may need to add END_STREAM */
2042 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
2043 es_now = 1;
2044
2045 /* update the frame's size */
2046 h2_set_frame_size(outbuf.str, outbuf.len - 9);
2047
2048 if (es_now)
2049 outbuf.str[4] |= H2_F_HEADERS_END_STREAM;
2050
2051 /* consume incoming H1 response */
2052 bo_del(buf, ret);
2053
2054 /* commit the H2 response */
2055 h2c->mbuf->o += outbuf.len;
2056 h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len);
2057 h2c->flags |= H2_CF_HEADERS_SENT;
2058
2059 /* for now we don't implemented CONTINUATION, so we wait for a
2060 * body or directly end in TRL2.
2061 */
2062 if (es_now) {
2063 h1m->state = HTTP_MSG_DONE;
2064 h2s->flags |= H2_SF_ES_SENT;
2065 if (h2s->st == H2_SS_OPEN)
2066 h2s->st = H2_SS_HLOC;
2067 else
2068 h2s->st = H2_SS_CLOSED;
2069 }
2070 else
2071 h1m->state = (h1m->flags & H1_MF_CLEN) ? HTTP_MSG_BODY : HTTP_MSG_CHUNK_SIZE;
2072
2073 end:
2074 //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));
2075 return ret;
2076}
2077
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002078/* Try to send a DATA frame matching HTTP/1 response present in the response
2079 * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error
2080 * (one of the H2_ERR* or h2_status codes), >0 on success in which case it
2081 * corresponds to the number of buffer bytes consumed.
2082 */
2083static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf)
2084{
2085 struct h2c *h2c = h2s->h2c;
2086 struct h1m *h1m = &h2s->res;
2087 struct chunk outbuf;
2088 int ret = 0;
2089 int total = 0;
2090 int es_now = 0;
2091 int size = 0;
2092 char *blk1, *blk2;
2093 int len1, len2;
2094
2095 if (h2c_mux_busy(h2c, h2s)) {
2096 h2s->flags |= H2_SF_BLK_MBUSY;
2097 goto end;
2098 }
2099
2100 if (!h2_get_mbuf(h2c)) {
2101 h2c->flags |= H2_CF_MUX_MALLOC;
2102 h2s->flags |= H2_SF_BLK_MROOM;
2103 goto end;
2104 }
2105
2106 new_frame:
2107 if (!buf->o)
2108 goto end;
2109
2110 chunk_reset(&outbuf);
2111
2112 while (1) {
2113 outbuf.str = bo_end(h2c->mbuf);
2114 outbuf.size = bo_contig_space(h2c->mbuf);
2115 outbuf.len = 0;
2116
2117 if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf))
2118 break;
2119 realign_again:
2120 buffer_slow_realign(h2c->mbuf);
2121 }
2122
2123 if (outbuf.size < 9) {
2124 h2c->flags |= H2_CF_MUX_MFULL;
2125 h2s->flags |= H2_SF_BLK_MROOM;
2126 goto end;
2127 }
2128
2129 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
2130 memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5);
2131 write_n32(outbuf.str + 5, h2s->id); // 4 bytes
2132 outbuf.len = 9;
2133
2134 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
2135 case 0: /* no content length, read till SHUTW */
2136 size = buf->o;
2137 break;
2138 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
2139 size = buf->o;
2140 if ((long long)size > h1m->curr_len)
2141 size = h1m->curr_len;
2142 break;
2143 default: /* te:chunked : parse chunks */
2144 if (h1m->state == HTTP_MSG_CHUNK_CRLF) {
2145 ret = h1_skip_chunk_crlf(buf, -buf->o, 0);
2146 if (!ret)
2147 goto end;
2148
2149 if (ret < 0) {
2150 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2151 h1m->err_pos = ret;
2152 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2153 goto end;
2154 }
2155 bo_del(buf, ret);
2156 total += ret;
2157 h1m->state = HTTP_MSG_CHUNK_SIZE;
2158 }
2159
2160 if (h1m->state == HTTP_MSG_CHUNK_SIZE) {
2161 unsigned int chunk;
2162
2163 ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk);
2164 if (!ret)
2165 goto end;
2166
2167 if (ret < 0) {
2168 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
2169 h1m->err_pos = ret;
2170 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2171 goto end;
2172 }
2173
2174 size = chunk;
2175 h1m->curr_len = chunk;
2176 h1m->body_len += chunk;
2177 bo_del(buf, ret);
2178 total += ret;
2179 h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
2180 if (!size)
2181 goto send_empty;
2182 }
2183
2184 /* in MSG_DATA state, continue below */
2185 size = h1m->curr_len;
2186 break;
2187 }
2188
2189 /* we have in <size> the exact number of bytes we need to copy from
2190 * the H1 buffer. We need to check this against the connection's and
2191 * the stream's send windows, and to ensure that this fits in the max
2192 * frame size and in the buffer's available space minus 9 bytes (for
2193 * the frame header). The connection's flow control is applied last so
2194 * that we can use a separate list of streams which are immediately
2195 * unblocked on window opening. Note: we don't implement padding.
2196 */
2197
2198 if (size > buf->o)
2199 size = buf->o;
2200
2201 if (size > h2s->mws)
2202 size = h2s->mws;
2203
2204 if (size <= 0) {
2205 h2s->flags |= H2_SF_BLK_SFCTL;
2206 goto end;
2207 }
2208
2209 if (h2c->mfs && size > h2c->mfs)
2210 size = h2c->mfs;
2211
2212 if (size + 9 > outbuf.size) {
2213 /* we have an opportunity for enlarging the too small
2214 * available space, let's try.
2215 */
2216 if (buffer_space_wraps(h2c->mbuf))
2217 goto realign_again;
2218 size = outbuf.size - 9;
2219 }
2220
2221 if (size <= 0) {
2222 h2c->flags |= H2_CF_MUX_MFULL;
2223 h2s->flags |= H2_SF_BLK_MROOM;
2224 goto end;
2225 }
2226
2227 if (size > h2c->mws)
2228 size = h2c->mws;
2229
2230 if (size <= 0) {
2231 h2s->flags |= H2_SF_BLK_MFCTL;
2232 goto end;
2233 }
2234
2235 /* copy whatever we can */
2236 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
2237 ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2);
2238 if (ret == 1)
2239 len2 = 0;
2240
2241 if (!ret || len1 + len2 < size) {
2242 /* FIXME: must normally never happen */
2243 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2244 goto end;
2245 }
2246
2247 /* limit len1/len2 to size */
2248 if (len1 + len2 > size) {
2249 int sub = len1 + len2 - size;
2250
2251 if (len2 > sub)
2252 len2 -= sub;
2253 else {
2254 sub -= len2;
2255 len2 = 0;
2256 len1 -= sub;
2257 }
2258 }
2259
2260 /* now let's copy this this into the output buffer */
2261 memcpy(outbuf.str + 9, blk1, len1);
2262 if (len2)
2263 memcpy(outbuf.str + 9 + len1, blk2, len2);
2264
2265 send_empty:
2266 /* we may need to add END_STREAM */
2267 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
2268 * could rely on the MSG_MORE flag as a hint for this ?
2269 */
2270 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
2271 !h1m->curr_len || h1m->state >= HTTP_MSG_DONE)
2272 es_now = 1;
2273
2274 /* update the frame's size */
2275 h2_set_frame_size(outbuf.str, size);
2276
2277 if (es_now)
2278 outbuf.str[4] |= H2_F_DATA_END_STREAM;
2279
2280 /* commit the H2 response */
2281 h2c->mbuf->o += size + 9;
2282 h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9);
2283
2284 /* consume incoming H1 response */
2285 if (size > 0) {
2286 bo_del(buf, size);
2287 total += size;
2288 h1m->curr_len -= size;
2289 h2s->mws -= size;
2290 h2c->mws -= size;
2291
2292 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
2293 h1m->state = HTTP_MSG_CHUNK_CRLF;
2294 goto new_frame;
2295 }
2296 }
2297
2298 if (es_now) {
2299 if (h2s->st == H2_SS_OPEN)
2300 h2s->st = H2_SS_HLOC;
2301 else
2302 h2s->st = H2_SS_CLOSED;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01002303
2304 if (!(h1m->flags & H1_MF_CHNK))
2305 h1m->state = HTTP_MSG_DONE;
2306
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002307 h2s->flags |= H2_SF_ES_SENT;
2308 }
2309
2310 end:
2311 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);
2312 return total;
2313}
2314
Willy Tarreau62f52692017-10-08 23:01:42 +02002315/* Called from the upper layer, to send data */
2316static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags)
2317{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002318 struct h2s *h2s = cs->ctx;
2319 int total = 0;
2320
2321 //fprintf(stderr, "cs=%p h2s=%p rqst=%d rsst=%d\n", cs, h2s, h2s->req.state, h2s->res.state);
2322 while (h2s->res.state < HTTP_MSG_DONE && buf->o) {
2323 if (h2s->res.state < HTTP_MSG_BODY) {
2324 total += h2s_frt_make_resp_headers(h2s, buf);
2325
2326 if (h2s->st == H2_SS_ERROR)
2327 break;
2328
2329 if (h2s->flags & H2_SF_BLK_ANY)
2330 break;
2331 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002332 else if (h2s->res.state < HTTP_MSG_TRAILERS) {
2333 total += h2s_frt_make_resp_data(h2s, buf);
2334
2335 if (h2s->st == H2_SS_ERROR)
2336 break;
2337
2338 if (h2s->flags & H2_SF_BLK_ANY)
2339 break;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01002340 }
2341 else if (h2s->res.state == HTTP_MSG_TRAILERS) {
2342 /* consume the trailers if any (we don't forward them for now) */
2343 int count = h1_measure_trailers(buf);
2344
2345 if (unlikely(count <= 0)) {
2346 if (count < 0)
2347 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2348 break;
2349 }
2350 total += count;
2351 bo_del(buf, count);
2352 h2s->res.state = HTTP_MSG_DONE;
2353 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002354 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002355 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002356 cs->flags |= CS_FL_ERROR;
2357 break;
2358 }
2359 }
2360
Willy Tarreauc652dbd2017-10-19 11:16:37 +02002361 if (h2s->flags & H2_SF_BLK_SFCTL) {
2362 /* stream flow control, quit the list */
2363 LIST_DEL(&h2s->list);
2364 LIST_INIT(&h2s->list);
2365 }
2366
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02002367 if (h2s->st == H2_SS_ERROR)
2368 cs->flags |= CS_FL_ERROR;
2369
2370 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02002371}
2372
2373
2374/*******************************************************/
2375/* functions below are dedicated to the config parsers */
2376/*******************************************************/
2377
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02002378/* config parser for global "tune.h2.header-table-size" */
2379static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
2380 struct proxy *defpx, const char *file, int line,
2381 char **err)
2382{
2383 if (too_many_args(1, args, err, NULL))
2384 return -1;
2385
2386 h2_settings_header_table_size = atoi(args[1]);
2387 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
2388 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
2389 return -1;
2390 }
2391 return 0;
2392}
Willy Tarreau62f52692017-10-08 23:01:42 +02002393
Willy Tarreaue6baec02017-07-27 11:45:11 +02002394/* config parser for global "tune.h2.initial-window-size" */
2395static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
2396 struct proxy *defpx, const char *file, int line,
2397 char **err)
2398{
2399 if (too_many_args(1, args, err, NULL))
2400 return -1;
2401
2402 h2_settings_initial_window_size = atoi(args[1]);
2403 if (h2_settings_initial_window_size < 0) {
2404 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
2405 return -1;
2406 }
2407 return 0;
2408}
2409
Willy Tarreau5242ef82017-07-27 11:47:28 +02002410/* config parser for global "tune.h2.max-concurrent-streams" */
2411static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
2412 struct proxy *defpx, const char *file, int line,
2413 char **err)
2414{
2415 if (too_many_args(1, args, err, NULL))
2416 return -1;
2417
2418 h2_settings_max_concurrent_streams = atoi(args[1]);
2419 if (h2_settings_max_concurrent_streams < 0) {
2420 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
2421 return -1;
2422 }
2423 return 0;
2424}
2425
Willy Tarreau62f52692017-10-08 23:01:42 +02002426
2427/****************************************/
2428/* MUX initialization and instanciation */
2429/***************************************/
2430
2431/* The mux operations */
2432const struct mux_ops h2_ops = {
2433 .init = h2_init,
2434 .recv = h2_recv,
2435 .send = h2_send,
2436 .wake = h2_wake,
2437 .update_poll = h2_update_poll,
2438 .rcv_buf = h2_rcv_buf,
2439 .snd_buf = h2_snd_buf,
2440 .attach = h2_attach,
2441 .detach = h2_detach,
2442 .shutr = h2_shutr,
2443 .shutw = h2_shutw,
2444 .release = h2_release,
2445 .name = "H2",
2446};
2447
2448/* ALPN selection : this mux registers ALPN tolen "h2" */
2449static struct alpn_mux_list alpn_mux_h2 =
2450 { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops };
2451
2452/* config keyword parsers */
2453static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02002454 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02002455 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02002456 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02002457 { 0, NULL, NULL }
2458}};
2459
Willy Tarreau5ab6b572017-09-22 08:05:00 +02002460static void __h2_deinit(void)
2461{
Willy Tarreau18312642017-10-11 07:57:07 +02002462 pool_destroy2(pool2_h2s);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02002463 pool_destroy2(pool2_h2c);
2464}
2465
Willy Tarreau62f52692017-10-08 23:01:42 +02002466__attribute__((constructor))
2467static void __h2_init(void)
2468{
2469 alpn_register_mux(&alpn_mux_h2);
2470 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02002471 hap_register_post_deinit(__h2_deinit);
2472 pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
Willy Tarreau18312642017-10-11 07:57:07 +02002473 pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02002474}