blob: cf6ad9dcf90c9656bae3bcb6a0a40b8361111590 [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
Olivier Houchard7505f942018-08-21 18:10:44 +02002738 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002739 b_realign_if_empty(buf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02002740 if (!b_data(buf)) {
2741 /* try to pre-align the buffer like the
Willy Tarreau2a59e872018-12-12 08:23:47 +01002742 * rxbufs will be to optimize memory copies. We'll make
2743 * sure that the frame header lands at the end of the
2744 * HTX block to alias it upon recv. We cannot use the
2745 * head because rcv_buf() will realign the buffer if
2746 * it's empty. Thus we cheat and pretend we already
2747 * have a few bytes there.
2748 */
2749 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002750 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002751 }
2752 else
2753 max = b_room(buf);
2754
Olivier Houchard7505f942018-08-21 18:10:44 +02002755 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002756 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002757 else
2758 ret = 0;
2759 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002760
Olivier Houchard53216e72018-10-10 15:46:36 +02002761 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002762 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002763
Olivier Houcharda1411e62018-08-17 18:42:48 +02002764 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002765 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002766 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002767 }
2768
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002769 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002770 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002771 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002772}
2773
Willy Tarreau479998a2018-11-18 06:30:59 +01002774/* Try to send data if possible.
2775 * The function returns 1 if data have been sent, otherwise zero.
2776 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002777static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002778{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002779 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002780 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002781 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002782
2783 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002784 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002785
Olivier Houchard7505f942018-08-21 18:10:44 +02002786
Willy Tarreaua2af5122017-10-09 11:56:46 +02002787 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2788 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002789 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002790 }
2791
Willy Tarreaubc933932017-10-09 16:21:43 +02002792 /* This loop is quite simple : it tries to fill as much as it can from
2793 * pending streams into the existing buffer until it's reportedly full
2794 * or the end of send requests is reached. Then it tries to send this
2795 * buffer's contents out, marks it not full if at least one byte could
2796 * be sent, and tries again.
2797 *
2798 * The snd_buf() function normally takes a "flags" argument which may
2799 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2800 * data immediately comes and CO_SFL_STREAMER to indicate that the
2801 * connection is streaming lots of data (used to increase TLS record
2802 * size at the expense of latency). The former can be sent any time
2803 * there's a buffer full flag, as it indicates at least one stream
2804 * attempted to send and failed so there are pending data. An
2805 * alternative would be to set it as long as there's an active stream
2806 * but that would be problematic for ACKs until we have an absolute
2807 * guarantee that all waiters have at least one byte to send. The
2808 * latter should possibly not be set for now.
2809 */
2810
2811 done = 0;
2812 while (!done) {
2813 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002814 unsigned int released = 0;
2815 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02002816
2817 /* fill as much as we can into the current buffer */
2818 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2819 done = h2_process_mux(h2c);
2820
Olivier Houchard2b094432019-01-29 18:28:36 +01002821 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002822 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01002823
Willy Tarreaubc933932017-10-09 16:21:43 +02002824 if (conn->flags & CO_FL_ERROR)
2825 break;
2826
2827 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2828 flags |= CO_SFL_MSG_MORE;
2829
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002830 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
2831 if (b_data(buf)) {
2832 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002833 if (!ret) {
2834 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002835 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002836 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002837 sent = 1;
2838 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002839 if (b_data(buf)) {
2840 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002841 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002842 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002843 }
2844 b_free(buf);
2845 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02002846 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002847
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002848 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02002849 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02002850
Willy Tarreaubc933932017-10-09 16:21:43 +02002851 /* wrote at least one byte, the buffer is not full anymore */
2852 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2853 }
2854
Willy Tarreaua2af5122017-10-09 11:56:46 +02002855 if (conn->flags & CO_FL_SOCK_WR_SH) {
2856 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02002857 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002858 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002859 /* We're not full anymore, so we can wake any task that are waiting
2860 * for us.
2861 */
2862 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002863 struct h2s *h2s;
2864
2865 list_for_each_entry(h2s, &h2c->send_list, list) {
2866 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2867 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002868
Willy Tarreauc234ae32019-05-13 17:56:11 +02002869 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002870 continue;
2871
Olivier Houchard998410a2019-04-15 19:23:37 +02002872 /* For some reason, the upper layer failed to subsribe again,
2873 * so remove it from the send_list
2874 */
2875 if (!h2s->send_wait) {
2876 LIST_DEL_INIT(&h2s->list);
2877 continue;
2878 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002879 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002880 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002881 tasklet_wakeup(h2s->send_wait->tasklet);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002882 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002883 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002884 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002885 /* We're done, no more to send */
Willy Tarreau662fafc2019-05-26 09:43:07 +02002886 if (!br_data(h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002887 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002888schedule:
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002889 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002890 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02002891
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002892 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002893}
2894
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02002895/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002896static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2897{
2898 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002899 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002900
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002901 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002902 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002903 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002904 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002905 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002906 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002907 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002908}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002909
Willy Tarreau62f52692017-10-08 23:01:42 +02002910/* callback called on any event by the connection handler.
2911 * It applies changes and returns zero, or < 0 if it wants immediate
2912 * destruction of the connection (which normally doesn not happen in h2).
2913 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002914static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002915{
Olivier Houchard7505f942018-08-21 18:10:44 +02002916 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002917
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002918 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002919 h2_process_demux(h2c);
2920
2921 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002922 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002923
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002924 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002925 h2c->flags &= ~H2_CF_DEM_DFULL;
2926 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002927 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002928
Willy Tarreau0b37d652018-10-03 10:33:02 +02002929 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002930 /* frontend is stopping, reload likely in progress, let's try
2931 * to announce a graceful shutdown if not yet done. We don't
2932 * care if it fails, it will be tried again later.
2933 */
2934 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2935 if (h2c->last_sid < 0)
2936 h2c->last_sid = (1U << 31) - 1;
2937 h2c_send_goaway_error(h2c, NULL);
2938 }
2939 }
2940
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002941 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002942 * If we received early data, and the handshake is done, wake
2943 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002944 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002945 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2946 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2947 struct eb32_node *node;
2948 struct h2s *h2s;
2949
2950 h2c->flags |= H2_CF_WAIT_FOR_HS;
2951 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2952
2953 while (node) {
2954 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002955 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002956 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002957 node = eb32_next(node);
2958 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002959 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002960
Willy Tarreau26bd7612017-10-09 16:47:04 +02002961 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002962 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2963 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2964 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02002965 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002966
2967 if (eb_is_empty(&h2c->streams_by_id)) {
2968 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002969 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002970 return -1;
2971 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002972 }
2973
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002974 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002975 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002976
Olivier Houchard53216e72018-10-10 15:46:36 +02002977 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2978 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2979 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02002980 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02002981 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2982 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02002983 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002984
Willy Tarreau3f133572017-10-31 19:21:06 +01002985 if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02002986 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2987 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01002988 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002989
Olivier Houchard7505f942018-08-21 18:10:44 +02002990 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002991 return 0;
2992}
2993
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002994/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002995static int h2_wake(struct connection *conn)
2996{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002997 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002998
2999 return (h2_process(h2c));
3000}
3001
Willy Tarreauea392822017-10-31 10:02:25 +01003002/* Connection timeout management. The principle is that if there's no receipt
3003 * nor sending for a certain amount of time, the connection is closed. If the
3004 * MUX buffer still has lying data or is not allocatable, the connection is
3005 * immediately killed. If it's allocatable and empty, we attempt to send a
3006 * GOAWAY frame.
3007 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003008static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003009{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003010 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003011 int expired = tick_is_expired(t->expire, now_ms);
3012
Willy Tarreau0975f112018-03-29 15:22:59 +02003013 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01003014 return t;
3015
Olivier Houchard3f795f72019-04-17 22:51:06 +02003016 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003017
3018 if (!h2c) {
3019 /* resources were already deleted */
3020 return NULL;
3021 }
3022
3023 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003024 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003025 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003026
Willy Tarreau662fafc2019-05-26 09:43:07 +02003027 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003028 /* don't even try to send a GOAWAY, the buffer is stuck */
3029 h2c->flags |= H2_CF_GOAWAY_FAILED;
3030 }
3031
3032 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003033 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003034 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3035 h2c->flags |= H2_CF_GOAWAY_FAILED;
3036
Willy Tarreau662fafc2019-05-26 09:43:07 +02003037 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003038 unsigned int released = 0;
3039 struct buffer *buf;
3040
3041 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3042 if (b_data(buf)) {
3043 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3044 if (!ret)
3045 break;
3046 b_del(buf, ret);
3047 if (b_data(buf))
3048 break;
3049 b_free(buf);
3050 released++;
3051 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003052 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003053
3054 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003055 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003056 }
Willy Tarreauea392822017-10-31 10:02:25 +01003057
Willy Tarreau0975f112018-03-29 15:22:59 +02003058 /* either we can release everything now or it will be done later once
3059 * the last stream closes.
3060 */
3061 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003062 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003063
Willy Tarreauea392822017-10-31 10:02:25 +01003064 return NULL;
3065}
3066
3067
Willy Tarreau62f52692017-10-08 23:01:42 +02003068/*******************************************/
3069/* functions below are used by the streams */
3070/*******************************************/
3071
3072/*
3073 * Attach a new stream to a connection
3074 * (Used for outgoing connections)
3075 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003076static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003077{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003078 struct conn_stream *cs;
3079 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003080 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003081
3082 cs = cs_new(conn);
3083 if (!cs)
3084 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003085 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003086 if (!h2s) {
3087 cs_free(cs);
3088 return NULL;
3089 }
3090 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003091}
3092
Willy Tarreaufafd3982018-11-18 21:29:20 +01003093/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3094 * We have to scan because we may have some orphan streams. It might be
3095 * beneficial to scan backwards from the end to reduce the likeliness to find
3096 * orphans.
3097 */
3098static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3099{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003100 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003101 struct h2s *h2s;
3102 struct eb32_node *node;
3103
3104 node = eb32_first(&h2c->streams_by_id);
3105 while (node) {
3106 h2s = container_of(node, struct h2s, by_id);
3107 if (h2s->cs)
3108 return h2s->cs;
3109 node = eb32_next(node);
3110 }
3111 return NULL;
3112}
3113
Willy Tarreau62f52692017-10-08 23:01:42 +02003114/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003115 * Destroy the mux and the associated connection, if it is no longer used
3116 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003117static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003118{
Christopher Faulet73c12072019-04-08 11:23:22 +02003119 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003120
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003121 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003122 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003123}
3124
3125/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003126 * Detach the stream from the connection and possibly release the connection.
3127 */
3128static void h2_detach(struct conn_stream *cs)
3129{
Willy Tarreau60935142017-10-16 18:11:19 +02003130 struct h2s *h2s = cs->ctx;
3131 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003132 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003133
3134 cs->ctx = NULL;
3135 if (!h2s)
3136 return;
3137
Olivier Houchard998410a2019-04-15 19:23:37 +02003138 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003139 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003140 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003141 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003142 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003143 /*
3144 * At this point, the stream_interface is supposed to have called
3145 * h2_unsubscribe(), so the only way there's still a
3146 * subscription that came from the stream_interface (as we
3147 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3148 * without the stream_interface involved) is that we subscribed
3149 * for sending, we woke the tasklet up and removed the
3150 * SUB_RETRY_SEND flag, so the stream_interface would not
3151 * know it has to unsubscribe for send, but the tasklet hasn't
3152 * run yet. Make sure to handle that by explicitely setting
3153 * send_wait to NULL, as nothing else will do it for us.
3154 */
3155 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003156 }
3157
Olivier Houchardf502aca2018-12-14 19:42:40 +01003158 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003159 h2c = h2s->h2c;
3160 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003161 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003162 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3163 !h2_frt_has_too_many_cs(h2c)) {
3164 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003165 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003166 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003167 }
Willy Tarreau60935142017-10-16 18:11:19 +02003168
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003169 /* this stream may be blocked waiting for some data to leave (possibly
3170 * an ES or RST frame), so orphan it in this case.
3171 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003172 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003173 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003174 (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 +01003175 return;
3176
Willy Tarreau45f752e2017-10-30 15:44:59 +01003177 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3178 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3179 /* unblock the connection if it was blocked on this
3180 * stream.
3181 */
3182 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3183 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003184 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003185 }
3186
Willy Tarreau71049cc2018-03-28 13:56:39 +02003187 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003188
Christopher Faulet9b79a102019-07-15 11:22:56 +02003189 if (h2c->flags & H2_CF_IS_BACK) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003190 if (!(h2c->conn->flags &
3191 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3192 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003193 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003194 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3195 h2c->conn->owner = NULL;
3196 if (eb_is_empty(&h2c->streams_by_id)) {
3197 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3198 /* The server doesn't want it, let's kill the connection right away */
3199 h2c->conn->mux->destroy(h2c->conn);
3200 return;
3201 }
3202 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003203 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003204 if (eb_is_empty(&h2c->streams_by_id)) {
3205 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3206 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3207 return;
3208 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003209 /* Never ever allow to reuse a connection from a non-reuse backend */
3210 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3211 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003212 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003213 struct server *srv = objt_server(h2c->conn->target);
3214
3215 if (srv) {
3216 if (h2c->conn->flags & CO_FL_PRIVATE)
3217 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3218 else
3219 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3220 }
3221
3222 }
3223 }
3224 }
3225
Willy Tarreaue323f342018-03-28 13:51:45 +02003226 /* We don't want to close right now unless we're removing the
3227 * last stream, and either the connection is in error, or it
3228 * reached the ID already specified in a GOAWAY frame received
3229 * or sent (as seen by last_sid >= 0).
3230 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003231 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003232 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003233 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003234 }
3235 else if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02003236 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3237 task_queue(h2c->task);
Willy Tarreau60935142017-10-16 18:11:19 +02003238 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003239}
3240
Willy Tarreau88bdba32019-05-13 18:17:53 +02003241/* Performs a synchronous or asynchronous shutr(). */
3242static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003243{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003244 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003245 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003246
Willy Tarreauf983d002019-05-14 10:40:21 +02003247 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003248 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003249
Willy Tarreau18059042019-01-31 19:12:48 +01003250 /* a connstream may require us to immediately kill the whole connection
3251 * for example because of a "tcp-request content reject" rule that is
3252 * normally used to limit abuse. In this case we schedule a goaway to
3253 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003254 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003255 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003256 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3257 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3258 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3259 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003260 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3261 /* Nothing was never sent for this stream, so reset with
3262 * REFUSED_STREAM error to let the client retry the
3263 * request.
3264 */
3265 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3266 }
Willy Tarreau18059042019-01-31 19:12:48 +01003267
Willy Tarreau90c32322017-11-24 08:00:30 +01003268 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003269 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003270 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003271
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003272 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003273 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003274 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003275 done:
3276 h2s->flags &= ~H2_SF_WANT_SHUTR;
3277 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003278add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003279 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003280 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003281 if (h2s->flags & H2_SF_BLK_MFCTL) {
3282 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3283 h2s->send_wait = sw;
3284 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3285 h2s->send_wait = sw;
3286 LIST_ADDQ(&h2c->send_list, &h2s->list);
3287 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003288 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003289 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003290 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003291 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003292}
3293
Willy Tarreau88bdba32019-05-13 18:17:53 +02003294/* Performs a synchronous or asynchronous shutw(). */
3295static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003296{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003297 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003298 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003299
Willy Tarreauf983d002019-05-14 10:40:21 +02003300 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003301 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003302
Willy Tarreauf983d002019-05-14 10:40:21 +02003303 if (h2s->st != H2_SS_HLOC && h2s->st != H2_SS_ERROR &&
3304 (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003305 /* we can cleanly close using an empty data frame only after headers */
3306
3307 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3308 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003309 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003310
3311 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003312 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003313 else
3314 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003315 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003316 /* a connstream may require us to immediately kill the whole connection
3317 * for example because of a "tcp-request content reject" rule that is
3318 * normally used to limit abuse. In this case we schedule a goaway to
3319 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003320 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003321 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003322 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3323 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3324 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3325 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003326 else {
3327 /* Nothing was never sent for this stream, so reset with
3328 * REFUSED_STREAM error to let the client retry the
3329 * request.
3330 */
3331 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3332 }
Willy Tarreau18059042019-01-31 19:12:48 +01003333
Willy Tarreau90c32322017-11-24 08:00:30 +01003334 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003335 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003336 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003337
Willy Tarreau00dd0782018-03-01 16:31:34 +01003338 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003339 }
3340
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003341 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003342 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003343 done:
3344 h2s->flags &= ~H2_SF_WANT_SHUTW;
3345 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003346
3347 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003348 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003349 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003350 if (h2s->flags & H2_SF_BLK_MFCTL) {
3351 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3352 h2s->send_wait = sw;
3353 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3354 h2s->send_wait = sw;
3355 LIST_ADDQ(&h2c->send_list, &h2s->list);
3356 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003357 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003358 /* let the handler know we want to shutw */
3359 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003360 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003361}
3362
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003363/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003364 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3365 * and prevented the last frame from being emitted.
3366 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003367static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3368{
3369 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003370 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003371
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003372 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003373 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003374 h2_do_shutw(h2s);
3375
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003376 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003377 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003378
Willy Tarreau88bdba32019-05-13 18:17:53 +02003379 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003380 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003381 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003382
Willy Tarreau88bdba32019-05-13 18:17:53 +02003383 if (!h2s->cs) {
3384 h2s_destroy(h2s);
3385 if (h2c_is_dead(h2c))
3386 h2_release(h2c);
3387 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003388 }
3389
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003390 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003391}
3392
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003393/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003394static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3395{
3396 struct h2s *h2s = cs->ctx;
3397
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003398 if (cs->flags & CS_FL_KILL_CONN)
3399 h2s->flags |= H2_SF_KILL_CONN;
3400
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003401 if (!mode)
3402 return;
3403
3404 h2_do_shutr(h2s);
3405}
3406
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003407/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003408static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3409{
3410 struct h2s *h2s = cs->ctx;
3411
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003412 if (cs->flags & CS_FL_KILL_CONN)
3413 h2s->flags |= H2_SF_KILL_CONN;
3414
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003415 h2_do_shutw(h2s);
3416}
3417
Christopher Faulet9b79a102019-07-15 11:22:56 +02003418/* Decode the payload of a HEADERS frame and produce the HTX request or response
3419 * depending on the connection's side. Returns a positive value on success, a
3420 * negative value on failure, or 0 if it couldn't proceed. May report connection
3421 * errors in h2c->errcode if the frame is non-decodable and the connection
3422 * unrecoverable. In absence of connection error when a failure is reported, the
3423 * caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003424 *
3425 * The function may fold CONTINUATION frames into the initial HEADERS frame
3426 * by removing padding and next frame header, then moving the CONTINUATION
3427 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3428 * leaving a hole between the main frame and the beginning of the next one.
3429 * The possibly remaining incomplete or next frame at the end may be moved
3430 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3431 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3432 *
3433 * A buffer at the beginning of processing may look like this :
3434 *
3435 * ,---.---------.-----.--------------.--------------.------.---.
3436 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3437 * `---^---------^-----^--------------^--------------^------^---'
3438 * | | <-----> | |
3439 * area | dpl | wrap
3440 * |<--------------> |
3441 * | dfl |
3442 * |<-------------------------------------------------->|
3443 * head data
3444 *
3445 * Padding is automatically overwritten when folding, participating to the
3446 * hole size after dfl :
3447 *
3448 * ,---.------------------------.-----.--------------.------.---.
3449 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3450 * `---^------------------------^-----^--------------^------^---'
3451 * | | <-----> | |
3452 * area | hole | wrap
3453 * |<-----------------------> |
3454 * | dfl |
3455 * |<-------------------------------------------------->|
3456 * head data
3457 *
3458 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3459 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3460 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003461 *
3462 * The <flags> field must point to either the stream's flags or to a copy of it
3463 * so that the function can update the following flags :
3464 * - H2_SF_DATA_CLEN when content-length is seen
3465 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3466 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003467 *
3468 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3469 * decoding, in order to detect if we're dealing with a headers or a trailers
3470 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003471 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003472static 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 +02003473{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003474 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003475 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003476 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003477 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003478 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003479 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003480 int flen; // header frame len
3481 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003482 int ret = 0;
3483 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003484 int wrap;
Willy Tarreau13278b42017-10-13 19:23:14 +02003485
Willy Tarreauea18f862018-12-22 20:19:26 +01003486next_frame:
3487 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3488 goto leave; // incomplete input frame
3489
3490 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3491 * this case, we'll try to paste it immediately after the initial
3492 * HEADERS frame payload and kill any possible padding. The initial
3493 * frame's length will be increased to represent the concatenation
3494 * of the two frames. The next frame is read from position <tlen>
3495 * and written at position <flen> (minus padding if some is present).
3496 */
3497 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3498 struct h2_fh hdr;
3499 int clen; // CONTINUATION frame's payload length
3500
3501 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3502 /* no more data, the buffer may be full, either due to
3503 * too large a frame or because of too large a hole that
3504 * we're going to compact at the end.
3505 */
3506 goto leave;
3507 }
3508
3509 if (hdr.ft != H2_FT_CONTINUATION) {
3510 /* RFC7540#6.10: frame of unexpected type */
3511 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3512 goto fail;
3513 }
3514
3515 if (hdr.sid != h2c->dsi) {
3516 /* RFC7540#6.10: frame of different stream */
3517 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3518 goto fail;
3519 }
3520
3521 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3522 /* RFC7540#4.2: invalid frame length */
3523 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3524 goto fail;
3525 }
3526
3527 /* detect when we must stop aggragating frames */
3528 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3529
3530 /* Take as much as we can of the CONTINUATION frame's payload */
3531 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3532 if (clen > hdr.len)
3533 clen = hdr.len;
3534
3535 /* Move the frame's payload over the padding, hole and frame
3536 * header. At least one of hole or dpl is null (see diagrams
3537 * above). The hole moves after the new aggragated frame.
3538 */
3539 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3540 h2c->dfl += clen - h2c->dpl;
3541 hole += h2c->dpl + 9;
3542 h2c->dpl = 0;
3543 goto next_frame;
3544 }
3545
3546 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003547
Willy Tarreau13278b42017-10-13 19:23:14 +02003548 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003549 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003550 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003551 copy = alloc_trash_chunk();
3552 if (!copy) {
3553 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3554 goto fail;
3555 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003556 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3557 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3558 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003559 }
3560
Willy Tarreau13278b42017-10-13 19:23:14 +02003561 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3562 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003563 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003564 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3565 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003566 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003567 }
3568
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003569 if (flen < 5) {
3570 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3571 goto fail;
3572 }
3573
Willy Tarreau13278b42017-10-13 19:23:14 +02003574 hdrs += 5; // stream dep = 4, weight = 1
3575 flen -= 5;
3576 }
3577
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003578 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003579 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003580 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003581 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003582
Willy Tarreau937f7602018-02-26 15:22:17 +01003583 /* we can't retry a failed decompression operation so we must be very
3584 * careful not to take any risks. In practice the output buffer is
3585 * always empty except maybe for trailers, in which case we simply have
3586 * to wait for the upper layer to finish consuming what is available.
3587 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02003588 htx = htx_from_buf(rxbuf);
3589 if (!htx_is_empty(htx)) {
3590 h2c->flags |= H2_CF_DEM_SFULL;
3591 goto leave;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003592 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003593
Willy Tarreau25919232019-01-03 14:48:18 +01003594 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003595 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3596 sizeof(list)/sizeof(list[0]), tmp);
3597 if (outlen < 0) {
3598 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3599 goto fail;
3600 }
3601
Willy Tarreau25919232019-01-03 14:48:18 +01003602 /* The PACK decompressor was updated, let's update the input buffer and
3603 * the parser's state to commit these changes and allow us to later
3604 * fail solely on the stream if needed.
3605 */
3606 b_del(&h2c->dbuf, h2c->dfl + hole);
3607 h2c->dfl = hole = 0;
3608 h2c->st0 = H2_CS_FRAME_H;
3609
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003610 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003611 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003612
Willy Tarreau88d138e2019-01-02 19:38:14 +01003613 if (*flags & H2_SF_HEADERS_RCVD)
3614 goto trailers;
3615
3616 /* This is the first HEADERS frame so it's a headers block */
Christopher Faulet9b79a102019-07-15 11:22:56 +02003617 if (h2c->flags & H2_CF_IS_BACK)
3618 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
3619 else
3620 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003621
3622 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003623 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003624 goto fail;
3625 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003626
Willy Tarreau174b06a2018-04-25 18:13:58 +02003627 if (msgf & H2_MSGF_BODY) {
3628 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003629 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003630 *flags |= H2_SF_DATA_CLEN;
Christopher Faulet9b79a102019-07-15 11:22:56 +02003631 htx->extra = *body_len;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003632 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003633 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003634 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003635 }
3636
Willy Tarreau88d138e2019-01-02 19:38:14 +01003637 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003638 /* indicate that a HEADERS frame was received for this stream, except
3639 * for 1xx responses. For 1xx responses, another HEADERS frame is
3640 * expected.
3641 */
3642 if (!(msgf & H2_MSGF_RSP_1XX))
3643 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003644
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02003645 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02003646 /* Mark the end of message using EOM */
3647 if (!htx_add_endof(htx, HTX_BLK_EOM))
3648 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003649 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003650
Willy Tarreau86277d42019-01-02 15:36:11 +01003651 /* success */
3652 ret = 1;
3653
Willy Tarreau68dd9852017-07-03 14:44:26 +02003654 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003655 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003656 * move the remaining data over it.
3657 */
3658 if (hole) {
3659 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3660 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3661 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3662 b_sub(&h2c->dbuf, hole);
3663 }
3664
Willy Tarreau97215ca2019-04-29 10:20:21 +02003665 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003666 /* too large frames */
3667 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003668 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003669 }
3670
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003671 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003672 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003673 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003674 return ret;
3675
Willy Tarreau68dd9852017-07-03 14:44:26 +02003676 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003677 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003678 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003679
3680 trailers:
3681 /* This is the last HEADERS frame hence a trailer */
Willy Tarreau88d138e2019-01-02 19:38:14 +01003682 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3683 /* It's a trailer but it's missing ES flag */
3684 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3685 goto fail;
3686 }
3687
Christopher Faulet9b79a102019-07-15 11:22:56 +02003688 /* Trailers terminate a DATA sequence */
3689 if (h2_make_htx_trailers(list, htx) <= 0)
3690 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003691 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003692}
3693
Christopher Faulet9b79a102019-07-15 11:22:56 +02003694/* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003695 * parser state is automatically updated. Returns > 0 if it could completely
3696 * send the current frame, 0 if it couldn't complete, in which case
3697 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3698 * DATA frame can return 0 as a valid result). Stream errors are reported in
3699 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3700 * have checked the frame header and ensured that the frame was complete or the
3701 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003702 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003703static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003704{
3705 struct h2c *h2c = h2s->h2c;
Christopher Faulet9b79a102019-07-15 11:22:56 +02003706 int block;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003707 unsigned int flen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003708 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003709 struct buffer *csbuf;
Christopher Faulet9b79a102019-07-15 11:22:56 +02003710 unsigned int sent;
Willy Tarreau454f9052017-10-26 19:40:35 +02003711
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003712 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003713
Olivier Houchard638b7992018-08-16 15:41:52 +02003714 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003715 if (!csbuf) {
3716 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003717 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003718 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02003719 htx = htx_from_buf(csbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003720
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003721try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003722 flen = h2c->dfl - h2c->dpl;
3723 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003724 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003725
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003726 if (flen > b_data(&h2c->dbuf)) {
3727 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003728 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003729 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003730 }
3731
Christopher Faulet9b79a102019-07-15 11:22:56 +02003732 block = htx_free_data_space(htx);
3733 if (!block) {
3734 h2c->flags |= H2_CF_DEM_SFULL;
3735 goto fail;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003736 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02003737 if (flen > block)
3738 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003739
Christopher Faulet9b79a102019-07-15 11:22:56 +02003740 /* here, flen is the max we can copy into the output buffer */
3741 block = b_contig_data(&h2c->dbuf, 0);
3742 if (flen > block)
3743 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003744
Christopher Faulet9b79a102019-07-15 11:22:56 +02003745 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau454f9052017-10-26 19:40:35 +02003746
Christopher Faulet9b79a102019-07-15 11:22:56 +02003747 b_del(&h2c->dbuf, sent);
3748 h2c->dfl -= sent;
3749 h2c->rcvd_c += sent;
3750 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau454f9052017-10-26 19:40:35 +02003751
Christopher Faulet9b79a102019-07-15 11:22:56 +02003752 if (h2s->flags & H2_SF_DATA_CLEN) {
3753 h2s->body_len -= sent;
3754 htx->extra = h2s->body_len;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003755 }
3756
Christopher Faulet9b79a102019-07-15 11:22:56 +02003757 if (sent < flen) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003758 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003759 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003760 }
3761
Christopher Faulet9b79a102019-07-15 11:22:56 +02003762 goto try_again;
3763
Willy Tarreau4a28da12018-01-04 14:41:00 +01003764 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003765 /* here we're done with the frame, all the payload (except padding) was
3766 * transferred.
3767 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003768
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003769 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02003770 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3771 h2c->flags |= H2_CF_DEM_SFULL;
3772 goto fail;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003773 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003774 }
3775
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003776 h2c->rcvd_c += h2c->dpl;
3777 h2c->rcvd_s += h2c->dpl;
3778 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003779 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet9b79a102019-07-15 11:22:56 +02003780 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003781 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003782 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003783 if (htx)
3784 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003785 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003786}
3787
Willy Tarreau115e83b2018-12-01 19:17:53 +01003788/* Try to send a HEADERS frame matching HTX response present in HTX message
3789 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3790 * must check the stream's status to detect any error which might have happened
3791 * subsequently to a successful send. The htx blocks are automatically removed
3792 * from the message. The htx message is assumed to be valid since produced from
3793 * the internal code, hence it contains a start line, an optional series of
3794 * header blocks and an end of header, otherwise an invalid frame could be
3795 * emitted and the resulting htx message could be left in an inconsistent state.
3796 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02003797static size_t h2s_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau115e83b2018-12-01 19:17:53 +01003798{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02003799 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01003800 struct h2c *h2c = h2s->h2c;
3801 struct htx_blk *blk;
3802 struct htx_blk *blk_end;
3803 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02003804 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003805 struct htx_sl *sl;
3806 enum htx_blk_type type;
3807 int es_now = 0;
3808 int ret = 0;
3809 int hdr;
3810 int idx;
3811
3812 if (h2c_mux_busy(h2c, h2s)) {
3813 h2s->flags |= H2_SF_BLK_MBUSY;
3814 return 0;
3815 }
3816
Willy Tarreau115e83b2018-12-01 19:17:53 +01003817 /* determine the first block which must not be deleted, blk_end may
3818 * be NULL if all blocks have to be deleted.
3819 */
3820 idx = htx_get_head(htx);
3821 blk_end = NULL;
3822 while (idx != -1) {
3823 type = htx_get_blk_type(htx_get_blk(htx, idx));
3824 idx = htx_get_next(htx, idx);
3825 if (type == HTX_BLK_EOH) {
3826 if (idx != -1)
3827 blk_end = htx_get_blk(htx, idx);
3828 break;
3829 }
3830 }
3831
3832 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02003833 blk = htx_get_head_blk(htx);
3834 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL);
3835 ALREADY_CHECKED(blk);
3836 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003837 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01003838 if (h2s->status < 100 || h2s->status > 999)
3839 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003840
3841 /* and the rest of the headers, that we dump starting at header 0 */
3842 hdr = 0;
3843
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003844 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003845 while ((idx = htx_get_next(htx, idx)) != -1) {
3846 blk = htx_get_blk(htx, idx);
3847 type = htx_get_blk_type(blk);
3848
3849 if (type == HTX_BLK_UNUSED)
3850 continue;
3851
3852 if (type != HTX_BLK_HDR)
3853 break;
3854
3855 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3856 goto fail;
3857
3858 list[hdr].n = htx_get_blk_name(htx, blk);
3859 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003860 hdr++;
3861 }
3862
3863 /* marker for end of headers */
3864 list[hdr].n = ist("");
3865
3866 if (h2s->status == 204 || h2s->status == 304) {
3867 /* no contents, claim c-len is present and set to zero */
3868 es_now = 1;
3869 }
3870
Willy Tarreau9c218e72019-05-26 10:08:28 +02003871 mbuf = br_tail(h2c->mbuf);
3872 retry:
3873 if (!h2_get_buf(h2c, mbuf)) {
3874 h2c->flags |= H2_CF_MUX_MALLOC;
3875 h2s->flags |= H2_SF_BLK_MROOM;
3876 return 0;
3877 }
3878
Willy Tarreau115e83b2018-12-01 19:17:53 +01003879 chunk_reset(&outbuf);
3880
3881 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02003882 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
3883 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01003884 break;
3885 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02003886 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01003887 }
3888
3889 if (outbuf.size < 9)
3890 goto full;
3891
3892 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3893 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3894 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3895 outbuf.data = 9;
3896
3897 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003898 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02003899 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01003900 goto realign_again;
3901 goto full;
3902 }
3903
3904 /* encode all headers, stop at empty name */
3905 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3906 /* these ones do not exist in H2 and must be dropped. */
3907 if (isteq(list[hdr].n, ist("connection")) ||
3908 isteq(list[hdr].n, ist("proxy-connection")) ||
3909 isteq(list[hdr].n, ist("keep-alive")) ||
3910 isteq(list[hdr].n, ist("upgrade")) ||
3911 isteq(list[hdr].n, ist("transfer-encoding")))
3912 continue;
3913
3914 if (isteq(list[hdr].n, ist("")))
3915 break; // end
3916
3917 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3918 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02003919 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01003920 goto realign_again;
3921 goto full;
3922 }
3923 }
3924
Christopher Faulet0b465482019-02-19 15:14:23 +01003925 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01003926 * FIXME: we should also set it when we know for sure that the
3927 * content-length is zero as well as on 204/304
3928 */
Christopher Faulet0b465482019-02-19 15:14:23 +01003929 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
3930 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01003931 es_now = 1;
3932
Willy Tarreau927b88b2019-03-04 08:03:25 +01003933 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01003934 es_now = 1;
3935
3936 /* update the frame's size */
3937 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3938
3939 if (es_now)
3940 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3941
3942 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02003943 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01003944
3945 /* indicates the HEADERS frame was sent, except for 1xx responses. For
3946 * 1xx responses, another HEADERS frame is expected.
3947 */
3948 if (h2s->status >= 200 || h2s->status == 101)
3949 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003950
Willy Tarreau115e83b2018-12-01 19:17:53 +01003951 if (es_now) {
3952 h2s->flags |= H2_SF_ES_SENT;
3953 if (h2s->st == H2_SS_OPEN)
3954 h2s->st = H2_SS_HLOC;
3955 else
3956 h2s_close(h2s);
3957 }
3958
3959 /* OK we could properly deliver the response */
3960
3961 /* remove all header blocks including the EOH and compute the
3962 * corresponding size.
3963 *
3964 * FIXME: We should remove everything when es_now is set.
3965 */
3966 ret = 0;
3967 idx = htx_get_head(htx);
3968 blk = htx_get_blk(htx, idx);
3969 while (blk != blk_end) {
3970 ret += htx_get_blksz(blk);
3971 blk = htx_remove_blk(htx, blk);
3972 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01003973
Christopher Faulet316934d2019-05-15 14:22:48 +02003974 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
3975 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01003976 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02003977 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01003978 end:
3979 return ret;
3980 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02003981 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
3982 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003983 h2c->flags |= H2_CF_MUX_MFULL;
3984 h2s->flags |= H2_SF_BLK_MROOM;
3985 ret = 0;
3986 goto end;
3987 fail:
3988 /* unparsable HTX messages, too large ones to be produced in the local
3989 * list etc go here (unrecoverable errors).
3990 */
3991 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3992 ret = 0;
3993 goto end;
3994}
3995
Willy Tarreau80739692018-10-05 11:35:57 +02003996/* Try to send a HEADERS frame matching HTX request present in HTX message
3997 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3998 * must check the stream's status to detect any error which might have happened
3999 * subsequently to a successful send. The htx blocks are automatically removed
4000 * from the message. The htx message is assumed to be valid since produced from
4001 * the internal code, hence it contains a start line, an optional series of
4002 * header blocks and an end of header, otherwise an invalid frame could be
4003 * emitted and the resulting htx message could be left in an inconsistent state.
4004 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004005static size_t h2s_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau80739692018-10-05 11:35:57 +02004006{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004007 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004008 struct h2c *h2c = h2s->h2c;
4009 struct htx_blk *blk;
4010 struct htx_blk *blk_end;
4011 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004012 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004013 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004014 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004015 enum htx_blk_type type;
4016 int es_now = 0;
4017 int ret = 0;
4018 int hdr;
4019 int idx;
4020
4021 if (h2c_mux_busy(h2c, h2s)) {
4022 h2s->flags |= H2_SF_BLK_MBUSY;
4023 return 0;
4024 }
4025
Willy Tarreau80739692018-10-05 11:35:57 +02004026 /* determine the first block which must not be deleted, blk_end may
4027 * be NULL if all blocks have to be deleted.
4028 */
4029 idx = htx_get_head(htx);
4030 blk_end = NULL;
4031 while (idx != -1) {
4032 type = htx_get_blk_type(htx_get_blk(htx, idx));
4033 idx = htx_get_next(htx, idx);
4034 if (type == HTX_BLK_EOH) {
4035 if (idx != -1)
4036 blk_end = htx_get_blk(htx, idx);
4037 break;
4038 }
4039 }
4040
4041 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004042 blk = htx_get_head_blk(htx);
4043 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
4044 ALREADY_CHECKED(blk);
4045 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004046 meth = htx_sl_req_meth(sl);
4047 path = htx_sl_req_uri(sl);
4048
4049 /* and the rest of the headers, that we dump starting at header 0 */
4050 hdr = 0;
4051
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004052 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004053 while ((idx = htx_get_next(htx, idx)) != -1) {
4054 blk = htx_get_blk(htx, idx);
4055 type = htx_get_blk_type(blk);
4056
4057 if (type == HTX_BLK_UNUSED)
4058 continue;
4059
4060 if (type != HTX_BLK_HDR)
4061 break;
4062
4063 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4064 goto fail;
4065
4066 list[hdr].n = htx_get_blk_name(htx, blk);
4067 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004068 hdr++;
4069 }
4070
4071 /* marker for end of headers */
4072 list[hdr].n = ist("");
4073
Willy Tarreau9c218e72019-05-26 10:08:28 +02004074 mbuf = br_tail(h2c->mbuf);
4075 retry:
4076 if (!h2_get_buf(h2c, mbuf)) {
4077 h2c->flags |= H2_CF_MUX_MALLOC;
4078 h2s->flags |= H2_SF_BLK_MROOM;
4079 return 0;
4080 }
4081
Willy Tarreau80739692018-10-05 11:35:57 +02004082 chunk_reset(&outbuf);
4083
4084 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004085 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4086 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004087 break;
4088 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004089 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004090 }
4091
4092 if (outbuf.size < 9)
4093 goto full;
4094
4095 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4096 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4097 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4098 outbuf.data = 9;
4099
4100 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004101 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004102 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004103 goto realign_again;
4104 goto full;
4105 }
4106
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004107 /* RFC7540 #8.3: the CONNECT method must have :
4108 * - :authority set to the URI part (host:port)
4109 * - :method set to CONNECT
4110 * - :scheme and :path omitted
4111 */
4112 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4113 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004114 struct ist scheme;
4115
4116 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4117 scheme = ist("http");
4118 else
4119 scheme = ist("https");
4120
4121 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004122 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004123 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004124 goto realign_again;
4125 goto full;
4126 }
Willy Tarreau80739692018-10-05 11:35:57 +02004127
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004128 /* encode the path, which necessarily is the second one */
4129 if (!hpack_encode_path(&outbuf, path)) {
4130 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004131 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004132 goto realign_again;
4133 goto full;
4134 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004135
4136 /* look for the Host header and place it in :authority */
4137 auth = ist2(NULL, 0);
4138 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4139 if (isteq(list[hdr].n, ist("")))
4140 break; // end
4141
4142 if (isteq(list[hdr].n, ist("host"))) {
4143 auth = list[hdr].v;
4144 break;
4145 }
4146 }
4147 }
4148 else {
4149 /* for CONNECT, :authority is taken from the path */
4150 auth = path;
4151 }
4152
4153 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4154 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004155 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004156 goto realign_again;
4157 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004158 }
4159
4160 /* encode all headers, stop at empty name */
4161 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4162 /* these ones do not exist in H2 and must be dropped. */
4163 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004164 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004165 isteq(list[hdr].n, ist("proxy-connection")) ||
4166 isteq(list[hdr].n, ist("keep-alive")) ||
4167 isteq(list[hdr].n, ist("upgrade")) ||
4168 isteq(list[hdr].n, ist("transfer-encoding")))
4169 continue;
4170
4171 if (isteq(list[hdr].n, ist("")))
4172 break; // end
4173
4174 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4175 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004176 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004177 goto realign_again;
4178 goto full;
4179 }
4180 }
4181
4182 /* we may need to add END_STREAM if we have no body :
4183 * - request already closed, or :
4184 * - no transfer-encoding, and :
4185 * - no content-length or content-length:0
4186 * Fixme: this doesn't take into account CONNECT requests.
4187 */
4188 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4189 es_now = 1;
4190
4191 if (sl->flags & HTX_SL_F_BODYLESS)
4192 es_now = 1;
4193
Willy Tarreau927b88b2019-03-04 08:03:25 +01004194 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004195 es_now = 1;
4196
4197 /* update the frame's size */
4198 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4199
4200 if (es_now)
4201 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4202
4203 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004204 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004205 h2s->flags |= H2_SF_HEADERS_SENT;
4206 h2s->st = H2_SS_OPEN;
4207
Willy Tarreau80739692018-10-05 11:35:57 +02004208 if (es_now) {
4209 // trim any possibly pending data (eg: inconsistent content-length)
4210 h2s->flags |= H2_SF_ES_SENT;
4211 h2s->st = H2_SS_HLOC;
4212 }
4213
4214 /* remove all header blocks including the EOH and compute the
4215 * corresponding size.
4216 *
4217 * FIXME: We should remove everything when es_now is set.
4218 */
4219 ret = 0;
4220 idx = htx_get_head(htx);
4221 blk = htx_get_blk(htx, idx);
4222 while (blk != blk_end) {
4223 ret += htx_get_blksz(blk);
4224 blk = htx_remove_blk(htx, blk);
4225 }
4226
Christopher Faulet316934d2019-05-15 14:22:48 +02004227 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4228 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004229 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004230 }
Willy Tarreau80739692018-10-05 11:35:57 +02004231
4232 end:
4233 return ret;
4234 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004235 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4236 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004237 h2c->flags |= H2_CF_MUX_MFULL;
4238 h2s->flags |= H2_SF_BLK_MROOM;
4239 ret = 0;
4240 goto end;
4241 fail:
4242 /* unparsable HTX messages, too large ones to be produced in the local
4243 * list etc go here (unrecoverable errors).
4244 */
4245 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4246 ret = 0;
4247 goto end;
4248}
4249
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004250/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004251 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4252 * caller must check the stream's status to detect any error which might have
4253 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02004254 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004255 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004256static size_t h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004257{
4258 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004259 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004260 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004261 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004262 size_t total = 0;
4263 int es_now = 0;
4264 int bsize; /* htx block size */
4265 int fsize; /* h2 frame size */
4266 struct htx_blk *blk;
4267 enum htx_blk_type type;
4268 int idx;
4269
4270 if (h2c_mux_busy(h2c, h2s)) {
4271 h2s->flags |= H2_SF_BLK_MBUSY;
4272 goto end;
4273 }
4274
Willy Tarreau98de12a2018-12-12 07:03:00 +01004275 htx = htx_from_buf(buf);
4276
Christopher Faulet54b5e212019-06-04 10:08:28 +02004277 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
4278 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
4279 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004280 */
4281
4282 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004283 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004284 goto end;
4285
4286 idx = htx_get_head(htx);
4287 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02004288 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004289 bsize = htx_get_blksz(blk);
4290 fsize = bsize;
4291
Christopher Faulet54b5e212019-06-04 10:08:28 +02004292 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004293 if (h2s->flags & H2_SF_ES_SENT) {
4294 /* ES already sent */
4295 htx_remove_blk(htx, blk);
4296 total++; // EOM counts as one byte
4297 count--;
4298 goto end;
4299 }
4300 }
4301 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004302 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004303
Willy Tarreau9c218e72019-05-26 10:08:28 +02004304 mbuf = br_tail(h2c->mbuf);
4305 retry:
4306 if (!h2_get_buf(h2c, mbuf)) {
4307 h2c->flags |= H2_CF_MUX_MALLOC;
4308 h2s->flags |= H2_SF_BLK_MROOM;
4309 goto end;
4310 }
4311
Willy Tarreau98de12a2018-12-12 07:03:00 +01004312 /* Perform some optimizations to reduce the number of buffer copies.
4313 * First, if the mux's buffer is empty and the htx area contains
4314 * exactly one data block of the same size as the requested count, and
4315 * this count fits within the frame size, the stream's window size, and
4316 * the connection's window size, then it's possible to simply swap the
4317 * caller's buffer with the mux's output buffer and adjust offsets and
4318 * length to match the entire DATA HTX block in the middle. In this
4319 * case we perform a true zero-copy operation from end-to-end. This is
4320 * the situation that happens all the time with large files. Second, if
4321 * this is not possible, but the mux's output buffer is empty, we still
4322 * have an opportunity to avoid the copy to the intermediary buffer, by
4323 * making the intermediary buffer's area point to the output buffer's
4324 * area. In this case we want to skip the HTX header to make sure that
4325 * copies remain aligned and that this operation remains possible all
4326 * the time. This goes for headers, data blocks and any data extracted
4327 * from the HTX blocks.
4328 */
4329 if (unlikely(fsize == count &&
Christopher Faulet192c6a22019-06-11 16:32:24 +02004330 htx_nbblks(htx) == 1 && type == HTX_BLK_DATA &&
Willy Tarreau98de12a2018-12-12 07:03:00 +01004331 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004332 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004333
Willy Tarreaubcc45952019-05-26 10:05:50 +02004334 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004335 /* Too bad there are data left there. We're willing to memcpy/memmove
4336 * up to 1/4 of the buffer, which means that it's OK to copy a large
4337 * frame into a buffer containing few data if it needs to be realigned,
4338 * and that it's also OK to copy few data without realigning. Otherwise
4339 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004340 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004341 if (fsize + 9 <= b_room(mbuf) &&
4342 (b_data(mbuf) <= b_size(mbuf) / 4 ||
4343 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004344 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004345
Willy Tarreau9c218e72019-05-26 10:08:28 +02004346 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4347 goto retry;
4348
Willy Tarreau98de12a2018-12-12 07:03:00 +01004349 h2c->flags |= H2_CF_MUX_MFULL;
4350 h2s->flags |= H2_SF_BLK_MROOM;
4351 goto end;
4352 }
4353
4354 /* map an H2 frame to the HTX block so that we can put the
4355 * frame header there.
4356 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004357 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
4358 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01004359
4360 /* prepend an H2 DATA frame header just before the DATA block */
4361 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4362 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4363 h2_set_frame_size(outbuf.area, fsize);
4364
4365 /* update windows */
4366 h2s->mws -= fsize;
4367 h2c->mws -= fsize;
4368
4369 /* and exchange with our old area */
4370 buf->area = old_area;
4371 buf->data = buf->head = 0;
4372 total += fsize;
4373 goto end;
4374 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004375
Willy Tarreau98de12a2018-12-12 07:03:00 +01004376 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004377 /* for DATA and EOM we'll have to emit a frame, even if empty */
4378
4379 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004380 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4381 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004382 break;
4383 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004384 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004385 }
4386
4387 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02004388 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4389 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004390 h2c->flags |= H2_CF_MUX_MFULL;
4391 h2s->flags |= H2_SF_BLK_MROOM;
4392 goto end;
4393 }
4394
4395 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4396 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4397 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4398 outbuf.data = 9;
4399
4400 /* we have in <fsize> the exact number of bytes we need to copy from
4401 * the HTX buffer. We need to check this against the connection's and
4402 * the stream's send windows, and to ensure that this fits in the max
4403 * frame size and in the buffer's available space minus 9 bytes (for
4404 * the frame header). The connection's flow control is applied last so
4405 * that we can use a separate list of streams which are immediately
4406 * unblocked on window opening. Note: we don't implement padding.
4407 */
4408
4409 /* EOM is presented with bsize==1 but would lead to the emission of an
4410 * empty frame, thus we force it to zero here.
4411 */
4412 if (type == HTX_BLK_EOM)
4413 bsize = fsize = 0;
4414
4415 if (!fsize)
4416 goto send_empty;
4417
4418 if (h2s->mws <= 0) {
4419 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004420 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004421 LIST_DEL_INIT(&h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004422 goto end;
4423 }
4424
Willy Tarreauee573762018-12-04 15:25:57 +01004425 if (fsize > count)
4426 fsize = count;
4427
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004428 if (fsize > h2s->mws)
4429 fsize = h2s->mws; // >0
4430
4431 if (h2c->mfs && fsize > h2c->mfs)
4432 fsize = h2c->mfs; // >0
4433
4434 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02004435 /* It doesn't fit at once. If it at least fits once split and
4436 * the amount of data to move is low, let's defragment the
4437 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004438 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004439 if (b_space_wraps(mbuf) &&
4440 (fsize + 9 <= b_room(mbuf)) &&
4441 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004442 goto realign_again;
4443 fsize = outbuf.size - 9;
4444
4445 if (fsize <= 0) {
4446 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02004447 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4448 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004449 h2c->flags |= H2_CF_MUX_MFULL;
4450 h2s->flags |= H2_SF_BLK_MROOM;
4451 goto end;
4452 }
4453 }
4454
4455 if (h2c->mws <= 0) {
4456 h2s->flags |= H2_SF_BLK_MFCTL;
4457 goto end;
4458 }
4459
4460 if (fsize > h2c->mws)
4461 fsize = h2c->mws;
4462
4463 /* now let's copy this this into the output buffer */
4464 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004465 h2s->mws -= fsize;
4466 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004467 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004468
4469 send_empty:
4470 /* update the frame's size */
4471 h2_set_frame_size(outbuf.area, fsize);
4472
4473 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4474 * meeting EOM. We should optimize this later.
4475 */
4476 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004477 total++; // EOM counts as one byte
4478 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004479 es_now = 1;
4480 }
4481
4482 if (es_now)
4483 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4484
4485 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004486 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004487
4488 /* consume incoming HTX block, including EOM */
4489 total += fsize;
4490 if (fsize == bsize) {
4491 htx_remove_blk(htx, blk);
4492 if (fsize)
4493 goto new_frame;
4494 } else {
4495 /* we've truncated this block */
4496 htx_cut_data_blk(htx, blk, fsize);
4497 }
4498
4499 if (es_now) {
4500 if (h2s->st == H2_SS_OPEN)
4501 h2s->st = H2_SS_HLOC;
4502 else
4503 h2s_close(h2s);
4504
4505 h2s->flags |= H2_SF_ES_SENT;
4506 }
4507
4508 end:
4509 return total;
4510}
4511
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004512/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4513 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4514 * processed. The caller must check the stream's status to detect any error
4515 * which might have happened subsequently to a successful send. The htx blocks
4516 * are automatically removed from the message. The htx message is assumed to be
4517 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02004518 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004519 * as a single frame. The ES flag is always set.
4520 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004521static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004522{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004523 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004524 struct h2c *h2c = h2s->h2c;
4525 struct htx_blk *blk;
4526 struct htx_blk *blk_end;
4527 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004528 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004529 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004530 int ret = 0;
4531 int hdr;
4532 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004533
4534 if (h2c_mux_busy(h2c, h2s)) {
4535 h2s->flags |= H2_SF_BLK_MBUSY;
4536 goto end;
4537 }
4538
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004539 /* determine the first block which must not be deleted, blk_end may
4540 * be NULL if all blocks have to be deleted. also get trailers.
4541 */
4542 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004543 blk_end = NULL;
4544
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004545 hdr = 0;
4546 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004547 blk = htx_get_blk(htx, idx);
4548 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004549 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004550 if (type == HTX_BLK_UNUSED)
4551 continue;
4552
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004553 if (type == HTX_BLK_EOT) {
4554 if (idx != -1)
4555 blk_end = blk;
4556 break;
4557 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02004558 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004559 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004560
4561 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4562 goto fail;
4563
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004564 list[hdr].n = htx_get_blk_name(htx, blk);
4565 list[hdr].v = htx_get_blk_value(htx, blk);
4566 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004567 }
4568
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004569 /* marker for end of trailers */
4570 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004571
Willy Tarreau9c218e72019-05-26 10:08:28 +02004572 mbuf = br_tail(h2c->mbuf);
4573 retry:
4574 if (!h2_get_buf(h2c, mbuf)) {
4575 h2c->flags |= H2_CF_MUX_MALLOC;
4576 h2s->flags |= H2_SF_BLK_MROOM;
4577 goto end;
4578 }
4579
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004580 chunk_reset(&outbuf);
4581
4582 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004583 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4584 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004585 break;
4586 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004587 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004588 }
4589
4590 if (outbuf.size < 9)
4591 goto full;
4592
4593 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
4594 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
4595 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4596 outbuf.data = 9;
4597
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004598 /* encode all headers */
4599 for (idx = 0; idx < hdr; idx++) {
4600 /* these ones do not exist in H2 or must not appear in
4601 * trailers and must be dropped.
4602 */
4603 if (isteq(list[idx].n, ist("host")) ||
4604 isteq(list[idx].n, ist("content-length")) ||
4605 isteq(list[idx].n, ist("connection")) ||
4606 isteq(list[idx].n, ist("proxy-connection")) ||
4607 isteq(list[idx].n, ist("keep-alive")) ||
4608 isteq(list[idx].n, ist("upgrade")) ||
4609 isteq(list[idx].n, ist("te")) ||
4610 isteq(list[idx].n, ist("transfer-encoding")))
4611 continue;
4612
4613 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
4614 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004615 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004616 goto realign_again;
4617 goto full;
4618 }
4619 }
4620
Willy Tarreau5121e5d2019-05-06 15:13:41 +02004621 if (outbuf.data == 9) {
4622 /* here we have a problem, we have nothing to emit (either we
4623 * received an empty trailers block followed or we removed its
4624 * contents above). Because of this we can't send a HEADERS
4625 * frame, so we have to cheat and instead send an empty DATA
4626 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01004627 */
4628 outbuf.area[3] = H2_FT_DATA;
4629 outbuf.area[4] = H2_F_DATA_END_STREAM;
4630 }
4631
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004632 /* update the frame's size */
4633 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4634
4635 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004636 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004637 h2s->flags |= H2_SF_ES_SENT;
4638
4639 if (h2s->st == H2_SS_OPEN)
4640 h2s->st = H2_SS_HLOC;
4641 else
4642 h2s_close(h2s);
4643
4644 /* OK we could properly deliver the response */
4645 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02004646 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004647 ret = 0;
4648 idx = htx_get_head(htx);
4649 blk = htx_get_blk(htx, idx);
4650 while (blk != blk_end) {
4651 ret += htx_get_blksz(blk);
4652 blk = htx_remove_blk(htx, blk);
4653 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004654
4655 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4656 ret += htx_get_blksz(blk_end);
4657 htx_remove_blk(htx, blk_end);
4658 }
4659
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004660 end:
4661 return ret;
4662 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004663 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4664 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004665 h2c->flags |= H2_CF_MUX_MFULL;
4666 h2s->flags |= H2_SF_BLK_MROOM;
4667 ret = 0;
4668 goto end;
4669 fail:
4670 /* unparsable HTX messages, too large ones to be produced in the local
4671 * list etc go here (unrecoverable errors).
4672 */
4673 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4674 ret = 0;
4675 goto end;
4676}
4677
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004678/* Called from the upper layer, to subscribe to events, such as being able to send.
4679 * The <param> argument here is supposed to be a pointer to a wait_event struct
4680 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
4681 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
4682 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
4683 * returns 0 except for the error above.
4684 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02004685static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4686{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004687 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004688 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004689 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004690
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004691 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004692 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02004693 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
4694 sw->events |= SUB_RETRY_RECV;
4695 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004696 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004697 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004698 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004699 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02004700 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
4701 sw->events |= SUB_RETRY_SEND;
4702 h2s->send_wait = sw;
4703 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
4704 !LIST_ADDED(&h2s->list)) {
4705 if (h2s->flags & H2_SF_BLK_MFCTL)
4706 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4707 else
4708 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004709 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004710 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004711 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004712 if (event_type != 0)
4713 return -1;
4714 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004715}
4716
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004717/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
4718 * The <param> argument here is supposed to be a pointer to the same wait_event
4719 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
4720 * It always returns zero.
4721 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004722static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4723{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004724 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004725 struct h2s *h2s = cs->ctx;
4726
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004727 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004728 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02004729 BUG_ON(h2s->recv_wait != sw);
4730 sw->events &= ~SUB_RETRY_RECV;
4731 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004732 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004733 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004734 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02004735 BUG_ON(h2s->send_wait != sw);
4736 LIST_DEL(&h2s->list);
4737 LIST_INIT(&h2s->list);
4738 sw->events &= ~SUB_RETRY_SEND;
4739 /* We were about to send, make sure it does not happen */
4740 if (LIST_ADDED(&h2s->sending_list) &&
4741 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02004742 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02004743 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02004744 }
Olivier Houchardf8338152019-05-14 17:50:32 +02004745 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02004746 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004747 return 0;
4748}
4749
4750
Olivier Houchard511efea2018-08-16 15:30:32 +02004751/* Called from the upper layer, to receive data */
4752static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4753{
Olivier Houchard638b7992018-08-16 15:41:52 +02004754 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004755 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004756 struct htx *h2s_htx = NULL;
4757 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02004758 size_t ret = 0;
4759
4760 /* transfer possibly pending data to the upper layer */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004761 h2s_htx = htx_from_buf(&h2s->rxbuf);
4762 if (htx_is_empty(h2s_htx)) {
4763 /* Here htx_to_buf() will set buffer data to 0 because
4764 * the HTX is empty.
4765 */
4766 htx_to_buf(h2s_htx, &h2s->rxbuf);
4767 goto end;
4768 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01004769
Christopher Faulet9b79a102019-07-15 11:22:56 +02004770 ret = h2s_htx->data;
4771 buf_htx = htx_from_buf(buf);
Willy Tarreau7196dd62019-03-05 10:51:11 +01004772
Christopher Faulet9b79a102019-07-15 11:22:56 +02004773 /* <buf> is empty and the message is small enough, swap the
4774 * buffers. */
4775 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004776 htx_to_buf(buf_htx, buf);
4777 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004778 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
4779 goto end;
Willy Tarreau86724e22018-12-01 23:19:43 +01004780 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004781
4782 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
4783
4784 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
4785 buf_htx->flags |= HTX_FL_PARSING_ERROR;
4786 if (htx_is_empty(buf_htx))
4787 cs->flags |= CS_FL_EOI;
Willy Tarreau86724e22018-12-01 23:19:43 +01004788 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004789
Christopher Faulet9b79a102019-07-15 11:22:56 +02004790 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
4791 htx_to_buf(buf_htx, buf);
4792 htx_to_buf(h2s_htx, &h2s->rxbuf);
4793 ret -= h2s_htx->data;
4794
Christopher Faulet37070b22019-02-14 15:12:14 +01004795 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02004796 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004797 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004798 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004799 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02004800 if (h2s->flags & H2_SF_ES_RCVD)
4801 cs->flags |= CS_FL_EOI;
Willy Tarreau76c83822019-06-15 09:55:50 +02004802 if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02004803 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004804 if (cs->flags & CS_FL_ERR_PENDING)
4805 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02004806 if (b_size(&h2s->rxbuf)) {
4807 b_free(&h2s->rxbuf);
4808 offer_buffers(NULL, tasks_run_queue);
4809 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004810 }
4811
Willy Tarreau082f5592018-11-25 08:03:32 +01004812 if (ret && h2c->dsi == h2s->id) {
4813 /* demux is blocking on this stream's buffer */
4814 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02004815 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01004816 }
Christopher Faulet37070b22019-02-14 15:12:14 +01004817
Olivier Houchard511efea2018-08-16 15:30:32 +02004818 return ret;
4819}
4820
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004821/* stops all senders of this connection for example when the mux buffer is full.
4822 * They are moved from the sending_list to either fctl_list or send_list.
4823 */
Olivier Houchardd846c262018-10-19 17:24:29 +02004824static void h2_stop_senders(struct h2c *h2c)
4825{
4826 struct h2s *h2s, *h2s_back;
4827
Olivier Houchardd360ac62019-03-22 17:37:16 +01004828 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
4829 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02004830 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004831 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02004832 }
4833}
4834
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004835/* Called from the upper layer, to send data from buffer <buf> for no more than
4836 * <count> bytes. Returns the number of bytes effectively sent. Some status
4837 * flags may be updated on the conn_stream.
4838 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004839static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004840{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004841 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004842 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004843 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004844 struct htx *htx;
4845 struct htx_blk *blk;
4846 enum htx_blk_type btype;
4847 uint32_t bsize;
4848 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004849
Olivier Houchardd360ac62019-03-22 17:37:16 +01004850 /* If we were not just woken because we wanted to send but couldn't,
4851 * and there's somebody else that is waiting to send, do nothing,
4852 * we will subscribe later and be put at the end of the list
4853 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02004854 if (!LIST_ADDED(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01004855 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
4856 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02004857 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01004858
Olivier Houchard998410a2019-04-15 19:23:37 +02004859 /* We couldn't set it to NULL before, because we needed it in case
4860 * we had to cancel the tasklet
4861 */
4862 h2s->send_wait = NULL;
4863
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004864 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4865 return 0;
4866
Christopher Faulet9b79a102019-07-15 11:22:56 +02004867 htx = htx_from_buf(buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004868
Willy Tarreau0bad0432018-06-14 16:54:01 +02004869 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004870 h2s->flags |= H2_SF_OUTGOING_DATA;
4871
Willy Tarreau751f2d02018-10-05 09:35:00 +02004872 if (h2s->id == 0) {
4873 int32_t id = h2c_get_next_sid(h2s->h2c);
4874
4875 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02004876 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02004877 return 0;
4878 }
4879
4880 eb32_delete(&h2s->by_id);
4881 h2s->by_id.key = h2s->id = id;
4882 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01004883 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02004884 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4885 }
4886
Christopher Faulet9b79a102019-07-15 11:22:56 +02004887 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
4888 count && !htx_is_empty(htx)) {
4889 idx = htx_get_head(htx);
4890 blk = htx_get_blk(htx, idx);
4891 btype = htx_get_blk_type(blk);
4892 bsize = htx_get_blksz(blk);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004893
Christopher Faulet9b79a102019-07-15 11:22:56 +02004894 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004895 case HTX_BLK_REQ_SL:
4896 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004897 ret = h2s_bck_make_req_headers(h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02004898 if (ret > 0) {
4899 total += ret;
4900 count -= ret;
4901 if (ret < bsize)
4902 goto done;
4903 }
4904 break;
4905
Willy Tarreau115e83b2018-12-01 19:17:53 +01004906 case HTX_BLK_RES_SL:
4907 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004908 ret = h2s_frt_make_resp_headers(h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004909 if (ret > 0) {
4910 total += ret;
4911 count -= ret;
4912 if (ret < bsize)
4913 goto done;
4914 }
4915 break;
4916
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004917 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004918 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004919 /* all these cause the emission of a DATA frame (possibly empty).
4920 * This EOM necessarily is one before trailers, as the EOM following
4921 * trailers would have been consumed by the trailers parser.
4922 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004923 ret = h2s_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004924 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004925 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004926 total += ret;
4927 count -= ret;
4928 if (ret < bsize)
4929 goto done;
4930 }
4931 break;
4932
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004933 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02004934 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004935 /* This is the first trailers block, all the subsequent ones AND
4936 * the EOM will be swallowed by the parser.
4937 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004938 ret = h2s_make_trailers(h2s, htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004939 if (ret > 0) {
4940 total += ret;
4941 count -= ret;
4942 if (ret < bsize)
4943 goto done;
4944 }
4945 break;
4946
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004947 default:
4948 htx_remove_blk(htx, blk);
4949 total += bsize;
4950 count -= bsize;
4951 break;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004952 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004953 }
4954
Christopher Faulet9b79a102019-07-15 11:22:56 +02004955 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02004956 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02004957 /* trim any possibly pending data after we close (extra CR-LF,
4958 * unprocessed trailers, abnormal extra data, ...)
4959 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004960 total += count;
4961 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004962 }
4963
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004964 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004965 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01004966 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004967 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004968 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004969 }
4970
Christopher Faulet9b79a102019-07-15 11:22:56 +02004971 htx_to_buf(htx, buf);
Olivier Houchardd846c262018-10-19 17:24:29 +02004972
4973 /* The mux is full, cancel the pending tasks */
4974 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4975 (h2s->flags & H2_SF_BLK_MBUSY))
4976 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004977
Olivier Houchard7505f942018-08-21 18:10:44 +02004978 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004979 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004980 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02004981
Olivier Houchard7505f942018-08-21 18:10:44 +02004982 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01004983 /* If we're waiting for flow control, and we got a shutr on the
4984 * connection, we will never be unlocked, so add an error on
4985 * the conn_stream.
4986 */
4987 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
4988 !b_data(&h2s->h2c->dbuf) &&
4989 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
4990 if (cs->flags & CS_FL_EOS)
4991 cs->flags |= CS_FL_ERROR;
4992 else
4993 cs->flags |= CS_FL_ERR_PENDING;
4994 }
Olivier Houchard998410a2019-04-15 19:23:37 +02004995 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01004996 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01004997 LIST_DEL_INIT(&h2s->list);
4998 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004999 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005000}
5001
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005002/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005003static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005004{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005005 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005006 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005007 struct eb32_node *node;
5008 int fctl_cnt = 0;
5009 int send_cnt = 0;
5010 int tree_cnt = 0;
5011 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005012 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005013
5014 if (!h2c)
5015 return;
5016
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005017 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005018 fctl_cnt++;
5019
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005020 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005021 send_cnt++;
5022
Willy Tarreau3af37712018-12-18 14:34:41 +01005023 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005024 node = eb32_first(&h2c->streams_by_id);
5025 while (node) {
5026 h2s = container_of(node, struct h2s, by_id);
5027 tree_cnt++;
5028 if (!h2s->cs)
5029 orph_cnt++;
5030 node = eb32_next(node);
5031 }
5032
Willy Tarreau60f62682019-05-26 11:32:27 +02005033 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005034 tmbuf = br_tail(h2c->mbuf);
Willy Tarreau987c0632018-12-18 10:32:05 +01005035 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5036 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005037 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5038 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreau616ac812018-07-24 14:12:42 +02005039 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5040 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005041 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005042 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5043 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5044 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005045 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5046 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5047 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005048 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5049 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005050
5051 if (h2s) {
5052 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5053 h2s, h2s->id, h2s->flags,
5054 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5055 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5056 h2s->cs);
5057 if (h2s->cs)
5058 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5059 h2s->cs->flags, h2s->cs->data);
5060 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005061}
Willy Tarreau62f52692017-10-08 23:01:42 +02005062
5063/*******************************************************/
5064/* functions below are dedicated to the config parsers */
5065/*******************************************************/
5066
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005067/* config parser for global "tune.h2.header-table-size" */
5068static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5069 struct proxy *defpx, const char *file, int line,
5070 char **err)
5071{
5072 if (too_many_args(1, args, err, NULL))
5073 return -1;
5074
5075 h2_settings_header_table_size = atoi(args[1]);
5076 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5077 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5078 return -1;
5079 }
5080 return 0;
5081}
Willy Tarreau62f52692017-10-08 23:01:42 +02005082
Willy Tarreaue6baec02017-07-27 11:45:11 +02005083/* config parser for global "tune.h2.initial-window-size" */
5084static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5085 struct proxy *defpx, const char *file, int line,
5086 char **err)
5087{
5088 if (too_many_args(1, args, err, NULL))
5089 return -1;
5090
5091 h2_settings_initial_window_size = atoi(args[1]);
5092 if (h2_settings_initial_window_size < 0) {
5093 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5094 return -1;
5095 }
5096 return 0;
5097}
5098
Willy Tarreau5242ef82017-07-27 11:47:28 +02005099/* config parser for global "tune.h2.max-concurrent-streams" */
5100static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5101 struct proxy *defpx, const char *file, int line,
5102 char **err)
5103{
5104 if (too_many_args(1, args, err, NULL))
5105 return -1;
5106
5107 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005108 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005109 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5110 return -1;
5111 }
5112 return 0;
5113}
5114
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005115/* config parser for global "tune.h2.max-frame-size" */
5116static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5117 struct proxy *defpx, const char *file, int line,
5118 char **err)
5119{
5120 if (too_many_args(1, args, err, NULL))
5121 return -1;
5122
5123 h2_settings_max_frame_size = atoi(args[1]);
5124 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5125 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5126 return -1;
5127 }
5128 return 0;
5129}
5130
Willy Tarreau62f52692017-10-08 23:01:42 +02005131
5132/****************************************/
5133/* MUX initialization and instanciation */
5134/***************************************/
5135
5136/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005137static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005138 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005139 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005140 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005141 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005142 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005143 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005144 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005145 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005146 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005147 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005148 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005149 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005150 .shutr = h2_shutr,
5151 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005152 .show_fd = h2_show_fd,
Christopher Faulet9b79a102019-07-15 11:22:56 +02005153 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Willy Tarreau62f52692017-10-08 23:01:42 +02005154 .name = "H2",
5155};
5156
Christopher Faulet32f61c02018-04-10 14:33:41 +02005157static struct mux_proto_list mux_proto_h2 =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02005158 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005159
Willy Tarreau0108d902018-11-25 19:14:37 +01005160INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5161
Willy Tarreau62f52692017-10-08 23:01:42 +02005162/* config keyword parsers */
5163static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005164 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005165 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005166 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005167 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005168 { 0, NULL, NULL }
5169}};
5170
Willy Tarreau0108d902018-11-25 19:14:37 +01005171INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);