blob: e3c0bb0033ac2b94eb6daa6b6b842c22109c1ea4 [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 */
Olivier Houchardd846c262018-10-19 17:24:29 +0200133 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100134 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200135 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200136};
137
Willy Tarreau18312642017-10-11 07:57:07 +0200138/* H2 stream state, in h2s->st */
139enum h2_ss {
140 H2_SS_IDLE = 0, // idle
141 H2_SS_RLOC, // reserved(local)
142 H2_SS_RREM, // reserved(remote)
143 H2_SS_OPEN, // open
144 H2_SS_HREM, // half-closed(remote)
145 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200146 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200147 H2_SS_CLOSED, // closed
148 H2_SS_ENTRIES // must be last
149} __attribute__((packed));
150
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200151#define H2_SS_MASK(state) (1UL << (state))
152#define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE)
153#define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC)
154#define H2_SS_RREM_BIT (1UL << H2_SS_RREM)
155#define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN)
156#define H2_SS_HREM_BIT (1UL << H2_SS_HREM)
157#define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC)
158#define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR)
159#define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED)
160#define H2_SS_EOS_BITS (H2_SS_CLOSED_BIT|H2_SS_ERROR_BIT|H2_SS_HREM_BIT)
161
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)
172#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
173#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
174#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
175#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_* */
212 int mws; /* mux window size for this stream */
213 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;
364 tasklet_wakeup(h2c->wait_event.task);
365}
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
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200549 h2c->wait_event.task = tasklet_new();
550 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200551 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200552 h2c->wait_event.task->process = h2_io_cb;
553 h2c->wait_event.task->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);
Olivier Houchardd846c262018-10-19 17:24:29 +0200586 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100587 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200588
Willy Tarreau3f133572017-10-31 19:21:06 +0100589 if (t)
590 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100591
Willy Tarreau01b44822018-10-03 14:26:37 +0200592 if (h2c->flags & H2_CF_IS_BACK) {
593 /* FIXME: this is temporary, for outgoing connections we need
594 * to immediately allocate a stream until the code is modified
595 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100596 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200597 */
598 struct h2s *h2s;
599
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100600 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200601 if (!h2s)
602 goto fail_stream;
603 }
604
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100605 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200606
Willy Tarreau0f383582018-10-03 14:22:21 +0200607 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200608 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200609 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200610 fail_stream:
611 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200612 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200613 task_destroy(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200614 if (h2c->wait_event.task)
615 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100616 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200617 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200618 return -1;
619}
620
Willy Tarreau751f2d02018-10-05 09:35:00 +0200621/* returns the next allocatable outgoing stream ID for the H2 connection, or
622 * -1 if no more is allocatable.
623 */
624static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
625{
626 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100627
628 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200629 id = -1;
630 return id;
631}
632
Willy Tarreau2373acc2017-10-12 17:35:14 +0200633/* returns the stream associated with id <id> or NULL if not found */
634static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
635{
636 struct eb32_node *node;
637
Willy Tarreau751f2d02018-10-05 09:35:00 +0200638 if (id == 0)
639 return (struct h2s *)h2_closed_stream;
640
Willy Tarreau2a856182017-05-16 15:20:39 +0200641 if (id > h2c->max_id)
642 return (struct h2s *)h2_idle_stream;
643
Willy Tarreau2373acc2017-10-12 17:35:14 +0200644 node = eb32_lookup(&h2c->streams_by_id, id);
645 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200646 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200647
648 return container_of(node, struct h2s, by_id);
649}
650
Christopher Faulet73c12072019-04-08 11:23:22 +0200651/* release function. This one should be called to free all resources allocated
652 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200653 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200654static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200655{
Christopher Faulet61840e72019-04-15 09:33:32 +0200656 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200657
Willy Tarreau32218eb2017-09-22 08:07:25 +0200658 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200659 /* The connection must be aattached to this mux to be released */
660 if (h2c->conn && h2c->conn->ctx == h2c)
661 conn = h2c->conn;
662
Willy Tarreau32218eb2017-09-22 08:07:25 +0200663 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200664
Willy Tarreau186e96e2019-05-28 17:21:18 +0200665 if (LIST_ADDED(&h2c->buf_wait.list)) {
666 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
667 LIST_DEL(&h2c->buf_wait.list);
668 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
669 }
Willy Tarreau14398122017-09-22 14:26:04 +0200670
Willy Tarreau44e973f2018-03-01 17:49:30 +0100671 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200672 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100673
Willy Tarreauea392822017-10-31 10:02:25 +0100674 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200675 h2c->task->context = NULL;
676 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100677 h2c->task = NULL;
678 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200679 if (h2c->wait_event.task)
680 tasklet_free(h2c->wait_event.task);
Olivier Houchard0e079372019-04-15 17:51:16 +0200681 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100682 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200683 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100684
Willy Tarreaubafbe012017-11-24 17:34:44 +0100685 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200686 }
687
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200688 if (conn) {
689 conn->mux = NULL;
690 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200691
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200692 conn_stop_tracking(conn);
693 conn_full_close(conn);
694 if (conn->destroy_cb)
695 conn->destroy_cb(conn);
696 conn_free(conn);
697 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200698}
699
700
Willy Tarreau71681172017-10-23 14:39:06 +0200701/******************************************************/
702/* functions below are for the H2 protocol processing */
703/******************************************************/
704
705/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100706static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200707{
708 return h2s ? h2s->id : 0;
709}
710
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200711/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100712static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200713{
714 if (h2c->msi < 0)
715 return 0;
716
717 if (h2c->msi == h2s_id(h2s))
718 return 0;
719
720 return 1;
721}
722
Willy Tarreau741d6df2017-10-17 08:00:59 +0200723/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100724static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200725{
726 h2c->errcode = err;
727 h2c->st0 = H2_CS_ERROR;
728}
729
Willy Tarreau175cebb2019-01-24 10:02:24 +0100730/* marks an error on the stream. It may also update an already closed stream
731 * (e.g. to report an error after an RST was received).
732 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100733static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200734{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100735 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200736 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100737 if (h2s->st < H2_SS_ERROR)
738 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100739 if (h2s->cs)
740 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200741 }
742}
743
Willy Tarreau7e094452018-12-19 18:08:52 +0100744/* attempt to notify the data layer of recv availability */
745static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
746{
747 struct wait_event *sw;
748
749 if (h2s->recv_wait) {
750 sw = h2s->recv_wait;
751 sw->events &= ~SUB_RETRY_RECV;
752 tasklet_wakeup(sw->task);
753 h2s->recv_wait = NULL;
754 }
755}
756
757/* attempt to notify the data layer of send availability */
758static void __maybe_unused h2s_notify_send(struct h2s *h2s)
759{
760 struct wait_event *sw;
761
Willy Tarreauc234ae32019-05-13 17:56:11 +0200762 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100763 sw = h2s->send_wait;
764 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100765 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100766 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100767 }
768}
769
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100770/* alerts the data layer, trying to wake it up by all means, following
771 * this sequence :
772 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
773 * - if its subscribed to send, then it's woken up for send
774 * - if it was subscribed to neither, its ->wake() callback is called
775 * It is safe to call this function with a closed stream which doesn't have a
776 * conn_stream anymore.
777 */
778static void __maybe_unused h2s_alert(struct h2s *h2s)
779{
780 if (h2s->recv_wait || h2s->send_wait) {
781 h2s_notify_recv(h2s);
782 h2s_notify_send(h2s);
783 }
784 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
785 h2s->cs->data_cb->wake(h2s->cs);
786}
787
Willy Tarreaue4820742017-07-27 13:37:23 +0200788/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100789static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200790{
791 uint8_t *out = frame;
792
793 *out = len >> 16;
794 write_n16(out + 1, len);
795}
796
Willy Tarreau54c15062017-10-10 17:10:03 +0200797/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
798 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
799 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200800 * available in the buffer's input prior to calling this function. The buffer
801 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200802 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100803static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200804 const struct buffer *b, int o)
805{
Willy Tarreau591d4452018-06-15 17:21:00 +0200806 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 +0200807}
808
Willy Tarreau1f094672017-11-20 21:27:45 +0100809static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200810{
Willy Tarreau591d4452018-06-15 17:21:00 +0200811 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200812}
813
Willy Tarreau1f094672017-11-20 21:27:45 +0100814static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200815{
Willy Tarreau591d4452018-06-15 17:21:00 +0200816 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200817}
818
Willy Tarreau1f094672017-11-20 21:27:45 +0100819static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200820{
Willy Tarreau591d4452018-06-15 17:21:00 +0200821 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200822}
823
824
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100825/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
826 * The algorithm is not obvious. It turns out that H2 headers are neither
827 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
828 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200829 *
830 * b0 b1 b2 b3 b4 b5..b8
831 * +----------+---------+--------+----+----+----------------------+
832 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
833 * +----------+---------+--------+----+----+----------------------+
834 *
835 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
836 * we get the sid properly aligned and ordered, and 16 bits of len properly
837 * ordered as well. The type and flags can be extracted using bit shifts from
838 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200839 * Returns zero if some bytes are missing, otherwise non-zero on success. The
840 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200841 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100842static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200843{
844 uint64_t w;
845
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100846 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200847 return 0;
848
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100849 w = h2_get_n64(b, o + 1);
850 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200851 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
852 h->ff = w >> 32;
853 h->ft = w >> 40;
854 h->len += w >> 48;
855 return 1;
856}
857
858/* skip the next 9 bytes corresponding to the frame header possibly parsed by
859 * h2_peek_frame_hdr() above.
860 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100861static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200862{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200863 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200864}
865
866/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100867static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200868{
869 int ret;
870
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100871 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200872 if (ret > 0)
873 h2_skip_frame_hdr(b);
874 return ret;
875}
876
Willy Tarreau00dd0782018-03-01 16:31:34 +0100877/* marks stream <h2s> as CLOSED and decrement the number of active streams for
878 * its connection if the stream was not yet closed. Please use this exclusively
879 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100880 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100881static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100882{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100883 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100884 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100885 if (!h2s->id)
886 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100887 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +0100888 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
889 h2s_notify_recv(h2s);
890 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100891 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100892 h2s->st = H2_SS_CLOSED;
893}
894
Willy Tarreau71049cc2018-03-28 13:56:39 +0200895/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
896static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100897{
898 h2s_close(h2s);
899 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200900 if (b_size(&h2s->rxbuf)) {
901 b_free(&h2s->rxbuf);
902 offer_buffers(NULL, tasks_run_queue);
903 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200904 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100905 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200906 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100907 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800908 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200909 * reference left would be in the h2c send_list/fctl_list, and if
910 * we're in it, we're getting out anyway
911 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100912 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200913 if (LIST_ADDED(&h2s->sending_list)) {
Olivier Houchard998410a2019-04-15 19:23:37 +0200914 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
915 LIST_DEL_INIT(&h2s->sending_list);
916 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200917 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100918 pool_free(pool_head_h2s, h2s);
919}
920
Willy Tarreaua8e49542018-10-03 18:53:55 +0200921/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
922 * stream tree. In case of error, nothing is added and NULL is returned. The
923 * causes of errors can be any failed memory allocation. The caller is
924 * responsible for checking if the connection may support an extra stream
925 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200926 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200927static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200928{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200929 struct h2s *h2s;
930
Willy Tarreaubafbe012017-11-24 17:34:44 +0100931 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200932 if (!h2s)
933 goto out;
934
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200935 h2s->wait_event.task = tasklet_new();
936 if (!h2s->wait_event.task) {
937 pool_free(pool_head_h2s, h2s);
938 goto out;
939 }
940 h2s->send_wait = NULL;
941 h2s->recv_wait = NULL;
942 h2s->wait_event.task->process = h2_deferred_shut;
943 h2s->wait_event.task->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100944 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200945 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100946 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200947 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200948 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200949 h2s->mws = h2c->miw;
950 h2s->flags = H2_SF_NONE;
951 h2s->errcode = H2_ERR_NO_ERROR;
952 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200953 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100954 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200955 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200956
957 if (h2c->flags & H2_CF_IS_BACK) {
958 h1m_init_req(&h2s->h1m);
959 h2s->h1m.err_pos = -1; // don't care about errors on the request path
960 h2s->h1m.flags |= H1_MF_TOLOWER;
961 } else {
962 h1m_init_res(&h2s->h1m);
963 h2s->h1m.err_pos = -1; // don't care about errors on the response path
964 h2s->h1m.flags |= H1_MF_TOLOWER;
965 }
966
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200967 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200968 if (id > 0)
969 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100970 else
971 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200972
973 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100974 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100975 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200976
977 return h2s;
978
979 out_free_h2s:
980 pool_free(pool_head_h2s, h2s);
981 out:
982 return NULL;
983}
984
985/* creates a new stream <id> on the h2c connection and returns it, or NULL in
986 * case of memory allocation error.
987 */
988static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
989{
990 struct session *sess = h2c->conn->owner;
991 struct conn_stream *cs;
992 struct h2s *h2s;
993
994 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
995 goto out;
996
997 h2s = h2s_new(h2c, id);
998 if (!h2s)
999 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001000
1001 cs = cs_new(h2c->conn);
1002 if (!cs)
1003 goto out_close;
1004
Olivier Houchard746fb772018-12-15 19:42:00 +01001005 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001006 h2s->cs = cs;
1007 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001008 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001009
1010 if (stream_create_from_cs(cs) < 0)
1011 goto out_free_cs;
1012
Willy Tarreau590a0512018-09-05 11:56:48 +02001013 /* We want the accept date presented to the next stream to be the one
1014 * we have now, the handshake time to be null (since the next stream
1015 * is not delayed by a handshake), and the idle time to count since
1016 * right now.
1017 */
1018 sess->accept_date = date;
1019 sess->tv_accept = now;
1020 sess->t_handshake = 0;
1021
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001022 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001023 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001024 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001025 return h2s;
1026
1027 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001028 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001029 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001030 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001031 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001032 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001033 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001034 sess_log(sess);
1035 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001036}
1037
Willy Tarreau751f2d02018-10-05 09:35:00 +02001038/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1039 * and returns it, or NULL in case of memory allocation error or if the highest
1040 * possible stream ID was reached.
1041 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001042static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001043{
1044 struct h2s *h2s = NULL;
1045
Willy Tarreau86949782019-01-31 10:42:05 +01001046 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001047 goto out;
1048
Willy Tarreaua80dca82019-01-24 17:08:28 +01001049 if (h2_streams_left(h2c) < 1)
1050 goto out;
1051
Willy Tarreau751f2d02018-10-05 09:35:00 +02001052 /* Defer choosing the ID until we send the first message to create the stream */
1053 h2s = h2s_new(h2c, 0);
1054 if (!h2s)
1055 goto out;
1056
1057 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001058 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001059 cs->ctx = h2s;
1060 h2c->nb_cs++;
1061
Willy Tarreau751f2d02018-10-05 09:35:00 +02001062 out:
1063 return h2s;
1064}
1065
Willy Tarreaube5b7152017-09-25 16:25:39 +02001066/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1067 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1068 * the various settings codes.
1069 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001070static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001071{
1072 struct buffer *res;
1073 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001074 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001075 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001076 int ret;
1077
1078 if (h2c_mux_busy(h2c, NULL)) {
1079 h2c->flags |= H2_CF_DEM_MBUSY;
1080 return 0;
1081 }
1082
Willy Tarreaube5b7152017-09-25 16:25:39 +02001083 chunk_init(&buf, buf_data, sizeof(buf_data));
1084 chunk_memcpy(&buf,
1085 "\x00\x00\x00" /* length : 0 for now */
1086 "\x04\x00" /* type : 4 (settings), flags : 0 */
1087 "\x00\x00\x00\x00", /* stream ID : 0 */
1088 9);
1089
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001090 if (h2c->flags & H2_CF_IS_BACK) {
1091 /* send settings_enable_push=0 */
1092 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1093 }
1094
Willy Tarreaube5b7152017-09-25 16:25:39 +02001095 if (h2_settings_header_table_size != 4096) {
1096 char str[6] = "\x00\x01"; /* header_table_size */
1097
1098 write_n32(str + 2, h2_settings_header_table_size);
1099 chunk_memcat(&buf, str, 6);
1100 }
1101
1102 if (h2_settings_initial_window_size != 65535) {
1103 char str[6] = "\x00\x04"; /* initial_window_size */
1104
1105 write_n32(str + 2, h2_settings_initial_window_size);
1106 chunk_memcat(&buf, str, 6);
1107 }
1108
1109 if (h2_settings_max_concurrent_streams != 0) {
1110 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1111
1112 /* Note: 0 means "unlimited" for haproxy's config but not for
1113 * the protocol, so never send this value!
1114 */
1115 write_n32(str + 2, h2_settings_max_concurrent_streams);
1116 chunk_memcat(&buf, str, 6);
1117 }
1118
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001119 mfs = h2_settings_max_frame_size;
1120 if (mfs > global.tune.bufsize)
1121 mfs = global.tune.bufsize;
1122
1123 if (!mfs)
1124 mfs = global.tune.bufsize;
1125
1126 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001127 char str[6] = "\x00\x05"; /* max_frame_size */
1128
1129 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1130 * match bufsize - rewrite size, but at the moment it seems
1131 * that clients don't take care of it.
1132 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001133 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001134 chunk_memcat(&buf, str, 6);
1135 }
1136
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001137 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001138
1139 res = br_tail(h2c->mbuf);
1140 retry:
1141 if (!h2_get_buf(h2c, res)) {
1142 h2c->flags |= H2_CF_MUX_MALLOC;
1143 h2c->flags |= H2_CF_DEM_MROOM;
1144 return 0;
1145 }
1146
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001147 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001148 if (unlikely(ret <= 0)) {
1149 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001150 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1151 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001152 h2c->flags |= H2_CF_MUX_MFULL;
1153 h2c->flags |= H2_CF_DEM_MROOM;
1154 return 0;
1155 }
1156 else {
1157 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1158 return 0;
1159 }
1160 }
1161 return ret;
1162}
1163
Willy Tarreau52eed752017-09-22 15:05:09 +02001164/* Try to receive a connection preface, then upon success try to send our
1165 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1166 * missing data. It may return an error in h2c.
1167 */
1168static int h2c_frt_recv_preface(struct h2c *h2c)
1169{
1170 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001171 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001172
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001173 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001174
1175 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001176 if (ret1 < 0)
1177 sess_log(h2c->conn->owner);
1178
Willy Tarreau52eed752017-09-22 15:05:09 +02001179 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1180 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1181 return 0;
1182 }
1183
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001184 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001185 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001186 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001187
Willy Tarreaube5b7152017-09-25 16:25:39 +02001188 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001189}
1190
Willy Tarreau01b44822018-10-03 14:26:37 +02001191/* Try to send a connection preface, then upon success try to send our
1192 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1193 * missing data. It may return an error in h2c.
1194 */
1195static int h2c_bck_send_preface(struct h2c *h2c)
1196{
1197 struct buffer *res;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001198 int ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001199
1200 if (h2c_mux_busy(h2c, NULL)) {
1201 h2c->flags |= H2_CF_DEM_MBUSY;
1202 return 0;
1203 }
1204
Willy Tarreaubcc45952019-05-26 10:05:50 +02001205 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001206 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001207 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001208 h2c->flags |= H2_CF_MUX_MALLOC;
1209 h2c->flags |= H2_CF_DEM_MROOM;
1210 return 0;
1211 }
1212
1213 if (!b_data(res)) {
1214 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001215 ret = b_istput(res, ist(H2_CONN_PREFACE));
1216 if (unlikely(ret <= 0)) {
1217 if (!ret) {
1218 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1219 goto retry;
1220 h2c->flags |= H2_CF_MUX_MFULL;
1221 h2c->flags |= H2_CF_DEM_MROOM;
1222 return 0;
1223 }
1224 else {
1225 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1226 return 0;
1227 }
1228 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001229 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001230 return h2c_send_settings(h2c);
1231}
1232
Willy Tarreau081d4722017-05-16 21:51:05 +02001233/* try to send a GOAWAY frame on the connection to report an error or a graceful
1234 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1235 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1236 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1237 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1238 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1239 * on unrecoverable failure. It will not attempt to send one again in this last
1240 * case so that it is safe to use h2c_error() to report such errors.
1241 */
1242static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1243{
1244 struct buffer *res;
1245 char str[17];
1246 int ret;
1247
1248 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1249 return 1; // claim that it worked
1250
1251 if (h2c_mux_busy(h2c, h2s)) {
1252 if (h2s)
1253 h2s->flags |= H2_SF_BLK_MBUSY;
1254 else
1255 h2c->flags |= H2_CF_DEM_MBUSY;
1256 return 0;
1257 }
1258
Willy Tarreau9c218e72019-05-26 10:08:28 +02001259 /* len: 8, type: 7, flags: none, sid: 0 */
1260 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1261
1262 if (h2c->last_sid < 0)
1263 h2c->last_sid = h2c->max_id;
1264
1265 write_n32(str + 9, h2c->last_sid);
1266 write_n32(str + 13, h2c->errcode);
1267
Willy Tarreaubcc45952019-05-26 10:05:50 +02001268 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001269 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001270 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001271 h2c->flags |= H2_CF_MUX_MALLOC;
1272 if (h2s)
1273 h2s->flags |= H2_SF_BLK_MROOM;
1274 else
1275 h2c->flags |= H2_CF_DEM_MROOM;
1276 return 0;
1277 }
1278
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001279 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001280 if (unlikely(ret <= 0)) {
1281 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001282 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1283 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001284 h2c->flags |= H2_CF_MUX_MFULL;
1285 if (h2s)
1286 h2s->flags |= H2_SF_BLK_MROOM;
1287 else
1288 h2c->flags |= H2_CF_DEM_MROOM;
1289 return 0;
1290 }
1291 else {
1292 /* we cannot report this error using GOAWAY, so we mark
1293 * it and claim a success.
1294 */
1295 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1296 h2c->flags |= H2_CF_GOAWAY_FAILED;
1297 return 1;
1298 }
1299 }
1300 h2c->flags |= H2_CF_GOAWAY_SENT;
1301 return ret;
1302}
1303
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001304/* Try to send an RST_STREAM frame on the connection for the indicated stream
1305 * during mux operations. This stream must be valid and cannot be closed
1306 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1307 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1308 * not yet.
1309 *
1310 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1311 * to write the message, it subscribes the stream to future notifications.
1312 */
1313static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1314{
1315 struct buffer *res;
1316 char str[13];
1317 int ret;
1318
1319 if (!h2s || h2s->st == H2_SS_CLOSED)
1320 return 1;
1321
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001322 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1323 * RST_STREAM in response to a RST_STREAM frame.
1324 */
1325 if (h2c->dft == H2_FT_RST_STREAM) {
1326 ret = 1;
1327 goto ignore;
1328 }
1329
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001330 if (h2c_mux_busy(h2c, h2s)) {
1331 h2s->flags |= H2_SF_BLK_MBUSY;
1332 return 0;
1333 }
1334
Willy Tarreau9c218e72019-05-26 10:08:28 +02001335 /* len: 4, type: 3, flags: none */
1336 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1337 write_n32(str + 5, h2s->id);
1338 write_n32(str + 9, h2s->errcode);
1339
Willy Tarreaubcc45952019-05-26 10:05:50 +02001340 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001341 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001342 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001343 h2c->flags |= H2_CF_MUX_MALLOC;
1344 h2s->flags |= H2_SF_BLK_MROOM;
1345 return 0;
1346 }
1347
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001348 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001349 if (unlikely(ret <= 0)) {
1350 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001351 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1352 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001353 h2c->flags |= H2_CF_MUX_MFULL;
1354 h2s->flags |= H2_SF_BLK_MROOM;
1355 return 0;
1356 }
1357 else {
1358 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1359 return 0;
1360 }
1361 }
1362
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001363 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001364 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001365 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001366 return ret;
1367}
1368
1369/* Try to send an RST_STREAM frame on the connection for the stream being
1370 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001371 * error code, even if the stream is one of the dummy ones, and will update
1372 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001373 *
1374 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1375 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001376 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001377 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001378 */
1379static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1380{
1381 struct buffer *res;
1382 char str[13];
1383 int ret;
1384
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001385 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1386 * RST_STREAM in response to a RST_STREAM frame.
1387 */
1388 if (h2c->dft == H2_FT_RST_STREAM) {
1389 ret = 1;
1390 goto ignore;
1391 }
1392
Willy Tarreau27a84c92017-10-17 08:10:17 +02001393 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001394 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001395 return 0;
1396 }
1397
Willy Tarreau9c218e72019-05-26 10:08:28 +02001398 /* len: 4, type: 3, flags: none */
1399 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1400
1401 write_n32(str + 5, h2c->dsi);
1402 write_n32(str + 9, h2s->errcode);
1403
Willy Tarreaubcc45952019-05-26 10:05:50 +02001404 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001405 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001406 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001407 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001408 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001409 return 0;
1410 }
1411
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001412 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001413 if (unlikely(ret <= 0)) {
1414 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001415 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1416 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001417 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001418 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001419 return 0;
1420 }
1421 else {
1422 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1423 return 0;
1424 }
1425 }
1426
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001427 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001428 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001429 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001430 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001431 }
1432
Willy Tarreau27a84c92017-10-17 08:10:17 +02001433 return ret;
1434}
1435
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001436/* try to send an empty DATA frame with the ES flag set to notify about the
1437 * end of stream and match a shutdown(write). If an ES was already sent as
1438 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1439 * on success or zero if nothing was done. In case of lack of room to write the
1440 * message, it subscribes the requesting stream to future notifications.
1441 */
1442static int h2_send_empty_data_es(struct h2s *h2s)
1443{
1444 struct h2c *h2c = h2s->h2c;
1445 struct buffer *res;
1446 char str[9];
1447 int ret;
1448
Willy Tarreau721c9742017-11-07 11:05:42 +01001449 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001450 return 1;
1451
1452 if (h2c_mux_busy(h2c, h2s)) {
1453 h2s->flags |= H2_SF_BLK_MBUSY;
1454 return 0;
1455 }
1456
Willy Tarreau9c218e72019-05-26 10:08:28 +02001457 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1458 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1459 write_n32(str + 5, h2s->id);
1460
Willy Tarreaubcc45952019-05-26 10:05:50 +02001461 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001462 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001463 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001464 h2c->flags |= H2_CF_MUX_MALLOC;
1465 h2s->flags |= H2_SF_BLK_MROOM;
1466 return 0;
1467 }
1468
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001469 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001470 if (likely(ret > 0)) {
1471 h2s->flags |= H2_SF_ES_SENT;
1472 }
1473 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001474 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1475 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001476 h2c->flags |= H2_CF_MUX_MFULL;
1477 h2s->flags |= H2_SF_BLK_MROOM;
1478 return 0;
1479 }
1480 else {
1481 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1482 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001483 }
1484 return ret;
1485}
1486
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001487/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001488 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001489 * is automatically updated accordingly. If the stream is orphaned, it is
1490 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001491 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001492static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001493{
1494 if (!h2s->cs) {
1495 /* this stream was already orphaned */
1496 h2s_destroy(h2s);
1497 return;
1498 }
1499
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001500 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001501 if (h2s->st == H2_SS_OPEN)
1502 h2s->st = H2_SS_HREM;
1503 else if (h2s->st == H2_SS_HLOC)
1504 h2s_close(h2s);
1505 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001506
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001507 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1508 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1509 h2s->cs->flags |= CS_FL_ERR_PENDING;
1510 if (h2s->cs->flags & CS_FL_EOS)
1511 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001512
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001513 if (h2s->st < H2_SS_ERROR)
1514 h2s->st = H2_SS_ERROR;
1515 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001516
1517 h2s_alert(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001518}
1519
1520/* wake the streams attached to the connection, whose id is greater than <last>
1521 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001522 */
Willy Tarreau23482912019-05-07 15:23:14 +02001523static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001524{
1525 struct eb32_node *node;
1526 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001527
Christopher Fauletf02ca002019-03-07 16:21:34 +01001528 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001529 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1530 while (node) {
1531 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001532 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001533 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001534 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001535
Christopher Fauletf02ca002019-03-07 16:21:34 +01001536 /* Wake all streams with unassigned ID (ID == 0) */
1537 node = eb32_lookup(&h2c->streams_by_id, 0);
1538 while (node) {
1539 h2s = container_of(node, struct h2s, by_id);
1540 if (h2s->id > 0)
1541 break;
1542 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001543 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001544 }
1545}
1546
Willy Tarreau3421aba2017-07-27 15:41:03 +02001547/* Increase all streams' outgoing window size by the difference passed in
1548 * argument. This is needed upon receipt of the settings frame if the initial
1549 * window size is different. The difference may be negative and the resulting
1550 * window size as well, for the time it takes to receive some window updates.
1551 */
1552static void h2c_update_all_ws(struct h2c *h2c, int diff)
1553{
1554 struct h2s *h2s;
1555 struct eb32_node *node;
1556
1557 if (!diff)
1558 return;
1559
1560 node = eb32_first(&h2c->streams_by_id);
1561 while (node) {
1562 h2s = container_of(node, struct h2s, by_id);
1563 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001564
1565 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1566 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001567 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001568 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001569 }
1570
Willy Tarreau3421aba2017-07-27 15:41:03 +02001571 node = eb32_next(node);
1572 }
1573}
1574
1575/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1576 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001577 * return an error in h2c. The caller must have already verified frame length
1578 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001579 */
1580static int h2c_handle_settings(struct h2c *h2c)
1581{
1582 unsigned int offset;
1583 int error;
1584
1585 if (h2c->dff & H2_F_SETTINGS_ACK) {
1586 if (h2c->dfl) {
1587 error = H2_ERR_FRAME_SIZE_ERROR;
1588 goto fail;
1589 }
1590 return 1;
1591 }
1592
Willy Tarreau3421aba2017-07-27 15:41:03 +02001593 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001594 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001595 return 0;
1596
1597 /* parse the frame */
1598 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001599 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1600 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001601
1602 switch (type) {
1603 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1604 /* we need to update all existing streams with the
1605 * difference from the previous iws.
1606 */
1607 if (arg < 0) { // RFC7540#6.5.2
1608 error = H2_ERR_FLOW_CONTROL_ERROR;
1609 goto fail;
1610 }
1611 h2c_update_all_ws(h2c, arg - h2c->miw);
1612 h2c->miw = arg;
1613 break;
1614 case H2_SETTINGS_MAX_FRAME_SIZE:
1615 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1616 error = H2_ERR_PROTOCOL_ERROR;
1617 goto fail;
1618 }
1619 h2c->mfs = arg;
1620 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001621 case H2_SETTINGS_ENABLE_PUSH:
1622 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1623 error = H2_ERR_PROTOCOL_ERROR;
1624 goto fail;
1625 }
1626 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001627 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1628 if (h2c->flags & H2_CF_IS_BACK) {
1629 /* the limit is only for the backend; for the frontend it is our limit */
1630 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1631 arg = h2_settings_max_concurrent_streams;
1632 h2c->streams_limit = arg;
1633 }
1634 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001635 }
1636 }
1637
1638 /* need to ACK this frame now */
1639 h2c->st0 = H2_CS_FRAME_A;
1640 return 1;
1641 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001642 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001643 h2c_error(h2c, error);
1644 return 0;
1645}
1646
1647/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1648 * success or one of the h2_status values.
1649 */
1650static int h2c_ack_settings(struct h2c *h2c)
1651{
1652 struct buffer *res;
1653 char str[9];
1654 int ret = -1;
1655
1656 if (h2c_mux_busy(h2c, NULL)) {
1657 h2c->flags |= H2_CF_DEM_MBUSY;
1658 return 0;
1659 }
1660
Willy Tarreau9c218e72019-05-26 10:08:28 +02001661 memcpy(str,
1662 "\x00\x00\x00" /* length : 0 (no data) */
1663 "\x04" "\x01" /* type : 4, flags : ACK */
1664 "\x00\x00\x00\x00" /* stream ID */, 9);
1665
Willy Tarreaubcc45952019-05-26 10:05:50 +02001666 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001667 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001668 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001669 h2c->flags |= H2_CF_MUX_MALLOC;
1670 h2c->flags |= H2_CF_DEM_MROOM;
1671 return 0;
1672 }
1673
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001674 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001675 if (unlikely(ret <= 0)) {
1676 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001677 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1678 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001679 h2c->flags |= H2_CF_MUX_MFULL;
1680 h2c->flags |= H2_CF_DEM_MROOM;
1681 return 0;
1682 }
1683 else {
1684 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1685 return 0;
1686 }
1687 }
1688 return ret;
1689}
1690
Willy Tarreaucf68c782017-10-10 17:11:41 +02001691/* processes a PING frame and schedules an ACK if needed. The caller must pass
1692 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001693 * missing data. The caller must have already verified frame length
1694 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001695 */
1696static int h2c_handle_ping(struct h2c *h2c)
1697{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001698 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001699 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001700 h2c->st0 = H2_CS_FRAME_A;
1701 return 1;
1702}
1703
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001704/* Try to send a window update for stream id <sid> and value <increment>.
1705 * Returns > 0 on success or zero on missing room or failure. It may return an
1706 * error in h2c.
1707 */
1708static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1709{
1710 struct buffer *res;
1711 char str[13];
1712 int ret = -1;
1713
1714 if (h2c_mux_busy(h2c, NULL)) {
1715 h2c->flags |= H2_CF_DEM_MBUSY;
1716 return 0;
1717 }
1718
Willy Tarreau9c218e72019-05-26 10:08:28 +02001719 /* length: 4, type: 8, flags: none */
1720 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1721 write_n32(str + 5, sid);
1722 write_n32(str + 9, increment);
1723
Willy Tarreaubcc45952019-05-26 10:05:50 +02001724 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001725 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001726 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001727 h2c->flags |= H2_CF_MUX_MALLOC;
1728 h2c->flags |= H2_CF_DEM_MROOM;
1729 return 0;
1730 }
1731
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001732 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001733 if (unlikely(ret <= 0)) {
1734 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001735 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1736 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001737 h2c->flags |= H2_CF_MUX_MFULL;
1738 h2c->flags |= H2_CF_DEM_MROOM;
1739 return 0;
1740 }
1741 else {
1742 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1743 return 0;
1744 }
1745 }
1746 return ret;
1747}
1748
1749/* try to send pending window update for the connection. It's safe to call it
1750 * with no pending updates. Returns > 0 on success or zero on missing room or
1751 * failure. It may return an error in h2c.
1752 */
1753static int h2c_send_conn_wu(struct h2c *h2c)
1754{
1755 int ret = 1;
1756
1757 if (h2c->rcvd_c <= 0)
1758 return 1;
1759
Willy Tarreau97aaa672018-12-23 09:49:04 +01001760 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1761 /* increase the advertised connection window to 2G on
1762 * first update.
1763 */
1764 h2c->flags |= H2_CF_WINDOW_OPENED;
1765 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1766 }
1767
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001768 /* send WU for the connection */
1769 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1770 if (ret > 0)
1771 h2c->rcvd_c = 0;
1772
1773 return ret;
1774}
1775
1776/* try to send pending window update for the current dmux stream. It's safe to
1777 * call it with no pending updates. Returns > 0 on success or zero on missing
1778 * room or failure. It may return an error in h2c.
1779 */
1780static int h2c_send_strm_wu(struct h2c *h2c)
1781{
1782 int ret = 1;
1783
1784 if (h2c->rcvd_s <= 0)
1785 return 1;
1786
1787 /* send WU for the stream */
1788 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1789 if (ret > 0)
1790 h2c->rcvd_s = 0;
1791
1792 return ret;
1793}
1794
Willy Tarreaucf68c782017-10-10 17:11:41 +02001795/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1796 * success, 0 on missing data or one of the h2_status values.
1797 */
1798static int h2c_ack_ping(struct h2c *h2c)
1799{
1800 struct buffer *res;
1801 char str[17];
1802 int ret = -1;
1803
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001804 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001805 return 0;
1806
1807 if (h2c_mux_busy(h2c, NULL)) {
1808 h2c->flags |= H2_CF_DEM_MBUSY;
1809 return 0;
1810 }
1811
Willy Tarreaucf68c782017-10-10 17:11:41 +02001812 memcpy(str,
1813 "\x00\x00\x08" /* length : 8 (same payload) */
1814 "\x06" "\x01" /* type : 6, flags : ACK */
1815 "\x00\x00\x00\x00" /* stream ID */, 9);
1816
1817 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001818 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001819
Willy Tarreau9c218e72019-05-26 10:08:28 +02001820 res = br_tail(h2c->mbuf);
1821 retry:
1822 if (!h2_get_buf(h2c, res)) {
1823 h2c->flags |= H2_CF_MUX_MALLOC;
1824 h2c->flags |= H2_CF_DEM_MROOM;
1825 return 0;
1826 }
1827
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001828 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001829 if (unlikely(ret <= 0)) {
1830 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001831 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1832 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02001833 h2c->flags |= H2_CF_MUX_MFULL;
1834 h2c->flags |= H2_CF_DEM_MROOM;
1835 return 0;
1836 }
1837 else {
1838 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1839 return 0;
1840 }
1841 }
1842 return ret;
1843}
1844
Willy Tarreau26f95952017-07-27 17:18:30 +02001845/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1846 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001847 * h2c or h2s. The caller must have already verified frame length and stream ID
1848 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001849 */
1850static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1851{
1852 int32_t inc;
1853 int error;
1854
Willy Tarreau26f95952017-07-27 17:18:30 +02001855 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001856 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001857 return 0;
1858
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001859 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001860
1861 if (h2c->dsi != 0) {
1862 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001863
1864 /* it's not an error to receive WU on a closed stream */
1865 if (h2s->st == H2_SS_CLOSED)
1866 return 1;
1867
1868 if (!inc) {
1869 error = H2_ERR_PROTOCOL_ERROR;
1870 goto strm_err;
1871 }
1872
1873 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1874 error = H2_ERR_FLOW_CONTROL_ERROR;
1875 goto strm_err;
1876 }
1877
1878 h2s->mws += inc;
1879 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1880 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001881 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Olivier Houcharddddfe312018-10-10 18:51:00 +02001882 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001883 }
1884 }
1885 else {
1886 /* connection window update */
1887 if (!inc) {
1888 error = H2_ERR_PROTOCOL_ERROR;
1889 goto conn_err;
1890 }
1891
1892 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1893 error = H2_ERR_FLOW_CONTROL_ERROR;
1894 goto conn_err;
1895 }
1896
1897 h2c->mws += inc;
1898 }
1899
1900 return 1;
1901
1902 conn_err:
1903 h2c_error(h2c, error);
1904 return 0;
1905
1906 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001907 h2s_error(h2s, error);
1908 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001909 return 0;
1910}
1911
Willy Tarreaue96b0922017-10-30 00:28:29 +01001912/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001913 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1914 * have already verified frame length and stream ID validity. Described in
1915 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001916 */
1917static int h2c_handle_goaway(struct h2c *h2c)
1918{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001919 int last;
1920
Willy Tarreaue96b0922017-10-30 00:28:29 +01001921 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001922 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001923 return 0;
1924
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001925 last = h2_get_n32(&h2c->dbuf, 0);
1926 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001927 if (h2c->last_sid < 0)
1928 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02001929 h2_wake_some_streams(h2c, last);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001930 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001931}
1932
Willy Tarreau92153fc2017-12-03 19:46:19 +01001933/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001934 * invalid. Returns > 0 on success or zero on missing data. It may return an
1935 * error in h2c. The caller must have already verified frame length and stream
1936 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001937 */
1938static int h2c_handle_priority(struct h2c *h2c)
1939{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001940 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001941 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001942 return 0;
1943
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001944 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001945 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001946 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1947 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001948 }
1949 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001950}
1951
Willy Tarreaucd234e92017-08-18 10:59:39 +02001952/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001953 * Returns > 0 on success or zero on missing data. The caller must have already
1954 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001955 */
1956static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1957{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001958 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001959 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001960 return 0;
1961
1962 /* late RST, already handled */
1963 if (h2s->st == H2_SS_CLOSED)
1964 return 1;
1965
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001966 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001967 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001968
1969 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001970 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001971 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001972 }
1973
1974 h2s->flags |= H2_SF_RST_RCVD;
1975 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001976}
1977
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001978/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1979 * It may return an error in h2c or h2s. The caller must consider that the
1980 * return value is the new h2s in case one was allocated (most common case).
1981 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001982 * errors here are reported as connection errors since it's impossible to
1983 * recover from such errors after the compression context has been altered.
1984 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001985static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001986{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001987 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001988 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001989 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001990 int error;
1991
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001992 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001993 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001994
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001995 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001996 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001997
1998 /* now either the frame is complete or the buffer is complete */
1999 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002000 /* The stream exists/existed, this must be a trailers frame */
2001 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02002002 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
2003 /* unrecoverable error ? */
2004 if (h2c->st0 >= H2_CS_ERROR)
2005 goto out;
2006
2007 if (error == 0)
2008 goto out; // missing data
2009
2010 if (error < 0) {
2011 /* Failed to decode this frame (e.g. too large request)
2012 * but the HPACK decompressor is still synchronized.
2013 */
2014 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2015 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002016 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002017 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002018 goto done;
2019 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002020 /* the connection was already killed by an RST, let's consume
2021 * the data and send another RST.
2022 */
2023 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2024 h2s = (struct h2s*)h2_error_stream;
2025 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002026 }
2027 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2028 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2029 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002030 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002031 goto conn_err;
2032 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002033 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2034 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002035
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002036 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002037
Willy Tarreau25919232019-01-03 14:48:18 +01002038 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002039 if (h2c->st0 >= H2_CS_ERROR)
2040 goto out;
2041
Willy Tarreau25919232019-01-03 14:48:18 +01002042 if (error <= 0) {
2043 if (error == 0)
2044 goto out; // missing data
2045
2046 /* Failed to decode this stream (e.g. too large request)
2047 * but the HPACK decompressor is still synchronized.
2048 */
2049 h2s = (struct h2s*)h2_error_stream;
2050 goto send_rst;
2051 }
2052
Willy Tarreau22de8d32018-09-05 19:55:58 +02002053 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002054 * positively from h2c_frt_stream_new(), the stream will report the error,
2055 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002056 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02002057 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02002058 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002059 h2s = (struct h2s*)h2_refused_stream;
2060 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002061 }
2062
2063 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002064 h2s->rxbuf = rxbuf;
2065 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002066 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002067
Willy Tarreau88d138e2019-01-02 19:38:14 +01002068 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002069 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002070 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002071
2072 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002073 if (h2s->st == H2_SS_OPEN)
2074 h2s->st = H2_SS_HREM;
2075 else
2076 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002077 }
2078
Willy Tarreau3a429f02019-01-03 11:41:50 +01002079 /* update the max stream ID if the request is being processed */
2080 if (h2s->id > h2c->max_id)
2081 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002082
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002083 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002084
2085 conn_err:
2086 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002087 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002088
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002089 out:
2090 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002091 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002092
2093 send_rst:
2094 /* make the demux send an RST for the current stream. We may only
2095 * do this if we're certain that the HEADERS frame was properly
2096 * decompressed so that the HPACK decoder is still kept up to date.
2097 */
2098 h2_release_buf(h2c, &rxbuf);
2099 h2c->st0 = H2_CS_FRAME_E;
2100 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002101}
2102
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002103/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2104 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2105 * errors here are reported as connection errors since it's impossible to
2106 * recover from such errors after the compression context has been altered.
2107 */
2108static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2109{
2110 int error;
2111
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002112 if (!b_size(&h2c->dbuf))
2113 return NULL; // empty buffer
2114
2115 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2116 return NULL; // incomplete frame
2117
Willy Tarreau1915ca22019-01-24 11:49:37 +01002118 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002119
Willy Tarreau25919232019-01-03 14:48:18 +01002120 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002121 if (h2c->st0 >= H2_CS_ERROR)
2122 return NULL;
2123
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002124 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2125 /* RFC7540#5.1 */
2126 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2127 h2c->st0 = H2_CS_FRAME_E;
2128 return NULL;
2129 }
2130
Willy Tarreau25919232019-01-03 14:48:18 +01002131 if (error <= 0) {
2132 if (error == 0)
2133 return NULL; // missing data
2134
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002135 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002136 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002137 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002138 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002139 }
2140
Christopher Fauletfa922f02019-05-07 10:55:17 +02002141 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002142 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002143
Willy Tarreau927b88b2019-03-04 08:03:25 +01002144 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002145 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002146 else if (h2s->flags & H2_SF_ES_RCVD) {
2147 if (h2s->st == H2_SS_OPEN)
2148 h2s->st = H2_SS_HREM;
2149 else if (h2s->st == H2_SS_HLOC)
2150 h2s_close(h2s);
2151 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002152
2153 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002154}
2155
Willy Tarreau454f9052017-10-26 19:40:35 +02002156/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2157 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2158 */
2159static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2160{
2161 int error;
2162
2163 /* note that empty DATA frames are perfectly valid and sometimes used
2164 * to signal an end of stream (with the ES flag).
2165 */
2166
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002167 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002168 return 0; // empty buffer
2169
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002170 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002171 return 0; // incomplete frame
2172
2173 /* now either the frame is complete or the buffer is complete */
2174
Willy Tarreau454f9052017-10-26 19:40:35 +02002175 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2176 /* RFC7540#6.1 */
2177 error = H2_ERR_STREAM_CLOSED;
2178 goto strm_err;
2179 }
2180
Willy Tarreau1915ca22019-01-24 11:49:37 +01002181 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2182 /* RFC7540#8.1.2 */
2183 error = H2_ERR_PROTOCOL_ERROR;
2184 goto strm_err;
2185 }
2186
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002187 if (!h2_frt_transfer_data(h2s))
2188 return 0;
2189
Willy Tarreau454f9052017-10-26 19:40:35 +02002190 /* call the upper layers to process the frame, then let the upper layer
2191 * notify the stream about any change.
2192 */
2193 if (!h2s->cs) {
2194 error = H2_ERR_STREAM_CLOSED;
2195 goto strm_err;
2196 }
2197
Willy Tarreau8f650c32017-11-21 19:36:21 +01002198 if (h2c->st0 >= H2_CS_ERROR)
2199 return 0;
2200
Willy Tarreau721c9742017-11-07 11:05:42 +01002201 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002202 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002203 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002204 }
2205
2206 /* check for completion : the callee will change this to FRAME_A or
2207 * FRAME_H once done.
2208 */
2209 if (h2c->st0 == H2_CS_FRAME_P)
2210 return 0;
2211
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002212 /* last frame */
2213 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002214 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002215 if (h2s->st == H2_SS_OPEN)
2216 h2s->st = H2_SS_HREM;
2217 else
2218 h2s_close(h2s);
2219
Willy Tarreau1915ca22019-01-24 11:49:37 +01002220 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2221 /* RFC7540#8.1.2 */
2222 error = H2_ERR_PROTOCOL_ERROR;
2223 goto strm_err;
2224 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002225 }
2226
Willy Tarreau454f9052017-10-26 19:40:35 +02002227 return 1;
2228
Willy Tarreau454f9052017-10-26 19:40:35 +02002229 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002230 h2s_error(h2s, error);
2231 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002232 return 0;
2233}
2234
Willy Tarreaubc933932017-10-09 16:21:43 +02002235/* process Rx frames to be demultiplexed */
2236static void h2_process_demux(struct h2c *h2c)
2237{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002238 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002239 struct h2_fh hdr;
2240 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002241
Willy Tarreau081d4722017-05-16 21:51:05 +02002242 if (h2c->st0 >= H2_CS_ERROR)
2243 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002244
2245 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2246 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002247 if (h2c->flags & H2_CF_IS_BACK)
2248 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002249 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2250 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002251 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002252 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002253 sess_log(h2c->conn->owner);
2254 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002255 goto fail;
2256 }
2257
2258 h2c->max_id = 0;
2259 h2c->st0 = H2_CS_SETTINGS1;
2260 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002261
2262 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002263 /* ensure that what is pending is a valid SETTINGS frame
2264 * without an ACK.
2265 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002266 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002267 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002268 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002269 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002270 sess_log(h2c->conn->owner);
2271 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002272 goto fail;
2273 }
2274
2275 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2276 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2277 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2278 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002279 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002280 goto fail;
2281 }
2282
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002283 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002284 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2285 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2286 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002287 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002288 goto fail;
2289 }
2290
Willy Tarreau3bf69182018-12-21 15:34:50 +01002291 /* that's OK, switch to FRAME_P to process it. This is
2292 * a SETTINGS frame whose header has already been
2293 * deleted above.
2294 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002295 padlen = 0;
2296 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002297 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002298 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002299
2300 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002301 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002302 int ret = 0;
2303
2304 if (h2c->st0 >= H2_CS_ERROR)
2305 break;
2306
2307 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002308 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002309 break;
2310
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002311 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002312 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002313 if (!h2c->nb_streams) {
2314 /* only log if no other stream can report the error */
2315 sess_log(h2c->conn->owner);
2316 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002317 break;
2318 }
2319
Willy Tarreau3bf69182018-12-21 15:34:50 +01002320 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2321 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2322 * we read the pad length and drop it from the remaining
2323 * payload (one byte + the 9 remaining ones = 10 total
2324 * removed), so we have a frame payload starting after the
2325 * pad len. Flow controlled frames (DATA) also count the
2326 * padlen in the flow control, so it must be adjusted.
2327 */
2328 if (hdr.len < 1) {
2329 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2330 sess_log(h2c->conn->owner);
2331 goto fail;
2332 }
2333 hdr.len--;
2334
2335 if (b_data(&h2c->dbuf) < 10)
2336 break; // missing padlen
2337
2338 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2339
2340 if (padlen > hdr.len) {
2341 /* RFC7540#6.1 : pad length = length of
2342 * frame payload or greater => error.
2343 */
2344 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2345 sess_log(h2c->conn->owner);
2346 goto fail;
2347 }
2348
2349 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2350 h2c->rcvd_c++;
2351 h2c->rcvd_s++;
2352 }
2353 b_del(&h2c->dbuf, 1);
2354 }
2355 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002356
2357 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002358 h2c->dfl = hdr.len;
2359 h2c->dsi = hdr.sid;
2360 h2c->dft = hdr.ft;
2361 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002362 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002363 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002364
2365 /* check for minimum basic frame format validity */
2366 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2367 if (ret != H2_ERR_NO_ERROR) {
2368 h2c_error(h2c, ret);
2369 sess_log(h2c->conn->owner);
2370 goto fail;
2371 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002372 }
2373
2374 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002375 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2376
Willy Tarreau567beb82018-12-18 16:52:44 +01002377 if (tmp_h2s != h2s && h2s && h2s->cs &&
2378 (b_data(&h2s->rxbuf) ||
Willy Tarreau99ad1b32019-05-14 11:46:28 +02002379 (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002380 (h2s->flags & H2_SF_ES_RCVD) ||
2381 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002382 /* we may have to signal the upper layers */
2383 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002384 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002385 }
2386 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002387
Willy Tarreaud7901432017-12-29 11:34:40 +01002388 if (h2c->st0 == H2_CS_FRAME_E)
2389 goto strm_err;
2390
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002391 if (h2s->st == H2_SS_IDLE &&
2392 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2393 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2394 * this state MUST be treated as a connection error
2395 */
2396 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002397 if (!h2c->nb_streams) {
2398 /* only log if no other stream can report the error */
2399 sess_log(h2c->conn->owner);
2400 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002401 break;
2402 }
2403
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002404 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2405 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2406 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002407 * this state MUST be treated as a stream error.
2408 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2409 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002410 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002411 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2412 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2413 else
2414 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002415 goto strm_err;
2416 }
2417
Willy Tarreauab837502017-12-27 15:07:30 +01002418 /* Below the management of frames received in closed state is a
2419 * bit hackish because the spec makes strong differences between
2420 * streams closed by receiving RST, sending RST, and seeing ES
2421 * in both directions. In addition to this, the creation of a
2422 * new stream reusing the identifier of a closed one will be
2423 * detected here. Given that we cannot keep track of all closed
2424 * streams forever, we consider that unknown closed streams were
2425 * closed on RST received, which allows us to respond with an
2426 * RST without breaking the connection (eg: to abort a transfer).
2427 * Some frames have to be silently ignored as well.
2428 */
2429 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002430 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002431 /* #5.1.1: The identifier of a newly
2432 * established stream MUST be numerically
2433 * greater than all streams that the initiating
2434 * endpoint has opened or reserved. This
2435 * governs streams that are opened using a
2436 * HEADERS frame and streams that are reserved
2437 * using PUSH_PROMISE. An endpoint that
2438 * receives an unexpected stream identifier
2439 * MUST respond with a connection error.
2440 */
2441 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2442 goto strm_err;
2443 }
2444
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002445 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002446 /* RFC7540#5.1:closed: an endpoint that
2447 * receives any frame other than PRIORITY after
2448 * receiving a RST_STREAM MUST treat that as a
2449 * stream error of type STREAM_CLOSED.
2450 *
2451 * Note that old streams fall into this category
2452 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002453 *
2454 * However, we cannot generalize this to all frame types. Those
2455 * carrying compression state must still be processed before
2456 * being dropped or we'll desynchronize the decoder. This can
2457 * happen with request trailers received after sending an
2458 * RST_STREAM, or with header/trailers responses received after
2459 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002460 */
2461 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2462 h2c->st0 = H2_CS_FRAME_E;
2463 goto strm_err;
2464 }
2465
2466 /* RFC7540#5.1:closed: if this state is reached as a
2467 * result of sending a RST_STREAM frame, the peer that
2468 * receives the RST_STREAM might have already sent
2469 * frames on the stream that cannot be withdrawn. An
2470 * endpoint MUST ignore frames that it receives on
2471 * closed streams after it has sent a RST_STREAM
2472 * frame. An endpoint MAY choose to limit the period
2473 * over which it ignores frames and treat frames that
2474 * arrive after this time as being in error.
2475 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002476 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002477 /* RFC7540#5.1:closed: any frame other than
2478 * PRIO/WU/RST in this state MUST be treated as
2479 * a connection error
2480 */
2481 if (h2c->dft != H2_FT_RST_STREAM &&
2482 h2c->dft != H2_FT_PRIORITY &&
2483 h2c->dft != H2_FT_WINDOW_UPDATE) {
2484 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2485 goto strm_err;
2486 }
2487 }
2488 }
2489
Willy Tarreauc0da1962017-10-30 18:38:00 +01002490#if 0
2491 // problem below: it is not possible to completely ignore such
2492 // streams as we need to maintain the compression state as well
2493 // and for this we need to completely process these frames (eg:
2494 // HEADERS frames) as well as counting DATA frames to emit
2495 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2496 // This is a typical case of layer violation where the
2497 // transported contents are critical to the connection's
2498 // validity and must be ignored at the same time :-(
2499
2500 /* graceful shutdown, ignore streams whose ID is higher than
2501 * the one advertised in GOAWAY. RFC7540#6.8.
2502 */
2503 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002504 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2505 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002506 h2c->dfl -= ret;
2507 ret = h2c->dfl == 0;
2508 goto strm_err;
2509 }
2510#endif
2511
Willy Tarreau7e98c052017-10-10 15:56:59 +02002512 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002513 case H2_FT_SETTINGS:
2514 if (h2c->st0 == H2_CS_FRAME_P)
2515 ret = h2c_handle_settings(h2c);
2516
2517 if (h2c->st0 == H2_CS_FRAME_A)
2518 ret = h2c_ack_settings(h2c);
2519 break;
2520
Willy Tarreaucf68c782017-10-10 17:11:41 +02002521 case H2_FT_PING:
2522 if (h2c->st0 == H2_CS_FRAME_P)
2523 ret = h2c_handle_ping(h2c);
2524
2525 if (h2c->st0 == H2_CS_FRAME_A)
2526 ret = h2c_ack_ping(h2c);
2527 break;
2528
Willy Tarreau26f95952017-07-27 17:18:30 +02002529 case H2_FT_WINDOW_UPDATE:
2530 if (h2c->st0 == H2_CS_FRAME_P)
2531 ret = h2c_handle_window_update(h2c, h2s);
2532 break;
2533
Willy Tarreau61290ec2017-10-17 08:19:21 +02002534 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002535 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2536 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2537 * frames' parsers consume all following CONTINUATION
2538 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002539 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002540 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2541 sess_log(h2c->conn->owner);
2542 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002543
Willy Tarreau13278b42017-10-13 19:23:14 +02002544 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002545 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002546 if (h2c->flags & H2_CF_IS_BACK)
2547 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2548 else
2549 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002550 if (tmp_h2s) {
2551 h2s = tmp_h2s;
2552 ret = 1;
2553 }
2554 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002555 break;
2556
Willy Tarreau454f9052017-10-26 19:40:35 +02002557 case H2_FT_DATA:
2558 if (h2c->st0 == H2_CS_FRAME_P)
2559 ret = h2c_frt_handle_data(h2c, h2s);
2560
2561 if (h2c->st0 == H2_CS_FRAME_A)
2562 ret = h2c_send_strm_wu(h2c);
2563 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002564
Willy Tarreau92153fc2017-12-03 19:46:19 +01002565 case H2_FT_PRIORITY:
2566 if (h2c->st0 == H2_CS_FRAME_P)
2567 ret = h2c_handle_priority(h2c);
2568 break;
2569
Willy Tarreaucd234e92017-08-18 10:59:39 +02002570 case H2_FT_RST_STREAM:
2571 if (h2c->st0 == H2_CS_FRAME_P)
2572 ret = h2c_handle_rst_stream(h2c, h2s);
2573 break;
2574
Willy Tarreaue96b0922017-10-30 00:28:29 +01002575 case H2_FT_GOAWAY:
2576 if (h2c->st0 == H2_CS_FRAME_P)
2577 ret = h2c_handle_goaway(h2c);
2578 break;
2579
Willy Tarreau1c661982017-10-30 13:52:01 +01002580 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002581 default:
2582 /* drop frames that we ignore. They may be larger than
2583 * the buffer so we drain all of their contents until
2584 * we reach the end.
2585 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002586 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2587 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002588 h2c->dfl -= ret;
2589 ret = h2c->dfl == 0;
2590 }
2591
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002592 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002593 /* We may have to send an RST if not done yet */
2594 if (h2s->st == H2_SS_ERROR)
2595 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002596
Willy Tarreaua20a5192017-12-27 11:02:06 +01002597 if (h2c->st0 == H2_CS_FRAME_E)
2598 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002599
Willy Tarreau7e98c052017-10-10 15:56:59 +02002600 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002601 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002602 break;
2603
2604 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002605 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002606 h2c->st0 = H2_CS_FRAME_H;
2607 }
2608 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002609
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002610 if (h2c->rcvd_c > 0 &&
2611 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2612 h2c_send_conn_wu(h2c);
2613
Willy Tarreau52eed752017-09-22 15:05:09 +02002614 fail:
2615 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002616 if (h2s && h2s->cs &&
2617 (b_data(&h2s->rxbuf) ||
Willy Tarreau99ad1b32019-05-14 11:46:28 +02002618 (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS) ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002619 (h2s->flags & H2_SF_ES_RCVD) ||
2620 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002621 /* we may have to signal the upper layers */
2622 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002623 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002624 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002625
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002626 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002627}
2628
2629/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2630 * the end.
2631 */
2632static int h2_process_mux(struct h2c *h2c)
2633{
Olivier Houchard998410a2019-04-15 19:23:37 +02002634 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002635
Willy Tarreau01b44822018-10-03 14:26:37 +02002636 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2637 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2638 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2639 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2640 if (h2c->st0 == H2_CS_ERROR) {
2641 h2c->st0 = H2_CS_ERROR2;
2642 sess_log(h2c->conn->owner);
2643 }
2644 goto fail;
2645 }
2646 h2c->st0 = H2_CS_SETTINGS1;
2647 }
2648 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002649 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002650 return 1;
2651 }
2652
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002653 /* start by sending possibly pending window updates */
2654 if (h2c->rcvd_c > 0 &&
2655 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2656 h2c_send_conn_wu(h2c) < 0)
2657 goto fail;
2658
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002659 /* First we always process the flow control list because the streams
2660 * waiting there were already elected for immediate emission but were
2661 * blocked just on this.
2662 */
2663
Olivier Houchard998410a2019-04-15 19:23:37 +02002664 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002665 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2666 h2c->st0 >= H2_CS_ERROR)
2667 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002668
Willy Tarreauc234ae32019-05-13 17:56:11 +02002669 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002670 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002671
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002672 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002673 /* For some reason, the upper layer failed to subsribe again,
2674 * so remove it from the send_list
2675 */
2676 if (!h2s->send_wait) {
2677 LIST_DEL_INIT(&h2s->list);
2678 continue;
2679 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002680 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002681 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002682 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002683 }
2684
Olivier Houchard998410a2019-04-15 19:23:37 +02002685 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002686 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2687 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002688
Willy Tarreauc234ae32019-05-13 17:56:11 +02002689 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002690 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002691
Olivier Houchard998410a2019-04-15 19:23:37 +02002692 /* For some reason, the upper layer failed to subsribe again,
2693 * so remove it from the send_list
2694 */
2695 if (!h2s->send_wait) {
2696 LIST_DEL_INIT(&h2s->list);
2697 continue;
2698 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002699 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002700 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002701 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002702 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002703 }
2704
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002705 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002706 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002707 if (h2c->st0 == H2_CS_ERROR) {
2708 if (h2c->max_id >= 0) {
2709 h2c_send_goaway_error(h2c, NULL);
2710 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2711 return 0;
2712 }
2713
2714 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2715 }
2716 return 1;
2717 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002718 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002719}
2720
Willy Tarreau62f52692017-10-08 23:01:42 +02002721
Willy Tarreau479998a2018-11-18 06:30:59 +01002722/* Attempt to read data, and subscribe if none available.
2723 * The function returns 1 if data has been received, otherwise zero.
2724 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002725static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002726{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002727 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002728 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002729 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002730 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002731
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002732 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002733 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002734
Willy Tarreau315d8072017-12-10 22:17:57 +01002735 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002736 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002737
Willy Tarreau44e973f2018-03-01 17:49:30 +01002738 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002739 if (!buf) {
2740 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002741 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002742 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002743
Olivier Houchard7505f942018-08-21 18:10:44 +02002744 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002745 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002746 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2747 /* HTX in use : try to pre-align the buffer like the
2748 * rxbufs will be to optimize memory copies. We'll make
2749 * sure that the frame header lands at the end of the
2750 * HTX block to alias it upon recv. We cannot use the
2751 * head because rcv_buf() will realign the buffer if
2752 * it's empty. Thus we cheat and pretend we already
2753 * have a few bytes there.
2754 */
2755 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002756 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002757 }
2758 else
2759 max = b_room(buf);
2760
Olivier Houchard7505f942018-08-21 18:10:44 +02002761 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002762 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002763 else
2764 ret = 0;
2765 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002766
Olivier Houchard53216e72018-10-10 15:46:36 +02002767 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002768 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002769
Olivier Houcharda1411e62018-08-17 18:42:48 +02002770 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002771 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002772 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002773 }
2774
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002775 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002776 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002777 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002778}
2779
Willy Tarreau479998a2018-11-18 06:30:59 +01002780/* Try to send data if possible.
2781 * The function returns 1 if data have been sent, otherwise zero.
2782 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002783static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002784{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002785 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002786 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002787 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002788
2789 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002790 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002791
Olivier Houchard7505f942018-08-21 18:10:44 +02002792
Willy Tarreaua2af5122017-10-09 11:56:46 +02002793 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2794 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002795 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002796 }
2797
Willy Tarreaubc933932017-10-09 16:21:43 +02002798 /* This loop is quite simple : it tries to fill as much as it can from
2799 * pending streams into the existing buffer until it's reportedly full
2800 * or the end of send requests is reached. Then it tries to send this
2801 * buffer's contents out, marks it not full if at least one byte could
2802 * be sent, and tries again.
2803 *
2804 * The snd_buf() function normally takes a "flags" argument which may
2805 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2806 * data immediately comes and CO_SFL_STREAMER to indicate that the
2807 * connection is streaming lots of data (used to increase TLS record
2808 * size at the expense of latency). The former can be sent any time
2809 * there's a buffer full flag, as it indicates at least one stream
2810 * attempted to send and failed so there are pending data. An
2811 * alternative would be to set it as long as there's an active stream
2812 * but that would be problematic for ACKs until we have an absolute
2813 * guarantee that all waiters have at least one byte to send. The
2814 * latter should possibly not be set for now.
2815 */
2816
2817 done = 0;
2818 while (!done) {
2819 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002820 unsigned int released = 0;
2821 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02002822
2823 /* fill as much as we can into the current buffer */
2824 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2825 done = h2_process_mux(h2c);
2826
Olivier Houchard2b094432019-01-29 18:28:36 +01002827 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002828 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01002829
Willy Tarreaubc933932017-10-09 16:21:43 +02002830 if (conn->flags & CO_FL_ERROR)
2831 break;
2832
2833 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2834 flags |= CO_SFL_MSG_MORE;
2835
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002836 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
2837 if (b_data(buf)) {
2838 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002839 if (!ret) {
2840 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002841 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002842 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002843 sent = 1;
2844 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002845 if (b_data(buf)) {
2846 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002847 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002848 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002849 }
2850 b_free(buf);
2851 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02002852 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002853
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002854 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02002855 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002856
Willy Tarreaubc933932017-10-09 16:21:43 +02002857 /* wrote at least one byte, the buffer is not full anymore */
2858 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2859 }
2860
Willy Tarreaua2af5122017-10-09 11:56:46 +02002861 if (conn->flags & CO_FL_SOCK_WR_SH) {
2862 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02002863 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002864 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002865 /* We're not full anymore, so we can wake any task that are waiting
2866 * for us.
2867 */
2868 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002869 struct h2s *h2s;
2870
2871 list_for_each_entry(h2s, &h2c->send_list, list) {
2872 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2873 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002874
Willy Tarreauc234ae32019-05-13 17:56:11 +02002875 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002876 continue;
2877
Olivier Houchard998410a2019-04-15 19:23:37 +02002878 /* For some reason, the upper layer failed to subsribe again,
2879 * so remove it from the send_list
2880 */
2881 if (!h2s->send_wait) {
2882 LIST_DEL_INIT(&h2s->list);
2883 continue;
2884 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002885 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002886 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002887 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002888 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002889 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002890 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002891 /* We're done, no more to send */
Willy Tarreau662fafc2019-05-26 09:43:07 +02002892 if (!br_data(h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002893 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002894schedule:
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002895 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002896 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002897
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002898 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002899}
2900
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002901/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002902static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2903{
2904 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002905 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002906
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002907 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002908 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002909 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002910 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002911 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002912 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002913 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002914}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002915
Willy Tarreau62f52692017-10-08 23:01:42 +02002916/* callback called on any event by the connection handler.
2917 * It applies changes and returns zero, or < 0 if it wants immediate
2918 * destruction of the connection (which normally doesn not happen in h2).
2919 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002920static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002921{
Olivier Houchard7505f942018-08-21 18:10:44 +02002922 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002923
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002924 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002925 h2_process_demux(h2c);
2926
2927 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002928 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002929
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002930 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002931 h2c->flags &= ~H2_CF_DEM_DFULL;
2932 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002933 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002934
Willy Tarreau0b37d652018-10-03 10:33:02 +02002935 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002936 /* frontend is stopping, reload likely in progress, let's try
2937 * to announce a graceful shutdown if not yet done. We don't
2938 * care if it fails, it will be tried again later.
2939 */
2940 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2941 if (h2c->last_sid < 0)
2942 h2c->last_sid = (1U << 31) - 1;
2943 h2c_send_goaway_error(h2c, NULL);
2944 }
2945 }
2946
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002947 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002948 * If we received early data, and the handshake is done, wake
2949 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002950 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002951 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2952 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2953 struct eb32_node *node;
2954 struct h2s *h2s;
2955
2956 h2c->flags |= H2_CF_WAIT_FOR_HS;
2957 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2958
2959 while (node) {
2960 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002961 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002962 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002963 node = eb32_next(node);
2964 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002965 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002966
Willy Tarreau26bd7612017-10-09 16:47:04 +02002967 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002968 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2969 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2970 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02002971 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002972
2973 if (eb_is_empty(&h2c->streams_by_id)) {
2974 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002975 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002976 return -1;
2977 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002978 }
2979
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002980 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002981 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002982
Olivier Houchard53216e72018-10-10 15:46:36 +02002983 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2984 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2985 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02002986 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02002987 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2988 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02002989 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002990
Willy Tarreau3f133572017-10-31 19:21:06 +01002991 if (h2c->task) {
Willy Tarreau662fafc2019-05-26 09:43:07 +02002992 if (eb_is_empty(&h2c->streams_by_id) || br_data(h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002993 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002994 task_queue(h2c->task);
2995 }
2996 else
2997 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002998 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002999
Olivier Houchard7505f942018-08-21 18:10:44 +02003000 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02003001 return 0;
3002}
3003
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003004/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003005static int h2_wake(struct connection *conn)
3006{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003007 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003008
3009 return (h2_process(h2c));
3010}
3011
Willy Tarreauea392822017-10-31 10:02:25 +01003012/* Connection timeout management. The principle is that if there's no receipt
3013 * nor sending for a certain amount of time, the connection is closed. If the
3014 * MUX buffer still has lying data or is not allocatable, the connection is
3015 * immediately killed. If it's allocatable and empty, we attempt to send a
3016 * GOAWAY frame.
3017 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003018static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003019{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003020 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003021 int expired = tick_is_expired(t->expire, now_ms);
3022
Willy Tarreau0975f112018-03-29 15:22:59 +02003023 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01003024 return t;
3025
Olivier Houchard3f795f72019-04-17 22:51:06 +02003026 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003027
3028 if (!h2c) {
3029 /* resources were already deleted */
3030 return NULL;
3031 }
3032
3033 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003034 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003035 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003036
Willy Tarreau662fafc2019-05-26 09:43:07 +02003037 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003038 /* don't even try to send a GOAWAY, the buffer is stuck */
3039 h2c->flags |= H2_CF_GOAWAY_FAILED;
3040 }
3041
3042 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003043 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003044 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3045 h2c->flags |= H2_CF_GOAWAY_FAILED;
3046
Willy Tarreau662fafc2019-05-26 09:43:07 +02003047 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003048 unsigned int released = 0;
3049 struct buffer *buf;
3050
3051 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3052 if (b_data(buf)) {
3053 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3054 if (!ret)
3055 break;
3056 b_del(buf, ret);
3057 if (b_data(buf))
3058 break;
3059 b_free(buf);
3060 released++;
3061 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003062 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003063
3064 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003065 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003066 }
Willy Tarreauea392822017-10-31 10:02:25 +01003067
Willy Tarreau0975f112018-03-29 15:22:59 +02003068 /* either we can release everything now or it will be done later once
3069 * the last stream closes.
3070 */
3071 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003072 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003073
Willy Tarreauea392822017-10-31 10:02:25 +01003074 return NULL;
3075}
3076
3077
Willy Tarreau62f52692017-10-08 23:01:42 +02003078/*******************************************/
3079/* functions below are used by the streams */
3080/*******************************************/
3081
3082/*
3083 * Attach a new stream to a connection
3084 * (Used for outgoing connections)
3085 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003086static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003087{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003088 struct conn_stream *cs;
3089 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003090 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003091
3092 cs = cs_new(conn);
3093 if (!cs)
3094 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003095 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003096 if (!h2s) {
3097 cs_free(cs);
3098 return NULL;
3099 }
3100 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003101}
3102
Willy Tarreaufafd3982018-11-18 21:29:20 +01003103/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3104 * We have to scan because we may have some orphan streams. It might be
3105 * beneficial to scan backwards from the end to reduce the likeliness to find
3106 * orphans.
3107 */
3108static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3109{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003110 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003111 struct h2s *h2s;
3112 struct eb32_node *node;
3113
3114 node = eb32_first(&h2c->streams_by_id);
3115 while (node) {
3116 h2s = container_of(node, struct h2s, by_id);
3117 if (h2s->cs)
3118 return h2s->cs;
3119 node = eb32_next(node);
3120 }
3121 return NULL;
3122}
3123
Willy Tarreau62f52692017-10-08 23:01:42 +02003124/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003125 * Destroy the mux and the associated connection, if it is no longer used
3126 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003127static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003128{
Christopher Faulet73c12072019-04-08 11:23:22 +02003129 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003130
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003131 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003132 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003133}
3134
3135/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003136 * Detach the stream from the connection and possibly release the connection.
3137 */
3138static void h2_detach(struct conn_stream *cs)
3139{
Willy Tarreau60935142017-10-16 18:11:19 +02003140 struct h2s *h2s = cs->ctx;
3141 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003142 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003143
3144 cs->ctx = NULL;
3145 if (!h2s)
3146 return;
3147
Olivier Houchard998410a2019-04-15 19:23:37 +02003148 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003149 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003150 h2s->send_wait != &h2s->wait_event) {
3151 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
3152 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003153 /*
3154 * At this point, the stream_interface is supposed to have called
3155 * h2_unsubscribe(), so the only way there's still a
3156 * subscription that came from the stream_interface (as we
3157 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3158 * without the stream_interface involved) is that we subscribed
3159 * for sending, we woke the tasklet up and removed the
3160 * SUB_RETRY_SEND flag, so the stream_interface would not
3161 * know it has to unsubscribe for send, but the tasklet hasn't
3162 * run yet. Make sure to handle that by explicitely setting
3163 * send_wait to NULL, as nothing else will do it for us.
3164 */
3165 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003166 }
3167
Olivier Houchardf502aca2018-12-14 19:42:40 +01003168 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003169 h2c = h2s->h2c;
3170 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003171 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003172 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3173 !h2_frt_has_too_many_cs(h2c)) {
3174 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003175 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003176 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003177 }
Willy Tarreau60935142017-10-16 18:11:19 +02003178
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003179 /* this stream may be blocked waiting for some data to leave (possibly
3180 * an ES or RST frame), so orphan it in this case.
3181 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003182 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003183 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003184 (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 +01003185 return;
3186
Willy Tarreau45f752e2017-10-30 15:44:59 +01003187 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3188 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3189 /* unblock the connection if it was blocked on this
3190 * stream.
3191 */
3192 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3193 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003194 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003195 }
3196
Willy Tarreau71049cc2018-03-28 13:56:39 +02003197 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003198
Olivier Houchard8a786902018-12-15 16:05:40 +01003199 if (h2c->flags & H2_CF_IS_BACK &&
3200 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003201 if (!(h2c->conn->flags &
3202 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3203 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003204 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003205 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3206 h2c->conn->owner = NULL;
3207 if (eb_is_empty(&h2c->streams_by_id)) {
3208 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3209 /* The server doesn't want it, let's kill the connection right away */
3210 h2c->conn->mux->destroy(h2c->conn);
3211 return;
3212 }
3213 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003214 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003215 if (eb_is_empty(&h2c->streams_by_id)) {
3216 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3217 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3218 return;
3219 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003220 /* Never ever allow to reuse a connection from a non-reuse backend */
3221 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3222 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003223 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003224 struct server *srv = objt_server(h2c->conn->target);
3225
3226 if (srv) {
3227 if (h2c->conn->flags & CO_FL_PRIVATE)
3228 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3229 else
3230 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3231 }
3232
3233 }
3234 }
3235 }
3236
Willy Tarreaue323f342018-03-28 13:51:45 +02003237 /* We don't want to close right now unless we're removing the
3238 * last stream, and either the connection is in error, or it
3239 * reached the ID already specified in a GOAWAY frame received
3240 * or sent (as seen by last_sid >= 0).
3241 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003242 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003243 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003244 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003245 }
3246 else if (h2c->task) {
Willy Tarreau662fafc2019-05-26 09:43:07 +02003247 if (eb_is_empty(&h2c->streams_by_id) || br_data(h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003248 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3249 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003250 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003251 else
3252 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003253 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003254}
3255
Willy Tarreau88bdba32019-05-13 18:17:53 +02003256/* Performs a synchronous or asynchronous shutr(). */
3257static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003258{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003259 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003260 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003261
Willy Tarreauf983d002019-05-14 10:40:21 +02003262 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003263 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003264
Willy Tarreau18059042019-01-31 19:12:48 +01003265 /* a connstream may require us to immediately kill the whole connection
3266 * for example because of a "tcp-request content reject" rule that is
3267 * normally used to limit abuse. In this case we schedule a goaway to
3268 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003269 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003270 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003271 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3272 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3273 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3274 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003275 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3276 /* Nothing was never sent for this stream, so reset with
3277 * REFUSED_STREAM error to let the client retry the
3278 * request.
3279 */
3280 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3281 }
Willy Tarreau18059042019-01-31 19:12:48 +01003282
Willy Tarreau90c32322017-11-24 08:00:30 +01003283 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003284 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003285 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003286
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003287 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003288 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003289 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003290 done:
3291 h2s->flags &= ~H2_SF_WANT_SHUTR;
3292 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003293add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003294 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003295 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003296 if (h2s->flags & H2_SF_BLK_MFCTL) {
3297 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3298 h2s->send_wait = sw;
3299 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3300 h2s->send_wait = sw;
3301 LIST_ADDQ(&h2c->send_list, &h2s->list);
3302 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003303 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003304 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003305 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003306 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003307}
3308
Willy Tarreau88bdba32019-05-13 18:17:53 +02003309/* Performs a synchronous or asynchronous shutw(). */
3310static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003311{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003312 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003313 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003314
Willy Tarreauf983d002019-05-14 10:40:21 +02003315 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003316 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003317
Willy Tarreauf983d002019-05-14 10:40:21 +02003318 if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR &&
3319 (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003320 /* we can cleanly close using an empty data frame only after headers */
3321
3322 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3323 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003324 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003325
3326 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003327 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003328 else
3329 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003330 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003331 /* a connstream may require us to immediately kill the whole connection
3332 * for example because of a "tcp-request content reject" rule that is
3333 * normally used to limit abuse. In this case we schedule a goaway to
3334 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003335 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003336 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003337 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3338 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3339 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3340 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003341 else {
3342 /* Nothing was never sent for this stream, so reset with
3343 * REFUSED_STREAM error to let the client retry the
3344 * request.
3345 */
3346 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3347 }
Willy Tarreau18059042019-01-31 19:12:48 +01003348
Willy Tarreau90c32322017-11-24 08:00:30 +01003349 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003350 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003351 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003352
Willy Tarreau00dd0782018-03-01 16:31:34 +01003353 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003354 }
3355
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003356 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003357 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003358 done:
3359 h2s->flags &= ~H2_SF_WANT_SHUTW;
3360 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003361
3362 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003363 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003364 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003365 if (h2s->flags & H2_SF_BLK_MFCTL) {
3366 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3367 h2s->send_wait = sw;
3368 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3369 h2s->send_wait = sw;
3370 LIST_ADDQ(&h2c->send_list, &h2s->list);
3371 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003372 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003373 /* let the handler know we want to shutw */
3374 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003375 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003376}
3377
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003378/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3379 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3380 * and prevented the last frame from being emitted.
3381 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003382static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3383{
3384 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003385 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003386
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003387 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003388 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003389 h2_do_shutw(h2s);
3390
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003391 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003392 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003393
Willy Tarreau88bdba32019-05-13 18:17:53 +02003394 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003395 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003396 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003397
Willy Tarreau88bdba32019-05-13 18:17:53 +02003398 if (!h2s->cs) {
3399 h2s_destroy(h2s);
3400 if (h2c_is_dead(h2c))
3401 h2_release(h2c);
3402 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003403 }
3404
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003405 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003406}
3407
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003408/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003409static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3410{
3411 struct h2s *h2s = cs->ctx;
3412
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003413 if (cs->flags & CS_FL_KILL_CONN)
3414 h2s->flags |= H2_SF_KILL_CONN;
3415
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003416 if (!mode)
3417 return;
3418
3419 h2_do_shutr(h2s);
3420}
3421
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003422/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003423static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3424{
3425 struct h2s *h2s = cs->ctx;
3426
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003427 if (cs->flags & CS_FL_KILL_CONN)
3428 h2s->flags |= H2_SF_KILL_CONN;
3429
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003430 h2_do_shutw(h2s);
3431}
3432
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003433/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003434 * HTX request or response depending on the connection's side. Returns a
3435 * positive value on success, a negative value on failure, or 0 if it couldn't
3436 * proceed. May report connection errors in h2c->errcode if the frame is
3437 * non-decodable and the connection unrecoverable. In absence of connection
3438 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003439 *
3440 * The function may fold CONTINUATION frames into the initial HEADERS frame
3441 * by removing padding and next frame header, then moving the CONTINUATION
3442 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3443 * leaving a hole between the main frame and the beginning of the next one.
3444 * The possibly remaining incomplete or next frame at the end may be moved
3445 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3446 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3447 *
3448 * A buffer at the beginning of processing may look like this :
3449 *
3450 * ,---.---------.-----.--------------.--------------.------.---.
3451 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3452 * `---^---------^-----^--------------^--------------^------^---'
3453 * | | <-----> | |
3454 * area | dpl | wrap
3455 * |<--------------> |
3456 * | dfl |
3457 * |<-------------------------------------------------->|
3458 * head data
3459 *
3460 * Padding is automatically overwritten when folding, participating to the
3461 * hole size after dfl :
3462 *
3463 * ,---.------------------------.-----.--------------.------.---.
3464 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3465 * `---^------------------------^-----^--------------^------^---'
3466 * | | <-----> | |
3467 * area | hole | wrap
3468 * |<-----------------------> |
3469 * | dfl |
3470 * |<-------------------------------------------------->|
3471 * head data
3472 *
3473 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3474 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3475 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003476 *
3477 * The <flags> field must point to either the stream's flags or to a copy of it
3478 * so that the function can update the following flags :
3479 * - H2_SF_DATA_CLEN when content-length is seen
3480 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3481 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003482 *
3483 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3484 * decoding, in order to detect if we're dealing with a headers or a trailers
3485 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003486 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003487static 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 +02003488{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003489 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003490 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003491 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003492 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003493 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003494 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003495 int flen; // header frame len
3496 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003497 int ret = 0;
3498 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003499 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003500 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003501
Willy Tarreauea18f862018-12-22 20:19:26 +01003502next_frame:
3503 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3504 goto leave; // incomplete input frame
3505
3506 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3507 * this case, we'll try to paste it immediately after the initial
3508 * HEADERS frame payload and kill any possible padding. The initial
3509 * frame's length will be increased to represent the concatenation
3510 * of the two frames. The next frame is read from position <tlen>
3511 * and written at position <flen> (minus padding if some is present).
3512 */
3513 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3514 struct h2_fh hdr;
3515 int clen; // CONTINUATION frame's payload length
3516
3517 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3518 /* no more data, the buffer may be full, either due to
3519 * too large a frame or because of too large a hole that
3520 * we're going to compact at the end.
3521 */
3522 goto leave;
3523 }
3524
3525 if (hdr.ft != H2_FT_CONTINUATION) {
3526 /* RFC7540#6.10: frame of unexpected type */
3527 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3528 goto fail;
3529 }
3530
3531 if (hdr.sid != h2c->dsi) {
3532 /* RFC7540#6.10: frame of different stream */
3533 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3534 goto fail;
3535 }
3536
3537 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3538 /* RFC7540#4.2: invalid frame length */
3539 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3540 goto fail;
3541 }
3542
3543 /* detect when we must stop aggragating frames */
3544 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3545
3546 /* Take as much as we can of the CONTINUATION frame's payload */
3547 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3548 if (clen > hdr.len)
3549 clen = hdr.len;
3550
3551 /* Move the frame's payload over the padding, hole and frame
3552 * header. At least one of hole or dpl is null (see diagrams
3553 * above). The hole moves after the new aggragated frame.
3554 */
3555 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3556 h2c->dfl += clen - h2c->dpl;
3557 hole += h2c->dpl + 9;
3558 h2c->dpl = 0;
3559 goto next_frame;
3560 }
3561
3562 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003563
Willy Tarreau13278b42017-10-13 19:23:14 +02003564 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003565 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003566 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003567 copy = alloc_trash_chunk();
3568 if (!copy) {
3569 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3570 goto fail;
3571 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003572 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3573 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3574 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003575 }
3576
Willy Tarreau13278b42017-10-13 19:23:14 +02003577 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3578 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003579 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003580 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3581 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003582 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003583 }
3584
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003585 if (flen < 5) {
3586 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3587 goto fail;
3588 }
3589
Willy Tarreau13278b42017-10-13 19:23:14 +02003590 hdrs += 5; // stream dep = 4, weight = 1
3591 flen -= 5;
3592 }
3593
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003594 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003595 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003596 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003597 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003598
Willy Tarreau937f7602018-02-26 15:22:17 +01003599 /* we can't retry a failed decompression operation so we must be very
3600 * careful not to take any risks. In practice the output buffer is
3601 * always empty except maybe for trailers, in which case we simply have
3602 * to wait for the upper layer to finish consuming what is available.
3603 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003604
3605 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003606 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003607 if (!htx_is_empty(htx)) {
3608 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003609 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003610 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003611 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003612 if (b_data(rxbuf)) {
3613 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003614 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003615 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003616
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003617 rxbuf->head = 0;
3618 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003619 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003620
Willy Tarreau25919232019-01-03 14:48:18 +01003621 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003622 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3623 sizeof(list)/sizeof(list[0]), tmp);
3624 if (outlen < 0) {
3625 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3626 goto fail;
3627 }
3628
Willy Tarreau25919232019-01-03 14:48:18 +01003629 /* The PACK decompressor was updated, let's update the input buffer and
3630 * the parser's state to commit these changes and allow us to later
3631 * fail solely on the stream if needed.
3632 */
3633 b_del(&h2c->dbuf, h2c->dfl + hole);
3634 h2c->dfl = hole = 0;
3635 h2c->st0 = H2_CS_FRAME_H;
3636
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003637 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003638 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003639
Willy Tarreau88d138e2019-01-02 19:38:14 +01003640 if (*flags & H2_SF_HEADERS_RCVD)
3641 goto trailers;
3642
3643 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003644 if (htx) {
3645 /* HTX mode */
3646 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003647 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003648 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003649 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003650 } else {
3651 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003652 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003653 if (outlen > 0)
3654 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003655 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003656
3657 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003658 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003659 goto fail;
3660 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003661
Willy Tarreau174b06a2018-04-25 18:13:58 +02003662 if (msgf & H2_MSGF_BODY) {
3663 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003664 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003665 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003666 if (htx)
3667 htx->extra = *body_len;
3668 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003669 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003670 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003671 }
3672
Willy Tarreau88d138e2019-01-02 19:38:14 +01003673 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003674 /* indicate that a HEADERS frame was received for this stream, except
3675 * for 1xx responses. For 1xx responses, another HEADERS frame is
3676 * expected.
3677 */
3678 if (!(msgf & H2_MSGF_RSP_1XX))
3679 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003680
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02003681 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003682 /* Mark the end of message, either using EOM in HTX or with the
3683 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003684 * is not set during headers with END_STREAM. For HTX trailers,
3685 * we must not leave an HTX trailers block not followed by an
3686 * EOM block, the two must be atomic. Thus if we fail to emit
3687 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003688 */
3689 if (htx) {
Willy Tarreau2135f912019-05-07 11:17:32 +02003690 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3691 struct htx_blk *tail = htx_get_tail_blk(htx);
3692
3693 if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR)
3694 htx_remove_blk(htx, tail);
Willy Tarreau88d138e2019-01-02 19:38:14 +01003695 goto fail;
Willy Tarreau2135f912019-05-07 11:17:32 +02003696 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01003697 }
3698 else if (*flags & H2_SF_DATA_CHNK) {
3699 if (!b_putblk(rxbuf, "\r\n", 2))
3700 goto fail;
3701 }
3702 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003703
Willy Tarreau86277d42019-01-02 15:36:11 +01003704 /* success */
3705 ret = 1;
3706
Willy Tarreau68dd9852017-07-03 14:44:26 +02003707 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003708 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003709 * move the remaining data over it.
3710 */
3711 if (hole) {
3712 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3713 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3714 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3715 b_sub(&h2c->dbuf, hole);
3716 }
3717
Willy Tarreau97215ca2019-04-29 10:20:21 +02003718 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003719 /* too large frames */
3720 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003721 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003722 }
3723
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003724 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003725 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003726 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003727 return ret;
3728
Willy Tarreau68dd9852017-07-03 14:44:26 +02003729 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003730 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003731 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003732
3733 trailers:
3734 /* This is the last HEADERS frame hence a trailer */
3735
3736 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3737 /* It's a trailer but it's missing ES flag */
3738 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3739 goto fail;
3740 }
3741
3742 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3743 * block, and when using chunks we must send the 0 CRLF marker. For
3744 * other modes, the trailers are silently dropped.
3745 */
3746 if (htx) {
3747 if (!htx_add_endof(htx, HTX_BLK_EOD))
3748 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003749 if (h2_make_htx_trailers(list, htx) <= 0)
3750 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003751 }
3752 else if (*flags & H2_SF_DATA_CHNK) {
3753 /* Legacy mode with chunked encoding : we must finalize the
3754 * data block message emit the trailing CRLF */
3755 if (!b_putblk(rxbuf, "0\r\n", 3))
3756 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003757
3758 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3759 if (outlen > 0)
3760 b_add(rxbuf, outlen);
3761 else
3762 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003763 }
3764
3765 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003766}
3767
Willy Tarreau454f9052017-10-26 19:40:35 +02003768/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3769 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3770 * in use, a new chunk is emitted for each frame. This is supposed to fit
3771 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3772 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3773 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003774 * parser state is automatically updated. Returns > 0 if it could completely
3775 * send the current frame, 0 if it couldn't complete, in which case
3776 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3777 * DATA frame can return 0 as a valid result). Stream errors are reported in
3778 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3779 * have checked the frame header and ensured that the frame was complete or the
3780 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003781 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003782static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003783{
3784 struct h2c *h2c = h2s->h2c;
3785 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003786 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003787 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003788 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003789 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003790
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003791 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003792
Olivier Houchard638b7992018-08-16 15:41:52 +02003793 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003794 if (!csbuf) {
3795 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003796 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003797 }
3798
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003799try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003800 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003801 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3802 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003803 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003804 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003805
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003806 if (flen > b_data(&h2c->dbuf)) {
3807 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003808 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003809 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003810 }
3811
Willy Tarreaua9b77962019-01-31 07:23:00 +01003812 if (htx) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003813 unsigned int sent;
3814
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003815 block1 = htx_free_data_space(htx);
3816 if (!block1) {
3817 h2c->flags |= H2_CF_DEM_SFULL;
3818 goto fail;
3819 }
3820 if (flen > block1)
3821 flen = block1;
3822
3823 /* here, flen is the max we can copy into the output buffer */
3824 block1 = b_contig_data(&h2c->dbuf, 0);
3825 if (flen > block1)
3826 flen = block1;
3827
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003828 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003829
3830 b_del(&h2c->dbuf, flen);
3831 h2c->dfl -= flen;
3832 h2c->rcvd_c += flen;
3833 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003834
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003835 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003836 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003837 htx->extra = h2s->body_len;
3838 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003839
3840 if (sent < flen) {
3841 h2c->flags |= H2_CF_DEM_SFULL;
3842 goto fail;
3843 }
3844
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003845 goto try_again;
3846 }
Willy Tarreau455d5682019-05-24 19:42:18 +02003847 else if (unlikely(b_space_wraps(csbuf) &&
3848 flen + chklen <= b_room(csbuf) &&
3849 b_data(csbuf) <= MAX_DATA_REALIGN)) {
3850 /* it doesn't fit in a single block and the buffer is fragmented, if there are
3851 * not too many data in the buffer, let's defragment it and try
3852 * again.
Willy Tarreaud755ea62018-02-26 15:44:54 +01003853 */
3854 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003855 }
3856
Willy Tarreaueba10f22018-04-25 20:44:22 +02003857 /* chunked-encoding requires more room */
3858 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003859 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003860 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3861 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3862 (chklen < 1048576) ? 4 : 8;
3863 chklen += 4; // CRLF, CRLF
3864 }
3865
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003866 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003867 if (flen + chklen > b_room(csbuf)) {
3868 if (chklen >= b_room(csbuf)) {
3869 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003870 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003871 }
3872 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003873 }
3874
3875 if (h2s->flags & H2_SF_DATA_CHNK) {
3876 /* emit the chunk size */
3877 unsigned int chksz = flen;
3878 char str[10];
3879 char *beg;
3880
3881 beg = str + sizeof(str);
3882 *--beg = '\n';
3883 *--beg = '\r';
3884 do {
3885 *--beg = hextab[chksz & 0xF];
3886 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003887 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003888 }
3889
Willy Tarreau454f9052017-10-26 19:40:35 +02003890 /* Block1 is the length of the first block before the buffer wraps,
3891 * block2 is the optional second block to reach the end of the frame.
3892 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003893 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003894 if (block1 > flen)
3895 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003896 block2 = flen - block1;
3897
3898 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003899 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003900
3901 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003902 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003903
Willy Tarreaueba10f22018-04-25 20:44:22 +02003904 if (h2s->flags & H2_SF_DATA_CHNK) {
3905 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003906 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003907 }
3908
Willy Tarreau454f9052017-10-26 19:40:35 +02003909 /* now mark the input data as consumed (will be deleted from the buffer
3910 * by the caller when seeing FRAME_A after sending the window update).
3911 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003912 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003913 h2c->dfl -= flen;
3914 h2c->rcvd_c += flen;
3915 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3916
Willy Tarreau1915ca22019-01-24 11:49:37 +01003917 if (h2s->flags & H2_SF_DATA_CLEN)
3918 h2s->body_len -= flen;
3919
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003920 if (h2c->dfl > h2c->dpl) {
3921 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003922 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003923 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003924 }
3925
Willy Tarreau4a28da12018-01-04 14:41:00 +01003926 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003927 /* here we're done with the frame, all the payload (except padding) was
3928 * transferred.
3929 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003930
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003931 if (h2c->dff & H2_F_DATA_END_STREAM) {
3932 if (htx) {
3933 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3934 h2c->flags |= H2_CF_DEM_SFULL;
3935 goto fail;
3936 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003937 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003938 else if (h2s->flags & H2_SF_DATA_CHNK) {
3939 /* emit the trailing 0 CRLF CRLF */
3940 if (b_room(csbuf) < 5) {
3941 h2c->flags |= H2_CF_DEM_SFULL;
3942 goto fail;
3943 }
3944 chklen += 5;
3945 b_putblk(csbuf, "0\r\n\r\n", 5);
3946 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003947 }
3948
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003949 h2c->rcvd_c += h2c->dpl;
3950 h2c->rcvd_s += h2c->dpl;
3951 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003952 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003953 if (htx)
3954 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003955 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003956 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003957 if (htx)
3958 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003959 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003960}
3961
Willy Tarreau5dd17352018-06-14 13:33:30 +02003962/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3963 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3964 * number of bytes sent. The caller must check the stream's status to detect
3965 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003966 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003967static 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 +02003968{
3969 struct http_hdr list[MAX_HTTP_HDR];
3970 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003971 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003972 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02003973 struct buffer *mbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003974 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003975 int es_now = 0;
3976 int ret = 0;
3977 int hdr;
3978
3979 if (h2c_mux_busy(h2c, h2s)) {
3980 h2s->flags |= H2_SF_BLK_MBUSY;
3981 return 0;
3982 }
3983
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003984 /* First, try to parse the H1 response and index it into <list>.
3985 * NOTE! Since it comes from haproxy, we *know* that a response header
3986 * block does not wrap and we can safely read it this way without
3987 * having to realign the buffer.
3988 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003989 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003990 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003991 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003992 /* incomplete or invalid response, this is abnormal coming from
3993 * haproxy and may only result in a bad errorfile or bad Lua code
3994 * so that won't be fixed, raise an error now.
3995 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003996 * FIXME: we should instead add the ability to only return a
3997 * 502 bad gateway. But in theory this is not supposed to
3998 * happen.
3999 */
4000 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4001 ret = 0;
4002 goto end;
4003 }
4004
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004005 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02004006
4007 /* certain statuses have no body or an empty one, regardless of
4008 * what the headers say.
4009 */
4010 if (sl.st.status >= 100 && sl.st.status < 200) {
4011 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
4012 h1m->curr_len = h1m->body_len = 0;
4013 }
4014 else if (sl.st.status == 204 || sl.st.status == 304) {
4015 /* no contents, claim c-len is present and set to zero */
4016 h1m->flags &= ~H1_MF_CHNK;
4017 h1m->flags |= H1_MF_CLEN;
4018 h1m->curr_len = h1m->body_len = 0;
4019 }
4020
Willy Tarreaubcc45952019-05-26 10:05:50 +02004021 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004022 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004023 if (!h2_get_buf(h2c, mbuf)) {
4024 h2c->flags |= H2_CF_MUX_MALLOC;
4025 h2s->flags |= H2_SF_BLK_MROOM;
4026 return 0;
4027 }
4028
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004029 chunk_reset(&outbuf);
4030
4031 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004032 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4033 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004034 break;
4035 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004036 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004037 }
4038
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004039 if (outbuf.size < 9)
4040 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004041
4042 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004043 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4044 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4045 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004046
4047 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004048 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01004049 /* this is an unparsable response */
4050 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4051 ret = 0;
4052 goto end;
4053 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01004054
4055 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004056 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004057 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004058 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004059 }
4060
4061 /* encode all headers, stop at empty name */
4062 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01004063 /* these ones do not exist in H2 and must be dropped. */
4064 if (isteq(list[hdr].n, ist("connection")) ||
4065 isteq(list[hdr].n, ist("proxy-connection")) ||
4066 isteq(list[hdr].n, ist("keep-alive")) ||
4067 isteq(list[hdr].n, ist("upgrade")) ||
4068 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004069 continue;
4070
4071 if (isteq(list[hdr].n, ist("")))
4072 break; // end
4073
4074 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4075 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004076 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004077 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004078 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004079 }
4080 }
4081
4082 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01004083 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004084 es_now = 1;
4085
4086 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004087 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004088
4089 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004090 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004091
4092 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004093 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004094
4095 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004096 b_add(mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01004097 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004098
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004099 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01004100 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004101 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01004102
Willy Tarreau801250e2018-09-11 11:45:04 +02004103 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004104 h2s->flags |= H2_SF_ES_SENT;
4105 if (h2s->st == H2_SS_OPEN)
4106 h2s->st = H2_SS_HLOC;
4107 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004108 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004109 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004110 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004111 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004112 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004113 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004114 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004115 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004116 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004117
4118 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004119
4120 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004121 //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 +02004122 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004123 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004124 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4125 goto retry;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004126 h1m_init_res(h1m);
4127 h1m->err_pos = -1; // don't care about errors on the response path
4128 h2c->flags |= H2_CF_MUX_MFULL;
4129 h2s->flags |= H2_SF_BLK_MROOM;
4130 ret = 0;
4131 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004132}
4133
Willy Tarreau5dd17352018-06-14 13:33:30 +02004134/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4135 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4136 * the number of bytes sent. The caller must check the stream's status to
4137 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004138 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004139static 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 +02004140{
4141 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004142 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004143 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004144 struct buffer *mbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004145 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004146 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004147 int es_now = 0;
4148 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004149 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004150 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004151
4152 if (h2c_mux_busy(h2c, h2s)) {
4153 h2s->flags |= H2_SF_BLK_MBUSY;
4154 goto end;
4155 }
4156
Willy Tarreaubcc45952019-05-26 10:05:50 +02004157 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004158 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004159 if (!h2_get_buf(h2c, mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004160 h2c->flags |= H2_CF_MUX_MALLOC;
4161 h2s->flags |= H2_SF_BLK_MROOM;
4162 goto end;
4163 }
4164
4165 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004166 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004167 goto end;
4168
4169 chunk_reset(&outbuf);
4170
4171 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004172 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4173 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004174 break;
4175 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004176 /* If there are pending data in the output buffer, and we have
4177 * less than 1/4 of the mbuf's size and everything fits, we'll
4178 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4179 * is full and wait, to save some slow realign calls.
4180 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004181 if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004182 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4183 goto retry;
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004184 h2c->flags |= H2_CF_MUX_MFULL;
4185 h2s->flags |= H2_SF_BLK_MROOM;
4186 goto end;
4187 }
4188
Willy Tarreaubcc45952019-05-26 10:05:50 +02004189 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004190 }
4191
4192 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004193 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4194 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004195 h2c->flags |= H2_CF_MUX_MFULL;
4196 h2s->flags |= H2_SF_BLK_MROOM;
4197 goto end;
4198 }
4199
4200 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004201 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4202 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4203 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004204
4205 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4206 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004207 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004208 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004209 break;
4210 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004211 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004212 if ((long long)size > h1m->curr_len)
4213 size = h1m->curr_len;
4214 break;
4215 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004216 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004217 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004218 if (!ret)
4219 goto end;
4220
4221 if (ret < 0) {
4222 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004223 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004224 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4225 goto end;
4226 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004227 max -= ret;
4228 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004229 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004230 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004231 }
4232
Willy Tarreau801250e2018-09-11 11:45:04 +02004233 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004234 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004235 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004236 if (!ret)
4237 goto end;
4238
4239 if (ret < 0) {
4240 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004241 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004242 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4243 goto end;
4244 }
4245
4246 size = chunk;
4247 h1m->curr_len = chunk;
4248 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004249 max -= ret;
4250 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004251 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004252 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004253 if (!size)
4254 goto send_empty;
4255 }
4256
4257 /* in MSG_DATA state, continue below */
4258 size = h1m->curr_len;
4259 break;
4260 }
4261
4262 /* we have in <size> the exact number of bytes we need to copy from
4263 * the H1 buffer. We need to check this against the connection's and
4264 * the stream's send windows, and to ensure that this fits in the max
4265 * frame size and in the buffer's available space minus 9 bytes (for
4266 * the frame header). The connection's flow control is applied last so
4267 * that we can use a separate list of streams which are immediately
4268 * unblocked on window opening. Note: we don't implement padding.
4269 */
4270
Willy Tarreau5dd17352018-06-14 13:33:30 +02004271 if (size > max)
4272 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004273
4274 if (size > h2s->mws)
4275 size = h2s->mws;
4276
4277 if (size <= 0) {
4278 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004279 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004280 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004281 goto end;
4282 }
4283
4284 if (h2c->mfs && size > h2c->mfs)
4285 size = h2c->mfs;
4286
4287 if (size + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02004288 /* It doesn't fit at once. If it at least fits once split and
4289 * the amount of data to move is low, let's defragment the
4290 * buffer now.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004291 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004292 if (b_space_wraps(mbuf) &&
4293 (size + 9 <= b_room(mbuf)) &&
4294 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004295 goto realign_again;
4296 size = outbuf.size - 9;
4297 }
4298
4299 if (size <= 0) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004300 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4301 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004302 h2c->flags |= H2_CF_MUX_MFULL;
4303 h2s->flags |= H2_SF_BLK_MROOM;
4304 goto end;
4305 }
4306
4307 if (size > h2c->mws)
4308 size = h2c->mws;
4309
4310 if (size <= 0) {
4311 h2s->flags |= H2_SF_BLK_MFCTL;
4312 goto end;
4313 }
4314
4315 /* copy whatever we can */
4316 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004317 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004318 if (ret == 1)
4319 len2 = 0;
4320
4321 if (!ret || len1 + len2 < size) {
4322 /* FIXME: must normally never happen */
4323 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4324 goto end;
4325 }
4326
4327 /* limit len1/len2 to size */
4328 if (len1 + len2 > size) {
4329 int sub = len1 + len2 - size;
4330
4331 if (len2 > sub)
4332 len2 -= sub;
4333 else {
4334 sub -= len2;
4335 len2 = 0;
4336 len1 -= sub;
4337 }
4338 }
4339
4340 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004341 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004342 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004343 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004344
4345 send_empty:
4346 /* we may need to add END_STREAM */
4347 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4348 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004349 *
4350 * FIXME: what we do here is not correct because we send end_stream
4351 * before knowing if we'll have to send a HEADERS frame for the
4352 * trailers. More importantly we're not consuming the trailing CRLF
4353 * after the end of trailers, so it will be left to the caller to
4354 * eat it. The right way to do it would be to measure trailers here
4355 * and to send ES only if there are no trailers.
4356 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004357 */
4358 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004359 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004360 es_now = 1;
4361
4362 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004363 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004364
4365 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004366 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004367
4368 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004369 b_add(mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004370
4371 /* consume incoming H1 response */
4372 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004373 max -= size;
4374 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004375 total += size;
4376 h1m->curr_len -= size;
4377 h2s->mws -= size;
4378 h2c->mws -= size;
4379
4380 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004381 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004382 goto new_frame;
4383 }
4384 }
4385
4386 if (es_now) {
4387 if (h2s->st == H2_SS_OPEN)
4388 h2s->st = H2_SS_HLOC;
4389 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004390 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004391
Willy Tarreau35a62702018-02-27 15:37:25 +01004392 if (!(h1m->flags & H1_MF_CHNK)) {
4393 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004394 total += max;
4395 ofs += max;
4396 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004397
Willy Tarreau801250e2018-09-11 11:45:04 +02004398 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004399 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004400
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004401 h2s->flags |= H2_SF_ES_SENT;
4402 }
4403
4404 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004405 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, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004406 return total;
4407}
4408
Willy Tarreau115e83b2018-12-01 19:17:53 +01004409/* Try to send a HEADERS frame matching HTX response present in HTX message
4410 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4411 * must check the stream's status to detect any error which might have happened
4412 * subsequently to a successful send. The htx blocks are automatically removed
4413 * from the message. The htx message is assumed to be valid since produced from
4414 * the internal code, hence it contains a start line, an optional series of
4415 * header blocks and an end of header, otherwise an invalid frame could be
4416 * emitted and the resulting htx message could be left in an inconsistent state.
4417 */
4418static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4419{
4420 struct http_hdr list[MAX_HTTP_HDR];
4421 struct h2c *h2c = h2s->h2c;
4422 struct htx_blk *blk;
4423 struct htx_blk *blk_end;
4424 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004425 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004426 struct htx_sl *sl;
4427 enum htx_blk_type type;
4428 int es_now = 0;
4429 int ret = 0;
4430 int hdr;
4431 int idx;
4432
4433 if (h2c_mux_busy(h2c, h2s)) {
4434 h2s->flags |= H2_SF_BLK_MBUSY;
4435 return 0;
4436 }
4437
Willy Tarreau115e83b2018-12-01 19:17:53 +01004438 /* determine the first block which must not be deleted, blk_end may
4439 * be NULL if all blocks have to be deleted.
4440 */
4441 idx = htx_get_head(htx);
4442 blk_end = NULL;
4443 while (idx != -1) {
4444 type = htx_get_blk_type(htx_get_blk(htx, idx));
4445 idx = htx_get_next(htx, idx);
4446 if (type == HTX_BLK_EOH) {
4447 if (idx != -1)
4448 blk_end = htx_get_blk(htx, idx);
4449 break;
4450 }
4451 }
4452
4453 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004454 blk = htx_get_head_blk(htx);
4455 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL);
4456 ALREADY_CHECKED(blk);
4457 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004458 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004459 if (h2s->status < 100 || h2s->status > 999)
4460 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004461
4462 /* and the rest of the headers, that we dump starting at header 0 */
4463 hdr = 0;
4464
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004465 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004466 while ((idx = htx_get_next(htx, idx)) != -1) {
4467 blk = htx_get_blk(htx, idx);
4468 type = htx_get_blk_type(blk);
4469
4470 if (type == HTX_BLK_UNUSED)
4471 continue;
4472
4473 if (type != HTX_BLK_HDR)
4474 break;
4475
4476 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4477 goto fail;
4478
4479 list[hdr].n = htx_get_blk_name(htx, blk);
4480 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004481 hdr++;
4482 }
4483
4484 /* marker for end of headers */
4485 list[hdr].n = ist("");
4486
4487 if (h2s->status == 204 || h2s->status == 304) {
4488 /* no contents, claim c-len is present and set to zero */
4489 es_now = 1;
4490 }
4491
Willy Tarreau9c218e72019-05-26 10:08:28 +02004492 mbuf = br_tail(h2c->mbuf);
4493 retry:
4494 if (!h2_get_buf(h2c, mbuf)) {
4495 h2c->flags |= H2_CF_MUX_MALLOC;
4496 h2s->flags |= H2_SF_BLK_MROOM;
4497 return 0;
4498 }
4499
Willy Tarreau115e83b2018-12-01 19:17:53 +01004500 chunk_reset(&outbuf);
4501
4502 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004503 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4504 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004505 break;
4506 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004507 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004508 }
4509
4510 if (outbuf.size < 9)
4511 goto full;
4512
4513 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4514 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4515 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4516 outbuf.data = 9;
4517
4518 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004519 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004520 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004521 goto realign_again;
4522 goto full;
4523 }
4524
4525 /* encode all headers, stop at empty name */
4526 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4527 /* these ones do not exist in H2 and must be dropped. */
4528 if (isteq(list[hdr].n, ist("connection")) ||
4529 isteq(list[hdr].n, ist("proxy-connection")) ||
4530 isteq(list[hdr].n, ist("keep-alive")) ||
4531 isteq(list[hdr].n, ist("upgrade")) ||
4532 isteq(list[hdr].n, ist("transfer-encoding")))
4533 continue;
4534
4535 if (isteq(list[hdr].n, ist("")))
4536 break; // end
4537
4538 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4539 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004540 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004541 goto realign_again;
4542 goto full;
4543 }
4544 }
4545
Christopher Faulet0b465482019-02-19 15:14:23 +01004546 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004547 * FIXME: we should also set it when we know for sure that the
4548 * content-length is zero as well as on 204/304
4549 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004550 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4551 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004552 es_now = 1;
4553
Willy Tarreau927b88b2019-03-04 08:03:25 +01004554 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004555 es_now = 1;
4556
4557 /* update the frame's size */
4558 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4559
4560 if (es_now)
4561 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4562
4563 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004564 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004565
4566 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4567 * 1xx responses, another HEADERS frame is expected.
4568 */
4569 if (h2s->status >= 200 || h2s->status == 101)
4570 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004571
Willy Tarreau115e83b2018-12-01 19:17:53 +01004572 if (es_now) {
4573 h2s->flags |= H2_SF_ES_SENT;
4574 if (h2s->st == H2_SS_OPEN)
4575 h2s->st = H2_SS_HLOC;
4576 else
4577 h2s_close(h2s);
4578 }
4579
4580 /* OK we could properly deliver the response */
4581
4582 /* remove all header blocks including the EOH and compute the
4583 * corresponding size.
4584 *
4585 * FIXME: We should remove everything when es_now is set.
4586 */
4587 ret = 0;
4588 idx = htx_get_head(htx);
4589 blk = htx_get_blk(htx, idx);
4590 while (blk != blk_end) {
4591 ret += htx_get_blksz(blk);
4592 blk = htx_remove_blk(htx, blk);
4593 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004594
Christopher Faulet316934d2019-05-15 14:22:48 +02004595 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4596 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004597 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004598 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004599 end:
4600 return ret;
4601 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004602 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4603 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004604 h2c->flags |= H2_CF_MUX_MFULL;
4605 h2s->flags |= H2_SF_BLK_MROOM;
4606 ret = 0;
4607 goto end;
4608 fail:
4609 /* unparsable HTX messages, too large ones to be produced in the local
4610 * list etc go here (unrecoverable errors).
4611 */
4612 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4613 ret = 0;
4614 goto end;
4615}
4616
Willy Tarreau80739692018-10-05 11:35:57 +02004617/* Try to send a HEADERS frame matching HTX request present in HTX message
4618 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4619 * must check the stream's status to detect any error which might have happened
4620 * subsequently to a successful send. The htx blocks are automatically removed
4621 * from the message. The htx message is assumed to be valid since produced from
4622 * the internal code, hence it contains a start line, an optional series of
4623 * header blocks and an end of header, otherwise an invalid frame could be
4624 * emitted and the resulting htx message could be left in an inconsistent state.
4625 */
4626static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4627{
4628 struct http_hdr list[MAX_HTTP_HDR];
4629 struct h2c *h2c = h2s->h2c;
4630 struct htx_blk *blk;
4631 struct htx_blk *blk_end;
4632 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004633 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004634 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004635 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004636 enum htx_blk_type type;
4637 int es_now = 0;
4638 int ret = 0;
4639 int hdr;
4640 int idx;
4641
4642 if (h2c_mux_busy(h2c, h2s)) {
4643 h2s->flags |= H2_SF_BLK_MBUSY;
4644 return 0;
4645 }
4646
Willy Tarreau80739692018-10-05 11:35:57 +02004647 /* determine the first block which must not be deleted, blk_end may
4648 * be NULL if all blocks have to be deleted.
4649 */
4650 idx = htx_get_head(htx);
4651 blk_end = NULL;
4652 while (idx != -1) {
4653 type = htx_get_blk_type(htx_get_blk(htx, idx));
4654 idx = htx_get_next(htx, idx);
4655 if (type == HTX_BLK_EOH) {
4656 if (idx != -1)
4657 blk_end = htx_get_blk(htx, idx);
4658 break;
4659 }
4660 }
4661
4662 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004663 blk = htx_get_head_blk(htx);
4664 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
4665 ALREADY_CHECKED(blk);
4666 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004667 meth = htx_sl_req_meth(sl);
4668 path = htx_sl_req_uri(sl);
4669
4670 /* and the rest of the headers, that we dump starting at header 0 */
4671 hdr = 0;
4672
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004673 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004674 while ((idx = htx_get_next(htx, idx)) != -1) {
4675 blk = htx_get_blk(htx, idx);
4676 type = htx_get_blk_type(blk);
4677
4678 if (type == HTX_BLK_UNUSED)
4679 continue;
4680
4681 if (type != HTX_BLK_HDR)
4682 break;
4683
4684 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4685 goto fail;
4686
4687 list[hdr].n = htx_get_blk_name(htx, blk);
4688 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004689 hdr++;
4690 }
4691
4692 /* marker for end of headers */
4693 list[hdr].n = ist("");
4694
Willy Tarreau9c218e72019-05-26 10:08:28 +02004695 mbuf = br_tail(h2c->mbuf);
4696 retry:
4697 if (!h2_get_buf(h2c, mbuf)) {
4698 h2c->flags |= H2_CF_MUX_MALLOC;
4699 h2s->flags |= H2_SF_BLK_MROOM;
4700 return 0;
4701 }
4702
Willy Tarreau80739692018-10-05 11:35:57 +02004703 chunk_reset(&outbuf);
4704
4705 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004706 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4707 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004708 break;
4709 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004710 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004711 }
4712
4713 if (outbuf.size < 9)
4714 goto full;
4715
4716 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4717 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4718 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4719 outbuf.data = 9;
4720
4721 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004722 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004723 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004724 goto realign_again;
4725 goto full;
4726 }
4727
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004728 /* RFC7540 #8.3: the CONNECT method must have :
4729 * - :authority set to the URI part (host:port)
4730 * - :method set to CONNECT
4731 * - :scheme and :path omitted
4732 */
4733 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4734 /* encode the scheme which is always "https" (or 0x86 for "http") */
4735 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4736 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004737 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004738 goto realign_again;
4739 goto full;
4740 }
Willy Tarreau80739692018-10-05 11:35:57 +02004741
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004742 /* encode the path, which necessarily is the second one */
4743 if (!hpack_encode_path(&outbuf, path)) {
4744 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004745 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004746 goto realign_again;
4747 goto full;
4748 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004749
4750 /* look for the Host header and place it in :authority */
4751 auth = ist2(NULL, 0);
4752 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4753 if (isteq(list[hdr].n, ist("")))
4754 break; // end
4755
4756 if (isteq(list[hdr].n, ist("host"))) {
4757 auth = list[hdr].v;
4758 break;
4759 }
4760 }
4761 }
4762 else {
4763 /* for CONNECT, :authority is taken from the path */
4764 auth = path;
4765 }
4766
4767 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4768 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004769 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004770 goto realign_again;
4771 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004772 }
4773
4774 /* encode all headers, stop at empty name */
4775 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4776 /* these ones do not exist in H2 and must be dropped. */
4777 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004778 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004779 isteq(list[hdr].n, ist("proxy-connection")) ||
4780 isteq(list[hdr].n, ist("keep-alive")) ||
4781 isteq(list[hdr].n, ist("upgrade")) ||
4782 isteq(list[hdr].n, ist("transfer-encoding")))
4783 continue;
4784
4785 if (isteq(list[hdr].n, ist("")))
4786 break; // end
4787
4788 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4789 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004790 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004791 goto realign_again;
4792 goto full;
4793 }
4794 }
4795
4796 /* we may need to add END_STREAM if we have no body :
4797 * - request already closed, or :
4798 * - no transfer-encoding, and :
4799 * - no content-length or content-length:0
4800 * Fixme: this doesn't take into account CONNECT requests.
4801 */
4802 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4803 es_now = 1;
4804
4805 if (sl->flags & HTX_SL_F_BODYLESS)
4806 es_now = 1;
4807
Willy Tarreau927b88b2019-03-04 08:03:25 +01004808 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004809 es_now = 1;
4810
4811 /* update the frame's size */
4812 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4813
4814 if (es_now)
4815 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4816
4817 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004818 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004819 h2s->flags |= H2_SF_HEADERS_SENT;
4820 h2s->st = H2_SS_OPEN;
4821
Willy Tarreau80739692018-10-05 11:35:57 +02004822 if (es_now) {
4823 // trim any possibly pending data (eg: inconsistent content-length)
4824 h2s->flags |= H2_SF_ES_SENT;
4825 h2s->st = H2_SS_HLOC;
4826 }
4827
4828 /* remove all header blocks including the EOH and compute the
4829 * corresponding size.
4830 *
4831 * FIXME: We should remove everything when es_now is set.
4832 */
4833 ret = 0;
4834 idx = htx_get_head(htx);
4835 blk = htx_get_blk(htx, idx);
4836 while (blk != blk_end) {
4837 ret += htx_get_blksz(blk);
4838 blk = htx_remove_blk(htx, blk);
4839 }
4840
Christopher Faulet316934d2019-05-15 14:22:48 +02004841 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4842 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004843 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004844 }
Willy Tarreau80739692018-10-05 11:35:57 +02004845
4846 end:
4847 return ret;
4848 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004849 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4850 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004851 h2c->flags |= H2_CF_MUX_MFULL;
4852 h2s->flags |= H2_SF_BLK_MROOM;
4853 ret = 0;
4854 goto end;
4855 fail:
4856 /* unparsable HTX messages, too large ones to be produced in the local
4857 * list etc go here (unrecoverable errors).
4858 */
4859 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4860 ret = 0;
4861 goto end;
4862}
4863
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004864/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004865 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4866 * caller must check the stream's status to detect any error which might have
4867 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004868 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4869 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004870static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004871{
4872 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004873 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004874 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004875 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004876 size_t total = 0;
4877 int es_now = 0;
4878 int bsize; /* htx block size */
4879 int fsize; /* h2 frame size */
4880 struct htx_blk *blk;
4881 enum htx_blk_type type;
4882 int idx;
4883
4884 if (h2c_mux_busy(h2c, h2s)) {
4885 h2s->flags |= H2_SF_BLK_MBUSY;
4886 goto end;
4887 }
4888
Willy Tarreau98de12a2018-12-12 07:03:00 +01004889 htx = htx_from_buf(buf);
4890
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004891 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4892 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4893 * the caller to handle.
4894 */
4895
4896 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004897 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004898 goto end;
4899
4900 idx = htx_get_head(htx);
4901 blk = htx_get_blk(htx, idx);
4902 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4903 bsize = htx_get_blksz(blk);
4904 fsize = bsize;
4905
4906 if (type == HTX_BLK_EOD) {
4907 /* if we have an EOD, we're dealing with chunked data. We may
4908 * have a set of trailers after us that the caller will want to
4909 * deal with. Let's simply remove the EOD and return.
4910 */
4911 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004912 total++; // EOD counts as one byte
4913 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004914 goto end;
4915 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004916 else if (type == HTX_BLK_EOM) {
4917 if (h2s->flags & H2_SF_ES_SENT) {
4918 /* ES already sent */
4919 htx_remove_blk(htx, blk);
4920 total++; // EOM counts as one byte
4921 count--;
4922 goto end;
4923 }
4924 }
4925 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004926 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004927
Willy Tarreau9c218e72019-05-26 10:08:28 +02004928 mbuf = br_tail(h2c->mbuf);
4929 retry:
4930 if (!h2_get_buf(h2c, mbuf)) {
4931 h2c->flags |= H2_CF_MUX_MALLOC;
4932 h2s->flags |= H2_SF_BLK_MROOM;
4933 goto end;
4934 }
4935
Willy Tarreau98de12a2018-12-12 07:03:00 +01004936 /* Perform some optimizations to reduce the number of buffer copies.
4937 * First, if the mux's buffer is empty and the htx area contains
4938 * exactly one data block of the same size as the requested count, and
4939 * this count fits within the frame size, the stream's window size, and
4940 * the connection's window size, then it's possible to simply swap the
4941 * caller's buffer with the mux's output buffer and adjust offsets and
4942 * length to match the entire DATA HTX block in the middle. In this
4943 * case we perform a true zero-copy operation from end-to-end. This is
4944 * the situation that happens all the time with large files. Second, if
4945 * this is not possible, but the mux's output buffer is empty, we still
4946 * have an opportunity to avoid the copy to the intermediary buffer, by
4947 * making the intermediary buffer's area point to the output buffer's
4948 * area. In this case we want to skip the HTX header to make sure that
4949 * copies remain aligned and that this operation remains possible all
4950 * the time. This goes for headers, data blocks and any data extracted
4951 * from the HTX blocks.
4952 */
4953 if (unlikely(fsize == count &&
4954 htx->used == 1 && type == HTX_BLK_DATA &&
4955 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004956 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004957
Willy Tarreaubcc45952019-05-26 10:05:50 +02004958 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004959 /* Too bad there are data left there. We're willing to memcpy/memmove
4960 * up to 1/4 of the buffer, which means that it's OK to copy a large
4961 * frame into a buffer containing few data if it needs to be realigned,
4962 * and that it's also OK to copy few data without realigning. Otherwise
4963 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004964 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004965 if (fsize + 9 <= b_room(mbuf) &&
4966 (b_data(mbuf) <= b_size(mbuf) / 4 ||
4967 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004968 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004969
Willy Tarreau9c218e72019-05-26 10:08:28 +02004970 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4971 goto retry;
4972
Willy Tarreau98de12a2018-12-12 07:03:00 +01004973 h2c->flags |= H2_CF_MUX_MFULL;
4974 h2s->flags |= H2_SF_BLK_MROOM;
4975 goto end;
4976 }
4977
4978 /* map an H2 frame to the HTX block so that we can put the
4979 * frame header there.
4980 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004981 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
4982 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01004983
4984 /* prepend an H2 DATA frame header just before the DATA block */
4985 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4986 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4987 h2_set_frame_size(outbuf.area, fsize);
4988
4989 /* update windows */
4990 h2s->mws -= fsize;
4991 h2c->mws -= fsize;
4992
4993 /* and exchange with our old area */
4994 buf->area = old_area;
4995 buf->data = buf->head = 0;
4996 total += fsize;
4997 goto end;
4998 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004999
Willy Tarreau98de12a2018-12-12 07:03:00 +01005000 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005001 /* for DATA and EOM we'll have to emit a frame, even if empty */
5002
5003 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005004 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5005 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005006 break;
5007 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005008 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005009 }
5010
5011 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005012 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5013 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005014 h2c->flags |= H2_CF_MUX_MFULL;
5015 h2s->flags |= H2_SF_BLK_MROOM;
5016 goto end;
5017 }
5018
5019 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5020 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5021 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5022 outbuf.data = 9;
5023
5024 /* we have in <fsize> the exact number of bytes we need to copy from
5025 * the HTX buffer. We need to check this against the connection's and
5026 * the stream's send windows, and to ensure that this fits in the max
5027 * frame size and in the buffer's available space minus 9 bytes (for
5028 * the frame header). The connection's flow control is applied last so
5029 * that we can use a separate list of streams which are immediately
5030 * unblocked on window opening. Note: we don't implement padding.
5031 */
5032
5033 /* EOM is presented with bsize==1 but would lead to the emission of an
5034 * empty frame, thus we force it to zero here.
5035 */
5036 if (type == HTX_BLK_EOM)
5037 bsize = fsize = 0;
5038
5039 if (!fsize)
5040 goto send_empty;
5041
5042 if (h2s->mws <= 0) {
5043 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005044 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005045 LIST_DEL_INIT(&h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005046 goto end;
5047 }
5048
Willy Tarreauee573762018-12-04 15:25:57 +01005049 if (fsize > count)
5050 fsize = count;
5051
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005052 if (fsize > h2s->mws)
5053 fsize = h2s->mws; // >0
5054
5055 if (h2c->mfs && fsize > h2c->mfs)
5056 fsize = h2c->mfs; // >0
5057
5058 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005059 /* It doesn't fit at once. If it at least fits once split and
5060 * the amount of data to move is low, let's defragment the
5061 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005062 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005063 if (b_space_wraps(mbuf) &&
5064 (fsize + 9 <= b_room(mbuf)) &&
5065 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005066 goto realign_again;
5067 fsize = outbuf.size - 9;
5068
5069 if (fsize <= 0) {
5070 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005071 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5072 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005073 h2c->flags |= H2_CF_MUX_MFULL;
5074 h2s->flags |= H2_SF_BLK_MROOM;
5075 goto end;
5076 }
5077 }
5078
5079 if (h2c->mws <= 0) {
5080 h2s->flags |= H2_SF_BLK_MFCTL;
5081 goto end;
5082 }
5083
5084 if (fsize > h2c->mws)
5085 fsize = h2c->mws;
5086
5087 /* now let's copy this this into the output buffer */
5088 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005089 h2s->mws -= fsize;
5090 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005091 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005092
5093 send_empty:
5094 /* update the frame's size */
5095 h2_set_frame_size(outbuf.area, fsize);
5096
5097 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5098 * meeting EOM. We should optimize this later.
5099 */
5100 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005101 total++; // EOM counts as one byte
5102 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005103 es_now = 1;
5104 }
5105
5106 if (es_now)
5107 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5108
5109 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005110 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005111
5112 /* consume incoming HTX block, including EOM */
5113 total += fsize;
5114 if (fsize == bsize) {
5115 htx_remove_blk(htx, blk);
5116 if (fsize)
5117 goto new_frame;
5118 } else {
5119 /* we've truncated this block */
5120 htx_cut_data_blk(htx, blk, fsize);
5121 }
5122
5123 if (es_now) {
5124 if (h2s->st == H2_SS_OPEN)
5125 h2s->st = H2_SS_HLOC;
5126 else
5127 h2s_close(h2s);
5128
5129 h2s->flags |= H2_SF_ES_SENT;
5130 }
5131
5132 end:
5133 return total;
5134}
5135
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005136/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5137 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5138 * processed. The caller must check the stream's status to detect any error
5139 * which might have happened subsequently to a successful send. The htx blocks
5140 * are automatically removed from the message. The htx message is assumed to be
5141 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005142 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005143 * as a single frame. The ES flag is always set.
5144 */
5145static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5146{
5147 struct http_hdr list[MAX_HTTP_HDR];
5148 struct h2c *h2c = h2s->h2c;
5149 struct htx_blk *blk;
5150 struct htx_blk *blk_end;
5151 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005152 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005153 struct h1m h1m;
5154 enum htx_blk_type type;
5155 uint32_t size;
5156 int ret = 0;
5157 int hdr;
5158 int idx;
5159 void *start;
5160
5161 if (h2c_mux_busy(h2c, h2s)) {
5162 h2s->flags |= H2_SF_BLK_MBUSY;
5163 goto end;
5164 }
5165
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005166 /* The principle is that we parse each and every trailers block using
5167 * the H1 headers parser, and append it to the list. We don't proceed
5168 * until EOM is met. blk_end will point to the EOM block.
5169 */
5170 hdr = 0;
5171 memset(list, 0, sizeof(list));
5172 blk_end = NULL;
5173
5174 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
5175 blk = htx_get_blk(htx, idx);
5176 type = htx_get_blk_type(blk);
5177
5178 if (type == HTX_BLK_UNUSED)
5179 continue;
5180
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005181 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005182 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005183
5184 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5185 goto fail;
5186
5187 size = htx_get_blksz(blk);
5188 start = htx_get_blk_ptr(htx, blk);
5189
5190 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5191 h1m.err_pos = 0;
5192 ret = h1_headers_to_hdr_list(start, start + size,
5193 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5194 &h1m, NULL);
5195 if (ret < 0)
5196 goto fail;
5197
5198 /* ret == 0 if an incomplete trailers block was found (missing
5199 * empty line), or > 0 if it was found. We have to continue on
5200 * incomplete messages because the trailers block might be
5201 * incomplete.
5202 */
5203
5204 /* search the new end */
5205 while (hdr <= sizeof(list)/sizeof(list[0])) {
5206 if (!list[hdr].n.len)
5207 break;
5208 hdr++;
5209 }
5210 }
5211
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005212 if (list[hdr].n.len != 0)
5213 goto fail; // empty trailer not found: internal error
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005214
Willy Tarreau9c218e72019-05-26 10:08:28 +02005215 mbuf = br_tail(h2c->mbuf);
5216 retry:
5217 if (!h2_get_buf(h2c, mbuf)) {
5218 h2c->flags |= H2_CF_MUX_MALLOC;
5219 h2s->flags |= H2_SF_BLK_MROOM;
5220 goto end;
5221 }
5222
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005223 chunk_reset(&outbuf);
5224
5225 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005226 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5227 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005228 break;
5229 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005230 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005231 }
5232
5233 if (outbuf.size < 9)
5234 goto full;
5235
5236 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5237 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5238 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5239 outbuf.data = 9;
5240
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005241 /* encode all headers */
5242 for (idx = 0; idx < hdr; idx++) {
5243 /* these ones do not exist in H2 or must not appear in
5244 * trailers and must be dropped.
5245 */
5246 if (isteq(list[idx].n, ist("host")) ||
5247 isteq(list[idx].n, ist("content-length")) ||
5248 isteq(list[idx].n, ist("connection")) ||
5249 isteq(list[idx].n, ist("proxy-connection")) ||
5250 isteq(list[idx].n, ist("keep-alive")) ||
5251 isteq(list[idx].n, ist("upgrade")) ||
5252 isteq(list[idx].n, ist("te")) ||
5253 isteq(list[idx].n, ist("transfer-encoding")))
5254 continue;
5255
5256 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5257 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005258 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005259 goto realign_again;
5260 goto full;
5261 }
5262 }
5263
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005264 if (outbuf.data == 9) {
5265 /* here we have a problem, we have nothing to emit (either we
5266 * received an empty trailers block followed or we removed its
5267 * contents above). Because of this we can't send a HEADERS
5268 * frame, so we have to cheat and instead send an empty DATA
5269 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005270 */
5271 outbuf.area[3] = H2_FT_DATA;
5272 outbuf.area[4] = H2_F_DATA_END_STREAM;
5273 }
5274
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005275 /* update the frame's size */
5276 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5277
5278 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005279 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005280 h2s->flags |= H2_SF_ES_SENT;
5281
5282 if (h2s->st == H2_SS_OPEN)
5283 h2s->st = H2_SS_HLOC;
5284 else
5285 h2s_close(h2s);
5286
5287 /* OK we could properly deliver the response */
5288 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005289 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005290 ret = 0;
5291 idx = htx_get_head(htx);
5292 blk = htx_get_blk(htx, idx);
5293 while (blk != blk_end) {
5294 ret += htx_get_blksz(blk);
5295 blk = htx_remove_blk(htx, blk);
5296 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005297 end:
5298 return ret;
5299 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005300 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5301 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005302 h2c->flags |= H2_CF_MUX_MFULL;
5303 h2s->flags |= H2_SF_BLK_MROOM;
5304 ret = 0;
5305 goto end;
5306 fail:
5307 /* unparsable HTX messages, too large ones to be produced in the local
5308 * list etc go here (unrecoverable errors).
5309 */
5310 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5311 ret = 0;
5312 goto end;
5313}
5314
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005315/* Called from the upper layer, to subscribe to events, such as being able to send.
5316 * The <param> argument here is supposed to be a pointer to a wait_event struct
5317 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5318 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5319 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5320 * returns 0 except for the error above.
5321 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005322static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5323{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005324 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005325 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005326 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005327
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005328 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005329 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005330 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5331 sw->events |= SUB_RETRY_RECV;
5332 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005333 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005334 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005335 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005336 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005337 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5338 sw->events |= SUB_RETRY_SEND;
5339 h2s->send_wait = sw;
5340 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5341 !LIST_ADDED(&h2s->list)) {
5342 if (h2s->flags & H2_SF_BLK_MFCTL)
5343 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5344 else
5345 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005346 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005347 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005348 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005349 if (event_type != 0)
5350 return -1;
5351 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005352}
5353
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005354/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5355 * The <param> argument here is supposed to be a pointer to the same wait_event
5356 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5357 * It always returns zero.
5358 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005359static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5360{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005361 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005362 struct h2s *h2s = cs->ctx;
5363
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005364 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005365 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005366 BUG_ON(h2s->recv_wait != sw);
5367 sw->events &= ~SUB_RETRY_RECV;
5368 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005369 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005370 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005371 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005372 BUG_ON(h2s->send_wait != sw);
5373 LIST_DEL(&h2s->list);
5374 LIST_INIT(&h2s->list);
5375 sw->events &= ~SUB_RETRY_SEND;
5376 /* We were about to send, make sure it does not happen */
5377 if (LIST_ADDED(&h2s->sending_list) &&
5378 h2s->send_wait != &h2s->wait_event) {
5379 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
5380 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005381 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005382 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005383 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005384 return 0;
5385}
5386
5387
Olivier Houchard511efea2018-08-16 15:30:32 +02005388/* Called from the upper layer, to receive data */
5389static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5390{
Olivier Houchard638b7992018-08-16 15:41:52 +02005391 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005392 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005393 struct htx *h2s_htx = NULL;
5394 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005395 size_t ret = 0;
5396
5397 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005398 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau86724e22018-12-01 23:19:43 +01005399 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005400 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005401 /* Here htx_to_buf() will set buffer data to 0 because
5402 * the HTX is empty.
5403 */
5404 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005405 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005406 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005407
Christopher Faulet156852b2019-05-16 11:29:13 +02005408 ret = h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005409 buf_htx = htx_from_buf(buf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005410
Christopher Faulet2f6edc82019-05-15 10:07:59 +02005411 /* <buf> is empty and the message is small enough, swap the
5412 * buffers. */
5413 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
5414 htx_to_buf(buf_htx, buf);
5415 htx_to_buf(h2s_htx, &h2s->rxbuf);
5416 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5417 goto end;
5418 }
5419
Christopher Faulet156852b2019-05-16 11:29:13 +02005420 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005421
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005422 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
Willy Tarreau7196dd62019-03-05 10:51:11 +01005423 buf_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005424 if (htx_is_empty(buf_htx))
5425 cs->flags |= CS_FL_EOI;
5426 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005427
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005428 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005429 htx_to_buf(buf_htx, buf);
5430 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet156852b2019-05-16 11:29:13 +02005431 ret -= h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005432 }
5433 else {
5434 ret = b_xfer(buf, &h2s->rxbuf, count);
5435 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005436
Christopher Faulet37070b22019-02-14 15:12:14 +01005437 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005438 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005439 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005440 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005441 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005442 if (h2s->flags & H2_SF_ES_RCVD)
5443 cs->flags |= CS_FL_EOI;
Willy Tarreau99ad1b32019-05-14 11:46:28 +02005444 if (H2_SS_MASK(h2s->st) & H2_SS_EOS_BITS)
Olivier Houchard511efea2018-08-16 15:30:32 +02005445 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005446 if (cs->flags & CS_FL_ERR_PENDING)
5447 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005448 if (b_size(&h2s->rxbuf)) {
5449 b_free(&h2s->rxbuf);
5450 offer_buffers(NULL, tasks_run_queue);
5451 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005452 }
5453
Willy Tarreau082f5592018-11-25 08:03:32 +01005454 if (ret && h2c->dsi == h2s->id) {
5455 /* demux is blocking on this stream's buffer */
5456 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005457 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005458 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005459
Olivier Houchard511efea2018-08-16 15:30:32 +02005460 return ret;
5461}
5462
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005463/* stops all senders of this connection for example when the mux buffer is full.
5464 * They are moved from the sending_list to either fctl_list or send_list.
5465 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005466static void h2_stop_senders(struct h2c *h2c)
5467{
5468 struct h2s *h2s, *h2s_back;
5469
Olivier Houchardd360ac62019-03-22 17:37:16 +01005470 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5471 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005472 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005473 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005474 }
5475}
5476
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005477/* Called from the upper layer, to send data from buffer <buf> for no more than
5478 * <count> bytes. Returns the number of bytes effectively sent. Some status
5479 * flags may be updated on the conn_stream.
5480 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005481static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005482{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005483 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005484 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005485 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005486 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005487 struct htx *htx;
5488 struct htx_blk *blk;
5489 enum htx_blk_type btype;
5490 uint32_t bsize;
5491 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005492
Olivier Houchardd360ac62019-03-22 17:37:16 +01005493 /* If we were not just woken because we wanted to send but couldn't,
5494 * and there's somebody else that is waiting to send, do nothing,
5495 * we will subscribe later and be put at the end of the list
5496 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005497 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005498 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5499 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005500 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005501
Olivier Houchard998410a2019-04-15 19:23:37 +02005502 /* We couldn't set it to NULL before, because we needed it in case
5503 * we had to cancel the tasklet
5504 */
5505 h2s->send_wait = NULL;
5506
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005507 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5508 return 0;
5509
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005510 /* htx will be enough to decide if we're using HTX or legacy */
5511 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5512
Willy Tarreau0bad0432018-06-14 16:54:01 +02005513 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005514 h2s->flags |= H2_SF_OUTGOING_DATA;
5515
Willy Tarreau751f2d02018-10-05 09:35:00 +02005516 if (h2s->id == 0) {
5517 int32_t id = h2c_get_next_sid(h2s->h2c);
5518
5519 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005520 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005521 return 0;
5522 }
5523
5524 eb32_delete(&h2s->by_id);
5525 h2s->by_id.key = h2s->id = id;
5526 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005527 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005528 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5529 }
5530
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005531 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005532 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005533 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005534 idx = htx_get_head(htx);
5535 blk = htx_get_blk(htx, idx);
5536 btype = htx_get_blk_type(blk);
5537 bsize = htx_get_blksz(blk);
5538
5539 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005540 case HTX_BLK_REQ_SL:
5541 /* start-line before headers */
5542 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5543 if (ret > 0) {
5544 total += ret;
5545 count -= ret;
5546 if (ret < bsize)
5547 goto done;
5548 }
5549 break;
5550
Willy Tarreau115e83b2018-12-01 19:17:53 +01005551 case HTX_BLK_RES_SL:
5552 /* start-line before headers */
5553 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5554 if (ret > 0) {
5555 total += ret;
5556 count -= ret;
5557 if (ret < bsize)
5558 goto done;
5559 }
5560 break;
5561
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005562 case HTX_BLK_DATA:
5563 case HTX_BLK_EOD:
5564 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005565 /* all these cause the emission of a DATA frame (possibly empty).
5566 * This EOM necessarily is one before trailers, as the EOM following
5567 * trailers would have been consumed by the trailers parser.
5568 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005569 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005570 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005571 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005572 total += ret;
5573 count -= ret;
5574 if (ret < bsize)
5575 goto done;
5576 }
5577 break;
5578
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005579 case HTX_BLK_TLR:
5580 /* This is the first trailers block, all the subsequent ones AND
5581 * the EOM will be swallowed by the parser.
5582 */
5583 ret = h2s_htx_make_trailers(h2s, htx);
5584 if (ret > 0) {
5585 total += ret;
5586 count -= ret;
5587 if (ret < bsize)
5588 goto done;
5589 }
5590 break;
5591
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005592 default:
5593 htx_remove_blk(htx, blk);
5594 total += bsize;
5595 count -= bsize;
5596 break;
5597 }
5598 }
5599 goto done;
5600 }
5601
5602 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005603 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005604 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005605 if (h2s->h2c->flags & H2_CF_IS_BACK)
5606 ret = -1;
5607 else
5608 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005609 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005610 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005611 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005612 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005613 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005614 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005615 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005616
Willy Tarreau5dd17352018-06-14 13:33:30 +02005617 if (unlikely((int)ret <= 0)) {
5618 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005619 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5620 break;
5621 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005622 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005623 total += count;
5624 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005625 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005626 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005627 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005628 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005629 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005630 break;
5631 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005632
5633 total += ret;
5634 count -= ret;
5635
Willy Tarreau2b778482019-05-06 15:00:22 +02005636 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005637 break;
5638
5639 if (h2s->flags & H2_SF_BLK_ANY)
5640 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005641 }
5642
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005643 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005644 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005645 /* trim any possibly pending data after we close (extra CR-LF,
5646 * unprocessed trailers, abnormal extra data, ...)
5647 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005648 total += count;
5649 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005650 }
5651
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005652 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005653 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005654 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005655 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005656 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005657 }
5658
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005659 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005660 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005661 } else {
5662 b_del(buf, total);
5663 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005664
5665 /* The mux is full, cancel the pending tasks */
5666 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5667 (h2s->flags & H2_SF_BLK_MBUSY))
5668 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005669
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005670 /* If we're running HTX, and we read the whole buffer, then pretend
5671 * we read exactly what the caller specified, as with HTX the caller
5672 * will always give the buffer size, instead of the amount of data
5673 * available.
5674 */
5675 if (htx && !b_data(buf))
5676 total = orig_count;
5677
Olivier Houchard7505f942018-08-21 18:10:44 +02005678 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005679 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005680 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005681
Olivier Houchard7505f942018-08-21 18:10:44 +02005682 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005683 /* If we're waiting for flow control, and we got a shutr on the
5684 * connection, we will never be unlocked, so add an error on
5685 * the conn_stream.
5686 */
5687 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5688 !b_data(&h2s->h2c->dbuf) &&
5689 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5690 if (cs->flags & CS_FL_EOS)
5691 cs->flags |= CS_FL_ERROR;
5692 else
5693 cs->flags |= CS_FL_ERR_PENDING;
5694 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005695 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005696 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005697 LIST_DEL_INIT(&h2s->list);
5698 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005699 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005700}
5701
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005702/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005703static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005704{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005705 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005706 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005707 struct eb32_node *node;
5708 int fctl_cnt = 0;
5709 int send_cnt = 0;
5710 int tree_cnt = 0;
5711 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005712 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005713
5714 if (!h2c)
5715 return;
5716
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005717 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005718 fctl_cnt++;
5719
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005720 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005721 send_cnt++;
5722
Willy Tarreau3af37712018-12-18 14:34:41 +01005723 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005724 node = eb32_first(&h2c->streams_by_id);
5725 while (node) {
5726 h2s = container_of(node, struct h2s, by_id);
5727 tree_cnt++;
5728 if (!h2s->cs)
5729 orph_cnt++;
5730 node = eb32_next(node);
5731 }
5732
Willy Tarreau60f62682019-05-26 11:32:27 +02005733 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005734 tmbuf = br_tail(h2c->mbuf);
Willy Tarreau987c0632018-12-18 10:32:05 +01005735 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5736 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005737 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5738 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau616ac812018-07-24 14:12:42 +02005739 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5740 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005741 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005742 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5743 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5744 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005745 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5746 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5747 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005748 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5749 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005750
5751 if (h2s) {
5752 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5753 h2s, h2s->id, h2s->flags,
5754 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5755 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5756 h2s->cs);
5757 if (h2s->cs)
5758 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5759 h2s->cs->flags, h2s->cs->data);
5760 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005761}
Willy Tarreau62f52692017-10-08 23:01:42 +02005762
5763/*******************************************************/
5764/* functions below are dedicated to the config parsers */
5765/*******************************************************/
5766
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005767/* config parser for global "tune.h2.header-table-size" */
5768static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5769 struct proxy *defpx, const char *file, int line,
5770 char **err)
5771{
5772 if (too_many_args(1, args, err, NULL))
5773 return -1;
5774
5775 h2_settings_header_table_size = atoi(args[1]);
5776 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5777 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5778 return -1;
5779 }
5780 return 0;
5781}
Willy Tarreau62f52692017-10-08 23:01:42 +02005782
Willy Tarreaue6baec02017-07-27 11:45:11 +02005783/* config parser for global "tune.h2.initial-window-size" */
5784static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5785 struct proxy *defpx, const char *file, int line,
5786 char **err)
5787{
5788 if (too_many_args(1, args, err, NULL))
5789 return -1;
5790
5791 h2_settings_initial_window_size = atoi(args[1]);
5792 if (h2_settings_initial_window_size < 0) {
5793 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5794 return -1;
5795 }
5796 return 0;
5797}
5798
Willy Tarreau5242ef82017-07-27 11:47:28 +02005799/* config parser for global "tune.h2.max-concurrent-streams" */
5800static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5801 struct proxy *defpx, const char *file, int line,
5802 char **err)
5803{
5804 if (too_many_args(1, args, err, NULL))
5805 return -1;
5806
5807 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005808 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005809 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5810 return -1;
5811 }
5812 return 0;
5813}
5814
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005815/* config parser for global "tune.h2.max-frame-size" */
5816static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5817 struct proxy *defpx, const char *file, int line,
5818 char **err)
5819{
5820 if (too_many_args(1, args, err, NULL))
5821 return -1;
5822
5823 h2_settings_max_frame_size = atoi(args[1]);
5824 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5825 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5826 return -1;
5827 }
5828 return 0;
5829}
5830
Willy Tarreau62f52692017-10-08 23:01:42 +02005831
5832/****************************************/
5833/* MUX initialization and instanciation */
5834/***************************************/
5835
5836/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005837static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005838 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005839 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005840 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005841 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005842 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005843 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005844 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005845 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005846 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005847 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005848 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005849 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005850 .shutr = h2_shutr,
5851 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005852 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005853 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005854 .name = "H2",
5855};
5856
Christopher Faulet32f61c02018-04-10 14:33:41 +02005857/* PROTO selection : this mux registers PROTO token "h2" */
5858static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005859 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005860
Willy Tarreau0108d902018-11-25 19:14:37 +01005861INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5862
Christopher Faulet9b579102019-04-11 22:52:25 +02005863
5864/* The mux operations */
5865static const struct mux_ops h2_htx_ops = {
5866 .init = h2_init,
5867 .wake = h2_wake,
5868 .snd_buf = h2_snd_buf,
5869 .rcv_buf = h2_rcv_buf,
5870 .subscribe = h2_subscribe,
5871 .unsubscribe = h2_unsubscribe,
5872 .attach = h2_attach,
5873 .get_first_cs = h2_get_first_cs,
5874 .detach = h2_detach,
5875 .destroy = h2_destroy,
5876 .avail_streams = h2_avail_streams,
5877 .used_streams = h2_used_streams,
5878 .shutr = h2_shutr,
5879 .shutw = h2_shutw,
5880 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005881 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005882 .name = "H2",
5883};
5884
Willy Tarreauf8957272018-10-03 10:25:20 +02005885static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005886 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005887
5888INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5889
Willy Tarreau62f52692017-10-08 23:01:42 +02005890/* config keyword parsers */
5891static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005892 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005893 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005894 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005895 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005896 { 0, NULL, NULL }
5897}};
5898
Willy Tarreau0108d902018-11-25 19:14:37 +01005899INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);