blob: eeb80b6422e19e970fec740244ec0f0cc7c6842a [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
Olivier Houchard7a977432019-03-21 15:47:13 +0100306static __inline int
307h2c_is_dead(struct h2c *h2c)
308{
309 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
310 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
311 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
312 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200313 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100314 (conn_xprt_read0_pending(h2c->conn) ||
315 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
316 return 1;
317
318 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100319}
320
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200321/*****************************************************/
322/* functions below are for dynamic buffer management */
323/*****************************************************/
324
Willy Tarreau315d8072017-12-10 22:17:57 +0100325/* indicates whether or not the we may call the h2_recv() function to attempt
326 * to receive data into the buffer and/or demux pending data. The condition is
327 * a bit complex due to some API limits for now. The rules are the following :
328 * - if an error or a shutdown was detected on the connection and the buffer
329 * is empty, we must not attempt to receive
330 * - if the demux buf failed to be allocated, we must not try to receive and
331 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100332 * - if no flag indicates a blocking condition, we may attempt to receive,
333 * regardless of whether the demux buffer is full or not, so that only
334 * de demux part decides whether or not to block. This is needed because
335 * the connection API indeed prevents us from re-enabling receipt that is
336 * already enabled in a polled state, so we must always immediately stop
337 * as soon as the demux can't proceed so as never to hit an end of read
338 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100339 * - otherwise must may not attempt
340 */
341static inline int h2_recv_allowed(const struct h2c *h2c)
342{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200343 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100344 (h2c->st0 >= H2_CS_ERROR ||
345 h2c->conn->flags & CO_FL_ERROR ||
346 conn_xprt_read0_pending(h2c->conn)))
347 return 0;
348
349 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100350 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100351 return 1;
352
353 return 0;
354}
355
Willy Tarreau47b515a2018-12-21 16:09:41 +0100356/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200357static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100358{
359 if (!h2_recv_allowed(h2c))
360 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200361 if ((!consider_buffer || !b_data(&h2c->dbuf))
362 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100363 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200364 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100365}
366
367
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100368/* returns true if the front connection has too many conn_streams attached */
369static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200370{
Willy Tarreaua8754662018-12-23 20:43:58 +0100371 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200372}
373
Willy Tarreau44e973f2018-03-01 17:49:30 +0100374/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
375 * flags are used to figure what buffer was requested. It returns 1 if the
376 * allocation succeeds, in which case the connection is woken up, or 0 if it's
377 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200378 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100379static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200380{
381 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100382 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200383
Willy Tarreau44e973f2018-03-01 17:49:30 +0100384 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200385 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200386 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200387 return 1;
388 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200389
Willy Tarreau51330962019-05-26 09:38:07 +0200390 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100391 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200392
393 if (h2c->flags & H2_CF_DEM_MROOM) {
394 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200395 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200396 }
Willy Tarreau14398122017-09-22 14:26:04 +0200397 return 1;
398 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100399
400 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
401 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200402 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100403 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200404 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100405 return 1;
406 }
407
Willy Tarreau14398122017-09-22 14:26:04 +0200408 return 0;
409}
410
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200411static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200412{
413 struct buffer *buf = NULL;
414
Willy Tarreauc234ae32019-05-13 17:56:11 +0200415 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100416 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
417 h2c->buf_wait.target = h2c;
418 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100419 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100420 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100421 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200422 __conn_xprt_stop_recv(h2c->conn);
423 }
424 return buf;
425}
426
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200427static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200428{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200429 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100430 b_free(bptr);
Willy Tarreau201840a2019-05-29 17:50:48 +0200431 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200432 }
433}
434
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200435static inline void h2_release_mbuf(struct h2c *h2c)
436{
437 struct buffer *buf;
438 unsigned int count = 0;
439
440 while (b_size(buf = br_head_pick(h2c->mbuf))) {
441 b_free(buf);
442 count++;
443 }
444 if (count)
Willy Tarreau201840a2019-05-29 17:50:48 +0200445 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200446}
447
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100448/* returns the number of allocatable outgoing streams for the connection taking
449 * the last_sid and the reserved ones into account.
450 */
451static inline int h2_streams_left(const struct h2c *h2c)
452{
453 int ret;
454
455 /* consider the number of outgoing streams we're allowed to create before
456 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
457 * nb_reserved is the number of streams which don't yet have an ID.
458 */
459 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
460 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
461 if (ret < 0)
462 ret = 0;
463 return ret;
464}
465
Willy Tarreau00f18a32019-01-26 12:19:01 +0100466/* returns the number of streams in use on a connection to figure if it's
467 * idle or not. We check nb_cs and not nb_streams as the caller will want
468 * to know if it was the last one after a detach().
469 */
470static int h2_used_streams(struct connection *conn)
471{
472 struct h2c *h2c = conn->ctx;
473
474 return h2c->nb_cs;
475}
476
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100477/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100478static int h2_avail_streams(struct connection *conn)
479{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100480 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100481 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100482 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100483
Willy Tarreau6afec462019-01-28 06:40:19 +0100484 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
485 * streams on the connection.
486 */
487 if (h2c->last_sid >= 0)
488 return 0;
489
Willy Tarreau86949782019-01-31 10:42:05 +0100490 /* note: may be negative if a SETTINGS frame changes the limit */
491 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100492
493 /* we must also consider the limit imposed by stream IDs */
494 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100495 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100496 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100497 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
498 ret1 = MIN(ret1, ret2);
499 }
500 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100501}
502
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200503
Willy Tarreau62f52692017-10-08 23:01:42 +0200504/*****************************************************************/
505/* functions below are dedicated to the mux setup and management */
506/*****************************************************************/
507
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200508/* Initialize the mux once it's attached. For outgoing connections, the context
509 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200510 * connections from the fact that the context is still NULL (even during mux
511 * upgrades). <input> is always used as Input buffer and may contain data. It is
512 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200513 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200514static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
515 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200516{
517 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100518 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200519
Willy Tarreaubafbe012017-11-24 17:34:44 +0100520 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200521 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200522 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200523
Christopher Faulete9b70722019-04-08 10:46:02 +0200524 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200525 h2c->flags = H2_CF_IS_BACK;
526 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
527 if (tick_isset(prx->timeout.serverfin))
528 h2c->shut_timeout = prx->timeout.serverfin;
529 } else {
530 h2c->flags = H2_CF_NONE;
531 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
532 if (tick_isset(prx->timeout.clientfin))
533 h2c->shut_timeout = prx->timeout.clientfin;
534 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100535
Willy Tarreau0b37d652018-10-03 10:33:02 +0200536 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100537 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100538 if (tick_isset(h2c->timeout)) {
539 t = task_new(tid_bit);
540 if (!t)
541 goto fail;
542
543 h2c->task = t;
544 t->process = h2_timeout_task;
545 t->context = h2c;
546 t->expire = tick_add(now_ms, h2c->timeout);
547 }
Willy Tarreauea392822017-10-31 10:02:25 +0100548
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200549 h2c->wait_event.tasklet = tasklet_new();
550 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200551 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200552 h2c->wait_event.tasklet->process = h2_io_cb;
553 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100554 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200555
Willy Tarreau32218eb2017-09-22 08:07:25 +0200556 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
557 if (!h2c->ddht)
558 goto fail;
559
560 /* Initialise the context. */
561 h2c->st0 = H2_CS_PREFACE;
562 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100563 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200564 h2c->max_id = -1;
565 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100566 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200567 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100568 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200569 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100570 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100571 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200572
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200573 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200574 h2c->dsi = -1;
575 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100576
Willy Tarreau32218eb2017-09-22 08:07:25 +0200577 h2c->last_sid = -1;
578
Willy Tarreau51330962019-05-26 09:38:07 +0200579 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200580 h2c->miw = 65535; /* mux initial window size */
581 h2c->mws = 65535; /* mux window size */
582 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200583 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200584 LIST_INIT(&h2c->send_list);
585 LIST_INIT(&h2c->fctl_list);
Willy Tarreaubda92dd2019-10-02 10:49:59 +0200586 LIST_INIT(&h2c->blocked_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200587 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100588 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200589
Willy Tarreau3f133572017-10-31 19:21:06 +0100590 if (t)
591 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100592
Willy Tarreau01b44822018-10-03 14:26:37 +0200593 if (h2c->flags & H2_CF_IS_BACK) {
594 /* FIXME: this is temporary, for outgoing connections we need
595 * to immediately allocate a stream until the code is modified
596 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100597 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200598 */
599 struct h2s *h2s;
600
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100601 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200602 if (!h2s)
603 goto fail_stream;
604 }
605
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100606 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200607
Willy Tarreau0f383582018-10-03 14:22:21 +0200608 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200609 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200610 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200611 fail_stream:
612 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200613 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200614 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200615 if (h2c->wait_event.tasklet)
616 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100617 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200618 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200619 return -1;
620}
621
Willy Tarreau751f2d02018-10-05 09:35:00 +0200622/* returns the next allocatable outgoing stream ID for the H2 connection, or
623 * -1 if no more is allocatable.
624 */
625static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
626{
627 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100628
629 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200630 id = -1;
631 return id;
632}
633
Willy Tarreau2373acc2017-10-12 17:35:14 +0200634/* returns the stream associated with id <id> or NULL if not found */
635static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
636{
637 struct eb32_node *node;
638
Willy Tarreau751f2d02018-10-05 09:35:00 +0200639 if (id == 0)
640 return (struct h2s *)h2_closed_stream;
641
Willy Tarreau2a856182017-05-16 15:20:39 +0200642 if (id > h2c->max_id)
643 return (struct h2s *)h2_idle_stream;
644
Willy Tarreau2373acc2017-10-12 17:35:14 +0200645 node = eb32_lookup(&h2c->streams_by_id, id);
646 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200647 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200648
649 return container_of(node, struct h2s, by_id);
650}
651
Christopher Faulet73c12072019-04-08 11:23:22 +0200652/* release function. This one should be called to free all resources allocated
653 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200654 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200655static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200656{
Christopher Faulet61840e72019-04-15 09:33:32 +0200657 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200658
Willy Tarreau32218eb2017-09-22 08:07:25 +0200659 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200660 /* The connection must be aattached to this mux to be released */
661 if (h2c->conn && h2c->conn->ctx == h2c)
662 conn = h2c->conn;
663
Willy Tarreau32218eb2017-09-22 08:07:25 +0200664 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200665
Willy Tarreau186e96e2019-05-28 17:21:18 +0200666 if (LIST_ADDED(&h2c->buf_wait.list)) {
667 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
668 LIST_DEL(&h2c->buf_wait.list);
669 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
670 }
Willy Tarreau14398122017-09-22 14:26:04 +0200671
Willy Tarreau44e973f2018-03-01 17:49:30 +0100672 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200673 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100674
Willy Tarreauea392822017-10-31 10:02:25 +0100675 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200676 h2c->task->context = NULL;
677 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100678 h2c->task = NULL;
679 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200680 if (h2c->wait_event.tasklet)
681 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet0e012562019-09-18 11:07:20 +0200682 if (conn && h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100683 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Christopher Faulet0e012562019-09-18 11:07:20 +0200684 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100685
Willy Tarreaubafbe012017-11-24 17:34:44 +0100686 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200687 }
688
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200689 if (conn) {
690 conn->mux = NULL;
691 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200692
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200693 conn_stop_tracking(conn);
694 conn_full_close(conn);
695 if (conn->destroy_cb)
696 conn->destroy_cb(conn);
697 conn_free(conn);
698 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200699}
700
701
Willy Tarreau71681172017-10-23 14:39:06 +0200702/******************************************************/
703/* functions below are for the H2 protocol processing */
704/******************************************************/
705
706/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100707static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200708{
709 return h2s ? h2s->id : 0;
710}
711
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200712/* returns the sum of the stream's own window size and the mux's initial
713 * window, which together form the stream's effective window size.
714 */
715static inline int h2s_mws(const struct h2s *h2s)
716{
717 return h2s->sws + h2s->h2c->miw;
718}
719
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200720/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100721static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200722{
723 if (h2c->msi < 0)
724 return 0;
725
726 if (h2c->msi == h2s_id(h2s))
727 return 0;
728
729 return 1;
730}
731
Willy Tarreau741d6df2017-10-17 08:00:59 +0200732/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100733static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200734{
735 h2c->errcode = err;
736 h2c->st0 = H2_CS_ERROR;
737}
738
Willy Tarreau175cebb2019-01-24 10:02:24 +0100739/* marks an error on the stream. It may also update an already closed stream
740 * (e.g. to report an error after an RST was received).
741 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100742static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200743{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100744 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200745 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100746 if (h2s->st < H2_SS_ERROR)
747 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100748 if (h2s->cs)
749 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200750 }
751}
752
Willy Tarreau7e094452018-12-19 18:08:52 +0100753/* attempt to notify the data layer of recv availability */
754static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
755{
756 struct wait_event *sw;
757
758 if (h2s->recv_wait) {
759 sw = h2s->recv_wait;
760 sw->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200761 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100762 h2s->recv_wait = NULL;
763 }
764}
765
766/* attempt to notify the data layer of send availability */
767static void __maybe_unused h2s_notify_send(struct h2s *h2s)
768{
769 struct wait_event *sw;
770
Willy Tarreauc234ae32019-05-13 17:56:11 +0200771 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100772 sw = h2s->send_wait;
773 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100774 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200775 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100776 }
777}
778
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100779/* alerts the data layer, trying to wake it up by all means, following
780 * this sequence :
781 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
782 * - if its subscribed to send, then it's woken up for send
783 * - if it was subscribed to neither, its ->wake() callback is called
784 * It is safe to call this function with a closed stream which doesn't have a
785 * conn_stream anymore.
786 */
787static void __maybe_unused h2s_alert(struct h2s *h2s)
788{
789 if (h2s->recv_wait || h2s->send_wait) {
790 h2s_notify_recv(h2s);
791 h2s_notify_send(h2s);
792 }
793 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
794 h2s->cs->data_cb->wake(h2s->cs);
795}
796
Willy Tarreaue4820742017-07-27 13:37:23 +0200797/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100798static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200799{
800 uint8_t *out = frame;
801
802 *out = len >> 16;
803 write_n16(out + 1, len);
804}
805
Willy Tarreau54c15062017-10-10 17:10:03 +0200806/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
807 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
808 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200809 * available in the buffer's input prior to calling this function. The buffer
810 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200811 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100812static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200813 const struct buffer *b, int o)
814{
Willy Tarreau591d4452018-06-15 17:21:00 +0200815 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 +0200816}
817
Willy Tarreau1f094672017-11-20 21:27:45 +0100818static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200819{
Willy Tarreau591d4452018-06-15 17:21:00 +0200820 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200821}
822
Willy Tarreau1f094672017-11-20 21:27:45 +0100823static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200824{
Willy Tarreau591d4452018-06-15 17:21:00 +0200825 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200826}
827
Willy Tarreau1f094672017-11-20 21:27:45 +0100828static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200829{
Willy Tarreau591d4452018-06-15 17:21:00 +0200830 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200831}
832
833
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100834/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
835 * The algorithm is not obvious. It turns out that H2 headers are neither
836 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
837 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200838 *
839 * b0 b1 b2 b3 b4 b5..b8
840 * +----------+---------+--------+----+----+----------------------+
841 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
842 * +----------+---------+--------+----+----+----------------------+
843 *
844 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
845 * we get the sid properly aligned and ordered, and 16 bits of len properly
846 * ordered as well. The type and flags can be extracted using bit shifts from
847 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200848 * Returns zero if some bytes are missing, otherwise non-zero on success. The
849 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200850 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100851static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200852{
853 uint64_t w;
854
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100855 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200856 return 0;
857
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100858 w = h2_get_n64(b, o + 1);
859 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200860 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
861 h->ff = w >> 32;
862 h->ft = w >> 40;
863 h->len += w >> 48;
864 return 1;
865}
866
867/* skip the next 9 bytes corresponding to the frame header possibly parsed by
868 * h2_peek_frame_hdr() above.
869 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100870static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200871{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200872 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200873}
874
875/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100876static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200877{
878 int ret;
879
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100880 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200881 if (ret > 0)
882 h2_skip_frame_hdr(b);
883 return ret;
884}
885
Willy Tarreau00dd0782018-03-01 16:31:34 +0100886/* marks stream <h2s> as CLOSED and decrement the number of active streams for
887 * its connection if the stream was not yet closed. Please use this exclusively
888 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100889 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100890static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100891{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100892 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100893 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100894 if (!h2s->id)
895 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100896 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +0100897 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
898 h2s_notify_recv(h2s);
899 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100900 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100901 h2s->st = H2_SS_CLOSED;
902}
903
Willy Tarreau71049cc2018-03-28 13:56:39 +0200904/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
905static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100906{
907 h2s_close(h2s);
908 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200909 if (b_size(&h2s->rxbuf)) {
910 b_free(&h2s->rxbuf);
911 offer_buffers(NULL, tasks_run_queue);
912 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200913 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100914 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200915 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100916 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800917 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200918 * reference left would be in the h2c send_list/fctl_list, and if
919 * we're in it, we're getting out anyway
920 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100921 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200922 if (LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau86eded62019-06-14 14:47:49 +0200923 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +0200924 LIST_DEL_INIT(&h2s->sending_list);
925 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200926 tasklet_free(h2s->wait_event.tasklet);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100927 pool_free(pool_head_h2s, h2s);
928}
929
Willy Tarreaua8e49542018-10-03 18:53:55 +0200930/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
931 * stream tree. In case of error, nothing is added and NULL is returned. The
932 * causes of errors can be any failed memory allocation. The caller is
933 * responsible for checking if the connection may support an extra stream
934 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200935 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200936static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200937{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200938 struct h2s *h2s;
939
Willy Tarreaubafbe012017-11-24 17:34:44 +0100940 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200941 if (!h2s)
942 goto out;
943
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200944 h2s->wait_event.tasklet = tasklet_new();
945 if (!h2s->wait_event.tasklet) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200946 pool_free(pool_head_h2s, h2s);
947 goto out;
948 }
949 h2s->send_wait = NULL;
950 h2s->recv_wait = NULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200951 h2s->wait_event.tasklet->process = h2_deferred_shut;
952 h2s->wait_event.tasklet->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100953 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200954 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100955 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200956 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200957 h2s->cs = NULL;
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200958 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200959 h2s->flags = H2_SF_NONE;
960 h2s->errcode = H2_ERR_NO_ERROR;
961 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200962 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100963 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200964 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200965
966 if (h2c->flags & H2_CF_IS_BACK) {
967 h1m_init_req(&h2s->h1m);
968 h2s->h1m.err_pos = -1; // don't care about errors on the request path
969 h2s->h1m.flags |= H1_MF_TOLOWER;
970 } else {
971 h1m_init_res(&h2s->h1m);
972 h2s->h1m.err_pos = -1; // don't care about errors on the response path
973 h2s->h1m.flags |= H1_MF_TOLOWER;
974 }
975
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200976 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200977 if (id > 0)
978 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100979 else
980 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200981
982 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100983 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100984 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200985
986 return h2s;
987
988 out_free_h2s:
989 pool_free(pool_head_h2s, h2s);
990 out:
991 return NULL;
992}
993
994/* creates a new stream <id> on the h2c connection and returns it, or NULL in
995 * case of memory allocation error.
996 */
997static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
998{
999 struct session *sess = h2c->conn->owner;
1000 struct conn_stream *cs;
1001 struct h2s *h2s;
1002
1003 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
1004 goto out;
1005
1006 h2s = h2s_new(h2c, id);
1007 if (!h2s)
1008 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001009
1010 cs = cs_new(h2c->conn);
1011 if (!cs)
1012 goto out_close;
1013
Olivier Houchard746fb772018-12-15 19:42:00 +01001014 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001015 h2s->cs = cs;
1016 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001017 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001018
1019 if (stream_create_from_cs(cs) < 0)
1020 goto out_free_cs;
1021
Willy Tarreau590a0512018-09-05 11:56:48 +02001022 /* We want the accept date presented to the next stream to be the one
1023 * we have now, the handshake time to be null (since the next stream
1024 * is not delayed by a handshake), and the idle time to count since
1025 * right now.
1026 */
1027 sess->accept_date = date;
1028 sess->tv_accept = now;
1029 sess->t_handshake = 0;
1030
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001031 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001032 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001033 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001034 return h2s;
1035
1036 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001037 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001038 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001039 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001040 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001041 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001042 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001043 sess_log(sess);
1044 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001045}
1046
Willy Tarreau751f2d02018-10-05 09:35:00 +02001047/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1048 * and returns it, or NULL in case of memory allocation error or if the highest
1049 * possible stream ID was reached.
1050 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001051static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001052{
1053 struct h2s *h2s = NULL;
1054
Willy Tarreau86949782019-01-31 10:42:05 +01001055 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001056 goto out;
1057
Willy Tarreaua80dca82019-01-24 17:08:28 +01001058 if (h2_streams_left(h2c) < 1)
1059 goto out;
1060
Willy Tarreau751f2d02018-10-05 09:35:00 +02001061 /* Defer choosing the ID until we send the first message to create the stream */
1062 h2s = h2s_new(h2c, 0);
1063 if (!h2s)
1064 goto out;
1065
1066 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001067 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001068 cs->ctx = h2s;
1069 h2c->nb_cs++;
1070
Willy Tarreau751f2d02018-10-05 09:35:00 +02001071 out:
1072 return h2s;
1073}
1074
Willy Tarreaube5b7152017-09-25 16:25:39 +02001075/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1076 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1077 * the various settings codes.
1078 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001079static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001080{
1081 struct buffer *res;
1082 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001083 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001084 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001085 int ret;
1086
1087 if (h2c_mux_busy(h2c, NULL)) {
1088 h2c->flags |= H2_CF_DEM_MBUSY;
1089 return 0;
1090 }
1091
Willy Tarreaube5b7152017-09-25 16:25:39 +02001092 chunk_init(&buf, buf_data, sizeof(buf_data));
1093 chunk_memcpy(&buf,
1094 "\x00\x00\x00" /* length : 0 for now */
1095 "\x04\x00" /* type : 4 (settings), flags : 0 */
1096 "\x00\x00\x00\x00", /* stream ID : 0 */
1097 9);
1098
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001099 if (h2c->flags & H2_CF_IS_BACK) {
1100 /* send settings_enable_push=0 */
1101 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1102 }
1103
Willy Tarreaube5b7152017-09-25 16:25:39 +02001104 if (h2_settings_header_table_size != 4096) {
1105 char str[6] = "\x00\x01"; /* header_table_size */
1106
1107 write_n32(str + 2, h2_settings_header_table_size);
1108 chunk_memcat(&buf, str, 6);
1109 }
1110
1111 if (h2_settings_initial_window_size != 65535) {
1112 char str[6] = "\x00\x04"; /* initial_window_size */
1113
1114 write_n32(str + 2, h2_settings_initial_window_size);
1115 chunk_memcat(&buf, str, 6);
1116 }
1117
1118 if (h2_settings_max_concurrent_streams != 0) {
1119 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1120
1121 /* Note: 0 means "unlimited" for haproxy's config but not for
1122 * the protocol, so never send this value!
1123 */
1124 write_n32(str + 2, h2_settings_max_concurrent_streams);
1125 chunk_memcat(&buf, str, 6);
1126 }
1127
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001128 mfs = h2_settings_max_frame_size;
1129 if (mfs > global.tune.bufsize)
1130 mfs = global.tune.bufsize;
1131
1132 if (!mfs)
1133 mfs = global.tune.bufsize;
1134
1135 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001136 char str[6] = "\x00\x05"; /* max_frame_size */
1137
1138 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1139 * match bufsize - rewrite size, but at the moment it seems
1140 * that clients don't take care of it.
1141 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001142 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001143 chunk_memcat(&buf, str, 6);
1144 }
1145
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001146 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001147
1148 res = br_tail(h2c->mbuf);
1149 retry:
1150 if (!h2_get_buf(h2c, res)) {
1151 h2c->flags |= H2_CF_MUX_MALLOC;
1152 h2c->flags |= H2_CF_DEM_MROOM;
1153 return 0;
1154 }
1155
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001156 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001157 if (unlikely(ret <= 0)) {
1158 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001159 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1160 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001161 h2c->flags |= H2_CF_MUX_MFULL;
1162 h2c->flags |= H2_CF_DEM_MROOM;
1163 return 0;
1164 }
1165 else {
1166 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1167 return 0;
1168 }
1169 }
1170 return ret;
1171}
1172
Willy Tarreau52eed752017-09-22 15:05:09 +02001173/* Try to receive a connection preface, then upon success try to send our
1174 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1175 * missing data. It may return an error in h2c.
1176 */
1177static int h2c_frt_recv_preface(struct h2c *h2c)
1178{
1179 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001180 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001181
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001182 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001183
1184 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001185 if (ret1 < 0)
1186 sess_log(h2c->conn->owner);
1187
Willy Tarreau52eed752017-09-22 15:05:09 +02001188 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1189 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1190 return 0;
1191 }
1192
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001193 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001194 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001195 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001196
Willy Tarreaube5b7152017-09-25 16:25:39 +02001197 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001198}
1199
Willy Tarreau01b44822018-10-03 14:26:37 +02001200/* Try to send a connection preface, then upon success try to send our
1201 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1202 * missing data. It may return an error in h2c.
1203 */
1204static int h2c_bck_send_preface(struct h2c *h2c)
1205{
1206 struct buffer *res;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001207 int ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001208
1209 if (h2c_mux_busy(h2c, NULL)) {
1210 h2c->flags |= H2_CF_DEM_MBUSY;
1211 return 0;
1212 }
1213
Willy Tarreaubcc45952019-05-26 10:05:50 +02001214 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001215 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001216 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001217 h2c->flags |= H2_CF_MUX_MALLOC;
1218 h2c->flags |= H2_CF_DEM_MROOM;
1219 return 0;
1220 }
1221
1222 if (!b_data(res)) {
1223 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001224 ret = b_istput(res, ist(H2_CONN_PREFACE));
1225 if (unlikely(ret <= 0)) {
1226 if (!ret) {
1227 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1228 goto retry;
1229 h2c->flags |= H2_CF_MUX_MFULL;
1230 h2c->flags |= H2_CF_DEM_MROOM;
1231 return 0;
1232 }
1233 else {
1234 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1235 return 0;
1236 }
1237 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001238 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001239 return h2c_send_settings(h2c);
1240}
1241
Willy Tarreau081d4722017-05-16 21:51:05 +02001242/* try to send a GOAWAY frame on the connection to report an error or a graceful
1243 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1244 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1245 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1246 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1247 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1248 * on unrecoverable failure. It will not attempt to send one again in this last
1249 * case so that it is safe to use h2c_error() to report such errors.
1250 */
1251static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1252{
1253 struct buffer *res;
1254 char str[17];
1255 int ret;
1256
1257 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1258 return 1; // claim that it worked
1259
1260 if (h2c_mux_busy(h2c, h2s)) {
1261 if (h2s)
1262 h2s->flags |= H2_SF_BLK_MBUSY;
1263 else
1264 h2c->flags |= H2_CF_DEM_MBUSY;
1265 return 0;
1266 }
1267
Willy Tarreau9c218e72019-05-26 10:08:28 +02001268 /* len: 8, type: 7, flags: none, sid: 0 */
1269 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1270
1271 if (h2c->last_sid < 0)
1272 h2c->last_sid = h2c->max_id;
1273
1274 write_n32(str + 9, h2c->last_sid);
1275 write_n32(str + 13, h2c->errcode);
1276
Willy Tarreaubcc45952019-05-26 10:05:50 +02001277 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001278 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001279 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001280 h2c->flags |= H2_CF_MUX_MALLOC;
1281 if (h2s)
1282 h2s->flags |= H2_SF_BLK_MROOM;
1283 else
1284 h2c->flags |= H2_CF_DEM_MROOM;
1285 return 0;
1286 }
1287
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001288 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001289 if (unlikely(ret <= 0)) {
1290 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001291 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1292 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001293 h2c->flags |= H2_CF_MUX_MFULL;
1294 if (h2s)
1295 h2s->flags |= H2_SF_BLK_MROOM;
1296 else
1297 h2c->flags |= H2_CF_DEM_MROOM;
1298 return 0;
1299 }
1300 else {
1301 /* we cannot report this error using GOAWAY, so we mark
1302 * it and claim a success.
1303 */
1304 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1305 h2c->flags |= H2_CF_GOAWAY_FAILED;
1306 return 1;
1307 }
1308 }
1309 h2c->flags |= H2_CF_GOAWAY_SENT;
1310 return ret;
1311}
1312
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001313/* Try to send an RST_STREAM frame on the connection for the indicated stream
1314 * during mux operations. This stream must be valid and cannot be closed
1315 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1316 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1317 * not yet.
1318 *
1319 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1320 * to write the message, it subscribes the stream to future notifications.
1321 */
1322static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1323{
1324 struct buffer *res;
1325 char str[13];
1326 int ret;
1327
1328 if (!h2s || h2s->st == H2_SS_CLOSED)
1329 return 1;
1330
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001331 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1332 * RST_STREAM in response to a RST_STREAM frame.
1333 */
Willy Tarreaubf9a20b2019-08-06 10:01:40 +02001334 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001335 ret = 1;
1336 goto ignore;
1337 }
1338
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001339 if (h2c_mux_busy(h2c, h2s)) {
1340 h2s->flags |= H2_SF_BLK_MBUSY;
1341 return 0;
1342 }
1343
Willy Tarreau9c218e72019-05-26 10:08:28 +02001344 /* len: 4, type: 3, flags: none */
1345 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1346 write_n32(str + 5, h2s->id);
1347 write_n32(str + 9, h2s->errcode);
1348
Willy Tarreaubcc45952019-05-26 10:05:50 +02001349 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001350 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001351 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001352 h2c->flags |= H2_CF_MUX_MALLOC;
1353 h2s->flags |= H2_SF_BLK_MROOM;
1354 return 0;
1355 }
1356
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001357 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001358 if (unlikely(ret <= 0)) {
1359 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001360 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1361 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001362 h2c->flags |= H2_CF_MUX_MFULL;
1363 h2s->flags |= H2_SF_BLK_MROOM;
1364 return 0;
1365 }
1366 else {
1367 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1368 return 0;
1369 }
1370 }
1371
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001372 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001373 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001374 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001375 return ret;
1376}
1377
1378/* Try to send an RST_STREAM frame on the connection for the stream being
1379 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001380 * error code, even if the stream is one of the dummy ones, and will update
1381 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001382 *
1383 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1384 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001385 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001386 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001387 */
1388static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1389{
1390 struct buffer *res;
1391 char str[13];
1392 int ret;
1393
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001394 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1395 * RST_STREAM in response to a RST_STREAM frame.
1396 */
1397 if (h2c->dft == H2_FT_RST_STREAM) {
1398 ret = 1;
1399 goto ignore;
1400 }
1401
Willy Tarreau27a84c92017-10-17 08:10:17 +02001402 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001403 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001404 return 0;
1405 }
1406
Willy Tarreau9c218e72019-05-26 10:08:28 +02001407 /* len: 4, type: 3, flags: none */
1408 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1409
1410 write_n32(str + 5, h2c->dsi);
1411 write_n32(str + 9, h2s->errcode);
1412
Willy Tarreaubcc45952019-05-26 10:05:50 +02001413 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001414 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001415 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001416 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001417 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001418 return 0;
1419 }
1420
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001421 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001422 if (unlikely(ret <= 0)) {
1423 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001424 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1425 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001426 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001427 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001428 return 0;
1429 }
1430 else {
1431 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1432 return 0;
1433 }
1434 }
1435
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001436 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001437 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001438 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001439 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001440 }
1441
Willy Tarreau27a84c92017-10-17 08:10:17 +02001442 return ret;
1443}
1444
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001445/* try to send an empty DATA frame with the ES flag set to notify about the
1446 * end of stream and match a shutdown(write). If an ES was already sent as
1447 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1448 * on success or zero if nothing was done. In case of lack of room to write the
1449 * message, it subscribes the requesting stream to future notifications.
1450 */
1451static int h2_send_empty_data_es(struct h2s *h2s)
1452{
1453 struct h2c *h2c = h2s->h2c;
1454 struct buffer *res;
1455 char str[9];
1456 int ret;
1457
Willy Tarreau721c9742017-11-07 11:05:42 +01001458 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001459 return 1;
1460
1461 if (h2c_mux_busy(h2c, h2s)) {
1462 h2s->flags |= H2_SF_BLK_MBUSY;
1463 return 0;
1464 }
1465
Willy Tarreau9c218e72019-05-26 10:08:28 +02001466 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1467 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1468 write_n32(str + 5, h2s->id);
1469
Willy Tarreaubcc45952019-05-26 10:05:50 +02001470 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001471 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001472 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001473 h2c->flags |= H2_CF_MUX_MALLOC;
1474 h2s->flags |= H2_SF_BLK_MROOM;
1475 return 0;
1476 }
1477
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001478 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001479 if (likely(ret > 0)) {
1480 h2s->flags |= H2_SF_ES_SENT;
1481 }
1482 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001483 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1484 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001485 h2c->flags |= H2_CF_MUX_MFULL;
1486 h2s->flags |= H2_SF_BLK_MROOM;
1487 return 0;
1488 }
1489 else {
1490 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1491 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001492 }
1493 return ret;
1494}
1495
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001496/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001497 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001498 * is automatically updated accordingly. If the stream is orphaned, it is
1499 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001500 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001501static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001502{
1503 if (!h2s->cs) {
1504 /* this stream was already orphaned */
1505 h2s_destroy(h2s);
1506 return;
1507 }
1508
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001509 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001510 if (h2s->st == H2_SS_OPEN)
1511 h2s->st = H2_SS_HREM;
1512 else if (h2s->st == H2_SS_HLOC)
1513 h2s_close(h2s);
1514 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001515
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001516 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1517 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1518 h2s->cs->flags |= CS_FL_ERR_PENDING;
1519 if (h2s->cs->flags & CS_FL_EOS)
1520 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001521
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001522 if (h2s->st < H2_SS_ERROR)
1523 h2s->st = H2_SS_ERROR;
1524 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001525
1526 h2s_alert(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001527}
1528
1529/* wake the streams attached to the connection, whose id is greater than <last>
1530 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001531 */
Willy Tarreau23482912019-05-07 15:23:14 +02001532static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001533{
1534 struct eb32_node *node;
1535 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001536
Christopher Fauletf02ca002019-03-07 16:21:34 +01001537 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001538 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1539 while (node) {
1540 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001541 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001542 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001543 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001544
Christopher Fauletf02ca002019-03-07 16:21:34 +01001545 /* Wake all streams with unassigned ID (ID == 0) */
1546 node = eb32_lookup(&h2c->streams_by_id, 0);
1547 while (node) {
1548 h2s = container_of(node, struct h2s, by_id);
1549 if (h2s->id > 0)
1550 break;
1551 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001552 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001553 }
1554}
1555
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001556/* Wake up all blocked streams whose window size has become positive after the
1557 * mux's initial window was adjusted. This should be done after having processed
1558 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001559 */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001560static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001561{
1562 struct h2s *h2s;
1563 struct eb32_node *node;
1564
Willy Tarreau3421aba2017-07-27 15:41:03 +02001565 node = eb32_first(&h2c->streams_by_id);
1566 while (node) {
1567 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001568 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001569 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreaubda92dd2019-10-02 10:49:59 +02001570 LIST_DEL_INIT(&h2s->list);
1571 if (h2s->send_wait)
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001572 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001573 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001574 node = eb32_next(node);
1575 }
1576}
1577
1578/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1579 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001580 * return an error in h2c. The caller must have already verified frame length
1581 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001582 */
1583static int h2c_handle_settings(struct h2c *h2c)
1584{
1585 unsigned int offset;
1586 int error;
1587
1588 if (h2c->dff & H2_F_SETTINGS_ACK) {
1589 if (h2c->dfl) {
1590 error = H2_ERR_FRAME_SIZE_ERROR;
1591 goto fail;
1592 }
1593 return 1;
1594 }
1595
Willy Tarreau3421aba2017-07-27 15:41:03 +02001596 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001597 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001598 return 0;
1599
1600 /* parse the frame */
1601 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001602 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1603 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001604
1605 switch (type) {
1606 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1607 /* we need to update all existing streams with the
1608 * difference from the previous iws.
1609 */
1610 if (arg < 0) { // RFC7540#6.5.2
1611 error = H2_ERR_FLOW_CONTROL_ERROR;
1612 goto fail;
1613 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001614 h2c->miw = arg;
1615 break;
1616 case H2_SETTINGS_MAX_FRAME_SIZE:
1617 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1618 error = H2_ERR_PROTOCOL_ERROR;
1619 goto fail;
1620 }
1621 h2c->mfs = arg;
1622 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001623 case H2_SETTINGS_ENABLE_PUSH:
1624 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1625 error = H2_ERR_PROTOCOL_ERROR;
1626 goto fail;
1627 }
1628 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001629 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1630 if (h2c->flags & H2_CF_IS_BACK) {
1631 /* the limit is only for the backend; for the frontend it is our limit */
1632 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1633 arg = h2_settings_max_concurrent_streams;
1634 h2c->streams_limit = arg;
1635 }
1636 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001637 }
1638 }
1639
1640 /* need to ACK this frame now */
1641 h2c->st0 = H2_CS_FRAME_A;
1642 return 1;
1643 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001644 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001645 h2c_error(h2c, error);
1646 return 0;
1647}
1648
1649/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1650 * success or one of the h2_status values.
1651 */
1652static int h2c_ack_settings(struct h2c *h2c)
1653{
1654 struct buffer *res;
1655 char str[9];
1656 int ret = -1;
1657
1658 if (h2c_mux_busy(h2c, NULL)) {
1659 h2c->flags |= H2_CF_DEM_MBUSY;
1660 return 0;
1661 }
1662
Willy Tarreau9c218e72019-05-26 10:08:28 +02001663 memcpy(str,
1664 "\x00\x00\x00" /* length : 0 (no data) */
1665 "\x04" "\x01" /* type : 4, flags : ACK */
1666 "\x00\x00\x00\x00" /* stream ID */, 9);
1667
Willy Tarreaubcc45952019-05-26 10:05:50 +02001668 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001669 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001670 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001671 h2c->flags |= H2_CF_MUX_MALLOC;
1672 h2c->flags |= H2_CF_DEM_MROOM;
1673 return 0;
1674 }
1675
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001676 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001677 if (unlikely(ret <= 0)) {
1678 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001679 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1680 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001681 h2c->flags |= H2_CF_MUX_MFULL;
1682 h2c->flags |= H2_CF_DEM_MROOM;
1683 return 0;
1684 }
1685 else {
1686 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1687 return 0;
1688 }
1689 }
1690 return ret;
1691}
1692
Willy Tarreaucf68c782017-10-10 17:11:41 +02001693/* processes a PING frame and schedules an ACK if needed. The caller must pass
1694 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001695 * missing data. The caller must have already verified frame length
1696 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001697 */
1698static int h2c_handle_ping(struct h2c *h2c)
1699{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001700 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001701 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001702 h2c->st0 = H2_CS_FRAME_A;
1703 return 1;
1704}
1705
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001706/* Try to send a window update for stream id <sid> and value <increment>.
1707 * Returns > 0 on success or zero on missing room or failure. It may return an
1708 * error in h2c.
1709 */
1710static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1711{
1712 struct buffer *res;
1713 char str[13];
1714 int ret = -1;
1715
1716 if (h2c_mux_busy(h2c, NULL)) {
1717 h2c->flags |= H2_CF_DEM_MBUSY;
1718 return 0;
1719 }
1720
Willy Tarreau9c218e72019-05-26 10:08:28 +02001721 /* length: 4, type: 8, flags: none */
1722 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1723 write_n32(str + 5, sid);
1724 write_n32(str + 9, increment);
1725
Willy Tarreaubcc45952019-05-26 10:05:50 +02001726 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001727 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001728 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001729 h2c->flags |= H2_CF_MUX_MALLOC;
1730 h2c->flags |= H2_CF_DEM_MROOM;
1731 return 0;
1732 }
1733
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001734 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001735 if (unlikely(ret <= 0)) {
1736 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001737 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1738 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001739 h2c->flags |= H2_CF_MUX_MFULL;
1740 h2c->flags |= H2_CF_DEM_MROOM;
1741 return 0;
1742 }
1743 else {
1744 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1745 return 0;
1746 }
1747 }
1748 return ret;
1749}
1750
1751/* try to send pending window update for the connection. It's safe to call it
1752 * with no pending updates. Returns > 0 on success or zero on missing room or
1753 * failure. It may return an error in h2c.
1754 */
1755static int h2c_send_conn_wu(struct h2c *h2c)
1756{
1757 int ret = 1;
1758
1759 if (h2c->rcvd_c <= 0)
1760 return 1;
1761
Willy Tarreau97aaa672018-12-23 09:49:04 +01001762 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1763 /* increase the advertised connection window to 2G on
1764 * first update.
1765 */
1766 h2c->flags |= H2_CF_WINDOW_OPENED;
1767 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1768 }
1769
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001770 /* send WU for the connection */
1771 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1772 if (ret > 0)
1773 h2c->rcvd_c = 0;
1774
1775 return ret;
1776}
1777
1778/* try to send pending window update for the current dmux stream. It's safe to
1779 * call it with no pending updates. Returns > 0 on success or zero on missing
1780 * room or failure. It may return an error in h2c.
1781 */
1782static int h2c_send_strm_wu(struct h2c *h2c)
1783{
1784 int ret = 1;
1785
1786 if (h2c->rcvd_s <= 0)
1787 return 1;
1788
1789 /* send WU for the stream */
1790 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1791 if (ret > 0)
1792 h2c->rcvd_s = 0;
1793
1794 return ret;
1795}
1796
Willy Tarreaucf68c782017-10-10 17:11:41 +02001797/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1798 * success, 0 on missing data or one of the h2_status values.
1799 */
1800static int h2c_ack_ping(struct h2c *h2c)
1801{
1802 struct buffer *res;
1803 char str[17];
1804 int ret = -1;
1805
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001806 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001807 return 0;
1808
1809 if (h2c_mux_busy(h2c, NULL)) {
1810 h2c->flags |= H2_CF_DEM_MBUSY;
1811 return 0;
1812 }
1813
Willy Tarreaucf68c782017-10-10 17:11:41 +02001814 memcpy(str,
1815 "\x00\x00\x08" /* length : 8 (same payload) */
1816 "\x06" "\x01" /* type : 6, flags : ACK */
1817 "\x00\x00\x00\x00" /* stream ID */, 9);
1818
1819 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001820 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001821
Willy Tarreau9c218e72019-05-26 10:08:28 +02001822 res = br_tail(h2c->mbuf);
1823 retry:
1824 if (!h2_get_buf(h2c, res)) {
1825 h2c->flags |= H2_CF_MUX_MALLOC;
1826 h2c->flags |= H2_CF_DEM_MROOM;
1827 return 0;
1828 }
1829
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001830 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001831 if (unlikely(ret <= 0)) {
1832 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001833 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1834 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02001835 h2c->flags |= H2_CF_MUX_MFULL;
1836 h2c->flags |= H2_CF_DEM_MROOM;
1837 return 0;
1838 }
1839 else {
1840 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1841 return 0;
1842 }
1843 }
1844 return ret;
1845}
1846
Willy Tarreau26f95952017-07-27 17:18:30 +02001847/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1848 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001849 * h2c or h2s. The caller must have already verified frame length and stream ID
1850 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001851 */
1852static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1853{
1854 int32_t inc;
1855 int error;
1856
Willy Tarreau26f95952017-07-27 17:18:30 +02001857 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001858 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001859 return 0;
1860
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001861 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001862
1863 if (h2c->dsi != 0) {
1864 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001865
1866 /* it's not an error to receive WU on a closed stream */
1867 if (h2s->st == H2_SS_CLOSED)
1868 return 1;
1869
1870 if (!inc) {
1871 error = H2_ERR_PROTOCOL_ERROR;
1872 goto strm_err;
1873 }
1874
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001875 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001876 error = H2_ERR_FLOW_CONTROL_ERROR;
1877 goto strm_err;
1878 }
1879
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001880 h2s->sws += inc;
1881 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001882 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreaubda92dd2019-10-02 10:49:59 +02001883 LIST_DEL_INIT(&h2s->list);
1884 if (h2s->send_wait)
Olivier Houcharddddfe312018-10-10 18:51:00 +02001885 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001886 }
1887 }
1888 else {
1889 /* connection window update */
1890 if (!inc) {
1891 error = H2_ERR_PROTOCOL_ERROR;
1892 goto conn_err;
1893 }
1894
1895 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1896 error = H2_ERR_FLOW_CONTROL_ERROR;
1897 goto conn_err;
1898 }
1899
1900 h2c->mws += inc;
1901 }
1902
1903 return 1;
1904
1905 conn_err:
1906 h2c_error(h2c, error);
1907 return 0;
1908
1909 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001910 h2s_error(h2s, error);
1911 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001912 return 0;
1913}
1914
Willy Tarreaue96b0922017-10-30 00:28:29 +01001915/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001916 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1917 * have already verified frame length and stream ID validity. Described in
1918 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001919 */
1920static int h2c_handle_goaway(struct h2c *h2c)
1921{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001922 int last;
1923
Willy Tarreaue96b0922017-10-30 00:28:29 +01001924 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001925 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001926 return 0;
1927
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001928 last = h2_get_n32(&h2c->dbuf, 0);
1929 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001930 if (h2c->last_sid < 0)
1931 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02001932 h2_wake_some_streams(h2c, last);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001933 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001934}
1935
Willy Tarreau92153fc2017-12-03 19:46:19 +01001936/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001937 * invalid. Returns > 0 on success or zero on missing data. It may return an
1938 * error in h2c. The caller must have already verified frame length and stream
1939 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001940 */
1941static int h2c_handle_priority(struct h2c *h2c)
1942{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001943 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001944 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001945 return 0;
1946
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001947 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001948 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001949 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1950 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001951 }
1952 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001953}
1954
Willy Tarreaucd234e92017-08-18 10:59:39 +02001955/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001956 * Returns > 0 on success or zero on missing data. The caller must have already
1957 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001958 */
1959static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1960{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001961 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001962 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001963 return 0;
1964
1965 /* late RST, already handled */
1966 if (h2s->st == H2_SS_CLOSED)
1967 return 1;
1968
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001969 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001970 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001971
1972 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001973 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001974 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001975 }
1976
1977 h2s->flags |= H2_SF_RST_RCVD;
1978 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001979}
1980
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001981/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1982 * It may return an error in h2c or h2s. The caller must consider that the
1983 * return value is the new h2s in case one was allocated (most common case).
1984 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001985 * errors here are reported as connection errors since it's impossible to
1986 * recover from such errors after the compression context has been altered.
1987 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001988static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001989{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001990 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001991 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001992 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001993 int error;
1994
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001995 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001996 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001997
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001998 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001999 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02002000
2001 /* now either the frame is complete or the buffer is complete */
2002 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002003 /* The stream exists/existed, this must be a trailers frame */
2004 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02002005 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
2006 /* unrecoverable error ? */
2007 if (h2c->st0 >= H2_CS_ERROR)
2008 goto out;
2009
2010 if (error == 0)
2011 goto out; // missing data
2012
2013 if (error < 0) {
2014 /* Failed to decode this frame (e.g. too large request)
2015 * but the HPACK decompressor is still synchronized.
2016 */
2017 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2018 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002019 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002020 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002021 goto done;
2022 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002023 /* the connection was already killed by an RST, let's consume
2024 * the data and send another RST.
2025 */
2026 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2027 h2s = (struct h2s*)h2_error_stream;
2028 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002029 }
2030 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2031 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2032 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002033 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002034 goto conn_err;
2035 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002036 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2037 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002038
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002039 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002040
Willy Tarreau25919232019-01-03 14:48:18 +01002041 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002042 if (h2c->st0 >= H2_CS_ERROR)
2043 goto out;
2044
Willy Tarreau25919232019-01-03 14:48:18 +01002045 if (error <= 0) {
2046 if (error == 0)
2047 goto out; // missing data
2048
2049 /* Failed to decode this stream (e.g. too large request)
2050 * but the HPACK decompressor is still synchronized.
2051 */
2052 h2s = (struct h2s*)h2_error_stream;
2053 goto send_rst;
2054 }
2055
Willy Tarreau22de8d32018-09-05 19:55:58 +02002056 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002057 * positively from h2c_frt_stream_new(), the stream will report the error,
2058 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002059 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02002060 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02002061 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002062 h2s = (struct h2s*)h2_refused_stream;
2063 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002064 }
2065
2066 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002067 h2s->rxbuf = rxbuf;
2068 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002069 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002070
Willy Tarreau88d138e2019-01-02 19:38:14 +01002071 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002072 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002073 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002074
2075 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002076 if (h2s->st == H2_SS_OPEN)
2077 h2s->st = H2_SS_HREM;
2078 else
2079 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002080 }
2081
Willy Tarreau3a429f02019-01-03 11:41:50 +01002082 /* update the max stream ID if the request is being processed */
2083 if (h2s->id > h2c->max_id)
2084 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002085
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002086 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002087
2088 conn_err:
2089 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002090 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002091
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002092 out:
2093 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002094 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002095
2096 send_rst:
2097 /* make the demux send an RST for the current stream. We may only
2098 * do this if we're certain that the HEADERS frame was properly
2099 * decompressed so that the HPACK decoder is still kept up to date.
2100 */
2101 h2_release_buf(h2c, &rxbuf);
2102 h2c->st0 = H2_CS_FRAME_E;
2103 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002104}
2105
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002106/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2107 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2108 * errors here are reported as connection errors since it's impossible to
2109 * recover from such errors after the compression context has been altered.
2110 */
2111static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2112{
Christopher Faulet96b88f22019-09-23 15:28:20 +02002113 struct buffer rxbuf = BUF_NULL;
2114 unsigned long long body_len = 0;
2115 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002116 int error;
2117
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002118 if (!b_size(&h2c->dbuf))
2119 return NULL; // empty buffer
2120
2121 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2122 return NULL; // incomplete frame
2123
Christopher Faulet96b88f22019-09-23 15:28:20 +02002124 if (h2s->st != H2_SS_CLOSED) {
2125 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
2126 }
2127 else {
2128 /* the connection was already killed by an RST, let's consume
2129 * the data and send another RST.
2130 */
2131 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Christopher Faulet03427052019-09-26 16:19:13 +02002132 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet96b88f22019-09-23 15:28:20 +02002133 h2c->st0 = H2_CS_FRAME_E;
2134 goto send_rst;
2135 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002136
Willy Tarreau25919232019-01-03 14:48:18 +01002137 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002138 if (h2c->st0 >= H2_CS_ERROR)
2139 return NULL;
2140
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002141 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2142 /* RFC7540#5.1 */
2143 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2144 h2c->st0 = H2_CS_FRAME_E;
2145 return NULL;
2146 }
2147
Willy Tarreau25919232019-01-03 14:48:18 +01002148 if (error <= 0) {
2149 if (error == 0)
2150 return NULL; // missing data
2151
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002152 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002153 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002154 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002155 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002156 }
2157
Christopher Fauletfa922f02019-05-07 10:55:17 +02002158 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002159 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002160
Willy Tarreau927b88b2019-03-04 08:03:25 +01002161 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002162 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002163 else if (h2s->flags & H2_SF_ES_RCVD) {
2164 if (h2s->st == H2_SS_OPEN)
2165 h2s->st = H2_SS_HREM;
2166 else if (h2s->st == H2_SS_HLOC)
2167 h2s_close(h2s);
2168 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002169
2170 return h2s;
Christopher Faulet96b88f22019-09-23 15:28:20 +02002171
2172 send_rst:
2173 /* make the demux send an RST for the current stream. We may only
2174 * do this if we're certain that the HEADERS frame was properly
2175 * decompressed so that the HPACK decoder is still kept up to date.
2176 */
2177 h2_release_buf(h2c, &rxbuf);
2178 h2c->st0 = H2_CS_FRAME_E;
2179 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002180}
2181
Willy Tarreau454f9052017-10-26 19:40:35 +02002182/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2183 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2184 */
2185static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2186{
2187 int error;
2188
2189 /* note that empty DATA frames are perfectly valid and sometimes used
2190 * to signal an end of stream (with the ES flag).
2191 */
2192
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002193 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002194 return 0; // empty buffer
2195
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002196 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002197 return 0; // incomplete frame
2198
2199 /* now either the frame is complete or the buffer is complete */
2200
Willy Tarreau454f9052017-10-26 19:40:35 +02002201 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2202 /* RFC7540#6.1 */
2203 error = H2_ERR_STREAM_CLOSED;
2204 goto strm_err;
2205 }
2206
Christopher Faulet4fb65f42019-06-19 09:25:58 +02002207 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002208 /* RFC7540#8.1.2 */
2209 error = H2_ERR_PROTOCOL_ERROR;
2210 goto strm_err;
2211 }
2212
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002213 if (!h2_frt_transfer_data(h2s))
2214 return 0;
2215
Willy Tarreau454f9052017-10-26 19:40:35 +02002216 /* call the upper layers to process the frame, then let the upper layer
2217 * notify the stream about any change.
2218 */
2219 if (!h2s->cs) {
Willy Tarreauc1a29652019-08-06 10:11:02 +02002220 /* The upper layer has already closed, this may happen on
2221 * 4xx/redirects during POST, or when receiving a response
2222 * from an H2 server after the client has aborted.
2223 */
2224 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002225 goto strm_err;
2226 }
2227
Willy Tarreau8f650c32017-11-21 19:36:21 +01002228 if (h2c->st0 >= H2_CS_ERROR)
2229 return 0;
2230
Willy Tarreau721c9742017-11-07 11:05:42 +01002231 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002232 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002233 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002234 }
2235
2236 /* check for completion : the callee will change this to FRAME_A or
2237 * FRAME_H once done.
2238 */
2239 if (h2c->st0 == H2_CS_FRAME_P)
2240 return 0;
2241
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002242 /* last frame */
2243 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002244 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002245 if (h2s->st == H2_SS_OPEN)
2246 h2s->st = H2_SS_HREM;
2247 else
2248 h2s_close(h2s);
2249
Willy Tarreau1915ca22019-01-24 11:49:37 +01002250 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2251 /* RFC7540#8.1.2 */
2252 error = H2_ERR_PROTOCOL_ERROR;
2253 goto strm_err;
2254 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002255 }
2256
Willy Tarreau454f9052017-10-26 19:40:35 +02002257 return 1;
2258
Willy Tarreau454f9052017-10-26 19:40:35 +02002259 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002260 h2s_error(h2s, error);
2261 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002262 return 0;
2263}
2264
Willy Tarreaubc933932017-10-09 16:21:43 +02002265/* process Rx frames to be demultiplexed */
2266static void h2_process_demux(struct h2c *h2c)
2267{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002268 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002269 struct h2_fh hdr;
2270 unsigned int padlen = 0;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002271 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002272
Willy Tarreau081d4722017-05-16 21:51:05 +02002273 if (h2c->st0 >= H2_CS_ERROR)
2274 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002275
2276 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2277 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002278 if (h2c->flags & H2_CF_IS_BACK)
2279 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002280 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2281 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002282 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002283 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002284 sess_log(h2c->conn->owner);
2285 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002286 goto fail;
2287 }
2288
2289 h2c->max_id = 0;
2290 h2c->st0 = H2_CS_SETTINGS1;
2291 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002292
2293 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002294 /* ensure that what is pending is a valid SETTINGS frame
2295 * without an ACK.
2296 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002297 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002298 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002299 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002300 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002301 sess_log(h2c->conn->owner);
2302 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002303 goto fail;
2304 }
2305
2306 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2307 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2308 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2309 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002310 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002311 goto fail;
2312 }
2313
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002314 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002315 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2316 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2317 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002318 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002319 goto fail;
2320 }
2321
Willy Tarreau3bf69182018-12-21 15:34:50 +01002322 /* that's OK, switch to FRAME_P to process it. This is
2323 * a SETTINGS frame whose header has already been
2324 * deleted above.
2325 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002326 padlen = 0;
2327 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002328 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002329 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002330
2331 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002332 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002333 int ret = 0;
2334
2335 if (h2c->st0 >= H2_CS_ERROR)
2336 break;
2337
2338 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreauab3ebbc2019-08-06 15:49:51 +02002339 h2c->rcvd_s = 0;
2340
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002341 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002342 break;
2343
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002344 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002345 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002346 if (!h2c->nb_streams) {
2347 /* only log if no other stream can report the error */
2348 sess_log(h2c->conn->owner);
2349 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002350 break;
2351 }
2352
Christopher Faulet3d574a52019-06-18 12:22:38 +02002353 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002354 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2355 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2356 * we read the pad length and drop it from the remaining
2357 * payload (one byte + the 9 remaining ones = 10 total
2358 * removed), so we have a frame payload starting after the
2359 * pad len. Flow controlled frames (DATA) also count the
2360 * padlen in the flow control, so it must be adjusted.
2361 */
2362 if (hdr.len < 1) {
2363 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2364 sess_log(h2c->conn->owner);
2365 goto fail;
2366 }
2367 hdr.len--;
2368
2369 if (b_data(&h2c->dbuf) < 10)
2370 break; // missing padlen
2371
2372 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2373
2374 if (padlen > hdr.len) {
2375 /* RFC7540#6.1 : pad length = length of
2376 * frame payload or greater => error.
2377 */
2378 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2379 sess_log(h2c->conn->owner);
2380 goto fail;
2381 }
2382
2383 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2384 h2c->rcvd_c++;
2385 h2c->rcvd_s++;
2386 }
2387 b_del(&h2c->dbuf, 1);
2388 }
2389 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002390
2391 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002392 h2c->dfl = hdr.len;
2393 h2c->dsi = hdr.sid;
2394 h2c->dft = hdr.ft;
2395 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002396 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002397 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002398
2399 /* check for minimum basic frame format validity */
2400 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2401 if (ret != H2_ERR_NO_ERROR) {
2402 h2c_error(h2c, ret);
2403 sess_log(h2c->conn->owner);
2404 goto fail;
2405 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002406 }
2407
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002408 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
2409 * H2_CS_FRAME_P indicates an incomplete previous operation
2410 * (most often the first attempt) and requires some validity
2411 * checks for the frame and the current state. The two other
2412 * ones are set after completion (or abortion) and must skip
2413 * validity checks.
2414 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002415 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2416
Willy Tarreau567beb82018-12-18 16:52:44 +01002417 if (tmp_h2s != h2s && h2s && h2s->cs &&
2418 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002419 conn_xprt_read0_pending(h2c->conn) ||
2420 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002421 (h2s->flags & H2_SF_ES_RCVD) ||
2422 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002423 /* we may have to signal the upper layers */
2424 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002425 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002426 }
2427 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002428
Willy Tarreaud7901432017-12-29 11:34:40 +01002429 if (h2c->st0 == H2_CS_FRAME_E)
2430 goto strm_err;
2431
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002432 if (h2c->st0 == H2_CS_FRAME_A)
2433 goto valid_frame_type;
2434
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002435 if (h2s->st == H2_SS_IDLE &&
2436 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2437 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2438 * this state MUST be treated as a connection error
2439 */
2440 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002441 if (!h2c->nb_streams) {
2442 /* only log if no other stream can report the error */
2443 sess_log(h2c->conn->owner);
2444 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002445 break;
2446 }
2447
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002448 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2449 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2450 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002451 * this state MUST be treated as a stream error.
2452 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2453 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002454 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002455 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2456 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2457 else
2458 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002459 goto strm_err;
2460 }
2461
Willy Tarreauab837502017-12-27 15:07:30 +01002462 /* Below the management of frames received in closed state is a
2463 * bit hackish because the spec makes strong differences between
2464 * streams closed by receiving RST, sending RST, and seeing ES
2465 * in both directions. In addition to this, the creation of a
2466 * new stream reusing the identifier of a closed one will be
2467 * detected here. Given that we cannot keep track of all closed
2468 * streams forever, we consider that unknown closed streams were
2469 * closed on RST received, which allows us to respond with an
2470 * RST without breaking the connection (eg: to abort a transfer).
2471 * Some frames have to be silently ignored as well.
2472 */
2473 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002474 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002475 /* #5.1.1: The identifier of a newly
2476 * established stream MUST be numerically
2477 * greater than all streams that the initiating
2478 * endpoint has opened or reserved. This
2479 * governs streams that are opened using a
2480 * HEADERS frame and streams that are reserved
2481 * using PUSH_PROMISE. An endpoint that
2482 * receives an unexpected stream identifier
2483 * MUST respond with a connection error.
2484 */
2485 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2486 goto strm_err;
2487 }
2488
Willy Tarreaueb5be9d2019-09-26 08:47:15 +02002489 if (h2s->flags & H2_SF_RST_RCVD &&
2490 !(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 +01002491 /* RFC7540#5.1:closed: an endpoint that
2492 * receives any frame other than PRIORITY after
2493 * receiving a RST_STREAM MUST treat that as a
2494 * stream error of type STREAM_CLOSED.
2495 *
2496 * Note that old streams fall into this category
2497 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002498 *
2499 * However, we cannot generalize this to all frame types. Those
2500 * carrying compression state must still be processed before
2501 * being dropped or we'll desynchronize the decoder. This can
2502 * happen with request trailers received after sending an
2503 * RST_STREAM, or with header/trailers responses received after
2504 * sending RST_STREAM (aborted stream).
Willy Tarreaueb5be9d2019-09-26 08:47:15 +02002505 *
2506 * In addition, since our CLOSED streams always carry the
2507 * RST_RCVD bit, we don't want to accidently catch valid
2508 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreauab837502017-12-27 15:07:30 +01002509 */
2510 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2511 h2c->st0 = H2_CS_FRAME_E;
2512 goto strm_err;
2513 }
2514
2515 /* RFC7540#5.1:closed: if this state is reached as a
2516 * result of sending a RST_STREAM frame, the peer that
2517 * receives the RST_STREAM might have already sent
2518 * frames on the stream that cannot be withdrawn. An
2519 * endpoint MUST ignore frames that it receives on
2520 * closed streams after it has sent a RST_STREAM
2521 * frame. An endpoint MAY choose to limit the period
2522 * over which it ignores frames and treat frames that
2523 * arrive after this time as being in error.
2524 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002525 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002526 /* RFC7540#5.1:closed: any frame other than
2527 * PRIO/WU/RST in this state MUST be treated as
2528 * a connection error
2529 */
2530 if (h2c->dft != H2_FT_RST_STREAM &&
2531 h2c->dft != H2_FT_PRIORITY &&
2532 h2c->dft != H2_FT_WINDOW_UPDATE) {
2533 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2534 goto strm_err;
2535 }
2536 }
2537 }
2538
Willy Tarreauc0da1962017-10-30 18:38:00 +01002539#if 0
2540 // problem below: it is not possible to completely ignore such
2541 // streams as we need to maintain the compression state as well
2542 // and for this we need to completely process these frames (eg:
2543 // HEADERS frames) as well as counting DATA frames to emit
2544 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2545 // This is a typical case of layer violation where the
2546 // transported contents are critical to the connection's
2547 // validity and must be ignored at the same time :-(
2548
2549 /* graceful shutdown, ignore streams whose ID is higher than
2550 * the one advertised in GOAWAY. RFC7540#6.8.
2551 */
2552 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002553 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2554 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002555 h2c->dfl -= ret;
2556 ret = h2c->dfl == 0;
2557 goto strm_err;
2558 }
2559#endif
2560
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002561 valid_frame_type:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002562 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002563 case H2_FT_SETTINGS:
2564 if (h2c->st0 == H2_CS_FRAME_P)
2565 ret = h2c_handle_settings(h2c);
2566
2567 if (h2c->st0 == H2_CS_FRAME_A)
2568 ret = h2c_ack_settings(h2c);
2569 break;
2570
Willy Tarreaucf68c782017-10-10 17:11:41 +02002571 case H2_FT_PING:
2572 if (h2c->st0 == H2_CS_FRAME_P)
2573 ret = h2c_handle_ping(h2c);
2574
2575 if (h2c->st0 == H2_CS_FRAME_A)
2576 ret = h2c_ack_ping(h2c);
2577 break;
2578
Willy Tarreau26f95952017-07-27 17:18:30 +02002579 case H2_FT_WINDOW_UPDATE:
2580 if (h2c->st0 == H2_CS_FRAME_P)
2581 ret = h2c_handle_window_update(h2c, h2s);
2582 break;
2583
Willy Tarreau61290ec2017-10-17 08:19:21 +02002584 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002585 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2586 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2587 * frames' parsers consume all following CONTINUATION
2588 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002589 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002590 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2591 sess_log(h2c->conn->owner);
2592 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002593
Willy Tarreau13278b42017-10-13 19:23:14 +02002594 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002595 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002596 if (h2c->flags & H2_CF_IS_BACK)
2597 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2598 else
2599 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002600 if (tmp_h2s) {
2601 h2s = tmp_h2s;
2602 ret = 1;
2603 }
2604 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002605 break;
2606
Willy Tarreau454f9052017-10-26 19:40:35 +02002607 case H2_FT_DATA:
2608 if (h2c->st0 == H2_CS_FRAME_P)
2609 ret = h2c_frt_handle_data(h2c, h2s);
2610
2611 if (h2c->st0 == H2_CS_FRAME_A)
2612 ret = h2c_send_strm_wu(h2c);
2613 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002614
Willy Tarreau92153fc2017-12-03 19:46:19 +01002615 case H2_FT_PRIORITY:
2616 if (h2c->st0 == H2_CS_FRAME_P)
2617 ret = h2c_handle_priority(h2c);
2618 break;
2619
Willy Tarreaucd234e92017-08-18 10:59:39 +02002620 case H2_FT_RST_STREAM:
2621 if (h2c->st0 == H2_CS_FRAME_P)
2622 ret = h2c_handle_rst_stream(h2c, h2s);
2623 break;
2624
Willy Tarreaue96b0922017-10-30 00:28:29 +01002625 case H2_FT_GOAWAY:
2626 if (h2c->st0 == H2_CS_FRAME_P)
2627 ret = h2c_handle_goaway(h2c);
2628 break;
2629
Willy Tarreau1c661982017-10-30 13:52:01 +01002630 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002631 default:
2632 /* drop frames that we ignore. They may be larger than
2633 * the buffer so we drain all of their contents until
2634 * we reach the end.
2635 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002636 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2637 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002638 h2c->dfl -= ret;
2639 ret = h2c->dfl == 0;
2640 }
2641
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002642 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002643 /* We may have to send an RST if not done yet */
2644 if (h2s->st == H2_SS_ERROR)
2645 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002646
Willy Tarreaua20a5192017-12-27 11:02:06 +01002647 if (h2c->st0 == H2_CS_FRAME_E)
2648 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002649
Willy Tarreau7e98c052017-10-10 15:56:59 +02002650 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002651 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002652 break;
2653
2654 if (h2c->st0 != H2_CS_FRAME_H) {
Christopher Faulete12a26f2019-09-26 16:38:28 +02002655 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2656 b_del(&h2c->dbuf, ret);
2657 h2c->dfl -= ret;
2658 if (!h2c->dfl)
2659 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002660 }
2661 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002662
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002663 if (h2c->rcvd_c > 0 &&
2664 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2665 h2c_send_conn_wu(h2c);
2666
Willy Tarreau52eed752017-09-22 15:05:09 +02002667 fail:
2668 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002669 if (h2s && h2s->cs &&
2670 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002671 conn_xprt_read0_pending(h2c->conn) ||
2672 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002673 (h2s->flags & H2_SF_ES_RCVD) ||
2674 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002675 /* we may have to signal the upper layers */
2676 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002677 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002678 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002679
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002680 if (old_iw != h2c->miw)
2681 h2c_unblock_sfctl(h2c);
2682
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002683 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002684}
2685
2686/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2687 * the end.
2688 */
2689static int h2_process_mux(struct h2c *h2c)
2690{
Olivier Houchard998410a2019-04-15 19:23:37 +02002691 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002692
Willy Tarreau01b44822018-10-03 14:26:37 +02002693 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2694 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2695 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2696 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2697 if (h2c->st0 == H2_CS_ERROR) {
2698 h2c->st0 = H2_CS_ERROR2;
2699 sess_log(h2c->conn->owner);
2700 }
2701 goto fail;
2702 }
2703 h2c->st0 = H2_CS_SETTINGS1;
2704 }
2705 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002706 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002707 return 1;
2708 }
2709
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002710 /* start by sending possibly pending window updates */
Willy Tarreau9c497c22019-08-06 15:39:32 +02002711 if (h2c->rcvd_s > 0 &&
2712 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2713 h2c_send_strm_wu(h2c) < 0)
2714 goto fail;
2715
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002716 if (h2c->rcvd_c > 0 &&
2717 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2718 h2c_send_conn_wu(h2c) < 0)
2719 goto fail;
2720
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002721 /* First we always process the flow control list because the streams
2722 * waiting there were already elected for immediate emission but were
2723 * blocked just on this.
2724 */
2725
Olivier Houchard998410a2019-04-15 19:23:37 +02002726 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002727 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2728 h2c->st0 >= H2_CS_ERROR)
2729 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002730
Willy Tarreauc234ae32019-05-13 17:56:11 +02002731 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002732 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002733
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002734 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002735 /* For some reason, the upper layer failed to subsribe again,
2736 * so remove it from the send_list
2737 */
2738 if (!h2s->send_wait) {
2739 LIST_DEL_INIT(&h2s->list);
2740 continue;
2741 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002742 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002743 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002744 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002745 }
2746
Olivier Houchard998410a2019-04-15 19:23:37 +02002747 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002748 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2749 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002750
Willy Tarreauc234ae32019-05-13 17:56:11 +02002751 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002752 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002753
Olivier Houchard998410a2019-04-15 19:23:37 +02002754 /* For some reason, the upper layer failed to subsribe again,
2755 * so remove it from the send_list
2756 */
2757 if (!h2s->send_wait) {
2758 LIST_DEL_INIT(&h2s->list);
2759 continue;
2760 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002761 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002762 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002763 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002764 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002765 }
2766
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002767 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002768 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002769 if (h2c->st0 == H2_CS_ERROR) {
2770 if (h2c->max_id >= 0) {
2771 h2c_send_goaway_error(h2c, NULL);
2772 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2773 return 0;
2774 }
2775
2776 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2777 }
2778 return 1;
2779 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002780 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002781}
2782
Willy Tarreau62f52692017-10-08 23:01:42 +02002783
Willy Tarreau479998a2018-11-18 06:30:59 +01002784/* Attempt to read data, and subscribe if none available.
2785 * The function returns 1 if data has been received, otherwise zero.
2786 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002787static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002788{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002789 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002790 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002791 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002792 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002793
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002794 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002795 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002796
Willy Tarreau315d8072017-12-10 22:17:57 +01002797 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002798 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002799
Willy Tarreau44e973f2018-03-01 17:49:30 +01002800 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002801 if (!buf) {
2802 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002803 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002804 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002805
Olivier Houchard7505f942018-08-21 18:10:44 +02002806 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002807 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002808 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2809 /* HTX in use : try to pre-align the buffer like the
2810 * rxbufs will be to optimize memory copies. We'll make
2811 * sure that the frame header lands at the end of the
2812 * HTX block to alias it upon recv. We cannot use the
2813 * head because rcv_buf() will realign the buffer if
2814 * it's empty. Thus we cheat and pretend we already
2815 * have a few bytes there.
2816 */
2817 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002818 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002819 }
2820 else
2821 max = b_room(buf);
2822
Olivier Houchard7505f942018-08-21 18:10:44 +02002823 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002824 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002825 else
2826 ret = 0;
2827 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002828
Willy Tarreaua8fcdac2019-08-02 07:48:47 +02002829 if (max && !ret && h2_recv_allowed(h2c))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002830 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002831
Olivier Houcharda1411e62018-08-17 18:42:48 +02002832 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002833 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002834 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002835 }
2836
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002837 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002838 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002839 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002840}
2841
Willy Tarreau479998a2018-11-18 06:30:59 +01002842/* Try to send data if possible.
2843 * The function returns 1 if data have been sent, otherwise zero.
2844 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002845static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002846{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002847 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002848 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002849 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002850
2851 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002852 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002853
Olivier Houchard7505f942018-08-21 18:10:44 +02002854
Willy Tarreaua2af5122017-10-09 11:56:46 +02002855 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2856 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002857 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002858 }
2859
Willy Tarreaubc933932017-10-09 16:21:43 +02002860 /* This loop is quite simple : it tries to fill as much as it can from
2861 * pending streams into the existing buffer until it's reportedly full
2862 * or the end of send requests is reached. Then it tries to send this
2863 * buffer's contents out, marks it not full if at least one byte could
2864 * be sent, and tries again.
2865 *
2866 * The snd_buf() function normally takes a "flags" argument which may
2867 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2868 * data immediately comes and CO_SFL_STREAMER to indicate that the
2869 * connection is streaming lots of data (used to increase TLS record
2870 * size at the expense of latency). The former can be sent any time
2871 * there's a buffer full flag, as it indicates at least one stream
2872 * attempted to send and failed so there are pending data. An
2873 * alternative would be to set it as long as there's an active stream
2874 * but that would be problematic for ACKs until we have an absolute
2875 * guarantee that all waiters have at least one byte to send. The
2876 * latter should possibly not be set for now.
2877 */
2878
2879 done = 0;
2880 while (!done) {
2881 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002882 unsigned int released = 0;
2883 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02002884
2885 /* fill as much as we can into the current buffer */
2886 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2887 done = h2_process_mux(h2c);
2888
Olivier Houchard2b094432019-01-29 18:28:36 +01002889 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002890 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01002891
Willy Tarreaubc933932017-10-09 16:21:43 +02002892 if (conn->flags & CO_FL_ERROR)
2893 break;
2894
2895 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2896 flags |= CO_SFL_MSG_MORE;
2897
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002898 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
2899 if (b_data(buf)) {
2900 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002901 if (!ret) {
2902 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002903 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002904 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002905 sent = 1;
2906 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002907 if (b_data(buf)) {
2908 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002909 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002910 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002911 }
2912 b_free(buf);
2913 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02002914 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002915
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002916 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02002917 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002918
Willy Tarreaubc933932017-10-09 16:21:43 +02002919 /* wrote at least one byte, the buffer is not full anymore */
2920 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2921 }
2922
Willy Tarreaua2af5122017-10-09 11:56:46 +02002923 if (conn->flags & CO_FL_SOCK_WR_SH) {
2924 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02002925 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002926 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002927 /* We're not full anymore, so we can wake any task that are waiting
2928 * for us.
2929 */
Willy Tarreauc51d47c2019-09-25 07:57:31 +02002930 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002931 struct h2s *h2s;
2932
2933 list_for_each_entry(h2s, &h2c->send_list, list) {
2934 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2935 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002936
Willy Tarreauc234ae32019-05-13 17:56:11 +02002937 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002938 continue;
2939
Olivier Houchard998410a2019-04-15 19:23:37 +02002940 /* For some reason, the upper layer failed to subsribe again,
2941 * so remove it from the send_list
2942 */
2943 if (!h2s->send_wait) {
2944 LIST_DEL_INIT(&h2s->list);
2945 continue;
2946 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002947 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002948 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002949 tasklet_wakeup(h2s->send_wait->tasklet);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002950 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002951 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002952 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002953 /* We're done, no more to send */
Willy Tarreau662fafc2019-05-26 09:43:07 +02002954 if (!br_data(h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002955 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002956schedule:
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002957 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002958 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002959
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002960 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002961}
2962
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002963/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002964static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2965{
2966 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002967 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002968
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002969 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002970 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002971 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002972 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002973 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002974 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002975 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002976}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002977
Willy Tarreau62f52692017-10-08 23:01:42 +02002978/* callback called on any event by the connection handler.
2979 * It applies changes and returns zero, or < 0 if it wants immediate
2980 * destruction of the connection (which normally doesn not happen in h2).
2981 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002982static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002983{
Olivier Houchard7505f942018-08-21 18:10:44 +02002984 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002985
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002986 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002987 h2_process_demux(h2c);
2988
2989 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002990 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002991
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002992 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002993 h2c->flags &= ~H2_CF_DEM_DFULL;
2994 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002995 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002996
Willy Tarreau0b37d652018-10-03 10:33:02 +02002997 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002998 /* frontend is stopping, reload likely in progress, let's try
2999 * to announce a graceful shutdown if not yet done. We don't
3000 * care if it fails, it will be tried again later.
3001 */
3002 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3003 if (h2c->last_sid < 0)
3004 h2c->last_sid = (1U << 31) - 1;
3005 h2c_send_goaway_error(h2c, NULL);
3006 }
3007 }
3008
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003009 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003010 * If we received early data, and the handshake is done, wake
3011 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003012 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003013 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
3014 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
3015 struct eb32_node *node;
3016 struct h2s *h2s;
3017
3018 h2c->flags |= H2_CF_WAIT_FOR_HS;
3019 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
3020
3021 while (node) {
3022 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01003023 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01003024 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003025 node = eb32_next(node);
3026 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003027 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003028
Willy Tarreau26bd7612017-10-09 16:47:04 +02003029 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01003030 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
3031 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
3032 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02003033 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003034
3035 if (eb_is_empty(&h2c->streams_by_id)) {
3036 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003037 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003038 return -1;
3039 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003040 }
3041
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003042 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003043 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003044
Olivier Houchard53216e72018-10-10 15:46:36 +02003045 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3046 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3047 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003048 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003049 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3050 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003051 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003052
Willy Tarreau3f133572017-10-31 19:21:06 +01003053 if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02003054 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3055 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003056 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003057
Olivier Houchard7505f942018-08-21 18:10:44 +02003058 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02003059 return 0;
3060}
3061
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003062/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003063static int h2_wake(struct connection *conn)
3064{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003065 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003066
3067 return (h2_process(h2c));
3068}
3069
Willy Tarreauea392822017-10-31 10:02:25 +01003070/* Connection timeout management. The principle is that if there's no receipt
3071 * nor sending for a certain amount of time, the connection is closed. If the
3072 * MUX buffer still has lying data or is not allocatable, the connection is
3073 * immediately killed. If it's allocatable and empty, we attempt to send a
3074 * GOAWAY frame.
3075 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003076static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003077{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003078 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003079 int expired = tick_is_expired(t->expire, now_ms);
3080
Willy Tarreau0975f112018-03-29 15:22:59 +02003081 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01003082 return t;
3083
Olivier Houchard3f795f72019-04-17 22:51:06 +02003084 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003085
3086 if (!h2c) {
3087 /* resources were already deleted */
3088 return NULL;
3089 }
3090
3091 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003092 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003093 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003094
Willy Tarreau662fafc2019-05-26 09:43:07 +02003095 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003096 /* don't even try to send a GOAWAY, the buffer is stuck */
3097 h2c->flags |= H2_CF_GOAWAY_FAILED;
3098 }
3099
3100 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003101 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003102 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3103 h2c->flags |= H2_CF_GOAWAY_FAILED;
3104
Willy Tarreau662fafc2019-05-26 09:43:07 +02003105 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003106 unsigned int released = 0;
3107 struct buffer *buf;
3108
3109 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3110 if (b_data(buf)) {
3111 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3112 if (!ret)
3113 break;
3114 b_del(buf, ret);
3115 if (b_data(buf))
3116 break;
3117 b_free(buf);
3118 released++;
3119 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003120 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003121
3122 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003123 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003124 }
Willy Tarreauea392822017-10-31 10:02:25 +01003125
Willy Tarreau0975f112018-03-29 15:22:59 +02003126 /* either we can release everything now or it will be done later once
3127 * the last stream closes.
3128 */
3129 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003130 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003131
Willy Tarreauea392822017-10-31 10:02:25 +01003132 return NULL;
3133}
3134
3135
Willy Tarreau62f52692017-10-08 23:01:42 +02003136/*******************************************/
3137/* functions below are used by the streams */
3138/*******************************************/
3139
3140/*
3141 * Attach a new stream to a connection
3142 * (Used for outgoing connections)
3143 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003144static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003145{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003146 struct conn_stream *cs;
3147 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003148 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003149
3150 cs = cs_new(conn);
3151 if (!cs)
3152 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003153 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003154 if (!h2s) {
3155 cs_free(cs);
3156 return NULL;
3157 }
3158 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003159}
3160
Willy Tarreaufafd3982018-11-18 21:29:20 +01003161/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3162 * We have to scan because we may have some orphan streams. It might be
3163 * beneficial to scan backwards from the end to reduce the likeliness to find
3164 * orphans.
3165 */
3166static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3167{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003168 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003169 struct h2s *h2s;
3170 struct eb32_node *node;
3171
3172 node = eb32_first(&h2c->streams_by_id);
3173 while (node) {
3174 h2s = container_of(node, struct h2s, by_id);
3175 if (h2s->cs)
3176 return h2s->cs;
3177 node = eb32_next(node);
3178 }
3179 return NULL;
3180}
3181
Willy Tarreau62f52692017-10-08 23:01:42 +02003182/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003183 * Destroy the mux and the associated connection, if it is no longer used
3184 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003185static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003186{
Christopher Faulet73c12072019-04-08 11:23:22 +02003187 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003188
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003189 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003190 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003191}
3192
3193/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003194 * Detach the stream from the connection and possibly release the connection.
3195 */
3196static void h2_detach(struct conn_stream *cs)
3197{
Willy Tarreau60935142017-10-16 18:11:19 +02003198 struct h2s *h2s = cs->ctx;
3199 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003200 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003201
3202 cs->ctx = NULL;
3203 if (!h2s)
3204 return;
3205
Olivier Houchard998410a2019-04-15 19:23:37 +02003206 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003207 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003208 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003209 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003210 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003211 /*
3212 * At this point, the stream_interface is supposed to have called
3213 * h2_unsubscribe(), so the only way there's still a
3214 * subscription that came from the stream_interface (as we
3215 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3216 * without the stream_interface involved) is that we subscribed
3217 * for sending, we woke the tasklet up and removed the
3218 * SUB_RETRY_SEND flag, so the stream_interface would not
3219 * know it has to unsubscribe for send, but the tasklet hasn't
3220 * run yet. Make sure to handle that by explicitely setting
3221 * send_wait to NULL, as nothing else will do it for us.
3222 */
3223 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003224 }
3225
Olivier Houchardf502aca2018-12-14 19:42:40 +01003226 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003227 h2c = h2s->h2c;
3228 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003229 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003230 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3231 !h2_frt_has_too_many_cs(h2c)) {
3232 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003233 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003234 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003235 }
Willy Tarreau60935142017-10-16 18:11:19 +02003236
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003237 /* this stream may be blocked waiting for some data to leave (possibly
3238 * an ES or RST frame), so orphan it in this case.
3239 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003240 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003241 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003242 (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 +01003243 return;
3244
Willy Tarreau45f752e2017-10-30 15:44:59 +01003245 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3246 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3247 /* unblock the connection if it was blocked on this
3248 * stream.
3249 */
3250 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3251 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003252 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003253 }
3254
Willy Tarreau71049cc2018-03-28 13:56:39 +02003255 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003256
Olivier Houchard8a786902018-12-15 16:05:40 +01003257 if (h2c->flags & H2_CF_IS_BACK &&
3258 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003259 if (!(h2c->conn->flags &
3260 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3261 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003262 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003263 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3264 h2c->conn->owner = NULL;
3265 if (eb_is_empty(&h2c->streams_by_id)) {
3266 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3267 /* The server doesn't want it, let's kill the connection right away */
3268 h2c->conn->mux->destroy(h2c->conn);
3269 return;
3270 }
3271 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003272 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003273 if (eb_is_empty(&h2c->streams_by_id)) {
3274 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3275 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3276 return;
3277 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003278 /* Never ever allow to reuse a connection from a non-reuse backend */
3279 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3280 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003281 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003282 struct server *srv = objt_server(h2c->conn->target);
3283
3284 if (srv) {
3285 if (h2c->conn->flags & CO_FL_PRIVATE)
3286 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3287 else
3288 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3289 }
3290
3291 }
3292 }
3293 }
3294
Willy Tarreaue323f342018-03-28 13:51:45 +02003295 /* We don't want to close right now unless we're removing the
3296 * last stream, and either the connection is in error, or it
3297 * reached the ID already specified in a GOAWAY frame received
3298 * or sent (as seen by last_sid >= 0).
3299 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003300 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003301 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003302 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003303 }
3304 else if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02003305 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3306 task_queue(h2c->task);
Willy Tarreau60935142017-10-16 18:11:19 +02003307 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003308}
3309
Willy Tarreau88bdba32019-05-13 18:17:53 +02003310/* Performs a synchronous or asynchronous shutr(). */
3311static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003312{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003313 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003314 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003315
Willy Tarreauf983d002019-05-14 10:40:21 +02003316 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003317 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003318
Willy Tarreau18059042019-01-31 19:12:48 +01003319 /* a connstream may require us to immediately kill the whole connection
3320 * for example because of a "tcp-request content reject" rule that is
3321 * normally used to limit abuse. In this case we schedule a goaway to
3322 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003323 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003324 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003325 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3326 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3327 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3328 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003329 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3330 /* Nothing was never sent for this stream, so reset with
3331 * REFUSED_STREAM error to let the client retry the
3332 * request.
3333 */
3334 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3335 }
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003336 else {
3337 /* a final response was already provided, we don't want this
3338 * stream anymore. This may happen when the server responds
3339 * before the end of an upload and closes quickly (redirect,
3340 * deny, ...)
3341 */
3342 h2s_error(h2s, H2_ERR_CANCEL);
3343 }
Willy Tarreau18059042019-01-31 19:12:48 +01003344
Willy Tarreau90c32322017-11-24 08:00:30 +01003345 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003346 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003347 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003348
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003349 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003350 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003351 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003352 done:
3353 h2s->flags &= ~H2_SF_WANT_SHUTR;
3354 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003355add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003356 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003357 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003358 if (h2s->flags & H2_SF_BLK_MFCTL) {
3359 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3360 h2s->send_wait = sw;
3361 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3362 h2s->send_wait = sw;
3363 LIST_ADDQ(&h2c->send_list, &h2s->list);
3364 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003365 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003366 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003367 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003368 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003369}
3370
Willy Tarreau88bdba32019-05-13 18:17:53 +02003371/* Performs a synchronous or asynchronous shutw(). */
3372static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003373{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003374 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003375 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003376
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003377 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003378 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003379
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003380 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003381 /* we can cleanly close using an empty data frame only after headers */
3382
3383 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3384 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003385 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003386
3387 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003388 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003389 else
3390 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003391 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003392 /* a connstream may require us to immediately kill the whole connection
3393 * for example because of a "tcp-request content reject" rule that is
3394 * normally used to limit abuse. In this case we schedule a goaway to
3395 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003396 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003397 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003398 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3399 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3400 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3401 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003402 else {
3403 /* Nothing was never sent for this stream, so reset with
3404 * REFUSED_STREAM error to let the client retry the
3405 * request.
3406 */
3407 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3408 }
Willy Tarreau18059042019-01-31 19:12:48 +01003409
Willy Tarreau90c32322017-11-24 08:00:30 +01003410 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003411 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003412 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003413
Willy Tarreau00dd0782018-03-01 16:31:34 +01003414 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003415 }
3416
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003417 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003418 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003419 done:
3420 h2s->flags &= ~H2_SF_WANT_SHUTW;
3421 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003422
3423 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003424 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003425 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003426 if (h2s->flags & H2_SF_BLK_MFCTL) {
3427 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3428 h2s->send_wait = sw;
3429 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3430 h2s->send_wait = sw;
3431 LIST_ADDQ(&h2c->send_list, &h2s->list);
3432 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003433 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003434 /* let the handler know we want to shutw */
3435 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003436 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003437}
3438
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003439/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003440 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3441 * and prevented the last frame from being emitted.
3442 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003443static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3444{
3445 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003446 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003447
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003448 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003449 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003450 h2_do_shutw(h2s);
3451
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003452 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003453 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003454
Willy Tarreau88bdba32019-05-13 18:17:53 +02003455 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003456 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003457 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003458
Willy Tarreau88bdba32019-05-13 18:17:53 +02003459 if (!h2s->cs) {
3460 h2s_destroy(h2s);
3461 if (h2c_is_dead(h2c))
3462 h2_release(h2c);
3463 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003464 }
3465
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003466 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003467}
3468
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003469/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003470static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3471{
3472 struct h2s *h2s = cs->ctx;
3473
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003474 if (cs->flags & CS_FL_KILL_CONN)
3475 h2s->flags |= H2_SF_KILL_CONN;
3476
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003477 if (!mode)
3478 return;
3479
3480 h2_do_shutr(h2s);
3481}
3482
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003483/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003484static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3485{
3486 struct h2s *h2s = cs->ctx;
3487
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003488 if (cs->flags & CS_FL_KILL_CONN)
3489 h2s->flags |= H2_SF_KILL_CONN;
3490
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003491 h2_do_shutw(h2s);
3492}
3493
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003494/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003495 * HTX request or response depending on the connection's side. Returns a
3496 * positive value on success, a negative value on failure, or 0 if it couldn't
3497 * proceed. May report connection errors in h2c->errcode if the frame is
3498 * non-decodable and the connection unrecoverable. In absence of connection
3499 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003500 *
3501 * The function may fold CONTINUATION frames into the initial HEADERS frame
3502 * by removing padding and next frame header, then moving the CONTINUATION
3503 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3504 * leaving a hole between the main frame and the beginning of the next one.
3505 * The possibly remaining incomplete or next frame at the end may be moved
3506 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3507 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3508 *
3509 * A buffer at the beginning of processing may look like this :
3510 *
3511 * ,---.---------.-----.--------------.--------------.------.---.
3512 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3513 * `---^---------^-----^--------------^--------------^------^---'
3514 * | | <-----> | |
3515 * area | dpl | wrap
3516 * |<--------------> |
3517 * | dfl |
3518 * |<-------------------------------------------------->|
3519 * head data
3520 *
3521 * Padding is automatically overwritten when folding, participating to the
3522 * hole size after dfl :
3523 *
3524 * ,---.------------------------.-----.--------------.------.---.
3525 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3526 * `---^------------------------^-----^--------------^------^---'
3527 * | | <-----> | |
3528 * area | hole | wrap
3529 * |<-----------------------> |
3530 * | dfl |
3531 * |<-------------------------------------------------->|
3532 * head data
3533 *
3534 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3535 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3536 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003537 *
3538 * The <flags> field must point to either the stream's flags or to a copy of it
3539 * so that the function can update the following flags :
3540 * - H2_SF_DATA_CLEN when content-length is seen
3541 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3542 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003543 *
3544 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3545 * decoding, in order to detect if we're dealing with a headers or a trailers
3546 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003547 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003548static 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 +02003549{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003550 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003551 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003552 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003553 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003554 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003555 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003556 int flen; // header frame len
3557 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003558 int ret = 0;
3559 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003560 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003561 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003562
Willy Tarreauea18f862018-12-22 20:19:26 +01003563next_frame:
3564 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3565 goto leave; // incomplete input frame
3566
3567 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3568 * this case, we'll try to paste it immediately after the initial
3569 * HEADERS frame payload and kill any possible padding. The initial
3570 * frame's length will be increased to represent the concatenation
3571 * of the two frames. The next frame is read from position <tlen>
3572 * and written at position <flen> (minus padding if some is present).
3573 */
3574 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3575 struct h2_fh hdr;
3576 int clen; // CONTINUATION frame's payload length
3577
3578 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3579 /* no more data, the buffer may be full, either due to
3580 * too large a frame or because of too large a hole that
3581 * we're going to compact at the end.
3582 */
3583 goto leave;
3584 }
3585
3586 if (hdr.ft != H2_FT_CONTINUATION) {
3587 /* RFC7540#6.10: frame of unexpected type */
3588 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3589 goto fail;
3590 }
3591
3592 if (hdr.sid != h2c->dsi) {
3593 /* RFC7540#6.10: frame of different stream */
3594 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3595 goto fail;
3596 }
3597
3598 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3599 /* RFC7540#4.2: invalid frame length */
3600 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3601 goto fail;
3602 }
3603
3604 /* detect when we must stop aggragating frames */
3605 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3606
3607 /* Take as much as we can of the CONTINUATION frame's payload */
3608 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3609 if (clen > hdr.len)
3610 clen = hdr.len;
3611
3612 /* Move the frame's payload over the padding, hole and frame
3613 * header. At least one of hole or dpl is null (see diagrams
3614 * above). The hole moves after the new aggragated frame.
3615 */
3616 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3617 h2c->dfl += clen - h2c->dpl;
3618 hole += h2c->dpl + 9;
3619 h2c->dpl = 0;
3620 goto next_frame;
3621 }
3622
3623 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003624
Willy Tarreau13278b42017-10-13 19:23:14 +02003625 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003626 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003627 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003628 copy = alloc_trash_chunk();
3629 if (!copy) {
3630 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3631 goto fail;
3632 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003633 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3634 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3635 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003636 }
3637
Willy Tarreau13278b42017-10-13 19:23:14 +02003638 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3639 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003640 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003641 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3642 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003643 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003644 }
3645
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003646 if (flen < 5) {
3647 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3648 goto fail;
3649 }
3650
Willy Tarreau13278b42017-10-13 19:23:14 +02003651 hdrs += 5; // stream dep = 4, weight = 1
3652 flen -= 5;
3653 }
3654
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003655 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003656 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003657 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003658 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003659
Willy Tarreau937f7602018-02-26 15:22:17 +01003660 /* we can't retry a failed decompression operation so we must be very
3661 * careful not to take any risks. In practice the output buffer is
3662 * always empty except maybe for trailers, in which case we simply have
3663 * to wait for the upper layer to finish consuming what is available.
3664 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003665
3666 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003667 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003668 if (!htx_is_empty(htx)) {
3669 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003670 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003671 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003672 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003673 if (b_data(rxbuf)) {
3674 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003675 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003676 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003677
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003678 rxbuf->head = 0;
3679 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003680 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003681
Willy Tarreau25919232019-01-03 14:48:18 +01003682 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003683 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3684 sizeof(list)/sizeof(list[0]), tmp);
3685 if (outlen < 0) {
3686 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3687 goto fail;
3688 }
3689
Willy Tarreau25919232019-01-03 14:48:18 +01003690 /* The PACK decompressor was updated, let's update the input buffer and
3691 * the parser's state to commit these changes and allow us to later
3692 * fail solely on the stream if needed.
3693 */
3694 b_del(&h2c->dbuf, h2c->dfl + hole);
3695 h2c->dfl = hole = 0;
3696 h2c->st0 = H2_CS_FRAME_H;
3697
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003698 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003699 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003700
Willy Tarreau88d138e2019-01-02 19:38:14 +01003701 if (*flags & H2_SF_HEADERS_RCVD)
3702 goto trailers;
3703
3704 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003705 if (htx) {
3706 /* HTX mode */
3707 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003708 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003709 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003710 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003711 } else {
3712 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003713 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003714 if (outlen > 0)
3715 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003716 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003717
3718 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003719 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003720 goto fail;
3721 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003722
Willy Tarreau174b06a2018-04-25 18:13:58 +02003723 if (msgf & H2_MSGF_BODY) {
3724 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003725 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003726 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003727 if (htx)
3728 htx->extra = *body_len;
3729 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003730 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003731 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003732 }
3733
Willy Tarreau88d138e2019-01-02 19:38:14 +01003734 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003735 /* indicate that a HEADERS frame was received for this stream, except
3736 * for 1xx responses. For 1xx responses, another HEADERS frame is
3737 * expected.
3738 */
3739 if (!(msgf & H2_MSGF_RSP_1XX))
3740 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003741
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02003742 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003743 /* Mark the end of message, either using EOM in HTX or with the
3744 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003745 * is not set during headers with END_STREAM. For HTX trailers,
3746 * we must not leave an HTX trailers block not followed by an
3747 * EOM block, the two must be atomic. Thus if we fail to emit
3748 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003749 */
3750 if (htx) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02003751 if (!htx_add_endof(htx, HTX_BLK_EOM))
Willy Tarreau88d138e2019-01-02 19:38:14 +01003752 goto fail;
3753 }
3754 else if (*flags & H2_SF_DATA_CHNK) {
3755 if (!b_putblk(rxbuf, "\r\n", 2))
3756 goto fail;
3757 }
3758 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003759
Willy Tarreau86277d42019-01-02 15:36:11 +01003760 /* success */
3761 ret = 1;
3762
Willy Tarreau68dd9852017-07-03 14:44:26 +02003763 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003764 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003765 * move the remaining data over it.
3766 */
3767 if (hole) {
3768 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3769 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3770 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3771 b_sub(&h2c->dbuf, hole);
3772 }
3773
Willy Tarreau97215ca2019-04-29 10:20:21 +02003774 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003775 /* too large frames */
3776 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003777 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003778 }
3779
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003780 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003781 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003782 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003783 return ret;
3784
Willy Tarreau68dd9852017-07-03 14:44:26 +02003785 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003786 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003787 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003788
3789 trailers:
3790 /* This is the last HEADERS frame hence a trailer */
3791
3792 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3793 /* It's a trailer but it's missing ES flag */
3794 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3795 goto fail;
3796 }
3797
Christopher Faulet54b5e212019-06-04 10:08:28 +02003798 /* Trailers terminate a DATA sequence. In HTX we always handle them. In
3799 * legacy, when using chunks, we have to emit the 0 CRLF marker first
3800 * and then handle the trailers. For other modes, the trailers are
3801 * silently dropped.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003802 */
3803 if (htx) {
Willy Tarreau5255f282019-01-03 18:41:05 +01003804 if (h2_make_htx_trailers(list, htx) <= 0)
3805 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003806 }
3807 else if (*flags & H2_SF_DATA_CHNK) {
3808 /* Legacy mode with chunked encoding : we must finalize the
3809 * data block message emit the trailing CRLF */
3810 if (!b_putblk(rxbuf, "0\r\n", 3))
3811 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003812
3813 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3814 if (outlen > 0)
3815 b_add(rxbuf, outlen);
3816 else
3817 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003818 }
3819
3820 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003821}
3822
Willy Tarreau454f9052017-10-26 19:40:35 +02003823/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3824 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3825 * in use, a new chunk is emitted for each frame. This is supposed to fit
3826 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3827 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3828 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003829 * parser state is automatically updated. Returns > 0 if it could completely
3830 * send the current frame, 0 if it couldn't complete, in which case
3831 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3832 * DATA frame can return 0 as a valid result). Stream errors are reported in
3833 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3834 * have checked the frame header and ensured that the frame was complete or the
3835 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003836 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003837static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003838{
3839 struct h2c *h2c = h2s->h2c;
3840 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003841 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003842 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003843 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003844 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003845
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003846 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003847
Olivier Houchard638b7992018-08-16 15:41:52 +02003848 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003849 if (!csbuf) {
3850 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003851 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003852 }
3853
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003854try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003855 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003856 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3857 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003858 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003859 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003860
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003861 if (flen > b_data(&h2c->dbuf)) {
3862 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003863 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003864 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003865 }
3866
Willy Tarreaua9b77962019-01-31 07:23:00 +01003867 if (htx) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003868 unsigned int sent;
3869
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003870 block1 = htx_free_data_space(htx);
3871 if (!block1) {
3872 h2c->flags |= H2_CF_DEM_SFULL;
3873 goto fail;
3874 }
3875 if (flen > block1)
3876 flen = block1;
3877
3878 /* here, flen is the max we can copy into the output buffer */
3879 block1 = b_contig_data(&h2c->dbuf, 0);
3880 if (flen > block1)
3881 flen = block1;
3882
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003883 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003884
Willy Tarreaub6563f42019-06-15 11:34:41 +02003885 b_del(&h2c->dbuf, sent);
3886 h2c->dfl -= sent;
3887 h2c->rcvd_c += sent;
3888 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003889
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003890 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreaub6563f42019-06-15 11:34:41 +02003891 h2s->body_len -= sent;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003892 htx->extra = h2s->body_len;
3893 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003894
3895 if (sent < flen) {
3896 h2c->flags |= H2_CF_DEM_SFULL;
3897 goto fail;
3898 }
3899
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003900 goto try_again;
3901 }
Willy Tarreau455d5682019-05-24 19:42:18 +02003902 else if (unlikely(b_space_wraps(csbuf) &&
3903 flen + chklen <= b_room(csbuf) &&
3904 b_data(csbuf) <= MAX_DATA_REALIGN)) {
3905 /* it doesn't fit in a single block and the buffer is fragmented, if there are
3906 * not too many data in the buffer, let's defragment it and try
3907 * again.
Willy Tarreaud755ea62018-02-26 15:44:54 +01003908 */
3909 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003910 }
3911
Willy Tarreaueba10f22018-04-25 20:44:22 +02003912 /* chunked-encoding requires more room */
3913 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003914 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003915 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3916 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3917 (chklen < 1048576) ? 4 : 8;
3918 chklen += 4; // CRLF, CRLF
3919 }
3920
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003921 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003922 if (flen + chklen > b_room(csbuf)) {
3923 if (chklen >= b_room(csbuf)) {
3924 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003925 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003926 }
3927 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003928 }
3929
3930 if (h2s->flags & H2_SF_DATA_CHNK) {
3931 /* emit the chunk size */
3932 unsigned int chksz = flen;
3933 char str[10];
3934 char *beg;
3935
3936 beg = str + sizeof(str);
3937 *--beg = '\n';
3938 *--beg = '\r';
3939 do {
3940 *--beg = hextab[chksz & 0xF];
3941 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003942 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003943 }
3944
Willy Tarreau454f9052017-10-26 19:40:35 +02003945 /* Block1 is the length of the first block before the buffer wraps,
3946 * block2 is the optional second block to reach the end of the frame.
3947 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003948 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003949 if (block1 > flen)
3950 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003951 block2 = flen - block1;
3952
3953 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003954 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003955
3956 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003957 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003958
Willy Tarreaueba10f22018-04-25 20:44:22 +02003959 if (h2s->flags & H2_SF_DATA_CHNK) {
3960 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003961 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003962 }
3963
Willy Tarreau454f9052017-10-26 19:40:35 +02003964 /* now mark the input data as consumed (will be deleted from the buffer
3965 * by the caller when seeing FRAME_A after sending the window update).
3966 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003967 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003968 h2c->dfl -= flen;
3969 h2c->rcvd_c += flen;
3970 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3971
Willy Tarreau1915ca22019-01-24 11:49:37 +01003972 if (h2s->flags & H2_SF_DATA_CLEN)
3973 h2s->body_len -= flen;
3974
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003975 if (h2c->dfl > h2c->dpl) {
3976 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003977 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003978 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003979 }
3980
Willy Tarreau4a28da12018-01-04 14:41:00 +01003981 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003982 /* here we're done with the frame, all the payload (except padding) was
3983 * transferred.
3984 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003985
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003986 if (h2c->dff & H2_F_DATA_END_STREAM) {
3987 if (htx) {
3988 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3989 h2c->flags |= H2_CF_DEM_SFULL;
3990 goto fail;
3991 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003992 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003993 else if (h2s->flags & H2_SF_DATA_CHNK) {
3994 /* emit the trailing 0 CRLF CRLF */
3995 if (b_room(csbuf) < 5) {
3996 h2c->flags |= H2_CF_DEM_SFULL;
3997 goto fail;
3998 }
3999 chklen += 5;
4000 b_putblk(csbuf, "0\r\n\r\n", 5);
4001 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02004002 }
4003
Willy Tarreaud1023bb2018-03-22 16:53:12 +01004004 h2c->rcvd_c += h2c->dpl;
4005 h2c->rcvd_s += h2c->dpl;
4006 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004007 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004008 if (htx)
4009 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004010 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004011 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004012 if (htx)
4013 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004014 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004015}
4016
Willy Tarreau5dd17352018-06-14 13:33:30 +02004017/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
4018 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
4019 * number of bytes sent. The caller must check the stream's status to detect
4020 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004021 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004022static 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 +02004023{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004024 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004025 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004026 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004027 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004028 struct buffer *mbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004029 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004030 int es_now = 0;
4031 int ret = 0;
4032 int hdr;
4033
4034 if (h2c_mux_busy(h2c, h2s)) {
4035 h2s->flags |= H2_SF_BLK_MBUSY;
4036 return 0;
4037 }
4038
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004039 /* First, try to parse the H1 response and index it into <list>.
4040 * NOTE! Since it comes from haproxy, we *know* that a response header
4041 * block does not wrap and we can safely read it this way without
4042 * having to realign the buffer.
4043 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004044 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004045 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004046 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01004047 /* incomplete or invalid response, this is abnormal coming from
4048 * haproxy and may only result in a bad errorfile or bad Lua code
4049 * so that won't be fixed, raise an error now.
4050 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004051 * FIXME: we should instead add the ability to only return a
4052 * 502 bad gateway. But in theory this is not supposed to
4053 * happen.
4054 */
4055 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4056 ret = 0;
4057 goto end;
4058 }
4059
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004060 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02004061
4062 /* certain statuses have no body or an empty one, regardless of
4063 * what the headers say.
4064 */
4065 if (sl.st.status >= 100 && sl.st.status < 200) {
4066 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
4067 h1m->curr_len = h1m->body_len = 0;
4068 }
4069 else if (sl.st.status == 204 || sl.st.status == 304) {
4070 /* no contents, claim c-len is present and set to zero */
4071 h1m->flags &= ~H1_MF_CHNK;
4072 h1m->flags |= H1_MF_CLEN;
4073 h1m->curr_len = h1m->body_len = 0;
4074 }
4075
Willy Tarreaubcc45952019-05-26 10:05:50 +02004076 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004077 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004078 if (!h2_get_buf(h2c, mbuf)) {
4079 h2c->flags |= H2_CF_MUX_MALLOC;
4080 h2s->flags |= H2_SF_BLK_MROOM;
4081 return 0;
4082 }
4083
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004084 chunk_reset(&outbuf);
4085
4086 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004087 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4088 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004089 break;
4090 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004091 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004092 }
4093
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004094 if (outbuf.size < 9)
4095 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004096
4097 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004098 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4099 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4100 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004101
4102 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004103 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01004104 /* this is an unparsable response */
4105 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4106 ret = 0;
4107 goto end;
4108 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01004109
4110 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004111 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004112 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004113 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004114 }
4115
4116 /* encode all headers, stop at empty name */
4117 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01004118 /* these ones do not exist in H2 and must be dropped. */
4119 if (isteq(list[hdr].n, ist("connection")) ||
4120 isteq(list[hdr].n, ist("proxy-connection")) ||
4121 isteq(list[hdr].n, ist("keep-alive")) ||
4122 isteq(list[hdr].n, ist("upgrade")) ||
4123 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004124 continue;
4125
4126 if (isteq(list[hdr].n, ist("")))
4127 break; // end
4128
4129 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4130 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004131 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004132 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004133 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004134 }
4135 }
4136
4137 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01004138 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004139 es_now = 1;
4140
4141 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004142 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004143
4144 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004145 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004146
4147 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004148 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004149
4150 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004151 b_add(mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01004152 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004153
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004154 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01004155 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004156 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01004157
Willy Tarreau801250e2018-09-11 11:45:04 +02004158 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004159 h2s->flags |= H2_SF_ES_SENT;
4160 if (h2s->st == H2_SS_OPEN)
4161 h2s->st = H2_SS_HLOC;
4162 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004163 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004164 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004165 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004166 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004167 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004168 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004169 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004170 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004171 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004172
4173 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004174
4175 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004176 //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 +02004177 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004178 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004179 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4180 goto retry;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004181 h1m_init_res(h1m);
4182 h1m->err_pos = -1; // don't care about errors on the response path
4183 h2c->flags |= H2_CF_MUX_MFULL;
4184 h2s->flags |= H2_SF_BLK_MROOM;
4185 ret = 0;
4186 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004187}
4188
Willy Tarreau5dd17352018-06-14 13:33:30 +02004189/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4190 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4191 * the number of bytes sent. The caller must check the stream's status to
4192 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004193 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004194static 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 +02004195{
4196 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004197 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004198 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004199 struct buffer *mbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004200 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004201 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004202 int es_now = 0;
4203 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004204 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004205 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004206
4207 if (h2c_mux_busy(h2c, h2s)) {
4208 h2s->flags |= H2_SF_BLK_MBUSY;
4209 goto end;
4210 }
4211
Willy Tarreaubcc45952019-05-26 10:05:50 +02004212 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004213 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004214 if (!h2_get_buf(h2c, mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004215 h2c->flags |= H2_CF_MUX_MALLOC;
4216 h2s->flags |= H2_SF_BLK_MROOM;
4217 goto end;
4218 }
4219
4220 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004221 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004222 goto end;
4223
4224 chunk_reset(&outbuf);
4225
4226 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004227 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4228 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004229 break;
4230 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004231 /* If there are pending data in the output buffer, and we have
4232 * less than 1/4 of the mbuf's size and everything fits, we'll
4233 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4234 * is full and wait, to save some slow realign calls.
4235 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004236 if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004237 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4238 goto retry;
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004239 h2c->flags |= H2_CF_MUX_MFULL;
4240 h2s->flags |= H2_SF_BLK_MROOM;
4241 goto end;
4242 }
4243
Willy Tarreaubcc45952019-05-26 10:05:50 +02004244 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004245 }
4246
4247 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004248 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4249 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004250 h2c->flags |= H2_CF_MUX_MFULL;
4251 h2s->flags |= H2_SF_BLK_MROOM;
4252 goto end;
4253 }
4254
4255 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004256 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4257 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4258 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004259
4260 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4261 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004262 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004263 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004264 break;
4265 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004266 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004267 if ((long long)size > h1m->curr_len)
4268 size = h1m->curr_len;
4269 break;
4270 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004271 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004272 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004273 if (!ret)
4274 goto end;
4275
4276 if (ret < 0) {
4277 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004278 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004279 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4280 goto end;
4281 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004282 max -= ret;
4283 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004284 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004285 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004286 }
4287
Willy Tarreau801250e2018-09-11 11:45:04 +02004288 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004289 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004290 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004291 if (!ret)
4292 goto end;
4293
4294 if (ret < 0) {
4295 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004296 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004297 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4298 goto end;
4299 }
4300
4301 size = chunk;
4302 h1m->curr_len = chunk;
4303 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004304 max -= ret;
4305 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004306 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004307 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004308 if (!size)
4309 goto send_empty;
4310 }
4311
4312 /* in MSG_DATA state, continue below */
4313 size = h1m->curr_len;
4314 break;
4315 }
4316
4317 /* we have in <size> the exact number of bytes we need to copy from
4318 * the H1 buffer. We need to check this against the connection's and
4319 * the stream's send windows, and to ensure that this fits in the max
4320 * frame size and in the buffer's available space minus 9 bytes (for
4321 * the frame header). The connection's flow control is applied last so
4322 * that we can use a separate list of streams which are immediately
4323 * unblocked on window opening. Note: we don't implement padding.
4324 */
4325
Willy Tarreau5dd17352018-06-14 13:33:30 +02004326 if (size > max)
4327 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004328
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004329 if (size > h2s_mws(h2s))
4330 size = h2s_mws(h2s);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004331
4332 if (size <= 0) {
4333 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004334 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004335 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004336 goto end;
4337 }
4338
4339 if (h2c->mfs && size > h2c->mfs)
4340 size = h2c->mfs;
4341
4342 if (size + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02004343 /* It doesn't fit at once. If it at least fits once split and
4344 * the amount of data to move is low, let's defragment the
4345 * buffer now.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004346 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004347 if (b_space_wraps(mbuf) &&
4348 (size + 9 <= b_room(mbuf)) &&
4349 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004350 goto realign_again;
4351 size = outbuf.size - 9;
4352 }
4353
4354 if (size <= 0) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004355 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4356 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004357 h2c->flags |= H2_CF_MUX_MFULL;
4358 h2s->flags |= H2_SF_BLK_MROOM;
4359 goto end;
4360 }
4361
4362 if (size > h2c->mws)
4363 size = h2c->mws;
4364
4365 if (size <= 0) {
4366 h2s->flags |= H2_SF_BLK_MFCTL;
4367 goto end;
4368 }
4369
4370 /* copy whatever we can */
4371 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004372 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004373 if (ret == 1)
4374 len2 = 0;
4375
4376 if (!ret || len1 + len2 < size) {
4377 /* FIXME: must normally never happen */
4378 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4379 goto end;
4380 }
4381
4382 /* limit len1/len2 to size */
4383 if (len1 + len2 > size) {
4384 int sub = len1 + len2 - size;
4385
4386 if (len2 > sub)
4387 len2 -= sub;
4388 else {
4389 sub -= len2;
4390 len2 = 0;
4391 len1 -= sub;
4392 }
4393 }
4394
4395 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004396 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004397 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004398 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004399
4400 send_empty:
4401 /* we may need to add END_STREAM */
4402 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4403 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004404 *
4405 * FIXME: what we do here is not correct because we send end_stream
4406 * before knowing if we'll have to send a HEADERS frame for the
4407 * trailers. More importantly we're not consuming the trailing CRLF
4408 * after the end of trailers, so it will be left to the caller to
4409 * eat it. The right way to do it would be to measure trailers here
4410 * and to send ES only if there are no trailers.
4411 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004412 */
4413 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004414 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004415 es_now = 1;
4416
4417 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004418 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004419
4420 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004421 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004422
4423 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004424 b_add(mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004425
4426 /* consume incoming H1 response */
4427 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004428 max -= size;
4429 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004430 total += size;
4431 h1m->curr_len -= size;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004432 h2s->sws -= size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004433 h2c->mws -= size;
4434
4435 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004436 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004437 goto new_frame;
4438 }
4439 }
4440
4441 if (es_now) {
4442 if (h2s->st == H2_SS_OPEN)
4443 h2s->st = H2_SS_HLOC;
4444 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004445 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004446
Willy Tarreau35a62702018-02-27 15:37:25 +01004447 if (!(h1m->flags & H1_MF_CHNK)) {
4448 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004449 total += max;
4450 ofs += max;
4451 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004452
Willy Tarreau801250e2018-09-11 11:45:04 +02004453 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004454 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004455
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004456 h2s->flags |= H2_SF_ES_SENT;
4457 }
4458
4459 end:
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004460 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 +02004461 return total;
4462}
4463
Willy Tarreau115e83b2018-12-01 19:17:53 +01004464/* Try to send a HEADERS frame matching HTX response present in HTX message
4465 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4466 * must check the stream's status to detect any error which might have happened
4467 * subsequently to a successful send. The htx blocks are automatically removed
4468 * from the message. The htx message is assumed to be valid since produced from
4469 * the internal code, hence it contains a start line, an optional series of
4470 * header blocks and an end of header, otherwise an invalid frame could be
4471 * emitted and the resulting htx message could be left in an inconsistent state.
4472 */
4473static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4474{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004475 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004476 struct h2c *h2c = h2s->h2c;
4477 struct htx_blk *blk;
4478 struct htx_blk *blk_end;
4479 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004480 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004481 struct htx_sl *sl;
4482 enum htx_blk_type type;
4483 int es_now = 0;
4484 int ret = 0;
4485 int hdr;
4486 int idx;
4487
4488 if (h2c_mux_busy(h2c, h2s)) {
4489 h2s->flags |= H2_SF_BLK_MBUSY;
4490 return 0;
4491 }
4492
Willy Tarreau115e83b2018-12-01 19:17:53 +01004493 /* determine the first block which must not be deleted, blk_end may
4494 * be NULL if all blocks have to be deleted.
4495 */
4496 idx = htx_get_head(htx);
4497 blk_end = NULL;
4498 while (idx != -1) {
4499 type = htx_get_blk_type(htx_get_blk(htx, idx));
4500 idx = htx_get_next(htx, idx);
4501 if (type == HTX_BLK_EOH) {
4502 if (idx != -1)
4503 blk_end = htx_get_blk(htx, idx);
4504 break;
4505 }
4506 }
4507
4508 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004509 blk = htx_get_head_blk(htx);
4510 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL);
4511 ALREADY_CHECKED(blk);
4512 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004513 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004514 if (h2s->status < 100 || h2s->status > 999)
4515 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004516
4517 /* and the rest of the headers, that we dump starting at header 0 */
4518 hdr = 0;
4519
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004520 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004521 while ((idx = htx_get_next(htx, idx)) != -1) {
4522 blk = htx_get_blk(htx, idx);
4523 type = htx_get_blk_type(blk);
4524
4525 if (type == HTX_BLK_UNUSED)
4526 continue;
4527
4528 if (type != HTX_BLK_HDR)
4529 break;
4530
4531 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4532 goto fail;
4533
4534 list[hdr].n = htx_get_blk_name(htx, blk);
4535 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004536 hdr++;
4537 }
4538
4539 /* marker for end of headers */
4540 list[hdr].n = ist("");
4541
4542 if (h2s->status == 204 || h2s->status == 304) {
4543 /* no contents, claim c-len is present and set to zero */
4544 es_now = 1;
4545 }
4546
Willy Tarreau9c218e72019-05-26 10:08:28 +02004547 mbuf = br_tail(h2c->mbuf);
4548 retry:
4549 if (!h2_get_buf(h2c, mbuf)) {
4550 h2c->flags |= H2_CF_MUX_MALLOC;
4551 h2s->flags |= H2_SF_BLK_MROOM;
4552 return 0;
4553 }
4554
Willy Tarreau115e83b2018-12-01 19:17:53 +01004555 chunk_reset(&outbuf);
4556
4557 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004558 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4559 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004560 break;
4561 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004562 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004563 }
4564
4565 if (outbuf.size < 9)
4566 goto full;
4567
4568 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4569 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4570 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4571 outbuf.data = 9;
4572
4573 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004574 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004575 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004576 goto realign_again;
4577 goto full;
4578 }
4579
4580 /* encode all headers, stop at empty name */
4581 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4582 /* these ones do not exist in H2 and must be dropped. */
4583 if (isteq(list[hdr].n, ist("connection")) ||
4584 isteq(list[hdr].n, ist("proxy-connection")) ||
4585 isteq(list[hdr].n, ist("keep-alive")) ||
4586 isteq(list[hdr].n, ist("upgrade")) ||
4587 isteq(list[hdr].n, ist("transfer-encoding")))
4588 continue;
4589
4590 if (isteq(list[hdr].n, ist("")))
4591 break; // end
4592
4593 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4594 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004595 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004596 goto realign_again;
4597 goto full;
4598 }
4599 }
4600
Christopher Faulet0b465482019-02-19 15:14:23 +01004601 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004602 * FIXME: we should also set it when we know for sure that the
4603 * content-length is zero as well as on 204/304
4604 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004605 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4606 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004607 es_now = 1;
4608
Willy Tarreau927b88b2019-03-04 08:03:25 +01004609 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004610 es_now = 1;
4611
4612 /* update the frame's size */
4613 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4614
4615 if (es_now)
4616 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4617
4618 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004619 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004620
4621 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4622 * 1xx responses, another HEADERS frame is expected.
4623 */
4624 if (h2s->status >= 200 || h2s->status == 101)
4625 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004626
Willy Tarreau115e83b2018-12-01 19:17:53 +01004627 if (es_now) {
4628 h2s->flags |= H2_SF_ES_SENT;
4629 if (h2s->st == H2_SS_OPEN)
4630 h2s->st = H2_SS_HLOC;
4631 else
4632 h2s_close(h2s);
4633 }
4634
4635 /* OK we could properly deliver the response */
4636
4637 /* remove all header blocks including the EOH and compute the
4638 * corresponding size.
4639 *
4640 * FIXME: We should remove everything when es_now is set.
4641 */
4642 ret = 0;
4643 idx = htx_get_head(htx);
4644 blk = htx_get_blk(htx, idx);
4645 while (blk != blk_end) {
4646 ret += htx_get_blksz(blk);
4647 blk = htx_remove_blk(htx, blk);
4648 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004649
Christopher Faulet316934d2019-05-15 14:22:48 +02004650 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4651 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004652 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004653 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004654 end:
4655 return ret;
4656 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004657 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4658 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004659 h2c->flags |= H2_CF_MUX_MFULL;
4660 h2s->flags |= H2_SF_BLK_MROOM;
4661 ret = 0;
4662 goto end;
4663 fail:
4664 /* unparsable HTX messages, too large ones to be produced in the local
4665 * list etc go here (unrecoverable errors).
4666 */
4667 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4668 ret = 0;
4669 goto end;
4670}
4671
Willy Tarreau80739692018-10-05 11:35:57 +02004672/* Try to send a HEADERS frame matching HTX request present in HTX message
4673 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4674 * must check the stream's status to detect any error which might have happened
4675 * subsequently to a successful send. The htx blocks are automatically removed
4676 * from the message. The htx message is assumed to be valid since produced from
4677 * the internal code, hence it contains a start line, an optional series of
4678 * header blocks and an end of header, otherwise an invalid frame could be
4679 * emitted and the resulting htx message could be left in an inconsistent state.
4680 */
4681static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4682{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004683 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004684 struct h2c *h2c = h2s->h2c;
4685 struct htx_blk *blk;
4686 struct htx_blk *blk_end;
4687 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004688 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004689 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004690 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004691 enum htx_blk_type type;
4692 int es_now = 0;
4693 int ret = 0;
4694 int hdr;
4695 int idx;
4696
4697 if (h2c_mux_busy(h2c, h2s)) {
4698 h2s->flags |= H2_SF_BLK_MBUSY;
4699 return 0;
4700 }
4701
Willy Tarreau80739692018-10-05 11:35:57 +02004702 /* determine the first block which must not be deleted, blk_end may
4703 * be NULL if all blocks have to be deleted.
4704 */
4705 idx = htx_get_head(htx);
4706 blk_end = NULL;
4707 while (idx != -1) {
4708 type = htx_get_blk_type(htx_get_blk(htx, idx));
4709 idx = htx_get_next(htx, idx);
4710 if (type == HTX_BLK_EOH) {
4711 if (idx != -1)
4712 blk_end = htx_get_blk(htx, idx);
4713 break;
4714 }
4715 }
4716
4717 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004718 blk = htx_get_head_blk(htx);
4719 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
4720 ALREADY_CHECKED(blk);
4721 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004722 meth = htx_sl_req_meth(sl);
4723 path = htx_sl_req_uri(sl);
4724
4725 /* and the rest of the headers, that we dump starting at header 0 */
4726 hdr = 0;
4727
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004728 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004729 while ((idx = htx_get_next(htx, idx)) != -1) {
4730 blk = htx_get_blk(htx, idx);
4731 type = htx_get_blk_type(blk);
4732
4733 if (type == HTX_BLK_UNUSED)
4734 continue;
4735
4736 if (type != HTX_BLK_HDR)
4737 break;
4738
4739 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4740 goto fail;
4741
4742 list[hdr].n = htx_get_blk_name(htx, blk);
4743 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004744 hdr++;
4745 }
4746
4747 /* marker for end of headers */
4748 list[hdr].n = ist("");
4749
Willy Tarreau9c218e72019-05-26 10:08:28 +02004750 mbuf = br_tail(h2c->mbuf);
4751 retry:
4752 if (!h2_get_buf(h2c, mbuf)) {
4753 h2c->flags |= H2_CF_MUX_MALLOC;
4754 h2s->flags |= H2_SF_BLK_MROOM;
4755 return 0;
4756 }
4757
Willy Tarreau80739692018-10-05 11:35:57 +02004758 chunk_reset(&outbuf);
4759
4760 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004761 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4762 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004763 break;
4764 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004765 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004766 }
4767
4768 if (outbuf.size < 9)
4769 goto full;
4770
4771 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4772 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4773 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4774 outbuf.data = 9;
4775
4776 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004777 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004778 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004779 goto realign_again;
4780 goto full;
4781 }
4782
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004783 /* RFC7540 #8.3: the CONNECT method must have :
4784 * - :authority set to the URI part (host:port)
4785 * - :method set to CONNECT
4786 * - :scheme and :path omitted
4787 */
4788 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4789 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004790 struct ist scheme;
4791
4792 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4793 scheme = ist("http");
4794 else
4795 scheme = ist("https");
4796
4797 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004798 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004799 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004800 goto realign_again;
4801 goto full;
4802 }
Willy Tarreau80739692018-10-05 11:35:57 +02004803
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004804 /* encode the path, which necessarily is the second one */
4805 if (!hpack_encode_path(&outbuf, path)) {
4806 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004807 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004808 goto realign_again;
4809 goto full;
4810 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004811
4812 /* look for the Host header and place it in :authority */
4813 auth = ist2(NULL, 0);
4814 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4815 if (isteq(list[hdr].n, ist("")))
4816 break; // end
4817
4818 if (isteq(list[hdr].n, ist("host"))) {
4819 auth = list[hdr].v;
4820 break;
4821 }
4822 }
4823 }
4824 else {
4825 /* for CONNECT, :authority is taken from the path */
4826 auth = path;
4827 }
4828
4829 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4830 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004831 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004832 goto realign_again;
4833 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004834 }
4835
4836 /* encode all headers, stop at empty name */
4837 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4838 /* these ones do not exist in H2 and must be dropped. */
4839 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004840 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004841 isteq(list[hdr].n, ist("proxy-connection")) ||
4842 isteq(list[hdr].n, ist("keep-alive")) ||
4843 isteq(list[hdr].n, ist("upgrade")) ||
4844 isteq(list[hdr].n, ist("transfer-encoding")))
4845 continue;
4846
4847 if (isteq(list[hdr].n, ist("")))
4848 break; // end
4849
4850 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4851 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004852 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004853 goto realign_again;
4854 goto full;
4855 }
4856 }
4857
4858 /* we may need to add END_STREAM if we have no body :
4859 * - request already closed, or :
4860 * - no transfer-encoding, and :
4861 * - no content-length or content-length:0
4862 * Fixme: this doesn't take into account CONNECT requests.
4863 */
4864 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4865 es_now = 1;
4866
4867 if (sl->flags & HTX_SL_F_BODYLESS)
4868 es_now = 1;
4869
Willy Tarreau927b88b2019-03-04 08:03:25 +01004870 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004871 es_now = 1;
4872
4873 /* update the frame's size */
4874 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4875
4876 if (es_now)
4877 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4878
4879 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004880 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004881 h2s->flags |= H2_SF_HEADERS_SENT;
4882 h2s->st = H2_SS_OPEN;
4883
Willy Tarreau80739692018-10-05 11:35:57 +02004884 if (es_now) {
4885 // trim any possibly pending data (eg: inconsistent content-length)
4886 h2s->flags |= H2_SF_ES_SENT;
4887 h2s->st = H2_SS_HLOC;
4888 }
4889
4890 /* remove all header blocks including the EOH and compute the
4891 * corresponding size.
4892 *
4893 * FIXME: We should remove everything when es_now is set.
4894 */
4895 ret = 0;
4896 idx = htx_get_head(htx);
4897 blk = htx_get_blk(htx, idx);
4898 while (blk != blk_end) {
4899 ret += htx_get_blksz(blk);
4900 blk = htx_remove_blk(htx, blk);
4901 }
4902
Christopher Faulet316934d2019-05-15 14:22:48 +02004903 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4904 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004905 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004906 }
Willy Tarreau80739692018-10-05 11:35:57 +02004907
4908 end:
4909 return ret;
4910 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004911 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4912 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004913 h2c->flags |= H2_CF_MUX_MFULL;
4914 h2s->flags |= H2_SF_BLK_MROOM;
4915 ret = 0;
4916 goto end;
4917 fail:
4918 /* unparsable HTX messages, too large ones to be produced in the local
4919 * list etc go here (unrecoverable errors).
4920 */
4921 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4922 ret = 0;
4923 goto end;
4924}
4925
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004926/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004927 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4928 * caller must check the stream's status to detect any error which might have
4929 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02004930 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004931 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004932static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004933{
4934 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004935 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004936 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004937 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004938 size_t total = 0;
4939 int es_now = 0;
4940 int bsize; /* htx block size */
4941 int fsize; /* h2 frame size */
4942 struct htx_blk *blk;
4943 enum htx_blk_type type;
4944 int idx;
4945
4946 if (h2c_mux_busy(h2c, h2s)) {
4947 h2s->flags |= H2_SF_BLK_MBUSY;
4948 goto end;
4949 }
4950
Willy Tarreau98de12a2018-12-12 07:03:00 +01004951 htx = htx_from_buf(buf);
4952
Christopher Faulet54b5e212019-06-04 10:08:28 +02004953 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
4954 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
4955 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004956 */
4957
4958 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004959 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004960 goto end;
4961
4962 idx = htx_get_head(htx);
4963 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02004964 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004965 bsize = htx_get_blksz(blk);
4966 fsize = bsize;
4967
Christopher Faulet54b5e212019-06-04 10:08:28 +02004968 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004969 if (h2s->flags & H2_SF_ES_SENT) {
4970 /* ES already sent */
4971 htx_remove_blk(htx, blk);
4972 total++; // EOM counts as one byte
4973 count--;
4974 goto end;
4975 }
4976 }
4977 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004978 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004979
Willy Tarreau9c218e72019-05-26 10:08:28 +02004980 mbuf = br_tail(h2c->mbuf);
4981 retry:
4982 if (!h2_get_buf(h2c, mbuf)) {
4983 h2c->flags |= H2_CF_MUX_MALLOC;
4984 h2s->flags |= H2_SF_BLK_MROOM;
4985 goto end;
4986 }
4987
Willy Tarreau98de12a2018-12-12 07:03:00 +01004988 /* Perform some optimizations to reduce the number of buffer copies.
4989 * First, if the mux's buffer is empty and the htx area contains
4990 * exactly one data block of the same size as the requested count, and
4991 * this count fits within the frame size, the stream's window size, and
4992 * the connection's window size, then it's possible to simply swap the
4993 * caller's buffer with the mux's output buffer and adjust offsets and
4994 * length to match the entire DATA HTX block in the middle. In this
4995 * case we perform a true zero-copy operation from end-to-end. This is
4996 * the situation that happens all the time with large files. Second, if
4997 * this is not possible, but the mux's output buffer is empty, we still
4998 * have an opportunity to avoid the copy to the intermediary buffer, by
4999 * making the intermediary buffer's area point to the output buffer's
5000 * area. In this case we want to skip the HTX header to make sure that
5001 * copies remain aligned and that this operation remains possible all
5002 * the time. This goes for headers, data blocks and any data extracted
5003 * from the HTX blocks.
5004 */
5005 if (unlikely(fsize == count &&
5006 htx->used == 1 && type == HTX_BLK_DATA &&
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005007 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005008 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005009
Willy Tarreaubcc45952019-05-26 10:05:50 +02005010 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005011 /* Too bad there are data left there. We're willing to memcpy/memmove
5012 * up to 1/4 of the buffer, which means that it's OK to copy a large
5013 * frame into a buffer containing few data if it needs to be realigned,
5014 * and that it's also OK to copy few data without realigning. Otherwise
5015 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01005016 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005017 if (fsize + 9 <= b_room(mbuf) &&
5018 (b_data(mbuf) <= b_size(mbuf) / 4 ||
5019 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01005020 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005021
Willy Tarreau9c218e72019-05-26 10:08:28 +02005022 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5023 goto retry;
5024
Willy Tarreau98de12a2018-12-12 07:03:00 +01005025 h2c->flags |= H2_CF_MUX_MFULL;
5026 h2s->flags |= H2_SF_BLK_MROOM;
5027 goto end;
5028 }
5029
5030 /* map an H2 frame to the HTX block so that we can put the
5031 * frame header there.
5032 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005033 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
5034 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005035
5036 /* prepend an H2 DATA frame header just before the DATA block */
5037 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5038 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5039 h2_set_frame_size(outbuf.area, fsize);
5040
5041 /* update windows */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005042 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005043 h2c->mws -= fsize;
5044
5045 /* and exchange with our old area */
5046 buf->area = old_area;
5047 buf->data = buf->head = 0;
5048 total += fsize;
5049 goto end;
5050 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005051
Willy Tarreau98de12a2018-12-12 07:03:00 +01005052 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005053 /* for DATA and EOM we'll have to emit a frame, even if empty */
5054
5055 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005056 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5057 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005058 break;
5059 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005060 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005061 }
5062
5063 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005064 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5065 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005066 h2c->flags |= H2_CF_MUX_MFULL;
5067 h2s->flags |= H2_SF_BLK_MROOM;
5068 goto end;
5069 }
5070
5071 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5072 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5073 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5074 outbuf.data = 9;
5075
5076 /* we have in <fsize> the exact number of bytes we need to copy from
5077 * the HTX buffer. We need to check this against the connection's and
5078 * the stream's send windows, and to ensure that this fits in the max
5079 * frame size and in the buffer's available space minus 9 bytes (for
5080 * the frame header). The connection's flow control is applied last so
5081 * that we can use a separate list of streams which are immediately
5082 * unblocked on window opening. Note: we don't implement padding.
5083 */
5084
5085 /* EOM is presented with bsize==1 but would lead to the emission of an
5086 * empty frame, thus we force it to zero here.
5087 */
5088 if (type == HTX_BLK_EOM)
5089 bsize = fsize = 0;
5090
5091 if (!fsize)
5092 goto send_empty;
5093
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005094 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005095 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005096 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005097 LIST_DEL_INIT(&h2s->list);
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005098 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005099 goto end;
5100 }
5101
Willy Tarreauee573762018-12-04 15:25:57 +01005102 if (fsize > count)
5103 fsize = count;
5104
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005105 if (fsize > h2s_mws(h2s))
5106 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005107
5108 if (h2c->mfs && fsize > h2c->mfs)
5109 fsize = h2c->mfs; // >0
5110
5111 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005112 /* It doesn't fit at once. If it at least fits once split and
5113 * the amount of data to move is low, let's defragment the
5114 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005115 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005116 if (b_space_wraps(mbuf) &&
5117 (fsize + 9 <= b_room(mbuf)) &&
5118 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005119 goto realign_again;
5120 fsize = outbuf.size - 9;
5121
5122 if (fsize <= 0) {
5123 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005124 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5125 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005126 h2c->flags |= H2_CF_MUX_MFULL;
5127 h2s->flags |= H2_SF_BLK_MROOM;
5128 goto end;
5129 }
5130 }
5131
5132 if (h2c->mws <= 0) {
5133 h2s->flags |= H2_SF_BLK_MFCTL;
5134 goto end;
5135 }
5136
5137 if (fsize > h2c->mws)
5138 fsize = h2c->mws;
5139
5140 /* now let's copy this this into the output buffer */
5141 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005142 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005143 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005144 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005145
5146 send_empty:
5147 /* update the frame's size */
5148 h2_set_frame_size(outbuf.area, fsize);
5149
5150 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5151 * meeting EOM. We should optimize this later.
5152 */
5153 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005154 total++; // EOM counts as one byte
5155 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005156 es_now = 1;
5157 }
5158
5159 if (es_now)
5160 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5161
5162 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005163 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005164
5165 /* consume incoming HTX block, including EOM */
5166 total += fsize;
5167 if (fsize == bsize) {
5168 htx_remove_blk(htx, blk);
5169 if (fsize)
5170 goto new_frame;
5171 } else {
5172 /* we've truncated this block */
5173 htx_cut_data_blk(htx, blk, fsize);
5174 }
5175
5176 if (es_now) {
5177 if (h2s->st == H2_SS_OPEN)
5178 h2s->st = H2_SS_HLOC;
5179 else
5180 h2s_close(h2s);
5181
5182 h2s->flags |= H2_SF_ES_SENT;
5183 }
5184
5185 end:
5186 return total;
5187}
5188
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005189/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5190 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5191 * processed. The caller must check the stream's status to detect any error
5192 * which might have happened subsequently to a successful send. The htx blocks
5193 * are automatically removed from the message. The htx message is assumed to be
5194 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005195 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005196 * as a single frame. The ES flag is always set.
5197 */
5198static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5199{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005200 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005201 struct h2c *h2c = h2s->h2c;
5202 struct htx_blk *blk;
5203 struct htx_blk *blk_end;
5204 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005205 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005206 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005207 int ret = 0;
5208 int hdr;
5209 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005210
5211 if (h2c_mux_busy(h2c, h2s)) {
5212 h2s->flags |= H2_SF_BLK_MBUSY;
5213 goto end;
5214 }
5215
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005216 /* determine the first block which must not be deleted, blk_end may
5217 * be NULL if all blocks have to be deleted. also get trailers.
5218 */
5219 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005220 blk_end = NULL;
5221
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005222 hdr = 0;
5223 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005224 blk = htx_get_blk(htx, idx);
5225 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005226 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005227 if (type == HTX_BLK_UNUSED)
5228 continue;
5229
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005230 if (type == HTX_BLK_EOT) {
5231 if (idx != -1)
5232 blk_end = blk;
5233 break;
5234 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005235 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005236 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005237
5238 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5239 goto fail;
5240
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005241 list[hdr].n = htx_get_blk_name(htx, blk);
5242 list[hdr].v = htx_get_blk_value(htx, blk);
5243 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005244 }
5245
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005246 /* marker for end of trailers */
5247 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005248
Willy Tarreau9c218e72019-05-26 10:08:28 +02005249 mbuf = br_tail(h2c->mbuf);
5250 retry:
5251 if (!h2_get_buf(h2c, mbuf)) {
5252 h2c->flags |= H2_CF_MUX_MALLOC;
5253 h2s->flags |= H2_SF_BLK_MROOM;
5254 goto end;
5255 }
5256
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005257 chunk_reset(&outbuf);
5258
5259 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005260 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5261 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005262 break;
5263 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005264 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005265 }
5266
5267 if (outbuf.size < 9)
5268 goto full;
5269
5270 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5271 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5272 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5273 outbuf.data = 9;
5274
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005275 /* encode all headers */
5276 for (idx = 0; idx < hdr; idx++) {
5277 /* these ones do not exist in H2 or must not appear in
5278 * trailers and must be dropped.
5279 */
5280 if (isteq(list[idx].n, ist("host")) ||
5281 isteq(list[idx].n, ist("content-length")) ||
5282 isteq(list[idx].n, ist("connection")) ||
5283 isteq(list[idx].n, ist("proxy-connection")) ||
5284 isteq(list[idx].n, ist("keep-alive")) ||
5285 isteq(list[idx].n, ist("upgrade")) ||
5286 isteq(list[idx].n, ist("te")) ||
5287 isteq(list[idx].n, ist("transfer-encoding")))
5288 continue;
5289
5290 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5291 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005292 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005293 goto realign_again;
5294 goto full;
5295 }
5296 }
5297
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005298 if (outbuf.data == 9) {
5299 /* here we have a problem, we have nothing to emit (either we
5300 * received an empty trailers block followed or we removed its
5301 * contents above). Because of this we can't send a HEADERS
5302 * frame, so we have to cheat and instead send an empty DATA
5303 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005304 */
5305 outbuf.area[3] = H2_FT_DATA;
5306 outbuf.area[4] = H2_F_DATA_END_STREAM;
5307 }
5308
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005309 /* update the frame's size */
5310 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5311
5312 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005313 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005314 h2s->flags |= H2_SF_ES_SENT;
5315
5316 if (h2s->st == H2_SS_OPEN)
5317 h2s->st = H2_SS_HLOC;
5318 else
5319 h2s_close(h2s);
5320
5321 /* OK we could properly deliver the response */
5322 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005323 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005324 ret = 0;
5325 idx = htx_get_head(htx);
5326 blk = htx_get_blk(htx, idx);
5327 while (blk != blk_end) {
5328 ret += htx_get_blksz(blk);
5329 blk = htx_remove_blk(htx, blk);
5330 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005331
5332 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5333 ret += htx_get_blksz(blk_end);
5334 htx_remove_blk(htx, blk_end);
5335 }
5336
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005337 end:
5338 return ret;
5339 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005340 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5341 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005342 h2c->flags |= H2_CF_MUX_MFULL;
5343 h2s->flags |= H2_SF_BLK_MROOM;
5344 ret = 0;
5345 goto end;
5346 fail:
5347 /* unparsable HTX messages, too large ones to be produced in the local
5348 * list etc go here (unrecoverable errors).
5349 */
5350 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5351 ret = 0;
5352 goto end;
5353}
5354
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005355/* Called from the upper layer, to subscribe to events, such as being able to send.
5356 * The <param> argument here is supposed to be a pointer to a wait_event struct
5357 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5358 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5359 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5360 * returns 0 except for the error above.
5361 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005362static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5363{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005364 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005365 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005366 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005367
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005368 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005369 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005370 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5371 sw->events |= SUB_RETRY_RECV;
5372 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005373 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005374 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005375 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005376 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005377 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5378 sw->events |= SUB_RETRY_SEND;
5379 h2s->send_wait = sw;
5380 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5381 !LIST_ADDED(&h2s->list)) {
5382 if (h2s->flags & H2_SF_BLK_MFCTL)
5383 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5384 else
5385 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005386 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005387 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005388 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005389 if (event_type != 0)
5390 return -1;
5391 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005392}
5393
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005394/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5395 * The <param> argument here is supposed to be a pointer to the same wait_event
5396 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5397 * It always returns zero.
5398 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005399static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5400{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005401 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005402 struct h2s *h2s = cs->ctx;
5403
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005404 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005405 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005406 BUG_ON(h2s->recv_wait != sw);
5407 sw->events &= ~SUB_RETRY_RECV;
5408 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005409 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005410 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005411 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005412 BUG_ON(h2s->send_wait != sw);
5413 LIST_DEL(&h2s->list);
5414 LIST_INIT(&h2s->list);
5415 sw->events &= ~SUB_RETRY_SEND;
5416 /* We were about to send, make sure it does not happen */
5417 if (LIST_ADDED(&h2s->sending_list) &&
5418 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02005419 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02005420 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005421 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005422 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005423 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005424 return 0;
5425}
5426
5427
Olivier Houchard511efea2018-08-16 15:30:32 +02005428/* Called from the upper layer, to receive data */
5429static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5430{
Olivier Houchard638b7992018-08-16 15:41:52 +02005431 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005432 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005433 struct htx *h2s_htx = NULL;
5434 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005435 size_t ret = 0;
5436
5437 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005438 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau86724e22018-12-01 23:19:43 +01005439 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005440 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005441 /* Here htx_to_buf() will set buffer data to 0 because
5442 * the HTX is empty.
5443 */
5444 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005445 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005446 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005447
Christopher Faulet156852b2019-05-16 11:29:13 +02005448 ret = h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005449 buf_htx = htx_from_buf(buf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005450
Christopher Faulet2f6edc82019-05-15 10:07:59 +02005451 /* <buf> is empty and the message is small enough, swap the
5452 * buffers. */
5453 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
5454 htx_to_buf(buf_htx, buf);
5455 htx_to_buf(h2s_htx, &h2s->rxbuf);
5456 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5457 goto end;
5458 }
5459
Christopher Faulet156852b2019-05-16 11:29:13 +02005460 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005461
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005462 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
Willy Tarreau7196dd62019-03-05 10:51:11 +01005463 buf_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005464 if (htx_is_empty(buf_htx))
5465 cs->flags |= CS_FL_EOI;
5466 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005467
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005468 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005469 htx_to_buf(buf_htx, buf);
5470 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet156852b2019-05-16 11:29:13 +02005471 ret -= h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005472 }
5473 else {
5474 ret = b_xfer(buf, &h2s->rxbuf, count);
5475 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005476
Christopher Faulet37070b22019-02-14 15:12:14 +01005477 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005478 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005479 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005480 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005481 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005482 if (h2s->flags & H2_SF_ES_RCVD)
5483 cs->flags |= CS_FL_EOI;
Willy Tarreau76c83822019-06-15 09:55:50 +02005484 if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02005485 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005486 if (cs->flags & CS_FL_ERR_PENDING)
5487 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005488 if (b_size(&h2s->rxbuf)) {
5489 b_free(&h2s->rxbuf);
5490 offer_buffers(NULL, tasks_run_queue);
5491 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005492 }
5493
Willy Tarreau082f5592018-11-25 08:03:32 +01005494 if (ret && h2c->dsi == h2s->id) {
5495 /* demux is blocking on this stream's buffer */
5496 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005497 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005498 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005499
Olivier Houchard511efea2018-08-16 15:30:32 +02005500 return ret;
5501}
5502
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005503/* stops all senders of this connection for example when the mux buffer is full.
5504 * They are moved from the sending_list to either fctl_list or send_list.
5505 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005506static void h2_stop_senders(struct h2c *h2c)
5507{
5508 struct h2s *h2s, *h2s_back;
5509
Olivier Houchardd360ac62019-03-22 17:37:16 +01005510 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5511 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02005512 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005513 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005514 }
5515}
5516
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005517/* Called from the upper layer, to send data from buffer <buf> for no more than
5518 * <count> bytes. Returns the number of bytes effectively sent. Some status
5519 * flags may be updated on the conn_stream.
5520 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005521static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005522{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005523 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005524 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005525 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005526 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005527 struct htx *htx;
5528 struct htx_blk *blk;
5529 enum htx_blk_type btype;
5530 uint32_t bsize;
5531 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005532
Olivier Houchardd360ac62019-03-22 17:37:16 +01005533 /* If we were not just woken because we wanted to send but couldn't,
5534 * and there's somebody else that is waiting to send, do nothing,
5535 * we will subscribe later and be put at the end of the list
5536 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005537 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005538 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5539 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005540 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005541
Olivier Houchard998410a2019-04-15 19:23:37 +02005542 /* We couldn't set it to NULL before, because we needed it in case
5543 * we had to cancel the tasklet
5544 */
5545 h2s->send_wait = NULL;
5546
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005547 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5548 return 0;
5549
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005550 /* htx will be enough to decide if we're using HTX or legacy */
5551 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5552
Willy Tarreau0bad0432018-06-14 16:54:01 +02005553 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005554 h2s->flags |= H2_SF_OUTGOING_DATA;
5555
Willy Tarreau751f2d02018-10-05 09:35:00 +02005556 if (h2s->id == 0) {
5557 int32_t id = h2c_get_next_sid(h2s->h2c);
5558
5559 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005560 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005561 return 0;
5562 }
5563
5564 eb32_delete(&h2s->by_id);
5565 h2s->by_id.key = h2s->id = id;
5566 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005567 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005568 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5569 }
5570
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005571 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005572 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005573 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005574 idx = htx_get_head(htx);
5575 blk = htx_get_blk(htx, idx);
5576 btype = htx_get_blk_type(blk);
5577 bsize = htx_get_blksz(blk);
5578
5579 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005580 case HTX_BLK_REQ_SL:
5581 /* start-line before headers */
5582 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5583 if (ret > 0) {
5584 total += ret;
5585 count -= ret;
5586 if (ret < bsize)
5587 goto done;
5588 }
5589 break;
5590
Willy Tarreau115e83b2018-12-01 19:17:53 +01005591 case HTX_BLK_RES_SL:
5592 /* start-line before headers */
5593 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5594 if (ret > 0) {
5595 total += ret;
5596 count -= ret;
5597 if (ret < bsize)
5598 goto done;
5599 }
5600 break;
5601
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005602 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005603 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005604 /* all these cause the emission of a DATA frame (possibly empty).
5605 * This EOM necessarily is one before trailers, as the EOM following
5606 * trailers would have been consumed by the trailers parser.
5607 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005608 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005609 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005610 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005611 total += ret;
5612 count -= ret;
5613 if (ret < bsize)
5614 goto done;
5615 }
5616 break;
5617
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005618 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005619 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005620 /* This is the first trailers block, all the subsequent ones AND
5621 * the EOM will be swallowed by the parser.
5622 */
5623 ret = h2s_htx_make_trailers(h2s, htx);
5624 if (ret > 0) {
5625 total += ret;
5626 count -= ret;
5627 if (ret < bsize)
5628 goto done;
5629 }
5630 break;
5631
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005632 default:
5633 htx_remove_blk(htx, blk);
5634 total += bsize;
5635 count -= bsize;
5636 break;
5637 }
5638 }
5639 goto done;
5640 }
5641
5642 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005643 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005644 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005645 if (h2s->h2c->flags & H2_CF_IS_BACK)
5646 ret = -1;
5647 else
5648 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005649 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005650 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005651 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005652 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005653 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005654 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005655 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005656
Willy Tarreau5dd17352018-06-14 13:33:30 +02005657 if (unlikely((int)ret <= 0)) {
5658 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005659 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5660 break;
5661 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005662 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005663 total += count;
5664 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005665 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005666 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005667 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005668 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005669 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005670 break;
5671 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005672
5673 total += ret;
5674 count -= ret;
5675
Willy Tarreau2b778482019-05-06 15:00:22 +02005676 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005677 break;
5678
5679 if (h2s->flags & H2_SF_BLK_ANY)
5680 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005681 }
5682
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005683 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005684 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005685 /* trim any possibly pending data after we close (extra CR-LF,
5686 * unprocessed trailers, abnormal extra data, ...)
5687 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005688 total += count;
5689 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005690 }
5691
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005692 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005693 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005694 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005695 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005696 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005697 }
5698
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005699 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005700 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005701 } else {
5702 b_del(buf, total);
5703 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005704
5705 /* The mux is full, cancel the pending tasks */
5706 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5707 (h2s->flags & H2_SF_BLK_MBUSY))
5708 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005709
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005710 /* If we're running HTX, and we read the whole buffer, then pretend
5711 * we read exactly what the caller specified, as with HTX the caller
5712 * will always give the buffer size, instead of the amount of data
5713 * available.
5714 */
5715 if (htx && !b_data(buf))
5716 total = orig_count;
5717
Olivier Houchard7505f942018-08-21 18:10:44 +02005718 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005719 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005720 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02005721
Olivier Houchard7505f942018-08-21 18:10:44 +02005722 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005723 /* If we're waiting for flow control, and we got a shutr on the
5724 * connection, we will never be unlocked, so add an error on
5725 * the conn_stream.
5726 */
5727 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5728 !b_data(&h2s->h2c->dbuf) &&
5729 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5730 if (cs->flags & CS_FL_EOS)
5731 cs->flags |= CS_FL_ERROR;
5732 else
5733 cs->flags |= CS_FL_ERR_PENDING;
5734 }
Willy Tarreaubda92dd2019-10-02 10:49:59 +02005735
5736 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL)) {
5737 /* Ok we managed to send something, leave the send_list if we were still there */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005738 LIST_DEL_INIT(&h2s->list);
5739 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005740 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005741}
5742
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005743/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005744static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005745{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005746 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005747 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005748 struct eb32_node *node;
5749 int fctl_cnt = 0;
5750 int send_cnt = 0;
5751 int tree_cnt = 0;
5752 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005753 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005754
5755 if (!h2c)
5756 return;
5757
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005758 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005759 fctl_cnt++;
5760
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005761 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005762 send_cnt++;
5763
Willy Tarreau3af37712018-12-18 14:34:41 +01005764 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005765 node = eb32_first(&h2c->streams_by_id);
5766 while (node) {
5767 h2s = container_of(node, struct h2s, by_id);
5768 tree_cnt++;
5769 if (!h2s->cs)
5770 orph_cnt++;
5771 node = eb32_next(node);
5772 }
5773
Willy Tarreau60f62682019-05-26 11:32:27 +02005774 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005775 tmbuf = br_tail(h2c->mbuf);
Willy Tarreau987c0632018-12-18 10:32:05 +01005776 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5777 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005778 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5779 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau616ac812018-07-24 14:12:42 +02005780 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5781 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005782 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005783 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5784 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5785 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005786 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5787 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5788 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005789 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5790 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005791
5792 if (h2s) {
5793 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5794 h2s, h2s->id, h2s->flags,
5795 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5796 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5797 h2s->cs);
5798 if (h2s->cs)
5799 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5800 h2s->cs->flags, h2s->cs->data);
5801 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005802}
Willy Tarreau62f52692017-10-08 23:01:42 +02005803
5804/*******************************************************/
5805/* functions below are dedicated to the config parsers */
5806/*******************************************************/
5807
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005808/* config parser for global "tune.h2.header-table-size" */
5809static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5810 struct proxy *defpx, const char *file, int line,
5811 char **err)
5812{
5813 if (too_many_args(1, args, err, NULL))
5814 return -1;
5815
5816 h2_settings_header_table_size = atoi(args[1]);
5817 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5818 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5819 return -1;
5820 }
5821 return 0;
5822}
Willy Tarreau62f52692017-10-08 23:01:42 +02005823
Willy Tarreaue6baec02017-07-27 11:45:11 +02005824/* config parser for global "tune.h2.initial-window-size" */
5825static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5826 struct proxy *defpx, const char *file, int line,
5827 char **err)
5828{
5829 if (too_many_args(1, args, err, NULL))
5830 return -1;
5831
5832 h2_settings_initial_window_size = atoi(args[1]);
5833 if (h2_settings_initial_window_size < 0) {
5834 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5835 return -1;
5836 }
5837 return 0;
5838}
5839
Willy Tarreau5242ef82017-07-27 11:47:28 +02005840/* config parser for global "tune.h2.max-concurrent-streams" */
5841static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5842 struct proxy *defpx, const char *file, int line,
5843 char **err)
5844{
5845 if (too_many_args(1, args, err, NULL))
5846 return -1;
5847
5848 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005849 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005850 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5851 return -1;
5852 }
5853 return 0;
5854}
5855
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005856/* config parser for global "tune.h2.max-frame-size" */
5857static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5858 struct proxy *defpx, const char *file, int line,
5859 char **err)
5860{
5861 if (too_many_args(1, args, err, NULL))
5862 return -1;
5863
5864 h2_settings_max_frame_size = atoi(args[1]);
5865 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5866 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5867 return -1;
5868 }
5869 return 0;
5870}
5871
Willy Tarreau62f52692017-10-08 23:01:42 +02005872
5873/****************************************/
5874/* MUX initialization and instanciation */
5875/***************************************/
5876
5877/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005878static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005879 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005880 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005881 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005882 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005883 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005884 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005885 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005886 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005887 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005888 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005889 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005890 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005891 .shutr = h2_shutr,
5892 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005893 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005894 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005895 .name = "H2",
5896};
5897
Christopher Faulet32f61c02018-04-10 14:33:41 +02005898/* PROTO selection : this mux registers PROTO token "h2" */
5899static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005900 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005901
Willy Tarreau0108d902018-11-25 19:14:37 +01005902INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5903
Christopher Faulet9b579102019-04-11 22:52:25 +02005904
5905/* The mux operations */
5906static const struct mux_ops h2_htx_ops = {
5907 .init = h2_init,
5908 .wake = h2_wake,
5909 .snd_buf = h2_snd_buf,
5910 .rcv_buf = h2_rcv_buf,
5911 .subscribe = h2_subscribe,
5912 .unsubscribe = h2_unsubscribe,
5913 .attach = h2_attach,
5914 .get_first_cs = h2_get_first_cs,
5915 .detach = h2_detach,
5916 .destroy = h2_destroy,
5917 .avail_streams = h2_avail_streams,
5918 .used_streams = h2_used_streams,
5919 .shutr = h2_shutr,
5920 .shutw = h2_shutw,
5921 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005922 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005923 .name = "H2",
5924};
5925
Willy Tarreauf8957272018-10-03 10:25:20 +02005926static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005927 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005928
5929INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5930
Willy Tarreau62f52692017-10-08 23:01:42 +02005931/* config keyword parsers */
5932static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005933 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005934 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005935 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005936 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005937 { 0, NULL, NULL }
5938}};
5939
Willy Tarreau0108d902018-11-25 19:14:37 +01005940INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);