blob: 90c6a440c6f288b651fba13a337e1a3346fe6209 [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 Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010090 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020091 int32_t max_id; /* highest ID known on this connection, <0 before preface */
92 uint32_t rcvd_c; /* newly received data to ACK for the connection */
93 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
94
95 /* states for the demux direction */
96 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020097 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098
99 int32_t dsi; /* demux stream ID (<0 = idle) */
100 int32_t dfl; /* demux frame length (if dsi >= 0) */
101 int8_t dft; /* demux frame type (if dsi >= 0) */
102 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100103 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
104 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
106
107 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t msi; /* mux stream ID (<0 = idle) */
110 int32_t mfl; /* mux frame length (if dsi >= 0) */
111 int8_t mft; /* mux frame type (if dsi >= 0) */
112 int8_t mff; /* mux frame flags (if dsi >= 0) */
113 /* 16 bit hole here */
114 int32_t miw; /* mux initial window size for all new streams */
115 int32_t mws; /* mux window size. Can be negative. */
116 int32_t mfs; /* mux's max frame size */
117
Willy Tarreauea392822017-10-31 10:02:25 +0100118 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100119 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100120 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200121 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100122 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100123 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200124 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100125 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126 struct eb_root streams_by_id; /* all active streams by their ID */
127 struct list send_list; /* list of blocked streams requesting to send */
128 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200129 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100130 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200131 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132};
133
Willy Tarreau18312642017-10-11 07:57:07 +0200134/* H2 stream state, in h2s->st */
135enum h2_ss {
136 H2_SS_IDLE = 0, // idle
137 H2_SS_RLOC, // reserved(local)
138 H2_SS_RREM, // reserved(remote)
139 H2_SS_OPEN, // open
140 H2_SS_HREM, // half-closed(remote)
141 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200142 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200143 H2_SS_CLOSED, // closed
144 H2_SS_ENTRIES // must be last
145} __attribute__((packed));
146
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200147#define H2_SS_MASK(state) (1UL << (state))
148#define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE)
149#define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC)
150#define H2_SS_RREM_BIT (1UL << H2_SS_RREM)
151#define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN)
152#define H2_SS_HREM_BIT (1UL << H2_SS_HREM)
153#define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC)
154#define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR)
155#define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED)
156#define H2_SS_EOS_BITS (H2_SS_CLOSED_BIT|H2_SS_ERROR_BIT|H2_SS_HREM_BIT)
157
Willy Tarreau18312642017-10-11 07:57:07 +0200158/* HTTP/2 stream flags (32 bit), in h2s->flags */
159#define H2_SF_NONE 0x00000000
160#define H2_SF_ES_RCVD 0x00000001
161#define H2_SF_ES_SENT 0x00000002
162
163#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
164#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
165
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200166/* stream flags indicating the reason the stream is blocked */
167#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
168#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
169#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
170#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
171#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
172
Willy Tarreau454f9052017-10-26 19:40:35 +0200173/* stream flags indicating how data is supposed to be sent */
174#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
175#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
176
177/* step we're currently in when sending chunks. This is needed because we may
178 * have to transfer chunks as large as a full buffer so there's no room left
179 * for size nor crlf around.
180 */
181#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
182#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
183#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
184
185#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
186
Willy Tarreau67434202017-11-06 20:20:51 +0100187#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100188#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100189
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100190#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
191
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200192#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
193#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
Willy Tarreau3cf69fe2019-05-14 10:44:40 +0200194#define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200195
196
Willy Tarreau18312642017-10-11 07:57:07 +0200197/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
198 * it is being processed in the internal HTTP representation (H1 for now).
199 */
200struct h2s {
201 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100202 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200203 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200204 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200205 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200206 int32_t id; /* stream ID */
207 uint32_t flags; /* H2_SF_* */
208 int mws; /* mux window size for this stream */
209 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
210 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200211 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100212 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200213 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200214 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100215 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
216 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200217 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100218 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200219};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200220
Willy Tarreauc6405142017-09-21 20:23:50 +0200221/* descriptor for an h2 frame header */
222struct h2_fh {
223 uint32_t len; /* length, host order, 24 bits */
224 uint32_t sid; /* stream id, host order, 31 bits */
225 uint8_t ft; /* frame type */
226 uint8_t ff; /* frame flags */
227};
228
Willy Tarreau8ceae722018-11-26 11:58:30 +0100229/* the h2c connection pool */
230DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
231
232/* the h2s stream pool */
233DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
234
Willy Tarreaudc572362018-12-12 08:08:05 +0100235/* The default connection window size is 65535, it may only be enlarged using
236 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
237 * we'll pretend we already received the difference between the two to send
238 * an equivalent window update to enlarge it to 2G-1.
239 */
240#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
241
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200242/* a few settings from the global section */
243static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200244static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100245static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100246static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200247
Willy Tarreau2a856182017-05-16 15:20:39 +0200248/* a dmumy closed stream */
249static const struct h2s *h2_closed_stream = &(const struct h2s){
250 .cs = NULL,
251 .h2c = NULL,
252 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100253 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100254 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200255 .id = 0,
256};
257
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100258/* a dmumy closed stream returning a PROTOCOL_ERROR error */
259static const struct h2s *h2_error_stream = &(const struct h2s){
260 .cs = NULL,
261 .h2c = NULL,
262 .st = H2_SS_CLOSED,
263 .errcode = H2_ERR_PROTOCOL_ERROR,
264 .flags = 0,
265 .id = 0,
266};
267
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100268/* a dmumy closed stream returning a REFUSED_STREAM error */
269static const struct h2s *h2_refused_stream = &(const struct h2s){
270 .cs = NULL,
271 .h2c = NULL,
272 .st = H2_SS_CLOSED,
273 .errcode = H2_ERR_REFUSED_STREAM,
274 .flags = 0,
275 .id = 0,
276};
277
Willy Tarreau2a856182017-05-16 15:20:39 +0200278/* and a dummy idle stream for use with any unannounced stream */
279static const struct h2s *h2_idle_stream = &(const struct h2s){
280 .cs = NULL,
281 .h2c = NULL,
282 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100283 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200284 .id = 0,
285};
286
Olivier Houchard9f6af332018-05-25 14:04:04 +0200287static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200288static int h2_send(struct h2c *h2c);
289static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200290static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200291static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100292static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100293static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100294static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200295static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100296static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100297static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200298
Olivier Houchard7a977432019-03-21 15:47:13 +0100299static __inline int
300h2c_is_dead(struct h2c *h2c)
301{
302 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
303 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
304 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
305 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
306 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
307 (conn_xprt_read0_pending(h2c->conn) ||
308 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
309 return 1;
310
311 return 0;
312
313}
314
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200315/*****************************************************/
316/* functions below are for dynamic buffer management */
317/*****************************************************/
318
Willy Tarreau315d8072017-12-10 22:17:57 +0100319/* indicates whether or not the we may call the h2_recv() function to attempt
320 * to receive data into the buffer and/or demux pending data. The condition is
321 * a bit complex due to some API limits for now. The rules are the following :
322 * - if an error or a shutdown was detected on the connection and the buffer
323 * is empty, we must not attempt to receive
324 * - if the demux buf failed to be allocated, we must not try to receive and
325 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100326 * - if no flag indicates a blocking condition, we may attempt to receive,
327 * regardless of whether the demux buffer is full or not, so that only
328 * de demux part decides whether or not to block. This is needed because
329 * the connection API indeed prevents us from re-enabling receipt that is
330 * already enabled in a polled state, so we must always immediately stop
331 * as soon as the demux can't proceed so as never to hit an end of read
332 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100333 * - otherwise must may not attempt
334 */
335static inline int h2_recv_allowed(const struct h2c *h2c)
336{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200337 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100338 (h2c->st0 >= H2_CS_ERROR ||
339 h2c->conn->flags & CO_FL_ERROR ||
340 conn_xprt_read0_pending(h2c->conn)))
341 return 0;
342
343 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100344 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100345 return 1;
346
347 return 0;
348}
349
Willy Tarreau47b515a2018-12-21 16:09:41 +0100350/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200351static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100352{
353 if (!h2_recv_allowed(h2c))
354 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200355 if ((!consider_buffer || !b_data(&h2c->dbuf))
356 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100357 return;
358 tasklet_wakeup(h2c->wait_event.task);
359}
360
361
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100362/* returns true if the front connection has too many conn_streams attached */
363static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200364{
Willy Tarreaua8754662018-12-23 20:43:58 +0100365 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200366}
367
Willy Tarreau44e973f2018-03-01 17:49:30 +0100368/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
369 * flags are used to figure what buffer was requested. It returns 1 if the
370 * allocation succeeds, in which case the connection is woken up, or 0 if it's
371 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200372 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100373static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200374{
375 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100376 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200377
Willy Tarreau44e973f2018-03-01 17:49:30 +0100378 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200379 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200380 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200381 return 1;
382 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200383
Willy Tarreau44e973f2018-03-01 17:49:30 +0100384 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
385 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200386
387 if (h2c->flags & H2_CF_DEM_MROOM) {
388 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200389 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200390 }
Willy Tarreau14398122017-09-22 14:26:04 +0200391 return 1;
392 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100393
394 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
395 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200396 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100397 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200398 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100399 return 1;
400 }
401
Willy Tarreau14398122017-09-22 14:26:04 +0200402 return 0;
403}
404
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200405static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200406{
407 struct buffer *buf = NULL;
408
Willy Tarreauc234ae32019-05-13 17:56:11 +0200409 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100410 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
411 h2c->buf_wait.target = h2c;
412 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100413 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100414 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100415 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200416 __conn_xprt_stop_recv(h2c->conn);
417 }
418 return buf;
419}
420
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200421static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200422{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200423 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100424 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200425 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200426 }
427}
428
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100429/* returns the number of allocatable outgoing streams for the connection taking
430 * the last_sid and the reserved ones into account.
431 */
432static inline int h2_streams_left(const struct h2c *h2c)
433{
434 int ret;
435
436 /* consider the number of outgoing streams we're allowed to create before
437 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
438 * nb_reserved is the number of streams which don't yet have an ID.
439 */
440 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
441 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
442 if (ret < 0)
443 ret = 0;
444 return ret;
445}
446
Willy Tarreau00f18a32019-01-26 12:19:01 +0100447/* returns the number of streams in use on a connection to figure if it's
448 * idle or not. We check nb_cs and not nb_streams as the caller will want
449 * to know if it was the last one after a detach().
450 */
451static int h2_used_streams(struct connection *conn)
452{
453 struct h2c *h2c = conn->ctx;
454
455 return h2c->nb_cs;
456}
457
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100458/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100459static int h2_avail_streams(struct connection *conn)
460{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100461 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100462 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100463 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100464
Willy Tarreau6afec462019-01-28 06:40:19 +0100465 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
466 * streams on the connection.
467 */
468 if (h2c->last_sid >= 0)
469 return 0;
470
Willy Tarreau86949782019-01-31 10:42:05 +0100471 /* note: may be negative if a SETTINGS frame changes the limit */
472 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100473
474 /* we must also consider the limit imposed by stream IDs */
475 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100476 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100477 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100478 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
479 ret1 = MIN(ret1, ret2);
480 }
481 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100482}
483
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200484
Willy Tarreau62f52692017-10-08 23:01:42 +0200485/*****************************************************************/
486/* functions below are dedicated to the mux setup and management */
487/*****************************************************************/
488
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200489/* Initialize the mux once it's attached. For outgoing connections, the context
490 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200491 * connections from the fact that the context is still NULL (even during mux
492 * upgrades). <input> is always used as Input buffer and may contain data. It is
493 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200494 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200495static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
496 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200497{
498 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100499 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200500
Willy Tarreaubafbe012017-11-24 17:34:44 +0100501 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200502 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200503 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200504
Christopher Faulete9b70722019-04-08 10:46:02 +0200505 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200506 h2c->flags = H2_CF_IS_BACK;
507 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
508 if (tick_isset(prx->timeout.serverfin))
509 h2c->shut_timeout = prx->timeout.serverfin;
510 } else {
511 h2c->flags = H2_CF_NONE;
512 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
513 if (tick_isset(prx->timeout.clientfin))
514 h2c->shut_timeout = prx->timeout.clientfin;
515 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100516
Willy Tarreau0b37d652018-10-03 10:33:02 +0200517 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100518 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100519 if (tick_isset(h2c->timeout)) {
520 t = task_new(tid_bit);
521 if (!t)
522 goto fail;
523
524 h2c->task = t;
525 t->process = h2_timeout_task;
526 t->context = h2c;
527 t->expire = tick_add(now_ms, h2c->timeout);
528 }
Willy Tarreauea392822017-10-31 10:02:25 +0100529
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200530 h2c->wait_event.task = tasklet_new();
531 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200532 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200533 h2c->wait_event.task->process = h2_io_cb;
534 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100535 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200536
Willy Tarreau32218eb2017-09-22 08:07:25 +0200537 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
538 if (!h2c->ddht)
539 goto fail;
540
541 /* Initialise the context. */
542 h2c->st0 = H2_CS_PREFACE;
543 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100544 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200545 h2c->max_id = -1;
546 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100547 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200548 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100549 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200550 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100551 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100552 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200554 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200555 h2c->dsi = -1;
556 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100557
Willy Tarreau32218eb2017-09-22 08:07:25 +0200558 h2c->last_sid = -1;
559
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200560 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200561 h2c->miw = 65535; /* mux initial window size */
562 h2c->mws = 65535; /* mux window size */
563 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200564 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200565 LIST_INIT(&h2c->send_list);
566 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200567 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100568 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200569
Willy Tarreau3f133572017-10-31 19:21:06 +0100570 if (t)
571 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100572
Willy Tarreau01b44822018-10-03 14:26:37 +0200573 if (h2c->flags & H2_CF_IS_BACK) {
574 /* FIXME: this is temporary, for outgoing connections we need
575 * to immediately allocate a stream until the code is modified
576 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100577 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200578 */
579 struct h2s *h2s;
580
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100581 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200582 if (!h2s)
583 goto fail_stream;
584 }
585
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100586 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200587
Willy Tarreau0f383582018-10-03 14:22:21 +0200588 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200589 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200590 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200591 fail_stream:
592 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200593 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200594 task_destroy(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200595 if (h2c->wait_event.task)
596 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100597 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200598 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200599 return -1;
600}
601
Willy Tarreau751f2d02018-10-05 09:35:00 +0200602/* returns the next allocatable outgoing stream ID for the H2 connection, or
603 * -1 if no more is allocatable.
604 */
605static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
606{
607 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100608
609 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200610 id = -1;
611 return id;
612}
613
Willy Tarreau2373acc2017-10-12 17:35:14 +0200614/* returns the stream associated with id <id> or NULL if not found */
615static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
616{
617 struct eb32_node *node;
618
Willy Tarreau751f2d02018-10-05 09:35:00 +0200619 if (id == 0)
620 return (struct h2s *)h2_closed_stream;
621
Willy Tarreau2a856182017-05-16 15:20:39 +0200622 if (id > h2c->max_id)
623 return (struct h2s *)h2_idle_stream;
624
Willy Tarreau2373acc2017-10-12 17:35:14 +0200625 node = eb32_lookup(&h2c->streams_by_id, id);
626 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200627 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200628
629 return container_of(node, struct h2s, by_id);
630}
631
Christopher Faulet73c12072019-04-08 11:23:22 +0200632/* release function. This one should be called to free all resources allocated
633 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200634 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200635static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200636{
Christopher Faulet61840e72019-04-15 09:33:32 +0200637 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200638
Willy Tarreau32218eb2017-09-22 08:07:25 +0200639 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200640 /* The connection must be aattached to this mux to be released */
641 if (h2c->conn && h2c->conn->ctx == h2c)
642 conn = h2c->conn;
643
Willy Tarreau32218eb2017-09-22 08:07:25 +0200644 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200645
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100646 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100647 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100648 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200649
Willy Tarreau44e973f2018-03-01 17:49:30 +0100650 h2_release_buf(h2c, &h2c->dbuf);
651 h2_release_buf(h2c, &h2c->mbuf);
652
Willy Tarreauea392822017-10-31 10:02:25 +0100653 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200654 h2c->task->context = NULL;
655 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100656 h2c->task = NULL;
657 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200658 if (h2c->wait_event.task)
659 tasklet_free(h2c->wait_event.task);
Olivier Houchard0e079372019-04-15 17:51:16 +0200660 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100661 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200662 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100663
Willy Tarreaubafbe012017-11-24 17:34:44 +0100664 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200665 }
666
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200667 if (conn) {
668 conn->mux = NULL;
669 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200670
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200671 conn_stop_tracking(conn);
672 conn_full_close(conn);
673 if (conn->destroy_cb)
674 conn->destroy_cb(conn);
675 conn_free(conn);
676 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200677}
678
679
Willy Tarreau71681172017-10-23 14:39:06 +0200680/******************************************************/
681/* functions below are for the H2 protocol processing */
682/******************************************************/
683
684/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100685static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200686{
687 return h2s ? h2s->id : 0;
688}
689
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200690/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100691static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200692{
693 if (h2c->msi < 0)
694 return 0;
695
696 if (h2c->msi == h2s_id(h2s))
697 return 0;
698
699 return 1;
700}
701
Willy Tarreau741d6df2017-10-17 08:00:59 +0200702/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100703static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200704{
705 h2c->errcode = err;
706 h2c->st0 = H2_CS_ERROR;
707}
708
Willy Tarreau175cebb2019-01-24 10:02:24 +0100709/* marks an error on the stream. It may also update an already closed stream
710 * (e.g. to report an error after an RST was received).
711 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100712static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200713{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100714 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200715 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100716 if (h2s->st < H2_SS_ERROR)
717 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100718 if (h2s->cs)
719 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200720 }
721}
722
Willy Tarreau7e094452018-12-19 18:08:52 +0100723/* attempt to notify the data layer of recv availability */
724static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
725{
726 struct wait_event *sw;
727
728 if (h2s->recv_wait) {
729 sw = h2s->recv_wait;
730 sw->events &= ~SUB_RETRY_RECV;
731 tasklet_wakeup(sw->task);
732 h2s->recv_wait = NULL;
733 }
734}
735
736/* attempt to notify the data layer of send availability */
737static void __maybe_unused h2s_notify_send(struct h2s *h2s)
738{
739 struct wait_event *sw;
740
Willy Tarreauc234ae32019-05-13 17:56:11 +0200741 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100742 sw = h2s->send_wait;
743 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100744 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100745 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100746 }
747}
748
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100749/* alerts the data layer, trying to wake it up by all means, following
750 * this sequence :
751 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
752 * - if its subscribed to send, then it's woken up for send
753 * - if it was subscribed to neither, its ->wake() callback is called
754 * It is safe to call this function with a closed stream which doesn't have a
755 * conn_stream anymore.
756 */
757static void __maybe_unused h2s_alert(struct h2s *h2s)
758{
759 if (h2s->recv_wait || h2s->send_wait) {
760 h2s_notify_recv(h2s);
761 h2s_notify_send(h2s);
762 }
763 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
764 h2s->cs->data_cb->wake(h2s->cs);
765}
766
Willy Tarreaue4820742017-07-27 13:37:23 +0200767/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100768static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200769{
770 uint8_t *out = frame;
771
772 *out = len >> 16;
773 write_n16(out + 1, len);
774}
775
Willy Tarreau54c15062017-10-10 17:10:03 +0200776/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
777 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
778 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200779 * available in the buffer's input prior to calling this function. The buffer
780 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200781 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100782static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200783 const struct buffer *b, int o)
784{
Willy Tarreau591d4452018-06-15 17:21:00 +0200785 readv_bytes(dst, bytes, b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200786}
787
Willy Tarreau1f094672017-11-20 21:27:45 +0100788static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200789{
Willy Tarreau591d4452018-06-15 17:21:00 +0200790 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200791}
792
Willy Tarreau1f094672017-11-20 21:27:45 +0100793static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200794{
Willy Tarreau591d4452018-06-15 17:21:00 +0200795 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200796}
797
Willy Tarreau1f094672017-11-20 21:27:45 +0100798static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200799{
Willy Tarreau591d4452018-06-15 17:21:00 +0200800 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200801}
802
803
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100804/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
805 * The algorithm is not obvious. It turns out that H2 headers are neither
806 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
807 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200808 *
809 * b0 b1 b2 b3 b4 b5..b8
810 * +----------+---------+--------+----+----+----------------------+
811 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
812 * +----------+---------+--------+----+----+----------------------+
813 *
814 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
815 * we get the sid properly aligned and ordered, and 16 bits of len properly
816 * ordered as well. The type and flags can be extracted using bit shifts from
817 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200818 * Returns zero if some bytes are missing, otherwise non-zero on success. The
819 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200820 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100821static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200822{
823 uint64_t w;
824
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100825 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200826 return 0;
827
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100828 w = h2_get_n64(b, o + 1);
829 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200830 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
831 h->ff = w >> 32;
832 h->ft = w >> 40;
833 h->len += w >> 48;
834 return 1;
835}
836
837/* skip the next 9 bytes corresponding to the frame header possibly parsed by
838 * h2_peek_frame_hdr() above.
839 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100840static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200841{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200842 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200843}
844
845/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100846static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200847{
848 int ret;
849
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100850 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200851 if (ret > 0)
852 h2_skip_frame_hdr(b);
853 return ret;
854}
855
Willy Tarreau00dd0782018-03-01 16:31:34 +0100856/* marks stream <h2s> as CLOSED and decrement the number of active streams for
857 * its connection if the stream was not yet closed. Please use this exclusively
858 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100859 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100860static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100861{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100862 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100863 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100864 if (!h2s->id)
865 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100866 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +0100867 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
868 h2s_notify_recv(h2s);
869 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100870 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100871 h2s->st = H2_SS_CLOSED;
872}
873
Willy Tarreau71049cc2018-03-28 13:56:39 +0200874/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
875static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100876{
877 h2s_close(h2s);
878 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200879 if (b_size(&h2s->rxbuf)) {
880 b_free(&h2s->rxbuf);
881 offer_buffers(NULL, tasks_run_queue);
882 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200883 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100884 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200885 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100886 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800887 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200888 * reference left would be in the h2c send_list/fctl_list, and if
889 * we're in it, we're getting out anyway
890 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100891 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200892 if (LIST_ADDED(&h2s->sending_list)) {
Olivier Houchard998410a2019-04-15 19:23:37 +0200893 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
894 LIST_DEL_INIT(&h2s->sending_list);
895 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200896 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100897 pool_free(pool_head_h2s, h2s);
898}
899
Willy Tarreaua8e49542018-10-03 18:53:55 +0200900/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
901 * stream tree. In case of error, nothing is added and NULL is returned. The
902 * causes of errors can be any failed memory allocation. The caller is
903 * responsible for checking if the connection may support an extra stream
904 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200905 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200906static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200907{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200908 struct h2s *h2s;
909
Willy Tarreaubafbe012017-11-24 17:34:44 +0100910 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200911 if (!h2s)
912 goto out;
913
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200914 h2s->wait_event.task = tasklet_new();
915 if (!h2s->wait_event.task) {
916 pool_free(pool_head_h2s, h2s);
917 goto out;
918 }
919 h2s->send_wait = NULL;
920 h2s->recv_wait = NULL;
921 h2s->wait_event.task->process = h2_deferred_shut;
922 h2s->wait_event.task->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100923 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200924 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100925 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200926 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200927 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200928 h2s->mws = h2c->miw;
929 h2s->flags = H2_SF_NONE;
930 h2s->errcode = H2_ERR_NO_ERROR;
931 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200932 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100933 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200934 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200935
936 if (h2c->flags & H2_CF_IS_BACK) {
937 h1m_init_req(&h2s->h1m);
938 h2s->h1m.err_pos = -1; // don't care about errors on the request path
939 h2s->h1m.flags |= H1_MF_TOLOWER;
940 } else {
941 h1m_init_res(&h2s->h1m);
942 h2s->h1m.err_pos = -1; // don't care about errors on the response path
943 h2s->h1m.flags |= H1_MF_TOLOWER;
944 }
945
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200946 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200947 if (id > 0)
948 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100949 else
950 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200951
952 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100953 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100954 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200955
956 return h2s;
957
958 out_free_h2s:
959 pool_free(pool_head_h2s, h2s);
960 out:
961 return NULL;
962}
963
964/* creates a new stream <id> on the h2c connection and returns it, or NULL in
965 * case of memory allocation error.
966 */
967static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
968{
969 struct session *sess = h2c->conn->owner;
970 struct conn_stream *cs;
971 struct h2s *h2s;
972
973 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
974 goto out;
975
976 h2s = h2s_new(h2c, id);
977 if (!h2s)
978 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200979
980 cs = cs_new(h2c->conn);
981 if (!cs)
982 goto out_close;
983
Olivier Houchard746fb772018-12-15 19:42:00 +0100984 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200985 h2s->cs = cs;
986 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200987 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200988
989 if (stream_create_from_cs(cs) < 0)
990 goto out_free_cs;
991
Willy Tarreau590a0512018-09-05 11:56:48 +0200992 /* We want the accept date presented to the next stream to be the one
993 * we have now, the handshake time to be null (since the next stream
994 * is not delayed by a handshake), and the idle time to count since
995 * right now.
996 */
997 sess->accept_date = date;
998 sess->tv_accept = now;
999 sess->t_handshake = 0;
1000
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001001 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001002 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001003 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001004 return h2s;
1005
1006 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001007 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001008 cs_free(cs);
1009 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001010 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001011 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001012 sess_log(sess);
1013 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001014}
1015
Willy Tarreau751f2d02018-10-05 09:35:00 +02001016/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1017 * and returns it, or NULL in case of memory allocation error or if the highest
1018 * possible stream ID was reached.
1019 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001020static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001021{
1022 struct h2s *h2s = NULL;
1023
Willy Tarreau86949782019-01-31 10:42:05 +01001024 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001025 goto out;
1026
Willy Tarreaua80dca82019-01-24 17:08:28 +01001027 if (h2_streams_left(h2c) < 1)
1028 goto out;
1029
Willy Tarreau751f2d02018-10-05 09:35:00 +02001030 /* Defer choosing the ID until we send the first message to create the stream */
1031 h2s = h2s_new(h2c, 0);
1032 if (!h2s)
1033 goto out;
1034
1035 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001036 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001037 cs->ctx = h2s;
1038 h2c->nb_cs++;
1039
Willy Tarreau751f2d02018-10-05 09:35:00 +02001040 out:
1041 return h2s;
1042}
1043
Willy Tarreaube5b7152017-09-25 16:25:39 +02001044/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1045 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1046 * the various settings codes.
1047 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001048static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001049{
1050 struct buffer *res;
1051 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001052 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001053 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001054 int ret;
1055
1056 if (h2c_mux_busy(h2c, NULL)) {
1057 h2c->flags |= H2_CF_DEM_MBUSY;
1058 return 0;
1059 }
1060
Willy Tarreau44e973f2018-03-01 17:49:30 +01001061 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001062 if (!res) {
1063 h2c->flags |= H2_CF_MUX_MALLOC;
1064 h2c->flags |= H2_CF_DEM_MROOM;
1065 return 0;
1066 }
1067
1068 chunk_init(&buf, buf_data, sizeof(buf_data));
1069 chunk_memcpy(&buf,
1070 "\x00\x00\x00" /* length : 0 for now */
1071 "\x04\x00" /* type : 4 (settings), flags : 0 */
1072 "\x00\x00\x00\x00", /* stream ID : 0 */
1073 9);
1074
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001075 if (h2c->flags & H2_CF_IS_BACK) {
1076 /* send settings_enable_push=0 */
1077 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1078 }
1079
Willy Tarreaube5b7152017-09-25 16:25:39 +02001080 if (h2_settings_header_table_size != 4096) {
1081 char str[6] = "\x00\x01"; /* header_table_size */
1082
1083 write_n32(str + 2, h2_settings_header_table_size);
1084 chunk_memcat(&buf, str, 6);
1085 }
1086
1087 if (h2_settings_initial_window_size != 65535) {
1088 char str[6] = "\x00\x04"; /* initial_window_size */
1089
1090 write_n32(str + 2, h2_settings_initial_window_size);
1091 chunk_memcat(&buf, str, 6);
1092 }
1093
1094 if (h2_settings_max_concurrent_streams != 0) {
1095 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1096
1097 /* Note: 0 means "unlimited" for haproxy's config but not for
1098 * the protocol, so never send this value!
1099 */
1100 write_n32(str + 2, h2_settings_max_concurrent_streams);
1101 chunk_memcat(&buf, str, 6);
1102 }
1103
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001104 mfs = h2_settings_max_frame_size;
1105 if (mfs > global.tune.bufsize)
1106 mfs = global.tune.bufsize;
1107
1108 if (!mfs)
1109 mfs = global.tune.bufsize;
1110
1111 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001112 char str[6] = "\x00\x05"; /* max_frame_size */
1113
1114 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1115 * match bufsize - rewrite size, but at the moment it seems
1116 * that clients don't take care of it.
1117 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001118 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001119 chunk_memcat(&buf, str, 6);
1120 }
1121
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001122 h2_set_frame_size(buf.area, buf.data - 9);
1123 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001124 if (unlikely(ret <= 0)) {
1125 if (!ret) {
1126 h2c->flags |= H2_CF_MUX_MFULL;
1127 h2c->flags |= H2_CF_DEM_MROOM;
1128 return 0;
1129 }
1130 else {
1131 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1132 return 0;
1133 }
1134 }
1135 return ret;
1136}
1137
Willy Tarreau52eed752017-09-22 15:05:09 +02001138/* Try to receive a connection preface, then upon success try to send our
1139 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1140 * missing data. It may return an error in h2c.
1141 */
1142static int h2c_frt_recv_preface(struct h2c *h2c)
1143{
1144 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001145 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001146
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001147 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001148
1149 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001150 if (ret1 < 0)
1151 sess_log(h2c->conn->owner);
1152
Willy Tarreau52eed752017-09-22 15:05:09 +02001153 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1154 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1155 return 0;
1156 }
1157
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001158 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001159 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001160 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001161
Willy Tarreaube5b7152017-09-25 16:25:39 +02001162 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001163}
1164
Willy Tarreau01b44822018-10-03 14:26:37 +02001165/* Try to send a connection preface, then upon success try to send our
1166 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1167 * missing data. It may return an error in h2c.
1168 */
1169static int h2c_bck_send_preface(struct h2c *h2c)
1170{
1171 struct buffer *res;
1172
1173 if (h2c_mux_busy(h2c, NULL)) {
1174 h2c->flags |= H2_CF_DEM_MBUSY;
1175 return 0;
1176 }
1177
1178 res = h2_get_buf(h2c, &h2c->mbuf);
1179 if (!res) {
1180 h2c->flags |= H2_CF_MUX_MALLOC;
1181 h2c->flags |= H2_CF_DEM_MROOM;
1182 return 0;
1183 }
1184
1185 if (!b_data(res)) {
1186 /* preface not yet sent */
1187 b_istput(res, ist(H2_CONN_PREFACE));
1188 }
1189
1190 return h2c_send_settings(h2c);
1191}
1192
Willy Tarreau081d4722017-05-16 21:51:05 +02001193/* try to send a GOAWAY frame on the connection to report an error or a graceful
1194 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1195 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1196 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1197 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1198 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1199 * on unrecoverable failure. It will not attempt to send one again in this last
1200 * case so that it is safe to use h2c_error() to report such errors.
1201 */
1202static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1203{
1204 struct buffer *res;
1205 char str[17];
1206 int ret;
1207
1208 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1209 return 1; // claim that it worked
1210
1211 if (h2c_mux_busy(h2c, h2s)) {
1212 if (h2s)
1213 h2s->flags |= H2_SF_BLK_MBUSY;
1214 else
1215 h2c->flags |= H2_CF_DEM_MBUSY;
1216 return 0;
1217 }
1218
Willy Tarreau44e973f2018-03-01 17:49:30 +01001219 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001220 if (!res) {
1221 h2c->flags |= H2_CF_MUX_MALLOC;
1222 if (h2s)
1223 h2s->flags |= H2_SF_BLK_MROOM;
1224 else
1225 h2c->flags |= H2_CF_DEM_MROOM;
1226 return 0;
1227 }
1228
1229 /* len: 8, type: 7, flags: none, sid: 0 */
1230 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1231
1232 if (h2c->last_sid < 0)
1233 h2c->last_sid = h2c->max_id;
1234
1235 write_n32(str + 9, h2c->last_sid);
1236 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001237 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001238 if (unlikely(ret <= 0)) {
1239 if (!ret) {
1240 h2c->flags |= H2_CF_MUX_MFULL;
1241 if (h2s)
1242 h2s->flags |= H2_SF_BLK_MROOM;
1243 else
1244 h2c->flags |= H2_CF_DEM_MROOM;
1245 return 0;
1246 }
1247 else {
1248 /* we cannot report this error using GOAWAY, so we mark
1249 * it and claim a success.
1250 */
1251 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1252 h2c->flags |= H2_CF_GOAWAY_FAILED;
1253 return 1;
1254 }
1255 }
1256 h2c->flags |= H2_CF_GOAWAY_SENT;
1257 return ret;
1258}
1259
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001260/* Try to send an RST_STREAM frame on the connection for the indicated stream
1261 * during mux operations. This stream must be valid and cannot be closed
1262 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1263 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1264 * not yet.
1265 *
1266 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1267 * to write the message, it subscribes the stream to future notifications.
1268 */
1269static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1270{
1271 struct buffer *res;
1272 char str[13];
1273 int ret;
1274
1275 if (!h2s || h2s->st == H2_SS_CLOSED)
1276 return 1;
1277
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001278 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1279 * RST_STREAM in response to a RST_STREAM frame.
1280 */
1281 if (h2c->dft == H2_FT_RST_STREAM) {
1282 ret = 1;
1283 goto ignore;
1284 }
1285
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001286 if (h2c_mux_busy(h2c, h2s)) {
1287 h2s->flags |= H2_SF_BLK_MBUSY;
1288 return 0;
1289 }
1290
Willy Tarreau44e973f2018-03-01 17:49:30 +01001291 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001292 if (!res) {
1293 h2c->flags |= H2_CF_MUX_MALLOC;
1294 h2s->flags |= H2_SF_BLK_MROOM;
1295 return 0;
1296 }
1297
1298 /* len: 4, type: 3, flags: none */
1299 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1300 write_n32(str + 5, h2s->id);
1301 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001302 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001303
1304 if (unlikely(ret <= 0)) {
1305 if (!ret) {
1306 h2c->flags |= H2_CF_MUX_MFULL;
1307 h2s->flags |= H2_SF_BLK_MROOM;
1308 return 0;
1309 }
1310 else {
1311 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1312 return 0;
1313 }
1314 }
1315
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001316 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001317 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001318 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001319 return ret;
1320}
1321
1322/* Try to send an RST_STREAM frame on the connection for the stream being
1323 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001324 * error code, even if the stream is one of the dummy ones, and will update
1325 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001326 *
1327 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1328 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001329 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001330 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001331 */
1332static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1333{
1334 struct buffer *res;
1335 char str[13];
1336 int ret;
1337
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001338 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1339 * RST_STREAM in response to a RST_STREAM frame.
1340 */
1341 if (h2c->dft == H2_FT_RST_STREAM) {
1342 ret = 1;
1343 goto ignore;
1344 }
1345
Willy Tarreau27a84c92017-10-17 08:10:17 +02001346 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001347 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001348 return 0;
1349 }
1350
Willy Tarreau44e973f2018-03-01 17:49:30 +01001351 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001352 if (!res) {
1353 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001354 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001355 return 0;
1356 }
1357
1358 /* len: 4, type: 3, flags: none */
1359 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001360
Willy Tarreau27a84c92017-10-17 08:10:17 +02001361 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001362 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001363 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001364
Willy Tarreau27a84c92017-10-17 08:10:17 +02001365 if (unlikely(ret <= 0)) {
1366 if (!ret) {
1367 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001368 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001369 return 0;
1370 }
1371 else {
1372 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1373 return 0;
1374 }
1375 }
1376
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001377 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001378 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001379 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001380 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001381 }
1382
Willy Tarreau27a84c92017-10-17 08:10:17 +02001383 return ret;
1384}
1385
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001386/* try to send an empty DATA frame with the ES flag set to notify about the
1387 * end of stream and match a shutdown(write). If an ES was already sent as
1388 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1389 * on success or zero if nothing was done. In case of lack of room to write the
1390 * message, it subscribes the requesting stream to future notifications.
1391 */
1392static int h2_send_empty_data_es(struct h2s *h2s)
1393{
1394 struct h2c *h2c = h2s->h2c;
1395 struct buffer *res;
1396 char str[9];
1397 int ret;
1398
Willy Tarreau721c9742017-11-07 11:05:42 +01001399 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001400 return 1;
1401
1402 if (h2c_mux_busy(h2c, h2s)) {
1403 h2s->flags |= H2_SF_BLK_MBUSY;
1404 return 0;
1405 }
1406
Willy Tarreau44e973f2018-03-01 17:49:30 +01001407 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001408 if (!res) {
1409 h2c->flags |= H2_CF_MUX_MALLOC;
1410 h2s->flags |= H2_SF_BLK_MROOM;
1411 return 0;
1412 }
1413
1414 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1415 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1416 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001417 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001418 if (likely(ret > 0)) {
1419 h2s->flags |= H2_SF_ES_SENT;
1420 }
1421 else if (!ret) {
1422 h2c->flags |= H2_CF_MUX_MFULL;
1423 h2s->flags |= H2_SF_BLK_MROOM;
1424 return 0;
1425 }
1426 else {
1427 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1428 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001429 }
1430 return ret;
1431}
1432
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001433/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001434 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001435 * is automatically updated accordingly. If the stream is orphaned, it is
1436 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001437 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001438static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001439{
1440 if (!h2s->cs) {
1441 /* this stream was already orphaned */
1442 h2s_destroy(h2s);
1443 return;
1444 }
1445
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001446 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001447 if (h2s->st == H2_SS_OPEN)
1448 h2s->st = H2_SS_HREM;
1449 else if (h2s->st == H2_SS_HLOC)
1450 h2s_close(h2s);
1451 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001452
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001453 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1454 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1455 h2s->cs->flags |= CS_FL_ERR_PENDING;
1456 if (h2s->cs->flags & CS_FL_EOS)
1457 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001458
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001459 if (h2s->st < H2_SS_ERROR)
1460 h2s->st = H2_SS_ERROR;
1461 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001462
1463 h2s_alert(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001464}
1465
1466/* wake the streams attached to the connection, whose id is greater than <last>
1467 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001468 */
Willy Tarreau23482912019-05-07 15:23:14 +02001469static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001470{
1471 struct eb32_node *node;
1472 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001473
Christopher Fauletf02ca002019-03-07 16:21:34 +01001474 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001475 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1476 while (node) {
1477 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001478 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001479 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001480 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001481
Christopher Fauletf02ca002019-03-07 16:21:34 +01001482 /* Wake all streams with unassigned ID (ID == 0) */
1483 node = eb32_lookup(&h2c->streams_by_id, 0);
1484 while (node) {
1485 h2s = container_of(node, struct h2s, by_id);
1486 if (h2s->id > 0)
1487 break;
1488 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001489 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001490 }
1491}
1492
Willy Tarreau3421aba2017-07-27 15:41:03 +02001493/* Increase all streams' outgoing window size by the difference passed in
1494 * argument. This is needed upon receipt of the settings frame if the initial
1495 * window size is different. The difference may be negative and the resulting
1496 * window size as well, for the time it takes to receive some window updates.
1497 */
1498static void h2c_update_all_ws(struct h2c *h2c, int diff)
1499{
1500 struct h2s *h2s;
1501 struct eb32_node *node;
1502
1503 if (!diff)
1504 return;
1505
1506 node = eb32_first(&h2c->streams_by_id);
1507 while (node) {
1508 h2s = container_of(node, struct h2s, by_id);
1509 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001510
1511 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1512 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001513 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001514 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001515 }
1516
Willy Tarreau3421aba2017-07-27 15:41:03 +02001517 node = eb32_next(node);
1518 }
1519}
1520
1521/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1522 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001523 * return an error in h2c. The caller must have already verified frame length
1524 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001525 */
1526static int h2c_handle_settings(struct h2c *h2c)
1527{
1528 unsigned int offset;
1529 int error;
1530
1531 if (h2c->dff & H2_F_SETTINGS_ACK) {
1532 if (h2c->dfl) {
1533 error = H2_ERR_FRAME_SIZE_ERROR;
1534 goto fail;
1535 }
1536 return 1;
1537 }
1538
Willy Tarreau3421aba2017-07-27 15:41:03 +02001539 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001540 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001541 return 0;
1542
1543 /* parse the frame */
1544 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001545 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1546 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001547
1548 switch (type) {
1549 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1550 /* we need to update all existing streams with the
1551 * difference from the previous iws.
1552 */
1553 if (arg < 0) { // RFC7540#6.5.2
1554 error = H2_ERR_FLOW_CONTROL_ERROR;
1555 goto fail;
1556 }
1557 h2c_update_all_ws(h2c, arg - h2c->miw);
1558 h2c->miw = arg;
1559 break;
1560 case H2_SETTINGS_MAX_FRAME_SIZE:
1561 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1562 error = H2_ERR_PROTOCOL_ERROR;
1563 goto fail;
1564 }
1565 h2c->mfs = arg;
1566 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001567 case H2_SETTINGS_ENABLE_PUSH:
1568 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1569 error = H2_ERR_PROTOCOL_ERROR;
1570 goto fail;
1571 }
1572 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001573 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1574 if (h2c->flags & H2_CF_IS_BACK) {
1575 /* the limit is only for the backend; for the frontend it is our limit */
1576 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1577 arg = h2_settings_max_concurrent_streams;
1578 h2c->streams_limit = arg;
1579 }
1580 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001581 }
1582 }
1583
1584 /* need to ACK this frame now */
1585 h2c->st0 = H2_CS_FRAME_A;
1586 return 1;
1587 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001588 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001589 h2c_error(h2c, error);
1590 return 0;
1591}
1592
1593/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1594 * success or one of the h2_status values.
1595 */
1596static int h2c_ack_settings(struct h2c *h2c)
1597{
1598 struct buffer *res;
1599 char str[9];
1600 int ret = -1;
1601
1602 if (h2c_mux_busy(h2c, NULL)) {
1603 h2c->flags |= H2_CF_DEM_MBUSY;
1604 return 0;
1605 }
1606
Willy Tarreau44e973f2018-03-01 17:49:30 +01001607 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001608 if (!res) {
1609 h2c->flags |= H2_CF_MUX_MALLOC;
1610 h2c->flags |= H2_CF_DEM_MROOM;
1611 return 0;
1612 }
1613
1614 memcpy(str,
1615 "\x00\x00\x00" /* length : 0 (no data) */
1616 "\x04" "\x01" /* type : 4, flags : ACK */
1617 "\x00\x00\x00\x00" /* stream ID */, 9);
1618
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001619 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001620 if (unlikely(ret <= 0)) {
1621 if (!ret) {
1622 h2c->flags |= H2_CF_MUX_MFULL;
1623 h2c->flags |= H2_CF_DEM_MROOM;
1624 return 0;
1625 }
1626 else {
1627 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1628 return 0;
1629 }
1630 }
1631 return ret;
1632}
1633
Willy Tarreaucf68c782017-10-10 17:11:41 +02001634/* processes a PING frame and schedules an ACK if needed. The caller must pass
1635 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001636 * missing data. The caller must have already verified frame length
1637 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001638 */
1639static int h2c_handle_ping(struct h2c *h2c)
1640{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001641 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001642 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001643 h2c->st0 = H2_CS_FRAME_A;
1644 return 1;
1645}
1646
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001647/* Try to send a window update for stream id <sid> and value <increment>.
1648 * Returns > 0 on success or zero on missing room or failure. It may return an
1649 * error in h2c.
1650 */
1651static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1652{
1653 struct buffer *res;
1654 char str[13];
1655 int ret = -1;
1656
1657 if (h2c_mux_busy(h2c, NULL)) {
1658 h2c->flags |= H2_CF_DEM_MBUSY;
1659 return 0;
1660 }
1661
Willy Tarreau44e973f2018-03-01 17:49:30 +01001662 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001663 if (!res) {
1664 h2c->flags |= H2_CF_MUX_MALLOC;
1665 h2c->flags |= H2_CF_DEM_MROOM;
1666 return 0;
1667 }
1668
1669 /* length: 4, type: 8, flags: none */
1670 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1671 write_n32(str + 5, sid);
1672 write_n32(str + 9, increment);
1673
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001674 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001675
1676 if (unlikely(ret <= 0)) {
1677 if (!ret) {
1678 h2c->flags |= H2_CF_MUX_MFULL;
1679 h2c->flags |= H2_CF_DEM_MROOM;
1680 return 0;
1681 }
1682 else {
1683 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1684 return 0;
1685 }
1686 }
1687 return ret;
1688}
1689
1690/* try to send pending window update for the connection. It's safe to call it
1691 * with no pending updates. Returns > 0 on success or zero on missing room or
1692 * failure. It may return an error in h2c.
1693 */
1694static int h2c_send_conn_wu(struct h2c *h2c)
1695{
1696 int ret = 1;
1697
1698 if (h2c->rcvd_c <= 0)
1699 return 1;
1700
Willy Tarreau97aaa672018-12-23 09:49:04 +01001701 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1702 /* increase the advertised connection window to 2G on
1703 * first update.
1704 */
1705 h2c->flags |= H2_CF_WINDOW_OPENED;
1706 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1707 }
1708
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001709 /* send WU for the connection */
1710 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1711 if (ret > 0)
1712 h2c->rcvd_c = 0;
1713
1714 return ret;
1715}
1716
1717/* try to send pending window update for the current dmux stream. It's safe to
1718 * call it with no pending updates. Returns > 0 on success or zero on missing
1719 * room or failure. It may return an error in h2c.
1720 */
1721static int h2c_send_strm_wu(struct h2c *h2c)
1722{
1723 int ret = 1;
1724
1725 if (h2c->rcvd_s <= 0)
1726 return 1;
1727
1728 /* send WU for the stream */
1729 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1730 if (ret > 0)
1731 h2c->rcvd_s = 0;
1732
1733 return ret;
1734}
1735
Willy Tarreaucf68c782017-10-10 17:11:41 +02001736/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1737 * success, 0 on missing data or one of the h2_status values.
1738 */
1739static int h2c_ack_ping(struct h2c *h2c)
1740{
1741 struct buffer *res;
1742 char str[17];
1743 int ret = -1;
1744
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001745 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001746 return 0;
1747
1748 if (h2c_mux_busy(h2c, NULL)) {
1749 h2c->flags |= H2_CF_DEM_MBUSY;
1750 return 0;
1751 }
1752
Willy Tarreau44e973f2018-03-01 17:49:30 +01001753 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001754 if (!res) {
1755 h2c->flags |= H2_CF_MUX_MALLOC;
1756 h2c->flags |= H2_CF_DEM_MROOM;
1757 return 0;
1758 }
1759
1760 memcpy(str,
1761 "\x00\x00\x08" /* length : 8 (same payload) */
1762 "\x06" "\x01" /* type : 6, flags : ACK */
1763 "\x00\x00\x00\x00" /* stream ID */, 9);
1764
1765 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001766 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001767
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001768 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001769 if (unlikely(ret <= 0)) {
1770 if (!ret) {
1771 h2c->flags |= H2_CF_MUX_MFULL;
1772 h2c->flags |= H2_CF_DEM_MROOM;
1773 return 0;
1774 }
1775 else {
1776 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1777 return 0;
1778 }
1779 }
1780 return ret;
1781}
1782
Willy Tarreau26f95952017-07-27 17:18:30 +02001783/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1784 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001785 * h2c or h2s. The caller must have already verified frame length and stream ID
1786 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001787 */
1788static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1789{
1790 int32_t inc;
1791 int error;
1792
Willy Tarreau26f95952017-07-27 17:18:30 +02001793 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001794 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001795 return 0;
1796
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001797 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001798
1799 if (h2c->dsi != 0) {
1800 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001801
1802 /* it's not an error to receive WU on a closed stream */
1803 if (h2s->st == H2_SS_CLOSED)
1804 return 1;
1805
1806 if (!inc) {
1807 error = H2_ERR_PROTOCOL_ERROR;
1808 goto strm_err;
1809 }
1810
1811 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1812 error = H2_ERR_FLOW_CONTROL_ERROR;
1813 goto strm_err;
1814 }
1815
1816 h2s->mws += inc;
1817 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1818 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001819 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Olivier Houcharddddfe312018-10-10 18:51:00 +02001820 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001821 }
1822 }
1823 else {
1824 /* connection window update */
1825 if (!inc) {
1826 error = H2_ERR_PROTOCOL_ERROR;
1827 goto conn_err;
1828 }
1829
1830 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1831 error = H2_ERR_FLOW_CONTROL_ERROR;
1832 goto conn_err;
1833 }
1834
1835 h2c->mws += inc;
1836 }
1837
1838 return 1;
1839
1840 conn_err:
1841 h2c_error(h2c, error);
1842 return 0;
1843
1844 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001845 h2s_error(h2s, error);
1846 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001847 return 0;
1848}
1849
Willy Tarreaue96b0922017-10-30 00:28:29 +01001850/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001851 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1852 * have already verified frame length and stream ID validity. Described in
1853 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001854 */
1855static int h2c_handle_goaway(struct h2c *h2c)
1856{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001857 int last;
1858
Willy Tarreaue96b0922017-10-30 00:28:29 +01001859 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001860 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001861 return 0;
1862
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001863 last = h2_get_n32(&h2c->dbuf, 0);
1864 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001865 if (h2c->last_sid < 0)
1866 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02001867 h2_wake_some_streams(h2c, last);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001868 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001869}
1870
Willy Tarreau92153fc2017-12-03 19:46:19 +01001871/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001872 * invalid. Returns > 0 on success or zero on missing data. It may return an
1873 * error in h2c. The caller must have already verified frame length and stream
1874 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001875 */
1876static int h2c_handle_priority(struct h2c *h2c)
1877{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001878 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001879 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001880 return 0;
1881
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001882 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001883 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001884 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1885 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001886 }
1887 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001888}
1889
Willy Tarreaucd234e92017-08-18 10:59:39 +02001890/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001891 * Returns > 0 on success or zero on missing data. The caller must have already
1892 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001893 */
1894static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1895{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001896 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001897 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001898 return 0;
1899
1900 /* late RST, already handled */
1901 if (h2s->st == H2_SS_CLOSED)
1902 return 1;
1903
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001904 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001905 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001906
1907 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001908 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001909 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001910 }
1911
1912 h2s->flags |= H2_SF_RST_RCVD;
1913 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001914}
1915
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001916/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1917 * It may return an error in h2c or h2s. The caller must consider that the
1918 * return value is the new h2s in case one was allocated (most common case).
1919 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001920 * errors here are reported as connection errors since it's impossible to
1921 * recover from such errors after the compression context has been altered.
1922 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001923static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001924{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001925 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001926 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001927 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001928 int error;
1929
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001930 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001931 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001932
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001933 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001934 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001935
1936 /* now either the frame is complete or the buffer is complete */
1937 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001938 /* The stream exists/existed, this must be a trailers frame */
1939 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02001940 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
1941 /* unrecoverable error ? */
1942 if (h2c->st0 >= H2_CS_ERROR)
1943 goto out;
1944
1945 if (error == 0)
1946 goto out; // missing data
1947
1948 if (error < 0) {
1949 /* Failed to decode this frame (e.g. too large request)
1950 * but the HPACK decompressor is still synchronized.
1951 */
1952 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1953 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01001954 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02001955 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01001956 goto done;
1957 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001958 /* the connection was already killed by an RST, let's consume
1959 * the data and send another RST.
1960 */
1961 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1962 h2s = (struct h2s*)h2_error_stream;
1963 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001964 }
1965 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1966 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1967 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001968 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001969 goto conn_err;
1970 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001971 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1972 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001973
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001974 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001975
Willy Tarreau25919232019-01-03 14:48:18 +01001976 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001977 if (h2c->st0 >= H2_CS_ERROR)
1978 goto out;
1979
Willy Tarreau25919232019-01-03 14:48:18 +01001980 if (error <= 0) {
1981 if (error == 0)
1982 goto out; // missing data
1983
1984 /* Failed to decode this stream (e.g. too large request)
1985 * but the HPACK decompressor is still synchronized.
1986 */
1987 h2s = (struct h2s*)h2_error_stream;
1988 goto send_rst;
1989 }
1990
Willy Tarreau22de8d32018-09-05 19:55:58 +02001991 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001992 * positively from h2c_frt_stream_new(), the stream will report the error,
1993 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001994 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001995 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001996 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001997 h2s = (struct h2s*)h2_refused_stream;
1998 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001999 }
2000
2001 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002002 h2s->rxbuf = rxbuf;
2003 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002004 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002005
Willy Tarreau88d138e2019-01-02 19:38:14 +01002006 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002007 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002008 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002009
2010 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002011 if (h2s->st == H2_SS_OPEN)
2012 h2s->st = H2_SS_HREM;
2013 else
2014 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002015 }
2016
Willy Tarreau3a429f02019-01-03 11:41:50 +01002017 /* update the max stream ID if the request is being processed */
2018 if (h2s->id > h2c->max_id)
2019 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002020
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002021 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002022
2023 conn_err:
2024 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002025 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002026
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002027 out:
2028 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002029 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002030
2031 send_rst:
2032 /* make the demux send an RST for the current stream. We may only
2033 * do this if we're certain that the HEADERS frame was properly
2034 * decompressed so that the HPACK decoder is still kept up to date.
2035 */
2036 h2_release_buf(h2c, &rxbuf);
2037 h2c->st0 = H2_CS_FRAME_E;
2038 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002039}
2040
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002041/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2042 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2043 * errors here are reported as connection errors since it's impossible to
2044 * recover from such errors after the compression context has been altered.
2045 */
2046static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2047{
2048 int error;
2049
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002050 if (!b_size(&h2c->dbuf))
2051 return NULL; // empty buffer
2052
2053 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2054 return NULL; // incomplete frame
2055
Willy Tarreau1915ca22019-01-24 11:49:37 +01002056 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002057
Willy Tarreau25919232019-01-03 14:48:18 +01002058 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002059 if (h2c->st0 >= H2_CS_ERROR)
2060 return NULL;
2061
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002062 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2063 /* RFC7540#5.1 */
2064 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2065 h2c->st0 = H2_CS_FRAME_E;
2066 return NULL;
2067 }
2068
Willy Tarreau25919232019-01-03 14:48:18 +01002069 if (error <= 0) {
2070 if (error == 0)
2071 return NULL; // missing data
2072
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002073 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002074 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002075 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002076 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002077 }
2078
Christopher Fauletfa922f02019-05-07 10:55:17 +02002079 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002080 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002081
Willy Tarreau927b88b2019-03-04 08:03:25 +01002082 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002083 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002084 else if (h2s->flags & H2_SF_ES_RCVD) {
2085 if (h2s->st == H2_SS_OPEN)
2086 h2s->st = H2_SS_HREM;
2087 else if (h2s->st == H2_SS_HLOC)
2088 h2s_close(h2s);
2089 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002090
2091 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002092}
2093
Willy Tarreau454f9052017-10-26 19:40:35 +02002094/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2095 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2096 */
2097static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2098{
2099 int error;
2100
2101 /* note that empty DATA frames are perfectly valid and sometimes used
2102 * to signal an end of stream (with the ES flag).
2103 */
2104
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002105 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002106 return 0; // empty buffer
2107
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002108 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002109 return 0; // incomplete frame
2110
2111 /* now either the frame is complete or the buffer is complete */
2112
Willy Tarreau454f9052017-10-26 19:40:35 +02002113 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2114 /* RFC7540#6.1 */
2115 error = H2_ERR_STREAM_CLOSED;
2116 goto strm_err;
2117 }
2118
Willy Tarreau1915ca22019-01-24 11:49:37 +01002119 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2120 /* RFC7540#8.1.2 */
2121 error = H2_ERR_PROTOCOL_ERROR;
2122 goto strm_err;
2123 }
2124
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002125 if (!h2_frt_transfer_data(h2s))
2126 return 0;
2127
Willy Tarreau454f9052017-10-26 19:40:35 +02002128 /* call the upper layers to process the frame, then let the upper layer
2129 * notify the stream about any change.
2130 */
2131 if (!h2s->cs) {
2132 error = H2_ERR_STREAM_CLOSED;
2133 goto strm_err;
2134 }
2135
Willy Tarreau8f650c32017-11-21 19:36:21 +01002136 if (h2c->st0 >= H2_CS_ERROR)
2137 return 0;
2138
Willy Tarreau721c9742017-11-07 11:05:42 +01002139 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002140 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002141 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002142 }
2143
2144 /* check for completion : the callee will change this to FRAME_A or
2145 * FRAME_H once done.
2146 */
2147 if (h2c->st0 == H2_CS_FRAME_P)
2148 return 0;
2149
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002150 /* last frame */
2151 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002152 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002153 if (h2s->st == H2_SS_OPEN)
2154 h2s->st = H2_SS_HREM;
2155 else
2156 h2s_close(h2s);
2157
Willy Tarreau1915ca22019-01-24 11:49:37 +01002158 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2159 /* RFC7540#8.1.2 */
2160 error = H2_ERR_PROTOCOL_ERROR;
2161 goto strm_err;
2162 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002163 }
2164
Willy Tarreau454f9052017-10-26 19:40:35 +02002165 return 1;
2166
Willy Tarreau454f9052017-10-26 19:40:35 +02002167 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002168 h2s_error(h2s, error);
2169 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002170 return 0;
2171}
2172
Willy Tarreaubc933932017-10-09 16:21:43 +02002173/* process Rx frames to be demultiplexed */
2174static void h2_process_demux(struct h2c *h2c)
2175{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002176 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002177 struct h2_fh hdr;
2178 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002179
Willy Tarreau081d4722017-05-16 21:51:05 +02002180 if (h2c->st0 >= H2_CS_ERROR)
2181 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002182
2183 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2184 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002185 if (h2c->flags & H2_CF_IS_BACK)
2186 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002187 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2188 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002189 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002190 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002191 sess_log(h2c->conn->owner);
2192 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002193 goto fail;
2194 }
2195
2196 h2c->max_id = 0;
2197 h2c->st0 = H2_CS_SETTINGS1;
2198 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002199
2200 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002201 /* ensure that what is pending is a valid SETTINGS frame
2202 * without an ACK.
2203 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002204 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002205 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002206 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002207 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002208 sess_log(h2c->conn->owner);
2209 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002210 goto fail;
2211 }
2212
2213 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2214 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2215 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2216 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002217 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002218 goto fail;
2219 }
2220
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002221 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002222 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2223 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2224 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002225 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002226 goto fail;
2227 }
2228
Willy Tarreau3bf69182018-12-21 15:34:50 +01002229 /* that's OK, switch to FRAME_P to process it. This is
2230 * a SETTINGS frame whose header has already been
2231 * deleted above.
2232 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002233 padlen = 0;
2234 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002235 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002236 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002237
2238 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002239 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002240 int ret = 0;
2241
2242 if (h2c->st0 >= H2_CS_ERROR)
2243 break;
2244
2245 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002246 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002247 break;
2248
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002249 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002250 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002251 if (!h2c->nb_streams) {
2252 /* only log if no other stream can report the error */
2253 sess_log(h2c->conn->owner);
2254 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002255 break;
2256 }
2257
Willy Tarreau3bf69182018-12-21 15:34:50 +01002258 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2259 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2260 * we read the pad length and drop it from the remaining
2261 * payload (one byte + the 9 remaining ones = 10 total
2262 * removed), so we have a frame payload starting after the
2263 * pad len. Flow controlled frames (DATA) also count the
2264 * padlen in the flow control, so it must be adjusted.
2265 */
2266 if (hdr.len < 1) {
2267 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2268 sess_log(h2c->conn->owner);
2269 goto fail;
2270 }
2271 hdr.len--;
2272
2273 if (b_data(&h2c->dbuf) < 10)
2274 break; // missing padlen
2275
2276 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2277
2278 if (padlen > hdr.len) {
2279 /* RFC7540#6.1 : pad length = length of
2280 * frame payload or greater => error.
2281 */
2282 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2283 sess_log(h2c->conn->owner);
2284 goto fail;
2285 }
2286
2287 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2288 h2c->rcvd_c++;
2289 h2c->rcvd_s++;
2290 }
2291 b_del(&h2c->dbuf, 1);
2292 }
2293 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002294
2295 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002296 h2c->dfl = hdr.len;
2297 h2c->dsi = hdr.sid;
2298 h2c->dft = hdr.ft;
2299 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002300 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002301 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002302
2303 /* check for minimum basic frame format validity */
2304 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2305 if (ret != H2_ERR_NO_ERROR) {
2306 h2c_error(h2c, ret);
2307 sess_log(h2c->conn->owner);
2308 goto fail;
2309 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002310 }
2311
2312 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002313 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2314
Willy Tarreau567beb82018-12-18 16:52:44 +01002315 if (tmp_h2s != h2s && h2s && h2s->cs &&
2316 (b_data(&h2s->rxbuf) ||
Willy Tarreau99ad1b32019-05-14 11:46:28 +02002317 (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002318 (h2s->flags & H2_SF_ES_RCVD) ||
2319 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002320 /* we may have to signal the upper layers */
2321 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002322 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002323 }
2324 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002325
Willy Tarreaud7901432017-12-29 11:34:40 +01002326 if (h2c->st0 == H2_CS_FRAME_E)
2327 goto strm_err;
2328
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002329 if (h2s->st == H2_SS_IDLE &&
2330 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2331 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2332 * this state MUST be treated as a connection error
2333 */
2334 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002335 if (!h2c->nb_streams) {
2336 /* only log if no other stream can report the error */
2337 sess_log(h2c->conn->owner);
2338 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002339 break;
2340 }
2341
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002342 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2343 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2344 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002345 * this state MUST be treated as a stream error.
2346 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2347 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002348 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002349 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2350 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2351 else
2352 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002353 goto strm_err;
2354 }
2355
Willy Tarreauab837502017-12-27 15:07:30 +01002356 /* Below the management of frames received in closed state is a
2357 * bit hackish because the spec makes strong differences between
2358 * streams closed by receiving RST, sending RST, and seeing ES
2359 * in both directions. In addition to this, the creation of a
2360 * new stream reusing the identifier of a closed one will be
2361 * detected here. Given that we cannot keep track of all closed
2362 * streams forever, we consider that unknown closed streams were
2363 * closed on RST received, which allows us to respond with an
2364 * RST without breaking the connection (eg: to abort a transfer).
2365 * Some frames have to be silently ignored as well.
2366 */
2367 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002368 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002369 /* #5.1.1: The identifier of a newly
2370 * established stream MUST be numerically
2371 * greater than all streams that the initiating
2372 * endpoint has opened or reserved. This
2373 * governs streams that are opened using a
2374 * HEADERS frame and streams that are reserved
2375 * using PUSH_PROMISE. An endpoint that
2376 * receives an unexpected stream identifier
2377 * MUST respond with a connection error.
2378 */
2379 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2380 goto strm_err;
2381 }
2382
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002383 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002384 /* RFC7540#5.1:closed: an endpoint that
2385 * receives any frame other than PRIORITY after
2386 * receiving a RST_STREAM MUST treat that as a
2387 * stream error of type STREAM_CLOSED.
2388 *
2389 * Note that old streams fall into this category
2390 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002391 *
2392 * However, we cannot generalize this to all frame types. Those
2393 * carrying compression state must still be processed before
2394 * being dropped or we'll desynchronize the decoder. This can
2395 * happen with request trailers received after sending an
2396 * RST_STREAM, or with header/trailers responses received after
2397 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002398 */
2399 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2400 h2c->st0 = H2_CS_FRAME_E;
2401 goto strm_err;
2402 }
2403
2404 /* RFC7540#5.1:closed: if this state is reached as a
2405 * result of sending a RST_STREAM frame, the peer that
2406 * receives the RST_STREAM might have already sent
2407 * frames on the stream that cannot be withdrawn. An
2408 * endpoint MUST ignore frames that it receives on
2409 * closed streams after it has sent a RST_STREAM
2410 * frame. An endpoint MAY choose to limit the period
2411 * over which it ignores frames and treat frames that
2412 * arrive after this time as being in error.
2413 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002414 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002415 /* RFC7540#5.1:closed: any frame other than
2416 * PRIO/WU/RST in this state MUST be treated as
2417 * a connection error
2418 */
2419 if (h2c->dft != H2_FT_RST_STREAM &&
2420 h2c->dft != H2_FT_PRIORITY &&
2421 h2c->dft != H2_FT_WINDOW_UPDATE) {
2422 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2423 goto strm_err;
2424 }
2425 }
2426 }
2427
Willy Tarreauc0da1962017-10-30 18:38:00 +01002428#if 0
2429 // problem below: it is not possible to completely ignore such
2430 // streams as we need to maintain the compression state as well
2431 // and for this we need to completely process these frames (eg:
2432 // HEADERS frames) as well as counting DATA frames to emit
2433 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2434 // This is a typical case of layer violation where the
2435 // transported contents are critical to the connection's
2436 // validity and must be ignored at the same time :-(
2437
2438 /* graceful shutdown, ignore streams whose ID is higher than
2439 * the one advertised in GOAWAY. RFC7540#6.8.
2440 */
2441 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002442 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2443 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002444 h2c->dfl -= ret;
2445 ret = h2c->dfl == 0;
2446 goto strm_err;
2447 }
2448#endif
2449
Willy Tarreau7e98c052017-10-10 15:56:59 +02002450 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002451 case H2_FT_SETTINGS:
2452 if (h2c->st0 == H2_CS_FRAME_P)
2453 ret = h2c_handle_settings(h2c);
2454
2455 if (h2c->st0 == H2_CS_FRAME_A)
2456 ret = h2c_ack_settings(h2c);
2457 break;
2458
Willy Tarreaucf68c782017-10-10 17:11:41 +02002459 case H2_FT_PING:
2460 if (h2c->st0 == H2_CS_FRAME_P)
2461 ret = h2c_handle_ping(h2c);
2462
2463 if (h2c->st0 == H2_CS_FRAME_A)
2464 ret = h2c_ack_ping(h2c);
2465 break;
2466
Willy Tarreau26f95952017-07-27 17:18:30 +02002467 case H2_FT_WINDOW_UPDATE:
2468 if (h2c->st0 == H2_CS_FRAME_P)
2469 ret = h2c_handle_window_update(h2c, h2s);
2470 break;
2471
Willy Tarreau61290ec2017-10-17 08:19:21 +02002472 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002473 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2474 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2475 * frames' parsers consume all following CONTINUATION
2476 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002477 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002478 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2479 sess_log(h2c->conn->owner);
2480 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002481
Willy Tarreau13278b42017-10-13 19:23:14 +02002482 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002483 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002484 if (h2c->flags & H2_CF_IS_BACK)
2485 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2486 else
2487 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002488 if (tmp_h2s) {
2489 h2s = tmp_h2s;
2490 ret = 1;
2491 }
2492 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002493 break;
2494
Willy Tarreau454f9052017-10-26 19:40:35 +02002495 case H2_FT_DATA:
2496 if (h2c->st0 == H2_CS_FRAME_P)
2497 ret = h2c_frt_handle_data(h2c, h2s);
2498
2499 if (h2c->st0 == H2_CS_FRAME_A)
2500 ret = h2c_send_strm_wu(h2c);
2501 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002502
Willy Tarreau92153fc2017-12-03 19:46:19 +01002503 case H2_FT_PRIORITY:
2504 if (h2c->st0 == H2_CS_FRAME_P)
2505 ret = h2c_handle_priority(h2c);
2506 break;
2507
Willy Tarreaucd234e92017-08-18 10:59:39 +02002508 case H2_FT_RST_STREAM:
2509 if (h2c->st0 == H2_CS_FRAME_P)
2510 ret = h2c_handle_rst_stream(h2c, h2s);
2511 break;
2512
Willy Tarreaue96b0922017-10-30 00:28:29 +01002513 case H2_FT_GOAWAY:
2514 if (h2c->st0 == H2_CS_FRAME_P)
2515 ret = h2c_handle_goaway(h2c);
2516 break;
2517
Willy Tarreau1c661982017-10-30 13:52:01 +01002518 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002519 default:
2520 /* drop frames that we ignore. They may be larger than
2521 * the buffer so we drain all of their contents until
2522 * we reach the end.
2523 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002524 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2525 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002526 h2c->dfl -= ret;
2527 ret = h2c->dfl == 0;
2528 }
2529
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002530 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002531 /* We may have to send an RST if not done yet */
2532 if (h2s->st == H2_SS_ERROR)
2533 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002534
Willy Tarreaua20a5192017-12-27 11:02:06 +01002535 if (h2c->st0 == H2_CS_FRAME_E)
2536 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002537
Willy Tarreau7e98c052017-10-10 15:56:59 +02002538 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002539 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002540 break;
2541
2542 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002543 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002544 h2c->st0 = H2_CS_FRAME_H;
2545 }
2546 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002547
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002548 if (h2c->rcvd_c > 0 &&
2549 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2550 h2c_send_conn_wu(h2c);
2551
Willy Tarreau52eed752017-09-22 15:05:09 +02002552 fail:
2553 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002554 if (h2s && h2s->cs &&
2555 (b_data(&h2s->rxbuf) ||
Willy Tarreau99ad1b32019-05-14 11:46:28 +02002556 (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002557 (h2s->flags & H2_SF_ES_RCVD) ||
2558 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002559 /* we may have to signal the upper layers */
2560 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002561 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002562 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002563
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002564 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002565}
2566
2567/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2568 * the end.
2569 */
2570static int h2_process_mux(struct h2c *h2c)
2571{
Olivier Houchard998410a2019-04-15 19:23:37 +02002572 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002573
Willy Tarreau01b44822018-10-03 14:26:37 +02002574 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2575 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2576 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2577 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2578 if (h2c->st0 == H2_CS_ERROR) {
2579 h2c->st0 = H2_CS_ERROR2;
2580 sess_log(h2c->conn->owner);
2581 }
2582 goto fail;
2583 }
2584 h2c->st0 = H2_CS_SETTINGS1;
2585 }
2586 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002587 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002588 return 1;
2589 }
2590
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002591 /* start by sending possibly pending window updates */
2592 if (h2c->rcvd_c > 0 &&
2593 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2594 h2c_send_conn_wu(h2c) < 0)
2595 goto fail;
2596
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002597 /* First we always process the flow control list because the streams
2598 * waiting there were already elected for immediate emission but were
2599 * blocked just on this.
2600 */
2601
Olivier Houchard998410a2019-04-15 19:23:37 +02002602 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002603 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2604 h2c->st0 >= H2_CS_ERROR)
2605 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002606
Willy Tarreauc234ae32019-05-13 17:56:11 +02002607 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002608 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002609
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002610 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002611 /* For some reason, the upper layer failed to subsribe again,
2612 * so remove it from the send_list
2613 */
2614 if (!h2s->send_wait) {
2615 LIST_DEL_INIT(&h2s->list);
2616 continue;
2617 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002618 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002619 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002620 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002621 }
2622
Olivier Houchard998410a2019-04-15 19:23:37 +02002623 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002624 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2625 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002626
Willy Tarreauc234ae32019-05-13 17:56:11 +02002627 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002628 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002629
Olivier Houchard998410a2019-04-15 19:23:37 +02002630 /* For some reason, the upper layer failed to subsribe again,
2631 * so remove it from the send_list
2632 */
2633 if (!h2s->send_wait) {
2634 LIST_DEL_INIT(&h2s->list);
2635 continue;
2636 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002637 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002638 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002639 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002640 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002641 }
2642
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002643 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002644 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002645 if (h2c->st0 == H2_CS_ERROR) {
2646 if (h2c->max_id >= 0) {
2647 h2c_send_goaway_error(h2c, NULL);
2648 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2649 return 0;
2650 }
2651
2652 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2653 }
2654 return 1;
2655 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002656 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002657}
2658
Willy Tarreau62f52692017-10-08 23:01:42 +02002659
Willy Tarreau479998a2018-11-18 06:30:59 +01002660/* Attempt to read data, and subscribe if none available.
2661 * The function returns 1 if data has been received, otherwise zero.
2662 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002663static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002664{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002665 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002666 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002667 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002668 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002669
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002670 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002671 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002672
Willy Tarreau315d8072017-12-10 22:17:57 +01002673 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002674 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002675
Willy Tarreau44e973f2018-03-01 17:49:30 +01002676 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002677 if (!buf) {
2678 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002679 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002680 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002681
Olivier Houchard7505f942018-08-21 18:10:44 +02002682 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002683 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002684 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2685 /* HTX in use : try to pre-align the buffer like the
2686 * rxbufs will be to optimize memory copies. We'll make
2687 * sure that the frame header lands at the end of the
2688 * HTX block to alias it upon recv. We cannot use the
2689 * head because rcv_buf() will realign the buffer if
2690 * it's empty. Thus we cheat and pretend we already
2691 * have a few bytes there.
2692 */
2693 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002694 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002695 }
2696 else
2697 max = b_room(buf);
2698
Olivier Houchard7505f942018-08-21 18:10:44 +02002699 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002700 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002701 else
2702 ret = 0;
2703 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002704
Olivier Houchard53216e72018-10-10 15:46:36 +02002705 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002706 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002707
Olivier Houcharda1411e62018-08-17 18:42:48 +02002708 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002709 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002710 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002711 }
2712
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002713 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002714 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002715 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002716}
2717
Willy Tarreau479998a2018-11-18 06:30:59 +01002718/* Try to send data if possible.
2719 * The function returns 1 if data have been sent, otherwise zero.
2720 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002721static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002722{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002723 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002724 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002725 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002726
2727 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002728 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002729
Olivier Houchard7505f942018-08-21 18:10:44 +02002730
Willy Tarreaua2af5122017-10-09 11:56:46 +02002731 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2732 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002733 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002734 }
2735
Willy Tarreaubc933932017-10-09 16:21:43 +02002736 /* This loop is quite simple : it tries to fill as much as it can from
2737 * pending streams into the existing buffer until it's reportedly full
2738 * or the end of send requests is reached. Then it tries to send this
2739 * buffer's contents out, marks it not full if at least one byte could
2740 * be sent, and tries again.
2741 *
2742 * The snd_buf() function normally takes a "flags" argument which may
2743 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2744 * data immediately comes and CO_SFL_STREAMER to indicate that the
2745 * connection is streaming lots of data (used to increase TLS record
2746 * size at the expense of latency). The former can be sent any time
2747 * there's a buffer full flag, as it indicates at least one stream
2748 * attempted to send and failed so there are pending data. An
2749 * alternative would be to set it as long as there's an active stream
2750 * but that would be problematic for ACKs until we have an absolute
2751 * guarantee that all waiters have at least one byte to send. The
2752 * latter should possibly not be set for now.
2753 */
2754
2755 done = 0;
2756 while (!done) {
2757 unsigned int flags = 0;
2758
2759 /* fill as much as we can into the current buffer */
2760 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2761 done = h2_process_mux(h2c);
2762
Olivier Houchard2b094432019-01-29 18:28:36 +01002763 if (h2c->flags & H2_CF_MUX_MALLOC)
2764 break;
2765
Willy Tarreaubc933932017-10-09 16:21:43 +02002766 if (conn->flags & CO_FL_ERROR)
2767 break;
2768
2769 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2770 flags |= CO_SFL_MSG_MORE;
2771
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002772 if (b_data(&h2c->mbuf)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002773 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002774 if (!ret)
2775 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002776 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002777 b_del(&h2c->mbuf, ret);
2778 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002779 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002780
2781 /* wrote at least one byte, the buffer is not full anymore */
2782 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2783 }
2784
Willy Tarreaua2af5122017-10-09 11:56:46 +02002785 if (conn->flags & CO_FL_SOCK_WR_SH) {
2786 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002787 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002788 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002789 /* We're not full anymore, so we can wake any task that are waiting
2790 * for us.
2791 */
2792 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002793 struct h2s *h2s;
2794
2795 list_for_each_entry(h2s, &h2c->send_list, list) {
2796 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2797 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002798
Willy Tarreauc234ae32019-05-13 17:56:11 +02002799 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002800 continue;
2801
Olivier Houchard998410a2019-04-15 19:23:37 +02002802 /* For some reason, the upper layer failed to subsribe again,
2803 * so remove it from the send_list
2804 */
2805 if (!h2s->send_wait) {
2806 LIST_DEL_INIT(&h2s->list);
2807 continue;
2808 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002809 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002810 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002811 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002812 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002813 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002814 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002815 /* We're done, no more to send */
2816 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002817 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002818schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002819 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002820 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002821 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002822}
2823
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002824/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002825static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2826{
2827 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002828 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002829
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002830 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002831 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002832 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002833 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002834 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002835 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002836 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002837}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002838
Willy Tarreau62f52692017-10-08 23:01:42 +02002839/* callback called on any event by the connection handler.
2840 * It applies changes and returns zero, or < 0 if it wants immediate
2841 * destruction of the connection (which normally doesn not happen in h2).
2842 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002843static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002844{
Olivier Houchard7505f942018-08-21 18:10:44 +02002845 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002846
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002847 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002848 h2_process_demux(h2c);
2849
2850 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002851 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002852
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002853 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002854 h2c->flags &= ~H2_CF_DEM_DFULL;
2855 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002856 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002857
Willy Tarreau0b37d652018-10-03 10:33:02 +02002858 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002859 /* frontend is stopping, reload likely in progress, let's try
2860 * to announce a graceful shutdown if not yet done. We don't
2861 * care if it fails, it will be tried again later.
2862 */
2863 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2864 if (h2c->last_sid < 0)
2865 h2c->last_sid = (1U << 31) - 1;
2866 h2c_send_goaway_error(h2c, NULL);
2867 }
2868 }
2869
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002870 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002871 * If we received early data, and the handshake is done, wake
2872 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002873 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002874 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2875 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2876 struct eb32_node *node;
2877 struct h2s *h2s;
2878
2879 h2c->flags |= H2_CF_WAIT_FOR_HS;
2880 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2881
2882 while (node) {
2883 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002884 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002885 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002886 node = eb32_next(node);
2887 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002888 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002889
Willy Tarreau26bd7612017-10-09 16:47:04 +02002890 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002891 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2892 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2893 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02002894 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002895
2896 if (eb_is_empty(&h2c->streams_by_id)) {
2897 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002898 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002899 return -1;
2900 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002901 }
2902
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002903 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002904 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002905
Olivier Houchard53216e72018-10-10 15:46:36 +02002906 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2907 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2908 (h2c->st0 != H2_CS_ERROR &&
2909 !b_data(&h2c->mbuf) &&
2910 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2911 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002912 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002913
Willy Tarreau3f133572017-10-31 19:21:06 +01002914 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002915 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002916 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002917 task_queue(h2c->task);
2918 }
2919 else
2920 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002921 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002922
Olivier Houchard7505f942018-08-21 18:10:44 +02002923 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002924 return 0;
2925}
2926
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002927/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002928static int h2_wake(struct connection *conn)
2929{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002930 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002931
2932 return (h2_process(h2c));
2933}
2934
Willy Tarreauea392822017-10-31 10:02:25 +01002935/* Connection timeout management. The principle is that if there's no receipt
2936 * nor sending for a certain amount of time, the connection is closed. If the
2937 * MUX buffer still has lying data or is not allocatable, the connection is
2938 * immediately killed. If it's allocatable and empty, we attempt to send a
2939 * GOAWAY frame.
2940 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002941static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002942{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002943 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002944 int expired = tick_is_expired(t->expire, now_ms);
2945
Willy Tarreau0975f112018-03-29 15:22:59 +02002946 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002947 return t;
2948
Olivier Houchard3f795f72019-04-17 22:51:06 +02002949 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02002950
2951 if (!h2c) {
2952 /* resources were already deleted */
2953 return NULL;
2954 }
2955
2956 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002957 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02002958 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01002959
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002960 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002961 /* don't even try to send a GOAWAY, the buffer is stuck */
2962 h2c->flags |= H2_CF_GOAWAY_FAILED;
2963 }
2964
2965 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002966 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002967 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2968 h2c->flags |= H2_CF_GOAWAY_FAILED;
2969
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002970 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002971 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002972 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002973 b_del(&h2c->mbuf, ret);
2974 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002975 }
2976 }
Willy Tarreauea392822017-10-31 10:02:25 +01002977
Willy Tarreau0975f112018-03-29 15:22:59 +02002978 /* either we can release everything now or it will be done later once
2979 * the last stream closes.
2980 */
2981 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002982 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002983
Willy Tarreauea392822017-10-31 10:02:25 +01002984 return NULL;
2985}
2986
2987
Willy Tarreau62f52692017-10-08 23:01:42 +02002988/*******************************************/
2989/* functions below are used by the streams */
2990/*******************************************/
2991
2992/*
2993 * Attach a new stream to a connection
2994 * (Used for outgoing connections)
2995 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002996static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002997{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002998 struct conn_stream *cs;
2999 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003000 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003001
3002 cs = cs_new(conn);
3003 if (!cs)
3004 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003005 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003006 if (!h2s) {
3007 cs_free(cs);
3008 return NULL;
3009 }
3010 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003011}
3012
Willy Tarreaufafd3982018-11-18 21:29:20 +01003013/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3014 * We have to scan because we may have some orphan streams. It might be
3015 * beneficial to scan backwards from the end to reduce the likeliness to find
3016 * orphans.
3017 */
3018static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3019{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003020 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003021 struct h2s *h2s;
3022 struct eb32_node *node;
3023
3024 node = eb32_first(&h2c->streams_by_id);
3025 while (node) {
3026 h2s = container_of(node, struct h2s, by_id);
3027 if (h2s->cs)
3028 return h2s->cs;
3029 node = eb32_next(node);
3030 }
3031 return NULL;
3032}
3033
Willy Tarreau62f52692017-10-08 23:01:42 +02003034/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003035 * Destroy the mux and the associated connection, if it is no longer used
3036 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003037static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003038{
Christopher Faulet73c12072019-04-08 11:23:22 +02003039 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003040
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003041 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003042 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003043}
3044
3045/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003046 * Detach the stream from the connection and possibly release the connection.
3047 */
3048static void h2_detach(struct conn_stream *cs)
3049{
Willy Tarreau60935142017-10-16 18:11:19 +02003050 struct h2s *h2s = cs->ctx;
3051 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003052 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003053
3054 cs->ctx = NULL;
3055 if (!h2s)
3056 return;
3057
Olivier Houchard998410a2019-04-15 19:23:37 +02003058 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003059 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003060 h2s->send_wait != &h2s->wait_event) {
3061 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
3062 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003063 /*
3064 * At this point, the stream_interface is supposed to have called
3065 * h2_unsubscribe(), so the only way there's still a
3066 * subscription that came from the stream_interface (as we
3067 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3068 * without the stream_interface involved) is that we subscribed
3069 * for sending, we woke the tasklet up and removed the
3070 * SUB_RETRY_SEND flag, so the stream_interface would not
3071 * know it has to unsubscribe for send, but the tasklet hasn't
3072 * run yet. Make sure to handle that by explicitely setting
3073 * send_wait to NULL, as nothing else will do it for us.
3074 */
3075 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003076 }
3077
Olivier Houchardf502aca2018-12-14 19:42:40 +01003078 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003079 h2c = h2s->h2c;
3080 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003081 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003082 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3083 !h2_frt_has_too_many_cs(h2c)) {
3084 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003085 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003086 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003087 }
Willy Tarreau60935142017-10-16 18:11:19 +02003088
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003089 /* this stream may be blocked waiting for some data to leave (possibly
3090 * an ES or RST frame), so orphan it in this case.
3091 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003092 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003093 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003094 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003095 return;
3096
Willy Tarreau45f752e2017-10-30 15:44:59 +01003097 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3098 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3099 /* unblock the connection if it was blocked on this
3100 * stream.
3101 */
3102 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3103 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003104 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003105 }
3106
Willy Tarreau71049cc2018-03-28 13:56:39 +02003107 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003108
Olivier Houchard8a786902018-12-15 16:05:40 +01003109 if (h2c->flags & H2_CF_IS_BACK &&
3110 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003111 if (!(h2c->conn->flags &
3112 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3113 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003114 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003115 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3116 h2c->conn->owner = NULL;
3117 if (eb_is_empty(&h2c->streams_by_id)) {
3118 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3119 /* The server doesn't want it, let's kill the connection right away */
3120 h2c->conn->mux->destroy(h2c->conn);
3121 return;
3122 }
3123 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003124 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003125 if (eb_is_empty(&h2c->streams_by_id)) {
3126 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3127 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3128 return;
3129 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003130 /* Never ever allow to reuse a connection from a non-reuse backend */
3131 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3132 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003133 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003134 struct server *srv = objt_server(h2c->conn->target);
3135
3136 if (srv) {
3137 if (h2c->conn->flags & CO_FL_PRIVATE)
3138 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3139 else
3140 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3141 }
3142
3143 }
3144 }
3145 }
3146
Willy Tarreaue323f342018-03-28 13:51:45 +02003147 /* We don't want to close right now unless we're removing the
3148 * last stream, and either the connection is in error, or it
3149 * reached the ID already specified in a GOAWAY frame received
3150 * or sent (as seen by last_sid >= 0).
3151 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003152 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003153 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003154 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003155 }
3156 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003157 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003158 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3159 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003160 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003161 else
3162 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003163 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003164}
3165
Willy Tarreau88bdba32019-05-13 18:17:53 +02003166/* Performs a synchronous or asynchronous shutr(). */
3167static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003168{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003169 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003170 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003171
Willy Tarreauf983d002019-05-14 10:40:21 +02003172 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003173 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003174
Willy Tarreau18059042019-01-31 19:12:48 +01003175 /* a connstream may require us to immediately kill the whole connection
3176 * for example because of a "tcp-request content reject" rule that is
3177 * normally used to limit abuse. In this case we schedule a goaway to
3178 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003179 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003180 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003181 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3182 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3183 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3184 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003185 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3186 /* Nothing was never sent for this stream, so reset with
3187 * REFUSED_STREAM error to let the client retry the
3188 * request.
3189 */
3190 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3191 }
Willy Tarreau18059042019-01-31 19:12:48 +01003192
Willy Tarreau90c32322017-11-24 08:00:30 +01003193 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003194 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003195 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003196
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003197 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003198 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003199 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003200 done:
3201 h2s->flags &= ~H2_SF_WANT_SHUTR;
3202 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003203add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003204 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003205 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003206 if (h2s->flags & H2_SF_BLK_MFCTL) {
3207 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3208 h2s->send_wait = sw;
3209 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3210 h2s->send_wait = sw;
3211 LIST_ADDQ(&h2c->send_list, &h2s->list);
3212 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003213 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003214 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003215 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003216 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003217}
3218
Willy Tarreau88bdba32019-05-13 18:17:53 +02003219/* Performs a synchronous or asynchronous shutw(). */
3220static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003221{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003222 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003223 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003224
Willy Tarreauf983d002019-05-14 10:40:21 +02003225 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003226 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003227
Willy Tarreauf983d002019-05-14 10:40:21 +02003228 if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR &&
3229 (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003230 /* we can cleanly close using an empty data frame only after headers */
3231
3232 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3233 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003234 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003235
3236 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003237 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003238 else
3239 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003240 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003241 /* a connstream may require us to immediately kill the whole connection
3242 * for example because of a "tcp-request content reject" rule that is
3243 * normally used to limit abuse. In this case we schedule a goaway to
3244 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003245 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003246 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003247 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3248 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3249 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3250 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003251 else {
3252 /* Nothing was never sent for this stream, so reset with
3253 * REFUSED_STREAM error to let the client retry the
3254 * request.
3255 */
3256 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3257 }
Willy Tarreau18059042019-01-31 19:12:48 +01003258
Willy Tarreau90c32322017-11-24 08:00:30 +01003259 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003260 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003261 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003262
Willy Tarreau00dd0782018-03-01 16:31:34 +01003263 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003264 }
3265
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003266 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003267 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003268 done:
3269 h2s->flags &= ~H2_SF_WANT_SHUTW;
3270 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003271
3272 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003273 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003274 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003275 if (h2s->flags & H2_SF_BLK_MFCTL) {
3276 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3277 h2s->send_wait = sw;
3278 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3279 h2s->send_wait = sw;
3280 LIST_ADDQ(&h2c->send_list, &h2s->list);
3281 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003282 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003283 /* let the handler know we want to shutw */
3284 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003285 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003286}
3287
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003288/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3289 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3290 * and prevented the last frame from being emitted.
3291 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003292static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3293{
3294 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003295 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003296
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003297 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003298 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003299 h2_do_shutw(h2s);
3300
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003301 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003302 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003303
Willy Tarreau88bdba32019-05-13 18:17:53 +02003304 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003305 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003306 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003307
Willy Tarreau88bdba32019-05-13 18:17:53 +02003308 if (!h2s->cs) {
3309 h2s_destroy(h2s);
3310 if (h2c_is_dead(h2c))
3311 h2_release(h2c);
3312 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003313 }
3314
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003315 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003316}
3317
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003318/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003319static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3320{
3321 struct h2s *h2s = cs->ctx;
3322
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003323 if (cs->flags & CS_FL_KILL_CONN)
3324 h2s->flags |= H2_SF_KILL_CONN;
3325
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003326 if (!mode)
3327 return;
3328
3329 h2_do_shutr(h2s);
3330}
3331
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003332/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003333static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3334{
3335 struct h2s *h2s = cs->ctx;
3336
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003337 if (cs->flags & CS_FL_KILL_CONN)
3338 h2s->flags |= H2_SF_KILL_CONN;
3339
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003340 h2_do_shutw(h2s);
3341}
3342
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003343/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003344 * HTX request or response depending on the connection's side. Returns a
3345 * positive value on success, a negative value on failure, or 0 if it couldn't
3346 * proceed. May report connection errors in h2c->errcode if the frame is
3347 * non-decodable and the connection unrecoverable. In absence of connection
3348 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003349 *
3350 * The function may fold CONTINUATION frames into the initial HEADERS frame
3351 * by removing padding and next frame header, then moving the CONTINUATION
3352 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3353 * leaving a hole between the main frame and the beginning of the next one.
3354 * The possibly remaining incomplete or next frame at the end may be moved
3355 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3356 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3357 *
3358 * A buffer at the beginning of processing may look like this :
3359 *
3360 * ,---.---------.-----.--------------.--------------.------.---.
3361 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3362 * `---^---------^-----^--------------^--------------^------^---'
3363 * | | <-----> | |
3364 * area | dpl | wrap
3365 * |<--------------> |
3366 * | dfl |
3367 * |<-------------------------------------------------->|
3368 * head data
3369 *
3370 * Padding is automatically overwritten when folding, participating to the
3371 * hole size after dfl :
3372 *
3373 * ,---.------------------------.-----.--------------.------.---.
3374 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3375 * `---^------------------------^-----^--------------^------^---'
3376 * | | <-----> | |
3377 * area | hole | wrap
3378 * |<-----------------------> |
3379 * | dfl |
3380 * |<-------------------------------------------------->|
3381 * head data
3382 *
3383 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3384 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3385 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003386 *
3387 * The <flags> field must point to either the stream's flags or to a copy of it
3388 * so that the function can update the following flags :
3389 * - H2_SF_DATA_CLEN when content-length is seen
3390 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3391 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003392 *
3393 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3394 * decoding, in order to detect if we're dealing with a headers or a trailers
3395 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003396 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003397static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len)
Willy Tarreau13278b42017-10-13 19:23:14 +02003398{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003399 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003400 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003401 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003402 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003403 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003404 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003405 int flen; // header frame len
3406 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003407 int ret = 0;
3408 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003409 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003410 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003411
Willy Tarreauea18f862018-12-22 20:19:26 +01003412next_frame:
3413 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3414 goto leave; // incomplete input frame
3415
3416 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3417 * this case, we'll try to paste it immediately after the initial
3418 * HEADERS frame payload and kill any possible padding. The initial
3419 * frame's length will be increased to represent the concatenation
3420 * of the two frames. The next frame is read from position <tlen>
3421 * and written at position <flen> (minus padding if some is present).
3422 */
3423 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3424 struct h2_fh hdr;
3425 int clen; // CONTINUATION frame's payload length
3426
3427 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3428 /* no more data, the buffer may be full, either due to
3429 * too large a frame or because of too large a hole that
3430 * we're going to compact at the end.
3431 */
3432 goto leave;
3433 }
3434
3435 if (hdr.ft != H2_FT_CONTINUATION) {
3436 /* RFC7540#6.10: frame of unexpected type */
3437 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3438 goto fail;
3439 }
3440
3441 if (hdr.sid != h2c->dsi) {
3442 /* RFC7540#6.10: frame of different stream */
3443 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3444 goto fail;
3445 }
3446
3447 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3448 /* RFC7540#4.2: invalid frame length */
3449 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3450 goto fail;
3451 }
3452
3453 /* detect when we must stop aggragating frames */
3454 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3455
3456 /* Take as much as we can of the CONTINUATION frame's payload */
3457 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3458 if (clen > hdr.len)
3459 clen = hdr.len;
3460
3461 /* Move the frame's payload over the padding, hole and frame
3462 * header. At least one of hole or dpl is null (see diagrams
3463 * above). The hole moves after the new aggragated frame.
3464 */
3465 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3466 h2c->dfl += clen - h2c->dpl;
3467 hole += h2c->dpl + 9;
3468 h2c->dpl = 0;
3469 goto next_frame;
3470 }
3471
3472 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003473
Willy Tarreau13278b42017-10-13 19:23:14 +02003474 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003475 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003476 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003477 copy = alloc_trash_chunk();
3478 if (!copy) {
3479 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3480 goto fail;
3481 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003482 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3483 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3484 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003485 }
3486
Willy Tarreau13278b42017-10-13 19:23:14 +02003487 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3488 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003489 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003490 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3491 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003492 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003493 }
3494
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003495 if (flen < 5) {
3496 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3497 goto fail;
3498 }
3499
Willy Tarreau13278b42017-10-13 19:23:14 +02003500 hdrs += 5; // stream dep = 4, weight = 1
3501 flen -= 5;
3502 }
3503
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003504 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003505 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003506 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003507 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003508
Willy Tarreau937f7602018-02-26 15:22:17 +01003509 /* we can't retry a failed decompression operation so we must be very
3510 * careful not to take any risks. In practice the output buffer is
3511 * always empty except maybe for trailers, in which case we simply have
3512 * to wait for the upper layer to finish consuming what is available.
3513 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003514
3515 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003516 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003517 if (!htx_is_empty(htx)) {
3518 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003519 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003520 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003521 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003522 if (b_data(rxbuf)) {
3523 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003524 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003525 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003526
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003527 rxbuf->head = 0;
3528 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003529 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003530
Willy Tarreau25919232019-01-03 14:48:18 +01003531 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003532 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3533 sizeof(list)/sizeof(list[0]), tmp);
3534 if (outlen < 0) {
3535 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3536 goto fail;
3537 }
3538
Willy Tarreau25919232019-01-03 14:48:18 +01003539 /* The PACK decompressor was updated, let's update the input buffer and
3540 * the parser's state to commit these changes and allow us to later
3541 * fail solely on the stream if needed.
3542 */
3543 b_del(&h2c->dbuf, h2c->dfl + hole);
3544 h2c->dfl = hole = 0;
3545 h2c->st0 = H2_CS_FRAME_H;
3546
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003547 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003548 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003549
Willy Tarreau88d138e2019-01-02 19:38:14 +01003550 if (*flags & H2_SF_HEADERS_RCVD)
3551 goto trailers;
3552
3553 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003554 if (htx) {
3555 /* HTX mode */
3556 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003557 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003558 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003559 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003560 } else {
3561 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003562 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003563 if (outlen > 0)
3564 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003565 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003566
3567 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003568 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003569 goto fail;
3570 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003571
Willy Tarreau174b06a2018-04-25 18:13:58 +02003572 if (msgf & H2_MSGF_BODY) {
3573 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003574 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003575 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003576 if (htx)
3577 htx->extra = *body_len;
3578 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003579 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003580 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003581 }
3582
Willy Tarreau88d138e2019-01-02 19:38:14 +01003583 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003584 /* indicate that a HEADERS frame was received for this stream, except
3585 * for 1xx responses. For 1xx responses, another HEADERS frame is
3586 * expected.
3587 */
3588 if (!(msgf & H2_MSGF_RSP_1XX))
3589 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003590
Christopher Faulet0b465482019-02-19 15:14:23 +01003591 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003592 /* Mark the end of message, either using EOM in HTX or with the
3593 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003594 * is not set during headers with END_STREAM. For HTX trailers,
3595 * we must not leave an HTX trailers block not followed by an
3596 * EOM block, the two must be atomic. Thus if we fail to emit
3597 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003598 */
3599 if (htx) {
Willy Tarreau2135f912019-05-07 11:17:32 +02003600 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3601 struct htx_blk *tail = htx_get_tail_blk(htx);
3602
3603 if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR)
3604 htx_remove_blk(htx, tail);
Willy Tarreau88d138e2019-01-02 19:38:14 +01003605 goto fail;
Willy Tarreau2135f912019-05-07 11:17:32 +02003606 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01003607 }
3608 else if (*flags & H2_SF_DATA_CHNK) {
3609 if (!b_putblk(rxbuf, "\r\n", 2))
3610 goto fail;
3611 }
3612 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003613
Willy Tarreau86277d42019-01-02 15:36:11 +01003614 /* success */
3615 ret = 1;
3616
Willy Tarreau68dd9852017-07-03 14:44:26 +02003617 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003618 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003619 * move the remaining data over it.
3620 */
3621 if (hole) {
3622 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3623 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3624 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3625 b_sub(&h2c->dbuf, hole);
3626 }
3627
Willy Tarreau97215ca2019-04-29 10:20:21 +02003628 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003629 /* too large frames */
3630 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003631 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003632 }
3633
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003634 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003635 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003636 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003637 return ret;
3638
Willy Tarreau68dd9852017-07-03 14:44:26 +02003639 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003640 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003641 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003642
3643 trailers:
3644 /* This is the last HEADERS frame hence a trailer */
3645
3646 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3647 /* It's a trailer but it's missing ES flag */
3648 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3649 goto fail;
3650 }
3651
3652 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3653 * block, and when using chunks we must send the 0 CRLF marker. For
3654 * other modes, the trailers are silently dropped.
3655 */
3656 if (htx) {
3657 if (!htx_add_endof(htx, HTX_BLK_EOD))
3658 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003659 if (h2_make_htx_trailers(list, htx) <= 0)
3660 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003661 }
3662 else if (*flags & H2_SF_DATA_CHNK) {
3663 /* Legacy mode with chunked encoding : we must finalize the
3664 * data block message emit the trailing CRLF */
3665 if (!b_putblk(rxbuf, "0\r\n", 3))
3666 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003667
3668 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3669 if (outlen > 0)
3670 b_add(rxbuf, outlen);
3671 else
3672 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003673 }
3674
3675 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003676}
3677
Willy Tarreau454f9052017-10-26 19:40:35 +02003678/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3679 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3680 * in use, a new chunk is emitted for each frame. This is supposed to fit
3681 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3682 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3683 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003684 * parser state is automatically updated. Returns > 0 if it could completely
3685 * send the current frame, 0 if it couldn't complete, in which case
3686 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3687 * DATA frame can return 0 as a valid result). Stream errors are reported in
3688 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3689 * have checked the frame header and ensured that the frame was complete or the
3690 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003691 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003692static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003693{
3694 struct h2c *h2c = h2s->h2c;
3695 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003696 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003697 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003698 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003699 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003700
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003701 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003702
Olivier Houchard638b7992018-08-16 15:41:52 +02003703 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003704 if (!csbuf) {
3705 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003706 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003707 }
3708
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003709try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003710 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003711 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3712 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003713 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003714 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003715
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003716 if (flen > b_data(&h2c->dbuf)) {
3717 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003718 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003719 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003720 }
3721
Willy Tarreaua9b77962019-01-31 07:23:00 +01003722 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003723 block1 = htx_free_data_space(htx);
3724 if (!block1) {
3725 h2c->flags |= H2_CF_DEM_SFULL;
3726 goto fail;
3727 }
3728 if (flen > block1)
3729 flen = block1;
3730
3731 /* here, flen is the max we can copy into the output buffer */
3732 block1 = b_contig_data(&h2c->dbuf, 0);
3733 if (flen > block1)
3734 flen = block1;
3735
3736 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3737 h2c->flags |= H2_CF_DEM_SFULL;
3738 goto fail;
3739 }
3740
3741 b_del(&h2c->dbuf, flen);
3742 h2c->dfl -= flen;
3743 h2c->rcvd_c += flen;
3744 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003745
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003746 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003747 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003748 htx->extra = h2s->body_len;
3749 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003750 goto try_again;
3751 }
3752 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003753 /* it doesn't fit and the buffer is fragmented,
3754 * so let's defragment it and try again.
3755 */
3756 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003757 }
3758
Willy Tarreaueba10f22018-04-25 20:44:22 +02003759 /* chunked-encoding requires more room */
3760 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003761 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003762 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3763 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3764 (chklen < 1048576) ? 4 : 8;
3765 chklen += 4; // CRLF, CRLF
3766 }
3767
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003768 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003769 if (flen + chklen > b_room(csbuf)) {
3770 if (chklen >= b_room(csbuf)) {
3771 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003772 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003773 }
3774 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003775 }
3776
3777 if (h2s->flags & H2_SF_DATA_CHNK) {
3778 /* emit the chunk size */
3779 unsigned int chksz = flen;
3780 char str[10];
3781 char *beg;
3782
3783 beg = str + sizeof(str);
3784 *--beg = '\n';
3785 *--beg = '\r';
3786 do {
3787 *--beg = hextab[chksz & 0xF];
3788 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003789 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003790 }
3791
Willy Tarreau454f9052017-10-26 19:40:35 +02003792 /* Block1 is the length of the first block before the buffer wraps,
3793 * block2 is the optional second block to reach the end of the frame.
3794 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003795 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003796 if (block1 > flen)
3797 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003798 block2 = flen - block1;
3799
3800 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003801 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003802
3803 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003804 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003805
Willy Tarreaueba10f22018-04-25 20:44:22 +02003806 if (h2s->flags & H2_SF_DATA_CHNK) {
3807 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003808 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003809 }
3810
Willy Tarreau454f9052017-10-26 19:40:35 +02003811 /* now mark the input data as consumed (will be deleted from the buffer
3812 * by the caller when seeing FRAME_A after sending the window update).
3813 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003814 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003815 h2c->dfl -= flen;
3816 h2c->rcvd_c += flen;
3817 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3818
Willy Tarreau1915ca22019-01-24 11:49:37 +01003819 if (h2s->flags & H2_SF_DATA_CLEN)
3820 h2s->body_len -= flen;
3821
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003822 if (h2c->dfl > h2c->dpl) {
3823 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003824 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003825 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003826 }
3827
Willy Tarreau4a28da12018-01-04 14:41:00 +01003828 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003829 /* here we're done with the frame, all the payload (except padding) was
3830 * transferred.
3831 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003832
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003833 if (h2c->dff & H2_F_DATA_END_STREAM) {
3834 if (htx) {
3835 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3836 h2c->flags |= H2_CF_DEM_SFULL;
3837 goto fail;
3838 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003839 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003840 else if (h2s->flags & H2_SF_DATA_CHNK) {
3841 /* emit the trailing 0 CRLF CRLF */
3842 if (b_room(csbuf) < 5) {
3843 h2c->flags |= H2_CF_DEM_SFULL;
3844 goto fail;
3845 }
3846 chklen += 5;
3847 b_putblk(csbuf, "0\r\n\r\n", 5);
3848 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003849 }
3850
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003851 h2c->rcvd_c += h2c->dpl;
3852 h2c->rcvd_s += h2c->dpl;
3853 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003854 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003855 if (htx)
3856 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003857 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003858 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003859 if (htx)
3860 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003861 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003862}
3863
Willy Tarreau5dd17352018-06-14 13:33:30 +02003864/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3865 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3866 * number of bytes sent. The caller must check the stream's status to detect
3867 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003868 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003869static size_t h2s_frt_make_resp_headers(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003870{
3871 struct http_hdr list[MAX_HTTP_HDR];
3872 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003873 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003874 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003875 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003876 int es_now = 0;
3877 int ret = 0;
3878 int hdr;
3879
3880 if (h2c_mux_busy(h2c, h2s)) {
3881 h2s->flags |= H2_SF_BLK_MBUSY;
3882 return 0;
3883 }
3884
Willy Tarreau44e973f2018-03-01 17:49:30 +01003885 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003886 h2c->flags |= H2_CF_MUX_MALLOC;
3887 h2s->flags |= H2_SF_BLK_MROOM;
3888 return 0;
3889 }
3890
3891 /* First, try to parse the H1 response and index it into <list>.
3892 * NOTE! Since it comes from haproxy, we *know* that a response header
3893 * block does not wrap and we can safely read it this way without
3894 * having to realign the buffer.
3895 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003896 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003897 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003898 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003899 /* incomplete or invalid response, this is abnormal coming from
3900 * haproxy and may only result in a bad errorfile or bad Lua code
3901 * so that won't be fixed, raise an error now.
3902 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003903 * FIXME: we should instead add the ability to only return a
3904 * 502 bad gateway. But in theory this is not supposed to
3905 * happen.
3906 */
3907 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3908 ret = 0;
3909 goto end;
3910 }
3911
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003912 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003913
3914 /* certain statuses have no body or an empty one, regardless of
3915 * what the headers say.
3916 */
3917 if (sl.st.status >= 100 && sl.st.status < 200) {
3918 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3919 h1m->curr_len = h1m->body_len = 0;
3920 }
3921 else if (sl.st.status == 204 || sl.st.status == 304) {
3922 /* no contents, claim c-len is present and set to zero */
3923 h1m->flags &= ~H1_MF_CHNK;
3924 h1m->flags |= H1_MF_CLEN;
3925 h1m->curr_len = h1m->body_len = 0;
3926 }
3927
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003928 chunk_reset(&outbuf);
3929
3930 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003931 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003932 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003933 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003934
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003935 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003936 break;
3937 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003938 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003939 }
3940
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003941 if (outbuf.size < 9)
3942 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003943
3944 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003945 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3946 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3947 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003948
3949 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003950 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003951 /* this is an unparsable response */
3952 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3953 ret = 0;
3954 goto end;
3955 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003956
3957 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003958 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003959 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003960 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003961 }
3962
3963 /* encode all headers, stop at empty name */
3964 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003965 /* these ones do not exist in H2 and must be dropped. */
3966 if (isteq(list[hdr].n, ist("connection")) ||
3967 isteq(list[hdr].n, ist("proxy-connection")) ||
3968 isteq(list[hdr].n, ist("keep-alive")) ||
3969 isteq(list[hdr].n, ist("upgrade")) ||
3970 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003971 continue;
3972
3973 if (isteq(list[hdr].n, ist("")))
3974 break; // end
3975
3976 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3977 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003978 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003979 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003980 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003981 }
3982 }
3983
3984 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003985 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003986 es_now = 1;
3987
3988 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003989 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003990
3991 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003992 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003993
3994 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003995 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003996
3997 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003998 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003999 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004000
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004001 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01004002 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004003 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01004004
Willy Tarreau801250e2018-09-11 11:45:04 +02004005 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004006 h2s->flags |= H2_SF_ES_SENT;
4007 if (h2s->st == H2_SS_OPEN)
4008 h2s->st = H2_SS_HLOC;
4009 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004010 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004011 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004012 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004013 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004014 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004015 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004016 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004017 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004018 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004019
4020 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004021
4022 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004023 //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, h1m_state_str(h1m->err_state));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004024 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004025 full:
4026 h1m_init_res(h1m);
4027 h1m->err_pos = -1; // don't care about errors on the response path
4028 h2c->flags |= H2_CF_MUX_MFULL;
4029 h2s->flags |= H2_SF_BLK_MROOM;
4030 ret = 0;
4031 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004032}
4033
Willy Tarreau5dd17352018-06-14 13:33:30 +02004034/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4035 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4036 * the number of bytes sent. The caller must check the stream's status to
4037 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004038 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004039static size_t h2s_frt_make_resp_data(struct h2s *h2s, const struct buffer *buf, size_t ofs, size_t max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004040{
4041 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004042 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004043 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004044 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004045 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004046 int es_now = 0;
4047 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004048 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004049 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004050
4051 if (h2c_mux_busy(h2c, h2s)) {
4052 h2s->flags |= H2_SF_BLK_MBUSY;
4053 goto end;
4054 }
4055
Willy Tarreau44e973f2018-03-01 17:49:30 +01004056 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004057 h2c->flags |= H2_CF_MUX_MALLOC;
4058 h2s->flags |= H2_SF_BLK_MROOM;
4059 goto end;
4060 }
4061
4062 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004063 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004064 goto end;
4065
4066 chunk_reset(&outbuf);
4067
4068 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004069 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004070 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004071 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004072
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004073 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004074 break;
4075 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004076 /* If there are pending data in the output buffer, and we have
4077 * less than 1/4 of the mbuf's size and everything fits, we'll
4078 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4079 * is full and wait, to save some slow realign calls.
4080 */
4081 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4082 h2c->flags |= H2_CF_MUX_MFULL;
4083 h2s->flags |= H2_SF_BLK_MROOM;
4084 goto end;
4085 }
4086
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004087 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004088 }
4089
4090 if (outbuf.size < 9) {
4091 h2c->flags |= H2_CF_MUX_MFULL;
4092 h2s->flags |= H2_SF_BLK_MROOM;
4093 goto end;
4094 }
4095
4096 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004097 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4098 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4099 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004100
4101 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4102 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004103 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004104 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004105 break;
4106 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004107 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004108 if ((long long)size > h1m->curr_len)
4109 size = h1m->curr_len;
4110 break;
4111 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004112 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004113 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004114 if (!ret)
4115 goto end;
4116
4117 if (ret < 0) {
4118 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004119 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004120 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4121 goto end;
4122 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004123 max -= ret;
4124 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004125 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004126 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004127 }
4128
Willy Tarreau801250e2018-09-11 11:45:04 +02004129 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004130 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004131 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004132 if (!ret)
4133 goto end;
4134
4135 if (ret < 0) {
4136 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004137 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004138 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4139 goto end;
4140 }
4141
4142 size = chunk;
4143 h1m->curr_len = chunk;
4144 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004145 max -= ret;
4146 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004147 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004148 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004149 if (!size)
4150 goto send_empty;
4151 }
4152
4153 /* in MSG_DATA state, continue below */
4154 size = h1m->curr_len;
4155 break;
4156 }
4157
4158 /* we have in <size> the exact number of bytes we need to copy from
4159 * the H1 buffer. We need to check this against the connection's and
4160 * the stream's send windows, and to ensure that this fits in the max
4161 * frame size and in the buffer's available space minus 9 bytes (for
4162 * the frame header). The connection's flow control is applied last so
4163 * that we can use a separate list of streams which are immediately
4164 * unblocked on window opening. Note: we don't implement padding.
4165 */
4166
Willy Tarreau5dd17352018-06-14 13:33:30 +02004167 if (size > max)
4168 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004169
4170 if (size > h2s->mws)
4171 size = h2s->mws;
4172
4173 if (size <= 0) {
4174 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004175 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004176 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004177 goto end;
4178 }
4179
4180 if (h2c->mfs && size > h2c->mfs)
4181 size = h2c->mfs;
4182
4183 if (size + 9 > outbuf.size) {
4184 /* we have an opportunity for enlarging the too small
4185 * available space, let's try.
4186 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004187 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004188 goto realign_again;
4189 size = outbuf.size - 9;
4190 }
4191
4192 if (size <= 0) {
4193 h2c->flags |= H2_CF_MUX_MFULL;
4194 h2s->flags |= H2_SF_BLK_MROOM;
4195 goto end;
4196 }
4197
4198 if (size > h2c->mws)
4199 size = h2c->mws;
4200
4201 if (size <= 0) {
4202 h2s->flags |= H2_SF_BLK_MFCTL;
4203 goto end;
4204 }
4205
4206 /* copy whatever we can */
4207 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004208 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004209 if (ret == 1)
4210 len2 = 0;
4211
4212 if (!ret || len1 + len2 < size) {
4213 /* FIXME: must normally never happen */
4214 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4215 goto end;
4216 }
4217
4218 /* limit len1/len2 to size */
4219 if (len1 + len2 > size) {
4220 int sub = len1 + len2 - size;
4221
4222 if (len2 > sub)
4223 len2 -= sub;
4224 else {
4225 sub -= len2;
4226 len2 = 0;
4227 len1 -= sub;
4228 }
4229 }
4230
4231 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004232 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004233 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004234 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004235
4236 send_empty:
4237 /* we may need to add END_STREAM */
4238 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4239 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004240 *
4241 * FIXME: what we do here is not correct because we send end_stream
4242 * before knowing if we'll have to send a HEADERS frame for the
4243 * trailers. More importantly we're not consuming the trailing CRLF
4244 * after the end of trailers, so it will be left to the caller to
4245 * eat it. The right way to do it would be to measure trailers here
4246 * and to send ES only if there are no trailers.
4247 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004248 */
4249 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004250 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004251 es_now = 1;
4252
4253 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004254 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004255
4256 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004257 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004258
4259 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004260 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004261
4262 /* consume incoming H1 response */
4263 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004264 max -= size;
4265 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004266 total += size;
4267 h1m->curr_len -= size;
4268 h2s->mws -= size;
4269 h2c->mws -= size;
4270
4271 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004272 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004273 goto new_frame;
4274 }
4275 }
4276
4277 if (es_now) {
4278 if (h2s->st == H2_SS_OPEN)
4279 h2s->st = H2_SS_HLOC;
4280 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004281 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004282
Willy Tarreau35a62702018-02-27 15:37:25 +01004283 if (!(h1m->flags & H1_MF_CHNK)) {
4284 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004285 total += max;
4286 ofs += max;
4287 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004288
Willy Tarreau801250e2018-09-11 11:45:04 +02004289 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004290 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004291
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004292 h2s->flags |= H2_SF_ES_SENT;
4293 }
4294
4295 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004296 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004297 return total;
4298}
4299
Willy Tarreau115e83b2018-12-01 19:17:53 +01004300/* Try to send a HEADERS frame matching HTX response present in HTX message
4301 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4302 * must check the stream's status to detect any error which might have happened
4303 * subsequently to a successful send. The htx blocks are automatically removed
4304 * from the message. The htx message is assumed to be valid since produced from
4305 * the internal code, hence it contains a start line, an optional series of
4306 * header blocks and an end of header, otherwise an invalid frame could be
4307 * emitted and the resulting htx message could be left in an inconsistent state.
4308 */
4309static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4310{
4311 struct http_hdr list[MAX_HTTP_HDR];
4312 struct h2c *h2c = h2s->h2c;
4313 struct htx_blk *blk;
4314 struct htx_blk *blk_end;
4315 struct buffer outbuf;
4316 struct htx_sl *sl;
4317 enum htx_blk_type type;
4318 int es_now = 0;
4319 int ret = 0;
4320 int hdr;
4321 int idx;
4322
4323 if (h2c_mux_busy(h2c, h2s)) {
4324 h2s->flags |= H2_SF_BLK_MBUSY;
4325 return 0;
4326 }
4327
4328 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4329 h2c->flags |= H2_CF_MUX_MALLOC;
4330 h2s->flags |= H2_SF_BLK_MROOM;
4331 return 0;
4332 }
4333
4334 /* determine the first block which must not be deleted, blk_end may
4335 * be NULL if all blocks have to be deleted.
4336 */
4337 idx = htx_get_head(htx);
4338 blk_end = NULL;
4339 while (idx != -1) {
4340 type = htx_get_blk_type(htx_get_blk(htx, idx));
4341 idx = htx_get_next(htx, idx);
4342 if (type == HTX_BLK_EOH) {
4343 if (idx != -1)
4344 blk_end = htx_get_blk(htx, idx);
4345 break;
4346 }
4347 }
4348
4349 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004350 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004351 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004352 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004353 if (h2s->status < 100 || h2s->status > 999)
4354 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004355
4356 /* and the rest of the headers, that we dump starting at header 0 */
4357 hdr = 0;
4358
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004359 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004360 while ((idx = htx_get_next(htx, idx)) != -1) {
4361 blk = htx_get_blk(htx, idx);
4362 type = htx_get_blk_type(blk);
4363
4364 if (type == HTX_BLK_UNUSED)
4365 continue;
4366
4367 if (type != HTX_BLK_HDR)
4368 break;
4369
4370 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4371 goto fail;
4372
4373 list[hdr].n = htx_get_blk_name(htx, blk);
4374 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004375 hdr++;
4376 }
4377
4378 /* marker for end of headers */
4379 list[hdr].n = ist("");
4380
4381 if (h2s->status == 204 || h2s->status == 304) {
4382 /* no contents, claim c-len is present and set to zero */
4383 es_now = 1;
4384 }
4385
4386 chunk_reset(&outbuf);
4387
4388 while (1) {
4389 outbuf.area = b_tail(&h2c->mbuf);
4390 outbuf.size = b_contig_space(&h2c->mbuf);
4391 outbuf.data = 0;
4392
4393 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4394 break;
4395 realign_again:
4396 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4397 }
4398
4399 if (outbuf.size < 9)
4400 goto full;
4401
4402 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4403 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4404 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4405 outbuf.data = 9;
4406
4407 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004408 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004409 if (b_space_wraps(&h2c->mbuf))
4410 goto realign_again;
4411 goto full;
4412 }
4413
4414 /* encode all headers, stop at empty name */
4415 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4416 /* these ones do not exist in H2 and must be dropped. */
4417 if (isteq(list[hdr].n, ist("connection")) ||
4418 isteq(list[hdr].n, ist("proxy-connection")) ||
4419 isteq(list[hdr].n, ist("keep-alive")) ||
4420 isteq(list[hdr].n, ist("upgrade")) ||
4421 isteq(list[hdr].n, ist("transfer-encoding")))
4422 continue;
4423
4424 if (isteq(list[hdr].n, ist("")))
4425 break; // end
4426
4427 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4428 /* output full */
4429 if (b_space_wraps(&h2c->mbuf))
4430 goto realign_again;
4431 goto full;
4432 }
4433 }
4434
Christopher Faulet0b465482019-02-19 15:14:23 +01004435 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004436 * FIXME: we should also set it when we know for sure that the
4437 * content-length is zero as well as on 204/304
4438 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004439 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4440 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004441 es_now = 1;
4442
Willy Tarreau927b88b2019-03-04 08:03:25 +01004443 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004444 es_now = 1;
4445
4446 /* update the frame's size */
4447 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4448
4449 if (es_now)
4450 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4451
4452 /* commit the H2 response */
4453 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004454
4455 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4456 * 1xx responses, another HEADERS frame is expected.
4457 */
4458 if (h2s->status >= 200 || h2s->status == 101)
4459 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004460
Willy Tarreau115e83b2018-12-01 19:17:53 +01004461 if (es_now) {
4462 h2s->flags |= H2_SF_ES_SENT;
4463 if (h2s->st == H2_SS_OPEN)
4464 h2s->st = H2_SS_HLOC;
4465 else
4466 h2s_close(h2s);
4467 }
4468
4469 /* OK we could properly deliver the response */
4470
4471 /* remove all header blocks including the EOH and compute the
4472 * corresponding size.
4473 *
4474 * FIXME: We should remove everything when es_now is set.
4475 */
4476 ret = 0;
4477 idx = htx_get_head(htx);
4478 blk = htx_get_blk(htx, idx);
4479 while (blk != blk_end) {
4480 ret += htx_get_blksz(blk);
4481 blk = htx_remove_blk(htx, blk);
4482 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004483
4484 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4485 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004486 end:
4487 return ret;
4488 full:
4489 h2c->flags |= H2_CF_MUX_MFULL;
4490 h2s->flags |= H2_SF_BLK_MROOM;
4491 ret = 0;
4492 goto end;
4493 fail:
4494 /* unparsable HTX messages, too large ones to be produced in the local
4495 * list etc go here (unrecoverable errors).
4496 */
4497 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4498 ret = 0;
4499 goto end;
4500}
4501
Willy Tarreau80739692018-10-05 11:35:57 +02004502/* Try to send a HEADERS frame matching HTX request present in HTX message
4503 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4504 * must check the stream's status to detect any error which might have happened
4505 * subsequently to a successful send. The htx blocks are automatically removed
4506 * from the message. The htx message is assumed to be valid since produced from
4507 * the internal code, hence it contains a start line, an optional series of
4508 * header blocks and an end of header, otherwise an invalid frame could be
4509 * emitted and the resulting htx message could be left in an inconsistent state.
4510 */
4511static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4512{
4513 struct http_hdr list[MAX_HTTP_HDR];
4514 struct h2c *h2c = h2s->h2c;
4515 struct htx_blk *blk;
4516 struct htx_blk *blk_end;
4517 struct buffer outbuf;
4518 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004519 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004520 enum htx_blk_type type;
4521 int es_now = 0;
4522 int ret = 0;
4523 int hdr;
4524 int idx;
4525
4526 if (h2c_mux_busy(h2c, h2s)) {
4527 h2s->flags |= H2_SF_BLK_MBUSY;
4528 return 0;
4529 }
4530
4531 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4532 h2c->flags |= H2_CF_MUX_MALLOC;
4533 h2s->flags |= H2_SF_BLK_MROOM;
4534 return 0;
4535 }
4536
4537 /* determine the first block which must not be deleted, blk_end may
4538 * be NULL if all blocks have to be deleted.
4539 */
4540 idx = htx_get_head(htx);
4541 blk_end = NULL;
4542 while (idx != -1) {
4543 type = htx_get_blk_type(htx_get_blk(htx, idx));
4544 idx = htx_get_next(htx, idx);
4545 if (type == HTX_BLK_EOH) {
4546 if (idx != -1)
4547 blk_end = htx_get_blk(htx, idx);
4548 break;
4549 }
4550 }
4551
4552 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004553 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004554 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004555 meth = htx_sl_req_meth(sl);
4556 path = htx_sl_req_uri(sl);
4557
4558 /* and the rest of the headers, that we dump starting at header 0 */
4559 hdr = 0;
4560
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004561 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004562 while ((idx = htx_get_next(htx, idx)) != -1) {
4563 blk = htx_get_blk(htx, idx);
4564 type = htx_get_blk_type(blk);
4565
4566 if (type == HTX_BLK_UNUSED)
4567 continue;
4568
4569 if (type != HTX_BLK_HDR)
4570 break;
4571
4572 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4573 goto fail;
4574
4575 list[hdr].n = htx_get_blk_name(htx, blk);
4576 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004577 hdr++;
4578 }
4579
4580 /* marker for end of headers */
4581 list[hdr].n = ist("");
4582
4583 chunk_reset(&outbuf);
4584
4585 while (1) {
4586 outbuf.area = b_tail(&h2c->mbuf);
4587 outbuf.size = b_contig_space(&h2c->mbuf);
4588 outbuf.data = 0;
4589
4590 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4591 break;
4592 realign_again:
4593 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4594 }
4595
4596 if (outbuf.size < 9)
4597 goto full;
4598
4599 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4600 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4601 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4602 outbuf.data = 9;
4603
4604 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004605 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004606 if (b_space_wraps(&h2c->mbuf))
4607 goto realign_again;
4608 goto full;
4609 }
4610
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004611 /* RFC7540 #8.3: the CONNECT method must have :
4612 * - :authority set to the URI part (host:port)
4613 * - :method set to CONNECT
4614 * - :scheme and :path omitted
4615 */
4616 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4617 /* encode the scheme which is always "https" (or 0x86 for "http") */
4618 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4619 /* output full */
4620 if (b_space_wraps(&h2c->mbuf))
4621 goto realign_again;
4622 goto full;
4623 }
Willy Tarreau80739692018-10-05 11:35:57 +02004624
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004625 /* encode the path, which necessarily is the second one */
4626 if (!hpack_encode_path(&outbuf, path)) {
4627 /* output full */
4628 if (b_space_wraps(&h2c->mbuf))
4629 goto realign_again;
4630 goto full;
4631 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004632
4633 /* look for the Host header and place it in :authority */
4634 auth = ist2(NULL, 0);
4635 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4636 if (isteq(list[hdr].n, ist("")))
4637 break; // end
4638
4639 if (isteq(list[hdr].n, ist("host"))) {
4640 auth = list[hdr].v;
4641 break;
4642 }
4643 }
4644 }
4645 else {
4646 /* for CONNECT, :authority is taken from the path */
4647 auth = path;
4648 }
4649
4650 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4651 /* output full */
4652 if (b_space_wraps(&h2c->mbuf))
4653 goto realign_again;
4654 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004655 }
4656
4657 /* encode all headers, stop at empty name */
4658 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4659 /* these ones do not exist in H2 and must be dropped. */
4660 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004661 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004662 isteq(list[hdr].n, ist("proxy-connection")) ||
4663 isteq(list[hdr].n, ist("keep-alive")) ||
4664 isteq(list[hdr].n, ist("upgrade")) ||
4665 isteq(list[hdr].n, ist("transfer-encoding")))
4666 continue;
4667
4668 if (isteq(list[hdr].n, ist("")))
4669 break; // end
4670
4671 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4672 /* output full */
4673 if (b_space_wraps(&h2c->mbuf))
4674 goto realign_again;
4675 goto full;
4676 }
4677 }
4678
4679 /* we may need to add END_STREAM if we have no body :
4680 * - request already closed, or :
4681 * - no transfer-encoding, and :
4682 * - no content-length or content-length:0
4683 * Fixme: this doesn't take into account CONNECT requests.
4684 */
4685 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4686 es_now = 1;
4687
4688 if (sl->flags & HTX_SL_F_BODYLESS)
4689 es_now = 1;
4690
Willy Tarreau927b88b2019-03-04 08:03:25 +01004691 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004692 es_now = 1;
4693
4694 /* update the frame's size */
4695 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4696
4697 if (es_now)
4698 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4699
4700 /* commit the H2 response */
4701 b_add(&h2c->mbuf, outbuf.data);
4702 h2s->flags |= H2_SF_HEADERS_SENT;
4703 h2s->st = H2_SS_OPEN;
4704
Willy Tarreau80739692018-10-05 11:35:57 +02004705 if (es_now) {
4706 // trim any possibly pending data (eg: inconsistent content-length)
4707 h2s->flags |= H2_SF_ES_SENT;
4708 h2s->st = H2_SS_HLOC;
4709 }
4710
4711 /* remove all header blocks including the EOH and compute the
4712 * corresponding size.
4713 *
4714 * FIXME: We should remove everything when es_now is set.
4715 */
4716 ret = 0;
4717 idx = htx_get_head(htx);
4718 blk = htx_get_blk(htx, idx);
4719 while (blk != blk_end) {
4720 ret += htx_get_blksz(blk);
4721 blk = htx_remove_blk(htx, blk);
4722 }
4723
4724 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4725 htx_remove_blk(htx, blk_end);
4726
4727 end:
4728 return ret;
4729 full:
4730 h2c->flags |= H2_CF_MUX_MFULL;
4731 h2s->flags |= H2_SF_BLK_MROOM;
4732 ret = 0;
4733 goto end;
4734 fail:
4735 /* unparsable HTX messages, too large ones to be produced in the local
4736 * list etc go here (unrecoverable errors).
4737 */
4738 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4739 ret = 0;
4740 goto end;
4741}
4742
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004743/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004744 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4745 * caller must check the stream's status to detect any error which might have
4746 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004747 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4748 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004749static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004750{
4751 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004752 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004753 struct buffer outbuf;
4754 size_t total = 0;
4755 int es_now = 0;
4756 int bsize; /* htx block size */
4757 int fsize; /* h2 frame size */
4758 struct htx_blk *blk;
4759 enum htx_blk_type type;
4760 int idx;
4761
4762 if (h2c_mux_busy(h2c, h2s)) {
4763 h2s->flags |= H2_SF_BLK_MBUSY;
4764 goto end;
4765 }
4766
4767 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4768 h2c->flags |= H2_CF_MUX_MALLOC;
4769 h2s->flags |= H2_SF_BLK_MROOM;
4770 goto end;
4771 }
4772
Willy Tarreau98de12a2018-12-12 07:03:00 +01004773 htx = htx_from_buf(buf);
4774
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004775 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4776 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4777 * the caller to handle.
4778 */
4779
4780 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004781 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004782 goto end;
4783
4784 idx = htx_get_head(htx);
4785 blk = htx_get_blk(htx, idx);
4786 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4787 bsize = htx_get_blksz(blk);
4788 fsize = bsize;
4789
4790 if (type == HTX_BLK_EOD) {
4791 /* if we have an EOD, we're dealing with chunked data. We may
4792 * have a set of trailers after us that the caller will want to
4793 * deal with. Let's simply remove the EOD and return.
4794 */
4795 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004796 total++; // EOD counts as one byte
4797 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004798 goto end;
4799 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004800 else if (type == HTX_BLK_EOM) {
4801 if (h2s->flags & H2_SF_ES_SENT) {
4802 /* ES already sent */
4803 htx_remove_blk(htx, blk);
4804 total++; // EOM counts as one byte
4805 count--;
4806 goto end;
4807 }
4808 }
4809 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004810 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004811
4812 /* Perform some optimizations to reduce the number of buffer copies.
4813 * First, if the mux's buffer is empty and the htx area contains
4814 * exactly one data block of the same size as the requested count, and
4815 * this count fits within the frame size, the stream's window size, and
4816 * the connection's window size, then it's possible to simply swap the
4817 * caller's buffer with the mux's output buffer and adjust offsets and
4818 * length to match the entire DATA HTX block in the middle. In this
4819 * case we perform a true zero-copy operation from end-to-end. This is
4820 * the situation that happens all the time with large files. Second, if
4821 * this is not possible, but the mux's output buffer is empty, we still
4822 * have an opportunity to avoid the copy to the intermediary buffer, by
4823 * making the intermediary buffer's area point to the output buffer's
4824 * area. In this case we want to skip the HTX header to make sure that
4825 * copies remain aligned and that this operation remains possible all
4826 * the time. This goes for headers, data blocks and any data extracted
4827 * from the HTX blocks.
4828 */
4829 if (unlikely(fsize == count &&
4830 htx->used == 1 && type == HTX_BLK_DATA &&
4831 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4832 void *old_area = h2c->mbuf.area;
4833
4834 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004835 /* Too bad there are data left there. We're willing to memcpy/memmove
4836 * up to 1/4 of the buffer, which means that it's OK to copy a large
4837 * frame into a buffer containing few data if it needs to be realigned,
4838 * and that it's also OK to copy few data without realigning. Otherwise
4839 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004840 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004841 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4842 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4843 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004844 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004845
Willy Tarreau98de12a2018-12-12 07:03:00 +01004846 h2c->flags |= H2_CF_MUX_MFULL;
4847 h2s->flags |= H2_SF_BLK_MROOM;
4848 goto end;
4849 }
4850
4851 /* map an H2 frame to the HTX block so that we can put the
4852 * frame header there.
4853 */
4854 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004855 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004856 h2c->mbuf.data = fsize + 9;
4857 outbuf.area = b_head(&h2c->mbuf);
4858
4859 /* prepend an H2 DATA frame header just before the DATA block */
4860 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4861 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4862 h2_set_frame_size(outbuf.area, fsize);
4863
4864 /* update windows */
4865 h2s->mws -= fsize;
4866 h2c->mws -= fsize;
4867
4868 /* and exchange with our old area */
4869 buf->area = old_area;
4870 buf->data = buf->head = 0;
4871 total += fsize;
4872 goto end;
4873 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004874
Willy Tarreau98de12a2018-12-12 07:03:00 +01004875 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004876 /* for DATA and EOM we'll have to emit a frame, even if empty */
4877
4878 while (1) {
4879 outbuf.area = b_tail(&h2c->mbuf);
4880 outbuf.size = b_contig_space(&h2c->mbuf);
4881 outbuf.data = 0;
4882
4883 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4884 break;
4885 realign_again:
4886 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4887 }
4888
4889 if (outbuf.size < 9) {
4890 h2c->flags |= H2_CF_MUX_MFULL;
4891 h2s->flags |= H2_SF_BLK_MROOM;
4892 goto end;
4893 }
4894
4895 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4896 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4897 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4898 outbuf.data = 9;
4899
4900 /* we have in <fsize> the exact number of bytes we need to copy from
4901 * the HTX buffer. We need to check this against the connection's and
4902 * the stream's send windows, and to ensure that this fits in the max
4903 * frame size and in the buffer's available space minus 9 bytes (for
4904 * the frame header). The connection's flow control is applied last so
4905 * that we can use a separate list of streams which are immediately
4906 * unblocked on window opening. Note: we don't implement padding.
4907 */
4908
4909 /* EOM is presented with bsize==1 but would lead to the emission of an
4910 * empty frame, thus we force it to zero here.
4911 */
4912 if (type == HTX_BLK_EOM)
4913 bsize = fsize = 0;
4914
4915 if (!fsize)
4916 goto send_empty;
4917
4918 if (h2s->mws <= 0) {
4919 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004920 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004921 LIST_DEL_INIT(&h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004922 goto end;
4923 }
4924
Willy Tarreauee573762018-12-04 15:25:57 +01004925 if (fsize > count)
4926 fsize = count;
4927
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004928 if (fsize > h2s->mws)
4929 fsize = h2s->mws; // >0
4930
4931 if (h2c->mfs && fsize > h2c->mfs)
4932 fsize = h2c->mfs; // >0
4933
4934 if (fsize + 9 > outbuf.size) {
4935 /* we have an opportunity for enlarging the too small
4936 * available space, let's try.
4937 * FIXME: is this really interesting to do? Maybe we'll
4938 * spend lots of time realigning instead of using two
4939 * frames.
4940 */
4941 if (b_space_wraps(&h2c->mbuf))
4942 goto realign_again;
4943 fsize = outbuf.size - 9;
4944
4945 if (fsize <= 0) {
4946 /* no need to send an empty frame here */
4947 h2c->flags |= H2_CF_MUX_MFULL;
4948 h2s->flags |= H2_SF_BLK_MROOM;
4949 goto end;
4950 }
4951 }
4952
4953 if (h2c->mws <= 0) {
4954 h2s->flags |= H2_SF_BLK_MFCTL;
4955 goto end;
4956 }
4957
4958 if (fsize > h2c->mws)
4959 fsize = h2c->mws;
4960
4961 /* now let's copy this this into the output buffer */
4962 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004963 h2s->mws -= fsize;
4964 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004965 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004966
4967 send_empty:
4968 /* update the frame's size */
4969 h2_set_frame_size(outbuf.area, fsize);
4970
4971 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4972 * meeting EOM. We should optimize this later.
4973 */
4974 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004975 total++; // EOM counts as one byte
4976 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004977 es_now = 1;
4978 }
4979
4980 if (es_now)
4981 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4982
4983 /* commit the H2 response */
4984 b_add(&h2c->mbuf, fsize + 9);
4985
4986 /* consume incoming HTX block, including EOM */
4987 total += fsize;
4988 if (fsize == bsize) {
4989 htx_remove_blk(htx, blk);
4990 if (fsize)
4991 goto new_frame;
4992 } else {
4993 /* we've truncated this block */
4994 htx_cut_data_blk(htx, blk, fsize);
4995 }
4996
4997 if (es_now) {
4998 if (h2s->st == H2_SS_OPEN)
4999 h2s->st = H2_SS_HLOC;
5000 else
5001 h2s_close(h2s);
5002
5003 h2s->flags |= H2_SF_ES_SENT;
5004 }
5005
5006 end:
5007 return total;
5008}
5009
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005010/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5011 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5012 * processed. The caller must check the stream's status to detect any error
5013 * which might have happened subsequently to a successful send. The htx blocks
5014 * are automatically removed from the message. The htx message is assumed to be
5015 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005016 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005017 * as a single frame. The ES flag is always set.
5018 */
5019static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5020{
5021 struct http_hdr list[MAX_HTTP_HDR];
5022 struct h2c *h2c = h2s->h2c;
5023 struct htx_blk *blk;
5024 struct htx_blk *blk_end;
5025 struct buffer outbuf;
5026 struct h1m h1m;
5027 enum htx_blk_type type;
5028 uint32_t size;
5029 int ret = 0;
5030 int hdr;
5031 int idx;
5032 void *start;
5033
5034 if (h2c_mux_busy(h2c, h2s)) {
5035 h2s->flags |= H2_SF_BLK_MBUSY;
5036 goto end;
5037 }
5038
5039 if (!h2_get_buf(h2c, &h2c->mbuf)) {
5040 h2c->flags |= H2_CF_MUX_MALLOC;
5041 h2s->flags |= H2_SF_BLK_MROOM;
5042 goto end;
5043 }
5044
5045 /* The principle is that we parse each and every trailers block using
5046 * the H1 headers parser, and append it to the list. We don't proceed
5047 * until EOM is met. blk_end will point to the EOM block.
5048 */
5049 hdr = 0;
5050 memset(list, 0, sizeof(list));
5051 blk_end = NULL;
5052
5053 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
5054 blk = htx_get_blk(htx, idx);
5055 type = htx_get_blk_type(blk);
5056
5057 if (type == HTX_BLK_UNUSED)
5058 continue;
5059
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005060 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005061 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005062
5063 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5064 goto fail;
5065
5066 size = htx_get_blksz(blk);
5067 start = htx_get_blk_ptr(htx, blk);
5068
5069 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5070 h1m.err_pos = 0;
5071 ret = h1_headers_to_hdr_list(start, start + size,
5072 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5073 &h1m, NULL);
5074 if (ret < 0)
5075 goto fail;
5076
5077 /* ret == 0 if an incomplete trailers block was found (missing
5078 * empty line), or > 0 if it was found. We have to continue on
5079 * incomplete messages because the trailers block might be
5080 * incomplete.
5081 */
5082
5083 /* search the new end */
5084 while (hdr <= sizeof(list)/sizeof(list[0])) {
5085 if (!list[hdr].n.len)
5086 break;
5087 hdr++;
5088 }
5089 }
5090
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005091 if (list[hdr].n.len != 0)
5092 goto fail; // empty trailer not found: internal error
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005093
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005094 chunk_reset(&outbuf);
5095
5096 while (1) {
5097 outbuf.area = b_tail(&h2c->mbuf);
5098 outbuf.size = b_contig_space(&h2c->mbuf);
5099 outbuf.data = 0;
5100
5101 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5102 break;
5103 realign_again:
5104 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5105 }
5106
5107 if (outbuf.size < 9)
5108 goto full;
5109
5110 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5111 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5112 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5113 outbuf.data = 9;
5114
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005115 /* encode all headers */
5116 for (idx = 0; idx < hdr; idx++) {
5117 /* these ones do not exist in H2 or must not appear in
5118 * trailers and must be dropped.
5119 */
5120 if (isteq(list[idx].n, ist("host")) ||
5121 isteq(list[idx].n, ist("content-length")) ||
5122 isteq(list[idx].n, ist("connection")) ||
5123 isteq(list[idx].n, ist("proxy-connection")) ||
5124 isteq(list[idx].n, ist("keep-alive")) ||
5125 isteq(list[idx].n, ist("upgrade")) ||
5126 isteq(list[idx].n, ist("te")) ||
5127 isteq(list[idx].n, ist("transfer-encoding")))
5128 continue;
5129
5130 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5131 /* output full */
5132 if (b_space_wraps(&h2c->mbuf))
5133 goto realign_again;
5134 goto full;
5135 }
5136 }
5137
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005138 if (outbuf.data == 9) {
5139 /* here we have a problem, we have nothing to emit (either we
5140 * received an empty trailers block followed or we removed its
5141 * contents above). Because of this we can't send a HEADERS
5142 * frame, so we have to cheat and instead send an empty DATA
5143 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005144 */
5145 outbuf.area[3] = H2_FT_DATA;
5146 outbuf.area[4] = H2_F_DATA_END_STREAM;
5147 }
5148
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005149 /* update the frame's size */
5150 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5151
5152 /* commit the H2 response */
5153 b_add(&h2c->mbuf, outbuf.data);
5154 h2s->flags |= H2_SF_ES_SENT;
5155
5156 if (h2s->st == H2_SS_OPEN)
5157 h2s->st = H2_SS_HLOC;
5158 else
5159 h2s_close(h2s);
5160
5161 /* OK we could properly deliver the response */
5162 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005163 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005164 ret = 0;
5165 idx = htx_get_head(htx);
5166 blk = htx_get_blk(htx, idx);
5167 while (blk != blk_end) {
5168 ret += htx_get_blksz(blk);
5169 blk = htx_remove_blk(htx, blk);
5170 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005171 end:
5172 return ret;
5173 full:
5174 h2c->flags |= H2_CF_MUX_MFULL;
5175 h2s->flags |= H2_SF_BLK_MROOM;
5176 ret = 0;
5177 goto end;
5178 fail:
5179 /* unparsable HTX messages, too large ones to be produced in the local
5180 * list etc go here (unrecoverable errors).
5181 */
5182 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5183 ret = 0;
5184 goto end;
5185}
5186
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005187/* Called from the upper layer, to subscribe to events, such as being able to send.
5188 * The <param> argument here is supposed to be a pointer to a wait_event struct
5189 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5190 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5191 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5192 * returns 0 except for the error above.
5193 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005194static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5195{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005196 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005197 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005198 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005199
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005200 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005201 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005202 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5203 sw->events |= SUB_RETRY_RECV;
5204 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005205 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005206 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005207 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005208 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005209 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5210 sw->events |= SUB_RETRY_SEND;
5211 h2s->send_wait = sw;
5212 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5213 !LIST_ADDED(&h2s->list)) {
5214 if (h2s->flags & H2_SF_BLK_MFCTL)
5215 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5216 else
5217 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005218 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005219 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005220 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005221 if (event_type != 0)
5222 return -1;
5223 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005224}
5225
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005226/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5227 * The <param> argument here is supposed to be a pointer to the same wait_event
5228 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5229 * It always returns zero.
5230 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005231static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5232{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005233 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005234 struct h2s *h2s = cs->ctx;
5235
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005236 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005237 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005238 BUG_ON(h2s->recv_wait != sw);
5239 sw->events &= ~SUB_RETRY_RECV;
5240 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005241 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005242 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005243 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005244 BUG_ON(h2s->send_wait != sw);
5245 LIST_DEL(&h2s->list);
5246 LIST_INIT(&h2s->list);
5247 sw->events &= ~SUB_RETRY_SEND;
5248 /* We were about to send, make sure it does not happen */
5249 if (LIST_ADDED(&h2s->sending_list) &&
5250 h2s->send_wait != &h2s->wait_event) {
5251 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
5252 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005253 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005254 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005255 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005256 return 0;
5257}
5258
5259
Olivier Houchard511efea2018-08-16 15:30:32 +02005260/* Called from the upper layer, to receive data */
5261static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5262{
Olivier Houchard638b7992018-08-16 15:41:52 +02005263 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005264 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005265 struct htx *h2s_htx = NULL;
5266 struct htx *buf_htx = NULL;
5267 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005268 size_t ret = 0;
5269
5270 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005271 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5272 /* in HTX mode we ignore the count argument */
5273 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005274 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005275 /* Here htx_to_buf() will set buffer data to 0 because
5276 * the HTX is empty.
5277 */
5278 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005279 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005280 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005281
5282 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005283 count = htx_free_data_space(buf_htx);
5284 if (flags & CO_RFL_KEEP_RSV) {
5285 if (count <= global.tune.maxrewrite)
5286 goto end;
5287 count -= global.tune.maxrewrite;
5288 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005289
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005290 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005291
5292 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5293 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5294
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005295 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005296 htx_to_buf(buf_htx, buf);
5297 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005298 ret = htx_ret.ret;
5299 }
5300 else {
5301 ret = b_xfer(buf, &h2s->rxbuf, count);
5302 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005303
Christopher Faulet37070b22019-02-14 15:12:14 +01005304 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005305 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005306 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005307 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005308 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005309 if (h2s->flags & H2_SF_ES_RCVD)
5310 cs->flags |= CS_FL_EOI;
Willy Tarreau99ad1b32019-05-14 11:46:28 +02005311 if (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS)
Olivier Houchard511efea2018-08-16 15:30:32 +02005312 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005313 if (cs->flags & CS_FL_ERR_PENDING)
5314 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005315 if (b_size(&h2s->rxbuf)) {
5316 b_free(&h2s->rxbuf);
5317 offer_buffers(NULL, tasks_run_queue);
5318 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005319 }
5320
Willy Tarreau082f5592018-11-25 08:03:32 +01005321 if (ret && h2c->dsi == h2s->id) {
5322 /* demux is blocking on this stream's buffer */
5323 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005324 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005325 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005326
Olivier Houchard511efea2018-08-16 15:30:32 +02005327 return ret;
5328}
5329
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005330/* stops all senders of this connection for example when the mux buffer is full.
5331 * They are moved from the sending_list to either fctl_list or send_list.
5332 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005333static void h2_stop_senders(struct h2c *h2c)
5334{
5335 struct h2s *h2s, *h2s_back;
5336
Olivier Houchardd360ac62019-03-22 17:37:16 +01005337 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5338 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005339 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005340 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005341 }
5342}
5343
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005344/* Called from the upper layer, to send data from buffer <buf> for no more than
5345 * <count> bytes. Returns the number of bytes effectively sent. Some status
5346 * flags may be updated on the conn_stream.
5347 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005348static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005349{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005350 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005351 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005352 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005353 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005354 struct htx *htx;
5355 struct htx_blk *blk;
5356 enum htx_blk_type btype;
5357 uint32_t bsize;
5358 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005359
Olivier Houchardd360ac62019-03-22 17:37:16 +01005360 /* If we were not just woken because we wanted to send but couldn't,
5361 * and there's somebody else that is waiting to send, do nothing,
5362 * we will subscribe later and be put at the end of the list
5363 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005364 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005365 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5366 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005367 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005368
Olivier Houchard998410a2019-04-15 19:23:37 +02005369 /* We couldn't set it to NULL before, because we needed it in case
5370 * we had to cancel the tasklet
5371 */
5372 h2s->send_wait = NULL;
5373
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005374 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5375 return 0;
5376
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005377 /* htx will be enough to decide if we're using HTX or legacy */
5378 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5379
Willy Tarreau0bad0432018-06-14 16:54:01 +02005380 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005381 h2s->flags |= H2_SF_OUTGOING_DATA;
5382
Willy Tarreau751f2d02018-10-05 09:35:00 +02005383 if (h2s->id == 0) {
5384 int32_t id = h2c_get_next_sid(h2s->h2c);
5385
5386 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005387 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005388 return 0;
5389 }
5390
5391 eb32_delete(&h2s->by_id);
5392 h2s->by_id.key = h2s->id = id;
5393 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005394 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005395 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5396 }
5397
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005398 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005399 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005400 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005401 idx = htx_get_head(htx);
5402 blk = htx_get_blk(htx, idx);
5403 btype = htx_get_blk_type(blk);
5404 bsize = htx_get_blksz(blk);
5405
5406 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005407 case HTX_BLK_REQ_SL:
5408 /* start-line before headers */
5409 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5410 if (ret > 0) {
5411 total += ret;
5412 count -= ret;
5413 if (ret < bsize)
5414 goto done;
5415 }
5416 break;
5417
Willy Tarreau115e83b2018-12-01 19:17:53 +01005418 case HTX_BLK_RES_SL:
5419 /* start-line before headers */
5420 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5421 if (ret > 0) {
5422 total += ret;
5423 count -= ret;
5424 if (ret < bsize)
5425 goto done;
5426 }
5427 break;
5428
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005429 case HTX_BLK_DATA:
5430 case HTX_BLK_EOD:
5431 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005432 /* all these cause the emission of a DATA frame (possibly empty).
5433 * This EOM necessarily is one before trailers, as the EOM following
5434 * trailers would have been consumed by the trailers parser.
5435 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005436 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005437 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005438 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005439 total += ret;
5440 count -= ret;
5441 if (ret < bsize)
5442 goto done;
5443 }
5444 break;
5445
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005446 case HTX_BLK_TLR:
5447 /* This is the first trailers block, all the subsequent ones AND
5448 * the EOM will be swallowed by the parser.
5449 */
5450 ret = h2s_htx_make_trailers(h2s, htx);
5451 if (ret > 0) {
5452 total += ret;
5453 count -= ret;
5454 if (ret < bsize)
5455 goto done;
5456 }
5457 break;
5458
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005459 default:
5460 htx_remove_blk(htx, blk);
5461 total += bsize;
5462 count -= bsize;
5463 break;
5464 }
5465 }
5466 goto done;
5467 }
5468
5469 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005470 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005471 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005472 if (h2s->h2c->flags & H2_CF_IS_BACK)
5473 ret = -1;
5474 else
5475 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005476 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005477 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005478 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005479 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005480 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005481 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005482 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005483
Willy Tarreau5dd17352018-06-14 13:33:30 +02005484 if (unlikely((int)ret <= 0)) {
5485 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005486 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5487 break;
5488 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005489 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005490 total += count;
5491 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005492 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005493 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005494 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005495 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005496 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005497 break;
5498 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005499
5500 total += ret;
5501 count -= ret;
5502
Willy Tarreau2b778482019-05-06 15:00:22 +02005503 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005504 break;
5505
5506 if (h2s->flags & H2_SF_BLK_ANY)
5507 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005508 }
5509
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005510 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005511 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005512 /* trim any possibly pending data after we close (extra CR-LF,
5513 * unprocessed trailers, abnormal extra data, ...)
5514 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005515 total += count;
5516 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005517 }
5518
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005519 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005520 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005521 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005522 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005523 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005524 }
5525
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005526 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005527 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005528 } else {
5529 b_del(buf, total);
5530 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005531
5532 /* The mux is full, cancel the pending tasks */
5533 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5534 (h2s->flags & H2_SF_BLK_MBUSY))
5535 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005536
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005537 /* If we're running HTX, and we read the whole buffer, then pretend
5538 * we read exactly what the caller specified, as with HTX the caller
5539 * will always give the buffer size, instead of the amount of data
5540 * available.
5541 */
5542 if (htx && !b_data(buf))
5543 total = orig_count;
5544
Olivier Houchard7505f942018-08-21 18:10:44 +02005545 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005546 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005547 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005548
Olivier Houchard7505f942018-08-21 18:10:44 +02005549 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005550 /* If we're waiting for flow control, and we got a shutr on the
5551 * connection, we will never be unlocked, so add an error on
5552 * the conn_stream.
5553 */
5554 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5555 !b_data(&h2s->h2c->dbuf) &&
5556 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5557 if (cs->flags & CS_FL_EOS)
5558 cs->flags |= CS_FL_ERROR;
5559 else
5560 cs->flags |= CS_FL_ERR_PENDING;
5561 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005562 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005563 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005564 LIST_DEL_INIT(&h2s->list);
5565 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005566 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005567}
5568
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005569/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005570static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005571{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005572 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005573 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005574 struct eb32_node *node;
5575 int fctl_cnt = 0;
5576 int send_cnt = 0;
5577 int tree_cnt = 0;
5578 int orph_cnt = 0;
5579
5580 if (!h2c)
5581 return;
5582
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005583 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005584 fctl_cnt++;
5585
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005586 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005587 send_cnt++;
5588
Willy Tarreau3af37712018-12-18 14:34:41 +01005589 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005590 node = eb32_first(&h2c->streams_by_id);
5591 while (node) {
5592 h2s = container_of(node, struct h2s, by_id);
5593 tree_cnt++;
5594 if (!h2s->cs)
5595 orph_cnt++;
5596 node = eb32_next(node);
5597 }
5598
Willy Tarreau987c0632018-12-18 10:32:05 +01005599 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5600 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5601 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d .mbuf=%u@%p+%u/%u",
Willy Tarreau616ac812018-07-24 14:12:42 +02005602 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5603 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005604 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005605 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5606 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5607 h2c->msi,
5608 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5609 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5610
5611 if (h2s) {
5612 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5613 h2s, h2s->id, h2s->flags,
5614 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5615 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5616 h2s->cs);
5617 if (h2s->cs)
5618 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5619 h2s->cs->flags, h2s->cs->data);
5620 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005621}
Willy Tarreau62f52692017-10-08 23:01:42 +02005622
5623/*******************************************************/
5624/* functions below are dedicated to the config parsers */
5625/*******************************************************/
5626
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005627/* config parser for global "tune.h2.header-table-size" */
5628static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5629 struct proxy *defpx, const char *file, int line,
5630 char **err)
5631{
5632 if (too_many_args(1, args, err, NULL))
5633 return -1;
5634
5635 h2_settings_header_table_size = atoi(args[1]);
5636 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5637 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5638 return -1;
5639 }
5640 return 0;
5641}
Willy Tarreau62f52692017-10-08 23:01:42 +02005642
Willy Tarreaue6baec02017-07-27 11:45:11 +02005643/* config parser for global "tune.h2.initial-window-size" */
5644static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5645 struct proxy *defpx, const char *file, int line,
5646 char **err)
5647{
5648 if (too_many_args(1, args, err, NULL))
5649 return -1;
5650
5651 h2_settings_initial_window_size = atoi(args[1]);
5652 if (h2_settings_initial_window_size < 0) {
5653 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5654 return -1;
5655 }
5656 return 0;
5657}
5658
Willy Tarreau5242ef82017-07-27 11:47:28 +02005659/* config parser for global "tune.h2.max-concurrent-streams" */
5660static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5661 struct proxy *defpx, const char *file, int line,
5662 char **err)
5663{
5664 if (too_many_args(1, args, err, NULL))
5665 return -1;
5666
5667 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005668 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005669 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5670 return -1;
5671 }
5672 return 0;
5673}
5674
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005675/* config parser for global "tune.h2.max-frame-size" */
5676static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5677 struct proxy *defpx, const char *file, int line,
5678 char **err)
5679{
5680 if (too_many_args(1, args, err, NULL))
5681 return -1;
5682
5683 h2_settings_max_frame_size = atoi(args[1]);
5684 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5685 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5686 return -1;
5687 }
5688 return 0;
5689}
5690
Willy Tarreau62f52692017-10-08 23:01:42 +02005691
5692/****************************************/
5693/* MUX initialization and instanciation */
5694/***************************************/
5695
5696/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005697static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005698 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005699 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005700 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005701 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005702 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005703 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005704 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005705 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005706 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005707 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005708 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005709 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005710 .shutr = h2_shutr,
5711 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005712 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005713 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005714 .name = "H2",
5715};
5716
Christopher Faulet32f61c02018-04-10 14:33:41 +02005717/* PROTO selection : this mux registers PROTO token "h2" */
5718static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005719 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005720
Willy Tarreau0108d902018-11-25 19:14:37 +01005721INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5722
Christopher Faulet9b579102019-04-11 22:52:25 +02005723
5724/* The mux operations */
5725static const struct mux_ops h2_htx_ops = {
5726 .init = h2_init,
5727 .wake = h2_wake,
5728 .snd_buf = h2_snd_buf,
5729 .rcv_buf = h2_rcv_buf,
5730 .subscribe = h2_subscribe,
5731 .unsubscribe = h2_unsubscribe,
5732 .attach = h2_attach,
5733 .get_first_cs = h2_get_first_cs,
5734 .detach = h2_detach,
5735 .destroy = h2_destroy,
5736 .avail_streams = h2_avail_streams,
5737 .used_streams = h2_used_streams,
5738 .shutr = h2_shutr,
5739 .shutw = h2_shutw,
5740 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005741 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005742 .name = "H2",
5743};
5744
Willy Tarreauf8957272018-10-03 10:25:20 +02005745static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005746 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005747
5748INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5749
Willy Tarreau62f52692017-10-08 23:01:42 +02005750/* config keyword parsers */
5751static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005752 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005753 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005754 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005755 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005756 { 0, NULL, NULL }
5757}};
5758
Willy Tarreau0108d902018-11-25 19:14:37 +01005759INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);