blob: ac34a7239f0f20d523dba7edb5aa75ebe0694fa7 [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
Willy Tarreau51330962019-05-26 09:38:07 +020081
Willy Tarreau9c218e72019-05-26 10:08:28 +020082/* 32 buffers: one for the ring's root, rest for the mbuf itself */
83#define H2C_MBUF_CNT 32
Willy Tarreau51330962019-05-26 09:38:07 +020084
Willy Tarreau5ab6b572017-09-22 08:05:00 +020085/* H2 connection descriptor */
86struct h2c {
87 struct connection *conn;
88
89 enum h2_cs st0; /* mux state */
90 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
91
92 /* 16 bit hole here */
93 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010094 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020095 int32_t max_id; /* highest ID known on this connection, <0 before preface */
96 uint32_t rcvd_c; /* newly received data to ACK for the connection */
97 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
98
99 /* states for the demux direction */
100 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200101 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200102
103 int32_t dsi; /* demux stream ID (<0 = idle) */
104 int32_t dfl; /* demux frame length (if dsi >= 0) */
105 int8_t dft; /* demux frame type (if dsi >= 0) */
106 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100107 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
108 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
110
111 /* states for the mux direction */
Willy Tarreau51330962019-05-26 09:38:07 +0200112 struct buffer mbuf[H2C_MBUF_CNT]; /* mux buffers (ring) */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200113 int32_t msi; /* mux stream ID (<0 = idle) */
114 int32_t mfl; /* mux frame length (if dsi >= 0) */
115 int8_t mft; /* mux frame type (if dsi >= 0) */
116 int8_t mff; /* mux frame flags (if dsi >= 0) */
117 /* 16 bit hole here */
118 int32_t miw; /* mux initial window size for all new streams */
119 int32_t mws; /* mux window size. Can be negative. */
120 int32_t mfs; /* mux's max frame size */
121
Willy Tarreauea392822017-10-31 10:02:25 +0100122 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100123 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100124 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200125 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100126 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100127 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200128 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100129 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200130 struct eb_root streams_by_id; /* all active streams by their ID */
131 struct list send_list; /* list of blocked streams requesting to send */
132 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreaubda92dd2019-10-02 10:49:59 +0200133 struct list blocked_list; /* list of streams blocked for other reasons (e.g. sfctl, dep) */
Olivier Houchardd846c262018-10-19 17:24:29 +0200134 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100135 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200136 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200137};
138
Willy Tarreau18312642017-10-11 07:57:07 +0200139/* H2 stream state, in h2s->st */
140enum h2_ss {
141 H2_SS_IDLE = 0, // idle
142 H2_SS_RLOC, // reserved(local)
143 H2_SS_RREM, // reserved(remote)
144 H2_SS_OPEN, // open
145 H2_SS_HREM, // half-closed(remote)
146 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200147 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200148 H2_SS_CLOSED, // closed
149 H2_SS_ENTRIES // must be last
150} __attribute__((packed));
151
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200152#define H2_SS_MASK(state) (1UL << (state))
153#define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE)
154#define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC)
155#define H2_SS_RREM_BIT (1UL << H2_SS_RREM)
156#define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN)
157#define H2_SS_HREM_BIT (1UL << H2_SS_HREM)
158#define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC)
159#define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR)
160#define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED)
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200161
Willy Tarreau18312642017-10-11 07:57:07 +0200162/* HTTP/2 stream flags (32 bit), in h2s->flags */
163#define H2_SF_NONE 0x00000000
164#define H2_SF_ES_RCVD 0x00000001
165#define H2_SF_ES_SENT 0x00000002
166
167#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
168#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
169
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200170/* stream flags indicating the reason the stream is blocked */
171#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
Willy Tarreaubda92dd2019-10-02 10:49:59 +0200172#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux (must be in send list)
173#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl (must be in fctl list)
174#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl (must be in blocked list)
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200175#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
176
Willy Tarreau454f9052017-10-26 19:40:35 +0200177/* stream flags indicating how data is supposed to be sent */
178#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
179#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
180
181/* step we're currently in when sending chunks. This is needed because we may
182 * have to transfer chunks as large as a full buffer so there's no room left
183 * for size nor crlf around.
184 */
185#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
186#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
187#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
188
189#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
190
Willy Tarreau67434202017-11-06 20:20:51 +0100191#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100192#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100193
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100194#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
195
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200196#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
197#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
Willy Tarreau3cf69fe2019-05-14 10:44:40 +0200198#define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200199
200
Willy Tarreau18312642017-10-11 07:57:07 +0200201/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
202 * it is being processed in the internal HTTP representation (H1 for now).
203 */
204struct h2s {
205 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100206 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200207 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200208 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200209 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200210 int32_t id; /* stream ID */
211 uint32_t flags; /* H2_SF_* */
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200212 int sws; /* stream window size, to be added to the mux's initial window size */
Willy Tarreau18312642017-10-11 07:57:07 +0200213 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
214 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200215 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100216 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200217 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200218 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 +0100219 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
220 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 +0200221 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100222 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200223};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200224
Willy Tarreauc6405142017-09-21 20:23:50 +0200225/* descriptor for an h2 frame header */
226struct h2_fh {
227 uint32_t len; /* length, host order, 24 bits */
228 uint32_t sid; /* stream id, host order, 31 bits */
229 uint8_t ft; /* frame type */
230 uint8_t ff; /* frame flags */
231};
232
Willy Tarreau8ceae722018-11-26 11:58:30 +0100233/* the h2c connection pool */
234DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
235
236/* the h2s stream pool */
237DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
238
Willy Tarreaudc572362018-12-12 08:08:05 +0100239/* The default connection window size is 65535, it may only be enlarged using
240 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
241 * we'll pretend we already received the difference between the two to send
242 * an equivalent window update to enlarge it to 2G-1.
243 */
244#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
245
Willy Tarreau455d5682019-05-24 19:42:18 +0200246/* maximum amount of data we're OK with re-aligning for buffer optimizations */
247#define MAX_DATA_REALIGN 1024
248
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200249/* a few settings from the global section */
250static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200251static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100252static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100253static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200254
Willy Tarreau2a856182017-05-16 15:20:39 +0200255/* a dmumy closed stream */
256static const struct h2s *h2_closed_stream = &(const struct h2s){
257 .cs = NULL,
258 .h2c = NULL,
259 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100260 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100261 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200262 .id = 0,
263};
264
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100265/* a dmumy closed stream returning a PROTOCOL_ERROR error */
266static const struct h2s *h2_error_stream = &(const struct h2s){
267 .cs = NULL,
268 .h2c = NULL,
269 .st = H2_SS_CLOSED,
270 .errcode = H2_ERR_PROTOCOL_ERROR,
271 .flags = 0,
272 .id = 0,
273};
274
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100275/* a dmumy closed stream returning a REFUSED_STREAM error */
276static const struct h2s *h2_refused_stream = &(const struct h2s){
277 .cs = NULL,
278 .h2c = NULL,
279 .st = H2_SS_CLOSED,
280 .errcode = H2_ERR_REFUSED_STREAM,
281 .flags = 0,
282 .id = 0,
283};
284
Willy Tarreau2a856182017-05-16 15:20:39 +0200285/* and a dummy idle stream for use with any unannounced stream */
286static const struct h2s *h2_idle_stream = &(const struct h2s){
287 .cs = NULL,
288 .h2c = NULL,
289 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100290 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200291 .id = 0,
292};
293
Olivier Houchard9f6af332018-05-25 14:04:04 +0200294static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200295static int h2_send(struct h2c *h2c);
296static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200297static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200298static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100299static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100300static 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 +0100301static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200302static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100303static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100304static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200305
Willy Tarreau534d8f92019-10-01 10:12:00 +0200306/* returns true if the connection is allowed to expire, false otherwise. A
307 * connection may expire when:
308 * - it has no stream
309 * - it has data in the mux buffer
310 * - it has streams in the blocked list
311 * - it has streams in the fctl list
312 * - it has streams in the send list
313 * Otherwise it means some streams are waiting in the data layer and it should
314 * not expire.
315 */
316static inline int h2c_may_expire(const struct h2c *h2c)
317{
318 return eb_is_empty(&h2c->streams_by_id) ||
319 br_data(h2c->mbuf) ||
320 !LIST_ISEMPTY(&h2c->blocked_list) ||
321 !LIST_ISEMPTY(&h2c->fctl_list) ||
322 !LIST_ISEMPTY(&h2c->send_list) ||
323 !LIST_ISEMPTY(&h2c->sending_list);
324}
325
Olivier Houchard7a977432019-03-21 15:47:13 +0100326static __inline int
Willy Tarreau534d8f92019-10-01 10:12:00 +0200327h2c_is_dead(const struct h2c *h2c)
Olivier Houchard7a977432019-03-21 15:47:13 +0100328{
329 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
330 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
331 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
332 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200333 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100334 (conn_xprt_read0_pending(h2c->conn) ||
335 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
336 return 1;
337
338 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100339}
340
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200341/*****************************************************/
342/* functions below are for dynamic buffer management */
343/*****************************************************/
344
Willy Tarreau315d8072017-12-10 22:17:57 +0100345/* indicates whether or not the we may call the h2_recv() function to attempt
346 * to receive data into the buffer and/or demux pending data. The condition is
347 * a bit complex due to some API limits for now. The rules are the following :
348 * - if an error or a shutdown was detected on the connection and the buffer
349 * is empty, we must not attempt to receive
350 * - if the demux buf failed to be allocated, we must not try to receive and
351 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100352 * - if no flag indicates a blocking condition, we may attempt to receive,
353 * regardless of whether the demux buffer is full or not, so that only
354 * de demux part decides whether or not to block. This is needed because
355 * the connection API indeed prevents us from re-enabling receipt that is
356 * already enabled in a polled state, so we must always immediately stop
357 * as soon as the demux can't proceed so as never to hit an end of read
358 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100359 * - otherwise must may not attempt
360 */
361static inline int h2_recv_allowed(const struct h2c *h2c)
362{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200363 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100364 (h2c->st0 >= H2_CS_ERROR ||
365 h2c->conn->flags & CO_FL_ERROR ||
366 conn_xprt_read0_pending(h2c->conn)))
367 return 0;
368
369 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100370 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100371 return 1;
372
373 return 0;
374}
375
Willy Tarreau47b515a2018-12-21 16:09:41 +0100376/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200377static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100378{
379 if (!h2_recv_allowed(h2c))
380 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200381 if ((!consider_buffer || !b_data(&h2c->dbuf))
382 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100383 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200384 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100385}
386
387
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100388/* returns true if the front connection has too many conn_streams attached */
389static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200390{
Willy Tarreaua8754662018-12-23 20:43:58 +0100391 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200392}
393
Willy Tarreau44e973f2018-03-01 17:49:30 +0100394/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
395 * flags are used to figure what buffer was requested. It returns 1 if the
396 * allocation succeeds, in which case the connection is woken up, or 0 if it's
397 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200398 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100399static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200400{
401 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100402 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200403
Willy Tarreau44e973f2018-03-01 17:49:30 +0100404 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200405 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200406 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200407 return 1;
408 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200409
Willy Tarreau51330962019-05-26 09:38:07 +0200410 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100411 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200412
413 if (h2c->flags & H2_CF_DEM_MROOM) {
414 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200415 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200416 }
Willy Tarreau14398122017-09-22 14:26:04 +0200417 return 1;
418 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100419
420 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
421 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200422 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100423 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200424 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100425 return 1;
426 }
427
Willy Tarreau14398122017-09-22 14:26:04 +0200428 return 0;
429}
430
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200431static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200432{
433 struct buffer *buf = NULL;
434
Willy Tarreauc234ae32019-05-13 17:56:11 +0200435 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100436 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
437 h2c->buf_wait.target = h2c;
438 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100439 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100440 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100441 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200442 __conn_xprt_stop_recv(h2c->conn);
443 }
444 return buf;
445}
446
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200447static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200448{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200449 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100450 b_free(bptr);
Willy Tarreau201840a2019-05-29 17:50:48 +0200451 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200452 }
453}
454
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200455static inline void h2_release_mbuf(struct h2c *h2c)
456{
457 struct buffer *buf;
458 unsigned int count = 0;
459
460 while (b_size(buf = br_head_pick(h2c->mbuf))) {
461 b_free(buf);
462 count++;
463 }
464 if (count)
Willy Tarreau201840a2019-05-29 17:50:48 +0200465 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200466}
467
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100468/* returns the number of allocatable outgoing streams for the connection taking
469 * the last_sid and the reserved ones into account.
470 */
471static inline int h2_streams_left(const struct h2c *h2c)
472{
473 int ret;
474
475 /* consider the number of outgoing streams we're allowed to create before
476 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
477 * nb_reserved is the number of streams which don't yet have an ID.
478 */
479 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
480 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
481 if (ret < 0)
482 ret = 0;
483 return ret;
484}
485
Willy Tarreau00f18a32019-01-26 12:19:01 +0100486/* returns the number of streams in use on a connection to figure if it's
487 * idle or not. We check nb_cs and not nb_streams as the caller will want
488 * to know if it was the last one after a detach().
489 */
490static int h2_used_streams(struct connection *conn)
491{
492 struct h2c *h2c = conn->ctx;
493
494 return h2c->nb_cs;
495}
496
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100497/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100498static int h2_avail_streams(struct connection *conn)
499{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100500 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100501 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100502 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100503
Willy Tarreau6afec462019-01-28 06:40:19 +0100504 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
505 * streams on the connection.
506 */
507 if (h2c->last_sid >= 0)
508 return 0;
509
Willy Tarreau86949782019-01-31 10:42:05 +0100510 /* note: may be negative if a SETTINGS frame changes the limit */
511 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100512
513 /* we must also consider the limit imposed by stream IDs */
514 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100515 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100516 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100517 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
518 ret1 = MIN(ret1, ret2);
519 }
520 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100521}
522
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200523
Willy Tarreau62f52692017-10-08 23:01:42 +0200524/*****************************************************************/
525/* functions below are dedicated to the mux setup and management */
526/*****************************************************************/
527
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200528/* Initialize the mux once it's attached. For outgoing connections, the context
529 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200530 * connections from the fact that the context is still NULL (even during mux
531 * upgrades). <input> is always used as Input buffer and may contain data. It is
532 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200533 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200534static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
535 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200536{
537 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100538 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539
Willy Tarreaubafbe012017-11-24 17:34:44 +0100540 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200541 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200542 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200543
Christopher Faulete9b70722019-04-08 10:46:02 +0200544 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200545 h2c->flags = H2_CF_IS_BACK;
546 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
547 if (tick_isset(prx->timeout.serverfin))
548 h2c->shut_timeout = prx->timeout.serverfin;
549 } else {
550 h2c->flags = H2_CF_NONE;
551 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
552 if (tick_isset(prx->timeout.clientfin))
553 h2c->shut_timeout = prx->timeout.clientfin;
554 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100555
Willy Tarreau0b37d652018-10-03 10:33:02 +0200556 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100557 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100558 if (tick_isset(h2c->timeout)) {
559 t = task_new(tid_bit);
560 if (!t)
561 goto fail;
562
563 h2c->task = t;
564 t->process = h2_timeout_task;
565 t->context = h2c;
566 t->expire = tick_add(now_ms, h2c->timeout);
567 }
Willy Tarreauea392822017-10-31 10:02:25 +0100568
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200569 h2c->wait_event.tasklet = tasklet_new();
570 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200571 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200572 h2c->wait_event.tasklet->process = h2_io_cb;
573 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100574 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200575
Willy Tarreau32218eb2017-09-22 08:07:25 +0200576 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
577 if (!h2c->ddht)
578 goto fail;
579
580 /* Initialise the context. */
581 h2c->st0 = H2_CS_PREFACE;
582 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100583 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200584 h2c->max_id = -1;
585 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100586 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200587 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100588 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200589 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100590 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100591 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200592
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200593 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200594 h2c->dsi = -1;
595 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100596
Willy Tarreau32218eb2017-09-22 08:07:25 +0200597 h2c->last_sid = -1;
598
Willy Tarreau51330962019-05-26 09:38:07 +0200599 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200600 h2c->miw = 65535; /* mux initial window size */
601 h2c->mws = 65535; /* mux window size */
602 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200603 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200604 LIST_INIT(&h2c->send_list);
605 LIST_INIT(&h2c->fctl_list);
Willy Tarreaubda92dd2019-10-02 10:49:59 +0200606 LIST_INIT(&h2c->blocked_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200607 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100608 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200609
Willy Tarreau3f133572017-10-31 19:21:06 +0100610 if (t)
611 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100612
Willy Tarreau01b44822018-10-03 14:26:37 +0200613 if (h2c->flags & H2_CF_IS_BACK) {
614 /* FIXME: this is temporary, for outgoing connections we need
615 * to immediately allocate a stream until the code is modified
616 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100617 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200618 */
619 struct h2s *h2s;
620
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100621 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200622 if (!h2s)
623 goto fail_stream;
624 }
625
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100626 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200627
Willy Tarreau0f383582018-10-03 14:22:21 +0200628 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200629 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200630 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200631 fail_stream:
632 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200633 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200634 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200635 if (h2c->wait_event.tasklet)
636 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100637 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200638 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200639 return -1;
640}
641
Willy Tarreau751f2d02018-10-05 09:35:00 +0200642/* returns the next allocatable outgoing stream ID for the H2 connection, or
643 * -1 if no more is allocatable.
644 */
645static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
646{
647 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100648
649 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200650 id = -1;
651 return id;
652}
653
Willy Tarreau2373acc2017-10-12 17:35:14 +0200654/* returns the stream associated with id <id> or NULL if not found */
655static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
656{
657 struct eb32_node *node;
658
Willy Tarreau751f2d02018-10-05 09:35:00 +0200659 if (id == 0)
660 return (struct h2s *)h2_closed_stream;
661
Willy Tarreau2a856182017-05-16 15:20:39 +0200662 if (id > h2c->max_id)
663 return (struct h2s *)h2_idle_stream;
664
Willy Tarreau2373acc2017-10-12 17:35:14 +0200665 node = eb32_lookup(&h2c->streams_by_id, id);
666 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200667 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200668
669 return container_of(node, struct h2s, by_id);
670}
671
Christopher Faulet73c12072019-04-08 11:23:22 +0200672/* release function. This one should be called to free all resources allocated
673 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200674 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200675static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200676{
Christopher Faulet61840e72019-04-15 09:33:32 +0200677 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200678
Willy Tarreau32218eb2017-09-22 08:07:25 +0200679 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200680 /* The connection must be aattached to this mux to be released */
681 if (h2c->conn && h2c->conn->ctx == h2c)
682 conn = h2c->conn;
683
Willy Tarreau32218eb2017-09-22 08:07:25 +0200684 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200685
Willy Tarreau186e96e2019-05-28 17:21:18 +0200686 if (LIST_ADDED(&h2c->buf_wait.list)) {
687 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
688 LIST_DEL(&h2c->buf_wait.list);
689 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
690 }
Willy Tarreau14398122017-09-22 14:26:04 +0200691
Willy Tarreau44e973f2018-03-01 17:49:30 +0100692 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200693 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100694
Willy Tarreauea392822017-10-31 10:02:25 +0100695 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200696 h2c->task->context = NULL;
697 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100698 h2c->task = NULL;
699 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200700 if (h2c->wait_event.tasklet)
701 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet0e012562019-09-18 11:07:20 +0200702 if (conn && h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100703 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Christopher Faulet0e012562019-09-18 11:07:20 +0200704 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100705
Willy Tarreaubafbe012017-11-24 17:34:44 +0100706 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200707 }
708
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200709 if (conn) {
710 conn->mux = NULL;
711 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200712
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200713 conn_stop_tracking(conn);
714 conn_full_close(conn);
715 if (conn->destroy_cb)
716 conn->destroy_cb(conn);
717 conn_free(conn);
718 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200719}
720
721
Willy Tarreau71681172017-10-23 14:39:06 +0200722/******************************************************/
723/* functions below are for the H2 protocol processing */
724/******************************************************/
725
726/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100727static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200728{
729 return h2s ? h2s->id : 0;
730}
731
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200732/* returns the sum of the stream's own window size and the mux's initial
733 * window, which together form the stream's effective window size.
734 */
735static inline int h2s_mws(const struct h2s *h2s)
736{
737 return h2s->sws + h2s->h2c->miw;
738}
739
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200740/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100741static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200742{
743 if (h2c->msi < 0)
744 return 0;
745
746 if (h2c->msi == h2s_id(h2s))
747 return 0;
748
749 return 1;
750}
751
Willy Tarreau741d6df2017-10-17 08:00:59 +0200752/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100753static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200754{
755 h2c->errcode = err;
756 h2c->st0 = H2_CS_ERROR;
757}
758
Willy Tarreau175cebb2019-01-24 10:02:24 +0100759/* marks an error on the stream. It may also update an already closed stream
760 * (e.g. to report an error after an RST was received).
761 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100762static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200763{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100764 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200765 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100766 if (h2s->st < H2_SS_ERROR)
767 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100768 if (h2s->cs)
769 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200770 }
771}
772
Willy Tarreau7e094452018-12-19 18:08:52 +0100773/* attempt to notify the data layer of recv availability */
774static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
775{
776 struct wait_event *sw;
777
778 if (h2s->recv_wait) {
779 sw = h2s->recv_wait;
780 sw->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200781 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100782 h2s->recv_wait = NULL;
783 }
784}
785
786/* attempt to notify the data layer of send availability */
787static void __maybe_unused h2s_notify_send(struct h2s *h2s)
788{
789 struct wait_event *sw;
790
Willy Tarreauc234ae32019-05-13 17:56:11 +0200791 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100792 sw = h2s->send_wait;
793 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100794 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200795 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100796 }
797}
798
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100799/* alerts the data layer, trying to wake it up by all means, following
800 * this sequence :
801 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
802 * - if its subscribed to send, then it's woken up for send
803 * - if it was subscribed to neither, its ->wake() callback is called
804 * It is safe to call this function with a closed stream which doesn't have a
805 * conn_stream anymore.
806 */
807static void __maybe_unused h2s_alert(struct h2s *h2s)
808{
809 if (h2s->recv_wait || h2s->send_wait) {
810 h2s_notify_recv(h2s);
811 h2s_notify_send(h2s);
812 }
813 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
814 h2s->cs->data_cb->wake(h2s->cs);
815}
816
Willy Tarreaue4820742017-07-27 13:37:23 +0200817/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100818static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200819{
820 uint8_t *out = frame;
821
822 *out = len >> 16;
823 write_n16(out + 1, len);
824}
825
Willy Tarreau54c15062017-10-10 17:10:03 +0200826/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
827 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
828 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200829 * available in the buffer's input prior to calling this function. The buffer
830 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200831 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100832static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200833 const struct buffer *b, int o)
834{
Willy Tarreau591d4452018-06-15 17:21:00 +0200835 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 +0200836}
837
Willy Tarreau1f094672017-11-20 21:27:45 +0100838static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200839{
Willy Tarreau591d4452018-06-15 17:21:00 +0200840 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200841}
842
Willy Tarreau1f094672017-11-20 21:27:45 +0100843static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200844{
Willy Tarreau591d4452018-06-15 17:21:00 +0200845 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200846}
847
Willy Tarreau1f094672017-11-20 21:27:45 +0100848static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200849{
Willy Tarreau591d4452018-06-15 17:21:00 +0200850 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200851}
852
853
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100854/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
855 * The algorithm is not obvious. It turns out that H2 headers are neither
856 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
857 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200858 *
859 * b0 b1 b2 b3 b4 b5..b8
860 * +----------+---------+--------+----+----+----------------------+
861 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
862 * +----------+---------+--------+----+----+----------------------+
863 *
864 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
865 * we get the sid properly aligned and ordered, and 16 bits of len properly
866 * ordered as well. The type and flags can be extracted using bit shifts from
867 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200868 * Returns zero if some bytes are missing, otherwise non-zero on success. The
869 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200870 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100871static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200872{
873 uint64_t w;
874
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100875 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200876 return 0;
877
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100878 w = h2_get_n64(b, o + 1);
879 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200880 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
881 h->ff = w >> 32;
882 h->ft = w >> 40;
883 h->len += w >> 48;
884 return 1;
885}
886
887/* skip the next 9 bytes corresponding to the frame header possibly parsed by
888 * h2_peek_frame_hdr() above.
889 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100890static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200891{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200892 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200893}
894
895/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100896static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200897{
898 int ret;
899
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100900 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200901 if (ret > 0)
902 h2_skip_frame_hdr(b);
903 return ret;
904}
905
Willy Tarreau00dd0782018-03-01 16:31:34 +0100906/* marks stream <h2s> as CLOSED and decrement the number of active streams for
907 * its connection if the stream was not yet closed. Please use this exclusively
908 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100909 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100910static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100911{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100912 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100913 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100914 if (!h2s->id)
915 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100916 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +0100917 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
918 h2s_notify_recv(h2s);
919 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100920 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100921 h2s->st = H2_SS_CLOSED;
922}
923
Willy Tarreau71049cc2018-03-28 13:56:39 +0200924/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
925static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100926{
927 h2s_close(h2s);
928 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200929 if (b_size(&h2s->rxbuf)) {
930 b_free(&h2s->rxbuf);
931 offer_buffers(NULL, tasks_run_queue);
932 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200933 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100934 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200935 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100936 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800937 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200938 * reference left would be in the h2c send_list/fctl_list, and if
939 * we're in it, we're getting out anyway
940 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100941 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200942 if (LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau86eded62019-06-14 14:47:49 +0200943 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +0200944 LIST_DEL_INIT(&h2s->sending_list);
945 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200946 tasklet_free(h2s->wait_event.tasklet);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100947 pool_free(pool_head_h2s, h2s);
948}
949
Willy Tarreaua8e49542018-10-03 18:53:55 +0200950/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
951 * stream tree. In case of error, nothing is added and NULL is returned. The
952 * causes of errors can be any failed memory allocation. The caller is
953 * responsible for checking if the connection may support an extra stream
954 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200955 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200956static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200957{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200958 struct h2s *h2s;
959
Willy Tarreaubafbe012017-11-24 17:34:44 +0100960 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200961 if (!h2s)
962 goto out;
963
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200964 h2s->wait_event.tasklet = tasklet_new();
965 if (!h2s->wait_event.tasklet) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200966 pool_free(pool_head_h2s, h2s);
967 goto out;
968 }
969 h2s->send_wait = NULL;
970 h2s->recv_wait = NULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200971 h2s->wait_event.tasklet->process = h2_deferred_shut;
972 h2s->wait_event.tasklet->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100973 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200974 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100975 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200976 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200977 h2s->cs = NULL;
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200978 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200979 h2s->flags = H2_SF_NONE;
980 h2s->errcode = H2_ERR_NO_ERROR;
981 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200982 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100983 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200984 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200985
986 if (h2c->flags & H2_CF_IS_BACK) {
987 h1m_init_req(&h2s->h1m);
988 h2s->h1m.err_pos = -1; // don't care about errors on the request path
989 h2s->h1m.flags |= H1_MF_TOLOWER;
990 } else {
991 h1m_init_res(&h2s->h1m);
992 h2s->h1m.err_pos = -1; // don't care about errors on the response path
993 h2s->h1m.flags |= H1_MF_TOLOWER;
994 }
995
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200996 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200997 if (id > 0)
998 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100999 else
1000 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001001
1002 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001003 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001004 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001005
1006 return h2s;
1007
1008 out_free_h2s:
1009 pool_free(pool_head_h2s, h2s);
1010 out:
1011 return NULL;
1012}
1013
1014/* creates a new stream <id> on the h2c connection and returns it, or NULL in
1015 * case of memory allocation error.
1016 */
1017static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
1018{
1019 struct session *sess = h2c->conn->owner;
1020 struct conn_stream *cs;
1021 struct h2s *h2s;
1022
1023 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
1024 goto out;
1025
1026 h2s = h2s_new(h2c, id);
1027 if (!h2s)
1028 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001029
1030 cs = cs_new(h2c->conn);
1031 if (!cs)
1032 goto out_close;
1033
Olivier Houchard746fb772018-12-15 19:42:00 +01001034 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001035 h2s->cs = cs;
1036 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001037 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001038
1039 if (stream_create_from_cs(cs) < 0)
1040 goto out_free_cs;
1041
Willy Tarreau590a0512018-09-05 11:56:48 +02001042 /* We want the accept date presented to the next stream to be the one
1043 * we have now, the handshake time to be null (since the next stream
1044 * is not delayed by a handshake), and the idle time to count since
1045 * right now.
1046 */
1047 sess->accept_date = date;
1048 sess->tv_accept = now;
1049 sess->t_handshake = 0;
1050
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001051 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001052 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001053 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001054 return h2s;
1055
1056 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001057 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001058 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001059 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001060 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001061 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001062 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001063 sess_log(sess);
1064 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001065}
1066
Willy Tarreau751f2d02018-10-05 09:35:00 +02001067/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1068 * and returns it, or NULL in case of memory allocation error or if the highest
1069 * possible stream ID was reached.
1070 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001071static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001072{
1073 struct h2s *h2s = NULL;
1074
Willy Tarreau86949782019-01-31 10:42:05 +01001075 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001076 goto out;
1077
Willy Tarreaua80dca82019-01-24 17:08:28 +01001078 if (h2_streams_left(h2c) < 1)
1079 goto out;
1080
Willy Tarreau751f2d02018-10-05 09:35:00 +02001081 /* Defer choosing the ID until we send the first message to create the stream */
1082 h2s = h2s_new(h2c, 0);
1083 if (!h2s)
1084 goto out;
1085
1086 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001087 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001088 cs->ctx = h2s;
1089 h2c->nb_cs++;
1090
Willy Tarreau751f2d02018-10-05 09:35:00 +02001091 out:
1092 return h2s;
1093}
1094
Willy Tarreaube5b7152017-09-25 16:25:39 +02001095/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1096 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1097 * the various settings codes.
1098 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001099static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001100{
1101 struct buffer *res;
1102 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001103 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001104 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001105 int ret;
1106
1107 if (h2c_mux_busy(h2c, NULL)) {
1108 h2c->flags |= H2_CF_DEM_MBUSY;
1109 return 0;
1110 }
1111
Willy Tarreaube5b7152017-09-25 16:25:39 +02001112 chunk_init(&buf, buf_data, sizeof(buf_data));
1113 chunk_memcpy(&buf,
1114 "\x00\x00\x00" /* length : 0 for now */
1115 "\x04\x00" /* type : 4 (settings), flags : 0 */
1116 "\x00\x00\x00\x00", /* stream ID : 0 */
1117 9);
1118
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001119 if (h2c->flags & H2_CF_IS_BACK) {
1120 /* send settings_enable_push=0 */
1121 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1122 }
1123
Willy Tarreaube5b7152017-09-25 16:25:39 +02001124 if (h2_settings_header_table_size != 4096) {
1125 char str[6] = "\x00\x01"; /* header_table_size */
1126
1127 write_n32(str + 2, h2_settings_header_table_size);
1128 chunk_memcat(&buf, str, 6);
1129 }
1130
1131 if (h2_settings_initial_window_size != 65535) {
1132 char str[6] = "\x00\x04"; /* initial_window_size */
1133
1134 write_n32(str + 2, h2_settings_initial_window_size);
1135 chunk_memcat(&buf, str, 6);
1136 }
1137
1138 if (h2_settings_max_concurrent_streams != 0) {
1139 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1140
1141 /* Note: 0 means "unlimited" for haproxy's config but not for
1142 * the protocol, so never send this value!
1143 */
1144 write_n32(str + 2, h2_settings_max_concurrent_streams);
1145 chunk_memcat(&buf, str, 6);
1146 }
1147
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001148 mfs = h2_settings_max_frame_size;
1149 if (mfs > global.tune.bufsize)
1150 mfs = global.tune.bufsize;
1151
1152 if (!mfs)
1153 mfs = global.tune.bufsize;
1154
1155 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001156 char str[6] = "\x00\x05"; /* max_frame_size */
1157
1158 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1159 * match bufsize - rewrite size, but at the moment it seems
1160 * that clients don't take care of it.
1161 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001162 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001163 chunk_memcat(&buf, str, 6);
1164 }
1165
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001166 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001167
1168 res = br_tail(h2c->mbuf);
1169 retry:
1170 if (!h2_get_buf(h2c, res)) {
1171 h2c->flags |= H2_CF_MUX_MALLOC;
1172 h2c->flags |= H2_CF_DEM_MROOM;
1173 return 0;
1174 }
1175
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001176 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001177 if (unlikely(ret <= 0)) {
1178 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001179 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1180 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001181 h2c->flags |= H2_CF_MUX_MFULL;
1182 h2c->flags |= H2_CF_DEM_MROOM;
1183 return 0;
1184 }
1185 else {
1186 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1187 return 0;
1188 }
1189 }
1190 return ret;
1191}
1192
Willy Tarreau52eed752017-09-22 15:05:09 +02001193/* Try to receive a connection preface, then upon success try to send our
1194 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1195 * missing data. It may return an error in h2c.
1196 */
1197static int h2c_frt_recv_preface(struct h2c *h2c)
1198{
1199 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001200 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001201
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001202 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001203
1204 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001205 if (ret1 < 0)
1206 sess_log(h2c->conn->owner);
1207
Willy Tarreau52eed752017-09-22 15:05:09 +02001208 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1209 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1210 return 0;
1211 }
1212
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001213 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001214 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001215 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001216
Willy Tarreaube5b7152017-09-25 16:25:39 +02001217 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001218}
1219
Willy Tarreau01b44822018-10-03 14:26:37 +02001220/* Try to send a connection preface, then upon success try to send our
1221 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1222 * missing data. It may return an error in h2c.
1223 */
1224static int h2c_bck_send_preface(struct h2c *h2c)
1225{
1226 struct buffer *res;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001227 int ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001228
1229 if (h2c_mux_busy(h2c, NULL)) {
1230 h2c->flags |= H2_CF_DEM_MBUSY;
1231 return 0;
1232 }
1233
Willy Tarreaubcc45952019-05-26 10:05:50 +02001234 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001235 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001236 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001237 h2c->flags |= H2_CF_MUX_MALLOC;
1238 h2c->flags |= H2_CF_DEM_MROOM;
1239 return 0;
1240 }
1241
1242 if (!b_data(res)) {
1243 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001244 ret = b_istput(res, ist(H2_CONN_PREFACE));
1245 if (unlikely(ret <= 0)) {
1246 if (!ret) {
1247 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1248 goto retry;
1249 h2c->flags |= H2_CF_MUX_MFULL;
1250 h2c->flags |= H2_CF_DEM_MROOM;
1251 return 0;
1252 }
1253 else {
1254 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1255 return 0;
1256 }
1257 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001258 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001259 return h2c_send_settings(h2c);
1260}
1261
Willy Tarreau081d4722017-05-16 21:51:05 +02001262/* try to send a GOAWAY frame on the connection to report an error or a graceful
1263 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1264 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1265 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1266 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1267 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1268 * on unrecoverable failure. It will not attempt to send one again in this last
1269 * case so that it is safe to use h2c_error() to report such errors.
1270 */
1271static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1272{
1273 struct buffer *res;
1274 char str[17];
1275 int ret;
1276
1277 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1278 return 1; // claim that it worked
1279
1280 if (h2c_mux_busy(h2c, h2s)) {
1281 if (h2s)
1282 h2s->flags |= H2_SF_BLK_MBUSY;
1283 else
1284 h2c->flags |= H2_CF_DEM_MBUSY;
1285 return 0;
1286 }
1287
Willy Tarreau9c218e72019-05-26 10:08:28 +02001288 /* len: 8, type: 7, flags: none, sid: 0 */
1289 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1290
1291 if (h2c->last_sid < 0)
1292 h2c->last_sid = h2c->max_id;
1293
1294 write_n32(str + 9, h2c->last_sid);
1295 write_n32(str + 13, h2c->errcode);
1296
Willy Tarreaubcc45952019-05-26 10:05:50 +02001297 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001298 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001299 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001300 h2c->flags |= H2_CF_MUX_MALLOC;
1301 if (h2s)
1302 h2s->flags |= H2_SF_BLK_MROOM;
1303 else
1304 h2c->flags |= H2_CF_DEM_MROOM;
1305 return 0;
1306 }
1307
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001308 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001309 if (unlikely(ret <= 0)) {
1310 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001311 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1312 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001313 h2c->flags |= H2_CF_MUX_MFULL;
1314 if (h2s)
1315 h2s->flags |= H2_SF_BLK_MROOM;
1316 else
1317 h2c->flags |= H2_CF_DEM_MROOM;
1318 return 0;
1319 }
1320 else {
1321 /* we cannot report this error using GOAWAY, so we mark
1322 * it and claim a success.
1323 */
1324 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1325 h2c->flags |= H2_CF_GOAWAY_FAILED;
1326 return 1;
1327 }
1328 }
1329 h2c->flags |= H2_CF_GOAWAY_SENT;
1330 return ret;
1331}
1332
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001333/* Try to send an RST_STREAM frame on the connection for the indicated stream
1334 * during mux operations. This stream must be valid and cannot be closed
1335 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1336 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1337 * not yet.
1338 *
1339 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1340 * to write the message, it subscribes the stream to future notifications.
1341 */
1342static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1343{
1344 struct buffer *res;
1345 char str[13];
1346 int ret;
1347
1348 if (!h2s || h2s->st == H2_SS_CLOSED)
1349 return 1;
1350
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001351 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1352 * RST_STREAM in response to a RST_STREAM frame.
1353 */
Willy Tarreaubf9a20b2019-08-06 10:01:40 +02001354 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001355 ret = 1;
1356 goto ignore;
1357 }
1358
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001359 if (h2c_mux_busy(h2c, h2s)) {
1360 h2s->flags |= H2_SF_BLK_MBUSY;
1361 return 0;
1362 }
1363
Willy Tarreau9c218e72019-05-26 10:08:28 +02001364 /* len: 4, type: 3, flags: none */
1365 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1366 write_n32(str + 5, h2s->id);
1367 write_n32(str + 9, h2s->errcode);
1368
Willy Tarreaubcc45952019-05-26 10:05:50 +02001369 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001370 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001371 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001372 h2c->flags |= H2_CF_MUX_MALLOC;
1373 h2s->flags |= H2_SF_BLK_MROOM;
1374 return 0;
1375 }
1376
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001377 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001378 if (unlikely(ret <= 0)) {
1379 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001380 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1381 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001382 h2c->flags |= H2_CF_MUX_MFULL;
1383 h2s->flags |= H2_SF_BLK_MROOM;
1384 return 0;
1385 }
1386 else {
1387 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1388 return 0;
1389 }
1390 }
1391
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001392 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001393 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001394 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001395 return ret;
1396}
1397
1398/* Try to send an RST_STREAM frame on the connection for the stream being
1399 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001400 * error code, even if the stream is one of the dummy ones, and will update
1401 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001402 *
1403 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1404 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001405 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001406 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001407 */
1408static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1409{
1410 struct buffer *res;
1411 char str[13];
1412 int ret;
1413
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001414 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1415 * RST_STREAM in response to a RST_STREAM frame.
1416 */
1417 if (h2c->dft == H2_FT_RST_STREAM) {
1418 ret = 1;
1419 goto ignore;
1420 }
1421
Willy Tarreau27a84c92017-10-17 08:10:17 +02001422 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001423 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001424 return 0;
1425 }
1426
Willy Tarreau9c218e72019-05-26 10:08:28 +02001427 /* len: 4, type: 3, flags: none */
1428 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1429
1430 write_n32(str + 5, h2c->dsi);
1431 write_n32(str + 9, h2s->errcode);
1432
Willy Tarreaubcc45952019-05-26 10:05:50 +02001433 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001434 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001435 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001436 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001437 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001438 return 0;
1439 }
1440
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001441 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001442 if (unlikely(ret <= 0)) {
1443 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001444 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1445 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001446 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001447 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001448 return 0;
1449 }
1450 else {
1451 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1452 return 0;
1453 }
1454 }
1455
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001456 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001457 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001458 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001459 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001460 }
1461
Willy Tarreau27a84c92017-10-17 08:10:17 +02001462 return ret;
1463}
1464
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001465/* try to send an empty DATA frame with the ES flag set to notify about the
1466 * end of stream and match a shutdown(write). If an ES was already sent as
1467 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1468 * on success or zero if nothing was done. In case of lack of room to write the
1469 * message, it subscribes the requesting stream to future notifications.
1470 */
1471static int h2_send_empty_data_es(struct h2s *h2s)
1472{
1473 struct h2c *h2c = h2s->h2c;
1474 struct buffer *res;
1475 char str[9];
1476 int ret;
1477
Willy Tarreau721c9742017-11-07 11:05:42 +01001478 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001479 return 1;
1480
1481 if (h2c_mux_busy(h2c, h2s)) {
1482 h2s->flags |= H2_SF_BLK_MBUSY;
1483 return 0;
1484 }
1485
Willy Tarreau9c218e72019-05-26 10:08:28 +02001486 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1487 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1488 write_n32(str + 5, h2s->id);
1489
Willy Tarreaubcc45952019-05-26 10:05:50 +02001490 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001491 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001492 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001493 h2c->flags |= H2_CF_MUX_MALLOC;
1494 h2s->flags |= H2_SF_BLK_MROOM;
1495 return 0;
1496 }
1497
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001498 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001499 if (likely(ret > 0)) {
1500 h2s->flags |= H2_SF_ES_SENT;
1501 }
1502 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001503 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1504 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001505 h2c->flags |= H2_CF_MUX_MFULL;
1506 h2s->flags |= H2_SF_BLK_MROOM;
1507 return 0;
1508 }
1509 else {
1510 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1511 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001512 }
1513 return ret;
1514}
1515
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001516/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001517 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001518 * is automatically updated accordingly. If the stream is orphaned, it is
1519 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001520 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001521static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001522{
1523 if (!h2s->cs) {
1524 /* this stream was already orphaned */
1525 h2s_destroy(h2s);
1526 return;
1527 }
1528
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001529 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001530 if (h2s->st == H2_SS_OPEN)
1531 h2s->st = H2_SS_HREM;
1532 else if (h2s->st == H2_SS_HLOC)
1533 h2s_close(h2s);
1534 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001535
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001536 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1537 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1538 h2s->cs->flags |= CS_FL_ERR_PENDING;
1539 if (h2s->cs->flags & CS_FL_EOS)
1540 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001541
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001542 if (h2s->st < H2_SS_ERROR)
1543 h2s->st = H2_SS_ERROR;
1544 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001545
1546 h2s_alert(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001547}
1548
1549/* wake the streams attached to the connection, whose id is greater than <last>
1550 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001551 */
Willy Tarreau23482912019-05-07 15:23:14 +02001552static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001553{
1554 struct eb32_node *node;
1555 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001556
Christopher Fauletf02ca002019-03-07 16:21:34 +01001557 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001558 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1559 while (node) {
1560 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001561 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001562 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001563 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001564
Christopher Fauletf02ca002019-03-07 16:21:34 +01001565 /* Wake all streams with unassigned ID (ID == 0) */
1566 node = eb32_lookup(&h2c->streams_by_id, 0);
1567 while (node) {
1568 h2s = container_of(node, struct h2s, by_id);
1569 if (h2s->id > 0)
1570 break;
1571 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001572 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001573 }
1574}
1575
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001576/* Wake up all blocked streams whose window size has become positive after the
1577 * mux's initial window was adjusted. This should be done after having processed
1578 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001579 */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001580static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001581{
1582 struct h2s *h2s;
1583 struct eb32_node *node;
1584
Willy Tarreau3421aba2017-07-27 15:41:03 +02001585 node = eb32_first(&h2c->streams_by_id);
1586 while (node) {
1587 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001588 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001589 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreaubda92dd2019-10-02 10:49:59 +02001590 LIST_DEL_INIT(&h2s->list);
1591 if (h2s->send_wait)
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001592 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001593 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001594 node = eb32_next(node);
1595 }
1596}
1597
1598/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1599 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001600 * return an error in h2c. The caller must have already verified frame length
1601 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001602 */
1603static int h2c_handle_settings(struct h2c *h2c)
1604{
1605 unsigned int offset;
1606 int error;
1607
1608 if (h2c->dff & H2_F_SETTINGS_ACK) {
1609 if (h2c->dfl) {
1610 error = H2_ERR_FRAME_SIZE_ERROR;
1611 goto fail;
1612 }
1613 return 1;
1614 }
1615
Willy Tarreau3421aba2017-07-27 15:41:03 +02001616 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001617 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001618 return 0;
1619
1620 /* parse the frame */
1621 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001622 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1623 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001624
1625 switch (type) {
1626 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1627 /* we need to update all existing streams with the
1628 * difference from the previous iws.
1629 */
1630 if (arg < 0) { // RFC7540#6.5.2
1631 error = H2_ERR_FLOW_CONTROL_ERROR;
1632 goto fail;
1633 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001634 h2c->miw = arg;
1635 break;
1636 case H2_SETTINGS_MAX_FRAME_SIZE:
1637 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1638 error = H2_ERR_PROTOCOL_ERROR;
1639 goto fail;
1640 }
1641 h2c->mfs = arg;
1642 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001643 case H2_SETTINGS_ENABLE_PUSH:
1644 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1645 error = H2_ERR_PROTOCOL_ERROR;
1646 goto fail;
1647 }
1648 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001649 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1650 if (h2c->flags & H2_CF_IS_BACK) {
1651 /* the limit is only for the backend; for the frontend it is our limit */
1652 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1653 arg = h2_settings_max_concurrent_streams;
1654 h2c->streams_limit = arg;
1655 }
1656 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001657 }
1658 }
1659
1660 /* need to ACK this frame now */
1661 h2c->st0 = H2_CS_FRAME_A;
1662 return 1;
1663 fail:
Willy Tarreau21178a52019-10-23 11:06:35 +02001664 if (!(h2c->flags & H2_CF_IS_BACK))
1665 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001666 h2c_error(h2c, error);
1667 return 0;
1668}
1669
1670/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1671 * success or one of the h2_status values.
1672 */
1673static int h2c_ack_settings(struct h2c *h2c)
1674{
1675 struct buffer *res;
1676 char str[9];
1677 int ret = -1;
1678
1679 if (h2c_mux_busy(h2c, NULL)) {
1680 h2c->flags |= H2_CF_DEM_MBUSY;
1681 return 0;
1682 }
1683
Willy Tarreau9c218e72019-05-26 10:08:28 +02001684 memcpy(str,
1685 "\x00\x00\x00" /* length : 0 (no data) */
1686 "\x04" "\x01" /* type : 4, flags : ACK */
1687 "\x00\x00\x00\x00" /* stream ID */, 9);
1688
Willy Tarreaubcc45952019-05-26 10:05:50 +02001689 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001690 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001691 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001692 h2c->flags |= H2_CF_MUX_MALLOC;
1693 h2c->flags |= H2_CF_DEM_MROOM;
1694 return 0;
1695 }
1696
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001697 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001698 if (unlikely(ret <= 0)) {
1699 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001700 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1701 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001702 h2c->flags |= H2_CF_MUX_MFULL;
1703 h2c->flags |= H2_CF_DEM_MROOM;
1704 return 0;
1705 }
1706 else {
1707 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1708 return 0;
1709 }
1710 }
1711 return ret;
1712}
1713
Willy Tarreaucf68c782017-10-10 17:11:41 +02001714/* processes a PING frame and schedules an ACK if needed. The caller must pass
1715 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001716 * missing data. The caller must have already verified frame length
1717 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001718 */
1719static int h2c_handle_ping(struct h2c *h2c)
1720{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001721 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001722 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001723 h2c->st0 = H2_CS_FRAME_A;
1724 return 1;
1725}
1726
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001727/* Try to send a window update for stream id <sid> and value <increment>.
1728 * Returns > 0 on success or zero on missing room or failure. It may return an
1729 * error in h2c.
1730 */
1731static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1732{
1733 struct buffer *res;
1734 char str[13];
1735 int ret = -1;
1736
1737 if (h2c_mux_busy(h2c, NULL)) {
1738 h2c->flags |= H2_CF_DEM_MBUSY;
1739 return 0;
1740 }
1741
Willy Tarreau9c218e72019-05-26 10:08:28 +02001742 /* length: 4, type: 8, flags: none */
1743 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1744 write_n32(str + 5, sid);
1745 write_n32(str + 9, increment);
1746
Willy Tarreaubcc45952019-05-26 10:05:50 +02001747 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001748 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001749 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001750 h2c->flags |= H2_CF_MUX_MALLOC;
1751 h2c->flags |= H2_CF_DEM_MROOM;
1752 return 0;
1753 }
1754
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001755 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001756 if (unlikely(ret <= 0)) {
1757 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001758 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1759 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001760 h2c->flags |= H2_CF_MUX_MFULL;
1761 h2c->flags |= H2_CF_DEM_MROOM;
1762 return 0;
1763 }
1764 else {
1765 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1766 return 0;
1767 }
1768 }
1769 return ret;
1770}
1771
1772/* try to send pending window update for the connection. It's safe to call it
1773 * with no pending updates. Returns > 0 on success or zero on missing room or
1774 * failure. It may return an error in h2c.
1775 */
1776static int h2c_send_conn_wu(struct h2c *h2c)
1777{
1778 int ret = 1;
1779
1780 if (h2c->rcvd_c <= 0)
1781 return 1;
1782
Willy Tarreau97aaa672018-12-23 09:49:04 +01001783 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1784 /* increase the advertised connection window to 2G on
1785 * first update.
1786 */
1787 h2c->flags |= H2_CF_WINDOW_OPENED;
1788 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1789 }
1790
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001791 /* send WU for the connection */
1792 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1793 if (ret > 0)
1794 h2c->rcvd_c = 0;
1795
1796 return ret;
1797}
1798
1799/* try to send pending window update for the current dmux stream. It's safe to
1800 * call it with no pending updates. Returns > 0 on success or zero on missing
1801 * room or failure. It may return an error in h2c.
1802 */
1803static int h2c_send_strm_wu(struct h2c *h2c)
1804{
1805 int ret = 1;
1806
1807 if (h2c->rcvd_s <= 0)
1808 return 1;
1809
1810 /* send WU for the stream */
1811 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1812 if (ret > 0)
1813 h2c->rcvd_s = 0;
1814
1815 return ret;
1816}
1817
Willy Tarreaucf68c782017-10-10 17:11:41 +02001818/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1819 * success, 0 on missing data or one of the h2_status values.
1820 */
1821static int h2c_ack_ping(struct h2c *h2c)
1822{
1823 struct buffer *res;
1824 char str[17];
1825 int ret = -1;
1826
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001827 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001828 return 0;
1829
1830 if (h2c_mux_busy(h2c, NULL)) {
1831 h2c->flags |= H2_CF_DEM_MBUSY;
1832 return 0;
1833 }
1834
Willy Tarreaucf68c782017-10-10 17:11:41 +02001835 memcpy(str,
1836 "\x00\x00\x08" /* length : 8 (same payload) */
1837 "\x06" "\x01" /* type : 6, flags : ACK */
1838 "\x00\x00\x00\x00" /* stream ID */, 9);
1839
1840 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001841 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001842
Willy Tarreau9c218e72019-05-26 10:08:28 +02001843 res = br_tail(h2c->mbuf);
1844 retry:
1845 if (!h2_get_buf(h2c, res)) {
1846 h2c->flags |= H2_CF_MUX_MALLOC;
1847 h2c->flags |= H2_CF_DEM_MROOM;
1848 return 0;
1849 }
1850
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001851 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001852 if (unlikely(ret <= 0)) {
1853 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001854 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1855 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02001856 h2c->flags |= H2_CF_MUX_MFULL;
1857 h2c->flags |= H2_CF_DEM_MROOM;
1858 return 0;
1859 }
1860 else {
1861 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1862 return 0;
1863 }
1864 }
1865 return ret;
1866}
1867
Willy Tarreau26f95952017-07-27 17:18:30 +02001868/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1869 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001870 * h2c or h2s. The caller must have already verified frame length and stream ID
1871 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001872 */
1873static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1874{
1875 int32_t inc;
1876 int error;
1877
Willy Tarreau26f95952017-07-27 17:18:30 +02001878 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001879 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001880 return 0;
1881
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001882 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001883
1884 if (h2c->dsi != 0) {
1885 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001886
1887 /* it's not an error to receive WU on a closed stream */
1888 if (h2s->st == H2_SS_CLOSED)
1889 return 1;
1890
1891 if (!inc) {
1892 error = H2_ERR_PROTOCOL_ERROR;
1893 goto strm_err;
1894 }
1895
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001896 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001897 error = H2_ERR_FLOW_CONTROL_ERROR;
1898 goto strm_err;
1899 }
1900
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001901 h2s->sws += inc;
1902 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001903 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreaubda92dd2019-10-02 10:49:59 +02001904 LIST_DEL_INIT(&h2s->list);
1905 if (h2s->send_wait)
Olivier Houcharddddfe312018-10-10 18:51:00 +02001906 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001907 }
1908 }
1909 else {
1910 /* connection window update */
1911 if (!inc) {
1912 error = H2_ERR_PROTOCOL_ERROR;
1913 goto conn_err;
1914 }
1915
1916 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1917 error = H2_ERR_FLOW_CONTROL_ERROR;
1918 goto conn_err;
1919 }
1920
1921 h2c->mws += inc;
1922 }
1923
1924 return 1;
1925
1926 conn_err:
1927 h2c_error(h2c, error);
1928 return 0;
1929
1930 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001931 h2s_error(h2s, error);
1932 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001933 return 0;
1934}
1935
Willy Tarreaue96b0922017-10-30 00:28:29 +01001936/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001937 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1938 * have already verified frame length and stream ID validity. Described in
1939 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001940 */
1941static int h2c_handle_goaway(struct h2c *h2c)
1942{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001943 int last;
1944
Willy Tarreaue96b0922017-10-30 00:28:29 +01001945 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001946 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001947 return 0;
1948
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001949 last = h2_get_n32(&h2c->dbuf, 0);
1950 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001951 if (h2c->last_sid < 0)
1952 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02001953 h2_wake_some_streams(h2c, last);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001954 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001955}
1956
Willy Tarreau92153fc2017-12-03 19:46:19 +01001957/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001958 * invalid. Returns > 0 on success or zero on missing data. It may return an
1959 * error in h2c. The caller must have already verified frame length and stream
1960 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001961 */
1962static int h2c_handle_priority(struct h2c *h2c)
1963{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001964 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001965 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001966 return 0;
1967
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001968 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001969 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001970 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1971 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001972 }
1973 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001974}
1975
Willy Tarreaucd234e92017-08-18 10:59:39 +02001976/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001977 * Returns > 0 on success or zero on missing data. The caller must have already
1978 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001979 */
1980static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1981{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001982 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001983 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001984 return 0;
1985
1986 /* late RST, already handled */
1987 if (h2s->st == H2_SS_CLOSED)
1988 return 1;
1989
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001990 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001991 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001992
1993 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001994 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001995 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001996 }
1997
1998 h2s->flags |= H2_SF_RST_RCVD;
1999 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002000}
2001
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002002/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2003 * It may return an error in h2c or h2s. The caller must consider that the
2004 * return value is the new h2s in case one was allocated (most common case).
2005 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002006 * errors here are reported as connection errors since it's impossible to
2007 * recover from such errors after the compression context has been altered.
2008 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002009static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002010{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002011 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002012 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002013 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002014 int error;
2015
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002016 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002017 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02002018
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002019 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002020 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02002021
2022 /* now either the frame is complete or the buffer is complete */
2023 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002024 /* The stream exists/existed, this must be a trailers frame */
2025 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02002026 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
2027 /* unrecoverable error ? */
2028 if (h2c->st0 >= H2_CS_ERROR)
2029 goto out;
2030
2031 if (error == 0)
2032 goto out; // missing data
2033
2034 if (error < 0) {
2035 /* Failed to decode this frame (e.g. too large request)
2036 * but the HPACK decompressor is still synchronized.
2037 */
2038 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2039 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002040 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002041 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002042 goto done;
2043 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002044 /* the connection was already killed by an RST, let's consume
2045 * the data and send another RST.
2046 */
2047 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2048 h2s = (struct h2s*)h2_error_stream;
2049 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002050 }
2051 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2052 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2053 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002054 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002055 goto conn_err;
2056 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002057 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2058 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002059
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002060 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002061
Willy Tarreau25919232019-01-03 14:48:18 +01002062 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002063 if (h2c->st0 >= H2_CS_ERROR)
2064 goto out;
2065
Willy Tarreau25919232019-01-03 14:48:18 +01002066 if (error <= 0) {
2067 if (error == 0)
2068 goto out; // missing data
2069
2070 /* Failed to decode this stream (e.g. too large request)
2071 * but the HPACK decompressor is still synchronized.
2072 */
2073 h2s = (struct h2s*)h2_error_stream;
2074 goto send_rst;
2075 }
2076
Willy Tarreau22de8d32018-09-05 19:55:58 +02002077 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002078 * positively from h2c_frt_stream_new(), the stream will report the error,
2079 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002080 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02002081 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02002082 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002083 h2s = (struct h2s*)h2_refused_stream;
2084 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002085 }
2086
2087 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002088 h2s->rxbuf = rxbuf;
2089 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002090 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002091
Willy Tarreau88d138e2019-01-02 19:38:14 +01002092 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002093 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002094 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002095
2096 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002097 if (h2s->st == H2_SS_OPEN)
2098 h2s->st = H2_SS_HREM;
2099 else
2100 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002101 }
2102
Willy Tarreau3a429f02019-01-03 11:41:50 +01002103 /* update the max stream ID if the request is being processed */
2104 if (h2s->id > h2c->max_id)
2105 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002106
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002107 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002108
2109 conn_err:
2110 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002111 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002112
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002113 out:
2114 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002115 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002116
2117 send_rst:
2118 /* make the demux send an RST for the current stream. We may only
2119 * do this if we're certain that the HEADERS frame was properly
2120 * decompressed so that the HPACK decoder is still kept up to date.
2121 */
2122 h2_release_buf(h2c, &rxbuf);
2123 h2c->st0 = H2_CS_FRAME_E;
2124 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002125}
2126
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002127/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2128 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2129 * errors here are reported as connection errors since it's impossible to
2130 * recover from such errors after the compression context has been altered.
2131 */
2132static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2133{
Christopher Faulet96b88f22019-09-23 15:28:20 +02002134 struct buffer rxbuf = BUF_NULL;
2135 unsigned long long body_len = 0;
2136 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002137 int error;
2138
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002139 if (!b_size(&h2c->dbuf))
2140 return NULL; // empty buffer
2141
2142 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2143 return NULL; // incomplete frame
2144
Christopher Faulet96b88f22019-09-23 15:28:20 +02002145 if (h2s->st != H2_SS_CLOSED) {
2146 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
2147 }
2148 else {
2149 /* the connection was already killed by an RST, let's consume
2150 * the data and send another RST.
2151 */
2152 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Christopher Faulet03427052019-09-26 16:19:13 +02002153 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet96b88f22019-09-23 15:28:20 +02002154 h2c->st0 = H2_CS_FRAME_E;
2155 goto send_rst;
2156 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002157
Willy Tarreau25919232019-01-03 14:48:18 +01002158 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002159 if (h2c->st0 >= H2_CS_ERROR)
2160 return NULL;
2161
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002162 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2163 /* RFC7540#5.1 */
2164 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2165 h2c->st0 = H2_CS_FRAME_E;
2166 return NULL;
2167 }
2168
Willy Tarreau25919232019-01-03 14:48:18 +01002169 if (error <= 0) {
2170 if (error == 0)
2171 return NULL; // missing data
2172
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002173 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002174 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002175 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002176 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002177 }
2178
Christopher Fauletfa922f02019-05-07 10:55:17 +02002179 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002180 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002181
Willy Tarreau927b88b2019-03-04 08:03:25 +01002182 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002183 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002184 else if (h2s->flags & H2_SF_ES_RCVD) {
2185 if (h2s->st == H2_SS_OPEN)
2186 h2s->st = H2_SS_HREM;
2187 else if (h2s->st == H2_SS_HLOC)
2188 h2s_close(h2s);
2189 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002190
2191 return h2s;
Christopher Faulet96b88f22019-09-23 15:28:20 +02002192
2193 send_rst:
2194 /* make the demux send an RST for the current stream. We may only
2195 * do this if we're certain that the HEADERS frame was properly
2196 * decompressed so that the HPACK decoder is still kept up to date.
2197 */
2198 h2_release_buf(h2c, &rxbuf);
2199 h2c->st0 = H2_CS_FRAME_E;
2200 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002201}
2202
Willy Tarreau454f9052017-10-26 19:40:35 +02002203/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2204 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2205 */
2206static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2207{
2208 int error;
2209
2210 /* note that empty DATA frames are perfectly valid and sometimes used
2211 * to signal an end of stream (with the ES flag).
2212 */
2213
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002214 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002215 return 0; // empty buffer
2216
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002217 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002218 return 0; // incomplete frame
2219
2220 /* now either the frame is complete or the buffer is complete */
2221
Willy Tarreau454f9052017-10-26 19:40:35 +02002222 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2223 /* RFC7540#6.1 */
2224 error = H2_ERR_STREAM_CLOSED;
2225 goto strm_err;
2226 }
2227
Christopher Faulet4fb65f42019-06-19 09:25:58 +02002228 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002229 /* RFC7540#8.1.2 */
2230 error = H2_ERR_PROTOCOL_ERROR;
2231 goto strm_err;
2232 }
2233
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002234 if (!h2_frt_transfer_data(h2s))
2235 return 0;
2236
Willy Tarreau454f9052017-10-26 19:40:35 +02002237 /* call the upper layers to process the frame, then let the upper layer
2238 * notify the stream about any change.
2239 */
2240 if (!h2s->cs) {
Willy Tarreauc1a29652019-08-06 10:11:02 +02002241 /* The upper layer has already closed, this may happen on
2242 * 4xx/redirects during POST, or when receiving a response
2243 * from an H2 server after the client has aborted.
2244 */
2245 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002246 goto strm_err;
2247 }
2248
Willy Tarreau8f650c32017-11-21 19:36:21 +01002249 if (h2c->st0 >= H2_CS_ERROR)
2250 return 0;
2251
Willy Tarreau721c9742017-11-07 11:05:42 +01002252 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002253 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002254 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002255 }
2256
2257 /* check for completion : the callee will change this to FRAME_A or
2258 * FRAME_H once done.
2259 */
2260 if (h2c->st0 == H2_CS_FRAME_P)
2261 return 0;
2262
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002263 /* last frame */
2264 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002265 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002266 if (h2s->st == H2_SS_OPEN)
2267 h2s->st = H2_SS_HREM;
2268 else
2269 h2s_close(h2s);
2270
Willy Tarreau1915ca22019-01-24 11:49:37 +01002271 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2272 /* RFC7540#8.1.2 */
2273 error = H2_ERR_PROTOCOL_ERROR;
2274 goto strm_err;
2275 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002276 }
2277
Willy Tarreau454f9052017-10-26 19:40:35 +02002278 return 1;
2279
Willy Tarreau454f9052017-10-26 19:40:35 +02002280 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002281 h2s_error(h2s, error);
2282 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002283 return 0;
2284}
2285
Willy Tarreaubc933932017-10-09 16:21:43 +02002286/* process Rx frames to be demultiplexed */
2287static void h2_process_demux(struct h2c *h2c)
2288{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002289 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002290 struct h2_fh hdr;
2291 unsigned int padlen = 0;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002292 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002293
Willy Tarreau081d4722017-05-16 21:51:05 +02002294 if (h2c->st0 >= H2_CS_ERROR)
2295 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002296
2297 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2298 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002299 if (h2c->flags & H2_CF_IS_BACK)
2300 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002301 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2302 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002303 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002304 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002305 sess_log(h2c->conn->owner);
2306 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002307 goto fail;
2308 }
2309
2310 h2c->max_id = 0;
2311 h2c->st0 = H2_CS_SETTINGS1;
2312 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002313
2314 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002315 /* ensure that what is pending is a valid SETTINGS frame
2316 * without an ACK.
2317 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002318 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002319 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002320 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002321 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002322 if (!(h2c->flags & H2_CF_IS_BACK))
2323 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002324 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002325 goto fail;
2326 }
2327
2328 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2329 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2330 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2331 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002332 if (!(h2c->flags & H2_CF_IS_BACK))
2333 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002334 goto fail;
2335 }
2336
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002337 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002338 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2339 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2340 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002341 if (!(h2c->flags & H2_CF_IS_BACK))
2342 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002343 goto fail;
2344 }
2345
Willy Tarreau3bf69182018-12-21 15:34:50 +01002346 /* that's OK, switch to FRAME_P to process it. This is
2347 * a SETTINGS frame whose header has already been
2348 * deleted above.
2349 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002350 padlen = 0;
2351 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002352 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002353 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002354
2355 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002356 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002357 int ret = 0;
2358
2359 if (h2c->st0 >= H2_CS_ERROR)
2360 break;
2361
2362 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreauab3ebbc2019-08-06 15:49:51 +02002363 h2c->rcvd_s = 0;
2364
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002365 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002366 break;
2367
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002368 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002369 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002370 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02002371 /* only log if no other stream can report the error */
2372 sess_log(h2c->conn->owner);
2373 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002374 break;
2375 }
2376
Christopher Faulet3d574a52019-06-18 12:22:38 +02002377 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002378 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2379 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2380 * we read the pad length and drop it from the remaining
2381 * payload (one byte + the 9 remaining ones = 10 total
2382 * removed), so we have a frame payload starting after the
2383 * pad len. Flow controlled frames (DATA) also count the
2384 * padlen in the flow control, so it must be adjusted.
2385 */
2386 if (hdr.len < 1) {
2387 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002388 if (!(h2c->flags & H2_CF_IS_BACK))
2389 sess_log(h2c->conn->owner);
Willy Tarreau3bf69182018-12-21 15:34:50 +01002390 goto fail;
2391 }
2392 hdr.len--;
2393
2394 if (b_data(&h2c->dbuf) < 10)
2395 break; // missing padlen
2396
2397 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2398
2399 if (padlen > hdr.len) {
2400 /* RFC7540#6.1 : pad length = length of
2401 * frame payload or greater => error.
2402 */
2403 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002404 if (!(h2c->flags & H2_CF_IS_BACK))
2405 sess_log(h2c->conn->owner);
Willy Tarreau3bf69182018-12-21 15:34:50 +01002406 goto fail;
2407 }
2408
2409 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2410 h2c->rcvd_c++;
2411 h2c->rcvd_s++;
2412 }
2413 b_del(&h2c->dbuf, 1);
2414 }
2415 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002416
2417 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002418 h2c->dfl = hdr.len;
2419 h2c->dsi = hdr.sid;
2420 h2c->dft = hdr.ft;
2421 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002422 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002423 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002424
2425 /* check for minimum basic frame format validity */
2426 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2427 if (ret != H2_ERR_NO_ERROR) {
2428 h2c_error(h2c, ret);
Willy Tarreau21178a52019-10-23 11:06:35 +02002429 if (!(h2c->flags & H2_CF_IS_BACK))
2430 sess_log(h2c->conn->owner);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002431 goto fail;
2432 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002433 }
2434
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002435 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
2436 * H2_CS_FRAME_P indicates an incomplete previous operation
2437 * (most often the first attempt) and requires some validity
2438 * checks for the frame and the current state. The two other
2439 * ones are set after completion (or abortion) and must skip
2440 * validity checks.
2441 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002442 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2443
Willy Tarreau567beb82018-12-18 16:52:44 +01002444 if (tmp_h2s != h2s && h2s && h2s->cs &&
2445 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002446 conn_xprt_read0_pending(h2c->conn) ||
2447 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002448 (h2s->flags & H2_SF_ES_RCVD) ||
2449 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002450 /* we may have to signal the upper layers */
2451 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002452 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002453 }
2454 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002455
Willy Tarreaud7901432017-12-29 11:34:40 +01002456 if (h2c->st0 == H2_CS_FRAME_E)
2457 goto strm_err;
2458
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002459 if (h2c->st0 == H2_CS_FRAME_A)
2460 goto valid_frame_type;
2461
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002462 if (h2s->st == H2_SS_IDLE &&
2463 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2464 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2465 * this state MUST be treated as a connection error
2466 */
2467 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002468 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02002469 /* only log if no other stream can report the error */
2470 sess_log(h2c->conn->owner);
2471 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002472 break;
2473 }
2474
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002475 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2476 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2477 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002478 * this state MUST be treated as a stream error.
2479 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2480 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002481 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002482 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2483 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2484 else
2485 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002486 goto strm_err;
2487 }
2488
Willy Tarreauab837502017-12-27 15:07:30 +01002489 /* Below the management of frames received in closed state is a
2490 * bit hackish because the spec makes strong differences between
2491 * streams closed by receiving RST, sending RST, and seeing ES
2492 * in both directions. In addition to this, the creation of a
2493 * new stream reusing the identifier of a closed one will be
2494 * detected here. Given that we cannot keep track of all closed
2495 * streams forever, we consider that unknown closed streams were
2496 * closed on RST received, which allows us to respond with an
2497 * RST without breaking the connection (eg: to abort a transfer).
2498 * Some frames have to be silently ignored as well.
2499 */
2500 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002501 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002502 /* #5.1.1: The identifier of a newly
2503 * established stream MUST be numerically
2504 * greater than all streams that the initiating
2505 * endpoint has opened or reserved. This
2506 * governs streams that are opened using a
2507 * HEADERS frame and streams that are reserved
2508 * using PUSH_PROMISE. An endpoint that
2509 * receives an unexpected stream identifier
2510 * MUST respond with a connection error.
2511 */
2512 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2513 goto strm_err;
2514 }
2515
Willy Tarreaueb5be9d2019-09-26 08:47:15 +02002516 if (h2s->flags & H2_SF_RST_RCVD &&
2517 !(h2_ft_bit(h2c->dft) & (H2_FT_HDR_MASK | H2_FT_RST_STREAM_BIT | H2_FT_PRIORITY_BIT | H2_FT_WINDOW_UPDATE_BIT))) {
Willy Tarreauab837502017-12-27 15:07:30 +01002518 /* RFC7540#5.1:closed: an endpoint that
2519 * receives any frame other than PRIORITY after
2520 * receiving a RST_STREAM MUST treat that as a
2521 * stream error of type STREAM_CLOSED.
2522 *
2523 * Note that old streams fall into this category
2524 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002525 *
2526 * However, we cannot generalize this to all frame types. Those
2527 * carrying compression state must still be processed before
2528 * being dropped or we'll desynchronize the decoder. This can
2529 * happen with request trailers received after sending an
2530 * RST_STREAM, or with header/trailers responses received after
2531 * sending RST_STREAM (aborted stream).
Willy Tarreaueb5be9d2019-09-26 08:47:15 +02002532 *
2533 * In addition, since our CLOSED streams always carry the
2534 * RST_RCVD bit, we don't want to accidently catch valid
2535 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreauab837502017-12-27 15:07:30 +01002536 */
2537 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2538 h2c->st0 = H2_CS_FRAME_E;
2539 goto strm_err;
2540 }
2541
2542 /* RFC7540#5.1:closed: if this state is reached as a
2543 * result of sending a RST_STREAM frame, the peer that
2544 * receives the RST_STREAM might have already sent
2545 * frames on the stream that cannot be withdrawn. An
2546 * endpoint MUST ignore frames that it receives on
2547 * closed streams after it has sent a RST_STREAM
2548 * frame. An endpoint MAY choose to limit the period
2549 * over which it ignores frames and treat frames that
2550 * arrive after this time as being in error.
2551 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002552 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002553 /* RFC7540#5.1:closed: any frame other than
2554 * PRIO/WU/RST in this state MUST be treated as
2555 * a connection error
2556 */
2557 if (h2c->dft != H2_FT_RST_STREAM &&
2558 h2c->dft != H2_FT_PRIORITY &&
2559 h2c->dft != H2_FT_WINDOW_UPDATE) {
2560 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2561 goto strm_err;
2562 }
2563 }
2564 }
2565
Willy Tarreauc0da1962017-10-30 18:38:00 +01002566#if 0
2567 // problem below: it is not possible to completely ignore such
2568 // streams as we need to maintain the compression state as well
2569 // and for this we need to completely process these frames (eg:
2570 // HEADERS frames) as well as counting DATA frames to emit
2571 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2572 // This is a typical case of layer violation where the
2573 // transported contents are critical to the connection's
2574 // validity and must be ignored at the same time :-(
2575
2576 /* graceful shutdown, ignore streams whose ID is higher than
2577 * the one advertised in GOAWAY. RFC7540#6.8.
2578 */
2579 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002580 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2581 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002582 h2c->dfl -= ret;
2583 ret = h2c->dfl == 0;
2584 goto strm_err;
2585 }
2586#endif
2587
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002588 valid_frame_type:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002589 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002590 case H2_FT_SETTINGS:
2591 if (h2c->st0 == H2_CS_FRAME_P)
2592 ret = h2c_handle_settings(h2c);
2593
2594 if (h2c->st0 == H2_CS_FRAME_A)
2595 ret = h2c_ack_settings(h2c);
2596 break;
2597
Willy Tarreaucf68c782017-10-10 17:11:41 +02002598 case H2_FT_PING:
2599 if (h2c->st0 == H2_CS_FRAME_P)
2600 ret = h2c_handle_ping(h2c);
2601
2602 if (h2c->st0 == H2_CS_FRAME_A)
2603 ret = h2c_ack_ping(h2c);
2604 break;
2605
Willy Tarreau26f95952017-07-27 17:18:30 +02002606 case H2_FT_WINDOW_UPDATE:
2607 if (h2c->st0 == H2_CS_FRAME_P)
2608 ret = h2c_handle_window_update(h2c, h2s);
2609 break;
2610
Willy Tarreau61290ec2017-10-17 08:19:21 +02002611 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002612 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2613 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2614 * frames' parsers consume all following CONTINUATION
2615 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002616 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002617 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002618 if (!(h2c->flags & H2_CF_IS_BACK))
2619 sess_log(h2c->conn->owner);
Willy Tarreauea18f862018-12-22 20:19:26 +01002620 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002621
Willy Tarreau13278b42017-10-13 19:23:14 +02002622 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002623 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002624 if (h2c->flags & H2_CF_IS_BACK)
2625 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2626 else
2627 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002628 if (tmp_h2s) {
2629 h2s = tmp_h2s;
2630 ret = 1;
2631 }
2632 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002633 break;
2634
Willy Tarreau454f9052017-10-26 19:40:35 +02002635 case H2_FT_DATA:
2636 if (h2c->st0 == H2_CS_FRAME_P)
2637 ret = h2c_frt_handle_data(h2c, h2s);
2638
2639 if (h2c->st0 == H2_CS_FRAME_A)
2640 ret = h2c_send_strm_wu(h2c);
2641 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002642
Willy Tarreau92153fc2017-12-03 19:46:19 +01002643 case H2_FT_PRIORITY:
2644 if (h2c->st0 == H2_CS_FRAME_P)
2645 ret = h2c_handle_priority(h2c);
2646 break;
2647
Willy Tarreaucd234e92017-08-18 10:59:39 +02002648 case H2_FT_RST_STREAM:
2649 if (h2c->st0 == H2_CS_FRAME_P)
2650 ret = h2c_handle_rst_stream(h2c, h2s);
2651 break;
2652
Willy Tarreaue96b0922017-10-30 00:28:29 +01002653 case H2_FT_GOAWAY:
2654 if (h2c->st0 == H2_CS_FRAME_P)
2655 ret = h2c_handle_goaway(h2c);
2656 break;
2657
Willy Tarreau1c661982017-10-30 13:52:01 +01002658 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002659 default:
2660 /* drop frames that we ignore. They may be larger than
2661 * the buffer so we drain all of their contents until
2662 * we reach the end.
2663 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002664 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2665 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002666 h2c->dfl -= ret;
2667 ret = h2c->dfl == 0;
2668 }
2669
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002670 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002671 /* We may have to send an RST if not done yet */
2672 if (h2s->st == H2_SS_ERROR)
2673 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002674
Willy Tarreaua20a5192017-12-27 11:02:06 +01002675 if (h2c->st0 == H2_CS_FRAME_E)
2676 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002677
Willy Tarreau7e98c052017-10-10 15:56:59 +02002678 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002679 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002680 break;
2681
2682 if (h2c->st0 != H2_CS_FRAME_H) {
Christopher Faulete12a26f2019-09-26 16:38:28 +02002683 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2684 b_del(&h2c->dbuf, ret);
2685 h2c->dfl -= ret;
2686 if (!h2c->dfl)
2687 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002688 }
2689 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002690
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002691 if (h2c->rcvd_c > 0 &&
2692 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2693 h2c_send_conn_wu(h2c);
2694
Willy Tarreau52eed752017-09-22 15:05:09 +02002695 fail:
2696 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002697 if (h2s && h2s->cs &&
2698 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002699 conn_xprt_read0_pending(h2c->conn) ||
2700 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002701 (h2s->flags & H2_SF_ES_RCVD) ||
2702 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002703 /* we may have to signal the upper layers */
2704 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002705 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002706 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002707
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002708 if (old_iw != h2c->miw)
2709 h2c_unblock_sfctl(h2c);
2710
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002711 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002712}
2713
2714/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2715 * the end.
2716 */
2717static int h2_process_mux(struct h2c *h2c)
2718{
Olivier Houchard998410a2019-04-15 19:23:37 +02002719 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002720
Willy Tarreau01b44822018-10-03 14:26:37 +02002721 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2722 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2723 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2724 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau21178a52019-10-23 11:06:35 +02002725 if (h2c->st0 == H2_CS_ERROR)
Willy Tarreau01b44822018-10-03 14:26:37 +02002726 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau01b44822018-10-03 14:26:37 +02002727 goto fail;
2728 }
2729 h2c->st0 = H2_CS_SETTINGS1;
2730 }
2731 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002732 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002733 return 1;
2734 }
2735
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002736 /* start by sending possibly pending window updates */
Willy Tarreau9c497c22019-08-06 15:39:32 +02002737 if (h2c->rcvd_s > 0 &&
2738 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2739 h2c_send_strm_wu(h2c) < 0)
2740 goto fail;
2741
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002742 if (h2c->rcvd_c > 0 &&
2743 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2744 h2c_send_conn_wu(h2c) < 0)
2745 goto fail;
2746
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002747 /* First we always process the flow control list because the streams
2748 * waiting there were already elected for immediate emission but were
2749 * blocked just on this.
2750 */
2751
Olivier Houchard998410a2019-04-15 19:23:37 +02002752 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002753 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2754 h2c->st0 >= H2_CS_ERROR)
2755 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002756
Willy Tarreauc234ae32019-05-13 17:56:11 +02002757 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002758 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002759
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002760 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002761 /* For some reason, the upper layer failed to subsribe again,
2762 * so remove it from the send_list
2763 */
2764 if (!h2s->send_wait) {
2765 LIST_DEL_INIT(&h2s->list);
2766 continue;
2767 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002768 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002769 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002770 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002771 }
2772
Olivier Houchard998410a2019-04-15 19:23:37 +02002773 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002774 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2775 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002776
Willy Tarreauc234ae32019-05-13 17:56:11 +02002777 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002778 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002779
Olivier Houchard998410a2019-04-15 19:23:37 +02002780 /* For some reason, the upper layer failed to subsribe again,
2781 * so remove it from the send_list
2782 */
2783 if (!h2s->send_wait) {
2784 LIST_DEL_INIT(&h2s->list);
2785 continue;
2786 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002787 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002788 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002789 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002790 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002791 }
2792
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002793 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002794 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002795 if (h2c->st0 == H2_CS_ERROR) {
2796 if (h2c->max_id >= 0) {
2797 h2c_send_goaway_error(h2c, NULL);
2798 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2799 return 0;
2800 }
2801
2802 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2803 }
2804 return 1;
2805 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002806 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002807}
2808
Willy Tarreau62f52692017-10-08 23:01:42 +02002809
Willy Tarreau479998a2018-11-18 06:30:59 +01002810/* Attempt to read data, and subscribe if none available.
2811 * The function returns 1 if data has been received, otherwise zero.
2812 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002813static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002814{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002815 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002816 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002817 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002818 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002819
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002820 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002821 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002822
Willy Tarreau315d8072017-12-10 22:17:57 +01002823 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002824 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002825
Willy Tarreau44e973f2018-03-01 17:49:30 +01002826 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002827 if (!buf) {
2828 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002829 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002830 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002831
Olivier Houchard7505f942018-08-21 18:10:44 +02002832 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002833 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002834 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2835 /* HTX in use : try to pre-align the buffer like the
2836 * rxbufs will be to optimize memory copies. We'll make
2837 * sure that the frame header lands at the end of the
2838 * HTX block to alias it upon recv. We cannot use the
2839 * head because rcv_buf() will realign the buffer if
2840 * it's empty. Thus we cheat and pretend we already
2841 * have a few bytes there.
2842 */
2843 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002844 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002845 }
2846 else
2847 max = b_room(buf);
2848
Olivier Houchard7505f942018-08-21 18:10:44 +02002849 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002850 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002851 else
2852 ret = 0;
2853 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002854
Willy Tarreaua8fcdac2019-08-02 07:48:47 +02002855 if (max && !ret && h2_recv_allowed(h2c))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002856 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002857
Olivier Houcharda1411e62018-08-17 18:42:48 +02002858 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002859 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002860 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002861 }
2862
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002863 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002864 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002865 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002866}
2867
Willy Tarreau479998a2018-11-18 06:30:59 +01002868/* Try to send data if possible.
2869 * The function returns 1 if data have been sent, otherwise zero.
2870 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002871static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002872{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002873 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002874 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002875 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002876
2877 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002878 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002879
Olivier Houchard7505f942018-08-21 18:10:44 +02002880
Willy Tarreaua2af5122017-10-09 11:56:46 +02002881 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2882 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002883 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002884 }
2885
Willy Tarreaubc933932017-10-09 16:21:43 +02002886 /* This loop is quite simple : it tries to fill as much as it can from
2887 * pending streams into the existing buffer until it's reportedly full
2888 * or the end of send requests is reached. Then it tries to send this
2889 * buffer's contents out, marks it not full if at least one byte could
2890 * be sent, and tries again.
2891 *
2892 * The snd_buf() function normally takes a "flags" argument which may
2893 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2894 * data immediately comes and CO_SFL_STREAMER to indicate that the
2895 * connection is streaming lots of data (used to increase TLS record
2896 * size at the expense of latency). The former can be sent any time
2897 * there's a buffer full flag, as it indicates at least one stream
2898 * attempted to send and failed so there are pending data. An
2899 * alternative would be to set it as long as there's an active stream
2900 * but that would be problematic for ACKs until we have an absolute
2901 * guarantee that all waiters have at least one byte to send. The
2902 * latter should possibly not be set for now.
2903 */
2904
2905 done = 0;
2906 while (!done) {
2907 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002908 unsigned int released = 0;
2909 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02002910
2911 /* fill as much as we can into the current buffer */
2912 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2913 done = h2_process_mux(h2c);
2914
Olivier Houchard2b094432019-01-29 18:28:36 +01002915 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002916 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01002917
Willy Tarreaubc933932017-10-09 16:21:43 +02002918 if (conn->flags & CO_FL_ERROR)
2919 break;
2920
2921 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2922 flags |= CO_SFL_MSG_MORE;
2923
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002924 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
2925 if (b_data(buf)) {
2926 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002927 if (!ret) {
2928 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002929 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002930 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002931 sent = 1;
2932 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002933 if (b_data(buf)) {
2934 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002935 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002936 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002937 }
2938 b_free(buf);
2939 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02002940 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002941
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002942 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02002943 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002944
Willy Tarreaubc933932017-10-09 16:21:43 +02002945 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet07423082019-10-24 10:31:01 +02002946 if (sent)
2947 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02002948 }
2949
Willy Tarreaua2af5122017-10-09 11:56:46 +02002950 if (conn->flags & CO_FL_SOCK_WR_SH) {
2951 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02002952 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002953 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002954 /* We're not full anymore, so we can wake any task that are waiting
2955 * for us.
2956 */
Willy Tarreauc51d47c2019-09-25 07:57:31 +02002957 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002958 struct h2s *h2s;
2959
2960 list_for_each_entry(h2s, &h2c->send_list, list) {
2961 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2962 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002963
Willy Tarreauc234ae32019-05-13 17:56:11 +02002964 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002965 continue;
2966
Olivier Houchard998410a2019-04-15 19:23:37 +02002967 /* For some reason, the upper layer failed to subsribe again,
2968 * so remove it from the send_list
2969 */
2970 if (!h2s->send_wait) {
2971 LIST_DEL_INIT(&h2s->list);
2972 continue;
2973 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002974 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002975 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002976 tasklet_wakeup(h2s->send_wait->tasklet);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002977 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002978 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002979 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002980 /* We're done, no more to send */
Willy Tarreau662fafc2019-05-26 09:43:07 +02002981 if (!br_data(h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002982 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002983schedule:
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002984 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002985 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002986
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002987 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002988}
2989
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002990/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002991static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2992{
2993 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002994 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002995
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002996 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002997 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002998 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002999 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01003000 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02003001 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003002 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003003}
Willy Tarreaua2af5122017-10-09 11:56:46 +02003004
Willy Tarreau62f52692017-10-08 23:01:42 +02003005/* callback called on any event by the connection handler.
3006 * It applies changes and returns zero, or < 0 if it wants immediate
3007 * destruction of the connection (which normally doesn not happen in h2).
3008 */
Olivier Houchard7505f942018-08-21 18:10:44 +02003009static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003010{
Olivier Houchard7505f942018-08-21 18:10:44 +02003011 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003012
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003013 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01003014 h2_process_demux(h2c);
3015
3016 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003017 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003018
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003019 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01003020 h2c->flags &= ~H2_CF_DEM_DFULL;
3021 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003022 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003023
Willy Tarreau0b37d652018-10-03 10:33:02 +02003024 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01003025 /* frontend is stopping, reload likely in progress, let's try
3026 * to announce a graceful shutdown if not yet done. We don't
3027 * care if it fails, it will be tried again later.
3028 */
3029 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3030 if (h2c->last_sid < 0)
3031 h2c->last_sid = (1U << 31) - 1;
3032 h2c_send_goaway_error(h2c, NULL);
3033 }
3034 }
3035
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003036 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003037 * If we received early data, and the handshake is done, wake
3038 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003039 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003040 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
3041 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
3042 struct eb32_node *node;
3043 struct h2s *h2s;
3044
3045 h2c->flags |= H2_CF_WAIT_FOR_HS;
3046 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
3047
3048 while (node) {
3049 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01003050 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01003051 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003052 node = eb32_next(node);
3053 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003054 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003055
Willy Tarreau26bd7612017-10-09 16:47:04 +02003056 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01003057 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
3058 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
3059 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02003060 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003061
3062 if (eb_is_empty(&h2c->streams_by_id)) {
3063 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003064 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003065 return -1;
3066 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003067 }
3068
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003069 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003070 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003071
Olivier Houchard53216e72018-10-10 15:46:36 +02003072 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3073 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3074 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003075 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003076 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3077 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003078 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003079
Willy Tarreau3f133572017-10-31 19:21:06 +01003080 if (h2c->task) {
Willy Tarreau534d8f92019-10-01 10:12:00 +02003081 if (h2c_may_expire(h2c))
3082 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3083 else
3084 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003085 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003086 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003087
Olivier Houchard7505f942018-08-21 18:10:44 +02003088 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02003089 return 0;
3090}
3091
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003092/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003093static int h2_wake(struct connection *conn)
3094{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003095 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003096
3097 return (h2_process(h2c));
3098}
3099
Willy Tarreauea392822017-10-31 10:02:25 +01003100/* Connection timeout management. The principle is that if there's no receipt
3101 * nor sending for a certain amount of time, the connection is closed. If the
3102 * MUX buffer still has lying data or is not allocatable, the connection is
3103 * immediately killed. If it's allocatable and empty, we attempt to send a
3104 * GOAWAY frame.
3105 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003106static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003107{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003108 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003109 int expired = tick_is_expired(t->expire, now_ms);
3110
Willy Tarreau0975f112018-03-29 15:22:59 +02003111 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01003112 return t;
3113
Willy Tarreau534d8f92019-10-01 10:12:00 +02003114 if (h2c && !h2c_may_expire(h2c)) {
3115 /* we do still have streams but all of them are idle, waiting
3116 * for the data layer, so we must not enforce the timeout here.
3117 */
3118 t->expire = TICK_ETERNITY;
3119 return t;
3120 }
3121
Olivier Houchard3f795f72019-04-17 22:51:06 +02003122 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003123
3124 if (!h2c) {
3125 /* resources were already deleted */
3126 return NULL;
3127 }
3128
3129 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003130 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003131 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003132
Willy Tarreau662fafc2019-05-26 09:43:07 +02003133 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003134 /* don't even try to send a GOAWAY, the buffer is stuck */
3135 h2c->flags |= H2_CF_GOAWAY_FAILED;
3136 }
3137
3138 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003139 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003140 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3141 h2c->flags |= H2_CF_GOAWAY_FAILED;
3142
Willy Tarreau662fafc2019-05-26 09:43:07 +02003143 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003144 unsigned int released = 0;
3145 struct buffer *buf;
3146
3147 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3148 if (b_data(buf)) {
3149 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3150 if (!ret)
3151 break;
3152 b_del(buf, ret);
3153 if (b_data(buf))
3154 break;
3155 b_free(buf);
3156 released++;
3157 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003158 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003159
3160 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003161 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003162 }
Willy Tarreauea392822017-10-31 10:02:25 +01003163
Willy Tarreau0975f112018-03-29 15:22:59 +02003164 /* either we can release everything now or it will be done later once
3165 * the last stream closes.
3166 */
3167 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003168 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003169
Willy Tarreauea392822017-10-31 10:02:25 +01003170 return NULL;
3171}
3172
3173
Willy Tarreau62f52692017-10-08 23:01:42 +02003174/*******************************************/
3175/* functions below are used by the streams */
3176/*******************************************/
3177
3178/*
3179 * Attach a new stream to a connection
3180 * (Used for outgoing connections)
3181 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003182static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003183{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003184 struct conn_stream *cs;
3185 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003186 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003187
3188 cs = cs_new(conn);
3189 if (!cs)
3190 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003191 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003192 if (!h2s) {
3193 cs_free(cs);
3194 return NULL;
3195 }
3196 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003197}
3198
Willy Tarreaufafd3982018-11-18 21:29:20 +01003199/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3200 * We have to scan because we may have some orphan streams. It might be
3201 * beneficial to scan backwards from the end to reduce the likeliness to find
3202 * orphans.
3203 */
3204static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3205{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003206 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003207 struct h2s *h2s;
3208 struct eb32_node *node;
3209
3210 node = eb32_first(&h2c->streams_by_id);
3211 while (node) {
3212 h2s = container_of(node, struct h2s, by_id);
3213 if (h2s->cs)
3214 return h2s->cs;
3215 node = eb32_next(node);
3216 }
3217 return NULL;
3218}
3219
Willy Tarreau62f52692017-10-08 23:01:42 +02003220/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003221 * Destroy the mux and the associated connection, if it is no longer used
3222 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003223static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003224{
Christopher Faulet73c12072019-04-08 11:23:22 +02003225 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003226
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003227 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003228 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003229}
3230
3231/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003232 * Detach the stream from the connection and possibly release the connection.
3233 */
3234static void h2_detach(struct conn_stream *cs)
3235{
Willy Tarreau60935142017-10-16 18:11:19 +02003236 struct h2s *h2s = cs->ctx;
3237 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003238 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003239
3240 cs->ctx = NULL;
3241 if (!h2s)
3242 return;
3243
Olivier Houchard998410a2019-04-15 19:23:37 +02003244 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003245 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003246 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003247 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003248 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003249 /*
3250 * At this point, the stream_interface is supposed to have called
3251 * h2_unsubscribe(), so the only way there's still a
3252 * subscription that came from the stream_interface (as we
3253 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3254 * without the stream_interface involved) is that we subscribed
3255 * for sending, we woke the tasklet up and removed the
3256 * SUB_RETRY_SEND flag, so the stream_interface would not
3257 * know it has to unsubscribe for send, but the tasklet hasn't
3258 * run yet. Make sure to handle that by explicitely setting
3259 * send_wait to NULL, as nothing else will do it for us.
3260 */
3261 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003262 }
3263
Olivier Houchardf502aca2018-12-14 19:42:40 +01003264 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003265 h2c = h2s->h2c;
3266 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003267 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003268 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3269 !h2_frt_has_too_many_cs(h2c)) {
3270 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003271 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003272 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003273 }
Willy Tarreau60935142017-10-16 18:11:19 +02003274
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003275 /* this stream may be blocked waiting for some data to leave (possibly
3276 * an ES or RST frame), so orphan it in this case.
3277 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003278 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003279 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003280 (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 +01003281 return;
3282
Willy Tarreau45f752e2017-10-30 15:44:59 +01003283 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3284 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3285 /* unblock the connection if it was blocked on this
3286 * stream.
3287 */
3288 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3289 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003290 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003291 }
3292
Willy Tarreau71049cc2018-03-28 13:56:39 +02003293 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003294
Olivier Houchard8a786902018-12-15 16:05:40 +01003295 if (h2c->flags & H2_CF_IS_BACK &&
3296 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003297 if (!(h2c->conn->flags &
3298 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3299 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003300 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003301 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3302 h2c->conn->owner = NULL;
3303 if (eb_is_empty(&h2c->streams_by_id)) {
3304 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3305 /* The server doesn't want it, let's kill the connection right away */
3306 h2c->conn->mux->destroy(h2c->conn);
3307 return;
3308 }
3309 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003310 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003311 if (eb_is_empty(&h2c->streams_by_id)) {
3312 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3313 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3314 return;
3315 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003316 /* Never ever allow to reuse a connection from a non-reuse backend */
3317 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3318 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003319 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003320 struct server *srv = objt_server(h2c->conn->target);
3321
3322 if (srv) {
3323 if (h2c->conn->flags & CO_FL_PRIVATE)
3324 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3325 else
3326 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3327 }
3328
3329 }
3330 }
3331 }
3332
Willy Tarreaue323f342018-03-28 13:51:45 +02003333 /* We don't want to close right now unless we're removing the
3334 * last stream, and either the connection is in error, or it
3335 * reached the ID already specified in a GOAWAY frame received
3336 * or sent (as seen by last_sid >= 0).
3337 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003338 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003339 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003340 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003341 }
3342 else if (h2c->task) {
Willy Tarreau534d8f92019-10-01 10:12:00 +02003343 if (h2c_may_expire(h2c))
3344 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3345 else
3346 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003347 task_queue(h2c->task);
Willy Tarreau60935142017-10-16 18:11:19 +02003348 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003349}
3350
Willy Tarreau88bdba32019-05-13 18:17:53 +02003351/* Performs a synchronous or asynchronous shutr(). */
3352static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003353{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003354 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003355 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003356
Willy Tarreauf983d002019-05-14 10:40:21 +02003357 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003358 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003359
Willy Tarreau18059042019-01-31 19:12:48 +01003360 /* a connstream may require us to immediately kill the whole connection
3361 * for example because of a "tcp-request content reject" rule that is
3362 * normally used to limit abuse. In this case we schedule a goaway to
3363 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003364 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003365 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003366 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3367 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3368 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3369 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003370 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3371 /* Nothing was never sent for this stream, so reset with
3372 * REFUSED_STREAM error to let the client retry the
3373 * request.
3374 */
3375 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3376 }
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003377 else {
3378 /* a final response was already provided, we don't want this
3379 * stream anymore. This may happen when the server responds
3380 * before the end of an upload and closes quickly (redirect,
3381 * deny, ...)
3382 */
3383 h2s_error(h2s, H2_ERR_CANCEL);
3384 }
Willy Tarreau18059042019-01-31 19:12:48 +01003385
Willy Tarreau90c32322017-11-24 08:00:30 +01003386 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003387 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003388 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003389
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003390 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003391 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003392 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003393 done:
3394 h2s->flags &= ~H2_SF_WANT_SHUTR;
3395 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003396add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003397 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003398 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003399 if (h2s->flags & H2_SF_BLK_MFCTL) {
3400 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3401 h2s->send_wait = sw;
3402 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3403 h2s->send_wait = sw;
3404 LIST_ADDQ(&h2c->send_list, &h2s->list);
3405 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003406 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003407 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003408 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003409 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003410}
3411
Willy Tarreau88bdba32019-05-13 18:17:53 +02003412/* Performs a synchronous or asynchronous shutw(). */
3413static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003414{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003415 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003416 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003417
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003418 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003419 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003420
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003421 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003422 /* we can cleanly close using an empty data frame only after headers */
3423
3424 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3425 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003426 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003427
3428 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003429 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003430 else
3431 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003432 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003433 /* a connstream may require us to immediately kill the whole connection
3434 * for example because of a "tcp-request content reject" rule that is
3435 * normally used to limit abuse. In this case we schedule a goaway to
3436 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003437 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003438 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003439 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3440 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3441 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3442 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003443 else {
3444 /* Nothing was never sent for this stream, so reset with
3445 * REFUSED_STREAM error to let the client retry the
3446 * request.
3447 */
3448 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3449 }
Willy Tarreau18059042019-01-31 19:12:48 +01003450
Willy Tarreau90c32322017-11-24 08:00:30 +01003451 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003452 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003453 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003454
Willy Tarreau00dd0782018-03-01 16:31:34 +01003455 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003456 }
3457
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003458 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003459 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003460 done:
3461 h2s->flags &= ~H2_SF_WANT_SHUTW;
3462 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003463
3464 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003465 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003466 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003467 if (h2s->flags & H2_SF_BLK_MFCTL) {
3468 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3469 h2s->send_wait = sw;
3470 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3471 h2s->send_wait = sw;
3472 LIST_ADDQ(&h2c->send_list, &h2s->list);
3473 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003474 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003475 /* let the handler know we want to shutw */
3476 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003477 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003478}
3479
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003480/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003481 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3482 * and prevented the last frame from being emitted.
3483 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003484static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3485{
3486 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003487 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003488
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003489 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003490 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003491 h2_do_shutw(h2s);
3492
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003493 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003494 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003495
Willy Tarreau88bdba32019-05-13 18:17:53 +02003496 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003497 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003498 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003499
Willy Tarreau88bdba32019-05-13 18:17:53 +02003500 if (!h2s->cs) {
3501 h2s_destroy(h2s);
3502 if (h2c_is_dead(h2c))
3503 h2_release(h2c);
3504 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003505 }
3506
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003507 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003508}
3509
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003510/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003511static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3512{
3513 struct h2s *h2s = cs->ctx;
3514
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003515 if (cs->flags & CS_FL_KILL_CONN)
3516 h2s->flags |= H2_SF_KILL_CONN;
3517
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003518 if (!mode)
3519 return;
3520
3521 h2_do_shutr(h2s);
3522}
3523
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003524/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003525static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3526{
3527 struct h2s *h2s = cs->ctx;
3528
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003529 if (cs->flags & CS_FL_KILL_CONN)
3530 h2s->flags |= H2_SF_KILL_CONN;
3531
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003532 h2_do_shutw(h2s);
3533}
3534
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003535/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003536 * HTX request or response depending on the connection's side. Returns a
3537 * positive value on success, a negative value on failure, or 0 if it couldn't
3538 * proceed. May report connection errors in h2c->errcode if the frame is
3539 * non-decodable and the connection unrecoverable. In absence of connection
3540 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003541 *
3542 * The function may fold CONTINUATION frames into the initial HEADERS frame
3543 * by removing padding and next frame header, then moving the CONTINUATION
3544 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3545 * leaving a hole between the main frame and the beginning of the next one.
3546 * The possibly remaining incomplete or next frame at the end may be moved
3547 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3548 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3549 *
3550 * A buffer at the beginning of processing may look like this :
3551 *
3552 * ,---.---------.-----.--------------.--------------.------.---.
3553 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3554 * `---^---------^-----^--------------^--------------^------^---'
3555 * | | <-----> | |
3556 * area | dpl | wrap
3557 * |<--------------> |
3558 * | dfl |
3559 * |<-------------------------------------------------->|
3560 * head data
3561 *
3562 * Padding is automatically overwritten when folding, participating to the
3563 * hole size after dfl :
3564 *
3565 * ,---.------------------------.-----.--------------.------.---.
3566 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3567 * `---^------------------------^-----^--------------^------^---'
3568 * | | <-----> | |
3569 * area | hole | wrap
3570 * |<-----------------------> |
3571 * | dfl |
3572 * |<-------------------------------------------------->|
3573 * head data
3574 *
3575 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3576 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3577 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003578 *
3579 * The <flags> field must point to either the stream's flags or to a copy of it
3580 * so that the function can update the following flags :
3581 * - H2_SF_DATA_CLEN when content-length is seen
3582 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3583 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003584 *
3585 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3586 * decoding, in order to detect if we're dealing with a headers or a trailers
3587 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003588 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003589static 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 +02003590{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003591 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003592 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003593 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003594 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003595 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003596 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003597 int flen; // header frame len
3598 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003599 int ret = 0;
3600 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003601 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003602 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003603
Willy Tarreauea18f862018-12-22 20:19:26 +01003604next_frame:
3605 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3606 goto leave; // incomplete input frame
3607
3608 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3609 * this case, we'll try to paste it immediately after the initial
3610 * HEADERS frame payload and kill any possible padding. The initial
3611 * frame's length will be increased to represent the concatenation
3612 * of the two frames. The next frame is read from position <tlen>
3613 * and written at position <flen> (minus padding if some is present).
3614 */
3615 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3616 struct h2_fh hdr;
3617 int clen; // CONTINUATION frame's payload length
3618
3619 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3620 /* no more data, the buffer may be full, either due to
3621 * too large a frame or because of too large a hole that
3622 * we're going to compact at the end.
3623 */
3624 goto leave;
3625 }
3626
3627 if (hdr.ft != H2_FT_CONTINUATION) {
3628 /* RFC7540#6.10: frame of unexpected type */
3629 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3630 goto fail;
3631 }
3632
3633 if (hdr.sid != h2c->dsi) {
3634 /* RFC7540#6.10: frame of different stream */
3635 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3636 goto fail;
3637 }
3638
3639 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3640 /* RFC7540#4.2: invalid frame length */
3641 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3642 goto fail;
3643 }
3644
3645 /* detect when we must stop aggragating frames */
3646 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3647
3648 /* Take as much as we can of the CONTINUATION frame's payload */
3649 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3650 if (clen > hdr.len)
3651 clen = hdr.len;
3652
3653 /* Move the frame's payload over the padding, hole and frame
3654 * header. At least one of hole or dpl is null (see diagrams
3655 * above). The hole moves after the new aggragated frame.
3656 */
3657 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3658 h2c->dfl += clen - h2c->dpl;
3659 hole += h2c->dpl + 9;
3660 h2c->dpl = 0;
3661 goto next_frame;
3662 }
3663
3664 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003665
Willy Tarreau13278b42017-10-13 19:23:14 +02003666 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003667 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003668 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003669 copy = alloc_trash_chunk();
3670 if (!copy) {
3671 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3672 goto fail;
3673 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003674 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3675 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3676 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003677 }
3678
Willy Tarreau13278b42017-10-13 19:23:14 +02003679 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3680 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003681 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003682 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3683 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003684 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003685 }
3686
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003687 if (flen < 5) {
3688 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3689 goto fail;
3690 }
3691
Willy Tarreau13278b42017-10-13 19:23:14 +02003692 hdrs += 5; // stream dep = 4, weight = 1
3693 flen -= 5;
3694 }
3695
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003696 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003697 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003698 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003699 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003700
Willy Tarreau937f7602018-02-26 15:22:17 +01003701 /* we can't retry a failed decompression operation so we must be very
3702 * careful not to take any risks. In practice the output buffer is
3703 * always empty except maybe for trailers, in which case we simply have
3704 * to wait for the upper layer to finish consuming what is available.
3705 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003706
3707 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003708 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003709 if (!htx_is_empty(htx)) {
3710 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003711 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003712 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003713 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003714 if (b_data(rxbuf)) {
3715 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003716 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003717 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003718
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003719 rxbuf->head = 0;
3720 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003721 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003722
Willy Tarreau25919232019-01-03 14:48:18 +01003723 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003724 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3725 sizeof(list)/sizeof(list[0]), tmp);
3726 if (outlen < 0) {
3727 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3728 goto fail;
3729 }
3730
Willy Tarreau25919232019-01-03 14:48:18 +01003731 /* The PACK decompressor was updated, let's update the input buffer and
3732 * the parser's state to commit these changes and allow us to later
3733 * fail solely on the stream if needed.
3734 */
3735 b_del(&h2c->dbuf, h2c->dfl + hole);
3736 h2c->dfl = hole = 0;
3737 h2c->st0 = H2_CS_FRAME_H;
3738
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003739 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003740 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003741
Willy Tarreau88d138e2019-01-02 19:38:14 +01003742 if (*flags & H2_SF_HEADERS_RCVD)
3743 goto trailers;
3744
3745 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003746 if (htx) {
3747 /* HTX mode */
3748 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003749 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003750 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003751 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003752 } else {
3753 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003754 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003755 if (outlen > 0)
3756 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003757 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003758
3759 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003760 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003761 goto fail;
3762 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003763
Willy Tarreau174b06a2018-04-25 18:13:58 +02003764 if (msgf & H2_MSGF_BODY) {
3765 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003766 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003767 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003768 if (htx)
3769 htx->extra = *body_len;
3770 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003771 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003772 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003773 }
3774
Willy Tarreau88d138e2019-01-02 19:38:14 +01003775 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003776 /* indicate that a HEADERS frame was received for this stream, except
3777 * for 1xx responses. For 1xx responses, another HEADERS frame is
3778 * expected.
3779 */
3780 if (!(msgf & H2_MSGF_RSP_1XX))
3781 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003782
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02003783 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003784 /* Mark the end of message, either using EOM in HTX or with the
3785 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003786 * is not set during headers with END_STREAM. For HTX trailers,
3787 * we must not leave an HTX trailers block not followed by an
3788 * EOM block, the two must be atomic. Thus if we fail to emit
3789 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003790 */
3791 if (htx) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02003792 if (!htx_add_endof(htx, HTX_BLK_EOM))
Willy Tarreau88d138e2019-01-02 19:38:14 +01003793 goto fail;
3794 }
3795 else if (*flags & H2_SF_DATA_CHNK) {
3796 if (!b_putblk(rxbuf, "\r\n", 2))
3797 goto fail;
3798 }
3799 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003800
Willy Tarreau86277d42019-01-02 15:36:11 +01003801 /* success */
3802 ret = 1;
3803
Willy Tarreau68dd9852017-07-03 14:44:26 +02003804 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003805 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003806 * move the remaining data over it.
3807 */
3808 if (hole) {
3809 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3810 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3811 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3812 b_sub(&h2c->dbuf, hole);
3813 }
3814
Willy Tarreau97215ca2019-04-29 10:20:21 +02003815 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003816 /* too large frames */
3817 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003818 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003819 }
3820
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003821 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003822 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003823 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003824 return ret;
3825
Willy Tarreau68dd9852017-07-03 14:44:26 +02003826 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003827 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003828 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003829
3830 trailers:
3831 /* This is the last HEADERS frame hence a trailer */
3832
3833 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3834 /* It's a trailer but it's missing ES flag */
3835 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3836 goto fail;
3837 }
3838
Christopher Faulet54b5e212019-06-04 10:08:28 +02003839 /* Trailers terminate a DATA sequence. In HTX we always handle them. In
3840 * legacy, when using chunks, we have to emit the 0 CRLF marker first
3841 * and then handle the trailers. For other modes, the trailers are
3842 * silently dropped.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003843 */
3844 if (htx) {
Willy Tarreau5255f282019-01-03 18:41:05 +01003845 if (h2_make_htx_trailers(list, htx) <= 0)
3846 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003847 }
3848 else if (*flags & H2_SF_DATA_CHNK) {
3849 /* Legacy mode with chunked encoding : we must finalize the
3850 * data block message emit the trailing CRLF */
3851 if (!b_putblk(rxbuf, "0\r\n", 3))
3852 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003853
3854 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3855 if (outlen > 0)
3856 b_add(rxbuf, outlen);
3857 else
3858 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003859 }
3860
3861 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003862}
3863
Willy Tarreau454f9052017-10-26 19:40:35 +02003864/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3865 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3866 * in use, a new chunk is emitted for each frame. This is supposed to fit
3867 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3868 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3869 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003870 * parser state is automatically updated. Returns > 0 if it could completely
3871 * send the current frame, 0 if it couldn't complete, in which case
3872 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3873 * DATA frame can return 0 as a valid result). Stream errors are reported in
3874 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3875 * have checked the frame header and ensured that the frame was complete or the
3876 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003877 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003878static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003879{
3880 struct h2c *h2c = h2s->h2c;
3881 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003882 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003883 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003884 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003885 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003886
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003887 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003888
Olivier Houchard638b7992018-08-16 15:41:52 +02003889 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003890 if (!csbuf) {
3891 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003892 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003893 }
3894
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003895try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003896 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003897 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3898 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003899 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003900 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003901
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003902 if (flen > b_data(&h2c->dbuf)) {
3903 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003904 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003905 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003906 }
3907
Willy Tarreaua9b77962019-01-31 07:23:00 +01003908 if (htx) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003909 unsigned int sent;
3910
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003911 block1 = htx_free_data_space(htx);
3912 if (!block1) {
3913 h2c->flags |= H2_CF_DEM_SFULL;
3914 goto fail;
3915 }
3916 if (flen > block1)
3917 flen = block1;
3918
3919 /* here, flen is the max we can copy into the output buffer */
3920 block1 = b_contig_data(&h2c->dbuf, 0);
3921 if (flen > block1)
3922 flen = block1;
3923
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003924 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003925
Willy Tarreaub6563f42019-06-15 11:34:41 +02003926 b_del(&h2c->dbuf, sent);
3927 h2c->dfl -= sent;
3928 h2c->rcvd_c += sent;
3929 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003930
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003931 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreaub6563f42019-06-15 11:34:41 +02003932 h2s->body_len -= sent;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003933 htx->extra = h2s->body_len;
3934 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003935
3936 if (sent < flen) {
3937 h2c->flags |= H2_CF_DEM_SFULL;
3938 goto fail;
3939 }
3940
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003941 goto try_again;
3942 }
Willy Tarreau455d5682019-05-24 19:42:18 +02003943 else if (unlikely(b_space_wraps(csbuf) &&
3944 flen + chklen <= b_room(csbuf) &&
3945 b_data(csbuf) <= MAX_DATA_REALIGN)) {
3946 /* it doesn't fit in a single block and the buffer is fragmented, if there are
3947 * not too many data in the buffer, let's defragment it and try
3948 * again.
Willy Tarreaud755ea62018-02-26 15:44:54 +01003949 */
3950 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003951 }
3952
Willy Tarreaueba10f22018-04-25 20:44:22 +02003953 /* chunked-encoding requires more room */
3954 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003955 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003956 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3957 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3958 (chklen < 1048576) ? 4 : 8;
3959 chklen += 4; // CRLF, CRLF
3960 }
3961
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003962 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003963 if (flen + chklen > b_room(csbuf)) {
3964 if (chklen >= b_room(csbuf)) {
3965 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003966 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003967 }
3968 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003969 }
3970
3971 if (h2s->flags & H2_SF_DATA_CHNK) {
3972 /* emit the chunk size */
3973 unsigned int chksz = flen;
3974 char str[10];
3975 char *beg;
3976
3977 beg = str + sizeof(str);
3978 *--beg = '\n';
3979 *--beg = '\r';
3980 do {
3981 *--beg = hextab[chksz & 0xF];
3982 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003983 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003984 }
3985
Willy Tarreau454f9052017-10-26 19:40:35 +02003986 /* Block1 is the length of the first block before the buffer wraps,
3987 * block2 is the optional second block to reach the end of the frame.
3988 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003989 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003990 if (block1 > flen)
3991 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003992 block2 = flen - block1;
3993
3994 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003995 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003996
3997 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003998 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003999
Willy Tarreaueba10f22018-04-25 20:44:22 +02004000 if (h2s->flags & H2_SF_DATA_CHNK) {
4001 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01004002 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02004003 }
4004
Willy Tarreau454f9052017-10-26 19:40:35 +02004005 /* now mark the input data as consumed (will be deleted from the buffer
4006 * by the caller when seeing FRAME_A after sending the window update).
4007 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004008 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004009 h2c->dfl -= flen;
4010 h2c->rcvd_c += flen;
4011 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
4012
Willy Tarreau1915ca22019-01-24 11:49:37 +01004013 if (h2s->flags & H2_SF_DATA_CLEN)
4014 h2s->body_len -= flen;
4015
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004016 if (h2c->dfl > h2c->dpl) {
4017 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01004018 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004019 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004020 }
4021
Willy Tarreau4a28da12018-01-04 14:41:00 +01004022 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004023 /* here we're done with the frame, all the payload (except padding) was
4024 * transferred.
4025 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02004026
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004027 if (h2c->dff & H2_F_DATA_END_STREAM) {
4028 if (htx) {
4029 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
4030 h2c->flags |= H2_CF_DEM_SFULL;
4031 goto fail;
4032 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01004033 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004034 else if (h2s->flags & H2_SF_DATA_CHNK) {
4035 /* emit the trailing 0 CRLF CRLF */
4036 if (b_room(csbuf) < 5) {
4037 h2c->flags |= H2_CF_DEM_SFULL;
4038 goto fail;
4039 }
4040 chklen += 5;
4041 b_putblk(csbuf, "0\r\n\r\n", 5);
4042 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02004043 }
4044
Willy Tarreaud1023bb2018-03-22 16:53:12 +01004045 h2c->rcvd_c += h2c->dpl;
4046 h2c->rcvd_s += h2c->dpl;
4047 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004048 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004049 if (htx)
4050 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004051 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004052 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004053 if (htx)
4054 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004055 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004056}
4057
Willy Tarreau5dd17352018-06-14 13:33:30 +02004058/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
4059 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
4060 * number of bytes sent. The caller must check the stream's status to detect
4061 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004062 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004063static 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 +02004064{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004065 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004066 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004067 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004068 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004069 struct buffer *mbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004070 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004071 int es_now = 0;
4072 int ret = 0;
4073 int hdr;
4074
4075 if (h2c_mux_busy(h2c, h2s)) {
4076 h2s->flags |= H2_SF_BLK_MBUSY;
4077 return 0;
4078 }
4079
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004080 /* First, try to parse the H1 response and index it into <list>.
4081 * NOTE! Since it comes from haproxy, we *know* that a response header
4082 * block does not wrap and we can safely read it this way without
4083 * having to realign the buffer.
4084 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004085 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004086 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004087 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01004088 /* incomplete or invalid response, this is abnormal coming from
4089 * haproxy and may only result in a bad errorfile or bad Lua code
4090 * so that won't be fixed, raise an error now.
4091 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004092 * FIXME: we should instead add the ability to only return a
4093 * 502 bad gateway. But in theory this is not supposed to
4094 * happen.
4095 */
4096 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4097 ret = 0;
4098 goto end;
4099 }
4100
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004101 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02004102
4103 /* certain statuses have no body or an empty one, regardless of
4104 * what the headers say.
4105 */
4106 if (sl.st.status >= 100 && sl.st.status < 200) {
4107 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
4108 h1m->curr_len = h1m->body_len = 0;
4109 }
4110 else if (sl.st.status == 204 || sl.st.status == 304) {
4111 /* no contents, claim c-len is present and set to zero */
4112 h1m->flags &= ~H1_MF_CHNK;
4113 h1m->flags |= H1_MF_CLEN;
4114 h1m->curr_len = h1m->body_len = 0;
4115 }
4116
Willy Tarreaubcc45952019-05-26 10:05:50 +02004117 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004118 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004119 if (!h2_get_buf(h2c, mbuf)) {
4120 h2c->flags |= H2_CF_MUX_MALLOC;
4121 h2s->flags |= H2_SF_BLK_MROOM;
4122 return 0;
4123 }
4124
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004125 chunk_reset(&outbuf);
4126
4127 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004128 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4129 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004130 break;
4131 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004132 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004133 }
4134
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004135 if (outbuf.size < 9)
4136 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004137
4138 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004139 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4140 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4141 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004142
4143 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004144 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01004145 /* this is an unparsable response */
4146 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4147 ret = 0;
4148 goto end;
4149 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01004150
4151 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004152 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004153 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004154 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004155 }
4156
4157 /* encode all headers, stop at empty name */
4158 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01004159 /* these ones do not exist in H2 and must be dropped. */
4160 if (isteq(list[hdr].n, ist("connection")) ||
4161 isteq(list[hdr].n, ist("proxy-connection")) ||
4162 isteq(list[hdr].n, ist("keep-alive")) ||
4163 isteq(list[hdr].n, ist("upgrade")) ||
4164 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004165 continue;
4166
4167 if (isteq(list[hdr].n, ist("")))
4168 break; // end
4169
4170 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4171 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004172 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004173 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004174 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004175 }
4176 }
4177
4178 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01004179 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004180 es_now = 1;
4181
4182 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004183 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004184
4185 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004186 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004187
4188 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004189 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004190
4191 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004192 b_add(mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01004193 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004194
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004195 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01004196 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004197 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01004198
Willy Tarreau801250e2018-09-11 11:45:04 +02004199 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004200 h2s->flags |= H2_SF_ES_SENT;
4201 if (h2s->st == H2_SS_OPEN)
4202 h2s->st = H2_SS_HLOC;
4203 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004204 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004205 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004206 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004207 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004208 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004209 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004210 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004211 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004212 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004213
4214 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004215
4216 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004217 //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 +02004218 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004219 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004220 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4221 goto retry;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004222 h1m_init_res(h1m);
4223 h1m->err_pos = -1; // don't care about errors on the response path
4224 h2c->flags |= H2_CF_MUX_MFULL;
4225 h2s->flags |= H2_SF_BLK_MROOM;
4226 ret = 0;
4227 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004228}
4229
Willy Tarreau5dd17352018-06-14 13:33:30 +02004230/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4231 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4232 * the number of bytes sent. The caller must check the stream's status to
4233 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004234 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004235static 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 +02004236{
4237 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004238 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004239 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004240 struct buffer *mbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004241 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004242 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004243 int es_now = 0;
4244 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004245 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004246 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004247
4248 if (h2c_mux_busy(h2c, h2s)) {
4249 h2s->flags |= H2_SF_BLK_MBUSY;
4250 goto end;
4251 }
4252
Willy Tarreaubcc45952019-05-26 10:05:50 +02004253 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004254 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004255 if (!h2_get_buf(h2c, mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004256 h2c->flags |= H2_CF_MUX_MALLOC;
4257 h2s->flags |= H2_SF_BLK_MROOM;
4258 goto end;
4259 }
4260
4261 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004262 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004263 goto end;
4264
4265 chunk_reset(&outbuf);
4266
4267 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004268 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4269 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004270 break;
4271 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004272 /* If there are pending data in the output buffer, and we have
4273 * less than 1/4 of the mbuf's size and everything fits, we'll
4274 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4275 * is full and wait, to save some slow realign calls.
4276 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004277 if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004278 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4279 goto retry;
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004280 h2c->flags |= H2_CF_MUX_MFULL;
4281 h2s->flags |= H2_SF_BLK_MROOM;
4282 goto end;
4283 }
4284
Willy Tarreaubcc45952019-05-26 10:05:50 +02004285 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004286 }
4287
4288 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004289 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4290 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004291 h2c->flags |= H2_CF_MUX_MFULL;
4292 h2s->flags |= H2_SF_BLK_MROOM;
4293 goto end;
4294 }
4295
4296 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004297 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4298 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4299 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004300
4301 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4302 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004303 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004304 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004305 break;
4306 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004307 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004308 if ((long long)size > h1m->curr_len)
4309 size = h1m->curr_len;
4310 break;
4311 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004312 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004313 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004314 if (!ret)
4315 goto end;
4316
4317 if (ret < 0) {
4318 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004319 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004320 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4321 goto end;
4322 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004323 max -= ret;
4324 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004325 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004326 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004327 }
4328
Willy Tarreau801250e2018-09-11 11:45:04 +02004329 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004330 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004331 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004332 if (!ret)
4333 goto end;
4334
4335 if (ret < 0) {
4336 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004337 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004338 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4339 goto end;
4340 }
4341
4342 size = chunk;
4343 h1m->curr_len = chunk;
4344 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004345 max -= ret;
4346 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004347 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004348 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004349 if (!size)
4350 goto send_empty;
4351 }
4352
4353 /* in MSG_DATA state, continue below */
4354 size = h1m->curr_len;
4355 break;
4356 }
4357
4358 /* we have in <size> the exact number of bytes we need to copy from
4359 * the H1 buffer. We need to check this against the connection's and
4360 * the stream's send windows, and to ensure that this fits in the max
4361 * frame size and in the buffer's available space minus 9 bytes (for
4362 * the frame header). The connection's flow control is applied last so
4363 * that we can use a separate list of streams which are immediately
4364 * unblocked on window opening. Note: we don't implement padding.
4365 */
4366
Willy Tarreau5dd17352018-06-14 13:33:30 +02004367 if (size > max)
4368 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004369
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004370 if (size > h2s_mws(h2s))
4371 size = h2s_mws(h2s);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004372
4373 if (size <= 0) {
4374 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004375 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004376 LIST_DEL_INIT(&h2s->list);
Willy Tarreau55dc0842019-10-22 10:04:39 +02004377 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004378 goto end;
4379 }
4380
4381 if (h2c->mfs && size > h2c->mfs)
4382 size = h2c->mfs;
4383
4384 if (size + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02004385 /* It doesn't fit at once. If it at least fits once split and
4386 * the amount of data to move is low, let's defragment the
4387 * buffer now.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004388 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004389 if (b_space_wraps(mbuf) &&
4390 (size + 9 <= b_room(mbuf)) &&
4391 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004392 goto realign_again;
4393 size = outbuf.size - 9;
4394 }
4395
4396 if (size <= 0) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004397 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4398 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004399 h2c->flags |= H2_CF_MUX_MFULL;
4400 h2s->flags |= H2_SF_BLK_MROOM;
4401 goto end;
4402 }
4403
4404 if (size > h2c->mws)
4405 size = h2c->mws;
4406
4407 if (size <= 0) {
4408 h2s->flags |= H2_SF_BLK_MFCTL;
4409 goto end;
4410 }
4411
4412 /* copy whatever we can */
4413 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004414 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004415 if (ret == 1)
4416 len2 = 0;
4417
4418 if (!ret || len1 + len2 < size) {
4419 /* FIXME: must normally never happen */
4420 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4421 goto end;
4422 }
4423
4424 /* limit len1/len2 to size */
4425 if (len1 + len2 > size) {
4426 int sub = len1 + len2 - size;
4427
4428 if (len2 > sub)
4429 len2 -= sub;
4430 else {
4431 sub -= len2;
4432 len2 = 0;
4433 len1 -= sub;
4434 }
4435 }
4436
4437 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004438 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004439 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004440 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004441
4442 send_empty:
4443 /* we may need to add END_STREAM */
4444 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4445 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004446 *
4447 * FIXME: what we do here is not correct because we send end_stream
4448 * before knowing if we'll have to send a HEADERS frame for the
4449 * trailers. More importantly we're not consuming the trailing CRLF
4450 * after the end of trailers, so it will be left to the caller to
4451 * eat it. The right way to do it would be to measure trailers here
4452 * and to send ES only if there are no trailers.
4453 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004454 */
4455 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004456 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004457 es_now = 1;
4458
4459 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004460 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004461
4462 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004463 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004464
4465 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004466 b_add(mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004467
4468 /* consume incoming H1 response */
4469 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004470 max -= size;
4471 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004472 total += size;
4473 h1m->curr_len -= size;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004474 h2s->sws -= size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004475 h2c->mws -= size;
4476
4477 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004478 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004479 goto new_frame;
4480 }
4481 }
4482
4483 if (es_now) {
4484 if (h2s->st == H2_SS_OPEN)
4485 h2s->st = H2_SS_HLOC;
4486 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004487 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004488
Willy Tarreau35a62702018-02-27 15:37:25 +01004489 if (!(h1m->flags & H1_MF_CHNK)) {
4490 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004491 total += max;
4492 ofs += max;
4493 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004494
Willy Tarreau801250e2018-09-11 11:45:04 +02004495 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004496 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004497
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004498 h2s->flags |= H2_SF_ES_SENT;
4499 }
4500
4501 end:
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004502 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(h2s), (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004503 return total;
4504}
4505
Willy Tarreau115e83b2018-12-01 19:17:53 +01004506/* Try to send a HEADERS frame matching HTX response present in HTX message
4507 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4508 * must check the stream's status to detect any error which might have happened
4509 * subsequently to a successful send. The htx blocks are automatically removed
4510 * from the message. The htx message is assumed to be valid since produced from
4511 * the internal code, hence it contains a start line, an optional series of
4512 * header blocks and an end of header, otherwise an invalid frame could be
4513 * emitted and the resulting htx message could be left in an inconsistent state.
4514 */
4515static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4516{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004517 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004518 struct h2c *h2c = h2s->h2c;
4519 struct htx_blk *blk;
4520 struct htx_blk *blk_end;
4521 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004522 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004523 struct htx_sl *sl;
4524 enum htx_blk_type type;
4525 int es_now = 0;
4526 int ret = 0;
4527 int hdr;
4528 int idx;
4529
4530 if (h2c_mux_busy(h2c, h2s)) {
4531 h2s->flags |= H2_SF_BLK_MBUSY;
4532 return 0;
4533 }
4534
Willy Tarreau115e83b2018-12-01 19:17:53 +01004535 /* determine the first block which must not be deleted, blk_end may
4536 * be NULL if all blocks have to be deleted.
4537 */
4538 idx = htx_get_head(htx);
4539 blk_end = NULL;
4540 while (idx != -1) {
4541 type = htx_get_blk_type(htx_get_blk(htx, idx));
4542 idx = htx_get_next(htx, idx);
4543 if (type == HTX_BLK_EOH) {
4544 if (idx != -1)
4545 blk_end = htx_get_blk(htx, idx);
4546 break;
4547 }
4548 }
4549
4550 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004551 blk = htx_get_head_blk(htx);
4552 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL);
4553 ALREADY_CHECKED(blk);
4554 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004555 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004556 if (h2s->status < 100 || h2s->status > 999)
4557 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004558
4559 /* and the rest of the headers, that we dump starting at header 0 */
4560 hdr = 0;
4561
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004562 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004563 while ((idx = htx_get_next(htx, idx)) != -1) {
4564 blk = htx_get_blk(htx, idx);
4565 type = htx_get_blk_type(blk);
4566
4567 if (type == HTX_BLK_UNUSED)
4568 continue;
4569
4570 if (type != HTX_BLK_HDR)
4571 break;
4572
4573 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4574 goto fail;
4575
4576 list[hdr].n = htx_get_blk_name(htx, blk);
4577 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004578 hdr++;
4579 }
4580
4581 /* marker for end of headers */
4582 list[hdr].n = ist("");
4583
4584 if (h2s->status == 204 || h2s->status == 304) {
4585 /* no contents, claim c-len is present and set to zero */
4586 es_now = 1;
4587 }
4588
Willy Tarreau9c218e72019-05-26 10:08:28 +02004589 mbuf = br_tail(h2c->mbuf);
4590 retry:
4591 if (!h2_get_buf(h2c, mbuf)) {
4592 h2c->flags |= H2_CF_MUX_MALLOC;
4593 h2s->flags |= H2_SF_BLK_MROOM;
4594 return 0;
4595 }
4596
Willy Tarreau115e83b2018-12-01 19:17:53 +01004597 chunk_reset(&outbuf);
4598
4599 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004600 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4601 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004602 break;
4603 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004604 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004605 }
4606
4607 if (outbuf.size < 9)
4608 goto full;
4609
4610 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4611 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4612 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4613 outbuf.data = 9;
4614
4615 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004616 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004617 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004618 goto realign_again;
4619 goto full;
4620 }
4621
4622 /* encode all headers, stop at empty name */
4623 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4624 /* these ones do not exist in H2 and must be dropped. */
4625 if (isteq(list[hdr].n, ist("connection")) ||
4626 isteq(list[hdr].n, ist("proxy-connection")) ||
4627 isteq(list[hdr].n, ist("keep-alive")) ||
4628 isteq(list[hdr].n, ist("upgrade")) ||
4629 isteq(list[hdr].n, ist("transfer-encoding")))
4630 continue;
4631
4632 if (isteq(list[hdr].n, ist("")))
4633 break; // end
4634
4635 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4636 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004637 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004638 goto realign_again;
4639 goto full;
4640 }
4641 }
4642
Christopher Faulet0b465482019-02-19 15:14:23 +01004643 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004644 * FIXME: we should also set it when we know for sure that the
4645 * content-length is zero as well as on 204/304
4646 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004647 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4648 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004649 es_now = 1;
4650
Willy Tarreau927b88b2019-03-04 08:03:25 +01004651 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004652 es_now = 1;
4653
4654 /* update the frame's size */
4655 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4656
4657 if (es_now)
4658 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4659
4660 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004661 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004662
4663 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4664 * 1xx responses, another HEADERS frame is expected.
4665 */
4666 if (h2s->status >= 200 || h2s->status == 101)
4667 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004668
Willy Tarreau115e83b2018-12-01 19:17:53 +01004669 if (es_now) {
4670 h2s->flags |= H2_SF_ES_SENT;
4671 if (h2s->st == H2_SS_OPEN)
4672 h2s->st = H2_SS_HLOC;
4673 else
4674 h2s_close(h2s);
4675 }
4676
4677 /* OK we could properly deliver the response */
4678
4679 /* remove all header blocks including the EOH and compute the
4680 * corresponding size.
4681 *
4682 * FIXME: We should remove everything when es_now is set.
4683 */
4684 ret = 0;
4685 idx = htx_get_head(htx);
4686 blk = htx_get_blk(htx, idx);
4687 while (blk != blk_end) {
4688 ret += htx_get_blksz(blk);
4689 blk = htx_remove_blk(htx, blk);
4690 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004691
Christopher Faulet316934d2019-05-15 14:22:48 +02004692 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4693 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004694 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004695 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004696 end:
4697 return ret;
4698 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004699 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4700 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004701 h2c->flags |= H2_CF_MUX_MFULL;
4702 h2s->flags |= H2_SF_BLK_MROOM;
4703 ret = 0;
4704 goto end;
4705 fail:
4706 /* unparsable HTX messages, too large ones to be produced in the local
4707 * list etc go here (unrecoverable errors).
4708 */
4709 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4710 ret = 0;
4711 goto end;
4712}
4713
Willy Tarreau80739692018-10-05 11:35:57 +02004714/* Try to send a HEADERS frame matching HTX request present in HTX message
4715 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4716 * must check the stream's status to detect any error which might have happened
4717 * subsequently to a successful send. The htx blocks are automatically removed
4718 * from the message. The htx message is assumed to be valid since produced from
4719 * the internal code, hence it contains a start line, an optional series of
4720 * header blocks and an end of header, otherwise an invalid frame could be
4721 * emitted and the resulting htx message could be left in an inconsistent state.
4722 */
4723static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4724{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004725 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004726 struct h2c *h2c = h2s->h2c;
4727 struct htx_blk *blk;
4728 struct htx_blk *blk_end;
4729 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004730 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004731 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004732 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004733 enum htx_blk_type type;
4734 int es_now = 0;
4735 int ret = 0;
4736 int hdr;
4737 int idx;
4738
4739 if (h2c_mux_busy(h2c, h2s)) {
4740 h2s->flags |= H2_SF_BLK_MBUSY;
4741 return 0;
4742 }
4743
Willy Tarreau80739692018-10-05 11:35:57 +02004744 /* determine the first block which must not be deleted, blk_end may
4745 * be NULL if all blocks have to be deleted.
4746 */
4747 idx = htx_get_head(htx);
4748 blk_end = NULL;
4749 while (idx != -1) {
4750 type = htx_get_blk_type(htx_get_blk(htx, idx));
4751 idx = htx_get_next(htx, idx);
4752 if (type == HTX_BLK_EOH) {
4753 if (idx != -1)
4754 blk_end = htx_get_blk(htx, idx);
4755 break;
4756 }
4757 }
4758
4759 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004760 blk = htx_get_head_blk(htx);
4761 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
4762 ALREADY_CHECKED(blk);
4763 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004764 meth = htx_sl_req_meth(sl);
4765 path = htx_sl_req_uri(sl);
4766
4767 /* and the rest of the headers, that we dump starting at header 0 */
4768 hdr = 0;
4769
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004770 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004771 while ((idx = htx_get_next(htx, idx)) != -1) {
4772 blk = htx_get_blk(htx, idx);
4773 type = htx_get_blk_type(blk);
4774
4775 if (type == HTX_BLK_UNUSED)
4776 continue;
4777
4778 if (type != HTX_BLK_HDR)
4779 break;
4780
4781 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4782 goto fail;
4783
4784 list[hdr].n = htx_get_blk_name(htx, blk);
4785 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004786 hdr++;
4787 }
4788
4789 /* marker for end of headers */
4790 list[hdr].n = ist("");
4791
Willy Tarreau9c218e72019-05-26 10:08:28 +02004792 mbuf = br_tail(h2c->mbuf);
4793 retry:
4794 if (!h2_get_buf(h2c, mbuf)) {
4795 h2c->flags |= H2_CF_MUX_MALLOC;
4796 h2s->flags |= H2_SF_BLK_MROOM;
4797 return 0;
4798 }
4799
Willy Tarreau80739692018-10-05 11:35:57 +02004800 chunk_reset(&outbuf);
4801
4802 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004803 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4804 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004805 break;
4806 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004807 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004808 }
4809
4810 if (outbuf.size < 9)
4811 goto full;
4812
4813 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4814 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4815 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4816 outbuf.data = 9;
4817
4818 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004819 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004820 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004821 goto realign_again;
4822 goto full;
4823 }
4824
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004825 /* RFC7540 #8.3: the CONNECT method must have :
4826 * - :authority set to the URI part (host:port)
4827 * - :method set to CONNECT
4828 * - :scheme and :path omitted
4829 */
4830 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4831 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004832 struct ist scheme;
4833
4834 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4835 scheme = ist("http");
4836 else
4837 scheme = ist("https");
4838
4839 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004840 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004841 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004842 goto realign_again;
4843 goto full;
4844 }
Willy Tarreau80739692018-10-05 11:35:57 +02004845
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004846 /* encode the path, which necessarily is the second one */
4847 if (!hpack_encode_path(&outbuf, path)) {
4848 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004849 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004850 goto realign_again;
4851 goto full;
4852 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004853
4854 /* look for the Host header and place it in :authority */
4855 auth = ist2(NULL, 0);
4856 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4857 if (isteq(list[hdr].n, ist("")))
4858 break; // end
4859
4860 if (isteq(list[hdr].n, ist("host"))) {
4861 auth = list[hdr].v;
4862 break;
4863 }
4864 }
4865 }
4866 else {
4867 /* for CONNECT, :authority is taken from the path */
4868 auth = path;
4869 }
4870
4871 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4872 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004873 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004874 goto realign_again;
4875 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004876 }
4877
4878 /* encode all headers, stop at empty name */
4879 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4880 /* these ones do not exist in H2 and must be dropped. */
4881 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004882 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004883 isteq(list[hdr].n, ist("proxy-connection")) ||
4884 isteq(list[hdr].n, ist("keep-alive")) ||
4885 isteq(list[hdr].n, ist("upgrade")) ||
4886 isteq(list[hdr].n, ist("transfer-encoding")))
4887 continue;
4888
4889 if (isteq(list[hdr].n, ist("")))
4890 break; // end
4891
4892 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4893 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004894 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004895 goto realign_again;
4896 goto full;
4897 }
4898 }
4899
4900 /* we may need to add END_STREAM if we have no body :
4901 * - request already closed, or :
4902 * - no transfer-encoding, and :
4903 * - no content-length or content-length:0
4904 * Fixme: this doesn't take into account CONNECT requests.
4905 */
4906 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4907 es_now = 1;
4908
4909 if (sl->flags & HTX_SL_F_BODYLESS)
4910 es_now = 1;
4911
Willy Tarreau927b88b2019-03-04 08:03:25 +01004912 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004913 es_now = 1;
4914
4915 /* update the frame's size */
4916 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4917
4918 if (es_now)
4919 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4920
4921 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004922 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004923 h2s->flags |= H2_SF_HEADERS_SENT;
4924 h2s->st = H2_SS_OPEN;
4925
Willy Tarreau80739692018-10-05 11:35:57 +02004926 if (es_now) {
4927 // trim any possibly pending data (eg: inconsistent content-length)
4928 h2s->flags |= H2_SF_ES_SENT;
4929 h2s->st = H2_SS_HLOC;
4930 }
4931
4932 /* remove all header blocks including the EOH and compute the
4933 * corresponding size.
4934 *
4935 * FIXME: We should remove everything when es_now is set.
4936 */
4937 ret = 0;
4938 idx = htx_get_head(htx);
4939 blk = htx_get_blk(htx, idx);
4940 while (blk != blk_end) {
4941 ret += htx_get_blksz(blk);
4942 blk = htx_remove_blk(htx, blk);
4943 }
4944
Christopher Faulet316934d2019-05-15 14:22:48 +02004945 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4946 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004947 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004948 }
Willy Tarreau80739692018-10-05 11:35:57 +02004949
4950 end:
4951 return ret;
4952 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004953 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4954 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004955 h2c->flags |= H2_CF_MUX_MFULL;
4956 h2s->flags |= H2_SF_BLK_MROOM;
4957 ret = 0;
4958 goto end;
4959 fail:
4960 /* unparsable HTX messages, too large ones to be produced in the local
4961 * list etc go here (unrecoverable errors).
4962 */
4963 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4964 ret = 0;
4965 goto end;
4966}
4967
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004968/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004969 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4970 * caller must check the stream's status to detect any error which might have
4971 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02004972 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004973 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004974static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004975{
4976 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004977 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004978 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004979 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004980 size_t total = 0;
4981 int es_now = 0;
4982 int bsize; /* htx block size */
4983 int fsize; /* h2 frame size */
4984 struct htx_blk *blk;
4985 enum htx_blk_type type;
4986 int idx;
4987
4988 if (h2c_mux_busy(h2c, h2s)) {
4989 h2s->flags |= H2_SF_BLK_MBUSY;
4990 goto end;
4991 }
4992
Willy Tarreau98de12a2018-12-12 07:03:00 +01004993 htx = htx_from_buf(buf);
4994
Christopher Faulet54b5e212019-06-04 10:08:28 +02004995 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
4996 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
4997 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004998 */
4999
5000 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01005001 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005002 goto end;
5003
5004 idx = htx_get_head(htx);
5005 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02005006 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005007 bsize = htx_get_blksz(blk);
5008 fsize = bsize;
5009
Christopher Faulet54b5e212019-06-04 10:08:28 +02005010 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01005011 if (h2s->flags & H2_SF_ES_SENT) {
5012 /* ES already sent */
5013 htx_remove_blk(htx, blk);
5014 total++; // EOM counts as one byte
5015 count--;
5016 goto end;
5017 }
5018 }
5019 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005020 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005021
Willy Tarreau9c218e72019-05-26 10:08:28 +02005022 mbuf = br_tail(h2c->mbuf);
5023 retry:
5024 if (!h2_get_buf(h2c, mbuf)) {
5025 h2c->flags |= H2_CF_MUX_MALLOC;
5026 h2s->flags |= H2_SF_BLK_MROOM;
5027 goto end;
5028 }
5029
Willy Tarreau98de12a2018-12-12 07:03:00 +01005030 /* Perform some optimizations to reduce the number of buffer copies.
5031 * First, if the mux's buffer is empty and the htx area contains
5032 * exactly one data block of the same size as the requested count, and
5033 * this count fits within the frame size, the stream's window size, and
5034 * the connection's window size, then it's possible to simply swap the
5035 * caller's buffer with the mux's output buffer and adjust offsets and
5036 * length to match the entire DATA HTX block in the middle. In this
5037 * case we perform a true zero-copy operation from end-to-end. This is
5038 * the situation that happens all the time with large files. Second, if
5039 * this is not possible, but the mux's output buffer is empty, we still
5040 * have an opportunity to avoid the copy to the intermediary buffer, by
5041 * making the intermediary buffer's area point to the output buffer's
5042 * area. In this case we want to skip the HTX header to make sure that
5043 * copies remain aligned and that this operation remains possible all
5044 * the time. This goes for headers, data blocks and any data extracted
5045 * from the HTX blocks.
5046 */
5047 if (unlikely(fsize == count &&
5048 htx->used == 1 && type == HTX_BLK_DATA &&
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005049 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005050 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005051
Willy Tarreaubcc45952019-05-26 10:05:50 +02005052 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005053 /* Too bad there are data left there. We're willing to memcpy/memmove
5054 * up to 1/4 of the buffer, which means that it's OK to copy a large
5055 * frame into a buffer containing few data if it needs to be realigned,
5056 * and that it's also OK to copy few data without realigning. Otherwise
5057 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01005058 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005059 if (fsize + 9 <= b_room(mbuf) &&
5060 (b_data(mbuf) <= b_size(mbuf) / 4 ||
5061 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01005062 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005063
Willy Tarreau9c218e72019-05-26 10:08:28 +02005064 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5065 goto retry;
5066
Willy Tarreau98de12a2018-12-12 07:03:00 +01005067 h2c->flags |= H2_CF_MUX_MFULL;
5068 h2s->flags |= H2_SF_BLK_MROOM;
5069 goto end;
5070 }
5071
5072 /* map an H2 frame to the HTX block so that we can put the
5073 * frame header there.
5074 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005075 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
5076 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005077
5078 /* prepend an H2 DATA frame header just before the DATA block */
5079 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5080 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5081 h2_set_frame_size(outbuf.area, fsize);
5082
5083 /* update windows */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005084 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005085 h2c->mws -= fsize;
5086
5087 /* and exchange with our old area */
5088 buf->area = old_area;
5089 buf->data = buf->head = 0;
5090 total += fsize;
5091 goto end;
5092 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005093
Willy Tarreau98de12a2018-12-12 07:03:00 +01005094 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005095 /* for DATA and EOM we'll have to emit a frame, even if empty */
5096
5097 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005098 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5099 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005100 break;
5101 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005102 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005103 }
5104
5105 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005106 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5107 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005108 h2c->flags |= H2_CF_MUX_MFULL;
5109 h2s->flags |= H2_SF_BLK_MROOM;
5110 goto end;
5111 }
5112
5113 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5114 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5115 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5116 outbuf.data = 9;
5117
5118 /* we have in <fsize> the exact number of bytes we need to copy from
5119 * the HTX buffer. We need to check this against the connection's and
5120 * the stream's send windows, and to ensure that this fits in the max
5121 * frame size and in the buffer's available space minus 9 bytes (for
5122 * the frame header). The connection's flow control is applied last so
5123 * that we can use a separate list of streams which are immediately
5124 * unblocked on window opening. Note: we don't implement padding.
5125 */
5126
5127 /* EOM is presented with bsize==1 but would lead to the emission of an
5128 * empty frame, thus we force it to zero here.
5129 */
5130 if (type == HTX_BLK_EOM)
5131 bsize = fsize = 0;
5132
5133 if (!fsize)
5134 goto send_empty;
5135
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005136 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005137 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005138 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005139 LIST_DEL_INIT(&h2s->list);
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005140 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005141 goto end;
5142 }
5143
Willy Tarreauee573762018-12-04 15:25:57 +01005144 if (fsize > count)
5145 fsize = count;
5146
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005147 if (fsize > h2s_mws(h2s))
5148 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005149
5150 if (h2c->mfs && fsize > h2c->mfs)
5151 fsize = h2c->mfs; // >0
5152
5153 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005154 /* It doesn't fit at once. If it at least fits once split and
5155 * the amount of data to move is low, let's defragment the
5156 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005157 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005158 if (b_space_wraps(mbuf) &&
5159 (fsize + 9 <= b_room(mbuf)) &&
5160 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005161 goto realign_again;
5162 fsize = outbuf.size - 9;
5163
5164 if (fsize <= 0) {
5165 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005166 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5167 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005168 h2c->flags |= H2_CF_MUX_MFULL;
5169 h2s->flags |= H2_SF_BLK_MROOM;
5170 goto end;
5171 }
5172 }
5173
5174 if (h2c->mws <= 0) {
5175 h2s->flags |= H2_SF_BLK_MFCTL;
5176 goto end;
5177 }
5178
5179 if (fsize > h2c->mws)
5180 fsize = h2c->mws;
5181
5182 /* now let's copy this this into the output buffer */
5183 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005184 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005185 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005186 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005187
5188 send_empty:
5189 /* update the frame's size */
5190 h2_set_frame_size(outbuf.area, fsize);
5191
5192 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5193 * meeting EOM. We should optimize this later.
5194 */
5195 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005196 total++; // EOM counts as one byte
5197 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005198 es_now = 1;
5199 }
5200
5201 if (es_now)
5202 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5203
5204 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005205 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005206
5207 /* consume incoming HTX block, including EOM */
5208 total += fsize;
5209 if (fsize == bsize) {
5210 htx_remove_blk(htx, blk);
5211 if (fsize)
5212 goto new_frame;
5213 } else {
5214 /* we've truncated this block */
5215 htx_cut_data_blk(htx, blk, fsize);
5216 }
5217
5218 if (es_now) {
5219 if (h2s->st == H2_SS_OPEN)
5220 h2s->st = H2_SS_HLOC;
5221 else
5222 h2s_close(h2s);
5223
5224 h2s->flags |= H2_SF_ES_SENT;
5225 }
5226
5227 end:
5228 return total;
5229}
5230
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005231/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5232 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5233 * processed. The caller must check the stream's status to detect any error
5234 * which might have happened subsequently to a successful send. The htx blocks
5235 * are automatically removed from the message. The htx message is assumed to be
5236 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005237 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005238 * as a single frame. The ES flag is always set.
5239 */
5240static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5241{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005242 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005243 struct h2c *h2c = h2s->h2c;
5244 struct htx_blk *blk;
5245 struct htx_blk *blk_end;
5246 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005247 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005248 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005249 int ret = 0;
5250 int hdr;
5251 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005252
5253 if (h2c_mux_busy(h2c, h2s)) {
5254 h2s->flags |= H2_SF_BLK_MBUSY;
5255 goto end;
5256 }
5257
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005258 /* determine the first block which must not be deleted, blk_end may
5259 * be NULL if all blocks have to be deleted. also get trailers.
5260 */
5261 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005262 blk_end = NULL;
5263
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005264 hdr = 0;
5265 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005266 blk = htx_get_blk(htx, idx);
5267 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005268 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005269 if (type == HTX_BLK_UNUSED)
5270 continue;
5271
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005272 if (type == HTX_BLK_EOT) {
5273 if (idx != -1)
5274 blk_end = blk;
5275 break;
5276 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005277 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005278 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005279
5280 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5281 goto fail;
5282
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005283 list[hdr].n = htx_get_blk_name(htx, blk);
5284 list[hdr].v = htx_get_blk_value(htx, blk);
5285 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005286 }
5287
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005288 /* marker for end of trailers */
5289 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005290
Willy Tarreau9c218e72019-05-26 10:08:28 +02005291 mbuf = br_tail(h2c->mbuf);
5292 retry:
5293 if (!h2_get_buf(h2c, mbuf)) {
5294 h2c->flags |= H2_CF_MUX_MALLOC;
5295 h2s->flags |= H2_SF_BLK_MROOM;
5296 goto end;
5297 }
5298
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005299 chunk_reset(&outbuf);
5300
5301 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005302 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5303 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005304 break;
5305 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005306 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005307 }
5308
5309 if (outbuf.size < 9)
5310 goto full;
5311
5312 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5313 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5314 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5315 outbuf.data = 9;
5316
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005317 /* encode all headers */
5318 for (idx = 0; idx < hdr; idx++) {
5319 /* these ones do not exist in H2 or must not appear in
5320 * trailers and must be dropped.
5321 */
5322 if (isteq(list[idx].n, ist("host")) ||
5323 isteq(list[idx].n, ist("content-length")) ||
5324 isteq(list[idx].n, ist("connection")) ||
5325 isteq(list[idx].n, ist("proxy-connection")) ||
5326 isteq(list[idx].n, ist("keep-alive")) ||
5327 isteq(list[idx].n, ist("upgrade")) ||
5328 isteq(list[idx].n, ist("te")) ||
5329 isteq(list[idx].n, ist("transfer-encoding")))
5330 continue;
5331
5332 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5333 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005334 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005335 goto realign_again;
5336 goto full;
5337 }
5338 }
5339
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005340 if (outbuf.data == 9) {
5341 /* here we have a problem, we have nothing to emit (either we
5342 * received an empty trailers block followed or we removed its
5343 * contents above). Because of this we can't send a HEADERS
5344 * frame, so we have to cheat and instead send an empty DATA
5345 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005346 */
5347 outbuf.area[3] = H2_FT_DATA;
5348 outbuf.area[4] = H2_F_DATA_END_STREAM;
5349 }
5350
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005351 /* update the frame's size */
5352 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5353
5354 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005355 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005356 h2s->flags |= H2_SF_ES_SENT;
5357
5358 if (h2s->st == H2_SS_OPEN)
5359 h2s->st = H2_SS_HLOC;
5360 else
5361 h2s_close(h2s);
5362
5363 /* OK we could properly deliver the response */
5364 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005365 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005366 ret = 0;
5367 idx = htx_get_head(htx);
5368 blk = htx_get_blk(htx, idx);
5369 while (blk != blk_end) {
5370 ret += htx_get_blksz(blk);
5371 blk = htx_remove_blk(htx, blk);
5372 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005373
5374 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5375 ret += htx_get_blksz(blk_end);
5376 htx_remove_blk(htx, blk_end);
5377 }
5378
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005379 end:
5380 return ret;
5381 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005382 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5383 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005384 h2c->flags |= H2_CF_MUX_MFULL;
5385 h2s->flags |= H2_SF_BLK_MROOM;
5386 ret = 0;
5387 goto end;
5388 fail:
5389 /* unparsable HTX messages, too large ones to be produced in the local
5390 * list etc go here (unrecoverable errors).
5391 */
5392 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5393 ret = 0;
5394 goto end;
5395}
5396
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005397/* Called from the upper layer, to subscribe to events, such as being able to send.
5398 * The <param> argument here is supposed to be a pointer to a wait_event struct
5399 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5400 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5401 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5402 * returns 0 except for the error above.
5403 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005404static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5405{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005406 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005407 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005408 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005409
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005410 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005411 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005412 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5413 sw->events |= SUB_RETRY_RECV;
5414 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005415 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005416 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005417 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005418 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005419 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5420 sw->events |= SUB_RETRY_SEND;
5421 h2s->send_wait = sw;
5422 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5423 !LIST_ADDED(&h2s->list)) {
5424 if (h2s->flags & H2_SF_BLK_MFCTL)
5425 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5426 else
5427 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005428 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005429 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005430 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005431 if (event_type != 0)
5432 return -1;
5433 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005434}
5435
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005436/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5437 * The <param> argument here is supposed to be a pointer to the same wait_event
5438 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5439 * It always returns zero.
5440 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005441static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5442{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005443 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005444 struct h2s *h2s = cs->ctx;
5445
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005446 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005447 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005448 BUG_ON(h2s->recv_wait != sw);
5449 sw->events &= ~SUB_RETRY_RECV;
5450 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005451 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005452 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005453 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005454 BUG_ON(h2s->send_wait != sw);
5455 LIST_DEL(&h2s->list);
5456 LIST_INIT(&h2s->list);
5457 sw->events &= ~SUB_RETRY_SEND;
5458 /* We were about to send, make sure it does not happen */
5459 if (LIST_ADDED(&h2s->sending_list) &&
5460 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02005461 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02005462 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005463 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005464 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005465 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005466 return 0;
5467}
5468
5469
Olivier Houchard511efea2018-08-16 15:30:32 +02005470/* Called from the upper layer, to receive data */
5471static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5472{
Olivier Houchard638b7992018-08-16 15:41:52 +02005473 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005474 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005475 struct htx *h2s_htx = NULL;
5476 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005477 size_t ret = 0;
5478
5479 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005480 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau86724e22018-12-01 23:19:43 +01005481 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005482 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005483 /* Here htx_to_buf() will set buffer data to 0 because
5484 * the HTX is empty.
5485 */
5486 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005487 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005488 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005489
Christopher Faulet156852b2019-05-16 11:29:13 +02005490 ret = h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005491 buf_htx = htx_from_buf(buf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005492
Christopher Faulet2f6edc82019-05-15 10:07:59 +02005493 /* <buf> is empty and the message is small enough, swap the
5494 * buffers. */
5495 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
5496 htx_to_buf(buf_htx, buf);
5497 htx_to_buf(h2s_htx, &h2s->rxbuf);
5498 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5499 goto end;
5500 }
5501
Christopher Faulet156852b2019-05-16 11:29:13 +02005502 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005503
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005504 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
Willy Tarreau7196dd62019-03-05 10:51:11 +01005505 buf_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005506 if (htx_is_empty(buf_htx))
5507 cs->flags |= CS_FL_EOI;
5508 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005509
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005510 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005511 htx_to_buf(buf_htx, buf);
5512 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet156852b2019-05-16 11:29:13 +02005513 ret -= h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005514 }
5515 else {
5516 ret = b_xfer(buf, &h2s->rxbuf, count);
5517 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005518
Christopher Faulet37070b22019-02-14 15:12:14 +01005519 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005520 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005521 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005522 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005523 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005524 if (h2s->flags & H2_SF_ES_RCVD)
5525 cs->flags |= CS_FL_EOI;
Willy Tarreau76c83822019-06-15 09:55:50 +02005526 if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02005527 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005528 if (cs->flags & CS_FL_ERR_PENDING)
5529 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005530 if (b_size(&h2s->rxbuf)) {
5531 b_free(&h2s->rxbuf);
5532 offer_buffers(NULL, tasks_run_queue);
5533 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005534 }
5535
Willy Tarreau082f5592018-11-25 08:03:32 +01005536 if (ret && h2c->dsi == h2s->id) {
5537 /* demux is blocking on this stream's buffer */
5538 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005539 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005540 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005541
Olivier Houchard511efea2018-08-16 15:30:32 +02005542 return ret;
5543}
5544
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005545/* stops all senders of this connection for example when the mux buffer is full.
5546 * They are moved from the sending_list to either fctl_list or send_list.
5547 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005548static void h2_stop_senders(struct h2c *h2c)
5549{
5550 struct h2s *h2s, *h2s_back;
5551
Olivier Houchardd360ac62019-03-22 17:37:16 +01005552 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5553 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02005554 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005555 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005556 }
5557}
5558
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005559/* Called from the upper layer, to send data from buffer <buf> for no more than
5560 * <count> bytes. Returns the number of bytes effectively sent. Some status
5561 * flags may be updated on the conn_stream.
5562 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005563static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005564{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005565 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005566 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005567 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005568 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005569 struct htx *htx;
5570 struct htx_blk *blk;
5571 enum htx_blk_type btype;
5572 uint32_t bsize;
5573 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005574
Olivier Houchardd360ac62019-03-22 17:37:16 +01005575 /* If we were not just woken because we wanted to send but couldn't,
5576 * and there's somebody else that is waiting to send, do nothing,
5577 * we will subscribe later and be put at the end of the list
5578 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005579 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005580 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5581 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005582 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005583
Olivier Houchard998410a2019-04-15 19:23:37 +02005584 /* We couldn't set it to NULL before, because we needed it in case
5585 * we had to cancel the tasklet
5586 */
5587 h2s->send_wait = NULL;
5588
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005589 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5590 return 0;
5591
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005592 /* htx will be enough to decide if we're using HTX or legacy */
5593 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5594
Willy Tarreau0bad0432018-06-14 16:54:01 +02005595 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005596 h2s->flags |= H2_SF_OUTGOING_DATA;
5597
Willy Tarreau751f2d02018-10-05 09:35:00 +02005598 if (h2s->id == 0) {
5599 int32_t id = h2c_get_next_sid(h2s->h2c);
5600
5601 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005602 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005603 return 0;
5604 }
5605
5606 eb32_delete(&h2s->by_id);
5607 h2s->by_id.key = h2s->id = id;
5608 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005609 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005610 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5611 }
5612
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005613 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005614 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005615 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005616 idx = htx_get_head(htx);
5617 blk = htx_get_blk(htx, idx);
5618 btype = htx_get_blk_type(blk);
5619 bsize = htx_get_blksz(blk);
5620
5621 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005622 case HTX_BLK_REQ_SL:
5623 /* start-line before headers */
5624 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5625 if (ret > 0) {
5626 total += ret;
5627 count -= ret;
5628 if (ret < bsize)
5629 goto done;
5630 }
5631 break;
5632
Willy Tarreau115e83b2018-12-01 19:17:53 +01005633 case HTX_BLK_RES_SL:
5634 /* start-line before headers */
5635 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5636 if (ret > 0) {
5637 total += ret;
5638 count -= ret;
5639 if (ret < bsize)
5640 goto done;
5641 }
5642 break;
5643
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005644 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005645 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005646 /* all these cause the emission of a DATA frame (possibly empty).
5647 * This EOM necessarily is one before trailers, as the EOM following
5648 * trailers would have been consumed by the trailers parser.
5649 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005650 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005651 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005652 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005653 total += ret;
5654 count -= ret;
5655 if (ret < bsize)
5656 goto done;
5657 }
5658 break;
5659
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005660 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005661 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005662 /* This is the first trailers block, all the subsequent ones AND
5663 * the EOM will be swallowed by the parser.
5664 */
5665 ret = h2s_htx_make_trailers(h2s, htx);
5666 if (ret > 0) {
5667 total += ret;
5668 count -= ret;
5669 if (ret < bsize)
5670 goto done;
5671 }
5672 break;
5673
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005674 default:
5675 htx_remove_blk(htx, blk);
5676 total += bsize;
5677 count -= bsize;
5678 break;
5679 }
5680 }
5681 goto done;
5682 }
5683
5684 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005685 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005686 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005687 if (h2s->h2c->flags & H2_CF_IS_BACK)
5688 ret = -1;
5689 else
5690 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005691 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005692 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005693 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005694 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005695 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005696 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005697 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005698
Willy Tarreau5dd17352018-06-14 13:33:30 +02005699 if (unlikely((int)ret <= 0)) {
5700 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005701 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5702 break;
5703 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005704 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005705 total += count;
5706 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005707 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005708 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005709 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005710 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005711 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005712 break;
5713 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005714
5715 total += ret;
5716 count -= ret;
5717
Willy Tarreau2b778482019-05-06 15:00:22 +02005718 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005719 break;
5720
5721 if (h2s->flags & H2_SF_BLK_ANY)
5722 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005723 }
5724
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005725 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005726 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005727 /* trim any possibly pending data after we close (extra CR-LF,
5728 * unprocessed trailers, abnormal extra data, ...)
5729 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005730 total += count;
5731 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005732 }
5733
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005734 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005735 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005736 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005737 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005738 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005739 }
5740
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005741 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005742 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005743 } else {
5744 b_del(buf, total);
5745 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005746
5747 /* The mux is full, cancel the pending tasks */
5748 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5749 (h2s->flags & H2_SF_BLK_MBUSY))
5750 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005751
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005752 /* If we're running HTX, and we read the whole buffer, then pretend
5753 * we read exactly what the caller specified, as with HTX the caller
5754 * will always give the buffer size, instead of the amount of data
5755 * available.
5756 */
5757 if (htx && !b_data(buf))
5758 total = orig_count;
5759
Olivier Houchard7505f942018-08-21 18:10:44 +02005760 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005761 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005762 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02005763
Olivier Houchard7505f942018-08-21 18:10:44 +02005764 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005765 /* If we're waiting for flow control, and we got a shutr on the
5766 * connection, we will never be unlocked, so add an error on
5767 * the conn_stream.
5768 */
5769 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5770 !b_data(&h2s->h2c->dbuf) &&
5771 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5772 if (cs->flags & CS_FL_EOS)
5773 cs->flags |= CS_FL_ERROR;
5774 else
5775 cs->flags |= CS_FL_ERR_PENDING;
5776 }
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005777
5778 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL)) {
5779 /* Ok we managed to send something, leave the send_list if we were still there */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005780 LIST_DEL_INIT(&h2s->list);
5781 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005782 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005783}
5784
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005785/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005786static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005787{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005788 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005789 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005790 struct eb32_node *node;
5791 int fctl_cnt = 0;
5792 int send_cnt = 0;
5793 int tree_cnt = 0;
5794 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005795 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005796
5797 if (!h2c)
5798 return;
5799
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005800 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005801 fctl_cnt++;
5802
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005803 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005804 send_cnt++;
5805
Willy Tarreau3af37712018-12-18 14:34:41 +01005806 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005807 node = eb32_first(&h2c->streams_by_id);
5808 while (node) {
5809 h2s = container_of(node, struct h2s, by_id);
5810 tree_cnt++;
5811 if (!h2s->cs)
5812 orph_cnt++;
5813 node = eb32_next(node);
5814 }
5815
Willy Tarreau60f62682019-05-26 11:32:27 +02005816 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005817 tmbuf = br_tail(h2c->mbuf);
Willy Tarreau987c0632018-12-18 10:32:05 +01005818 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5819 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005820 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5821 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau616ac812018-07-24 14:12:42 +02005822 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5823 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005824 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005825 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5826 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5827 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005828 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5829 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5830 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005831 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5832 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005833
5834 if (h2s) {
5835 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5836 h2s, h2s->id, h2s->flags,
5837 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5838 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5839 h2s->cs);
5840 if (h2s->cs)
5841 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5842 h2s->cs->flags, h2s->cs->data);
5843 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005844}
Willy Tarreau62f52692017-10-08 23:01:42 +02005845
5846/*******************************************************/
5847/* functions below are dedicated to the config parsers */
5848/*******************************************************/
5849
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005850/* config parser for global "tune.h2.header-table-size" */
5851static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5852 struct proxy *defpx, const char *file, int line,
5853 char **err)
5854{
5855 if (too_many_args(1, args, err, NULL))
5856 return -1;
5857
5858 h2_settings_header_table_size = atoi(args[1]);
5859 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5860 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5861 return -1;
5862 }
5863 return 0;
5864}
Willy Tarreau62f52692017-10-08 23:01:42 +02005865
Willy Tarreaue6baec02017-07-27 11:45:11 +02005866/* config parser for global "tune.h2.initial-window-size" */
5867static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5868 struct proxy *defpx, const char *file, int line,
5869 char **err)
5870{
5871 if (too_many_args(1, args, err, NULL))
5872 return -1;
5873
5874 h2_settings_initial_window_size = atoi(args[1]);
5875 if (h2_settings_initial_window_size < 0) {
5876 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5877 return -1;
5878 }
5879 return 0;
5880}
5881
Willy Tarreau5242ef82017-07-27 11:47:28 +02005882/* config parser for global "tune.h2.max-concurrent-streams" */
5883static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5884 struct proxy *defpx, const char *file, int line,
5885 char **err)
5886{
5887 if (too_many_args(1, args, err, NULL))
5888 return -1;
5889
5890 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005891 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005892 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5893 return -1;
5894 }
5895 return 0;
5896}
5897
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005898/* config parser for global "tune.h2.max-frame-size" */
5899static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5900 struct proxy *defpx, const char *file, int line,
5901 char **err)
5902{
5903 if (too_many_args(1, args, err, NULL))
5904 return -1;
5905
5906 h2_settings_max_frame_size = atoi(args[1]);
5907 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5908 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5909 return -1;
5910 }
5911 return 0;
5912}
5913
Willy Tarreau62f52692017-10-08 23:01:42 +02005914
5915/****************************************/
5916/* MUX initialization and instanciation */
5917/***************************************/
5918
5919/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005920static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005921 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005922 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005923 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005924 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005925 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005926 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005927 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005928 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005929 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005930 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005931 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005932 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005933 .shutr = h2_shutr,
5934 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005935 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005936 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005937 .name = "H2",
5938};
5939
Christopher Faulet32f61c02018-04-10 14:33:41 +02005940/* PROTO selection : this mux registers PROTO token "h2" */
5941static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005942 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005943
Willy Tarreau0108d902018-11-25 19:14:37 +01005944INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5945
Christopher Faulet9b579102019-04-11 22:52:25 +02005946
5947/* The mux operations */
5948static const struct mux_ops h2_htx_ops = {
5949 .init = h2_init,
5950 .wake = h2_wake,
5951 .snd_buf = h2_snd_buf,
5952 .rcv_buf = h2_rcv_buf,
5953 .subscribe = h2_subscribe,
5954 .unsubscribe = h2_unsubscribe,
5955 .attach = h2_attach,
5956 .get_first_cs = h2_get_first_cs,
5957 .detach = h2_detach,
5958 .destroy = h2_destroy,
5959 .avail_streams = h2_avail_streams,
5960 .used_streams = h2_used_streams,
5961 .shutr = h2_shutr,
5962 .shutw = h2_shutw,
5963 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005964 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005965 .name = "H2",
5966};
5967
Willy Tarreauf8957272018-10-03 10:25:20 +02005968static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005969 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005970
5971INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5972
Willy Tarreau62f52692017-10-08 23:01:42 +02005973/* config keyword parsers */
5974static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005975 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005976 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005977 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005978 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005979 { 0, NULL, NULL }
5980}};
5981
Willy Tarreau0108d902018-11-25 19:14:37 +01005982INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);