blob: b58ba20bc0c025c1544c1046b724062a84820c80 [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
Christopher Faulet4da05472019-07-18 15:26:47 +0200180/* unused flags: 0x00000400, 0x00000800 */
Willy Tarreau454f9052017-10-26 19:40:35 +0200181
Willy Tarreau67434202017-11-06 20:20:51 +0100182#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100183#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100184
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100185#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
186
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200187#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
188#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
Willy Tarreau3cf69fe2019-05-14 10:44:40 +0200189#define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200190
191
Willy Tarreau18312642017-10-11 07:57:07 +0200192/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
193 * it is being processed in the internal HTTP representation (H1 for now).
194 */
195struct h2s {
196 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100197 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200198 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200199 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200200 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200201 int32_t id; /* stream ID */
202 uint32_t flags; /* H2_SF_* */
203 int mws; /* mux window size for this stream */
204 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
205 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200206 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100207 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200208 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200209 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 +0100210 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
211 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 +0200212 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100213 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200214};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200215
Willy Tarreauc6405142017-09-21 20:23:50 +0200216/* descriptor for an h2 frame header */
217struct h2_fh {
218 uint32_t len; /* length, host order, 24 bits */
219 uint32_t sid; /* stream id, host order, 31 bits */
220 uint8_t ft; /* frame type */
221 uint8_t ff; /* frame flags */
222};
223
Willy Tarreau8ceae722018-11-26 11:58:30 +0100224/* the h2c connection pool */
225DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
226
227/* the h2s stream pool */
228DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
229
Willy Tarreaudc572362018-12-12 08:08:05 +0100230/* The default connection window size is 65535, it may only be enlarged using
231 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
232 * we'll pretend we already received the difference between the two to send
233 * an equivalent window update to enlarge it to 2G-1.
234 */
235#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
236
Willy Tarreau455d5682019-05-24 19:42:18 +0200237/* maximum amount of data we're OK with re-aligning for buffer optimizations */
238#define MAX_DATA_REALIGN 1024
239
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200240/* a few settings from the global section */
241static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200242static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100243static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100244static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200245
Willy Tarreau2a856182017-05-16 15:20:39 +0200246/* a dmumy closed stream */
247static const struct h2s *h2_closed_stream = &(const struct h2s){
248 .cs = NULL,
249 .h2c = NULL,
250 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100251 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100252 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200253 .id = 0,
254};
255
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100256/* a dmumy closed stream returning a PROTOCOL_ERROR error */
257static const struct h2s *h2_error_stream = &(const struct h2s){
258 .cs = NULL,
259 .h2c = NULL,
260 .st = H2_SS_CLOSED,
261 .errcode = H2_ERR_PROTOCOL_ERROR,
262 .flags = 0,
263 .id = 0,
264};
265
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100266/* a dmumy closed stream returning a REFUSED_STREAM error */
267static const struct h2s *h2_refused_stream = &(const struct h2s){
268 .cs = NULL,
269 .h2c = NULL,
270 .st = H2_SS_CLOSED,
271 .errcode = H2_ERR_REFUSED_STREAM,
272 .flags = 0,
273 .id = 0,
274};
275
Willy Tarreau2a856182017-05-16 15:20:39 +0200276/* and a dummy idle stream for use with any unannounced stream */
277static const struct h2s *h2_idle_stream = &(const struct h2s){
278 .cs = NULL,
279 .h2c = NULL,
280 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100281 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200282 .id = 0,
283};
284
Olivier Houchard9f6af332018-05-25 14:04:04 +0200285static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200286static int h2_send(struct h2c *h2c);
287static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200288static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200289static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100290static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100291static 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 +0100292static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200293static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100294static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100295static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200296
Olivier Houchard7a977432019-03-21 15:47:13 +0100297static __inline int
298h2c_is_dead(struct h2c *h2c)
299{
300 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
301 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
302 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
303 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200304 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100305 (conn_xprt_read0_pending(h2c->conn) ||
306 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
307 return 1;
308
309 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100310}
311
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200312/*****************************************************/
313/* functions below are for dynamic buffer management */
314/*****************************************************/
315
Willy Tarreau315d8072017-12-10 22:17:57 +0100316/* indicates whether or not the we may call the h2_recv() function to attempt
317 * to receive data into the buffer and/or demux pending data. The condition is
318 * a bit complex due to some API limits for now. The rules are the following :
319 * - if an error or a shutdown was detected on the connection and the buffer
320 * is empty, we must not attempt to receive
321 * - if the demux buf failed to be allocated, we must not try to receive and
322 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100323 * - if no flag indicates a blocking condition, we may attempt to receive,
324 * regardless of whether the demux buffer is full or not, so that only
325 * de demux part decides whether or not to block. This is needed because
326 * the connection API indeed prevents us from re-enabling receipt that is
327 * already enabled in a polled state, so we must always immediately stop
328 * as soon as the demux can't proceed so as never to hit an end of read
329 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100330 * - otherwise must may not attempt
331 */
332static inline int h2_recv_allowed(const struct h2c *h2c)
333{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200334 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100335 (h2c->st0 >= H2_CS_ERROR ||
336 h2c->conn->flags & CO_FL_ERROR ||
337 conn_xprt_read0_pending(h2c->conn)))
338 return 0;
339
340 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100341 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100342 return 1;
343
344 return 0;
345}
346
Willy Tarreau47b515a2018-12-21 16:09:41 +0100347/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200348static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100349{
350 if (!h2_recv_allowed(h2c))
351 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200352 if ((!consider_buffer || !b_data(&h2c->dbuf))
353 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100354 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200355 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100356}
357
358
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100359/* returns true if the front connection has too many conn_streams attached */
360static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200361{
Willy Tarreaua8754662018-12-23 20:43:58 +0100362 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200363}
364
Willy Tarreau44e973f2018-03-01 17:49:30 +0100365/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
366 * flags are used to figure what buffer was requested. It returns 1 if the
367 * allocation succeeds, in which case the connection is woken up, or 0 if it's
368 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200369 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100370static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200371{
372 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100373 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200374
Willy Tarreau44e973f2018-03-01 17:49:30 +0100375 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200376 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200377 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200378 return 1;
379 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200380
Willy Tarreau51330962019-05-26 09:38:07 +0200381 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100382 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200383
384 if (h2c->flags & H2_CF_DEM_MROOM) {
385 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200386 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200387 }
Willy Tarreau14398122017-09-22 14:26:04 +0200388 return 1;
389 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100390
391 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
392 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200393 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100394 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200395 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100396 return 1;
397 }
398
Willy Tarreau14398122017-09-22 14:26:04 +0200399 return 0;
400}
401
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200402static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200403{
404 struct buffer *buf = NULL;
405
Willy Tarreauc234ae32019-05-13 17:56:11 +0200406 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100407 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
408 h2c->buf_wait.target = h2c;
409 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100410 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100411 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100412 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200413 __conn_xprt_stop_recv(h2c->conn);
414 }
415 return buf;
416}
417
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200418static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200419{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200420 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100421 b_free(bptr);
Willy Tarreau201840a2019-05-29 17:50:48 +0200422 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200423 }
424}
425
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200426static inline void h2_release_mbuf(struct h2c *h2c)
427{
428 struct buffer *buf;
429 unsigned int count = 0;
430
431 while (b_size(buf = br_head_pick(h2c->mbuf))) {
432 b_free(buf);
433 count++;
434 }
435 if (count)
Willy Tarreau201840a2019-05-29 17:50:48 +0200436 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200437}
438
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100439/* returns the number of allocatable outgoing streams for the connection taking
440 * the last_sid and the reserved ones into account.
441 */
442static inline int h2_streams_left(const struct h2c *h2c)
443{
444 int ret;
445
446 /* consider the number of outgoing streams we're allowed to create before
447 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
448 * nb_reserved is the number of streams which don't yet have an ID.
449 */
450 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
451 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
452 if (ret < 0)
453 ret = 0;
454 return ret;
455}
456
Willy Tarreau00f18a32019-01-26 12:19:01 +0100457/* returns the number of streams in use on a connection to figure if it's
458 * idle or not. We check nb_cs and not nb_streams as the caller will want
459 * to know if it was the last one after a detach().
460 */
461static int h2_used_streams(struct connection *conn)
462{
463 struct h2c *h2c = conn->ctx;
464
465 return h2c->nb_cs;
466}
467
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100468/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100469static int h2_avail_streams(struct connection *conn)
470{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100471 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100472 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100473 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100474
Willy Tarreau6afec462019-01-28 06:40:19 +0100475 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
476 * streams on the connection.
477 */
478 if (h2c->last_sid >= 0)
479 return 0;
480
Willy Tarreau86949782019-01-31 10:42:05 +0100481 /* note: may be negative if a SETTINGS frame changes the limit */
482 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100483
484 /* we must also consider the limit imposed by stream IDs */
485 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100486 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100487 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100488 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
489 ret1 = MIN(ret1, ret2);
490 }
491 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100492}
493
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200494
Willy Tarreau62f52692017-10-08 23:01:42 +0200495/*****************************************************************/
496/* functions below are dedicated to the mux setup and management */
497/*****************************************************************/
498
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200499/* Initialize the mux once it's attached. For outgoing connections, the context
500 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200501 * connections from the fact that the context is still NULL (even during mux
502 * upgrades). <input> is always used as Input buffer and may contain data. It is
503 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200504 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200505static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
506 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200507{
508 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100509 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200510
Willy Tarreaubafbe012017-11-24 17:34:44 +0100511 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200512 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200513 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200514
Christopher Faulete9b70722019-04-08 10:46:02 +0200515 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200516 h2c->flags = H2_CF_IS_BACK;
517 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
518 if (tick_isset(prx->timeout.serverfin))
519 h2c->shut_timeout = prx->timeout.serverfin;
520 } else {
521 h2c->flags = H2_CF_NONE;
522 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
523 if (tick_isset(prx->timeout.clientfin))
524 h2c->shut_timeout = prx->timeout.clientfin;
525 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100526
Willy Tarreau0b37d652018-10-03 10:33:02 +0200527 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100528 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100529 if (tick_isset(h2c->timeout)) {
530 t = task_new(tid_bit);
531 if (!t)
532 goto fail;
533
534 h2c->task = t;
535 t->process = h2_timeout_task;
536 t->context = h2c;
537 t->expire = tick_add(now_ms, h2c->timeout);
538 }
Willy Tarreauea392822017-10-31 10:02:25 +0100539
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200540 h2c->wait_event.tasklet = tasklet_new();
541 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200542 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200543 h2c->wait_event.tasklet->process = h2_io_cb;
544 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100545 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200546
Willy Tarreau32218eb2017-09-22 08:07:25 +0200547 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
548 if (!h2c->ddht)
549 goto fail;
550
551 /* Initialise the context. */
552 h2c->st0 = H2_CS_PREFACE;
553 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100554 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200555 h2c->max_id = -1;
556 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100557 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200558 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100559 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200560 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100561 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100562 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200563
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200564 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200565 h2c->dsi = -1;
566 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100567
Willy Tarreau32218eb2017-09-22 08:07:25 +0200568 h2c->last_sid = -1;
569
Willy Tarreau51330962019-05-26 09:38:07 +0200570 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200571 h2c->miw = 65535; /* mux initial window size */
572 h2c->mws = 65535; /* mux window size */
573 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200574 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200575 LIST_INIT(&h2c->send_list);
576 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200577 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100578 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200579
Willy Tarreau3f133572017-10-31 19:21:06 +0100580 if (t)
581 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100582
Willy Tarreau01b44822018-10-03 14:26:37 +0200583 if (h2c->flags & H2_CF_IS_BACK) {
584 /* FIXME: this is temporary, for outgoing connections we need
585 * to immediately allocate a stream until the code is modified
586 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100587 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200588 */
589 struct h2s *h2s;
590
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100591 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200592 if (!h2s)
593 goto fail_stream;
594 }
595
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100596 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200597
Willy Tarreau0f383582018-10-03 14:22:21 +0200598 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200599 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200600 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200601 fail_stream:
602 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200603 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200604 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200605 if (h2c->wait_event.tasklet)
606 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100607 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200608 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200609 return -1;
610}
611
Willy Tarreau751f2d02018-10-05 09:35:00 +0200612/* returns the next allocatable outgoing stream ID for the H2 connection, or
613 * -1 if no more is allocatable.
614 */
615static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
616{
617 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100618
619 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200620 id = -1;
621 return id;
622}
623
Willy Tarreau2373acc2017-10-12 17:35:14 +0200624/* returns the stream associated with id <id> or NULL if not found */
625static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
626{
627 struct eb32_node *node;
628
Willy Tarreau751f2d02018-10-05 09:35:00 +0200629 if (id == 0)
630 return (struct h2s *)h2_closed_stream;
631
Willy Tarreau2a856182017-05-16 15:20:39 +0200632 if (id > h2c->max_id)
633 return (struct h2s *)h2_idle_stream;
634
Willy Tarreau2373acc2017-10-12 17:35:14 +0200635 node = eb32_lookup(&h2c->streams_by_id, id);
636 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200637 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200638
639 return container_of(node, struct h2s, by_id);
640}
641
Christopher Faulet73c12072019-04-08 11:23:22 +0200642/* release function. This one should be called to free all resources allocated
643 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200644 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200645static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200646{
Christopher Faulet61840e72019-04-15 09:33:32 +0200647 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200648
Willy Tarreau32218eb2017-09-22 08:07:25 +0200649 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200650 /* The connection must be aattached to this mux to be released */
651 if (h2c->conn && h2c->conn->ctx == h2c)
652 conn = h2c->conn;
653
Willy Tarreau32218eb2017-09-22 08:07:25 +0200654 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200655
Willy Tarreau186e96e2019-05-28 17:21:18 +0200656 if (LIST_ADDED(&h2c->buf_wait.list)) {
657 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
658 LIST_DEL(&h2c->buf_wait.list);
659 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
660 }
Willy Tarreau14398122017-09-22 14:26:04 +0200661
Willy Tarreau44e973f2018-03-01 17:49:30 +0100662 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200663 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100664
Willy Tarreauea392822017-10-31 10:02:25 +0100665 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200666 h2c->task->context = NULL;
667 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100668 h2c->task = NULL;
669 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200670 if (h2c->wait_event.tasklet)
671 tasklet_free(h2c->wait_event.tasklet);
Olivier Houchard0e079372019-04-15 17:51:16 +0200672 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100673 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200674 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100675
Willy Tarreaubafbe012017-11-24 17:34:44 +0100676 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200677 }
678
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200679 if (conn) {
680 conn->mux = NULL;
681 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200682
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200683 conn_stop_tracking(conn);
684 conn_full_close(conn);
685 if (conn->destroy_cb)
686 conn->destroy_cb(conn);
687 conn_free(conn);
688 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200689}
690
691
Willy Tarreau71681172017-10-23 14:39:06 +0200692/******************************************************/
693/* functions below are for the H2 protocol processing */
694/******************************************************/
695
696/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100697static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200698{
699 return h2s ? h2s->id : 0;
700}
701
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200702/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100703static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200704{
705 if (h2c->msi < 0)
706 return 0;
707
708 if (h2c->msi == h2s_id(h2s))
709 return 0;
710
711 return 1;
712}
713
Willy Tarreau741d6df2017-10-17 08:00:59 +0200714/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100715static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200716{
717 h2c->errcode = err;
718 h2c->st0 = H2_CS_ERROR;
719}
720
Willy Tarreau175cebb2019-01-24 10:02:24 +0100721/* marks an error on the stream. It may also update an already closed stream
722 * (e.g. to report an error after an RST was received).
723 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100724static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200725{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100726 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200727 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100728 if (h2s->st < H2_SS_ERROR)
729 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100730 if (h2s->cs)
731 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200732 }
733}
734
Willy Tarreau7e094452018-12-19 18:08:52 +0100735/* attempt to notify the data layer of recv availability */
736static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
737{
738 struct wait_event *sw;
739
740 if (h2s->recv_wait) {
741 sw = h2s->recv_wait;
742 sw->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200743 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100744 h2s->recv_wait = NULL;
745 }
746}
747
748/* attempt to notify the data layer of send availability */
749static void __maybe_unused h2s_notify_send(struct h2s *h2s)
750{
751 struct wait_event *sw;
752
Willy Tarreauc234ae32019-05-13 17:56:11 +0200753 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100754 sw = h2s->send_wait;
755 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100756 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200757 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +0100758 }
759}
760
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100761/* alerts the data layer, trying to wake it up by all means, following
762 * this sequence :
763 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
764 * - if its subscribed to send, then it's woken up for send
765 * - if it was subscribed to neither, its ->wake() callback is called
766 * It is safe to call this function with a closed stream which doesn't have a
767 * conn_stream anymore.
768 */
769static void __maybe_unused h2s_alert(struct h2s *h2s)
770{
771 if (h2s->recv_wait || h2s->send_wait) {
772 h2s_notify_recv(h2s);
773 h2s_notify_send(h2s);
774 }
775 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
776 h2s->cs->data_cb->wake(h2s->cs);
777}
778
Willy Tarreaue4820742017-07-27 13:37:23 +0200779/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100780static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200781{
782 uint8_t *out = frame;
783
784 *out = len >> 16;
785 write_n16(out + 1, len);
786}
787
Willy Tarreau54c15062017-10-10 17:10:03 +0200788/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
789 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
790 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200791 * available in the buffer's input prior to calling this function. The buffer
792 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200793 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100794static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200795 const struct buffer *b, int o)
796{
Willy Tarreau591d4452018-06-15 17:21:00 +0200797 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 +0200798}
799
Willy Tarreau1f094672017-11-20 21:27:45 +0100800static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200801{
Willy Tarreau591d4452018-06-15 17:21:00 +0200802 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200803}
804
Willy Tarreau1f094672017-11-20 21:27:45 +0100805static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200806{
Willy Tarreau591d4452018-06-15 17:21:00 +0200807 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200808}
809
Willy Tarreau1f094672017-11-20 21:27:45 +0100810static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200811{
Willy Tarreau591d4452018-06-15 17:21:00 +0200812 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200813}
814
815
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100816/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
817 * The algorithm is not obvious. It turns out that H2 headers are neither
818 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
819 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200820 *
821 * b0 b1 b2 b3 b4 b5..b8
822 * +----------+---------+--------+----+----+----------------------+
823 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
824 * +----------+---------+--------+----+----+----------------------+
825 *
826 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
827 * we get the sid properly aligned and ordered, and 16 bits of len properly
828 * ordered as well. The type and flags can be extracted using bit shifts from
829 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200830 * Returns zero if some bytes are missing, otherwise non-zero on success. The
831 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200832 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100833static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200834{
835 uint64_t w;
836
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100837 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200838 return 0;
839
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100840 w = h2_get_n64(b, o + 1);
841 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200842 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
843 h->ff = w >> 32;
844 h->ft = w >> 40;
845 h->len += w >> 48;
846 return 1;
847}
848
849/* skip the next 9 bytes corresponding to the frame header possibly parsed by
850 * h2_peek_frame_hdr() above.
851 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100852static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200853{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200854 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200855}
856
857/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100858static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200859{
860 int ret;
861
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100862 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200863 if (ret > 0)
864 h2_skip_frame_hdr(b);
865 return ret;
866}
867
Willy Tarreau00dd0782018-03-01 16:31:34 +0100868/* marks stream <h2s> as CLOSED and decrement the number of active streams for
869 * its connection if the stream was not yet closed. Please use this exclusively
870 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100871 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100872static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100873{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100874 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100875 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100876 if (!h2s->id)
877 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100878 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +0100879 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
880 h2s_notify_recv(h2s);
881 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100882 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100883 h2s->st = H2_SS_CLOSED;
884}
885
Willy Tarreau71049cc2018-03-28 13:56:39 +0200886/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
887static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100888{
889 h2s_close(h2s);
890 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200891 if (b_size(&h2s->rxbuf)) {
892 b_free(&h2s->rxbuf);
893 offer_buffers(NULL, tasks_run_queue);
894 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200895 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100896 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200897 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100898 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800899 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200900 * reference left would be in the h2c send_list/fctl_list, and if
901 * we're in it, we're getting out anyway
902 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100903 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +0200904 if (LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau86eded62019-06-14 14:47:49 +0200905 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +0200906 LIST_DEL_INIT(&h2s->sending_list);
907 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200908 tasklet_free(h2s->wait_event.tasklet);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100909 pool_free(pool_head_h2s, h2s);
910}
911
Willy Tarreaua8e49542018-10-03 18:53:55 +0200912/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
913 * stream tree. In case of error, nothing is added and NULL is returned. The
914 * causes of errors can be any failed memory allocation. The caller is
915 * responsible for checking if the connection may support an extra stream
916 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200917 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200918static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200919{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200920 struct h2s *h2s;
921
Willy Tarreaubafbe012017-11-24 17:34:44 +0100922 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200923 if (!h2s)
924 goto out;
925
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200926 h2s->wait_event.tasklet = tasklet_new();
927 if (!h2s->wait_event.tasklet) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200928 pool_free(pool_head_h2s, h2s);
929 goto out;
930 }
931 h2s->send_wait = NULL;
932 h2s->recv_wait = NULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200933 h2s->wait_event.tasklet->process = h2_deferred_shut;
934 h2s->wait_event.tasklet->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100935 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200936 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100937 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200938 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200939 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200940 h2s->mws = h2c->miw;
941 h2s->flags = H2_SF_NONE;
942 h2s->errcode = H2_ERR_NO_ERROR;
943 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200944 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100945 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200946 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200947
948 if (h2c->flags & H2_CF_IS_BACK) {
949 h1m_init_req(&h2s->h1m);
950 h2s->h1m.err_pos = -1; // don't care about errors on the request path
951 h2s->h1m.flags |= H1_MF_TOLOWER;
952 } else {
953 h1m_init_res(&h2s->h1m);
954 h2s->h1m.err_pos = -1; // don't care about errors on the response path
955 h2s->h1m.flags |= H1_MF_TOLOWER;
956 }
957
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200958 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200959 if (id > 0)
960 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100961 else
962 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200963
964 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100965 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100966 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200967
968 return h2s;
969
970 out_free_h2s:
971 pool_free(pool_head_h2s, h2s);
972 out:
973 return NULL;
974}
975
976/* creates a new stream <id> on the h2c connection and returns it, or NULL in
977 * case of memory allocation error.
978 */
979static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
980{
981 struct session *sess = h2c->conn->owner;
982 struct conn_stream *cs;
983 struct h2s *h2s;
984
985 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
986 goto out;
987
988 h2s = h2s_new(h2c, id);
989 if (!h2s)
990 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200991
992 cs = cs_new(h2c->conn);
993 if (!cs)
994 goto out_close;
995
Olivier Houchard746fb772018-12-15 19:42:00 +0100996 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200997 h2s->cs = cs;
998 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200999 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001000
1001 if (stream_create_from_cs(cs) < 0)
1002 goto out_free_cs;
1003
Willy Tarreau590a0512018-09-05 11:56:48 +02001004 /* We want the accept date presented to the next stream to be the one
1005 * we have now, the handshake time to be null (since the next stream
1006 * is not delayed by a handshake), and the idle time to count since
1007 * right now.
1008 */
1009 sess->accept_date = date;
1010 sess->tv_accept = now;
1011 sess->t_handshake = 0;
1012
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001013 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001014 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001015 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001016 return h2s;
1017
1018 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001019 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001020 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001021 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001022 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001023 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001024 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001025 sess_log(sess);
1026 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001027}
1028
Willy Tarreau751f2d02018-10-05 09:35:00 +02001029/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1030 * and returns it, or NULL in case of memory allocation error or if the highest
1031 * possible stream ID was reached.
1032 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001033static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001034{
1035 struct h2s *h2s = NULL;
1036
Willy Tarreau86949782019-01-31 10:42:05 +01001037 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001038 goto out;
1039
Willy Tarreaua80dca82019-01-24 17:08:28 +01001040 if (h2_streams_left(h2c) < 1)
1041 goto out;
1042
Willy Tarreau751f2d02018-10-05 09:35:00 +02001043 /* Defer choosing the ID until we send the first message to create the stream */
1044 h2s = h2s_new(h2c, 0);
1045 if (!h2s)
1046 goto out;
1047
1048 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001049 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001050 cs->ctx = h2s;
1051 h2c->nb_cs++;
1052
Willy Tarreau751f2d02018-10-05 09:35:00 +02001053 out:
1054 return h2s;
1055}
1056
Willy Tarreaube5b7152017-09-25 16:25:39 +02001057/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1058 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1059 * the various settings codes.
1060 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001061static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001062{
1063 struct buffer *res;
1064 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001065 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001066 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001067 int ret;
1068
1069 if (h2c_mux_busy(h2c, NULL)) {
1070 h2c->flags |= H2_CF_DEM_MBUSY;
1071 return 0;
1072 }
1073
Willy Tarreaube5b7152017-09-25 16:25:39 +02001074 chunk_init(&buf, buf_data, sizeof(buf_data));
1075 chunk_memcpy(&buf,
1076 "\x00\x00\x00" /* length : 0 for now */
1077 "\x04\x00" /* type : 4 (settings), flags : 0 */
1078 "\x00\x00\x00\x00", /* stream ID : 0 */
1079 9);
1080
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001081 if (h2c->flags & H2_CF_IS_BACK) {
1082 /* send settings_enable_push=0 */
1083 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1084 }
1085
Willy Tarreaube5b7152017-09-25 16:25:39 +02001086 if (h2_settings_header_table_size != 4096) {
1087 char str[6] = "\x00\x01"; /* header_table_size */
1088
1089 write_n32(str + 2, h2_settings_header_table_size);
1090 chunk_memcat(&buf, str, 6);
1091 }
1092
1093 if (h2_settings_initial_window_size != 65535) {
1094 char str[6] = "\x00\x04"; /* initial_window_size */
1095
1096 write_n32(str + 2, h2_settings_initial_window_size);
1097 chunk_memcat(&buf, str, 6);
1098 }
1099
1100 if (h2_settings_max_concurrent_streams != 0) {
1101 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1102
1103 /* Note: 0 means "unlimited" for haproxy's config but not for
1104 * the protocol, so never send this value!
1105 */
1106 write_n32(str + 2, h2_settings_max_concurrent_streams);
1107 chunk_memcat(&buf, str, 6);
1108 }
1109
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001110 mfs = h2_settings_max_frame_size;
1111 if (mfs > global.tune.bufsize)
1112 mfs = global.tune.bufsize;
1113
1114 if (!mfs)
1115 mfs = global.tune.bufsize;
1116
1117 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001118 char str[6] = "\x00\x05"; /* max_frame_size */
1119
1120 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1121 * match bufsize - rewrite size, but at the moment it seems
1122 * that clients don't take care of it.
1123 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001124 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001125 chunk_memcat(&buf, str, 6);
1126 }
1127
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001128 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001129
1130 res = br_tail(h2c->mbuf);
1131 retry:
1132 if (!h2_get_buf(h2c, res)) {
1133 h2c->flags |= H2_CF_MUX_MALLOC;
1134 h2c->flags |= H2_CF_DEM_MROOM;
1135 return 0;
1136 }
1137
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001138 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001139 if (unlikely(ret <= 0)) {
1140 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001141 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1142 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001143 h2c->flags |= H2_CF_MUX_MFULL;
1144 h2c->flags |= H2_CF_DEM_MROOM;
1145 return 0;
1146 }
1147 else {
1148 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1149 return 0;
1150 }
1151 }
1152 return ret;
1153}
1154
Willy Tarreau52eed752017-09-22 15:05:09 +02001155/* Try to receive a connection preface, then upon success try to send our
1156 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1157 * missing data. It may return an error in h2c.
1158 */
1159static int h2c_frt_recv_preface(struct h2c *h2c)
1160{
1161 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001162 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001163
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001164 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001165
1166 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001167 if (ret1 < 0)
1168 sess_log(h2c->conn->owner);
1169
Willy Tarreau52eed752017-09-22 15:05:09 +02001170 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1171 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1172 return 0;
1173 }
1174
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001175 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001176 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001177 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001178
Willy Tarreaube5b7152017-09-25 16:25:39 +02001179 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001180}
1181
Willy Tarreau01b44822018-10-03 14:26:37 +02001182/* Try to send a connection preface, then upon success try to send our
1183 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1184 * missing data. It may return an error in h2c.
1185 */
1186static int h2c_bck_send_preface(struct h2c *h2c)
1187{
1188 struct buffer *res;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001189 int ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001190
1191 if (h2c_mux_busy(h2c, NULL)) {
1192 h2c->flags |= H2_CF_DEM_MBUSY;
1193 return 0;
1194 }
1195
Willy Tarreaubcc45952019-05-26 10:05:50 +02001196 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001197 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001198 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001199 h2c->flags |= H2_CF_MUX_MALLOC;
1200 h2c->flags |= H2_CF_DEM_MROOM;
1201 return 0;
1202 }
1203
1204 if (!b_data(res)) {
1205 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001206 ret = b_istput(res, ist(H2_CONN_PREFACE));
1207 if (unlikely(ret <= 0)) {
1208 if (!ret) {
1209 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1210 goto retry;
1211 h2c->flags |= H2_CF_MUX_MFULL;
1212 h2c->flags |= H2_CF_DEM_MROOM;
1213 return 0;
1214 }
1215 else {
1216 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1217 return 0;
1218 }
1219 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001220 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001221 return h2c_send_settings(h2c);
1222}
1223
Willy Tarreau081d4722017-05-16 21:51:05 +02001224/* try to send a GOAWAY frame on the connection to report an error or a graceful
1225 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1226 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1227 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1228 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1229 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1230 * on unrecoverable failure. It will not attempt to send one again in this last
1231 * case so that it is safe to use h2c_error() to report such errors.
1232 */
1233static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1234{
1235 struct buffer *res;
1236 char str[17];
1237 int ret;
1238
1239 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1240 return 1; // claim that it worked
1241
1242 if (h2c_mux_busy(h2c, h2s)) {
1243 if (h2s)
1244 h2s->flags |= H2_SF_BLK_MBUSY;
1245 else
1246 h2c->flags |= H2_CF_DEM_MBUSY;
1247 return 0;
1248 }
1249
Willy Tarreau9c218e72019-05-26 10:08:28 +02001250 /* len: 8, type: 7, flags: none, sid: 0 */
1251 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1252
1253 if (h2c->last_sid < 0)
1254 h2c->last_sid = h2c->max_id;
1255
1256 write_n32(str + 9, h2c->last_sid);
1257 write_n32(str + 13, h2c->errcode);
1258
Willy Tarreaubcc45952019-05-26 10:05:50 +02001259 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001260 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001261 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001262 h2c->flags |= H2_CF_MUX_MALLOC;
1263 if (h2s)
1264 h2s->flags |= H2_SF_BLK_MROOM;
1265 else
1266 h2c->flags |= H2_CF_DEM_MROOM;
1267 return 0;
1268 }
1269
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001270 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001271 if (unlikely(ret <= 0)) {
1272 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001273 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1274 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001275 h2c->flags |= H2_CF_MUX_MFULL;
1276 if (h2s)
1277 h2s->flags |= H2_SF_BLK_MROOM;
1278 else
1279 h2c->flags |= H2_CF_DEM_MROOM;
1280 return 0;
1281 }
1282 else {
1283 /* we cannot report this error using GOAWAY, so we mark
1284 * it and claim a success.
1285 */
1286 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1287 h2c->flags |= H2_CF_GOAWAY_FAILED;
1288 return 1;
1289 }
1290 }
1291 h2c->flags |= H2_CF_GOAWAY_SENT;
1292 return ret;
1293}
1294
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001295/* Try to send an RST_STREAM frame on the connection for the indicated stream
1296 * during mux operations. This stream must be valid and cannot be closed
1297 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1298 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1299 * not yet.
1300 *
1301 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1302 * to write the message, it subscribes the stream to future notifications.
1303 */
1304static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1305{
1306 struct buffer *res;
1307 char str[13];
1308 int ret;
1309
1310 if (!h2s || h2s->st == H2_SS_CLOSED)
1311 return 1;
1312
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001313 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1314 * RST_STREAM in response to a RST_STREAM frame.
1315 */
1316 if (h2c->dft == H2_FT_RST_STREAM) {
1317 ret = 1;
1318 goto ignore;
1319 }
1320
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001321 if (h2c_mux_busy(h2c, h2s)) {
1322 h2s->flags |= H2_SF_BLK_MBUSY;
1323 return 0;
1324 }
1325
Willy Tarreau9c218e72019-05-26 10:08:28 +02001326 /* len: 4, type: 3, flags: none */
1327 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1328 write_n32(str + 5, h2s->id);
1329 write_n32(str + 9, h2s->errcode);
1330
Willy Tarreaubcc45952019-05-26 10:05:50 +02001331 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001332 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001333 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001334 h2c->flags |= H2_CF_MUX_MALLOC;
1335 h2s->flags |= H2_SF_BLK_MROOM;
1336 return 0;
1337 }
1338
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001339 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001340 if (unlikely(ret <= 0)) {
1341 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001342 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1343 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001344 h2c->flags |= H2_CF_MUX_MFULL;
1345 h2s->flags |= H2_SF_BLK_MROOM;
1346 return 0;
1347 }
1348 else {
1349 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1350 return 0;
1351 }
1352 }
1353
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001354 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001355 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001356 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001357 return ret;
1358}
1359
1360/* Try to send an RST_STREAM frame on the connection for the stream being
1361 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001362 * error code, even if the stream is one of the dummy ones, and will update
1363 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001364 *
1365 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1366 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001367 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001368 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001369 */
1370static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1371{
1372 struct buffer *res;
1373 char str[13];
1374 int ret;
1375
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001376 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1377 * RST_STREAM in response to a RST_STREAM frame.
1378 */
1379 if (h2c->dft == H2_FT_RST_STREAM) {
1380 ret = 1;
1381 goto ignore;
1382 }
1383
Willy Tarreau27a84c92017-10-17 08:10:17 +02001384 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001385 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001386 return 0;
1387 }
1388
Willy Tarreau9c218e72019-05-26 10:08:28 +02001389 /* len: 4, type: 3, flags: none */
1390 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1391
1392 write_n32(str + 5, h2c->dsi);
1393 write_n32(str + 9, h2s->errcode);
1394
Willy Tarreaubcc45952019-05-26 10:05:50 +02001395 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001396 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001397 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001398 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001399 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001400 return 0;
1401 }
1402
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001403 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001404 if (unlikely(ret <= 0)) {
1405 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001406 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1407 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001408 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001409 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001410 return 0;
1411 }
1412 else {
1413 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1414 return 0;
1415 }
1416 }
1417
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001418 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001419 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001420 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001421 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001422 }
1423
Willy Tarreau27a84c92017-10-17 08:10:17 +02001424 return ret;
1425}
1426
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001427/* try to send an empty DATA frame with the ES flag set to notify about the
1428 * end of stream and match a shutdown(write). If an ES was already sent as
1429 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1430 * on success or zero if nothing was done. In case of lack of room to write the
1431 * message, it subscribes the requesting stream to future notifications.
1432 */
1433static int h2_send_empty_data_es(struct h2s *h2s)
1434{
1435 struct h2c *h2c = h2s->h2c;
1436 struct buffer *res;
1437 char str[9];
1438 int ret;
1439
Willy Tarreau721c9742017-11-07 11:05:42 +01001440 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001441 return 1;
1442
1443 if (h2c_mux_busy(h2c, h2s)) {
1444 h2s->flags |= H2_SF_BLK_MBUSY;
1445 return 0;
1446 }
1447
Willy Tarreau9c218e72019-05-26 10:08:28 +02001448 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1449 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1450 write_n32(str + 5, h2s->id);
1451
Willy Tarreaubcc45952019-05-26 10:05:50 +02001452 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001453 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001454 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001455 h2c->flags |= H2_CF_MUX_MALLOC;
1456 h2s->flags |= H2_SF_BLK_MROOM;
1457 return 0;
1458 }
1459
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001460 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001461 if (likely(ret > 0)) {
1462 h2s->flags |= H2_SF_ES_SENT;
1463 }
1464 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001465 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1466 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001467 h2c->flags |= H2_CF_MUX_MFULL;
1468 h2s->flags |= H2_SF_BLK_MROOM;
1469 return 0;
1470 }
1471 else {
1472 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1473 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001474 }
1475 return ret;
1476}
1477
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001478/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001479 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001480 * is automatically updated accordingly. If the stream is orphaned, it is
1481 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001482 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001483static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001484{
1485 if (!h2s->cs) {
1486 /* this stream was already orphaned */
1487 h2s_destroy(h2s);
1488 return;
1489 }
1490
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001491 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001492 if (h2s->st == H2_SS_OPEN)
1493 h2s->st = H2_SS_HREM;
1494 else if (h2s->st == H2_SS_HLOC)
1495 h2s_close(h2s);
1496 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001497
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001498 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1499 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1500 h2s->cs->flags |= CS_FL_ERR_PENDING;
1501 if (h2s->cs->flags & CS_FL_EOS)
1502 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001503
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001504 if (h2s->st < H2_SS_ERROR)
1505 h2s->st = H2_SS_ERROR;
1506 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001507
1508 h2s_alert(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001509}
1510
1511/* wake the streams attached to the connection, whose id is greater than <last>
1512 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001513 */
Willy Tarreau23482912019-05-07 15:23:14 +02001514static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001515{
1516 struct eb32_node *node;
1517 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001518
Christopher Fauletf02ca002019-03-07 16:21:34 +01001519 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001520 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1521 while (node) {
1522 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001523 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001524 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001525 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001526
Christopher Fauletf02ca002019-03-07 16:21:34 +01001527 /* Wake all streams with unassigned ID (ID == 0) */
1528 node = eb32_lookup(&h2c->streams_by_id, 0);
1529 while (node) {
1530 h2s = container_of(node, struct h2s, by_id);
1531 if (h2s->id > 0)
1532 break;
1533 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001534 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001535 }
1536}
1537
Willy Tarreau3421aba2017-07-27 15:41:03 +02001538/* Increase all streams' outgoing window size by the difference passed in
1539 * argument. This is needed upon receipt of the settings frame if the initial
1540 * window size is different. The difference may be negative and the resulting
1541 * window size as well, for the time it takes to receive some window updates.
1542 */
1543static void h2c_update_all_ws(struct h2c *h2c, int diff)
1544{
1545 struct h2s *h2s;
1546 struct eb32_node *node;
1547
1548 if (!diff)
1549 return;
1550
1551 node = eb32_first(&h2c->streams_by_id);
1552 while (node) {
1553 h2s = container_of(node, struct h2s, by_id);
1554 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001555
1556 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1557 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001558 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001559 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001560 }
1561
Willy Tarreau3421aba2017-07-27 15:41:03 +02001562 node = eb32_next(node);
1563 }
1564}
1565
1566/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1567 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001568 * return an error in h2c. The caller must have already verified frame length
1569 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001570 */
1571static int h2c_handle_settings(struct h2c *h2c)
1572{
1573 unsigned int offset;
1574 int error;
1575
1576 if (h2c->dff & H2_F_SETTINGS_ACK) {
1577 if (h2c->dfl) {
1578 error = H2_ERR_FRAME_SIZE_ERROR;
1579 goto fail;
1580 }
1581 return 1;
1582 }
1583
Willy Tarreau3421aba2017-07-27 15:41:03 +02001584 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001585 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001586 return 0;
1587
1588 /* parse the frame */
1589 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001590 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1591 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001592
1593 switch (type) {
1594 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1595 /* we need to update all existing streams with the
1596 * difference from the previous iws.
1597 */
1598 if (arg < 0) { // RFC7540#6.5.2
1599 error = H2_ERR_FLOW_CONTROL_ERROR;
1600 goto fail;
1601 }
1602 h2c_update_all_ws(h2c, arg - h2c->miw);
1603 h2c->miw = arg;
1604 break;
1605 case H2_SETTINGS_MAX_FRAME_SIZE:
1606 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1607 error = H2_ERR_PROTOCOL_ERROR;
1608 goto fail;
1609 }
1610 h2c->mfs = arg;
1611 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001612 case H2_SETTINGS_ENABLE_PUSH:
1613 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1614 error = H2_ERR_PROTOCOL_ERROR;
1615 goto fail;
1616 }
1617 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001618 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1619 if (h2c->flags & H2_CF_IS_BACK) {
1620 /* the limit is only for the backend; for the frontend it is our limit */
1621 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1622 arg = h2_settings_max_concurrent_streams;
1623 h2c->streams_limit = arg;
1624 }
1625 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001626 }
1627 }
1628
1629 /* need to ACK this frame now */
1630 h2c->st0 = H2_CS_FRAME_A;
1631 return 1;
1632 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001633 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001634 h2c_error(h2c, error);
1635 return 0;
1636}
1637
1638/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1639 * success or one of the h2_status values.
1640 */
1641static int h2c_ack_settings(struct h2c *h2c)
1642{
1643 struct buffer *res;
1644 char str[9];
1645 int ret = -1;
1646
1647 if (h2c_mux_busy(h2c, NULL)) {
1648 h2c->flags |= H2_CF_DEM_MBUSY;
1649 return 0;
1650 }
1651
Willy Tarreau9c218e72019-05-26 10:08:28 +02001652 memcpy(str,
1653 "\x00\x00\x00" /* length : 0 (no data) */
1654 "\x04" "\x01" /* type : 4, flags : ACK */
1655 "\x00\x00\x00\x00" /* stream ID */, 9);
1656
Willy Tarreaubcc45952019-05-26 10:05:50 +02001657 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001658 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001659 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02001660 h2c->flags |= H2_CF_MUX_MALLOC;
1661 h2c->flags |= H2_CF_DEM_MROOM;
1662 return 0;
1663 }
1664
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001665 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001666 if (unlikely(ret <= 0)) {
1667 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001668 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1669 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001670 h2c->flags |= H2_CF_MUX_MFULL;
1671 h2c->flags |= H2_CF_DEM_MROOM;
1672 return 0;
1673 }
1674 else {
1675 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1676 return 0;
1677 }
1678 }
1679 return ret;
1680}
1681
Willy Tarreaucf68c782017-10-10 17:11:41 +02001682/* processes a PING frame and schedules an ACK if needed. The caller must pass
1683 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001684 * missing data. The caller must have already verified frame length
1685 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001686 */
1687static int h2c_handle_ping(struct h2c *h2c)
1688{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001689 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001690 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001691 h2c->st0 = H2_CS_FRAME_A;
1692 return 1;
1693}
1694
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001695/* Try to send a window update for stream id <sid> and value <increment>.
1696 * Returns > 0 on success or zero on missing room or failure. It may return an
1697 * error in h2c.
1698 */
1699static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1700{
1701 struct buffer *res;
1702 char str[13];
1703 int ret = -1;
1704
1705 if (h2c_mux_busy(h2c, NULL)) {
1706 h2c->flags |= H2_CF_DEM_MBUSY;
1707 return 0;
1708 }
1709
Willy Tarreau9c218e72019-05-26 10:08:28 +02001710 /* length: 4, type: 8, flags: none */
1711 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1712 write_n32(str + 5, sid);
1713 write_n32(str + 9, increment);
1714
Willy Tarreaubcc45952019-05-26 10:05:50 +02001715 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001716 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001717 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001718 h2c->flags |= H2_CF_MUX_MALLOC;
1719 h2c->flags |= H2_CF_DEM_MROOM;
1720 return 0;
1721 }
1722
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001723 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001724 if (unlikely(ret <= 0)) {
1725 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001726 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1727 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001728 h2c->flags |= H2_CF_MUX_MFULL;
1729 h2c->flags |= H2_CF_DEM_MROOM;
1730 return 0;
1731 }
1732 else {
1733 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1734 return 0;
1735 }
1736 }
1737 return ret;
1738}
1739
1740/* try to send pending window update for the connection. It's safe to call it
1741 * with no pending updates. Returns > 0 on success or zero on missing room or
1742 * failure. It may return an error in h2c.
1743 */
1744static int h2c_send_conn_wu(struct h2c *h2c)
1745{
1746 int ret = 1;
1747
1748 if (h2c->rcvd_c <= 0)
1749 return 1;
1750
Willy Tarreau97aaa672018-12-23 09:49:04 +01001751 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1752 /* increase the advertised connection window to 2G on
1753 * first update.
1754 */
1755 h2c->flags |= H2_CF_WINDOW_OPENED;
1756 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1757 }
1758
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001759 /* send WU for the connection */
1760 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1761 if (ret > 0)
1762 h2c->rcvd_c = 0;
1763
1764 return ret;
1765}
1766
1767/* try to send pending window update for the current dmux stream. It's safe to
1768 * call it with no pending updates. Returns > 0 on success or zero on missing
1769 * room or failure. It may return an error in h2c.
1770 */
1771static int h2c_send_strm_wu(struct h2c *h2c)
1772{
1773 int ret = 1;
1774
1775 if (h2c->rcvd_s <= 0)
1776 return 1;
1777
1778 /* send WU for the stream */
1779 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1780 if (ret > 0)
1781 h2c->rcvd_s = 0;
1782
1783 return ret;
1784}
1785
Willy Tarreaucf68c782017-10-10 17:11:41 +02001786/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1787 * success, 0 on missing data or one of the h2_status values.
1788 */
1789static int h2c_ack_ping(struct h2c *h2c)
1790{
1791 struct buffer *res;
1792 char str[17];
1793 int ret = -1;
1794
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001795 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001796 return 0;
1797
1798 if (h2c_mux_busy(h2c, NULL)) {
1799 h2c->flags |= H2_CF_DEM_MBUSY;
1800 return 0;
1801 }
1802
Willy Tarreaucf68c782017-10-10 17:11:41 +02001803 memcpy(str,
1804 "\x00\x00\x08" /* length : 8 (same payload) */
1805 "\x06" "\x01" /* type : 6, flags : ACK */
1806 "\x00\x00\x00\x00" /* stream ID */, 9);
1807
1808 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001809 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001810
Willy Tarreau9c218e72019-05-26 10:08:28 +02001811 res = br_tail(h2c->mbuf);
1812 retry:
1813 if (!h2_get_buf(h2c, res)) {
1814 h2c->flags |= H2_CF_MUX_MALLOC;
1815 h2c->flags |= H2_CF_DEM_MROOM;
1816 return 0;
1817 }
1818
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001819 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001820 if (unlikely(ret <= 0)) {
1821 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001822 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1823 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02001824 h2c->flags |= H2_CF_MUX_MFULL;
1825 h2c->flags |= H2_CF_DEM_MROOM;
1826 return 0;
1827 }
1828 else {
1829 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1830 return 0;
1831 }
1832 }
1833 return ret;
1834}
1835
Willy Tarreau26f95952017-07-27 17:18:30 +02001836/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1837 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001838 * h2c or h2s. The caller must have already verified frame length and stream ID
1839 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001840 */
1841static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1842{
1843 int32_t inc;
1844 int error;
1845
Willy Tarreau26f95952017-07-27 17:18:30 +02001846 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001847 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001848 return 0;
1849
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001850 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001851
1852 if (h2c->dsi != 0) {
1853 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001854
1855 /* it's not an error to receive WU on a closed stream */
1856 if (h2s->st == H2_SS_CLOSED)
1857 return 1;
1858
1859 if (!inc) {
1860 error = H2_ERR_PROTOCOL_ERROR;
1861 goto strm_err;
1862 }
1863
1864 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1865 error = H2_ERR_FLOW_CONTROL_ERROR;
1866 goto strm_err;
1867 }
1868
1869 h2s->mws += inc;
1870 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1871 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001872 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Olivier Houcharddddfe312018-10-10 18:51:00 +02001873 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001874 }
1875 }
1876 else {
1877 /* connection window update */
1878 if (!inc) {
1879 error = H2_ERR_PROTOCOL_ERROR;
1880 goto conn_err;
1881 }
1882
1883 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1884 error = H2_ERR_FLOW_CONTROL_ERROR;
1885 goto conn_err;
1886 }
1887
1888 h2c->mws += inc;
1889 }
1890
1891 return 1;
1892
1893 conn_err:
1894 h2c_error(h2c, error);
1895 return 0;
1896
1897 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001898 h2s_error(h2s, error);
1899 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001900 return 0;
1901}
1902
Willy Tarreaue96b0922017-10-30 00:28:29 +01001903/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001904 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1905 * have already verified frame length and stream ID validity. Described in
1906 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001907 */
1908static int h2c_handle_goaway(struct h2c *h2c)
1909{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001910 int last;
1911
Willy Tarreaue96b0922017-10-30 00:28:29 +01001912 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001913 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001914 return 0;
1915
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001916 last = h2_get_n32(&h2c->dbuf, 0);
1917 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001918 if (h2c->last_sid < 0)
1919 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02001920 h2_wake_some_streams(h2c, last);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001921 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001922}
1923
Willy Tarreau92153fc2017-12-03 19:46:19 +01001924/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001925 * invalid. Returns > 0 on success or zero on missing data. It may return an
1926 * error in h2c. The caller must have already verified frame length and stream
1927 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001928 */
1929static int h2c_handle_priority(struct h2c *h2c)
1930{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001931 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001932 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001933 return 0;
1934
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001935 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001936 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001937 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1938 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001939 }
1940 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001941}
1942
Willy Tarreaucd234e92017-08-18 10:59:39 +02001943/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001944 * Returns > 0 on success or zero on missing data. The caller must have already
1945 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001946 */
1947static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1948{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001949 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001950 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001951 return 0;
1952
1953 /* late RST, already handled */
1954 if (h2s->st == H2_SS_CLOSED)
1955 return 1;
1956
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001957 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001958 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001959
1960 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001961 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001962 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001963 }
1964
1965 h2s->flags |= H2_SF_RST_RCVD;
1966 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001967}
1968
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001969/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1970 * It may return an error in h2c or h2s. The caller must consider that the
1971 * return value is the new h2s in case one was allocated (most common case).
1972 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001973 * errors here are reported as connection errors since it's impossible to
1974 * recover from such errors after the compression context has been altered.
1975 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001976static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001977{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001978 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001979 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001980 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001981 int error;
1982
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001983 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001984 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001985
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001986 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001987 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001988
1989 /* now either the frame is complete or the buffer is complete */
1990 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001991 /* The stream exists/existed, this must be a trailers frame */
1992 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02001993 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
1994 /* unrecoverable error ? */
1995 if (h2c->st0 >= H2_CS_ERROR)
1996 goto out;
1997
1998 if (error == 0)
1999 goto out; // missing data
2000
2001 if (error < 0) {
2002 /* Failed to decode this frame (e.g. too large request)
2003 * but the HPACK decompressor is still synchronized.
2004 */
2005 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2006 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002007 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002008 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002009 goto done;
2010 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002011 /* the connection was already killed by an RST, let's consume
2012 * the data and send another RST.
2013 */
2014 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2015 h2s = (struct h2s*)h2_error_stream;
2016 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002017 }
2018 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2019 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2020 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002021 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002022 goto conn_err;
2023 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002024 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2025 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002026
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002027 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002028
Willy Tarreau25919232019-01-03 14:48:18 +01002029 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002030 if (h2c->st0 >= H2_CS_ERROR)
2031 goto out;
2032
Willy Tarreau25919232019-01-03 14:48:18 +01002033 if (error <= 0) {
2034 if (error == 0)
2035 goto out; // missing data
2036
2037 /* Failed to decode this stream (e.g. too large request)
2038 * but the HPACK decompressor is still synchronized.
2039 */
2040 h2s = (struct h2s*)h2_error_stream;
2041 goto send_rst;
2042 }
2043
Willy Tarreau22de8d32018-09-05 19:55:58 +02002044 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002045 * positively from h2c_frt_stream_new(), the stream will report the error,
2046 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002047 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02002048 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02002049 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002050 h2s = (struct h2s*)h2_refused_stream;
2051 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002052 }
2053
2054 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002055 h2s->rxbuf = rxbuf;
2056 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002057 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002058
Willy Tarreau88d138e2019-01-02 19:38:14 +01002059 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002060 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002061 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002062
2063 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002064 if (h2s->st == H2_SS_OPEN)
2065 h2s->st = H2_SS_HREM;
2066 else
2067 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002068 }
2069
Willy Tarreau3a429f02019-01-03 11:41:50 +01002070 /* update the max stream ID if the request is being processed */
2071 if (h2s->id > h2c->max_id)
2072 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002073
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002074 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002075
2076 conn_err:
2077 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002078 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002079
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002080 out:
2081 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002082 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002083
2084 send_rst:
2085 /* make the demux send an RST for the current stream. We may only
2086 * do this if we're certain that the HEADERS frame was properly
2087 * decompressed so that the HPACK decoder is still kept up to date.
2088 */
2089 h2_release_buf(h2c, &rxbuf);
2090 h2c->st0 = H2_CS_FRAME_E;
2091 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002092}
2093
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002094/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2095 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2096 * errors here are reported as connection errors since it's impossible to
2097 * recover from such errors after the compression context has been altered.
2098 */
2099static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2100{
2101 int error;
2102
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002103 if (!b_size(&h2c->dbuf))
2104 return NULL; // empty buffer
2105
2106 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2107 return NULL; // incomplete frame
2108
Willy Tarreau1915ca22019-01-24 11:49:37 +01002109 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002110
Willy Tarreau25919232019-01-03 14:48:18 +01002111 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002112 if (h2c->st0 >= H2_CS_ERROR)
2113 return NULL;
2114
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002115 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2116 /* RFC7540#5.1 */
2117 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2118 h2c->st0 = H2_CS_FRAME_E;
2119 return NULL;
2120 }
2121
Willy Tarreau25919232019-01-03 14:48:18 +01002122 if (error <= 0) {
2123 if (error == 0)
2124 return NULL; // missing data
2125
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002126 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002127 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002128 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002129 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002130 }
2131
Christopher Fauletfa922f02019-05-07 10:55:17 +02002132 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002133 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002134
Willy Tarreau927b88b2019-03-04 08:03:25 +01002135 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002136 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002137 else if (h2s->flags & H2_SF_ES_RCVD) {
2138 if (h2s->st == H2_SS_OPEN)
2139 h2s->st = H2_SS_HREM;
2140 else if (h2s->st == H2_SS_HLOC)
2141 h2s_close(h2s);
2142 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002143
2144 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002145}
2146
Willy Tarreau454f9052017-10-26 19:40:35 +02002147/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2148 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2149 */
2150static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2151{
2152 int error;
2153
2154 /* note that empty DATA frames are perfectly valid and sometimes used
2155 * to signal an end of stream (with the ES flag).
2156 */
2157
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002158 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002159 return 0; // empty buffer
2160
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002161 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002162 return 0; // incomplete frame
2163
2164 /* now either the frame is complete or the buffer is complete */
2165
Willy Tarreau454f9052017-10-26 19:40:35 +02002166 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2167 /* RFC7540#6.1 */
2168 error = H2_ERR_STREAM_CLOSED;
2169 goto strm_err;
2170 }
2171
Christopher Faulet4f09ec82019-06-19 09:25:58 +02002172 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002173 /* RFC7540#8.1.2 */
2174 error = H2_ERR_PROTOCOL_ERROR;
2175 goto strm_err;
2176 }
2177
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002178 if (!h2_frt_transfer_data(h2s))
2179 return 0;
2180
Willy Tarreau454f9052017-10-26 19:40:35 +02002181 /* call the upper layers to process the frame, then let the upper layer
2182 * notify the stream about any change.
2183 */
2184 if (!h2s->cs) {
2185 error = H2_ERR_STREAM_CLOSED;
2186 goto strm_err;
2187 }
2188
Willy Tarreau8f650c32017-11-21 19:36:21 +01002189 if (h2c->st0 >= H2_CS_ERROR)
2190 return 0;
2191
Willy Tarreau721c9742017-11-07 11:05:42 +01002192 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002193 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002194 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002195 }
2196
2197 /* check for completion : the callee will change this to FRAME_A or
2198 * FRAME_H once done.
2199 */
2200 if (h2c->st0 == H2_CS_FRAME_P)
2201 return 0;
2202
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002203 /* last frame */
2204 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002205 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002206 if (h2s->st == H2_SS_OPEN)
2207 h2s->st = H2_SS_HREM;
2208 else
2209 h2s_close(h2s);
2210
Willy Tarreau1915ca22019-01-24 11:49:37 +01002211 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2212 /* RFC7540#8.1.2 */
2213 error = H2_ERR_PROTOCOL_ERROR;
2214 goto strm_err;
2215 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002216 }
2217
Willy Tarreau454f9052017-10-26 19:40:35 +02002218 return 1;
2219
Willy Tarreau454f9052017-10-26 19:40:35 +02002220 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002221 h2s_error(h2s, error);
2222 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002223 return 0;
2224}
2225
Willy Tarreaubc933932017-10-09 16:21:43 +02002226/* process Rx frames to be demultiplexed */
2227static void h2_process_demux(struct h2c *h2c)
2228{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002229 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002230 struct h2_fh hdr;
2231 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002232
Willy Tarreau081d4722017-05-16 21:51:05 +02002233 if (h2c->st0 >= H2_CS_ERROR)
2234 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002235
2236 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2237 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002238 if (h2c->flags & H2_CF_IS_BACK)
2239 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002240 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2241 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002242 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002243 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002244 sess_log(h2c->conn->owner);
2245 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002246 goto fail;
2247 }
2248
2249 h2c->max_id = 0;
2250 h2c->st0 = H2_CS_SETTINGS1;
2251 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002252
2253 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002254 /* ensure that what is pending is a valid SETTINGS frame
2255 * without an ACK.
2256 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002257 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002258 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002259 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002260 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002261 sess_log(h2c->conn->owner);
2262 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002263 goto fail;
2264 }
2265
2266 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2267 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2268 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2269 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002270 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002271 goto fail;
2272 }
2273
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002274 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002275 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2276 h2c_error(h2c, H2_ERR_FRAME_SIZE_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 Tarreau3bf69182018-12-21 15:34:50 +01002282 /* that's OK, switch to FRAME_P to process it. This is
2283 * a SETTINGS frame whose header has already been
2284 * deleted above.
2285 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002286 padlen = 0;
2287 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002288 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002289 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002290
2291 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002292 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002293 int ret = 0;
2294
2295 if (h2c->st0 >= H2_CS_ERROR)
2296 break;
2297
2298 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002299 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002300 break;
2301
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002302 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002303 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002304 if (!h2c->nb_streams) {
2305 /* only log if no other stream can report the error */
2306 sess_log(h2c->conn->owner);
2307 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002308 break;
2309 }
2310
Christopher Fauletdd2a5622019-06-18 12:22:38 +02002311 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002312 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2313 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2314 * we read the pad length and drop it from the remaining
2315 * payload (one byte + the 9 remaining ones = 10 total
2316 * removed), so we have a frame payload starting after the
2317 * pad len. Flow controlled frames (DATA) also count the
2318 * padlen in the flow control, so it must be adjusted.
2319 */
2320 if (hdr.len < 1) {
2321 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2322 sess_log(h2c->conn->owner);
2323 goto fail;
2324 }
2325 hdr.len--;
2326
2327 if (b_data(&h2c->dbuf) < 10)
2328 break; // missing padlen
2329
2330 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2331
2332 if (padlen > hdr.len) {
2333 /* RFC7540#6.1 : pad length = length of
2334 * frame payload or greater => error.
2335 */
2336 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2337 sess_log(h2c->conn->owner);
2338 goto fail;
2339 }
2340
2341 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2342 h2c->rcvd_c++;
2343 h2c->rcvd_s++;
2344 }
2345 b_del(&h2c->dbuf, 1);
2346 }
2347 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002348
2349 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002350 h2c->dfl = hdr.len;
2351 h2c->dsi = hdr.sid;
2352 h2c->dft = hdr.ft;
2353 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002354 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002355 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002356
2357 /* check for minimum basic frame format validity */
2358 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2359 if (ret != H2_ERR_NO_ERROR) {
2360 h2c_error(h2c, ret);
2361 sess_log(h2c->conn->owner);
2362 goto fail;
2363 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002364 }
2365
2366 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002367 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2368
Willy Tarreau567beb82018-12-18 16:52:44 +01002369 if (tmp_h2s != h2s && h2s && h2s->cs &&
2370 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002371 conn_xprt_read0_pending(h2c->conn) ||
2372 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002373 (h2s->flags & H2_SF_ES_RCVD) ||
2374 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002375 /* we may have to signal the upper layers */
2376 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002377 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002378 }
2379 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002380
Willy Tarreaud7901432017-12-29 11:34:40 +01002381 if (h2c->st0 == H2_CS_FRAME_E)
2382 goto strm_err;
2383
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002384 if (h2s->st == H2_SS_IDLE &&
2385 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2386 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2387 * this state MUST be treated as a connection error
2388 */
2389 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002390 if (!h2c->nb_streams) {
2391 /* only log if no other stream can report the error */
2392 sess_log(h2c->conn->owner);
2393 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002394 break;
2395 }
2396
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002397 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2398 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2399 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002400 * this state MUST be treated as a stream error.
2401 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2402 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002403 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002404 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2405 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2406 else
2407 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002408 goto strm_err;
2409 }
2410
Willy Tarreauab837502017-12-27 15:07:30 +01002411 /* Below the management of frames received in closed state is a
2412 * bit hackish because the spec makes strong differences between
2413 * streams closed by receiving RST, sending RST, and seeing ES
2414 * in both directions. In addition to this, the creation of a
2415 * new stream reusing the identifier of a closed one will be
2416 * detected here. Given that we cannot keep track of all closed
2417 * streams forever, we consider that unknown closed streams were
2418 * closed on RST received, which allows us to respond with an
2419 * RST without breaking the connection (eg: to abort a transfer).
2420 * Some frames have to be silently ignored as well.
2421 */
2422 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002423 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002424 /* #5.1.1: The identifier of a newly
2425 * established stream MUST be numerically
2426 * greater than all streams that the initiating
2427 * endpoint has opened or reserved. This
2428 * governs streams that are opened using a
2429 * HEADERS frame and streams that are reserved
2430 * using PUSH_PROMISE. An endpoint that
2431 * receives an unexpected stream identifier
2432 * MUST respond with a connection error.
2433 */
2434 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2435 goto strm_err;
2436 }
2437
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002438 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002439 /* RFC7540#5.1:closed: an endpoint that
2440 * receives any frame other than PRIORITY after
2441 * receiving a RST_STREAM MUST treat that as a
2442 * stream error of type STREAM_CLOSED.
2443 *
2444 * Note that old streams fall into this category
2445 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002446 *
2447 * However, we cannot generalize this to all frame types. Those
2448 * carrying compression state must still be processed before
2449 * being dropped or we'll desynchronize the decoder. This can
2450 * happen with request trailers received after sending an
2451 * RST_STREAM, or with header/trailers responses received after
2452 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002453 */
2454 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2455 h2c->st0 = H2_CS_FRAME_E;
2456 goto strm_err;
2457 }
2458
2459 /* RFC7540#5.1:closed: if this state is reached as a
2460 * result of sending a RST_STREAM frame, the peer that
2461 * receives the RST_STREAM might have already sent
2462 * frames on the stream that cannot be withdrawn. An
2463 * endpoint MUST ignore frames that it receives on
2464 * closed streams after it has sent a RST_STREAM
2465 * frame. An endpoint MAY choose to limit the period
2466 * over which it ignores frames and treat frames that
2467 * arrive after this time as being in error.
2468 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002469 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002470 /* RFC7540#5.1:closed: any frame other than
2471 * PRIO/WU/RST in this state MUST be treated as
2472 * a connection error
2473 */
2474 if (h2c->dft != H2_FT_RST_STREAM &&
2475 h2c->dft != H2_FT_PRIORITY &&
2476 h2c->dft != H2_FT_WINDOW_UPDATE) {
2477 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2478 goto strm_err;
2479 }
2480 }
2481 }
2482
Willy Tarreauc0da1962017-10-30 18:38:00 +01002483#if 0
2484 // problem below: it is not possible to completely ignore such
2485 // streams as we need to maintain the compression state as well
2486 // and for this we need to completely process these frames (eg:
2487 // HEADERS frames) as well as counting DATA frames to emit
2488 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2489 // This is a typical case of layer violation where the
2490 // transported contents are critical to the connection's
2491 // validity and must be ignored at the same time :-(
2492
2493 /* graceful shutdown, ignore streams whose ID is higher than
2494 * the one advertised in GOAWAY. RFC7540#6.8.
2495 */
2496 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002497 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2498 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002499 h2c->dfl -= ret;
2500 ret = h2c->dfl == 0;
2501 goto strm_err;
2502 }
2503#endif
2504
Willy Tarreau7e98c052017-10-10 15:56:59 +02002505 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002506 case H2_FT_SETTINGS:
2507 if (h2c->st0 == H2_CS_FRAME_P)
2508 ret = h2c_handle_settings(h2c);
2509
2510 if (h2c->st0 == H2_CS_FRAME_A)
2511 ret = h2c_ack_settings(h2c);
2512 break;
2513
Willy Tarreaucf68c782017-10-10 17:11:41 +02002514 case H2_FT_PING:
2515 if (h2c->st0 == H2_CS_FRAME_P)
2516 ret = h2c_handle_ping(h2c);
2517
2518 if (h2c->st0 == H2_CS_FRAME_A)
2519 ret = h2c_ack_ping(h2c);
2520 break;
2521
Willy Tarreau26f95952017-07-27 17:18:30 +02002522 case H2_FT_WINDOW_UPDATE:
2523 if (h2c->st0 == H2_CS_FRAME_P)
2524 ret = h2c_handle_window_update(h2c, h2s);
2525 break;
2526
Willy Tarreau61290ec2017-10-17 08:19:21 +02002527 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002528 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2529 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2530 * frames' parsers consume all following CONTINUATION
2531 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002532 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002533 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2534 sess_log(h2c->conn->owner);
2535 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002536
Willy Tarreau13278b42017-10-13 19:23:14 +02002537 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002538 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002539 if (h2c->flags & H2_CF_IS_BACK)
2540 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2541 else
2542 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002543 if (tmp_h2s) {
2544 h2s = tmp_h2s;
2545 ret = 1;
2546 }
2547 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002548 break;
2549
Willy Tarreau454f9052017-10-26 19:40:35 +02002550 case H2_FT_DATA:
2551 if (h2c->st0 == H2_CS_FRAME_P)
2552 ret = h2c_frt_handle_data(h2c, h2s);
2553
2554 if (h2c->st0 == H2_CS_FRAME_A)
2555 ret = h2c_send_strm_wu(h2c);
2556 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002557
Willy Tarreau92153fc2017-12-03 19:46:19 +01002558 case H2_FT_PRIORITY:
2559 if (h2c->st0 == H2_CS_FRAME_P)
2560 ret = h2c_handle_priority(h2c);
2561 break;
2562
Willy Tarreaucd234e92017-08-18 10:59:39 +02002563 case H2_FT_RST_STREAM:
2564 if (h2c->st0 == H2_CS_FRAME_P)
2565 ret = h2c_handle_rst_stream(h2c, h2s);
2566 break;
2567
Willy Tarreaue96b0922017-10-30 00:28:29 +01002568 case H2_FT_GOAWAY:
2569 if (h2c->st0 == H2_CS_FRAME_P)
2570 ret = h2c_handle_goaway(h2c);
2571 break;
2572
Willy Tarreau1c661982017-10-30 13:52:01 +01002573 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002574 default:
2575 /* drop frames that we ignore. They may be larger than
2576 * the buffer so we drain all of their contents until
2577 * we reach the end.
2578 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002579 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2580 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002581 h2c->dfl -= ret;
2582 ret = h2c->dfl == 0;
2583 }
2584
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002585 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002586 /* We may have to send an RST if not done yet */
2587 if (h2s->st == H2_SS_ERROR)
2588 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002589
Willy Tarreaua20a5192017-12-27 11:02:06 +01002590 if (h2c->st0 == H2_CS_FRAME_E)
2591 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002592
Willy Tarreau7e98c052017-10-10 15:56:59 +02002593 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002594 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002595 break;
2596
2597 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002598 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002599 h2c->st0 = H2_CS_FRAME_H;
2600 }
2601 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002602
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002603 if (h2c->rcvd_c > 0 &&
2604 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2605 h2c_send_conn_wu(h2c);
2606
Willy Tarreau52eed752017-09-22 15:05:09 +02002607 fail:
2608 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002609 if (h2s && h2s->cs &&
2610 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002611 conn_xprt_read0_pending(h2c->conn) ||
2612 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002613 (h2s->flags & H2_SF_ES_RCVD) ||
2614 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002615 /* we may have to signal the upper layers */
2616 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002617 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002618 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002619
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002620 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002621}
2622
2623/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2624 * the end.
2625 */
2626static int h2_process_mux(struct h2c *h2c)
2627{
Olivier Houchard998410a2019-04-15 19:23:37 +02002628 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002629
Willy Tarreau01b44822018-10-03 14:26:37 +02002630 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2631 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2632 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2633 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2634 if (h2c->st0 == H2_CS_ERROR) {
2635 h2c->st0 = H2_CS_ERROR2;
2636 sess_log(h2c->conn->owner);
2637 }
2638 goto fail;
2639 }
2640 h2c->st0 = H2_CS_SETTINGS1;
2641 }
2642 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002643 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002644 return 1;
2645 }
2646
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002647 /* start by sending possibly pending window updates */
2648 if (h2c->rcvd_c > 0 &&
2649 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2650 h2c_send_conn_wu(h2c) < 0)
2651 goto fail;
2652
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002653 /* First we always process the flow control list because the streams
2654 * waiting there were already elected for immediate emission but were
2655 * blocked just on this.
2656 */
2657
Olivier Houchard998410a2019-04-15 19:23:37 +02002658 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002659 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2660 h2c->st0 >= H2_CS_ERROR)
2661 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002662
Willy Tarreauc234ae32019-05-13 17:56:11 +02002663 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002664 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002665
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002666 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002667 /* For some reason, the upper layer failed to subsribe again,
2668 * so remove it from the send_list
2669 */
2670 if (!h2s->send_wait) {
2671 LIST_DEL_INIT(&h2s->list);
2672 continue;
2673 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002674 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002675 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002676 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002677 }
2678
Olivier Houchard998410a2019-04-15 19:23:37 +02002679 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002680 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2681 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002682
Willy Tarreauc234ae32019-05-13 17:56:11 +02002683 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002684 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002685
Olivier Houchard998410a2019-04-15 19:23:37 +02002686 /* For some reason, the upper layer failed to subsribe again,
2687 * so remove it from the send_list
2688 */
2689 if (!h2s->send_wait) {
2690 LIST_DEL_INIT(&h2s->list);
2691 continue;
2692 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002693 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002694 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002695 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002696 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002697 }
2698
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002699 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002700 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002701 if (h2c->st0 == H2_CS_ERROR) {
2702 if (h2c->max_id >= 0) {
2703 h2c_send_goaway_error(h2c, NULL);
2704 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2705 return 0;
2706 }
2707
2708 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2709 }
2710 return 1;
2711 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002712 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002713}
2714
Willy Tarreau62f52692017-10-08 23:01:42 +02002715
Willy Tarreau479998a2018-11-18 06:30:59 +01002716/* Attempt to read data, and subscribe if none available.
2717 * The function returns 1 if data has been received, otherwise zero.
2718 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002719static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002720{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002721 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002722 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002723 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002724 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002725
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002726 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002727 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002728
Willy Tarreau315d8072017-12-10 22:17:57 +01002729 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002730 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002731
Willy Tarreau44e973f2018-03-01 17:49:30 +01002732 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002733 if (!buf) {
2734 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002735 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002736 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002737
Willy Tarreau4d7a8842019-07-31 16:00:48 +02002738 b_realign_if_empty(buf);
2739 if (!b_data(buf)) {
2740 /* try to pre-align the buffer like the
2741 * rxbufs will be to optimize memory copies. We'll make
2742 * sure that the frame header lands at the end of the
2743 * HTX block to alias it upon recv. We cannot use the
2744 * head because rcv_buf() will realign the buffer if
2745 * it's empty. Thus we cheat and pretend we already
2746 * have a few bytes there.
2747 */
2748 max = buf_room_for_htx_data(buf) + 9;
2749 buf->head = sizeof(struct htx) - 9;
2750 }
2751 else
2752 max = b_room(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002753
Willy Tarreau4d7a8842019-07-31 16:00:48 +02002754 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002755
Olivier Houchard53216e72018-10-10 15:46:36 +02002756 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002757 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002758
Olivier Houcharda1411e62018-08-17 18:42:48 +02002759 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002760 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002761 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002762 }
2763
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002764 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002765 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreau4d7a8842019-07-31 16:00:48 +02002766 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002767}
2768
Willy Tarreau479998a2018-11-18 06:30:59 +01002769/* Try to send data if possible.
2770 * The function returns 1 if data have been sent, otherwise zero.
2771 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002772static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002773{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002774 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002775 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002776 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002777
2778 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002779 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002780
Olivier Houchard7505f942018-08-21 18:10:44 +02002781
Willy Tarreaua2af5122017-10-09 11:56:46 +02002782 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2783 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002784 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002785 }
2786
Willy Tarreaubc933932017-10-09 16:21:43 +02002787 /* This loop is quite simple : it tries to fill as much as it can from
2788 * pending streams into the existing buffer until it's reportedly full
2789 * or the end of send requests is reached. Then it tries to send this
2790 * buffer's contents out, marks it not full if at least one byte could
2791 * be sent, and tries again.
2792 *
2793 * The snd_buf() function normally takes a "flags" argument which may
2794 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2795 * data immediately comes and CO_SFL_STREAMER to indicate that the
2796 * connection is streaming lots of data (used to increase TLS record
2797 * size at the expense of latency). The former can be sent any time
2798 * there's a buffer full flag, as it indicates at least one stream
2799 * attempted to send and failed so there are pending data. An
2800 * alternative would be to set it as long as there's an active stream
2801 * but that would be problematic for ACKs until we have an absolute
2802 * guarantee that all waiters have at least one byte to send. The
2803 * latter should possibly not be set for now.
2804 */
2805
2806 done = 0;
2807 while (!done) {
2808 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002809 unsigned int released = 0;
2810 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02002811
2812 /* fill as much as we can into the current buffer */
2813 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2814 done = h2_process_mux(h2c);
2815
Olivier Houchard2b094432019-01-29 18:28:36 +01002816 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002817 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01002818
Willy Tarreaubc933932017-10-09 16:21:43 +02002819 if (conn->flags & CO_FL_ERROR)
2820 break;
2821
2822 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2823 flags |= CO_SFL_MSG_MORE;
2824
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002825 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
2826 if (b_data(buf)) {
2827 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002828 if (!ret) {
2829 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002830 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002831 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002832 sent = 1;
2833 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002834 if (b_data(buf)) {
2835 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002836 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002837 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002838 }
2839 b_free(buf);
2840 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02002841 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002842
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002843 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02002844 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002845
Willy Tarreaubc933932017-10-09 16:21:43 +02002846 /* wrote at least one byte, the buffer is not full anymore */
2847 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2848 }
2849
Willy Tarreaua2af5122017-10-09 11:56:46 +02002850 if (conn->flags & CO_FL_SOCK_WR_SH) {
2851 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02002852 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002853 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002854 /* We're not full anymore, so we can wake any task that are waiting
2855 * for us.
2856 */
2857 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002858 struct h2s *h2s;
2859
2860 list_for_each_entry(h2s, &h2c->send_list, list) {
2861 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2862 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002863
Willy Tarreauc234ae32019-05-13 17:56:11 +02002864 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002865 continue;
2866
Olivier Houchard998410a2019-04-15 19:23:37 +02002867 /* For some reason, the upper layer failed to subsribe again,
2868 * so remove it from the send_list
2869 */
2870 if (!h2s->send_wait) {
2871 LIST_DEL_INIT(&h2s->list);
2872 continue;
2873 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002874 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002875 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002876 tasklet_wakeup(h2s->send_wait->tasklet);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002877 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002878 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002879 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002880 /* We're done, no more to send */
Willy Tarreau662fafc2019-05-26 09:43:07 +02002881 if (!br_data(h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002882 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002883schedule:
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002884 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002885 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002886
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002887 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002888}
2889
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002890/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002891static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2892{
2893 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002894 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002895
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002896 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002897 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002898 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002899 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002900 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002901 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002902 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002903}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002904
Willy Tarreau62f52692017-10-08 23:01:42 +02002905/* callback called on any event by the connection handler.
2906 * It applies changes and returns zero, or < 0 if it wants immediate
2907 * destruction of the connection (which normally doesn not happen in h2).
2908 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002909static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002910{
Olivier Houchard7505f942018-08-21 18:10:44 +02002911 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002912
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002913 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002914 h2_process_demux(h2c);
2915
2916 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002917 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002918
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002919 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002920 h2c->flags &= ~H2_CF_DEM_DFULL;
2921 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002922 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002923
Willy Tarreau0b37d652018-10-03 10:33:02 +02002924 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002925 /* frontend is stopping, reload likely in progress, let's try
2926 * to announce a graceful shutdown if not yet done. We don't
2927 * care if it fails, it will be tried again later.
2928 */
2929 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2930 if (h2c->last_sid < 0)
2931 h2c->last_sid = (1U << 31) - 1;
2932 h2c_send_goaway_error(h2c, NULL);
2933 }
2934 }
2935
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002936 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002937 * If we received early data, and the handshake is done, wake
2938 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002939 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002940 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2941 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2942 struct eb32_node *node;
2943 struct h2s *h2s;
2944
2945 h2c->flags |= H2_CF_WAIT_FOR_HS;
2946 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2947
2948 while (node) {
2949 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002950 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002951 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002952 node = eb32_next(node);
2953 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002954 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002955
Willy Tarreau26bd7612017-10-09 16:47:04 +02002956 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002957 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2958 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2959 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02002960 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002961
2962 if (eb_is_empty(&h2c->streams_by_id)) {
2963 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002964 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002965 return -1;
2966 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002967 }
2968
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002969 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002970 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002971
Olivier Houchard53216e72018-10-10 15:46:36 +02002972 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2973 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2974 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02002975 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02002976 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2977 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02002978 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002979
Willy Tarreau3f133572017-10-31 19:21:06 +01002980 if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02002981 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2982 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01002983 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002984
Olivier Houchard7505f942018-08-21 18:10:44 +02002985 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002986 return 0;
2987}
2988
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002989/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002990static int h2_wake(struct connection *conn)
2991{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002992 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002993
2994 return (h2_process(h2c));
2995}
2996
Willy Tarreauea392822017-10-31 10:02:25 +01002997/* Connection timeout management. The principle is that if there's no receipt
2998 * nor sending for a certain amount of time, the connection is closed. If the
2999 * MUX buffer still has lying data or is not allocatable, the connection is
3000 * immediately killed. If it's allocatable and empty, we attempt to send a
3001 * GOAWAY frame.
3002 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003003static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003004{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003005 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003006 int expired = tick_is_expired(t->expire, now_ms);
3007
Willy Tarreau0975f112018-03-29 15:22:59 +02003008 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01003009 return t;
3010
Olivier Houchard3f795f72019-04-17 22:51:06 +02003011 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003012
3013 if (!h2c) {
3014 /* resources were already deleted */
3015 return NULL;
3016 }
3017
3018 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003019 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003020 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003021
Willy Tarreau662fafc2019-05-26 09:43:07 +02003022 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003023 /* don't even try to send a GOAWAY, the buffer is stuck */
3024 h2c->flags |= H2_CF_GOAWAY_FAILED;
3025 }
3026
3027 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003028 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003029 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3030 h2c->flags |= H2_CF_GOAWAY_FAILED;
3031
Willy Tarreau662fafc2019-05-26 09:43:07 +02003032 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003033 unsigned int released = 0;
3034 struct buffer *buf;
3035
3036 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3037 if (b_data(buf)) {
3038 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3039 if (!ret)
3040 break;
3041 b_del(buf, ret);
3042 if (b_data(buf))
3043 break;
3044 b_free(buf);
3045 released++;
3046 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003047 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003048
3049 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003050 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003051 }
Willy Tarreauea392822017-10-31 10:02:25 +01003052
Willy Tarreau0975f112018-03-29 15:22:59 +02003053 /* either we can release everything now or it will be done later once
3054 * the last stream closes.
3055 */
3056 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003057 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003058
Willy Tarreauea392822017-10-31 10:02:25 +01003059 return NULL;
3060}
3061
3062
Willy Tarreau62f52692017-10-08 23:01:42 +02003063/*******************************************/
3064/* functions below are used by the streams */
3065/*******************************************/
3066
3067/*
3068 * Attach a new stream to a connection
3069 * (Used for outgoing connections)
3070 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003071static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003072{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003073 struct conn_stream *cs;
3074 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003075 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003076
3077 cs = cs_new(conn);
3078 if (!cs)
3079 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003080 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003081 if (!h2s) {
3082 cs_free(cs);
3083 return NULL;
3084 }
3085 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003086}
3087
Willy Tarreaufafd3982018-11-18 21:29:20 +01003088/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3089 * We have to scan because we may have some orphan streams. It might be
3090 * beneficial to scan backwards from the end to reduce the likeliness to find
3091 * orphans.
3092 */
3093static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3094{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003095 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003096 struct h2s *h2s;
3097 struct eb32_node *node;
3098
3099 node = eb32_first(&h2c->streams_by_id);
3100 while (node) {
3101 h2s = container_of(node, struct h2s, by_id);
3102 if (h2s->cs)
3103 return h2s->cs;
3104 node = eb32_next(node);
3105 }
3106 return NULL;
3107}
3108
Willy Tarreau62f52692017-10-08 23:01:42 +02003109/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003110 * Destroy the mux and the associated connection, if it is no longer used
3111 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003112static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003113{
Christopher Faulet73c12072019-04-08 11:23:22 +02003114 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003115
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003116 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003117 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003118}
3119
3120/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003121 * Detach the stream from the connection and possibly release the connection.
3122 */
3123static void h2_detach(struct conn_stream *cs)
3124{
Willy Tarreau60935142017-10-16 18:11:19 +02003125 struct h2s *h2s = cs->ctx;
3126 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003127 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003128
3129 cs->ctx = NULL;
3130 if (!h2s)
3131 return;
3132
Olivier Houchard998410a2019-04-15 19:23:37 +02003133 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003134 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003135 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003136 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003137 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003138 /*
3139 * At this point, the stream_interface is supposed to have called
3140 * h2_unsubscribe(), so the only way there's still a
3141 * subscription that came from the stream_interface (as we
3142 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3143 * without the stream_interface involved) is that we subscribed
3144 * for sending, we woke the tasklet up and removed the
3145 * SUB_RETRY_SEND flag, so the stream_interface would not
3146 * know it has to unsubscribe for send, but the tasklet hasn't
3147 * run yet. Make sure to handle that by explicitely setting
3148 * send_wait to NULL, as nothing else will do it for us.
3149 */
3150 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003151 }
3152
Olivier Houchardf502aca2018-12-14 19:42:40 +01003153 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003154 h2c = h2s->h2c;
3155 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003156 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003157 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3158 !h2_frt_has_too_many_cs(h2c)) {
3159 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003160 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003161 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003162 }
Willy Tarreau60935142017-10-16 18:11:19 +02003163
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003164 /* this stream may be blocked waiting for some data to leave (possibly
3165 * an ES or RST frame), so orphan it in this case.
3166 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003167 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003168 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003169 (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 +01003170 return;
3171
Willy Tarreau45f752e2017-10-30 15:44:59 +01003172 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3173 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3174 /* unblock the connection if it was blocked on this
3175 * stream.
3176 */
3177 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3178 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003179 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003180 }
3181
Willy Tarreau71049cc2018-03-28 13:56:39 +02003182 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003183
Christopher Faulet9b79a102019-07-15 11:22:56 +02003184 if (h2c->flags & H2_CF_IS_BACK) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003185 if (!(h2c->conn->flags &
3186 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3187 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003188 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003189 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3190 h2c->conn->owner = NULL;
3191 if (eb_is_empty(&h2c->streams_by_id)) {
3192 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3193 /* The server doesn't want it, let's kill the connection right away */
3194 h2c->conn->mux->destroy(h2c->conn);
3195 return;
3196 }
3197 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003198 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003199 if (eb_is_empty(&h2c->streams_by_id)) {
3200 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3201 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3202 return;
3203 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003204 /* Never ever allow to reuse a connection from a non-reuse backend */
3205 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3206 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003207 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003208 struct server *srv = objt_server(h2c->conn->target);
3209
3210 if (srv) {
3211 if (h2c->conn->flags & CO_FL_PRIVATE)
3212 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3213 else
3214 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3215 }
3216
3217 }
3218 }
3219 }
3220
Willy Tarreaue323f342018-03-28 13:51:45 +02003221 /* We don't want to close right now unless we're removing the
3222 * last stream, and either the connection is in error, or it
3223 * reached the ID already specified in a GOAWAY frame received
3224 * or sent (as seen by last_sid >= 0).
3225 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003226 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003227 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003228 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003229 }
3230 else if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02003231 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3232 task_queue(h2c->task);
Willy Tarreau60935142017-10-16 18:11:19 +02003233 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003234}
3235
Willy Tarreau88bdba32019-05-13 18:17:53 +02003236/* Performs a synchronous or asynchronous shutr(). */
3237static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003238{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003239 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003240 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003241
Willy Tarreauf983d002019-05-14 10:40:21 +02003242 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003243 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003244
Willy Tarreau18059042019-01-31 19:12:48 +01003245 /* a connstream may require us to immediately kill the whole connection
3246 * for example because of a "tcp-request content reject" rule that is
3247 * normally used to limit abuse. In this case we schedule a goaway to
3248 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003249 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003250 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003251 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3252 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3253 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3254 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003255 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3256 /* Nothing was never sent for this stream, so reset with
3257 * REFUSED_STREAM error to let the client retry the
3258 * request.
3259 */
3260 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3261 }
Willy Tarreau18059042019-01-31 19:12:48 +01003262
Willy Tarreau90c32322017-11-24 08:00:30 +01003263 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003264 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003265 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003266
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003267 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003268 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003269 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003270 done:
3271 h2s->flags &= ~H2_SF_WANT_SHUTR;
3272 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003273add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003274 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003275 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003276 if (h2s->flags & H2_SF_BLK_MFCTL) {
3277 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3278 h2s->send_wait = sw;
3279 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3280 h2s->send_wait = sw;
3281 LIST_ADDQ(&h2c->send_list, &h2s->list);
3282 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003283 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003284 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003285 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003286 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003287}
3288
Willy Tarreau88bdba32019-05-13 18:17:53 +02003289/* Performs a synchronous or asynchronous shutw(). */
3290static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003291{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003292 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003293 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003294
Willy Tarreauf983d002019-05-14 10:40:21 +02003295 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003296 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003297
Willy Tarreauf983d002019-05-14 10:40:21 +02003298 if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR &&
3299 (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003300 /* we can cleanly close using an empty data frame only after headers */
3301
3302 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3303 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003304 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003305
3306 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003307 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003308 else
3309 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003310 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003311 /* a connstream may require us to immediately kill the whole connection
3312 * for example because of a "tcp-request content reject" rule that is
3313 * normally used to limit abuse. In this case we schedule a goaway to
3314 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003315 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003316 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003317 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3318 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3319 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3320 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003321 else {
3322 /* Nothing was never sent for this stream, so reset with
3323 * REFUSED_STREAM error to let the client retry the
3324 * request.
3325 */
3326 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3327 }
Willy Tarreau18059042019-01-31 19:12:48 +01003328
Willy Tarreau90c32322017-11-24 08:00:30 +01003329 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003330 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003331 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003332
Willy Tarreau00dd0782018-03-01 16:31:34 +01003333 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003334 }
3335
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003336 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003337 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003338 done:
3339 h2s->flags &= ~H2_SF_WANT_SHUTW;
3340 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003341
3342 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003343 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003344 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003345 if (h2s->flags & H2_SF_BLK_MFCTL) {
3346 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3347 h2s->send_wait = sw;
3348 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3349 h2s->send_wait = sw;
3350 LIST_ADDQ(&h2c->send_list, &h2s->list);
3351 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003352 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003353 /* let the handler know we want to shutw */
3354 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003355 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003356}
3357
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003358/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003359 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3360 * and prevented the last frame from being emitted.
3361 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003362static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3363{
3364 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003365 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003366
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003367 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003368 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003369 h2_do_shutw(h2s);
3370
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003371 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003372 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003373
Willy Tarreau88bdba32019-05-13 18:17:53 +02003374 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003375 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003376 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003377
Willy Tarreau88bdba32019-05-13 18:17:53 +02003378 if (!h2s->cs) {
3379 h2s_destroy(h2s);
3380 if (h2c_is_dead(h2c))
3381 h2_release(h2c);
3382 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003383 }
3384
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003385 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003386}
3387
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003388/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003389static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3390{
3391 struct h2s *h2s = cs->ctx;
3392
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003393 if (cs->flags & CS_FL_KILL_CONN)
3394 h2s->flags |= H2_SF_KILL_CONN;
3395
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003396 if (!mode)
3397 return;
3398
3399 h2_do_shutr(h2s);
3400}
3401
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003402/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003403static void h2_shutw(struct conn_stream *cs, enum cs_shw_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 h2_do_shutw(h2s);
3411}
3412
Christopher Faulet9b79a102019-07-15 11:22:56 +02003413/* Decode the payload of a HEADERS frame and produce the HTX request or response
3414 * depending on the connection's side. Returns a positive value on success, a
3415 * negative value on failure, or 0 if it couldn't proceed. May report connection
3416 * errors in h2c->errcode if the frame is non-decodable and the connection
3417 * unrecoverable. In absence of connection error when a failure is reported, the
3418 * caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003419 *
3420 * The function may fold CONTINUATION frames into the initial HEADERS frame
3421 * by removing padding and next frame header, then moving the CONTINUATION
3422 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3423 * leaving a hole between the main frame and the beginning of the next one.
3424 * The possibly remaining incomplete or next frame at the end may be moved
3425 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3426 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3427 *
3428 * A buffer at the beginning of processing may look like this :
3429 *
3430 * ,---.---------.-----.--------------.--------------.------.---.
3431 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3432 * `---^---------^-----^--------------^--------------^------^---'
3433 * | | <-----> | |
3434 * area | dpl | wrap
3435 * |<--------------> |
3436 * | dfl |
3437 * |<-------------------------------------------------->|
3438 * head data
3439 *
3440 * Padding is automatically overwritten when folding, participating to the
3441 * hole size after dfl :
3442 *
3443 * ,---.------------------------.-----.--------------.------.---.
3444 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3445 * `---^------------------------^-----^--------------^------^---'
3446 * | | <-----> | |
3447 * area | hole | wrap
3448 * |<-----------------------> |
3449 * | dfl |
3450 * |<-------------------------------------------------->|
3451 * head data
3452 *
3453 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3454 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3455 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003456 *
3457 * The <flags> field must point to either the stream's flags or to a copy of it
3458 * so that the function can update the following flags :
3459 * - H2_SF_DATA_CLEN when content-length is seen
3460 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3461 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003462 *
3463 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3464 * decoding, in order to detect if we're dealing with a headers or a trailers
3465 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003466 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003467static 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 +02003468{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003469 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003470 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003471 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003472 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003473 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003474 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003475 int flen; // header frame len
3476 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003477 int ret = 0;
3478 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003479 int wrap;
Willy Tarreau13278b42017-10-13 19:23:14 +02003480
Willy Tarreauea18f862018-12-22 20:19:26 +01003481next_frame:
3482 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3483 goto leave; // incomplete input frame
3484
3485 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3486 * this case, we'll try to paste it immediately after the initial
3487 * HEADERS frame payload and kill any possible padding. The initial
3488 * frame's length will be increased to represent the concatenation
3489 * of the two frames. The next frame is read from position <tlen>
3490 * and written at position <flen> (minus padding if some is present).
3491 */
3492 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3493 struct h2_fh hdr;
3494 int clen; // CONTINUATION frame's payload length
3495
3496 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3497 /* no more data, the buffer may be full, either due to
3498 * too large a frame or because of too large a hole that
3499 * we're going to compact at the end.
3500 */
3501 goto leave;
3502 }
3503
3504 if (hdr.ft != H2_FT_CONTINUATION) {
3505 /* RFC7540#6.10: frame of unexpected type */
3506 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3507 goto fail;
3508 }
3509
3510 if (hdr.sid != h2c->dsi) {
3511 /* RFC7540#6.10: frame of different stream */
3512 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3513 goto fail;
3514 }
3515
3516 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3517 /* RFC7540#4.2: invalid frame length */
3518 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3519 goto fail;
3520 }
3521
3522 /* detect when we must stop aggragating frames */
3523 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3524
3525 /* Take as much as we can of the CONTINUATION frame's payload */
3526 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3527 if (clen > hdr.len)
3528 clen = hdr.len;
3529
3530 /* Move the frame's payload over the padding, hole and frame
3531 * header. At least one of hole or dpl is null (see diagrams
3532 * above). The hole moves after the new aggragated frame.
3533 */
3534 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3535 h2c->dfl += clen - h2c->dpl;
3536 hole += h2c->dpl + 9;
3537 h2c->dpl = 0;
3538 goto next_frame;
3539 }
3540
3541 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003542
Willy Tarreau13278b42017-10-13 19:23:14 +02003543 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003544 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003545 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003546 copy = alloc_trash_chunk();
3547 if (!copy) {
3548 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3549 goto fail;
3550 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003551 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3552 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3553 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003554 }
3555
Willy Tarreau13278b42017-10-13 19:23:14 +02003556 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3557 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003558 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003559 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3560 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003561 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003562 }
3563
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003564 if (flen < 5) {
3565 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3566 goto fail;
3567 }
3568
Willy Tarreau13278b42017-10-13 19:23:14 +02003569 hdrs += 5; // stream dep = 4, weight = 1
3570 flen -= 5;
3571 }
3572
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003573 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003574 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003575 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003576 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003577
Willy Tarreau937f7602018-02-26 15:22:17 +01003578 /* we can't retry a failed decompression operation so we must be very
3579 * careful not to take any risks. In practice the output buffer is
3580 * always empty except maybe for trailers, in which case we simply have
3581 * to wait for the upper layer to finish consuming what is available.
3582 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02003583 htx = htx_from_buf(rxbuf);
3584 if (!htx_is_empty(htx)) {
3585 h2c->flags |= H2_CF_DEM_SFULL;
3586 goto leave;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003587 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003588
Willy Tarreau25919232019-01-03 14:48:18 +01003589 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003590 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3591 sizeof(list)/sizeof(list[0]), tmp);
3592 if (outlen < 0) {
3593 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3594 goto fail;
3595 }
3596
Willy Tarreau25919232019-01-03 14:48:18 +01003597 /* The PACK decompressor was updated, let's update the input buffer and
3598 * the parser's state to commit these changes and allow us to later
3599 * fail solely on the stream if needed.
3600 */
3601 b_del(&h2c->dbuf, h2c->dfl + hole);
3602 h2c->dfl = hole = 0;
3603 h2c->st0 = H2_CS_FRAME_H;
3604
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003605 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003606 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003607
Willy Tarreau88d138e2019-01-02 19:38:14 +01003608 if (*flags & H2_SF_HEADERS_RCVD)
3609 goto trailers;
3610
3611 /* This is the first HEADERS frame so it's a headers block */
Christopher Faulet9b79a102019-07-15 11:22:56 +02003612 if (h2c->flags & H2_CF_IS_BACK)
3613 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
3614 else
3615 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003616
3617 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003618 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003619 goto fail;
3620 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003621
Willy Tarreau174b06a2018-04-25 18:13:58 +02003622 if (msgf & H2_MSGF_BODY) {
3623 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003624 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003625 *flags |= H2_SF_DATA_CLEN;
Christopher Faulet9b79a102019-07-15 11:22:56 +02003626 htx->extra = *body_len;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003627 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003628 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003629 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003630 }
3631
Willy Tarreau88d138e2019-01-02 19:38:14 +01003632 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003633 /* indicate that a HEADERS frame was received for this stream, except
3634 * for 1xx responses. For 1xx responses, another HEADERS frame is
3635 * expected.
3636 */
3637 if (!(msgf & H2_MSGF_RSP_1XX))
3638 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003639
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02003640 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02003641 /* Mark the end of message using EOM */
3642 if (!htx_add_endof(htx, HTX_BLK_EOM))
3643 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003644 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003645
Willy Tarreau86277d42019-01-02 15:36:11 +01003646 /* success */
3647 ret = 1;
3648
Willy Tarreau68dd9852017-07-03 14:44:26 +02003649 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003650 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003651 * move the remaining data over it.
3652 */
3653 if (hole) {
3654 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3655 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3656 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3657 b_sub(&h2c->dbuf, hole);
3658 }
3659
Willy Tarreau97215ca2019-04-29 10:20:21 +02003660 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003661 /* too large frames */
3662 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003663 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003664 }
3665
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003666 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003667 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003668 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003669 return ret;
3670
Willy Tarreau68dd9852017-07-03 14:44:26 +02003671 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003672 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003673 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003674
3675 trailers:
3676 /* This is the last HEADERS frame hence a trailer */
Willy Tarreau88d138e2019-01-02 19:38:14 +01003677 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3678 /* It's a trailer but it's missing ES flag */
3679 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3680 goto fail;
3681 }
3682
Christopher Faulet9b79a102019-07-15 11:22:56 +02003683 /* Trailers terminate a DATA sequence */
3684 if (h2_make_htx_trailers(list, htx) <= 0)
3685 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003686 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003687}
3688
Christopher Faulet9b79a102019-07-15 11:22:56 +02003689/* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003690 * parser state is automatically updated. Returns > 0 if it could completely
3691 * send the current frame, 0 if it couldn't complete, in which case
3692 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3693 * DATA frame can return 0 as a valid result). Stream errors are reported in
3694 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3695 * have checked the frame header and ensured that the frame was complete or the
3696 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003697 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003698static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003699{
3700 struct h2c *h2c = h2s->h2c;
Christopher Faulet9b79a102019-07-15 11:22:56 +02003701 int block;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003702 unsigned int flen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003703 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003704 struct buffer *csbuf;
Christopher Faulet9b79a102019-07-15 11:22:56 +02003705 unsigned int sent;
Willy Tarreau454f9052017-10-26 19:40:35 +02003706
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003707 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003708
Olivier Houchard638b7992018-08-16 15:41:52 +02003709 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003710 if (!csbuf) {
3711 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003712 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003713 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02003714 htx = htx_from_buf(csbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003715
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003716try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003717 flen = h2c->dfl - h2c->dpl;
3718 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003719 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003720
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003721 if (flen > b_data(&h2c->dbuf)) {
3722 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003723 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003724 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003725 }
3726
Christopher Faulet9b79a102019-07-15 11:22:56 +02003727 block = htx_free_data_space(htx);
3728 if (!block) {
3729 h2c->flags |= H2_CF_DEM_SFULL;
3730 goto fail;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003731 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02003732 if (flen > block)
3733 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003734
Christopher Faulet9b79a102019-07-15 11:22:56 +02003735 /* here, flen is the max we can copy into the output buffer */
3736 block = b_contig_data(&h2c->dbuf, 0);
3737 if (flen > block)
3738 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003739
Christopher Faulet9b79a102019-07-15 11:22:56 +02003740 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau454f9052017-10-26 19:40:35 +02003741
Christopher Faulet9b79a102019-07-15 11:22:56 +02003742 b_del(&h2c->dbuf, sent);
3743 h2c->dfl -= sent;
3744 h2c->rcvd_c += sent;
3745 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau454f9052017-10-26 19:40:35 +02003746
Christopher Faulet9b79a102019-07-15 11:22:56 +02003747 if (h2s->flags & H2_SF_DATA_CLEN) {
3748 h2s->body_len -= sent;
3749 htx->extra = h2s->body_len;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003750 }
3751
Christopher Faulet9b79a102019-07-15 11:22:56 +02003752 if (sent < flen) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003753 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003754 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003755 }
3756
Christopher Faulet9b79a102019-07-15 11:22:56 +02003757 goto try_again;
3758
Willy Tarreau4a28da12018-01-04 14:41:00 +01003759 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003760 /* here we're done with the frame, all the payload (except padding) was
3761 * transferred.
3762 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003763
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003764 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02003765 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3766 h2c->flags |= H2_CF_DEM_SFULL;
3767 goto fail;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003768 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003769 }
3770
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003771 h2c->rcvd_c += h2c->dpl;
3772 h2c->rcvd_s += h2c->dpl;
3773 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003774 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet9b79a102019-07-15 11:22:56 +02003775 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003776 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003777 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003778 if (htx)
3779 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003780 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003781}
3782
Willy Tarreau115e83b2018-12-01 19:17:53 +01003783/* Try to send a HEADERS frame matching HTX response present in HTX message
3784 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3785 * must check the stream's status to detect any error which might have happened
3786 * subsequently to a successful send. The htx blocks are automatically removed
3787 * from the message. The htx message is assumed to be valid since produced from
3788 * the internal code, hence it contains a start line, an optional series of
3789 * header blocks and an end of header, otherwise an invalid frame could be
3790 * emitted and the resulting htx message could be left in an inconsistent state.
3791 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02003792static size_t h2s_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau115e83b2018-12-01 19:17:53 +01003793{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003794 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01003795 struct h2c *h2c = h2s->h2c;
3796 struct htx_blk *blk;
3797 struct htx_blk *blk_end;
3798 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02003799 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003800 struct htx_sl *sl;
3801 enum htx_blk_type type;
3802 int es_now = 0;
3803 int ret = 0;
3804 int hdr;
3805 int idx;
3806
3807 if (h2c_mux_busy(h2c, h2s)) {
3808 h2s->flags |= H2_SF_BLK_MBUSY;
3809 return 0;
3810 }
3811
Willy Tarreau115e83b2018-12-01 19:17:53 +01003812 /* determine the first block which must not be deleted, blk_end may
3813 * be NULL if all blocks have to be deleted.
3814 */
3815 idx = htx_get_head(htx);
3816 blk_end = NULL;
3817 while (idx != -1) {
3818 type = htx_get_blk_type(htx_get_blk(htx, idx));
3819 idx = htx_get_next(htx, idx);
3820 if (type == HTX_BLK_EOH) {
3821 if (idx != -1)
3822 blk_end = htx_get_blk(htx, idx);
3823 break;
3824 }
3825 }
3826
3827 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02003828 blk = htx_get_head_blk(htx);
3829 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL);
3830 ALREADY_CHECKED(blk);
3831 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003832 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01003833 if (h2s->status < 100 || h2s->status > 999)
3834 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003835
3836 /* and the rest of the headers, that we dump starting at header 0 */
3837 hdr = 0;
3838
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003839 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003840 while ((idx = htx_get_next(htx, idx)) != -1) {
3841 blk = htx_get_blk(htx, idx);
3842 type = htx_get_blk_type(blk);
3843
3844 if (type == HTX_BLK_UNUSED)
3845 continue;
3846
3847 if (type != HTX_BLK_HDR)
3848 break;
3849
3850 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3851 goto fail;
3852
3853 list[hdr].n = htx_get_blk_name(htx, blk);
3854 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003855 hdr++;
3856 }
3857
3858 /* marker for end of headers */
3859 list[hdr].n = ist("");
3860
3861 if (h2s->status == 204 || h2s->status == 304) {
3862 /* no contents, claim c-len is present and set to zero */
3863 es_now = 1;
3864 }
3865
Willy Tarreau9c218e72019-05-26 10:08:28 +02003866 mbuf = br_tail(h2c->mbuf);
3867 retry:
3868 if (!h2_get_buf(h2c, mbuf)) {
3869 h2c->flags |= H2_CF_MUX_MALLOC;
3870 h2s->flags |= H2_SF_BLK_MROOM;
3871 return 0;
3872 }
3873
Willy Tarreau115e83b2018-12-01 19:17:53 +01003874 chunk_reset(&outbuf);
3875
3876 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02003877 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
3878 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01003879 break;
3880 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02003881 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01003882 }
3883
3884 if (outbuf.size < 9)
3885 goto full;
3886
3887 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3888 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3889 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3890 outbuf.data = 9;
3891
3892 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003893 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02003894 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01003895 goto realign_again;
3896 goto full;
3897 }
3898
3899 /* encode all headers, stop at empty name */
3900 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3901 /* these ones do not exist in H2 and must be dropped. */
3902 if (isteq(list[hdr].n, ist("connection")) ||
3903 isteq(list[hdr].n, ist("proxy-connection")) ||
3904 isteq(list[hdr].n, ist("keep-alive")) ||
3905 isteq(list[hdr].n, ist("upgrade")) ||
3906 isteq(list[hdr].n, ist("transfer-encoding")))
3907 continue;
3908
3909 if (isteq(list[hdr].n, ist("")))
3910 break; // end
3911
3912 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3913 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02003914 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01003915 goto realign_again;
3916 goto full;
3917 }
3918 }
3919
Christopher Faulet0b465482019-02-19 15:14:23 +01003920 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01003921 * FIXME: we should also set it when we know for sure that the
3922 * content-length is zero as well as on 204/304
3923 */
Christopher Faulet0b465482019-02-19 15:14:23 +01003924 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
3925 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01003926 es_now = 1;
3927
Willy Tarreau927b88b2019-03-04 08:03:25 +01003928 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01003929 es_now = 1;
3930
3931 /* update the frame's size */
3932 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3933
3934 if (es_now)
3935 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3936
3937 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02003938 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01003939
3940 /* indicates the HEADERS frame was sent, except for 1xx responses. For
3941 * 1xx responses, another HEADERS frame is expected.
3942 */
3943 if (h2s->status >= 200 || h2s->status == 101)
3944 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003945
Willy Tarreau115e83b2018-12-01 19:17:53 +01003946 if (es_now) {
3947 h2s->flags |= H2_SF_ES_SENT;
3948 if (h2s->st == H2_SS_OPEN)
3949 h2s->st = H2_SS_HLOC;
3950 else
3951 h2s_close(h2s);
3952 }
3953
3954 /* OK we could properly deliver the response */
3955
3956 /* remove all header blocks including the EOH and compute the
3957 * corresponding size.
3958 *
3959 * FIXME: We should remove everything when es_now is set.
3960 */
3961 ret = 0;
3962 idx = htx_get_head(htx);
3963 blk = htx_get_blk(htx, idx);
3964 while (blk != blk_end) {
3965 ret += htx_get_blksz(blk);
3966 blk = htx_remove_blk(htx, blk);
3967 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01003968
Christopher Faulet316934d2019-05-15 14:22:48 +02003969 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
3970 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01003971 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02003972 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01003973 end:
3974 return ret;
3975 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02003976 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
3977 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003978 h2c->flags |= H2_CF_MUX_MFULL;
3979 h2s->flags |= H2_SF_BLK_MROOM;
3980 ret = 0;
3981 goto end;
3982 fail:
3983 /* unparsable HTX messages, too large ones to be produced in the local
3984 * list etc go here (unrecoverable errors).
3985 */
3986 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3987 ret = 0;
3988 goto end;
3989}
3990
Willy Tarreau80739692018-10-05 11:35:57 +02003991/* Try to send a HEADERS frame matching HTX request present in HTX message
3992 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3993 * must check the stream's status to detect any error which might have happened
3994 * subsequently to a successful send. The htx blocks are automatically removed
3995 * from the message. The htx message is assumed to be valid since produced from
3996 * the internal code, hence it contains a start line, an optional series of
3997 * header blocks and an end of header, otherwise an invalid frame could be
3998 * emitted and the resulting htx message could be left in an inconsistent state.
3999 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004000static size_t h2s_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau80739692018-10-05 11:35:57 +02004001{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004002 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004003 struct h2c *h2c = h2s->h2c;
4004 struct htx_blk *blk;
4005 struct htx_blk *blk_end;
4006 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004007 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004008 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004009 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004010 enum htx_blk_type type;
4011 int es_now = 0;
4012 int ret = 0;
4013 int hdr;
4014 int idx;
4015
4016 if (h2c_mux_busy(h2c, h2s)) {
4017 h2s->flags |= H2_SF_BLK_MBUSY;
4018 return 0;
4019 }
4020
Willy Tarreau80739692018-10-05 11:35:57 +02004021 /* determine the first block which must not be deleted, blk_end may
4022 * be NULL if all blocks have to be deleted.
4023 */
4024 idx = htx_get_head(htx);
4025 blk_end = NULL;
4026 while (idx != -1) {
4027 type = htx_get_blk_type(htx_get_blk(htx, idx));
4028 idx = htx_get_next(htx, idx);
4029 if (type == HTX_BLK_EOH) {
4030 if (idx != -1)
4031 blk_end = htx_get_blk(htx, idx);
4032 break;
4033 }
4034 }
4035
4036 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004037 blk = htx_get_head_blk(htx);
4038 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
4039 ALREADY_CHECKED(blk);
4040 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004041 meth = htx_sl_req_meth(sl);
4042 path = htx_sl_req_uri(sl);
4043
4044 /* and the rest of the headers, that we dump starting at header 0 */
4045 hdr = 0;
4046
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004047 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004048 while ((idx = htx_get_next(htx, idx)) != -1) {
4049 blk = htx_get_blk(htx, idx);
4050 type = htx_get_blk_type(blk);
4051
4052 if (type == HTX_BLK_UNUSED)
4053 continue;
4054
4055 if (type != HTX_BLK_HDR)
4056 break;
4057
4058 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4059 goto fail;
4060
4061 list[hdr].n = htx_get_blk_name(htx, blk);
4062 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004063 hdr++;
4064 }
4065
4066 /* marker for end of headers */
4067 list[hdr].n = ist("");
4068
Willy Tarreau9c218e72019-05-26 10:08:28 +02004069 mbuf = br_tail(h2c->mbuf);
4070 retry:
4071 if (!h2_get_buf(h2c, mbuf)) {
4072 h2c->flags |= H2_CF_MUX_MALLOC;
4073 h2s->flags |= H2_SF_BLK_MROOM;
4074 return 0;
4075 }
4076
Willy Tarreau80739692018-10-05 11:35:57 +02004077 chunk_reset(&outbuf);
4078
4079 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004080 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4081 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004082 break;
4083 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004084 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004085 }
4086
4087 if (outbuf.size < 9)
4088 goto full;
4089
4090 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4091 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4092 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4093 outbuf.data = 9;
4094
4095 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004096 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004097 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004098 goto realign_again;
4099 goto full;
4100 }
4101
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004102 /* RFC7540 #8.3: the CONNECT method must have :
4103 * - :authority set to the URI part (host:port)
4104 * - :method set to CONNECT
4105 * - :scheme and :path omitted
4106 */
4107 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4108 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004109 struct ist scheme;
4110
4111 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4112 scheme = ist("http");
4113 else
4114 scheme = ist("https");
4115
4116 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004117 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004118 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004119 goto realign_again;
4120 goto full;
4121 }
Willy Tarreau80739692018-10-05 11:35:57 +02004122
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004123 /* encode the path, which necessarily is the second one */
4124 if (!hpack_encode_path(&outbuf, path)) {
4125 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004126 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004127 goto realign_again;
4128 goto full;
4129 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004130
4131 /* look for the Host header and place it in :authority */
4132 auth = ist2(NULL, 0);
4133 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4134 if (isteq(list[hdr].n, ist("")))
4135 break; // end
4136
4137 if (isteq(list[hdr].n, ist("host"))) {
4138 auth = list[hdr].v;
4139 break;
4140 }
4141 }
4142 }
4143 else {
4144 /* for CONNECT, :authority is taken from the path */
4145 auth = path;
4146 }
4147
4148 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4149 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004150 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004151 goto realign_again;
4152 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004153 }
4154
4155 /* encode all headers, stop at empty name */
4156 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4157 /* these ones do not exist in H2 and must be dropped. */
4158 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004159 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004160 isteq(list[hdr].n, ist("proxy-connection")) ||
4161 isteq(list[hdr].n, ist("keep-alive")) ||
4162 isteq(list[hdr].n, ist("upgrade")) ||
4163 isteq(list[hdr].n, ist("transfer-encoding")))
4164 continue;
4165
4166 if (isteq(list[hdr].n, ist("")))
4167 break; // end
4168
4169 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4170 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004171 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004172 goto realign_again;
4173 goto full;
4174 }
4175 }
4176
4177 /* we may need to add END_STREAM if we have no body :
4178 * - request already closed, or :
4179 * - no transfer-encoding, and :
4180 * - no content-length or content-length:0
4181 * Fixme: this doesn't take into account CONNECT requests.
4182 */
4183 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4184 es_now = 1;
4185
4186 if (sl->flags & HTX_SL_F_BODYLESS)
4187 es_now = 1;
4188
Willy Tarreau927b88b2019-03-04 08:03:25 +01004189 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004190 es_now = 1;
4191
4192 /* update the frame's size */
4193 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4194
4195 if (es_now)
4196 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4197
4198 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004199 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004200 h2s->flags |= H2_SF_HEADERS_SENT;
4201 h2s->st = H2_SS_OPEN;
4202
Willy Tarreau80739692018-10-05 11:35:57 +02004203 if (es_now) {
4204 // trim any possibly pending data (eg: inconsistent content-length)
4205 h2s->flags |= H2_SF_ES_SENT;
4206 h2s->st = H2_SS_HLOC;
4207 }
4208
4209 /* remove all header blocks including the EOH and compute the
4210 * corresponding size.
4211 *
4212 * FIXME: We should remove everything when es_now is set.
4213 */
4214 ret = 0;
4215 idx = htx_get_head(htx);
4216 blk = htx_get_blk(htx, idx);
4217 while (blk != blk_end) {
4218 ret += htx_get_blksz(blk);
4219 blk = htx_remove_blk(htx, blk);
4220 }
4221
Christopher Faulet316934d2019-05-15 14:22:48 +02004222 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4223 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004224 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004225 }
Willy Tarreau80739692018-10-05 11:35:57 +02004226
4227 end:
4228 return ret;
4229 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004230 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4231 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004232 h2c->flags |= H2_CF_MUX_MFULL;
4233 h2s->flags |= H2_SF_BLK_MROOM;
4234 ret = 0;
4235 goto end;
4236 fail:
4237 /* unparsable HTX messages, too large ones to be produced in the local
4238 * list etc go here (unrecoverable errors).
4239 */
4240 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4241 ret = 0;
4242 goto end;
4243}
4244
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004245/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004246 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4247 * caller must check the stream's status to detect any error which might have
4248 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02004249 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004250 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004251static size_t h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004252{
4253 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004254 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004255 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004256 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004257 size_t total = 0;
4258 int es_now = 0;
4259 int bsize; /* htx block size */
4260 int fsize; /* h2 frame size */
4261 struct htx_blk *blk;
4262 enum htx_blk_type type;
4263 int idx;
4264
4265 if (h2c_mux_busy(h2c, h2s)) {
4266 h2s->flags |= H2_SF_BLK_MBUSY;
4267 goto end;
4268 }
4269
Willy Tarreau98de12a2018-12-12 07:03:00 +01004270 htx = htx_from_buf(buf);
4271
Christopher Faulet54b5e212019-06-04 10:08:28 +02004272 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
4273 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
4274 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004275 */
4276
4277 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004278 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004279 goto end;
4280
4281 idx = htx_get_head(htx);
4282 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02004283 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004284 bsize = htx_get_blksz(blk);
4285 fsize = bsize;
4286
Christopher Faulet54b5e212019-06-04 10:08:28 +02004287 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004288 if (h2s->flags & H2_SF_ES_SENT) {
4289 /* ES already sent */
4290 htx_remove_blk(htx, blk);
4291 total++; // EOM counts as one byte
4292 count--;
4293 goto end;
4294 }
4295 }
4296 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004297 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004298
Willy Tarreau9c218e72019-05-26 10:08:28 +02004299 mbuf = br_tail(h2c->mbuf);
4300 retry:
4301 if (!h2_get_buf(h2c, mbuf)) {
4302 h2c->flags |= H2_CF_MUX_MALLOC;
4303 h2s->flags |= H2_SF_BLK_MROOM;
4304 goto end;
4305 }
4306
Willy Tarreau98de12a2018-12-12 07:03:00 +01004307 /* Perform some optimizations to reduce the number of buffer copies.
4308 * First, if the mux's buffer is empty and the htx area contains
4309 * exactly one data block of the same size as the requested count, and
4310 * this count fits within the frame size, the stream's window size, and
4311 * the connection's window size, then it's possible to simply swap the
4312 * caller's buffer with the mux's output buffer and adjust offsets and
4313 * length to match the entire DATA HTX block in the middle. In this
4314 * case we perform a true zero-copy operation from end-to-end. This is
4315 * the situation that happens all the time with large files. Second, if
4316 * this is not possible, but the mux's output buffer is empty, we still
4317 * have an opportunity to avoid the copy to the intermediary buffer, by
4318 * making the intermediary buffer's area point to the output buffer's
4319 * area. In this case we want to skip the HTX header to make sure that
4320 * copies remain aligned and that this operation remains possible all
4321 * the time. This goes for headers, data blocks and any data extracted
4322 * from the HTX blocks.
4323 */
4324 if (unlikely(fsize == count &&
Christopher Faulet192c6a22019-06-11 16:32:24 +02004325 htx_nbblks(htx) == 1 && type == HTX_BLK_DATA &&
Willy Tarreau98de12a2018-12-12 07:03:00 +01004326 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004327 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004328
Willy Tarreaubcc45952019-05-26 10:05:50 +02004329 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004330 /* Too bad there are data left there. We're willing to memcpy/memmove
4331 * up to 1/4 of the buffer, which means that it's OK to copy a large
4332 * frame into a buffer containing few data if it needs to be realigned,
4333 * and that it's also OK to copy few data without realigning. Otherwise
4334 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004335 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004336 if (fsize + 9 <= b_room(mbuf) &&
4337 (b_data(mbuf) <= b_size(mbuf) / 4 ||
4338 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004339 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004340
Willy Tarreau9c218e72019-05-26 10:08:28 +02004341 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4342 goto retry;
4343
Willy Tarreau98de12a2018-12-12 07:03:00 +01004344 h2c->flags |= H2_CF_MUX_MFULL;
4345 h2s->flags |= H2_SF_BLK_MROOM;
4346 goto end;
4347 }
4348
4349 /* map an H2 frame to the HTX block so that we can put the
4350 * frame header there.
4351 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004352 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
4353 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01004354
4355 /* prepend an H2 DATA frame header just before the DATA block */
4356 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4357 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4358 h2_set_frame_size(outbuf.area, fsize);
4359
4360 /* update windows */
4361 h2s->mws -= fsize;
4362 h2c->mws -= fsize;
4363
4364 /* and exchange with our old area */
4365 buf->area = old_area;
4366 buf->data = buf->head = 0;
4367 total += fsize;
4368 goto end;
4369 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004370
Willy Tarreau98de12a2018-12-12 07:03:00 +01004371 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004372 /* for DATA and EOM we'll have to emit a frame, even if empty */
4373
4374 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004375 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4376 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004377 break;
4378 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004379 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004380 }
4381
4382 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004383 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4384 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004385 h2c->flags |= H2_CF_MUX_MFULL;
4386 h2s->flags |= H2_SF_BLK_MROOM;
4387 goto end;
4388 }
4389
4390 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4391 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4392 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4393 outbuf.data = 9;
4394
4395 /* we have in <fsize> the exact number of bytes we need to copy from
4396 * the HTX buffer. We need to check this against the connection's and
4397 * the stream's send windows, and to ensure that this fits in the max
4398 * frame size and in the buffer's available space minus 9 bytes (for
4399 * the frame header). The connection's flow control is applied last so
4400 * that we can use a separate list of streams which are immediately
4401 * unblocked on window opening. Note: we don't implement padding.
4402 */
4403
4404 /* EOM is presented with bsize==1 but would lead to the emission of an
4405 * empty frame, thus we force it to zero here.
4406 */
4407 if (type == HTX_BLK_EOM)
4408 bsize = fsize = 0;
4409
4410 if (!fsize)
4411 goto send_empty;
4412
4413 if (h2s->mws <= 0) {
4414 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004415 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004416 LIST_DEL_INIT(&h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004417 goto end;
4418 }
4419
Willy Tarreauee573762018-12-04 15:25:57 +01004420 if (fsize > count)
4421 fsize = count;
4422
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004423 if (fsize > h2s->mws)
4424 fsize = h2s->mws; // >0
4425
4426 if (h2c->mfs && fsize > h2c->mfs)
4427 fsize = h2c->mfs; // >0
4428
4429 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02004430 /* It doesn't fit at once. If it at least fits once split and
4431 * the amount of data to move is low, let's defragment the
4432 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004433 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004434 if (b_space_wraps(mbuf) &&
4435 (fsize + 9 <= b_room(mbuf)) &&
4436 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004437 goto realign_again;
4438 fsize = outbuf.size - 9;
4439
4440 if (fsize <= 0) {
4441 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02004442 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4443 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004444 h2c->flags |= H2_CF_MUX_MFULL;
4445 h2s->flags |= H2_SF_BLK_MROOM;
4446 goto end;
4447 }
4448 }
4449
4450 if (h2c->mws <= 0) {
4451 h2s->flags |= H2_SF_BLK_MFCTL;
4452 goto end;
4453 }
4454
4455 if (fsize > h2c->mws)
4456 fsize = h2c->mws;
4457
4458 /* now let's copy this this into the output buffer */
4459 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004460 h2s->mws -= fsize;
4461 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004462 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004463
4464 send_empty:
4465 /* update the frame's size */
4466 h2_set_frame_size(outbuf.area, fsize);
4467
4468 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4469 * meeting EOM. We should optimize this later.
4470 */
4471 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004472 total++; // EOM counts as one byte
4473 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004474 es_now = 1;
4475 }
4476
4477 if (es_now)
4478 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4479
4480 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004481 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004482
4483 /* consume incoming HTX block, including EOM */
4484 total += fsize;
4485 if (fsize == bsize) {
4486 htx_remove_blk(htx, blk);
4487 if (fsize)
4488 goto new_frame;
4489 } else {
4490 /* we've truncated this block */
4491 htx_cut_data_blk(htx, blk, fsize);
4492 }
4493
4494 if (es_now) {
4495 if (h2s->st == H2_SS_OPEN)
4496 h2s->st = H2_SS_HLOC;
4497 else
4498 h2s_close(h2s);
4499
4500 h2s->flags |= H2_SF_ES_SENT;
4501 }
4502
4503 end:
4504 return total;
4505}
4506
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004507/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4508 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4509 * processed. The caller must check the stream's status to detect any error
4510 * which might have happened subsequently to a successful send. The htx blocks
4511 * are automatically removed from the message. The htx message is assumed to be
4512 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02004513 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004514 * as a single frame. The ES flag is always set.
4515 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004516static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004517{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004518 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004519 struct h2c *h2c = h2s->h2c;
4520 struct htx_blk *blk;
4521 struct htx_blk *blk_end;
4522 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004523 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004524 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004525 int ret = 0;
4526 int hdr;
4527 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004528
4529 if (h2c_mux_busy(h2c, h2s)) {
4530 h2s->flags |= H2_SF_BLK_MBUSY;
4531 goto end;
4532 }
4533
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004534 /* determine the first block which must not be deleted, blk_end may
4535 * be NULL if all blocks have to be deleted. also get trailers.
4536 */
4537 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004538 blk_end = NULL;
4539
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004540 hdr = 0;
4541 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004542 blk = htx_get_blk(htx, idx);
4543 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004544 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004545 if (type == HTX_BLK_UNUSED)
4546 continue;
4547
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004548 if (type == HTX_BLK_EOT) {
4549 if (idx != -1)
4550 blk_end = blk;
4551 break;
4552 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02004553 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004554 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004555
4556 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4557 goto fail;
4558
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004559 list[hdr].n = htx_get_blk_name(htx, blk);
4560 list[hdr].v = htx_get_blk_value(htx, blk);
4561 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004562 }
4563
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004564 /* marker for end of trailers */
4565 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004566
Willy Tarreau9c218e72019-05-26 10:08:28 +02004567 mbuf = br_tail(h2c->mbuf);
4568 retry:
4569 if (!h2_get_buf(h2c, mbuf)) {
4570 h2c->flags |= H2_CF_MUX_MALLOC;
4571 h2s->flags |= H2_SF_BLK_MROOM;
4572 goto end;
4573 }
4574
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004575 chunk_reset(&outbuf);
4576
4577 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004578 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4579 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004580 break;
4581 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004582 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004583 }
4584
4585 if (outbuf.size < 9)
4586 goto full;
4587
4588 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4589 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4590 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4591 outbuf.data = 9;
4592
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004593 /* encode all headers */
4594 for (idx = 0; idx < hdr; idx++) {
4595 /* these ones do not exist in H2 or must not appear in
4596 * trailers and must be dropped.
4597 */
4598 if (isteq(list[idx].n, ist("host")) ||
4599 isteq(list[idx].n, ist("content-length")) ||
4600 isteq(list[idx].n, ist("connection")) ||
4601 isteq(list[idx].n, ist("proxy-connection")) ||
4602 isteq(list[idx].n, ist("keep-alive")) ||
4603 isteq(list[idx].n, ist("upgrade")) ||
4604 isteq(list[idx].n, ist("te")) ||
4605 isteq(list[idx].n, ist("transfer-encoding")))
4606 continue;
4607
4608 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
4609 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004610 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004611 goto realign_again;
4612 goto full;
4613 }
4614 }
4615
Willy Tarreau5121e5d2019-05-06 15:13:41 +02004616 if (outbuf.data == 9) {
4617 /* here we have a problem, we have nothing to emit (either we
4618 * received an empty trailers block followed or we removed its
4619 * contents above). Because of this we can't send a HEADERS
4620 * frame, so we have to cheat and instead send an empty DATA
4621 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01004622 */
4623 outbuf.area[3] = H2_FT_DATA;
4624 outbuf.area[4] = H2_F_DATA_END_STREAM;
4625 }
4626
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004627 /* update the frame's size */
4628 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4629
4630 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004631 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004632 h2s->flags |= H2_SF_ES_SENT;
4633
4634 if (h2s->st == H2_SS_OPEN)
4635 h2s->st = H2_SS_HLOC;
4636 else
4637 h2s_close(h2s);
4638
4639 /* OK we could properly deliver the response */
4640 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02004641 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004642 ret = 0;
4643 idx = htx_get_head(htx);
4644 blk = htx_get_blk(htx, idx);
4645 while (blk != blk_end) {
4646 ret += htx_get_blksz(blk);
4647 blk = htx_remove_blk(htx, blk);
4648 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004649
4650 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4651 ret += htx_get_blksz(blk_end);
4652 htx_remove_blk(htx, blk_end);
4653 }
4654
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004655 end:
4656 return ret;
4657 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004658 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4659 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004660 h2c->flags |= H2_CF_MUX_MFULL;
4661 h2s->flags |= H2_SF_BLK_MROOM;
4662 ret = 0;
4663 goto end;
4664 fail:
4665 /* unparsable HTX messages, too large ones to be produced in the local
4666 * list etc go here (unrecoverable errors).
4667 */
4668 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4669 ret = 0;
4670 goto end;
4671}
4672
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004673/* Called from the upper layer, to subscribe to events, such as being able to send.
4674 * The <param> argument here is supposed to be a pointer to a wait_event struct
4675 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
4676 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
4677 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
4678 * returns 0 except for the error above.
4679 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02004680static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4681{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004682 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004683 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004684 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004685
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004686 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004687 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02004688 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
4689 sw->events |= SUB_RETRY_RECV;
4690 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004691 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004692 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004693 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004694 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02004695 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
4696 sw->events |= SUB_RETRY_SEND;
4697 h2s->send_wait = sw;
4698 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
4699 !LIST_ADDED(&h2s->list)) {
4700 if (h2s->flags & H2_SF_BLK_MFCTL)
4701 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4702 else
4703 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004704 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004705 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004706 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004707 if (event_type != 0)
4708 return -1;
4709 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004710}
4711
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004712/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
4713 * The <param> argument here is supposed to be a pointer to the same wait_event
4714 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
4715 * It always returns zero.
4716 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004717static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4718{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004719 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004720 struct h2s *h2s = cs->ctx;
4721
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004722 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004723 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02004724 BUG_ON(h2s->recv_wait != sw);
4725 sw->events &= ~SUB_RETRY_RECV;
4726 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004727 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004728 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004729 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02004730 BUG_ON(h2s->send_wait != sw);
4731 LIST_DEL(&h2s->list);
4732 LIST_INIT(&h2s->list);
4733 sw->events &= ~SUB_RETRY_SEND;
4734 /* We were about to send, make sure it does not happen */
4735 if (LIST_ADDED(&h2s->sending_list) &&
4736 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02004737 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02004738 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02004739 }
Olivier Houchardf8338152019-05-14 17:50:32 +02004740 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02004741 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004742 return 0;
4743}
4744
4745
Olivier Houchard511efea2018-08-16 15:30:32 +02004746/* Called from the upper layer, to receive data */
4747static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4748{
Olivier Houchard638b7992018-08-16 15:41:52 +02004749 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004750 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004751 struct htx *h2s_htx = NULL;
4752 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02004753 size_t ret = 0;
4754
4755 /* transfer possibly pending data to the upper layer */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004756 h2s_htx = htx_from_buf(&h2s->rxbuf);
4757 if (htx_is_empty(h2s_htx)) {
4758 /* Here htx_to_buf() will set buffer data to 0 because
4759 * the HTX is empty.
4760 */
4761 htx_to_buf(h2s_htx, &h2s->rxbuf);
4762 goto end;
4763 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01004764
Christopher Faulet9b79a102019-07-15 11:22:56 +02004765 ret = h2s_htx->data;
4766 buf_htx = htx_from_buf(buf);
Willy Tarreau7196dd62019-03-05 10:51:11 +01004767
Christopher Faulet9b79a102019-07-15 11:22:56 +02004768 /* <buf> is empty and the message is small enough, swap the
4769 * buffers. */
4770 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004771 htx_to_buf(buf_htx, buf);
4772 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004773 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
4774 goto end;
Willy Tarreau86724e22018-12-01 23:19:43 +01004775 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004776
4777 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
4778
4779 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
4780 buf_htx->flags |= HTX_FL_PARSING_ERROR;
4781 if (htx_is_empty(buf_htx))
4782 cs->flags |= CS_FL_EOI;
Willy Tarreau86724e22018-12-01 23:19:43 +01004783 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004784
Christopher Faulet9b79a102019-07-15 11:22:56 +02004785 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
4786 htx_to_buf(buf_htx, buf);
4787 htx_to_buf(h2s_htx, &h2s->rxbuf);
4788 ret -= h2s_htx->data;
4789
Christopher Faulet37070b22019-02-14 15:12:14 +01004790 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02004791 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004792 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004793 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004794 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02004795 if (h2s->flags & H2_SF_ES_RCVD)
4796 cs->flags |= CS_FL_EOI;
Willy Tarreau76c83822019-06-15 09:55:50 +02004797 if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02004798 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004799 if (cs->flags & CS_FL_ERR_PENDING)
4800 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02004801 if (b_size(&h2s->rxbuf)) {
4802 b_free(&h2s->rxbuf);
4803 offer_buffers(NULL, tasks_run_queue);
4804 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004805 }
4806
Willy Tarreau082f5592018-11-25 08:03:32 +01004807 if (ret && h2c->dsi == h2s->id) {
4808 /* demux is blocking on this stream's buffer */
4809 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004810 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01004811 }
Christopher Faulet37070b22019-02-14 15:12:14 +01004812
Olivier Houchard511efea2018-08-16 15:30:32 +02004813 return ret;
4814}
4815
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004816/* stops all senders of this connection for example when the mux buffer is full.
4817 * They are moved from the sending_list to either fctl_list or send_list.
4818 */
Olivier Houchardd846c262018-10-19 17:24:29 +02004819static void h2_stop_senders(struct h2c *h2c)
4820{
4821 struct h2s *h2s, *h2s_back;
4822
Olivier Houchardd360ac62019-03-22 17:37:16 +01004823 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
4824 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02004825 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004826 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02004827 }
4828}
4829
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004830/* Called from the upper layer, to send data from buffer <buf> for no more than
4831 * <count> bytes. Returns the number of bytes effectively sent. Some status
4832 * flags may be updated on the conn_stream.
4833 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004834static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004835{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004836 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004837 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004838 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004839 struct htx *htx;
4840 struct htx_blk *blk;
4841 enum htx_blk_type btype;
4842 uint32_t bsize;
4843 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004844
Olivier Houchardd360ac62019-03-22 17:37:16 +01004845 /* If we were not just woken because we wanted to send but couldn't,
4846 * and there's somebody else that is waiting to send, do nothing,
4847 * we will subscribe later and be put at the end of the list
4848 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02004849 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01004850 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
4851 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02004852 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01004853
Olivier Houchard998410a2019-04-15 19:23:37 +02004854 /* We couldn't set it to NULL before, because we needed it in case
4855 * we had to cancel the tasklet
4856 */
4857 h2s->send_wait = NULL;
4858
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004859 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4860 return 0;
4861
Christopher Faulet9b79a102019-07-15 11:22:56 +02004862 htx = htx_from_buf(buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004863
Willy Tarreau0bad0432018-06-14 16:54:01 +02004864 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004865 h2s->flags |= H2_SF_OUTGOING_DATA;
4866
Willy Tarreau751f2d02018-10-05 09:35:00 +02004867 if (h2s->id == 0) {
4868 int32_t id = h2c_get_next_sid(h2s->h2c);
4869
4870 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02004871 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02004872 return 0;
4873 }
4874
4875 eb32_delete(&h2s->by_id);
4876 h2s->by_id.key = h2s->id = id;
4877 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01004878 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02004879 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4880 }
4881
Christopher Faulet9b79a102019-07-15 11:22:56 +02004882 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
4883 count && !htx_is_empty(htx)) {
4884 idx = htx_get_head(htx);
4885 blk = htx_get_blk(htx, idx);
4886 btype = htx_get_blk_type(blk);
4887 bsize = htx_get_blksz(blk);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004888
Christopher Faulet9b79a102019-07-15 11:22:56 +02004889 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004890 case HTX_BLK_REQ_SL:
4891 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004892 ret = h2s_bck_make_req_headers(h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02004893 if (ret > 0) {
4894 total += ret;
4895 count -= ret;
4896 if (ret < bsize)
4897 goto done;
4898 }
4899 break;
4900
Willy Tarreau115e83b2018-12-01 19:17:53 +01004901 case HTX_BLK_RES_SL:
4902 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004903 ret = h2s_frt_make_resp_headers(h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004904 if (ret > 0) {
4905 total += ret;
4906 count -= ret;
4907 if (ret < bsize)
4908 goto done;
4909 }
4910 break;
4911
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004912 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004913 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004914 /* all these cause the emission of a DATA frame (possibly empty).
4915 * This EOM necessarily is one before trailers, as the EOM following
4916 * trailers would have been consumed by the trailers parser.
4917 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004918 ret = h2s_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004919 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004920 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004921 total += ret;
4922 count -= ret;
4923 if (ret < bsize)
4924 goto done;
4925 }
4926 break;
4927
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004928 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004929 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004930 /* This is the first trailers block, all the subsequent ones AND
4931 * the EOM will be swallowed by the parser.
4932 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004933 ret = h2s_make_trailers(h2s, htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004934 if (ret > 0) {
4935 total += ret;
4936 count -= ret;
4937 if (ret < bsize)
4938 goto done;
4939 }
4940 break;
4941
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004942 default:
4943 htx_remove_blk(htx, blk);
4944 total += bsize;
4945 count -= bsize;
4946 break;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004947 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004948 }
4949
Christopher Faulet9b79a102019-07-15 11:22:56 +02004950 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02004951 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02004952 /* trim any possibly pending data after we close (extra CR-LF,
4953 * unprocessed trailers, abnormal extra data, ...)
4954 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004955 total += count;
4956 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004957 }
4958
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004959 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004960 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01004961 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004962 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004963 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004964 }
4965
Christopher Faulet9b79a102019-07-15 11:22:56 +02004966 htx_to_buf(htx, buf);
Olivier Houchardd846c262018-10-19 17:24:29 +02004967
4968 /* The mux is full, cancel the pending tasks */
4969 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4970 (h2s->flags & H2_SF_BLK_MBUSY))
4971 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004972
Olivier Houchard7505f942018-08-21 18:10:44 +02004973 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004974 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004975 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02004976
Olivier Houchard7505f942018-08-21 18:10:44 +02004977 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01004978 /* If we're waiting for flow control, and we got a shutr on the
4979 * connection, we will never be unlocked, so add an error on
4980 * the conn_stream.
4981 */
4982 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
4983 !b_data(&h2s->h2c->dbuf) &&
4984 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
4985 if (cs->flags & CS_FL_EOS)
4986 cs->flags |= CS_FL_ERROR;
4987 else
4988 cs->flags |= CS_FL_ERR_PENDING;
4989 }
Olivier Houchard998410a2019-04-15 19:23:37 +02004990 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01004991 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01004992 LIST_DEL_INIT(&h2s->list);
4993 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004994 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004995}
4996
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004997/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004998static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004999{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005000 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005001 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005002 struct eb32_node *node;
5003 int fctl_cnt = 0;
5004 int send_cnt = 0;
5005 int tree_cnt = 0;
5006 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005007 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005008
5009 if (!h2c)
5010 return;
5011
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005012 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005013 fctl_cnt++;
5014
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005015 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005016 send_cnt++;
5017
Willy Tarreau3af37712018-12-18 14:34:41 +01005018 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005019 node = eb32_first(&h2c->streams_by_id);
5020 while (node) {
5021 h2s = container_of(node, struct h2s, by_id);
5022 tree_cnt++;
5023 if (!h2s->cs)
5024 orph_cnt++;
5025 node = eb32_next(node);
5026 }
5027
Willy Tarreau60f62682019-05-26 11:32:27 +02005028 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005029 tmbuf = br_tail(h2c->mbuf);
Willy Tarreau987c0632018-12-18 10:32:05 +01005030 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5031 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005032 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5033 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau616ac812018-07-24 14:12:42 +02005034 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5035 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005036 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005037 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5038 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5039 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005040 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5041 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5042 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005043 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5044 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005045
5046 if (h2s) {
5047 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5048 h2s, h2s->id, h2s->flags,
5049 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5050 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5051 h2s->cs);
5052 if (h2s->cs)
5053 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5054 h2s->cs->flags, h2s->cs->data);
5055 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005056}
Willy Tarreau62f52692017-10-08 23:01:42 +02005057
5058/*******************************************************/
5059/* functions below are dedicated to the config parsers */
5060/*******************************************************/
5061
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005062/* config parser for global "tune.h2.header-table-size" */
5063static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5064 struct proxy *defpx, const char *file, int line,
5065 char **err)
5066{
5067 if (too_many_args(1, args, err, NULL))
5068 return -1;
5069
5070 h2_settings_header_table_size = atoi(args[1]);
5071 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5072 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5073 return -1;
5074 }
5075 return 0;
5076}
Willy Tarreau62f52692017-10-08 23:01:42 +02005077
Willy Tarreaue6baec02017-07-27 11:45:11 +02005078/* config parser for global "tune.h2.initial-window-size" */
5079static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5080 struct proxy *defpx, const char *file, int line,
5081 char **err)
5082{
5083 if (too_many_args(1, args, err, NULL))
5084 return -1;
5085
5086 h2_settings_initial_window_size = atoi(args[1]);
5087 if (h2_settings_initial_window_size < 0) {
5088 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5089 return -1;
5090 }
5091 return 0;
5092}
5093
Willy Tarreau5242ef82017-07-27 11:47:28 +02005094/* config parser for global "tune.h2.max-concurrent-streams" */
5095static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5096 struct proxy *defpx, const char *file, int line,
5097 char **err)
5098{
5099 if (too_many_args(1, args, err, NULL))
5100 return -1;
5101
5102 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005103 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005104 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5105 return -1;
5106 }
5107 return 0;
5108}
5109
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005110/* config parser for global "tune.h2.max-frame-size" */
5111static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5112 struct proxy *defpx, const char *file, int line,
5113 char **err)
5114{
5115 if (too_many_args(1, args, err, NULL))
5116 return -1;
5117
5118 h2_settings_max_frame_size = atoi(args[1]);
5119 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5120 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5121 return -1;
5122 }
5123 return 0;
5124}
5125
Willy Tarreau62f52692017-10-08 23:01:42 +02005126
5127/****************************************/
5128/* MUX initialization and instanciation */
5129/***************************************/
5130
5131/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005132static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005133 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005134 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005135 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005136 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005137 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005138 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005139 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005140 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005141 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005142 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005143 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005144 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005145 .shutr = h2_shutr,
5146 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005147 .show_fd = h2_show_fd,
Christopher Faulet9b79a102019-07-15 11:22:56 +02005148 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Willy Tarreau62f52692017-10-08 23:01:42 +02005149 .name = "H2",
5150};
5151
Christopher Faulet32f61c02018-04-10 14:33:41 +02005152static struct mux_proto_list mux_proto_h2 =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02005153 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005154
Willy Tarreau0108d902018-11-25 19:14:37 +01005155INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5156
Willy Tarreau62f52692017-10-08 23:01:42 +02005157/* config keyword parsers */
5158static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005159 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005160 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005161 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005162 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005163 { 0, NULL, NULL }
5164}};
5165
Willy Tarreau0108d902018-11-25 19:14:37 +01005166INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);