blob: 2b87a5268e351e32a3d1183c9f084cbc57099087 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
Willy Tarreau51330962019-05-26 09:38:07 +020081
Willy Tarreau9c218e72019-05-26 10:08:28 +020082/* 32 buffers: one for the ring's root, rest for the mbuf itself */
83#define H2C_MBUF_CNT 32
Willy Tarreau51330962019-05-26 09:38:07 +020084
Willy Tarreau5ab6b572017-09-22 08:05:00 +020085/* H2 connection descriptor */
86struct h2c {
87 struct connection *conn;
88
89 enum h2_cs st0; /* mux state */
90 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
91
92 /* 16 bit hole here */
93 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010094 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020095 int32_t max_id; /* highest ID known on this connection, <0 before preface */
96 uint32_t rcvd_c; /* newly received data to ACK for the connection */
97 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
98
99 /* states for the demux direction */
100 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200101 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200102
103 int32_t dsi; /* demux stream ID (<0 = idle) */
104 int32_t dfl; /* demux frame length (if dsi >= 0) */
105 int8_t dft; /* demux frame type (if dsi >= 0) */
106 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100107 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
108 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
110
111 /* states for the mux direction */
Willy Tarreau51330962019-05-26 09:38:07 +0200112 struct buffer mbuf[H2C_MBUF_CNT]; /* mux buffers (ring) */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200113 int32_t msi; /* mux stream ID (<0 = idle) */
114 int32_t mfl; /* mux frame length (if dsi >= 0) */
115 int8_t mft; /* mux frame type (if dsi >= 0) */
116 int8_t mff; /* mux frame flags (if dsi >= 0) */
117 /* 16 bit hole here */
118 int32_t miw; /* mux initial window size for all new streams */
119 int32_t mws; /* mux window size. Can be negative. */
120 int32_t mfs; /* mux's max frame size */
121
Willy Tarreauea392822017-10-31 10:02:25 +0100122 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100123 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100124 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200125 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100126 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100127 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200128 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100129 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200130 struct eb_root streams_by_id; /* all active streams by their ID */
131 struct list send_list; /* list of blocked streams requesting to send */
132 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreaubda92dd2019-10-02 10:49:59 +0200133 struct list blocked_list; /* list of streams blocked for other reasons (e.g. sfctl, dep) */
Olivier Houchardd846c262018-10-19 17:24:29 +0200134 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100135 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200136 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200137};
138
Willy Tarreau18312642017-10-11 07:57:07 +0200139/* H2 stream state, in h2s->st */
140enum h2_ss {
141 H2_SS_IDLE = 0, // idle
142 H2_SS_RLOC, // reserved(local)
143 H2_SS_RREM, // reserved(remote)
144 H2_SS_OPEN, // open
145 H2_SS_HREM, // half-closed(remote)
146 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200147 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200148 H2_SS_CLOSED, // closed
149 H2_SS_ENTRIES // must be last
150} __attribute__((packed));
151
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200152#define H2_SS_MASK(state) (1UL << (state))
153#define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE)
154#define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC)
155#define H2_SS_RREM_BIT (1UL << H2_SS_RREM)
156#define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN)
157#define H2_SS_HREM_BIT (1UL << H2_SS_HREM)
158#define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC)
159#define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR)
160#define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED)
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200161
Willy Tarreau18312642017-10-11 07:57:07 +0200162/* HTTP/2 stream flags (32 bit), in h2s->flags */
163#define H2_SF_NONE 0x00000000
164#define H2_SF_ES_RCVD 0x00000001
165#define H2_SF_ES_SENT 0x00000002
166
167#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
168#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
169
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200170/* stream flags indicating the reason the stream is blocked */
171#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
Willy Tarreaubda92dd2019-10-02 10:49:59 +0200172#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux (must be in send list)
173#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl (must be in fctl list)
174#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl (must be in blocked list)
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200175#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
176
Willy Tarreau454f9052017-10-26 19:40:35 +0200177/* stream flags indicating how data is supposed to be sent */
178#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
179#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
180
181/* step we're currently in when sending chunks. This is needed because we may
182 * have to transfer chunks as large as a full buffer so there's no room left
183 * for size nor crlf around.
184 */
185#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
186#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
187#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
188
189#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
190
Willy Tarreau67434202017-11-06 20:20:51 +0100191#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100192#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100193
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100194#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
195
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200196#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
197#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
Willy Tarreau3cf69fe2019-05-14 10:44:40 +0200198#define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200199
200
Willy Tarreau18312642017-10-11 07:57:07 +0200201/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
202 * it is being processed in the internal HTTP representation (H1 for now).
203 */
204struct h2s {
205 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100206 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200207 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200208 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200209 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200210 int32_t id; /* stream ID */
211 uint32_t flags; /* H2_SF_* */
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200212 int sws; /* stream window size, to be added to the mux's initial window size */
Willy Tarreau18312642017-10-11 07:57:07 +0200213 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
214 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200215 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100216 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200217 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200218 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100219 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
220 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200221 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100222 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200223};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200224
Willy Tarreauc6405142017-09-21 20:23:50 +0200225/* descriptor for an h2 frame header */
226struct h2_fh {
227 uint32_t len; /* length, host order, 24 bits */
228 uint32_t sid; /* stream id, host order, 31 bits */
229 uint8_t ft; /* frame type */
230 uint8_t ff; /* frame flags */
231};
232
Willy Tarreau8ceae722018-11-26 11:58:30 +0100233/* the h2c connection pool */
234DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
235
236/* the h2s stream pool */
237DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
238
Willy Tarreaudc572362018-12-12 08:08:05 +0100239/* The default connection window size is 65535, it may only be enlarged using
240 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
241 * we'll pretend we already received the difference between the two to send
242 * an equivalent window update to enlarge it to 2G-1.
243 */
244#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
245
Willy Tarreau455d5682019-05-24 19:42:18 +0200246/* maximum amount of data we're OK with re-aligning for buffer optimizations */
247#define MAX_DATA_REALIGN 1024
248
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200249/* a few settings from the global section */
250static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200251static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100252static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100253static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200254
Willy Tarreau2a856182017-05-16 15:20:39 +0200255/* a dmumy closed stream */
256static const struct h2s *h2_closed_stream = &(const struct h2s){
257 .cs = NULL,
258 .h2c = NULL,
259 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100260 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100261 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200262 .id = 0,
263};
264
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100265/* a dmumy closed stream returning a PROTOCOL_ERROR error */
266static const struct h2s *h2_error_stream = &(const struct h2s){
267 .cs = NULL,
268 .h2c = NULL,
269 .st = H2_SS_CLOSED,
270 .errcode = H2_ERR_PROTOCOL_ERROR,
271 .flags = 0,
272 .id = 0,
273};
274
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100275/* a dmumy closed stream returning a REFUSED_STREAM error */
276static const struct h2s *h2_refused_stream = &(const struct h2s){
277 .cs = NULL,
278 .h2c = NULL,
279 .st = H2_SS_CLOSED,
280 .errcode = H2_ERR_REFUSED_STREAM,
281 .flags = 0,
282 .id = 0,
283};
284
Willy Tarreau2a856182017-05-16 15:20:39 +0200285/* and a dummy idle stream for use with any unannounced stream */
286static const struct h2s *h2_idle_stream = &(const struct h2s){
287 .cs = NULL,
288 .h2c = NULL,
289 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100290 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200291 .id = 0,
292};
293
Olivier Houchard9f6af332018-05-25 14:04:04 +0200294static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200295static int h2_send(struct h2c *h2c);
296static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200297static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200298static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100299static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100300static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100301static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200302static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100303static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100304static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200305
Willy Tarreau534d8f92019-10-01 10:12:00 +0200306/* returns true if the connection is allowed to expire, false otherwise. A
307 * connection may expire when:
308 * - it has no stream
309 * - it has data in the mux buffer
310 * - it has streams in the blocked list
311 * - it has streams in the fctl list
312 * - it has streams in the send list
313 * Otherwise it means some streams are waiting in the data layer and it should
314 * not expire.
315 */
316static inline int h2c_may_expire(const struct h2c *h2c)
317{
318 return eb_is_empty(&h2c->streams_by_id) ||
319 br_data(h2c->mbuf) ||
320 !LIST_ISEMPTY(&h2c->blocked_list) ||
321 !LIST_ISEMPTY(&h2c->fctl_list) ||
322 !LIST_ISEMPTY(&h2c->send_list) ||
323 !LIST_ISEMPTY(&h2c->sending_list);
324}
325
Olivier Houchard7a977432019-03-21 15:47:13 +0100326static __inline int
Willy Tarreau534d8f92019-10-01 10:12:00 +0200327h2c_is_dead(const struct h2c *h2c)
Olivier Houchard7a977432019-03-21 15:47:13 +0100328{
329 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
330 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
331 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
332 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200333 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100334 (conn_xprt_read0_pending(h2c->conn) ||
335 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
336 return 1;
337
338 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100339}
340
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200341/*****************************************************/
342/* functions below are for dynamic buffer management */
343/*****************************************************/
344
Willy Tarreau315d8072017-12-10 22:17:57 +0100345/* indicates whether or not the we may call the h2_recv() function to attempt
346 * to receive data into the buffer and/or demux pending data. The condition is
347 * a bit complex due to some API limits for now. The rules are the following :
348 * - if an error or a shutdown was detected on the connection and the buffer
349 * is empty, we must not attempt to receive
350 * - if the demux buf failed to be allocated, we must not try to receive and
351 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100352 * - if no flag indicates a blocking condition, we may attempt to receive,
353 * regardless of whether the demux buffer is full or not, so that only
354 * de demux part decides whether or not to block. This is needed because
355 * the connection API indeed prevents us from re-enabling receipt that is
356 * already enabled in a polled state, so we must always immediately stop
357 * as soon as the demux can't proceed so as never to hit an end of read
358 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100359 * - otherwise must may not attempt
360 */
361static inline int h2_recv_allowed(const struct h2c *h2c)
362{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200363 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100364 (h2c->st0 >= H2_CS_ERROR ||
365 h2c->conn->flags & CO_FL_ERROR ||
366 conn_xprt_read0_pending(h2c->conn)))
367 return 0;
368
369 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100370 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100371 return 1;
372
373 return 0;
374}
375
Willy Tarreau47b515a2018-12-21 16:09:41 +0100376/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200377static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100378{
379 if (!h2_recv_allowed(h2c))
380 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200381 if ((!consider_buffer || !b_data(&h2c->dbuf))
382 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100383 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200384 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100385}
386
387
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100388/* returns true if the front connection has too many conn_streams attached */
389static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200390{
Willy Tarreaua8754662018-12-23 20:43:58 +0100391 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200392}
393
Willy Tarreau44e973f2018-03-01 17:49:30 +0100394/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
395 * flags are used to figure what buffer was requested. It returns 1 if the
396 * allocation succeeds, in which case the connection is woken up, or 0 if it's
397 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200398 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100399static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200400{
401 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100402 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200403
Willy Tarreau44e973f2018-03-01 17:49:30 +0100404 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200405 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200406 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200407 return 1;
408 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200409
Willy Tarreau51330962019-05-26 09:38:07 +0200410 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100411 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200412
413 if (h2c->flags & H2_CF_DEM_MROOM) {
414 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200415 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200416 }
Willy Tarreau14398122017-09-22 14:26:04 +0200417 return 1;
418 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100419
420 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
421 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200422 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100423 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200424 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100425 return 1;
426 }
427
Willy Tarreau14398122017-09-22 14:26:04 +0200428 return 0;
429}
430
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200431static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200432{
433 struct buffer *buf = NULL;
434
Willy Tarreauc234ae32019-05-13 17:56:11 +0200435 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100436 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
437 h2c->buf_wait.target = h2c;
438 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100439 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100440 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100441 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200442 __conn_xprt_stop_recv(h2c->conn);
443 }
444 return buf;
445}
446
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200447static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200448{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200449 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100450 b_free(bptr);
Willy Tarreau201840a2019-05-29 17:50:48 +0200451 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200452 }
453}
454
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200455static inline void h2_release_mbuf(struct h2c *h2c)
456{
457 struct buffer *buf;
458 unsigned int count = 0;
459
460 while (b_size(buf = br_head_pick(h2c->mbuf))) {
461 b_free(buf);
462 count++;
463 }
464 if (count)
Willy Tarreau201840a2019-05-29 17:50:48 +0200465 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200466}
467
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100468/* returns the number of allocatable outgoing streams for the connection taking
469 * the last_sid and the reserved ones into account.
470 */
471static inline int h2_streams_left(const struct h2c *h2c)
472{
473 int ret;
474
475 /* consider the number of outgoing streams we're allowed to create before
476 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
477 * nb_reserved is the number of streams which don't yet have an ID.
478 */
479 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
480 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
481 if (ret < 0)
482 ret = 0;
483 return ret;
484}
485
Willy Tarreau00f18a32019-01-26 12:19:01 +0100486/* returns the number of streams in use on a connection to figure if it's
487 * idle or not. We check nb_cs and not nb_streams as the caller will want
488 * to know if it was the last one after a detach().
489 */
490static int h2_used_streams(struct connection *conn)
491{
492 struct h2c *h2c = conn->ctx;
493
494 return h2c->nb_cs;
495}
496
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100497/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100498static int h2_avail_streams(struct connection *conn)
499{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100500 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100501 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100502 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100503
Willy Tarreau6afec462019-01-28 06:40:19 +0100504 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
505 * streams on the connection.
506 */
507 if (h2c->last_sid >= 0)
508 return 0;
509
Willy Tarreaud63b85d2019-10-31 15:10:03 +0100510 if (h2c->st0 >= H2_CS_ERROR)
511 return 0;
512
Willy Tarreau86949782019-01-31 10:42:05 +0100513 /* note: may be negative if a SETTINGS frame changes the limit */
514 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100515
516 /* we must also consider the limit imposed by stream IDs */
517 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100518 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100519 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100520 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
521 ret1 = MIN(ret1, ret2);
522 }
523 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100524}
525
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200526
Willy Tarreau62f52692017-10-08 23:01:42 +0200527/*****************************************************************/
528/* functions below are dedicated to the mux setup and management */
529/*****************************************************************/
530
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200531/* Initialize the mux once it's attached. For outgoing connections, the context
532 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200533 * connections from the fact that the context is still NULL (even during mux
534 * upgrades). <input> is always used as Input buffer and may contain data. It is
535 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200536 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200537static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
538 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539{
540 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100541 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542
Willy Tarreaubafbe012017-11-24 17:34:44 +0100543 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200544 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200545 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200546
Christopher Faulete9b70722019-04-08 10:46:02 +0200547 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200548 h2c->flags = H2_CF_IS_BACK;
549 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
550 if (tick_isset(prx->timeout.serverfin))
551 h2c->shut_timeout = prx->timeout.serverfin;
552 } else {
553 h2c->flags = H2_CF_NONE;
554 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
555 if (tick_isset(prx->timeout.clientfin))
556 h2c->shut_timeout = prx->timeout.clientfin;
557 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100558
Willy Tarreau0b37d652018-10-03 10:33:02 +0200559 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100560 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100561 if (tick_isset(h2c->timeout)) {
562 t = task_new(tid_bit);
563 if (!t)
564 goto fail;
565
566 h2c->task = t;
567 t->process = h2_timeout_task;
568 t->context = h2c;
569 t->expire = tick_add(now_ms, h2c->timeout);
570 }
Willy Tarreauea392822017-10-31 10:02:25 +0100571
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200572 h2c->wait_event.tasklet = tasklet_new();
573 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200574 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200575 h2c->wait_event.tasklet->process = h2_io_cb;
576 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100577 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200578
Willy Tarreau32218eb2017-09-22 08:07:25 +0200579 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
580 if (!h2c->ddht)
581 goto fail;
582
583 /* Initialise the context. */
584 h2c->st0 = H2_CS_PREFACE;
585 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100586 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200587 h2c->max_id = -1;
588 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100589 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200590 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100591 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200592 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100593 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100594 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200595
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200596 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200597 h2c->dsi = -1;
598 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100599
Willy Tarreau32218eb2017-09-22 08:07:25 +0200600 h2c->last_sid = -1;
601
Willy Tarreau51330962019-05-26 09:38:07 +0200602 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200603 h2c->miw = 65535; /* mux initial window size */
604 h2c->mws = 65535; /* mux window size */
605 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200606 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200607 LIST_INIT(&h2c->send_list);
608 LIST_INIT(&h2c->fctl_list);
Willy Tarreaubda92dd2019-10-02 10:49:59 +0200609 LIST_INIT(&h2c->blocked_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200610 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100611 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200612
Willy Tarreau3f133572017-10-31 19:21:06 +0100613 if (t)
614 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100615
Willy Tarreau01b44822018-10-03 14:26:37 +0200616 if (h2c->flags & H2_CF_IS_BACK) {
617 /* FIXME: this is temporary, for outgoing connections we need
618 * to immediately allocate a stream until the code is modified
619 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100620 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200621 */
622 struct h2s *h2s;
623
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100624 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200625 if (!h2s)
626 goto fail_stream;
627 }
628
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100629 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200630
Willy Tarreau0f383582018-10-03 14:22:21 +0200631 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200632 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200633 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200634 fail_stream:
635 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200636 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200637 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200638 if (h2c->wait_event.tasklet)
639 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100640 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200641 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200642 return -1;
643}
644
Willy Tarreau751f2d02018-10-05 09:35:00 +0200645/* returns the next allocatable outgoing stream ID for the H2 connection, or
646 * -1 if no more is allocatable.
647 */
648static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
649{
650 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100651
652 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200653 id = -1;
654 return id;
655}
656
Willy Tarreau2373acc2017-10-12 17:35:14 +0200657/* returns the stream associated with id <id> or NULL if not found */
658static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
659{
660 struct eb32_node *node;
661
Willy Tarreau751f2d02018-10-05 09:35:00 +0200662 if (id == 0)
663 return (struct h2s *)h2_closed_stream;
664
Willy Tarreau2a856182017-05-16 15:20:39 +0200665 if (id > h2c->max_id)
666 return (struct h2s *)h2_idle_stream;
667
Willy Tarreau2373acc2017-10-12 17:35:14 +0200668 node = eb32_lookup(&h2c->streams_by_id, id);
669 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200670 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200671
672 return container_of(node, struct h2s, by_id);
673}
674
Christopher Faulet73c12072019-04-08 11:23:22 +0200675/* release function. This one should be called to free all resources allocated
676 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200677 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200678static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200679{
Christopher Faulet61840e72019-04-15 09:33:32 +0200680 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200681
Willy Tarreau32218eb2017-09-22 08:07:25 +0200682 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200683 /* The connection must be aattached to this mux to be released */
684 if (h2c->conn && h2c->conn->ctx == h2c)
685 conn = h2c->conn;
686
Willy Tarreau32218eb2017-09-22 08:07:25 +0200687 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200688
Willy Tarreau186e96e2019-05-28 17:21:18 +0200689 if (LIST_ADDED(&h2c->buf_wait.list)) {
690 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
691 LIST_DEL(&h2c->buf_wait.list);
692 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
693 }
Willy Tarreau14398122017-09-22 14:26:04 +0200694
Willy Tarreau44e973f2018-03-01 17:49:30 +0100695 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200696 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100697
Willy Tarreauea392822017-10-31 10:02:25 +0100698 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200699 h2c->task->context = NULL;
700 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100701 h2c->task = NULL;
702 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200703 if (h2c->wait_event.tasklet)
704 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet0e012562019-09-18 11:07:20 +0200705 if (conn && h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100706 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Christopher Faulet0e012562019-09-18 11:07:20 +0200707 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100708
Willy Tarreaubafbe012017-11-24 17:34:44 +0100709 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200710 }
711
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200712 if (conn) {
713 conn->mux = NULL;
714 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200715
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200716 conn_stop_tracking(conn);
717 conn_full_close(conn);
718 if (conn->destroy_cb)
719 conn->destroy_cb(conn);
720 conn_free(conn);
721 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200722}
723
724
Willy Tarreau71681172017-10-23 14:39:06 +0200725/******************************************************/
726/* functions below are for the H2 protocol processing */
727/******************************************************/
728
729/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100730static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200731{
732 return h2s ? h2s->id : 0;
733}
734
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200735/* returns the sum of the stream's own window size and the mux's initial
736 * window, which together form the stream's effective window size.
737 */
738static inline int h2s_mws(const struct h2s *h2s)
739{
740 return h2s->sws + h2s->h2c->miw;
741}
742
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200743/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100744static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200745{
746 if (h2c->msi < 0)
747 return 0;
748
749 if (h2c->msi == h2s_id(h2s))
750 return 0;
751
752 return 1;
753}
754
Willy Tarreau741d6df2017-10-17 08:00:59 +0200755/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100756static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200757{
758 h2c->errcode = err;
759 h2c->st0 = H2_CS_ERROR;
760}
761
Willy Tarreau175cebb2019-01-24 10:02:24 +0100762/* marks an error on the stream. It may also update an already closed stream
763 * (e.g. to report an error after an RST was received).
764 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100765static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200766{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100767 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200768 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100769 if (h2s->st < H2_SS_ERROR)
770 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100771 if (h2s->cs)
772 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200773 }
774}
775
Willy Tarreau7e094452018-12-19 18:08:52 +0100776/* attempt to notify the data layer of recv availability */
777static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
778{
779 struct wait_event *sw;
780
781 if (h2s->recv_wait) {
782 sw = h2s->recv_wait;
783 sw->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200784 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100785 h2s->recv_wait = NULL;
786 }
787}
788
789/* attempt to notify the data layer of send availability */
790static void __maybe_unused h2s_notify_send(struct h2s *h2s)
791{
792 struct wait_event *sw;
793
Willy Tarreauc234ae32019-05-13 17:56:11 +0200794 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100795 sw = h2s->send_wait;
796 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100797 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200798 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100799 }
800}
801
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100802/* alerts the data layer, trying to wake it up by all means, following
803 * this sequence :
804 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
805 * - if its subscribed to send, then it's woken up for send
806 * - if it was subscribed to neither, its ->wake() callback is called
807 * It is safe to call this function with a closed stream which doesn't have a
808 * conn_stream anymore.
809 */
810static void __maybe_unused h2s_alert(struct h2s *h2s)
811{
812 if (h2s->recv_wait || h2s->send_wait) {
813 h2s_notify_recv(h2s);
814 h2s_notify_send(h2s);
815 }
816 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
817 h2s->cs->data_cb->wake(h2s->cs);
818}
819
Willy Tarreaue4820742017-07-27 13:37:23 +0200820/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100821static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200822{
823 uint8_t *out = frame;
824
825 *out = len >> 16;
826 write_n16(out + 1, len);
827}
828
Willy Tarreau54c15062017-10-10 17:10:03 +0200829/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
830 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
831 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200832 * available in the buffer's input prior to calling this function. The buffer
833 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200834 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100835static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200836 const struct buffer *b, int o)
837{
Willy Tarreau591d4452018-06-15 17:21:00 +0200838 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 +0200839}
840
Willy Tarreau1f094672017-11-20 21:27:45 +0100841static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200842{
Willy Tarreau591d4452018-06-15 17:21:00 +0200843 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200844}
845
Willy Tarreau1f094672017-11-20 21:27:45 +0100846static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200847{
Willy Tarreau591d4452018-06-15 17:21:00 +0200848 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200849}
850
Willy Tarreau1f094672017-11-20 21:27:45 +0100851static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200852{
Willy Tarreau591d4452018-06-15 17:21:00 +0200853 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200854}
855
856
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100857/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
858 * The algorithm is not obvious. It turns out that H2 headers are neither
859 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
860 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200861 *
862 * b0 b1 b2 b3 b4 b5..b8
863 * +----------+---------+--------+----+----+----------------------+
864 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
865 * +----------+---------+--------+----+----+----------------------+
866 *
867 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
868 * we get the sid properly aligned and ordered, and 16 bits of len properly
869 * ordered as well. The type and flags can be extracted using bit shifts from
870 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200871 * Returns zero if some bytes are missing, otherwise non-zero on success. The
872 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200873 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100874static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200875{
876 uint64_t w;
877
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100878 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200879 return 0;
880
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100881 w = h2_get_n64(b, o + 1);
882 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200883 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
884 h->ff = w >> 32;
885 h->ft = w >> 40;
886 h->len += w >> 48;
887 return 1;
888}
889
890/* skip the next 9 bytes corresponding to the frame header possibly parsed by
891 * h2_peek_frame_hdr() above.
892 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100893static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200894{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200895 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200896}
897
898/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100899static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200900{
901 int ret;
902
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100903 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200904 if (ret > 0)
905 h2_skip_frame_hdr(b);
906 return ret;
907}
908
Willy Tarreau00dd0782018-03-01 16:31:34 +0100909/* marks stream <h2s> as CLOSED and decrement the number of active streams for
910 * its connection if the stream was not yet closed. Please use this exclusively
911 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100912 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100913static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100914{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100915 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100916 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100917 if (!h2s->id)
918 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100919 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +0100920 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
921 h2s_notify_recv(h2s);
922 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100923 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100924 h2s->st = H2_SS_CLOSED;
925}
926
Willy Tarreau71049cc2018-03-28 13:56:39 +0200927/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
928static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100929{
930 h2s_close(h2s);
931 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200932 if (b_size(&h2s->rxbuf)) {
933 b_free(&h2s->rxbuf);
934 offer_buffers(NULL, tasks_run_queue);
935 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200936 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100937 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200938 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100939 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800940 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200941 * reference left would be in the h2c send_list/fctl_list, and if
942 * we're in it, we're getting out anyway
943 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100944 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200945 if (LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau86eded62019-06-14 14:47:49 +0200946 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +0200947 LIST_DEL_INIT(&h2s->sending_list);
948 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200949 tasklet_free(h2s->wait_event.tasklet);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100950 pool_free(pool_head_h2s, h2s);
951}
952
Willy Tarreaua8e49542018-10-03 18:53:55 +0200953/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
954 * stream tree. In case of error, nothing is added and NULL is returned. The
955 * causes of errors can be any failed memory allocation. The caller is
956 * responsible for checking if the connection may support an extra stream
957 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200958 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200959static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200960{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200961 struct h2s *h2s;
962
Willy Tarreaubafbe012017-11-24 17:34:44 +0100963 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200964 if (!h2s)
965 goto out;
966
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200967 h2s->wait_event.tasklet = tasklet_new();
968 if (!h2s->wait_event.tasklet) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200969 pool_free(pool_head_h2s, h2s);
970 goto out;
971 }
972 h2s->send_wait = NULL;
973 h2s->recv_wait = NULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200974 h2s->wait_event.tasklet->process = h2_deferred_shut;
975 h2s->wait_event.tasklet->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100976 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200977 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100978 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200979 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200980 h2s->cs = NULL;
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200981 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200982 h2s->flags = H2_SF_NONE;
983 h2s->errcode = H2_ERR_NO_ERROR;
984 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200985 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100986 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200987 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200988
989 if (h2c->flags & H2_CF_IS_BACK) {
990 h1m_init_req(&h2s->h1m);
991 h2s->h1m.err_pos = -1; // don't care about errors on the request path
992 h2s->h1m.flags |= H1_MF_TOLOWER;
993 } else {
994 h1m_init_res(&h2s->h1m);
995 h2s->h1m.err_pos = -1; // don't care about errors on the response path
996 h2s->h1m.flags |= H1_MF_TOLOWER;
997 }
998
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200999 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001000 if (id > 0)
1001 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001002 else
1003 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001004
1005 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001006 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001007 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001008
1009 return h2s;
1010
1011 out_free_h2s:
1012 pool_free(pool_head_h2s, h2s);
1013 out:
1014 return NULL;
1015}
1016
1017/* creates a new stream <id> on the h2c connection and returns it, or NULL in
1018 * case of memory allocation error.
1019 */
1020static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
1021{
1022 struct session *sess = h2c->conn->owner;
1023 struct conn_stream *cs;
1024 struct h2s *h2s;
1025
1026 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
1027 goto out;
1028
1029 h2s = h2s_new(h2c, id);
1030 if (!h2s)
1031 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001032
1033 cs = cs_new(h2c->conn);
1034 if (!cs)
1035 goto out_close;
1036
Olivier Houchard746fb772018-12-15 19:42:00 +01001037 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001038 h2s->cs = cs;
1039 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001040 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001041
1042 if (stream_create_from_cs(cs) < 0)
1043 goto out_free_cs;
1044
Willy Tarreau590a0512018-09-05 11:56:48 +02001045 /* We want the accept date presented to the next stream to be the one
1046 * we have now, the handshake time to be null (since the next stream
1047 * is not delayed by a handshake), and the idle time to count since
1048 * right now.
1049 */
1050 sess->accept_date = date;
1051 sess->tv_accept = now;
1052 sess->t_handshake = 0;
1053
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001054 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001055 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001056 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001057 return h2s;
1058
1059 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001060 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001061 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001062 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001063 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001064 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001065 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001066 sess_log(sess);
1067 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001068}
1069
Willy Tarreau751f2d02018-10-05 09:35:00 +02001070/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1071 * and returns it, or NULL in case of memory allocation error or if the highest
1072 * possible stream ID was reached.
1073 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001074static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001075{
1076 struct h2s *h2s = NULL;
1077
Willy Tarreau86949782019-01-31 10:42:05 +01001078 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001079 goto out;
1080
Willy Tarreaua80dca82019-01-24 17:08:28 +01001081 if (h2_streams_left(h2c) < 1)
1082 goto out;
1083
Willy Tarreau751f2d02018-10-05 09:35:00 +02001084 /* Defer choosing the ID until we send the first message to create the stream */
1085 h2s = h2s_new(h2c, 0);
1086 if (!h2s)
1087 goto out;
1088
1089 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001090 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001091 cs->ctx = h2s;
1092 h2c->nb_cs++;
1093
Willy Tarreau751f2d02018-10-05 09:35:00 +02001094 out:
1095 return h2s;
1096}
1097
Willy Tarreaube5b7152017-09-25 16:25:39 +02001098/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1099 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1100 * the various settings codes.
1101 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001102static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001103{
1104 struct buffer *res;
1105 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001106 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001107 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001108 int ret;
1109
1110 if (h2c_mux_busy(h2c, NULL)) {
1111 h2c->flags |= H2_CF_DEM_MBUSY;
1112 return 0;
1113 }
1114
Willy Tarreaube5b7152017-09-25 16:25:39 +02001115 chunk_init(&buf, buf_data, sizeof(buf_data));
1116 chunk_memcpy(&buf,
1117 "\x00\x00\x00" /* length : 0 for now */
1118 "\x04\x00" /* type : 4 (settings), flags : 0 */
1119 "\x00\x00\x00\x00", /* stream ID : 0 */
1120 9);
1121
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001122 if (h2c->flags & H2_CF_IS_BACK) {
1123 /* send settings_enable_push=0 */
1124 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1125 }
1126
Willy Tarreaube5b7152017-09-25 16:25:39 +02001127 if (h2_settings_header_table_size != 4096) {
1128 char str[6] = "\x00\x01"; /* header_table_size */
1129
1130 write_n32(str + 2, h2_settings_header_table_size);
1131 chunk_memcat(&buf, str, 6);
1132 }
1133
1134 if (h2_settings_initial_window_size != 65535) {
1135 char str[6] = "\x00\x04"; /* initial_window_size */
1136
1137 write_n32(str + 2, h2_settings_initial_window_size);
1138 chunk_memcat(&buf, str, 6);
1139 }
1140
1141 if (h2_settings_max_concurrent_streams != 0) {
1142 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1143
1144 /* Note: 0 means "unlimited" for haproxy's config but not for
1145 * the protocol, so never send this value!
1146 */
1147 write_n32(str + 2, h2_settings_max_concurrent_streams);
1148 chunk_memcat(&buf, str, 6);
1149 }
1150
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001151 mfs = h2_settings_max_frame_size;
1152 if (mfs > global.tune.bufsize)
1153 mfs = global.tune.bufsize;
1154
1155 if (!mfs)
1156 mfs = global.tune.bufsize;
1157
1158 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001159 char str[6] = "\x00\x05"; /* max_frame_size */
1160
1161 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1162 * match bufsize - rewrite size, but at the moment it seems
1163 * that clients don't take care of it.
1164 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001165 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001166 chunk_memcat(&buf, str, 6);
1167 }
1168
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001169 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001170
1171 res = br_tail(h2c->mbuf);
1172 retry:
1173 if (!h2_get_buf(h2c, res)) {
1174 h2c->flags |= H2_CF_MUX_MALLOC;
1175 h2c->flags |= H2_CF_DEM_MROOM;
1176 return 0;
1177 }
1178
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001179 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001180 if (unlikely(ret <= 0)) {
1181 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001182 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1183 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001184 h2c->flags |= H2_CF_MUX_MFULL;
1185 h2c->flags |= H2_CF_DEM_MROOM;
1186 return 0;
1187 }
1188 else {
1189 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1190 return 0;
1191 }
1192 }
1193 return ret;
1194}
1195
Willy Tarreau52eed752017-09-22 15:05:09 +02001196/* Try to receive a connection preface, then upon success try to send our
1197 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1198 * missing data. It may return an error in h2c.
1199 */
1200static int h2c_frt_recv_preface(struct h2c *h2c)
1201{
1202 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001203 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001204
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001205 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001206
1207 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001208 if (ret1 < 0)
1209 sess_log(h2c->conn->owner);
1210
Willy Tarreau52eed752017-09-22 15:05:09 +02001211 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1212 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1213 return 0;
1214 }
1215
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001216 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001217 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001218 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001219
Willy Tarreaube5b7152017-09-25 16:25:39 +02001220 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001221}
1222
Willy Tarreau01b44822018-10-03 14:26:37 +02001223/* Try to send a connection preface, then upon success try to send our
1224 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1225 * missing data. It may return an error in h2c.
1226 */
1227static int h2c_bck_send_preface(struct h2c *h2c)
1228{
1229 struct buffer *res;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001230 int ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001231
1232 if (h2c_mux_busy(h2c, NULL)) {
1233 h2c->flags |= H2_CF_DEM_MBUSY;
1234 return 0;
1235 }
1236
Willy Tarreaubcc45952019-05-26 10:05:50 +02001237 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001238 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001239 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001240 h2c->flags |= H2_CF_MUX_MALLOC;
1241 h2c->flags |= H2_CF_DEM_MROOM;
1242 return 0;
1243 }
1244
1245 if (!b_data(res)) {
1246 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001247 ret = b_istput(res, ist(H2_CONN_PREFACE));
1248 if (unlikely(ret <= 0)) {
1249 if (!ret) {
1250 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1251 goto retry;
1252 h2c->flags |= H2_CF_MUX_MFULL;
1253 h2c->flags |= H2_CF_DEM_MROOM;
1254 return 0;
1255 }
1256 else {
1257 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1258 return 0;
1259 }
1260 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001261 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001262 return h2c_send_settings(h2c);
1263}
1264
Willy Tarreau081d4722017-05-16 21:51:05 +02001265/* try to send a GOAWAY frame on the connection to report an error or a graceful
1266 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1267 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1268 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1269 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1270 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1271 * on unrecoverable failure. It will not attempt to send one again in this last
1272 * case so that it is safe to use h2c_error() to report such errors.
1273 */
1274static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1275{
1276 struct buffer *res;
1277 char str[17];
1278 int ret;
1279
1280 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1281 return 1; // claim that it worked
1282
1283 if (h2c_mux_busy(h2c, h2s)) {
1284 if (h2s)
1285 h2s->flags |= H2_SF_BLK_MBUSY;
1286 else
1287 h2c->flags |= H2_CF_DEM_MBUSY;
1288 return 0;
1289 }
1290
Willy Tarreau9c218e72019-05-26 10:08:28 +02001291 /* len: 8, type: 7, flags: none, sid: 0 */
1292 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1293
1294 if (h2c->last_sid < 0)
1295 h2c->last_sid = h2c->max_id;
1296
1297 write_n32(str + 9, h2c->last_sid);
1298 write_n32(str + 13, h2c->errcode);
1299
Willy Tarreaubcc45952019-05-26 10:05:50 +02001300 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001301 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001302 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001303 h2c->flags |= H2_CF_MUX_MALLOC;
1304 if (h2s)
1305 h2s->flags |= H2_SF_BLK_MROOM;
1306 else
1307 h2c->flags |= H2_CF_DEM_MROOM;
1308 return 0;
1309 }
1310
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001311 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001312 if (unlikely(ret <= 0)) {
1313 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001314 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1315 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001316 h2c->flags |= H2_CF_MUX_MFULL;
1317 if (h2s)
1318 h2s->flags |= H2_SF_BLK_MROOM;
1319 else
1320 h2c->flags |= H2_CF_DEM_MROOM;
1321 return 0;
1322 }
1323 else {
1324 /* we cannot report this error using GOAWAY, so we mark
1325 * it and claim a success.
1326 */
1327 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1328 h2c->flags |= H2_CF_GOAWAY_FAILED;
1329 return 1;
1330 }
1331 }
1332 h2c->flags |= H2_CF_GOAWAY_SENT;
1333 return ret;
1334}
1335
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001336/* Try to send an RST_STREAM frame on the connection for the indicated stream
1337 * during mux operations. This stream must be valid and cannot be closed
1338 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1339 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1340 * not yet.
1341 *
1342 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1343 * to write the message, it subscribes the stream to future notifications.
1344 */
1345static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1346{
1347 struct buffer *res;
1348 char str[13];
1349 int ret;
1350
1351 if (!h2s || h2s->st == H2_SS_CLOSED)
1352 return 1;
1353
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001354 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1355 * RST_STREAM in response to a RST_STREAM frame.
1356 */
Willy Tarreaubf9a20b2019-08-06 10:01:40 +02001357 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001358 ret = 1;
1359 goto ignore;
1360 }
1361
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001362 if (h2c_mux_busy(h2c, h2s)) {
1363 h2s->flags |= H2_SF_BLK_MBUSY;
1364 return 0;
1365 }
1366
Willy Tarreau9c218e72019-05-26 10:08:28 +02001367 /* len: 4, type: 3, flags: none */
1368 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1369 write_n32(str + 5, h2s->id);
1370 write_n32(str + 9, h2s->errcode);
1371
Willy Tarreaubcc45952019-05-26 10:05:50 +02001372 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001373 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001374 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001375 h2c->flags |= H2_CF_MUX_MALLOC;
1376 h2s->flags |= H2_SF_BLK_MROOM;
1377 return 0;
1378 }
1379
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001380 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001381 if (unlikely(ret <= 0)) {
1382 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001383 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1384 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001385 h2c->flags |= H2_CF_MUX_MFULL;
1386 h2s->flags |= H2_SF_BLK_MROOM;
1387 return 0;
1388 }
1389 else {
1390 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1391 return 0;
1392 }
1393 }
1394
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001395 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001396 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001397 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001398 return ret;
1399}
1400
1401/* Try to send an RST_STREAM frame on the connection for the stream being
1402 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001403 * error code, even if the stream is one of the dummy ones, and will update
1404 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001405 *
1406 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1407 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001408 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001409 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001410 */
1411static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1412{
1413 struct buffer *res;
1414 char str[13];
1415 int ret;
1416
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001417 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1418 * RST_STREAM in response to a RST_STREAM frame.
1419 */
1420 if (h2c->dft == H2_FT_RST_STREAM) {
1421 ret = 1;
1422 goto ignore;
1423 }
1424
Willy Tarreau27a84c92017-10-17 08:10:17 +02001425 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001426 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001427 return 0;
1428 }
1429
Willy Tarreau9c218e72019-05-26 10:08:28 +02001430 /* len: 4, type: 3, flags: none */
1431 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1432
1433 write_n32(str + 5, h2c->dsi);
1434 write_n32(str + 9, h2s->errcode);
1435
Willy Tarreaubcc45952019-05-26 10:05:50 +02001436 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001437 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001438 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001439 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001440 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001441 return 0;
1442 }
1443
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001444 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001445 if (unlikely(ret <= 0)) {
1446 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001447 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1448 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001449 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001450 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001451 return 0;
1452 }
1453 else {
1454 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1455 return 0;
1456 }
1457 }
1458
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001459 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001460 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001461 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001462 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001463 }
1464
Willy Tarreau27a84c92017-10-17 08:10:17 +02001465 return ret;
1466}
1467
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001468/* try to send an empty DATA frame with the ES flag set to notify about the
1469 * end of stream and match a shutdown(write). If an ES was already sent as
1470 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1471 * on success or zero if nothing was done. In case of lack of room to write the
1472 * message, it subscribes the requesting stream to future notifications.
1473 */
1474static int h2_send_empty_data_es(struct h2s *h2s)
1475{
1476 struct h2c *h2c = h2s->h2c;
1477 struct buffer *res;
1478 char str[9];
1479 int ret;
1480
Willy Tarreau721c9742017-11-07 11:05:42 +01001481 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001482 return 1;
1483
1484 if (h2c_mux_busy(h2c, h2s)) {
1485 h2s->flags |= H2_SF_BLK_MBUSY;
1486 return 0;
1487 }
1488
Willy Tarreau9c218e72019-05-26 10:08:28 +02001489 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1490 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1491 write_n32(str + 5, h2s->id);
1492
Willy Tarreaubcc45952019-05-26 10:05:50 +02001493 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001494 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001495 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001496 h2c->flags |= H2_CF_MUX_MALLOC;
1497 h2s->flags |= H2_SF_BLK_MROOM;
1498 return 0;
1499 }
1500
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001501 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001502 if (likely(ret > 0)) {
1503 h2s->flags |= H2_SF_ES_SENT;
1504 }
1505 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001506 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1507 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001508 h2c->flags |= H2_CF_MUX_MFULL;
1509 h2s->flags |= H2_SF_BLK_MROOM;
1510 return 0;
1511 }
1512 else {
1513 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1514 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001515 }
1516 return ret;
1517}
1518
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001519/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001520 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001521 * is automatically updated accordingly. If the stream is orphaned, it is
1522 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001523 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001524static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001525{
1526 if (!h2s->cs) {
1527 /* this stream was already orphaned */
1528 h2s_destroy(h2s);
1529 return;
1530 }
1531
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001532 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001533 if (h2s->st == H2_SS_OPEN)
1534 h2s->st = H2_SS_HREM;
1535 else if (h2s->st == H2_SS_HLOC)
1536 h2s_close(h2s);
1537 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001538
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001539 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1540 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1541 h2s->cs->flags |= CS_FL_ERR_PENDING;
1542 if (h2s->cs->flags & CS_FL_EOS)
1543 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001544
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001545 if (h2s->st < H2_SS_ERROR)
1546 h2s->st = H2_SS_ERROR;
1547 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001548
1549 h2s_alert(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001550}
1551
1552/* wake the streams attached to the connection, whose id is greater than <last>
1553 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001554 */
Willy Tarreau23482912019-05-07 15:23:14 +02001555static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001556{
1557 struct eb32_node *node;
1558 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001559
Christopher Fauletf02ca002019-03-07 16:21:34 +01001560 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001561 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1562 while (node) {
1563 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001564 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001565 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001566 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001567
Christopher Fauletf02ca002019-03-07 16:21:34 +01001568 /* Wake all streams with unassigned ID (ID == 0) */
1569 node = eb32_lookup(&h2c->streams_by_id, 0);
1570 while (node) {
1571 h2s = container_of(node, struct h2s, by_id);
1572 if (h2s->id > 0)
1573 break;
1574 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001575 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001576 }
1577}
1578
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001579/* Wake up all blocked streams whose window size has become positive after the
1580 * mux's initial window was adjusted. This should be done after having processed
1581 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001582 */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001583static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001584{
1585 struct h2s *h2s;
1586 struct eb32_node *node;
1587
Willy Tarreau3421aba2017-07-27 15:41:03 +02001588 node = eb32_first(&h2c->streams_by_id);
1589 while (node) {
1590 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001591 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001592 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreaubda92dd2019-10-02 10:49:59 +02001593 LIST_DEL_INIT(&h2s->list);
1594 if (h2s->send_wait)
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001595 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001596 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001597 node = eb32_next(node);
1598 }
1599}
1600
1601/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1602 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001603 * return an error in h2c. The caller must have already verified frame length
1604 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001605 */
1606static int h2c_handle_settings(struct h2c *h2c)
1607{
1608 unsigned int offset;
1609 int error;
1610
1611 if (h2c->dff & H2_F_SETTINGS_ACK) {
1612 if (h2c->dfl) {
1613 error = H2_ERR_FRAME_SIZE_ERROR;
1614 goto fail;
1615 }
1616 return 1;
1617 }
1618
Willy Tarreau3421aba2017-07-27 15:41:03 +02001619 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001620 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001621 return 0;
1622
1623 /* parse the frame */
1624 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001625 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1626 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001627
1628 switch (type) {
1629 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1630 /* we need to update all existing streams with the
1631 * difference from the previous iws.
1632 */
1633 if (arg < 0) { // RFC7540#6.5.2
1634 error = H2_ERR_FLOW_CONTROL_ERROR;
1635 goto fail;
1636 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001637 h2c->miw = arg;
1638 break;
1639 case H2_SETTINGS_MAX_FRAME_SIZE:
1640 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1641 error = H2_ERR_PROTOCOL_ERROR;
1642 goto fail;
1643 }
1644 h2c->mfs = arg;
1645 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001646 case H2_SETTINGS_ENABLE_PUSH:
1647 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1648 error = H2_ERR_PROTOCOL_ERROR;
1649 goto fail;
1650 }
1651 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001652 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1653 if (h2c->flags & H2_CF_IS_BACK) {
1654 /* the limit is only for the backend; for the frontend it is our limit */
1655 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1656 arg = h2_settings_max_concurrent_streams;
1657 h2c->streams_limit = arg;
1658 }
1659 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001660 }
1661 }
1662
1663 /* need to ACK this frame now */
1664 h2c->st0 = H2_CS_FRAME_A;
1665 return 1;
1666 fail:
Willy Tarreau21178a52019-10-23 11:06:35 +02001667 if (!(h2c->flags & H2_CF_IS_BACK))
1668 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001669 h2c_error(h2c, error);
1670 return 0;
1671}
1672
1673/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1674 * success or one of the h2_status values.
1675 */
1676static int h2c_ack_settings(struct h2c *h2c)
1677{
1678 struct buffer *res;
1679 char str[9];
1680 int ret = -1;
1681
1682 if (h2c_mux_busy(h2c, NULL)) {
1683 h2c->flags |= H2_CF_DEM_MBUSY;
1684 return 0;
1685 }
1686
Willy Tarreau9c218e72019-05-26 10:08:28 +02001687 memcpy(str,
1688 "\x00\x00\x00" /* length : 0 (no data) */
1689 "\x04" "\x01" /* type : 4, flags : ACK */
1690 "\x00\x00\x00\x00" /* stream ID */, 9);
1691
Willy Tarreaubcc45952019-05-26 10:05:50 +02001692 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001693 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001694 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001695 h2c->flags |= H2_CF_MUX_MALLOC;
1696 h2c->flags |= H2_CF_DEM_MROOM;
1697 return 0;
1698 }
1699
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001700 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001701 if (unlikely(ret <= 0)) {
1702 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001703 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1704 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001705 h2c->flags |= H2_CF_MUX_MFULL;
1706 h2c->flags |= H2_CF_DEM_MROOM;
1707 return 0;
1708 }
1709 else {
1710 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1711 return 0;
1712 }
1713 }
1714 return ret;
1715}
1716
Willy Tarreaucf68c782017-10-10 17:11:41 +02001717/* processes a PING frame and schedules an ACK if needed. The caller must pass
1718 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001719 * missing data. The caller must have already verified frame length
1720 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001721 */
1722static int h2c_handle_ping(struct h2c *h2c)
1723{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001724 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001725 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001726 h2c->st0 = H2_CS_FRAME_A;
1727 return 1;
1728}
1729
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001730/* Try to send a window update for stream id <sid> and value <increment>.
1731 * Returns > 0 on success or zero on missing room or failure. It may return an
1732 * error in h2c.
1733 */
1734static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1735{
1736 struct buffer *res;
1737 char str[13];
1738 int ret = -1;
1739
1740 if (h2c_mux_busy(h2c, NULL)) {
1741 h2c->flags |= H2_CF_DEM_MBUSY;
1742 return 0;
1743 }
1744
Willy Tarreau9c218e72019-05-26 10:08:28 +02001745 /* length: 4, type: 8, flags: none */
1746 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1747 write_n32(str + 5, sid);
1748 write_n32(str + 9, increment);
1749
Willy Tarreaubcc45952019-05-26 10:05:50 +02001750 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001751 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001752 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001753 h2c->flags |= H2_CF_MUX_MALLOC;
1754 h2c->flags |= H2_CF_DEM_MROOM;
1755 return 0;
1756 }
1757
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001758 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001759 if (unlikely(ret <= 0)) {
1760 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001761 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1762 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001763 h2c->flags |= H2_CF_MUX_MFULL;
1764 h2c->flags |= H2_CF_DEM_MROOM;
1765 return 0;
1766 }
1767 else {
1768 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1769 return 0;
1770 }
1771 }
1772 return ret;
1773}
1774
1775/* try to send pending window update for the connection. It's safe to call it
1776 * with no pending updates. Returns > 0 on success or zero on missing room or
1777 * failure. It may return an error in h2c.
1778 */
1779static int h2c_send_conn_wu(struct h2c *h2c)
1780{
1781 int ret = 1;
1782
1783 if (h2c->rcvd_c <= 0)
1784 return 1;
1785
Willy Tarreau97aaa672018-12-23 09:49:04 +01001786 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1787 /* increase the advertised connection window to 2G on
1788 * first update.
1789 */
1790 h2c->flags |= H2_CF_WINDOW_OPENED;
1791 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1792 }
1793
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001794 /* send WU for the connection */
1795 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1796 if (ret > 0)
1797 h2c->rcvd_c = 0;
1798
1799 return ret;
1800}
1801
1802/* try to send pending window update for the current dmux stream. It's safe to
1803 * call it with no pending updates. Returns > 0 on success or zero on missing
1804 * room or failure. It may return an error in h2c.
1805 */
1806static int h2c_send_strm_wu(struct h2c *h2c)
1807{
1808 int ret = 1;
1809
1810 if (h2c->rcvd_s <= 0)
1811 return 1;
1812
1813 /* send WU for the stream */
1814 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1815 if (ret > 0)
1816 h2c->rcvd_s = 0;
1817
1818 return ret;
1819}
1820
Willy Tarreaucf68c782017-10-10 17:11:41 +02001821/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1822 * success, 0 on missing data or one of the h2_status values.
1823 */
1824static int h2c_ack_ping(struct h2c *h2c)
1825{
1826 struct buffer *res;
1827 char str[17];
1828 int ret = -1;
1829
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001830 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001831 return 0;
1832
1833 if (h2c_mux_busy(h2c, NULL)) {
1834 h2c->flags |= H2_CF_DEM_MBUSY;
1835 return 0;
1836 }
1837
Willy Tarreaucf68c782017-10-10 17:11:41 +02001838 memcpy(str,
1839 "\x00\x00\x08" /* length : 8 (same payload) */
1840 "\x06" "\x01" /* type : 6, flags : ACK */
1841 "\x00\x00\x00\x00" /* stream ID */, 9);
1842
1843 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001844 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001845
Willy Tarreau9c218e72019-05-26 10:08:28 +02001846 res = br_tail(h2c->mbuf);
1847 retry:
1848 if (!h2_get_buf(h2c, res)) {
1849 h2c->flags |= H2_CF_MUX_MALLOC;
1850 h2c->flags |= H2_CF_DEM_MROOM;
1851 return 0;
1852 }
1853
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001854 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001855 if (unlikely(ret <= 0)) {
1856 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001857 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1858 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02001859 h2c->flags |= H2_CF_MUX_MFULL;
1860 h2c->flags |= H2_CF_DEM_MROOM;
1861 return 0;
1862 }
1863 else {
1864 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1865 return 0;
1866 }
1867 }
1868 return ret;
1869}
1870
Willy Tarreau26f95952017-07-27 17:18:30 +02001871/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1872 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001873 * h2c or h2s. The caller must have already verified frame length and stream ID
1874 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001875 */
1876static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1877{
1878 int32_t inc;
1879 int error;
1880
Willy Tarreau26f95952017-07-27 17:18:30 +02001881 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001882 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001883 return 0;
1884
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001885 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001886
1887 if (h2c->dsi != 0) {
1888 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001889
1890 /* it's not an error to receive WU on a closed stream */
1891 if (h2s->st == H2_SS_CLOSED)
1892 return 1;
1893
1894 if (!inc) {
1895 error = H2_ERR_PROTOCOL_ERROR;
1896 goto strm_err;
1897 }
1898
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001899 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001900 error = H2_ERR_FLOW_CONTROL_ERROR;
1901 goto strm_err;
1902 }
1903
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001904 h2s->sws += inc;
1905 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001906 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreaubda92dd2019-10-02 10:49:59 +02001907 LIST_DEL_INIT(&h2s->list);
1908 if (h2s->send_wait)
Olivier Houcharddddfe312018-10-10 18:51:00 +02001909 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001910 }
1911 }
1912 else {
1913 /* connection window update */
1914 if (!inc) {
1915 error = H2_ERR_PROTOCOL_ERROR;
1916 goto conn_err;
1917 }
1918
1919 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1920 error = H2_ERR_FLOW_CONTROL_ERROR;
1921 goto conn_err;
1922 }
1923
1924 h2c->mws += inc;
1925 }
1926
1927 return 1;
1928
1929 conn_err:
1930 h2c_error(h2c, error);
1931 return 0;
1932
1933 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001934 h2s_error(h2s, error);
1935 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001936 return 0;
1937}
1938
Willy Tarreaue96b0922017-10-30 00:28:29 +01001939/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001940 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1941 * have already verified frame length and stream ID validity. Described in
1942 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001943 */
1944static int h2c_handle_goaway(struct h2c *h2c)
1945{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001946 int last;
1947
Willy Tarreaue96b0922017-10-30 00:28:29 +01001948 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001949 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001950 return 0;
1951
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001952 last = h2_get_n32(&h2c->dbuf, 0);
1953 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001954 if (h2c->last_sid < 0)
1955 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02001956 h2_wake_some_streams(h2c, last);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001957 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001958}
1959
Willy Tarreau92153fc2017-12-03 19:46:19 +01001960/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001961 * invalid. Returns > 0 on success or zero on missing data. It may return an
1962 * error in h2c. The caller must have already verified frame length and stream
1963 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001964 */
1965static int h2c_handle_priority(struct h2c *h2c)
1966{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001967 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001968 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001969 return 0;
1970
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001971 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001972 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001973 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1974 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001975 }
1976 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001977}
1978
Willy Tarreaucd234e92017-08-18 10:59:39 +02001979/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001980 * Returns > 0 on success or zero on missing data. The caller must have already
1981 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001982 */
1983static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1984{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001985 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001986 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001987 return 0;
1988
1989 /* late RST, already handled */
1990 if (h2s->st == H2_SS_CLOSED)
1991 return 1;
1992
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001993 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001994 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001995
1996 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001997 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001998 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001999 }
2000
2001 h2s->flags |= H2_SF_RST_RCVD;
2002 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002003}
2004
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002005/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2006 * It may return an error in h2c or h2s. The caller must consider that the
2007 * return value is the new h2s in case one was allocated (most common case).
2008 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002009 * errors here are reported as connection errors since it's impossible to
2010 * recover from such errors after the compression context has been altered.
2011 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002012static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002013{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002014 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002015 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002016 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002017 int error;
2018
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002019 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002020 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02002021
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002022 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002023 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02002024
2025 /* now either the frame is complete or the buffer is complete */
2026 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002027 /* The stream exists/existed, this must be a trailers frame */
2028 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02002029 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
2030 /* unrecoverable error ? */
2031 if (h2c->st0 >= H2_CS_ERROR)
2032 goto out;
2033
2034 if (error == 0)
2035 goto out; // missing data
2036
2037 if (error < 0) {
2038 /* Failed to decode this frame (e.g. too large request)
2039 * but the HPACK decompressor is still synchronized.
2040 */
2041 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2042 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002043 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002044 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002045 goto done;
2046 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002047 /* the connection was already killed by an RST, let's consume
2048 * the data and send another RST.
2049 */
2050 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2051 h2s = (struct h2s*)h2_error_stream;
2052 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002053 }
2054 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2055 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2056 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002057 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002058 goto conn_err;
2059 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002060 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2061 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002062
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002063 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002064
Willy Tarreau25919232019-01-03 14:48:18 +01002065 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002066 if (h2c->st0 >= H2_CS_ERROR)
2067 goto out;
2068
Willy Tarreau25919232019-01-03 14:48:18 +01002069 if (error <= 0) {
2070 if (error == 0)
2071 goto out; // missing data
2072
2073 /* Failed to decode this stream (e.g. too large request)
2074 * but the HPACK decompressor is still synchronized.
2075 */
2076 h2s = (struct h2s*)h2_error_stream;
2077 goto send_rst;
2078 }
2079
Willy Tarreau22de8d32018-09-05 19:55:58 +02002080 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002081 * positively from h2c_frt_stream_new(), the stream will report the error,
2082 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002083 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02002084 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02002085 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002086 h2s = (struct h2s*)h2_refused_stream;
2087 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002088 }
2089
2090 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002091 h2s->rxbuf = rxbuf;
2092 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002093 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002094
Willy Tarreau88d138e2019-01-02 19:38:14 +01002095 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002096 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002097 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002098
2099 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002100 if (h2s->st == H2_SS_OPEN)
2101 h2s->st = H2_SS_HREM;
2102 else
2103 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002104 }
2105
Willy Tarreau3a429f02019-01-03 11:41:50 +01002106 /* update the max stream ID if the request is being processed */
2107 if (h2s->id > h2c->max_id)
2108 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002109
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002110 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002111
2112 conn_err:
2113 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002114 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002115
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002116 out:
2117 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002118 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002119
2120 send_rst:
2121 /* make the demux send an RST for the current stream. We may only
2122 * do this if we're certain that the HEADERS frame was properly
2123 * decompressed so that the HPACK decoder is still kept up to date.
2124 */
2125 h2_release_buf(h2c, &rxbuf);
2126 h2c->st0 = H2_CS_FRAME_E;
2127 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002128}
2129
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002130/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2131 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2132 * errors here are reported as connection errors since it's impossible to
2133 * recover from such errors after the compression context has been altered.
2134 */
2135static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2136{
Christopher Faulet96b88f22019-09-23 15:28:20 +02002137 struct buffer rxbuf = BUF_NULL;
2138 unsigned long long body_len = 0;
2139 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002140 int error;
2141
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002142 if (!b_size(&h2c->dbuf))
2143 return NULL; // empty buffer
2144
2145 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2146 return NULL; // incomplete frame
2147
Christopher Faulet96b88f22019-09-23 15:28:20 +02002148 if (h2s->st != H2_SS_CLOSED) {
2149 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
2150 }
2151 else {
2152 /* the connection was already killed by an RST, let's consume
2153 * the data and send another RST.
2154 */
2155 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Christopher Faulet03427052019-09-26 16:19:13 +02002156 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet96b88f22019-09-23 15:28:20 +02002157 h2c->st0 = H2_CS_FRAME_E;
2158 goto send_rst;
2159 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002160
Willy Tarreau25919232019-01-03 14:48:18 +01002161 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002162 if (h2c->st0 >= H2_CS_ERROR)
2163 return NULL;
2164
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002165 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2166 /* RFC7540#5.1 */
2167 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2168 h2c->st0 = H2_CS_FRAME_E;
2169 return NULL;
2170 }
2171
Willy Tarreau25919232019-01-03 14:48:18 +01002172 if (error <= 0) {
2173 if (error == 0)
2174 return NULL; // missing data
2175
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002176 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002177 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002178 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002179 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002180 }
2181
Christopher Fauletfa922f02019-05-07 10:55:17 +02002182 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002183 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002184
Willy Tarreau927b88b2019-03-04 08:03:25 +01002185 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002186 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002187 else if (h2s->flags & H2_SF_ES_RCVD) {
2188 if (h2s->st == H2_SS_OPEN)
2189 h2s->st = H2_SS_HREM;
2190 else if (h2s->st == H2_SS_HLOC)
2191 h2s_close(h2s);
2192 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002193
2194 return h2s;
Christopher Faulet96b88f22019-09-23 15:28:20 +02002195
2196 send_rst:
2197 /* make the demux send an RST for the current stream. We may only
2198 * do this if we're certain that the HEADERS frame was properly
2199 * decompressed so that the HPACK decoder is still kept up to date.
2200 */
2201 h2_release_buf(h2c, &rxbuf);
2202 h2c->st0 = H2_CS_FRAME_E;
2203 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002204}
2205
Willy Tarreau454f9052017-10-26 19:40:35 +02002206/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2207 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2208 */
2209static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2210{
2211 int error;
2212
2213 /* note that empty DATA frames are perfectly valid and sometimes used
2214 * to signal an end of stream (with the ES flag).
2215 */
2216
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002217 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002218 return 0; // empty buffer
2219
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002220 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002221 return 0; // incomplete frame
2222
2223 /* now either the frame is complete or the buffer is complete */
2224
Willy Tarreau454f9052017-10-26 19:40:35 +02002225 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2226 /* RFC7540#6.1 */
2227 error = H2_ERR_STREAM_CLOSED;
2228 goto strm_err;
2229 }
2230
Christopher Faulet4fb65f42019-06-19 09:25:58 +02002231 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002232 /* RFC7540#8.1.2 */
2233 error = H2_ERR_PROTOCOL_ERROR;
2234 goto strm_err;
2235 }
2236
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002237 if (!h2_frt_transfer_data(h2s))
2238 return 0;
2239
Willy Tarreau454f9052017-10-26 19:40:35 +02002240 /* call the upper layers to process the frame, then let the upper layer
2241 * notify the stream about any change.
2242 */
2243 if (!h2s->cs) {
Willy Tarreauc1a29652019-08-06 10:11:02 +02002244 /* The upper layer has already closed, this may happen on
2245 * 4xx/redirects during POST, or when receiving a response
2246 * from an H2 server after the client has aborted.
2247 */
2248 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002249 goto strm_err;
2250 }
2251
Willy Tarreau8f650c32017-11-21 19:36:21 +01002252 if (h2c->st0 >= H2_CS_ERROR)
2253 return 0;
2254
Willy Tarreau721c9742017-11-07 11:05:42 +01002255 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002256 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002257 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002258 }
2259
2260 /* check for completion : the callee will change this to FRAME_A or
2261 * FRAME_H once done.
2262 */
2263 if (h2c->st0 == H2_CS_FRAME_P)
2264 return 0;
2265
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002266 /* last frame */
2267 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002268 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002269 if (h2s->st == H2_SS_OPEN)
2270 h2s->st = H2_SS_HREM;
2271 else
2272 h2s_close(h2s);
2273
Willy Tarreau1915ca22019-01-24 11:49:37 +01002274 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2275 /* RFC7540#8.1.2 */
2276 error = H2_ERR_PROTOCOL_ERROR;
2277 goto strm_err;
2278 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002279 }
2280
Willy Tarreau454f9052017-10-26 19:40:35 +02002281 return 1;
2282
Willy Tarreau454f9052017-10-26 19:40:35 +02002283 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002284 h2s_error(h2s, error);
2285 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002286 return 0;
2287}
2288
Willy Tarreaubc933932017-10-09 16:21:43 +02002289/* process Rx frames to be demultiplexed */
2290static void h2_process_demux(struct h2c *h2c)
2291{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002292 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002293 struct h2_fh hdr;
2294 unsigned int padlen = 0;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002295 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002296
Willy Tarreau081d4722017-05-16 21:51:05 +02002297 if (h2c->st0 >= H2_CS_ERROR)
2298 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002299
2300 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2301 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002302 if (h2c->flags & H2_CF_IS_BACK)
2303 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002304 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2305 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002306 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002307 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002308 sess_log(h2c->conn->owner);
2309 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002310 goto fail;
2311 }
2312
2313 h2c->max_id = 0;
2314 h2c->st0 = H2_CS_SETTINGS1;
2315 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002316
2317 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002318 /* ensure that what is pending is a valid SETTINGS frame
2319 * without an ACK.
2320 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002321 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002322 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002323 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002324 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002325 if (!(h2c->flags & H2_CF_IS_BACK))
2326 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002327 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002328 goto fail;
2329 }
2330
2331 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2332 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2333 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2334 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002335 if (!(h2c->flags & H2_CF_IS_BACK))
2336 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002337 goto fail;
2338 }
2339
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002340 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002341 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2342 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2343 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau21178a52019-10-23 11:06:35 +02002344 if (!(h2c->flags & H2_CF_IS_BACK))
2345 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002346 goto fail;
2347 }
2348
Willy Tarreau3bf69182018-12-21 15:34:50 +01002349 /* that's OK, switch to FRAME_P to process it. This is
2350 * a SETTINGS frame whose header has already been
2351 * deleted above.
2352 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002353 padlen = 0;
2354 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002355 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002356 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002357
2358 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002359 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002360 int ret = 0;
2361
2362 if (h2c->st0 >= H2_CS_ERROR)
2363 break;
2364
2365 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreauab3ebbc2019-08-06 15:49:51 +02002366 h2c->rcvd_s = 0;
2367
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002368 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002369 break;
2370
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002371 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002372 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002373 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02002374 /* only log if no other stream can report the error */
2375 sess_log(h2c->conn->owner);
2376 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002377 break;
2378 }
2379
Christopher Faulet3d574a52019-06-18 12:22:38 +02002380 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002381 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2382 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2383 * we read the pad length and drop it from the remaining
2384 * payload (one byte + the 9 remaining ones = 10 total
2385 * removed), so we have a frame payload starting after the
2386 * pad len. Flow controlled frames (DATA) also count the
2387 * padlen in the flow control, so it must be adjusted.
2388 */
2389 if (hdr.len < 1) {
2390 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002391 if (!(h2c->flags & H2_CF_IS_BACK))
2392 sess_log(h2c->conn->owner);
Willy Tarreau3bf69182018-12-21 15:34:50 +01002393 goto fail;
2394 }
2395 hdr.len--;
2396
2397 if (b_data(&h2c->dbuf) < 10)
2398 break; // missing padlen
2399
2400 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2401
2402 if (padlen > hdr.len) {
2403 /* RFC7540#6.1 : pad length = length of
2404 * frame payload or greater => error.
2405 */
2406 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002407 if (!(h2c->flags & H2_CF_IS_BACK))
2408 sess_log(h2c->conn->owner);
Willy Tarreau3bf69182018-12-21 15:34:50 +01002409 goto fail;
2410 }
2411
2412 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2413 h2c->rcvd_c++;
2414 h2c->rcvd_s++;
2415 }
2416 b_del(&h2c->dbuf, 1);
2417 }
2418 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002419
2420 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002421 h2c->dfl = hdr.len;
2422 h2c->dsi = hdr.sid;
2423 h2c->dft = hdr.ft;
2424 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002425 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002426 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002427
2428 /* check for minimum basic frame format validity */
2429 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2430 if (ret != H2_ERR_NO_ERROR) {
2431 h2c_error(h2c, ret);
Willy Tarreau21178a52019-10-23 11:06:35 +02002432 if (!(h2c->flags & H2_CF_IS_BACK))
2433 sess_log(h2c->conn->owner);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002434 goto fail;
2435 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002436 }
2437
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002438 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
2439 * H2_CS_FRAME_P indicates an incomplete previous operation
2440 * (most often the first attempt) and requires some validity
2441 * checks for the frame and the current state. The two other
2442 * ones are set after completion (or abortion) and must skip
2443 * validity checks.
2444 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002445 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2446
Willy Tarreau567beb82018-12-18 16:52:44 +01002447 if (tmp_h2s != h2s && h2s && h2s->cs &&
2448 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002449 conn_xprt_read0_pending(h2c->conn) ||
2450 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002451 (h2s->flags & H2_SF_ES_RCVD) ||
2452 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002453 /* we may have to signal the upper layers */
2454 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002455 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002456 }
2457 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002458
Willy Tarreaud7901432017-12-29 11:34:40 +01002459 if (h2c->st0 == H2_CS_FRAME_E)
2460 goto strm_err;
2461
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002462 if (h2c->st0 == H2_CS_FRAME_A)
2463 goto valid_frame_type;
2464
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002465 if (h2s->st == H2_SS_IDLE &&
2466 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2467 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2468 * this state MUST be treated as a connection error
2469 */
2470 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002471 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02002472 /* only log if no other stream can report the error */
2473 sess_log(h2c->conn->owner);
2474 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002475 break;
2476 }
2477
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002478 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2479 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2480 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002481 * this state MUST be treated as a stream error.
2482 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2483 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002484 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002485 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2486 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2487 else
2488 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002489 goto strm_err;
2490 }
2491
Willy Tarreauab837502017-12-27 15:07:30 +01002492 /* Below the management of frames received in closed state is a
2493 * bit hackish because the spec makes strong differences between
2494 * streams closed by receiving RST, sending RST, and seeing ES
2495 * in both directions. In addition to this, the creation of a
2496 * new stream reusing the identifier of a closed one will be
2497 * detected here. Given that we cannot keep track of all closed
2498 * streams forever, we consider that unknown closed streams were
2499 * closed on RST received, which allows us to respond with an
2500 * RST without breaking the connection (eg: to abort a transfer).
2501 * Some frames have to be silently ignored as well.
2502 */
2503 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002504 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002505 /* #5.1.1: The identifier of a newly
2506 * established stream MUST be numerically
2507 * greater than all streams that the initiating
2508 * endpoint has opened or reserved. This
2509 * governs streams that are opened using a
2510 * HEADERS frame and streams that are reserved
2511 * using PUSH_PROMISE. An endpoint that
2512 * receives an unexpected stream identifier
2513 * MUST respond with a connection error.
2514 */
2515 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2516 goto strm_err;
2517 }
2518
Willy Tarreaueb5be9d2019-09-26 08:47:15 +02002519 if (h2s->flags & H2_SF_RST_RCVD &&
2520 !(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 +01002521 /* RFC7540#5.1:closed: an endpoint that
2522 * receives any frame other than PRIORITY after
2523 * receiving a RST_STREAM MUST treat that as a
2524 * stream error of type STREAM_CLOSED.
2525 *
2526 * Note that old streams fall into this category
2527 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002528 *
2529 * However, we cannot generalize this to all frame types. Those
2530 * carrying compression state must still be processed before
2531 * being dropped or we'll desynchronize the decoder. This can
2532 * happen with request trailers received after sending an
2533 * RST_STREAM, or with header/trailers responses received after
2534 * sending RST_STREAM (aborted stream).
Willy Tarreaueb5be9d2019-09-26 08:47:15 +02002535 *
2536 * In addition, since our CLOSED streams always carry the
2537 * RST_RCVD bit, we don't want to accidently catch valid
2538 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreauab837502017-12-27 15:07:30 +01002539 */
2540 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2541 h2c->st0 = H2_CS_FRAME_E;
2542 goto strm_err;
2543 }
2544
2545 /* RFC7540#5.1:closed: if this state is reached as a
2546 * result of sending a RST_STREAM frame, the peer that
2547 * receives the RST_STREAM might have already sent
2548 * frames on the stream that cannot be withdrawn. An
2549 * endpoint MUST ignore frames that it receives on
2550 * closed streams after it has sent a RST_STREAM
2551 * frame. An endpoint MAY choose to limit the period
2552 * over which it ignores frames and treat frames that
2553 * arrive after this time as being in error.
2554 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002555 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002556 /* RFC7540#5.1:closed: any frame other than
2557 * PRIO/WU/RST in this state MUST be treated as
2558 * a connection error
2559 */
2560 if (h2c->dft != H2_FT_RST_STREAM &&
2561 h2c->dft != H2_FT_PRIORITY &&
2562 h2c->dft != H2_FT_WINDOW_UPDATE) {
2563 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2564 goto strm_err;
2565 }
2566 }
2567 }
2568
Willy Tarreauc0da1962017-10-30 18:38:00 +01002569#if 0
2570 // problem below: it is not possible to completely ignore such
2571 // streams as we need to maintain the compression state as well
2572 // and for this we need to completely process these frames (eg:
2573 // HEADERS frames) as well as counting DATA frames to emit
2574 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2575 // This is a typical case of layer violation where the
2576 // transported contents are critical to the connection's
2577 // validity and must be ignored at the same time :-(
2578
2579 /* graceful shutdown, ignore streams whose ID is higher than
2580 * the one advertised in GOAWAY. RFC7540#6.8.
2581 */
2582 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002583 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2584 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002585 h2c->dfl -= ret;
2586 ret = h2c->dfl == 0;
2587 goto strm_err;
2588 }
2589#endif
2590
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002591 valid_frame_type:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002592 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002593 case H2_FT_SETTINGS:
2594 if (h2c->st0 == H2_CS_FRAME_P)
2595 ret = h2c_handle_settings(h2c);
2596
2597 if (h2c->st0 == H2_CS_FRAME_A)
2598 ret = h2c_ack_settings(h2c);
2599 break;
2600
Willy Tarreaucf68c782017-10-10 17:11:41 +02002601 case H2_FT_PING:
2602 if (h2c->st0 == H2_CS_FRAME_P)
2603 ret = h2c_handle_ping(h2c);
2604
2605 if (h2c->st0 == H2_CS_FRAME_A)
2606 ret = h2c_ack_ping(h2c);
2607 break;
2608
Willy Tarreau26f95952017-07-27 17:18:30 +02002609 case H2_FT_WINDOW_UPDATE:
2610 if (h2c->st0 == H2_CS_FRAME_P)
2611 ret = h2c_handle_window_update(h2c, h2s);
2612 break;
2613
Willy Tarreau61290ec2017-10-17 08:19:21 +02002614 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002615 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2616 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2617 * frames' parsers consume all following CONTINUATION
2618 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002619 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002620 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau21178a52019-10-23 11:06:35 +02002621 if (!(h2c->flags & H2_CF_IS_BACK))
2622 sess_log(h2c->conn->owner);
Willy Tarreauea18f862018-12-22 20:19:26 +01002623 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002624
Willy Tarreau13278b42017-10-13 19:23:14 +02002625 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002626 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002627 if (h2c->flags & H2_CF_IS_BACK)
2628 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2629 else
2630 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002631 if (tmp_h2s) {
2632 h2s = tmp_h2s;
2633 ret = 1;
2634 }
2635 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002636 break;
2637
Willy Tarreau454f9052017-10-26 19:40:35 +02002638 case H2_FT_DATA:
2639 if (h2c->st0 == H2_CS_FRAME_P)
2640 ret = h2c_frt_handle_data(h2c, h2s);
2641
2642 if (h2c->st0 == H2_CS_FRAME_A)
2643 ret = h2c_send_strm_wu(h2c);
2644 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002645
Willy Tarreau92153fc2017-12-03 19:46:19 +01002646 case H2_FT_PRIORITY:
2647 if (h2c->st0 == H2_CS_FRAME_P)
2648 ret = h2c_handle_priority(h2c);
2649 break;
2650
Willy Tarreaucd234e92017-08-18 10:59:39 +02002651 case H2_FT_RST_STREAM:
2652 if (h2c->st0 == H2_CS_FRAME_P)
2653 ret = h2c_handle_rst_stream(h2c, h2s);
2654 break;
2655
Willy Tarreaue96b0922017-10-30 00:28:29 +01002656 case H2_FT_GOAWAY:
2657 if (h2c->st0 == H2_CS_FRAME_P)
2658 ret = h2c_handle_goaway(h2c);
2659 break;
2660
Willy Tarreau1c661982017-10-30 13:52:01 +01002661 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002662 default:
2663 /* drop frames that we ignore. They may be larger than
2664 * the buffer so we drain all of their contents until
2665 * we reach the end.
2666 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002667 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2668 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002669 h2c->dfl -= ret;
2670 ret = h2c->dfl == 0;
2671 }
2672
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002673 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002674 /* We may have to send an RST if not done yet */
2675 if (h2s->st == H2_SS_ERROR)
2676 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002677
Willy Tarreaua20a5192017-12-27 11:02:06 +01002678 if (h2c->st0 == H2_CS_FRAME_E)
2679 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002680
Willy Tarreau7e98c052017-10-10 15:56:59 +02002681 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002682 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002683 break;
2684
2685 if (h2c->st0 != H2_CS_FRAME_H) {
Christopher Faulete12a26f2019-09-26 16:38:28 +02002686 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2687 b_del(&h2c->dbuf, ret);
2688 h2c->dfl -= ret;
2689 if (!h2c->dfl)
2690 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002691 }
2692 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002693
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002694 if (h2c->rcvd_c > 0 &&
2695 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2696 h2c_send_conn_wu(h2c);
2697
Willy Tarreau52eed752017-09-22 15:05:09 +02002698 fail:
2699 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002700 if (h2s && h2s->cs &&
2701 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002702 conn_xprt_read0_pending(h2c->conn) ||
2703 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002704 (h2s->flags & H2_SF_ES_RCVD) ||
2705 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002706 /* we may have to signal the upper layers */
2707 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002708 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002709 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002710
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002711 if (old_iw != h2c->miw)
2712 h2c_unblock_sfctl(h2c);
2713
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002714 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002715}
2716
2717/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2718 * the end.
2719 */
2720static int h2_process_mux(struct h2c *h2c)
2721{
Olivier Houchard998410a2019-04-15 19:23:37 +02002722 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002723
Willy Tarreau01b44822018-10-03 14:26:37 +02002724 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2725 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2726 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2727 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau21178a52019-10-23 11:06:35 +02002728 if (h2c->st0 == H2_CS_ERROR)
Willy Tarreau01b44822018-10-03 14:26:37 +02002729 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau01b44822018-10-03 14:26:37 +02002730 goto fail;
2731 }
2732 h2c->st0 = H2_CS_SETTINGS1;
2733 }
2734 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002735 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002736 return 1;
2737 }
2738
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002739 /* start by sending possibly pending window updates */
Willy Tarreau9c497c22019-08-06 15:39:32 +02002740 if (h2c->rcvd_s > 0 &&
2741 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2742 h2c_send_strm_wu(h2c) < 0)
2743 goto fail;
2744
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002745 if (h2c->rcvd_c > 0 &&
2746 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2747 h2c_send_conn_wu(h2c) < 0)
2748 goto fail;
2749
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002750 /* First we always process the flow control list because the streams
2751 * waiting there were already elected for immediate emission but were
2752 * blocked just on this.
2753 */
2754
Olivier Houchard998410a2019-04-15 19:23:37 +02002755 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002756 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2757 h2c->st0 >= H2_CS_ERROR)
2758 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002759
Willy Tarreauc234ae32019-05-13 17:56:11 +02002760 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002761 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002762
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002763 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002764 /* For some reason, the upper layer failed to subsribe again,
2765 * so remove it from the send_list
2766 */
2767 if (!h2s->send_wait) {
2768 LIST_DEL_INIT(&h2s->list);
2769 continue;
2770 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002771 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002772 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002773 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002774 }
2775
Olivier Houchard998410a2019-04-15 19:23:37 +02002776 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002777 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2778 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002779
Willy Tarreauc234ae32019-05-13 17:56:11 +02002780 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002781 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002782
Olivier Houchard998410a2019-04-15 19:23:37 +02002783 /* For some reason, the upper layer failed to subsribe again,
2784 * so remove it from the send_list
2785 */
2786 if (!h2s->send_wait) {
2787 LIST_DEL_INIT(&h2s->list);
2788 continue;
2789 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002790 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002791 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002792 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002793 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002794 }
2795
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002796 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002797 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002798 if (h2c->st0 == H2_CS_ERROR) {
2799 if (h2c->max_id >= 0) {
2800 h2c_send_goaway_error(h2c, NULL);
2801 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2802 return 0;
2803 }
2804
2805 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2806 }
2807 return 1;
2808 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002809 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002810}
2811
Willy Tarreau62f52692017-10-08 23:01:42 +02002812
Willy Tarreau479998a2018-11-18 06:30:59 +01002813/* Attempt to read data, and subscribe if none available.
2814 * The function returns 1 if data has been received, otherwise zero.
2815 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002816static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002817{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002818 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002819 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002820 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002821 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002822
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002823 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002824 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002825
Willy Tarreau315d8072017-12-10 22:17:57 +01002826 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002827 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002828
Willy Tarreau44e973f2018-03-01 17:49:30 +01002829 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002830 if (!buf) {
2831 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002832 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002833 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002834
Olivier Houchard7505f942018-08-21 18:10:44 +02002835 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002836 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002837 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2838 /* HTX in use : try to pre-align the buffer like the
2839 * rxbufs will be to optimize memory copies. We'll make
2840 * sure that the frame header lands at the end of the
2841 * HTX block to alias it upon recv. We cannot use the
2842 * head because rcv_buf() will realign the buffer if
2843 * it's empty. Thus we cheat and pretend we already
2844 * have a few bytes there.
2845 */
2846 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002847 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002848 }
2849 else
2850 max = b_room(buf);
2851
Olivier Houchard7505f942018-08-21 18:10:44 +02002852 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002853 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002854 else
2855 ret = 0;
2856 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002857
Willy Tarreaua8fcdac2019-08-02 07:48:47 +02002858 if (max && !ret && h2_recv_allowed(h2c))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002859 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002860
Olivier Houcharda1411e62018-08-17 18:42:48 +02002861 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002862 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002863 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002864 }
2865
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002866 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002867 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002868 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002869}
2870
Willy Tarreau479998a2018-11-18 06:30:59 +01002871/* Try to send data if possible.
2872 * The function returns 1 if data have been sent, otherwise zero.
2873 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002874static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002875{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002876 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002877 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002878 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002879
2880 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002881 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002882
Olivier Houchard7505f942018-08-21 18:10:44 +02002883
Willy Tarreaua2af5122017-10-09 11:56:46 +02002884 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2885 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002886 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002887 }
2888
Willy Tarreaubc933932017-10-09 16:21:43 +02002889 /* This loop is quite simple : it tries to fill as much as it can from
2890 * pending streams into the existing buffer until it's reportedly full
2891 * or the end of send requests is reached. Then it tries to send this
2892 * buffer's contents out, marks it not full if at least one byte could
2893 * be sent, and tries again.
2894 *
2895 * The snd_buf() function normally takes a "flags" argument which may
2896 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2897 * data immediately comes and CO_SFL_STREAMER to indicate that the
2898 * connection is streaming lots of data (used to increase TLS record
2899 * size at the expense of latency). The former can be sent any time
2900 * there's a buffer full flag, as it indicates at least one stream
2901 * attempted to send and failed so there are pending data. An
2902 * alternative would be to set it as long as there's an active stream
2903 * but that would be problematic for ACKs until we have an absolute
2904 * guarantee that all waiters have at least one byte to send. The
2905 * latter should possibly not be set for now.
2906 */
2907
2908 done = 0;
2909 while (!done) {
2910 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002911 unsigned int released = 0;
2912 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02002913
2914 /* fill as much as we can into the current buffer */
2915 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2916 done = h2_process_mux(h2c);
2917
Olivier Houchard2b094432019-01-29 18:28:36 +01002918 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002919 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01002920
Willy Tarreaubc933932017-10-09 16:21:43 +02002921 if (conn->flags & CO_FL_ERROR)
2922 break;
2923
2924 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2925 flags |= CO_SFL_MSG_MORE;
2926
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002927 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
2928 if (b_data(buf)) {
2929 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002930 if (!ret) {
2931 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002932 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002933 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002934 sent = 1;
2935 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002936 if (b_data(buf)) {
2937 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002938 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002939 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002940 }
2941 b_free(buf);
2942 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02002943 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002944
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002945 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02002946 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002947
Willy Tarreaubc933932017-10-09 16:21:43 +02002948 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet07423082019-10-24 10:31:01 +02002949 if (sent)
2950 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02002951 }
2952
Willy Tarreaua2af5122017-10-09 11:56:46 +02002953 if (conn->flags & CO_FL_SOCK_WR_SH) {
2954 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02002955 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002956 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002957 /* We're not full anymore, so we can wake any task that are waiting
2958 * for us.
2959 */
Willy Tarreauc51d47c2019-09-25 07:57:31 +02002960 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002961 struct h2s *h2s;
2962
2963 list_for_each_entry(h2s, &h2c->send_list, list) {
2964 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2965 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002966
Willy Tarreauc234ae32019-05-13 17:56:11 +02002967 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002968 continue;
2969
Olivier Houchard998410a2019-04-15 19:23:37 +02002970 /* For some reason, the upper layer failed to subsribe again,
2971 * so remove it from the send_list
2972 */
2973 if (!h2s->send_wait) {
2974 LIST_DEL_INIT(&h2s->list);
2975 continue;
2976 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002977 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002978 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002979 tasklet_wakeup(h2s->send_wait->tasklet);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002980 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002981 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002982 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002983 /* We're done, no more to send */
Willy Tarreau662fafc2019-05-26 09:43:07 +02002984 if (!br_data(h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002985 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002986schedule:
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002987 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002988 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002989
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002990 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002991}
2992
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002993/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002994static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2995{
2996 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002997 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002998
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002999 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02003000 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003001 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02003002 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01003003 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02003004 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003005 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003006}
Willy Tarreaua2af5122017-10-09 11:56:46 +02003007
Willy Tarreau62f52692017-10-08 23:01:42 +02003008/* callback called on any event by the connection handler.
3009 * It applies changes and returns zero, or < 0 if it wants immediate
3010 * destruction of the connection (which normally doesn not happen in h2).
3011 */
Olivier Houchard7505f942018-08-21 18:10:44 +02003012static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003013{
Olivier Houchard7505f942018-08-21 18:10:44 +02003014 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003015
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003016 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01003017 h2_process_demux(h2c);
3018
3019 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003020 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003021
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003022 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01003023 h2c->flags &= ~H2_CF_DEM_DFULL;
3024 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003025 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003026
Willy Tarreau0b37d652018-10-03 10:33:02 +02003027 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01003028 /* frontend is stopping, reload likely in progress, let's try
3029 * to announce a graceful shutdown if not yet done. We don't
3030 * care if it fails, it will be tried again later.
3031 */
3032 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3033 if (h2c->last_sid < 0)
3034 h2c->last_sid = (1U << 31) - 1;
3035 h2c_send_goaway_error(h2c, NULL);
3036 }
3037 }
3038
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003039 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003040 * If we received early data, and the handshake is done, wake
3041 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003042 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003043 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
3044 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
3045 struct eb32_node *node;
3046 struct h2s *h2s;
3047
3048 h2c->flags |= H2_CF_WAIT_FOR_HS;
3049 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
3050
3051 while (node) {
3052 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01003053 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01003054 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003055 node = eb32_next(node);
3056 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003057 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003058
Willy Tarreau26bd7612017-10-09 16:47:04 +02003059 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01003060 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
3061 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
3062 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02003063 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003064
3065 if (eb_is_empty(&h2c->streams_by_id)) {
3066 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003067 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003068 return -1;
3069 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003070 }
3071
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003072 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003073 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003074
Olivier Houchard53216e72018-10-10 15:46:36 +02003075 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3076 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3077 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003078 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003079 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3080 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003081 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003082
Willy Tarreau3f133572017-10-31 19:21:06 +01003083 if (h2c->task) {
Willy Tarreau534d8f92019-10-01 10:12:00 +02003084 if (h2c_may_expire(h2c))
3085 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3086 else
3087 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003088 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003089 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003090
Olivier Houchard7505f942018-08-21 18:10:44 +02003091 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02003092 return 0;
3093}
3094
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003095/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003096static int h2_wake(struct connection *conn)
3097{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003098 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003099
3100 return (h2_process(h2c));
3101}
3102
Willy Tarreauea392822017-10-31 10:02:25 +01003103/* Connection timeout management. The principle is that if there's no receipt
3104 * nor sending for a certain amount of time, the connection is closed. If the
3105 * MUX buffer still has lying data or is not allocatable, the connection is
3106 * immediately killed. If it's allocatable and empty, we attempt to send a
3107 * GOAWAY frame.
3108 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003109static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003110{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003111 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003112 int expired = tick_is_expired(t->expire, now_ms);
3113
Willy Tarreau0975f112018-03-29 15:22:59 +02003114 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01003115 return t;
3116
Willy Tarreau534d8f92019-10-01 10:12:00 +02003117 if (h2c && !h2c_may_expire(h2c)) {
3118 /* we do still have streams but all of them are idle, waiting
3119 * for the data layer, so we must not enforce the timeout here.
3120 */
3121 t->expire = TICK_ETERNITY;
3122 return t;
3123 }
3124
Olivier Houchard3f795f72019-04-17 22:51:06 +02003125 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003126
3127 if (!h2c) {
3128 /* resources were already deleted */
3129 return NULL;
3130 }
3131
3132 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003133 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003134 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003135
Willy Tarreau662fafc2019-05-26 09:43:07 +02003136 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003137 /* don't even try to send a GOAWAY, the buffer is stuck */
3138 h2c->flags |= H2_CF_GOAWAY_FAILED;
3139 }
3140
3141 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003142 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003143 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3144 h2c->flags |= H2_CF_GOAWAY_FAILED;
3145
Willy Tarreau662fafc2019-05-26 09:43:07 +02003146 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003147 unsigned int released = 0;
3148 struct buffer *buf;
3149
3150 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3151 if (b_data(buf)) {
3152 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3153 if (!ret)
3154 break;
3155 b_del(buf, ret);
3156 if (b_data(buf))
3157 break;
3158 b_free(buf);
3159 released++;
3160 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003161 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003162
3163 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003164 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003165 }
Willy Tarreauea392822017-10-31 10:02:25 +01003166
Willy Tarreau0975f112018-03-29 15:22:59 +02003167 /* either we can release everything now or it will be done later once
3168 * the last stream closes.
3169 */
3170 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003171 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003172
Willy Tarreauea392822017-10-31 10:02:25 +01003173 return NULL;
3174}
3175
3176
Willy Tarreau62f52692017-10-08 23:01:42 +02003177/*******************************************/
3178/* functions below are used by the streams */
3179/*******************************************/
3180
3181/*
3182 * Attach a new stream to a connection
3183 * (Used for outgoing connections)
3184 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003185static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003186{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003187 struct conn_stream *cs;
3188 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003189 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003190
3191 cs = cs_new(conn);
3192 if (!cs)
3193 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003194 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003195 if (!h2s) {
3196 cs_free(cs);
3197 return NULL;
3198 }
3199 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003200}
3201
Willy Tarreaufafd3982018-11-18 21:29:20 +01003202/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3203 * We have to scan because we may have some orphan streams. It might be
3204 * beneficial to scan backwards from the end to reduce the likeliness to find
3205 * orphans.
3206 */
3207static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3208{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003209 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003210 struct h2s *h2s;
3211 struct eb32_node *node;
3212
3213 node = eb32_first(&h2c->streams_by_id);
3214 while (node) {
3215 h2s = container_of(node, struct h2s, by_id);
3216 if (h2s->cs)
3217 return h2s->cs;
3218 node = eb32_next(node);
3219 }
3220 return NULL;
3221}
3222
Olivier Houchard198d1292019-10-25 16:19:26 +02003223static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3224{
3225 int ret = 0;
3226 struct h2c *h2c = conn->ctx;
3227
3228 switch (mux_ctl) {
3229 case MUX_STATUS:
3230 /* Only consider the mux to be ready if we're done with
3231 * the preface and settings, and we had no error.
3232 */
3233 if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR)
3234 ret |= MUX_STATUS_READY;
3235 return ret;
3236 default:
3237 return -1;
3238 }
3239}
3240
Willy Tarreau62f52692017-10-08 23:01:42 +02003241/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003242 * Destroy the mux and the associated connection, if it is no longer used
3243 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003244static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003245{
Christopher Faulet73c12072019-04-08 11:23:22 +02003246 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003247
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003248 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003249 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003250}
3251
3252/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003253 * Detach the stream from the connection and possibly release the connection.
3254 */
3255static void h2_detach(struct conn_stream *cs)
3256{
Willy Tarreau60935142017-10-16 18:11:19 +02003257 struct h2s *h2s = cs->ctx;
3258 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003259 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003260
3261 cs->ctx = NULL;
3262 if (!h2s)
3263 return;
3264
Olivier Houchard998410a2019-04-15 19:23:37 +02003265 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003266 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003267 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003268 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003269 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003270 /*
3271 * At this point, the stream_interface is supposed to have called
3272 * h2_unsubscribe(), so the only way there's still a
3273 * subscription that came from the stream_interface (as we
3274 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3275 * without the stream_interface involved) is that we subscribed
3276 * for sending, we woke the tasklet up and removed the
3277 * SUB_RETRY_SEND flag, so the stream_interface would not
3278 * know it has to unsubscribe for send, but the tasklet hasn't
3279 * run yet. Make sure to handle that by explicitely setting
3280 * send_wait to NULL, as nothing else will do it for us.
3281 */
3282 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003283 }
3284
Olivier Houchardf502aca2018-12-14 19:42:40 +01003285 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003286 h2c = h2s->h2c;
3287 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003288 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003289 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3290 !h2_frt_has_too_many_cs(h2c)) {
3291 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003292 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003293 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003294 }
Willy Tarreau60935142017-10-16 18:11:19 +02003295
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003296 /* this stream may be blocked waiting for some data to leave (possibly
3297 * an ES or RST frame), so orphan it in this case.
3298 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003299 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003300 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003301 (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 +01003302 return;
3303
Willy Tarreau45f752e2017-10-30 15:44:59 +01003304 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3305 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3306 /* unblock the connection if it was blocked on this
3307 * stream.
3308 */
3309 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3310 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003311 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003312 }
3313
Willy Tarreau71049cc2018-03-28 13:56:39 +02003314 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003315
Olivier Houchard8a786902018-12-15 16:05:40 +01003316 if (h2c->flags & H2_CF_IS_BACK &&
3317 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003318 if (!(h2c->conn->flags &
3319 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3320 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003321 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003322 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3323 h2c->conn->owner = NULL;
3324 if (eb_is_empty(&h2c->streams_by_id)) {
3325 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3326 /* The server doesn't want it, let's kill the connection right away */
3327 h2c->conn->mux->destroy(h2c->conn);
3328 return;
3329 }
3330 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003331 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003332 if (eb_is_empty(&h2c->streams_by_id)) {
3333 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3334 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3335 return;
3336 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003337 /* Never ever allow to reuse a connection from a non-reuse backend */
3338 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3339 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003340 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003341 struct server *srv = objt_server(h2c->conn->target);
3342
3343 if (srv) {
3344 if (h2c->conn->flags & CO_FL_PRIVATE)
3345 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3346 else
3347 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3348 }
3349
3350 }
3351 }
3352 }
3353
Willy Tarreaue323f342018-03-28 13:51:45 +02003354 /* We don't want to close right now unless we're removing the
3355 * last stream, and either the connection is in error, or it
3356 * reached the ID already specified in a GOAWAY frame received
3357 * or sent (as seen by last_sid >= 0).
3358 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003359 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003360 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003361 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003362 }
3363 else if (h2c->task) {
Willy Tarreau534d8f92019-10-01 10:12:00 +02003364 if (h2c_may_expire(h2c))
3365 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3366 else
3367 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003368 task_queue(h2c->task);
Willy Tarreau60935142017-10-16 18:11:19 +02003369 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003370}
3371
Willy Tarreau88bdba32019-05-13 18:17:53 +02003372/* Performs a synchronous or asynchronous shutr(). */
3373static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003374{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003375 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003376 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003377
Willy Tarreauf983d002019-05-14 10:40:21 +02003378 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003379 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003380
Willy Tarreau18059042019-01-31 19:12:48 +01003381 /* a connstream may require us to immediately kill the whole connection
3382 * for example because of a "tcp-request content reject" rule that is
3383 * normally used to limit abuse. In this case we schedule a goaway to
3384 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003385 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003386 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003387 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3388 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3389 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3390 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003391 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3392 /* Nothing was never sent for this stream, so reset with
3393 * REFUSED_STREAM error to let the client retry the
3394 * request.
3395 */
3396 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3397 }
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003398 else {
3399 /* a final response was already provided, we don't want this
3400 * stream anymore. This may happen when the server responds
3401 * before the end of an upload and closes quickly (redirect,
3402 * deny, ...)
3403 */
3404 h2s_error(h2s, H2_ERR_CANCEL);
3405 }
Willy Tarreau18059042019-01-31 19:12:48 +01003406
Willy Tarreau90c32322017-11-24 08:00:30 +01003407 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003408 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003409 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003410
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003411 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003412 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003413 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003414 done:
3415 h2s->flags &= ~H2_SF_WANT_SHUTR;
3416 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003417add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003418 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003419 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003420 if (h2s->flags & H2_SF_BLK_MFCTL) {
3421 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3422 h2s->send_wait = sw;
3423 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3424 h2s->send_wait = sw;
3425 LIST_ADDQ(&h2c->send_list, &h2s->list);
3426 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003427 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003428 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003429 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003430 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003431}
3432
Willy Tarreau88bdba32019-05-13 18:17:53 +02003433/* Performs a synchronous or asynchronous shutw(). */
3434static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003435{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003436 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003437 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003438
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003439 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003440 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003441
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003442 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003443 /* we can cleanly close using an empty data frame only after headers */
3444
3445 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3446 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003447 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003448
3449 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003450 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003451 else
3452 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003453 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003454 /* a connstream may require us to immediately kill the whole connection
3455 * for example because of a "tcp-request content reject" rule that is
3456 * normally used to limit abuse. In this case we schedule a goaway to
3457 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003458 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003459 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003460 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3461 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3462 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3463 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003464 else {
3465 /* Nothing was never sent for this stream, so reset with
3466 * REFUSED_STREAM error to let the client retry the
3467 * request.
3468 */
3469 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3470 }
Willy Tarreau18059042019-01-31 19:12:48 +01003471
Willy Tarreau90c32322017-11-24 08:00:30 +01003472 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003473 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003474 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003475
Willy Tarreau00dd0782018-03-01 16:31:34 +01003476 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003477 }
3478
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003479 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003480 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003481 done:
3482 h2s->flags &= ~H2_SF_WANT_SHUTW;
3483 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003484
3485 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003486 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003487 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003488 if (h2s->flags & H2_SF_BLK_MFCTL) {
3489 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3490 h2s->send_wait = sw;
3491 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3492 h2s->send_wait = sw;
3493 LIST_ADDQ(&h2c->send_list, &h2s->list);
3494 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003495 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003496 /* let the handler know we want to shutw */
3497 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003498 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003499}
3500
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003501/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003502 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3503 * and prevented the last frame from being emitted.
3504 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003505static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3506{
3507 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003508 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003509
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003510 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003511 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003512 h2_do_shutw(h2s);
3513
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003514 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003515 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003516
Willy Tarreau88bdba32019-05-13 18:17:53 +02003517 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003518 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003519 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003520
Willy Tarreau88bdba32019-05-13 18:17:53 +02003521 if (!h2s->cs) {
3522 h2s_destroy(h2s);
3523 if (h2c_is_dead(h2c))
3524 h2_release(h2c);
3525 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003526 }
3527
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003528 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003529}
3530
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003531/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003532static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3533{
3534 struct h2s *h2s = cs->ctx;
3535
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003536 if (cs->flags & CS_FL_KILL_CONN)
3537 h2s->flags |= H2_SF_KILL_CONN;
3538
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003539 if (!mode)
3540 return;
3541
3542 h2_do_shutr(h2s);
3543}
3544
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003545/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003546static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3547{
3548 struct h2s *h2s = cs->ctx;
3549
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003550 if (cs->flags & CS_FL_KILL_CONN)
3551 h2s->flags |= H2_SF_KILL_CONN;
3552
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003553 h2_do_shutw(h2s);
3554}
3555
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003556/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003557 * HTX request or response depending on the connection's side. Returns a
3558 * positive value on success, a negative value on failure, or 0 if it couldn't
3559 * proceed. May report connection errors in h2c->errcode if the frame is
3560 * non-decodable and the connection unrecoverable. In absence of connection
3561 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003562 *
3563 * The function may fold CONTINUATION frames into the initial HEADERS frame
3564 * by removing padding and next frame header, then moving the CONTINUATION
3565 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3566 * leaving a hole between the main frame and the beginning of the next one.
3567 * The possibly remaining incomplete or next frame at the end may be moved
3568 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3569 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3570 *
3571 * A buffer at the beginning of processing may look like this :
3572 *
3573 * ,---.---------.-----.--------------.--------------.------.---.
3574 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3575 * `---^---------^-----^--------------^--------------^------^---'
3576 * | | <-----> | |
3577 * area | dpl | wrap
3578 * |<--------------> |
3579 * | dfl |
3580 * |<-------------------------------------------------->|
3581 * head data
3582 *
3583 * Padding is automatically overwritten when folding, participating to the
3584 * hole size after dfl :
3585 *
3586 * ,---.------------------------.-----.--------------.------.---.
3587 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3588 * `---^------------------------^-----^--------------^------^---'
3589 * | | <-----> | |
3590 * area | hole | wrap
3591 * |<-----------------------> |
3592 * | dfl |
3593 * |<-------------------------------------------------->|
3594 * head data
3595 *
3596 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3597 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3598 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003599 *
3600 * The <flags> field must point to either the stream's flags or to a copy of it
3601 * so that the function can update the following flags :
3602 * - H2_SF_DATA_CLEN when content-length is seen
3603 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3604 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003605 *
3606 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3607 * decoding, in order to detect if we're dealing with a headers or a trailers
3608 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003609 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003610static 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 +02003611{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003612 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003613 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003614 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003615 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003616 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003617 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003618 int flen; // header frame len
3619 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003620 int ret = 0;
3621 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003622 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003623 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003624
Willy Tarreauea18f862018-12-22 20:19:26 +01003625next_frame:
3626 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3627 goto leave; // incomplete input frame
3628
3629 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3630 * this case, we'll try to paste it immediately after the initial
3631 * HEADERS frame payload and kill any possible padding. The initial
3632 * frame's length will be increased to represent the concatenation
3633 * of the two frames. The next frame is read from position <tlen>
3634 * and written at position <flen> (minus padding if some is present).
3635 */
3636 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3637 struct h2_fh hdr;
3638 int clen; // CONTINUATION frame's payload length
3639
3640 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3641 /* no more data, the buffer may be full, either due to
3642 * too large a frame or because of too large a hole that
3643 * we're going to compact at the end.
3644 */
3645 goto leave;
3646 }
3647
3648 if (hdr.ft != H2_FT_CONTINUATION) {
3649 /* RFC7540#6.10: frame of unexpected type */
3650 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3651 goto fail;
3652 }
3653
3654 if (hdr.sid != h2c->dsi) {
3655 /* RFC7540#6.10: frame of different stream */
3656 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3657 goto fail;
3658 }
3659
3660 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3661 /* RFC7540#4.2: invalid frame length */
3662 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3663 goto fail;
3664 }
3665
3666 /* detect when we must stop aggragating frames */
3667 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3668
3669 /* Take as much as we can of the CONTINUATION frame's payload */
3670 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3671 if (clen > hdr.len)
3672 clen = hdr.len;
3673
3674 /* Move the frame's payload over the padding, hole and frame
3675 * header. At least one of hole or dpl is null (see diagrams
3676 * above). The hole moves after the new aggragated frame.
3677 */
3678 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3679 h2c->dfl += clen - h2c->dpl;
3680 hole += h2c->dpl + 9;
3681 h2c->dpl = 0;
3682 goto next_frame;
3683 }
3684
3685 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003686
Willy Tarreau13278b42017-10-13 19:23:14 +02003687 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003688 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003689 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003690 copy = alloc_trash_chunk();
3691 if (!copy) {
3692 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3693 goto fail;
3694 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003695 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3696 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3697 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003698 }
3699
Willy Tarreau13278b42017-10-13 19:23:14 +02003700 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3701 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003702 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003703 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3704 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003705 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003706 }
3707
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003708 if (flen < 5) {
3709 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3710 goto fail;
3711 }
3712
Willy Tarreau13278b42017-10-13 19:23:14 +02003713 hdrs += 5; // stream dep = 4, weight = 1
3714 flen -= 5;
3715 }
3716
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003717 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003718 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003719 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003720 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003721
Willy Tarreau937f7602018-02-26 15:22:17 +01003722 /* we can't retry a failed decompression operation so we must be very
3723 * careful not to take any risks. In practice the output buffer is
3724 * always empty except maybe for trailers, in which case we simply have
3725 * to wait for the upper layer to finish consuming what is available.
3726 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003727
3728 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003729 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003730 if (!htx_is_empty(htx)) {
3731 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003732 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003733 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003734 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003735 if (b_data(rxbuf)) {
3736 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003737 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003738 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003739
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003740 rxbuf->head = 0;
3741 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003742 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003743
Willy Tarreau25919232019-01-03 14:48:18 +01003744 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003745 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3746 sizeof(list)/sizeof(list[0]), tmp);
3747 if (outlen < 0) {
3748 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3749 goto fail;
3750 }
3751
Willy Tarreau25919232019-01-03 14:48:18 +01003752 /* The PACK decompressor was updated, let's update the input buffer and
3753 * the parser's state to commit these changes and allow us to later
3754 * fail solely on the stream if needed.
3755 */
3756 b_del(&h2c->dbuf, h2c->dfl + hole);
3757 h2c->dfl = hole = 0;
3758 h2c->st0 = H2_CS_FRAME_H;
3759
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003760 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003761 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003762
Willy Tarreau88d138e2019-01-02 19:38:14 +01003763 if (*flags & H2_SF_HEADERS_RCVD)
3764 goto trailers;
3765
3766 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003767 if (htx) {
3768 /* HTX mode */
3769 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003770 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003771 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003772 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003773 } else {
3774 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003775 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003776 if (outlen > 0)
3777 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003778 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003779
3780 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003781 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003782 goto fail;
3783 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003784
Willy Tarreau174b06a2018-04-25 18:13:58 +02003785 if (msgf & H2_MSGF_BODY) {
3786 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003787 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003788 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003789 if (htx)
3790 htx->extra = *body_len;
3791 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003792 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003793 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003794 }
3795
Willy Tarreau88d138e2019-01-02 19:38:14 +01003796 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003797 /* indicate that a HEADERS frame was received for this stream, except
3798 * for 1xx responses. For 1xx responses, another HEADERS frame is
3799 * expected.
3800 */
3801 if (!(msgf & H2_MSGF_RSP_1XX))
3802 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003803
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02003804 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003805 /* Mark the end of message, either using EOM in HTX or with the
3806 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003807 * is not set during headers with END_STREAM. For HTX trailers,
3808 * we must not leave an HTX trailers block not followed by an
3809 * EOM block, the two must be atomic. Thus if we fail to emit
3810 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003811 */
3812 if (htx) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02003813 if (!htx_add_endof(htx, HTX_BLK_EOM))
Willy Tarreau88d138e2019-01-02 19:38:14 +01003814 goto fail;
3815 }
3816 else if (*flags & H2_SF_DATA_CHNK) {
3817 if (!b_putblk(rxbuf, "\r\n", 2))
3818 goto fail;
3819 }
3820 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003821
Willy Tarreau86277d42019-01-02 15:36:11 +01003822 /* success */
3823 ret = 1;
3824
Willy Tarreau68dd9852017-07-03 14:44:26 +02003825 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003826 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003827 * move the remaining data over it.
3828 */
3829 if (hole) {
3830 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3831 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3832 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3833 b_sub(&h2c->dbuf, hole);
3834 }
3835
Willy Tarreau97215ca2019-04-29 10:20:21 +02003836 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003837 /* too large frames */
3838 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003839 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003840 }
3841
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003842 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003843 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003844 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003845 return ret;
3846
Willy Tarreau68dd9852017-07-03 14:44:26 +02003847 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003848 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003849 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003850
3851 trailers:
3852 /* This is the last HEADERS frame hence a trailer */
3853
3854 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3855 /* It's a trailer but it's missing ES flag */
3856 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3857 goto fail;
3858 }
3859
Christopher Faulet54b5e212019-06-04 10:08:28 +02003860 /* Trailers terminate a DATA sequence. In HTX we always handle them. In
3861 * legacy, when using chunks, we have to emit the 0 CRLF marker first
3862 * and then handle the trailers. For other modes, the trailers are
3863 * silently dropped.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003864 */
3865 if (htx) {
Willy Tarreau5255f282019-01-03 18:41:05 +01003866 if (h2_make_htx_trailers(list, htx) <= 0)
3867 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003868 }
3869 else if (*flags & H2_SF_DATA_CHNK) {
3870 /* Legacy mode with chunked encoding : we must finalize the
3871 * data block message emit the trailing CRLF */
3872 if (!b_putblk(rxbuf, "0\r\n", 3))
3873 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003874
3875 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3876 if (outlen > 0)
3877 b_add(rxbuf, outlen);
3878 else
3879 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003880 }
3881
3882 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003883}
3884
Willy Tarreau454f9052017-10-26 19:40:35 +02003885/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3886 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3887 * in use, a new chunk is emitted for each frame. This is supposed to fit
3888 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3889 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3890 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003891 * parser state is automatically updated. Returns > 0 if it could completely
3892 * send the current frame, 0 if it couldn't complete, in which case
3893 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3894 * DATA frame can return 0 as a valid result). Stream errors are reported in
3895 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3896 * have checked the frame header and ensured that the frame was complete or the
3897 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003898 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003899static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003900{
3901 struct h2c *h2c = h2s->h2c;
3902 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003903 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003904 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003905 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003906 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003907
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003908 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003909
Olivier Houchard638b7992018-08-16 15:41:52 +02003910 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003911 if (!csbuf) {
3912 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003913 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003914 }
3915
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003916try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003917 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003918 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3919 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003920 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003921 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003922
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003923 if (flen > b_data(&h2c->dbuf)) {
3924 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003925 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003926 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003927 }
3928
Willy Tarreaua9b77962019-01-31 07:23:00 +01003929 if (htx) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003930 unsigned int sent;
3931
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003932 block1 = htx_free_data_space(htx);
3933 if (!block1) {
3934 h2c->flags |= H2_CF_DEM_SFULL;
3935 goto fail;
3936 }
3937 if (flen > block1)
3938 flen = block1;
3939
3940 /* here, flen is the max we can copy into the output buffer */
3941 block1 = b_contig_data(&h2c->dbuf, 0);
3942 if (flen > block1)
3943 flen = block1;
3944
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003945 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003946
Willy Tarreaub6563f42019-06-15 11:34:41 +02003947 b_del(&h2c->dbuf, sent);
3948 h2c->dfl -= sent;
3949 h2c->rcvd_c += sent;
3950 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003951
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003952 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreaub6563f42019-06-15 11:34:41 +02003953 h2s->body_len -= sent;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003954 htx->extra = h2s->body_len;
3955 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003956
3957 if (sent < flen) {
3958 h2c->flags |= H2_CF_DEM_SFULL;
3959 goto fail;
3960 }
3961
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003962 goto try_again;
3963 }
Willy Tarreau455d5682019-05-24 19:42:18 +02003964 else if (unlikely(b_space_wraps(csbuf) &&
3965 flen + chklen <= b_room(csbuf) &&
3966 b_data(csbuf) <= MAX_DATA_REALIGN)) {
3967 /* it doesn't fit in a single block and the buffer is fragmented, if there are
3968 * not too many data in the buffer, let's defragment it and try
3969 * again.
Willy Tarreaud755ea62018-02-26 15:44:54 +01003970 */
3971 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003972 }
3973
Willy Tarreaueba10f22018-04-25 20:44:22 +02003974 /* chunked-encoding requires more room */
3975 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003976 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003977 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3978 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3979 (chklen < 1048576) ? 4 : 8;
3980 chklen += 4; // CRLF, CRLF
3981 }
3982
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003983 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003984 if (flen + chklen > b_room(csbuf)) {
3985 if (chklen >= b_room(csbuf)) {
3986 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003987 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003988 }
3989 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003990 }
3991
3992 if (h2s->flags & H2_SF_DATA_CHNK) {
3993 /* emit the chunk size */
3994 unsigned int chksz = flen;
3995 char str[10];
3996 char *beg;
3997
3998 beg = str + sizeof(str);
3999 *--beg = '\n';
4000 *--beg = '\r';
4001 do {
4002 *--beg = hextab[chksz & 0xF];
4003 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004004 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004005 }
4006
Willy Tarreau454f9052017-10-26 19:40:35 +02004007 /* Block1 is the length of the first block before the buffer wraps,
4008 * block2 is the optional second block to reach the end of the frame.
4009 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004010 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004011 if (block1 > flen)
4012 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02004013 block2 = flen - block1;
4014
4015 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01004016 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02004017
4018 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01004019 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02004020
Willy Tarreaueba10f22018-04-25 20:44:22 +02004021 if (h2s->flags & H2_SF_DATA_CHNK) {
4022 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01004023 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02004024 }
4025
Willy Tarreau454f9052017-10-26 19:40:35 +02004026 /* now mark the input data as consumed (will be deleted from the buffer
4027 * by the caller when seeing FRAME_A after sending the window update).
4028 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004029 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004030 h2c->dfl -= flen;
4031 h2c->rcvd_c += flen;
4032 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
4033
Willy Tarreau1915ca22019-01-24 11:49:37 +01004034 if (h2s->flags & H2_SF_DATA_CLEN)
4035 h2s->body_len -= flen;
4036
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004037 if (h2c->dfl > h2c->dpl) {
4038 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01004039 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004040 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004041 }
4042
Willy Tarreau4a28da12018-01-04 14:41:00 +01004043 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004044 /* here we're done with the frame, all the payload (except padding) was
4045 * transferred.
4046 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02004047
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004048 if (h2c->dff & H2_F_DATA_END_STREAM) {
4049 if (htx) {
4050 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
4051 h2c->flags |= H2_CF_DEM_SFULL;
4052 goto fail;
4053 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01004054 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004055 else if (h2s->flags & H2_SF_DATA_CHNK) {
4056 /* emit the trailing 0 CRLF CRLF */
4057 if (b_room(csbuf) < 5) {
4058 h2c->flags |= H2_CF_DEM_SFULL;
4059 goto fail;
4060 }
4061 chklen += 5;
4062 b_putblk(csbuf, "0\r\n\r\n", 5);
4063 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02004064 }
4065
Willy Tarreaud1023bb2018-03-22 16:53:12 +01004066 h2c->rcvd_c += h2c->dpl;
4067 h2c->rcvd_s += h2c->dpl;
4068 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004069 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004070 if (htx)
4071 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004072 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004073 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004074 if (htx)
4075 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004076 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004077}
4078
Willy Tarreau5dd17352018-06-14 13:33:30 +02004079/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
4080 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
4081 * number of bytes sent. The caller must check the stream's status to detect
4082 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004083 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004084static 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 +02004085{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004086 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004087 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004088 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004089 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004090 struct buffer *mbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004091 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004092 int es_now = 0;
4093 int ret = 0;
4094 int hdr;
4095
4096 if (h2c_mux_busy(h2c, h2s)) {
4097 h2s->flags |= H2_SF_BLK_MBUSY;
4098 return 0;
4099 }
4100
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004101 /* First, try to parse the H1 response and index it into <list>.
4102 * NOTE! Since it comes from haproxy, we *know* that a response header
4103 * block does not wrap and we can safely read it this way without
4104 * having to realign the buffer.
4105 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004106 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004107 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004108 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01004109 /* incomplete or invalid response, this is abnormal coming from
4110 * haproxy and may only result in a bad errorfile or bad Lua code
4111 * so that won't be fixed, raise an error now.
4112 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004113 * FIXME: we should instead add the ability to only return a
4114 * 502 bad gateway. But in theory this is not supposed to
4115 * happen.
4116 */
4117 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4118 ret = 0;
4119 goto end;
4120 }
4121
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004122 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02004123
4124 /* certain statuses have no body or an empty one, regardless of
4125 * what the headers say.
4126 */
4127 if (sl.st.status >= 100 && sl.st.status < 200) {
4128 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
4129 h1m->curr_len = h1m->body_len = 0;
4130 }
4131 else if (sl.st.status == 204 || sl.st.status == 304) {
4132 /* no contents, claim c-len is present and set to zero */
4133 h1m->flags &= ~H1_MF_CHNK;
4134 h1m->flags |= H1_MF_CLEN;
4135 h1m->curr_len = h1m->body_len = 0;
4136 }
4137
Willy Tarreaubcc45952019-05-26 10:05:50 +02004138 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004139 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004140 if (!h2_get_buf(h2c, mbuf)) {
4141 h2c->flags |= H2_CF_MUX_MALLOC;
4142 h2s->flags |= H2_SF_BLK_MROOM;
4143 return 0;
4144 }
4145
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004146 chunk_reset(&outbuf);
4147
4148 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004149 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4150 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004151 break;
4152 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004153 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004154 }
4155
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004156 if (outbuf.size < 9)
4157 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004158
4159 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004160 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4161 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4162 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004163
4164 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004165 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01004166 /* this is an unparsable response */
4167 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4168 ret = 0;
4169 goto end;
4170 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01004171
4172 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004173 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004174 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004175 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004176 }
4177
4178 /* encode all headers, stop at empty name */
4179 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01004180 /* these ones do not exist in H2 and must be dropped. */
4181 if (isteq(list[hdr].n, ist("connection")) ||
4182 isteq(list[hdr].n, ist("proxy-connection")) ||
4183 isteq(list[hdr].n, ist("keep-alive")) ||
4184 isteq(list[hdr].n, ist("upgrade")) ||
4185 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004186 continue;
4187
4188 if (isteq(list[hdr].n, ist("")))
4189 break; // end
4190
4191 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4192 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004193 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004194 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004195 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004196 }
4197 }
4198
4199 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01004200 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004201 es_now = 1;
4202
4203 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004204 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004205
4206 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004207 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004208
4209 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004210 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004211
4212 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004213 b_add(mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01004214 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004215
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004216 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01004217 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004218 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01004219
Willy Tarreau801250e2018-09-11 11:45:04 +02004220 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004221 h2s->flags |= H2_SF_ES_SENT;
4222 if (h2s->st == H2_SS_OPEN)
4223 h2s->st = H2_SS_HLOC;
4224 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004225 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004226 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004227 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004228 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004229 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004230 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004231 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004232 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004233 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004234
4235 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004236
4237 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004238 //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 +02004239 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004240 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004241 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4242 goto retry;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004243 h1m_init_res(h1m);
4244 h1m->err_pos = -1; // don't care about errors on the response path
4245 h2c->flags |= H2_CF_MUX_MFULL;
4246 h2s->flags |= H2_SF_BLK_MROOM;
4247 ret = 0;
4248 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004249}
4250
Willy Tarreau5dd17352018-06-14 13:33:30 +02004251/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4252 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4253 * the number of bytes sent. The caller must check the stream's status to
4254 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004255 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004256static 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 +02004257{
4258 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004259 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004260 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004261 struct buffer *mbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004262 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004263 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004264 int es_now = 0;
4265 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004266 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004267 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004268
4269 if (h2c_mux_busy(h2c, h2s)) {
4270 h2s->flags |= H2_SF_BLK_MBUSY;
4271 goto end;
4272 }
4273
Willy Tarreaubcc45952019-05-26 10:05:50 +02004274 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004275 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004276 if (!h2_get_buf(h2c, mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004277 h2c->flags |= H2_CF_MUX_MALLOC;
4278 h2s->flags |= H2_SF_BLK_MROOM;
4279 goto end;
4280 }
4281
4282 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004283 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004284 goto end;
4285
4286 chunk_reset(&outbuf);
4287
4288 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004289 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4290 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004291 break;
4292 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004293 /* If there are pending data in the output buffer, and we have
4294 * less than 1/4 of the mbuf's size and everything fits, we'll
4295 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4296 * is full and wait, to save some slow realign calls.
4297 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004298 if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004299 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4300 goto retry;
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004301 h2c->flags |= H2_CF_MUX_MFULL;
4302 h2s->flags |= H2_SF_BLK_MROOM;
4303 goto end;
4304 }
4305
Willy Tarreaubcc45952019-05-26 10:05:50 +02004306 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004307 }
4308
4309 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004310 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4311 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004312 h2c->flags |= H2_CF_MUX_MFULL;
4313 h2s->flags |= H2_SF_BLK_MROOM;
4314 goto end;
4315 }
4316
4317 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004318 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4319 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4320 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004321
4322 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4323 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004324 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004325 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004326 break;
4327 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004328 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004329 if ((long long)size > h1m->curr_len)
4330 size = h1m->curr_len;
4331 break;
4332 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004333 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004334 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004335 if (!ret)
4336 goto end;
4337
4338 if (ret < 0) {
4339 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004340 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004341 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4342 goto end;
4343 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004344 max -= ret;
4345 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004346 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004347 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004348 }
4349
Willy Tarreau801250e2018-09-11 11:45:04 +02004350 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004351 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004352 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004353 if (!ret)
4354 goto end;
4355
4356 if (ret < 0) {
4357 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004358 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004359 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4360 goto end;
4361 }
4362
4363 size = chunk;
4364 h1m->curr_len = chunk;
4365 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004366 max -= ret;
4367 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004368 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004369 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004370 if (!size)
4371 goto send_empty;
4372 }
4373
4374 /* in MSG_DATA state, continue below */
4375 size = h1m->curr_len;
4376 break;
4377 }
4378
4379 /* we have in <size> the exact number of bytes we need to copy from
4380 * the H1 buffer. We need to check this against the connection's and
4381 * the stream's send windows, and to ensure that this fits in the max
4382 * frame size and in the buffer's available space minus 9 bytes (for
4383 * the frame header). The connection's flow control is applied last so
4384 * that we can use a separate list of streams which are immediately
4385 * unblocked on window opening. Note: we don't implement padding.
4386 */
4387
Willy Tarreau5dd17352018-06-14 13:33:30 +02004388 if (size > max)
4389 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004390
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004391 if (size > h2s_mws(h2s))
4392 size = h2s_mws(h2s);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004393
4394 if (size <= 0) {
4395 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004396 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004397 LIST_DEL_INIT(&h2s->list);
Willy Tarreau55dc0842019-10-22 10:04:39 +02004398 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004399 goto end;
4400 }
4401
4402 if (h2c->mfs && size > h2c->mfs)
4403 size = h2c->mfs;
4404
4405 if (size + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02004406 /* It doesn't fit at once. If it at least fits once split and
4407 * the amount of data to move is low, let's defragment the
4408 * buffer now.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004409 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004410 if (b_space_wraps(mbuf) &&
4411 (size + 9 <= b_room(mbuf)) &&
4412 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004413 goto realign_again;
4414 size = outbuf.size - 9;
4415 }
4416
4417 if (size <= 0) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004418 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4419 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004420 h2c->flags |= H2_CF_MUX_MFULL;
4421 h2s->flags |= H2_SF_BLK_MROOM;
4422 goto end;
4423 }
4424
4425 if (size > h2c->mws)
4426 size = h2c->mws;
4427
4428 if (size <= 0) {
4429 h2s->flags |= H2_SF_BLK_MFCTL;
4430 goto end;
4431 }
4432
4433 /* copy whatever we can */
4434 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004435 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004436 if (ret == 1)
4437 len2 = 0;
4438
4439 if (!ret || len1 + len2 < size) {
4440 /* FIXME: must normally never happen */
4441 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4442 goto end;
4443 }
4444
4445 /* limit len1/len2 to size */
4446 if (len1 + len2 > size) {
4447 int sub = len1 + len2 - size;
4448
4449 if (len2 > sub)
4450 len2 -= sub;
4451 else {
4452 sub -= len2;
4453 len2 = 0;
4454 len1 -= sub;
4455 }
4456 }
4457
4458 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004459 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004460 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004461 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004462
4463 send_empty:
4464 /* we may need to add END_STREAM */
4465 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4466 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004467 *
4468 * FIXME: what we do here is not correct because we send end_stream
4469 * before knowing if we'll have to send a HEADERS frame for the
4470 * trailers. More importantly we're not consuming the trailing CRLF
4471 * after the end of trailers, so it will be left to the caller to
4472 * eat it. The right way to do it would be to measure trailers here
4473 * and to send ES only if there are no trailers.
4474 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004475 */
4476 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004477 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004478 es_now = 1;
4479
4480 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004481 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004482
4483 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004484 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004485
4486 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004487 b_add(mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004488
4489 /* consume incoming H1 response */
4490 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004491 max -= size;
4492 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004493 total += size;
4494 h1m->curr_len -= size;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004495 h2s->sws -= size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004496 h2c->mws -= size;
4497
4498 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004499 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004500 goto new_frame;
4501 }
4502 }
4503
4504 if (es_now) {
4505 if (h2s->st == H2_SS_OPEN)
4506 h2s->st = H2_SS_HLOC;
4507 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004508 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004509
Willy Tarreau35a62702018-02-27 15:37:25 +01004510 if (!(h1m->flags & H1_MF_CHNK)) {
4511 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004512 total += max;
4513 ofs += max;
4514 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004515
Willy Tarreau801250e2018-09-11 11:45:04 +02004516 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004517 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004518
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004519 h2s->flags |= H2_SF_ES_SENT;
4520 }
4521
4522 end:
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004523 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 +02004524 return total;
4525}
4526
Willy Tarreau115e83b2018-12-01 19:17:53 +01004527/* Try to send a HEADERS frame matching HTX response present in HTX message
4528 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4529 * must check the stream's status to detect any error which might have happened
4530 * subsequently to a successful send. The htx blocks are automatically removed
4531 * from the message. The htx message is assumed to be valid since produced from
4532 * the internal code, hence it contains a start line, an optional series of
4533 * header blocks and an end of header, otherwise an invalid frame could be
4534 * emitted and the resulting htx message could be left in an inconsistent state.
4535 */
4536static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4537{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004538 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004539 struct h2c *h2c = h2s->h2c;
4540 struct htx_blk *blk;
4541 struct htx_blk *blk_end;
4542 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004543 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004544 struct htx_sl *sl;
4545 enum htx_blk_type type;
4546 int es_now = 0;
4547 int ret = 0;
4548 int hdr;
4549 int idx;
4550
4551 if (h2c_mux_busy(h2c, h2s)) {
4552 h2s->flags |= H2_SF_BLK_MBUSY;
4553 return 0;
4554 }
4555
Willy Tarreau115e83b2018-12-01 19:17:53 +01004556 /* determine the first block which must not be deleted, blk_end may
4557 * be NULL if all blocks have to be deleted.
4558 */
4559 idx = htx_get_head(htx);
4560 blk_end = NULL;
4561 while (idx != -1) {
4562 type = htx_get_blk_type(htx_get_blk(htx, idx));
4563 idx = htx_get_next(htx, idx);
4564 if (type == HTX_BLK_EOH) {
4565 if (idx != -1)
4566 blk_end = htx_get_blk(htx, idx);
4567 break;
4568 }
4569 }
4570
4571 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004572 blk = htx_get_head_blk(htx);
4573 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL);
4574 ALREADY_CHECKED(blk);
4575 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004576 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004577 if (h2s->status < 100 || h2s->status > 999)
4578 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004579
4580 /* and the rest of the headers, that we dump starting at header 0 */
4581 hdr = 0;
4582
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004583 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004584 while ((idx = htx_get_next(htx, idx)) != -1) {
4585 blk = htx_get_blk(htx, idx);
4586 type = htx_get_blk_type(blk);
4587
4588 if (type == HTX_BLK_UNUSED)
4589 continue;
4590
4591 if (type != HTX_BLK_HDR)
4592 break;
4593
4594 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4595 goto fail;
4596
4597 list[hdr].n = htx_get_blk_name(htx, blk);
4598 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004599 hdr++;
4600 }
4601
4602 /* marker for end of headers */
4603 list[hdr].n = ist("");
4604
4605 if (h2s->status == 204 || h2s->status == 304) {
4606 /* no contents, claim c-len is present and set to zero */
4607 es_now = 1;
4608 }
4609
Willy Tarreau9c218e72019-05-26 10:08:28 +02004610 mbuf = br_tail(h2c->mbuf);
4611 retry:
4612 if (!h2_get_buf(h2c, mbuf)) {
4613 h2c->flags |= H2_CF_MUX_MALLOC;
4614 h2s->flags |= H2_SF_BLK_MROOM;
4615 return 0;
4616 }
4617
Willy Tarreau115e83b2018-12-01 19:17:53 +01004618 chunk_reset(&outbuf);
4619
4620 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004621 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4622 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004623 break;
4624 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004625 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004626 }
4627
4628 if (outbuf.size < 9)
4629 goto full;
4630
4631 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4632 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4633 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4634 outbuf.data = 9;
4635
4636 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004637 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004638 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004639 goto realign_again;
4640 goto full;
4641 }
4642
4643 /* encode all headers, stop at empty name */
4644 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4645 /* these ones do not exist in H2 and must be dropped. */
4646 if (isteq(list[hdr].n, ist("connection")) ||
4647 isteq(list[hdr].n, ist("proxy-connection")) ||
4648 isteq(list[hdr].n, ist("keep-alive")) ||
4649 isteq(list[hdr].n, ist("upgrade")) ||
4650 isteq(list[hdr].n, ist("transfer-encoding")))
4651 continue;
4652
4653 if (isteq(list[hdr].n, ist("")))
4654 break; // end
4655
4656 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4657 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004658 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004659 goto realign_again;
4660 goto full;
4661 }
4662 }
4663
Christopher Faulet0b465482019-02-19 15:14:23 +01004664 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004665 * FIXME: we should also set it when we know for sure that the
4666 * content-length is zero as well as on 204/304
4667 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004668 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4669 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004670 es_now = 1;
4671
Willy Tarreau927b88b2019-03-04 08:03:25 +01004672 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004673 es_now = 1;
4674
4675 /* update the frame's size */
4676 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4677
4678 if (es_now)
4679 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4680
4681 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004682 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004683
4684 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4685 * 1xx responses, another HEADERS frame is expected.
4686 */
4687 if (h2s->status >= 200 || h2s->status == 101)
4688 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004689
Willy Tarreau115e83b2018-12-01 19:17:53 +01004690 if (es_now) {
4691 h2s->flags |= H2_SF_ES_SENT;
4692 if (h2s->st == H2_SS_OPEN)
4693 h2s->st = H2_SS_HLOC;
4694 else
4695 h2s_close(h2s);
4696 }
4697
4698 /* OK we could properly deliver the response */
4699
4700 /* remove all header blocks including the EOH and compute the
4701 * corresponding size.
4702 *
4703 * FIXME: We should remove everything when es_now is set.
4704 */
4705 ret = 0;
4706 idx = htx_get_head(htx);
4707 blk = htx_get_blk(htx, idx);
4708 while (blk != blk_end) {
4709 ret += htx_get_blksz(blk);
4710 blk = htx_remove_blk(htx, blk);
4711 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004712
Christopher Faulet316934d2019-05-15 14:22:48 +02004713 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4714 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004715 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004716 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004717 end:
4718 return ret;
4719 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004720 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4721 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004722 h2c->flags |= H2_CF_MUX_MFULL;
4723 h2s->flags |= H2_SF_BLK_MROOM;
4724 ret = 0;
4725 goto end;
4726 fail:
4727 /* unparsable HTX messages, too large ones to be produced in the local
4728 * list etc go here (unrecoverable errors).
4729 */
4730 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4731 ret = 0;
4732 goto end;
4733}
4734
Willy Tarreau80739692018-10-05 11:35:57 +02004735/* Try to send a HEADERS frame matching HTX request present in HTX message
4736 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4737 * must check the stream's status to detect any error which might have happened
4738 * subsequently to a successful send. The htx blocks are automatically removed
4739 * from the message. The htx message is assumed to be valid since produced from
4740 * the internal code, hence it contains a start line, an optional series of
4741 * header blocks and an end of header, otherwise an invalid frame could be
4742 * emitted and the resulting htx message could be left in an inconsistent state.
4743 */
4744static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4745{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004746 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004747 struct h2c *h2c = h2s->h2c;
4748 struct htx_blk *blk;
4749 struct htx_blk *blk_end;
4750 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004751 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004752 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004753 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004754 enum htx_blk_type type;
4755 int es_now = 0;
4756 int ret = 0;
4757 int hdr;
4758 int idx;
4759
4760 if (h2c_mux_busy(h2c, h2s)) {
4761 h2s->flags |= H2_SF_BLK_MBUSY;
4762 return 0;
4763 }
4764
Willy Tarreau80739692018-10-05 11:35:57 +02004765 /* determine the first block which must not be deleted, blk_end may
4766 * be NULL if all blocks have to be deleted.
4767 */
4768 idx = htx_get_head(htx);
4769 blk_end = NULL;
4770 while (idx != -1) {
4771 type = htx_get_blk_type(htx_get_blk(htx, idx));
4772 idx = htx_get_next(htx, idx);
4773 if (type == HTX_BLK_EOH) {
4774 if (idx != -1)
4775 blk_end = htx_get_blk(htx, idx);
4776 break;
4777 }
4778 }
4779
4780 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004781 blk = htx_get_head_blk(htx);
4782 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
4783 ALREADY_CHECKED(blk);
4784 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004785 meth = htx_sl_req_meth(sl);
4786 path = htx_sl_req_uri(sl);
4787
4788 /* and the rest of the headers, that we dump starting at header 0 */
4789 hdr = 0;
4790
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004791 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004792 while ((idx = htx_get_next(htx, idx)) != -1) {
4793 blk = htx_get_blk(htx, idx);
4794 type = htx_get_blk_type(blk);
4795
4796 if (type == HTX_BLK_UNUSED)
4797 continue;
4798
4799 if (type != HTX_BLK_HDR)
4800 break;
4801
4802 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4803 goto fail;
4804
4805 list[hdr].n = htx_get_blk_name(htx, blk);
4806 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004807 hdr++;
4808 }
4809
4810 /* marker for end of headers */
4811 list[hdr].n = ist("");
4812
Willy Tarreau9c218e72019-05-26 10:08:28 +02004813 mbuf = br_tail(h2c->mbuf);
4814 retry:
4815 if (!h2_get_buf(h2c, mbuf)) {
4816 h2c->flags |= H2_CF_MUX_MALLOC;
4817 h2s->flags |= H2_SF_BLK_MROOM;
4818 return 0;
4819 }
4820
Willy Tarreau80739692018-10-05 11:35:57 +02004821 chunk_reset(&outbuf);
4822
4823 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004824 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4825 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004826 break;
4827 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004828 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004829 }
4830
4831 if (outbuf.size < 9)
4832 goto full;
4833
4834 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4835 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4836 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4837 outbuf.data = 9;
4838
4839 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004840 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004841 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004842 goto realign_again;
4843 goto full;
4844 }
4845
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004846 /* RFC7540 #8.3: the CONNECT method must have :
4847 * - :authority set to the URI part (host:port)
4848 * - :method set to CONNECT
4849 * - :scheme and :path omitted
4850 */
4851 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4852 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004853 struct ist scheme;
4854
4855 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4856 scheme = ist("http");
4857 else
4858 scheme = ist("https");
4859
4860 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004861 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004862 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004863 goto realign_again;
4864 goto full;
4865 }
Willy Tarreau80739692018-10-05 11:35:57 +02004866
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004867 /* encode the path, which necessarily is the second one */
4868 if (!hpack_encode_path(&outbuf, path)) {
4869 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004870 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004871 goto realign_again;
4872 goto full;
4873 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004874
4875 /* look for the Host header and place it in :authority */
4876 auth = ist2(NULL, 0);
4877 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4878 if (isteq(list[hdr].n, ist("")))
4879 break; // end
4880
4881 if (isteq(list[hdr].n, ist("host"))) {
4882 auth = list[hdr].v;
4883 break;
4884 }
4885 }
4886 }
4887 else {
4888 /* for CONNECT, :authority is taken from the path */
4889 auth = path;
4890 }
4891
4892 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4893 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004894 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004895 goto realign_again;
4896 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004897 }
4898
4899 /* encode all headers, stop at empty name */
4900 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4901 /* these ones do not exist in H2 and must be dropped. */
4902 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004903 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004904 isteq(list[hdr].n, ist("proxy-connection")) ||
4905 isteq(list[hdr].n, ist("keep-alive")) ||
4906 isteq(list[hdr].n, ist("upgrade")) ||
4907 isteq(list[hdr].n, ist("transfer-encoding")))
4908 continue;
4909
4910 if (isteq(list[hdr].n, ist("")))
4911 break; // end
4912
4913 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4914 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004915 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004916 goto realign_again;
4917 goto full;
4918 }
4919 }
4920
4921 /* we may need to add END_STREAM if we have no body :
4922 * - request already closed, or :
4923 * - no transfer-encoding, and :
4924 * - no content-length or content-length:0
4925 * Fixme: this doesn't take into account CONNECT requests.
4926 */
4927 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4928 es_now = 1;
4929
4930 if (sl->flags & HTX_SL_F_BODYLESS)
4931 es_now = 1;
4932
Willy Tarreau927b88b2019-03-04 08:03:25 +01004933 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004934 es_now = 1;
4935
4936 /* update the frame's size */
4937 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4938
4939 if (es_now)
4940 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4941
4942 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004943 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004944 h2s->flags |= H2_SF_HEADERS_SENT;
4945 h2s->st = H2_SS_OPEN;
4946
Willy Tarreau80739692018-10-05 11:35:57 +02004947 if (es_now) {
4948 // trim any possibly pending data (eg: inconsistent content-length)
4949 h2s->flags |= H2_SF_ES_SENT;
4950 h2s->st = H2_SS_HLOC;
4951 }
4952
4953 /* remove all header blocks including the EOH and compute the
4954 * corresponding size.
4955 *
4956 * FIXME: We should remove everything when es_now is set.
4957 */
4958 ret = 0;
4959 idx = htx_get_head(htx);
4960 blk = htx_get_blk(htx, idx);
4961 while (blk != blk_end) {
4962 ret += htx_get_blksz(blk);
4963 blk = htx_remove_blk(htx, blk);
4964 }
4965
Christopher Faulet316934d2019-05-15 14:22:48 +02004966 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4967 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004968 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004969 }
Willy Tarreau80739692018-10-05 11:35:57 +02004970
4971 end:
4972 return ret;
4973 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004974 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4975 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004976 h2c->flags |= H2_CF_MUX_MFULL;
4977 h2s->flags |= H2_SF_BLK_MROOM;
4978 ret = 0;
4979 goto end;
4980 fail:
4981 /* unparsable HTX messages, too large ones to be produced in the local
4982 * list etc go here (unrecoverable errors).
4983 */
4984 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4985 ret = 0;
4986 goto end;
4987}
4988
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004989/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004990 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4991 * caller must check the stream's status to detect any error which might have
4992 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02004993 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004994 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004995static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004996{
4997 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004998 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004999 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005000 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005001 size_t total = 0;
5002 int es_now = 0;
5003 int bsize; /* htx block size */
5004 int fsize; /* h2 frame size */
5005 struct htx_blk *blk;
5006 enum htx_blk_type type;
5007 int idx;
5008
5009 if (h2c_mux_busy(h2c, h2s)) {
5010 h2s->flags |= H2_SF_BLK_MBUSY;
5011 goto end;
5012 }
5013
Willy Tarreau98de12a2018-12-12 07:03:00 +01005014 htx = htx_from_buf(buf);
5015
Christopher Faulet54b5e212019-06-04 10:08:28 +02005016 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
5017 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
5018 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005019 */
5020
5021 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01005022 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005023 goto end;
5024
5025 idx = htx_get_head(htx);
5026 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02005027 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005028 bsize = htx_get_blksz(blk);
5029 fsize = bsize;
5030
Christopher Faulet54b5e212019-06-04 10:08:28 +02005031 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01005032 if (h2s->flags & H2_SF_ES_SENT) {
5033 /* ES already sent */
5034 htx_remove_blk(htx, blk);
5035 total++; // EOM counts as one byte
5036 count--;
5037 goto end;
5038 }
5039 }
5040 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005041 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005042
Willy Tarreau9c218e72019-05-26 10:08:28 +02005043 mbuf = br_tail(h2c->mbuf);
5044 retry:
5045 if (!h2_get_buf(h2c, mbuf)) {
5046 h2c->flags |= H2_CF_MUX_MALLOC;
5047 h2s->flags |= H2_SF_BLK_MROOM;
5048 goto end;
5049 }
5050
Willy Tarreau98de12a2018-12-12 07:03:00 +01005051 /* Perform some optimizations to reduce the number of buffer copies.
5052 * First, if the mux's buffer is empty and the htx area contains
5053 * exactly one data block of the same size as the requested count, and
5054 * this count fits within the frame size, the stream's window size, and
5055 * the connection's window size, then it's possible to simply swap the
5056 * caller's buffer with the mux's output buffer and adjust offsets and
5057 * length to match the entire DATA HTX block in the middle. In this
5058 * case we perform a true zero-copy operation from end-to-end. This is
5059 * the situation that happens all the time with large files. Second, if
5060 * this is not possible, but the mux's output buffer is empty, we still
5061 * have an opportunity to avoid the copy to the intermediary buffer, by
5062 * making the intermediary buffer's area point to the output buffer's
5063 * area. In this case we want to skip the HTX header to make sure that
5064 * copies remain aligned and that this operation remains possible all
5065 * the time. This goes for headers, data blocks and any data extracted
5066 * from the HTX blocks.
5067 */
5068 if (unlikely(fsize == count &&
5069 htx->used == 1 && type == HTX_BLK_DATA &&
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005070 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005071 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005072
Willy Tarreaubcc45952019-05-26 10:05:50 +02005073 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005074 /* Too bad there are data left there. We're willing to memcpy/memmove
5075 * up to 1/4 of the buffer, which means that it's OK to copy a large
5076 * frame into a buffer containing few data if it needs to be realigned,
5077 * and that it's also OK to copy few data without realigning. Otherwise
5078 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01005079 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005080 if (fsize + 9 <= b_room(mbuf) &&
5081 (b_data(mbuf) <= b_size(mbuf) / 4 ||
5082 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01005083 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005084
Willy Tarreau9c218e72019-05-26 10:08:28 +02005085 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5086 goto retry;
5087
Willy Tarreau98de12a2018-12-12 07:03:00 +01005088 h2c->flags |= H2_CF_MUX_MFULL;
5089 h2s->flags |= H2_SF_BLK_MROOM;
5090 goto end;
5091 }
5092
5093 /* map an H2 frame to the HTX block so that we can put the
5094 * frame header there.
5095 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005096 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
5097 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005098
5099 /* prepend an H2 DATA frame header just before the DATA block */
5100 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5101 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5102 h2_set_frame_size(outbuf.area, fsize);
5103
5104 /* update windows */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005105 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005106 h2c->mws -= fsize;
5107
5108 /* and exchange with our old area */
5109 buf->area = old_area;
5110 buf->data = buf->head = 0;
5111 total += fsize;
5112 goto end;
5113 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005114
Willy Tarreau98de12a2018-12-12 07:03:00 +01005115 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005116 /* for DATA and EOM we'll have to emit a frame, even if empty */
5117
5118 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005119 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5120 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005121 break;
5122 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005123 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005124 }
5125
5126 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005127 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5128 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005129 h2c->flags |= H2_CF_MUX_MFULL;
5130 h2s->flags |= H2_SF_BLK_MROOM;
5131 goto end;
5132 }
5133
5134 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5135 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5136 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5137 outbuf.data = 9;
5138
5139 /* we have in <fsize> the exact number of bytes we need to copy from
5140 * the HTX buffer. We need to check this against the connection's and
5141 * the stream's send windows, and to ensure that this fits in the max
5142 * frame size and in the buffer's available space minus 9 bytes (for
5143 * the frame header). The connection's flow control is applied last so
5144 * that we can use a separate list of streams which are immediately
5145 * unblocked on window opening. Note: we don't implement padding.
5146 */
5147
5148 /* EOM is presented with bsize==1 but would lead to the emission of an
5149 * empty frame, thus we force it to zero here.
5150 */
5151 if (type == HTX_BLK_EOM)
5152 bsize = fsize = 0;
5153
5154 if (!fsize)
5155 goto send_empty;
5156
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005157 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005158 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005159 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005160 LIST_DEL_INIT(&h2s->list);
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005161 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005162 goto end;
5163 }
5164
Willy Tarreauee573762018-12-04 15:25:57 +01005165 if (fsize > count)
5166 fsize = count;
5167
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005168 if (fsize > h2s_mws(h2s))
5169 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005170
5171 if (h2c->mfs && fsize > h2c->mfs)
5172 fsize = h2c->mfs; // >0
5173
5174 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005175 /* It doesn't fit at once. If it at least fits once split and
5176 * the amount of data to move is low, let's defragment the
5177 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005178 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005179 if (b_space_wraps(mbuf) &&
5180 (fsize + 9 <= b_room(mbuf)) &&
5181 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005182 goto realign_again;
5183 fsize = outbuf.size - 9;
5184
5185 if (fsize <= 0) {
5186 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005187 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5188 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005189 h2c->flags |= H2_CF_MUX_MFULL;
5190 h2s->flags |= H2_SF_BLK_MROOM;
5191 goto end;
5192 }
5193 }
5194
5195 if (h2c->mws <= 0) {
5196 h2s->flags |= H2_SF_BLK_MFCTL;
5197 goto end;
5198 }
5199
5200 if (fsize > h2c->mws)
5201 fsize = h2c->mws;
5202
5203 /* now let's copy this this into the output buffer */
5204 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005205 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005206 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005207 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005208
5209 send_empty:
5210 /* update the frame's size */
5211 h2_set_frame_size(outbuf.area, fsize);
5212
5213 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5214 * meeting EOM. We should optimize this later.
5215 */
5216 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005217 total++; // EOM counts as one byte
5218 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005219 es_now = 1;
5220 }
5221
5222 if (es_now)
5223 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5224
5225 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005226 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005227
5228 /* consume incoming HTX block, including EOM */
5229 total += fsize;
5230 if (fsize == bsize) {
5231 htx_remove_blk(htx, blk);
5232 if (fsize)
5233 goto new_frame;
5234 } else {
5235 /* we've truncated this block */
5236 htx_cut_data_blk(htx, blk, fsize);
5237 }
5238
5239 if (es_now) {
5240 if (h2s->st == H2_SS_OPEN)
5241 h2s->st = H2_SS_HLOC;
5242 else
5243 h2s_close(h2s);
5244
5245 h2s->flags |= H2_SF_ES_SENT;
5246 }
5247
5248 end:
5249 return total;
5250}
5251
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005252/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5253 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5254 * processed. The caller must check the stream's status to detect any error
5255 * which might have happened subsequently to a successful send. The htx blocks
5256 * are automatically removed from the message. The htx message is assumed to be
5257 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005258 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005259 * as a single frame. The ES flag is always set.
5260 */
5261static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5262{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005263 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005264 struct h2c *h2c = h2s->h2c;
5265 struct htx_blk *blk;
5266 struct htx_blk *blk_end;
5267 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005268 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005269 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005270 int ret = 0;
5271 int hdr;
5272 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005273
5274 if (h2c_mux_busy(h2c, h2s)) {
5275 h2s->flags |= H2_SF_BLK_MBUSY;
5276 goto end;
5277 }
5278
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005279 /* determine the first block which must not be deleted, blk_end may
5280 * be NULL if all blocks have to be deleted. also get trailers.
5281 */
5282 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005283 blk_end = NULL;
5284
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005285 hdr = 0;
5286 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005287 blk = htx_get_blk(htx, idx);
5288 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005289 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005290 if (type == HTX_BLK_UNUSED)
5291 continue;
5292
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005293 if (type == HTX_BLK_EOT) {
5294 if (idx != -1)
5295 blk_end = blk;
5296 break;
5297 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005298 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005299 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005300
5301 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5302 goto fail;
5303
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005304 list[hdr].n = htx_get_blk_name(htx, blk);
5305 list[hdr].v = htx_get_blk_value(htx, blk);
5306 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005307 }
5308
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005309 /* marker for end of trailers */
5310 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005311
Willy Tarreau9c218e72019-05-26 10:08:28 +02005312 mbuf = br_tail(h2c->mbuf);
5313 retry:
5314 if (!h2_get_buf(h2c, mbuf)) {
5315 h2c->flags |= H2_CF_MUX_MALLOC;
5316 h2s->flags |= H2_SF_BLK_MROOM;
5317 goto end;
5318 }
5319
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005320 chunk_reset(&outbuf);
5321
5322 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005323 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5324 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005325 break;
5326 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005327 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005328 }
5329
5330 if (outbuf.size < 9)
5331 goto full;
5332
5333 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5334 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5335 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5336 outbuf.data = 9;
5337
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005338 /* encode all headers */
5339 for (idx = 0; idx < hdr; idx++) {
5340 /* these ones do not exist in H2 or must not appear in
5341 * trailers and must be dropped.
5342 */
5343 if (isteq(list[idx].n, ist("host")) ||
5344 isteq(list[idx].n, ist("content-length")) ||
5345 isteq(list[idx].n, ist("connection")) ||
5346 isteq(list[idx].n, ist("proxy-connection")) ||
5347 isteq(list[idx].n, ist("keep-alive")) ||
5348 isteq(list[idx].n, ist("upgrade")) ||
5349 isteq(list[idx].n, ist("te")) ||
5350 isteq(list[idx].n, ist("transfer-encoding")))
5351 continue;
5352
5353 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5354 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005355 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005356 goto realign_again;
5357 goto full;
5358 }
5359 }
5360
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005361 if (outbuf.data == 9) {
5362 /* here we have a problem, we have nothing to emit (either we
5363 * received an empty trailers block followed or we removed its
5364 * contents above). Because of this we can't send a HEADERS
5365 * frame, so we have to cheat and instead send an empty DATA
5366 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005367 */
5368 outbuf.area[3] = H2_FT_DATA;
5369 outbuf.area[4] = H2_F_DATA_END_STREAM;
5370 }
5371
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005372 /* update the frame's size */
5373 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5374
5375 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005376 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005377 h2s->flags |= H2_SF_ES_SENT;
5378
5379 if (h2s->st == H2_SS_OPEN)
5380 h2s->st = H2_SS_HLOC;
5381 else
5382 h2s_close(h2s);
5383
5384 /* OK we could properly deliver the response */
5385 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005386 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005387 ret = 0;
5388 idx = htx_get_head(htx);
5389 blk = htx_get_blk(htx, idx);
5390 while (blk != blk_end) {
5391 ret += htx_get_blksz(blk);
5392 blk = htx_remove_blk(htx, blk);
5393 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005394
5395 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5396 ret += htx_get_blksz(blk_end);
5397 htx_remove_blk(htx, blk_end);
5398 }
5399
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005400 end:
5401 return ret;
5402 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005403 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5404 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005405 h2c->flags |= H2_CF_MUX_MFULL;
5406 h2s->flags |= H2_SF_BLK_MROOM;
5407 ret = 0;
5408 goto end;
5409 fail:
5410 /* unparsable HTX messages, too large ones to be produced in the local
5411 * list etc go here (unrecoverable errors).
5412 */
5413 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5414 ret = 0;
5415 goto end;
5416}
5417
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005418/* Called from the upper layer, to subscribe to events, such as being able to send.
5419 * The <param> argument here is supposed to be a pointer to a wait_event struct
5420 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5421 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5422 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5423 * returns 0 except for the error above.
5424 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005425static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5426{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005427 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005428 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005429 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005430
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005431 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005432 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005433 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5434 sw->events |= SUB_RETRY_RECV;
5435 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005436 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005437 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005438 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005439 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005440 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5441 sw->events |= SUB_RETRY_SEND;
5442 h2s->send_wait = sw;
5443 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5444 !LIST_ADDED(&h2s->list)) {
5445 if (h2s->flags & H2_SF_BLK_MFCTL)
5446 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5447 else
5448 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005449 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005450 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005451 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005452 if (event_type != 0)
5453 return -1;
5454 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005455}
5456
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005457/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5458 * The <param> argument here is supposed to be a pointer to the same wait_event
5459 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5460 * It always returns zero.
5461 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005462static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5463{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005464 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005465 struct h2s *h2s = cs->ctx;
5466
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005467 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005468 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005469 BUG_ON(h2s->recv_wait != sw);
5470 sw->events &= ~SUB_RETRY_RECV;
5471 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005472 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005473 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005474 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005475 BUG_ON(h2s->send_wait != sw);
5476 LIST_DEL(&h2s->list);
5477 LIST_INIT(&h2s->list);
5478 sw->events &= ~SUB_RETRY_SEND;
5479 /* We were about to send, make sure it does not happen */
5480 if (LIST_ADDED(&h2s->sending_list) &&
5481 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02005482 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02005483 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005484 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005485 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005486 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005487 return 0;
5488}
5489
5490
Olivier Houchard511efea2018-08-16 15:30:32 +02005491/* Called from the upper layer, to receive data */
5492static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5493{
Olivier Houchard638b7992018-08-16 15:41:52 +02005494 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005495 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005496 struct htx *h2s_htx = NULL;
5497 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005498 size_t ret = 0;
5499
5500 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005501 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau86724e22018-12-01 23:19:43 +01005502 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005503 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005504 /* Here htx_to_buf() will set buffer data to 0 because
5505 * the HTX is empty.
5506 */
5507 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005508 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005509 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005510
Christopher Faulet156852b2019-05-16 11:29:13 +02005511 ret = h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005512 buf_htx = htx_from_buf(buf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005513
Christopher Faulet2f6edc82019-05-15 10:07:59 +02005514 /* <buf> is empty and the message is small enough, swap the
5515 * buffers. */
5516 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
5517 htx_to_buf(buf_htx, buf);
5518 htx_to_buf(h2s_htx, &h2s->rxbuf);
5519 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5520 goto end;
5521 }
5522
Christopher Faulet156852b2019-05-16 11:29:13 +02005523 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005524
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005525 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
Willy Tarreau7196dd62019-03-05 10:51:11 +01005526 buf_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005527 if (htx_is_empty(buf_htx))
5528 cs->flags |= CS_FL_EOI;
5529 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005530
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005531 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005532 htx_to_buf(buf_htx, buf);
5533 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet156852b2019-05-16 11:29:13 +02005534 ret -= h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005535 }
5536 else {
5537 ret = b_xfer(buf, &h2s->rxbuf, count);
5538 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005539
Christopher Faulet37070b22019-02-14 15:12:14 +01005540 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005541 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005542 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005543 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005544 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005545 if (h2s->flags & H2_SF_ES_RCVD)
5546 cs->flags |= CS_FL_EOI;
Willy Tarreau76c83822019-06-15 09:55:50 +02005547 if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02005548 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005549 if (cs->flags & CS_FL_ERR_PENDING)
5550 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005551 if (b_size(&h2s->rxbuf)) {
5552 b_free(&h2s->rxbuf);
5553 offer_buffers(NULL, tasks_run_queue);
5554 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005555 }
5556
Willy Tarreau082f5592018-11-25 08:03:32 +01005557 if (ret && h2c->dsi == h2s->id) {
5558 /* demux is blocking on this stream's buffer */
5559 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005560 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005561 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005562
Olivier Houchard511efea2018-08-16 15:30:32 +02005563 return ret;
5564}
5565
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005566/* stops all senders of this connection for example when the mux buffer is full.
5567 * They are moved from the sending_list to either fctl_list or send_list.
5568 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005569static void h2_stop_senders(struct h2c *h2c)
5570{
5571 struct h2s *h2s, *h2s_back;
5572
Olivier Houchardd360ac62019-03-22 17:37:16 +01005573 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5574 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02005575 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005576 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005577 }
5578}
5579
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005580/* Called from the upper layer, to send data from buffer <buf> for no more than
5581 * <count> bytes. Returns the number of bytes effectively sent. Some status
5582 * flags may be updated on the conn_stream.
5583 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005584static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005585{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005586 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005587 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005588 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005589 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005590 struct htx *htx;
5591 struct htx_blk *blk;
5592 enum htx_blk_type btype;
5593 uint32_t bsize;
5594 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005595
Olivier Houchardd360ac62019-03-22 17:37:16 +01005596 /* If we were not just woken because we wanted to send but couldn't,
5597 * and there's somebody else that is waiting to send, do nothing,
5598 * we will subscribe later and be put at the end of the list
5599 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005600 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005601 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5602 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005603 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005604
Olivier Houchard998410a2019-04-15 19:23:37 +02005605 /* We couldn't set it to NULL before, because we needed it in case
5606 * we had to cancel the tasklet
5607 */
5608 h2s->send_wait = NULL;
5609
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005610 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5611 return 0;
5612
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005613 /* htx will be enough to decide if we're using HTX or legacy */
5614 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5615
Willy Tarreau0bad0432018-06-14 16:54:01 +02005616 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005617 h2s->flags |= H2_SF_OUTGOING_DATA;
5618
Willy Tarreau751f2d02018-10-05 09:35:00 +02005619 if (h2s->id == 0) {
5620 int32_t id = h2c_get_next_sid(h2s->h2c);
5621
5622 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005623 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005624 return 0;
5625 }
5626
5627 eb32_delete(&h2s->by_id);
5628 h2s->by_id.key = h2s->id = id;
5629 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005630 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005631 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5632 }
5633
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005634 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005635 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005636 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005637 idx = htx_get_head(htx);
5638 blk = htx_get_blk(htx, idx);
5639 btype = htx_get_blk_type(blk);
5640 bsize = htx_get_blksz(blk);
5641
5642 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005643 case HTX_BLK_REQ_SL:
5644 /* start-line before headers */
5645 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5646 if (ret > 0) {
5647 total += ret;
5648 count -= ret;
5649 if (ret < bsize)
5650 goto done;
5651 }
5652 break;
5653
Willy Tarreau115e83b2018-12-01 19:17:53 +01005654 case HTX_BLK_RES_SL:
5655 /* start-line before headers */
5656 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5657 if (ret > 0) {
5658 total += ret;
5659 count -= ret;
5660 if (ret < bsize)
5661 goto done;
5662 }
5663 break;
5664
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005665 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005666 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005667 /* all these cause the emission of a DATA frame (possibly empty).
5668 * This EOM necessarily is one before trailers, as the EOM following
5669 * trailers would have been consumed by the trailers parser.
5670 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005671 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005672 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005673 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005674 total += ret;
5675 count -= ret;
5676 if (ret < bsize)
5677 goto done;
5678 }
5679 break;
5680
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005681 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005682 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005683 /* This is the first trailers block, all the subsequent ones AND
5684 * the EOM will be swallowed by the parser.
5685 */
5686 ret = h2s_htx_make_trailers(h2s, htx);
5687 if (ret > 0) {
5688 total += ret;
5689 count -= ret;
5690 if (ret < bsize)
5691 goto done;
5692 }
5693 break;
5694
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005695 default:
5696 htx_remove_blk(htx, blk);
5697 total += bsize;
5698 count -= bsize;
5699 break;
5700 }
5701 }
5702 goto done;
5703 }
5704
5705 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005706 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005707 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005708 if (h2s->h2c->flags & H2_CF_IS_BACK)
5709 ret = -1;
5710 else
5711 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005712 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005713 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005714 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005715 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005716 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005717 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005718 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005719
Willy Tarreau5dd17352018-06-14 13:33:30 +02005720 if (unlikely((int)ret <= 0)) {
5721 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005722 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5723 break;
5724 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005725 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005726 total += count;
5727 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005728 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005729 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005730 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005731 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005732 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005733 break;
5734 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005735
5736 total += ret;
5737 count -= ret;
5738
Willy Tarreau2b778482019-05-06 15:00:22 +02005739 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005740 break;
5741
5742 if (h2s->flags & H2_SF_BLK_ANY)
5743 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005744 }
5745
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005746 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005747 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005748 /* trim any possibly pending data after we close (extra CR-LF,
5749 * unprocessed trailers, abnormal extra data, ...)
5750 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005751 total += count;
5752 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005753 }
5754
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005755 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005756 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005757 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005758 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005759 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005760 }
5761
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005762 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005763 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005764 } else {
5765 b_del(buf, total);
5766 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005767
5768 /* The mux is full, cancel the pending tasks */
5769 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5770 (h2s->flags & H2_SF_BLK_MBUSY))
5771 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005772
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005773 /* If we're running HTX, and we read the whole buffer, then pretend
5774 * we read exactly what the caller specified, as with HTX the caller
5775 * will always give the buffer size, instead of the amount of data
5776 * available.
5777 */
5778 if (htx && !b_data(buf))
5779 total = orig_count;
5780
Olivier Houchard7505f942018-08-21 18:10:44 +02005781 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005782 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005783 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02005784
Olivier Houchard7505f942018-08-21 18:10:44 +02005785 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005786 /* If we're waiting for flow control, and we got a shutr on the
5787 * connection, we will never be unlocked, so add an error on
5788 * the conn_stream.
5789 */
5790 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5791 !b_data(&h2s->h2c->dbuf) &&
5792 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5793 if (cs->flags & CS_FL_EOS)
5794 cs->flags |= CS_FL_ERROR;
5795 else
5796 cs->flags |= CS_FL_ERR_PENDING;
5797 }
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005798
5799 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL)) {
5800 /* Ok we managed to send something, leave the send_list if we were still there */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005801 LIST_DEL_INIT(&h2s->list);
5802 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005803 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005804}
5805
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005806/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005807static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005808{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005809 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005810 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005811 struct eb32_node *node;
5812 int fctl_cnt = 0;
5813 int send_cnt = 0;
5814 int tree_cnt = 0;
5815 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005816 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005817
5818 if (!h2c)
5819 return;
5820
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005821 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005822 fctl_cnt++;
5823
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005824 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005825 send_cnt++;
5826
Willy Tarreau3af37712018-12-18 14:34:41 +01005827 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005828 node = eb32_first(&h2c->streams_by_id);
5829 while (node) {
5830 h2s = container_of(node, struct h2s, by_id);
5831 tree_cnt++;
5832 if (!h2s->cs)
5833 orph_cnt++;
5834 node = eb32_next(node);
5835 }
5836
Willy Tarreau60f62682019-05-26 11:32:27 +02005837 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005838 tmbuf = br_tail(h2c->mbuf);
Willy Tarreau987c0632018-12-18 10:32:05 +01005839 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5840 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005841 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5842 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau616ac812018-07-24 14:12:42 +02005843 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5844 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005845 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005846 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5847 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5848 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005849 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5850 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5851 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005852 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5853 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005854
5855 if (h2s) {
5856 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5857 h2s, h2s->id, h2s->flags,
5858 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5859 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5860 h2s->cs);
5861 if (h2s->cs)
5862 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5863 h2s->cs->flags, h2s->cs->data);
5864 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005865}
Willy Tarreau62f52692017-10-08 23:01:42 +02005866
5867/*******************************************************/
5868/* functions below are dedicated to the config parsers */
5869/*******************************************************/
5870
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005871/* config parser for global "tune.h2.header-table-size" */
5872static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5873 struct proxy *defpx, const char *file, int line,
5874 char **err)
5875{
5876 if (too_many_args(1, args, err, NULL))
5877 return -1;
5878
5879 h2_settings_header_table_size = atoi(args[1]);
5880 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5881 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5882 return -1;
5883 }
5884 return 0;
5885}
Willy Tarreau62f52692017-10-08 23:01:42 +02005886
Willy Tarreaue6baec02017-07-27 11:45:11 +02005887/* config parser for global "tune.h2.initial-window-size" */
5888static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5889 struct proxy *defpx, const char *file, int line,
5890 char **err)
5891{
5892 if (too_many_args(1, args, err, NULL))
5893 return -1;
5894
5895 h2_settings_initial_window_size = atoi(args[1]);
5896 if (h2_settings_initial_window_size < 0) {
5897 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5898 return -1;
5899 }
5900 return 0;
5901}
5902
Willy Tarreau5242ef82017-07-27 11:47:28 +02005903/* config parser for global "tune.h2.max-concurrent-streams" */
5904static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5905 struct proxy *defpx, const char *file, int line,
5906 char **err)
5907{
5908 if (too_many_args(1, args, err, NULL))
5909 return -1;
5910
5911 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005912 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005913 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5914 return -1;
5915 }
5916 return 0;
5917}
5918
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005919/* config parser for global "tune.h2.max-frame-size" */
5920static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5921 struct proxy *defpx, const char *file, int line,
5922 char **err)
5923{
5924 if (too_many_args(1, args, err, NULL))
5925 return -1;
5926
5927 h2_settings_max_frame_size = atoi(args[1]);
5928 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5929 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5930 return -1;
5931 }
5932 return 0;
5933}
5934
Willy Tarreau62f52692017-10-08 23:01:42 +02005935
5936/****************************************/
5937/* MUX initialization and instanciation */
5938/***************************************/
5939
5940/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005941static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005942 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005943 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005944 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005945 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005946 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005947 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005948 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005949 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005950 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005951 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005952 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005953 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005954 .shutr = h2_shutr,
5955 .shutw = h2_shutw,
Olivier Houchard198d1292019-10-25 16:19:26 +02005956 .ctl = h2_ctl,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005957 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005958 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005959 .name = "H2",
5960};
5961
Christopher Faulet32f61c02018-04-10 14:33:41 +02005962/* PROTO selection : this mux registers PROTO token "h2" */
5963static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005964 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005965
Willy Tarreau0108d902018-11-25 19:14:37 +01005966INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5967
Christopher Faulet9b579102019-04-11 22:52:25 +02005968
5969/* The mux operations */
5970static const struct mux_ops h2_htx_ops = {
5971 .init = h2_init,
5972 .wake = h2_wake,
5973 .snd_buf = h2_snd_buf,
5974 .rcv_buf = h2_rcv_buf,
5975 .subscribe = h2_subscribe,
5976 .unsubscribe = h2_unsubscribe,
5977 .attach = h2_attach,
5978 .get_first_cs = h2_get_first_cs,
5979 .detach = h2_detach,
5980 .destroy = h2_destroy,
5981 .avail_streams = h2_avail_streams,
5982 .used_streams = h2_used_streams,
5983 .shutr = h2_shutr,
5984 .shutw = h2_shutw,
Olivier Houchard198d1292019-10-25 16:19:26 +02005985 .ctl = h2_ctl,
Christopher Faulet9b579102019-04-11 22:52:25 +02005986 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005987 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005988 .name = "H2",
5989};
5990
Willy Tarreauf8957272018-10-03 10:25:20 +02005991static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005992 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005993
5994INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5995
Willy Tarreau62f52692017-10-08 23:01:42 +02005996/* config keyword parsers */
5997static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005998 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005999 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02006000 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01006001 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02006002 { 0, NULL, NULL }
6003}};
6004
Willy Tarreau0108d902018-11-25 19:14:37 +01006005INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);