blob: 5bb851819d29bebcf499939e672e494774ebe687 [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_* */
211 int mws; /* mux window size for this stream */
212 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 Tarreau5b5e6872017-09-25 16:17:25 +0200710/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100711static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200712{
713 if (h2c->msi < 0)
714 return 0;
715
716 if (h2c->msi == h2s_id(h2s))
717 return 0;
718
719 return 1;
720}
721
Willy Tarreau741d6df2017-10-17 08:00:59 +0200722/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100723static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200724{
725 h2c->errcode = err;
726 h2c->st0 = H2_CS_ERROR;
727}
728
Willy Tarreau175cebb2019-01-24 10:02:24 +0100729/* marks an error on the stream. It may also update an already closed stream
730 * (e.g. to report an error after an RST was received).
731 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100732static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200733{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100734 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200735 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100736 if (h2s->st < H2_SS_ERROR)
737 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100738 if (h2s->cs)
739 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200740 }
741}
742
Willy Tarreau7e094452018-12-19 18:08:52 +0100743/* attempt to notify the data layer of recv availability */
744static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
745{
746 struct wait_event *sw;
747
748 if (h2s->recv_wait) {
749 sw = h2s->recv_wait;
750 sw->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200751 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100752 h2s->recv_wait = NULL;
753 }
754}
755
756/* attempt to notify the data layer of send availability */
757static void __maybe_unused h2s_notify_send(struct h2s *h2s)
758{
759 struct wait_event *sw;
760
Willy Tarreauc234ae32019-05-13 17:56:11 +0200761 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100762 sw = h2s->send_wait;
763 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100764 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200765 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100766 }
767}
768
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100769/* alerts the data layer, trying to wake it up by all means, following
770 * this sequence :
771 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
772 * - if its subscribed to send, then it's woken up for send
773 * - if it was subscribed to neither, its ->wake() callback is called
774 * It is safe to call this function with a closed stream which doesn't have a
775 * conn_stream anymore.
776 */
777static void __maybe_unused h2s_alert(struct h2s *h2s)
778{
779 if (h2s->recv_wait || h2s->send_wait) {
780 h2s_notify_recv(h2s);
781 h2s_notify_send(h2s);
782 }
783 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
784 h2s->cs->data_cb->wake(h2s->cs);
785}
786
Willy Tarreaue4820742017-07-27 13:37:23 +0200787/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100788static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200789{
790 uint8_t *out = frame;
791
792 *out = len >> 16;
793 write_n16(out + 1, len);
794}
795
Willy Tarreau54c15062017-10-10 17:10:03 +0200796/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
797 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
798 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200799 * available in the buffer's input prior to calling this function. The buffer
800 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200801 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100802static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200803 const struct buffer *b, int o)
804{
Willy Tarreau591d4452018-06-15 17:21:00 +0200805 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 +0200806}
807
Willy Tarreau1f094672017-11-20 21:27:45 +0100808static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200809{
Willy Tarreau591d4452018-06-15 17:21:00 +0200810 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200811}
812
Willy Tarreau1f094672017-11-20 21:27:45 +0100813static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200814{
Willy Tarreau591d4452018-06-15 17:21:00 +0200815 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200816}
817
Willy Tarreau1f094672017-11-20 21:27:45 +0100818static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200819{
Willy Tarreau591d4452018-06-15 17:21:00 +0200820 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200821}
822
823
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100824/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
825 * The algorithm is not obvious. It turns out that H2 headers are neither
826 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
827 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200828 *
829 * b0 b1 b2 b3 b4 b5..b8
830 * +----------+---------+--------+----+----+----------------------+
831 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
832 * +----------+---------+--------+----+----+----------------------+
833 *
834 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
835 * we get the sid properly aligned and ordered, and 16 bits of len properly
836 * ordered as well. The type and flags can be extracted using bit shifts from
837 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200838 * Returns zero if some bytes are missing, otherwise non-zero on success. The
839 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200840 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100841static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200842{
843 uint64_t w;
844
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100845 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200846 return 0;
847
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100848 w = h2_get_n64(b, o + 1);
849 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200850 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
851 h->ff = w >> 32;
852 h->ft = w >> 40;
853 h->len += w >> 48;
854 return 1;
855}
856
857/* skip the next 9 bytes corresponding to the frame header possibly parsed by
858 * h2_peek_frame_hdr() above.
859 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100860static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200861{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200862 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200863}
864
865/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100866static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200867{
868 int ret;
869
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100870 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200871 if (ret > 0)
872 h2_skip_frame_hdr(b);
873 return ret;
874}
875
Willy Tarreau00dd0782018-03-01 16:31:34 +0100876/* marks stream <h2s> as CLOSED and decrement the number of active streams for
877 * its connection if the stream was not yet closed. Please use this exclusively
878 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100879 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100880static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100881{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100882 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100883 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100884 if (!h2s->id)
885 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100886 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +0100887 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
888 h2s_notify_recv(h2s);
889 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100890 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100891 h2s->st = H2_SS_CLOSED;
892}
893
Willy Tarreau71049cc2018-03-28 13:56:39 +0200894/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
895static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100896{
897 h2s_close(h2s);
898 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200899 if (b_size(&h2s->rxbuf)) {
900 b_free(&h2s->rxbuf);
901 offer_buffers(NULL, tasks_run_queue);
902 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200903 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100904 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200905 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100906 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800907 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200908 * reference left would be in the h2c send_list/fctl_list, and if
909 * we're in it, we're getting out anyway
910 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100911 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200912 if (LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau86eded62019-06-14 14:47:49 +0200913 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +0200914 LIST_DEL_INIT(&h2s->sending_list);
915 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200916 tasklet_free(h2s->wait_event.tasklet);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100917 pool_free(pool_head_h2s, h2s);
918}
919
Willy Tarreaua8e49542018-10-03 18:53:55 +0200920/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
921 * stream tree. In case of error, nothing is added and NULL is returned. The
922 * causes of errors can be any failed memory allocation. The caller is
923 * responsible for checking if the connection may support an extra stream
924 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200925 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200926static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200927{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200928 struct h2s *h2s;
929
Willy Tarreaubafbe012017-11-24 17:34:44 +0100930 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200931 if (!h2s)
932 goto out;
933
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200934 h2s->wait_event.tasklet = tasklet_new();
935 if (!h2s->wait_event.tasklet) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200936 pool_free(pool_head_h2s, h2s);
937 goto out;
938 }
939 h2s->send_wait = NULL;
940 h2s->recv_wait = NULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200941 h2s->wait_event.tasklet->process = h2_deferred_shut;
942 h2s->wait_event.tasklet->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100943 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200944 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100945 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200946 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200947 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200948 h2s->mws = h2c->miw;
949 h2s->flags = H2_SF_NONE;
950 h2s->errcode = H2_ERR_NO_ERROR;
951 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200952 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100953 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200954 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200955
956 if (h2c->flags & H2_CF_IS_BACK) {
957 h1m_init_req(&h2s->h1m);
958 h2s->h1m.err_pos = -1; // don't care about errors on the request path
959 h2s->h1m.flags |= H1_MF_TOLOWER;
960 } else {
961 h1m_init_res(&h2s->h1m);
962 h2s->h1m.err_pos = -1; // don't care about errors on the response path
963 h2s->h1m.flags |= H1_MF_TOLOWER;
964 }
965
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200966 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200967 if (id > 0)
968 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100969 else
970 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200971
972 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100973 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100974 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200975
976 return h2s;
977
978 out_free_h2s:
979 pool_free(pool_head_h2s, h2s);
980 out:
981 return NULL;
982}
983
984/* creates a new stream <id> on the h2c connection and returns it, or NULL in
985 * case of memory allocation error.
986 */
987static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
988{
989 struct session *sess = h2c->conn->owner;
990 struct conn_stream *cs;
991 struct h2s *h2s;
992
993 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
994 goto out;
995
996 h2s = h2s_new(h2c, id);
997 if (!h2s)
998 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200999
1000 cs = cs_new(h2c->conn);
1001 if (!cs)
1002 goto out_close;
1003
Olivier Houchard746fb772018-12-15 19:42:00 +01001004 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001005 h2s->cs = cs;
1006 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001007 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001008
1009 if (stream_create_from_cs(cs) < 0)
1010 goto out_free_cs;
1011
Willy Tarreau590a0512018-09-05 11:56:48 +02001012 /* We want the accept date presented to the next stream to be the one
1013 * we have now, the handshake time to be null (since the next stream
1014 * is not delayed by a handshake), and the idle time to count since
1015 * right now.
1016 */
1017 sess->accept_date = date;
1018 sess->tv_accept = now;
1019 sess->t_handshake = 0;
1020
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001021 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001022 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001023 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001024 return h2s;
1025
1026 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001027 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001028 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001029 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001030 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001031 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001032 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001033 sess_log(sess);
1034 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001035}
1036
Willy Tarreau751f2d02018-10-05 09:35:00 +02001037/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1038 * and returns it, or NULL in case of memory allocation error or if the highest
1039 * possible stream ID was reached.
1040 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001041static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001042{
1043 struct h2s *h2s = NULL;
1044
Willy Tarreau86949782019-01-31 10:42:05 +01001045 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001046 goto out;
1047
Willy Tarreaua80dca82019-01-24 17:08:28 +01001048 if (h2_streams_left(h2c) < 1)
1049 goto out;
1050
Willy Tarreau751f2d02018-10-05 09:35:00 +02001051 /* Defer choosing the ID until we send the first message to create the stream */
1052 h2s = h2s_new(h2c, 0);
1053 if (!h2s)
1054 goto out;
1055
1056 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001057 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001058 cs->ctx = h2s;
1059 h2c->nb_cs++;
1060
Willy Tarreau751f2d02018-10-05 09:35:00 +02001061 out:
1062 return h2s;
1063}
1064
Willy Tarreaube5b7152017-09-25 16:25:39 +02001065/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1066 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1067 * the various settings codes.
1068 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001069static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001070{
1071 struct buffer *res;
1072 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001073 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001074 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001075 int ret;
1076
1077 if (h2c_mux_busy(h2c, NULL)) {
1078 h2c->flags |= H2_CF_DEM_MBUSY;
1079 return 0;
1080 }
1081
Willy Tarreaube5b7152017-09-25 16:25:39 +02001082 chunk_init(&buf, buf_data, sizeof(buf_data));
1083 chunk_memcpy(&buf,
1084 "\x00\x00\x00" /* length : 0 for now */
1085 "\x04\x00" /* type : 4 (settings), flags : 0 */
1086 "\x00\x00\x00\x00", /* stream ID : 0 */
1087 9);
1088
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001089 if (h2c->flags & H2_CF_IS_BACK) {
1090 /* send settings_enable_push=0 */
1091 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1092 }
1093
Willy Tarreaube5b7152017-09-25 16:25:39 +02001094 if (h2_settings_header_table_size != 4096) {
1095 char str[6] = "\x00\x01"; /* header_table_size */
1096
1097 write_n32(str + 2, h2_settings_header_table_size);
1098 chunk_memcat(&buf, str, 6);
1099 }
1100
1101 if (h2_settings_initial_window_size != 65535) {
1102 char str[6] = "\x00\x04"; /* initial_window_size */
1103
1104 write_n32(str + 2, h2_settings_initial_window_size);
1105 chunk_memcat(&buf, str, 6);
1106 }
1107
1108 if (h2_settings_max_concurrent_streams != 0) {
1109 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1110
1111 /* Note: 0 means "unlimited" for haproxy's config but not for
1112 * the protocol, so never send this value!
1113 */
1114 write_n32(str + 2, h2_settings_max_concurrent_streams);
1115 chunk_memcat(&buf, str, 6);
1116 }
1117
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001118 mfs = h2_settings_max_frame_size;
1119 if (mfs > global.tune.bufsize)
1120 mfs = global.tune.bufsize;
1121
1122 if (!mfs)
1123 mfs = global.tune.bufsize;
1124
1125 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001126 char str[6] = "\x00\x05"; /* max_frame_size */
1127
1128 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1129 * match bufsize - rewrite size, but at the moment it seems
1130 * that clients don't take care of it.
1131 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001132 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001133 chunk_memcat(&buf, str, 6);
1134 }
1135
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001136 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001137
1138 res = br_tail(h2c->mbuf);
1139 retry:
1140 if (!h2_get_buf(h2c, res)) {
1141 h2c->flags |= H2_CF_MUX_MALLOC;
1142 h2c->flags |= H2_CF_DEM_MROOM;
1143 return 0;
1144 }
1145
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001146 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001147 if (unlikely(ret <= 0)) {
1148 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001149 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1150 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001151 h2c->flags |= H2_CF_MUX_MFULL;
1152 h2c->flags |= H2_CF_DEM_MROOM;
1153 return 0;
1154 }
1155 else {
1156 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1157 return 0;
1158 }
1159 }
1160 return ret;
1161}
1162
Willy Tarreau52eed752017-09-22 15:05:09 +02001163/* Try to receive a connection preface, then upon success try to send our
1164 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1165 * missing data. It may return an error in h2c.
1166 */
1167static int h2c_frt_recv_preface(struct h2c *h2c)
1168{
1169 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001170 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001171
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001172 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001173
1174 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001175 if (ret1 < 0)
1176 sess_log(h2c->conn->owner);
1177
Willy Tarreau52eed752017-09-22 15:05:09 +02001178 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1179 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1180 return 0;
1181 }
1182
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001183 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001184 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001185 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001186
Willy Tarreaube5b7152017-09-25 16:25:39 +02001187 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001188}
1189
Willy Tarreau01b44822018-10-03 14:26:37 +02001190/* Try to send a connection preface, then upon success try to send our
1191 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1192 * missing data. It may return an error in h2c.
1193 */
1194static int h2c_bck_send_preface(struct h2c *h2c)
1195{
1196 struct buffer *res;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001197 int ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001198
1199 if (h2c_mux_busy(h2c, NULL)) {
1200 h2c->flags |= H2_CF_DEM_MBUSY;
1201 return 0;
1202 }
1203
Willy Tarreaubcc45952019-05-26 10:05:50 +02001204 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001205 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001206 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001207 h2c->flags |= H2_CF_MUX_MALLOC;
1208 h2c->flags |= H2_CF_DEM_MROOM;
1209 return 0;
1210 }
1211
1212 if (!b_data(res)) {
1213 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001214 ret = b_istput(res, ist(H2_CONN_PREFACE));
1215 if (unlikely(ret <= 0)) {
1216 if (!ret) {
1217 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1218 goto retry;
1219 h2c->flags |= H2_CF_MUX_MFULL;
1220 h2c->flags |= H2_CF_DEM_MROOM;
1221 return 0;
1222 }
1223 else {
1224 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1225 return 0;
1226 }
1227 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001228 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001229 return h2c_send_settings(h2c);
1230}
1231
Willy Tarreau081d4722017-05-16 21:51:05 +02001232/* try to send a GOAWAY frame on the connection to report an error or a graceful
1233 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1234 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1235 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1236 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1237 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1238 * on unrecoverable failure. It will not attempt to send one again in this last
1239 * case so that it is safe to use h2c_error() to report such errors.
1240 */
1241static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1242{
1243 struct buffer *res;
1244 char str[17];
1245 int ret;
1246
1247 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1248 return 1; // claim that it worked
1249
1250 if (h2c_mux_busy(h2c, h2s)) {
1251 if (h2s)
1252 h2s->flags |= H2_SF_BLK_MBUSY;
1253 else
1254 h2c->flags |= H2_CF_DEM_MBUSY;
1255 return 0;
1256 }
1257
Willy Tarreau9c218e72019-05-26 10:08:28 +02001258 /* len: 8, type: 7, flags: none, sid: 0 */
1259 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1260
1261 if (h2c->last_sid < 0)
1262 h2c->last_sid = h2c->max_id;
1263
1264 write_n32(str + 9, h2c->last_sid);
1265 write_n32(str + 13, h2c->errcode);
1266
Willy Tarreaubcc45952019-05-26 10:05:50 +02001267 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001268 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001269 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001270 h2c->flags |= H2_CF_MUX_MALLOC;
1271 if (h2s)
1272 h2s->flags |= H2_SF_BLK_MROOM;
1273 else
1274 h2c->flags |= H2_CF_DEM_MROOM;
1275 return 0;
1276 }
1277
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001278 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001279 if (unlikely(ret <= 0)) {
1280 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001281 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1282 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001283 h2c->flags |= H2_CF_MUX_MFULL;
1284 if (h2s)
1285 h2s->flags |= H2_SF_BLK_MROOM;
1286 else
1287 h2c->flags |= H2_CF_DEM_MROOM;
1288 return 0;
1289 }
1290 else {
1291 /* we cannot report this error using GOAWAY, so we mark
1292 * it and claim a success.
1293 */
1294 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1295 h2c->flags |= H2_CF_GOAWAY_FAILED;
1296 return 1;
1297 }
1298 }
1299 h2c->flags |= H2_CF_GOAWAY_SENT;
1300 return ret;
1301}
1302
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001303/* Try to send an RST_STREAM frame on the connection for the indicated stream
1304 * during mux operations. This stream must be valid and cannot be closed
1305 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1306 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1307 * not yet.
1308 *
1309 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1310 * to write the message, it subscribes the stream to future notifications.
1311 */
1312static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1313{
1314 struct buffer *res;
1315 char str[13];
1316 int ret;
1317
1318 if (!h2s || h2s->st == H2_SS_CLOSED)
1319 return 1;
1320
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001321 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1322 * RST_STREAM in response to a RST_STREAM frame.
1323 */
1324 if (h2c->dft == H2_FT_RST_STREAM) {
1325 ret = 1;
1326 goto ignore;
1327 }
1328
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001329 if (h2c_mux_busy(h2c, h2s)) {
1330 h2s->flags |= H2_SF_BLK_MBUSY;
1331 return 0;
1332 }
1333
Willy Tarreau9c218e72019-05-26 10:08:28 +02001334 /* len: 4, type: 3, flags: none */
1335 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1336 write_n32(str + 5, h2s->id);
1337 write_n32(str + 9, h2s->errcode);
1338
Willy Tarreaubcc45952019-05-26 10:05:50 +02001339 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001340 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001341 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001342 h2c->flags |= H2_CF_MUX_MALLOC;
1343 h2s->flags |= H2_SF_BLK_MROOM;
1344 return 0;
1345 }
1346
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001347 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001348 if (unlikely(ret <= 0)) {
1349 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001350 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1351 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001352 h2c->flags |= H2_CF_MUX_MFULL;
1353 h2s->flags |= H2_SF_BLK_MROOM;
1354 return 0;
1355 }
1356 else {
1357 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1358 return 0;
1359 }
1360 }
1361
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001362 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001363 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001364 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001365 return ret;
1366}
1367
1368/* Try to send an RST_STREAM frame on the connection for the stream being
1369 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001370 * error code, even if the stream is one of the dummy ones, and will update
1371 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001372 *
1373 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1374 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001375 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001376 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001377 */
1378static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1379{
1380 struct buffer *res;
1381 char str[13];
1382 int ret;
1383
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001384 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1385 * RST_STREAM in response to a RST_STREAM frame.
1386 */
1387 if (h2c->dft == H2_FT_RST_STREAM) {
1388 ret = 1;
1389 goto ignore;
1390 }
1391
Willy Tarreau27a84c92017-10-17 08:10:17 +02001392 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001393 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001394 return 0;
1395 }
1396
Willy Tarreau9c218e72019-05-26 10:08:28 +02001397 /* len: 4, type: 3, flags: none */
1398 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1399
1400 write_n32(str + 5, h2c->dsi);
1401 write_n32(str + 9, h2s->errcode);
1402
Willy Tarreaubcc45952019-05-26 10:05:50 +02001403 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001404 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001405 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001406 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001407 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001408 return 0;
1409 }
1410
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001411 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001412 if (unlikely(ret <= 0)) {
1413 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001414 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1415 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001416 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001417 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001418 return 0;
1419 }
1420 else {
1421 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1422 return 0;
1423 }
1424 }
1425
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001426 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001427 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001428 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001429 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001430 }
1431
Willy Tarreau27a84c92017-10-17 08:10:17 +02001432 return ret;
1433}
1434
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001435/* try to send an empty DATA frame with the ES flag set to notify about the
1436 * end of stream and match a shutdown(write). If an ES was already sent as
1437 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1438 * on success or zero if nothing was done. In case of lack of room to write the
1439 * message, it subscribes the requesting stream to future notifications.
1440 */
1441static int h2_send_empty_data_es(struct h2s *h2s)
1442{
1443 struct h2c *h2c = h2s->h2c;
1444 struct buffer *res;
1445 char str[9];
1446 int ret;
1447
Willy Tarreau721c9742017-11-07 11:05:42 +01001448 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001449 return 1;
1450
1451 if (h2c_mux_busy(h2c, h2s)) {
1452 h2s->flags |= H2_SF_BLK_MBUSY;
1453 return 0;
1454 }
1455
Willy Tarreau9c218e72019-05-26 10:08:28 +02001456 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1457 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1458 write_n32(str + 5, h2s->id);
1459
Willy Tarreaubcc45952019-05-26 10:05:50 +02001460 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001461 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001462 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001463 h2c->flags |= H2_CF_MUX_MALLOC;
1464 h2s->flags |= H2_SF_BLK_MROOM;
1465 return 0;
1466 }
1467
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001468 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001469 if (likely(ret > 0)) {
1470 h2s->flags |= H2_SF_ES_SENT;
1471 }
1472 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001473 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1474 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001475 h2c->flags |= H2_CF_MUX_MFULL;
1476 h2s->flags |= H2_SF_BLK_MROOM;
1477 return 0;
1478 }
1479 else {
1480 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1481 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001482 }
1483 return ret;
1484}
1485
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001486/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001487 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001488 * is automatically updated accordingly. If the stream is orphaned, it is
1489 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001490 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001491static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001492{
1493 if (!h2s->cs) {
1494 /* this stream was already orphaned */
1495 h2s_destroy(h2s);
1496 return;
1497 }
1498
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001499 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001500 if (h2s->st == H2_SS_OPEN)
1501 h2s->st = H2_SS_HREM;
1502 else if (h2s->st == H2_SS_HLOC)
1503 h2s_close(h2s);
1504 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001505
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001506 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1507 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1508 h2s->cs->flags |= CS_FL_ERR_PENDING;
1509 if (h2s->cs->flags & CS_FL_EOS)
1510 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001511
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001512 if (h2s->st < H2_SS_ERROR)
1513 h2s->st = H2_SS_ERROR;
1514 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001515
1516 h2s_alert(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001517}
1518
1519/* wake the streams attached to the connection, whose id is greater than <last>
1520 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001521 */
Willy Tarreau23482912019-05-07 15:23:14 +02001522static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001523{
1524 struct eb32_node *node;
1525 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001526
Christopher Fauletf02ca002019-03-07 16:21:34 +01001527 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001528 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1529 while (node) {
1530 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001531 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001532 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001533 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001534
Christopher Fauletf02ca002019-03-07 16:21:34 +01001535 /* Wake all streams with unassigned ID (ID == 0) */
1536 node = eb32_lookup(&h2c->streams_by_id, 0);
1537 while (node) {
1538 h2s = container_of(node, struct h2s, by_id);
1539 if (h2s->id > 0)
1540 break;
1541 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001542 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001543 }
1544}
1545
Willy Tarreau3421aba2017-07-27 15:41:03 +02001546/* Increase all streams' outgoing window size by the difference passed in
1547 * argument. This is needed upon receipt of the settings frame if the initial
1548 * window size is different. The difference may be negative and the resulting
1549 * window size as well, for the time it takes to receive some window updates.
1550 */
1551static void h2c_update_all_ws(struct h2c *h2c, int diff)
1552{
1553 struct h2s *h2s;
1554 struct eb32_node *node;
1555
1556 if (!diff)
1557 return;
1558
1559 node = eb32_first(&h2c->streams_by_id);
1560 while (node) {
1561 h2s = container_of(node, struct h2s, by_id);
1562 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001563
1564 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1565 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001566 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001567 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001568 }
1569
Willy Tarreau3421aba2017-07-27 15:41:03 +02001570 node = eb32_next(node);
1571 }
1572}
1573
1574/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1575 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001576 * return an error in h2c. The caller must have already verified frame length
1577 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001578 */
1579static int h2c_handle_settings(struct h2c *h2c)
1580{
1581 unsigned int offset;
1582 int error;
1583
1584 if (h2c->dff & H2_F_SETTINGS_ACK) {
1585 if (h2c->dfl) {
1586 error = H2_ERR_FRAME_SIZE_ERROR;
1587 goto fail;
1588 }
1589 return 1;
1590 }
1591
Willy Tarreau3421aba2017-07-27 15:41:03 +02001592 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001593 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001594 return 0;
1595
1596 /* parse the frame */
1597 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001598 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1599 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001600
1601 switch (type) {
1602 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1603 /* we need to update all existing streams with the
1604 * difference from the previous iws.
1605 */
1606 if (arg < 0) { // RFC7540#6.5.2
1607 error = H2_ERR_FLOW_CONTROL_ERROR;
1608 goto fail;
1609 }
1610 h2c_update_all_ws(h2c, arg - h2c->miw);
1611 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
1872 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1873 error = H2_ERR_FLOW_CONTROL_ERROR;
1874 goto strm_err;
1875 }
1876
1877 h2s->mws += inc;
1878 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1879 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) {
2193 error = H2_ERR_STREAM_CLOSED;
2194 goto strm_err;
2195 }
2196
Willy Tarreau8f650c32017-11-21 19:36:21 +01002197 if (h2c->st0 >= H2_CS_ERROR)
2198 return 0;
2199
Willy Tarreau721c9742017-11-07 11:05:42 +01002200 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002201 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002202 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002203 }
2204
2205 /* check for completion : the callee will change this to FRAME_A or
2206 * FRAME_H once done.
2207 */
2208 if (h2c->st0 == H2_CS_FRAME_P)
2209 return 0;
2210
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002211 /* last frame */
2212 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002213 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002214 if (h2s->st == H2_SS_OPEN)
2215 h2s->st = H2_SS_HREM;
2216 else
2217 h2s_close(h2s);
2218
Willy Tarreau1915ca22019-01-24 11:49:37 +01002219 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2220 /* RFC7540#8.1.2 */
2221 error = H2_ERR_PROTOCOL_ERROR;
2222 goto strm_err;
2223 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002224 }
2225
Willy Tarreau454f9052017-10-26 19:40:35 +02002226 return 1;
2227
Willy Tarreau454f9052017-10-26 19:40:35 +02002228 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002229 h2s_error(h2s, error);
2230 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002231 return 0;
2232}
2233
Willy Tarreaubc933932017-10-09 16:21:43 +02002234/* process Rx frames to be demultiplexed */
2235static void h2_process_demux(struct h2c *h2c)
2236{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002237 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002238 struct h2_fh hdr;
2239 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002240
Willy Tarreau081d4722017-05-16 21:51:05 +02002241 if (h2c->st0 >= H2_CS_ERROR)
2242 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002243
2244 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2245 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002246 if (h2c->flags & H2_CF_IS_BACK)
2247 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002248 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2249 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002250 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002251 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002252 sess_log(h2c->conn->owner);
2253 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002254 goto fail;
2255 }
2256
2257 h2c->max_id = 0;
2258 h2c->st0 = H2_CS_SETTINGS1;
2259 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002260
2261 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002262 /* ensure that what is pending is a valid SETTINGS frame
2263 * without an ACK.
2264 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002265 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002266 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002267 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002268 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002269 sess_log(h2c->conn->owner);
2270 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002271 goto fail;
2272 }
2273
2274 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2275 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2276 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2277 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002278 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002279 goto fail;
2280 }
2281
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002282 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002283 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2284 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2285 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002286 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002287 goto fail;
2288 }
2289
Willy Tarreau3bf69182018-12-21 15:34:50 +01002290 /* that's OK, switch to FRAME_P to process it. This is
2291 * a SETTINGS frame whose header has already been
2292 * deleted above.
2293 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002294 padlen = 0;
2295 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002296 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002297 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002298
2299 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002300 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002301 int ret = 0;
2302
2303 if (h2c->st0 >= H2_CS_ERROR)
2304 break;
2305
2306 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002307 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002308 break;
2309
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002310 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002311 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002312 if (!h2c->nb_streams) {
2313 /* only log if no other stream can report the error */
2314 sess_log(h2c->conn->owner);
2315 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002316 break;
2317 }
2318
Christopher Faulet3d574a52019-06-18 12:22:38 +02002319 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002320 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2321 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2322 * we read the pad length and drop it from the remaining
2323 * payload (one byte + the 9 remaining ones = 10 total
2324 * removed), so we have a frame payload starting after the
2325 * pad len. Flow controlled frames (DATA) also count the
2326 * padlen in the flow control, so it must be adjusted.
2327 */
2328 if (hdr.len < 1) {
2329 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2330 sess_log(h2c->conn->owner);
2331 goto fail;
2332 }
2333 hdr.len--;
2334
2335 if (b_data(&h2c->dbuf) < 10)
2336 break; // missing padlen
2337
2338 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2339
2340 if (padlen > hdr.len) {
2341 /* RFC7540#6.1 : pad length = length of
2342 * frame payload or greater => error.
2343 */
2344 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2345 sess_log(h2c->conn->owner);
2346 goto fail;
2347 }
2348
2349 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2350 h2c->rcvd_c++;
2351 h2c->rcvd_s++;
2352 }
2353 b_del(&h2c->dbuf, 1);
2354 }
2355 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002356
2357 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002358 h2c->dfl = hdr.len;
2359 h2c->dsi = hdr.sid;
2360 h2c->dft = hdr.ft;
2361 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002362 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002363 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002364
2365 /* check for minimum basic frame format validity */
2366 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2367 if (ret != H2_ERR_NO_ERROR) {
2368 h2c_error(h2c, ret);
2369 sess_log(h2c->conn->owner);
2370 goto fail;
2371 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002372 }
2373
2374 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002375 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2376
Willy Tarreau567beb82018-12-18 16:52:44 +01002377 if (tmp_h2s != h2s && h2s && h2s->cs &&
2378 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002379 conn_xprt_read0_pending(h2c->conn) ||
2380 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002381 (h2s->flags & H2_SF_ES_RCVD) ||
2382 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002383 /* we may have to signal the upper layers */
2384 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002385 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002386 }
2387 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002388
Willy Tarreaud7901432017-12-29 11:34:40 +01002389 if (h2c->st0 == H2_CS_FRAME_E)
2390 goto strm_err;
2391
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002392 if (h2s->st == H2_SS_IDLE &&
2393 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2394 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2395 * this state MUST be treated as a connection error
2396 */
2397 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002398 if (!h2c->nb_streams) {
2399 /* only log if no other stream can report the error */
2400 sess_log(h2c->conn->owner);
2401 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002402 break;
2403 }
2404
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002405 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2406 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2407 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002408 * this state MUST be treated as a stream error.
2409 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2410 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002411 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002412 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2413 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2414 else
2415 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002416 goto strm_err;
2417 }
2418
Willy Tarreauab837502017-12-27 15:07:30 +01002419 /* Below the management of frames received in closed state is a
2420 * bit hackish because the spec makes strong differences between
2421 * streams closed by receiving RST, sending RST, and seeing ES
2422 * in both directions. In addition to this, the creation of a
2423 * new stream reusing the identifier of a closed one will be
2424 * detected here. Given that we cannot keep track of all closed
2425 * streams forever, we consider that unknown closed streams were
2426 * closed on RST received, which allows us to respond with an
2427 * RST without breaking the connection (eg: to abort a transfer).
2428 * Some frames have to be silently ignored as well.
2429 */
2430 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002431 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002432 /* #5.1.1: The identifier of a newly
2433 * established stream MUST be numerically
2434 * greater than all streams that the initiating
2435 * endpoint has opened or reserved. This
2436 * governs streams that are opened using a
2437 * HEADERS frame and streams that are reserved
2438 * using PUSH_PROMISE. An endpoint that
2439 * receives an unexpected stream identifier
2440 * MUST respond with a connection error.
2441 */
2442 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2443 goto strm_err;
2444 }
2445
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002446 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002447 /* RFC7540#5.1:closed: an endpoint that
2448 * receives any frame other than PRIORITY after
2449 * receiving a RST_STREAM MUST treat that as a
2450 * stream error of type STREAM_CLOSED.
2451 *
2452 * Note that old streams fall into this category
2453 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002454 *
2455 * However, we cannot generalize this to all frame types. Those
2456 * carrying compression state must still be processed before
2457 * being dropped or we'll desynchronize the decoder. This can
2458 * happen with request trailers received after sending an
2459 * RST_STREAM, or with header/trailers responses received after
2460 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002461 */
2462 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2463 h2c->st0 = H2_CS_FRAME_E;
2464 goto strm_err;
2465 }
2466
2467 /* RFC7540#5.1:closed: if this state is reached as a
2468 * result of sending a RST_STREAM frame, the peer that
2469 * receives the RST_STREAM might have already sent
2470 * frames on the stream that cannot be withdrawn. An
2471 * endpoint MUST ignore frames that it receives on
2472 * closed streams after it has sent a RST_STREAM
2473 * frame. An endpoint MAY choose to limit the period
2474 * over which it ignores frames and treat frames that
2475 * arrive after this time as being in error.
2476 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002477 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002478 /* RFC7540#5.1:closed: any frame other than
2479 * PRIO/WU/RST in this state MUST be treated as
2480 * a connection error
2481 */
2482 if (h2c->dft != H2_FT_RST_STREAM &&
2483 h2c->dft != H2_FT_PRIORITY &&
2484 h2c->dft != H2_FT_WINDOW_UPDATE) {
2485 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2486 goto strm_err;
2487 }
2488 }
2489 }
2490
Willy Tarreauc0da1962017-10-30 18:38:00 +01002491#if 0
2492 // problem below: it is not possible to completely ignore such
2493 // streams as we need to maintain the compression state as well
2494 // and for this we need to completely process these frames (eg:
2495 // HEADERS frames) as well as counting DATA frames to emit
2496 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2497 // This is a typical case of layer violation where the
2498 // transported contents are critical to the connection's
2499 // validity and must be ignored at the same time :-(
2500
2501 /* graceful shutdown, ignore streams whose ID is higher than
2502 * the one advertised in GOAWAY. RFC7540#6.8.
2503 */
2504 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002505 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2506 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002507 h2c->dfl -= ret;
2508 ret = h2c->dfl == 0;
2509 goto strm_err;
2510 }
2511#endif
2512
Willy Tarreau7e98c052017-10-10 15:56:59 +02002513 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002514 case H2_FT_SETTINGS:
2515 if (h2c->st0 == H2_CS_FRAME_P)
2516 ret = h2c_handle_settings(h2c);
2517
2518 if (h2c->st0 == H2_CS_FRAME_A)
2519 ret = h2c_ack_settings(h2c);
2520 break;
2521
Willy Tarreaucf68c782017-10-10 17:11:41 +02002522 case H2_FT_PING:
2523 if (h2c->st0 == H2_CS_FRAME_P)
2524 ret = h2c_handle_ping(h2c);
2525
2526 if (h2c->st0 == H2_CS_FRAME_A)
2527 ret = h2c_ack_ping(h2c);
2528 break;
2529
Willy Tarreau26f95952017-07-27 17:18:30 +02002530 case H2_FT_WINDOW_UPDATE:
2531 if (h2c->st0 == H2_CS_FRAME_P)
2532 ret = h2c_handle_window_update(h2c, h2s);
2533 break;
2534
Willy Tarreau61290ec2017-10-17 08:19:21 +02002535 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002536 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2537 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2538 * frames' parsers consume all following CONTINUATION
2539 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002540 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002541 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2542 sess_log(h2c->conn->owner);
2543 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002544
Willy Tarreau13278b42017-10-13 19:23:14 +02002545 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002546 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002547 if (h2c->flags & H2_CF_IS_BACK)
2548 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2549 else
2550 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002551 if (tmp_h2s) {
2552 h2s = tmp_h2s;
2553 ret = 1;
2554 }
2555 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002556 break;
2557
Willy Tarreau454f9052017-10-26 19:40:35 +02002558 case H2_FT_DATA:
2559 if (h2c->st0 == H2_CS_FRAME_P)
2560 ret = h2c_frt_handle_data(h2c, h2s);
2561
2562 if (h2c->st0 == H2_CS_FRAME_A)
2563 ret = h2c_send_strm_wu(h2c);
2564 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002565
Willy Tarreau92153fc2017-12-03 19:46:19 +01002566 case H2_FT_PRIORITY:
2567 if (h2c->st0 == H2_CS_FRAME_P)
2568 ret = h2c_handle_priority(h2c);
2569 break;
2570
Willy Tarreaucd234e92017-08-18 10:59:39 +02002571 case H2_FT_RST_STREAM:
2572 if (h2c->st0 == H2_CS_FRAME_P)
2573 ret = h2c_handle_rst_stream(h2c, h2s);
2574 break;
2575
Willy Tarreaue96b0922017-10-30 00:28:29 +01002576 case H2_FT_GOAWAY:
2577 if (h2c->st0 == H2_CS_FRAME_P)
2578 ret = h2c_handle_goaway(h2c);
2579 break;
2580
Willy Tarreau1c661982017-10-30 13:52:01 +01002581 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002582 default:
2583 /* drop frames that we ignore. They may be larger than
2584 * the buffer so we drain all of their contents until
2585 * we reach the end.
2586 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002587 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2588 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002589 h2c->dfl -= ret;
2590 ret = h2c->dfl == 0;
2591 }
2592
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002593 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002594 /* We may have to send an RST if not done yet */
2595 if (h2s->st == H2_SS_ERROR)
2596 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002597
Willy Tarreaua20a5192017-12-27 11:02:06 +01002598 if (h2c->st0 == H2_CS_FRAME_E)
2599 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002600
Willy Tarreau7e98c052017-10-10 15:56:59 +02002601 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002602 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002603 break;
2604
2605 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002606 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002607 h2c->st0 = H2_CS_FRAME_H;
2608 }
2609 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002610
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002611 if (h2c->rcvd_c > 0 &&
2612 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2613 h2c_send_conn_wu(h2c);
2614
Willy Tarreau52eed752017-09-22 15:05:09 +02002615 fail:
2616 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002617 if (h2s && h2s->cs &&
2618 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002619 conn_xprt_read0_pending(h2c->conn) ||
2620 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002621 (h2s->flags & H2_SF_ES_RCVD) ||
2622 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002623 /* we may have to signal the upper layers */
2624 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002625 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002626 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002627
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002628 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002629}
2630
2631/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2632 * the end.
2633 */
2634static int h2_process_mux(struct h2c *h2c)
2635{
Olivier Houchard998410a2019-04-15 19:23:37 +02002636 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002637
Willy Tarreau01b44822018-10-03 14:26:37 +02002638 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2639 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2640 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2641 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2642 if (h2c->st0 == H2_CS_ERROR) {
2643 h2c->st0 = H2_CS_ERROR2;
2644 sess_log(h2c->conn->owner);
2645 }
2646 goto fail;
2647 }
2648 h2c->st0 = H2_CS_SETTINGS1;
2649 }
2650 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002651 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002652 return 1;
2653 }
2654
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002655 /* start by sending possibly pending window updates */
2656 if (h2c->rcvd_c > 0 &&
2657 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2658 h2c_send_conn_wu(h2c) < 0)
2659 goto fail;
2660
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002661 /* First we always process the flow control list because the streams
2662 * waiting there were already elected for immediate emission but were
2663 * blocked just on this.
2664 */
2665
Olivier Houchard998410a2019-04-15 19:23:37 +02002666 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002667 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2668 h2c->st0 >= H2_CS_ERROR)
2669 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002670
Willy Tarreauc234ae32019-05-13 17:56:11 +02002671 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002672 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002673
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002674 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002675 /* For some reason, the upper layer failed to subsribe again,
2676 * so remove it from the send_list
2677 */
2678 if (!h2s->send_wait) {
2679 LIST_DEL_INIT(&h2s->list);
2680 continue;
2681 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002682 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002683 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002684 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002685 }
2686
Olivier Houchard998410a2019-04-15 19:23:37 +02002687 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002688 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2689 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002690
Willy Tarreauc234ae32019-05-13 17:56:11 +02002691 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002692 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002693
Olivier Houchard998410a2019-04-15 19:23:37 +02002694 /* For some reason, the upper layer failed to subsribe again,
2695 * so remove it from the send_list
2696 */
2697 if (!h2s->send_wait) {
2698 LIST_DEL_INIT(&h2s->list);
2699 continue;
2700 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002701 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002702 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002703 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002704 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002705 }
2706
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002707 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002708 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002709 if (h2c->st0 == H2_CS_ERROR) {
2710 if (h2c->max_id >= 0) {
2711 h2c_send_goaway_error(h2c, NULL);
2712 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2713 return 0;
2714 }
2715
2716 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2717 }
2718 return 1;
2719 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002720 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002721}
2722
Willy Tarreau62f52692017-10-08 23:01:42 +02002723
Willy Tarreau479998a2018-11-18 06:30:59 +01002724/* Attempt to read data, and subscribe if none available.
2725 * The function returns 1 if data has been received, otherwise zero.
2726 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002727static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002728{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002729 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002730 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002731 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002732 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002733
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002734 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002735 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002736
Willy Tarreau315d8072017-12-10 22:17:57 +01002737 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002738 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002739
Willy Tarreau44e973f2018-03-01 17:49:30 +01002740 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002741 if (!buf) {
2742 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002743 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002744 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002745
Olivier Houchard7505f942018-08-21 18:10:44 +02002746 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002747 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002748 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2749 /* HTX in use : try to pre-align the buffer like the
2750 * rxbufs will be to optimize memory copies. We'll make
2751 * sure that the frame header lands at the end of the
2752 * HTX block to alias it upon recv. We cannot use the
2753 * head because rcv_buf() will realign the buffer if
2754 * it's empty. Thus we cheat and pretend we already
2755 * have a few bytes there.
2756 */
2757 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002758 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002759 }
2760 else
2761 max = b_room(buf);
2762
Olivier Houchard7505f942018-08-21 18:10:44 +02002763 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002764 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002765 else
2766 ret = 0;
2767 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002768
Olivier Houchard53216e72018-10-10 15:46:36 +02002769 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002770 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002771
Olivier Houcharda1411e62018-08-17 18:42:48 +02002772 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002773 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002774 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002775 }
2776
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002777 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002778 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002779 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002780}
2781
Willy Tarreau479998a2018-11-18 06:30:59 +01002782/* Try to send data if possible.
2783 * The function returns 1 if data have been sent, otherwise zero.
2784 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002785static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002786{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002787 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002788 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002789 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002790
2791 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002792 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002793
Olivier Houchard7505f942018-08-21 18:10:44 +02002794
Willy Tarreaua2af5122017-10-09 11:56:46 +02002795 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2796 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002797 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002798 }
2799
Willy Tarreaubc933932017-10-09 16:21:43 +02002800 /* This loop is quite simple : it tries to fill as much as it can from
2801 * pending streams into the existing buffer until it's reportedly full
2802 * or the end of send requests is reached. Then it tries to send this
2803 * buffer's contents out, marks it not full if at least one byte could
2804 * be sent, and tries again.
2805 *
2806 * The snd_buf() function normally takes a "flags" argument which may
2807 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2808 * data immediately comes and CO_SFL_STREAMER to indicate that the
2809 * connection is streaming lots of data (used to increase TLS record
2810 * size at the expense of latency). The former can be sent any time
2811 * there's a buffer full flag, as it indicates at least one stream
2812 * attempted to send and failed so there are pending data. An
2813 * alternative would be to set it as long as there's an active stream
2814 * but that would be problematic for ACKs until we have an absolute
2815 * guarantee that all waiters have at least one byte to send. The
2816 * latter should possibly not be set for now.
2817 */
2818
2819 done = 0;
2820 while (!done) {
2821 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002822 unsigned int released = 0;
2823 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02002824
2825 /* fill as much as we can into the current buffer */
2826 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2827 done = h2_process_mux(h2c);
2828
Olivier Houchard2b094432019-01-29 18:28:36 +01002829 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002830 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01002831
Willy Tarreaubc933932017-10-09 16:21:43 +02002832 if (conn->flags & CO_FL_ERROR)
2833 break;
2834
2835 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2836 flags |= CO_SFL_MSG_MORE;
2837
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002838 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
2839 if (b_data(buf)) {
2840 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002841 if (!ret) {
2842 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002843 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002844 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002845 sent = 1;
2846 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002847 if (b_data(buf)) {
2848 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002849 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002850 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002851 }
2852 b_free(buf);
2853 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02002854 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002855
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002856 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02002857 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002858
Willy Tarreaubc933932017-10-09 16:21:43 +02002859 /* wrote at least one byte, the buffer is not full anymore */
2860 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2861 }
2862
Willy Tarreaua2af5122017-10-09 11:56:46 +02002863 if (conn->flags & CO_FL_SOCK_WR_SH) {
2864 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02002865 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002866 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002867 /* We're not full anymore, so we can wake any task that are waiting
2868 * for us.
2869 */
2870 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002871 struct h2s *h2s;
2872
2873 list_for_each_entry(h2s, &h2c->send_list, list) {
2874 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2875 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002876
Willy Tarreauc234ae32019-05-13 17:56:11 +02002877 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002878 continue;
2879
Olivier Houchard998410a2019-04-15 19:23:37 +02002880 /* For some reason, the upper layer failed to subsribe again,
2881 * so remove it from the send_list
2882 */
2883 if (!h2s->send_wait) {
2884 LIST_DEL_INIT(&h2s->list);
2885 continue;
2886 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002887 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002888 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002889 tasklet_wakeup(h2s->send_wait->tasklet);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002890 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002891 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002892 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002893 /* We're done, no more to send */
Willy Tarreau662fafc2019-05-26 09:43:07 +02002894 if (!br_data(h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002895 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002896schedule:
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002897 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002898 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002899
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002900 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002901}
2902
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002903/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002904static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2905{
2906 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002907 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002908
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002909 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002910 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002911 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002912 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002913 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002914 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002915 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002916}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002917
Willy Tarreau62f52692017-10-08 23:01:42 +02002918/* callback called on any event by the connection handler.
2919 * It applies changes and returns zero, or < 0 if it wants immediate
2920 * destruction of the connection (which normally doesn not happen in h2).
2921 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002922static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002923{
Olivier Houchard7505f942018-08-21 18:10:44 +02002924 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002925
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002926 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002927 h2_process_demux(h2c);
2928
2929 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002930 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002931
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002932 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002933 h2c->flags &= ~H2_CF_DEM_DFULL;
2934 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002935 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002936
Willy Tarreau0b37d652018-10-03 10:33:02 +02002937 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002938 /* frontend is stopping, reload likely in progress, let's try
2939 * to announce a graceful shutdown if not yet done. We don't
2940 * care if it fails, it will be tried again later.
2941 */
2942 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2943 if (h2c->last_sid < 0)
2944 h2c->last_sid = (1U << 31) - 1;
2945 h2c_send_goaway_error(h2c, NULL);
2946 }
2947 }
2948
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002949 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002950 * If we received early data, and the handshake is done, wake
2951 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002952 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002953 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2954 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2955 struct eb32_node *node;
2956 struct h2s *h2s;
2957
2958 h2c->flags |= H2_CF_WAIT_FOR_HS;
2959 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2960
2961 while (node) {
2962 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002963 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002964 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002965 node = eb32_next(node);
2966 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002967 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002968
Willy Tarreau26bd7612017-10-09 16:47:04 +02002969 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002970 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2971 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2972 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02002973 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002974
2975 if (eb_is_empty(&h2c->streams_by_id)) {
2976 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002977 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002978 return -1;
2979 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002980 }
2981
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002982 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002983 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002984
Olivier Houchard53216e72018-10-10 15:46:36 +02002985 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2986 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2987 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02002988 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02002989 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2990 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02002991 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002992
Willy Tarreau3f133572017-10-31 19:21:06 +01002993 if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02002994 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2995 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01002996 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002997
Olivier Houchard7505f942018-08-21 18:10:44 +02002998 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002999 return 0;
3000}
3001
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003002/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003003static int h2_wake(struct connection *conn)
3004{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003005 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003006
3007 return (h2_process(h2c));
3008}
3009
Willy Tarreauea392822017-10-31 10:02:25 +01003010/* Connection timeout management. The principle is that if there's no receipt
3011 * nor sending for a certain amount of time, the connection is closed. If the
3012 * MUX buffer still has lying data or is not allocatable, the connection is
3013 * immediately killed. If it's allocatable and empty, we attempt to send a
3014 * GOAWAY frame.
3015 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003016static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003017{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003018 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003019 int expired = tick_is_expired(t->expire, now_ms);
3020
Willy Tarreau0975f112018-03-29 15:22:59 +02003021 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01003022 return t;
3023
Olivier Houchard3f795f72019-04-17 22:51:06 +02003024 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003025
3026 if (!h2c) {
3027 /* resources were already deleted */
3028 return NULL;
3029 }
3030
3031 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003032 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003033 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003034
Willy Tarreau662fafc2019-05-26 09:43:07 +02003035 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003036 /* don't even try to send a GOAWAY, the buffer is stuck */
3037 h2c->flags |= H2_CF_GOAWAY_FAILED;
3038 }
3039
3040 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003041 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003042 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3043 h2c->flags |= H2_CF_GOAWAY_FAILED;
3044
Willy Tarreau662fafc2019-05-26 09:43:07 +02003045 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003046 unsigned int released = 0;
3047 struct buffer *buf;
3048
3049 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3050 if (b_data(buf)) {
3051 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3052 if (!ret)
3053 break;
3054 b_del(buf, ret);
3055 if (b_data(buf))
3056 break;
3057 b_free(buf);
3058 released++;
3059 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003060 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003061
3062 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003063 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003064 }
Willy Tarreauea392822017-10-31 10:02:25 +01003065
Willy Tarreau0975f112018-03-29 15:22:59 +02003066 /* either we can release everything now or it will be done later once
3067 * the last stream closes.
3068 */
3069 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003070 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003071
Willy Tarreauea392822017-10-31 10:02:25 +01003072 return NULL;
3073}
3074
3075
Willy Tarreau62f52692017-10-08 23:01:42 +02003076/*******************************************/
3077/* functions below are used by the streams */
3078/*******************************************/
3079
3080/*
3081 * Attach a new stream to a connection
3082 * (Used for outgoing connections)
3083 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003084static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003085{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003086 struct conn_stream *cs;
3087 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003088 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003089
3090 cs = cs_new(conn);
3091 if (!cs)
3092 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003093 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003094 if (!h2s) {
3095 cs_free(cs);
3096 return NULL;
3097 }
3098 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003099}
3100
Willy Tarreaufafd3982018-11-18 21:29:20 +01003101/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3102 * We have to scan because we may have some orphan streams. It might be
3103 * beneficial to scan backwards from the end to reduce the likeliness to find
3104 * orphans.
3105 */
3106static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3107{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003108 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003109 struct h2s *h2s;
3110 struct eb32_node *node;
3111
3112 node = eb32_first(&h2c->streams_by_id);
3113 while (node) {
3114 h2s = container_of(node, struct h2s, by_id);
3115 if (h2s->cs)
3116 return h2s->cs;
3117 node = eb32_next(node);
3118 }
3119 return NULL;
3120}
3121
Willy Tarreau62f52692017-10-08 23:01:42 +02003122/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003123 * Destroy the mux and the associated connection, if it is no longer used
3124 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003125static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003126{
Christopher Faulet73c12072019-04-08 11:23:22 +02003127 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003128
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003129 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003130 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003131}
3132
3133/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003134 * Detach the stream from the connection and possibly release the connection.
3135 */
3136static void h2_detach(struct conn_stream *cs)
3137{
Willy Tarreau60935142017-10-16 18:11:19 +02003138 struct h2s *h2s = cs->ctx;
3139 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003140 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003141
3142 cs->ctx = NULL;
3143 if (!h2s)
3144 return;
3145
Olivier Houchard998410a2019-04-15 19:23:37 +02003146 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003147 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003148 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003149 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003150 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003151 /*
3152 * At this point, the stream_interface is supposed to have called
3153 * h2_unsubscribe(), so the only way there's still a
3154 * subscription that came from the stream_interface (as we
3155 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3156 * without the stream_interface involved) is that we subscribed
3157 * for sending, we woke the tasklet up and removed the
3158 * SUB_RETRY_SEND flag, so the stream_interface would not
3159 * know it has to unsubscribe for send, but the tasklet hasn't
3160 * run yet. Make sure to handle that by explicitely setting
3161 * send_wait to NULL, as nothing else will do it for us.
3162 */
3163 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003164 }
3165
Olivier Houchardf502aca2018-12-14 19:42:40 +01003166 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003167 h2c = h2s->h2c;
3168 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003169 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003170 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3171 !h2_frt_has_too_many_cs(h2c)) {
3172 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003173 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003174 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003175 }
Willy Tarreau60935142017-10-16 18:11:19 +02003176
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003177 /* this stream may be blocked waiting for some data to leave (possibly
3178 * an ES or RST frame), so orphan it in this case.
3179 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003180 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003181 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003182 (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 +01003183 return;
3184
Willy Tarreau45f752e2017-10-30 15:44:59 +01003185 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3186 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3187 /* unblock the connection if it was blocked on this
3188 * stream.
3189 */
3190 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3191 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003192 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003193 }
3194
Willy Tarreau71049cc2018-03-28 13:56:39 +02003195 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003196
Olivier Houchard8a786902018-12-15 16:05:40 +01003197 if (h2c->flags & H2_CF_IS_BACK &&
3198 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003199 if (!(h2c->conn->flags &
3200 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3201 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003202 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003203 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3204 h2c->conn->owner = NULL;
3205 if (eb_is_empty(&h2c->streams_by_id)) {
3206 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3207 /* The server doesn't want it, let's kill the connection right away */
3208 h2c->conn->mux->destroy(h2c->conn);
3209 return;
3210 }
3211 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003212 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003213 if (eb_is_empty(&h2c->streams_by_id)) {
3214 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3215 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3216 return;
3217 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003218 /* Never ever allow to reuse a connection from a non-reuse backend */
3219 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3220 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003221 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003222 struct server *srv = objt_server(h2c->conn->target);
3223
3224 if (srv) {
3225 if (h2c->conn->flags & CO_FL_PRIVATE)
3226 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3227 else
3228 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3229 }
3230
3231 }
3232 }
3233 }
3234
Willy Tarreaue323f342018-03-28 13:51:45 +02003235 /* We don't want to close right now unless we're removing the
3236 * last stream, and either the connection is in error, or it
3237 * reached the ID already specified in a GOAWAY frame received
3238 * or sent (as seen by last_sid >= 0).
3239 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003240 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003241 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003242 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003243 }
3244 else if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02003245 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3246 task_queue(h2c->task);
Willy Tarreau60935142017-10-16 18:11:19 +02003247 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003248}
3249
Willy Tarreau88bdba32019-05-13 18:17:53 +02003250/* Performs a synchronous or asynchronous shutr(). */
3251static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003252{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003253 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003254 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003255
Willy Tarreauf983d002019-05-14 10:40:21 +02003256 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003257 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003258
Willy Tarreau18059042019-01-31 19:12:48 +01003259 /* a connstream may require us to immediately kill the whole connection
3260 * for example because of a "tcp-request content reject" rule that is
3261 * normally used to limit abuse. In this case we schedule a goaway to
3262 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003263 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003264 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003265 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3266 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3267 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3268 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003269 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3270 /* Nothing was never sent for this stream, so reset with
3271 * REFUSED_STREAM error to let the client retry the
3272 * request.
3273 */
3274 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3275 }
Willy Tarreau18059042019-01-31 19:12:48 +01003276
Willy Tarreau90c32322017-11-24 08:00:30 +01003277 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003278 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003279 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003280
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003281 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003282 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003283 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003284 done:
3285 h2s->flags &= ~H2_SF_WANT_SHUTR;
3286 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003287add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003288 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003289 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003290 if (h2s->flags & H2_SF_BLK_MFCTL) {
3291 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3292 h2s->send_wait = sw;
3293 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3294 h2s->send_wait = sw;
3295 LIST_ADDQ(&h2c->send_list, &h2s->list);
3296 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003297 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003298 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003299 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003300 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003301}
3302
Willy Tarreau88bdba32019-05-13 18:17:53 +02003303/* Performs a synchronous or asynchronous shutw(). */
3304static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003305{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003306 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003307 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003308
Willy Tarreauf983d002019-05-14 10:40:21 +02003309 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003310 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003311
Willy Tarreauf983d002019-05-14 10:40:21 +02003312 if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR &&
3313 (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003314 /* we can cleanly close using an empty data frame only after headers */
3315
3316 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3317 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003318 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003319
3320 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003321 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003322 else
3323 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003324 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003325 /* a connstream may require us to immediately kill the whole connection
3326 * for example because of a "tcp-request content reject" rule that is
3327 * normally used to limit abuse. In this case we schedule a goaway to
3328 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003329 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003330 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003331 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3332 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3333 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3334 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003335 else {
3336 /* Nothing was never sent for this stream, so reset with
3337 * REFUSED_STREAM error to let the client retry the
3338 * request.
3339 */
3340 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3341 }
Willy Tarreau18059042019-01-31 19:12:48 +01003342
Willy Tarreau90c32322017-11-24 08:00:30 +01003343 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003344 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003345 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003346
Willy Tarreau00dd0782018-03-01 16:31:34 +01003347 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003348 }
3349
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003350 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003351 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003352 done:
3353 h2s->flags &= ~H2_SF_WANT_SHUTW;
3354 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003355
3356 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003357 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003358 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003359 if (h2s->flags & H2_SF_BLK_MFCTL) {
3360 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3361 h2s->send_wait = sw;
3362 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3363 h2s->send_wait = sw;
3364 LIST_ADDQ(&h2c->send_list, &h2s->list);
3365 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003366 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003367 /* let the handler know we want to shutw */
3368 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003369 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003370}
3371
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003372/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003373 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3374 * and prevented the last frame from being emitted.
3375 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003376static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3377{
3378 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003379 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003380
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003381 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003382 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003383 h2_do_shutw(h2s);
3384
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003385 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003386 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003387
Willy Tarreau88bdba32019-05-13 18:17:53 +02003388 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003389 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003390 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003391
Willy Tarreau88bdba32019-05-13 18:17:53 +02003392 if (!h2s->cs) {
3393 h2s_destroy(h2s);
3394 if (h2c_is_dead(h2c))
3395 h2_release(h2c);
3396 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003397 }
3398
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003399 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003400}
3401
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003402/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003403static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3404{
3405 struct h2s *h2s = cs->ctx;
3406
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003407 if (cs->flags & CS_FL_KILL_CONN)
3408 h2s->flags |= H2_SF_KILL_CONN;
3409
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003410 if (!mode)
3411 return;
3412
3413 h2_do_shutr(h2s);
3414}
3415
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003416/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003417static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3418{
3419 struct h2s *h2s = cs->ctx;
3420
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003421 if (cs->flags & CS_FL_KILL_CONN)
3422 h2s->flags |= H2_SF_KILL_CONN;
3423
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003424 h2_do_shutw(h2s);
3425}
3426
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003427/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003428 * HTX request or response depending on the connection's side. Returns a
3429 * positive value on success, a negative value on failure, or 0 if it couldn't
3430 * proceed. May report connection errors in h2c->errcode if the frame is
3431 * non-decodable and the connection unrecoverable. In absence of connection
3432 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003433 *
3434 * The function may fold CONTINUATION frames into the initial HEADERS frame
3435 * by removing padding and next frame header, then moving the CONTINUATION
3436 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3437 * leaving a hole between the main frame and the beginning of the next one.
3438 * The possibly remaining incomplete or next frame at the end may be moved
3439 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3440 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3441 *
3442 * A buffer at the beginning of processing may look like this :
3443 *
3444 * ,---.---------.-----.--------------.--------------.------.---.
3445 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3446 * `---^---------^-----^--------------^--------------^------^---'
3447 * | | <-----> | |
3448 * area | dpl | wrap
3449 * |<--------------> |
3450 * | dfl |
3451 * |<-------------------------------------------------->|
3452 * head data
3453 *
3454 * Padding is automatically overwritten when folding, participating to the
3455 * hole size after dfl :
3456 *
3457 * ,---.------------------------.-----.--------------.------.---.
3458 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3459 * `---^------------------------^-----^--------------^------^---'
3460 * | | <-----> | |
3461 * area | hole | wrap
3462 * |<-----------------------> |
3463 * | dfl |
3464 * |<-------------------------------------------------->|
3465 * head data
3466 *
3467 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3468 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3469 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003470 *
3471 * The <flags> field must point to either the stream's flags or to a copy of it
3472 * so that the function can update the following flags :
3473 * - H2_SF_DATA_CLEN when content-length is seen
3474 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3475 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003476 *
3477 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3478 * decoding, in order to detect if we're dealing with a headers or a trailers
3479 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003480 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003481static 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 +02003482{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003483 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003484 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003485 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003486 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003487 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003488 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003489 int flen; // header frame len
3490 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003491 int ret = 0;
3492 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003493 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003494 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003495
Willy Tarreauea18f862018-12-22 20:19:26 +01003496next_frame:
3497 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3498 goto leave; // incomplete input frame
3499
3500 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3501 * this case, we'll try to paste it immediately after the initial
3502 * HEADERS frame payload and kill any possible padding. The initial
3503 * frame's length will be increased to represent the concatenation
3504 * of the two frames. The next frame is read from position <tlen>
3505 * and written at position <flen> (minus padding if some is present).
3506 */
3507 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3508 struct h2_fh hdr;
3509 int clen; // CONTINUATION frame's payload length
3510
3511 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3512 /* no more data, the buffer may be full, either due to
3513 * too large a frame or because of too large a hole that
3514 * we're going to compact at the end.
3515 */
3516 goto leave;
3517 }
3518
3519 if (hdr.ft != H2_FT_CONTINUATION) {
3520 /* RFC7540#6.10: frame of unexpected type */
3521 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3522 goto fail;
3523 }
3524
3525 if (hdr.sid != h2c->dsi) {
3526 /* RFC7540#6.10: frame of different stream */
3527 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3528 goto fail;
3529 }
3530
3531 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3532 /* RFC7540#4.2: invalid frame length */
3533 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3534 goto fail;
3535 }
3536
3537 /* detect when we must stop aggragating frames */
3538 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3539
3540 /* Take as much as we can of the CONTINUATION frame's payload */
3541 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3542 if (clen > hdr.len)
3543 clen = hdr.len;
3544
3545 /* Move the frame's payload over the padding, hole and frame
3546 * header. At least one of hole or dpl is null (see diagrams
3547 * above). The hole moves after the new aggragated frame.
3548 */
3549 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3550 h2c->dfl += clen - h2c->dpl;
3551 hole += h2c->dpl + 9;
3552 h2c->dpl = 0;
3553 goto next_frame;
3554 }
3555
3556 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003557
Willy Tarreau13278b42017-10-13 19:23:14 +02003558 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003559 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003560 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003561 copy = alloc_trash_chunk();
3562 if (!copy) {
3563 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3564 goto fail;
3565 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003566 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3567 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3568 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003569 }
3570
Willy Tarreau13278b42017-10-13 19:23:14 +02003571 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3572 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003573 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003574 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3575 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003576 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003577 }
3578
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003579 if (flen < 5) {
3580 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3581 goto fail;
3582 }
3583
Willy Tarreau13278b42017-10-13 19:23:14 +02003584 hdrs += 5; // stream dep = 4, weight = 1
3585 flen -= 5;
3586 }
3587
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003588 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003589 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003590 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003591 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003592
Willy Tarreau937f7602018-02-26 15:22:17 +01003593 /* we can't retry a failed decompression operation so we must be very
3594 * careful not to take any risks. In practice the output buffer is
3595 * always empty except maybe for trailers, in which case we simply have
3596 * to wait for the upper layer to finish consuming what is available.
3597 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003598
3599 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003600 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003601 if (!htx_is_empty(htx)) {
3602 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003603 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003604 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003605 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003606 if (b_data(rxbuf)) {
3607 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003608 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003609 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003610
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003611 rxbuf->head = 0;
3612 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003613 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003614
Willy Tarreau25919232019-01-03 14:48:18 +01003615 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003616 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3617 sizeof(list)/sizeof(list[0]), tmp);
3618 if (outlen < 0) {
3619 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3620 goto fail;
3621 }
3622
Willy Tarreau25919232019-01-03 14:48:18 +01003623 /* The PACK decompressor was updated, let's update the input buffer and
3624 * the parser's state to commit these changes and allow us to later
3625 * fail solely on the stream if needed.
3626 */
3627 b_del(&h2c->dbuf, h2c->dfl + hole);
3628 h2c->dfl = hole = 0;
3629 h2c->st0 = H2_CS_FRAME_H;
3630
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003631 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003632 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003633
Willy Tarreau88d138e2019-01-02 19:38:14 +01003634 if (*flags & H2_SF_HEADERS_RCVD)
3635 goto trailers;
3636
3637 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003638 if (htx) {
3639 /* HTX mode */
3640 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003641 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003642 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003643 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003644 } else {
3645 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003646 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003647 if (outlen > 0)
3648 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003649 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003650
3651 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003652 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003653 goto fail;
3654 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003655
Willy Tarreau174b06a2018-04-25 18:13:58 +02003656 if (msgf & H2_MSGF_BODY) {
3657 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003658 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003659 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003660 if (htx)
3661 htx->extra = *body_len;
3662 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003663 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003664 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003665 }
3666
Willy Tarreau88d138e2019-01-02 19:38:14 +01003667 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003668 /* indicate that a HEADERS frame was received for this stream, except
3669 * for 1xx responses. For 1xx responses, another HEADERS frame is
3670 * expected.
3671 */
3672 if (!(msgf & H2_MSGF_RSP_1XX))
3673 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003674
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02003675 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003676 /* Mark the end of message, either using EOM in HTX or with the
3677 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003678 * is not set during headers with END_STREAM. For HTX trailers,
3679 * we must not leave an HTX trailers block not followed by an
3680 * EOM block, the two must be atomic. Thus if we fail to emit
3681 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003682 */
3683 if (htx) {
Christopher Faulet2d7c5392019-06-03 10:41:26 +02003684 if (!htx_add_endof(htx, HTX_BLK_EOM))
Willy Tarreau88d138e2019-01-02 19:38:14 +01003685 goto fail;
3686 }
3687 else if (*flags & H2_SF_DATA_CHNK) {
3688 if (!b_putblk(rxbuf, "\r\n", 2))
3689 goto fail;
3690 }
3691 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003692
Willy Tarreau86277d42019-01-02 15:36:11 +01003693 /* success */
3694 ret = 1;
3695
Willy Tarreau68dd9852017-07-03 14:44:26 +02003696 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003697 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003698 * move the remaining data over it.
3699 */
3700 if (hole) {
3701 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3702 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3703 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3704 b_sub(&h2c->dbuf, hole);
3705 }
3706
Willy Tarreau97215ca2019-04-29 10:20:21 +02003707 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003708 /* too large frames */
3709 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003710 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003711 }
3712
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003713 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003714 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003715 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003716 return ret;
3717
Willy Tarreau68dd9852017-07-03 14:44:26 +02003718 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003719 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003720 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003721
3722 trailers:
3723 /* This is the last HEADERS frame hence a trailer */
3724
3725 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3726 /* It's a trailer but it's missing ES flag */
3727 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3728 goto fail;
3729 }
3730
Christopher Faulet54b5e212019-06-04 10:08:28 +02003731 /* Trailers terminate a DATA sequence. In HTX we always handle them. In
3732 * legacy, when using chunks, we have to emit the 0 CRLF marker first
3733 * and then handle the trailers. For other modes, the trailers are
3734 * silently dropped.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003735 */
3736 if (htx) {
Willy Tarreau5255f282019-01-03 18:41:05 +01003737 if (h2_make_htx_trailers(list, htx) <= 0)
3738 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003739 }
3740 else if (*flags & H2_SF_DATA_CHNK) {
3741 /* Legacy mode with chunked encoding : we must finalize the
3742 * data block message emit the trailing CRLF */
3743 if (!b_putblk(rxbuf, "0\r\n", 3))
3744 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003745
3746 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3747 if (outlen > 0)
3748 b_add(rxbuf, outlen);
3749 else
3750 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003751 }
3752
3753 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003754}
3755
Willy Tarreau454f9052017-10-26 19:40:35 +02003756/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3757 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3758 * in use, a new chunk is emitted for each frame. This is supposed to fit
3759 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3760 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3761 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003762 * parser state is automatically updated. Returns > 0 if it could completely
3763 * send the current frame, 0 if it couldn't complete, in which case
3764 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3765 * DATA frame can return 0 as a valid result). Stream errors are reported in
3766 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3767 * have checked the frame header and ensured that the frame was complete or the
3768 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003769 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003770static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003771{
3772 struct h2c *h2c = h2s->h2c;
3773 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003774 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003775 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003776 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003777 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003778
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003779 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003780
Olivier Houchard638b7992018-08-16 15:41:52 +02003781 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003782 if (!csbuf) {
3783 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003784 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003785 }
3786
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003787try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003788 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003789 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3790 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003791 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003792 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003793
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003794 if (flen > b_data(&h2c->dbuf)) {
3795 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003796 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003797 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003798 }
3799
Willy Tarreaua9b77962019-01-31 07:23:00 +01003800 if (htx) {
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003801 unsigned int sent;
3802
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003803 block1 = htx_free_data_space(htx);
3804 if (!block1) {
3805 h2c->flags |= H2_CF_DEM_SFULL;
3806 goto fail;
3807 }
3808 if (flen > block1)
3809 flen = block1;
3810
3811 /* here, flen is the max we can copy into the output buffer */
3812 block1 = b_contig_data(&h2c->dbuf, 0);
3813 if (flen > block1)
3814 flen = block1;
3815
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003816 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003817
Willy Tarreaub6563f42019-06-15 11:34:41 +02003818 b_del(&h2c->dbuf, sent);
3819 h2c->dfl -= sent;
3820 h2c->rcvd_c += sent;
3821 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003822
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003823 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreaub6563f42019-06-15 11:34:41 +02003824 h2s->body_len -= sent;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003825 htx->extra = h2s->body_len;
3826 }
Willy Tarreau0a7ef022019-05-28 10:30:11 +02003827
3828 if (sent < flen) {
3829 h2c->flags |= H2_CF_DEM_SFULL;
3830 goto fail;
3831 }
3832
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003833 goto try_again;
3834 }
Willy Tarreau455d5682019-05-24 19:42:18 +02003835 else if (unlikely(b_space_wraps(csbuf) &&
3836 flen + chklen <= b_room(csbuf) &&
3837 b_data(csbuf) <= MAX_DATA_REALIGN)) {
3838 /* it doesn't fit in a single block and the buffer is fragmented, if there are
3839 * not too many data in the buffer, let's defragment it and try
3840 * again.
Willy Tarreaud755ea62018-02-26 15:44:54 +01003841 */
3842 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003843 }
3844
Willy Tarreaueba10f22018-04-25 20:44:22 +02003845 /* chunked-encoding requires more room */
3846 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003847 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003848 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3849 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3850 (chklen < 1048576) ? 4 : 8;
3851 chklen += 4; // CRLF, CRLF
3852 }
3853
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003854 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003855 if (flen + chklen > b_room(csbuf)) {
3856 if (chklen >= b_room(csbuf)) {
3857 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003858 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003859 }
3860 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003861 }
3862
3863 if (h2s->flags & H2_SF_DATA_CHNK) {
3864 /* emit the chunk size */
3865 unsigned int chksz = flen;
3866 char str[10];
3867 char *beg;
3868
3869 beg = str + sizeof(str);
3870 *--beg = '\n';
3871 *--beg = '\r';
3872 do {
3873 *--beg = hextab[chksz & 0xF];
3874 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003875 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003876 }
3877
Willy Tarreau454f9052017-10-26 19:40:35 +02003878 /* Block1 is the length of the first block before the buffer wraps,
3879 * block2 is the optional second block to reach the end of the frame.
3880 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003881 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003882 if (block1 > flen)
3883 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003884 block2 = flen - block1;
3885
3886 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003887 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003888
3889 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003890 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003891
Willy Tarreaueba10f22018-04-25 20:44:22 +02003892 if (h2s->flags & H2_SF_DATA_CHNK) {
3893 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003894 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003895 }
3896
Willy Tarreau454f9052017-10-26 19:40:35 +02003897 /* now mark the input data as consumed (will be deleted from the buffer
3898 * by the caller when seeing FRAME_A after sending the window update).
3899 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003900 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003901 h2c->dfl -= flen;
3902 h2c->rcvd_c += flen;
3903 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3904
Willy Tarreau1915ca22019-01-24 11:49:37 +01003905 if (h2s->flags & H2_SF_DATA_CLEN)
3906 h2s->body_len -= flen;
3907
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003908 if (h2c->dfl > h2c->dpl) {
3909 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003910 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003911 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003912 }
3913
Willy Tarreau4a28da12018-01-04 14:41:00 +01003914 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003915 /* here we're done with the frame, all the payload (except padding) was
3916 * transferred.
3917 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003918
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003919 if (h2c->dff & H2_F_DATA_END_STREAM) {
3920 if (htx) {
3921 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3922 h2c->flags |= H2_CF_DEM_SFULL;
3923 goto fail;
3924 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003925 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003926 else if (h2s->flags & H2_SF_DATA_CHNK) {
3927 /* emit the trailing 0 CRLF CRLF */
3928 if (b_room(csbuf) < 5) {
3929 h2c->flags |= H2_CF_DEM_SFULL;
3930 goto fail;
3931 }
3932 chklen += 5;
3933 b_putblk(csbuf, "0\r\n\r\n", 5);
3934 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003935 }
3936
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003937 h2c->rcvd_c += h2c->dpl;
3938 h2c->rcvd_s += h2c->dpl;
3939 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003940 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003941 if (htx)
3942 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003943 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003944 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003945 if (htx)
3946 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003947 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003948}
3949
Willy Tarreau5dd17352018-06-14 13:33:30 +02003950/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3951 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3952 * number of bytes sent. The caller must check the stream's status to detect
3953 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003954 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003955static 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 +02003956{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003957 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003958 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003959 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003960 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02003961 struct buffer *mbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003962 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003963 int es_now = 0;
3964 int ret = 0;
3965 int hdr;
3966
3967 if (h2c_mux_busy(h2c, h2s)) {
3968 h2s->flags |= H2_SF_BLK_MBUSY;
3969 return 0;
3970 }
3971
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003972 /* First, try to parse the H1 response and index it into <list>.
3973 * NOTE! Since it comes from haproxy, we *know* that a response header
3974 * block does not wrap and we can safely read it this way without
3975 * having to realign the buffer.
3976 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003977 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003978 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003979 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003980 /* incomplete or invalid response, this is abnormal coming from
3981 * haproxy and may only result in a bad errorfile or bad Lua code
3982 * so that won't be fixed, raise an error now.
3983 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003984 * FIXME: we should instead add the ability to only return a
3985 * 502 bad gateway. But in theory this is not supposed to
3986 * happen.
3987 */
3988 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3989 ret = 0;
3990 goto end;
3991 }
3992
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003993 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003994
3995 /* certain statuses have no body or an empty one, regardless of
3996 * what the headers say.
3997 */
3998 if (sl.st.status >= 100 && sl.st.status < 200) {
3999 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
4000 h1m->curr_len = h1m->body_len = 0;
4001 }
4002 else if (sl.st.status == 204 || sl.st.status == 304) {
4003 /* no contents, claim c-len is present and set to zero */
4004 h1m->flags &= ~H1_MF_CHNK;
4005 h1m->flags |= H1_MF_CLEN;
4006 h1m->curr_len = h1m->body_len = 0;
4007 }
4008
Willy Tarreaubcc45952019-05-26 10:05:50 +02004009 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004010 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004011 if (!h2_get_buf(h2c, mbuf)) {
4012 h2c->flags |= H2_CF_MUX_MALLOC;
4013 h2s->flags |= H2_SF_BLK_MROOM;
4014 return 0;
4015 }
4016
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004017 chunk_reset(&outbuf);
4018
4019 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004020 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4021 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004022 break;
4023 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004024 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004025 }
4026
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004027 if (outbuf.size < 9)
4028 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004029
4030 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004031 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4032 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4033 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004034
4035 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004036 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01004037 /* this is an unparsable response */
4038 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4039 ret = 0;
4040 goto end;
4041 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01004042
4043 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004044 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004045 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004046 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004047 }
4048
4049 /* encode all headers, stop at empty name */
4050 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01004051 /* these ones do not exist in H2 and must be dropped. */
4052 if (isteq(list[hdr].n, ist("connection")) ||
4053 isteq(list[hdr].n, ist("proxy-connection")) ||
4054 isteq(list[hdr].n, ist("keep-alive")) ||
4055 isteq(list[hdr].n, ist("upgrade")) ||
4056 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004057 continue;
4058
4059 if (isteq(list[hdr].n, ist("")))
4060 break; // end
4061
4062 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4063 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004064 if (b_space_wraps(mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004065 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004066 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004067 }
4068 }
4069
4070 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01004071 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004072 es_now = 1;
4073
4074 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004075 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004076
4077 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004078 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004079
4080 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004081 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004082
4083 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004084 b_add(mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01004085 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004086
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004087 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01004088 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004089 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01004090
Willy Tarreau801250e2018-09-11 11:45:04 +02004091 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004092 h2s->flags |= H2_SF_ES_SENT;
4093 if (h2s->st == H2_SS_OPEN)
4094 h2s->st = H2_SS_HLOC;
4095 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004096 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004097 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02004098 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01004099 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004100 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004101 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004102 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004103 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004104 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004105
4106 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004107
4108 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004109 //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 +02004110 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004111 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004112 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4113 goto retry;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004114 h1m_init_res(h1m);
4115 h1m->err_pos = -1; // don't care about errors on the response path
4116 h2c->flags |= H2_CF_MUX_MFULL;
4117 h2s->flags |= H2_SF_BLK_MROOM;
4118 ret = 0;
4119 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004120}
4121
Willy Tarreau5dd17352018-06-14 13:33:30 +02004122/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4123 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4124 * the number of bytes sent. The caller must check the stream's status to
4125 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004126 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004127static 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 +02004128{
4129 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004130 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004131 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004132 struct buffer *mbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004133 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004134 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004135 int es_now = 0;
4136 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004137 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004138 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004139
4140 if (h2c_mux_busy(h2c, h2s)) {
4141 h2s->flags |= H2_SF_BLK_MBUSY;
4142 goto end;
4143 }
4144
Willy Tarreaubcc45952019-05-26 10:05:50 +02004145 mbuf = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004146 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004147 if (!h2_get_buf(h2c, mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004148 h2c->flags |= H2_CF_MUX_MALLOC;
4149 h2s->flags |= H2_SF_BLK_MROOM;
4150 goto end;
4151 }
4152
4153 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004154 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004155 goto end;
4156
4157 chunk_reset(&outbuf);
4158
4159 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004160 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4161 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004162 break;
4163 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004164 /* If there are pending data in the output buffer, and we have
4165 * less than 1/4 of the mbuf's size and everything fits, we'll
4166 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4167 * is full and wait, to save some slow realign calls.
4168 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004169 if ((max + 9 > b_room(mbuf) || max >= b_size(mbuf) / 4)) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004170 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4171 goto retry;
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004172 h2c->flags |= H2_CF_MUX_MFULL;
4173 h2s->flags |= H2_SF_BLK_MROOM;
4174 goto end;
4175 }
4176
Willy Tarreaubcc45952019-05-26 10:05:50 +02004177 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004178 }
4179
4180 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004181 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4182 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004183 h2c->flags |= H2_CF_MUX_MFULL;
4184 h2s->flags |= H2_SF_BLK_MROOM;
4185 goto end;
4186 }
4187
4188 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004189 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4190 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4191 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004192
4193 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4194 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004195 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004196 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004197 break;
4198 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004199 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004200 if ((long long)size > h1m->curr_len)
4201 size = h1m->curr_len;
4202 break;
4203 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004204 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004205 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004206 if (!ret)
4207 goto end;
4208
4209 if (ret < 0) {
4210 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004211 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004212 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4213 goto end;
4214 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004215 max -= ret;
4216 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004217 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004218 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004219 }
4220
Willy Tarreau801250e2018-09-11 11:45:04 +02004221 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004222 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004223 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004224 if (!ret)
4225 goto end;
4226
4227 if (ret < 0) {
4228 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004229 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004230 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4231 goto end;
4232 }
4233
4234 size = chunk;
4235 h1m->curr_len = chunk;
4236 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004237 max -= ret;
4238 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004239 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004240 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004241 if (!size)
4242 goto send_empty;
4243 }
4244
4245 /* in MSG_DATA state, continue below */
4246 size = h1m->curr_len;
4247 break;
4248 }
4249
4250 /* we have in <size> the exact number of bytes we need to copy from
4251 * the H1 buffer. We need to check this against the connection's and
4252 * the stream's send windows, and to ensure that this fits in the max
4253 * frame size and in the buffer's available space minus 9 bytes (for
4254 * the frame header). The connection's flow control is applied last so
4255 * that we can use a separate list of streams which are immediately
4256 * unblocked on window opening. Note: we don't implement padding.
4257 */
4258
Willy Tarreau5dd17352018-06-14 13:33:30 +02004259 if (size > max)
4260 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004261
4262 if (size > h2s->mws)
4263 size = h2s->mws;
4264
4265 if (size <= 0) {
4266 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004267 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004268 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004269 goto end;
4270 }
4271
4272 if (h2c->mfs && size > h2c->mfs)
4273 size = h2c->mfs;
4274
4275 if (size + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02004276 /* It doesn't fit at once. If it at least fits once split and
4277 * the amount of data to move is low, let's defragment the
4278 * buffer now.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004279 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004280 if (b_space_wraps(mbuf) &&
4281 (size + 9 <= b_room(mbuf)) &&
4282 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004283 goto realign_again;
4284 size = outbuf.size - 9;
4285 }
4286
4287 if (size <= 0) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004288 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4289 goto retry;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004290 h2c->flags |= H2_CF_MUX_MFULL;
4291 h2s->flags |= H2_SF_BLK_MROOM;
4292 goto end;
4293 }
4294
4295 if (size > h2c->mws)
4296 size = h2c->mws;
4297
4298 if (size <= 0) {
4299 h2s->flags |= H2_SF_BLK_MFCTL;
4300 goto end;
4301 }
4302
4303 /* copy whatever we can */
4304 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004305 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004306 if (ret == 1)
4307 len2 = 0;
4308
4309 if (!ret || len1 + len2 < size) {
4310 /* FIXME: must normally never happen */
4311 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4312 goto end;
4313 }
4314
4315 /* limit len1/len2 to size */
4316 if (len1 + len2 > size) {
4317 int sub = len1 + len2 - size;
4318
4319 if (len2 > sub)
4320 len2 -= sub;
4321 else {
4322 sub -= len2;
4323 len2 = 0;
4324 len1 -= sub;
4325 }
4326 }
4327
4328 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004329 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004330 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004331 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004332
4333 send_empty:
4334 /* we may need to add END_STREAM */
4335 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4336 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004337 *
4338 * FIXME: what we do here is not correct because we send end_stream
4339 * before knowing if we'll have to send a HEADERS frame for the
4340 * trailers. More importantly we're not consuming the trailing CRLF
4341 * after the end of trailers, so it will be left to the caller to
4342 * eat it. The right way to do it would be to measure trailers here
4343 * and to send ES only if there are no trailers.
4344 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004345 */
4346 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004347 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004348 es_now = 1;
4349
4350 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004351 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004352
4353 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004354 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004355
4356 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004357 b_add(mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004358
4359 /* consume incoming H1 response */
4360 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004361 max -= size;
4362 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004363 total += size;
4364 h1m->curr_len -= size;
4365 h2s->mws -= size;
4366 h2c->mws -= size;
4367
4368 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004369 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004370 goto new_frame;
4371 }
4372 }
4373
4374 if (es_now) {
4375 if (h2s->st == H2_SS_OPEN)
4376 h2s->st = H2_SS_HLOC;
4377 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004378 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004379
Willy Tarreau35a62702018-02-27 15:37:25 +01004380 if (!(h1m->flags & H1_MF_CHNK)) {
4381 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004382 total += max;
4383 ofs += max;
4384 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004385
Willy Tarreau801250e2018-09-11 11:45:04 +02004386 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004387 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004388
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004389 h2s->flags |= H2_SF_ES_SENT;
4390 }
4391
4392 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004393 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004394 return total;
4395}
4396
Willy Tarreau115e83b2018-12-01 19:17:53 +01004397/* Try to send a HEADERS frame matching HTX response present in HTX message
4398 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4399 * must check the stream's status to detect any error which might have happened
4400 * subsequently to a successful send. The htx blocks are automatically removed
4401 * from the message. The htx message is assumed to be valid since produced from
4402 * the internal code, hence it contains a start line, an optional series of
4403 * header blocks and an end of header, otherwise an invalid frame could be
4404 * emitted and the resulting htx message could be left in an inconsistent state.
4405 */
4406static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4407{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004408 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004409 struct h2c *h2c = h2s->h2c;
4410 struct htx_blk *blk;
4411 struct htx_blk *blk_end;
4412 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004413 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004414 struct htx_sl *sl;
4415 enum htx_blk_type type;
4416 int es_now = 0;
4417 int ret = 0;
4418 int hdr;
4419 int idx;
4420
4421 if (h2c_mux_busy(h2c, h2s)) {
4422 h2s->flags |= H2_SF_BLK_MBUSY;
4423 return 0;
4424 }
4425
Willy Tarreau115e83b2018-12-01 19:17:53 +01004426 /* determine the first block which must not be deleted, blk_end may
4427 * be NULL if all blocks have to be deleted.
4428 */
4429 idx = htx_get_head(htx);
4430 blk_end = NULL;
4431 while (idx != -1) {
4432 type = htx_get_blk_type(htx_get_blk(htx, idx));
4433 idx = htx_get_next(htx, idx);
4434 if (type == HTX_BLK_EOH) {
4435 if (idx != -1)
4436 blk_end = htx_get_blk(htx, idx);
4437 break;
4438 }
4439 }
4440
4441 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004442 blk = htx_get_head_blk(htx);
4443 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL);
4444 ALREADY_CHECKED(blk);
4445 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004446 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004447 if (h2s->status < 100 || h2s->status > 999)
4448 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004449
4450 /* and the rest of the headers, that we dump starting at header 0 */
4451 hdr = 0;
4452
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004453 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004454 while ((idx = htx_get_next(htx, idx)) != -1) {
4455 blk = htx_get_blk(htx, idx);
4456 type = htx_get_blk_type(blk);
4457
4458 if (type == HTX_BLK_UNUSED)
4459 continue;
4460
4461 if (type != HTX_BLK_HDR)
4462 break;
4463
4464 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4465 goto fail;
4466
4467 list[hdr].n = htx_get_blk_name(htx, blk);
4468 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004469 hdr++;
4470 }
4471
4472 /* marker for end of headers */
4473 list[hdr].n = ist("");
4474
4475 if (h2s->status == 204 || h2s->status == 304) {
4476 /* no contents, claim c-len is present and set to zero */
4477 es_now = 1;
4478 }
4479
Willy Tarreau9c218e72019-05-26 10:08:28 +02004480 mbuf = br_tail(h2c->mbuf);
4481 retry:
4482 if (!h2_get_buf(h2c, mbuf)) {
4483 h2c->flags |= H2_CF_MUX_MALLOC;
4484 h2s->flags |= H2_SF_BLK_MROOM;
4485 return 0;
4486 }
4487
Willy Tarreau115e83b2018-12-01 19:17:53 +01004488 chunk_reset(&outbuf);
4489
4490 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004491 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4492 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004493 break;
4494 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004495 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004496 }
4497
4498 if (outbuf.size < 9)
4499 goto full;
4500
4501 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4502 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4503 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4504 outbuf.data = 9;
4505
4506 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004507 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004508 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004509 goto realign_again;
4510 goto full;
4511 }
4512
4513 /* encode all headers, stop at empty name */
4514 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4515 /* these ones do not exist in H2 and must be dropped. */
4516 if (isteq(list[hdr].n, ist("connection")) ||
4517 isteq(list[hdr].n, ist("proxy-connection")) ||
4518 isteq(list[hdr].n, ist("keep-alive")) ||
4519 isteq(list[hdr].n, ist("upgrade")) ||
4520 isteq(list[hdr].n, ist("transfer-encoding")))
4521 continue;
4522
4523 if (isteq(list[hdr].n, ist("")))
4524 break; // end
4525
4526 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4527 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004528 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004529 goto realign_again;
4530 goto full;
4531 }
4532 }
4533
Christopher Faulet0b465482019-02-19 15:14:23 +01004534 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004535 * FIXME: we should also set it when we know for sure that the
4536 * content-length is zero as well as on 204/304
4537 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004538 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4539 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004540 es_now = 1;
4541
Willy Tarreau927b88b2019-03-04 08:03:25 +01004542 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004543 es_now = 1;
4544
4545 /* update the frame's size */
4546 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4547
4548 if (es_now)
4549 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4550
4551 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004552 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004553
4554 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4555 * 1xx responses, another HEADERS frame is expected.
4556 */
4557 if (h2s->status >= 200 || h2s->status == 101)
4558 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004559
Willy Tarreau115e83b2018-12-01 19:17:53 +01004560 if (es_now) {
4561 h2s->flags |= H2_SF_ES_SENT;
4562 if (h2s->st == H2_SS_OPEN)
4563 h2s->st = H2_SS_HLOC;
4564 else
4565 h2s_close(h2s);
4566 }
4567
4568 /* OK we could properly deliver the response */
4569
4570 /* remove all header blocks including the EOH and compute the
4571 * corresponding size.
4572 *
4573 * FIXME: We should remove everything when es_now is set.
4574 */
4575 ret = 0;
4576 idx = htx_get_head(htx);
4577 blk = htx_get_blk(htx, idx);
4578 while (blk != blk_end) {
4579 ret += htx_get_blksz(blk);
4580 blk = htx_remove_blk(htx, blk);
4581 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004582
Christopher Faulet316934d2019-05-15 14:22:48 +02004583 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4584 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004585 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004586 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004587 end:
4588 return ret;
4589 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004590 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4591 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004592 h2c->flags |= H2_CF_MUX_MFULL;
4593 h2s->flags |= H2_SF_BLK_MROOM;
4594 ret = 0;
4595 goto end;
4596 fail:
4597 /* unparsable HTX messages, too large ones to be produced in the local
4598 * list etc go here (unrecoverable errors).
4599 */
4600 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4601 ret = 0;
4602 goto end;
4603}
4604
Willy Tarreau80739692018-10-05 11:35:57 +02004605/* Try to send a HEADERS frame matching HTX request present in HTX message
4606 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4607 * must check the stream's status to detect any error which might have happened
4608 * subsequently to a successful send. The htx blocks are automatically removed
4609 * from the message. The htx message is assumed to be valid since produced from
4610 * the internal code, hence it contains a start line, an optional series of
4611 * header blocks and an end of header, otherwise an invalid frame could be
4612 * emitted and the resulting htx message could be left in an inconsistent state.
4613 */
4614static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4615{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004616 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004617 struct h2c *h2c = h2s->h2c;
4618 struct htx_blk *blk;
4619 struct htx_blk *blk_end;
4620 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004621 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004622 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004623 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004624 enum htx_blk_type type;
4625 int es_now = 0;
4626 int ret = 0;
4627 int hdr;
4628 int idx;
4629
4630 if (h2c_mux_busy(h2c, h2s)) {
4631 h2s->flags |= H2_SF_BLK_MBUSY;
4632 return 0;
4633 }
4634
Willy Tarreau80739692018-10-05 11:35:57 +02004635 /* determine the first block which must not be deleted, blk_end may
4636 * be NULL if all blocks have to be deleted.
4637 */
4638 idx = htx_get_head(htx);
4639 blk_end = NULL;
4640 while (idx != -1) {
4641 type = htx_get_blk_type(htx_get_blk(htx, idx));
4642 idx = htx_get_next(htx, idx);
4643 if (type == HTX_BLK_EOH) {
4644 if (idx != -1)
4645 blk_end = htx_get_blk(htx, idx);
4646 break;
4647 }
4648 }
4649
4650 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004651 blk = htx_get_head_blk(htx);
4652 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
4653 ALREADY_CHECKED(blk);
4654 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004655 meth = htx_sl_req_meth(sl);
4656 path = htx_sl_req_uri(sl);
4657
4658 /* and the rest of the headers, that we dump starting at header 0 */
4659 hdr = 0;
4660
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004661 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004662 while ((idx = htx_get_next(htx, idx)) != -1) {
4663 blk = htx_get_blk(htx, idx);
4664 type = htx_get_blk_type(blk);
4665
4666 if (type == HTX_BLK_UNUSED)
4667 continue;
4668
4669 if (type != HTX_BLK_HDR)
4670 break;
4671
4672 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4673 goto fail;
4674
4675 list[hdr].n = htx_get_blk_name(htx, blk);
4676 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004677 hdr++;
4678 }
4679
4680 /* marker for end of headers */
4681 list[hdr].n = ist("");
4682
Willy Tarreau9c218e72019-05-26 10:08:28 +02004683 mbuf = br_tail(h2c->mbuf);
4684 retry:
4685 if (!h2_get_buf(h2c, mbuf)) {
4686 h2c->flags |= H2_CF_MUX_MALLOC;
4687 h2s->flags |= H2_SF_BLK_MROOM;
4688 return 0;
4689 }
4690
Willy Tarreau80739692018-10-05 11:35:57 +02004691 chunk_reset(&outbuf);
4692
4693 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004694 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4695 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004696 break;
4697 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004698 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004699 }
4700
4701 if (outbuf.size < 9)
4702 goto full;
4703
4704 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4705 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4706 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4707 outbuf.data = 9;
4708
4709 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004710 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004711 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004712 goto realign_again;
4713 goto full;
4714 }
4715
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004716 /* RFC7540 #8.3: the CONNECT method must have :
4717 * - :authority set to the URI part (host:port)
4718 * - :method set to CONNECT
4719 * - :scheme and :path omitted
4720 */
4721 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4722 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004723 struct ist scheme;
4724
4725 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4726 scheme = ist("http");
4727 else
4728 scheme = ist("https");
4729
4730 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004731 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004732 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004733 goto realign_again;
4734 goto full;
4735 }
Willy Tarreau80739692018-10-05 11:35:57 +02004736
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004737 /* encode the path, which necessarily is the second one */
4738 if (!hpack_encode_path(&outbuf, path)) {
4739 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004740 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004741 goto realign_again;
4742 goto full;
4743 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004744
4745 /* look for the Host header and place it in :authority */
4746 auth = ist2(NULL, 0);
4747 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4748 if (isteq(list[hdr].n, ist("")))
4749 break; // end
4750
4751 if (isteq(list[hdr].n, ist("host"))) {
4752 auth = list[hdr].v;
4753 break;
4754 }
4755 }
4756 }
4757 else {
4758 /* for CONNECT, :authority is taken from the path */
4759 auth = path;
4760 }
4761
4762 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4763 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004764 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004765 goto realign_again;
4766 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004767 }
4768
4769 /* encode all headers, stop at empty name */
4770 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4771 /* these ones do not exist in H2 and must be dropped. */
4772 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004773 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004774 isteq(list[hdr].n, ist("proxy-connection")) ||
4775 isteq(list[hdr].n, ist("keep-alive")) ||
4776 isteq(list[hdr].n, ist("upgrade")) ||
4777 isteq(list[hdr].n, ist("transfer-encoding")))
4778 continue;
4779
4780 if (isteq(list[hdr].n, ist("")))
4781 break; // end
4782
4783 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4784 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004785 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004786 goto realign_again;
4787 goto full;
4788 }
4789 }
4790
4791 /* we may need to add END_STREAM if we have no body :
4792 * - request already closed, or :
4793 * - no transfer-encoding, and :
4794 * - no content-length or content-length:0
4795 * Fixme: this doesn't take into account CONNECT requests.
4796 */
4797 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4798 es_now = 1;
4799
4800 if (sl->flags & HTX_SL_F_BODYLESS)
4801 es_now = 1;
4802
Willy Tarreau927b88b2019-03-04 08:03:25 +01004803 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004804 es_now = 1;
4805
4806 /* update the frame's size */
4807 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4808
4809 if (es_now)
4810 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4811
4812 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004813 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004814 h2s->flags |= H2_SF_HEADERS_SENT;
4815 h2s->st = H2_SS_OPEN;
4816
Willy Tarreau80739692018-10-05 11:35:57 +02004817 if (es_now) {
4818 // trim any possibly pending data (eg: inconsistent content-length)
4819 h2s->flags |= H2_SF_ES_SENT;
4820 h2s->st = H2_SS_HLOC;
4821 }
4822
4823 /* remove all header blocks including the EOH and compute the
4824 * corresponding size.
4825 *
4826 * FIXME: We should remove everything when es_now is set.
4827 */
4828 ret = 0;
4829 idx = htx_get_head(htx);
4830 blk = htx_get_blk(htx, idx);
4831 while (blk != blk_end) {
4832 ret += htx_get_blksz(blk);
4833 blk = htx_remove_blk(htx, blk);
4834 }
4835
Christopher Faulet316934d2019-05-15 14:22:48 +02004836 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4837 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004838 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004839 }
Willy Tarreau80739692018-10-05 11:35:57 +02004840
4841 end:
4842 return ret;
4843 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004844 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4845 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004846 h2c->flags |= H2_CF_MUX_MFULL;
4847 h2s->flags |= H2_SF_BLK_MROOM;
4848 ret = 0;
4849 goto end;
4850 fail:
4851 /* unparsable HTX messages, too large ones to be produced in the local
4852 * list etc go here (unrecoverable errors).
4853 */
4854 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4855 ret = 0;
4856 goto end;
4857}
4858
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004859/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004860 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4861 * caller must check the stream's status to detect any error which might have
4862 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02004863 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004864 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004865static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004866{
4867 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004868 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004869 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004870 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004871 size_t total = 0;
4872 int es_now = 0;
4873 int bsize; /* htx block size */
4874 int fsize; /* h2 frame size */
4875 struct htx_blk *blk;
4876 enum htx_blk_type type;
4877 int idx;
4878
4879 if (h2c_mux_busy(h2c, h2s)) {
4880 h2s->flags |= H2_SF_BLK_MBUSY;
4881 goto end;
4882 }
4883
Willy Tarreau98de12a2018-12-12 07:03:00 +01004884 htx = htx_from_buf(buf);
4885
Christopher Faulet54b5e212019-06-04 10:08:28 +02004886 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
4887 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
4888 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004889 */
4890
4891 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004892 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004893 goto end;
4894
4895 idx = htx_get_head(htx);
4896 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02004897 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004898 bsize = htx_get_blksz(blk);
4899 fsize = bsize;
4900
Christopher Faulet54b5e212019-06-04 10:08:28 +02004901 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004902 if (h2s->flags & H2_SF_ES_SENT) {
4903 /* ES already sent */
4904 htx_remove_blk(htx, blk);
4905 total++; // EOM counts as one byte
4906 count--;
4907 goto end;
4908 }
4909 }
4910 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004911 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004912
Willy Tarreau9c218e72019-05-26 10:08:28 +02004913 mbuf = br_tail(h2c->mbuf);
4914 retry:
4915 if (!h2_get_buf(h2c, mbuf)) {
4916 h2c->flags |= H2_CF_MUX_MALLOC;
4917 h2s->flags |= H2_SF_BLK_MROOM;
4918 goto end;
4919 }
4920
Willy Tarreau98de12a2018-12-12 07:03:00 +01004921 /* Perform some optimizations to reduce the number of buffer copies.
4922 * First, if the mux's buffer is empty and the htx area contains
4923 * exactly one data block of the same size as the requested count, and
4924 * this count fits within the frame size, the stream's window size, and
4925 * the connection's window size, then it's possible to simply swap the
4926 * caller's buffer with the mux's output buffer and adjust offsets and
4927 * length to match the entire DATA HTX block in the middle. In this
4928 * case we perform a true zero-copy operation from end-to-end. This is
4929 * the situation that happens all the time with large files. Second, if
4930 * this is not possible, but the mux's output buffer is empty, we still
4931 * have an opportunity to avoid the copy to the intermediary buffer, by
4932 * making the intermediary buffer's area point to the output buffer's
4933 * area. In this case we want to skip the HTX header to make sure that
4934 * copies remain aligned and that this operation remains possible all
4935 * the time. This goes for headers, data blocks and any data extracted
4936 * from the HTX blocks.
4937 */
4938 if (unlikely(fsize == count &&
4939 htx->used == 1 && type == HTX_BLK_DATA &&
4940 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004941 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004942
Willy Tarreaubcc45952019-05-26 10:05:50 +02004943 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004944 /* Too bad there are data left there. We're willing to memcpy/memmove
4945 * up to 1/4 of the buffer, which means that it's OK to copy a large
4946 * frame into a buffer containing few data if it needs to be realigned,
4947 * and that it's also OK to copy few data without realigning. Otherwise
4948 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004949 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004950 if (fsize + 9 <= b_room(mbuf) &&
4951 (b_data(mbuf) <= b_size(mbuf) / 4 ||
4952 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004953 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004954
Willy Tarreau9c218e72019-05-26 10:08:28 +02004955 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4956 goto retry;
4957
Willy Tarreau98de12a2018-12-12 07:03:00 +01004958 h2c->flags |= H2_CF_MUX_MFULL;
4959 h2s->flags |= H2_SF_BLK_MROOM;
4960 goto end;
4961 }
4962
4963 /* map an H2 frame to the HTX block so that we can put the
4964 * frame header there.
4965 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004966 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
4967 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01004968
4969 /* prepend an H2 DATA frame header just before the DATA block */
4970 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4971 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4972 h2_set_frame_size(outbuf.area, fsize);
4973
4974 /* update windows */
4975 h2s->mws -= fsize;
4976 h2c->mws -= fsize;
4977
4978 /* and exchange with our old area */
4979 buf->area = old_area;
4980 buf->data = buf->head = 0;
4981 total += fsize;
4982 goto end;
4983 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004984
Willy Tarreau98de12a2018-12-12 07:03:00 +01004985 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004986 /* for DATA and EOM we'll have to emit a frame, even if empty */
4987
4988 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004989 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4990 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004991 break;
4992 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004993 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004994 }
4995
4996 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004997 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4998 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004999 h2c->flags |= H2_CF_MUX_MFULL;
5000 h2s->flags |= H2_SF_BLK_MROOM;
5001 goto end;
5002 }
5003
5004 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5005 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5006 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5007 outbuf.data = 9;
5008
5009 /* we have in <fsize> the exact number of bytes we need to copy from
5010 * the HTX buffer. We need to check this against the connection's and
5011 * the stream's send windows, and to ensure that this fits in the max
5012 * frame size and in the buffer's available space minus 9 bytes (for
5013 * the frame header). The connection's flow control is applied last so
5014 * that we can use a separate list of streams which are immediately
5015 * unblocked on window opening. Note: we don't implement padding.
5016 */
5017
5018 /* EOM is presented with bsize==1 but would lead to the emission of an
5019 * empty frame, thus we force it to zero here.
5020 */
5021 if (type == HTX_BLK_EOM)
5022 bsize = fsize = 0;
5023
5024 if (!fsize)
5025 goto send_empty;
5026
5027 if (h2s->mws <= 0) {
5028 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005029 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005030 LIST_DEL_INIT(&h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005031 goto end;
5032 }
5033
Willy Tarreauee573762018-12-04 15:25:57 +01005034 if (fsize > count)
5035 fsize = count;
5036
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005037 if (fsize > h2s->mws)
5038 fsize = h2s->mws; // >0
5039
5040 if (h2c->mfs && fsize > h2c->mfs)
5041 fsize = h2c->mfs; // >0
5042
5043 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005044 /* It doesn't fit at once. If it at least fits once split and
5045 * the amount of data to move is low, let's defragment the
5046 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005047 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005048 if (b_space_wraps(mbuf) &&
5049 (fsize + 9 <= b_room(mbuf)) &&
5050 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005051 goto realign_again;
5052 fsize = outbuf.size - 9;
5053
5054 if (fsize <= 0) {
5055 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005056 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5057 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005058 h2c->flags |= H2_CF_MUX_MFULL;
5059 h2s->flags |= H2_SF_BLK_MROOM;
5060 goto end;
5061 }
5062 }
5063
5064 if (h2c->mws <= 0) {
5065 h2s->flags |= H2_SF_BLK_MFCTL;
5066 goto end;
5067 }
5068
5069 if (fsize > h2c->mws)
5070 fsize = h2c->mws;
5071
5072 /* now let's copy this this into the output buffer */
5073 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005074 h2s->mws -= fsize;
5075 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005076 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005077
5078 send_empty:
5079 /* update the frame's size */
5080 h2_set_frame_size(outbuf.area, fsize);
5081
5082 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5083 * meeting EOM. We should optimize this later.
5084 */
5085 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005086 total++; // EOM counts as one byte
5087 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005088 es_now = 1;
5089 }
5090
5091 if (es_now)
5092 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5093
5094 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005095 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005096
5097 /* consume incoming HTX block, including EOM */
5098 total += fsize;
5099 if (fsize == bsize) {
5100 htx_remove_blk(htx, blk);
5101 if (fsize)
5102 goto new_frame;
5103 } else {
5104 /* we've truncated this block */
5105 htx_cut_data_blk(htx, blk, fsize);
5106 }
5107
5108 if (es_now) {
5109 if (h2s->st == H2_SS_OPEN)
5110 h2s->st = H2_SS_HLOC;
5111 else
5112 h2s_close(h2s);
5113
5114 h2s->flags |= H2_SF_ES_SENT;
5115 }
5116
5117 end:
5118 return total;
5119}
5120
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005121/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5122 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5123 * processed. The caller must check the stream's status to detect any error
5124 * which might have happened subsequently to a successful send. The htx blocks
5125 * are automatically removed from the message. The htx message is assumed to be
5126 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005127 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005128 * as a single frame. The ES flag is always set.
5129 */
5130static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5131{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005132 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005133 struct h2c *h2c = h2s->h2c;
5134 struct htx_blk *blk;
5135 struct htx_blk *blk_end;
5136 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005137 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005138 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005139 int ret = 0;
5140 int hdr;
5141 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005142
5143 if (h2c_mux_busy(h2c, h2s)) {
5144 h2s->flags |= H2_SF_BLK_MBUSY;
5145 goto end;
5146 }
5147
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005148 /* determine the first block which must not be deleted, blk_end may
5149 * be NULL if all blocks have to be deleted. also get trailers.
5150 */
5151 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005152 blk_end = NULL;
5153
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005154 hdr = 0;
5155 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005156 blk = htx_get_blk(htx, idx);
5157 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005158 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005159 if (type == HTX_BLK_UNUSED)
5160 continue;
5161
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005162 if (type == HTX_BLK_EOT) {
5163 if (idx != -1)
5164 blk_end = blk;
5165 break;
5166 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005167 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005168 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005169
5170 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5171 goto fail;
5172
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005173 list[hdr].n = htx_get_blk_name(htx, blk);
5174 list[hdr].v = htx_get_blk_value(htx, blk);
5175 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005176 }
5177
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005178 /* marker for end of trailers */
5179 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005180
Willy Tarreau9c218e72019-05-26 10:08:28 +02005181 mbuf = br_tail(h2c->mbuf);
5182 retry:
5183 if (!h2_get_buf(h2c, mbuf)) {
5184 h2c->flags |= H2_CF_MUX_MALLOC;
5185 h2s->flags |= H2_SF_BLK_MROOM;
5186 goto end;
5187 }
5188
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005189 chunk_reset(&outbuf);
5190
5191 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005192 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5193 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005194 break;
5195 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005196 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005197 }
5198
5199 if (outbuf.size < 9)
5200 goto full;
5201
5202 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5203 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5204 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5205 outbuf.data = 9;
5206
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005207 /* encode all headers */
5208 for (idx = 0; idx < hdr; idx++) {
5209 /* these ones do not exist in H2 or must not appear in
5210 * trailers and must be dropped.
5211 */
5212 if (isteq(list[idx].n, ist("host")) ||
5213 isteq(list[idx].n, ist("content-length")) ||
5214 isteq(list[idx].n, ist("connection")) ||
5215 isteq(list[idx].n, ist("proxy-connection")) ||
5216 isteq(list[idx].n, ist("keep-alive")) ||
5217 isteq(list[idx].n, ist("upgrade")) ||
5218 isteq(list[idx].n, ist("te")) ||
5219 isteq(list[idx].n, ist("transfer-encoding")))
5220 continue;
5221
5222 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5223 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005224 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005225 goto realign_again;
5226 goto full;
5227 }
5228 }
5229
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005230 if (outbuf.data == 9) {
5231 /* here we have a problem, we have nothing to emit (either we
5232 * received an empty trailers block followed or we removed its
5233 * contents above). Because of this we can't send a HEADERS
5234 * frame, so we have to cheat and instead send an empty DATA
5235 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005236 */
5237 outbuf.area[3] = H2_FT_DATA;
5238 outbuf.area[4] = H2_F_DATA_END_STREAM;
5239 }
5240
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005241 /* update the frame's size */
5242 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5243
5244 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005245 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005246 h2s->flags |= H2_SF_ES_SENT;
5247
5248 if (h2s->st == H2_SS_OPEN)
5249 h2s->st = H2_SS_HLOC;
5250 else
5251 h2s_close(h2s);
5252
5253 /* OK we could properly deliver the response */
5254 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005255 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005256 ret = 0;
5257 idx = htx_get_head(htx);
5258 blk = htx_get_blk(htx, idx);
5259 while (blk != blk_end) {
5260 ret += htx_get_blksz(blk);
5261 blk = htx_remove_blk(htx, blk);
5262 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005263
5264 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5265 ret += htx_get_blksz(blk_end);
5266 htx_remove_blk(htx, blk_end);
5267 }
5268
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005269 end:
5270 return ret;
5271 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005272 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5273 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005274 h2c->flags |= H2_CF_MUX_MFULL;
5275 h2s->flags |= H2_SF_BLK_MROOM;
5276 ret = 0;
5277 goto end;
5278 fail:
5279 /* unparsable HTX messages, too large ones to be produced in the local
5280 * list etc go here (unrecoverable errors).
5281 */
5282 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5283 ret = 0;
5284 goto end;
5285}
5286
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005287/* Called from the upper layer, to subscribe to events, such as being able to send.
5288 * The <param> argument here is supposed to be a pointer to a wait_event struct
5289 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5290 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5291 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5292 * returns 0 except for the error above.
5293 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005294static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5295{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005296 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005297 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005298 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005299
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005300 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005301 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005302 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5303 sw->events |= SUB_RETRY_RECV;
5304 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005305 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005306 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005307 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005308 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005309 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5310 sw->events |= SUB_RETRY_SEND;
5311 h2s->send_wait = sw;
5312 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5313 !LIST_ADDED(&h2s->list)) {
5314 if (h2s->flags & H2_SF_BLK_MFCTL)
5315 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5316 else
5317 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005318 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005319 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005320 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005321 if (event_type != 0)
5322 return -1;
5323 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005324}
5325
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005326/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5327 * The <param> argument here is supposed to be a pointer to the same wait_event
5328 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5329 * It always returns zero.
5330 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005331static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5332{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005333 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005334 struct h2s *h2s = cs->ctx;
5335
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005336 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005337 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005338 BUG_ON(h2s->recv_wait != sw);
5339 sw->events &= ~SUB_RETRY_RECV;
5340 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005341 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005342 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005343 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005344 BUG_ON(h2s->send_wait != sw);
5345 LIST_DEL(&h2s->list);
5346 LIST_INIT(&h2s->list);
5347 sw->events &= ~SUB_RETRY_SEND;
5348 /* We were about to send, make sure it does not happen */
5349 if (LIST_ADDED(&h2s->sending_list) &&
5350 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02005351 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02005352 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005353 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005354 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005355 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005356 return 0;
5357}
5358
5359
Olivier Houchard511efea2018-08-16 15:30:32 +02005360/* Called from the upper layer, to receive data */
5361static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5362{
Olivier Houchard638b7992018-08-16 15:41:52 +02005363 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005364 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005365 struct htx *h2s_htx = NULL;
5366 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005367 size_t ret = 0;
5368
5369 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005370 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau86724e22018-12-01 23:19:43 +01005371 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005372 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005373 /* Here htx_to_buf() will set buffer data to 0 because
5374 * the HTX is empty.
5375 */
5376 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005377 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005378 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005379
Christopher Faulet156852b2019-05-16 11:29:13 +02005380 ret = h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005381 buf_htx = htx_from_buf(buf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005382
Christopher Faulet2f6edc82019-05-15 10:07:59 +02005383 /* <buf> is empty and the message is small enough, swap the
5384 * buffers. */
5385 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
5386 htx_to_buf(buf_htx, buf);
5387 htx_to_buf(h2s_htx, &h2s->rxbuf);
5388 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5389 goto end;
5390 }
5391
Christopher Faulet156852b2019-05-16 11:29:13 +02005392 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005393
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005394 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
Willy Tarreau7196dd62019-03-05 10:51:11 +01005395 buf_htx->flags |= HTX_FL_PARSING_ERROR;
Christopher Fauleta61e97b2019-05-16 11:30:31 +02005396 if (htx_is_empty(buf_htx))
5397 cs->flags |= CS_FL_EOI;
5398 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005399
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005400 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005401 htx_to_buf(buf_htx, buf);
5402 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet156852b2019-05-16 11:29:13 +02005403 ret -= h2s_htx->data;
Willy Tarreau86724e22018-12-01 23:19:43 +01005404 }
5405 else {
5406 ret = b_xfer(buf, &h2s->rxbuf, count);
5407 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005408
Christopher Faulet37070b22019-02-14 15:12:14 +01005409 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005410 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005411 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005412 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005413 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005414 if (h2s->flags & H2_SF_ES_RCVD)
5415 cs->flags |= CS_FL_EOI;
Willy Tarreau76c83822019-06-15 09:55:50 +02005416 if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02005417 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005418 if (cs->flags & CS_FL_ERR_PENDING)
5419 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005420 if (b_size(&h2s->rxbuf)) {
5421 b_free(&h2s->rxbuf);
5422 offer_buffers(NULL, tasks_run_queue);
5423 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005424 }
5425
Willy Tarreau082f5592018-11-25 08:03:32 +01005426 if (ret && h2c->dsi == h2s->id) {
5427 /* demux is blocking on this stream's buffer */
5428 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005429 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005430 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005431
Olivier Houchard511efea2018-08-16 15:30:32 +02005432 return ret;
5433}
5434
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005435/* stops all senders of this connection for example when the mux buffer is full.
5436 * They are moved from the sending_list to either fctl_list or send_list.
5437 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005438static void h2_stop_senders(struct h2c *h2c)
5439{
5440 struct h2s *h2s, *h2s_back;
5441
Olivier Houchardd360ac62019-03-22 17:37:16 +01005442 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5443 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02005444 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005445 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005446 }
5447}
5448
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005449/* Called from the upper layer, to send data from buffer <buf> for no more than
5450 * <count> bytes. Returns the number of bytes effectively sent. Some status
5451 * flags may be updated on the conn_stream.
5452 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005453static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005454{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005455 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005456 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005457 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005458 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005459 struct htx *htx;
5460 struct htx_blk *blk;
5461 enum htx_blk_type btype;
5462 uint32_t bsize;
5463 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005464
Olivier Houchardd360ac62019-03-22 17:37:16 +01005465 /* If we were not just woken because we wanted to send but couldn't,
5466 * and there's somebody else that is waiting to send, do nothing,
5467 * we will subscribe later and be put at the end of the list
5468 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005469 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005470 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5471 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005472 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005473
Olivier Houchard998410a2019-04-15 19:23:37 +02005474 /* We couldn't set it to NULL before, because we needed it in case
5475 * we had to cancel the tasklet
5476 */
5477 h2s->send_wait = NULL;
5478
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005479 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5480 return 0;
5481
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005482 /* htx will be enough to decide if we're using HTX or legacy */
5483 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5484
Willy Tarreau0bad0432018-06-14 16:54:01 +02005485 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005486 h2s->flags |= H2_SF_OUTGOING_DATA;
5487
Willy Tarreau751f2d02018-10-05 09:35:00 +02005488 if (h2s->id == 0) {
5489 int32_t id = h2c_get_next_sid(h2s->h2c);
5490
5491 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005492 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005493 return 0;
5494 }
5495
5496 eb32_delete(&h2s->by_id);
5497 h2s->by_id.key = h2s->id = id;
5498 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005499 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005500 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5501 }
5502
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005503 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005504 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005505 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005506 idx = htx_get_head(htx);
5507 blk = htx_get_blk(htx, idx);
5508 btype = htx_get_blk_type(blk);
5509 bsize = htx_get_blksz(blk);
5510
5511 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005512 case HTX_BLK_REQ_SL:
5513 /* start-line before headers */
5514 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5515 if (ret > 0) {
5516 total += ret;
5517 count -= ret;
5518 if (ret < bsize)
5519 goto done;
5520 }
5521 break;
5522
Willy Tarreau115e83b2018-12-01 19:17:53 +01005523 case HTX_BLK_RES_SL:
5524 /* start-line before headers */
5525 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5526 if (ret > 0) {
5527 total += ret;
5528 count -= ret;
5529 if (ret < bsize)
5530 goto done;
5531 }
5532 break;
5533
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005534 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005535 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005536 /* all these cause the emission of a DATA frame (possibly empty).
5537 * This EOM necessarily is one before trailers, as the EOM following
5538 * trailers would have been consumed by the trailers parser.
5539 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005540 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005541 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005542 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005543 total += ret;
5544 count -= ret;
5545 if (ret < bsize)
5546 goto done;
5547 }
5548 break;
5549
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005550 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005551 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005552 /* This is the first trailers block, all the subsequent ones AND
5553 * the EOM will be swallowed by the parser.
5554 */
5555 ret = h2s_htx_make_trailers(h2s, htx);
5556 if (ret > 0) {
5557 total += ret;
5558 count -= ret;
5559 if (ret < bsize)
5560 goto done;
5561 }
5562 break;
5563
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005564 default:
5565 htx_remove_blk(htx, blk);
5566 total += bsize;
5567 count -= bsize;
5568 break;
5569 }
5570 }
5571 goto done;
5572 }
5573
5574 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005575 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005576 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005577 if (h2s->h2c->flags & H2_CF_IS_BACK)
5578 ret = -1;
5579 else
5580 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005581 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005582 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005583 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005584 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005585 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005586 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005587 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005588
Willy Tarreau5dd17352018-06-14 13:33:30 +02005589 if (unlikely((int)ret <= 0)) {
5590 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005591 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5592 break;
5593 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005594 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005595 total += count;
5596 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005597 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005598 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005599 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005600 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005601 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005602 break;
5603 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005604
5605 total += ret;
5606 count -= ret;
5607
Willy Tarreau2b778482019-05-06 15:00:22 +02005608 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005609 break;
5610
5611 if (h2s->flags & H2_SF_BLK_ANY)
5612 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005613 }
5614
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005615 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005616 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005617 /* trim any possibly pending data after we close (extra CR-LF,
5618 * unprocessed trailers, abnormal extra data, ...)
5619 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005620 total += count;
5621 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005622 }
5623
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005624 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005625 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005626 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005627 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005628 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005629 }
5630
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005631 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005632 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005633 } else {
5634 b_del(buf, total);
5635 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005636
5637 /* The mux is full, cancel the pending tasks */
5638 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5639 (h2s->flags & H2_SF_BLK_MBUSY))
5640 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005641
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005642 /* If we're running HTX, and we read the whole buffer, then pretend
5643 * we read exactly what the caller specified, as with HTX the caller
5644 * will always give the buffer size, instead of the amount of data
5645 * available.
5646 */
5647 if (htx && !b_data(buf))
5648 total = orig_count;
5649
Olivier Houchard7505f942018-08-21 18:10:44 +02005650 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005651 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005652 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02005653
Olivier Houchard7505f942018-08-21 18:10:44 +02005654 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005655 /* If we're waiting for flow control, and we got a shutr on the
5656 * connection, we will never be unlocked, so add an error on
5657 * the conn_stream.
5658 */
5659 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5660 !b_data(&h2s->h2c->dbuf) &&
5661 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5662 if (cs->flags & CS_FL_EOS)
5663 cs->flags |= CS_FL_ERROR;
5664 else
5665 cs->flags |= CS_FL_ERR_PENDING;
5666 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005667 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005668 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005669 LIST_DEL_INIT(&h2s->list);
5670 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005671 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005672}
5673
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005674/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005675static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005676{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005677 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005678 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005679 struct eb32_node *node;
5680 int fctl_cnt = 0;
5681 int send_cnt = 0;
5682 int tree_cnt = 0;
5683 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005684 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005685
5686 if (!h2c)
5687 return;
5688
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005689 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005690 fctl_cnt++;
5691
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005692 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005693 send_cnt++;
5694
Willy Tarreau3af37712018-12-18 14:34:41 +01005695 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005696 node = eb32_first(&h2c->streams_by_id);
5697 while (node) {
5698 h2s = container_of(node, struct h2s, by_id);
5699 tree_cnt++;
5700 if (!h2s->cs)
5701 orph_cnt++;
5702 node = eb32_next(node);
5703 }
5704
Willy Tarreau60f62682019-05-26 11:32:27 +02005705 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005706 tmbuf = br_tail(h2c->mbuf);
Willy Tarreau987c0632018-12-18 10:32:05 +01005707 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5708 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005709 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5710 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau616ac812018-07-24 14:12:42 +02005711 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5712 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005713 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005714 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5715 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5716 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005717 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5718 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5719 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005720 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5721 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005722
5723 if (h2s) {
5724 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5725 h2s, h2s->id, h2s->flags,
5726 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5727 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5728 h2s->cs);
5729 if (h2s->cs)
5730 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5731 h2s->cs->flags, h2s->cs->data);
5732 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005733}
Willy Tarreau62f52692017-10-08 23:01:42 +02005734
5735/*******************************************************/
5736/* functions below are dedicated to the config parsers */
5737/*******************************************************/
5738
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005739/* config parser for global "tune.h2.header-table-size" */
5740static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5741 struct proxy *defpx, const char *file, int line,
5742 char **err)
5743{
5744 if (too_many_args(1, args, err, NULL))
5745 return -1;
5746
5747 h2_settings_header_table_size = atoi(args[1]);
5748 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5749 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5750 return -1;
5751 }
5752 return 0;
5753}
Willy Tarreau62f52692017-10-08 23:01:42 +02005754
Willy Tarreaue6baec02017-07-27 11:45:11 +02005755/* config parser for global "tune.h2.initial-window-size" */
5756static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5757 struct proxy *defpx, const char *file, int line,
5758 char **err)
5759{
5760 if (too_many_args(1, args, err, NULL))
5761 return -1;
5762
5763 h2_settings_initial_window_size = atoi(args[1]);
5764 if (h2_settings_initial_window_size < 0) {
5765 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5766 return -1;
5767 }
5768 return 0;
5769}
5770
Willy Tarreau5242ef82017-07-27 11:47:28 +02005771/* config parser for global "tune.h2.max-concurrent-streams" */
5772static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5773 struct proxy *defpx, const char *file, int line,
5774 char **err)
5775{
5776 if (too_many_args(1, args, err, NULL))
5777 return -1;
5778
5779 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005780 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005781 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5782 return -1;
5783 }
5784 return 0;
5785}
5786
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005787/* config parser for global "tune.h2.max-frame-size" */
5788static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5789 struct proxy *defpx, const char *file, int line,
5790 char **err)
5791{
5792 if (too_many_args(1, args, err, NULL))
5793 return -1;
5794
5795 h2_settings_max_frame_size = atoi(args[1]);
5796 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5797 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5798 return -1;
5799 }
5800 return 0;
5801}
5802
Willy Tarreau62f52692017-10-08 23:01:42 +02005803
5804/****************************************/
5805/* MUX initialization and instanciation */
5806/***************************************/
5807
5808/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005809static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005810 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005811 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005812 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005813 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005814 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005815 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005816 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005817 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005818 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005819 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005820 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005821 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005822 .shutr = h2_shutr,
5823 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005824 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005825 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005826 .name = "H2",
5827};
5828
Christopher Faulet32f61c02018-04-10 14:33:41 +02005829/* PROTO selection : this mux registers PROTO token "h2" */
5830static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005831 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005832
Willy Tarreau0108d902018-11-25 19:14:37 +01005833INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5834
Christopher Faulet9b579102019-04-11 22:52:25 +02005835
5836/* The mux operations */
5837static const struct mux_ops h2_htx_ops = {
5838 .init = h2_init,
5839 .wake = h2_wake,
5840 .snd_buf = h2_snd_buf,
5841 .rcv_buf = h2_rcv_buf,
5842 .subscribe = h2_subscribe,
5843 .unsubscribe = h2_unsubscribe,
5844 .attach = h2_attach,
5845 .get_first_cs = h2_get_first_cs,
5846 .detach = h2_detach,
5847 .destroy = h2_destroy,
5848 .avail_streams = h2_avail_streams,
5849 .used_streams = h2_used_streams,
5850 .shutr = h2_shutr,
5851 .shutw = h2_shutw,
5852 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005853 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005854 .name = "H2",
5855};
5856
Willy Tarreauf8957272018-10-03 10:25:20 +02005857static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005858 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005859
5860INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5861
Willy Tarreau62f52692017-10-08 23:01:42 +02005862/* config keyword parsers */
5863static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005864 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005865 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005866 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005867 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005868 { 0, NULL, NULL }
5869}};
5870
Willy Tarreau0108d902018-11-25 19:14:37 +01005871INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);