blob: c2a8406c307477bfb8b199a7b940b834bb1777c2 [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)
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200160
Willy Tarreau18312642017-10-11 07:57:07 +0200161/* HTTP/2 stream flags (32 bit), in h2s->flags */
162#define H2_SF_NONE 0x00000000
163#define H2_SF_ES_RCVD 0x00000001
164#define H2_SF_ES_SENT 0x00000002
165
166#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
167#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
168
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200169/* stream flags indicating the reason the stream is blocked */
170#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
171#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
172#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
173#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
174#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
175
Willy Tarreau454f9052017-10-26 19:40:35 +0200176/* stream flags indicating how data is supposed to be sent */
177#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
178#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
179
180/* step we're currently in when sending chunks. This is needed because we may
181 * have to transfer chunks as large as a full buffer so there's no room left
182 * for size nor crlf around.
183 */
184#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
185#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
186#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
187
188#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
189
Willy Tarreau67434202017-11-06 20:20:51 +0100190#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100191#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100192
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100193#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
194
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200195#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
196#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
Willy Tarreau3cf69fe2019-05-14 10:44:40 +0200197#define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200198
199
Willy Tarreau18312642017-10-11 07:57:07 +0200200/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
201 * it is being processed in the internal HTTP representation (H1 for now).
202 */
203struct h2s {
204 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100205 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200206 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200207 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200208 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200209 int32_t id; /* stream ID */
210 uint32_t flags; /* H2_SF_* */
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200211 int sws; /* stream window size, to be added to the mux's initial window size */
Willy Tarreau18312642017-10-11 07:57:07 +0200212 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
213 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200214 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100215 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200216 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200217 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 +0100218 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
219 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 +0200220 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100221 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200222};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200223
Willy Tarreauc6405142017-09-21 20:23:50 +0200224/* descriptor for an h2 frame header */
225struct h2_fh {
226 uint32_t len; /* length, host order, 24 bits */
227 uint32_t sid; /* stream id, host order, 31 bits */
228 uint8_t ft; /* frame type */
229 uint8_t ff; /* frame flags */
230};
231
Willy Tarreau8ceae722018-11-26 11:58:30 +0100232/* the h2c connection pool */
233DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
234
235/* the h2s stream pool */
236DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
237
Willy Tarreaudc572362018-12-12 08:08:05 +0100238/* The default connection window size is 65535, it may only be enlarged using
239 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
240 * we'll pretend we already received the difference between the two to send
241 * an equivalent window update to enlarge it to 2G-1.
242 */
243#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
244
Willy Tarreau455d5682019-05-24 19:42:18 +0200245/* maximum amount of data we're OK with re-aligning for buffer optimizations */
246#define MAX_DATA_REALIGN 1024
247
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200248/* a few settings from the global section */
249static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200250static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100251static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100252static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200253
Willy Tarreau2a856182017-05-16 15:20:39 +0200254/* a dmumy closed stream */
255static const struct h2s *h2_closed_stream = &(const struct h2s){
256 .cs = NULL,
257 .h2c = NULL,
258 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100259 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100260 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200261 .id = 0,
262};
263
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100264/* a dmumy closed stream returning a PROTOCOL_ERROR error */
265static const struct h2s *h2_error_stream = &(const struct h2s){
266 .cs = NULL,
267 .h2c = NULL,
268 .st = H2_SS_CLOSED,
269 .errcode = H2_ERR_PROTOCOL_ERROR,
270 .flags = 0,
271 .id = 0,
272};
273
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100274/* a dmumy closed stream returning a REFUSED_STREAM error */
275static const struct h2s *h2_refused_stream = &(const struct h2s){
276 .cs = NULL,
277 .h2c = NULL,
278 .st = H2_SS_CLOSED,
279 .errcode = H2_ERR_REFUSED_STREAM,
280 .flags = 0,
281 .id = 0,
282};
283
Willy Tarreau2a856182017-05-16 15:20:39 +0200284/* and a dummy idle stream for use with any unannounced stream */
285static const struct h2s *h2_idle_stream = &(const struct h2s){
286 .cs = NULL,
287 .h2c = NULL,
288 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100289 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200290 .id = 0,
291};
292
Olivier Houchard9f6af332018-05-25 14:04:04 +0200293static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200294static int h2_send(struct h2c *h2c);
295static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200296static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200297static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100298static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100299static 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 +0100300static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200301static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100302static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100303static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200304
Olivier Houchard7a977432019-03-21 15:47:13 +0100305static __inline int
306h2c_is_dead(struct h2c *h2c)
307{
308 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
309 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
310 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
311 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200312 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100313 (conn_xprt_read0_pending(h2c->conn) ||
314 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
315 return 1;
316
317 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100318}
319
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200320/*****************************************************/
321/* functions below are for dynamic buffer management */
322/*****************************************************/
323
Willy Tarreau315d8072017-12-10 22:17:57 +0100324/* indicates whether or not the we may call the h2_recv() function to attempt
325 * to receive data into the buffer and/or demux pending data. The condition is
326 * a bit complex due to some API limits for now. The rules are the following :
327 * - if an error or a shutdown was detected on the connection and the buffer
328 * is empty, we must not attempt to receive
329 * - if the demux buf failed to be allocated, we must not try to receive and
330 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100331 * - if no flag indicates a blocking condition, we may attempt to receive,
332 * regardless of whether the demux buffer is full or not, so that only
333 * de demux part decides whether or not to block. This is needed because
334 * the connection API indeed prevents us from re-enabling receipt that is
335 * already enabled in a polled state, so we must always immediately stop
336 * as soon as the demux can't proceed so as never to hit an end of read
337 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100338 * - otherwise must may not attempt
339 */
340static inline int h2_recv_allowed(const struct h2c *h2c)
341{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200342 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100343 (h2c->st0 >= H2_CS_ERROR ||
344 h2c->conn->flags & CO_FL_ERROR ||
345 conn_xprt_read0_pending(h2c->conn)))
346 return 0;
347
348 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100349 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100350 return 1;
351
352 return 0;
353}
354
Willy Tarreau47b515a2018-12-21 16:09:41 +0100355/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200356static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100357{
358 if (!h2_recv_allowed(h2c))
359 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200360 if ((!consider_buffer || !b_data(&h2c->dbuf))
361 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100362 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200363 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100364}
365
366
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100367/* returns true if the front connection has too many conn_streams attached */
368static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200369{
Willy Tarreaua8754662018-12-23 20:43:58 +0100370 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200371}
372
Willy Tarreau44e973f2018-03-01 17:49:30 +0100373/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
374 * flags are used to figure what buffer was requested. It returns 1 if the
375 * allocation succeeds, in which case the connection is woken up, or 0 if it's
376 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200377 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100378static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200379{
380 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100381 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200382
Willy Tarreau44e973f2018-03-01 17:49:30 +0100383 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200384 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200385 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200386 return 1;
387 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200388
Willy Tarreau51330962019-05-26 09:38:07 +0200389 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100390 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200391
392 if (h2c->flags & H2_CF_DEM_MROOM) {
393 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200394 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200395 }
Willy Tarreau14398122017-09-22 14:26:04 +0200396 return 1;
397 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100398
399 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
400 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200401 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100402 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200403 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100404 return 1;
405 }
406
Willy Tarreau14398122017-09-22 14:26:04 +0200407 return 0;
408}
409
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200410static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200411{
412 struct buffer *buf = NULL;
413
Willy Tarreauc234ae32019-05-13 17:56:11 +0200414 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100415 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
416 h2c->buf_wait.target = h2c;
417 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100418 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100419 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100420 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200421 __conn_xprt_stop_recv(h2c->conn);
422 }
423 return buf;
424}
425
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200426static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200427{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200428 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100429 b_free(bptr);
Willy Tarreau201840a2019-05-29 17:50:48 +0200430 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200431 }
432}
433
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200434static inline void h2_release_mbuf(struct h2c *h2c)
435{
436 struct buffer *buf;
437 unsigned int count = 0;
438
439 while (b_size(buf = br_head_pick(h2c->mbuf))) {
440 b_free(buf);
441 count++;
442 }
443 if (count)
Willy Tarreau201840a2019-05-29 17:50:48 +0200444 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200445}
446
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100447/* returns the number of allocatable outgoing streams for the connection taking
448 * the last_sid and the reserved ones into account.
449 */
450static inline int h2_streams_left(const struct h2c *h2c)
451{
452 int ret;
453
454 /* consider the number of outgoing streams we're allowed to create before
455 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
456 * nb_reserved is the number of streams which don't yet have an ID.
457 */
458 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
459 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
460 if (ret < 0)
461 ret = 0;
462 return ret;
463}
464
Willy Tarreau00f18a32019-01-26 12:19:01 +0100465/* returns the number of streams in use on a connection to figure if it's
466 * idle or not. We check nb_cs and not nb_streams as the caller will want
467 * to know if it was the last one after a detach().
468 */
469static int h2_used_streams(struct connection *conn)
470{
471 struct h2c *h2c = conn->ctx;
472
473 return h2c->nb_cs;
474}
475
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100476/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100477static int h2_avail_streams(struct connection *conn)
478{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100479 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100480 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100481 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100482
Willy Tarreau6afec462019-01-28 06:40:19 +0100483 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
484 * streams on the connection.
485 */
486 if (h2c->last_sid >= 0)
487 return 0;
488
Willy Tarreau86949782019-01-31 10:42:05 +0100489 /* note: may be negative if a SETTINGS frame changes the limit */
490 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100491
492 /* we must also consider the limit imposed by stream IDs */
493 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100494 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100495 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100496 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
497 ret1 = MIN(ret1, ret2);
498 }
499 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100500}
501
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200502
Willy Tarreau62f52692017-10-08 23:01:42 +0200503/*****************************************************************/
504/* functions below are dedicated to the mux setup and management */
505/*****************************************************************/
506
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200507/* Initialize the mux once it's attached. For outgoing connections, the context
508 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200509 * connections from the fact that the context is still NULL (even during mux
510 * upgrades). <input> is always used as Input buffer and may contain data. It is
511 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200512 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200513static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
514 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200515{
516 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100517 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200518
Willy Tarreaubafbe012017-11-24 17:34:44 +0100519 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200520 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200521 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200522
Christopher Faulete9b70722019-04-08 10:46:02 +0200523 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200524 h2c->flags = H2_CF_IS_BACK;
525 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
526 if (tick_isset(prx->timeout.serverfin))
527 h2c->shut_timeout = prx->timeout.serverfin;
528 } else {
529 h2c->flags = H2_CF_NONE;
530 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
531 if (tick_isset(prx->timeout.clientfin))
532 h2c->shut_timeout = prx->timeout.clientfin;
533 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100534
Willy Tarreau0b37d652018-10-03 10:33:02 +0200535 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100536 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100537 if (tick_isset(h2c->timeout)) {
538 t = task_new(tid_bit);
539 if (!t)
540 goto fail;
541
542 h2c->task = t;
543 t->process = h2_timeout_task;
544 t->context = h2c;
545 t->expire = tick_add(now_ms, h2c->timeout);
546 }
Willy Tarreauea392822017-10-31 10:02:25 +0100547
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200548 h2c->wait_event.tasklet = tasklet_new();
549 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200550 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200551 h2c->wait_event.tasklet->process = h2_io_cb;
552 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100553 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200554
Willy Tarreau32218eb2017-09-22 08:07:25 +0200555 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
556 if (!h2c->ddht)
557 goto fail;
558
559 /* Initialise the context. */
560 h2c->st0 = H2_CS_PREFACE;
561 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100562 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200563 h2c->max_id = -1;
564 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100565 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200566 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100567 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200568 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100569 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100570 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200571
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200572 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200573 h2c->dsi = -1;
574 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100575
Willy Tarreau32218eb2017-09-22 08:07:25 +0200576 h2c->last_sid = -1;
577
Willy Tarreau51330962019-05-26 09:38:07 +0200578 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200579 h2c->miw = 65535; /* mux initial window size */
580 h2c->mws = 65535; /* mux window size */
581 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200582 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200583 LIST_INIT(&h2c->send_list);
584 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200585 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100586 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200587
Willy Tarreau3f133572017-10-31 19:21:06 +0100588 if (t)
589 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100590
Willy Tarreau01b44822018-10-03 14:26:37 +0200591 if (h2c->flags & H2_CF_IS_BACK) {
592 /* FIXME: this is temporary, for outgoing connections we need
593 * to immediately allocate a stream until the code is modified
594 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100595 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200596 */
597 struct h2s *h2s;
598
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100599 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200600 if (!h2s)
601 goto fail_stream;
602 }
603
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100604 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200605
Willy Tarreau0f383582018-10-03 14:22:21 +0200606 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200607 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200608 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200609 fail_stream:
610 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200611 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200612 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200613 if (h2c->wait_event.tasklet)
614 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100615 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200616 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200617 return -1;
618}
619
Willy Tarreau751f2d02018-10-05 09:35:00 +0200620/* returns the next allocatable outgoing stream ID for the H2 connection, or
621 * -1 if no more is allocatable.
622 */
623static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
624{
625 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100626
627 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200628 id = -1;
629 return id;
630}
631
Willy Tarreau2373acc2017-10-12 17:35:14 +0200632/* returns the stream associated with id <id> or NULL if not found */
633static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
634{
635 struct eb32_node *node;
636
Willy Tarreau751f2d02018-10-05 09:35:00 +0200637 if (id == 0)
638 return (struct h2s *)h2_closed_stream;
639
Willy Tarreau2a856182017-05-16 15:20:39 +0200640 if (id > h2c->max_id)
641 return (struct h2s *)h2_idle_stream;
642
Willy Tarreau2373acc2017-10-12 17:35:14 +0200643 node = eb32_lookup(&h2c->streams_by_id, id);
644 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200645 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200646
647 return container_of(node, struct h2s, by_id);
648}
649
Christopher Faulet73c12072019-04-08 11:23:22 +0200650/* release function. This one should be called to free all resources allocated
651 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200652 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200653static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200654{
Christopher Faulet61840e72019-04-15 09:33:32 +0200655 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200656
Willy Tarreau32218eb2017-09-22 08:07:25 +0200657 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200658 /* The connection must be aattached to this mux to be released */
659 if (h2c->conn && h2c->conn->ctx == h2c)
660 conn = h2c->conn;
661
Willy Tarreau32218eb2017-09-22 08:07:25 +0200662 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200663
Willy Tarreau186e96e2019-05-28 17:21:18 +0200664 if (LIST_ADDED(&h2c->buf_wait.list)) {
665 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
666 LIST_DEL(&h2c->buf_wait.list);
667 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
668 }
Willy Tarreau14398122017-09-22 14:26:04 +0200669
Willy Tarreau44e973f2018-03-01 17:49:30 +0100670 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200671 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100672
Willy Tarreauea392822017-10-31 10:02:25 +0100673 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200674 h2c->task->context = NULL;
675 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100676 h2c->task = NULL;
677 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200678 if (h2c->wait_event.tasklet)
679 tasklet_free(h2c->wait_event.tasklet);
Olivier Houchard0e079372019-04-15 17:51:16 +0200680 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100681 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200682 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100683
Willy Tarreaubafbe012017-11-24 17:34:44 +0100684 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200685 }
686
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200687 if (conn) {
688 conn->mux = NULL;
689 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200690
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200691 conn_stop_tracking(conn);
692 conn_full_close(conn);
693 if (conn->destroy_cb)
694 conn->destroy_cb(conn);
695 conn_free(conn);
696 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200697}
698
699
Willy Tarreau71681172017-10-23 14:39:06 +0200700/******************************************************/
701/* functions below are for the H2 protocol processing */
702/******************************************************/
703
704/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100705static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200706{
707 return h2s ? h2s->id : 0;
708}
709
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200710/* returns the sum of the stream's own window size and the mux's initial
711 * window, which together form the stream's effective window size.
712 */
713static inline int h2s_mws(const struct h2s *h2s)
714{
715 return h2s->sws + h2s->h2c->miw;
716}
717
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200718/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100719static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200720{
721 if (h2c->msi < 0)
722 return 0;
723
724 if (h2c->msi == h2s_id(h2s))
725 return 0;
726
727 return 1;
728}
729
Willy Tarreau741d6df2017-10-17 08:00:59 +0200730/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100731static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200732{
733 h2c->errcode = err;
734 h2c->st0 = H2_CS_ERROR;
735}
736
Willy Tarreau175cebb2019-01-24 10:02:24 +0100737/* marks an error on the stream. It may also update an already closed stream
738 * (e.g. to report an error after an RST was received).
739 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100740static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200741{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100742 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200743 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100744 if (h2s->st < H2_SS_ERROR)
745 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100746 if (h2s->cs)
747 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200748 }
749}
750
Willy Tarreau7e094452018-12-19 18:08:52 +0100751/* attempt to notify the data layer of recv availability */
752static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
753{
754 struct wait_event *sw;
755
756 if (h2s->recv_wait) {
757 sw = h2s->recv_wait;
758 sw->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200759 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100760 h2s->recv_wait = NULL;
761 }
762}
763
764/* attempt to notify the data layer of send availability */
765static void __maybe_unused h2s_notify_send(struct h2s *h2s)
766{
767 struct wait_event *sw;
768
Willy Tarreauc234ae32019-05-13 17:56:11 +0200769 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100770 sw = h2s->send_wait;
771 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100772 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200773 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100774 }
775}
776
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100777/* alerts the data layer, trying to wake it up by all means, following
778 * this sequence :
779 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
780 * - if its subscribed to send, then it's woken up for send
781 * - if it was subscribed to neither, its ->wake() callback is called
782 * It is safe to call this function with a closed stream which doesn't have a
783 * conn_stream anymore.
784 */
785static void __maybe_unused h2s_alert(struct h2s *h2s)
786{
787 if (h2s->recv_wait || h2s->send_wait) {
788 h2s_notify_recv(h2s);
789 h2s_notify_send(h2s);
790 }
791 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
792 h2s->cs->data_cb->wake(h2s->cs);
793}
794
Willy Tarreaue4820742017-07-27 13:37:23 +0200795/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100796static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200797{
798 uint8_t *out = frame;
799
800 *out = len >> 16;
801 write_n16(out + 1, len);
802}
803
Willy Tarreau54c15062017-10-10 17:10:03 +0200804/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
805 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
806 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200807 * available in the buffer's input prior to calling this function. The buffer
808 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200809 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100810static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200811 const struct buffer *b, int o)
812{
Willy Tarreau591d4452018-06-15 17:21:00 +0200813 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 +0200814}
815
Willy Tarreau1f094672017-11-20 21:27:45 +0100816static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200817{
Willy Tarreau591d4452018-06-15 17:21:00 +0200818 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200819}
820
Willy Tarreau1f094672017-11-20 21:27:45 +0100821static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200822{
Willy Tarreau591d4452018-06-15 17:21:00 +0200823 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200824}
825
Willy Tarreau1f094672017-11-20 21:27:45 +0100826static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200827{
Willy Tarreau591d4452018-06-15 17:21:00 +0200828 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200829}
830
831
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100832/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
833 * The algorithm is not obvious. It turns out that H2 headers are neither
834 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
835 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200836 *
837 * b0 b1 b2 b3 b4 b5..b8
838 * +----------+---------+--------+----+----+----------------------+
839 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
840 * +----------+---------+--------+----+----+----------------------+
841 *
842 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
843 * we get the sid properly aligned and ordered, and 16 bits of len properly
844 * ordered as well. The type and flags can be extracted using bit shifts from
845 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200846 * Returns zero if some bytes are missing, otherwise non-zero on success. The
847 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200848 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100849static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200850{
851 uint64_t w;
852
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100853 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200854 return 0;
855
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100856 w = h2_get_n64(b, o + 1);
857 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200858 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
859 h->ff = w >> 32;
860 h->ft = w >> 40;
861 h->len += w >> 48;
862 return 1;
863}
864
865/* skip the next 9 bytes corresponding to the frame header possibly parsed by
866 * h2_peek_frame_hdr() above.
867 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100868static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200869{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200870 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200871}
872
873/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100874static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200875{
876 int ret;
877
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100878 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200879 if (ret > 0)
880 h2_skip_frame_hdr(b);
881 return ret;
882}
883
Willy Tarreau00dd0782018-03-01 16:31:34 +0100884/* marks stream <h2s> as CLOSED and decrement the number of active streams for
885 * its connection if the stream was not yet closed. Please use this exclusively
886 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100887 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100888static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100889{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100890 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100891 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100892 if (!h2s->id)
893 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100894 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +0100895 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
896 h2s_notify_recv(h2s);
897 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100898 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100899 h2s->st = H2_SS_CLOSED;
900}
901
Willy Tarreau71049cc2018-03-28 13:56:39 +0200902/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
903static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100904{
905 h2s_close(h2s);
906 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200907 if (b_size(&h2s->rxbuf)) {
908 b_free(&h2s->rxbuf);
909 offer_buffers(NULL, tasks_run_queue);
910 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200911 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100912 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200913 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100914 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800915 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200916 * reference left would be in the h2c send_list/fctl_list, and if
917 * we're in it, we're getting out anyway
918 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100919 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200920 if (LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau86eded62019-06-14 14:47:49 +0200921 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +0200922 LIST_DEL_INIT(&h2s->sending_list);
923 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200924 tasklet_free(h2s->wait_event.tasklet);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100925 pool_free(pool_head_h2s, h2s);
926}
927
Willy Tarreaua8e49542018-10-03 18:53:55 +0200928/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
929 * stream tree. In case of error, nothing is added and NULL is returned. The
930 * causes of errors can be any failed memory allocation. The caller is
931 * responsible for checking if the connection may support an extra stream
932 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200933 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200934static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200935{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200936 struct h2s *h2s;
937
Willy Tarreaubafbe012017-11-24 17:34:44 +0100938 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200939 if (!h2s)
940 goto out;
941
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200942 h2s->wait_event.tasklet = tasklet_new();
943 if (!h2s->wait_event.tasklet) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200944 pool_free(pool_head_h2s, h2s);
945 goto out;
946 }
947 h2s->send_wait = NULL;
948 h2s->recv_wait = NULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200949 h2s->wait_event.tasklet->process = h2_deferred_shut;
950 h2s->wait_event.tasklet->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100951 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200952 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100953 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200954 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200955 h2s->cs = NULL;
Willy Tarreau5a9c8752019-08-02 07:52:08 +0200956 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200957 h2s->flags = H2_SF_NONE;
958 h2s->errcode = H2_ERR_NO_ERROR;
959 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200960 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100961 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200962 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200963
964 if (h2c->flags & H2_CF_IS_BACK) {
965 h1m_init_req(&h2s->h1m);
966 h2s->h1m.err_pos = -1; // don't care about errors on the request path
967 h2s->h1m.flags |= H1_MF_TOLOWER;
968 } else {
969 h1m_init_res(&h2s->h1m);
970 h2s->h1m.err_pos = -1; // don't care about errors on the response path
971 h2s->h1m.flags |= H1_MF_TOLOWER;
972 }
973
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200974 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200975 if (id > 0)
976 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100977 else
978 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200979
980 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100981 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100982 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200983
984 return h2s;
985
986 out_free_h2s:
987 pool_free(pool_head_h2s, h2s);
988 out:
989 return NULL;
990}
991
992/* creates a new stream <id> on the h2c connection and returns it, or NULL in
993 * case of memory allocation error.
994 */
995static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
996{
997 struct session *sess = h2c->conn->owner;
998 struct conn_stream *cs;
999 struct h2s *h2s;
1000
1001 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
1002 goto out;
1003
1004 h2s = h2s_new(h2c, id);
1005 if (!h2s)
1006 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001007
1008 cs = cs_new(h2c->conn);
1009 if (!cs)
1010 goto out_close;
1011
Olivier Houchard746fb772018-12-15 19:42:00 +01001012 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001013 h2s->cs = cs;
1014 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001015 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001016
1017 if (stream_create_from_cs(cs) < 0)
1018 goto out_free_cs;
1019
Willy Tarreau590a0512018-09-05 11:56:48 +02001020 /* We want the accept date presented to the next stream to be the one
1021 * we have now, the handshake time to be null (since the next stream
1022 * is not delayed by a handshake), and the idle time to count since
1023 * right now.
1024 */
1025 sess->accept_date = date;
1026 sess->tv_accept = now;
1027 sess->t_handshake = 0;
1028
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001029 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001030 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001031 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001032 return h2s;
1033
1034 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001035 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001036 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001037 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001038 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001039 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001040 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001041 sess_log(sess);
1042 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001043}
1044
Willy Tarreau751f2d02018-10-05 09:35:00 +02001045/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1046 * and returns it, or NULL in case of memory allocation error or if the highest
1047 * possible stream ID was reached.
1048 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001049static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001050{
1051 struct h2s *h2s = NULL;
1052
Willy Tarreau86949782019-01-31 10:42:05 +01001053 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001054 goto out;
1055
Willy Tarreaua80dca82019-01-24 17:08:28 +01001056 if (h2_streams_left(h2c) < 1)
1057 goto out;
1058
Willy Tarreau751f2d02018-10-05 09:35:00 +02001059 /* Defer choosing the ID until we send the first message to create the stream */
1060 h2s = h2s_new(h2c, 0);
1061 if (!h2s)
1062 goto out;
1063
1064 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001065 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001066 cs->ctx = h2s;
1067 h2c->nb_cs++;
1068
Willy Tarreau751f2d02018-10-05 09:35:00 +02001069 out:
1070 return h2s;
1071}
1072
Willy Tarreaube5b7152017-09-25 16:25:39 +02001073/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1074 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1075 * the various settings codes.
1076 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001077static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001078{
1079 struct buffer *res;
1080 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001081 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001082 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001083 int ret;
1084
1085 if (h2c_mux_busy(h2c, NULL)) {
1086 h2c->flags |= H2_CF_DEM_MBUSY;
1087 return 0;
1088 }
1089
Willy Tarreaube5b7152017-09-25 16:25:39 +02001090 chunk_init(&buf, buf_data, sizeof(buf_data));
1091 chunk_memcpy(&buf,
1092 "\x00\x00\x00" /* length : 0 for now */
1093 "\x04\x00" /* type : 4 (settings), flags : 0 */
1094 "\x00\x00\x00\x00", /* stream ID : 0 */
1095 9);
1096
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001097 if (h2c->flags & H2_CF_IS_BACK) {
1098 /* send settings_enable_push=0 */
1099 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1100 }
1101
Willy Tarreaube5b7152017-09-25 16:25:39 +02001102 if (h2_settings_header_table_size != 4096) {
1103 char str[6] = "\x00\x01"; /* header_table_size */
1104
1105 write_n32(str + 2, h2_settings_header_table_size);
1106 chunk_memcat(&buf, str, 6);
1107 }
1108
1109 if (h2_settings_initial_window_size != 65535) {
1110 char str[6] = "\x00\x04"; /* initial_window_size */
1111
1112 write_n32(str + 2, h2_settings_initial_window_size);
1113 chunk_memcat(&buf, str, 6);
1114 }
1115
1116 if (h2_settings_max_concurrent_streams != 0) {
1117 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1118
1119 /* Note: 0 means "unlimited" for haproxy's config but not for
1120 * the protocol, so never send this value!
1121 */
1122 write_n32(str + 2, h2_settings_max_concurrent_streams);
1123 chunk_memcat(&buf, str, 6);
1124 }
1125
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001126 mfs = h2_settings_max_frame_size;
1127 if (mfs > global.tune.bufsize)
1128 mfs = global.tune.bufsize;
1129
1130 if (!mfs)
1131 mfs = global.tune.bufsize;
1132
1133 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001134 char str[6] = "\x00\x05"; /* max_frame_size */
1135
1136 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1137 * match bufsize - rewrite size, but at the moment it seems
1138 * that clients don't take care of it.
1139 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001140 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001141 chunk_memcat(&buf, str, 6);
1142 }
1143
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001144 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001145
1146 res = br_tail(h2c->mbuf);
1147 retry:
1148 if (!h2_get_buf(h2c, res)) {
1149 h2c->flags |= H2_CF_MUX_MALLOC;
1150 h2c->flags |= H2_CF_DEM_MROOM;
1151 return 0;
1152 }
1153
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001154 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001155 if (unlikely(ret <= 0)) {
1156 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001157 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1158 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001159 h2c->flags |= H2_CF_MUX_MFULL;
1160 h2c->flags |= H2_CF_DEM_MROOM;
1161 return 0;
1162 }
1163 else {
1164 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1165 return 0;
1166 }
1167 }
1168 return ret;
1169}
1170
Willy Tarreau52eed752017-09-22 15:05:09 +02001171/* Try to receive a connection preface, then upon success try to send our
1172 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1173 * missing data. It may return an error in h2c.
1174 */
1175static int h2c_frt_recv_preface(struct h2c *h2c)
1176{
1177 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001178 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001179
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001180 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001181
1182 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001183 if (ret1 < 0)
1184 sess_log(h2c->conn->owner);
1185
Willy Tarreau52eed752017-09-22 15:05:09 +02001186 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1187 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1188 return 0;
1189 }
1190
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001191 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001192 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001193 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001194
Willy Tarreaube5b7152017-09-25 16:25:39 +02001195 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001196}
1197
Willy Tarreau01b44822018-10-03 14:26:37 +02001198/* Try to send a connection preface, then upon success try to send our
1199 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1200 * missing data. It may return an error in h2c.
1201 */
1202static int h2c_bck_send_preface(struct h2c *h2c)
1203{
1204 struct buffer *res;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001205 int ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001206
1207 if (h2c_mux_busy(h2c, NULL)) {
1208 h2c->flags |= H2_CF_DEM_MBUSY;
1209 return 0;
1210 }
1211
Willy Tarreaubcc45952019-05-26 10:05:50 +02001212 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001213 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001214 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001215 h2c->flags |= H2_CF_MUX_MALLOC;
1216 h2c->flags |= H2_CF_DEM_MROOM;
1217 return 0;
1218 }
1219
1220 if (!b_data(res)) {
1221 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001222 ret = b_istput(res, ist(H2_CONN_PREFACE));
1223 if (unlikely(ret <= 0)) {
1224 if (!ret) {
1225 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1226 goto retry;
1227 h2c->flags |= H2_CF_MUX_MFULL;
1228 h2c->flags |= H2_CF_DEM_MROOM;
1229 return 0;
1230 }
1231 else {
1232 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1233 return 0;
1234 }
1235 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001236 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001237 return h2c_send_settings(h2c);
1238}
1239
Willy Tarreau081d4722017-05-16 21:51:05 +02001240/* try to send a GOAWAY frame on the connection to report an error or a graceful
1241 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1242 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1243 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1244 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1245 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1246 * on unrecoverable failure. It will not attempt to send one again in this last
1247 * case so that it is safe to use h2c_error() to report such errors.
1248 */
1249static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1250{
1251 struct buffer *res;
1252 char str[17];
1253 int ret;
1254
1255 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1256 return 1; // claim that it worked
1257
1258 if (h2c_mux_busy(h2c, h2s)) {
1259 if (h2s)
1260 h2s->flags |= H2_SF_BLK_MBUSY;
1261 else
1262 h2c->flags |= H2_CF_DEM_MBUSY;
1263 return 0;
1264 }
1265
Willy Tarreau9c218e72019-05-26 10:08:28 +02001266 /* len: 8, type: 7, flags: none, sid: 0 */
1267 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1268
1269 if (h2c->last_sid < 0)
1270 h2c->last_sid = h2c->max_id;
1271
1272 write_n32(str + 9, h2c->last_sid);
1273 write_n32(str + 13, h2c->errcode);
1274
Willy Tarreaubcc45952019-05-26 10:05:50 +02001275 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001276 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001277 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001278 h2c->flags |= H2_CF_MUX_MALLOC;
1279 if (h2s)
1280 h2s->flags |= H2_SF_BLK_MROOM;
1281 else
1282 h2c->flags |= H2_CF_DEM_MROOM;
1283 return 0;
1284 }
1285
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001286 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001287 if (unlikely(ret <= 0)) {
1288 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001289 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1290 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001291 h2c->flags |= H2_CF_MUX_MFULL;
1292 if (h2s)
1293 h2s->flags |= H2_SF_BLK_MROOM;
1294 else
1295 h2c->flags |= H2_CF_DEM_MROOM;
1296 return 0;
1297 }
1298 else {
1299 /* we cannot report this error using GOAWAY, so we mark
1300 * it and claim a success.
1301 */
1302 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1303 h2c->flags |= H2_CF_GOAWAY_FAILED;
1304 return 1;
1305 }
1306 }
1307 h2c->flags |= H2_CF_GOAWAY_SENT;
1308 return ret;
1309}
1310
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001311/* Try to send an RST_STREAM frame on the connection for the indicated stream
1312 * during mux operations. This stream must be valid and cannot be closed
1313 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1314 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1315 * not yet.
1316 *
1317 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1318 * to write the message, it subscribes the stream to future notifications.
1319 */
1320static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1321{
1322 struct buffer *res;
1323 char str[13];
1324 int ret;
1325
1326 if (!h2s || h2s->st == H2_SS_CLOSED)
1327 return 1;
1328
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001329 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1330 * RST_STREAM in response to a RST_STREAM frame.
1331 */
Willy Tarreaubf9a20b2019-08-06 10:01:40 +02001332 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001333 ret = 1;
1334 goto ignore;
1335 }
1336
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001337 if (h2c_mux_busy(h2c, h2s)) {
1338 h2s->flags |= H2_SF_BLK_MBUSY;
1339 return 0;
1340 }
1341
Willy Tarreau9c218e72019-05-26 10:08:28 +02001342 /* len: 4, type: 3, flags: none */
1343 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1344 write_n32(str + 5, h2s->id);
1345 write_n32(str + 9, h2s->errcode);
1346
Willy Tarreaubcc45952019-05-26 10:05:50 +02001347 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001348 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001349 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001350 h2c->flags |= H2_CF_MUX_MALLOC;
1351 h2s->flags |= H2_SF_BLK_MROOM;
1352 return 0;
1353 }
1354
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001355 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001356 if (unlikely(ret <= 0)) {
1357 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001358 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1359 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001360 h2c->flags |= H2_CF_MUX_MFULL;
1361 h2s->flags |= H2_SF_BLK_MROOM;
1362 return 0;
1363 }
1364 else {
1365 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1366 return 0;
1367 }
1368 }
1369
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001370 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001371 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001372 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001373 return ret;
1374}
1375
1376/* Try to send an RST_STREAM frame on the connection for the stream being
1377 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001378 * error code, even if the stream is one of the dummy ones, and will update
1379 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001380 *
1381 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1382 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001383 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001384 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001385 */
1386static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1387{
1388 struct buffer *res;
1389 char str[13];
1390 int ret;
1391
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001392 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1393 * RST_STREAM in response to a RST_STREAM frame.
1394 */
1395 if (h2c->dft == H2_FT_RST_STREAM) {
1396 ret = 1;
1397 goto ignore;
1398 }
1399
Willy Tarreau27a84c92017-10-17 08:10:17 +02001400 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001401 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001402 return 0;
1403 }
1404
Willy Tarreau9c218e72019-05-26 10:08:28 +02001405 /* len: 4, type: 3, flags: none */
1406 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1407
1408 write_n32(str + 5, h2c->dsi);
1409 write_n32(str + 9, h2s->errcode);
1410
Willy Tarreaubcc45952019-05-26 10:05:50 +02001411 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001412 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001413 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001414 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001415 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001416 return 0;
1417 }
1418
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001419 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001420 if (unlikely(ret <= 0)) {
1421 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001422 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1423 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001424 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001425 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001426 return 0;
1427 }
1428 else {
1429 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1430 return 0;
1431 }
1432 }
1433
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001434 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001435 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001436 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001437 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001438 }
1439
Willy Tarreau27a84c92017-10-17 08:10:17 +02001440 return ret;
1441}
1442
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001443/* try to send an empty DATA frame with the ES flag set to notify about the
1444 * end of stream and match a shutdown(write). If an ES was already sent as
1445 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1446 * on success or zero if nothing was done. In case of lack of room to write the
1447 * message, it subscribes the requesting stream to future notifications.
1448 */
1449static int h2_send_empty_data_es(struct h2s *h2s)
1450{
1451 struct h2c *h2c = h2s->h2c;
1452 struct buffer *res;
1453 char str[9];
1454 int ret;
1455
Willy Tarreau721c9742017-11-07 11:05:42 +01001456 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001457 return 1;
1458
1459 if (h2c_mux_busy(h2c, h2s)) {
1460 h2s->flags |= H2_SF_BLK_MBUSY;
1461 return 0;
1462 }
1463
Willy Tarreau9c218e72019-05-26 10:08:28 +02001464 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1465 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1466 write_n32(str + 5, h2s->id);
1467
Willy Tarreaubcc45952019-05-26 10:05:50 +02001468 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001469 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001470 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001471 h2c->flags |= H2_CF_MUX_MALLOC;
1472 h2s->flags |= H2_SF_BLK_MROOM;
1473 return 0;
1474 }
1475
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001476 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001477 if (likely(ret > 0)) {
1478 h2s->flags |= H2_SF_ES_SENT;
1479 }
1480 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001481 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1482 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001483 h2c->flags |= H2_CF_MUX_MFULL;
1484 h2s->flags |= H2_SF_BLK_MROOM;
1485 return 0;
1486 }
1487 else {
1488 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1489 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001490 }
1491 return ret;
1492}
1493
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001494/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001495 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001496 * is automatically updated accordingly. If the stream is orphaned, it is
1497 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001498 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001499static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001500{
1501 if (!h2s->cs) {
1502 /* this stream was already orphaned */
1503 h2s_destroy(h2s);
1504 return;
1505 }
1506
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001507 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001508 if (h2s->st == H2_SS_OPEN)
1509 h2s->st = H2_SS_HREM;
1510 else if (h2s->st == H2_SS_HLOC)
1511 h2s_close(h2s);
1512 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001513
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001514 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1515 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1516 h2s->cs->flags |= CS_FL_ERR_PENDING;
1517 if (h2s->cs->flags & CS_FL_EOS)
1518 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001519
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001520 if (h2s->st < H2_SS_ERROR)
1521 h2s->st = H2_SS_ERROR;
1522 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001523
1524 h2s_alert(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001525}
1526
1527/* wake the streams attached to the connection, whose id is greater than <last>
1528 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001529 */
Willy Tarreau23482912019-05-07 15:23:14 +02001530static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001531{
1532 struct eb32_node *node;
1533 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001534
Christopher Fauletf02ca002019-03-07 16:21:34 +01001535 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001536 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1537 while (node) {
1538 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001539 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001540 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001541 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001542
Christopher Fauletf02ca002019-03-07 16:21:34 +01001543 /* Wake all streams with unassigned ID (ID == 0) */
1544 node = eb32_lookup(&h2c->streams_by_id, 0);
1545 while (node) {
1546 h2s = container_of(node, struct h2s, by_id);
1547 if (h2s->id > 0)
1548 break;
1549 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001550 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001551 }
1552}
1553
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001554/* Wake up all blocked streams whose window size has become positive after the
1555 * mux's initial window was adjusted. This should be done after having processed
1556 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001557 */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001558static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001559{
1560 struct h2s *h2s;
1561 struct eb32_node *node;
1562
Willy Tarreau3421aba2017-07-27 15:41:03 +02001563 node = eb32_first(&h2c->streams_by_id);
1564 while (node) {
1565 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001566 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001567 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001568 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001569 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001570 }
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 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001611 h2c->miw = arg;
1612 break;
1613 case H2_SETTINGS_MAX_FRAME_SIZE:
1614 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1615 error = H2_ERR_PROTOCOL_ERROR;
1616 goto fail;
1617 }
1618 h2c->mfs = arg;
1619 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001620 case H2_SETTINGS_ENABLE_PUSH:
1621 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1622 error = H2_ERR_PROTOCOL_ERROR;
1623 goto fail;
1624 }
1625 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001626 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1627 if (h2c->flags & H2_CF_IS_BACK) {
1628 /* the limit is only for the backend; for the frontend it is our limit */
1629 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1630 arg = h2_settings_max_concurrent_streams;
1631 h2c->streams_limit = arg;
1632 }
1633 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001634 }
1635 }
1636
1637 /* need to ACK this frame now */
1638 h2c->st0 = H2_CS_FRAME_A;
1639 return 1;
1640 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001641 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001642 h2c_error(h2c, error);
1643 return 0;
1644}
1645
1646/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1647 * success or one of the h2_status values.
1648 */
1649static int h2c_ack_settings(struct h2c *h2c)
1650{
1651 struct buffer *res;
1652 char str[9];
1653 int ret = -1;
1654
1655 if (h2c_mux_busy(h2c, NULL)) {
1656 h2c->flags |= H2_CF_DEM_MBUSY;
1657 return 0;
1658 }
1659
Willy Tarreau9c218e72019-05-26 10:08:28 +02001660 memcpy(str,
1661 "\x00\x00\x00" /* length : 0 (no data) */
1662 "\x04" "\x01" /* type : 4, flags : ACK */
1663 "\x00\x00\x00\x00" /* stream ID */, 9);
1664
Willy Tarreaubcc45952019-05-26 10:05:50 +02001665 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001666 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001667 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001668 h2c->flags |= H2_CF_MUX_MALLOC;
1669 h2c->flags |= H2_CF_DEM_MROOM;
1670 return 0;
1671 }
1672
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001673 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001674 if (unlikely(ret <= 0)) {
1675 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001676 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1677 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001678 h2c->flags |= H2_CF_MUX_MFULL;
1679 h2c->flags |= H2_CF_DEM_MROOM;
1680 return 0;
1681 }
1682 else {
1683 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1684 return 0;
1685 }
1686 }
1687 return ret;
1688}
1689
Willy Tarreaucf68c782017-10-10 17:11:41 +02001690/* processes a PING frame and schedules an ACK if needed. The caller must pass
1691 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001692 * missing data. The caller must have already verified frame length
1693 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001694 */
1695static int h2c_handle_ping(struct h2c *h2c)
1696{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001697 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001698 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001699 h2c->st0 = H2_CS_FRAME_A;
1700 return 1;
1701}
1702
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001703/* Try to send a window update for stream id <sid> and value <increment>.
1704 * Returns > 0 on success or zero on missing room or failure. It may return an
1705 * error in h2c.
1706 */
1707static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1708{
1709 struct buffer *res;
1710 char str[13];
1711 int ret = -1;
1712
1713 if (h2c_mux_busy(h2c, NULL)) {
1714 h2c->flags |= H2_CF_DEM_MBUSY;
1715 return 0;
1716 }
1717
Willy Tarreau9c218e72019-05-26 10:08:28 +02001718 /* length: 4, type: 8, flags: none */
1719 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1720 write_n32(str + 5, sid);
1721 write_n32(str + 9, increment);
1722
Willy Tarreaubcc45952019-05-26 10:05:50 +02001723 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001724 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001725 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001726 h2c->flags |= H2_CF_MUX_MALLOC;
1727 h2c->flags |= H2_CF_DEM_MROOM;
1728 return 0;
1729 }
1730
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001731 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001732 if (unlikely(ret <= 0)) {
1733 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001734 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1735 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001736 h2c->flags |= H2_CF_MUX_MFULL;
1737 h2c->flags |= H2_CF_DEM_MROOM;
1738 return 0;
1739 }
1740 else {
1741 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1742 return 0;
1743 }
1744 }
1745 return ret;
1746}
1747
1748/* try to send pending window update for the connection. It's safe to call it
1749 * with no pending updates. Returns > 0 on success or zero on missing room or
1750 * failure. It may return an error in h2c.
1751 */
1752static int h2c_send_conn_wu(struct h2c *h2c)
1753{
1754 int ret = 1;
1755
1756 if (h2c->rcvd_c <= 0)
1757 return 1;
1758
Willy Tarreau97aaa672018-12-23 09:49:04 +01001759 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1760 /* increase the advertised connection window to 2G on
1761 * first update.
1762 */
1763 h2c->flags |= H2_CF_WINDOW_OPENED;
1764 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1765 }
1766
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001767 /* send WU for the connection */
1768 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1769 if (ret > 0)
1770 h2c->rcvd_c = 0;
1771
1772 return ret;
1773}
1774
1775/* try to send pending window update for the current dmux stream. It's safe to
1776 * call it with no pending updates. Returns > 0 on success or zero on missing
1777 * room or failure. It may return an error in h2c.
1778 */
1779static int h2c_send_strm_wu(struct h2c *h2c)
1780{
1781 int ret = 1;
1782
1783 if (h2c->rcvd_s <= 0)
1784 return 1;
1785
1786 /* send WU for the stream */
1787 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1788 if (ret > 0)
1789 h2c->rcvd_s = 0;
1790
1791 return ret;
1792}
1793
Willy Tarreaucf68c782017-10-10 17:11:41 +02001794/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1795 * success, 0 on missing data or one of the h2_status values.
1796 */
1797static int h2c_ack_ping(struct h2c *h2c)
1798{
1799 struct buffer *res;
1800 char str[17];
1801 int ret = -1;
1802
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001803 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001804 return 0;
1805
1806 if (h2c_mux_busy(h2c, NULL)) {
1807 h2c->flags |= H2_CF_DEM_MBUSY;
1808 return 0;
1809 }
1810
Willy Tarreaucf68c782017-10-10 17:11:41 +02001811 memcpy(str,
1812 "\x00\x00\x08" /* length : 8 (same payload) */
1813 "\x06" "\x01" /* type : 6, flags : ACK */
1814 "\x00\x00\x00\x00" /* stream ID */, 9);
1815
1816 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001817 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001818
Willy Tarreau9c218e72019-05-26 10:08:28 +02001819 res = br_tail(h2c->mbuf);
1820 retry:
1821 if (!h2_get_buf(h2c, res)) {
1822 h2c->flags |= H2_CF_MUX_MALLOC;
1823 h2c->flags |= H2_CF_DEM_MROOM;
1824 return 0;
1825 }
1826
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001827 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001828 if (unlikely(ret <= 0)) {
1829 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001830 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1831 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02001832 h2c->flags |= H2_CF_MUX_MFULL;
1833 h2c->flags |= H2_CF_DEM_MROOM;
1834 return 0;
1835 }
1836 else {
1837 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1838 return 0;
1839 }
1840 }
1841 return ret;
1842}
1843
Willy Tarreau26f95952017-07-27 17:18:30 +02001844/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1845 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001846 * h2c or h2s. The caller must have already verified frame length and stream ID
1847 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001848 */
1849static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1850{
1851 int32_t inc;
1852 int error;
1853
Willy Tarreau26f95952017-07-27 17:18:30 +02001854 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001855 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001856 return 0;
1857
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001858 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001859
1860 if (h2c->dsi != 0) {
1861 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001862
1863 /* it's not an error to receive WU on a closed stream */
1864 if (h2s->st == H2_SS_CLOSED)
1865 return 1;
1866
1867 if (!inc) {
1868 error = H2_ERR_PROTOCOL_ERROR;
1869 goto strm_err;
1870 }
1871
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001872 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001873 error = H2_ERR_FLOW_CONTROL_ERROR;
1874 goto strm_err;
1875 }
1876
Willy Tarreau5a9c8752019-08-02 07:52:08 +02001877 h2s->sws += inc;
1878 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02001879 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001880 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Olivier Houcharddddfe312018-10-10 18:51:00 +02001881 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001882 }
1883 }
1884 else {
1885 /* connection window update */
1886 if (!inc) {
1887 error = H2_ERR_PROTOCOL_ERROR;
1888 goto conn_err;
1889 }
1890
1891 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1892 error = H2_ERR_FLOW_CONTROL_ERROR;
1893 goto conn_err;
1894 }
1895
1896 h2c->mws += inc;
1897 }
1898
1899 return 1;
1900
1901 conn_err:
1902 h2c_error(h2c, error);
1903 return 0;
1904
1905 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001906 h2s_error(h2s, error);
1907 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001908 return 0;
1909}
1910
Willy Tarreaue96b0922017-10-30 00:28:29 +01001911/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001912 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1913 * have already verified frame length and stream ID validity. Described in
1914 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001915 */
1916static int h2c_handle_goaway(struct h2c *h2c)
1917{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001918 int last;
1919
Willy Tarreaue96b0922017-10-30 00:28:29 +01001920 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001921 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001922 return 0;
1923
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001924 last = h2_get_n32(&h2c->dbuf, 0);
1925 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001926 if (h2c->last_sid < 0)
1927 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02001928 h2_wake_some_streams(h2c, last);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001929 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001930}
1931
Willy Tarreau92153fc2017-12-03 19:46:19 +01001932/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001933 * invalid. Returns > 0 on success or zero on missing data. It may return an
1934 * error in h2c. The caller must have already verified frame length and stream
1935 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001936 */
1937static int h2c_handle_priority(struct h2c *h2c)
1938{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001939 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001940 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001941 return 0;
1942
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001943 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001944 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001945 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1946 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001947 }
1948 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001949}
1950
Willy Tarreaucd234e92017-08-18 10:59:39 +02001951/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001952 * Returns > 0 on success or zero on missing data. The caller must have already
1953 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001954 */
1955static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1956{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001957 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001958 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001959 return 0;
1960
1961 /* late RST, already handled */
1962 if (h2s->st == H2_SS_CLOSED)
1963 return 1;
1964
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001965 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001966 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001967
1968 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001969 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001970 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001971 }
1972
1973 h2s->flags |= H2_SF_RST_RCVD;
1974 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001975}
1976
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001977/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1978 * It may return an error in h2c or h2s. The caller must consider that the
1979 * return value is the new h2s in case one was allocated (most common case).
1980 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001981 * errors here are reported as connection errors since it's impossible to
1982 * recover from such errors after the compression context has been altered.
1983 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001984static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001985{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001986 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001987 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001988 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001989 int error;
1990
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001991 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001992 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001993
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001994 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001995 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001996
1997 /* now either the frame is complete or the buffer is complete */
1998 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001999 /* The stream exists/existed, this must be a trailers frame */
2000 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02002001 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
2002 /* unrecoverable error ? */
2003 if (h2c->st0 >= H2_CS_ERROR)
2004 goto out;
2005
2006 if (error == 0)
2007 goto out; // missing data
2008
2009 if (error < 0) {
2010 /* Failed to decode this frame (e.g. too large request)
2011 * but the HPACK decompressor is still synchronized.
2012 */
2013 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2014 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002015 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002016 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002017 goto done;
2018 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002019 /* the connection was already killed by an RST, let's consume
2020 * the data and send another RST.
2021 */
2022 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2023 h2s = (struct h2s*)h2_error_stream;
2024 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002025 }
2026 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2027 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2028 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002029 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002030 goto conn_err;
2031 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002032 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2033 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002034
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002035 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002036
Willy Tarreau25919232019-01-03 14:48:18 +01002037 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002038 if (h2c->st0 >= H2_CS_ERROR)
2039 goto out;
2040
Willy Tarreau25919232019-01-03 14:48:18 +01002041 if (error <= 0) {
2042 if (error == 0)
2043 goto out; // missing data
2044
2045 /* Failed to decode this stream (e.g. too large request)
2046 * but the HPACK decompressor is still synchronized.
2047 */
2048 h2s = (struct h2s*)h2_error_stream;
2049 goto send_rst;
2050 }
2051
Willy Tarreau22de8d32018-09-05 19:55:58 +02002052 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002053 * positively from h2c_frt_stream_new(), the stream will report the error,
2054 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002055 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02002056 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02002057 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002058 h2s = (struct h2s*)h2_refused_stream;
2059 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002060 }
2061
2062 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002063 h2s->rxbuf = rxbuf;
2064 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002065 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002066
Willy Tarreau88d138e2019-01-02 19:38:14 +01002067 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002068 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002069 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002070
2071 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002072 if (h2s->st == H2_SS_OPEN)
2073 h2s->st = H2_SS_HREM;
2074 else
2075 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002076 }
2077
Willy Tarreau3a429f02019-01-03 11:41:50 +01002078 /* update the max stream ID if the request is being processed */
2079 if (h2s->id > h2c->max_id)
2080 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002081
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002082 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002083
2084 conn_err:
2085 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002086 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002087
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002088 out:
2089 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002090 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002091
2092 send_rst:
2093 /* make the demux send an RST for the current stream. We may only
2094 * do this if we're certain that the HEADERS frame was properly
2095 * decompressed so that the HPACK decoder is still kept up to date.
2096 */
2097 h2_release_buf(h2c, &rxbuf);
2098 h2c->st0 = H2_CS_FRAME_E;
2099 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002100}
2101
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002102/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2103 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2104 * errors here are reported as connection errors since it's impossible to
2105 * recover from such errors after the compression context has been altered.
2106 */
2107static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2108{
2109 int error;
2110
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002111 if (!b_size(&h2c->dbuf))
2112 return NULL; // empty buffer
2113
2114 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2115 return NULL; // incomplete frame
2116
Willy Tarreau1915ca22019-01-24 11:49:37 +01002117 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002118
Willy Tarreau25919232019-01-03 14:48:18 +01002119 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002120 if (h2c->st0 >= H2_CS_ERROR)
2121 return NULL;
2122
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002123 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2124 /* RFC7540#5.1 */
2125 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2126 h2c->st0 = H2_CS_FRAME_E;
2127 return NULL;
2128 }
2129
Willy Tarreau25919232019-01-03 14:48:18 +01002130 if (error <= 0) {
2131 if (error == 0)
2132 return NULL; // missing data
2133
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002134 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002135 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002136 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002137 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002138 }
2139
Christopher Fauletfa922f02019-05-07 10:55:17 +02002140 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002141 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002142
Willy Tarreau927b88b2019-03-04 08:03:25 +01002143 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002144 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002145 else if (h2s->flags & H2_SF_ES_RCVD) {
2146 if (h2s->st == H2_SS_OPEN)
2147 h2s->st = H2_SS_HREM;
2148 else if (h2s->st == H2_SS_HLOC)
2149 h2s_close(h2s);
2150 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002151
2152 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002153}
2154
Willy Tarreau454f9052017-10-26 19:40:35 +02002155/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2156 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2157 */
2158static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2159{
2160 int error;
2161
2162 /* note that empty DATA frames are perfectly valid and sometimes used
2163 * to signal an end of stream (with the ES flag).
2164 */
2165
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002166 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002167 return 0; // empty buffer
2168
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002169 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002170 return 0; // incomplete frame
2171
2172 /* now either the frame is complete or the buffer is complete */
2173
Willy Tarreau454f9052017-10-26 19:40:35 +02002174 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2175 /* RFC7540#6.1 */
2176 error = H2_ERR_STREAM_CLOSED;
2177 goto strm_err;
2178 }
2179
Christopher Faulet4fb65f42019-06-19 09:25:58 +02002180 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002181 /* RFC7540#8.1.2 */
2182 error = H2_ERR_PROTOCOL_ERROR;
2183 goto strm_err;
2184 }
2185
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002186 if (!h2_frt_transfer_data(h2s))
2187 return 0;
2188
Willy Tarreau454f9052017-10-26 19:40:35 +02002189 /* call the upper layers to process the frame, then let the upper layer
2190 * notify the stream about any change.
2191 */
2192 if (!h2s->cs) {
Willy Tarreauc1a29652019-08-06 10:11:02 +02002193 /* The upper layer has already closed, this may happen on
2194 * 4xx/redirects during POST, or when receiving a response
2195 * from an H2 server after the client has aborted.
2196 */
2197 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002198 goto strm_err;
2199 }
2200
Willy Tarreau8f650c32017-11-21 19:36:21 +01002201 if (h2c->st0 >= H2_CS_ERROR)
2202 return 0;
2203
Willy Tarreau721c9742017-11-07 11:05:42 +01002204 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002205 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002206 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002207 }
2208
2209 /* check for completion : the callee will change this to FRAME_A or
2210 * FRAME_H once done.
2211 */
2212 if (h2c->st0 == H2_CS_FRAME_P)
2213 return 0;
2214
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002215 /* last frame */
2216 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002217 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002218 if (h2s->st == H2_SS_OPEN)
2219 h2s->st = H2_SS_HREM;
2220 else
2221 h2s_close(h2s);
2222
Willy Tarreau1915ca22019-01-24 11:49:37 +01002223 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2224 /* RFC7540#8.1.2 */
2225 error = H2_ERR_PROTOCOL_ERROR;
2226 goto strm_err;
2227 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002228 }
2229
Willy Tarreau454f9052017-10-26 19:40:35 +02002230 return 1;
2231
Willy Tarreau454f9052017-10-26 19:40:35 +02002232 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002233 h2s_error(h2s, error);
2234 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002235 return 0;
2236}
2237
Willy Tarreaubc933932017-10-09 16:21:43 +02002238/* process Rx frames to be demultiplexed */
2239static void h2_process_demux(struct h2c *h2c)
2240{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002241 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002242 struct h2_fh hdr;
2243 unsigned int padlen = 0;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002244 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002245
Willy Tarreau081d4722017-05-16 21:51:05 +02002246 if (h2c->st0 >= H2_CS_ERROR)
2247 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002248
2249 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2250 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002251 if (h2c->flags & H2_CF_IS_BACK)
2252 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002253 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2254 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002255 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002256 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002257 sess_log(h2c->conn->owner);
2258 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002259 goto fail;
2260 }
2261
2262 h2c->max_id = 0;
2263 h2c->st0 = H2_CS_SETTINGS1;
2264 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002265
2266 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002267 /* ensure that what is pending is a valid SETTINGS frame
2268 * without an ACK.
2269 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002270 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002271 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002272 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002273 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002274 sess_log(h2c->conn->owner);
2275 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002276 goto fail;
2277 }
2278
2279 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2280 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2281 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2282 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002283 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002284 goto fail;
2285 }
2286
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002287 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002288 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2289 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2290 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002291 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002292 goto fail;
2293 }
2294
Willy Tarreau3bf69182018-12-21 15:34:50 +01002295 /* that's OK, switch to FRAME_P to process it. This is
2296 * a SETTINGS frame whose header has already been
2297 * deleted above.
2298 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002299 padlen = 0;
2300 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002301 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002302 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002303
2304 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002305 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002306 int ret = 0;
2307
2308 if (h2c->st0 >= H2_CS_ERROR)
2309 break;
2310
2311 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002312 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002313 break;
2314
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002315 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002316 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002317 if (!h2c->nb_streams) {
2318 /* only log if no other stream can report the error */
2319 sess_log(h2c->conn->owner);
2320 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002321 break;
2322 }
2323
Christopher Faulet3d574a52019-06-18 12:22:38 +02002324 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002325 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2326 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2327 * we read the pad length and drop it from the remaining
2328 * payload (one byte + the 9 remaining ones = 10 total
2329 * removed), so we have a frame payload starting after the
2330 * pad len. Flow controlled frames (DATA) also count the
2331 * padlen in the flow control, so it must be adjusted.
2332 */
2333 if (hdr.len < 1) {
2334 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2335 sess_log(h2c->conn->owner);
2336 goto fail;
2337 }
2338 hdr.len--;
2339
2340 if (b_data(&h2c->dbuf) < 10)
2341 break; // missing padlen
2342
2343 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2344
2345 if (padlen > hdr.len) {
2346 /* RFC7540#6.1 : pad length = length of
2347 * frame payload or greater => error.
2348 */
2349 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2350 sess_log(h2c->conn->owner);
2351 goto fail;
2352 }
2353
2354 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2355 h2c->rcvd_c++;
2356 h2c->rcvd_s++;
2357 }
2358 b_del(&h2c->dbuf, 1);
2359 }
2360 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002361
2362 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002363 h2c->dfl = hdr.len;
2364 h2c->dsi = hdr.sid;
2365 h2c->dft = hdr.ft;
2366 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002367 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002368 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002369
2370 /* check for minimum basic frame format validity */
2371 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2372 if (ret != H2_ERR_NO_ERROR) {
2373 h2c_error(h2c, ret);
2374 sess_log(h2c->conn->owner);
2375 goto fail;
2376 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002377 }
2378
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002379 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
2380 * H2_CS_FRAME_P indicates an incomplete previous operation
2381 * (most often the first attempt) and requires some validity
2382 * checks for the frame and the current state. The two other
2383 * ones are set after completion (or abortion) and must skip
2384 * validity checks.
2385 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002386 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2387
Willy Tarreau567beb82018-12-18 16:52:44 +01002388 if (tmp_h2s != h2s && h2s && h2s->cs &&
2389 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002390 conn_xprt_read0_pending(h2c->conn) ||
2391 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002392 (h2s->flags & H2_SF_ES_RCVD) ||
2393 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002394 /* we may have to signal the upper layers */
2395 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002396 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002397 }
2398 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002399
Willy Tarreaud7901432017-12-29 11:34:40 +01002400 if (h2c->st0 == H2_CS_FRAME_E)
2401 goto strm_err;
2402
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002403 if (h2c->st0 == H2_CS_FRAME_A)
2404 goto valid_frame_type;
2405
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002406 if (h2s->st == H2_SS_IDLE &&
2407 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2408 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2409 * this state MUST be treated as a connection error
2410 */
2411 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002412 if (!h2c->nb_streams) {
2413 /* only log if no other stream can report the error */
2414 sess_log(h2c->conn->owner);
2415 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002416 break;
2417 }
2418
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002419 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2420 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2421 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002422 * this state MUST be treated as a stream error.
2423 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2424 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002425 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002426 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2427 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2428 else
2429 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002430 goto strm_err;
2431 }
2432
Willy Tarreauab837502017-12-27 15:07:30 +01002433 /* Below the management of frames received in closed state is a
2434 * bit hackish because the spec makes strong differences between
2435 * streams closed by receiving RST, sending RST, and seeing ES
2436 * in both directions. In addition to this, the creation of a
2437 * new stream reusing the identifier of a closed one will be
2438 * detected here. Given that we cannot keep track of all closed
2439 * streams forever, we consider that unknown closed streams were
2440 * closed on RST received, which allows us to respond with an
2441 * RST without breaking the connection (eg: to abort a transfer).
2442 * Some frames have to be silently ignored as well.
2443 */
2444 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002445 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002446 /* #5.1.1: The identifier of a newly
2447 * established stream MUST be numerically
2448 * greater than all streams that the initiating
2449 * endpoint has opened or reserved. This
2450 * governs streams that are opened using a
2451 * HEADERS frame and streams that are reserved
2452 * using PUSH_PROMISE. An endpoint that
2453 * receives an unexpected stream identifier
2454 * MUST respond with a connection error.
2455 */
2456 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2457 goto strm_err;
2458 }
2459
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002460 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002461 /* RFC7540#5.1:closed: an endpoint that
2462 * receives any frame other than PRIORITY after
2463 * receiving a RST_STREAM MUST treat that as a
2464 * stream error of type STREAM_CLOSED.
2465 *
2466 * Note that old streams fall into this category
2467 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002468 *
2469 * However, we cannot generalize this to all frame types. Those
2470 * carrying compression state must still be processed before
2471 * being dropped or we'll desynchronize the decoder. This can
2472 * happen with request trailers received after sending an
2473 * RST_STREAM, or with header/trailers responses received after
2474 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002475 */
2476 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2477 h2c->st0 = H2_CS_FRAME_E;
2478 goto strm_err;
2479 }
2480
2481 /* RFC7540#5.1:closed: if this state is reached as a
2482 * result of sending a RST_STREAM frame, the peer that
2483 * receives the RST_STREAM might have already sent
2484 * frames on the stream that cannot be withdrawn. An
2485 * endpoint MUST ignore frames that it receives on
2486 * closed streams after it has sent a RST_STREAM
2487 * frame. An endpoint MAY choose to limit the period
2488 * over which it ignores frames and treat frames that
2489 * arrive after this time as being in error.
2490 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002491 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002492 /* RFC7540#5.1:closed: any frame other than
2493 * PRIO/WU/RST in this state MUST be treated as
2494 * a connection error
2495 */
2496 if (h2c->dft != H2_FT_RST_STREAM &&
2497 h2c->dft != H2_FT_PRIORITY &&
2498 h2c->dft != H2_FT_WINDOW_UPDATE) {
2499 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2500 goto strm_err;
2501 }
2502 }
2503 }
2504
Willy Tarreauc0da1962017-10-30 18:38:00 +01002505#if 0
2506 // problem below: it is not possible to completely ignore such
2507 // streams as we need to maintain the compression state as well
2508 // and for this we need to completely process these frames (eg:
2509 // HEADERS frames) as well as counting DATA frames to emit
2510 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2511 // This is a typical case of layer violation where the
2512 // transported contents are critical to the connection's
2513 // validity and must be ignored at the same time :-(
2514
2515 /* graceful shutdown, ignore streams whose ID is higher than
2516 * the one advertised in GOAWAY. RFC7540#6.8.
2517 */
2518 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002519 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2520 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002521 h2c->dfl -= ret;
2522 ret = h2c->dfl == 0;
2523 goto strm_err;
2524 }
2525#endif
2526
Willy Tarreaubde2f0a2019-08-06 15:21:45 +02002527 valid_frame_type:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002528 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002529 case H2_FT_SETTINGS:
2530 if (h2c->st0 == H2_CS_FRAME_P)
2531 ret = h2c_handle_settings(h2c);
2532
2533 if (h2c->st0 == H2_CS_FRAME_A)
2534 ret = h2c_ack_settings(h2c);
2535 break;
2536
Willy Tarreaucf68c782017-10-10 17:11:41 +02002537 case H2_FT_PING:
2538 if (h2c->st0 == H2_CS_FRAME_P)
2539 ret = h2c_handle_ping(h2c);
2540
2541 if (h2c->st0 == H2_CS_FRAME_A)
2542 ret = h2c_ack_ping(h2c);
2543 break;
2544
Willy Tarreau26f95952017-07-27 17:18:30 +02002545 case H2_FT_WINDOW_UPDATE:
2546 if (h2c->st0 == H2_CS_FRAME_P)
2547 ret = h2c_handle_window_update(h2c, h2s);
2548 break;
2549
Willy Tarreau61290ec2017-10-17 08:19:21 +02002550 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002551 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2552 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2553 * frames' parsers consume all following CONTINUATION
2554 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002555 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002556 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2557 sess_log(h2c->conn->owner);
2558 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002559
Willy Tarreau13278b42017-10-13 19:23:14 +02002560 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002561 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002562 if (h2c->flags & H2_CF_IS_BACK)
2563 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2564 else
2565 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002566 if (tmp_h2s) {
2567 h2s = tmp_h2s;
2568 ret = 1;
2569 }
2570 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002571 break;
2572
Willy Tarreau454f9052017-10-26 19:40:35 +02002573 case H2_FT_DATA:
2574 if (h2c->st0 == H2_CS_FRAME_P)
2575 ret = h2c_frt_handle_data(h2c, h2s);
2576
2577 if (h2c->st0 == H2_CS_FRAME_A)
2578 ret = h2c_send_strm_wu(h2c);
2579 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002580
Willy Tarreau92153fc2017-12-03 19:46:19 +01002581 case H2_FT_PRIORITY:
2582 if (h2c->st0 == H2_CS_FRAME_P)
2583 ret = h2c_handle_priority(h2c);
2584 break;
2585
Willy Tarreaucd234e92017-08-18 10:59:39 +02002586 case H2_FT_RST_STREAM:
2587 if (h2c->st0 == H2_CS_FRAME_P)
2588 ret = h2c_handle_rst_stream(h2c, h2s);
2589 break;
2590
Willy Tarreaue96b0922017-10-30 00:28:29 +01002591 case H2_FT_GOAWAY:
2592 if (h2c->st0 == H2_CS_FRAME_P)
2593 ret = h2c_handle_goaway(h2c);
2594 break;
2595
Willy Tarreau1c661982017-10-30 13:52:01 +01002596 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002597 default:
2598 /* drop frames that we ignore. They may be larger than
2599 * the buffer so we drain all of their contents until
2600 * we reach the end.
2601 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002602 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2603 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002604 h2c->dfl -= ret;
2605 ret = h2c->dfl == 0;
2606 }
2607
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002608 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002609 /* We may have to send an RST if not done yet */
2610 if (h2s->st == H2_SS_ERROR)
2611 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002612
Willy Tarreaua20a5192017-12-27 11:02:06 +01002613 if (h2c->st0 == H2_CS_FRAME_E)
2614 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002615
Willy Tarreau7e98c052017-10-10 15:56:59 +02002616 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002617 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002618 break;
2619
2620 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002621 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002622 h2c->st0 = H2_CS_FRAME_H;
2623 }
2624 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002625
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002626 if (h2c->rcvd_c > 0 &&
2627 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2628 h2c_send_conn_wu(h2c);
2629
Willy Tarreau52eed752017-09-22 15:05:09 +02002630 fail:
2631 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002632 if (h2s && h2s->cs &&
2633 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002634 conn_xprt_read0_pending(h2c->conn) ||
2635 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002636 (h2s->flags & H2_SF_ES_RCVD) ||
2637 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002638 /* we may have to signal the upper layers */
2639 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002640 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002641 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002642
Willy Tarreau5a9c8752019-08-02 07:52:08 +02002643 if (old_iw != h2c->miw)
2644 h2c_unblock_sfctl(h2c);
2645
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002646 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002647}
2648
2649/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2650 * the end.
2651 */
2652static int h2_process_mux(struct h2c *h2c)
2653{
Olivier Houchard998410a2019-04-15 19:23:37 +02002654 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002655
Willy Tarreau01b44822018-10-03 14:26:37 +02002656 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2657 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2658 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2659 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2660 if (h2c->st0 == H2_CS_ERROR) {
2661 h2c->st0 = H2_CS_ERROR2;
2662 sess_log(h2c->conn->owner);
2663 }
2664 goto fail;
2665 }
2666 h2c->st0 = H2_CS_SETTINGS1;
2667 }
2668 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002669 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002670 return 1;
2671 }
2672
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002673 /* start by sending possibly pending window updates */
2674 if (h2c->rcvd_c > 0 &&
2675 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2676 h2c_send_conn_wu(h2c) < 0)
2677 goto fail;
2678
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002679 /* First we always process the flow control list because the streams
2680 * waiting there were already elected for immediate emission but were
2681 * blocked just on this.
2682 */
2683
Olivier Houchard998410a2019-04-15 19:23:37 +02002684 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002685 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2686 h2c->st0 >= H2_CS_ERROR)
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 Houchardfa8aa862018-10-10 18:25:41 +02002692 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002693 /* For some reason, the upper layer failed to subsribe again,
2694 * so remove it from the send_list
2695 */
2696 if (!h2s->send_wait) {
2697 LIST_DEL_INIT(&h2s->list);
2698 continue;
2699 }
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);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002702 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002703 }
2704
Olivier Houchard998410a2019-04-15 19:23:37 +02002705 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002706 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2707 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002708
Willy Tarreauc234ae32019-05-13 17:56:11 +02002709 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002710 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002711
Olivier Houchard998410a2019-04-15 19:23:37 +02002712 /* For some reason, the upper layer failed to subsribe again,
2713 * so remove it from the send_list
2714 */
2715 if (!h2s->send_wait) {
2716 LIST_DEL_INIT(&h2s->list);
2717 continue;
2718 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002719 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002720 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002721 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002722 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002723 }
2724
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002725 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002726 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002727 if (h2c->st0 == H2_CS_ERROR) {
2728 if (h2c->max_id >= 0) {
2729 h2c_send_goaway_error(h2c, NULL);
2730 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2731 return 0;
2732 }
2733
2734 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2735 }
2736 return 1;
2737 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002738 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002739}
2740
Willy Tarreau62f52692017-10-08 23:01:42 +02002741
Willy Tarreau479998a2018-11-18 06:30:59 +01002742/* Attempt to read data, and subscribe if none available.
2743 * The function returns 1 if data has been received, otherwise zero.
2744 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002745static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002746{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002747 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002748 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002749 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002750 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002751
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002752 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002753 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002754
Willy Tarreau315d8072017-12-10 22:17:57 +01002755 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002756 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002757
Willy Tarreau44e973f2018-03-01 17:49:30 +01002758 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002759 if (!buf) {
2760 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002761 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002762 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002763
Olivier Houchard7505f942018-08-21 18:10:44 +02002764 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002765 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002766 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2767 /* HTX in use : try to pre-align the buffer like the
2768 * rxbufs will be to optimize memory copies. We'll make
2769 * sure that the frame header lands at the end of the
2770 * HTX block to alias it upon recv. We cannot use the
2771 * head because rcv_buf() will realign the buffer if
2772 * it's empty. Thus we cheat and pretend we already
2773 * have a few bytes there.
2774 */
2775 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002776 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002777 }
2778 else
2779 max = b_room(buf);
2780
Olivier Houchard7505f942018-08-21 18:10:44 +02002781 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002782 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002783 else
2784 ret = 0;
2785 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002786
Willy Tarreaua8fcdac2019-08-02 07:48:47 +02002787 if (max && !ret && h2_recv_allowed(h2c))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002788 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002789
Olivier Houcharda1411e62018-08-17 18:42:48 +02002790 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002791 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002792 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002793 }
2794
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002795 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002796 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002797 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002798}
2799
Willy Tarreau479998a2018-11-18 06:30:59 +01002800/* Try to send data if possible.
2801 * The function returns 1 if data have been sent, otherwise zero.
2802 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002803static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002804{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002805 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002806 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002807 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002808
2809 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002810 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002811
Olivier Houchard7505f942018-08-21 18:10:44 +02002812
Willy Tarreaua2af5122017-10-09 11:56:46 +02002813 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2814 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002815 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002816 }
2817
Willy Tarreaubc933932017-10-09 16:21:43 +02002818 /* This loop is quite simple : it tries to fill as much as it can from
2819 * pending streams into the existing buffer until it's reportedly full
2820 * or the end of send requests is reached. Then it tries to send this
2821 * buffer's contents out, marks it not full if at least one byte could
2822 * be sent, and tries again.
2823 *
2824 * The snd_buf() function normally takes a "flags" argument which may
2825 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2826 * data immediately comes and CO_SFL_STREAMER to indicate that the
2827 * connection is streaming lots of data (used to increase TLS record
2828 * size at the expense of latency). The former can be sent any time
2829 * there's a buffer full flag, as it indicates at least one stream
2830 * attempted to send and failed so there are pending data. An
2831 * alternative would be to set it as long as there's an active stream
2832 * but that would be problematic for ACKs until we have an absolute
2833 * guarantee that all waiters have at least one byte to send. The
2834 * latter should possibly not be set for now.
2835 */
2836
2837 done = 0;
2838 while (!done) {
2839 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002840 unsigned int released = 0;
2841 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02002842
2843 /* fill as much as we can into the current buffer */
2844 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2845 done = h2_process_mux(h2c);
2846
Olivier Houchard2b094432019-01-29 18:28:36 +01002847 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002848 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01002849
Willy Tarreaubc933932017-10-09 16:21:43 +02002850 if (conn->flags & CO_FL_ERROR)
2851 break;
2852
2853 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2854 flags |= CO_SFL_MSG_MORE;
2855
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002856 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
2857 if (b_data(buf)) {
2858 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002859 if (!ret) {
2860 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002861 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002862 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002863 sent = 1;
2864 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002865 if (b_data(buf)) {
2866 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002867 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002868 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002869 }
2870 b_free(buf);
2871 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02002872 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002873
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002874 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02002875 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002876
Willy Tarreaubc933932017-10-09 16:21:43 +02002877 /* wrote at least one byte, the buffer is not full anymore */
2878 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2879 }
2880
Willy Tarreaua2af5122017-10-09 11:56:46 +02002881 if (conn->flags & CO_FL_SOCK_WR_SH) {
2882 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02002883 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002884 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002885 /* We're not full anymore, so we can wake any task that are waiting
2886 * for us.
2887 */
2888 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002889 struct h2s *h2s;
2890
2891 list_for_each_entry(h2s, &h2c->send_list, list) {
2892 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2893 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002894
Willy Tarreauc234ae32019-05-13 17:56:11 +02002895 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002896 continue;
2897
Olivier Houchard998410a2019-04-15 19:23:37 +02002898 /* For some reason, the upper layer failed to subsribe again,
2899 * so remove it from the send_list
2900 */
2901 if (!h2s->send_wait) {
2902 LIST_DEL_INIT(&h2s->list);
2903 continue;
2904 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002905 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002906 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002907 tasklet_wakeup(h2s->send_wait->tasklet);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002908 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002909 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002910 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002911 /* We're done, no more to send */
Willy Tarreau662fafc2019-05-26 09:43:07 +02002912 if (!br_data(h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002913 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002914schedule:
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002915 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002916 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002917
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002918 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002919}
2920
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002921/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002922static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2923{
2924 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002925 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002926
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002927 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002928 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002929 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002930 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002931 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002932 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002933 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002934}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002935
Willy Tarreau62f52692017-10-08 23:01:42 +02002936/* callback called on any event by the connection handler.
2937 * It applies changes and returns zero, or < 0 if it wants immediate
2938 * destruction of the connection (which normally doesn not happen in h2).
2939 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002940static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002941{
Olivier Houchard7505f942018-08-21 18:10:44 +02002942 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002943
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002944 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002945 h2_process_demux(h2c);
2946
2947 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002948 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002949
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002950 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002951 h2c->flags &= ~H2_CF_DEM_DFULL;
2952 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002953 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002954
Willy Tarreau0b37d652018-10-03 10:33:02 +02002955 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002956 /* frontend is stopping, reload likely in progress, let's try
2957 * to announce a graceful shutdown if not yet done. We don't
2958 * care if it fails, it will be tried again later.
2959 */
2960 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2961 if (h2c->last_sid < 0)
2962 h2c->last_sid = (1U << 31) - 1;
2963 h2c_send_goaway_error(h2c, NULL);
2964 }
2965 }
2966
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002967 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002968 * If we received early data, and the handshake is done, wake
2969 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002970 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002971 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2972 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2973 struct eb32_node *node;
2974 struct h2s *h2s;
2975
2976 h2c->flags |= H2_CF_WAIT_FOR_HS;
2977 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2978
2979 while (node) {
2980 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002981 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002982 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002983 node = eb32_next(node);
2984 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002985 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002986
Willy Tarreau26bd7612017-10-09 16:47:04 +02002987 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002988 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2989 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2990 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02002991 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002992
2993 if (eb_is_empty(&h2c->streams_by_id)) {
2994 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002995 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002996 return -1;
2997 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002998 }
2999
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003000 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003001 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003002
Olivier Houchard53216e72018-10-10 15:46:36 +02003003 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3004 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3005 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003006 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003007 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3008 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003009 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003010
Willy Tarreau3f133572017-10-31 19:21:06 +01003011 if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02003012 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3013 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003014 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003015
Olivier Houchard7505f942018-08-21 18:10:44 +02003016 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02003017 return 0;
3018}
3019
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003020/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003021static int h2_wake(struct connection *conn)
3022{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003023 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003024
3025 return (h2_process(h2c));
3026}
3027
Willy Tarreauea392822017-10-31 10:02:25 +01003028/* Connection timeout management. The principle is that if there's no receipt
3029 * nor sending for a certain amount of time, the connection is closed. If the
3030 * MUX buffer still has lying data or is not allocatable, the connection is
3031 * immediately killed. If it's allocatable and empty, we attempt to send a
3032 * GOAWAY frame.
3033 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003034static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003035{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003036 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003037 int expired = tick_is_expired(t->expire, now_ms);
3038
Willy Tarreau0975f112018-03-29 15:22:59 +02003039 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01003040 return t;
3041
Olivier Houchard3f795f72019-04-17 22:51:06 +02003042 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003043
3044 if (!h2c) {
3045 /* resources were already deleted */
3046 return NULL;
3047 }
3048
3049 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003050 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003051 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003052
Willy Tarreau662fafc2019-05-26 09:43:07 +02003053 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003054 /* don't even try to send a GOAWAY, the buffer is stuck */
3055 h2c->flags |= H2_CF_GOAWAY_FAILED;
3056 }
3057
3058 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003059 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003060 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3061 h2c->flags |= H2_CF_GOAWAY_FAILED;
3062
Willy Tarreau662fafc2019-05-26 09:43:07 +02003063 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003064 unsigned int released = 0;
3065 struct buffer *buf;
3066
3067 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3068 if (b_data(buf)) {
3069 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3070 if (!ret)
3071 break;
3072 b_del(buf, ret);
3073 if (b_data(buf))
3074 break;
3075 b_free(buf);
3076 released++;
3077 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003078 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003079
3080 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003081 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003082 }
Willy Tarreauea392822017-10-31 10:02:25 +01003083
Willy Tarreau0975f112018-03-29 15:22:59 +02003084 /* either we can release everything now or it will be done later once
3085 * the last stream closes.
3086 */
3087 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003088 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003089
Willy Tarreauea392822017-10-31 10:02:25 +01003090 return NULL;
3091}
3092
3093
Willy Tarreau62f52692017-10-08 23:01:42 +02003094/*******************************************/
3095/* functions below are used by the streams */
3096/*******************************************/
3097
3098/*
3099 * Attach a new stream to a connection
3100 * (Used for outgoing connections)
3101 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003102static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003103{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003104 struct conn_stream *cs;
3105 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003106 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003107
3108 cs = cs_new(conn);
3109 if (!cs)
3110 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003111 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003112 if (!h2s) {
3113 cs_free(cs);
3114 return NULL;
3115 }
3116 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003117}
3118
Willy Tarreaufafd3982018-11-18 21:29:20 +01003119/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3120 * We have to scan because we may have some orphan streams. It might be
3121 * beneficial to scan backwards from the end to reduce the likeliness to find
3122 * orphans.
3123 */
3124static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3125{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003126 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003127 struct h2s *h2s;
3128 struct eb32_node *node;
3129
3130 node = eb32_first(&h2c->streams_by_id);
3131 while (node) {
3132 h2s = container_of(node, struct h2s, by_id);
3133 if (h2s->cs)
3134 return h2s->cs;
3135 node = eb32_next(node);
3136 }
3137 return NULL;
3138}
3139
Willy Tarreau62f52692017-10-08 23:01:42 +02003140/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003141 * Destroy the mux and the associated connection, if it is no longer used
3142 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003143static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003144{
Christopher Faulet73c12072019-04-08 11:23:22 +02003145 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003146
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003147 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003148 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003149}
3150
3151/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003152 * Detach the stream from the connection and possibly release the connection.
3153 */
3154static void h2_detach(struct conn_stream *cs)
3155{
Willy Tarreau60935142017-10-16 18:11:19 +02003156 struct h2s *h2s = cs->ctx;
3157 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003158 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003159
3160 cs->ctx = NULL;
3161 if (!h2s)
3162 return;
3163
Olivier Houchard998410a2019-04-15 19:23:37 +02003164 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003165 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003166 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003167 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003168 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003169 /*
3170 * At this point, the stream_interface is supposed to have called
3171 * h2_unsubscribe(), so the only way there's still a
3172 * subscription that came from the stream_interface (as we
3173 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3174 * without the stream_interface involved) is that we subscribed
3175 * for sending, we woke the tasklet up and removed the
3176 * SUB_RETRY_SEND flag, so the stream_interface would not
3177 * know it has to unsubscribe for send, but the tasklet hasn't
3178 * run yet. Make sure to handle that by explicitely setting
3179 * send_wait to NULL, as nothing else will do it for us.
3180 */
3181 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003182 }
3183
Olivier Houchardf502aca2018-12-14 19:42:40 +01003184 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003185 h2c = h2s->h2c;
3186 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003187 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003188 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3189 !h2_frt_has_too_many_cs(h2c)) {
3190 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003191 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003192 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003193 }
Willy Tarreau60935142017-10-16 18:11:19 +02003194
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003195 /* this stream may be blocked waiting for some data to leave (possibly
3196 * an ES or RST frame), so orphan it in this case.
3197 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003198 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003199 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003200 (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 +01003201 return;
3202
Willy Tarreau45f752e2017-10-30 15:44:59 +01003203 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3204 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3205 /* unblock the connection if it was blocked on this
3206 * stream.
3207 */
3208 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3209 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003210 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003211 }
3212
Willy Tarreau71049cc2018-03-28 13:56:39 +02003213 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003214
Olivier Houchard8a786902018-12-15 16:05:40 +01003215 if (h2c->flags & H2_CF_IS_BACK &&
3216 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003217 if (!(h2c->conn->flags &
3218 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3219 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003220 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003221 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3222 h2c->conn->owner = NULL;
3223 if (eb_is_empty(&h2c->streams_by_id)) {
3224 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3225 /* The server doesn't want it, let's kill the connection right away */
3226 h2c->conn->mux->destroy(h2c->conn);
3227 return;
3228 }
3229 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003230 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003231 if (eb_is_empty(&h2c->streams_by_id)) {
3232 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3233 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3234 return;
3235 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003236 /* Never ever allow to reuse a connection from a non-reuse backend */
3237 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3238 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003239 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003240 struct server *srv = objt_server(h2c->conn->target);
3241
3242 if (srv) {
3243 if (h2c->conn->flags & CO_FL_PRIVATE)
3244 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3245 else
3246 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3247 }
3248
3249 }
3250 }
3251 }
3252
Willy Tarreaue323f342018-03-28 13:51:45 +02003253 /* We don't want to close right now unless we're removing the
3254 * last stream, and either the connection is in error, or it
3255 * reached the ID already specified in a GOAWAY frame received
3256 * or sent (as seen by last_sid >= 0).
3257 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003258 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003259 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003260 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003261 }
3262 else if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02003263 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3264 task_queue(h2c->task);
Willy Tarreau60935142017-10-16 18:11:19 +02003265 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003266}
3267
Willy Tarreau88bdba32019-05-13 18:17:53 +02003268/* Performs a synchronous or asynchronous shutr(). */
3269static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003270{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003271 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003272 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003273
Willy Tarreauf983d002019-05-14 10:40:21 +02003274 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003275 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003276
Willy Tarreau18059042019-01-31 19:12:48 +01003277 /* a connstream may require us to immediately kill the whole connection
3278 * for example because of a "tcp-request content reject" rule that is
3279 * normally used to limit abuse. In this case we schedule a goaway to
3280 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003281 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003282 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003283 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3284 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3285 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3286 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003287 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3288 /* Nothing was never sent for this stream, so reset with
3289 * REFUSED_STREAM error to let the client retry the
3290 * request.
3291 */
3292 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3293 }
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003294 else {
3295 /* a final response was already provided, we don't want this
3296 * stream anymore. This may happen when the server responds
3297 * before the end of an upload and closes quickly (redirect,
3298 * deny, ...)
3299 */
3300 h2s_error(h2s, H2_ERR_CANCEL);
3301 }
Willy Tarreau18059042019-01-31 19:12:48 +01003302
Willy Tarreau90c32322017-11-24 08:00:30 +01003303 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003304 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003305 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003306
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003307 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003308 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003309 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003310 done:
3311 h2s->flags &= ~H2_SF_WANT_SHUTR;
3312 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003313add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003314 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003315 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003316 if (h2s->flags & H2_SF_BLK_MFCTL) {
3317 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3318 h2s->send_wait = sw;
3319 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3320 h2s->send_wait = sw;
3321 LIST_ADDQ(&h2c->send_list, &h2s->list);
3322 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003323 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003324 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003325 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003326 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003327}
3328
Willy Tarreau88bdba32019-05-13 18:17:53 +02003329/* Performs a synchronous or asynchronous shutw(). */
3330static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003331{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003332 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003333 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003334
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003335 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003336 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003337
Willy Tarreauebdb6a02019-08-06 10:30:58 +02003338 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003339 /* we can cleanly close using an empty data frame only after headers */
3340
3341 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3342 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003343 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003344
3345 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003346 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003347 else
3348 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003349 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003350 /* a connstream may require us to immediately kill the whole connection
3351 * for example because of a "tcp-request content reject" rule that is
3352 * normally used to limit abuse. In this case we schedule a goaway to
3353 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003354 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003355 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003356 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3357 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3358 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3359 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003360 else {
3361 /* Nothing was never sent for this stream, so reset with
3362 * REFUSED_STREAM error to let the client retry the
3363 * request.
3364 */
3365 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3366 }
Willy Tarreau18059042019-01-31 19:12:48 +01003367
Willy Tarreau90c32322017-11-24 08:00:30 +01003368 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003369 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003370 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003371
Willy Tarreau00dd0782018-03-01 16:31:34 +01003372 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003373 }
3374
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003375 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003376 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003377 done:
3378 h2s->flags &= ~H2_SF_WANT_SHUTW;
3379 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003380
3381 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003382 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003383 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003384 if (h2s->flags & H2_SF_BLK_MFCTL) {
3385 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3386 h2s->send_wait = sw;
3387 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3388 h2s->send_wait = sw;
3389 LIST_ADDQ(&h2c->send_list, &h2s->list);
3390 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003391 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003392 /* let the handler know we want to shutw */
3393 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003394 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003395}
3396
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003397/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003398 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3399 * and prevented the last frame from being emitted.
3400 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003401static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3402{
3403 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003404 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003405
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003406 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003407 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003408 h2_do_shutw(h2s);
3409
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003410 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003411 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003412
Willy Tarreau88bdba32019-05-13 18:17:53 +02003413 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003414 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003415 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003416
Willy Tarreau88bdba32019-05-13 18:17:53 +02003417 if (!h2s->cs) {
3418 h2s_destroy(h2s);
3419 if (h2c_is_dead(h2c))
3420 h2_release(h2c);
3421 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003422 }
3423
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003424 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003425}
3426
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003427/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003428static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3429{
3430 struct h2s *h2s = cs->ctx;
3431
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003432 if (cs->flags & CS_FL_KILL_CONN)
3433 h2s->flags |= H2_SF_KILL_CONN;
3434
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003435 if (!mode)
3436 return;
3437
3438 h2_do_shutr(h2s);
3439}
3440
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003441/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003442static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3443{
3444 struct h2s *h2s = cs->ctx;
3445
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003446 if (cs->flags & CS_FL_KILL_CONN)
3447 h2s->flags |= H2_SF_KILL_CONN;
3448
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003449 h2_do_shutw(h2s);
3450}
3451
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003452/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003453 * HTX request or response depending on the connection's side. Returns a
3454 * positive value on success, a negative value on failure, or 0 if it couldn't
3455 * proceed. May report connection errors in h2c->errcode if the frame is
3456 * non-decodable and the connection unrecoverable. In absence of connection
3457 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003458 *
3459 * The function may fold CONTINUATION frames into the initial HEADERS frame
3460 * by removing padding and next frame header, then moving the CONTINUATION
3461 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3462 * leaving a hole between the main frame and the beginning of the next one.
3463 * The possibly remaining incomplete or next frame at the end may be moved
3464 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3465 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3466 *
3467 * A buffer at the beginning of processing may look like this :
3468 *
3469 * ,---.---------.-----.--------------.--------------.------.---.
3470 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3471 * `---^---------^-----^--------------^--------------^------^---'
3472 * | | <-----> | |
3473 * area | dpl | wrap
3474 * |<--------------> |
3475 * | dfl |
3476 * |<-------------------------------------------------->|
3477 * head data
3478 *
3479 * Padding is automatically overwritten when folding, participating to the
3480 * hole size after dfl :
3481 *
3482 * ,---.------------------------.-----.--------------.------.---.
3483 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3484 * `---^------------------------^-----^--------------^------^---'
3485 * | | <-----> | |
3486 * area | hole | wrap
3487 * |<-----------------------> |
3488 * | dfl |
3489 * |<-------------------------------------------------->|
3490 * head data
3491 *
3492 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3493 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3494 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003495 *
3496 * The <flags> field must point to either the stream's flags or to a copy of it
3497 * so that the function can update the following flags :
3498 * - H2_SF_DATA_CLEN when content-length is seen
3499 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3500 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003501 *
3502 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3503 * decoding, in order to detect if we're dealing with a headers or a trailers
3504 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003505 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003506static 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 +02003507{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003508 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003509 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003510 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003511 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003512 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003513 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003514 int flen; // header frame len
3515 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003516 int ret = 0;
3517 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003518 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003519 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003520
Willy Tarreauea18f862018-12-22 20:19:26 +01003521next_frame:
3522 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3523 goto leave; // incomplete input frame
3524
3525 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3526 * this case, we'll try to paste it immediately after the initial
3527 * HEADERS frame payload and kill any possible padding. The initial
3528 * frame's length will be increased to represent the concatenation
3529 * of the two frames. The next frame is read from position <tlen>
3530 * and written at position <flen> (minus padding if some is present).
3531 */
3532 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3533 struct h2_fh hdr;
3534 int clen; // CONTINUATION frame's payload length
3535
3536 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3537 /* no more data, the buffer may be full, either due to
3538 * too large a frame or because of too large a hole that
3539 * we're going to compact at the end.
3540 */
3541 goto leave;
3542 }
3543
3544 if (hdr.ft != H2_FT_CONTINUATION) {
3545 /* RFC7540#6.10: frame of unexpected type */
3546 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3547 goto fail;
3548 }
3549
3550 if (hdr.sid != h2c->dsi) {
3551 /* RFC7540#6.10: frame of different stream */
3552 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3553 goto fail;
3554 }
3555
3556 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3557 /* RFC7540#4.2: invalid frame length */
3558 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3559 goto fail;
3560 }
3561
3562 /* detect when we must stop aggragating frames */
3563 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3564
3565 /* Take as much as we can of the CONTINUATION frame's payload */
3566 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3567 if (clen > hdr.len)
3568 clen = hdr.len;
3569
3570 /* Move the frame's payload over the padding, hole and frame
3571 * header. At least one of hole or dpl is null (see diagrams
3572 * above). The hole moves after the new aggragated frame.
3573 */
3574 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3575 h2c->dfl += clen - h2c->dpl;
3576 hole += h2c->dpl + 9;
3577 h2c->dpl = 0;
3578 goto next_frame;
3579 }
3580
3581 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003582
Willy Tarreau13278b42017-10-13 19:23:14 +02003583 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003584 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003585 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003586 copy = alloc_trash_chunk();
3587 if (!copy) {
3588 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3589 goto fail;
3590 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003591 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3592 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3593 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003594 }
3595
Willy Tarreau13278b42017-10-13 19:23:14 +02003596 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3597 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003598 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003599 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3600 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003601 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003602 }
3603
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003604 if (flen < 5) {
3605 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3606 goto fail;
3607 }
3608
Willy Tarreau13278b42017-10-13 19:23:14 +02003609 hdrs += 5; // stream dep = 4, weight = 1
3610 flen -= 5;
3611 }
3612
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003613 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003614 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003615 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003616 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003617
Willy Tarreau937f7602018-02-26 15:22:17 +01003618 /* we can't retry a failed decompression operation so we must be very
3619 * careful not to take any risks. In practice the output buffer is
3620 * always empty except maybe for trailers, in which case we simply have
3621 * to wait for the upper layer to finish consuming what is available.
3622 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003623
3624 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003625 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003626 if (!htx_is_empty(htx)) {
3627 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003628 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003629 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003630 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003631 if (b_data(rxbuf)) {
3632 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003633 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003634 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003635
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003636 rxbuf->head = 0;
3637 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003638 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003639
Willy Tarreau25919232019-01-03 14:48:18 +01003640 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003641 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3642 sizeof(list)/sizeof(list[0]), tmp);
3643 if (outlen < 0) {
3644 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3645 goto fail;
3646 }
3647
Willy Tarreau25919232019-01-03 14:48:18 +01003648 /* The PACK decompressor was updated, let's update the input buffer and
3649 * the parser's state to commit these changes and allow us to later
3650 * fail solely on the stream if needed.
3651 */
3652 b_del(&h2c->dbuf, h2c->dfl + hole);
3653 h2c->dfl = hole = 0;
3654 h2c->st0 = H2_CS_FRAME_H;
3655
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003656 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003657 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003658
Willy Tarreau88d138e2019-01-02 19:38:14 +01003659 if (*flags & H2_SF_HEADERS_RCVD)
3660 goto trailers;
3661
3662 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003663 if (htx) {
3664 /* HTX mode */
3665 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003666 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003667 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003668 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003669 } else {
3670 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003671 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003672 if (outlen > 0)
3673 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003674 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003675
3676 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003677 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003678 goto fail;
3679 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003680
Willy Tarreau174b06a2018-04-25 18:13:58 +02003681 if (msgf & H2_MSGF_BODY) {
3682 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003683 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003684 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003685 if (htx)
3686 htx->extra = *body_len;
3687 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003688 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003689 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003690 }
3691
Willy Tarreau88d138e2019-01-02 19:38:14 +01003692 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003693 /* indicate that a HEADERS frame was received for this stream, except
3694 * for 1xx responses. For 1xx responses, another HEADERS frame is
3695 * expected.
3696 */
3697 if (!(msgf & H2_MSGF_RSP_1XX))
3698 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003699
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02003700 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003701 /* Mark the end of message, either using EOM in HTX or with the
3702 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003703 * is not set during headers with END_STREAM. For HTX trailers,
3704 * we must not leave an HTX trailers block not followed by an
3705 * EOM block, the two must be atomic. Thus if we fail to emit
3706 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003707 */
3708 if (htx) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02003709 if (!htx_add_endof(htx, HTX_BLK_EOM))
Willy Tarreau88d138e2019-01-02 19:38:14 +01003710 goto fail;
3711 }
3712 else if (*flags & H2_SF_DATA_CHNK) {
3713 if (!b_putblk(rxbuf, "\r\n", 2))
3714 goto fail;
3715 }
3716 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003717
Willy Tarreau86277d42019-01-02 15:36:11 +01003718 /* success */
3719 ret = 1;
3720
Willy Tarreau68dd9852017-07-03 14:44:26 +02003721 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003722 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003723 * move the remaining data over it.
3724 */
3725 if (hole) {
3726 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3727 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3728 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3729 b_sub(&h2c->dbuf, hole);
3730 }
3731
Willy Tarreau97215ca2019-04-29 10:20:21 +02003732 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003733 /* too large frames */
3734 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003735 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003736 }
3737
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003738 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003739 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003740 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003741 return ret;
3742
Willy Tarreau68dd9852017-07-03 14:44:26 +02003743 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003744 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003745 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003746
3747 trailers:
3748 /* This is the last HEADERS frame hence a trailer */
3749
3750 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3751 /* It's a trailer but it's missing ES flag */
3752 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3753 goto fail;
3754 }
3755
Christopher Faulet54b5e212019-06-04 10:08:28 +02003756 /* Trailers terminate a DATA sequence. In HTX we always handle them. In
3757 * legacy, when using chunks, we have to emit the 0 CRLF marker first
3758 * and then handle the trailers. For other modes, the trailers are
3759 * silently dropped.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003760 */
3761 if (htx) {
Willy Tarreau5255f282019-01-03 18:41:05 +01003762 if (h2_make_htx_trailers(list, htx) <= 0)
3763 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003764 }
3765 else if (*flags & H2_SF_DATA_CHNK) {
3766 /* Legacy mode with chunked encoding : we must finalize the
3767 * data block message emit the trailing CRLF */
3768 if (!b_putblk(rxbuf, "0\r\n", 3))
3769 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003770
3771 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3772 if (outlen > 0)
3773 b_add(rxbuf, outlen);
3774 else
3775 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003776 }
3777
3778 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003779}
3780
Willy Tarreau454f9052017-10-26 19:40:35 +02003781/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3782 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3783 * in use, a new chunk is emitted for each frame. This is supposed to fit
3784 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3785 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3786 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003787 * parser state is automatically updated. Returns > 0 if it could completely
3788 * send the current frame, 0 if it couldn't complete, in which case
3789 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3790 * DATA frame can return 0 as a valid result). Stream errors are reported in
3791 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3792 * have checked the frame header and ensured that the frame was complete or the
3793 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003794 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003795static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003796{
3797 struct h2c *h2c = h2s->h2c;
3798 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003799 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003800 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003801 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003802 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003803
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003804 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003805
Olivier Houchard638b7992018-08-16 15:41:52 +02003806 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003807 if (!csbuf) {
3808 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003809 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003810 }
3811
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003812try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003813 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003814 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3815 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003816 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003817 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003818
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003819 if (flen > b_data(&h2c->dbuf)) {
3820 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003821 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003822 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003823 }
3824
Willy Tarreaua9b77962019-01-31 07:23:00 +01003825 if (htx) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003826 unsigned int sent;
3827
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003828 block1 = htx_free_data_space(htx);
3829 if (!block1) {
3830 h2c->flags |= H2_CF_DEM_SFULL;
3831 goto fail;
3832 }
3833 if (flen > block1)
3834 flen = block1;
3835
3836 /* here, flen is the max we can copy into the output buffer */
3837 block1 = b_contig_data(&h2c->dbuf, 0);
3838 if (flen > block1)
3839 flen = block1;
3840
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003841 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003842
Willy Tarreaub6563f42019-06-15 11:34:41 +02003843 b_del(&h2c->dbuf, sent);
3844 h2c->dfl -= sent;
3845 h2c->rcvd_c += sent;
3846 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003847
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003848 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreaub6563f42019-06-15 11:34:41 +02003849 h2s->body_len -= sent;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003850 htx->extra = h2s->body_len;
3851 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003852
3853 if (sent < flen) {
3854 h2c->flags |= H2_CF_DEM_SFULL;
3855 goto fail;
3856 }
3857
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003858 goto try_again;
3859 }
Willy Tarreau455d5682019-05-24 19:42:18 +02003860 else if (unlikely(b_space_wraps(csbuf) &&
3861 flen + chklen <= b_room(csbuf) &&
3862 b_data(csbuf) <= MAX_DATA_REALIGN)) {
3863 /* it doesn't fit in a single block and the buffer is fragmented, if there are
3864 * not too many data in the buffer, let's defragment it and try
3865 * again.
Willy Tarreaud755ea62018-02-26 15:44:54 +01003866 */
3867 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003868 }
3869
Willy Tarreaueba10f22018-04-25 20:44:22 +02003870 /* chunked-encoding requires more room */
3871 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003872 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003873 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3874 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3875 (chklen < 1048576) ? 4 : 8;
3876 chklen += 4; // CRLF, CRLF
3877 }
3878
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003879 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003880 if (flen + chklen > b_room(csbuf)) {
3881 if (chklen >= b_room(csbuf)) {
3882 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003883 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003884 }
3885 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003886 }
3887
3888 if (h2s->flags & H2_SF_DATA_CHNK) {
3889 /* emit the chunk size */
3890 unsigned int chksz = flen;
3891 char str[10];
3892 char *beg;
3893
3894 beg = str + sizeof(str);
3895 *--beg = '\n';
3896 *--beg = '\r';
3897 do {
3898 *--beg = hextab[chksz & 0xF];
3899 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003900 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003901 }
3902
Willy Tarreau454f9052017-10-26 19:40:35 +02003903 /* Block1 is the length of the first block before the buffer wraps,
3904 * block2 is the optional second block to reach the end of the frame.
3905 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003906 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003907 if (block1 > flen)
3908 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003909 block2 = flen - block1;
3910
3911 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003912 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003913
3914 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003915 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003916
Willy Tarreaueba10f22018-04-25 20:44:22 +02003917 if (h2s->flags & H2_SF_DATA_CHNK) {
3918 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003919 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003920 }
3921
Willy Tarreau454f9052017-10-26 19:40:35 +02003922 /* now mark the input data as consumed (will be deleted from the buffer
3923 * by the caller when seeing FRAME_A after sending the window update).
3924 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003925 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003926 h2c->dfl -= flen;
3927 h2c->rcvd_c += flen;
3928 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3929
Willy Tarreau1915ca22019-01-24 11:49:37 +01003930 if (h2s->flags & H2_SF_DATA_CLEN)
3931 h2s->body_len -= flen;
3932
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003933 if (h2c->dfl > h2c->dpl) {
3934 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003935 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003936 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003937 }
3938
Willy Tarreau4a28da12018-01-04 14:41:00 +01003939 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003940 /* here we're done with the frame, all the payload (except padding) was
3941 * transferred.
3942 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003943
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003944 if (h2c->dff & H2_F_DATA_END_STREAM) {
3945 if (htx) {
3946 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3947 h2c->flags |= H2_CF_DEM_SFULL;
3948 goto fail;
3949 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003950 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003951 else if (h2s->flags & H2_SF_DATA_CHNK) {
3952 /* emit the trailing 0 CRLF CRLF */
3953 if (b_room(csbuf) < 5) {
3954 h2c->flags |= H2_CF_DEM_SFULL;
3955 goto fail;
3956 }
3957 chklen += 5;
3958 b_putblk(csbuf, "0\r\n\r\n", 5);
3959 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003960 }
3961
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003962 h2c->rcvd_c += h2c->dpl;
3963 h2c->rcvd_s += h2c->dpl;
3964 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003965 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003966 if (htx)
3967 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003968 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003969 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003970 if (htx)
3971 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003972 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003973}
3974
Willy Tarreau5dd17352018-06-14 13:33:30 +02003975/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3976 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3977 * number of bytes sent. The caller must check the stream's status to detect
3978 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003979 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003980static 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 +02003981{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003982 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003983 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003984 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003985 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02003986 struct buffer *mbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003987 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003988 int es_now = 0;
3989 int ret = 0;
3990 int hdr;
3991
3992 if (h2c_mux_busy(h2c, h2s)) {
3993 h2s->flags |= H2_SF_BLK_MBUSY;
3994 return 0;
3995 }
3996
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003997 /* First, try to parse the H1 response and index it into <list>.
3998 * NOTE! Since it comes from haproxy, we *know* that a response header
3999 * block does not wrap and we can safely read it this way without
4000 * having to realign the buffer.
4001 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004002 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004003 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004004 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01004005 /* incomplete or invalid response, this is abnormal coming from
4006 * haproxy and may only result in a bad errorfile or bad Lua code
4007 * so that won't be fixed, raise an error now.
4008 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004009 * FIXME: we should instead add the ability to only return a
4010 * 502 bad gateway. But in theory this is not supposed to
4011 * happen.
4012 */
4013 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4014 ret = 0;
4015 goto end;
4016 }
4017
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004018 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02004019
4020 /* certain statuses have no body or an empty one, regardless of
4021 * what the headers say.
4022 */
4023 if (sl.st.status >= 100 && sl.st.status < 200) {
4024 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
4025 h1m->curr_len = h1m->body_len = 0;
4026 }
4027 else if (sl.st.status == 204 || sl.st.status == 304) {
4028 /* no contents, claim c-len is present and set to zero */
4029 h1m->flags &= ~H1_MF_CHNK;
4030 h1m->flags |= H1_MF_CLEN;
4031 h1m->curr_len = h1m->body_len = 0;
4032 }
4033
Willy Tarreaubcc45952019-05-26 10:05:50 +02004034 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004035 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004036 if (!h2_get_buf(h2c, mbuf)) {
4037 h2c->flags |= H2_CF_MUX_MALLOC;
4038 h2s->flags |= H2_SF_BLK_MROOM;
4039 return 0;
4040 }
4041
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004042 chunk_reset(&outbuf);
4043
4044 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004045 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4046 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004047 break;
4048 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004049 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004050 }
4051
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004052 if (outbuf.size < 9)
4053 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004054
4055 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004056 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4057 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4058 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004059
4060 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004061 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01004062 /* this is an unparsable response */
4063 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4064 ret = 0;
4065 goto end;
4066 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01004067
4068 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004069 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004070 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004071 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004072 }
4073
4074 /* encode all headers, stop at empty name */
4075 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01004076 /* these ones do not exist in H2 and must be dropped. */
4077 if (isteq(list[hdr].n, ist("connection")) ||
4078 isteq(list[hdr].n, ist("proxy-connection")) ||
4079 isteq(list[hdr].n, ist("keep-alive")) ||
4080 isteq(list[hdr].n, ist("upgrade")) ||
4081 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004082 continue;
4083
4084 if (isteq(list[hdr].n, ist("")))
4085 break; // end
4086
4087 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4088 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004089 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004090 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004091 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004092 }
4093 }
4094
4095 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01004096 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004097 es_now = 1;
4098
4099 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004100 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004101
4102 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004103 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004104
4105 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004106 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004107
4108 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004109 b_add(mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01004110 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004111
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004112 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01004113 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004114 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01004115
Willy Tarreau801250e2018-09-11 11:45:04 +02004116 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004117 h2s->flags |= H2_SF_ES_SENT;
4118 if (h2s->st == H2_SS_OPEN)
4119 h2s->st = H2_SS_HLOC;
4120 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004121 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004122 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004123 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004124 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004125 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004126 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004127 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004128 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004129 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004130
4131 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004132
4133 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004134 //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 +02004135 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004136 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004137 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4138 goto retry;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004139 h1m_init_res(h1m);
4140 h1m->err_pos = -1; // don't care about errors on the response path
4141 h2c->flags |= H2_CF_MUX_MFULL;
4142 h2s->flags |= H2_SF_BLK_MROOM;
4143 ret = 0;
4144 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004145}
4146
Willy Tarreau5dd17352018-06-14 13:33:30 +02004147/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4148 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4149 * the number of bytes sent. The caller must check the stream's status to
4150 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004151 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004152static 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 +02004153{
4154 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004155 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004156 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004157 struct buffer *mbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004158 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004159 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004160 int es_now = 0;
4161 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004162 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004163 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004164
4165 if (h2c_mux_busy(h2c, h2s)) {
4166 h2s->flags |= H2_SF_BLK_MBUSY;
4167 goto end;
4168 }
4169
Willy Tarreaubcc45952019-05-26 10:05:50 +02004170 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004171 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004172 if (!h2_get_buf(h2c, mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004173 h2c->flags |= H2_CF_MUX_MALLOC;
4174 h2s->flags |= H2_SF_BLK_MROOM;
4175 goto end;
4176 }
4177
4178 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004179 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004180 goto end;
4181
4182 chunk_reset(&outbuf);
4183
4184 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004185 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4186 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004187 break;
4188 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004189 /* If there are pending data in the output buffer, and we have
4190 * less than 1/4 of the mbuf's size and everything fits, we'll
4191 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4192 * is full and wait, to save some slow realign calls.
4193 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004194 if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004195 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4196 goto retry;
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004197 h2c->flags |= H2_CF_MUX_MFULL;
4198 h2s->flags |= H2_SF_BLK_MROOM;
4199 goto end;
4200 }
4201
Willy Tarreaubcc45952019-05-26 10:05:50 +02004202 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004203 }
4204
4205 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004206 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4207 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004208 h2c->flags |= H2_CF_MUX_MFULL;
4209 h2s->flags |= H2_SF_BLK_MROOM;
4210 goto end;
4211 }
4212
4213 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004214 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4215 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4216 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004217
4218 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4219 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004220 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004221 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004222 break;
4223 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004224 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004225 if ((long long)size > h1m->curr_len)
4226 size = h1m->curr_len;
4227 break;
4228 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004229 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004230 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004231 if (!ret)
4232 goto end;
4233
4234 if (ret < 0) {
4235 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004236 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004237 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4238 goto end;
4239 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004240 max -= ret;
4241 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004242 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004243 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004244 }
4245
Willy Tarreau801250e2018-09-11 11:45:04 +02004246 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004247 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004248 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004249 if (!ret)
4250 goto end;
4251
4252 if (ret < 0) {
4253 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004254 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004255 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4256 goto end;
4257 }
4258
4259 size = chunk;
4260 h1m->curr_len = chunk;
4261 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004262 max -= ret;
4263 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004264 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004265 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004266 if (!size)
4267 goto send_empty;
4268 }
4269
4270 /* in MSG_DATA state, continue below */
4271 size = h1m->curr_len;
4272 break;
4273 }
4274
4275 /* we have in <size> the exact number of bytes we need to copy from
4276 * the H1 buffer. We need to check this against the connection's and
4277 * the stream's send windows, and to ensure that this fits in the max
4278 * frame size and in the buffer's available space minus 9 bytes (for
4279 * the frame header). The connection's flow control is applied last so
4280 * that we can use a separate list of streams which are immediately
4281 * unblocked on window opening. Note: we don't implement padding.
4282 */
4283
Willy Tarreau5dd17352018-06-14 13:33:30 +02004284 if (size > max)
4285 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004286
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004287 if (size > h2s_mws(h2s))
4288 size = h2s_mws(h2s);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004289
4290 if (size <= 0) {
4291 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004292 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004293 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004294 goto end;
4295 }
4296
4297 if (h2c->mfs && size > h2c->mfs)
4298 size = h2c->mfs;
4299
4300 if (size + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02004301 /* It doesn't fit at once. If it at least fits once split and
4302 * the amount of data to move is low, let's defragment the
4303 * buffer now.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004304 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004305 if (b_space_wraps(mbuf) &&
4306 (size + 9 <= b_room(mbuf)) &&
4307 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004308 goto realign_again;
4309 size = outbuf.size - 9;
4310 }
4311
4312 if (size <= 0) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004313 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4314 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004315 h2c->flags |= H2_CF_MUX_MFULL;
4316 h2s->flags |= H2_SF_BLK_MROOM;
4317 goto end;
4318 }
4319
4320 if (size > h2c->mws)
4321 size = h2c->mws;
4322
4323 if (size <= 0) {
4324 h2s->flags |= H2_SF_BLK_MFCTL;
4325 goto end;
4326 }
4327
4328 /* copy whatever we can */
4329 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004330 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004331 if (ret == 1)
4332 len2 = 0;
4333
4334 if (!ret || len1 + len2 < size) {
4335 /* FIXME: must normally never happen */
4336 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4337 goto end;
4338 }
4339
4340 /* limit len1/len2 to size */
4341 if (len1 + len2 > size) {
4342 int sub = len1 + len2 - size;
4343
4344 if (len2 > sub)
4345 len2 -= sub;
4346 else {
4347 sub -= len2;
4348 len2 = 0;
4349 len1 -= sub;
4350 }
4351 }
4352
4353 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004354 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004355 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004356 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004357
4358 send_empty:
4359 /* we may need to add END_STREAM */
4360 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4361 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004362 *
4363 * FIXME: what we do here is not correct because we send end_stream
4364 * before knowing if we'll have to send a HEADERS frame for the
4365 * trailers. More importantly we're not consuming the trailing CRLF
4366 * after the end of trailers, so it will be left to the caller to
4367 * eat it. The right way to do it would be to measure trailers here
4368 * and to send ES only if there are no trailers.
4369 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004370 */
4371 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004372 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004373 es_now = 1;
4374
4375 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004376 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004377
4378 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004379 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004380
4381 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004382 b_add(mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004383
4384 /* consume incoming H1 response */
4385 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004386 max -= size;
4387 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004388 total += size;
4389 h1m->curr_len -= size;
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004390 h2s->sws -= size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004391 h2c->mws -= size;
4392
4393 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004394 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004395 goto new_frame;
4396 }
4397 }
4398
4399 if (es_now) {
4400 if (h2s->st == H2_SS_OPEN)
4401 h2s->st = H2_SS_HLOC;
4402 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004403 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004404
Willy Tarreau35a62702018-02-27 15:37:25 +01004405 if (!(h1m->flags & H1_MF_CHNK)) {
4406 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004407 total += max;
4408 ofs += max;
4409 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004410
Willy Tarreau801250e2018-09-11 11:45:04 +02004411 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004412 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004413
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004414 h2s->flags |= H2_SF_ES_SENT;
4415 }
4416
4417 end:
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004418 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s_mws(h2s), (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004419 return total;
4420}
4421
Willy Tarreau115e83b2018-12-01 19:17:53 +01004422/* Try to send a HEADERS frame matching HTX response present in HTX message
4423 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4424 * must check the stream's status to detect any error which might have happened
4425 * subsequently to a successful send. The htx blocks are automatically removed
4426 * from the message. The htx message is assumed to be valid since produced from
4427 * the internal code, hence it contains a start line, an optional series of
4428 * header blocks and an end of header, otherwise an invalid frame could be
4429 * emitted and the resulting htx message could be left in an inconsistent state.
4430 */
4431static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4432{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004433 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004434 struct h2c *h2c = h2s->h2c;
4435 struct htx_blk *blk;
4436 struct htx_blk *blk_end;
4437 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004438 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004439 struct htx_sl *sl;
4440 enum htx_blk_type type;
4441 int es_now = 0;
4442 int ret = 0;
4443 int hdr;
4444 int idx;
4445
4446 if (h2c_mux_busy(h2c, h2s)) {
4447 h2s->flags |= H2_SF_BLK_MBUSY;
4448 return 0;
4449 }
4450
Willy Tarreau115e83b2018-12-01 19:17:53 +01004451 /* determine the first block which must not be deleted, blk_end may
4452 * be NULL if all blocks have to be deleted.
4453 */
4454 idx = htx_get_head(htx);
4455 blk_end = NULL;
4456 while (idx != -1) {
4457 type = htx_get_blk_type(htx_get_blk(htx, idx));
4458 idx = htx_get_next(htx, idx);
4459 if (type == HTX_BLK_EOH) {
4460 if (idx != -1)
4461 blk_end = htx_get_blk(htx, idx);
4462 break;
4463 }
4464 }
4465
4466 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004467 blk = htx_get_head_blk(htx);
4468 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL);
4469 ALREADY_CHECKED(blk);
4470 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004471 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004472 if (h2s->status < 100 || h2s->status > 999)
4473 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004474
4475 /* and the rest of the headers, that we dump starting at header 0 */
4476 hdr = 0;
4477
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004478 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004479 while ((idx = htx_get_next(htx, idx)) != -1) {
4480 blk = htx_get_blk(htx, idx);
4481 type = htx_get_blk_type(blk);
4482
4483 if (type == HTX_BLK_UNUSED)
4484 continue;
4485
4486 if (type != HTX_BLK_HDR)
4487 break;
4488
4489 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4490 goto fail;
4491
4492 list[hdr].n = htx_get_blk_name(htx, blk);
4493 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004494 hdr++;
4495 }
4496
4497 /* marker for end of headers */
4498 list[hdr].n = ist("");
4499
4500 if (h2s->status == 204 || h2s->status == 304) {
4501 /* no contents, claim c-len is present and set to zero */
4502 es_now = 1;
4503 }
4504
Willy Tarreau9c218e72019-05-26 10:08:28 +02004505 mbuf = br_tail(h2c->mbuf);
4506 retry:
4507 if (!h2_get_buf(h2c, mbuf)) {
4508 h2c->flags |= H2_CF_MUX_MALLOC;
4509 h2s->flags |= H2_SF_BLK_MROOM;
4510 return 0;
4511 }
4512
Willy Tarreau115e83b2018-12-01 19:17:53 +01004513 chunk_reset(&outbuf);
4514
4515 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004516 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4517 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004518 break;
4519 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004520 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004521 }
4522
4523 if (outbuf.size < 9)
4524 goto full;
4525
4526 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4527 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4528 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4529 outbuf.data = 9;
4530
4531 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004532 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004533 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004534 goto realign_again;
4535 goto full;
4536 }
4537
4538 /* encode all headers, stop at empty name */
4539 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4540 /* these ones do not exist in H2 and must be dropped. */
4541 if (isteq(list[hdr].n, ist("connection")) ||
4542 isteq(list[hdr].n, ist("proxy-connection")) ||
4543 isteq(list[hdr].n, ist("keep-alive")) ||
4544 isteq(list[hdr].n, ist("upgrade")) ||
4545 isteq(list[hdr].n, ist("transfer-encoding")))
4546 continue;
4547
4548 if (isteq(list[hdr].n, ist("")))
4549 break; // end
4550
4551 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4552 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004553 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004554 goto realign_again;
4555 goto full;
4556 }
4557 }
4558
Christopher Faulet0b465482019-02-19 15:14:23 +01004559 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004560 * FIXME: we should also set it when we know for sure that the
4561 * content-length is zero as well as on 204/304
4562 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004563 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4564 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004565 es_now = 1;
4566
Willy Tarreau927b88b2019-03-04 08:03:25 +01004567 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004568 es_now = 1;
4569
4570 /* update the frame's size */
4571 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4572
4573 if (es_now)
4574 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4575
4576 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004577 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004578
4579 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4580 * 1xx responses, another HEADERS frame is expected.
4581 */
4582 if (h2s->status >= 200 || h2s->status == 101)
4583 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004584
Willy Tarreau115e83b2018-12-01 19:17:53 +01004585 if (es_now) {
4586 h2s->flags |= H2_SF_ES_SENT;
4587 if (h2s->st == H2_SS_OPEN)
4588 h2s->st = H2_SS_HLOC;
4589 else
4590 h2s_close(h2s);
4591 }
4592
4593 /* OK we could properly deliver the response */
4594
4595 /* remove all header blocks including the EOH and compute the
4596 * corresponding size.
4597 *
4598 * FIXME: We should remove everything when es_now is set.
4599 */
4600 ret = 0;
4601 idx = htx_get_head(htx);
4602 blk = htx_get_blk(htx, idx);
4603 while (blk != blk_end) {
4604 ret += htx_get_blksz(blk);
4605 blk = htx_remove_blk(htx, blk);
4606 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004607
Christopher Faulet316934d2019-05-15 14:22:48 +02004608 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4609 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004610 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004611 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004612 end:
4613 return ret;
4614 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004615 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4616 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004617 h2c->flags |= H2_CF_MUX_MFULL;
4618 h2s->flags |= H2_SF_BLK_MROOM;
4619 ret = 0;
4620 goto end;
4621 fail:
4622 /* unparsable HTX messages, too large ones to be produced in the local
4623 * list etc go here (unrecoverable errors).
4624 */
4625 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4626 ret = 0;
4627 goto end;
4628}
4629
Willy Tarreau80739692018-10-05 11:35:57 +02004630/* Try to send a HEADERS frame matching HTX request present in HTX message
4631 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4632 * must check the stream's status to detect any error which might have happened
4633 * subsequently to a successful send. The htx blocks are automatically removed
4634 * from the message. The htx message is assumed to be valid since produced from
4635 * the internal code, hence it contains a start line, an optional series of
4636 * header blocks and an end of header, otherwise an invalid frame could be
4637 * emitted and the resulting htx message could be left in an inconsistent state.
4638 */
4639static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4640{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004641 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004642 struct h2c *h2c = h2s->h2c;
4643 struct htx_blk *blk;
4644 struct htx_blk *blk_end;
4645 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004646 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004647 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004648 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004649 enum htx_blk_type type;
4650 int es_now = 0;
4651 int ret = 0;
4652 int hdr;
4653 int idx;
4654
4655 if (h2c_mux_busy(h2c, h2s)) {
4656 h2s->flags |= H2_SF_BLK_MBUSY;
4657 return 0;
4658 }
4659
Willy Tarreau80739692018-10-05 11:35:57 +02004660 /* determine the first block which must not be deleted, blk_end may
4661 * be NULL if all blocks have to be deleted.
4662 */
4663 idx = htx_get_head(htx);
4664 blk_end = NULL;
4665 while (idx != -1) {
4666 type = htx_get_blk_type(htx_get_blk(htx, idx));
4667 idx = htx_get_next(htx, idx);
4668 if (type == HTX_BLK_EOH) {
4669 if (idx != -1)
4670 blk_end = htx_get_blk(htx, idx);
4671 break;
4672 }
4673 }
4674
4675 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004676 blk = htx_get_head_blk(htx);
4677 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
4678 ALREADY_CHECKED(blk);
4679 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004680 meth = htx_sl_req_meth(sl);
4681 path = htx_sl_req_uri(sl);
4682
4683 /* and the rest of the headers, that we dump starting at header 0 */
4684 hdr = 0;
4685
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004686 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004687 while ((idx = htx_get_next(htx, idx)) != -1) {
4688 blk = htx_get_blk(htx, idx);
4689 type = htx_get_blk_type(blk);
4690
4691 if (type == HTX_BLK_UNUSED)
4692 continue;
4693
4694 if (type != HTX_BLK_HDR)
4695 break;
4696
4697 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4698 goto fail;
4699
4700 list[hdr].n = htx_get_blk_name(htx, blk);
4701 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004702 hdr++;
4703 }
4704
4705 /* marker for end of headers */
4706 list[hdr].n = ist("");
4707
Willy Tarreau9c218e72019-05-26 10:08:28 +02004708 mbuf = br_tail(h2c->mbuf);
4709 retry:
4710 if (!h2_get_buf(h2c, mbuf)) {
4711 h2c->flags |= H2_CF_MUX_MALLOC;
4712 h2s->flags |= H2_SF_BLK_MROOM;
4713 return 0;
4714 }
4715
Willy Tarreau80739692018-10-05 11:35:57 +02004716 chunk_reset(&outbuf);
4717
4718 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004719 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4720 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004721 break;
4722 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004723 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004724 }
4725
4726 if (outbuf.size < 9)
4727 goto full;
4728
4729 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4730 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4731 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4732 outbuf.data = 9;
4733
4734 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004735 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004736 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004737 goto realign_again;
4738 goto full;
4739 }
4740
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004741 /* RFC7540 #8.3: the CONNECT method must have :
4742 * - :authority set to the URI part (host:port)
4743 * - :method set to CONNECT
4744 * - :scheme and :path omitted
4745 */
4746 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4747 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004748 struct ist scheme;
4749
4750 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4751 scheme = ist("http");
4752 else
4753 scheme = ist("https");
4754
4755 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004756 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004757 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004758 goto realign_again;
4759 goto full;
4760 }
Willy Tarreau80739692018-10-05 11:35:57 +02004761
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004762 /* encode the path, which necessarily is the second one */
4763 if (!hpack_encode_path(&outbuf, path)) {
4764 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004765 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004766 goto realign_again;
4767 goto full;
4768 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004769
4770 /* look for the Host header and place it in :authority */
4771 auth = ist2(NULL, 0);
4772 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4773 if (isteq(list[hdr].n, ist("")))
4774 break; // end
4775
4776 if (isteq(list[hdr].n, ist("host"))) {
4777 auth = list[hdr].v;
4778 break;
4779 }
4780 }
4781 }
4782 else {
4783 /* for CONNECT, :authority is taken from the path */
4784 auth = path;
4785 }
4786
4787 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4788 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004789 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004790 goto realign_again;
4791 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004792 }
4793
4794 /* encode all headers, stop at empty name */
4795 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4796 /* these ones do not exist in H2 and must be dropped. */
4797 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004798 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004799 isteq(list[hdr].n, ist("proxy-connection")) ||
4800 isteq(list[hdr].n, ist("keep-alive")) ||
4801 isteq(list[hdr].n, ist("upgrade")) ||
4802 isteq(list[hdr].n, ist("transfer-encoding")))
4803 continue;
4804
4805 if (isteq(list[hdr].n, ist("")))
4806 break; // end
4807
4808 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4809 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004810 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004811 goto realign_again;
4812 goto full;
4813 }
4814 }
4815
4816 /* we may need to add END_STREAM if we have no body :
4817 * - request already closed, or :
4818 * - no transfer-encoding, and :
4819 * - no content-length or content-length:0
4820 * Fixme: this doesn't take into account CONNECT requests.
4821 */
4822 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4823 es_now = 1;
4824
4825 if (sl->flags & HTX_SL_F_BODYLESS)
4826 es_now = 1;
4827
Willy Tarreau927b88b2019-03-04 08:03:25 +01004828 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004829 es_now = 1;
4830
4831 /* update the frame's size */
4832 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4833
4834 if (es_now)
4835 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4836
4837 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004838 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004839 h2s->flags |= H2_SF_HEADERS_SENT;
4840 h2s->st = H2_SS_OPEN;
4841
Willy Tarreau80739692018-10-05 11:35:57 +02004842 if (es_now) {
4843 // trim any possibly pending data (eg: inconsistent content-length)
4844 h2s->flags |= H2_SF_ES_SENT;
4845 h2s->st = H2_SS_HLOC;
4846 }
4847
4848 /* remove all header blocks including the EOH and compute the
4849 * corresponding size.
4850 *
4851 * FIXME: We should remove everything when es_now is set.
4852 */
4853 ret = 0;
4854 idx = htx_get_head(htx);
4855 blk = htx_get_blk(htx, idx);
4856 while (blk != blk_end) {
4857 ret += htx_get_blksz(blk);
4858 blk = htx_remove_blk(htx, blk);
4859 }
4860
Christopher Faulet316934d2019-05-15 14:22:48 +02004861 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4862 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004863 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004864 }
Willy Tarreau80739692018-10-05 11:35:57 +02004865
4866 end:
4867 return ret;
4868 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004869 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4870 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004871 h2c->flags |= H2_CF_MUX_MFULL;
4872 h2s->flags |= H2_SF_BLK_MROOM;
4873 ret = 0;
4874 goto end;
4875 fail:
4876 /* unparsable HTX messages, too large ones to be produced in the local
4877 * list etc go here (unrecoverable errors).
4878 */
4879 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4880 ret = 0;
4881 goto end;
4882}
4883
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004884/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004885 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4886 * caller must check the stream's status to detect any error which might have
4887 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02004888 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004889 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004890static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004891{
4892 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004893 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004894 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004895 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004896 size_t total = 0;
4897 int es_now = 0;
4898 int bsize; /* htx block size */
4899 int fsize; /* h2 frame size */
4900 struct htx_blk *blk;
4901 enum htx_blk_type type;
4902 int idx;
4903
4904 if (h2c_mux_busy(h2c, h2s)) {
4905 h2s->flags |= H2_SF_BLK_MBUSY;
4906 goto end;
4907 }
4908
Willy Tarreau98de12a2018-12-12 07:03:00 +01004909 htx = htx_from_buf(buf);
4910
Christopher Faulet54b5e212019-06-04 10:08:28 +02004911 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
4912 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
4913 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004914 */
4915
4916 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004917 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004918 goto end;
4919
4920 idx = htx_get_head(htx);
4921 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02004922 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004923 bsize = htx_get_blksz(blk);
4924 fsize = bsize;
4925
Christopher Faulet54b5e212019-06-04 10:08:28 +02004926 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004927 if (h2s->flags & H2_SF_ES_SENT) {
4928 /* ES already sent */
4929 htx_remove_blk(htx, blk);
4930 total++; // EOM counts as one byte
4931 count--;
4932 goto end;
4933 }
4934 }
4935 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004936 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004937
Willy Tarreau9c218e72019-05-26 10:08:28 +02004938 mbuf = br_tail(h2c->mbuf);
4939 retry:
4940 if (!h2_get_buf(h2c, mbuf)) {
4941 h2c->flags |= H2_CF_MUX_MALLOC;
4942 h2s->flags |= H2_SF_BLK_MROOM;
4943 goto end;
4944 }
4945
Willy Tarreau98de12a2018-12-12 07:03:00 +01004946 /* Perform some optimizations to reduce the number of buffer copies.
4947 * First, if the mux's buffer is empty and the htx area contains
4948 * exactly one data block of the same size as the requested count, and
4949 * this count fits within the frame size, the stream's window size, and
4950 * the connection's window size, then it's possible to simply swap the
4951 * caller's buffer with the mux's output buffer and adjust offsets and
4952 * length to match the entire DATA HTX block in the middle. In this
4953 * case we perform a true zero-copy operation from end-to-end. This is
4954 * the situation that happens all the time with large files. Second, if
4955 * this is not possible, but the mux's output buffer is empty, we still
4956 * have an opportunity to avoid the copy to the intermediary buffer, by
4957 * making the intermediary buffer's area point to the output buffer's
4958 * area. In this case we want to skip the HTX header to make sure that
4959 * copies remain aligned and that this operation remains possible all
4960 * the time. This goes for headers, data blocks and any data extracted
4961 * from the HTX blocks.
4962 */
4963 if (unlikely(fsize == count &&
4964 htx->used == 1 && type == HTX_BLK_DATA &&
Willy Tarreau5a9c8752019-08-02 07:52:08 +02004965 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004966 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004967
Willy Tarreaubcc45952019-05-26 10:05:50 +02004968 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004969 /* Too bad there are data left there. We're willing to memcpy/memmove
4970 * up to 1/4 of the buffer, which means that it's OK to copy a large
4971 * frame into a buffer containing few data if it needs to be realigned,
4972 * and that it's also OK to copy few data without realigning. Otherwise
4973 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004974 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004975 if (fsize + 9 <= b_room(mbuf) &&
4976 (b_data(mbuf) <= b_size(mbuf) / 4 ||
4977 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004978 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004979
Willy Tarreau9c218e72019-05-26 10:08:28 +02004980 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4981 goto retry;
4982
Willy Tarreau98de12a2018-12-12 07:03:00 +01004983 h2c->flags |= H2_CF_MUX_MFULL;
4984 h2s->flags |= H2_SF_BLK_MROOM;
4985 goto end;
4986 }
4987
4988 /* map an H2 frame to the HTX block so that we can put the
4989 * frame header there.
4990 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004991 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
4992 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01004993
4994 /* prepend an H2 DATA frame header just before the DATA block */
4995 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4996 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4997 h2_set_frame_size(outbuf.area, fsize);
4998
4999 /* update windows */
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005000 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005001 h2c->mws -= fsize;
5002
5003 /* and exchange with our old area */
5004 buf->area = old_area;
5005 buf->data = buf->head = 0;
5006 total += fsize;
5007 goto end;
5008 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005009
Willy Tarreau98de12a2018-12-12 07:03:00 +01005010 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005011 /* for DATA and EOM we'll have to emit a frame, even if empty */
5012
5013 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005014 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5015 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005016 break;
5017 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005018 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005019 }
5020
5021 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005022 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5023 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005024 h2c->flags |= H2_CF_MUX_MFULL;
5025 h2s->flags |= H2_SF_BLK_MROOM;
5026 goto end;
5027 }
5028
5029 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5030 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5031 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5032 outbuf.data = 9;
5033
5034 /* we have in <fsize> the exact number of bytes we need to copy from
5035 * the HTX buffer. We need to check this against the connection's and
5036 * the stream's send windows, and to ensure that this fits in the max
5037 * frame size and in the buffer's available space minus 9 bytes (for
5038 * the frame header). The connection's flow control is applied last so
5039 * that we can use a separate list of streams which are immediately
5040 * unblocked on window opening. Note: we don't implement padding.
5041 */
5042
5043 /* EOM is presented with bsize==1 but would lead to the emission of an
5044 * empty frame, thus we force it to zero here.
5045 */
5046 if (type == HTX_BLK_EOM)
5047 bsize = fsize = 0;
5048
5049 if (!fsize)
5050 goto send_empty;
5051
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005052 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005053 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005054 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005055 LIST_DEL_INIT(&h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005056 goto end;
5057 }
5058
Willy Tarreauee573762018-12-04 15:25:57 +01005059 if (fsize > count)
5060 fsize = count;
5061
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005062 if (fsize > h2s_mws(h2s))
5063 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005064
5065 if (h2c->mfs && fsize > h2c->mfs)
5066 fsize = h2c->mfs; // >0
5067
5068 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005069 /* It doesn't fit at once. If it at least fits once split and
5070 * the amount of data to move is low, let's defragment the
5071 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005072 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005073 if (b_space_wraps(mbuf) &&
5074 (fsize + 9 <= b_room(mbuf)) &&
5075 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005076 goto realign_again;
5077 fsize = outbuf.size - 9;
5078
5079 if (fsize <= 0) {
5080 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005081 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5082 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005083 h2c->flags |= H2_CF_MUX_MFULL;
5084 h2s->flags |= H2_SF_BLK_MROOM;
5085 goto end;
5086 }
5087 }
5088
5089 if (h2c->mws <= 0) {
5090 h2s->flags |= H2_SF_BLK_MFCTL;
5091 goto end;
5092 }
5093
5094 if (fsize > h2c->mws)
5095 fsize = h2c->mws;
5096
5097 /* now let's copy this this into the output buffer */
5098 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau5a9c8752019-08-02 07:52:08 +02005099 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005100 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005101 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005102
5103 send_empty:
5104 /* update the frame's size */
5105 h2_set_frame_size(outbuf.area, fsize);
5106
5107 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5108 * meeting EOM. We should optimize this later.
5109 */
5110 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005111 total++; // EOM counts as one byte
5112 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005113 es_now = 1;
5114 }
5115
5116 if (es_now)
5117 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5118
5119 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005120 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005121
5122 /* consume incoming HTX block, including EOM */
5123 total += fsize;
5124 if (fsize == bsize) {
5125 htx_remove_blk(htx, blk);
5126 if (fsize)
5127 goto new_frame;
5128 } else {
5129 /* we've truncated this block */
5130 htx_cut_data_blk(htx, blk, fsize);
5131 }
5132
5133 if (es_now) {
5134 if (h2s->st == H2_SS_OPEN)
5135 h2s->st = H2_SS_HLOC;
5136 else
5137 h2s_close(h2s);
5138
5139 h2s->flags |= H2_SF_ES_SENT;
5140 }
5141
5142 end:
5143 return total;
5144}
5145
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005146/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5147 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5148 * processed. The caller must check the stream's status to detect any error
5149 * which might have happened subsequently to a successful send. The htx blocks
5150 * are automatically removed from the message. The htx message is assumed to be
5151 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005152 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005153 * as a single frame. The ES flag is always set.
5154 */
5155static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5156{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005157 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005158 struct h2c *h2c = h2s->h2c;
5159 struct htx_blk *blk;
5160 struct htx_blk *blk_end;
5161 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005162 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005163 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005164 int ret = 0;
5165 int hdr;
5166 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005167
5168 if (h2c_mux_busy(h2c, h2s)) {
5169 h2s->flags |= H2_SF_BLK_MBUSY;
5170 goto end;
5171 }
5172
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005173 /* determine the first block which must not be deleted, blk_end may
5174 * be NULL if all blocks have to be deleted. also get trailers.
5175 */
5176 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005177 blk_end = NULL;
5178
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005179 hdr = 0;
5180 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005181 blk = htx_get_blk(htx, idx);
5182 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005183 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005184 if (type == HTX_BLK_UNUSED)
5185 continue;
5186
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005187 if (type == HTX_BLK_EOT) {
5188 if (idx != -1)
5189 blk_end = blk;
5190 break;
5191 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005192 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005193 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005194
5195 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5196 goto fail;
5197
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005198 list[hdr].n = htx_get_blk_name(htx, blk);
5199 list[hdr].v = htx_get_blk_value(htx, blk);
5200 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005201 }
5202
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005203 /* marker for end of trailers */
5204 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005205
Willy Tarreau9c218e72019-05-26 10:08:28 +02005206 mbuf = br_tail(h2c->mbuf);
5207 retry:
5208 if (!h2_get_buf(h2c, mbuf)) {
5209 h2c->flags |= H2_CF_MUX_MALLOC;
5210 h2s->flags |= H2_SF_BLK_MROOM;
5211 goto end;
5212 }
5213
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005214 chunk_reset(&outbuf);
5215
5216 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005217 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5218 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005219 break;
5220 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005221 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005222 }
5223
5224 if (outbuf.size < 9)
5225 goto full;
5226
5227 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5228 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5229 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5230 outbuf.data = 9;
5231
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005232 /* encode all headers */
5233 for (idx = 0; idx < hdr; idx++) {
5234 /* these ones do not exist in H2 or must not appear in
5235 * trailers and must be dropped.
5236 */
5237 if (isteq(list[idx].n, ist("host")) ||
5238 isteq(list[idx].n, ist("content-length")) ||
5239 isteq(list[idx].n, ist("connection")) ||
5240 isteq(list[idx].n, ist("proxy-connection")) ||
5241 isteq(list[idx].n, ist("keep-alive")) ||
5242 isteq(list[idx].n, ist("upgrade")) ||
5243 isteq(list[idx].n, ist("te")) ||
5244 isteq(list[idx].n, ist("transfer-encoding")))
5245 continue;
5246
5247 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5248 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005249 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005250 goto realign_again;
5251 goto full;
5252 }
5253 }
5254
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005255 if (outbuf.data == 9) {
5256 /* here we have a problem, we have nothing to emit (either we
5257 * received an empty trailers block followed or we removed its
5258 * contents above). Because of this we can't send a HEADERS
5259 * frame, so we have to cheat and instead send an empty DATA
5260 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005261 */
5262 outbuf.area[3] = H2_FT_DATA;
5263 outbuf.area[4] = H2_F_DATA_END_STREAM;
5264 }
5265
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005266 /* update the frame's size */
5267 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5268
5269 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005270 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005271 h2s->flags |= H2_SF_ES_SENT;
5272
5273 if (h2s->st == H2_SS_OPEN)
5274 h2s->st = H2_SS_HLOC;
5275 else
5276 h2s_close(h2s);
5277
5278 /* OK we could properly deliver the response */
5279 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005280 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005281 ret = 0;
5282 idx = htx_get_head(htx);
5283 blk = htx_get_blk(htx, idx);
5284 while (blk != blk_end) {
5285 ret += htx_get_blksz(blk);
5286 blk = htx_remove_blk(htx, blk);
5287 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005288
5289 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5290 ret += htx_get_blksz(blk_end);
5291 htx_remove_blk(htx, blk_end);
5292 }
5293
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005294 end:
5295 return ret;
5296 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005297 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5298 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005299 h2c->flags |= H2_CF_MUX_MFULL;
5300 h2s->flags |= H2_SF_BLK_MROOM;
5301 ret = 0;
5302 goto end;
5303 fail:
5304 /* unparsable HTX messages, too large ones to be produced in the local
5305 * list etc go here (unrecoverable errors).
5306 */
5307 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5308 ret = 0;
5309 goto end;
5310}
5311
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005312/* Called from the upper layer, to subscribe to events, such as being able to send.
5313 * The <param> argument here is supposed to be a pointer to a wait_event struct
5314 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5315 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5316 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5317 * returns 0 except for the error above.
5318 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005319static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5320{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005321 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005322 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005323 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005324
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005325 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005326 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005327 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5328 sw->events |= SUB_RETRY_RECV;
5329 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005330 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005331 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005332 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005333 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005334 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5335 sw->events |= SUB_RETRY_SEND;
5336 h2s->send_wait = sw;
5337 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5338 !LIST_ADDED(&h2s->list)) {
5339 if (h2s->flags & H2_SF_BLK_MFCTL)
5340 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5341 else
5342 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005343 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005344 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005345 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005346 if (event_type != 0)
5347 return -1;
5348 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005349}
5350
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005351/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5352 * The <param> argument here is supposed to be a pointer to the same wait_event
5353 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5354 * It always returns zero.
5355 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005356static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5357{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005358 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005359 struct h2s *h2s = cs->ctx;
5360
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005361 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005362 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005363 BUG_ON(h2s->recv_wait != sw);
5364 sw->events &= ~SUB_RETRY_RECV;
5365 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005366 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005367 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005368 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005369 BUG_ON(h2s->send_wait != sw);
5370 LIST_DEL(&h2s->list);
5371 LIST_INIT(&h2s->list);
5372 sw->events &= ~SUB_RETRY_SEND;
5373 /* We were about to send, make sure it does not happen */
5374 if (LIST_ADDED(&h2s->sending_list) &&
5375 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02005376 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02005377 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005378 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005379 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005380 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005381 return 0;
5382}
5383
5384
Olivier Houchard511efea2018-08-16 15:30:32 +02005385/* Called from the upper layer, to receive data */
5386static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5387{
Olivier Houchard638b7992018-08-16 15:41:52 +02005388 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005389 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005390 struct htx *h2s_htx = NULL;
5391 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005392 size_t ret = 0;
5393
5394 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005395 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau86724e22018-12-01 23:19:43 +01005396 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005397 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005398 /* Here htx_to_buf() will set buffer data to 0 because
5399 * the HTX is empty.
5400 */
5401 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005402 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005403 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005404
Christopher Faulet156852b2019-05-16 11:29:13 +02005405 ret = h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005406 buf_htx = htx_from_buf(buf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005407
Christopher Faulet2f6edc82019-05-15 10:07:59 +02005408 /* <buf> is empty and the message is small enough, swap the
5409 * buffers. */
5410 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
5411 htx_to_buf(buf_htx, buf);
5412 htx_to_buf(h2s_htx, &h2s->rxbuf);
5413 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5414 goto end;
5415 }
5416
Christopher Faulet156852b2019-05-16 11:29:13 +02005417 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005418
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005419 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
Willy Tarreau7196dd62019-03-05 10:51:11 +01005420 buf_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005421 if (htx_is_empty(buf_htx))
5422 cs->flags |= CS_FL_EOI;
5423 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005424
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005425 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005426 htx_to_buf(buf_htx, buf);
5427 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet156852b2019-05-16 11:29:13 +02005428 ret -= h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005429 }
5430 else {
5431 ret = b_xfer(buf, &h2s->rxbuf, count);
5432 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005433
Christopher Faulet37070b22019-02-14 15:12:14 +01005434 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005435 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005436 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005437 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005438 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005439 if (h2s->flags & H2_SF_ES_RCVD)
5440 cs->flags |= CS_FL_EOI;
Willy Tarreau76c83822019-06-15 09:55:50 +02005441 if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02005442 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005443 if (cs->flags & CS_FL_ERR_PENDING)
5444 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005445 if (b_size(&h2s->rxbuf)) {
5446 b_free(&h2s->rxbuf);
5447 offer_buffers(NULL, tasks_run_queue);
5448 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005449 }
5450
Willy Tarreau082f5592018-11-25 08:03:32 +01005451 if (ret && h2c->dsi == h2s->id) {
5452 /* demux is blocking on this stream's buffer */
5453 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005454 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005455 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005456
Olivier Houchard511efea2018-08-16 15:30:32 +02005457 return ret;
5458}
5459
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005460/* stops all senders of this connection for example when the mux buffer is full.
5461 * They are moved from the sending_list to either fctl_list or send_list.
5462 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005463static void h2_stop_senders(struct h2c *h2c)
5464{
5465 struct h2s *h2s, *h2s_back;
5466
Olivier Houchardd360ac62019-03-22 17:37:16 +01005467 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5468 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02005469 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005470 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005471 }
5472}
5473
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005474/* Called from the upper layer, to send data from buffer <buf> for no more than
5475 * <count> bytes. Returns the number of bytes effectively sent. Some status
5476 * flags may be updated on the conn_stream.
5477 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005478static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005479{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005480 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005481 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005482 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005483 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005484 struct htx *htx;
5485 struct htx_blk *blk;
5486 enum htx_blk_type btype;
5487 uint32_t bsize;
5488 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005489
Olivier Houchardd360ac62019-03-22 17:37:16 +01005490 /* If we were not just woken because we wanted to send but couldn't,
5491 * and there's somebody else that is waiting to send, do nothing,
5492 * we will subscribe later and be put at the end of the list
5493 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005494 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005495 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5496 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005497 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005498
Olivier Houchard998410a2019-04-15 19:23:37 +02005499 /* We couldn't set it to NULL before, because we needed it in case
5500 * we had to cancel the tasklet
5501 */
5502 h2s->send_wait = NULL;
5503
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005504 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5505 return 0;
5506
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005507 /* htx will be enough to decide if we're using HTX or legacy */
5508 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5509
Willy Tarreau0bad0432018-06-14 16:54:01 +02005510 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005511 h2s->flags |= H2_SF_OUTGOING_DATA;
5512
Willy Tarreau751f2d02018-10-05 09:35:00 +02005513 if (h2s->id == 0) {
5514 int32_t id = h2c_get_next_sid(h2s->h2c);
5515
5516 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005517 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005518 return 0;
5519 }
5520
5521 eb32_delete(&h2s->by_id);
5522 h2s->by_id.key = h2s->id = id;
5523 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005524 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005525 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5526 }
5527
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005528 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005529 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005530 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005531 idx = htx_get_head(htx);
5532 blk = htx_get_blk(htx, idx);
5533 btype = htx_get_blk_type(blk);
5534 bsize = htx_get_blksz(blk);
5535
5536 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005537 case HTX_BLK_REQ_SL:
5538 /* start-line before headers */
5539 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5540 if (ret > 0) {
5541 total += ret;
5542 count -= ret;
5543 if (ret < bsize)
5544 goto done;
5545 }
5546 break;
5547
Willy Tarreau115e83b2018-12-01 19:17:53 +01005548 case HTX_BLK_RES_SL:
5549 /* start-line before headers */
5550 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5551 if (ret > 0) {
5552 total += ret;
5553 count -= ret;
5554 if (ret < bsize)
5555 goto done;
5556 }
5557 break;
5558
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005559 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005560 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005561 /* all these cause the emission of a DATA frame (possibly empty).
5562 * This EOM necessarily is one before trailers, as the EOM following
5563 * trailers would have been consumed by the trailers parser.
5564 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005565 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005566 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005567 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005568 total += ret;
5569 count -= ret;
5570 if (ret < bsize)
5571 goto done;
5572 }
5573 break;
5574
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005575 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005576 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005577 /* This is the first trailers block, all the subsequent ones AND
5578 * the EOM will be swallowed by the parser.
5579 */
5580 ret = h2s_htx_make_trailers(h2s, htx);
5581 if (ret > 0) {
5582 total += ret;
5583 count -= ret;
5584 if (ret < bsize)
5585 goto done;
5586 }
5587 break;
5588
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005589 default:
5590 htx_remove_blk(htx, blk);
5591 total += bsize;
5592 count -= bsize;
5593 break;
5594 }
5595 }
5596 goto done;
5597 }
5598
5599 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005600 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005601 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005602 if (h2s->h2c->flags & H2_CF_IS_BACK)
5603 ret = -1;
5604 else
5605 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005606 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005607 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005608 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005609 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005610 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005611 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005612 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005613
Willy Tarreau5dd17352018-06-14 13:33:30 +02005614 if (unlikely((int)ret <= 0)) {
5615 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005616 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5617 break;
5618 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005619 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005620 total += count;
5621 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005622 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005623 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005624 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005625 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005626 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005627 break;
5628 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005629
5630 total += ret;
5631 count -= ret;
5632
Willy Tarreau2b778482019-05-06 15:00:22 +02005633 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005634 break;
5635
5636 if (h2s->flags & H2_SF_BLK_ANY)
5637 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005638 }
5639
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005640 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005641 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005642 /* trim any possibly pending data after we close (extra CR-LF,
5643 * unprocessed trailers, abnormal extra data, ...)
5644 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005645 total += count;
5646 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005647 }
5648
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005649 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005650 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005651 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005652 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005653 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005654 }
5655
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005656 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005657 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005658 } else {
5659 b_del(buf, total);
5660 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005661
5662 /* The mux is full, cancel the pending tasks */
5663 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5664 (h2s->flags & H2_SF_BLK_MBUSY))
5665 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005666
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005667 /* If we're running HTX, and we read the whole buffer, then pretend
5668 * we read exactly what the caller specified, as with HTX the caller
5669 * will always give the buffer size, instead of the amount of data
5670 * available.
5671 */
5672 if (htx && !b_data(buf))
5673 total = orig_count;
5674
Olivier Houchard7505f942018-08-21 18:10:44 +02005675 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005676 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005677 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02005678
Olivier Houchard7505f942018-08-21 18:10:44 +02005679 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005680 /* If we're waiting for flow control, and we got a shutr on the
5681 * connection, we will never be unlocked, so add an error on
5682 * the conn_stream.
5683 */
5684 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5685 !b_data(&h2s->h2c->dbuf) &&
5686 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5687 if (cs->flags & CS_FL_EOS)
5688 cs->flags |= CS_FL_ERROR;
5689 else
5690 cs->flags |= CS_FL_ERR_PENDING;
5691 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005692 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005693 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005694 LIST_DEL_INIT(&h2s->list);
5695 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005696 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005697}
5698
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005699/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005700static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005701{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005702 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005703 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005704 struct eb32_node *node;
5705 int fctl_cnt = 0;
5706 int send_cnt = 0;
5707 int tree_cnt = 0;
5708 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005709 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005710
5711 if (!h2c)
5712 return;
5713
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005714 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005715 fctl_cnt++;
5716
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005717 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005718 send_cnt++;
5719
Willy Tarreau3af37712018-12-18 14:34:41 +01005720 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005721 node = eb32_first(&h2c->streams_by_id);
5722 while (node) {
5723 h2s = container_of(node, struct h2s, by_id);
5724 tree_cnt++;
5725 if (!h2s->cs)
5726 orph_cnt++;
5727 node = eb32_next(node);
5728 }
5729
Willy Tarreau60f62682019-05-26 11:32:27 +02005730 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005731 tmbuf = br_tail(h2c->mbuf);
Willy Tarreau987c0632018-12-18 10:32:05 +01005732 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5733 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005734 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5735 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau616ac812018-07-24 14:12:42 +02005736 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5737 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005738 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005739 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5740 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5741 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005742 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5743 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5744 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005745 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5746 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005747
5748 if (h2s) {
5749 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5750 h2s, h2s->id, h2s->flags,
5751 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5752 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5753 h2s->cs);
5754 if (h2s->cs)
5755 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5756 h2s->cs->flags, h2s->cs->data);
5757 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005758}
Willy Tarreau62f52692017-10-08 23:01:42 +02005759
5760/*******************************************************/
5761/* functions below are dedicated to the config parsers */
5762/*******************************************************/
5763
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005764/* config parser for global "tune.h2.header-table-size" */
5765static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5766 struct proxy *defpx, const char *file, int line,
5767 char **err)
5768{
5769 if (too_many_args(1, args, err, NULL))
5770 return -1;
5771
5772 h2_settings_header_table_size = atoi(args[1]);
5773 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5774 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5775 return -1;
5776 }
5777 return 0;
5778}
Willy Tarreau62f52692017-10-08 23:01:42 +02005779
Willy Tarreaue6baec02017-07-27 11:45:11 +02005780/* config parser for global "tune.h2.initial-window-size" */
5781static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5782 struct proxy *defpx, const char *file, int line,
5783 char **err)
5784{
5785 if (too_many_args(1, args, err, NULL))
5786 return -1;
5787
5788 h2_settings_initial_window_size = atoi(args[1]);
5789 if (h2_settings_initial_window_size < 0) {
5790 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5791 return -1;
5792 }
5793 return 0;
5794}
5795
Willy Tarreau5242ef82017-07-27 11:47:28 +02005796/* config parser for global "tune.h2.max-concurrent-streams" */
5797static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5798 struct proxy *defpx, const char *file, int line,
5799 char **err)
5800{
5801 if (too_many_args(1, args, err, NULL))
5802 return -1;
5803
5804 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005805 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005806 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5807 return -1;
5808 }
5809 return 0;
5810}
5811
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005812/* config parser for global "tune.h2.max-frame-size" */
5813static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5814 struct proxy *defpx, const char *file, int line,
5815 char **err)
5816{
5817 if (too_many_args(1, args, err, NULL))
5818 return -1;
5819
5820 h2_settings_max_frame_size = atoi(args[1]);
5821 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5822 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5823 return -1;
5824 }
5825 return 0;
5826}
5827
Willy Tarreau62f52692017-10-08 23:01:42 +02005828
5829/****************************************/
5830/* MUX initialization and instanciation */
5831/***************************************/
5832
5833/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005834static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005835 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005836 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005837 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005838 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005839 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005840 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005841 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005842 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005843 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005844 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005845 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005846 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005847 .shutr = h2_shutr,
5848 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005849 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005850 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005851 .name = "H2",
5852};
5853
Christopher Faulet32f61c02018-04-10 14:33:41 +02005854/* PROTO selection : this mux registers PROTO token "h2" */
5855static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005856 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005857
Willy Tarreau0108d902018-11-25 19:14:37 +01005858INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5859
Christopher Faulet9b579102019-04-11 22:52:25 +02005860
5861/* The mux operations */
5862static const struct mux_ops h2_htx_ops = {
5863 .init = h2_init,
5864 .wake = h2_wake,
5865 .snd_buf = h2_snd_buf,
5866 .rcv_buf = h2_rcv_buf,
5867 .subscribe = h2_subscribe,
5868 .unsubscribe = h2_unsubscribe,
5869 .attach = h2_attach,
5870 .get_first_cs = h2_get_first_cs,
5871 .detach = h2_detach,
5872 .destroy = h2_destroy,
5873 .avail_streams = h2_avail_streams,
5874 .used_streams = h2_used_streams,
5875 .shutr = h2_shutr,
5876 .shutw = h2_shutw,
5877 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005878 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005879 .name = "H2",
5880};
5881
Willy Tarreauf8957272018-10-03 10:25:20 +02005882static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005883 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005884
5885INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5886
Willy Tarreau62f52692017-10-08 23:01:42 +02005887/* config keyword parsers */
5888static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005889 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005890 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005891 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005892 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005893 { 0, NULL, NULL }
5894}};
5895
Willy Tarreau0108d902018-11-25 19:14:37 +01005896INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);