blob: 27af8262eb2df2902919742ab37cbe48f2be18ed [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
Christopher Faulet71633512020-10-22 16:24:58 +02002928 if ((conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR)) ||
2929 (h2c->st0 == H2_CS_ERROR2) || (h2c->flags & H2_CF_GOAWAY_FAILED))
Willy Tarreaubc933932017-10-09 16:21:43 +02002930 break;
2931
2932 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2933 flags |= CO_SFL_MSG_MORE;
2934
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002935 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
2936 if (b_data(buf)) {
2937 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002938 if (!ret) {
2939 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002940 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002941 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002942 sent = 1;
2943 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002944 if (b_data(buf)) {
2945 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002946 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002947 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002948 }
2949 b_free(buf);
2950 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02002951 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002952
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002953 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02002954 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002955
Willy Tarreaubc933932017-10-09 16:21:43 +02002956 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet07423082019-10-24 10:31:01 +02002957 if (sent)
2958 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02002959 }
2960
Willy Tarreaua2af5122017-10-09 11:56:46 +02002961 if (conn->flags & CO_FL_SOCK_WR_SH) {
2962 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02002963 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002964 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002965 /* We're not full anymore, so we can wake any task that are waiting
2966 * for us.
2967 */
Willy Tarreaufa9a0402020-01-10 17:01:29 +01002968 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H)
2969 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Olivier Houchard998410a2019-04-15 19:23:37 +02002970
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002971 /* We're done, no more to send */
Willy Tarreau662fafc2019-05-26 09:43:07 +02002972 if (!br_data(h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002973 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002974schedule:
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002975 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002976 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002977
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002978 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002979}
2980
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002981/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002982static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2983{
2984 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002985 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002986
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002987 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002988 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002989 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002990 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002991 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002992 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002993 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002994}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002995
Willy Tarreau62f52692017-10-08 23:01:42 +02002996/* callback called on any event by the connection handler.
2997 * It applies changes and returns zero, or < 0 if it wants immediate
2998 * destruction of the connection (which normally doesn not happen in h2).
2999 */
Olivier Houchard7505f942018-08-21 18:10:44 +02003000static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003001{
Olivier Houchard7505f942018-08-21 18:10:44 +02003002 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003003
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003004 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01003005 h2_process_demux(h2c);
3006
3007 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003008 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003009
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003010 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01003011 h2c->flags &= ~H2_CF_DEM_DFULL;
3012 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003013 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003014
Willy Tarreau318822b2020-10-13 18:09:15 +02003015 if (unlikely(h2c->proxy->state == PR_STSTOPPED) && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01003016 /* frontend is stopping, reload likely in progress, let's try
3017 * to announce a graceful shutdown if not yet done. We don't
3018 * care if it fails, it will be tried again later.
3019 */
3020 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3021 if (h2c->last_sid < 0)
3022 h2c->last_sid = (1U << 31) - 1;
3023 h2c_send_goaway_error(h2c, NULL);
3024 }
3025 }
3026
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003027 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003028 * If we received early data, and the handshake is done, wake
3029 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003030 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003031 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
3032 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
3033 struct eb32_node *node;
3034 struct h2s *h2s;
3035
3036 h2c->flags |= H2_CF_WAIT_FOR_HS;
3037 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
3038
3039 while (node) {
3040 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01003041 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01003042 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003043 node = eb32_next(node);
3044 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003045 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003046
Christopher Faulet11b10532020-10-08 15:38:41 +02003047 if (conn->flags & CO_FL_ERROR || h2c_read0_pending(h2c) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01003048 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
3049 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
3050 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02003051 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003052
3053 if (eb_is_empty(&h2c->streams_by_id)) {
3054 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003055 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003056 return -1;
3057 }
Willy Tarreauab66e152019-10-31 15:36:30 +01003058
3059 /* connections in error must be removed from the idle lists */
3060 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3061 LIST_DEL_LOCKED(&conn->list);
3062 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3063 }
3064 else if (h2c->st0 == H2_CS_ERROR) {
3065 /* connections in error must be removed from the idle lists */
3066 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3067 LIST_DEL_LOCKED(&conn->list);
3068 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003069 }
3070
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003071 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003072 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003073
Olivier Houchard53216e72018-10-10 15:46:36 +02003074 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3075 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3076 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003077 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003078 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3079 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003080 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003081
Willy Tarreau3f133572017-10-31 19:21:06 +01003082 if (h2c->task) {
Willy Tarreau534d8f92019-10-01 10:12:00 +02003083 if (h2c_may_expire(h2c))
3084 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3085 else
3086 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003087 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003088 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003089
Olivier Houchard7505f942018-08-21 18:10:44 +02003090 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02003091 return 0;
3092}
3093
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003094/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003095static int h2_wake(struct connection *conn)
3096{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003097 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003098
3099 return (h2_process(h2c));
3100}
3101
Willy Tarreauea392822017-10-31 10:02:25 +01003102/* Connection timeout management. The principle is that if there's no receipt
3103 * nor sending for a certain amount of time, the connection is closed. If the
3104 * MUX buffer still has lying data or is not allocatable, the connection is
3105 * immediately killed. If it's allocatable and empty, we attempt to send a
3106 * GOAWAY frame.
3107 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003108static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003109{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003110 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003111 int expired = tick_is_expired(t->expire, now_ms);
3112
Willy Tarreau0975f112018-03-29 15:22:59 +02003113 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01003114 return t;
3115
Willy Tarreau534d8f92019-10-01 10:12:00 +02003116 if (h2c && !h2c_may_expire(h2c)) {
3117 /* we do still have streams but all of them are idle, waiting
3118 * for the data layer, so we must not enforce the timeout here.
3119 */
3120 t->expire = TICK_ETERNITY;
3121 return t;
3122 }
3123
Olivier Houchard3f795f72019-04-17 22:51:06 +02003124 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003125
3126 if (!h2c) {
3127 /* resources were already deleted */
3128 return NULL;
3129 }
3130
3131 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003132 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003133 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003134
Willy Tarreau662fafc2019-05-26 09:43:07 +02003135 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003136 /* don't even try to send a GOAWAY, the buffer is stuck */
3137 h2c->flags |= H2_CF_GOAWAY_FAILED;
3138 }
3139
3140 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003141 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003142 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3143 h2c->flags |= H2_CF_GOAWAY_FAILED;
3144
Willy Tarreau662fafc2019-05-26 09:43:07 +02003145 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003146 unsigned int released = 0;
3147 struct buffer *buf;
3148
3149 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3150 if (b_data(buf)) {
3151 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3152 if (!ret)
3153 break;
3154 b_del(buf, ret);
3155 if (b_data(buf))
3156 break;
3157 b_free(buf);
3158 released++;
3159 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003160 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003161
3162 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003163 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003164 }
Willy Tarreauea392822017-10-31 10:02:25 +01003165
Willy Tarreauab66e152019-10-31 15:36:30 +01003166 /* in any case this connection must not be considered idle anymore */
3167 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3168 LIST_DEL_LOCKED(&h2c->conn->list);
3169 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3170
Willy Tarreau0975f112018-03-29 15:22:59 +02003171 /* either we can release everything now or it will be done later once
3172 * the last stream closes.
3173 */
3174 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003175 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003176
Willy Tarreauea392822017-10-31 10:02:25 +01003177 return NULL;
3178}
3179
3180
Willy Tarreau62f52692017-10-08 23:01:42 +02003181/*******************************************/
3182/* functions below are used by the streams */
3183/*******************************************/
3184
3185/*
3186 * Attach a new stream to a connection
3187 * (Used for outgoing connections)
3188 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003189static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003190{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003191 struct conn_stream *cs;
3192 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003193 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003194
3195 cs = cs_new(conn);
3196 if (!cs)
3197 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003198 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003199 if (!h2s) {
3200 cs_free(cs);
3201 return NULL;
3202 }
3203 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003204}
3205
Willy Tarreaufafd3982018-11-18 21:29:20 +01003206/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3207 * We have to scan because we may have some orphan streams. It might be
3208 * beneficial to scan backwards from the end to reduce the likeliness to find
3209 * orphans.
3210 */
3211static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3212{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003213 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003214 struct h2s *h2s;
3215 struct eb32_node *node;
3216
3217 node = eb32_first(&h2c->streams_by_id);
3218 while (node) {
3219 h2s = container_of(node, struct h2s, by_id);
3220 if (h2s->cs)
3221 return h2s->cs;
3222 node = eb32_next(node);
3223 }
3224 return NULL;
3225}
3226
Olivier Houchard198d1292019-10-25 16:19:26 +02003227static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3228{
3229 int ret = 0;
3230 struct h2c *h2c = conn->ctx;
3231
3232 switch (mux_ctl) {
3233 case MUX_STATUS:
3234 /* Only consider the mux to be ready if we're done with
3235 * the preface and settings, and we had no error.
3236 */
3237 if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR)
3238 ret |= MUX_STATUS_READY;
3239 return ret;
3240 default:
3241 return -1;
3242 }
3243}
3244
Willy Tarreau62f52692017-10-08 23:01:42 +02003245/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003246 * Destroy the mux and the associated connection, if it is no longer used
3247 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003248static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003249{
Christopher Faulet73c12072019-04-08 11:23:22 +02003250 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003251
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003252 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003253 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003254}
3255
3256/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003257 * Detach the stream from the connection and possibly release the connection.
3258 */
3259static void h2_detach(struct conn_stream *cs)
3260{
Willy Tarreau60935142017-10-16 18:11:19 +02003261 struct h2s *h2s = cs->ctx;
3262 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003263 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003264
3265 cs->ctx = NULL;
3266 if (!h2s)
3267 return;
3268
Olivier Houchard998410a2019-04-15 19:23:37 +02003269 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003270 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003271 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003272 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003273 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003274 /*
3275 * At this point, the stream_interface is supposed to have called
3276 * h2_unsubscribe(), so the only way there's still a
3277 * subscription that came from the stream_interface (as we
3278 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3279 * without the stream_interface involved) is that we subscribed
3280 * for sending, we woke the tasklet up and removed the
3281 * SUB_RETRY_SEND flag, so the stream_interface would not
3282 * know it has to unsubscribe for send, but the tasklet hasn't
3283 * run yet. Make sure to handle that by explicitely setting
3284 * send_wait to NULL, as nothing else will do it for us.
3285 */
3286 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003287 }
3288
Olivier Houchardf502aca2018-12-14 19:42:40 +01003289 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003290 h2c = h2s->h2c;
3291 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003292 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003293 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3294 !h2_frt_has_too_many_cs(h2c)) {
3295 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003296 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003297 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003298 }
Willy Tarreau60935142017-10-16 18:11:19 +02003299
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003300 /* this stream may be blocked waiting for some data to leave (possibly
3301 * an ES or RST frame), so orphan it in this case.
3302 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003303 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003304 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003305 (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 +01003306 return;
3307
Willy Tarreau45f752e2017-10-30 15:44:59 +01003308 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3309 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3310 /* unblock the connection if it was blocked on this
3311 * stream.
3312 */
3313 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3314 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003315 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003316 }
3317
Willy Tarreau71049cc2018-03-28 13:56:39 +02003318 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003319
Olivier Houchard8a786902018-12-15 16:05:40 +01003320 if (h2c->flags & H2_CF_IS_BACK &&
3321 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003322 if (!(h2c->conn->flags &
3323 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3324 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003325 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003326 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3327 h2c->conn->owner = NULL;
3328 if (eb_is_empty(&h2c->streams_by_id)) {
3329 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3330 /* The server doesn't want it, let's kill the connection right away */
Olivier Houchard109ba132020-02-14 13:23:45 +01003331 h2c->conn->mux->destroy(h2c);
Olivier Houchard351411f2018-12-27 17:20:54 +01003332 return;
3333 }
3334 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003335 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003336 if (eb_is_empty(&h2c->streams_by_id)) {
3337 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3338 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3339 return;
3340 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003341 /* Never ever allow to reuse a connection from a non-reuse backend */
3342 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3343 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003344 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003345 struct server *srv = objt_server(h2c->conn->target);
3346
3347 if (srv) {
3348 if (h2c->conn->flags & CO_FL_PRIVATE)
3349 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3350 else
3351 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3352 }
3353
3354 }
3355 }
3356 }
3357
Willy Tarreaue323f342018-03-28 13:51:45 +02003358 /* We don't want to close right now unless we're removing the
3359 * last stream, and either the connection is in error, or it
3360 * reached the ID already specified in a GOAWAY frame received
3361 * or sent (as seen by last_sid >= 0).
3362 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003363 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003364 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003365 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003366 }
3367 else if (h2c->task) {
Willy Tarreau534d8f92019-10-01 10:12:00 +02003368 if (h2c_may_expire(h2c))
3369 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3370 else
3371 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003372 task_queue(h2c->task);
Willy Tarreau60935142017-10-16 18:11:19 +02003373 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003374}
3375
Willy Tarreau88bdba32019-05-13 18:17:53 +02003376/* Performs a synchronous or asynchronous shutr(). */
3377static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003378{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003379 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003380 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003381
Willy Tarreauf983d002019-05-14 10:40:21 +02003382 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003383 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003384
Willy Tarreau18059042019-01-31 19:12:48 +01003385 /* a connstream may require us to immediately kill the whole connection
3386 * for example because of a "tcp-request content reject" rule that is
3387 * normally used to limit abuse. In this case we schedule a goaway to
3388 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003389 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003390 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003391 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3392 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3393 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3394 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003395 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3396 /* Nothing was never sent for this stream, so reset with
3397 * REFUSED_STREAM error to let the client retry the
3398 * request.
3399 */
3400 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3401 }
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003402 else {
3403 /* a final response was already provided, we don't want this
3404 * stream anymore. This may happen when the server responds
3405 * before the end of an upload and closes quickly (redirect,
3406 * deny, ...)
3407 */
3408 h2s_error(h2s, H2_ERR_CANCEL);
3409 }
Willy Tarreau18059042019-01-31 19:12:48 +01003410
Willy Tarreau90c32322017-11-24 08:00:30 +01003411 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003412 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003413 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003414
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003415 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003416 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003417 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003418 done:
3419 h2s->flags &= ~H2_SF_WANT_SHUTR;
3420 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003421add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003422 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003423 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003424 if (h2s->flags & H2_SF_BLK_MFCTL) {
3425 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3426 h2s->send_wait = sw;
3427 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3428 h2s->send_wait = sw;
3429 LIST_ADDQ(&h2c->send_list, &h2s->list);
3430 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003431 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003432 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003433 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003434 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003435}
3436
Willy Tarreau88bdba32019-05-13 18:17:53 +02003437/* Performs a synchronous or asynchronous shutw(). */
3438static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003439{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003440 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003441 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003442
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003443 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003444 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003445
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003446 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003447 /* we can cleanly close using an empty data frame only after headers */
3448
3449 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3450 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003451 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003452
3453 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003454 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003455 else
3456 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003457 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003458 /* a connstream may require us to immediately kill the whole connection
3459 * for example because of a "tcp-request content reject" rule that is
3460 * normally used to limit abuse. In this case we schedule a goaway to
3461 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003462 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003463 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003464 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3465 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3466 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3467 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003468 else {
3469 /* Nothing was never sent for this stream, so reset with
3470 * REFUSED_STREAM error to let the client retry the
3471 * request.
3472 */
3473 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3474 }
Willy Tarreau18059042019-01-31 19:12:48 +01003475
Willy Tarreau90c32322017-11-24 08:00:30 +01003476 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003477 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003478 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003479
Willy Tarreau00dd0782018-03-01 16:31:34 +01003480 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003481 }
3482
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003483 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003484 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003485 done:
3486 h2s->flags &= ~H2_SF_WANT_SHUTW;
3487 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003488
3489 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003490 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003491 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003492 if (h2s->flags & H2_SF_BLK_MFCTL) {
3493 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3494 h2s->send_wait = sw;
3495 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3496 h2s->send_wait = sw;
3497 LIST_ADDQ(&h2c->send_list, &h2s->list);
3498 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003499 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003500 /* let the handler know we want to shutw */
3501 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003502 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003503}
3504
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003505/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003506 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3507 * and prevented the last frame from being emitted.
3508 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003509static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3510{
3511 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003512 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003513
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003514 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003515 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003516 h2_do_shutw(h2s);
3517
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003518 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003519 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003520
Willy Tarreau88bdba32019-05-13 18:17:53 +02003521 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003522 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003523 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003524
Willy Tarreau88bdba32019-05-13 18:17:53 +02003525 if (!h2s->cs) {
3526 h2s_destroy(h2s);
3527 if (h2c_is_dead(h2c))
3528 h2_release(h2c);
3529 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003530 }
3531
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003532 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003533}
3534
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003535/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003536static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3537{
3538 struct h2s *h2s = cs->ctx;
3539
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003540 if (cs->flags & CS_FL_KILL_CONN)
3541 h2s->flags |= H2_SF_KILL_CONN;
3542
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003543 if (!mode)
3544 return;
3545
3546 h2_do_shutr(h2s);
3547}
3548
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003549/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003550static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3551{
3552 struct h2s *h2s = cs->ctx;
3553
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003554 if (cs->flags & CS_FL_KILL_CONN)
3555 h2s->flags |= H2_SF_KILL_CONN;
3556
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003557 h2_do_shutw(h2s);
3558}
3559
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003560/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003561 * HTX request or response depending on the connection's side. Returns a
3562 * positive value on success, a negative value on failure, or 0 if it couldn't
3563 * proceed. May report connection errors in h2c->errcode if the frame is
3564 * non-decodable and the connection unrecoverable. In absence of connection
3565 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003566 *
3567 * The function may fold CONTINUATION frames into the initial HEADERS frame
3568 * by removing padding and next frame header, then moving the CONTINUATION
3569 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3570 * leaving a hole between the main frame and the beginning of the next one.
3571 * The possibly remaining incomplete or next frame at the end may be moved
3572 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3573 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3574 *
3575 * A buffer at the beginning of processing may look like this :
3576 *
3577 * ,---.---------.-----.--------------.--------------.------.---.
3578 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3579 * `---^---------^-----^--------------^--------------^------^---'
3580 * | | <-----> | |
3581 * area | dpl | wrap
3582 * |<--------------> |
3583 * | dfl |
3584 * |<-------------------------------------------------->|
3585 * head data
3586 *
3587 * Padding is automatically overwritten when folding, participating to the
3588 * hole size after dfl :
3589 *
3590 * ,---.------------------------.-----.--------------.------.---.
3591 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3592 * `---^------------------------^-----^--------------^------^---'
3593 * | | <-----> | |
3594 * area | hole | wrap
3595 * |<-----------------------> |
3596 * | dfl |
3597 * |<-------------------------------------------------->|
3598 * head data
3599 *
3600 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3601 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3602 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003603 *
3604 * The <flags> field must point to either the stream's flags or to a copy of it
3605 * so that the function can update the following flags :
3606 * - H2_SF_DATA_CLEN when content-length is seen
3607 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3608 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003609 *
3610 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3611 * decoding, in order to detect if we're dealing with a headers or a trailers
3612 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003613 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003614static 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 +02003615{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003616 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003617 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003618 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003619 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003620 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003621 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003622 int flen; // header frame len
3623 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003624 int ret = 0;
3625 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003626 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003627 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003628
Willy Tarreauea18f862018-12-22 20:19:26 +01003629next_frame:
3630 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3631 goto leave; // incomplete input frame
3632
3633 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3634 * this case, we'll try to paste it immediately after the initial
3635 * HEADERS frame payload and kill any possible padding. The initial
3636 * frame's length will be increased to represent the concatenation
3637 * of the two frames. The next frame is read from position <tlen>
3638 * and written at position <flen> (minus padding if some is present).
3639 */
3640 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3641 struct h2_fh hdr;
3642 int clen; // CONTINUATION frame's payload length
3643
3644 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3645 /* no more data, the buffer may be full, either due to
3646 * too large a frame or because of too large a hole that
3647 * we're going to compact at the end.
3648 */
3649 goto leave;
3650 }
3651
3652 if (hdr.ft != H2_FT_CONTINUATION) {
3653 /* RFC7540#6.10: frame of unexpected type */
3654 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3655 goto fail;
3656 }
3657
3658 if (hdr.sid != h2c->dsi) {
3659 /* RFC7540#6.10: frame of different stream */
3660 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3661 goto fail;
3662 }
3663
3664 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3665 /* RFC7540#4.2: invalid frame length */
3666 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3667 goto fail;
3668 }
3669
3670 /* detect when we must stop aggragating frames */
3671 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3672
3673 /* Take as much as we can of the CONTINUATION frame's payload */
3674 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3675 if (clen > hdr.len)
3676 clen = hdr.len;
3677
3678 /* Move the frame's payload over the padding, hole and frame
3679 * header. At least one of hole or dpl is null (see diagrams
3680 * above). The hole moves after the new aggragated frame.
3681 */
3682 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3683 h2c->dfl += clen - h2c->dpl;
3684 hole += h2c->dpl + 9;
3685 h2c->dpl = 0;
3686 goto next_frame;
3687 }
3688
3689 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003690
Willy Tarreau13278b42017-10-13 19:23:14 +02003691 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003692 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003693 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003694 copy = alloc_trash_chunk();
3695 if (!copy) {
3696 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3697 goto fail;
3698 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003699 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3700 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3701 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003702 }
3703
Willy Tarreau13278b42017-10-13 19:23:14 +02003704 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3705 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003706 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003707 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3708 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003709 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003710 }
3711
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003712 if (flen < 5) {
3713 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3714 goto fail;
3715 }
3716
Willy Tarreau13278b42017-10-13 19:23:14 +02003717 hdrs += 5; // stream dep = 4, weight = 1
3718 flen -= 5;
3719 }
3720
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003721 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003722 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003723 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003724 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003725
Willy Tarreau937f7602018-02-26 15:22:17 +01003726 /* we can't retry a failed decompression operation so we must be very
3727 * careful not to take any risks. In practice the output buffer is
3728 * always empty except maybe for trailers, in which case we simply have
3729 * to wait for the upper layer to finish consuming what is available.
3730 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003731
3732 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003733 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003734 if (!htx_is_empty(htx)) {
3735 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003736 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003737 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003738 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003739 if (b_data(rxbuf)) {
3740 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003741 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003742 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003743
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003744 rxbuf->head = 0;
3745 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003746 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003747
Willy Tarreau25919232019-01-03 14:48:18 +01003748 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003749 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3750 sizeof(list)/sizeof(list[0]), tmp);
3751 if (outlen < 0) {
3752 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3753 goto fail;
3754 }
3755
Willy Tarreau25919232019-01-03 14:48:18 +01003756 /* The PACK decompressor was updated, let's update the input buffer and
3757 * the parser's state to commit these changes and allow us to later
3758 * fail solely on the stream if needed.
3759 */
3760 b_del(&h2c->dbuf, h2c->dfl + hole);
3761 h2c->dfl = hole = 0;
3762 h2c->st0 = H2_CS_FRAME_H;
3763
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003764 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003765 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003766
Willy Tarreau88d138e2019-01-02 19:38:14 +01003767 if (*flags & H2_SF_HEADERS_RCVD)
3768 goto trailers;
3769
3770 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003771 if (htx) {
3772 /* HTX mode */
3773 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003774 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003775 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003776 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003777 } else {
3778 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003779 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003780 if (outlen > 0)
3781 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003782 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003783
3784 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003785 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003786 goto fail;
3787 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003788
Willy Tarreau174b06a2018-04-25 18:13:58 +02003789 if (msgf & H2_MSGF_BODY) {
3790 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003791 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003792 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003793 if (htx)
3794 htx->extra = *body_len;
3795 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003796 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003797 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003798 }
3799
Willy Tarreau88d138e2019-01-02 19:38:14 +01003800 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003801 /* indicate that a HEADERS frame was received for this stream, except
3802 * for 1xx responses. For 1xx responses, another HEADERS frame is
3803 * expected.
3804 */
3805 if (!(msgf & H2_MSGF_RSP_1XX))
3806 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003807
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02003808 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003809 /* Mark the end of message, either using EOM in HTX or with the
3810 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003811 * is not set during headers with END_STREAM. For HTX trailers,
3812 * we must not leave an HTX trailers block not followed by an
3813 * EOM block, the two must be atomic. Thus if we fail to emit
3814 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003815 */
3816 if (htx) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02003817 if (!htx_add_endof(htx, HTX_BLK_EOM))
Willy Tarreau88d138e2019-01-02 19:38:14 +01003818 goto fail;
3819 }
3820 else if (*flags & H2_SF_DATA_CHNK) {
3821 if (!b_putblk(rxbuf, "\r\n", 2))
3822 goto fail;
3823 }
3824 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003825
Willy Tarreau86277d42019-01-02 15:36:11 +01003826 /* success */
3827 ret = 1;
3828
Willy Tarreau68dd9852017-07-03 14:44:26 +02003829 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003830 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003831 * move the remaining data over it.
3832 */
3833 if (hole) {
3834 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3835 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3836 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3837 b_sub(&h2c->dbuf, hole);
3838 }
3839
Willy Tarreau97215ca2019-04-29 10:20:21 +02003840 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003841 /* too large frames */
3842 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003843 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003844 }
3845
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003846 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003847 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003848 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003849 return ret;
3850
Willy Tarreau68dd9852017-07-03 14:44:26 +02003851 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003852 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003853 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003854
3855 trailers:
3856 /* This is the last HEADERS frame hence a trailer */
3857
3858 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3859 /* It's a trailer but it's missing ES flag */
3860 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3861 goto fail;
3862 }
3863
Christopher Faulet54b5e212019-06-04 10:08:28 +02003864 /* Trailers terminate a DATA sequence. In HTX we always handle them. In
3865 * legacy, when using chunks, we have to emit the 0 CRLF marker first
3866 * and then handle the trailers. For other modes, the trailers are
3867 * silently dropped.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003868 */
3869 if (htx) {
Willy Tarreau5255f282019-01-03 18:41:05 +01003870 if (h2_make_htx_trailers(list, htx) <= 0)
3871 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003872 }
3873 else if (*flags & H2_SF_DATA_CHNK) {
3874 /* Legacy mode with chunked encoding : we must finalize the
3875 * data block message emit the trailing CRLF */
3876 if (!b_putblk(rxbuf, "0\r\n", 3))
3877 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003878
3879 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3880 if (outlen > 0)
3881 b_add(rxbuf, outlen);
3882 else
3883 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003884 }
3885
3886 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003887}
3888
Willy Tarreau454f9052017-10-26 19:40:35 +02003889/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3890 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3891 * in use, a new chunk is emitted for each frame. This is supposed to fit
3892 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3893 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3894 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003895 * parser state is automatically updated. Returns > 0 if it could completely
3896 * send the current frame, 0 if it couldn't complete, in which case
3897 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3898 * DATA frame can return 0 as a valid result). Stream errors are reported in
3899 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3900 * have checked the frame header and ensured that the frame was complete or the
3901 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003902 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003903static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003904{
3905 struct h2c *h2c = h2s->h2c;
3906 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003907 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003908 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003909 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003910 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003911
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003912 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003913
Olivier Houchard638b7992018-08-16 15:41:52 +02003914 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003915 if (!csbuf) {
3916 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003917 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003918 }
3919
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003920try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003921 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003922 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3923 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003924 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003925 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003926
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003927 if (flen > b_data(&h2c->dbuf)) {
3928 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003929 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003930 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003931 }
3932
Willy Tarreaua9b77962019-01-31 07:23:00 +01003933 if (htx) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003934 unsigned int sent;
3935
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003936 block1 = htx_free_data_space(htx);
3937 if (!block1) {
3938 h2c->flags |= H2_CF_DEM_SFULL;
3939 goto fail;
3940 }
3941 if (flen > block1)
3942 flen = block1;
3943
3944 /* here, flen is the max we can copy into the output buffer */
3945 block1 = b_contig_data(&h2c->dbuf, 0);
3946 if (flen > block1)
3947 flen = block1;
3948
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003949 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003950
Willy Tarreaub6563f42019-06-15 11:34:41 +02003951 b_del(&h2c->dbuf, sent);
3952 h2c->dfl -= sent;
3953 h2c->rcvd_c += sent;
3954 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003955
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003956 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreaub6563f42019-06-15 11:34:41 +02003957 h2s->body_len -= sent;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003958 htx->extra = h2s->body_len;
3959 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003960
3961 if (sent < flen) {
3962 h2c->flags |= H2_CF_DEM_SFULL;
3963 goto fail;
3964 }
3965
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003966 goto try_again;
3967 }
Willy Tarreau455d5682019-05-24 19:42:18 +02003968 else if (unlikely(b_space_wraps(csbuf) &&
3969 flen + chklen <= b_room(csbuf) &&
3970 b_data(csbuf) <= MAX_DATA_REALIGN)) {
3971 /* it doesn't fit in a single block and the buffer is fragmented, if there are
3972 * not too many data in the buffer, let's defragment it and try
3973 * again.
Willy Tarreaud755ea62018-02-26 15:44:54 +01003974 */
3975 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003976 }
3977
Willy Tarreaueba10f22018-04-25 20:44:22 +02003978 /* chunked-encoding requires more room */
3979 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003980 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003981 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3982 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3983 (chklen < 1048576) ? 4 : 8;
3984 chklen += 4; // CRLF, CRLF
3985 }
3986
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003987 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003988 if (flen + chklen > b_room(csbuf)) {
3989 if (chklen >= b_room(csbuf)) {
3990 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003991 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003992 }
3993 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003994 }
3995
3996 if (h2s->flags & H2_SF_DATA_CHNK) {
3997 /* emit the chunk size */
3998 unsigned int chksz = flen;
3999 char str[10];
4000 char *beg;
4001
4002 beg = str + sizeof(str);
4003 *--beg = '\n';
4004 *--beg = '\r';
4005 do {
4006 *--beg = hextab[chksz & 0xF];
4007 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004008 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004009 }
4010
Willy Tarreau454f9052017-10-26 19:40:35 +02004011 /* Block1 is the length of the first block before the buffer wraps,
4012 * block2 is the optional second block to reach the end of the frame.
4013 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004014 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004015 if (block1 > flen)
4016 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02004017 block2 = flen - block1;
4018
4019 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01004020 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02004021
4022 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01004023 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02004024
Willy Tarreaueba10f22018-04-25 20:44:22 +02004025 if (h2s->flags & H2_SF_DATA_CHNK) {
4026 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01004027 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02004028 }
4029
Willy Tarreau454f9052017-10-26 19:40:35 +02004030 /* now mark the input data as consumed (will be deleted from the buffer
4031 * by the caller when seeing FRAME_A after sending the window update).
4032 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004033 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004034 h2c->dfl -= flen;
4035 h2c->rcvd_c += flen;
4036 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
4037
Willy Tarreau1915ca22019-01-24 11:49:37 +01004038 if (h2s->flags & H2_SF_DATA_CLEN)
4039 h2s->body_len -= flen;
4040
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004041 if (h2c->dfl > h2c->dpl) {
4042 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01004043 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004044 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004045 }
4046
Willy Tarreau4a28da12018-01-04 14:41:00 +01004047 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004048 /* here we're done with the frame, all the payload (except padding) was
4049 * transferred.
4050 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02004051
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004052 if (h2c->dff & H2_F_DATA_END_STREAM) {
4053 if (htx) {
4054 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
4055 h2c->flags |= H2_CF_DEM_SFULL;
4056 goto fail;
4057 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01004058 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004059 else if (h2s->flags & H2_SF_DATA_CHNK) {
4060 /* emit the trailing 0 CRLF CRLF */
4061 if (b_room(csbuf) < 5) {
4062 h2c->flags |= H2_CF_DEM_SFULL;
4063 goto fail;
4064 }
4065 chklen += 5;
4066 b_putblk(csbuf, "0\r\n\r\n", 5);
4067 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02004068 }
4069
Willy Tarreaud1023bb2018-03-22 16:53:12 +01004070 h2c->rcvd_c += h2c->dpl;
4071 h2c->rcvd_s += h2c->dpl;
4072 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004073 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004074 if (htx)
4075 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004076 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004077 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004078 if (htx)
4079 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004080 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004081}
4082
Willy Tarreau5dd17352018-06-14 13:33:30 +02004083/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
4084 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
4085 * number of bytes sent. The caller must check the stream's status to detect
4086 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004087 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004088static 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 +02004089{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004090 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004091 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004092 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004093 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004094 struct buffer *mbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004095 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004096 int es_now = 0;
4097 int ret = 0;
4098 int hdr;
4099
4100 if (h2c_mux_busy(h2c, h2s)) {
4101 h2s->flags |= H2_SF_BLK_MBUSY;
4102 return 0;
4103 }
4104
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004105 /* First, try to parse the H1 response and index it into <list>.
4106 * NOTE! Since it comes from haproxy, we *know* that a response header
4107 * block does not wrap and we can safely read it this way without
4108 * having to realign the buffer.
4109 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004110 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004111 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004112 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01004113 /* incomplete or invalid response, this is abnormal coming from
4114 * haproxy and may only result in a bad errorfile or bad Lua code
4115 * so that won't be fixed, raise an error now.
4116 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004117 * FIXME: we should instead add the ability to only return a
4118 * 502 bad gateway. But in theory this is not supposed to
4119 * happen.
4120 */
4121 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4122 ret = 0;
4123 goto end;
4124 }
4125
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004126 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02004127
4128 /* certain statuses have no body or an empty one, regardless of
4129 * what the headers say.
4130 */
4131 if (sl.st.status >= 100 && sl.st.status < 200) {
4132 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
4133 h1m->curr_len = h1m->body_len = 0;
4134 }
4135 else if (sl.st.status == 204 || sl.st.status == 304) {
4136 /* no contents, claim c-len is present and set to zero */
4137 h1m->flags &= ~H1_MF_CHNK;
4138 h1m->flags |= H1_MF_CLEN;
4139 h1m->curr_len = h1m->body_len = 0;
4140 }
4141
Willy Tarreaubcc45952019-05-26 10:05:50 +02004142 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004143 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004144 if (!h2_get_buf(h2c, mbuf)) {
4145 h2c->flags |= H2_CF_MUX_MALLOC;
4146 h2s->flags |= H2_SF_BLK_MROOM;
4147 return 0;
4148 }
4149
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004150 chunk_reset(&outbuf);
4151
4152 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004153 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4154 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004155 break;
4156 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004157 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004158 }
4159
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004160 if (outbuf.size < 9)
4161 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004162
4163 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004164 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4165 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4166 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004167
4168 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004169 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01004170 /* this is an unparsable response */
4171 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4172 ret = 0;
4173 goto end;
4174 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01004175
4176 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004177 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004178 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004179 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004180 }
4181
4182 /* encode all headers, stop at empty name */
4183 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01004184 /* these ones do not exist in H2 and must be dropped. */
4185 if (isteq(list[hdr].n, ist("connection")) ||
4186 isteq(list[hdr].n, ist("proxy-connection")) ||
4187 isteq(list[hdr].n, ist("keep-alive")) ||
4188 isteq(list[hdr].n, ist("upgrade")) ||
4189 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004190 continue;
4191
4192 if (isteq(list[hdr].n, ist("")))
4193 break; // end
4194
4195 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4196 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004197 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004198 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004199 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004200 }
4201 }
4202
4203 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01004204 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004205 es_now = 1;
4206
4207 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004208 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004209
4210 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004211 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004212
4213 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004214 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004215
4216 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004217 b_add(mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01004218 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004219
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004220 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01004221 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004222 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01004223
Willy Tarreau801250e2018-09-11 11:45:04 +02004224 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004225 h2s->flags |= H2_SF_ES_SENT;
4226 if (h2s->st == H2_SS_OPEN)
4227 h2s->st = H2_SS_HLOC;
4228 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004229 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004230 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004231 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004232 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004233 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004234 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004235 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004236 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004237 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004238
4239 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004240
4241 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004242 //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 +02004243 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004244 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004245 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4246 goto retry;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004247 h1m_init_res(h1m);
4248 h1m->err_pos = -1; // don't care about errors on the response path
4249 h2c->flags |= H2_CF_MUX_MFULL;
4250 h2s->flags |= H2_SF_BLK_MROOM;
4251 ret = 0;
4252 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004253}
4254
Willy Tarreau5dd17352018-06-14 13:33:30 +02004255/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4256 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4257 * the number of bytes sent. The caller must check the stream's status to
4258 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004259 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004260static 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 +02004261{
4262 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004263 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004264 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004265 struct buffer *mbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004266 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004267 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004268 int es_now = 0;
4269 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004270 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004271 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004272
4273 if (h2c_mux_busy(h2c, h2s)) {
4274 h2s->flags |= H2_SF_BLK_MBUSY;
4275 goto end;
4276 }
4277
Willy Tarreaubcc45952019-05-26 10:05:50 +02004278 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004279 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004280 if (!h2_get_buf(h2c, mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004281 h2c->flags |= H2_CF_MUX_MALLOC;
4282 h2s->flags |= H2_SF_BLK_MROOM;
4283 goto end;
4284 }
4285
4286 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004287 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004288 goto end;
4289
4290 chunk_reset(&outbuf);
4291
4292 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004293 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4294 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004295 break;
4296 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004297 /* If there are pending data in the output buffer, and we have
4298 * less than 1/4 of the mbuf's size and everything fits, we'll
4299 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4300 * is full and wait, to save some slow realign calls.
4301 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004302 if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004303 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4304 goto retry;
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004305 h2c->flags |= H2_CF_MUX_MFULL;
4306 h2s->flags |= H2_SF_BLK_MROOM;
4307 goto end;
4308 }
4309
Willy Tarreaubcc45952019-05-26 10:05:50 +02004310 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004311 }
4312
4313 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004314 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4315 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004316 h2c->flags |= H2_CF_MUX_MFULL;
4317 h2s->flags |= H2_SF_BLK_MROOM;
4318 goto end;
4319 }
4320
4321 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004322 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4323 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4324 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004325
4326 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4327 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004328 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004329 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004330 break;
4331 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004332 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004333 if ((long long)size > h1m->curr_len)
4334 size = h1m->curr_len;
4335 break;
4336 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004337 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004338 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02004339 if (ret <= 0) {
Christopher Faulet307f31e2020-08-05 14:37:13 +02004340 /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ?
4341 * It should not happen except if opposite connection was closed.
4342 * Note: check there is at least something to parse to be sure the chunk crlf
4343 * is truncated.
4344 */
4345 if (max) {
4346 h1m->err_pos = ofs + max + ret;
4347 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4348 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004349 goto end;
4350 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004351 max -= ret;
4352 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004353 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004354 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004355 }
4356
Willy Tarreau801250e2018-09-11 11:45:04 +02004357 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004358 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004359 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02004360 if (ret <= 0) {
Christopher Faulet307f31e2020-08-05 14:37:13 +02004361 /* FIXME: bad contents or truncated response. how to proceed here when we're in H2 ?
4362 * It should not happen except if opposite connection was closed.
4363 * Note: check there is at least something to parse to be sure the chunke size
4364 * is truncated.
4365 */
4366 if (max) {
4367 h1m->err_pos = ofs + max + ret;
4368 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4369 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004370 goto end;
4371 }
4372
4373 size = chunk;
4374 h1m->curr_len = chunk;
4375 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004376 max -= ret;
4377 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004378 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004379 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004380 if (!size)
4381 goto send_empty;
4382 }
4383
4384 /* in MSG_DATA state, continue below */
4385 size = h1m->curr_len;
4386 break;
4387 }
4388
4389 /* we have in <size> the exact number of bytes we need to copy from
4390 * the H1 buffer. We need to check this against the connection's and
4391 * the stream's send windows, and to ensure that this fits in the max
4392 * frame size and in the buffer's available space minus 9 bytes (for
4393 * the frame header). The connection's flow control is applied last so
4394 * that we can use a separate list of streams which are immediately
4395 * unblocked on window opening. Note: we don't implement padding.
4396 */
4397
Willy Tarreau5dd17352018-06-14 13:33:30 +02004398 if (size > max)
4399 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004400
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004401 if (size > h2s_mws(h2s))
4402 size = h2s_mws(h2s);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004403
4404 if (size <= 0) {
4405 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004406 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004407 LIST_DEL_INIT(&h2s->list);
Willy Tarreau55dc0842019-10-22 10:04:39 +02004408 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004409 goto end;
4410 }
4411
4412 if (h2c->mfs && size > h2c->mfs)
4413 size = h2c->mfs;
4414
4415 if (size + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02004416 /* It doesn't fit at once. If it at least fits once split and
4417 * the amount of data to move is low, let's defragment the
4418 * buffer now.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004419 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004420 if (b_space_wraps(mbuf) &&
4421 (size + 9 <= b_room(mbuf)) &&
4422 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004423 goto realign_again;
4424 size = outbuf.size - 9;
4425 }
4426
4427 if (size <= 0) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004428 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4429 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004430 h2c->flags |= H2_CF_MUX_MFULL;
4431 h2s->flags |= H2_SF_BLK_MROOM;
4432 goto end;
4433 }
4434
4435 if (size > h2c->mws)
4436 size = h2c->mws;
4437
4438 if (size <= 0) {
4439 h2s->flags |= H2_SF_BLK_MFCTL;
4440 goto end;
4441 }
4442
4443 /* copy whatever we can */
4444 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004445 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004446 if (ret == 1)
4447 len2 = 0;
4448
4449 if (!ret || len1 + len2 < size) {
4450 /* FIXME: must normally never happen */
4451 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4452 goto end;
4453 }
4454
4455 /* limit len1/len2 to size */
4456 if (len1 + len2 > size) {
4457 int sub = len1 + len2 - size;
4458
4459 if (len2 > sub)
4460 len2 -= sub;
4461 else {
4462 sub -= len2;
4463 len2 = 0;
4464 len1 -= sub;
4465 }
4466 }
4467
4468 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004469 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004470 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004471 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004472
4473 send_empty:
4474 /* we may need to add END_STREAM */
4475 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4476 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004477 *
4478 * FIXME: what we do here is not correct because we send end_stream
4479 * before knowing if we'll have to send a HEADERS frame for the
4480 * trailers. More importantly we're not consuming the trailing CRLF
4481 * after the end of trailers, so it will be left to the caller to
4482 * eat it. The right way to do it would be to measure trailers here
4483 * and to send ES only if there are no trailers.
4484 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004485 */
4486 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004487 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004488 es_now = 1;
4489
4490 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004491 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004492
4493 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004494 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004495
4496 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004497 b_add(mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004498
4499 /* consume incoming H1 response */
4500 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004501 max -= size;
4502 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004503 total += size;
4504 h1m->curr_len -= size;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004505 h2s->sws -= size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004506 h2c->mws -= size;
4507
4508 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004509 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004510 goto new_frame;
4511 }
4512 }
4513
4514 if (es_now) {
4515 if (h2s->st == H2_SS_OPEN)
4516 h2s->st = H2_SS_HLOC;
4517 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004518 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004519
Willy Tarreau35a62702018-02-27 15:37:25 +01004520 if (!(h1m->flags & H1_MF_CHNK)) {
4521 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004522 total += max;
4523 ofs += max;
4524 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004525
Willy Tarreau801250e2018-09-11 11:45:04 +02004526 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004527 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004528
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004529 h2s->flags |= H2_SF_ES_SENT;
4530 }
4531
4532 end:
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004533 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 +02004534 return total;
4535}
4536
Willy Tarreau115e83b2018-12-01 19:17:53 +01004537/* Try to send a HEADERS frame matching HTX response present in HTX message
4538 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4539 * must check the stream's status to detect any error which might have happened
4540 * subsequently to a successful send. The htx blocks are automatically removed
4541 * from the message. The htx message is assumed to be valid since produced from
4542 * the internal code, hence it contains a start line, an optional series of
4543 * header blocks and an end of header, otherwise an invalid frame could be
4544 * emitted and the resulting htx message could be left in an inconsistent state.
4545 */
4546static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4547{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004548 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004549 struct h2c *h2c = h2s->h2c;
4550 struct htx_blk *blk;
4551 struct htx_blk *blk_end;
4552 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004553 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004554 struct htx_sl *sl;
4555 enum htx_blk_type type;
4556 int es_now = 0;
4557 int ret = 0;
4558 int hdr;
4559 int idx;
4560
4561 if (h2c_mux_busy(h2c, h2s)) {
4562 h2s->flags |= H2_SF_BLK_MBUSY;
4563 return 0;
4564 }
4565
Willy Tarreau115e83b2018-12-01 19:17:53 +01004566 /* determine the first block which must not be deleted, blk_end may
4567 * be NULL if all blocks have to be deleted.
4568 */
4569 idx = htx_get_head(htx);
4570 blk_end = NULL;
4571 while (idx != -1) {
4572 type = htx_get_blk_type(htx_get_blk(htx, idx));
4573 idx = htx_get_next(htx, idx);
4574 if (type == HTX_BLK_EOH) {
4575 if (idx != -1)
4576 blk_end = htx_get_blk(htx, idx);
4577 break;
4578 }
4579 }
4580
4581 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004582 blk = htx_get_head_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01004583 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_RES_SL);
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004584 ALREADY_CHECKED(blk);
4585 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004586 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004587 if (h2s->status < 100 || h2s->status > 999)
4588 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004589
4590 /* and the rest of the headers, that we dump starting at header 0 */
4591 hdr = 0;
4592
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004593 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004594 while ((idx = htx_get_next(htx, idx)) != -1) {
4595 blk = htx_get_blk(htx, idx);
4596 type = htx_get_blk_type(blk);
4597
4598 if (type == HTX_BLK_UNUSED)
4599 continue;
4600
4601 if (type != HTX_BLK_HDR)
4602 break;
4603
4604 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4605 goto fail;
4606
4607 list[hdr].n = htx_get_blk_name(htx, blk);
4608 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004609 hdr++;
4610 }
4611
4612 /* marker for end of headers */
4613 list[hdr].n = ist("");
4614
4615 if (h2s->status == 204 || h2s->status == 304) {
4616 /* no contents, claim c-len is present and set to zero */
4617 es_now = 1;
4618 }
4619
Willy Tarreau9c218e72019-05-26 10:08:28 +02004620 mbuf = br_tail(h2c->mbuf);
4621 retry:
4622 if (!h2_get_buf(h2c, mbuf)) {
4623 h2c->flags |= H2_CF_MUX_MALLOC;
4624 h2s->flags |= H2_SF_BLK_MROOM;
4625 return 0;
4626 }
4627
Willy Tarreau115e83b2018-12-01 19:17:53 +01004628 chunk_reset(&outbuf);
4629
4630 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004631 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4632 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004633 break;
4634 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004635 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004636 }
4637
4638 if (outbuf.size < 9)
4639 goto full;
4640
4641 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4642 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4643 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4644 outbuf.data = 9;
4645
4646 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004647 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004648 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004649 goto realign_again;
4650 goto full;
4651 }
4652
4653 /* encode all headers, stop at empty name */
4654 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4655 /* these ones do not exist in H2 and must be dropped. */
4656 if (isteq(list[hdr].n, ist("connection")) ||
4657 isteq(list[hdr].n, ist("proxy-connection")) ||
4658 isteq(list[hdr].n, ist("keep-alive")) ||
4659 isteq(list[hdr].n, ist("upgrade")) ||
4660 isteq(list[hdr].n, ist("transfer-encoding")))
4661 continue;
4662
4663 if (isteq(list[hdr].n, ist("")))
4664 break; // end
4665
4666 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4667 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004668 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004669 goto realign_again;
4670 goto full;
4671 }
4672 }
4673
Christopher Faulet0b465482019-02-19 15:14:23 +01004674 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004675 * FIXME: we should also set it when we know for sure that the
4676 * content-length is zero as well as on 204/304
4677 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004678 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4679 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004680 es_now = 1;
4681
Willy Tarreau927b88b2019-03-04 08:03:25 +01004682 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004683 es_now = 1;
4684
4685 /* update the frame's size */
4686 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4687
4688 if (es_now)
4689 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4690
4691 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004692 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004693
4694 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4695 * 1xx responses, another HEADERS frame is expected.
4696 */
4697 if (h2s->status >= 200 || h2s->status == 101)
4698 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004699
Willy Tarreau115e83b2018-12-01 19:17:53 +01004700 if (es_now) {
4701 h2s->flags |= H2_SF_ES_SENT;
4702 if (h2s->st == H2_SS_OPEN)
4703 h2s->st = H2_SS_HLOC;
4704 else
4705 h2s_close(h2s);
4706 }
4707
4708 /* OK we could properly deliver the response */
4709
4710 /* remove all header blocks including the EOH and compute the
4711 * corresponding size.
4712 *
4713 * FIXME: We should remove everything when es_now is set.
4714 */
4715 ret = 0;
4716 idx = htx_get_head(htx);
4717 blk = htx_get_blk(htx, idx);
4718 while (blk != blk_end) {
4719 ret += htx_get_blksz(blk);
4720 blk = htx_remove_blk(htx, blk);
4721 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004722
Christopher Faulet316934d2019-05-15 14:22:48 +02004723 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4724 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004725 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004726 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004727 end:
4728 return ret;
4729 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004730 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4731 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004732 h2c->flags |= H2_CF_MUX_MFULL;
4733 h2s->flags |= H2_SF_BLK_MROOM;
4734 ret = 0;
4735 goto end;
4736 fail:
4737 /* unparsable HTX messages, too large ones to be produced in the local
4738 * list etc go here (unrecoverable errors).
4739 */
4740 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4741 ret = 0;
4742 goto end;
4743}
4744
Willy Tarreau80739692018-10-05 11:35:57 +02004745/* Try to send a HEADERS frame matching HTX request present in HTX message
4746 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4747 * must check the stream's status to detect any error which might have happened
4748 * subsequently to a successful send. The htx blocks are automatically removed
4749 * from the message. The htx message is assumed to be valid since produced from
4750 * the internal code, hence it contains a start line, an optional series of
4751 * header blocks and an end of header, otherwise an invalid frame could be
4752 * emitted and the resulting htx message could be left in an inconsistent state.
4753 */
4754static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4755{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004756 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004757 struct h2c *h2c = h2s->h2c;
4758 struct htx_blk *blk;
4759 struct htx_blk *blk_end;
4760 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004761 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004762 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004763 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004764 enum htx_blk_type type;
4765 int es_now = 0;
4766 int ret = 0;
4767 int hdr;
4768 int idx;
4769
4770 if (h2c_mux_busy(h2c, h2s)) {
4771 h2s->flags |= H2_SF_BLK_MBUSY;
4772 return 0;
4773 }
4774
Willy Tarreau80739692018-10-05 11:35:57 +02004775 /* determine the first block which must not be deleted, blk_end may
4776 * be NULL if all blocks have to be deleted.
4777 */
4778 idx = htx_get_head(htx);
4779 blk_end = NULL;
4780 while (idx != -1) {
4781 type = htx_get_blk_type(htx_get_blk(htx, idx));
4782 idx = htx_get_next(htx, idx);
4783 if (type == HTX_BLK_EOH) {
4784 if (idx != -1)
4785 blk_end = htx_get_blk(htx, idx);
4786 break;
4787 }
4788 }
4789
4790 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004791 blk = htx_get_head_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +01004792 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004793 ALREADY_CHECKED(blk);
4794 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004795 meth = htx_sl_req_meth(sl);
4796 path = htx_sl_req_uri(sl);
4797
4798 /* and the rest of the headers, that we dump starting at header 0 */
4799 hdr = 0;
4800
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004801 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004802 while ((idx = htx_get_next(htx, idx)) != -1) {
4803 blk = htx_get_blk(htx, idx);
4804 type = htx_get_blk_type(blk);
4805
4806 if (type == HTX_BLK_UNUSED)
4807 continue;
4808
4809 if (type != HTX_BLK_HDR)
4810 break;
4811
4812 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4813 goto fail;
4814
4815 list[hdr].n = htx_get_blk_name(htx, blk);
4816 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004817 hdr++;
4818 }
4819
4820 /* marker for end of headers */
4821 list[hdr].n = ist("");
4822
Willy Tarreau9c218e72019-05-26 10:08:28 +02004823 mbuf = br_tail(h2c->mbuf);
4824 retry:
4825 if (!h2_get_buf(h2c, mbuf)) {
4826 h2c->flags |= H2_CF_MUX_MALLOC;
4827 h2s->flags |= H2_SF_BLK_MROOM;
4828 return 0;
4829 }
4830
Willy Tarreau80739692018-10-05 11:35:57 +02004831 chunk_reset(&outbuf);
4832
4833 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004834 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4835 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004836 break;
4837 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004838 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004839 }
4840
4841 if (outbuf.size < 9)
4842 goto full;
4843
4844 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4845 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4846 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4847 outbuf.data = 9;
4848
4849 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004850 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004851 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004852 goto realign_again;
4853 goto full;
4854 }
4855
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004856 /* RFC7540 #8.3: the CONNECT method must have :
4857 * - :authority set to the URI part (host:port)
4858 * - :method set to CONNECT
4859 * - :scheme and :path omitted
4860 */
4861 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4862 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004863 struct ist scheme;
4864
4865 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4866 scheme = ist("http");
4867 else
4868 scheme = ist("https");
4869
4870 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004871 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004872 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004873 goto realign_again;
4874 goto full;
4875 }
Willy Tarreau80739692018-10-05 11:35:57 +02004876
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004877 /* encode the path, which necessarily is the second one */
4878 if (!hpack_encode_path(&outbuf, path)) {
4879 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004880 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004881 goto realign_again;
4882 goto full;
4883 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004884
4885 /* look for the Host header and place it in :authority */
4886 auth = ist2(NULL, 0);
4887 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4888 if (isteq(list[hdr].n, ist("")))
4889 break; // end
4890
4891 if (isteq(list[hdr].n, ist("host"))) {
4892 auth = list[hdr].v;
4893 break;
4894 }
4895 }
4896 }
4897 else {
4898 /* for CONNECT, :authority is taken from the path */
4899 auth = path;
4900 }
4901
4902 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4903 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004904 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004905 goto realign_again;
4906 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004907 }
4908
4909 /* encode all headers, stop at empty name */
4910 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004911 struct ist n = list[hdr].n;
4912 struct ist v = list[hdr].v;
4913
Willy Tarreau80739692018-10-05 11:35:57 +02004914 /* these ones do not exist in H2 and must be dropped. */
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004915 if (isteq(n, ist("connection")) ||
4916 isteq(n, ist("host")) ||
4917 isteq(n, ist("proxy-connection")) ||
4918 isteq(n, ist("keep-alive")) ||
4919 isteq(n, ist("upgrade")) ||
4920 isteq(n, ist("transfer-encoding")))
Willy Tarreau80739692018-10-05 11:35:57 +02004921 continue;
4922
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004923 if (isteq(n, ist("te"))) {
4924 /* "te" may only be sent with "trailers" if this value
4925 * is present, otherwise it must be deleted.
4926 */
4927 v = istist(v, ist("trailers"));
4928 if (!v.ptr || (v.len > 8 && v.ptr[8] != ','))
4929 continue;
4930 v = ist("trailers");
4931 }
4932
4933 if (isteq(n, ist("")))
Willy Tarreau80739692018-10-05 11:35:57 +02004934 break; // end
4935
Willy Tarreau4928b6b2020-01-24 09:07:53 +01004936 if (!hpack_encode_header(&outbuf, n, v)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004937 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004938 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004939 goto realign_again;
4940 goto full;
4941 }
4942 }
4943
4944 /* we may need to add END_STREAM if we have no body :
4945 * - request already closed, or :
4946 * - no transfer-encoding, and :
4947 * - no content-length or content-length:0
4948 * Fixme: this doesn't take into account CONNECT requests.
4949 */
4950 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4951 es_now = 1;
4952
4953 if (sl->flags & HTX_SL_F_BODYLESS)
4954 es_now = 1;
4955
Willy Tarreau927b88b2019-03-04 08:03:25 +01004956 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004957 es_now = 1;
4958
4959 /* update the frame's size */
4960 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4961
4962 if (es_now)
4963 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4964
4965 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004966 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004967 h2s->flags |= H2_SF_HEADERS_SENT;
4968 h2s->st = H2_SS_OPEN;
4969
Willy Tarreau80739692018-10-05 11:35:57 +02004970 if (es_now) {
4971 // trim any possibly pending data (eg: inconsistent content-length)
4972 h2s->flags |= H2_SF_ES_SENT;
4973 h2s->st = H2_SS_HLOC;
4974 }
4975
4976 /* remove all header blocks including the EOH and compute the
4977 * corresponding size.
4978 *
4979 * FIXME: We should remove everything when es_now is set.
4980 */
4981 ret = 0;
4982 idx = htx_get_head(htx);
4983 blk = htx_get_blk(htx, idx);
4984 while (blk != blk_end) {
4985 ret += htx_get_blksz(blk);
4986 blk = htx_remove_blk(htx, blk);
4987 }
4988
Christopher Faulet316934d2019-05-15 14:22:48 +02004989 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4990 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004991 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004992 }
Willy Tarreau80739692018-10-05 11:35:57 +02004993
4994 end:
4995 return ret;
4996 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004997 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4998 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004999 h2c->flags |= H2_CF_MUX_MFULL;
5000 h2s->flags |= H2_SF_BLK_MROOM;
5001 ret = 0;
5002 goto end;
5003 fail:
5004 /* unparsable HTX messages, too large ones to be produced in the local
5005 * list etc go here (unrecoverable errors).
5006 */
5007 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5008 ret = 0;
5009 goto end;
5010}
5011
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005012/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01005013 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
5014 * caller must check the stream's status to detect any error which might have
5015 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02005016 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005017 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005018static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005019{
5020 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005021 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005022 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005023 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005024 size_t total = 0;
5025 int es_now = 0;
5026 int bsize; /* htx block size */
5027 int fsize; /* h2 frame size */
5028 struct htx_blk *blk;
5029 enum htx_blk_type type;
5030 int idx;
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005031 int trunc_out; /* non-zero if truncated on out buf */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005032
5033 if (h2c_mux_busy(h2c, h2s)) {
5034 h2s->flags |= H2_SF_BLK_MBUSY;
5035 goto end;
5036 }
5037
Willy Tarreau98de12a2018-12-12 07:03:00 +01005038 htx = htx_from_buf(buf);
5039
Christopher Faulet54b5e212019-06-04 10:08:28 +02005040 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
5041 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
5042 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005043 */
5044
5045 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01005046 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005047 goto end;
5048
5049 idx = htx_get_head(htx);
5050 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02005051 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005052 bsize = htx_get_blksz(blk);
5053 fsize = bsize;
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005054 trunc_out = 0;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005055
Christopher Faulet54b5e212019-06-04 10:08:28 +02005056 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01005057 if (h2s->flags & H2_SF_ES_SENT) {
5058 /* ES already sent */
5059 htx_remove_blk(htx, blk);
5060 total++; // EOM counts as one byte
5061 count--;
5062 goto end;
5063 }
5064 }
5065 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005066 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005067
Willy Tarreau9c218e72019-05-26 10:08:28 +02005068 mbuf = br_tail(h2c->mbuf);
5069 retry:
5070 if (!h2_get_buf(h2c, mbuf)) {
5071 h2c->flags |= H2_CF_MUX_MALLOC;
5072 h2s->flags |= H2_SF_BLK_MROOM;
5073 goto end;
5074 }
5075
Willy Tarreau98de12a2018-12-12 07:03:00 +01005076 /* Perform some optimizations to reduce the number of buffer copies.
5077 * First, if the mux's buffer is empty and the htx area contains
5078 * exactly one data block of the same size as the requested count, and
5079 * this count fits within the frame size, the stream's window size, and
5080 * the connection's window size, then it's possible to simply swap the
5081 * caller's buffer with the mux's output buffer and adjust offsets and
5082 * length to match the entire DATA HTX block in the middle. In this
5083 * case we perform a true zero-copy operation from end-to-end. This is
5084 * the situation that happens all the time with large files. Second, if
5085 * this is not possible, but the mux's output buffer is empty, we still
5086 * have an opportunity to avoid the copy to the intermediary buffer, by
5087 * making the intermediary buffer's area point to the output buffer's
5088 * area. In this case we want to skip the HTX header to make sure that
5089 * copies remain aligned and that this operation remains possible all
5090 * the time. This goes for headers, data blocks and any data extracted
5091 * from the HTX blocks.
5092 */
5093 if (unlikely(fsize == count &&
5094 htx->used == 1 && type == HTX_BLK_DATA &&
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005095 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005096 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005097
Willy Tarreaubcc45952019-05-26 10:05:50 +02005098 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005099 /* Too bad there are data left there. We're willing to memcpy/memmove
5100 * up to 1/4 of the buffer, which means that it's OK to copy a large
5101 * frame into a buffer containing few data if it needs to be realigned,
5102 * and that it's also OK to copy few data without realigning. Otherwise
5103 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01005104 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005105 if (fsize + 9 <= b_room(mbuf) &&
5106 (b_data(mbuf) <= b_size(mbuf) / 4 ||
5107 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01005108 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005109
Willy Tarreau9c218e72019-05-26 10:08:28 +02005110 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5111 goto retry;
5112
Willy Tarreau98de12a2018-12-12 07:03:00 +01005113 h2c->flags |= H2_CF_MUX_MFULL;
5114 h2s->flags |= H2_SF_BLK_MROOM;
5115 goto end;
5116 }
5117
5118 /* map an H2 frame to the HTX block so that we can put the
5119 * frame header there.
5120 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005121 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
5122 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005123
5124 /* prepend an H2 DATA frame header just before the DATA block */
5125 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5126 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5127 h2_set_frame_size(outbuf.area, fsize);
5128
5129 /* update windows */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005130 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005131 h2c->mws -= fsize;
5132
5133 /* and exchange with our old area */
5134 buf->area = old_area;
5135 buf->data = buf->head = 0;
5136 total += fsize;
5137 goto end;
5138 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005139
Willy Tarreau98de12a2018-12-12 07:03:00 +01005140 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005141 /* for DATA and EOM we'll have to emit a frame, even if empty */
5142
5143 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005144 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5145 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005146 break;
5147 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005148 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005149 }
5150
5151 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005152 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5153 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005154 h2c->flags |= H2_CF_MUX_MFULL;
5155 h2s->flags |= H2_SF_BLK_MROOM;
5156 goto end;
5157 }
5158
5159 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5160 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5161 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5162 outbuf.data = 9;
5163
5164 /* we have in <fsize> the exact number of bytes we need to copy from
5165 * the HTX buffer. We need to check this against the connection's and
5166 * the stream's send windows, and to ensure that this fits in the max
5167 * frame size and in the buffer's available space minus 9 bytes (for
5168 * the frame header). The connection's flow control is applied last so
5169 * that we can use a separate list of streams which are immediately
5170 * unblocked on window opening. Note: we don't implement padding.
5171 */
5172
5173 /* EOM is presented with bsize==1 but would lead to the emission of an
5174 * empty frame, thus we force it to zero here.
5175 */
5176 if (type == HTX_BLK_EOM)
5177 bsize = fsize = 0;
5178
5179 if (!fsize)
5180 goto send_empty;
5181
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005182 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005183 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005184 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005185 LIST_DEL_INIT(&h2s->list);
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005186 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005187 goto end;
5188 }
5189
Willy Tarreauee573762018-12-04 15:25:57 +01005190 if (fsize > count)
5191 fsize = count;
5192
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005193 if (fsize > h2s_mws(h2s))
5194 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005195
5196 if (h2c->mfs && fsize > h2c->mfs)
5197 fsize = h2c->mfs; // >0
5198
5199 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005200 /* It doesn't fit at once. If it at least fits once split and
5201 * the amount of data to move is low, let's defragment the
5202 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005203 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005204 if (b_space_wraps(mbuf) &&
5205 (fsize + 9 <= b_room(mbuf)) &&
5206 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005207 goto realign_again;
5208 fsize = outbuf.size - 9;
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005209 trunc_out = 1;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005210
5211 if (fsize <= 0) {
5212 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005213 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5214 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005215 h2c->flags |= H2_CF_MUX_MFULL;
5216 h2s->flags |= H2_SF_BLK_MROOM;
5217 goto end;
5218 }
5219 }
5220
5221 if (h2c->mws <= 0) {
5222 h2s->flags |= H2_SF_BLK_MFCTL;
5223 goto end;
5224 }
5225
5226 if (fsize > h2c->mws)
5227 fsize = h2c->mws;
5228
5229 /* now let's copy this this into the output buffer */
5230 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005231 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005232 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005233 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005234
5235 send_empty:
5236 /* update the frame's size */
5237 h2_set_frame_size(outbuf.area, fsize);
5238
5239 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5240 * meeting EOM. We should optimize this later.
5241 */
5242 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005243 total++; // EOM counts as one byte
5244 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005245 es_now = 1;
5246 }
5247
5248 if (es_now)
5249 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5250
5251 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005252 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005253
5254 /* consume incoming HTX block, including EOM */
5255 total += fsize;
5256 if (fsize == bsize) {
5257 htx_remove_blk(htx, blk);
5258 if (fsize)
5259 goto new_frame;
5260 } else {
5261 /* we've truncated this block */
5262 htx_cut_data_blk(htx, blk, fsize);
Willy Tarreau5fc1cfa2020-01-14 11:42:59 +01005263 if (trunc_out)
5264 goto new_frame;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005265 }
5266
5267 if (es_now) {
5268 if (h2s->st == H2_SS_OPEN)
5269 h2s->st = H2_SS_HLOC;
5270 else
5271 h2s_close(h2s);
5272
5273 h2s->flags |= H2_SF_ES_SENT;
5274 }
5275
5276 end:
5277 return total;
5278}
5279
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005280/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5281 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5282 * processed. The caller must check the stream's status to detect any error
5283 * which might have happened subsequently to a successful send. The htx blocks
5284 * are automatically removed from the message. The htx message is assumed to be
5285 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005286 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005287 * as a single frame. The ES flag is always set.
5288 */
5289static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5290{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005291 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005292 struct h2c *h2c = h2s->h2c;
5293 struct htx_blk *blk;
5294 struct htx_blk *blk_end;
5295 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005296 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005297 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005298 int ret = 0;
5299 int hdr;
5300 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005301
5302 if (h2c_mux_busy(h2c, h2s)) {
5303 h2s->flags |= H2_SF_BLK_MBUSY;
5304 goto end;
5305 }
5306
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005307 /* determine the first block which must not be deleted, blk_end may
5308 * be NULL if all blocks have to be deleted. also get trailers.
5309 */
5310 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005311 blk_end = NULL;
5312
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005313 hdr = 0;
5314 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005315 blk = htx_get_blk(htx, idx);
5316 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005317 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005318 if (type == HTX_BLK_UNUSED)
5319 continue;
5320
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005321 if (type == HTX_BLK_EOT) {
5322 if (idx != -1)
5323 blk_end = blk;
5324 break;
5325 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005326 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005327 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005328
5329 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5330 goto fail;
5331
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005332 list[hdr].n = htx_get_blk_name(htx, blk);
5333 list[hdr].v = htx_get_blk_value(htx, blk);
5334 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005335 }
5336
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005337 /* marker for end of trailers */
5338 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005339
Willy Tarreau9c218e72019-05-26 10:08:28 +02005340 mbuf = br_tail(h2c->mbuf);
5341 retry:
5342 if (!h2_get_buf(h2c, mbuf)) {
5343 h2c->flags |= H2_CF_MUX_MALLOC;
5344 h2s->flags |= H2_SF_BLK_MROOM;
5345 goto end;
5346 }
5347
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005348 chunk_reset(&outbuf);
5349
5350 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005351 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5352 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005353 break;
5354 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005355 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005356 }
5357
5358 if (outbuf.size < 9)
5359 goto full;
5360
5361 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5362 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5363 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5364 outbuf.data = 9;
5365
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005366 /* encode all headers */
5367 for (idx = 0; idx < hdr; idx++) {
5368 /* these ones do not exist in H2 or must not appear in
5369 * trailers and must be dropped.
5370 */
5371 if (isteq(list[idx].n, ist("host")) ||
5372 isteq(list[idx].n, ist("content-length")) ||
5373 isteq(list[idx].n, ist("connection")) ||
5374 isteq(list[idx].n, ist("proxy-connection")) ||
5375 isteq(list[idx].n, ist("keep-alive")) ||
5376 isteq(list[idx].n, ist("upgrade")) ||
5377 isteq(list[idx].n, ist("te")) ||
5378 isteq(list[idx].n, ist("transfer-encoding")))
5379 continue;
5380
5381 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5382 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005383 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005384 goto realign_again;
5385 goto full;
5386 }
5387 }
5388
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005389 if (outbuf.data == 9) {
5390 /* here we have a problem, we have nothing to emit (either we
5391 * received an empty trailers block followed or we removed its
5392 * contents above). Because of this we can't send a HEADERS
5393 * frame, so we have to cheat and instead send an empty DATA
5394 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005395 */
5396 outbuf.area[3] = H2_FT_DATA;
5397 outbuf.area[4] = H2_F_DATA_END_STREAM;
5398 }
5399
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005400 /* update the frame's size */
5401 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5402
5403 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005404 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005405 h2s->flags |= H2_SF_ES_SENT;
5406
5407 if (h2s->st == H2_SS_OPEN)
5408 h2s->st = H2_SS_HLOC;
5409 else
5410 h2s_close(h2s);
5411
5412 /* OK we could properly deliver the response */
5413 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005414 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005415 ret = 0;
5416 idx = htx_get_head(htx);
5417 blk = htx_get_blk(htx, idx);
5418 while (blk != blk_end) {
5419 ret += htx_get_blksz(blk);
5420 blk = htx_remove_blk(htx, blk);
5421 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005422
5423 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5424 ret += htx_get_blksz(blk_end);
5425 htx_remove_blk(htx, blk_end);
5426 }
5427
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005428 end:
5429 return ret;
5430 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005431 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5432 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005433 h2c->flags |= H2_CF_MUX_MFULL;
5434 h2s->flags |= H2_SF_BLK_MROOM;
5435 ret = 0;
5436 goto end;
5437 fail:
5438 /* unparsable HTX messages, too large ones to be produced in the local
5439 * list etc go here (unrecoverable errors).
5440 */
5441 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5442 ret = 0;
5443 goto end;
5444}
5445
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005446/* Called from the upper layer, to subscribe to events, such as being able to send.
5447 * The <param> argument here is supposed to be a pointer to a wait_event struct
5448 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5449 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5450 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5451 * returns 0 except for the error above.
5452 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005453static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5454{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005455 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005456 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005457 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005458
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005459 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005460 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005461 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5462 sw->events |= SUB_RETRY_RECV;
5463 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005464 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005465 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005466 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005467 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005468 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5469 sw->events |= SUB_RETRY_SEND;
5470 h2s->send_wait = sw;
5471 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5472 !LIST_ADDED(&h2s->list)) {
5473 if (h2s->flags & H2_SF_BLK_MFCTL)
5474 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5475 else
5476 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005477 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005478 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005479 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005480 if (event_type != 0)
5481 return -1;
5482 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005483}
5484
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005485/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5486 * The <param> argument here is supposed to be a pointer to the same wait_event
5487 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5488 * It always returns zero.
5489 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005490static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5491{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005492 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005493 struct h2s *h2s = cs->ctx;
5494
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005495 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005496 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005497 BUG_ON(h2s->recv_wait != sw);
5498 sw->events &= ~SUB_RETRY_RECV;
5499 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005500 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005501 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005502 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005503 BUG_ON(h2s->send_wait != sw);
5504 LIST_DEL(&h2s->list);
5505 LIST_INIT(&h2s->list);
5506 sw->events &= ~SUB_RETRY_SEND;
5507 /* We were about to send, make sure it does not happen */
5508 if (LIST_ADDED(&h2s->sending_list) &&
5509 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02005510 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02005511 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005512 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005513 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005514 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005515 return 0;
5516}
5517
5518
Olivier Houchard511efea2018-08-16 15:30:32 +02005519/* Called from the upper layer, to receive data */
5520static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5521{
Olivier Houchard638b7992018-08-16 15:41:52 +02005522 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005523 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005524 struct htx *h2s_htx = NULL;
5525 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005526 size_t ret = 0;
5527
5528 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005529 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau86724e22018-12-01 23:19:43 +01005530 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005531 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005532 /* Here htx_to_buf() will set buffer data to 0 because
5533 * the HTX is empty.
5534 */
5535 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005536 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005537 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005538
Christopher Faulet156852b2019-05-16 11:29:13 +02005539 ret = h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005540 buf_htx = htx_from_buf(buf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005541
Christopher Faulet2f6edc82019-05-15 10:07:59 +02005542 /* <buf> is empty and the message is small enough, swap the
5543 * buffers. */
5544 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
5545 htx_to_buf(buf_htx, buf);
5546 htx_to_buf(h2s_htx, &h2s->rxbuf);
5547 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5548 goto end;
5549 }
5550
Christopher Faulet156852b2019-05-16 11:29:13 +02005551 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005552
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005553 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
Willy Tarreau7196dd62019-03-05 10:51:11 +01005554 buf_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005555 if (htx_is_empty(buf_htx))
5556 cs->flags |= CS_FL_EOI;
5557 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005558
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005559 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005560 htx_to_buf(buf_htx, buf);
5561 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet156852b2019-05-16 11:29:13 +02005562 ret -= h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005563 }
5564 else {
5565 ret = b_xfer(buf, &h2s->rxbuf, count);
5566 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005567
Christopher Faulet37070b22019-02-14 15:12:14 +01005568 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005569 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005570 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005571 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005572 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005573 if (h2s->flags & H2_SF_ES_RCVD)
5574 cs->flags |= CS_FL_EOI;
Christopher Faulet11b10532020-10-08 15:38:41 +02005575 if (h2c_read0_pending(h2c) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02005576 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005577 if (cs->flags & CS_FL_ERR_PENDING)
5578 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005579 if (b_size(&h2s->rxbuf)) {
5580 b_free(&h2s->rxbuf);
5581 offer_buffers(NULL, tasks_run_queue);
5582 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005583 }
5584
Willy Tarreau082f5592018-11-25 08:03:32 +01005585 if (ret && h2c->dsi == h2s->id) {
5586 /* demux is blocking on this stream's buffer */
5587 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005588 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005589 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005590
Olivier Houchard511efea2018-08-16 15:30:32 +02005591 return ret;
5592}
5593
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005594/* stops all senders of this connection for example when the mux buffer is full.
5595 * They are moved from the sending_list to either fctl_list or send_list.
5596 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005597static void h2_stop_senders(struct h2c *h2c)
5598{
5599 struct h2s *h2s, *h2s_back;
5600
Olivier Houchardd360ac62019-03-22 17:37:16 +01005601 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5602 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02005603 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005604 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005605 }
5606}
5607
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005608/* Called from the upper layer, to send data from buffer <buf> for no more than
5609 * <count> bytes. Returns the number of bytes effectively sent. Some status
5610 * flags may be updated on the conn_stream.
5611 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005612static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005613{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005614 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005615 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005616 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005617 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005618 struct htx *htx;
5619 struct htx_blk *blk;
5620 enum htx_blk_type btype;
5621 uint32_t bsize;
5622 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005623
Olivier Houchardd360ac62019-03-22 17:37:16 +01005624 /* If we were not just woken because we wanted to send but couldn't,
5625 * and there's somebody else that is waiting to send, do nothing,
5626 * we will subscribe later and be put at the end of the list
5627 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005628 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005629 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5630 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005631 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005632
Olivier Houchard998410a2019-04-15 19:23:37 +02005633 /* We couldn't set it to NULL before, because we needed it in case
5634 * we had to cancel the tasklet
5635 */
5636 h2s->send_wait = NULL;
5637
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005638 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5639 return 0;
5640
Willy Tarreau902df222019-10-31 15:48:18 +01005641 if (h2s->h2c->st0 >= H2_CS_ERROR) {
5642 cs->flags |= CS_FL_ERROR;
5643 return 0;
5644 }
5645
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005646 /* htx will be enough to decide if we're using HTX or legacy */
5647 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5648
Willy Tarreau0bad0432018-06-14 16:54:01 +02005649 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005650 h2s->flags |= H2_SF_OUTGOING_DATA;
5651
Willy Tarreau751f2d02018-10-05 09:35:00 +02005652 if (h2s->id == 0) {
5653 int32_t id = h2c_get_next_sid(h2s->h2c);
5654
5655 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005656 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005657 return 0;
5658 }
5659
5660 eb32_delete(&h2s->by_id);
5661 h2s->by_id.key = h2s->id = id;
5662 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005663 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005664 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5665 }
5666
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005667 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005668 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005669 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005670 idx = htx_get_head(htx);
5671 blk = htx_get_blk(htx, idx);
5672 btype = htx_get_blk_type(blk);
5673 bsize = htx_get_blksz(blk);
5674
5675 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005676 case HTX_BLK_REQ_SL:
5677 /* start-line before headers */
5678 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5679 if (ret > 0) {
5680 total += ret;
5681 count -= ret;
5682 if (ret < bsize)
5683 goto done;
5684 }
5685 break;
5686
Willy Tarreau115e83b2018-12-01 19:17:53 +01005687 case HTX_BLK_RES_SL:
5688 /* start-line before headers */
5689 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5690 if (ret > 0) {
5691 total += ret;
5692 count -= ret;
5693 if (ret < bsize)
5694 goto done;
5695 }
5696 break;
5697
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005698 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005699 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005700 /* all these cause the emission of a DATA frame (possibly empty).
5701 * This EOM necessarily is one before trailers, as the EOM following
5702 * trailers would have been consumed by the trailers parser.
5703 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005704 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005705 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005706 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005707 total += ret;
5708 count -= ret;
5709 if (ret < bsize)
5710 goto done;
5711 }
5712 break;
5713
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005714 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005715 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005716 /* This is the first trailers block, all the subsequent ones AND
5717 * the EOM will be swallowed by the parser.
5718 */
5719 ret = h2s_htx_make_trailers(h2s, htx);
5720 if (ret > 0) {
5721 total += ret;
5722 count -= ret;
5723 if (ret < bsize)
5724 goto done;
5725 }
5726 break;
5727
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005728 default:
5729 htx_remove_blk(htx, blk);
5730 total += bsize;
5731 count -= bsize;
5732 break;
5733 }
5734 }
5735 goto done;
5736 }
5737
5738 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005739 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005740 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005741 if (h2s->h2c->flags & H2_CF_IS_BACK)
5742 ret = -1;
5743 else
5744 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005745 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005746 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005747 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005748 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005749 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005750 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005751 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005752
Willy Tarreau5dd17352018-06-14 13:33:30 +02005753 if (unlikely((int)ret <= 0)) {
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02005754 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005755 break;
5756 }
Christopher Faulet6ad7cd92020-07-23 14:50:36 +02005757
Willy Tarreau35a62702018-02-27 15:37:25 +01005758 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005759 total += count;
5760 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005761 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005762 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005763 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005764 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005765 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005766 break;
5767 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005768
5769 total += ret;
5770 count -= ret;
5771
Willy Tarreau2b778482019-05-06 15:00:22 +02005772 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005773 break;
5774
5775 if (h2s->flags & H2_SF_BLK_ANY)
5776 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005777 }
5778
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005779 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005780 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005781 /* trim any possibly pending data after we close (extra CR-LF,
5782 * unprocessed trailers, abnormal extra data, ...)
5783 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005784 total += count;
5785 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005786 }
5787
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005788 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005789 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005790 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005791 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005792 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005793 }
5794
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005795 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005796 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005797 } else {
5798 b_del(buf, total);
5799 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005800
5801 /* The mux is full, cancel the pending tasks */
5802 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5803 (h2s->flags & H2_SF_BLK_MBUSY))
5804 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005805
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005806 /* If we're running HTX, and we read the whole buffer, then pretend
5807 * we read exactly what the caller specified, as with HTX the caller
5808 * will always give the buffer size, instead of the amount of data
5809 * available.
5810 */
5811 if (htx && !b_data(buf))
5812 total = orig_count;
5813
Olivier Houchard7505f942018-08-21 18:10:44 +02005814 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005815 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005816 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02005817
Olivier Houchard7505f942018-08-21 18:10:44 +02005818 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005819 /* If we're waiting for flow control, and we got a shutr on the
5820 * connection, we will never be unlocked, so add an error on
5821 * the conn_stream.
5822 */
5823 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5824 !b_data(&h2s->h2c->dbuf) &&
5825 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5826 if (cs->flags & CS_FL_EOS)
5827 cs->flags |= CS_FL_ERROR;
5828 else
5829 cs->flags |= CS_FL_ERR_PENDING;
5830 }
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005831
5832 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL)) {
5833 /* Ok we managed to send something, leave the send_list if we were still there */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005834 LIST_DEL_INIT(&h2s->list);
5835 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005836 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005837}
5838
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005839/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005840static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005841{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005842 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005843 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005844 struct eb32_node *node;
5845 int fctl_cnt = 0;
5846 int send_cnt = 0;
5847 int tree_cnt = 0;
5848 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005849 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005850
5851 if (!h2c)
5852 return;
5853
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005854 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005855 fctl_cnt++;
5856
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005857 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005858 send_cnt++;
5859
Willy Tarreau3af37712018-12-18 14:34:41 +01005860 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005861 node = eb32_first(&h2c->streams_by_id);
5862 while (node) {
5863 h2s = container_of(node, struct h2s, by_id);
5864 tree_cnt++;
5865 if (!h2s->cs)
5866 orph_cnt++;
5867 node = eb32_next(node);
5868 }
5869
Willy Tarreau60f62682019-05-26 11:32:27 +02005870 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005871 tmbuf = br_tail(h2c->mbuf);
Willy Tarreau987c0632018-12-18 10:32:05 +01005872 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5873 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005874 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5875 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau616ac812018-07-24 14:12:42 +02005876 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5877 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005878 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005879 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5880 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5881 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005882 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5883 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5884 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005885 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5886 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005887
5888 if (h2s) {
5889 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5890 h2s, h2s->id, h2s->flags,
5891 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5892 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5893 h2s->cs);
5894 if (h2s->cs)
5895 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5896 h2s->cs->flags, h2s->cs->data);
5897 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005898}
Willy Tarreau62f52692017-10-08 23:01:42 +02005899
5900/*******************************************************/
5901/* functions below are dedicated to the config parsers */
5902/*******************************************************/
5903
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005904/* config parser for global "tune.h2.header-table-size" */
5905static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5906 struct proxy *defpx, const char *file, int line,
5907 char **err)
5908{
5909 if (too_many_args(1, args, err, NULL))
5910 return -1;
5911
5912 h2_settings_header_table_size = atoi(args[1]);
5913 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5914 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5915 return -1;
5916 }
5917 return 0;
5918}
Willy Tarreau62f52692017-10-08 23:01:42 +02005919
Willy Tarreaue6baec02017-07-27 11:45:11 +02005920/* config parser for global "tune.h2.initial-window-size" */
5921static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5922 struct proxy *defpx, const char *file, int line,
5923 char **err)
5924{
5925 if (too_many_args(1, args, err, NULL))
5926 return -1;
5927
5928 h2_settings_initial_window_size = atoi(args[1]);
5929 if (h2_settings_initial_window_size < 0) {
5930 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5931 return -1;
5932 }
5933 return 0;
5934}
5935
Willy Tarreau5242ef82017-07-27 11:47:28 +02005936/* config parser for global "tune.h2.max-concurrent-streams" */
5937static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5938 struct proxy *defpx, const char *file, int line,
5939 char **err)
5940{
5941 if (too_many_args(1, args, err, NULL))
5942 return -1;
5943
5944 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005945 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005946 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5947 return -1;
5948 }
5949 return 0;
5950}
5951
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005952/* config parser for global "tune.h2.max-frame-size" */
5953static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5954 struct proxy *defpx, const char *file, int line,
5955 char **err)
5956{
5957 if (too_many_args(1, args, err, NULL))
5958 return -1;
5959
5960 h2_settings_max_frame_size = atoi(args[1]);
5961 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5962 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5963 return -1;
5964 }
5965 return 0;
5966}
5967
Willy Tarreau62f52692017-10-08 23:01:42 +02005968
5969/****************************************/
5970/* MUX initialization and instanciation */
5971/***************************************/
5972
5973/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005974static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005975 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005976 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005977 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005978 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005979 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005980 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005981 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005982 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005983 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005984 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005985 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005986 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005987 .shutr = h2_shutr,
5988 .shutw = h2_shutw,
Olivier Houchard198d1292019-10-25 16:19:26 +02005989 .ctl = h2_ctl,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005990 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005991 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005992 .name = "H2",
5993};
5994
Christopher Faulet32f61c02018-04-10 14:33:41 +02005995/* PROTO selection : this mux registers PROTO token "h2" */
5996static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005997 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005998
Willy Tarreau0108d902018-11-25 19:14:37 +01005999INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
6000
Christopher Faulet9b579102019-04-11 22:52:25 +02006001
6002/* The mux operations */
6003static const struct mux_ops h2_htx_ops = {
6004 .init = h2_init,
6005 .wake = h2_wake,
6006 .snd_buf = h2_snd_buf,
6007 .rcv_buf = h2_rcv_buf,
6008 .subscribe = h2_subscribe,
6009 .unsubscribe = h2_unsubscribe,
6010 .attach = h2_attach,
6011 .get_first_cs = h2_get_first_cs,
6012 .detach = h2_detach,
6013 .destroy = h2_destroy,
6014 .avail_streams = h2_avail_streams,
6015 .used_streams = h2_used_streams,
6016 .shutr = h2_shutr,
6017 .shutw = h2_shutw,
Olivier Houchard198d1292019-10-25 16:19:26 +02006018 .ctl = h2_ctl,
Christopher Faulet9b579102019-04-11 22:52:25 +02006019 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02006020 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02006021 .name = "H2",
6022};
6023
Willy Tarreauf8957272018-10-03 10:25:20 +02006024static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02006025 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02006026
6027INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
6028
Willy Tarreau62f52692017-10-08 23:01:42 +02006029/* config keyword parsers */
6030static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02006031 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02006032 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02006033 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01006034 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02006035 { 0, NULL, NULL }
6036}};
6037
Willy Tarreau0108d902018-11-25 19:14:37 +01006038INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);