blob: 5cdd43a95e08e15269e47ae9f1d4835e7b876156 [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
Christopher Faulet11b10532020-10-08 15:38:41 +0200306
307/* Detect a pending read0 for a H2 connection. It happens if a read0 is pending
308 * on the connection AND if there is no more data in the demux buffer. The
309 * function returns 1 to report a read0 or 0 otherwise.
310 */
311static int h2c_read0_pending(struct h2c *h2c)
312{
313 if (conn_xprt_read0_pending(h2c->conn) && !b_data(&h2c->dbuf))
314 return 1;
315 return 0;
316}
317
Willy Tarreau534d8f92019-10-01 10:12:00 +0200318/* returns true if the connection is allowed to expire, false otherwise. A
319 * connection may expire when:
320 * - it has no stream
321 * - it has data in the mux buffer
322 * - it has streams in the blocked list
323 * - it has streams in the fctl list
324 * - it has streams in the send list
325 * Otherwise it means some streams are waiting in the data layer and it should
326 * not expire.
327 */
328static inline int h2c_may_expire(const struct h2c *h2c)
329{
330 return eb_is_empty(&h2c->streams_by_id) ||
331 br_data(h2c->mbuf) ||
332 !LIST_ISEMPTY(&h2c->blocked_list) ||
333 !LIST_ISEMPTY(&h2c->fctl_list) ||
334 !LIST_ISEMPTY(&h2c->send_list) ||
335 !LIST_ISEMPTY(&h2c->sending_list);
336}
337
Olivier Houchard7a977432019-03-21 15:47:13 +0100338static __inline int
Willy Tarreau534d8f92019-10-01 10:12:00 +0200339h2c_is_dead(const struct h2c *h2c)
Olivier Houchard7a977432019-03-21 15:47:13 +0100340{
341 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
342 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
343 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
344 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200345 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100346 (conn_xprt_read0_pending(h2c->conn) ||
347 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
348 return 1;
349
350 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100351}
352
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200353/*****************************************************/
354/* functions below are for dynamic buffer management */
355/*****************************************************/
356
Willy Tarreau315d8072017-12-10 22:17:57 +0100357/* indicates whether or not the we may call the h2_recv() function to attempt
358 * to receive data into the buffer and/or demux pending data. The condition is
359 * a bit complex due to some API limits for now. The rules are the following :
360 * - if an error or a shutdown was detected on the connection and the buffer
361 * is empty, we must not attempt to receive
362 * - if the demux buf failed to be allocated, we must not try to receive and
363 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100364 * - if no flag indicates a blocking condition, we may attempt to receive,
365 * regardless of whether the demux buffer is full or not, so that only
366 * de demux part decides whether or not to block. This is needed because
367 * the connection API indeed prevents us from re-enabling receipt that is
368 * already enabled in a polled state, so we must always immediately stop
369 * as soon as the demux can't proceed so as never to hit an end of read
370 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100371 * - otherwise must may not attempt
372 */
373static inline int h2_recv_allowed(const struct h2c *h2c)
374{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200375 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100376 (h2c->st0 >= H2_CS_ERROR ||
377 h2c->conn->flags & CO_FL_ERROR ||
378 conn_xprt_read0_pending(h2c->conn)))
379 return 0;
380
381 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100382 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100383 return 1;
384
385 return 0;
386}
387
Willy Tarreau47b515a2018-12-21 16:09:41 +0100388/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200389static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100390{
391 if (!h2_recv_allowed(h2c))
392 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200393 if ((!consider_buffer || !b_data(&h2c->dbuf))
394 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100395 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200396 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100397}
398
399
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100400/* returns true if the front connection has too many conn_streams attached */
401static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200402{
Willy Tarreaua8754662018-12-23 20:43:58 +0100403 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200404}
405
Willy Tarreau44e973f2018-03-01 17:49:30 +0100406/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
407 * flags are used to figure what buffer was requested. It returns 1 if the
408 * allocation succeeds, in which case the connection is woken up, or 0 if it's
409 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200410 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100411static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200412{
413 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100414 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200415
Willy Tarreau44e973f2018-03-01 17:49:30 +0100416 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200417 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200418 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200419 return 1;
420 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200421
Willy Tarreau51330962019-05-26 09:38:07 +0200422 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100423 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200424
425 if (h2c->flags & H2_CF_DEM_MROOM) {
426 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200427 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200428 }
Willy Tarreau14398122017-09-22 14:26:04 +0200429 return 1;
430 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100431
432 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
433 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200434 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100435 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200436 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100437 return 1;
438 }
439
Willy Tarreau14398122017-09-22 14:26:04 +0200440 return 0;
441}
442
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200443static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200444{
445 struct buffer *buf = NULL;
446
Willy Tarreauc234ae32019-05-13 17:56:11 +0200447 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100448 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
449 h2c->buf_wait.target = h2c;
450 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100451 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100452 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100453 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200454 __conn_xprt_stop_recv(h2c->conn);
455 }
456 return buf;
457}
458
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200459static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200460{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200461 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100462 b_free(bptr);
Willy Tarreau201840a2019-05-29 17:50:48 +0200463 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200464 }
465}
466
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200467static inline void h2_release_mbuf(struct h2c *h2c)
468{
469 struct buffer *buf;
470 unsigned int count = 0;
471
472 while (b_size(buf = br_head_pick(h2c->mbuf))) {
473 b_free(buf);
474 count++;
475 }
476 if (count)
Willy Tarreau201840a2019-05-29 17:50:48 +0200477 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200478}
479
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100480/* returns the number of allocatable outgoing streams for the connection taking
481 * the last_sid and the reserved ones into account.
482 */
483static inline int h2_streams_left(const struct h2c *h2c)
484{
485 int ret;
486
487 /* consider the number of outgoing streams we're allowed to create before
488 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
489 * nb_reserved is the number of streams which don't yet have an ID.
490 */
491 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
492 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
493 if (ret < 0)
494 ret = 0;
495 return ret;
496}
497
Willy Tarreau00f18a32019-01-26 12:19:01 +0100498/* returns the number of streams in use on a connection to figure if it's
499 * idle or not. We check nb_cs and not nb_streams as the caller will want
500 * to know if it was the last one after a detach().
501 */
502static int h2_used_streams(struct connection *conn)
503{
504 struct h2c *h2c = conn->ctx;
505
506 return h2c->nb_cs;
507}
508
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100509/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100510static int h2_avail_streams(struct connection *conn)
511{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100512 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100513 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100514 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100515
Willy Tarreau6afec462019-01-28 06:40:19 +0100516 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
517 * streams on the connection.
518 */
519 if (h2c->last_sid >= 0)
520 return 0;
521
Willy Tarreaud63b85d2019-10-31 15:10:03 +0100522 if (h2c->st0 >= H2_CS_ERROR)
523 return 0;
524
Willy Tarreau86949782019-01-31 10:42:05 +0100525 /* note: may be negative if a SETTINGS frame changes the limit */
526 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100527
528 /* we must also consider the limit imposed by stream IDs */
529 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100530 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100531 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100532 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
533 ret1 = MIN(ret1, ret2);
534 }
535 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100536}
537
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200538
Willy Tarreau62f52692017-10-08 23:01:42 +0200539/*****************************************************************/
540/* functions below are dedicated to the mux setup and management */
541/*****************************************************************/
542
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200543/* Initialize the mux once it's attached. For outgoing connections, the context
544 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200545 * connections from the fact that the context is still NULL (even during mux
546 * upgrades). <input> is always used as Input buffer and may contain data. It is
547 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200548 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200549static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
550 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200551{
552 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100553 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200554
Willy Tarreaubafbe012017-11-24 17:34:44 +0100555 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200556 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200557 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200558
Christopher Faulete9b70722019-04-08 10:46:02 +0200559 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200560 h2c->flags = H2_CF_IS_BACK;
561 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
562 if (tick_isset(prx->timeout.serverfin))
563 h2c->shut_timeout = prx->timeout.serverfin;
564 } else {
565 h2c->flags = H2_CF_NONE;
566 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
567 if (tick_isset(prx->timeout.clientfin))
568 h2c->shut_timeout = prx->timeout.clientfin;
569 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100570
Willy Tarreau0b37d652018-10-03 10:33:02 +0200571 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100572 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100573 if (tick_isset(h2c->timeout)) {
574 t = task_new(tid_bit);
575 if (!t)
576 goto fail;
577
578 h2c->task = t;
579 t->process = h2_timeout_task;
580 t->context = h2c;
581 t->expire = tick_add(now_ms, h2c->timeout);
582 }
Willy Tarreauea392822017-10-31 10:02:25 +0100583
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200584 h2c->wait_event.tasklet = tasklet_new();
585 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200586 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200587 h2c->wait_event.tasklet->process = h2_io_cb;
588 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100589 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200590
Willy Tarreau32218eb2017-09-22 08:07:25 +0200591 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
592 if (!h2c->ddht)
593 goto fail;
594
595 /* Initialise the context. */
596 h2c->st0 = H2_CS_PREFACE;
597 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100598 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200599 h2c->max_id = -1;
600 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100601 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200602 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100603 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200604 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100605 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100606 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200607
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200608 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200609 h2c->dsi = -1;
610 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100611
Willy Tarreau32218eb2017-09-22 08:07:25 +0200612 h2c->last_sid = -1;
613
Willy Tarreau51330962019-05-26 09:38:07 +0200614 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200615 h2c->miw = 65535; /* mux initial window size */
616 h2c->mws = 65535; /* mux window size */
617 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200618 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200619 LIST_INIT(&h2c->send_list);
620 LIST_INIT(&h2c->fctl_list);
Willy Tarreaubda92dd2019-10-02 10:49:59 +0200621 LIST_INIT(&h2c->blocked_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200622 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100623 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200624
Willy Tarreau3f133572017-10-31 19:21:06 +0100625 if (t)
626 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100627
Willy Tarreau01b44822018-10-03 14:26:37 +0200628 if (h2c->flags & H2_CF_IS_BACK) {
629 /* FIXME: this is temporary, for outgoing connections we need
630 * to immediately allocate a stream until the code is modified
631 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100632 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200633 */
634 struct h2s *h2s;
635
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100636 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200637 if (!h2s)
638 goto fail_stream;
639 }
640
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100641 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200642
Willy Tarreau0f383582018-10-03 14:22:21 +0200643 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200644 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200645 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200646 fail_stream:
647 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200648 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200649 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200650 if (h2c->wait_event.tasklet)
651 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100652 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200653 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200654 return -1;
655}
656
Willy Tarreau751f2d02018-10-05 09:35:00 +0200657/* returns the next allocatable outgoing stream ID for the H2 connection, or
658 * -1 if no more is allocatable.
659 */
660static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
661{
662 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100663
664 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200665 id = -1;
666 return id;
667}
668
Willy Tarreau2373acc2017-10-12 17:35:14 +0200669/* returns the stream associated with id <id> or NULL if not found */
670static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
671{
672 struct eb32_node *node;
673
Willy Tarreau751f2d02018-10-05 09:35:00 +0200674 if (id == 0)
675 return (struct h2s *)h2_closed_stream;
676
Willy Tarreau2a856182017-05-16 15:20:39 +0200677 if (id > h2c->max_id)
678 return (struct h2s *)h2_idle_stream;
679
Willy Tarreau2373acc2017-10-12 17:35:14 +0200680 node = eb32_lookup(&h2c->streams_by_id, id);
681 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200682 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200683
684 return container_of(node, struct h2s, by_id);
685}
686
Christopher Faulet73c12072019-04-08 11:23:22 +0200687/* release function. This one should be called to free all resources allocated
688 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200689 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200690static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200691{
Christopher Faulet61840e72019-04-15 09:33:32 +0200692 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200693
Willy Tarreau32218eb2017-09-22 08:07:25 +0200694 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200695 /* The connection must be aattached to this mux to be released */
696 if (h2c->conn && h2c->conn->ctx == h2c)
697 conn = h2c->conn;
698
Willy Tarreau32218eb2017-09-22 08:07:25 +0200699 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200700
Willy Tarreau186e96e2019-05-28 17:21:18 +0200701 if (LIST_ADDED(&h2c->buf_wait.list)) {
702 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
703 LIST_DEL(&h2c->buf_wait.list);
704 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
705 }
Willy Tarreau14398122017-09-22 14:26:04 +0200706
Willy Tarreau44e973f2018-03-01 17:49:30 +0100707 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200708 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100709
Willy Tarreauea392822017-10-31 10:02:25 +0100710 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200711 h2c->task->context = NULL;
712 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100713 h2c->task = NULL;
714 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200715 if (h2c->wait_event.tasklet)
716 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet0e012562019-09-18 11:07:20 +0200717 if (conn && h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100718 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Christopher Faulet0e012562019-09-18 11:07:20 +0200719 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100720
Willy Tarreaubafbe012017-11-24 17:34:44 +0100721 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200722 }
723
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200724 if (conn) {
725 conn->mux = NULL;
726 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200727
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200728 conn_stop_tracking(conn);
729 conn_full_close(conn);
730 if (conn->destroy_cb)
731 conn->destroy_cb(conn);
732 conn_free(conn);
733 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200734}
735
736
Willy Tarreau71681172017-10-23 14:39:06 +0200737/******************************************************/
738/* functions below are for the H2 protocol processing */
739/******************************************************/
740
741/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100742static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200743{
744 return h2s ? h2s->id : 0;
745}
746
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200747/* returns the sum of the stream's own window size and the mux's initial
748 * window, which together form the stream's effective window size.
749 */
750static inline int h2s_mws(const struct h2s *h2s)
751{
752 return h2s->sws + h2s->h2c->miw;
753}
754
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200755/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100756static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200757{
758 if (h2c->msi < 0)
759 return 0;
760
761 if (h2c->msi == h2s_id(h2s))
762 return 0;
763
764 return 1;
765}
766
Willy Tarreau741d6df2017-10-17 08:00:59 +0200767/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100768static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200769{
770 h2c->errcode = err;
771 h2c->st0 = H2_CS_ERROR;
772}
773
Willy Tarreau175cebb2019-01-24 10:02:24 +0100774/* marks an error on the stream. It may also update an already closed stream
775 * (e.g. to report an error after an RST was received).
776 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100777static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200778{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100779 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200780 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100781 if (h2s->st < H2_SS_ERROR)
782 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100783 if (h2s->cs)
784 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200785 }
786}
787
Willy Tarreau7e094452018-12-19 18:08:52 +0100788/* attempt to notify the data layer of recv availability */
789static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
790{
791 struct wait_event *sw;
792
793 if (h2s->recv_wait) {
794 sw = h2s->recv_wait;
795 sw->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200796 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100797 h2s->recv_wait = NULL;
798 }
799}
800
801/* attempt to notify the data layer of send availability */
802static void __maybe_unused h2s_notify_send(struct h2s *h2s)
803{
804 struct wait_event *sw;
805
Willy Tarreauc234ae32019-05-13 17:56:11 +0200806 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100807 sw = h2s->send_wait;
808 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100809 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200810 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100811 }
812}
813
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100814/* alerts the data layer, trying to wake it up by all means, following
815 * this sequence :
816 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
817 * - if its subscribed to send, then it's woken up for send
818 * - if it was subscribed to neither, its ->wake() callback is called
819 * It is safe to call this function with a closed stream which doesn't have a
820 * conn_stream anymore.
821 */
822static void __maybe_unused h2s_alert(struct h2s *h2s)
823{
824 if (h2s->recv_wait || h2s->send_wait) {
825 h2s_notify_recv(h2s);
826 h2s_notify_send(h2s);
827 }
828 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
829 h2s->cs->data_cb->wake(h2s->cs);
830}
831
Willy Tarreaue4820742017-07-27 13:37:23 +0200832/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100833static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200834{
835 uint8_t *out = frame;
836
837 *out = len >> 16;
838 write_n16(out + 1, len);
839}
840
Willy Tarreau54c15062017-10-10 17:10:03 +0200841/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
842 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
843 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200844 * available in the buffer's input prior to calling this function. The buffer
845 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200846 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100847static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200848 const struct buffer *b, int o)
849{
Willy Tarreau591d4452018-06-15 17:21:00 +0200850 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 +0200851}
852
Willy Tarreau1f094672017-11-20 21:27:45 +0100853static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200854{
Willy Tarreau591d4452018-06-15 17:21:00 +0200855 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200856}
857
Willy Tarreau1f094672017-11-20 21:27:45 +0100858static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200859{
Willy Tarreau591d4452018-06-15 17:21:00 +0200860 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200861}
862
Willy Tarreau1f094672017-11-20 21:27:45 +0100863static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200864{
Willy Tarreau591d4452018-06-15 17:21:00 +0200865 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200866}
867
868
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100869/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
870 * The algorithm is not obvious. It turns out that H2 headers are neither
871 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
872 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200873 *
874 * b0 b1 b2 b3 b4 b5..b8
875 * +----------+---------+--------+----+----+----------------------+
876 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
877 * +----------+---------+--------+----+----+----------------------+
878 *
879 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
880 * we get the sid properly aligned and ordered, and 16 bits of len properly
881 * ordered as well. The type and flags can be extracted using bit shifts from
882 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200883 * Returns zero if some bytes are missing, otherwise non-zero on success. The
884 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200885 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100886static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200887{
888 uint64_t w;
889
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100890 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200891 return 0;
892
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100893 w = h2_get_n64(b, o + 1);
894 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200895 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
896 h->ff = w >> 32;
897 h->ft = w >> 40;
898 h->len += w >> 48;
899 return 1;
900}
901
902/* skip the next 9 bytes corresponding to the frame header possibly parsed by
903 * h2_peek_frame_hdr() above.
904 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100905static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200906{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200907 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200908}
909
910/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100911static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200912{
913 int ret;
914
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100915 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200916 if (ret > 0)
917 h2_skip_frame_hdr(b);
918 return ret;
919}
920
Willy Tarreau00dd0782018-03-01 16:31:34 +0100921/* marks stream <h2s> as CLOSED and decrement the number of active streams for
922 * its connection if the stream was not yet closed. Please use this exclusively
923 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100924 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100925static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100926{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100927 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100928 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100929 if (!h2s->id)
930 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100931 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +0100932 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
933 h2s_notify_recv(h2s);
934 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100935 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100936 h2s->st = H2_SS_CLOSED;
937}
938
Willy Tarreau71049cc2018-03-28 13:56:39 +0200939/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
940static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100941{
942 h2s_close(h2s);
943 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200944 if (b_size(&h2s->rxbuf)) {
945 b_free(&h2s->rxbuf);
946 offer_buffers(NULL, tasks_run_queue);
947 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200948 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100949 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200950 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100951 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800952 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200953 * reference left would be in the h2c send_list/fctl_list, and if
954 * we're in it, we're getting out anyway
955 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100956 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200957 if (LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau86eded62019-06-14 14:47:49 +0200958 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +0200959 LIST_DEL_INIT(&h2s->sending_list);
960 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200961 tasklet_free(h2s->wait_event.tasklet);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100962 pool_free(pool_head_h2s, h2s);
963}
964
Willy Tarreaua8e49542018-10-03 18:53:55 +0200965/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
966 * stream tree. In case of error, nothing is added and NULL is returned. The
967 * causes of errors can be any failed memory allocation. The caller is
968 * responsible for checking if the connection may support an extra stream
969 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200970 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200971static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200972{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200973 struct h2s *h2s;
974
Willy Tarreaubafbe012017-11-24 17:34:44 +0100975 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200976 if (!h2s)
977 goto out;
978
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200979 h2s->wait_event.tasklet = tasklet_new();
980 if (!h2s->wait_event.tasklet) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200981 pool_free(pool_head_h2s, h2s);
982 goto out;
983 }
984 h2s->send_wait = NULL;
985 h2s->recv_wait = NULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200986 h2s->wait_event.tasklet->process = h2_deferred_shut;
987 h2s->wait_event.tasklet->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100988 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200989 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100990 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200991 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200992 h2s->cs = NULL;
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200993 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200994 h2s->flags = H2_SF_NONE;
995 h2s->errcode = H2_ERR_NO_ERROR;
996 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200997 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100998 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200999 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001000
1001 if (h2c->flags & H2_CF_IS_BACK) {
1002 h1m_init_req(&h2s->h1m);
1003 h2s->h1m.err_pos = -1; // don't care about errors on the request path
1004 h2s->h1m.flags |= H1_MF_TOLOWER;
1005 } else {
1006 h1m_init_res(&h2s->h1m);
1007 h2s->h1m.err_pos = -1; // don't care about errors on the response path
1008 h2s->h1m.flags |= H1_MF_TOLOWER;
1009 }
1010
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001011 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001012 if (id > 0)
1013 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001014 else
1015 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001016
1017 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001018 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001019 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001020
1021 return h2s;
1022
1023 out_free_h2s:
1024 pool_free(pool_head_h2s, h2s);
1025 out:
1026 return NULL;
1027}
1028
1029/* creates a new stream <id> on the h2c connection and returns it, or NULL in
1030 * case of memory allocation error.
1031 */
1032static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
1033{
1034 struct session *sess = h2c->conn->owner;
1035 struct conn_stream *cs;
1036 struct h2s *h2s;
1037
1038 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
1039 goto out;
1040
1041 h2s = h2s_new(h2c, id);
1042 if (!h2s)
1043 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001044
1045 cs = cs_new(h2c->conn);
1046 if (!cs)
1047 goto out_close;
1048
Olivier Houchard746fb772018-12-15 19:42:00 +01001049 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001050 h2s->cs = cs;
1051 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001052 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001053
1054 if (stream_create_from_cs(cs) < 0)
1055 goto out_free_cs;
1056
Willy Tarreau590a0512018-09-05 11:56:48 +02001057 /* We want the accept date presented to the next stream to be the one
1058 * we have now, the handshake time to be null (since the next stream
1059 * is not delayed by a handshake), and the idle time to count since
1060 * right now.
1061 */
1062 sess->accept_date = date;
1063 sess->tv_accept = now;
1064 sess->t_handshake = 0;
1065
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001066 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001067 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001068 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001069 return h2s;
1070
1071 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001072 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001073 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001074 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001075 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001076 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001077 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001078 sess_log(sess);
1079 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001080}
1081
Willy Tarreau751f2d02018-10-05 09:35:00 +02001082/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1083 * and returns it, or NULL in case of memory allocation error or if the highest
1084 * possible stream ID was reached.
1085 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001086static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001087{
1088 struct h2s *h2s = NULL;
1089
Willy Tarreau86949782019-01-31 10:42:05 +01001090 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001091 goto out;
1092
Willy Tarreaua80dca82019-01-24 17:08:28 +01001093 if (h2_streams_left(h2c) < 1)
1094 goto out;
1095
Willy Tarreau751f2d02018-10-05 09:35:00 +02001096 /* Defer choosing the ID until we send the first message to create the stream */
1097 h2s = h2s_new(h2c, 0);
1098 if (!h2s)
1099 goto out;
1100
1101 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001102 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001103 cs->ctx = h2s;
1104 h2c->nb_cs++;
1105
Willy Tarreau751f2d02018-10-05 09:35:00 +02001106 out:
1107 return h2s;
1108}
1109
Willy Tarreaube5b7152017-09-25 16:25:39 +02001110/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1111 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1112 * the various settings codes.
1113 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001114static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001115{
1116 struct buffer *res;
1117 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001118 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001119 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001120 int ret;
1121
1122 if (h2c_mux_busy(h2c, NULL)) {
1123 h2c->flags |= H2_CF_DEM_MBUSY;
1124 return 0;
1125 }
1126
Willy Tarreaube5b7152017-09-25 16:25:39 +02001127 chunk_init(&buf, buf_data, sizeof(buf_data));
1128 chunk_memcpy(&buf,
1129 "\x00\x00\x00" /* length : 0 for now */
1130 "\x04\x00" /* type : 4 (settings), flags : 0 */
1131 "\x00\x00\x00\x00", /* stream ID : 0 */
1132 9);
1133
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001134 if (h2c->flags & H2_CF_IS_BACK) {
1135 /* send settings_enable_push=0 */
1136 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1137 }
1138
Willy Tarreaube5b7152017-09-25 16:25:39 +02001139 if (h2_settings_header_table_size != 4096) {
1140 char str[6] = "\x00\x01"; /* header_table_size */
1141
1142 write_n32(str + 2, h2_settings_header_table_size);
1143 chunk_memcat(&buf, str, 6);
1144 }
1145
1146 if (h2_settings_initial_window_size != 65535) {
1147 char str[6] = "\x00\x04"; /* initial_window_size */
1148
1149 write_n32(str + 2, h2_settings_initial_window_size);
1150 chunk_memcat(&buf, str, 6);
1151 }
1152
1153 if (h2_settings_max_concurrent_streams != 0) {
1154 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1155
1156 /* Note: 0 means "unlimited" for haproxy's config but not for
1157 * the protocol, so never send this value!
1158 */
1159 write_n32(str + 2, h2_settings_max_concurrent_streams);
1160 chunk_memcat(&buf, str, 6);
1161 }
1162
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001163 mfs = h2_settings_max_frame_size;
1164 if (mfs > global.tune.bufsize)
1165 mfs = global.tune.bufsize;
1166
1167 if (!mfs)
1168 mfs = global.tune.bufsize;
1169
1170 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001171 char str[6] = "\x00\x05"; /* max_frame_size */
1172
1173 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1174 * match bufsize - rewrite size, but at the moment it seems
1175 * that clients don't take care of it.
1176 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001177 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001178 chunk_memcat(&buf, str, 6);
1179 }
1180
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001181 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001182
1183 res = br_tail(h2c->mbuf);
1184 retry:
1185 if (!h2_get_buf(h2c, res)) {
1186 h2c->flags |= H2_CF_MUX_MALLOC;
1187 h2c->flags |= H2_CF_DEM_MROOM;
1188 return 0;
1189 }
1190
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001191 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001192 if (unlikely(ret <= 0)) {
1193 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001194 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1195 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001196 h2c->flags |= H2_CF_MUX_MFULL;
1197 h2c->flags |= H2_CF_DEM_MROOM;
1198 return 0;
1199 }
1200 else {
1201 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1202 return 0;
1203 }
1204 }
1205 return ret;
1206}
1207
Willy Tarreau52eed752017-09-22 15:05:09 +02001208/* Try to receive a connection preface, then upon success try to send our
1209 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1210 * missing data. It may return an error in h2c.
1211 */
1212static int h2c_frt_recv_preface(struct h2c *h2c)
1213{
1214 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001215 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001216
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001217 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001218
1219 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001220 if (ret1 < 0)
1221 sess_log(h2c->conn->owner);
1222
Willy Tarreau52eed752017-09-22 15:05:09 +02001223 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1224 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1225 return 0;
1226 }
1227
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001228 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001229 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001230 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001231
Willy Tarreaube5b7152017-09-25 16:25:39 +02001232 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001233}
1234
Willy Tarreau01b44822018-10-03 14:26:37 +02001235/* Try to send a connection preface, then upon success try to send our
1236 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1237 * missing data. It may return an error in h2c.
1238 */
1239static int h2c_bck_send_preface(struct h2c *h2c)
1240{
1241 struct buffer *res;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001242 int ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001243
1244 if (h2c_mux_busy(h2c, NULL)) {
1245 h2c->flags |= H2_CF_DEM_MBUSY;
1246 return 0;
1247 }
1248
Willy Tarreaubcc45952019-05-26 10:05:50 +02001249 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001250 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001251 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001252 h2c->flags |= H2_CF_MUX_MALLOC;
1253 h2c->flags |= H2_CF_DEM_MROOM;
1254 return 0;
1255 }
1256
1257 if (!b_data(res)) {
1258 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001259 ret = b_istput(res, ist(H2_CONN_PREFACE));
1260 if (unlikely(ret <= 0)) {
1261 if (!ret) {
1262 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1263 goto retry;
1264 h2c->flags |= H2_CF_MUX_MFULL;
1265 h2c->flags |= H2_CF_DEM_MROOM;
1266 return 0;
1267 }
1268 else {
1269 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1270 return 0;
1271 }
1272 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001273 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001274 return h2c_send_settings(h2c);
1275}
1276
Willy Tarreau081d4722017-05-16 21:51:05 +02001277/* try to send a GOAWAY frame on the connection to report an error or a graceful
1278 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1279 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1280 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1281 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1282 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1283 * on unrecoverable failure. It will not attempt to send one again in this last
1284 * case so that it is safe to use h2c_error() to report such errors.
1285 */
1286static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1287{
1288 struct buffer *res;
1289 char str[17];
1290 int ret;
1291
1292 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1293 return 1; // claim that it worked
1294
1295 if (h2c_mux_busy(h2c, h2s)) {
1296 if (h2s)
1297 h2s->flags |= H2_SF_BLK_MBUSY;
1298 else
1299 h2c->flags |= H2_CF_DEM_MBUSY;
1300 return 0;
1301 }
1302
Willy Tarreau9c218e72019-05-26 10:08:28 +02001303 /* len: 8, type: 7, flags: none, sid: 0 */
1304 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1305
1306 if (h2c->last_sid < 0)
1307 h2c->last_sid = h2c->max_id;
1308
1309 write_n32(str + 9, h2c->last_sid);
1310 write_n32(str + 13, h2c->errcode);
1311
Willy Tarreaubcc45952019-05-26 10:05:50 +02001312 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001313 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001314 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001315 h2c->flags |= H2_CF_MUX_MALLOC;
1316 if (h2s)
1317 h2s->flags |= H2_SF_BLK_MROOM;
1318 else
1319 h2c->flags |= H2_CF_DEM_MROOM;
1320 return 0;
1321 }
1322
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001323 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001324 if (unlikely(ret <= 0)) {
1325 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001326 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1327 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001328 h2c->flags |= H2_CF_MUX_MFULL;
1329 if (h2s)
1330 h2s->flags |= H2_SF_BLK_MROOM;
1331 else
1332 h2c->flags |= H2_CF_DEM_MROOM;
1333 return 0;
1334 }
1335 else {
1336 /* we cannot report this error using GOAWAY, so we mark
1337 * it and claim a success.
1338 */
1339 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1340 h2c->flags |= H2_CF_GOAWAY_FAILED;
1341 return 1;
1342 }
1343 }
1344 h2c->flags |= H2_CF_GOAWAY_SENT;
1345 return ret;
1346}
1347
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001348/* Try to send an RST_STREAM frame on the connection for the indicated stream
1349 * during mux operations. This stream must be valid and cannot be closed
1350 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1351 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1352 * not yet.
1353 *
1354 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1355 * to write the message, it subscribes the stream to future notifications.
1356 */
1357static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1358{
1359 struct buffer *res;
1360 char str[13];
1361 int ret;
1362
1363 if (!h2s || h2s->st == H2_SS_CLOSED)
1364 return 1;
1365
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001366 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1367 * RST_STREAM in response to a RST_STREAM frame.
1368 */
Willy Tarreaubf9a20b2019-08-06 10:01:40 +02001369 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001370 ret = 1;
1371 goto ignore;
1372 }
1373
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001374 if (h2c_mux_busy(h2c, h2s)) {
1375 h2s->flags |= H2_SF_BLK_MBUSY;
1376 return 0;
1377 }
1378
Willy Tarreau9c218e72019-05-26 10:08:28 +02001379 /* len: 4, type: 3, flags: none */
1380 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1381 write_n32(str + 5, h2s->id);
1382 write_n32(str + 9, h2s->errcode);
1383
Willy Tarreaubcc45952019-05-26 10:05:50 +02001384 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001385 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001386 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001387 h2c->flags |= H2_CF_MUX_MALLOC;
1388 h2s->flags |= H2_SF_BLK_MROOM;
1389 return 0;
1390 }
1391
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001392 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001393 if (unlikely(ret <= 0)) {
1394 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001395 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1396 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001397 h2c->flags |= H2_CF_MUX_MFULL;
1398 h2s->flags |= H2_SF_BLK_MROOM;
1399 return 0;
1400 }
1401 else {
1402 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1403 return 0;
1404 }
1405 }
1406
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001407 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001408 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001409 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001410 return ret;
1411}
1412
1413/* Try to send an RST_STREAM frame on the connection for the stream being
1414 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001415 * error code, even if the stream is one of the dummy ones, and will update
1416 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001417 *
1418 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1419 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001420 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001421 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001422 */
1423static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1424{
1425 struct buffer *res;
1426 char str[13];
1427 int ret;
1428
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001429 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1430 * RST_STREAM in response to a RST_STREAM frame.
1431 */
1432 if (h2c->dft == H2_FT_RST_STREAM) {
1433 ret = 1;
1434 goto ignore;
1435 }
1436
Willy Tarreau27a84c92017-10-17 08:10:17 +02001437 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001438 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001439 return 0;
1440 }
1441
Willy Tarreau9c218e72019-05-26 10:08:28 +02001442 /* len: 4, type: 3, flags: none */
1443 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1444
1445 write_n32(str + 5, h2c->dsi);
1446 write_n32(str + 9, h2s->errcode);
1447
Willy Tarreaubcc45952019-05-26 10:05:50 +02001448 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001449 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001450 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001451 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001452 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001453 return 0;
1454 }
1455
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001456 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001457 if (unlikely(ret <= 0)) {
1458 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001459 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1460 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001461 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001462 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001463 return 0;
1464 }
1465 else {
1466 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1467 return 0;
1468 }
1469 }
1470
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001471 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001472 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001473 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001474 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001475 }
1476
Willy Tarreau27a84c92017-10-17 08:10:17 +02001477 return ret;
1478}
1479
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001480/* try to send an empty DATA frame with the ES flag set to notify about the
1481 * end of stream and match a shutdown(write). If an ES was already sent as
1482 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1483 * on success or zero if nothing was done. In case of lack of room to write the
1484 * message, it subscribes the requesting stream to future notifications.
1485 */
1486static int h2_send_empty_data_es(struct h2s *h2s)
1487{
1488 struct h2c *h2c = h2s->h2c;
1489 struct buffer *res;
1490 char str[9];
1491 int ret;
1492
Willy Tarreau721c9742017-11-07 11:05:42 +01001493 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001494 return 1;
1495
1496 if (h2c_mux_busy(h2c, h2s)) {
1497 h2s->flags |= H2_SF_BLK_MBUSY;
1498 return 0;
1499 }
1500
Willy Tarreau9c218e72019-05-26 10:08:28 +02001501 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1502 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1503 write_n32(str + 5, h2s->id);
1504
Willy Tarreaubcc45952019-05-26 10:05:50 +02001505 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001506 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001507 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001508 h2c->flags |= H2_CF_MUX_MALLOC;
1509 h2s->flags |= H2_SF_BLK_MROOM;
1510 return 0;
1511 }
1512
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001513 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001514 if (likely(ret > 0)) {
1515 h2s->flags |= H2_SF_ES_SENT;
1516 }
1517 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001518 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1519 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001520 h2c->flags |= H2_CF_MUX_MFULL;
1521 h2s->flags |= H2_SF_BLK_MROOM;
1522 return 0;
1523 }
1524 else {
1525 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1526 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001527 }
1528 return ret;
1529}
1530
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001531/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001532 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001533 * is automatically updated accordingly. If the stream is orphaned, it is
1534 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001535 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001536static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001537{
1538 if (!h2s->cs) {
1539 /* this stream was already orphaned */
1540 h2s_destroy(h2s);
1541 return;
1542 }
1543
Christopher Faulet11b10532020-10-08 15:38:41 +02001544 if (h2c_read0_pending(h2s->h2c)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001545 if (h2s->st == H2_SS_OPEN)
1546 h2s->st = H2_SS_HREM;
1547 else if (h2s->st == H2_SS_HLOC)
1548 h2s_close(h2s);
1549 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001550
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001551 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1552 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1553 h2s->cs->flags |= CS_FL_ERR_PENDING;
1554 if (h2s->cs->flags & CS_FL_EOS)
1555 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001556
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001557 if (h2s->st < H2_SS_ERROR)
1558 h2s->st = H2_SS_ERROR;
1559 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001560
1561 h2s_alert(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001562}
1563
1564/* wake the streams attached to the connection, whose id is greater than <last>
1565 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001566 */
Willy Tarreau23482912019-05-07 15:23:14 +02001567static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001568{
1569 struct eb32_node *node;
1570 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001571
Christopher Fauletf02ca002019-03-07 16:21:34 +01001572 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001573 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1574 while (node) {
1575 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001576 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001577 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001578 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001579
Christopher Fauletf02ca002019-03-07 16:21:34 +01001580 /* Wake all streams with unassigned ID (ID == 0) */
1581 node = eb32_lookup(&h2c->streams_by_id, 0);
1582 while (node) {
1583 h2s = container_of(node, struct h2s, by_id);
1584 if (h2s->id > 0)
1585 break;
1586 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001587 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001588 }
1589}
1590
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001591/* Wake up all blocked streams whose window size has become positive after the
1592 * mux's initial window was adjusted. This should be done after having processed
1593 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001594 */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001595static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001596{
1597 struct h2s *h2s;
1598 struct eb32_node *node;
1599
Willy Tarreau3421aba2017-07-27 15:41:03 +02001600 node = eb32_first(&h2c->streams_by_id);
1601 while (node) {
1602 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001603 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001604 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreaubda92dd2019-10-02 10:49:59 +02001605 LIST_DEL_INIT(&h2s->list);
1606 if (h2s->send_wait)
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001607 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001608 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001609 node = eb32_next(node);
1610 }
1611}
1612
1613/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1614 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001615 * return an error in h2c. The caller must have already verified frame length
1616 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001617 */
1618static int h2c_handle_settings(struct h2c *h2c)
1619{
1620 unsigned int offset;
1621 int error;
1622
1623 if (h2c->dff & H2_F_SETTINGS_ACK) {
1624 if (h2c->dfl) {
1625 error = H2_ERR_FRAME_SIZE_ERROR;
1626 goto fail;
1627 }
1628 return 1;
1629 }
1630
Willy Tarreau3421aba2017-07-27 15:41:03 +02001631 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001632 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001633 return 0;
1634
1635 /* parse the frame */
1636 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001637 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1638 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001639
1640 switch (type) {
1641 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1642 /* we need to update all existing streams with the
1643 * difference from the previous iws.
1644 */
1645 if (arg < 0) { // RFC7540#6.5.2
1646 error = H2_ERR_FLOW_CONTROL_ERROR;
1647 goto fail;
1648 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001649 h2c->miw = arg;
1650 break;
1651 case H2_SETTINGS_MAX_FRAME_SIZE:
1652 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1653 error = H2_ERR_PROTOCOL_ERROR;
1654 goto fail;
1655 }
1656 h2c->mfs = arg;
1657 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001658 case H2_SETTINGS_ENABLE_PUSH:
1659 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1660 error = H2_ERR_PROTOCOL_ERROR;
1661 goto fail;
1662 }
1663 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001664 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1665 if (h2c->flags & H2_CF_IS_BACK) {
1666 /* the limit is only for the backend; for the frontend it is our limit */
1667 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1668 arg = h2_settings_max_concurrent_streams;
1669 h2c->streams_limit = arg;
1670 }
1671 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001672 }
1673 }
1674
1675 /* need to ACK this frame now */
1676 h2c->st0 = H2_CS_FRAME_A;
1677 return 1;
1678 fail:
Willy Tarreau21178a52019-10-23 11:06:35 +02001679 if (!(h2c->flags & H2_CF_IS_BACK))
1680 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001681 h2c_error(h2c, error);
1682 return 0;
1683}
1684
1685/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1686 * success or one of the h2_status values.
1687 */
1688static int h2c_ack_settings(struct h2c *h2c)
1689{
1690 struct buffer *res;
1691 char str[9];
1692 int ret = -1;
1693
1694 if (h2c_mux_busy(h2c, NULL)) {
1695 h2c->flags |= H2_CF_DEM_MBUSY;
1696 return 0;
1697 }
1698
Willy Tarreau9c218e72019-05-26 10:08:28 +02001699 memcpy(str,
1700 "\x00\x00\x00" /* length : 0 (no data) */
1701 "\x04" "\x01" /* type : 4, flags : ACK */
1702 "\x00\x00\x00\x00" /* stream ID */, 9);
1703
Willy Tarreaubcc45952019-05-26 10:05:50 +02001704 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001705 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001706 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001707 h2c->flags |= H2_CF_MUX_MALLOC;
1708 h2c->flags |= H2_CF_DEM_MROOM;
1709 return 0;
1710 }
1711
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001712 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001713 if (unlikely(ret <= 0)) {
1714 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001715 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1716 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001717 h2c->flags |= H2_CF_MUX_MFULL;
1718 h2c->flags |= H2_CF_DEM_MROOM;
1719 return 0;
1720 }
1721 else {
1722 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1723 return 0;
1724 }
1725 }
1726 return ret;
1727}
1728
Willy Tarreaucf68c782017-10-10 17:11:41 +02001729/* processes a PING frame and schedules an ACK if needed. The caller must pass
1730 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001731 * missing data. The caller must have already verified frame length
1732 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001733 */
1734static int h2c_handle_ping(struct h2c *h2c)
1735{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001736 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001737 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001738 h2c->st0 = H2_CS_FRAME_A;
1739 return 1;
1740}
1741
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001742/* Try to send a window update for stream id <sid> and value <increment>.
1743 * Returns > 0 on success or zero on missing room or failure. It may return an
1744 * error in h2c.
1745 */
1746static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1747{
1748 struct buffer *res;
1749 char str[13];
1750 int ret = -1;
1751
1752 if (h2c_mux_busy(h2c, NULL)) {
1753 h2c->flags |= H2_CF_DEM_MBUSY;
1754 return 0;
1755 }
1756
Willy Tarreau9c218e72019-05-26 10:08:28 +02001757 /* length: 4, type: 8, flags: none */
1758 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1759 write_n32(str + 5, sid);
1760 write_n32(str + 9, increment);
1761
Willy Tarreaubcc45952019-05-26 10:05:50 +02001762 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001763 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001764 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001765 h2c->flags |= H2_CF_MUX_MALLOC;
1766 h2c->flags |= H2_CF_DEM_MROOM;
1767 return 0;
1768 }
1769
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001770 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001771 if (unlikely(ret <= 0)) {
1772 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001773 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1774 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001775 h2c->flags |= H2_CF_MUX_MFULL;
1776 h2c->flags |= H2_CF_DEM_MROOM;
1777 return 0;
1778 }
1779 else {
1780 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1781 return 0;
1782 }
1783 }
1784 return ret;
1785}
1786
1787/* try to send pending window update for the connection. It's safe to call it
1788 * with no pending updates. Returns > 0 on success or zero on missing room or
1789 * failure. It may return an error in h2c.
1790 */
1791static int h2c_send_conn_wu(struct h2c *h2c)
1792{
1793 int ret = 1;
1794
1795 if (h2c->rcvd_c <= 0)
1796 return 1;
1797
Willy Tarreau97aaa672018-12-23 09:49:04 +01001798 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1799 /* increase the advertised connection window to 2G on
1800 * first update.
1801 */
1802 h2c->flags |= H2_CF_WINDOW_OPENED;
1803 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1804 }
1805
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001806 /* send WU for the connection */
1807 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1808 if (ret > 0)
1809 h2c->rcvd_c = 0;
1810
1811 return ret;
1812}
1813
1814/* try to send pending window update for the current dmux stream. It's safe to
1815 * call it with no pending updates. Returns > 0 on success or zero on missing
1816 * room or failure. It may return an error in h2c.
1817 */
1818static int h2c_send_strm_wu(struct h2c *h2c)
1819{
1820 int ret = 1;
1821
1822 if (h2c->rcvd_s <= 0)
1823 return 1;
1824
1825 /* send WU for the stream */
1826 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1827 if (ret > 0)
1828 h2c->rcvd_s = 0;
1829
1830 return ret;
1831}
1832
Willy Tarreaucf68c782017-10-10 17:11:41 +02001833/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1834 * success, 0 on missing data or one of the h2_status values.
1835 */
1836static int h2c_ack_ping(struct h2c *h2c)
1837{
1838 struct buffer *res;
1839 char str[17];
1840 int ret = -1;
1841
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001842 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001843 return 0;
1844
1845 if (h2c_mux_busy(h2c, NULL)) {
1846 h2c->flags |= H2_CF_DEM_MBUSY;
1847 return 0;
1848 }
1849
Willy Tarreaucf68c782017-10-10 17:11:41 +02001850 memcpy(str,
1851 "\x00\x00\x08" /* length : 8 (same payload) */
1852 "\x06" "\x01" /* type : 6, flags : ACK */
1853 "\x00\x00\x00\x00" /* stream ID */, 9);
1854
1855 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001856 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001857
Willy Tarreau9c218e72019-05-26 10:08:28 +02001858 res = br_tail(h2c->mbuf);
1859 retry:
1860 if (!h2_get_buf(h2c, res)) {
1861 h2c->flags |= H2_CF_MUX_MALLOC;
1862 h2c->flags |= H2_CF_DEM_MROOM;
1863 return 0;
1864 }
1865
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001866 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001867 if (unlikely(ret <= 0)) {
1868 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001869 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1870 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02001871 h2c->flags |= H2_CF_MUX_MFULL;
1872 h2c->flags |= H2_CF_DEM_MROOM;
1873 return 0;
1874 }
1875 else {
1876 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1877 return 0;
1878 }
1879 }
1880 return ret;
1881}
1882
Willy Tarreau26f95952017-07-27 17:18:30 +02001883/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1884 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001885 * h2c or h2s. The caller must have already verified frame length and stream ID
1886 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001887 */
1888static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1889{
1890 int32_t inc;
1891 int error;
1892
Willy Tarreau26f95952017-07-27 17:18:30 +02001893 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001894 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001895 return 0;
1896
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001897 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001898
1899 if (h2c->dsi != 0) {
1900 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001901
1902 /* it's not an error to receive WU on a closed stream */
1903 if (h2s->st == H2_SS_CLOSED)
1904 return 1;
1905
1906 if (!inc) {
1907 error = H2_ERR_PROTOCOL_ERROR;
1908 goto strm_err;
1909 }
1910
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001911 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001912 error = H2_ERR_FLOW_CONTROL_ERROR;
1913 goto strm_err;
1914 }
1915
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001916 h2s->sws += inc;
1917 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001918 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreaubda92dd2019-10-02 10:49:59 +02001919 LIST_DEL_INIT(&h2s->list);
1920 if (h2s->send_wait)
Olivier Houcharddddfe312018-10-10 18:51:00 +02001921 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001922 }
1923 }
1924 else {
1925 /* connection window update */
1926 if (!inc) {
1927 error = H2_ERR_PROTOCOL_ERROR;
1928 goto conn_err;
1929 }
1930
1931 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1932 error = H2_ERR_FLOW_CONTROL_ERROR;
1933 goto conn_err;
1934 }
1935
1936 h2c->mws += inc;
1937 }
1938
1939 return 1;
1940
1941 conn_err:
1942 h2c_error(h2c, error);
1943 return 0;
1944
1945 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001946 h2s_error(h2s, error);
1947 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001948 return 0;
1949}
1950
Willy Tarreaue96b0922017-10-30 00:28:29 +01001951/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001952 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1953 * have already verified frame length and stream ID validity. Described in
1954 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001955 */
1956static int h2c_handle_goaway(struct h2c *h2c)
1957{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001958 int last;
1959
Willy Tarreaue96b0922017-10-30 00:28:29 +01001960 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001961 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001962 return 0;
1963
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001964 last = h2_get_n32(&h2c->dbuf, 0);
1965 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001966 if (h2c->last_sid < 0)
1967 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02001968 h2_wake_some_streams(h2c, last);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001969 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001970}
1971
Willy Tarreau92153fc2017-12-03 19:46:19 +01001972/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001973 * invalid. Returns > 0 on success or zero on missing data. It may return an
1974 * error in h2c. The caller must have already verified frame length and stream
1975 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001976 */
1977static int h2c_handle_priority(struct h2c *h2c)
1978{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001979 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001980 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001981 return 0;
1982
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001983 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001984 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001985 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1986 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001987 }
1988 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001989}
1990
Willy Tarreaucd234e92017-08-18 10:59:39 +02001991/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001992 * Returns > 0 on success or zero on missing data. The caller must have already
1993 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001994 */
1995static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1996{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001997 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001998 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001999 return 0;
2000
2001 /* late RST, already handled */
2002 if (h2s->st == H2_SS_CLOSED)
2003 return 1;
2004
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002005 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002006 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002007
2008 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01002009 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01002010 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002011 }
2012
2013 h2s->flags |= H2_SF_RST_RCVD;
2014 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002015}
2016
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002017/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2018 * It may return an error in h2c or h2s. The caller must consider that the
2019 * return value is the new h2s in case one was allocated (most common case).
2020 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002021 * errors here are reported as connection errors since it's impossible to
2022 * recover from such errors after the compression context has been altered.
2023 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002024static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002025{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002026 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002027 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002028 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002029 int error;
2030
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002031 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002032 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02002033
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002034 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002035 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02002036
2037 /* now either the frame is complete or the buffer is complete */
2038 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002039 /* The stream exists/existed, this must be a trailers frame */
2040 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02002041 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
2042 /* unrecoverable error ? */
2043 if (h2c->st0 >= H2_CS_ERROR)
2044 goto out;
2045
2046 if (error == 0)
2047 goto out; // missing data
2048
2049 if (error < 0) {
2050 /* Failed to decode this frame (e.g. too large request)
2051 * but the HPACK decompressor is still synchronized.
2052 */
2053 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2054 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002055 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002056 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002057 goto done;
2058 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002059 /* the connection was already killed by an RST, let's consume
2060 * the data and send another RST.
2061 */
2062 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2063 h2s = (struct h2s*)h2_error_stream;
2064 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002065 }
2066 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2067 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2068 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002069 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002070 goto conn_err;
2071 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002072 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2073 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002074
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002075 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002076
Willy Tarreau25919232019-01-03 14:48:18 +01002077 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002078 if (h2c->st0 >= H2_CS_ERROR)
2079 goto out;
2080
Willy Tarreau25919232019-01-03 14:48:18 +01002081 if (error <= 0) {
2082 if (error == 0)
2083 goto out; // missing data
2084
2085 /* Failed to decode this stream (e.g. too large request)
2086 * but the HPACK decompressor is still synchronized.
2087 */
2088 h2s = (struct h2s*)h2_error_stream;
2089 goto send_rst;
2090 }
2091
Willy Tarreau22de8d32018-09-05 19:55:58 +02002092 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002093 * positively from h2c_frt_stream_new(), the stream will report the error,
2094 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002095 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02002096 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02002097 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002098 h2s = (struct h2s*)h2_refused_stream;
2099 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002100 }
2101
2102 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002103 h2s->rxbuf = rxbuf;
2104 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002105 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002106
Willy Tarreau88d138e2019-01-02 19:38:14 +01002107 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002108 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002109 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002110
2111 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002112 if (h2s->st == H2_SS_OPEN)
2113 h2s->st = H2_SS_HREM;
2114 else
2115 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002116 }
2117
Willy Tarreau3a429f02019-01-03 11:41:50 +01002118 /* update the max stream ID if the request is being processed */
2119 if (h2s->id > h2c->max_id)
2120 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002121
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002122 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002123
2124 conn_err:
2125 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002126 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002127
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002128 out:
2129 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002130 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002131
2132 send_rst:
2133 /* make the demux send an RST for the current stream. We may only
2134 * do this if we're certain that the HEADERS frame was properly
2135 * decompressed so that the HPACK decoder is still kept up to date.
2136 */
2137 h2_release_buf(h2c, &rxbuf);
2138 h2c->st0 = H2_CS_FRAME_E;
2139 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002140}
2141
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002142/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2143 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2144 * errors here are reported as connection errors since it's impossible to
2145 * recover from such errors after the compression context has been altered.
2146 */
2147static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2148{
Christopher Faulet96b88f22019-09-23 15:28:20 +02002149 struct buffer rxbuf = BUF_NULL;
2150 unsigned long long body_len = 0;
2151 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002152 int error;
2153
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002154 if (!b_size(&h2c->dbuf))
2155 return NULL; // empty buffer
2156
2157 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2158 return NULL; // incomplete frame
2159
Christopher Faulet96b88f22019-09-23 15:28:20 +02002160 if (h2s->st != H2_SS_CLOSED) {
2161 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
2162 }
2163 else {
2164 /* the connection was already killed by an RST, let's consume
2165 * the data and send another RST.
2166 */
2167 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Christopher Faulet03427052019-09-26 16:19:13 +02002168 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet96b88f22019-09-23 15:28:20 +02002169 h2c->st0 = H2_CS_FRAME_E;
2170 goto send_rst;
2171 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002172
Willy Tarreau25919232019-01-03 14:48:18 +01002173 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002174 if (h2c->st0 >= H2_CS_ERROR)
2175 return NULL;
2176
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002177 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2178 /* RFC7540#5.1 */
2179 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2180 h2c->st0 = H2_CS_FRAME_E;
2181 return NULL;
2182 }
2183
Willy Tarreau25919232019-01-03 14:48:18 +01002184 if (error <= 0) {
2185 if (error == 0)
2186 return NULL; // missing data
2187
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002188 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002189 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002190 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002191 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002192 }
2193
Christopher Fauletfa922f02019-05-07 10:55:17 +02002194 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002195 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002196
Willy Tarreau927b88b2019-03-04 08:03:25 +01002197 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002198 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002199 else if (h2s->flags & H2_SF_ES_RCVD) {
2200 if (h2s->st == H2_SS_OPEN)
2201 h2s->st = H2_SS_HREM;
2202 else if (h2s->st == H2_SS_HLOC)
2203 h2s_close(h2s);
2204 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002205
2206 return h2s;
Christopher Faulet96b88f22019-09-23 15:28:20 +02002207
2208 send_rst:
2209 /* make the demux send an RST for the current stream. We may only
2210 * do this if we're certain that the HEADERS frame was properly
2211 * decompressed so that the HPACK decoder is still kept up to date.
2212 */
2213 h2_release_buf(h2c, &rxbuf);
2214 h2c->st0 = H2_CS_FRAME_E;
2215 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002216}
2217
Willy Tarreau454f9052017-10-26 19:40:35 +02002218/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2219 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2220 */
2221static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2222{
2223 int error;
2224
2225 /* note that empty DATA frames are perfectly valid and sometimes used
2226 * to signal an end of stream (with the ES flag).
2227 */
2228
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002229 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002230 return 0; // empty buffer
2231
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002232 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002233 return 0; // incomplete frame
2234
2235 /* now either the frame is complete or the buffer is complete */
2236
Willy Tarreau454f9052017-10-26 19:40:35 +02002237 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2238 /* RFC7540#6.1 */
2239 error = H2_ERR_STREAM_CLOSED;
2240 goto strm_err;
2241 }
2242
Christopher Faulet4fb65f42019-06-19 09:25:58 +02002243 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002244 /* RFC7540#8.1.2 */
2245 error = H2_ERR_PROTOCOL_ERROR;
2246 goto strm_err;
2247 }
2248
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002249 if (!h2_frt_transfer_data(h2s))
2250 return 0;
2251
Willy Tarreau454f9052017-10-26 19:40:35 +02002252 /* call the upper layers to process the frame, then let the upper layer
2253 * notify the stream about any change.
2254 */
2255 if (!h2s->cs) {
Willy Tarreauc1a29652019-08-06 10:11:02 +02002256 /* The upper layer has already closed, this may happen on
2257 * 4xx/redirects during POST, or when receiving a response
2258 * from an H2 server after the client has aborted.
2259 */
2260 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002261 goto strm_err;
2262 }
2263
Willy Tarreau8f650c32017-11-21 19:36:21 +01002264 if (h2c->st0 >= H2_CS_ERROR)
2265 return 0;
2266
Willy Tarreau721c9742017-11-07 11:05:42 +01002267 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002268 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002269 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002270 }
2271
2272 /* check for completion : the callee will change this to FRAME_A or
2273 * FRAME_H once done.
2274 */
2275 if (h2c->st0 == H2_CS_FRAME_P)
2276 return 0;
2277
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002278 /* last frame */
2279 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002280 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002281 if (h2s->st == H2_SS_OPEN)
2282 h2s->st = H2_SS_HREM;
2283 else
2284 h2s_close(h2s);
2285
Willy Tarreau1915ca22019-01-24 11:49:37 +01002286 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2287 /* RFC7540#8.1.2 */
2288 error = H2_ERR_PROTOCOL_ERROR;
2289 goto strm_err;
2290 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002291 }
2292
Willy Tarreau454f9052017-10-26 19:40:35 +02002293 return 1;
2294
Willy Tarreau454f9052017-10-26 19:40:35 +02002295 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002296 h2s_error(h2s, error);
2297 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002298 return 0;
2299}
2300
Willy Tarreaubc933932017-10-09 16:21:43 +02002301/* process Rx frames to be demultiplexed */
2302static void h2_process_demux(struct h2c *h2c)
2303{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002304 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002305 struct h2_fh hdr;
2306 unsigned int padlen = 0;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002307 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002308
Willy Tarreau081d4722017-05-16 21:51:05 +02002309 if (h2c->st0 >= H2_CS_ERROR)
2310 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002311
2312 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2313 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002314 if (h2c->flags & H2_CF_IS_BACK)
2315 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002316 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2317 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002318 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002319 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002320 sess_log(h2c->conn->owner);
2321 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002322 goto fail;
2323 }
2324
2325 h2c->max_id = 0;
2326 h2c->st0 = H2_CS_SETTINGS1;
2327 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002328
2329 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002330 /* ensure that what is pending is a valid SETTINGS frame
2331 * without an ACK.
2332 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002333 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002334 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002335 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002336 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002337 if (!(h2c->flags & H2_CF_IS_BACK))
2338 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002339 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002340 goto fail;
2341 }
2342
2343 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2344 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2345 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2346 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002347 if (!(h2c->flags & H2_CF_IS_BACK))
2348 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002349 goto fail;
2350 }
2351
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002352 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002353 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2354 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2355 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002356 if (!(h2c->flags & H2_CF_IS_BACK))
2357 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002358 goto fail;
2359 }
2360
Willy Tarreau3bf69182018-12-21 15:34:50 +01002361 /* that's OK, switch to FRAME_P to process it. This is
2362 * a SETTINGS frame whose header has already been
2363 * deleted above.
2364 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002365 padlen = 0;
2366 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002367 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002368 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002369
2370 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002371 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002372 int ret = 0;
2373
2374 if (h2c->st0 >= H2_CS_ERROR)
2375 break;
2376
2377 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreauab3ebbc2019-08-06 15:49:51 +02002378 h2c->rcvd_s = 0;
2379
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002380 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002381 break;
2382
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002383 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002384 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002385 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02002386 /* only log if no other stream can report the error */
2387 sess_log(h2c->conn->owner);
2388 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002389 break;
2390 }
2391
Christopher Faulet3d574a52019-06-18 12:22:38 +02002392 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002393 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2394 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2395 * we read the pad length and drop it from the remaining
2396 * payload (one byte + the 9 remaining ones = 10 total
2397 * removed), so we have a frame payload starting after the
2398 * pad len. Flow controlled frames (DATA) also count the
2399 * padlen in the flow control, so it must be adjusted.
2400 */
2401 if (hdr.len < 1) {
2402 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002403 if (!(h2c->flags & H2_CF_IS_BACK))
2404 sess_log(h2c->conn->owner);
Willy Tarreau3bf69182018-12-21 15:34:50 +01002405 goto fail;
2406 }
2407 hdr.len--;
2408
2409 if (b_data(&h2c->dbuf) < 10)
2410 break; // missing padlen
2411
2412 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2413
2414 if (padlen > hdr.len) {
2415 /* RFC7540#6.1 : pad length = length of
2416 * frame payload or greater => error.
2417 */
2418 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002419 if (!(h2c->flags & H2_CF_IS_BACK))
2420 sess_log(h2c->conn->owner);
Willy Tarreau3bf69182018-12-21 15:34:50 +01002421 goto fail;
2422 }
2423
2424 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2425 h2c->rcvd_c++;
2426 h2c->rcvd_s++;
2427 }
2428 b_del(&h2c->dbuf, 1);
2429 }
2430 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002431
2432 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002433 h2c->dfl = hdr.len;
2434 h2c->dsi = hdr.sid;
2435 h2c->dft = hdr.ft;
2436 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002437 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002438 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002439
2440 /* check for minimum basic frame format validity */
2441 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2442 if (ret != H2_ERR_NO_ERROR) {
2443 h2c_error(h2c, ret);
Willy Tarreau21178a52019-10-23 11:06:35 +02002444 if (!(h2c->flags & H2_CF_IS_BACK))
2445 sess_log(h2c->conn->owner);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002446 goto fail;
2447 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002448 }
2449
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002450 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
2451 * H2_CS_FRAME_P indicates an incomplete previous operation
2452 * (most often the first attempt) and requires some validity
2453 * checks for the frame and the current state. The two other
2454 * ones are set after completion (or abortion) and must skip
2455 * validity checks.
2456 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002457 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2458
Willy Tarreau567beb82018-12-18 16:52:44 +01002459 if (tmp_h2s != h2s && h2s && h2s->cs &&
2460 (b_data(&h2s->rxbuf) ||
Christopher Faulet11b10532020-10-08 15:38:41 +02002461 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002462 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002463 (h2s->flags & H2_SF_ES_RCVD) ||
2464 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002465 /* we may have to signal the upper layers */
2466 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002467 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002468 }
2469 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002470
Willy Tarreaud7901432017-12-29 11:34:40 +01002471 if (h2c->st0 == H2_CS_FRAME_E)
2472 goto strm_err;
2473
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002474 if (h2c->st0 == H2_CS_FRAME_A)
2475 goto valid_frame_type;
2476
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002477 if (h2s->st == H2_SS_IDLE &&
2478 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2479 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2480 * this state MUST be treated as a connection error
2481 */
2482 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002483 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02002484 /* only log if no other stream can report the error */
2485 sess_log(h2c->conn->owner);
2486 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002487 break;
2488 }
2489
Willy Tarreau85ee6e82019-11-24 14:57:53 +01002490 if (h2s->st == H2_SS_IDLE && (h2c->flags & H2_CF_IS_BACK)) {
2491 /* only PUSH_PROMISE would be permitted here */
2492 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2493 break;
2494 }
2495
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002496 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2497 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2498 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002499 * this state MUST be treated as a stream error.
2500 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2501 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002502 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002503 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2504 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2505 else
2506 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002507 goto strm_err;
2508 }
2509
Willy Tarreauab837502017-12-27 15:07:30 +01002510 /* Below the management of frames received in closed state is a
2511 * bit hackish because the spec makes strong differences between
2512 * streams closed by receiving RST, sending RST, and seeing ES
2513 * in both directions. In addition to this, the creation of a
2514 * new stream reusing the identifier of a closed one will be
2515 * detected here. Given that we cannot keep track of all closed
2516 * streams forever, we consider that unknown closed streams were
2517 * closed on RST received, which allows us to respond with an
2518 * RST without breaking the connection (eg: to abort a transfer).
2519 * Some frames have to be silently ignored as well.
2520 */
2521 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002522 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002523 /* #5.1.1: The identifier of a newly
2524 * established stream MUST be numerically
2525 * greater than all streams that the initiating
2526 * endpoint has opened or reserved. This
2527 * governs streams that are opened using a
2528 * HEADERS frame and streams that are reserved
2529 * using PUSH_PROMISE. An endpoint that
2530 * receives an unexpected stream identifier
2531 * MUST respond with a connection error.
2532 */
2533 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2534 goto strm_err;
2535 }
2536
Willy Tarreaueb5be9d2019-09-26 08:47:15 +02002537 if (h2s->flags & H2_SF_RST_RCVD &&
2538 !(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 +01002539 /* RFC7540#5.1:closed: an endpoint that
2540 * receives any frame other than PRIORITY after
2541 * receiving a RST_STREAM MUST treat that as a
2542 * stream error of type STREAM_CLOSED.
2543 *
2544 * Note that old streams fall into this category
2545 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002546 *
2547 * However, we cannot generalize this to all frame types. Those
2548 * carrying compression state must still be processed before
2549 * being dropped or we'll desynchronize the decoder. This can
2550 * happen with request trailers received after sending an
2551 * RST_STREAM, or with header/trailers responses received after
2552 * sending RST_STREAM (aborted stream).
Willy Tarreaueb5be9d2019-09-26 08:47:15 +02002553 *
2554 * In addition, since our CLOSED streams always carry the
2555 * RST_RCVD bit, we don't want to accidently catch valid
2556 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreauab837502017-12-27 15:07:30 +01002557 */
2558 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2559 h2c->st0 = H2_CS_FRAME_E;
2560 goto strm_err;
2561 }
2562
2563 /* RFC7540#5.1:closed: if this state is reached as a
2564 * result of sending a RST_STREAM frame, the peer that
2565 * receives the RST_STREAM might have already sent
2566 * frames on the stream that cannot be withdrawn. An
2567 * endpoint MUST ignore frames that it receives on
2568 * closed streams after it has sent a RST_STREAM
2569 * frame. An endpoint MAY choose to limit the period
2570 * over which it ignores frames and treat frames that
2571 * arrive after this time as being in error.
2572 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002573 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002574 /* RFC7540#5.1:closed: any frame other than
2575 * PRIO/WU/RST in this state MUST be treated as
2576 * a connection error
2577 */
2578 if (h2c->dft != H2_FT_RST_STREAM &&
2579 h2c->dft != H2_FT_PRIORITY &&
2580 h2c->dft != H2_FT_WINDOW_UPDATE) {
2581 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2582 goto strm_err;
2583 }
2584 }
2585 }
2586
Willy Tarreauc0da1962017-10-30 18:38:00 +01002587#if 0
2588 // problem below: it is not possible to completely ignore such
2589 // streams as we need to maintain the compression state as well
2590 // and for this we need to completely process these frames (eg:
2591 // HEADERS frames) as well as counting DATA frames to emit
2592 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2593 // This is a typical case of layer violation where the
2594 // transported contents are critical to the connection's
2595 // validity and must be ignored at the same time :-(
2596
2597 /* graceful shutdown, ignore streams whose ID is higher than
2598 * the one advertised in GOAWAY. RFC7540#6.8.
2599 */
2600 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002601 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2602 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002603 h2c->dfl -= ret;
2604 ret = h2c->dfl == 0;
2605 goto strm_err;
2606 }
2607#endif
2608
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002609 valid_frame_type:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002610 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002611 case H2_FT_SETTINGS:
2612 if (h2c->st0 == H2_CS_FRAME_P)
2613 ret = h2c_handle_settings(h2c);
2614
2615 if (h2c->st0 == H2_CS_FRAME_A)
2616 ret = h2c_ack_settings(h2c);
2617 break;
2618
Willy Tarreaucf68c782017-10-10 17:11:41 +02002619 case H2_FT_PING:
2620 if (h2c->st0 == H2_CS_FRAME_P)
2621 ret = h2c_handle_ping(h2c);
2622
2623 if (h2c->st0 == H2_CS_FRAME_A)
2624 ret = h2c_ack_ping(h2c);
2625 break;
2626
Willy Tarreau26f95952017-07-27 17:18:30 +02002627 case H2_FT_WINDOW_UPDATE:
2628 if (h2c->st0 == H2_CS_FRAME_P)
2629 ret = h2c_handle_window_update(h2c, h2s);
2630 break;
2631
Willy Tarreau61290ec2017-10-17 08:19:21 +02002632 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002633 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2634 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2635 * frames' parsers consume all following CONTINUATION
2636 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002637 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002638 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002639 if (!(h2c->flags & H2_CF_IS_BACK))
2640 sess_log(h2c->conn->owner);
Willy Tarreauea18f862018-12-22 20:19:26 +01002641 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002642
Willy Tarreau13278b42017-10-13 19:23:14 +02002643 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002644 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002645 if (h2c->flags & H2_CF_IS_BACK)
2646 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2647 else
2648 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002649 if (tmp_h2s) {
2650 h2s = tmp_h2s;
2651 ret = 1;
2652 }
2653 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002654 break;
2655
Willy Tarreau454f9052017-10-26 19:40:35 +02002656 case H2_FT_DATA:
2657 if (h2c->st0 == H2_CS_FRAME_P)
2658 ret = h2c_frt_handle_data(h2c, h2s);
2659
2660 if (h2c->st0 == H2_CS_FRAME_A)
2661 ret = h2c_send_strm_wu(h2c);
2662 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002663
Willy Tarreau92153fc2017-12-03 19:46:19 +01002664 case H2_FT_PRIORITY:
2665 if (h2c->st0 == H2_CS_FRAME_P)
2666 ret = h2c_handle_priority(h2c);
2667 break;
2668
Willy Tarreaucd234e92017-08-18 10:59:39 +02002669 case H2_FT_RST_STREAM:
2670 if (h2c->st0 == H2_CS_FRAME_P)
2671 ret = h2c_handle_rst_stream(h2c, h2s);
2672 break;
2673
Willy Tarreaue96b0922017-10-30 00:28:29 +01002674 case H2_FT_GOAWAY:
2675 if (h2c->st0 == H2_CS_FRAME_P)
2676 ret = h2c_handle_goaway(h2c);
2677 break;
2678
Willy Tarreau1c661982017-10-30 13:52:01 +01002679 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002680 default:
2681 /* drop frames that we ignore. They may be larger than
2682 * the buffer so we drain all of their contents until
2683 * we reach the end.
2684 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002685 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2686 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002687 h2c->dfl -= ret;
2688 ret = h2c->dfl == 0;
2689 }
2690
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002691 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002692 /* We may have to send an RST if not done yet */
2693 if (h2s->st == H2_SS_ERROR)
2694 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002695
Willy Tarreaua20a5192017-12-27 11:02:06 +01002696 if (h2c->st0 == H2_CS_FRAME_E)
2697 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002698
Willy Tarreau7e98c052017-10-10 15:56:59 +02002699 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002700 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002701 break;
2702
2703 if (h2c->st0 != H2_CS_FRAME_H) {
Christopher Faulete12a26f2019-09-26 16:38:28 +02002704 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2705 b_del(&h2c->dbuf, ret);
2706 h2c->dfl -= ret;
2707 if (!h2c->dfl)
2708 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002709 }
2710 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002711
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002712 if (h2c->rcvd_c > 0 &&
2713 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2714 h2c_send_conn_wu(h2c);
2715
Willy Tarreau52eed752017-09-22 15:05:09 +02002716 fail:
2717 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002718 if (h2s && h2s->cs &&
2719 (b_data(&h2s->rxbuf) ||
Christopher Faulet11b10532020-10-08 15:38:41 +02002720 h2c_read0_pending(h2c) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002721 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002722 (h2s->flags & H2_SF_ES_RCVD) ||
2723 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002724 /* we may have to signal the upper layers */
2725 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002726 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002727 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002728
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002729 if (old_iw != h2c->miw)
2730 h2c_unblock_sfctl(h2c);
2731
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002732 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002733}
2734
Willy Tarreaufa9a0402020-01-10 17:01:29 +01002735/* resume each h2s eligible for sending in list head <head> */
2736static void h2_resume_each_sending_h2s(struct h2c *h2c, struct list *head)
2737{
2738 struct h2s *h2s, *h2s_back;
2739
2740 list_for_each_entry_safe(h2s, h2s_back, head, list) {
2741 if (h2c->mws <= 0 ||
2742 h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2743 h2c->st0 >= H2_CS_ERROR)
2744 break;
2745
2746 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreaud4e72e22020-01-10 18:20:15 +01002747
2748 if (LIST_ADDED(&h2s->sending_list))
2749 continue;
2750
Willy Tarreaufa9a0402020-01-10 17:01:29 +01002751 /* For some reason, the upper layer failed to subscribe again,
2752 * so remove it from the send_list
2753 */
2754 if (!h2s->send_wait) {
2755 LIST_DEL_INIT(&h2s->list);
2756 continue;
2757 }
2758
2759 h2s->send_wait->events &= ~SUB_RETRY_SEND;
2760 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
2761 tasklet_wakeup(h2s->send_wait->tasklet);
2762 }
2763}
2764
Willy Tarreaubc933932017-10-09 16:21:43 +02002765/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2766 * the end.
2767 */
2768static int h2_process_mux(struct h2c *h2c)
2769{
Willy Tarreau01b44822018-10-03 14:26:37 +02002770 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2771 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2772 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2773 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau21178a52019-10-23 11:06:35 +02002774 if (h2c->st0 == H2_CS_ERROR)
Willy Tarreau01b44822018-10-03 14:26:37 +02002775 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau01b44822018-10-03 14:26:37 +02002776 goto fail;
2777 }
2778 h2c->st0 = H2_CS_SETTINGS1;
2779 }
2780 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002781 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002782 return 1;
2783 }
2784
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002785 /* start by sending possibly pending window updates */
Willy Tarreau9c497c22019-08-06 15:39:32 +02002786 if (h2c->rcvd_s > 0 &&
2787 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2788 h2c_send_strm_wu(h2c) < 0)
2789 goto fail;
2790
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002791 if (h2c->rcvd_c > 0 &&
2792 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2793 h2c_send_conn_wu(h2c) < 0)
2794 goto fail;
2795
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002796 /* First we always process the flow control list because the streams
2797 * waiting there were already elected for immediate emission but were
2798 * blocked just on this.
2799 */
Willy Tarreaufa9a0402020-01-10 17:01:29 +01002800 h2_resume_each_sending_h2s(h2c, &h2c->fctl_list);
2801 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002802
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002803 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002804 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002805 if (h2c->st0 == H2_CS_ERROR) {
2806 if (h2c->max_id >= 0) {
2807 h2c_send_goaway_error(h2c, NULL);
2808 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2809 return 0;
2810 }
2811
2812 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2813 }
2814 return 1;
2815 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002816 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002817}
2818
Willy Tarreau62f52692017-10-08 23:01:42 +02002819
Willy Tarreau479998a2018-11-18 06:30:59 +01002820/* Attempt to read data, and subscribe if none available.
2821 * The function returns 1 if data has been received, otherwise zero.
2822 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002823static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002824{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002825 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002826 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002827 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002828 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002829
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002830 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002831 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002832
Willy Tarreau315d8072017-12-10 22:17:57 +01002833 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002834 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002835
Willy Tarreau44e973f2018-03-01 17:49:30 +01002836 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002837 if (!buf) {
2838 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002839 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002840 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002841
Olivier Houchard7505f942018-08-21 18:10:44 +02002842 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002843 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002844 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2845 /* HTX in use : try to pre-align the buffer like the
2846 * rxbufs will be to optimize memory copies. We'll make
2847 * sure that the frame header lands at the end of the
2848 * HTX block to alias it upon recv. We cannot use the
2849 * head because rcv_buf() will realign the buffer if
2850 * it's empty. Thus we cheat and pretend we already
2851 * have a few bytes there.
2852 */
2853 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002854 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002855 }
2856 else
2857 max = b_room(buf);
2858
Olivier Houchard7505f942018-08-21 18:10:44 +02002859 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002860 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002861 else
2862 ret = 0;
2863 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002864
Willy Tarreaua8fcdac2019-08-02 07:48:47 +02002865 if (max && !ret && h2_recv_allowed(h2c))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002866 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002867
Olivier Houcharda1411e62018-08-17 18:42:48 +02002868 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002869 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002870 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002871 }
2872
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002873 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002874 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002875 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002876}
2877
Willy Tarreau479998a2018-11-18 06:30:59 +01002878/* Try to send data if possible.
2879 * The function returns 1 if data have been sent, otherwise zero.
2880 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002881static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002882{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002883 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002884 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002885 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002886
2887 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002888 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002889
Olivier Houchard7505f942018-08-21 18:10:44 +02002890
Willy Tarreaua2af5122017-10-09 11:56:46 +02002891 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2892 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002893 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002894 }
2895
Willy Tarreaubc933932017-10-09 16:21:43 +02002896 /* This loop is quite simple : it tries to fill as much as it can from
2897 * pending streams into the existing buffer until it's reportedly full
2898 * or the end of send requests is reached. Then it tries to send this
2899 * buffer's contents out, marks it not full if at least one byte could
2900 * be sent, and tries again.
2901 *
2902 * The snd_buf() function normally takes a "flags" argument which may
2903 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2904 * data immediately comes and CO_SFL_STREAMER to indicate that the
2905 * connection is streaming lots of data (used to increase TLS record
2906 * size at the expense of latency). The former can be sent any time
2907 * there's a buffer full flag, as it indicates at least one stream
2908 * attempted to send and failed so there are pending data. An
2909 * alternative would be to set it as long as there's an active stream
2910 * but that would be problematic for ACKs until we have an absolute
2911 * guarantee that all waiters have at least one byte to send. The
2912 * latter should possibly not be set for now.
2913 */
2914
2915 done = 0;
2916 while (!done) {
2917 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002918 unsigned int released = 0;
2919 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02002920
2921 /* fill as much as we can into the current buffer */
2922 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2923 done = h2_process_mux(h2c);
2924
Olivier Houchard2b094432019-01-29 18:28:36 +01002925 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002926 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01002927
Willy Tarreaubc933932017-10-09 16:21:43 +02002928 if (conn->flags & CO_FL_ERROR)
2929 break;
2930
2931 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2932 flags |= CO_SFL_MSG_MORE;
2933
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002934 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
2935 if (b_data(buf)) {
2936 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002937 if (!ret) {
2938 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002939 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002940 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002941 sent = 1;
2942 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002943 if (b_data(buf)) {
2944 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002945 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002946 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002947 }
2948 b_free(buf);
2949 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02002950 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002951
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002952 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02002953 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002954
Willy Tarreaubc933932017-10-09 16:21:43 +02002955 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet07423082019-10-24 10:31:01 +02002956 if (sent)
2957 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02002958 }
2959
Willy Tarreaua2af5122017-10-09 11:56:46 +02002960 if (conn->flags & CO_FL_SOCK_WR_SH) {
2961 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02002962 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002963 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002964 /* We're not full anymore, so we can wake any task that are waiting
2965 * for us.
2966 */
Willy Tarreaufa9a0402020-01-10 17:01:29 +01002967 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H)
2968 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Olivier Houchard998410a2019-04-15 19:23:37 +02002969
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002970 /* We're done, no more to send */
Willy Tarreau662fafc2019-05-26 09:43:07 +02002971 if (!br_data(h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002972 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002973schedule:
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002974 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002975 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002976
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002977 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002978}
2979
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002980/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002981static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2982{
2983 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002984 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002985
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002986 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002987 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002988 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002989 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002990 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002991 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002992 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002993}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002994
Willy Tarreau62f52692017-10-08 23:01:42 +02002995/* callback called on any event by the connection handler.
2996 * It applies changes and returns zero, or < 0 if it wants immediate
2997 * destruction of the connection (which normally doesn not happen in h2).
2998 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002999static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003000{
Olivier Houchard7505f942018-08-21 18:10:44 +02003001 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003002
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003003 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01003004 h2_process_demux(h2c);
3005
3006 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003007 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003008
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003009 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01003010 h2c->flags &= ~H2_CF_DEM_DFULL;
3011 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003012 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003013
Willy Tarreau0b37d652018-10-03 10:33:02 +02003014 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01003015 /* frontend is stopping, reload likely in progress, let's try
3016 * to announce a graceful shutdown if not yet done. We don't
3017 * care if it fails, it will be tried again later.
3018 */
3019 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3020 if (h2c->last_sid < 0)
3021 h2c->last_sid = (1U << 31) - 1;
3022 h2c_send_goaway_error(h2c, NULL);
3023 }
3024 }
3025
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003026 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003027 * If we received early data, and the handshake is done, wake
3028 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003029 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003030 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
3031 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
3032 struct eb32_node *node;
3033 struct h2s *h2s;
3034
3035 h2c->flags |= H2_CF_WAIT_FOR_HS;
3036 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
3037
3038 while (node) {
3039 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01003040 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01003041 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003042 node = eb32_next(node);
3043 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003044 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003045
Christopher Faulet11b10532020-10-08 15:38:41 +02003046 if (conn->flags & CO_FL_ERROR || h2c_read0_pending(h2c) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01003047 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
3048 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
3049 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02003050 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003051
3052 if (eb_is_empty(&h2c->streams_by_id)) {
3053 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003054 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003055 return -1;
3056 }
Willy Tarreauab66e152019-10-31 15:36:30 +01003057
3058 /* connections in error must be removed from the idle lists */
3059 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3060 LIST_DEL_LOCKED(&conn->list);
3061 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3062 }
3063 else if (h2c->st0 == H2_CS_ERROR) {
3064 /* connections in error must be removed from the idle lists */
3065 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3066 LIST_DEL_LOCKED(&conn->list);
3067 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003068 }
3069
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003070 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003071 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003072
Olivier Houchard53216e72018-10-10 15:46:36 +02003073 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3074 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3075 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003076 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003077 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3078 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003079 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003080
Willy Tarreau3f133572017-10-31 19:21:06 +01003081 if (h2c->task) {
Willy Tarreau534d8f92019-10-01 10:12:00 +02003082 if (h2c_may_expire(h2c))
3083 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3084 else
3085 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003086 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003087 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003088
Olivier Houchard7505f942018-08-21 18:10:44 +02003089 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02003090 return 0;
3091}
3092
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003093/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003094static int h2_wake(struct connection *conn)
3095{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003096 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003097
3098 return (h2_process(h2c));
3099}
3100
Willy Tarreauea392822017-10-31 10:02:25 +01003101/* Connection timeout management. The principle is that if there's no receipt
3102 * nor sending for a certain amount of time, the connection is closed. If the
3103 * MUX buffer still has lying data or is not allocatable, the connection is
3104 * immediately killed. If it's allocatable and empty, we attempt to send a
3105 * GOAWAY frame.
3106 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003107static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003108{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003109 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003110 int expired = tick_is_expired(t->expire, now_ms);
3111
Willy Tarreau0975f112018-03-29 15:22:59 +02003112 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01003113 return t;
3114
Willy Tarreau534d8f92019-10-01 10:12:00 +02003115 if (h2c && !h2c_may_expire(h2c)) {
3116 /* we do still have streams but all of them are idle, waiting
3117 * for the data layer, so we must not enforce the timeout here.
3118 */
3119 t->expire = TICK_ETERNITY;
3120 return t;
3121 }
3122
Olivier Houchard3f795f72019-04-17 22:51:06 +02003123 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003124
3125 if (!h2c) {
3126 /* resources were already deleted */
3127 return NULL;
3128 }
3129
3130 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003131 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003132 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003133
Willy Tarreau662fafc2019-05-26 09:43:07 +02003134 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003135 /* don't even try to send a GOAWAY, the buffer is stuck */
3136 h2c->flags |= H2_CF_GOAWAY_FAILED;
3137 }
3138
3139 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003140 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003141 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3142 h2c->flags |= H2_CF_GOAWAY_FAILED;
3143
Willy Tarreau662fafc2019-05-26 09:43:07 +02003144 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003145 unsigned int released = 0;
3146 struct buffer *buf;
3147
3148 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3149 if (b_data(buf)) {
3150 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3151 if (!ret)
3152 break;
3153 b_del(buf, ret);
3154 if (b_data(buf))
3155 break;
3156 b_free(buf);
3157 released++;
3158 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003159 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003160
3161 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003162 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003163 }
Willy Tarreauea392822017-10-31 10:02:25 +01003164
Willy Tarreauab66e152019-10-31 15:36:30 +01003165 /* in any case this connection must not be considered idle anymore */
3166 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3167 LIST_DEL_LOCKED(&h2c->conn->list);
3168 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3169
Willy Tarreau0975f112018-03-29 15:22:59 +02003170 /* either we can release everything now or it will be done later once
3171 * the last stream closes.
3172 */
3173 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003174 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003175
Willy Tarreauea392822017-10-31 10:02:25 +01003176 return NULL;
3177}
3178
3179
Willy Tarreau62f52692017-10-08 23:01:42 +02003180/*******************************************/
3181/* functions below are used by the streams */
3182/*******************************************/
3183
3184/*
3185 * Attach a new stream to a connection
3186 * (Used for outgoing connections)
3187 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003188static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003189{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003190 struct conn_stream *cs;
3191 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003192 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003193
3194 cs = cs_new(conn);
3195 if (!cs)
3196 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003197 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003198 if (!h2s) {
3199 cs_free(cs);
3200 return NULL;
3201 }
3202 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003203}
3204
Willy Tarreaufafd3982018-11-18 21:29:20 +01003205/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3206 * We have to scan because we may have some orphan streams. It might be
3207 * beneficial to scan backwards from the end to reduce the likeliness to find
3208 * orphans.
3209 */
3210static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3211{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003212 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003213 struct h2s *h2s;
3214 struct eb32_node *node;
3215
3216 node = eb32_first(&h2c->streams_by_id);
3217 while (node) {
3218 h2s = container_of(node, struct h2s, by_id);
3219 if (h2s->cs)
3220 return h2s->cs;
3221 node = eb32_next(node);
3222 }
3223 return NULL;
3224}
3225
Olivier Houchard198d1292019-10-25 16:19:26 +02003226static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3227{
3228 int ret = 0;
3229 struct h2c *h2c = conn->ctx;
3230
3231 switch (mux_ctl) {
3232 case MUX_STATUS:
3233 /* Only consider the mux to be ready if we're done with
3234 * the preface and settings, and we had no error.
3235 */
3236 if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR)
3237 ret |= MUX_STATUS_READY;
3238 return ret;
3239 default:
3240 return -1;
3241 }
3242}
3243
Willy Tarreau62f52692017-10-08 23:01:42 +02003244/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003245 * Destroy the mux and the associated connection, if it is no longer used
3246 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003247static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003248{
Christopher Faulet73c12072019-04-08 11:23:22 +02003249 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003250
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003251 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003252 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003253}
3254
3255/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003256 * Detach the stream from the connection and possibly release the connection.
3257 */
3258static void h2_detach(struct conn_stream *cs)
3259{
Willy Tarreau60935142017-10-16 18:11:19 +02003260 struct h2s *h2s = cs->ctx;
3261 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003262 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003263
3264 cs->ctx = NULL;
3265 if (!h2s)
3266 return;
3267
Olivier Houchard998410a2019-04-15 19:23:37 +02003268 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003269 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003270 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003271 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003272 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003273 /*
3274 * At this point, the stream_interface is supposed to have called
3275 * h2_unsubscribe(), so the only way there's still a
3276 * subscription that came from the stream_interface (as we
3277 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3278 * without the stream_interface involved) is that we subscribed
3279 * for sending, we woke the tasklet up and removed the
3280 * SUB_RETRY_SEND flag, so the stream_interface would not
3281 * know it has to unsubscribe for send, but the tasklet hasn't
3282 * run yet. Make sure to handle that by explicitely setting
3283 * send_wait to NULL, as nothing else will do it for us.
3284 */
3285 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003286 }
3287
Olivier Houchardf502aca2018-12-14 19:42:40 +01003288 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003289 h2c = h2s->h2c;
3290 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003291 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003292 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3293 !h2_frt_has_too_many_cs(h2c)) {
3294 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003295 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003296 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003297 }
Willy Tarreau60935142017-10-16 18:11:19 +02003298
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003299 /* this stream may be blocked waiting for some data to leave (possibly
3300 * an ES or RST frame), so orphan it in this case.
3301 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003302 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003303 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003304 (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 +01003305 return;
3306
Willy Tarreau45f752e2017-10-30 15:44:59 +01003307 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3308 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3309 /* unblock the connection if it was blocked on this
3310 * stream.
3311 */
3312 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3313 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003314 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003315 }
3316
Willy Tarreau71049cc2018-03-28 13:56:39 +02003317 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003318
Olivier Houchard8a786902018-12-15 16:05:40 +01003319 if (h2c->flags & H2_CF_IS_BACK &&
3320 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003321 if (!(h2c->conn->flags &
3322 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3323 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003324 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003325 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3326 h2c->conn->owner = NULL;
3327 if (eb_is_empty(&h2c->streams_by_id)) {
3328 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3329 /* The server doesn't want it, let's kill the connection right away */
Olivier Houchard109ba132020-02-14 13:23:45 +01003330 h2c->conn->mux->destroy(h2c);
Olivier Houchard351411f2018-12-27 17:20:54 +01003331 return;
3332 }
3333 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003334 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003335 if (eb_is_empty(&h2c->streams_by_id)) {
3336 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3337 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3338 return;
3339 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003340 /* Never ever allow to reuse a connection from a non-reuse backend */
3341 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3342 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003343 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003344 struct server *srv = objt_server(h2c->conn->target);
3345
3346 if (srv) {
3347 if (h2c->conn->flags & CO_FL_PRIVATE)
3348 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3349 else
3350 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3351 }
3352
3353 }
3354 }
3355 }
3356
Willy Tarreaue323f342018-03-28 13:51:45 +02003357 /* We don't want to close right now unless we're removing the
3358 * last stream, and either the connection is in error, or it
3359 * reached the ID already specified in a GOAWAY frame received
3360 * or sent (as seen by last_sid >= 0).
3361 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003362 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003363 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003364 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003365 }
3366 else if (h2c->task) {
Willy Tarreau534d8f92019-10-01 10:12:00 +02003367 if (h2c_may_expire(h2c))
3368 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3369 else
3370 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003371 task_queue(h2c->task);
Willy Tarreau60935142017-10-16 18:11:19 +02003372 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003373}
3374
Willy Tarreau88bdba32019-05-13 18:17:53 +02003375/* Performs a synchronous or asynchronous shutr(). */
3376static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003377{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003378 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003379 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003380
Willy Tarreauf983d002019-05-14 10:40:21 +02003381 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003382 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003383
Willy Tarreau18059042019-01-31 19:12:48 +01003384 /* a connstream may require us to immediately kill the whole connection
3385 * for example because of a "tcp-request content reject" rule that is
3386 * normally used to limit abuse. In this case we schedule a goaway to
3387 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003388 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003389 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003390 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3391 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3392 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3393 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003394 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3395 /* Nothing was never sent for this stream, so reset with
3396 * REFUSED_STREAM error to let the client retry the
3397 * request.
3398 */
3399 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3400 }
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003401 else {
3402 /* a final response was already provided, we don't want this
3403 * stream anymore. This may happen when the server responds
3404 * before the end of an upload and closes quickly (redirect,
3405 * deny, ...)
3406 */
3407 h2s_error(h2s, H2_ERR_CANCEL);
3408 }
Willy Tarreau18059042019-01-31 19:12:48 +01003409
Willy Tarreau90c32322017-11-24 08:00:30 +01003410 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003411 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003412 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003413
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003414 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003415 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003416 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003417 done:
3418 h2s->flags &= ~H2_SF_WANT_SHUTR;
3419 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003420add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003421 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003422 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003423 if (h2s->flags & H2_SF_BLK_MFCTL) {
3424 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3425 h2s->send_wait = sw;
3426 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3427 h2s->send_wait = sw;
3428 LIST_ADDQ(&h2c->send_list, &h2s->list);
3429 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003430 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003431 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003432 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003433 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003434}
3435
Willy Tarreau88bdba32019-05-13 18:17:53 +02003436/* Performs a synchronous or asynchronous shutw(). */
3437static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003438{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003439 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003440 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003441
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003442 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003443 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003444
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003445 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003446 /* we can cleanly close using an empty data frame only after headers */
3447
3448 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3449 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003450 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003451
3452 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003453 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003454 else
3455 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003456 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003457 /* a connstream may require us to immediately kill the whole connection
3458 * for example because of a "tcp-request content reject" rule that is
3459 * normally used to limit abuse. In this case we schedule a goaway to
3460 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003461 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003462 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003463 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3464 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3465 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3466 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003467 else {
3468 /* Nothing was never sent for this stream, so reset with
3469 * REFUSED_STREAM error to let the client retry the
3470 * request.
3471 */
3472 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3473 }
Willy Tarreau18059042019-01-31 19:12:48 +01003474
Willy Tarreau90c32322017-11-24 08:00:30 +01003475 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003476 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003477 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003478
Willy Tarreau00dd0782018-03-01 16:31:34 +01003479 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003480 }
3481
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003482 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003483 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003484 done:
3485 h2s->flags &= ~H2_SF_WANT_SHUTW;
3486 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003487
3488 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003489 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003490 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003491 if (h2s->flags & H2_SF_BLK_MFCTL) {
3492 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3493 h2s->send_wait = sw;
3494 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3495 h2s->send_wait = sw;
3496 LIST_ADDQ(&h2c->send_list, &h2s->list);
3497 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003498 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003499 /* let the handler know we want to shutw */
3500 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003501 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003502}
3503
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003504/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003505 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3506 * and prevented the last frame from being emitted.
3507 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003508static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3509{
3510 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003511 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003512
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003513 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003514 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003515 h2_do_shutw(h2s);
3516
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003517 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003518 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003519
Willy Tarreau88bdba32019-05-13 18:17:53 +02003520 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003521 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003522 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003523
Willy Tarreau88bdba32019-05-13 18:17:53 +02003524 if (!h2s->cs) {
3525 h2s_destroy(h2s);
3526 if (h2c_is_dead(h2c))
3527 h2_release(h2c);
3528 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003529 }
3530
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003531 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003532}
3533
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003534/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003535static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3536{
3537 struct h2s *h2s = cs->ctx;
3538
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003539 if (cs->flags & CS_FL_KILL_CONN)
3540 h2s->flags |= H2_SF_KILL_CONN;
3541
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003542 if (!mode)
3543 return;
3544
3545 h2_do_shutr(h2s);
3546}
3547
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003548/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003549static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3550{
3551 struct h2s *h2s = cs->ctx;
3552
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003553 if (cs->flags & CS_FL_KILL_CONN)
3554 h2s->flags |= H2_SF_KILL_CONN;
3555
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003556 h2_do_shutw(h2s);
3557}
3558
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003559/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003560 * HTX request or response depending on the connection's side. Returns a
3561 * positive value on success, a negative value on failure, or 0 if it couldn't
3562 * proceed. May report connection errors in h2c->errcode if the frame is
3563 * non-decodable and the connection unrecoverable. In absence of connection
3564 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003565 *
3566 * The function may fold CONTINUATION frames into the initial HEADERS frame
3567 * by removing padding and next frame header, then moving the CONTINUATION
3568 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3569 * leaving a hole between the main frame and the beginning of the next one.
3570 * The possibly remaining incomplete or next frame at the end may be moved
3571 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3572 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3573 *
3574 * A buffer at the beginning of processing may look like this :
3575 *
3576 * ,---.---------.-----.--------------.--------------.------.---.
3577 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3578 * `---^---------^-----^--------------^--------------^------^---'
3579 * | | <-----> | |
3580 * area | dpl | wrap
3581 * |<--------------> |
3582 * | dfl |
3583 * |<-------------------------------------------------->|
3584 * head data
3585 *
3586 * Padding is automatically overwritten when folding, participating to the
3587 * hole size after dfl :
3588 *
3589 * ,---.------------------------.-----.--------------.------.---.
3590 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3591 * `---^------------------------^-----^--------------^------^---'
3592 * | | <-----> | |
3593 * area | hole | wrap
3594 * |<-----------------------> |
3595 * | dfl |
3596 * |<-------------------------------------------------->|
3597 * head data
3598 *
3599 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3600 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3601 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003602 *
3603 * The <flags> field must point to either the stream's flags or to a copy of it
3604 * so that the function can update the following flags :
3605 * - H2_SF_DATA_CLEN when content-length is seen
3606 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3607 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003608 *
3609 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3610 * decoding, in order to detect if we're dealing with a headers or a trailers
3611 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003612 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003613static 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 +02003614{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003615 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003616 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003617 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003618 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003619 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003620 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003621 int flen; // header frame len
3622 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003623 int ret = 0;
3624 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003625 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003626 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003627
Willy Tarreauea18f862018-12-22 20:19:26 +01003628next_frame:
3629 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3630 goto leave; // incomplete input frame
3631
3632 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3633 * this case, we'll try to paste it immediately after the initial
3634 * HEADERS frame payload and kill any possible padding. The initial
3635 * frame's length will be increased to represent the concatenation
3636 * of the two frames. The next frame is read from position <tlen>
3637 * and written at position <flen> (minus padding if some is present).
3638 */
3639 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3640 struct h2_fh hdr;
3641 int clen; // CONTINUATION frame's payload length
3642
3643 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3644 /* no more data, the buffer may be full, either due to
3645 * too large a frame or because of too large a hole that
3646 * we're going to compact at the end.
3647 */
3648 goto leave;
3649 }
3650
3651 if (hdr.ft != H2_FT_CONTINUATION) {
3652 /* RFC7540#6.10: frame of unexpected type */
3653 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3654 goto fail;
3655 }
3656
3657 if (hdr.sid != h2c->dsi) {
3658 /* RFC7540#6.10: frame of different stream */
3659 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3660 goto fail;
3661 }
3662
3663 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3664 /* RFC7540#4.2: invalid frame length */
3665 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3666 goto fail;
3667 }
3668
3669 /* detect when we must stop aggragating frames */
3670 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3671
3672 /* Take as much as we can of the CONTINUATION frame's payload */
3673 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3674 if (clen > hdr.len)
3675 clen = hdr.len;
3676
3677 /* Move the frame's payload over the padding, hole and frame
3678 * header. At least one of hole or dpl is null (see diagrams
3679 * above). The hole moves after the new aggragated frame.
3680 */
3681 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3682 h2c->dfl += clen - h2c->dpl;
3683 hole += h2c->dpl + 9;
3684 h2c->dpl = 0;
3685 goto next_frame;
3686 }
3687
3688 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003689
Willy Tarreau13278b42017-10-13 19:23:14 +02003690 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003691 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003692 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003693 copy = alloc_trash_chunk();
3694 if (!copy) {
3695 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3696 goto fail;
3697 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003698 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3699 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3700 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003701 }
3702
Willy Tarreau13278b42017-10-13 19:23:14 +02003703 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3704 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003705 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003706 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3707 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003708 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003709 }
3710
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003711 if (flen < 5) {
3712 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3713 goto fail;
3714 }
3715
Willy Tarreau13278b42017-10-13 19:23:14 +02003716 hdrs += 5; // stream dep = 4, weight = 1
3717 flen -= 5;
3718 }
3719
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003720 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003721 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003722 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003723 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003724
Willy Tarreau937f7602018-02-26 15:22:17 +01003725 /* we can't retry a failed decompression operation so we must be very
3726 * careful not to take any risks. In practice the output buffer is
3727 * always empty except maybe for trailers, in which case we simply have
3728 * to wait for the upper layer to finish consuming what is available.
3729 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003730
3731 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003732 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003733 if (!htx_is_empty(htx)) {
3734 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003735 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003736 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003737 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003738 if (b_data(rxbuf)) {
3739 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003740 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003741 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003742
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003743 rxbuf->head = 0;
3744 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003745 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003746
Willy Tarreau25919232019-01-03 14:48:18 +01003747 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003748 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3749 sizeof(list)/sizeof(list[0]), tmp);
3750 if (outlen < 0) {
3751 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3752 goto fail;
3753 }
3754
Willy Tarreau25919232019-01-03 14:48:18 +01003755 /* The PACK decompressor was updated, let's update the input buffer and
3756 * the parser's state to commit these changes and allow us to later
3757 * fail solely on the stream if needed.
3758 */
3759 b_del(&h2c->dbuf, h2c->dfl + hole);
3760 h2c->dfl = hole = 0;
3761 h2c->st0 = H2_CS_FRAME_H;
3762
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003763 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003764 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003765
Willy Tarreau88d138e2019-01-02 19:38:14 +01003766 if (*flags & H2_SF_HEADERS_RCVD)
3767 goto trailers;
3768
3769 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003770 if (htx) {
3771 /* HTX mode */
3772 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003773 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003774 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003775 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003776 } else {
3777 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003778 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003779 if (outlen > 0)
3780 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003781 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003782
3783 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003784 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003785 goto fail;
3786 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003787
Willy Tarreau174b06a2018-04-25 18:13:58 +02003788 if (msgf & H2_MSGF_BODY) {
3789 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003790 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003791 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003792 if (htx)
3793 htx->extra = *body_len;
3794 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003795 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003796 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003797 }
3798
Willy Tarreau88d138e2019-01-02 19:38:14 +01003799 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003800 /* indicate that a HEADERS frame was received for this stream, except
3801 * for 1xx responses. For 1xx responses, another HEADERS frame is
3802 * expected.
3803 */
3804 if (!(msgf & H2_MSGF_RSP_1XX))
3805 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003806
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02003807 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003808 /* Mark the end of message, either using EOM in HTX or with the
3809 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003810 * is not set during headers with END_STREAM. For HTX trailers,
3811 * we must not leave an HTX trailers block not followed by an
3812 * EOM block, the two must be atomic. Thus if we fail to emit
3813 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003814 */
3815 if (htx) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02003816 if (!htx_add_endof(htx, HTX_BLK_EOM))
Willy Tarreau88d138e2019-01-02 19:38:14 +01003817 goto fail;
3818 }
3819 else if (*flags & H2_SF_DATA_CHNK) {
3820 if (!b_putblk(rxbuf, "\r\n", 2))
3821 goto fail;
3822 }
3823 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003824
Willy Tarreau86277d42019-01-02 15:36:11 +01003825 /* success */
3826 ret = 1;
3827
Willy Tarreau68dd9852017-07-03 14:44:26 +02003828 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003829 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003830 * move the remaining data over it.
3831 */
3832 if (hole) {
3833 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3834 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3835 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3836 b_sub(&h2c->dbuf, hole);
3837 }
3838
Willy Tarreau97215ca2019-04-29 10:20:21 +02003839 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003840 /* too large frames */
3841 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003842 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003843 }
3844
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003845 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003846 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003847 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003848 return ret;
3849
Willy Tarreau68dd9852017-07-03 14:44:26 +02003850 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003851 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003852 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003853
3854 trailers:
3855 /* This is the last HEADERS frame hence a trailer */
3856
3857 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3858 /* It's a trailer but it's missing ES flag */
3859 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3860 goto fail;
3861 }
3862
Christopher Faulet54b5e212019-06-04 10:08:28 +02003863 /* Trailers terminate a DATA sequence. In HTX we always handle them. In
3864 * legacy, when using chunks, we have to emit the 0 CRLF marker first
3865 * and then handle the trailers. For other modes, the trailers are
3866 * silently dropped.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003867 */
3868 if (htx) {
Willy Tarreau5255f282019-01-03 18:41:05 +01003869 if (h2_make_htx_trailers(list, htx) <= 0)
3870 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003871 }
3872 else if (*flags & H2_SF_DATA_CHNK) {
3873 /* Legacy mode with chunked encoding : we must finalize the
3874 * data block message emit the trailing CRLF */
3875 if (!b_putblk(rxbuf, "0\r\n", 3))
3876 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003877
3878 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3879 if (outlen > 0)
3880 b_add(rxbuf, outlen);
3881 else
3882 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003883 }
3884
3885 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003886}
3887
Willy Tarreau454f9052017-10-26 19:40:35 +02003888/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3889 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3890 * in use, a new chunk is emitted for each frame. This is supposed to fit
3891 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3892 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3893 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003894 * parser state is automatically updated. Returns > 0 if it could completely
3895 * send the current frame, 0 if it couldn't complete, in which case
3896 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3897 * DATA frame can return 0 as a valid result). Stream errors are reported in
3898 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3899 * have checked the frame header and ensured that the frame was complete or the
3900 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003901 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003902static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003903{
3904 struct h2c *h2c = h2s->h2c;
3905 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003906 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003907 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003908 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003909 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003910
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003911 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003912
Olivier Houchard638b7992018-08-16 15:41:52 +02003913 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003914 if (!csbuf) {
3915 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003916 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003917 }
3918
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003919try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003920 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003921 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3922 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003923 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003924 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003925
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003926 if (flen > b_data(&h2c->dbuf)) {
3927 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003928 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003929 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003930 }
3931
Willy Tarreaua9b77962019-01-31 07:23:00 +01003932 if (htx) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003933 unsigned int sent;
3934
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003935 block1 = htx_free_data_space(htx);
3936 if (!block1) {
3937 h2c->flags |= H2_CF_DEM_SFULL;
3938 goto fail;
3939 }
3940 if (flen > block1)
3941 flen = block1;
3942
3943 /* here, flen is the max we can copy into the output buffer */
3944 block1 = b_contig_data(&h2c->dbuf, 0);
3945 if (flen > block1)
3946 flen = block1;
3947
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003948 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003949
Willy Tarreaub6563f42019-06-15 11:34:41 +02003950 b_del(&h2c->dbuf, sent);
3951 h2c->dfl -= sent;
3952 h2c->rcvd_c += sent;
3953 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003954
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003955 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreaub6563f42019-06-15 11:34:41 +02003956 h2s->body_len -= sent;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003957 htx->extra = h2s->body_len;
3958 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003959
3960 if (sent < flen) {
3961 h2c->flags |= H2_CF_DEM_SFULL;
3962 goto fail;
3963 }
3964
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003965 goto try_again;
3966 }
Willy Tarreau455d5682019-05-24 19:42:18 +02003967 else if (unlikely(b_space_wraps(csbuf) &&
3968 flen + chklen <= b_room(csbuf) &&
3969 b_data(csbuf) <= MAX_DATA_REALIGN)) {
3970 /* it doesn't fit in a single block and the buffer is fragmented, if there are
3971 * not too many data in the buffer, let's defragment it and try
3972 * again.
Willy Tarreaud755ea62018-02-26 15:44:54 +01003973 */
3974 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003975 }
3976
Willy Tarreaueba10f22018-04-25 20:44:22 +02003977 /* chunked-encoding requires more room */
3978 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003979 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003980 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3981 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3982 (chklen < 1048576) ? 4 : 8;
3983 chklen += 4; // CRLF, CRLF
3984 }
3985
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003986 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003987 if (flen + chklen > b_room(csbuf)) {
3988 if (chklen >= b_room(csbuf)) {
3989 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003990 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003991 }
3992 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003993 }
3994
3995 if (h2s->flags & H2_SF_DATA_CHNK) {
3996 /* emit the chunk size */
3997 unsigned int chksz = flen;
3998 char str[10];
3999 char *beg;
4000
4001 beg = str + sizeof(str);
4002 *--beg = '\n';
4003 *--beg = '\r';
4004 do {
4005 *--beg = hextab[chksz & 0xF];
4006 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004007 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004008 }
4009
Willy Tarreau454f9052017-10-26 19:40:35 +02004010 /* Block1 is the length of the first block before the buffer wraps,
4011 * block2 is the optional second block to reach the end of the frame.
4012 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004013 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004014 if (block1 > flen)
4015 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02004016 block2 = flen - block1;
4017
4018 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01004019 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02004020
4021 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01004022 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02004023
Willy Tarreaueba10f22018-04-25 20:44:22 +02004024 if (h2s->flags & H2_SF_DATA_CHNK) {
4025 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01004026 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02004027 }
4028
Willy Tarreau454f9052017-10-26 19:40:35 +02004029 /* now mark the input data as consumed (will be deleted from the buffer
4030 * by the caller when seeing FRAME_A after sending the window update).
4031 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004032 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004033 h2c->dfl -= flen;
4034 h2c->rcvd_c += flen;
4035 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
4036
Willy Tarreau1915ca22019-01-24 11:49:37 +01004037 if (h2s->flags & H2_SF_DATA_CLEN)
4038 h2s->body_len -= flen;
4039
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004040 if (h2c->dfl > h2c->dpl) {
4041 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01004042 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004043 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004044 }
4045
Willy Tarreau4a28da12018-01-04 14:41:00 +01004046 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004047 /* here we're done with the frame, all the payload (except padding) was
4048 * transferred.
4049 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02004050
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004051 if (h2c->dff & H2_F_DATA_END_STREAM) {
4052 if (htx) {
4053 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
4054 h2c->flags |= H2_CF_DEM_SFULL;
4055 goto fail;
4056 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01004057 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004058 else if (h2s->flags & H2_SF_DATA_CHNK) {
4059 /* emit the trailing 0 CRLF CRLF */
4060 if (b_room(csbuf) < 5) {
4061 h2c->flags |= H2_CF_DEM_SFULL;
4062 goto fail;
4063 }
4064 chklen += 5;
4065 b_putblk(csbuf, "0\r\n\r\n", 5);
4066 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02004067 }
4068
Willy Tarreaud1023bb2018-03-22 16:53:12 +01004069 h2c->rcvd_c += h2c->dpl;
4070 h2c->rcvd_s += h2c->dpl;
4071 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004072 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004073 if (htx)
4074 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004075 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004076 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004077 if (htx)
4078 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004079 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004080}
4081
Willy Tarreau5dd17352018-06-14 13:33:30 +02004082/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
4083 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
4084 * number of bytes sent. The caller must check the stream's status to detect
4085 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004086 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004087static 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 +02004088{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004089 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004090 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004091 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004092 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004093 struct buffer *mbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004094 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004095 int es_now = 0;
4096 int ret = 0;
4097 int hdr;
4098
4099 if (h2c_mux_busy(h2c, h2s)) {
4100 h2s->flags |= H2_SF_BLK_MBUSY;
4101 return 0;
4102 }
4103
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004104 /* First, try to parse the H1 response and index it into <list>.
4105 * NOTE! Since it comes from haproxy, we *know* that a response header
4106 * block does not wrap and we can safely read it this way without
4107 * having to realign the buffer.
4108 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004109 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004110 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004111 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01004112 /* incomplete or invalid response, this is abnormal coming from
4113 * haproxy and may only result in a bad errorfile or bad Lua code
4114 * so that won't be fixed, raise an error now.
4115 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004116 * FIXME: we should instead add the ability to only return a
4117 * 502 bad gateway. But in theory this is not supposed to
4118 * happen.
4119 */
4120 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4121 ret = 0;
4122 goto end;
4123 }
4124
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004125 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02004126
4127 /* certain statuses have no body or an empty one, regardless of
4128 * what the headers say.
4129 */
4130 if (sl.st.status >= 100 && sl.st.status < 200) {
4131 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
4132 h1m->curr_len = h1m->body_len = 0;
4133 }
4134 else if (sl.st.status == 204 || sl.st.status == 304) {
4135 /* no contents, claim c-len is present and set to zero */
4136 h1m->flags &= ~H1_MF_CHNK;
4137 h1m->flags |= H1_MF_CLEN;
4138 h1m->curr_len = h1m->body_len = 0;
4139 }
4140
Willy Tarreaubcc45952019-05-26 10:05:50 +02004141 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004142 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004143 if (!h2_get_buf(h2c, mbuf)) {
4144 h2c->flags |= H2_CF_MUX_MALLOC;
4145 h2s->flags |= H2_SF_BLK_MROOM;
4146 return 0;
4147 }
4148
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004149 chunk_reset(&outbuf);
4150
4151 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004152 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4153 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004154 break;
4155 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004156 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004157 }
4158
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004159 if (outbuf.size < 9)
4160 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004161
4162 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004163 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4164 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4165 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004166
4167 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004168 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01004169 /* this is an unparsable response */
4170 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4171 ret = 0;
4172 goto end;
4173 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01004174
4175 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004176 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004177 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004178 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004179 }
4180
4181 /* encode all headers, stop at empty name */
4182 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01004183 /* these ones do not exist in H2 and must be dropped. */
4184 if (isteq(list[hdr].n, ist("connection")) ||
4185 isteq(list[hdr].n, ist("proxy-connection")) ||
4186 isteq(list[hdr].n, ist("keep-alive")) ||
4187 isteq(list[hdr].n, ist("upgrade")) ||
4188 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004189 continue;
4190
4191 if (isteq(list[hdr].n, ist("")))
4192 break; // end
4193
4194 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4195 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004196 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004197 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004198 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004199 }
4200 }
4201
4202 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01004203 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004204 es_now = 1;
4205
4206 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004207 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004208
4209 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004210 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004211
4212 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004213 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004214
4215 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004216 b_add(mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01004217 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004218
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004219 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01004220 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004221 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01004222
Willy Tarreau801250e2018-09-11 11:45:04 +02004223 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004224 h2s->flags |= H2_SF_ES_SENT;
4225 if (h2s->st == H2_SS_OPEN)
4226 h2s->st = H2_SS_HLOC;
4227 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004228 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004229 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004230 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004231 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004232 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004233 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004234 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004235 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004236 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004237
4238 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004239
4240 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004241 //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 +02004242 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004243 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004244 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4245 goto retry;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004246 h1m_init_res(h1m);
4247 h1m->err_pos = -1; // don't care about errors on the response path
4248 h2c->flags |= H2_CF_MUX_MFULL;
4249 h2s->flags |= H2_SF_BLK_MROOM;
4250 ret = 0;
4251 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004252}
4253
Willy Tarreau5dd17352018-06-14 13:33:30 +02004254/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4255 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4256 * the number of bytes sent. The caller must check the stream's status to
4257 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004258 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004259static 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 +02004260{
4261 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004262 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004263 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004264 struct buffer *mbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004265 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004266 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004267 int es_now = 0;
4268 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004269 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004270 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004271
4272 if (h2c_mux_busy(h2c, h2s)) {
4273 h2s->flags |= H2_SF_BLK_MBUSY;
4274 goto end;
4275 }
4276
Willy Tarreaubcc45952019-05-26 10:05:50 +02004277 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004278 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004279 if (!h2_get_buf(h2c, mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004280 h2c->flags |= H2_CF_MUX_MALLOC;
4281 h2s->flags |= H2_SF_BLK_MROOM;
4282 goto end;
4283 }
4284
4285 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004286 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004287 goto end;
4288
4289 chunk_reset(&outbuf);
4290
4291 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004292 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4293 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004294 break;
4295 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004296 /* If there are pending data in the output buffer, and we have
4297 * less than 1/4 of the mbuf's size and everything fits, we'll
4298 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4299 * is full and wait, to save some slow realign calls.
4300 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004301 if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004302 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4303 goto retry;
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004304 h2c->flags |= H2_CF_MUX_MFULL;
4305 h2s->flags |= H2_SF_BLK_MROOM;
4306 goto end;
4307 }
4308
Willy Tarreaubcc45952019-05-26 10:05:50 +02004309 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004310 }
4311
4312 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004313 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4314 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004315 h2c->flags |= H2_CF_MUX_MFULL;
4316 h2s->flags |= H2_SF_BLK_MROOM;
4317 goto end;
4318 }
4319
4320 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004321 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4322 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4323 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004324
4325 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4326 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004327 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004328 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004329 break;
4330 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004331 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004332 if ((long long)size > h1m->curr_len)
4333 size = h1m->curr_len;
4334 break;
4335 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004336 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004337 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02004338 if (ret <= 0) {
Christopher Faulet307f31e2020-08-05 14:37:13 +02004339 /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ?
4340 * It should not happen except if opposite connection was closed.
4341 * Note: check there is at least something to parse to be sure the chunk crlf
4342 * is truncated.
4343 */
4344 if (max) {
4345 h1m->err_pos = ofs + max + ret;
4346 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4347 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004348 goto end;
4349 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004350 max -= ret;
4351 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004352 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004353 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004354 }
4355
Willy Tarreau801250e2018-09-11 11:45:04 +02004356 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004357 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004358 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02004359 if (ret <= 0) {
Christopher Faulet307f31e2020-08-05 14:37:13 +02004360 /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ?
4361 * It should not happen except if opposite connection was closed.
4362 * Note: check there is at least something to parse to be sure the chunke size
4363 * is truncated.
4364 */
4365 if (max) {
4366 h1m->err_pos = ofs + max + ret;
4367 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4368 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004369 goto end;
4370 }
4371
4372 size = chunk;
4373 h1m->curr_len = chunk;
4374 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004375 max -= ret;
4376 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004377 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004378 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004379 if (!size)
4380 goto send_empty;
4381 }
4382
4383 /* in MSG_DATA state, continue below */
4384 size = h1m->curr_len;
4385 break;
4386 }
4387
4388 /* we have in <size> the exact number of bytes we need to copy from
4389 * the H1 buffer. We need to check this against the connection's and
4390 * the stream's send windows, and to ensure that this fits in the max
4391 * frame size and in the buffer's available space minus 9 bytes (for
4392 * the frame header). The connection's flow control is applied last so
4393 * that we can use a separate list of streams which are immediately
4394 * unblocked on window opening. Note: we don't implement padding.
4395 */
4396
Willy Tarreau5dd17352018-06-14 13:33:30 +02004397 if (size > max)
4398 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004399
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004400 if (size > h2s_mws(h2s))
4401 size = h2s_mws(h2s);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004402
4403 if (size <= 0) {
4404 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004405 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004406 LIST_DEL_INIT(&h2s->list);
Willy Tarreau55dc0842019-10-22 10:04:39 +02004407 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004408 goto end;
4409 }
4410
4411 if (h2c->mfs && size > h2c->mfs)
4412 size = h2c->mfs;
4413
4414 if (size + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02004415 /* It doesn't fit at once. If it at least fits once split and
4416 * the amount of data to move is low, let's defragment the
4417 * buffer now.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004418 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004419 if (b_space_wraps(mbuf) &&
4420 (size + 9 <= b_room(mbuf)) &&
4421 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004422 goto realign_again;
4423 size = outbuf.size - 9;
4424 }
4425
4426 if (size <= 0) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004427 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4428 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004429 h2c->flags |= H2_CF_MUX_MFULL;
4430 h2s->flags |= H2_SF_BLK_MROOM;
4431 goto end;
4432 }
4433
4434 if (size > h2c->mws)
4435 size = h2c->mws;
4436
4437 if (size <= 0) {
4438 h2s->flags |= H2_SF_BLK_MFCTL;
4439 goto end;
4440 }
4441
4442 /* copy whatever we can */
4443 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004444 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004445 if (ret == 1)
4446 len2 = 0;
4447
4448 if (!ret || len1 + len2 < size) {
4449 /* FIXME: must normally never happen */
4450 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4451 goto end;
4452 }
4453
4454 /* limit len1/len2 to size */
4455 if (len1 + len2 > size) {
4456 int sub = len1 + len2 - size;
4457
4458 if (len2 > sub)
4459 len2 -= sub;
4460 else {
4461 sub -= len2;
4462 len2 = 0;
4463 len1 -= sub;
4464 }
4465 }
4466
4467 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004468 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004469 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004470 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004471
4472 send_empty:
4473 /* we may need to add END_STREAM */
4474 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4475 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004476 *
4477 * FIXME: what we do here is not correct because we send end_stream
4478 * before knowing if we'll have to send a HEADERS frame for the
4479 * trailers. More importantly we're not consuming the trailing CRLF
4480 * after the end of trailers, so it will be left to the caller to
4481 * eat it. The right way to do it would be to measure trailers here
4482 * and to send ES only if there are no trailers.
4483 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004484 */
4485 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004486 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004487 es_now = 1;
4488
4489 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004490 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004491
4492 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004493 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004494
4495 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004496 b_add(mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004497
4498 /* consume incoming H1 response */
4499 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004500 max -= size;
4501 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004502 total += size;
4503 h1m->curr_len -= size;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004504 h2s->sws -= size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004505 h2c->mws -= size;
4506
4507 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004508 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004509 goto new_frame;
4510 }
4511 }
4512
4513 if (es_now) {
4514 if (h2s->st == H2_SS_OPEN)
4515 h2s->st = H2_SS_HLOC;
4516 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004517 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004518
Willy Tarreau35a62702018-02-27 15:37:25 +01004519 if (!(h1m->flags & H1_MF_CHNK)) {
4520 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004521 total += max;
4522 ofs += max;
4523 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004524
Willy Tarreau801250e2018-09-11 11:45:04 +02004525 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004526 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004527
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004528 h2s->flags |= H2_SF_ES_SENT;
4529 }
4530
4531 end:
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004532 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 +02004533 return total;
4534}
4535
Willy Tarreau115e83b2018-12-01 19:17:53 +01004536/* Try to send a HEADERS frame matching HTX response present in HTX message
4537 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4538 * must check the stream's status to detect any error which might have happened
4539 * subsequently to a successful send. The htx blocks are automatically removed
4540 * from the message. The htx message is assumed to be valid since produced from
4541 * the internal code, hence it contains a start line, an optional series of
4542 * header blocks and an end of header, otherwise an invalid frame could be
4543 * emitted and the resulting htx message could be left in an inconsistent state.
4544 */
4545static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4546{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004547 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004548 struct h2c *h2c = h2s->h2c;
4549 struct htx_blk *blk;
4550 struct htx_blk *blk_end;
4551 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004552 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004553 struct htx_sl *sl;
4554 enum htx_blk_type type;
4555 int es_now = 0;
4556 int ret = 0;
4557 int hdr;
4558 int idx;
4559
4560 if (h2c_mux_busy(h2c, h2s)) {
4561 h2s->flags |= H2_SF_BLK_MBUSY;
4562 return 0;
4563 }
4564
Willy Tarreau115e83b2018-12-01 19:17:53 +01004565 /* determine the first block which must not be deleted, blk_end may
4566 * be NULL if all blocks have to be deleted.
4567 */
4568 idx = htx_get_head(htx);
4569 blk_end = NULL;
4570 while (idx != -1) {
4571 type = htx_get_blk_type(htx_get_blk(htx, idx));
4572 idx = htx_get_next(htx, idx);
4573 if (type == HTX_BLK_EOH) {
4574 if (idx != -1)
4575 blk_end = htx_get_blk(htx, idx);
4576 break;
4577 }
4578 }
4579
4580 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004581 blk = htx_get_head_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01004582 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_RES_SL);
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004583 ALREADY_CHECKED(blk);
4584 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004585 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004586 if (h2s->status < 100 || h2s->status > 999)
4587 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004588
4589 /* and the rest of the headers, that we dump starting at header 0 */
4590 hdr = 0;
4591
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004592 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004593 while ((idx = htx_get_next(htx, idx)) != -1) {
4594 blk = htx_get_blk(htx, idx);
4595 type = htx_get_blk_type(blk);
4596
4597 if (type == HTX_BLK_UNUSED)
4598 continue;
4599
4600 if (type != HTX_BLK_HDR)
4601 break;
4602
4603 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4604 goto fail;
4605
4606 list[hdr].n = htx_get_blk_name(htx, blk);
4607 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004608 hdr++;
4609 }
4610
4611 /* marker for end of headers */
4612 list[hdr].n = ist("");
4613
4614 if (h2s->status == 204 || h2s->status == 304) {
4615 /* no contents, claim c-len is present and set to zero */
4616 es_now = 1;
4617 }
4618
Willy Tarreau9c218e72019-05-26 10:08:28 +02004619 mbuf = br_tail(h2c->mbuf);
4620 retry:
4621 if (!h2_get_buf(h2c, mbuf)) {
4622 h2c->flags |= H2_CF_MUX_MALLOC;
4623 h2s->flags |= H2_SF_BLK_MROOM;
4624 return 0;
4625 }
4626
Willy Tarreau115e83b2018-12-01 19:17:53 +01004627 chunk_reset(&outbuf);
4628
4629 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004630 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4631 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004632 break;
4633 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004634 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004635 }
4636
4637 if (outbuf.size < 9)
4638 goto full;
4639
4640 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4641 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4642 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4643 outbuf.data = 9;
4644
4645 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004646 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004647 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004648 goto realign_again;
4649 goto full;
4650 }
4651
4652 /* encode all headers, stop at empty name */
4653 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4654 /* these ones do not exist in H2 and must be dropped. */
4655 if (isteq(list[hdr].n, ist("connection")) ||
4656 isteq(list[hdr].n, ist("proxy-connection")) ||
4657 isteq(list[hdr].n, ist("keep-alive")) ||
4658 isteq(list[hdr].n, ist("upgrade")) ||
4659 isteq(list[hdr].n, ist("transfer-encoding")))
4660 continue;
4661
4662 if (isteq(list[hdr].n, ist("")))
4663 break; // end
4664
4665 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4666 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004667 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004668 goto realign_again;
4669 goto full;
4670 }
4671 }
4672
Christopher Faulet0b465482019-02-19 15:14:23 +01004673 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004674 * FIXME: we should also set it when we know for sure that the
4675 * content-length is zero as well as on 204/304
4676 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004677 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4678 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004679 es_now = 1;
4680
Willy Tarreau927b88b2019-03-04 08:03:25 +01004681 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004682 es_now = 1;
4683
4684 /* update the frame's size */
4685 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4686
4687 if (es_now)
4688 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4689
4690 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004691 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004692
4693 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4694 * 1xx responses, another HEADERS frame is expected.
4695 */
4696 if (h2s->status >= 200 || h2s->status == 101)
4697 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004698
Willy Tarreau115e83b2018-12-01 19:17:53 +01004699 if (es_now) {
4700 h2s->flags |= H2_SF_ES_SENT;
4701 if (h2s->st == H2_SS_OPEN)
4702 h2s->st = H2_SS_HLOC;
4703 else
4704 h2s_close(h2s);
4705 }
4706
4707 /* OK we could properly deliver the response */
4708
4709 /* remove all header blocks including the EOH and compute the
4710 * corresponding size.
4711 *
4712 * FIXME: We should remove everything when es_now is set.
4713 */
4714 ret = 0;
4715 idx = htx_get_head(htx);
4716 blk = htx_get_blk(htx, idx);
4717 while (blk != blk_end) {
4718 ret += htx_get_blksz(blk);
4719 blk = htx_remove_blk(htx, blk);
4720 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004721
Christopher Faulet316934d2019-05-15 14:22:48 +02004722 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4723 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004724 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004725 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004726 end:
4727 return ret;
4728 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004729 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4730 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004731 h2c->flags |= H2_CF_MUX_MFULL;
4732 h2s->flags |= H2_SF_BLK_MROOM;
4733 ret = 0;
4734 goto end;
4735 fail:
4736 /* unparsable HTX messages, too large ones to be produced in the local
4737 * list etc go here (unrecoverable errors).
4738 */
4739 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4740 ret = 0;
4741 goto end;
4742}
4743
Willy Tarreau80739692018-10-05 11:35:57 +02004744/* Try to send a HEADERS frame matching HTX request present in HTX message
4745 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4746 * must check the stream's status to detect any error which might have happened
4747 * subsequently to a successful send. The htx blocks are automatically removed
4748 * from the message. The htx message is assumed to be valid since produced from
4749 * the internal code, hence it contains a start line, an optional series of
4750 * header blocks and an end of header, otherwise an invalid frame could be
4751 * emitted and the resulting htx message could be left in an inconsistent state.
4752 */
4753static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4754{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004755 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004756 struct h2c *h2c = h2s->h2c;
4757 struct htx_blk *blk;
4758 struct htx_blk *blk_end;
4759 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004760 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004761 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004762 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004763 enum htx_blk_type type;
4764 int es_now = 0;
4765 int ret = 0;
4766 int hdr;
4767 int idx;
4768
4769 if (h2c_mux_busy(h2c, h2s)) {
4770 h2s->flags |= H2_SF_BLK_MBUSY;
4771 return 0;
4772 }
4773
Willy Tarreau80739692018-10-05 11:35:57 +02004774 /* determine the first block which must not be deleted, blk_end may
4775 * be NULL if all blocks have to be deleted.
4776 */
4777 idx = htx_get_head(htx);
4778 blk_end = NULL;
4779 while (idx != -1) {
4780 type = htx_get_blk_type(htx_get_blk(htx, idx));
4781 idx = htx_get_next(htx, idx);
4782 if (type == HTX_BLK_EOH) {
4783 if (idx != -1)
4784 blk_end = htx_get_blk(htx, idx);
4785 break;
4786 }
4787 }
4788
4789 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004790 blk = htx_get_head_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01004791 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004792 ALREADY_CHECKED(blk);
4793 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004794 meth = htx_sl_req_meth(sl);
4795 path = htx_sl_req_uri(sl);
4796
4797 /* and the rest of the headers, that we dump starting at header 0 */
4798 hdr = 0;
4799
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004800 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004801 while ((idx = htx_get_next(htx, idx)) != -1) {
4802 blk = htx_get_blk(htx, idx);
4803 type = htx_get_blk_type(blk);
4804
4805 if (type == HTX_BLK_UNUSED)
4806 continue;
4807
4808 if (type != HTX_BLK_HDR)
4809 break;
4810
4811 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4812 goto fail;
4813
4814 list[hdr].n = htx_get_blk_name(htx, blk);
4815 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004816 hdr++;
4817 }
4818
4819 /* marker for end of headers */
4820 list[hdr].n = ist("");
4821
Willy Tarreau9c218e72019-05-26 10:08:28 +02004822 mbuf = br_tail(h2c->mbuf);
4823 retry:
4824 if (!h2_get_buf(h2c, mbuf)) {
4825 h2c->flags |= H2_CF_MUX_MALLOC;
4826 h2s->flags |= H2_SF_BLK_MROOM;
4827 return 0;
4828 }
4829
Willy Tarreau80739692018-10-05 11:35:57 +02004830 chunk_reset(&outbuf);
4831
4832 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004833 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4834 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004835 break;
4836 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004837 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004838 }
4839
4840 if (outbuf.size < 9)
4841 goto full;
4842
4843 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4844 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4845 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4846 outbuf.data = 9;
4847
4848 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004849 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004850 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004851 goto realign_again;
4852 goto full;
4853 }
4854
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004855 /* RFC7540 #8.3: the CONNECT method must have :
4856 * - :authority set to the URI part (host:port)
4857 * - :method set to CONNECT
4858 * - :scheme and :path omitted
4859 */
4860 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4861 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004862 struct ist scheme;
4863
4864 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4865 scheme = ist("http");
4866 else
4867 scheme = ist("https");
4868
4869 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004870 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004871 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004872 goto realign_again;
4873 goto full;
4874 }
Willy Tarreau80739692018-10-05 11:35:57 +02004875
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004876 /* encode the path, which necessarily is the second one */
4877 if (!hpack_encode_path(&outbuf, path)) {
4878 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004879 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004880 goto realign_again;
4881 goto full;
4882 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004883
4884 /* look for the Host header and place it in :authority */
4885 auth = ist2(NULL, 0);
4886 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4887 if (isteq(list[hdr].n, ist("")))
4888 break; // end
4889
4890 if (isteq(list[hdr].n, ist("host"))) {
4891 auth = list[hdr].v;
4892 break;
4893 }
4894 }
4895 }
4896 else {
4897 /* for CONNECT, :authority is taken from the path */
4898 auth = path;
4899 }
4900
4901 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4902 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004903 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004904 goto realign_again;
4905 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004906 }
4907
4908 /* encode all headers, stop at empty name */
4909 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004910 struct ist n = list[hdr].n;
4911 struct ist v = list[hdr].v;
4912
Willy Tarreau80739692018-10-05 11:35:57 +02004913 /* these ones do not exist in H2 and must be dropped. */
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004914 if (isteq(n, ist("connection")) ||
4915 isteq(n, ist("host")) ||
4916 isteq(n, ist("proxy-connection")) ||
4917 isteq(n, ist("keep-alive")) ||
4918 isteq(n, ist("upgrade")) ||
4919 isteq(n, ist("transfer-encoding")))
Willy Tarreau80739692018-10-05 11:35:57 +02004920 continue;
4921
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004922 if (isteq(n, ist("te"))) {
4923 /* "te" may only be sent with "trailers" if this value
4924 * is present, otherwise it must be deleted.
4925 */
4926 v = istist(v, ist("trailers"));
4927 if (!v.ptr || (v.len > 8 && v.ptr[8] != ','))
4928 continue;
4929 v = ist("trailers");
4930 }
4931
4932 if (isteq(n, ist("")))
Willy Tarreau80739692018-10-05 11:35:57 +02004933 break; // end
4934
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004935 if (!hpack_encode_header(&outbuf, n, v)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004936 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004937 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004938 goto realign_again;
4939 goto full;
4940 }
4941 }
4942
4943 /* we may need to add END_STREAM if we have no body :
4944 * - request already closed, or :
4945 * - no transfer-encoding, and :
4946 * - no content-length or content-length:0
4947 * Fixme: this doesn't take into account CONNECT requests.
4948 */
4949 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4950 es_now = 1;
4951
4952 if (sl->flags & HTX_SL_F_BODYLESS)
4953 es_now = 1;
4954
Willy Tarreau927b88b2019-03-04 08:03:25 +01004955 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004956 es_now = 1;
4957
4958 /* update the frame's size */
4959 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4960
4961 if (es_now)
4962 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4963
4964 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004965 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004966 h2s->flags |= H2_SF_HEADERS_SENT;
4967 h2s->st = H2_SS_OPEN;
4968
Willy Tarreau80739692018-10-05 11:35:57 +02004969 if (es_now) {
4970 // trim any possibly pending data (eg: inconsistent content-length)
4971 h2s->flags |= H2_SF_ES_SENT;
4972 h2s->st = H2_SS_HLOC;
4973 }
4974
4975 /* remove all header blocks including the EOH and compute the
4976 * corresponding size.
4977 *
4978 * FIXME: We should remove everything when es_now is set.
4979 */
4980 ret = 0;
4981 idx = htx_get_head(htx);
4982 blk = htx_get_blk(htx, idx);
4983 while (blk != blk_end) {
4984 ret += htx_get_blksz(blk);
4985 blk = htx_remove_blk(htx, blk);
4986 }
4987
Christopher Faulet316934d2019-05-15 14:22:48 +02004988 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4989 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004990 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004991 }
Willy Tarreau80739692018-10-05 11:35:57 +02004992
4993 end:
4994 return ret;
4995 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004996 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4997 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004998 h2c->flags |= H2_CF_MUX_MFULL;
4999 h2s->flags |= H2_SF_BLK_MROOM;
5000 ret = 0;
5001 goto end;
5002 fail:
5003 /* unparsable HTX messages, too large ones to be produced in the local
5004 * list etc go here (unrecoverable errors).
5005 */
5006 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5007 ret = 0;
5008 goto end;
5009}
5010
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005011/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01005012 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
5013 * caller must check the stream's status to detect any error which might have
5014 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02005015 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005016 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005017static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005018{
5019 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005020 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005021 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005022 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005023 size_t total = 0;
5024 int es_now = 0;
5025 int bsize; /* htx block size */
5026 int fsize; /* h2 frame size */
5027 struct htx_blk *blk;
5028 enum htx_blk_type type;
5029 int idx;
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005030 int trunc_out; /* non-zero if truncated on out buf */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005031
5032 if (h2c_mux_busy(h2c, h2s)) {
5033 h2s->flags |= H2_SF_BLK_MBUSY;
5034 goto end;
5035 }
5036
Willy Tarreau98de12a2018-12-12 07:03:00 +01005037 htx = htx_from_buf(buf);
5038
Christopher Faulet54b5e212019-06-04 10:08:28 +02005039 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
5040 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
5041 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005042 */
5043
5044 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01005045 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005046 goto end;
5047
5048 idx = htx_get_head(htx);
5049 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02005050 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005051 bsize = htx_get_blksz(blk);
5052 fsize = bsize;
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005053 trunc_out = 0;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005054
Christopher Faulet54b5e212019-06-04 10:08:28 +02005055 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01005056 if (h2s->flags & H2_SF_ES_SENT) {
5057 /* ES already sent */
5058 htx_remove_blk(htx, blk);
5059 total++; // EOM counts as one byte
5060 count--;
5061 goto end;
5062 }
5063 }
5064 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005065 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005066
Willy Tarreau9c218e72019-05-26 10:08:28 +02005067 mbuf = br_tail(h2c->mbuf);
5068 retry:
5069 if (!h2_get_buf(h2c, mbuf)) {
5070 h2c->flags |= H2_CF_MUX_MALLOC;
5071 h2s->flags |= H2_SF_BLK_MROOM;
5072 goto end;
5073 }
5074
Willy Tarreau98de12a2018-12-12 07:03:00 +01005075 /* Perform some optimizations to reduce the number of buffer copies.
5076 * First, if the mux's buffer is empty and the htx area contains
5077 * exactly one data block of the same size as the requested count, and
5078 * this count fits within the frame size, the stream's window size, and
5079 * the connection's window size, then it's possible to simply swap the
5080 * caller's buffer with the mux's output buffer and adjust offsets and
5081 * length to match the entire DATA HTX block in the middle. In this
5082 * case we perform a true zero-copy operation from end-to-end. This is
5083 * the situation that happens all the time with large files. Second, if
5084 * this is not possible, but the mux's output buffer is empty, we still
5085 * have an opportunity to avoid the copy to the intermediary buffer, by
5086 * making the intermediary buffer's area point to the output buffer's
5087 * area. In this case we want to skip the HTX header to make sure that
5088 * copies remain aligned and that this operation remains possible all
5089 * the time. This goes for headers, data blocks and any data extracted
5090 * from the HTX blocks.
5091 */
5092 if (unlikely(fsize == count &&
5093 htx->used == 1 && type == HTX_BLK_DATA &&
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005094 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005095 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005096
Willy Tarreaubcc45952019-05-26 10:05:50 +02005097 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005098 /* Too bad there are data left there. We're willing to memcpy/memmove
5099 * up to 1/4 of the buffer, which means that it's OK to copy a large
5100 * frame into a buffer containing few data if it needs to be realigned,
5101 * and that it's also OK to copy few data without realigning. Otherwise
5102 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01005103 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005104 if (fsize + 9 <= b_room(mbuf) &&
5105 (b_data(mbuf) <= b_size(mbuf) / 4 ||
5106 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01005107 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005108
Willy Tarreau9c218e72019-05-26 10:08:28 +02005109 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5110 goto retry;
5111
Willy Tarreau98de12a2018-12-12 07:03:00 +01005112 h2c->flags |= H2_CF_MUX_MFULL;
5113 h2s->flags |= H2_SF_BLK_MROOM;
5114 goto end;
5115 }
5116
5117 /* map an H2 frame to the HTX block so that we can put the
5118 * frame header there.
5119 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005120 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
5121 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005122
5123 /* prepend an H2 DATA frame header just before the DATA block */
5124 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5125 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5126 h2_set_frame_size(outbuf.area, fsize);
5127
5128 /* update windows */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005129 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005130 h2c->mws -= fsize;
5131
5132 /* and exchange with our old area */
5133 buf->area = old_area;
5134 buf->data = buf->head = 0;
5135 total += fsize;
5136 goto end;
5137 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005138
Willy Tarreau98de12a2018-12-12 07:03:00 +01005139 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005140 /* for DATA and EOM we'll have to emit a frame, even if empty */
5141
5142 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005143 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5144 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005145 break;
5146 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005147 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005148 }
5149
5150 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005151 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5152 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005153 h2c->flags |= H2_CF_MUX_MFULL;
5154 h2s->flags |= H2_SF_BLK_MROOM;
5155 goto end;
5156 }
5157
5158 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5159 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5160 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5161 outbuf.data = 9;
5162
5163 /* we have in <fsize> the exact number of bytes we need to copy from
5164 * the HTX buffer. We need to check this against the connection's and
5165 * the stream's send windows, and to ensure that this fits in the max
5166 * frame size and in the buffer's available space minus 9 bytes (for
5167 * the frame header). The connection's flow control is applied last so
5168 * that we can use a separate list of streams which are immediately
5169 * unblocked on window opening. Note: we don't implement padding.
5170 */
5171
5172 /* EOM is presented with bsize==1 but would lead to the emission of an
5173 * empty frame, thus we force it to zero here.
5174 */
5175 if (type == HTX_BLK_EOM)
5176 bsize = fsize = 0;
5177
5178 if (!fsize)
5179 goto send_empty;
5180
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005181 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005182 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005183 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005184 LIST_DEL_INIT(&h2s->list);
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005185 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005186 goto end;
5187 }
5188
Willy Tarreauee573762018-12-04 15:25:57 +01005189 if (fsize > count)
5190 fsize = count;
5191
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005192 if (fsize > h2s_mws(h2s))
5193 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005194
5195 if (h2c->mfs && fsize > h2c->mfs)
5196 fsize = h2c->mfs; // >0
5197
5198 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005199 /* It doesn't fit at once. If it at least fits once split and
5200 * the amount of data to move is low, let's defragment the
5201 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005202 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005203 if (b_space_wraps(mbuf) &&
5204 (fsize + 9 <= b_room(mbuf)) &&
5205 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005206 goto realign_again;
5207 fsize = outbuf.size - 9;
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005208 trunc_out = 1;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005209
5210 if (fsize <= 0) {
5211 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005212 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5213 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005214 h2c->flags |= H2_CF_MUX_MFULL;
5215 h2s->flags |= H2_SF_BLK_MROOM;
5216 goto end;
5217 }
5218 }
5219
5220 if (h2c->mws <= 0) {
5221 h2s->flags |= H2_SF_BLK_MFCTL;
5222 goto end;
5223 }
5224
5225 if (fsize > h2c->mws)
5226 fsize = h2c->mws;
5227
5228 /* now let's copy this this into the output buffer */
5229 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005230 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005231 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005232 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005233
5234 send_empty:
5235 /* update the frame's size */
5236 h2_set_frame_size(outbuf.area, fsize);
5237
5238 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5239 * meeting EOM. We should optimize this later.
5240 */
5241 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005242 total++; // EOM counts as one byte
5243 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005244 es_now = 1;
5245 }
5246
5247 if (es_now)
5248 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5249
5250 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005251 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005252
5253 /* consume incoming HTX block, including EOM */
5254 total += fsize;
5255 if (fsize == bsize) {
5256 htx_remove_blk(htx, blk);
5257 if (fsize)
5258 goto new_frame;
5259 } else {
5260 /* we've truncated this block */
5261 htx_cut_data_blk(htx, blk, fsize);
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005262 if (trunc_out)
5263 goto new_frame;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005264 }
5265
5266 if (es_now) {
5267 if (h2s->st == H2_SS_OPEN)
5268 h2s->st = H2_SS_HLOC;
5269 else
5270 h2s_close(h2s);
5271
5272 h2s->flags |= H2_SF_ES_SENT;
5273 }
5274
5275 end:
5276 return total;
5277}
5278
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005279/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5280 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5281 * processed. The caller must check the stream's status to detect any error
5282 * which might have happened subsequently to a successful send. The htx blocks
5283 * are automatically removed from the message. The htx message is assumed to be
5284 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005285 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005286 * as a single frame. The ES flag is always set.
5287 */
5288static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5289{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005290 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005291 struct h2c *h2c = h2s->h2c;
5292 struct htx_blk *blk;
5293 struct htx_blk *blk_end;
5294 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005295 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005296 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005297 int ret = 0;
5298 int hdr;
5299 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005300
5301 if (h2c_mux_busy(h2c, h2s)) {
5302 h2s->flags |= H2_SF_BLK_MBUSY;
5303 goto end;
5304 }
5305
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005306 /* determine the first block which must not be deleted, blk_end may
5307 * be NULL if all blocks have to be deleted. also get trailers.
5308 */
5309 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005310 blk_end = NULL;
5311
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005312 hdr = 0;
5313 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005314 blk = htx_get_blk(htx, idx);
5315 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005316 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005317 if (type == HTX_BLK_UNUSED)
5318 continue;
5319
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005320 if (type == HTX_BLK_EOT) {
5321 if (idx != -1)
5322 blk_end = blk;
5323 break;
5324 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005325 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005326 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005327
5328 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5329 goto fail;
5330
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005331 list[hdr].n = htx_get_blk_name(htx, blk);
5332 list[hdr].v = htx_get_blk_value(htx, blk);
5333 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005334 }
5335
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005336 /* marker for end of trailers */
5337 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005338
Willy Tarreau9c218e72019-05-26 10:08:28 +02005339 mbuf = br_tail(h2c->mbuf);
5340 retry:
5341 if (!h2_get_buf(h2c, mbuf)) {
5342 h2c->flags |= H2_CF_MUX_MALLOC;
5343 h2s->flags |= H2_SF_BLK_MROOM;
5344 goto end;
5345 }
5346
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005347 chunk_reset(&outbuf);
5348
5349 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005350 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5351 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005352 break;
5353 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005354 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005355 }
5356
5357 if (outbuf.size < 9)
5358 goto full;
5359
5360 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5361 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5362 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5363 outbuf.data = 9;
5364
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005365 /* encode all headers */
5366 for (idx = 0; idx < hdr; idx++) {
5367 /* these ones do not exist in H2 or must not appear in
5368 * trailers and must be dropped.
5369 */
5370 if (isteq(list[idx].n, ist("host")) ||
5371 isteq(list[idx].n, ist("content-length")) ||
5372 isteq(list[idx].n, ist("connection")) ||
5373 isteq(list[idx].n, ist("proxy-connection")) ||
5374 isteq(list[idx].n, ist("keep-alive")) ||
5375 isteq(list[idx].n, ist("upgrade")) ||
5376 isteq(list[idx].n, ist("te")) ||
5377 isteq(list[idx].n, ist("transfer-encoding")))
5378 continue;
5379
5380 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5381 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005382 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005383 goto realign_again;
5384 goto full;
5385 }
5386 }
5387
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005388 if (outbuf.data == 9) {
5389 /* here we have a problem, we have nothing to emit (either we
5390 * received an empty trailers block followed or we removed its
5391 * contents above). Because of this we can't send a HEADERS
5392 * frame, so we have to cheat and instead send an empty DATA
5393 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005394 */
5395 outbuf.area[3] = H2_FT_DATA;
5396 outbuf.area[4] = H2_F_DATA_END_STREAM;
5397 }
5398
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005399 /* update the frame's size */
5400 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5401
5402 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005403 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005404 h2s->flags |= H2_SF_ES_SENT;
5405
5406 if (h2s->st == H2_SS_OPEN)
5407 h2s->st = H2_SS_HLOC;
5408 else
5409 h2s_close(h2s);
5410
5411 /* OK we could properly deliver the response */
5412 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005413 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005414 ret = 0;
5415 idx = htx_get_head(htx);
5416 blk = htx_get_blk(htx, idx);
5417 while (blk != blk_end) {
5418 ret += htx_get_blksz(blk);
5419 blk = htx_remove_blk(htx, blk);
5420 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005421
5422 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5423 ret += htx_get_blksz(blk_end);
5424 htx_remove_blk(htx, blk_end);
5425 }
5426
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005427 end:
5428 return ret;
5429 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005430 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5431 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005432 h2c->flags |= H2_CF_MUX_MFULL;
5433 h2s->flags |= H2_SF_BLK_MROOM;
5434 ret = 0;
5435 goto end;
5436 fail:
5437 /* unparsable HTX messages, too large ones to be produced in the local
5438 * list etc go here (unrecoverable errors).
5439 */
5440 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5441 ret = 0;
5442 goto end;
5443}
5444
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005445/* Called from the upper layer, to subscribe to events, such as being able to send.
5446 * The <param> argument here is supposed to be a pointer to a wait_event struct
5447 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5448 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5449 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5450 * returns 0 except for the error above.
5451 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005452static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5453{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005454 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005455 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005456 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005457
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005458 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005459 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005460 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5461 sw->events |= SUB_RETRY_RECV;
5462 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005463 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005464 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005465 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005466 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005467 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5468 sw->events |= SUB_RETRY_SEND;
5469 h2s->send_wait = sw;
5470 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5471 !LIST_ADDED(&h2s->list)) {
5472 if (h2s->flags & H2_SF_BLK_MFCTL)
5473 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5474 else
5475 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005476 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005477 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005478 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005479 if (event_type != 0)
5480 return -1;
5481 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005482}
5483
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005484/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5485 * The <param> argument here is supposed to be a pointer to the same wait_event
5486 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5487 * It always returns zero.
5488 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005489static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5490{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005491 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005492 struct h2s *h2s = cs->ctx;
5493
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005494 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005495 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005496 BUG_ON(h2s->recv_wait != sw);
5497 sw->events &= ~SUB_RETRY_RECV;
5498 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005499 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005500 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005501 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005502 BUG_ON(h2s->send_wait != sw);
5503 LIST_DEL(&h2s->list);
5504 LIST_INIT(&h2s->list);
5505 sw->events &= ~SUB_RETRY_SEND;
5506 /* We were about to send, make sure it does not happen */
5507 if (LIST_ADDED(&h2s->sending_list) &&
5508 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02005509 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02005510 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005511 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005512 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005513 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005514 return 0;
5515}
5516
5517
Olivier Houchard511efea2018-08-16 15:30:32 +02005518/* Called from the upper layer, to receive data */
5519static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5520{
Olivier Houchard638b7992018-08-16 15:41:52 +02005521 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005522 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005523 struct htx *h2s_htx = NULL;
5524 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005525 size_t ret = 0;
5526
5527 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005528 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau86724e22018-12-01 23:19:43 +01005529 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005530 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005531 /* Here htx_to_buf() will set buffer data to 0 because
5532 * the HTX is empty.
5533 */
5534 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005535 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005536 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005537
Christopher Faulet156852b2019-05-16 11:29:13 +02005538 ret = h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005539 buf_htx = htx_from_buf(buf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005540
Christopher Faulet2f6edc82019-05-15 10:07:59 +02005541 /* <buf> is empty and the message is small enough, swap the
5542 * buffers. */
5543 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
5544 htx_to_buf(buf_htx, buf);
5545 htx_to_buf(h2s_htx, &h2s->rxbuf);
5546 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5547 goto end;
5548 }
5549
Christopher Faulet156852b2019-05-16 11:29:13 +02005550 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005551
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005552 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
Willy Tarreau7196dd62019-03-05 10:51:11 +01005553 buf_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005554 if (htx_is_empty(buf_htx))
5555 cs->flags |= CS_FL_EOI;
5556 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005557
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005558 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005559 htx_to_buf(buf_htx, buf);
5560 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet156852b2019-05-16 11:29:13 +02005561 ret -= h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005562 }
5563 else {
5564 ret = b_xfer(buf, &h2s->rxbuf, count);
5565 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005566
Christopher Faulet37070b22019-02-14 15:12:14 +01005567 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005568 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005569 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005570 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005571 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005572 if (h2s->flags & H2_SF_ES_RCVD)
5573 cs->flags |= CS_FL_EOI;
Christopher Faulet11b10532020-10-08 15:38:41 +02005574 if (h2c_read0_pending(h2c) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02005575 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005576 if (cs->flags & CS_FL_ERR_PENDING)
5577 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005578 if (b_size(&h2s->rxbuf)) {
5579 b_free(&h2s->rxbuf);
5580 offer_buffers(NULL, tasks_run_queue);
5581 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005582 }
5583
Willy Tarreau082f5592018-11-25 08:03:32 +01005584 if (ret && h2c->dsi == h2s->id) {
5585 /* demux is blocking on this stream's buffer */
5586 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005587 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005588 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005589
Olivier Houchard511efea2018-08-16 15:30:32 +02005590 return ret;
5591}
5592
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005593/* stops all senders of this connection for example when the mux buffer is full.
5594 * They are moved from the sending_list to either fctl_list or send_list.
5595 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005596static void h2_stop_senders(struct h2c *h2c)
5597{
5598 struct h2s *h2s, *h2s_back;
5599
Olivier Houchardd360ac62019-03-22 17:37:16 +01005600 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5601 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02005602 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005603 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005604 }
5605}
5606
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005607/* Called from the upper layer, to send data from buffer <buf> for no more than
5608 * <count> bytes. Returns the number of bytes effectively sent. Some status
5609 * flags may be updated on the conn_stream.
5610 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005611static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005612{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005613 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005614 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005615 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005616 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005617 struct htx *htx;
5618 struct htx_blk *blk;
5619 enum htx_blk_type btype;
5620 uint32_t bsize;
5621 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005622
Olivier Houchardd360ac62019-03-22 17:37:16 +01005623 /* If we were not just woken because we wanted to send but couldn't,
5624 * and there's somebody else that is waiting to send, do nothing,
5625 * we will subscribe later and be put at the end of the list
5626 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005627 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005628 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5629 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005630 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005631
Olivier Houchard998410a2019-04-15 19:23:37 +02005632 /* We couldn't set it to NULL before, because we needed it in case
5633 * we had to cancel the tasklet
5634 */
5635 h2s->send_wait = NULL;
5636
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005637 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5638 return 0;
5639
Willy Tarreau902df222019-10-31 15:48:18 +01005640 if (h2s->h2c->st0 >= H2_CS_ERROR) {
5641 cs->flags |= CS_FL_ERROR;
5642 return 0;
5643 }
5644
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005645 /* htx will be enough to decide if we're using HTX or legacy */
5646 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5647
Willy Tarreau0bad0432018-06-14 16:54:01 +02005648 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005649 h2s->flags |= H2_SF_OUTGOING_DATA;
5650
Willy Tarreau751f2d02018-10-05 09:35:00 +02005651 if (h2s->id == 0) {
5652 int32_t id = h2c_get_next_sid(h2s->h2c);
5653
5654 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005655 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005656 return 0;
5657 }
5658
5659 eb32_delete(&h2s->by_id);
5660 h2s->by_id.key = h2s->id = id;
5661 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005662 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005663 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5664 }
5665
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005666 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005667 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005668 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005669 idx = htx_get_head(htx);
5670 blk = htx_get_blk(htx, idx);
5671 btype = htx_get_blk_type(blk);
5672 bsize = htx_get_blksz(blk);
5673
5674 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005675 case HTX_BLK_REQ_SL:
5676 /* start-line before headers */
5677 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5678 if (ret > 0) {
5679 total += ret;
5680 count -= ret;
5681 if (ret < bsize)
5682 goto done;
5683 }
5684 break;
5685
Willy Tarreau115e83b2018-12-01 19:17:53 +01005686 case HTX_BLK_RES_SL:
5687 /* start-line before headers */
5688 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5689 if (ret > 0) {
5690 total += ret;
5691 count -= ret;
5692 if (ret < bsize)
5693 goto done;
5694 }
5695 break;
5696
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005697 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005698 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005699 /* all these cause the emission of a DATA frame (possibly empty).
5700 * This EOM necessarily is one before trailers, as the EOM following
5701 * trailers would have been consumed by the trailers parser.
5702 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005703 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005704 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005705 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005706 total += ret;
5707 count -= ret;
5708 if (ret < bsize)
5709 goto done;
5710 }
5711 break;
5712
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005713 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005714 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005715 /* This is the first trailers block, all the subsequent ones AND
5716 * the EOM will be swallowed by the parser.
5717 */
5718 ret = h2s_htx_make_trailers(h2s, htx);
5719 if (ret > 0) {
5720 total += ret;
5721 count -= ret;
5722 if (ret < bsize)
5723 goto done;
5724 }
5725 break;
5726
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005727 default:
5728 htx_remove_blk(htx, blk);
5729 total += bsize;
5730 count -= bsize;
5731 break;
5732 }
5733 }
5734 goto done;
5735 }
5736
5737 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005738 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005739 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005740 if (h2s->h2c->flags & H2_CF_IS_BACK)
5741 ret = -1;
5742 else
5743 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005744 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005745 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005746 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005747 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005748 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005749 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005750 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005751
Willy Tarreau5dd17352018-06-14 13:33:30 +02005752 if (unlikely((int)ret <= 0)) {
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02005753 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005754 break;
5755 }
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02005756
Willy Tarreau35a62702018-02-27 15:37:25 +01005757 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005758 total += count;
5759 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005760 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005761 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005762 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005763 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005764 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005765 break;
5766 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005767
5768 total += ret;
5769 count -= ret;
5770
Willy Tarreau2b778482019-05-06 15:00:22 +02005771 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005772 break;
5773
5774 if (h2s->flags & H2_SF_BLK_ANY)
5775 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005776 }
5777
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005778 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005779 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005780 /* trim any possibly pending data after we close (extra CR-LF,
5781 * unprocessed trailers, abnormal extra data, ...)
5782 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005783 total += count;
5784 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005785 }
5786
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005787 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005788 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005789 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005790 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005791 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005792 }
5793
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005794 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005795 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005796 } else {
5797 b_del(buf, total);
5798 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005799
5800 /* The mux is full, cancel the pending tasks */
5801 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5802 (h2s->flags & H2_SF_BLK_MBUSY))
5803 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005804
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005805 /* If we're running HTX, and we read the whole buffer, then pretend
5806 * we read exactly what the caller specified, as with HTX the caller
5807 * will always give the buffer size, instead of the amount of data
5808 * available.
5809 */
5810 if (htx && !b_data(buf))
5811 total = orig_count;
5812
Olivier Houchard7505f942018-08-21 18:10:44 +02005813 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005814 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005815 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02005816
Olivier Houchard7505f942018-08-21 18:10:44 +02005817 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005818 /* If we're waiting for flow control, and we got a shutr on the
5819 * connection, we will never be unlocked, so add an error on
5820 * the conn_stream.
5821 */
5822 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5823 !b_data(&h2s->h2c->dbuf) &&
5824 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5825 if (cs->flags & CS_FL_EOS)
5826 cs->flags |= CS_FL_ERROR;
5827 else
5828 cs->flags |= CS_FL_ERR_PENDING;
5829 }
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005830
5831 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL)) {
5832 /* Ok we managed to send something, leave the send_list if we were still there */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005833 LIST_DEL_INIT(&h2s->list);
5834 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005835 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005836}
5837
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005838/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005839static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005840{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005841 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005842 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005843 struct eb32_node *node;
5844 int fctl_cnt = 0;
5845 int send_cnt = 0;
5846 int tree_cnt = 0;
5847 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005848 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005849
5850 if (!h2c)
5851 return;
5852
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005853 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005854 fctl_cnt++;
5855
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005856 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005857 send_cnt++;
5858
Willy Tarreau3af37712018-12-18 14:34:41 +01005859 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005860 node = eb32_first(&h2c->streams_by_id);
5861 while (node) {
5862 h2s = container_of(node, struct h2s, by_id);
5863 tree_cnt++;
5864 if (!h2s->cs)
5865 orph_cnt++;
5866 node = eb32_next(node);
5867 }
5868
Willy Tarreau60f62682019-05-26 11:32:27 +02005869 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005870 tmbuf = br_tail(h2c->mbuf);
Willy Tarreau987c0632018-12-18 10:32:05 +01005871 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5872 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005873 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5874 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau616ac812018-07-24 14:12:42 +02005875 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5876 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005877 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005878 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5879 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5880 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005881 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5882 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5883 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005884 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5885 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005886
5887 if (h2s) {
5888 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5889 h2s, h2s->id, h2s->flags,
5890 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5891 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5892 h2s->cs);
5893 if (h2s->cs)
5894 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5895 h2s->cs->flags, h2s->cs->data);
5896 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005897}
Willy Tarreau62f52692017-10-08 23:01:42 +02005898
5899/*******************************************************/
5900/* functions below are dedicated to the config parsers */
5901/*******************************************************/
5902
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005903/* config parser for global "tune.h2.header-table-size" */
5904static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5905 struct proxy *defpx, const char *file, int line,
5906 char **err)
5907{
5908 if (too_many_args(1, args, err, NULL))
5909 return -1;
5910
5911 h2_settings_header_table_size = atoi(args[1]);
5912 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5913 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5914 return -1;
5915 }
5916 return 0;
5917}
Willy Tarreau62f52692017-10-08 23:01:42 +02005918
Willy Tarreaue6baec02017-07-27 11:45:11 +02005919/* config parser for global "tune.h2.initial-window-size" */
5920static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5921 struct proxy *defpx, const char *file, int line,
5922 char **err)
5923{
5924 if (too_many_args(1, args, err, NULL))
5925 return -1;
5926
5927 h2_settings_initial_window_size = atoi(args[1]);
5928 if (h2_settings_initial_window_size < 0) {
5929 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5930 return -1;
5931 }
5932 return 0;
5933}
5934
Willy Tarreau5242ef82017-07-27 11:47:28 +02005935/* config parser for global "tune.h2.max-concurrent-streams" */
5936static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5937 struct proxy *defpx, const char *file, int line,
5938 char **err)
5939{
5940 if (too_many_args(1, args, err, NULL))
5941 return -1;
5942
5943 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005944 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005945 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5946 return -1;
5947 }
5948 return 0;
5949}
5950
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005951/* config parser for global "tune.h2.max-frame-size" */
5952static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5953 struct proxy *defpx, const char *file, int line,
5954 char **err)
5955{
5956 if (too_many_args(1, args, err, NULL))
5957 return -1;
5958
5959 h2_settings_max_frame_size = atoi(args[1]);
5960 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5961 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5962 return -1;
5963 }
5964 return 0;
5965}
5966
Willy Tarreau62f52692017-10-08 23:01:42 +02005967
5968/****************************************/
5969/* MUX initialization and instanciation */
5970/***************************************/
5971
5972/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005973static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005974 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005975 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005976 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005977 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005978 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005979 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005980 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005981 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005982 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005983 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005984 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005985 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005986 .shutr = h2_shutr,
5987 .shutw = h2_shutw,
Olivier Houchard198d1292019-10-25 16:19:26 +02005988 .ctl = h2_ctl,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005989 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005990 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005991 .name = "H2",
5992};
5993
Christopher Faulet32f61c02018-04-10 14:33:41 +02005994/* PROTO selection : this mux registers PROTO token "h2" */
5995static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005996 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005997
Willy Tarreau0108d902018-11-25 19:14:37 +01005998INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5999
Christopher Faulet9b579102019-04-11 22:52:25 +02006000
6001/* The mux operations */
6002static const struct mux_ops h2_htx_ops = {
6003 .init = h2_init,
6004 .wake = h2_wake,
6005 .snd_buf = h2_snd_buf,
6006 .rcv_buf = h2_rcv_buf,
6007 .subscribe = h2_subscribe,
6008 .unsubscribe = h2_unsubscribe,
6009 .attach = h2_attach,
6010 .get_first_cs = h2_get_first_cs,
6011 .detach = h2_detach,
6012 .destroy = h2_destroy,
6013 .avail_streams = h2_avail_streams,
6014 .used_streams = h2_used_streams,
6015 .shutr = h2_shutr,
6016 .shutw = h2_shutw,
Olivier Houchard198d1292019-10-25 16:19:26 +02006017 .ctl = h2_ctl,
Christopher Faulet9b579102019-04-11 22:52:25 +02006018 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02006019 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02006020 .name = "H2",
6021};
6022
Willy Tarreauf8957272018-10-03 10:25:20 +02006023static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02006024 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02006025
6026INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
6027
Willy Tarreau62f52692017-10-08 23:01:42 +02006028/* config keyword parsers */
6029static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02006030 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02006031 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02006032 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01006033 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02006034 { 0, NULL, NULL }
6035}};
6036
Willy Tarreau0108d902018-11-25 19:14:37 +01006037INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);