blob: 3097fc45c48cb31197d7491e696c8a9a71d143bb [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>
Willy Tarreau12ae2122019-08-08 18:23:12 +020025#include <proto/trace.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010026#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020027#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010028#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010029#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020030#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020031
32
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010033/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020034static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010035static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010036static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020037static const struct h2s *h2_idle_stream;
38
Willy Tarreau5ab6b572017-09-22 08:05:00 +020039/* Connection flags (32 bit), in h2c->flags */
40#define H2_CF_NONE 0x00000000
41
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020042/* Flags indicating why writing to the mux is blocked. */
43#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
44#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
45#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
46
Willy Tarreau315d8072017-12-10 22:17:57 +010047/* Flags indicating why writing to the demux is blocked.
48 * The first two ones directly affect the ability for the mux to receive data
49 * from the connection. The other ones affect the mux's ability to demux
50 * received data.
51 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
53#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010054
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020055#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
56#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
57#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
58#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020059#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
60#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020061
Willy Tarreau081d4722017-05-16 21:51:05 +020062/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020063#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
64#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
65#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 +020066#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010067#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020068
Willy Tarreau5ab6b572017-09-22 08:05:00 +020069/* H2 connection state, in h2c->st0 */
70enum h2_cs {
71 H2_CS_PREFACE, // init done, waiting for connection preface
72 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
73 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
74 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010075 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
76 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020077 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
78 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
79 H2_CS_ENTRIES // must be last
80} __attribute__((packed));
81
Willy Tarreau51330962019-05-26 09:38:07 +020082
Willy Tarreau9c218e72019-05-26 10:08:28 +020083/* 32 buffers: one for the ring's root, rest for the mbuf itself */
84#define H2C_MBUF_CNT 32
Willy Tarreau51330962019-05-26 09:38:07 +020085
Willy Tarreau5ab6b572017-09-22 08:05:00 +020086/* H2 connection descriptor */
87struct h2c {
88 struct connection *conn;
89
90 enum h2_cs st0; /* mux state */
91 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
92
93 /* 16 bit hole here */
94 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010095 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020096 int32_t max_id; /* highest ID known on this connection, <0 before preface */
97 uint32_t rcvd_c; /* newly received data to ACK for the connection */
98 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
99
100 /* states for the demux direction */
101 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200102 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200103
104 int32_t dsi; /* demux stream ID (<0 = idle) */
105 int32_t dfl; /* demux frame length (if dsi >= 0) */
106 int8_t dft; /* demux frame type (if dsi >= 0) */
107 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100108 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
109 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200110 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
111
112 /* states for the mux direction */
Willy Tarreau51330962019-05-26 09:38:07 +0200113 struct buffer mbuf[H2C_MBUF_CNT]; /* mux buffers (ring) */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200114 int32_t msi; /* mux stream ID (<0 = idle) */
115 int32_t mfl; /* mux frame length (if dsi >= 0) */
116 int8_t mft; /* mux frame type (if dsi >= 0) */
117 int8_t mff; /* mux frame flags (if dsi >= 0) */
118 /* 16 bit hole here */
119 int32_t miw; /* mux initial window size for all new streams */
120 int32_t mws; /* mux window size. Can be negative. */
121 int32_t mfs; /* mux's max frame size */
122
Willy Tarreauea392822017-10-31 10:02:25 +0100123 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100124 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100125 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200126 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100127 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100128 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200129 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100130 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200131 struct eb_root streams_by_id; /* all active streams by their ID */
132 struct list send_list; /* list of blocked streams requesting to send */
133 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200134 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100135 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200136 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200137};
138
Willy Tarreau18312642017-10-11 07:57:07 +0200139/* H2 stream state, in h2s->st */
140enum h2_ss {
141 H2_SS_IDLE = 0, // idle
142 H2_SS_RLOC, // reserved(local)
143 H2_SS_RREM, // reserved(remote)
144 H2_SS_OPEN, // open
145 H2_SS_HREM, // half-closed(remote)
146 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200147 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200148 H2_SS_CLOSED, // closed
149 H2_SS_ENTRIES // must be last
150} __attribute__((packed));
151
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200152#define H2_SS_MASK(state) (1UL << (state))
153#define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE)
154#define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC)
155#define H2_SS_RREM_BIT (1UL << H2_SS_RREM)
156#define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN)
157#define H2_SS_HREM_BIT (1UL << H2_SS_HREM)
158#define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC)
159#define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR)
160#define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED)
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200161
Willy Tarreau18312642017-10-11 07:57:07 +0200162/* HTTP/2 stream flags (32 bit), in h2s->flags */
163#define H2_SF_NONE 0x00000000
164#define H2_SF_ES_RCVD 0x00000001
165#define H2_SF_ES_SENT 0x00000002
166
167#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
168#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
169
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200170/* stream flags indicating the reason the stream is blocked */
171#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
172#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
173#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
174#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
175#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
176
Willy Tarreau454f9052017-10-26 19:40:35 +0200177/* stream flags indicating how data is supposed to be sent */
178#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
179#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
180
Christopher Faulet4da05472019-07-18 15:26:47 +0200181/* unused flags: 0x00000400, 0x00000800 */
Willy Tarreau454f9052017-10-26 19:40:35 +0200182
Willy Tarreau67434202017-11-06 20:20:51 +0100183#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100184#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100185
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100186#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
187
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200188#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
189#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
Willy Tarreau3cf69fe2019-05-14 10:44:40 +0200190#define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200191
192
Willy Tarreau18312642017-10-11 07:57:07 +0200193/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
194 * it is being processed in the internal HTTP representation (H1 for now).
195 */
196struct h2s {
197 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100198 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200199 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200200 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200201 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200202 int32_t id; /* stream ID */
203 uint32_t flags; /* H2_SF_* */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +0200204 int sws; /* stream window size, to be added to the mux's initial window size */
Willy Tarreau18312642017-10-11 07:57:07 +0200205 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
206 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200207 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100208 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200209 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200210 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 +0100211 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
212 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 +0200213 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100214 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200215};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200216
Willy Tarreauc6405142017-09-21 20:23:50 +0200217/* descriptor for an h2 frame header */
218struct h2_fh {
219 uint32_t len; /* length, host order, 24 bits */
220 uint32_t sid; /* stream id, host order, 31 bits */
221 uint8_t ft; /* frame type */
222 uint8_t ff; /* frame flags */
223};
224
Willy Tarreau12ae2122019-08-08 18:23:12 +0200225/* trace source and events */
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200226static void h2_trace(enum trace_level level, uint64_t mask, \
227 const struct trace_source *src,
228 const struct ist where, const struct ist func,
229 const void *a1, const void *a2, const void *a3, const void *a4);
Willy Tarreau12ae2122019-08-08 18:23:12 +0200230
231/* The event representation is split like this :
232 * strm - application layer
233 * h2s - internal H2 stream
234 * h2c - internal H2 connection
235 * conn - external connection
236 *
237 */
238static const struct trace_event h2_trace_events[] = {
239#define H2_EV_H2C_NEW (1ULL << 0)
240 { .mask = H2_EV_H2C_NEW, .name = "H2C_NEW", .desc = "new H2 connection" },
241#define H2_EV_H2C_RECV (1ULL << 1)
242 { .mask = H2_EV_H2C_RECV, .name = "H2C_RECV", .desc = "Rx on H2 connection" },
243#define H2_EV_H2C_SEND (1ULL << 2)
244 { .mask = H2_EV_H2C_SEND, .name = "H2C_SEND", .desc = "Tx on H2 connection" },
245#define H2_EV_H2C_FCTL (1ULL << 3)
246 { .mask = H2_EV_H2C_FCTL, .name = "H2C_FCTL", .desc = "H2 connection flow-controlled" },
247#define H2_EV_H2C_BLK (1ULL << 4)
248 { .mask = H2_EV_H2C_BLK, .name = "H2C_BLK", .desc = "H2 connection blocked" },
249#define H2_EV_H2C_WAKE (1ULL << 5)
250 { .mask = H2_EV_H2C_WAKE, .name = "H2C_WAKE", .desc = "H2 connection woken up" },
251#define H2_EV_H2C_END (1ULL << 6)
252 { .mask = H2_EV_H2C_END, .name = "H2C_END", .desc = "H2 connection terminated" },
253#define H2_EV_H2C_ERR (1ULL << 7)
254 { .mask = H2_EV_H2C_ERR, .name = "H2C_ERR", .desc = "error on H2 connection" },
255#define H2_EV_RX_FHDR (1ULL << 8)
256 { .mask = H2_EV_RX_FHDR, .name = "RX_FHDR", .desc = "H2 frame header received" },
257#define H2_EV_RX_FRAME (1ULL << 9)
258 { .mask = H2_EV_RX_FRAME, .name = "RX_FRAME", .desc = "receipt of any H2 frame" },
259#define H2_EV_RX_EOI (1ULL << 10)
260 { .mask = H2_EV_RX_EOI, .name = "RX_EOI", .desc = "receipt of end of H2 input (ES or RST)" },
261#define H2_EV_RX_PREFACE (1ULL << 11)
262 { .mask = H2_EV_RX_PREFACE, .name = "RX_PREFACE", .desc = "receipt of H2 preface" },
263#define H2_EV_RX_DATA (1ULL << 12)
264 { .mask = H2_EV_RX_DATA, .name = "RX_DATA", .desc = "receipt of H2 DATA frame" },
265#define H2_EV_RX_HDR (1ULL << 13)
266 { .mask = H2_EV_RX_HDR, .name = "RX_HDR", .desc = "receipt of H2 HEADERS frame" },
267#define H2_EV_RX_PRIO (1ULL << 14)
268 { .mask = H2_EV_RX_PRIO, .name = "RX_PRIO", .desc = "receipt of H2 PRIORITY frame" },
269#define H2_EV_RX_RST (1ULL << 15)
270 { .mask = H2_EV_RX_RST, .name = "RX_RST", .desc = "receipt of H2 RST_STREAM frame" },
271#define H2_EV_RX_SETTINGS (1ULL << 16)
272 { .mask = H2_EV_RX_SETTINGS, .name = "RX_SETTINGS", .desc = "receipt of H2 SETTINGS frame" },
273#define H2_EV_RX_PUSH (1ULL << 17)
274 { .mask = H2_EV_RX_PUSH, .name = "RX_PUSH", .desc = "receipt of H2 PUSH_PROMISE frame" },
275#define H2_EV_RX_PING (1ULL << 18)
276 { .mask = H2_EV_RX_PING, .name = "RX_PING", .desc = "receipt of H2 PING frame" },
277#define H2_EV_RX_GOAWAY (1ULL << 19)
278 { .mask = H2_EV_RX_GOAWAY, .name = "RX_GOAWAY", .desc = "receipt of H2 GOAWAY frame" },
279#define H2_EV_RX_WU (1ULL << 20)
280 { .mask = H2_EV_RX_WU, .name = "RX_WU", .desc = "receipt of H2 WINDOW_UPDATE frame" },
281#define H2_EV_RX_CONT (1ULL << 21)
282 { .mask = H2_EV_RX_CONT, .name = "RX_CONT", .desc = "receipt of H2 CONTINUATION frame" },
283#define H2_EV_TX_FRAME (1ULL << 22)
284 { .mask = H2_EV_TX_FRAME, .name = "TX_FRAME", .desc = "transmission of any H2 frame" },
285#define H2_EV_TX_EOI (1ULL << 23)
286 { .mask = H2_EV_TX_EOI, .name = "TX_EOI", .desc = "transmission of H2 end of input (ES or RST)" },
287#define H2_EV_TX_PREFACE (1ULL << 24)
288 { .mask = H2_EV_TX_PREFACE, .name = "TX_PREFACE", .desc = "transmission of H2 preface" },
289#define H2_EV_TX_DATA (1ULL << 25)
290 { .mask = H2_EV_TX_DATA, .name = "TX_DATA", .desc = "transmission of H2 DATA frame" },
291#define H2_EV_TX_HDR (1ULL << 26)
292 { .mask = H2_EV_TX_HDR, .name = "TX_HDR", .desc = "transmission of H2 HEADERS frame" },
293#define H2_EV_TX_PRIO (1ULL << 27)
294 { .mask = H2_EV_TX_PRIO, .name = "TX_PRIO", .desc = "transmission of H2 PRIORITY frame" },
295#define H2_EV_TX_RST (1ULL << 28)
296 { .mask = H2_EV_TX_RST, .name = "TX_RST", .desc = "transmission of H2 RST_STREAM frame" },
297#define H2_EV_TX_SETTINGS (1ULL << 29)
298 { .mask = H2_EV_TX_SETTINGS, .name = "TX_SETTINGS", .desc = "transmission of H2 SETTINGS frame" },
299#define H2_EV_TX_PUSH (1ULL << 30)
300 { .mask = H2_EV_TX_PUSH, .name = "TX_PUSH", .desc = "transmission of H2 PUSH_PROMISE frame" },
301#define H2_EV_TX_PING (1ULL << 31)
302 { .mask = H2_EV_TX_PING, .name = "TX_PING", .desc = "transmission of H2 PING frame" },
303#define H2_EV_TX_GOAWAY (1ULL << 32)
304 { .mask = H2_EV_TX_GOAWAY, .name = "TX_GOAWAY", .desc = "transmission of H2 GOAWAY frame" },
305#define H2_EV_TX_WU (1ULL << 33)
306 { .mask = H2_EV_TX_WU, .name = "TX_WU", .desc = "transmission of H2 WINDOW_UPDATE frame" },
307#define H2_EV_TX_CONT (1ULL << 34)
308 { .mask = H2_EV_TX_CONT, .name = "TX_CONT", .desc = "transmission of H2 CONTINUATION frame" },
309#define H2_EV_H2S_NEW (1ULL << 35)
310 { .mask = H2_EV_H2S_NEW, .name = "H2S_NEW", .desc = "new H2 stream" },
311#define H2_EV_H2S_RECV (1ULL << 36)
312 { .mask = H2_EV_H2S_RECV, .name = "H2S_RECV", .desc = "Rx for H2 stream" },
313#define H2_EV_H2S_SEND (1ULL << 37)
314 { .mask = H2_EV_H2S_SEND, .name = "H2S_SEND", .desc = "Tx for H2 stream" },
315#define H2_EV_H2S_FCTL (1ULL << 38)
316 { .mask = H2_EV_H2S_FCTL, .name = "H2S_FCTL", .desc = "H2 stream flow-controlled" },
317#define H2_EV_H2S_BLK (1ULL << 39)
318 { .mask = H2_EV_H2S_BLK, .name = "H2S_BLK", .desc = "H2 stream blocked" },
319#define H2_EV_H2S_WAKE (1ULL << 40)
320 { .mask = H2_EV_H2S_WAKE, .name = "H2S_WAKE", .desc = "H2 stream woken up" },
321#define H2_EV_H2S_END (1ULL << 41)
322 { .mask = H2_EV_H2S_END, .name = "H2S_END", .desc = "H2 stream terminated" },
323#define H2_EV_H2S_ERR (1ULL << 42)
324 { .mask = H2_EV_H2S_ERR, .name = "H2S_ERR", .desc = "error on H2 stream" },
325#define H2_EV_STRM_NEW (1ULL << 43)
326 { .mask = H2_EV_STRM_NEW, .name = "STRM_NEW", .desc = "app-layer stream creation" },
327#define H2_EV_STRM_RECV (1ULL << 44)
328 { .mask = H2_EV_STRM_RECV, .name = "STRM_RECV", .desc = "receiving data for stream" },
329#define H2_EV_STRM_SEND (1ULL << 45)
330 { .mask = H2_EV_STRM_SEND, .name = "STRM_SEND", .desc = "sending data for stream" },
331#define H2_EV_STRM_FULL (1ULL << 46)
332 { .mask = H2_EV_STRM_FULL, .name = "STRM_FULL", .desc = "stream buffer full" },
333#define H2_EV_STRM_WAKE (1ULL << 47)
334 { .mask = H2_EV_STRM_WAKE, .name = "STRM_WAKE", .desc = "stream woken up" },
335#define H2_EV_STRM_SHUT (1ULL << 48)
336 { .mask = H2_EV_STRM_SHUT, .name = "STRM_SHUT", .desc = "stream shutdown" },
337#define H2_EV_STRM_END (1ULL << 49)
338 { .mask = H2_EV_STRM_END, .name = "STRM_END", .desc = "detaching app-layer stream" },
339#define H2_EV_STRM_ERR (1ULL << 50)
340 { .mask = H2_EV_STRM_ERR, .name = "STRM_ERR", .desc = "stream error" },
341#define H2_EV_PROTO_ERR (1ULL << 51)
342 { .mask = H2_EV_PROTO_ERR, .name = "PROTO_ERR", .desc = "protocol error" },
343 { }
344};
345
346static const struct name_desc h2_trace_lockon_args[4] = {
347 /* arg1 */ { /* already used by the connection */ },
348 /* arg2 */ { .name="h2s", .desc="H2 stream" },
349 /* arg3 */ { },
350 /* arg4 */ { }
351};
352
353static const struct name_desc h2_trace_decoding[] = {
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200354#define H2_VERB_CLEAN 1
355 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
356#define H2_VERB_MINIMAL 2
Willy Tarreau12ae2122019-08-08 18:23:12 +0200357 { .name="minimal", .desc="report only h2c/h2s state and flags, no real decoding" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200358#define H2_VERB_SIMPLE 3
Willy Tarreau12ae2122019-08-08 18:23:12 +0200359 { .name="simple", .desc="add request/response status line or frame info when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200360#define H2_VERB_ADVANCED 4
Willy Tarreau12ae2122019-08-08 18:23:12 +0200361 { .name="advanced", .desc="add header fields or frame decoding when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200362#define H2_VERB_COMPLETE 5
Willy Tarreau12ae2122019-08-08 18:23:12 +0200363 { .name="complete", .desc="add full data dump when available" },
364 { /* end */ }
365};
366
367static struct trace_source trace_h2 = {
368 .name = IST("h2"),
369 .desc = "HTTP/2 multiplexer",
370 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200371 .default_cb = h2_trace,
Willy Tarreau12ae2122019-08-08 18:23:12 +0200372 .known_events = h2_trace_events,
373 .lockon_args = h2_trace_lockon_args,
374 .decoding = h2_trace_decoding,
375 .report_events = ~0, // report everything by default
376};
377
378#define TRACE_SOURCE &trace_h2
379INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
380
Willy Tarreau8ceae722018-11-26 11:58:30 +0100381/* the h2c connection pool */
382DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
383
384/* the h2s stream pool */
385DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
386
Willy Tarreaudc572362018-12-12 08:08:05 +0100387/* The default connection window size is 65535, it may only be enlarged using
388 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
389 * we'll pretend we already received the difference between the two to send
390 * an equivalent window update to enlarge it to 2G-1.
391 */
392#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
393
Willy Tarreau455d5682019-05-24 19:42:18 +0200394/* maximum amount of data we're OK with re-aligning for buffer optimizations */
395#define MAX_DATA_REALIGN 1024
396
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200397/* a few settings from the global section */
398static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200399static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100400static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100401static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200402
Willy Tarreau2a856182017-05-16 15:20:39 +0200403/* a dmumy closed stream */
404static const struct h2s *h2_closed_stream = &(const struct h2s){
405 .cs = NULL,
406 .h2c = NULL,
407 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100408 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100409 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200410 .id = 0,
411};
412
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100413/* a dmumy closed stream returning a PROTOCOL_ERROR error */
414static const struct h2s *h2_error_stream = &(const struct h2s){
415 .cs = NULL,
416 .h2c = NULL,
417 .st = H2_SS_CLOSED,
418 .errcode = H2_ERR_PROTOCOL_ERROR,
419 .flags = 0,
420 .id = 0,
421};
422
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100423/* a dmumy closed stream returning a REFUSED_STREAM error */
424static const struct h2s *h2_refused_stream = &(const struct h2s){
425 .cs = NULL,
426 .h2c = NULL,
427 .st = H2_SS_CLOSED,
428 .errcode = H2_ERR_REFUSED_STREAM,
429 .flags = 0,
430 .id = 0,
431};
432
Willy Tarreau2a856182017-05-16 15:20:39 +0200433/* and a dummy idle stream for use with any unannounced stream */
434static const struct h2s *h2_idle_stream = &(const struct h2s){
435 .cs = NULL,
436 .h2c = NULL,
437 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100438 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200439 .id = 0,
440};
441
Olivier Houchard9f6af332018-05-25 14:04:04 +0200442static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200443static int h2_send(struct h2c *h2c);
444static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200445static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200446static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100447static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100448static 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 +0100449static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200450static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100451static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100452static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200453
Willy Tarreauab2ec452019-08-30 07:07:08 +0200454/* returns a h2c state as an abbreviated 3-letter string, or "???" if unknown */
455static inline const char *h2c_st_to_str(enum h2_cs st)
456{
457 switch (st) {
458 case H2_CS_PREFACE: return "PRF";
459 case H2_CS_SETTINGS1: return "STG";
460 case H2_CS_FRAME_H: return "FRH";
461 case H2_CS_FRAME_P: return "FRP";
462 case H2_CS_FRAME_A: return "FRA";
463 case H2_CS_FRAME_E: return "FRE";
464 case H2_CS_ERROR: return "ERR";
465 case H2_CS_ERROR2: return "ER2";
466 default: return "???";
467 }
468}
469
470/* returns a h2s state as an abbreviated 3-letter string, or "???" if unknown */
471static inline const char *h2s_st_to_str(enum h2_ss st)
472{
473 switch (st) {
474 case H2_SS_IDLE: return "IDL"; // idle
475 case H2_SS_RLOC: return "RSL"; // reserved local
476 case H2_SS_RREM: return "RSR"; // reserved remote
477 case H2_SS_OPEN: return "OPN"; // open
478 case H2_SS_HREM: return "HCR"; // half-closed remote
479 case H2_SS_HLOC: return "HCL"; // half-closed local
480 case H2_SS_ERROR : return "ERR"; // error
481 case H2_SS_CLOSED: return "CLO"; // closed
482 default: return "???";
483 }
484}
485
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200486/* the H2 traces always expect that arg1, if non-null, is of type connection
487 * (from which we can derive h2c), that arg2, if non-null, is of type h2s, and
488 * that arg3, if non-null, is either of type htx for tx headers, or of type
489 * buffer for everything else.
490 */
491static void h2_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
492 const struct ist where, const struct ist func,
493 const void *a1, const void *a2, const void *a3, const void *a4)
494{
495 const struct connection *conn = a1;
496 const struct h2c *h2c = conn ? conn->ctx : NULL;
497 const struct h2s *h2s = a2;
498 const struct buffer *buf = a3;
499 const struct htx *htx;
500 int pos;
501
502 if (!h2c) // nothing to add
503 return;
504
505 if (h2c->st0 < H2_CS_FRAME_H) // nothing to add for now
506 return;
507
508 if (src->level >= TRACE_LEVEL_STATE) {
509 if (!h2s)
510 chunk_appendf(&trace_buf, " : h2c=%p st=%d", h2c, h2c->st0);
511 else if (h2s->id <= 0)
512 chunk_appendf(&trace_buf, " : h2c=%p st=%d dsi=%d (st=%d)", h2c, h2c->st0, h2c->dsi, h2s->st);
513 else
514 chunk_appendf(&trace_buf, " : h2c=%p st=%d h2s=%p id=%d st=%d", h2c, h2c->st0, h2s, h2s->id, h2s->st);
515 }
516
517 /* Let's dump decoded requests and responses right after parsing. They
518 * are traced at level USER with a few recognizable flags.
519 */
520 if ((mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW) ||
521 mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR)) && buf)
522 htx = htxbuf(buf); // recv req/res
523 else if (mask == (H2_EV_TX_FRAME|H2_EV_TX_HDR))
524 htx = a3; // send req/res
525 else
526 htx = NULL;
527
Willy Tarreau94f1dcf2019-08-30 07:11:30 +0200528 if (level == TRACE_LEVEL_USER && src->verbosity != H2_VERB_MINIMAL && htx && (pos = htx_get_head(htx)) != -1) {
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200529 const struct htx_blk *blk = htx_get_blk(htx, pos);
530 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
531 enum htx_blk_type type = htx_get_blk_type(blk);
532
533 if (type == HTX_BLK_REQ_SL)
534 chunk_appendf(&trace_buf, " : [%d] H2 REQ: %.*s %.*s %.*s",
535 h2c->dsi,
536 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
537 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
538 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
539 else if (type == HTX_BLK_RES_SL)
540 chunk_appendf(&trace_buf, " : [%d] H2 RES: %.*s %.*s %.*s",
541 h2c->dsi,
542 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
543 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
544 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
545 }
546}
547
Olivier Houchard7a977432019-03-21 15:47:13 +0100548static __inline int
549h2c_is_dead(struct h2c *h2c)
550{
551 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
552 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
553 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
554 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200555 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100556 (conn_xprt_read0_pending(h2c->conn) ||
557 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
558 return 1;
559
560 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100561}
562
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200563/*****************************************************/
564/* functions below are for dynamic buffer management */
565/*****************************************************/
566
Willy Tarreau315d8072017-12-10 22:17:57 +0100567/* indicates whether or not the we may call the h2_recv() function to attempt
568 * to receive data into the buffer and/or demux pending data. The condition is
569 * a bit complex due to some API limits for now. The rules are the following :
570 * - if an error or a shutdown was detected on the connection and the buffer
571 * is empty, we must not attempt to receive
572 * - if the demux buf failed to be allocated, we must not try to receive and
573 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100574 * - if no flag indicates a blocking condition, we may attempt to receive,
575 * regardless of whether the demux buffer is full or not, so that only
576 * de demux part decides whether or not to block. This is needed because
577 * the connection API indeed prevents us from re-enabling receipt that is
578 * already enabled in a polled state, so we must always immediately stop
579 * as soon as the demux can't proceed so as never to hit an end of read
580 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100581 * - otherwise must may not attempt
582 */
583static inline int h2_recv_allowed(const struct h2c *h2c)
584{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200585 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100586 (h2c->st0 >= H2_CS_ERROR ||
587 h2c->conn->flags & CO_FL_ERROR ||
588 conn_xprt_read0_pending(h2c->conn)))
589 return 0;
590
591 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100592 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100593 return 1;
594
595 return 0;
596}
597
Willy Tarreau47b515a2018-12-21 16:09:41 +0100598/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200599static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100600{
601 if (!h2_recv_allowed(h2c))
602 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200603 if ((!consider_buffer || !b_data(&h2c->dbuf))
604 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100605 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200606 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100607}
608
609
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100610/* returns true if the front connection has too many conn_streams attached */
611static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200612{
Willy Tarreaua8754662018-12-23 20:43:58 +0100613 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200614}
615
Willy Tarreau44e973f2018-03-01 17:49:30 +0100616/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
617 * flags are used to figure what buffer was requested. It returns 1 if the
618 * allocation succeeds, in which case the connection is woken up, or 0 if it's
619 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200620 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100621static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200622{
623 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100624 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200625
Willy Tarreau44e973f2018-03-01 17:49:30 +0100626 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200627 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200628 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200629 return 1;
630 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200631
Willy Tarreau51330962019-05-26 09:38:07 +0200632 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100633 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200634
635 if (h2c->flags & H2_CF_DEM_MROOM) {
636 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200637 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200638 }
Willy Tarreau14398122017-09-22 14:26:04 +0200639 return 1;
640 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100641
642 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
643 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200644 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100645 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200646 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100647 return 1;
648 }
649
Willy Tarreau14398122017-09-22 14:26:04 +0200650 return 0;
651}
652
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200653static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200654{
655 struct buffer *buf = NULL;
656
Willy Tarreauc234ae32019-05-13 17:56:11 +0200657 if (likely(!LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100658 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
659 h2c->buf_wait.target = h2c;
660 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100661 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100662 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100663 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200664 __conn_xprt_stop_recv(h2c->conn);
665 }
666 return buf;
667}
668
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200669static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200670{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200671 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100672 b_free(bptr);
Willy Tarreau201840a2019-05-29 17:50:48 +0200673 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200674 }
675}
676
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200677static inline void h2_release_mbuf(struct h2c *h2c)
678{
679 struct buffer *buf;
680 unsigned int count = 0;
681
682 while (b_size(buf = br_head_pick(h2c->mbuf))) {
683 b_free(buf);
684 count++;
685 }
686 if (count)
Willy Tarreau201840a2019-05-29 17:50:48 +0200687 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200688}
689
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100690/* returns the number of allocatable outgoing streams for the connection taking
691 * the last_sid and the reserved ones into account.
692 */
693static inline int h2_streams_left(const struct h2c *h2c)
694{
695 int ret;
696
697 /* consider the number of outgoing streams we're allowed to create before
698 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
699 * nb_reserved is the number of streams which don't yet have an ID.
700 */
701 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
702 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
703 if (ret < 0)
704 ret = 0;
705 return ret;
706}
707
Willy Tarreau00f18a32019-01-26 12:19:01 +0100708/* returns the number of streams in use on a connection to figure if it's
709 * idle or not. We check nb_cs and not nb_streams as the caller will want
710 * to know if it was the last one after a detach().
711 */
712static int h2_used_streams(struct connection *conn)
713{
714 struct h2c *h2c = conn->ctx;
715
716 return h2c->nb_cs;
717}
718
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100719/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100720static int h2_avail_streams(struct connection *conn)
721{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100722 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100723 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100724 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100725
Willy Tarreau6afec462019-01-28 06:40:19 +0100726 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
727 * streams on the connection.
728 */
729 if (h2c->last_sid >= 0)
730 return 0;
731
Willy Tarreau86949782019-01-31 10:42:05 +0100732 /* note: may be negative if a SETTINGS frame changes the limit */
733 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100734
735 /* we must also consider the limit imposed by stream IDs */
736 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100737 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100738 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100739 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
740 ret1 = MIN(ret1, ret2);
741 }
742 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100743}
744
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200745
Willy Tarreau62f52692017-10-08 23:01:42 +0200746/*****************************************************************/
747/* functions below are dedicated to the mux setup and management */
748/*****************************************************************/
749
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200750/* Initialize the mux once it's attached. For outgoing connections, the context
751 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200752 * connections from the fact that the context is still NULL (even during mux
753 * upgrades). <input> is always used as Input buffer and may contain data. It is
754 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200755 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200756static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
757 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200758{
759 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100760 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200761
Willy Tarreau7838a792019-08-12 18:42:03 +0200762 TRACE_ENTER(H2_EV_H2C_NEW, conn);
763
Willy Tarreaubafbe012017-11-24 17:34:44 +0100764 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200765 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200766 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200767
Christopher Faulete9b70722019-04-08 10:46:02 +0200768 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200769 h2c->flags = H2_CF_IS_BACK;
770 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
771 if (tick_isset(prx->timeout.serverfin))
772 h2c->shut_timeout = prx->timeout.serverfin;
773 } else {
774 h2c->flags = H2_CF_NONE;
775 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
776 if (tick_isset(prx->timeout.clientfin))
777 h2c->shut_timeout = prx->timeout.clientfin;
778 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100779
Willy Tarreau0b37d652018-10-03 10:33:02 +0200780 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100781 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100782 if (tick_isset(h2c->timeout)) {
783 t = task_new(tid_bit);
784 if (!t)
785 goto fail;
786
787 h2c->task = t;
788 t->process = h2_timeout_task;
789 t->context = h2c;
790 t->expire = tick_add(now_ms, h2c->timeout);
791 }
Willy Tarreauea392822017-10-31 10:02:25 +0100792
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200793 h2c->wait_event.tasklet = tasklet_new();
794 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200795 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200796 h2c->wait_event.tasklet->process = h2_io_cb;
797 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100798 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200799
Willy Tarreau32218eb2017-09-22 08:07:25 +0200800 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
801 if (!h2c->ddht)
802 goto fail;
803
804 /* Initialise the context. */
805 h2c->st0 = H2_CS_PREFACE;
806 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100807 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200808 h2c->max_id = -1;
809 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100810 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200811 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100812 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200813 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100814 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100815 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200816
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200817 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200818 h2c->dsi = -1;
819 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100820
Willy Tarreau32218eb2017-09-22 08:07:25 +0200821 h2c->last_sid = -1;
822
Willy Tarreau51330962019-05-26 09:38:07 +0200823 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200824 h2c->miw = 65535; /* mux initial window size */
825 h2c->mws = 65535; /* mux window size */
826 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200827 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200828 LIST_INIT(&h2c->send_list);
829 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200830 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100831 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200832
Willy Tarreau3f133572017-10-31 19:21:06 +0100833 if (t)
834 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100835
Willy Tarreau01b44822018-10-03 14:26:37 +0200836 if (h2c->flags & H2_CF_IS_BACK) {
837 /* FIXME: this is temporary, for outgoing connections we need
838 * to immediately allocate a stream until the code is modified
839 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100840 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200841 */
842 struct h2s *h2s;
843
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100844 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200845 if (!h2s)
846 goto fail_stream;
847 }
848
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100849 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200850
Willy Tarreau0f383582018-10-03 14:22:21 +0200851 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200852 h2c_restart_reading(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +0200853 TRACE_LEAVE(H2_EV_H2C_NEW, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200854 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200855 fail_stream:
856 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200857 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200858 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200859 if (h2c->wait_event.tasklet)
860 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100861 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200862 fail_no_h2c:
Willy Tarreau7838a792019-08-12 18:42:03 +0200863 TRACE_DEVEL("leaving in error", H2_EV_H2C_NEW|H2_EV_H2C_END|H2_EV_H2C_ERR, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200864 return -1;
865}
866
Willy Tarreau751f2d02018-10-05 09:35:00 +0200867/* returns the next allocatable outgoing stream ID for the H2 connection, or
868 * -1 if no more is allocatable.
869 */
870static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
871{
872 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100873
874 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200875 id = -1;
876 return id;
877}
878
Willy Tarreau2373acc2017-10-12 17:35:14 +0200879/* returns the stream associated with id <id> or NULL if not found */
880static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
881{
882 struct eb32_node *node;
883
Willy Tarreau751f2d02018-10-05 09:35:00 +0200884 if (id == 0)
885 return (struct h2s *)h2_closed_stream;
886
Willy Tarreau2a856182017-05-16 15:20:39 +0200887 if (id > h2c->max_id)
888 return (struct h2s *)h2_idle_stream;
889
Willy Tarreau2373acc2017-10-12 17:35:14 +0200890 node = eb32_lookup(&h2c->streams_by_id, id);
891 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200892 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200893
894 return container_of(node, struct h2s, by_id);
895}
896
Christopher Faulet73c12072019-04-08 11:23:22 +0200897/* release function. This one should be called to free all resources allocated
898 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200899 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200900static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200901{
Christopher Faulet61840e72019-04-15 09:33:32 +0200902 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200903
Willy Tarreau7838a792019-08-12 18:42:03 +0200904 TRACE_ENTER(H2_EV_H2C_END);
905
Willy Tarreau32218eb2017-09-22 08:07:25 +0200906 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200907 /* The connection must be aattached to this mux to be released */
908 if (h2c->conn && h2c->conn->ctx == h2c)
909 conn = h2c->conn;
910
Willy Tarreau7838a792019-08-12 18:42:03 +0200911 TRACE_DEVEL("freeing h2c", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200912 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200913
Willy Tarreau186e96e2019-05-28 17:21:18 +0200914 if (LIST_ADDED(&h2c->buf_wait.list)) {
915 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
916 LIST_DEL(&h2c->buf_wait.list);
917 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
918 }
Willy Tarreau14398122017-09-22 14:26:04 +0200919
Willy Tarreau44e973f2018-03-01 17:49:30 +0100920 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200921 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100922
Willy Tarreauea392822017-10-31 10:02:25 +0100923 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200924 h2c->task->context = NULL;
925 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100926 h2c->task = NULL;
927 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200928 if (h2c->wait_event.tasklet)
929 tasklet_free(h2c->wait_event.tasklet);
Olivier Houchard0e079372019-04-15 17:51:16 +0200930 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100931 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200932 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100933
Willy Tarreaubafbe012017-11-24 17:34:44 +0100934 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200935 }
936
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200937 if (conn) {
938 conn->mux = NULL;
939 conn->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +0200940 TRACE_DEVEL("freeing conn", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200941
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200942 conn_stop_tracking(conn);
943 conn_full_close(conn);
944 if (conn->destroy_cb)
945 conn->destroy_cb(conn);
946 conn_free(conn);
947 }
Willy Tarreau7838a792019-08-12 18:42:03 +0200948
949 TRACE_LEAVE(H2_EV_H2C_END);
Willy Tarreau62f52692017-10-08 23:01:42 +0200950}
951
952
Willy Tarreau71681172017-10-23 14:39:06 +0200953/******************************************************/
954/* functions below are for the H2 protocol processing */
955/******************************************************/
956
957/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100958static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200959{
960 return h2s ? h2s->id : 0;
961}
962
Willy Tarreau1d4a0f82019-08-02 07:52:08 +0200963/* returns the sum of the stream's own window size and the mux's initial
964 * window, which together form the stream's effective window size.
965 */
966static inline int h2s_mws(const struct h2s *h2s)
967{
968 return h2s->sws + h2s->h2c->miw;
969}
970
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200971/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100972static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200973{
974 if (h2c->msi < 0)
975 return 0;
976
977 if (h2c->msi == h2s_id(h2s))
978 return 0;
979
980 return 1;
981}
982
Willy Tarreau741d6df2017-10-17 08:00:59 +0200983/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100984static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200985{
Willy Tarreau7838a792019-08-12 18:42:03 +0200986 TRACE_POINT(H2_EV_H2C_ERR, h2c->conn,,, (void *)(long)(err));
Willy Tarreau741d6df2017-10-17 08:00:59 +0200987 h2c->errcode = err;
988 h2c->st0 = H2_CS_ERROR;
989}
990
Willy Tarreau175cebb2019-01-24 10:02:24 +0100991/* marks an error on the stream. It may also update an already closed stream
992 * (e.g. to report an error after an RST was received).
993 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100994static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200995{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100996 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +0200997 TRACE_POINT(H2_EV_H2S_ERR, h2s->h2c->conn, h2s,, (void *)(long)(err));
Willy Tarreau2e43f082017-10-17 08:03:59 +0200998 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100999 if (h2s->st < H2_SS_ERROR)
1000 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +01001001 if (h2s->cs)
1002 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +02001003 }
1004}
1005
Willy Tarreau7e094452018-12-19 18:08:52 +01001006/* attempt to notify the data layer of recv availability */
1007static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
1008{
1009 struct wait_event *sw;
1010
1011 if (h2s->recv_wait) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001012 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau7e094452018-12-19 18:08:52 +01001013 sw = h2s->recv_wait;
1014 sw->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001015 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +01001016 h2s->recv_wait = NULL;
1017 }
1018}
1019
1020/* attempt to notify the data layer of send availability */
1021static void __maybe_unused h2s_notify_send(struct h2s *h2s)
1022{
1023 struct wait_event *sw;
1024
Willy Tarreauc234ae32019-05-13 17:56:11 +02001025 if (h2s->send_wait && !LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001026 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau7e094452018-12-19 18:08:52 +01001027 sw = h2s->send_wait;
1028 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +01001029 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001030 tasklet_wakeup(sw->tasklet);
Willy Tarreau7e094452018-12-19 18:08:52 +01001031 }
1032}
1033
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001034/* alerts the data layer, trying to wake it up by all means, following
1035 * this sequence :
1036 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
1037 * - if its subscribed to send, then it's woken up for send
1038 * - if it was subscribed to neither, its ->wake() callback is called
1039 * It is safe to call this function with a closed stream which doesn't have a
1040 * conn_stream anymore.
1041 */
1042static void __maybe_unused h2s_alert(struct h2s *h2s)
1043{
Willy Tarreau7838a792019-08-12 18:42:03 +02001044 TRACE_ENTER(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
1045
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001046 if (h2s->recv_wait || h2s->send_wait) {
1047 h2s_notify_recv(h2s);
1048 h2s_notify_send(h2s);
1049 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001050 else if (h2s->cs && h2s->cs->data_cb->wake != NULL) {
1051 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001052 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau7838a792019-08-12 18:42:03 +02001053 }
1054
1055 TRACE_LEAVE(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001056}
1057
Willy Tarreaue4820742017-07-27 13:37:23 +02001058/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +01001059static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +02001060{
1061 uint8_t *out = frame;
1062
1063 *out = len >> 16;
1064 write_n16(out + 1, len);
1065}
1066
Willy Tarreau54c15062017-10-10 17:10:03 +02001067/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
1068 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
1069 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001070 * available in the buffer's input prior to calling this function. The buffer
1071 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +02001072 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001073static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +02001074 const struct buffer *b, int o)
1075{
Willy Tarreau591d4452018-06-15 17:21:00 +02001076 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 +02001077}
1078
Willy Tarreau1f094672017-11-20 21:27:45 +01001079static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001080{
Willy Tarreau591d4452018-06-15 17:21:00 +02001081 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001082}
1083
Willy Tarreau1f094672017-11-20 21:27:45 +01001084static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001085{
Willy Tarreau591d4452018-06-15 17:21:00 +02001086 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001087}
1088
Willy Tarreau1f094672017-11-20 21:27:45 +01001089static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001090{
Willy Tarreau591d4452018-06-15 17:21:00 +02001091 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001092}
1093
1094
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001095/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
1096 * The algorithm is not obvious. It turns out that H2 headers are neither
1097 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
1098 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +02001099 *
1100 * b0 b1 b2 b3 b4 b5..b8
1101 * +----------+---------+--------+----+----+----------------------+
1102 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
1103 * +----------+---------+--------+----+----+----------------------+
1104 *
1105 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
1106 * we get the sid properly aligned and ordered, and 16 bits of len properly
1107 * ordered as well. The type and flags can be extracted using bit shifts from
1108 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001109 * Returns zero if some bytes are missing, otherwise non-zero on success. The
1110 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +02001111 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001112static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001113{
1114 uint64_t w;
1115
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001116 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +02001117 return 0;
1118
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001119 w = h2_get_n64(b, o + 1);
1120 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +02001121 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
1122 h->ff = w >> 32;
1123 h->ft = w >> 40;
1124 h->len += w >> 48;
1125 return 1;
1126}
1127
1128/* skip the next 9 bytes corresponding to the frame header possibly parsed by
1129 * h2_peek_frame_hdr() above.
1130 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001131static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +02001132{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001133 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +02001134}
1135
1136/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +01001137static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001138{
1139 int ret;
1140
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001141 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +02001142 if (ret > 0)
1143 h2_skip_frame_hdr(b);
1144 return ret;
1145}
1146
Willy Tarreau00dd0782018-03-01 16:31:34 +01001147/* marks stream <h2s> as CLOSED and decrement the number of active streams for
1148 * its connection if the stream was not yet closed. Please use this exclusively
1149 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001150 */
Willy Tarreau00dd0782018-03-01 16:31:34 +01001151static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001152{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001153 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001154 TRACE_ENTER(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001155 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001156 if (!h2s->id)
1157 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +01001158 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +01001159 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
1160 h2s_notify_recv(h2s);
1161 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001162 TRACE_LEAVE(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001163 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001164 h2s->st = H2_SS_CLOSED;
1165}
1166
Willy Tarreau71049cc2018-03-28 13:56:39 +02001167/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
1168static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +01001169{
Willy Tarreau7838a792019-08-12 18:42:03 +02001170 struct connection *conn = h2s->h2c->conn;
1171
1172 TRACE_ENTER(H2_EV_H2S_END, conn, h2s);
1173
Willy Tarreau0a10de62018-03-01 16:27:53 +01001174 h2s_close(h2s);
1175 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +02001176 if (b_size(&h2s->rxbuf)) {
1177 b_free(&h2s->rxbuf);
1178 offer_buffers(NULL, tasks_run_queue);
1179 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001180 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001181 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001182 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001183 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -08001184 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001185 * reference left would be in the h2c send_list/fctl_list, and if
1186 * we're in it, we're getting out anyway
1187 */
Olivier Houchardd360ac62019-03-22 17:37:16 +01001188 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc234ae32019-05-13 17:56:11 +02001189 if (LIST_ADDED(&h2s->sending_list)) {
Willy Tarreau86eded62019-06-14 14:47:49 +02001190 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02001191 LIST_DEL_INIT(&h2s->sending_list);
1192 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001193 tasklet_free(h2s->wait_event.tasklet);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001194 pool_free(pool_head_h2s, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001195
1196 TRACE_LEAVE(H2_EV_H2S_END, conn);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001197}
1198
Willy Tarreaua8e49542018-10-03 18:53:55 +02001199/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
1200 * stream tree. In case of error, nothing is added and NULL is returned. The
1201 * causes of errors can be any failed memory allocation. The caller is
1202 * responsible for checking if the connection may support an extra stream
1203 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001204 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001205static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001206{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001207 struct h2s *h2s;
1208
Willy Tarreau7838a792019-08-12 18:42:03 +02001209 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1210
Willy Tarreaubafbe012017-11-24 17:34:44 +01001211 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001212 if (!h2s)
1213 goto out;
1214
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001215 h2s->wait_event.tasklet = tasklet_new();
1216 if (!h2s->wait_event.tasklet) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001217 pool_free(pool_head_h2s, h2s);
1218 goto out;
1219 }
1220 h2s->send_wait = NULL;
1221 h2s->recv_wait = NULL;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02001222 h2s->wait_event.tasklet->process = h2_deferred_shut;
1223 h2s->wait_event.tasklet->context = h2s;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01001224 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001225 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01001226 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001227 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001228 h2s->cs = NULL;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001229 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001230 h2s->flags = H2_SF_NONE;
1231 h2s->errcode = H2_ERR_NO_ERROR;
1232 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02001233 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001234 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +02001235 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001236
1237 if (h2c->flags & H2_CF_IS_BACK) {
1238 h1m_init_req(&h2s->h1m);
1239 h2s->h1m.err_pos = -1; // don't care about errors on the request path
1240 h2s->h1m.flags |= H1_MF_TOLOWER;
1241 } else {
1242 h1m_init_res(&h2s->h1m);
1243 h2s->h1m.err_pos = -1; // don't care about errors on the response path
1244 h2s->h1m.flags |= H1_MF_TOLOWER;
1245 }
1246
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001247 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001248 if (id > 0)
1249 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001250 else
1251 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001252
1253 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001254 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001255 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001256
Willy Tarreau7838a792019-08-12 18:42:03 +02001257 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001258 return h2s;
1259
1260 out_free_h2s:
1261 pool_free(pool_head_h2s, h2s);
1262 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001263 TRACE_DEVEL("leaving in error", H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001264 return NULL;
1265}
1266
1267/* creates a new stream <id> on the h2c connection and returns it, or NULL in
1268 * case of memory allocation error.
1269 */
1270static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
1271{
1272 struct session *sess = h2c->conn->owner;
1273 struct conn_stream *cs;
1274 struct h2s *h2s;
1275
Willy Tarreau7838a792019-08-12 18:42:03 +02001276 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1277
Willy Tarreaua8e49542018-10-03 18:53:55 +02001278 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
1279 goto out;
1280
1281 h2s = h2s_new(h2c, id);
1282 if (!h2s)
1283 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001284
1285 cs = cs_new(h2c->conn);
1286 if (!cs)
1287 goto out_close;
1288
Olivier Houchard746fb772018-12-15 19:42:00 +01001289 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001290 h2s->cs = cs;
1291 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001292 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001293
1294 if (stream_create_from_cs(cs) < 0)
1295 goto out_free_cs;
1296
Willy Tarreau590a0512018-09-05 11:56:48 +02001297 /* We want the accept date presented to the next stream to be the one
1298 * we have now, the handshake time to be null (since the next stream
1299 * is not delayed by a handshake), and the idle time to count since
1300 * right now.
1301 */
1302 sess->accept_date = date;
1303 sess->tv_accept = now;
1304 sess->t_handshake = 0;
1305
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001306 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001307 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001308 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001309 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001310 return h2s;
1311
1312 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001313 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001314 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001315 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001316 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001317 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001318 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001319 sess_log(sess);
Willy Tarreau7838a792019-08-12 18:42:03 +02001320 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreau45efc072018-10-03 18:27:52 +02001321 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001322}
1323
Willy Tarreau751f2d02018-10-05 09:35:00 +02001324/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1325 * and returns it, or NULL in case of memory allocation error or if the highest
1326 * possible stream ID was reached.
1327 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001328static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001329{
1330 struct h2s *h2s = NULL;
1331
Willy Tarreau7838a792019-08-12 18:42:03 +02001332 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1333
Willy Tarreau86949782019-01-31 10:42:05 +01001334 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001335 goto out;
1336
Willy Tarreaua80dca82019-01-24 17:08:28 +01001337 if (h2_streams_left(h2c) < 1)
1338 goto out;
1339
Willy Tarreau751f2d02018-10-05 09:35:00 +02001340 /* Defer choosing the ID until we send the first message to create the stream */
1341 h2s = h2s_new(h2c, 0);
1342 if (!h2s)
1343 goto out;
1344
1345 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001346 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001347 cs->ctx = h2s;
1348 h2c->nb_cs++;
1349
Willy Tarreau751f2d02018-10-05 09:35:00 +02001350 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001351 if (likely(h2s))
1352 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
1353 else
1354 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn, h2s);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001355 return h2s;
1356}
1357
Willy Tarreaube5b7152017-09-25 16:25:39 +02001358/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1359 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1360 * the various settings codes.
1361 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001362static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001363{
1364 struct buffer *res;
1365 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001366 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001367 int mfs;
Willy Tarreau7838a792019-08-12 18:42:03 +02001368 int ret = 0;
1369
1370 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001371
1372 if (h2c_mux_busy(h2c, NULL)) {
1373 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001374 goto out;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001375 }
1376
Willy Tarreaube5b7152017-09-25 16:25:39 +02001377 chunk_init(&buf, buf_data, sizeof(buf_data));
1378 chunk_memcpy(&buf,
1379 "\x00\x00\x00" /* length : 0 for now */
1380 "\x04\x00" /* type : 4 (settings), flags : 0 */
1381 "\x00\x00\x00\x00", /* stream ID : 0 */
1382 9);
1383
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001384 if (h2c->flags & H2_CF_IS_BACK) {
1385 /* send settings_enable_push=0 */
1386 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1387 }
1388
Willy Tarreaube5b7152017-09-25 16:25:39 +02001389 if (h2_settings_header_table_size != 4096) {
1390 char str[6] = "\x00\x01"; /* header_table_size */
1391
1392 write_n32(str + 2, h2_settings_header_table_size);
1393 chunk_memcat(&buf, str, 6);
1394 }
1395
1396 if (h2_settings_initial_window_size != 65535) {
1397 char str[6] = "\x00\x04"; /* initial_window_size */
1398
1399 write_n32(str + 2, h2_settings_initial_window_size);
1400 chunk_memcat(&buf, str, 6);
1401 }
1402
1403 if (h2_settings_max_concurrent_streams != 0) {
1404 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1405
1406 /* Note: 0 means "unlimited" for haproxy's config but not for
1407 * the protocol, so never send this value!
1408 */
1409 write_n32(str + 2, h2_settings_max_concurrent_streams);
1410 chunk_memcat(&buf, str, 6);
1411 }
1412
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001413 mfs = h2_settings_max_frame_size;
1414 if (mfs > global.tune.bufsize)
1415 mfs = global.tune.bufsize;
1416
1417 if (!mfs)
1418 mfs = global.tune.bufsize;
1419
1420 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001421 char str[6] = "\x00\x05"; /* max_frame_size */
1422
1423 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1424 * match bufsize - rewrite size, but at the moment it seems
1425 * that clients don't take care of it.
1426 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001427 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001428 chunk_memcat(&buf, str, 6);
1429 }
1430
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001431 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001432
1433 res = br_tail(h2c->mbuf);
1434 retry:
1435 if (!h2_get_buf(h2c, res)) {
1436 h2c->flags |= H2_CF_MUX_MALLOC;
1437 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001438 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001439 }
1440
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001441 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001442 if (unlikely(ret <= 0)) {
1443 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001444 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1445 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001446 h2c->flags |= H2_CF_MUX_MFULL;
1447 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001448 }
1449 else {
1450 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001451 ret = 0;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001452 }
1453 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001454 out:
1455 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001456 return ret;
1457}
1458
Willy Tarreau52eed752017-09-22 15:05:09 +02001459/* Try to receive a connection preface, then upon success try to send our
1460 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1461 * missing data. It may return an error in h2c.
1462 */
1463static int h2c_frt_recv_preface(struct h2c *h2c)
1464{
1465 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001466 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001467
Willy Tarreau7838a792019-08-12 18:42:03 +02001468 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
1469
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001470 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001471
1472 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001473 if (ret1 < 0)
1474 sess_log(h2c->conn->owner);
1475
Willy Tarreau52eed752017-09-22 15:05:09 +02001476 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1477 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001478 ret2 = 0;
1479 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02001480 }
1481
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001482 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001483 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001484 b_del(&h2c->dbuf, ret1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001485 out:
1486 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001487 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001488}
1489
Willy Tarreau01b44822018-10-03 14:26:37 +02001490/* Try to send a connection preface, then upon success try to send our
1491 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1492 * missing data. It may return an error in h2c.
1493 */
1494static int h2c_bck_send_preface(struct h2c *h2c)
1495{
1496 struct buffer *res;
Willy Tarreau7838a792019-08-12 18:42:03 +02001497 int ret = 0;
1498
1499 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02001500
1501 if (h2c_mux_busy(h2c, NULL)) {
1502 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001503 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001504 }
1505
Willy Tarreaubcc45952019-05-26 10:05:50 +02001506 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001507 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001508 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001509 h2c->flags |= H2_CF_MUX_MALLOC;
1510 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001511 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001512 }
1513
1514 if (!b_data(res)) {
1515 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001516 ret = b_istput(res, ist(H2_CONN_PREFACE));
1517 if (unlikely(ret <= 0)) {
1518 if (!ret) {
1519 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1520 goto retry;
1521 h2c->flags |= H2_CF_MUX_MFULL;
1522 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001523 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001524 }
1525 else {
1526 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001527 ret = 0;
1528 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001529 }
1530 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001531 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001532 ret = h2c_send_settings(h2c);
1533 out:
1534 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
1535 return ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001536}
1537
Willy Tarreau081d4722017-05-16 21:51:05 +02001538/* try to send a GOAWAY frame on the connection to report an error or a graceful
1539 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1540 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1541 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1542 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1543 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1544 * on unrecoverable failure. It will not attempt to send one again in this last
1545 * case so that it is safe to use h2c_error() to report such errors.
1546 */
1547static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1548{
1549 struct buffer *res;
1550 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02001551 int ret = 0;
Willy Tarreau081d4722017-05-16 21:51:05 +02001552
Willy Tarreau7838a792019-08-12 18:42:03 +02001553 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
1554
1555 if (h2c->flags & H2_CF_GOAWAY_FAILED) {
1556 ret = 1; // claim that it worked
1557 goto out;
1558 }
Willy Tarreau081d4722017-05-16 21:51:05 +02001559
1560 if (h2c_mux_busy(h2c, h2s)) {
1561 if (h2s)
1562 h2s->flags |= H2_SF_BLK_MBUSY;
1563 else
1564 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001565 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001566 }
1567
Willy Tarreau9c218e72019-05-26 10:08:28 +02001568 /* len: 8, type: 7, flags: none, sid: 0 */
1569 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1570
1571 if (h2c->last_sid < 0)
1572 h2c->last_sid = h2c->max_id;
1573
1574 write_n32(str + 9, h2c->last_sid);
1575 write_n32(str + 13, h2c->errcode);
1576
Willy Tarreaubcc45952019-05-26 10:05:50 +02001577 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001578 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001579 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001580 h2c->flags |= H2_CF_MUX_MALLOC;
1581 if (h2s)
1582 h2s->flags |= H2_SF_BLK_MROOM;
1583 else
1584 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001585 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001586 }
1587
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001588 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001589 if (unlikely(ret <= 0)) {
1590 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001591 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1592 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001593 h2c->flags |= H2_CF_MUX_MFULL;
1594 if (h2s)
1595 h2s->flags |= H2_SF_BLK_MROOM;
1596 else
1597 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001598 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001599 }
1600 else {
1601 /* we cannot report this error using GOAWAY, so we mark
1602 * it and claim a success.
1603 */
1604 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1605 h2c->flags |= H2_CF_GOAWAY_FAILED;
Willy Tarreau7838a792019-08-12 18:42:03 +02001606 ret = 1;
1607 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001608 }
1609 }
1610 h2c->flags |= H2_CF_GOAWAY_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02001611 out:
1612 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
Willy Tarreau081d4722017-05-16 21:51:05 +02001613 return ret;
1614}
1615
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001616/* Try to send an RST_STREAM frame on the connection for the indicated stream
1617 * during mux operations. This stream must be valid and cannot be closed
1618 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1619 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1620 * not yet.
1621 *
1622 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1623 * to write the message, it subscribes the stream to future notifications.
1624 */
1625static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1626{
1627 struct buffer *res;
1628 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001629 int ret = 0;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001630
Willy Tarreau7838a792019-08-12 18:42:03 +02001631 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
1632
1633 if (!h2s || h2s->st == H2_SS_CLOSED) {
1634 ret = 1;
1635 goto out;
1636 }
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001637
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001638 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1639 * RST_STREAM in response to a RST_STREAM frame.
1640 */
Willy Tarreau231f6162019-08-06 10:01:40 +02001641 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001642 ret = 1;
1643 goto ignore;
1644 }
1645
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001646 if (h2c_mux_busy(h2c, h2s)) {
1647 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001648 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001649 }
1650
Willy Tarreau9c218e72019-05-26 10:08:28 +02001651 /* len: 4, type: 3, flags: none */
1652 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1653 write_n32(str + 5, h2s->id);
1654 write_n32(str + 9, h2s->errcode);
1655
Willy Tarreaubcc45952019-05-26 10:05:50 +02001656 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001657 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001658 if (!h2_get_buf(h2c, res)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001659 h2c->flags |= H2_CF_MUX_MALLOC;
1660 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001661 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001662 }
1663
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001664 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001665 if (unlikely(ret <= 0)) {
1666 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001667 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1668 goto retry;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001669 h2c->flags |= H2_CF_MUX_MFULL;
1670 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001671 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001672 }
1673 else {
1674 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001675 ret = 0;
1676 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001677 }
1678 }
1679
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001680 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001681 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001682 h2s_close(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001683 out:
1684 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001685 return ret;
1686}
1687
1688/* Try to send an RST_STREAM frame on the connection for the stream being
1689 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001690 * error code, even if the stream is one of the dummy ones, and will update
1691 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001692 *
1693 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1694 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001695 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001696 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001697 */
1698static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1699{
1700 struct buffer *res;
1701 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001702 int ret = 0;
1703
1704 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001705
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001706 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1707 * RST_STREAM in response to a RST_STREAM frame.
1708 */
1709 if (h2c->dft == H2_FT_RST_STREAM) {
1710 ret = 1;
1711 goto ignore;
1712 }
1713
Willy Tarreau27a84c92017-10-17 08:10:17 +02001714 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001715 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001716 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001717 }
1718
Willy Tarreau9c218e72019-05-26 10:08:28 +02001719 /* len: 4, type: 3, flags: none */
1720 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1721
1722 write_n32(str + 5, h2c->dsi);
1723 write_n32(str + 9, h2s->errcode);
1724
Willy Tarreaubcc45952019-05-26 10:05:50 +02001725 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001726 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001727 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001728 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001729 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001730 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001731 }
1732
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001733 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001734 if (unlikely(ret <= 0)) {
1735 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001736 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1737 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001738 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001739 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001740 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001741 }
1742 else {
1743 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001744 ret = 0;
1745 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001746 }
1747 }
1748
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001749 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001750 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001751 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001752 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001753 }
1754
Willy Tarreau7838a792019-08-12 18:42:03 +02001755 out:
1756 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001757 return ret;
1758}
1759
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001760/* try to send an empty DATA frame with the ES flag set to notify about the
1761 * end of stream and match a shutdown(write). If an ES was already sent as
1762 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1763 * on success or zero if nothing was done. In case of lack of room to write the
1764 * message, it subscribes the requesting stream to future notifications.
1765 */
1766static int h2_send_empty_data_es(struct h2s *h2s)
1767{
1768 struct h2c *h2c = h2s->h2c;
1769 struct buffer *res;
1770 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02001771 int ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001772
Willy Tarreau7838a792019-08-12 18:42:03 +02001773 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
1774
1775 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) {
1776 ret = 1;
1777 goto out;
1778 }
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001779
1780 if (h2c_mux_busy(h2c, h2s)) {
1781 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001782 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001783 }
1784
Willy Tarreau9c218e72019-05-26 10:08:28 +02001785 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1786 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1787 write_n32(str + 5, h2s->id);
1788
Willy Tarreaubcc45952019-05-26 10:05:50 +02001789 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001790 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001791 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001792 h2c->flags |= H2_CF_MUX_MALLOC;
1793 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001794 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001795 }
1796
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001797 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001798 if (likely(ret > 0)) {
1799 h2s->flags |= H2_SF_ES_SENT;
1800 }
1801 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001802 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1803 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001804 h2c->flags |= H2_CF_MUX_MFULL;
1805 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001806 }
1807 else {
1808 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001809 ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001810 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001811 out:
1812 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001813 return ret;
1814}
1815
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001816/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001817 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001818 * is automatically updated accordingly. If the stream is orphaned, it is
1819 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001820 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001821static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001822{
Willy Tarreau7838a792019-08-12 18:42:03 +02001823 struct h2c *h2c = h2s->h2c;
1824
1825 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn, h2s);
1826
Christopher Fauletf02ca002019-03-07 16:21:34 +01001827 if (!h2s->cs) {
1828 /* this stream was already orphaned */
1829 h2s_destroy(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001830 TRACE_DEVEL("leaving with no h2s", H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001831 return;
1832 }
1833
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001834 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001835 if (h2s->st == H2_SS_OPEN)
1836 h2s->st = H2_SS_HREM;
1837 else if (h2s->st == H2_SS_HLOC)
1838 h2s_close(h2s);
1839 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001840
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001841 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1842 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1843 h2s->cs->flags |= CS_FL_ERR_PENDING;
1844 if (h2s->cs->flags & CS_FL_EOS)
1845 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001846
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001847 if (h2s->st < H2_SS_ERROR)
1848 h2s->st = H2_SS_ERROR;
1849 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001850
1851 h2s_alert(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001852 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001853}
1854
1855/* wake the streams attached to the connection, whose id is greater than <last>
1856 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001857 */
Willy Tarreau23482912019-05-07 15:23:14 +02001858static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001859{
1860 struct eb32_node *node;
1861 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001862
Willy Tarreau7838a792019-08-12 18:42:03 +02001863 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn);
1864
Christopher Fauletf02ca002019-03-07 16:21:34 +01001865 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001866 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1867 while (node) {
1868 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001869 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001870 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001871 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001872
Christopher Fauletf02ca002019-03-07 16:21:34 +01001873 /* Wake all streams with unassigned ID (ID == 0) */
1874 node = eb32_lookup(&h2c->streams_by_id, 0);
1875 while (node) {
1876 h2s = container_of(node, struct h2s, by_id);
1877 if (h2s->id > 0)
1878 break;
1879 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001880 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001881 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001882
1883 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001884}
1885
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001886/* Wake up all blocked streams whose window size has become positive after the
1887 * mux's initial window was adjusted. This should be done after having processed
1888 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001889 */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001890static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001891{
1892 struct h2s *h2s;
1893 struct eb32_node *node;
1894
Willy Tarreau7838a792019-08-12 18:42:03 +02001895 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
1896
Willy Tarreau3421aba2017-07-27 15:41:03 +02001897 node = eb32_first(&h2c->streams_by_id);
1898 while (node) {
1899 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001900 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001901 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02001902 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001903 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001904 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001905 node = eb32_next(node);
1906 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001907
1908 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001909}
1910
1911/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1912 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001913 * return an error in h2c. The caller must have already verified frame length
1914 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001915 */
1916static int h2c_handle_settings(struct h2c *h2c)
1917{
1918 unsigned int offset;
1919 int error;
1920
Willy Tarreau7838a792019-08-12 18:42:03 +02001921 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
1922
Willy Tarreau3421aba2017-07-27 15:41:03 +02001923 if (h2c->dff & H2_F_SETTINGS_ACK) {
1924 if (h2c->dfl) {
1925 error = H2_ERR_FRAME_SIZE_ERROR;
1926 goto fail;
1927 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001928 goto done;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001929 }
1930
Willy Tarreau3421aba2017-07-27 15:41:03 +02001931 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001932 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02001933 goto out0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001934
1935 /* parse the frame */
1936 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001937 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1938 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001939
1940 switch (type) {
1941 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1942 /* we need to update all existing streams with the
1943 * difference from the previous iws.
1944 */
1945 if (arg < 0) { // RFC7540#6.5.2
1946 error = H2_ERR_FLOW_CONTROL_ERROR;
1947 goto fail;
1948 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001949 h2c->miw = arg;
1950 break;
1951 case H2_SETTINGS_MAX_FRAME_SIZE:
1952 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1953 error = H2_ERR_PROTOCOL_ERROR;
1954 goto fail;
1955 }
1956 h2c->mfs = arg;
1957 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001958 case H2_SETTINGS_ENABLE_PUSH:
1959 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1960 error = H2_ERR_PROTOCOL_ERROR;
1961 goto fail;
1962 }
1963 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001964 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1965 if (h2c->flags & H2_CF_IS_BACK) {
1966 /* the limit is only for the backend; for the frontend it is our limit */
1967 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1968 arg = h2_settings_max_concurrent_streams;
1969 h2c->streams_limit = arg;
1970 }
1971 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001972 }
1973 }
1974
1975 /* need to ACK this frame now */
1976 h2c->st0 = H2_CS_FRAME_A;
Willy Tarreau7838a792019-08-12 18:42:03 +02001977 done:
1978 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001979 return 1;
1980 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001981 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001982 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02001983 out0:
1984 TRACE_DEVEL("leaving with missing data or error", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001985 return 0;
1986}
1987
1988/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1989 * success or one of the h2_status values.
1990 */
1991static int h2c_ack_settings(struct h2c *h2c)
1992{
1993 struct buffer *res;
1994 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02001995 int ret = 0;
1996
1997 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001998
1999 if (h2c_mux_busy(h2c, NULL)) {
2000 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002001 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002002 }
2003
Willy Tarreau9c218e72019-05-26 10:08:28 +02002004 memcpy(str,
2005 "\x00\x00\x00" /* length : 0 (no data) */
2006 "\x04" "\x01" /* type : 4, flags : ACK */
2007 "\x00\x00\x00\x00" /* stream ID */, 9);
2008
Willy Tarreaubcc45952019-05-26 10:05:50 +02002009 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002010 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002011 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002012 h2c->flags |= H2_CF_MUX_MALLOC;
2013 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002014 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002015 }
2016
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002017 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02002018 if (unlikely(ret <= 0)) {
2019 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002020 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2021 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002022 h2c->flags |= H2_CF_MUX_MFULL;
2023 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002024 }
2025 else {
2026 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002027 ret = 0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002028 }
2029 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002030 out:
2031 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002032 return ret;
2033}
2034
Willy Tarreaucf68c782017-10-10 17:11:41 +02002035/* processes a PING frame and schedules an ACK if needed. The caller must pass
2036 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01002037 * missing data. The caller must have already verified frame length
2038 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02002039 */
2040static int h2c_handle_ping(struct h2c *h2c)
2041{
Willy Tarreaucf68c782017-10-10 17:11:41 +02002042 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01002043 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02002044 h2c->st0 = H2_CS_FRAME_A;
2045 return 1;
2046}
2047
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002048/* Try to send a window update for stream id <sid> and value <increment>.
2049 * Returns > 0 on success or zero on missing room or failure. It may return an
2050 * error in h2c.
2051 */
2052static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
2053{
2054 struct buffer *res;
2055 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002056 int ret = 0;
2057
2058 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002059
2060 if (h2c_mux_busy(h2c, NULL)) {
2061 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002062 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002063 }
2064
Willy Tarreau9c218e72019-05-26 10:08:28 +02002065 /* length: 4, type: 8, flags: none */
2066 memcpy(str, "\x00\x00\x04\x08\x00", 5);
2067 write_n32(str + 5, sid);
2068 write_n32(str + 9, increment);
2069
Willy Tarreaubcc45952019-05-26 10:05:50 +02002070 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002071 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002072 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002073 h2c->flags |= H2_CF_MUX_MALLOC;
2074 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002075 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002076 }
2077
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002078 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002079 if (unlikely(ret <= 0)) {
2080 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002081 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2082 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002083 h2c->flags |= H2_CF_MUX_MFULL;
2084 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002085 }
2086 else {
2087 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002088 ret = 0;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002089 }
2090 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002091 out:
2092 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002093 return ret;
2094}
2095
2096/* try to send pending window update for the connection. It's safe to call it
2097 * with no pending updates. Returns > 0 on success or zero on missing room or
2098 * failure. It may return an error in h2c.
2099 */
2100static int h2c_send_conn_wu(struct h2c *h2c)
2101{
2102 int ret = 1;
2103
Willy Tarreau7838a792019-08-12 18:42:03 +02002104 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2105
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002106 if (h2c->rcvd_c <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002107 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002108
Willy Tarreau97aaa672018-12-23 09:49:04 +01002109 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
2110 /* increase the advertised connection window to 2G on
2111 * first update.
2112 */
2113 h2c->flags |= H2_CF_WINDOW_OPENED;
2114 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
2115 }
2116
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002117 /* send WU for the connection */
2118 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
2119 if (ret > 0)
2120 h2c->rcvd_c = 0;
2121
Willy Tarreau7838a792019-08-12 18:42:03 +02002122 out:
2123 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002124 return ret;
2125}
2126
2127/* try to send pending window update for the current dmux stream. It's safe to
2128 * call it with no pending updates. Returns > 0 on success or zero on missing
2129 * room or failure. It may return an error in h2c.
2130 */
2131static int h2c_send_strm_wu(struct h2c *h2c)
2132{
2133 int ret = 1;
2134
Willy Tarreau7838a792019-08-12 18:42:03 +02002135 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2136
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002137 if (h2c->rcvd_s <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002138 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002139
2140 /* send WU for the stream */
2141 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
2142 if (ret > 0)
2143 h2c->rcvd_s = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002144 out:
2145 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002146 return ret;
2147}
2148
Willy Tarreaucf68c782017-10-10 17:11:41 +02002149/* try to send an ACK for a ping frame on the connection. Returns > 0 on
2150 * success, 0 on missing data or one of the h2_status values.
2151 */
2152static int h2c_ack_ping(struct h2c *h2c)
2153{
2154 struct buffer *res;
2155 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02002156 int ret = 0;
2157
2158 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002159
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002160 if (b_data(&h2c->dbuf) < 8)
Willy Tarreau7838a792019-08-12 18:42:03 +02002161 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002162
2163 if (h2c_mux_busy(h2c, NULL)) {
2164 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002165 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002166 }
2167
Willy Tarreaucf68c782017-10-10 17:11:41 +02002168 memcpy(str,
2169 "\x00\x00\x08" /* length : 8 (same payload) */
2170 "\x06" "\x01" /* type : 6, flags : ACK */
2171 "\x00\x00\x00\x00" /* stream ID */, 9);
2172
2173 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002174 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002175
Willy Tarreau9c218e72019-05-26 10:08:28 +02002176 res = br_tail(h2c->mbuf);
2177 retry:
2178 if (!h2_get_buf(h2c, res)) {
2179 h2c->flags |= H2_CF_MUX_MALLOC;
2180 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002181 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02002182 }
2183
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002184 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02002185 if (unlikely(ret <= 0)) {
2186 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002187 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2188 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002189 h2c->flags |= H2_CF_MUX_MFULL;
2190 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002191 }
2192 else {
2193 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002194 ret = 0;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002195 }
2196 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002197 out:
2198 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002199 return ret;
2200}
2201
Willy Tarreau26f95952017-07-27 17:18:30 +02002202/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
2203 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01002204 * h2c or h2s. The caller must have already verified frame length and stream ID
2205 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02002206 */
2207static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
2208{
2209 int32_t inc;
2210 int error;
2211
Willy Tarreau7838a792019-08-12 18:42:03 +02002212 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
2213
Willy Tarreau26f95952017-07-27 17:18:30 +02002214 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002215 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002216 goto out0;
Willy Tarreau26f95952017-07-27 17:18:30 +02002217
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002218 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02002219
2220 if (h2c->dsi != 0) {
2221 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02002222
2223 /* it's not an error to receive WU on a closed stream */
2224 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau7838a792019-08-12 18:42:03 +02002225 goto done;
Willy Tarreau26f95952017-07-27 17:18:30 +02002226
2227 if (!inc) {
2228 error = H2_ERR_PROTOCOL_ERROR;
2229 goto strm_err;
2230 }
2231
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002232 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002233 error = H2_ERR_FLOW_CONTROL_ERROR;
2234 goto strm_err;
2235 }
2236
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002237 h2s->sws += inc;
2238 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002239 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02002240 if (h2s->send_wait && !LIST_ADDED(&h2s->list))
Olivier Houcharddddfe312018-10-10 18:51:00 +02002241 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02002242 }
2243 }
2244 else {
2245 /* connection window update */
2246 if (!inc) {
2247 error = H2_ERR_PROTOCOL_ERROR;
2248 goto conn_err;
2249 }
2250
2251 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
2252 error = H2_ERR_FLOW_CONTROL_ERROR;
2253 goto conn_err;
2254 }
2255
2256 h2c->mws += inc;
2257 }
2258
Willy Tarreau7838a792019-08-12 18:42:03 +02002259 done:
2260 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002261 return 1;
2262
2263 conn_err:
2264 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002265 out0:
2266 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002267 return 0;
2268
2269 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002270 h2s_error(h2s, error);
2271 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002272 TRACE_DEVEL("leaving on stream error", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002273 return 0;
2274}
2275
Willy Tarreaue96b0922017-10-30 00:28:29 +01002276/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01002277 * the last ID. Returns > 0 on success or zero on missing data. The caller must
2278 * have already verified frame length and stream ID validity. Described in
2279 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01002280 */
2281static int h2c_handle_goaway(struct h2c *h2c)
2282{
Willy Tarreaue96b0922017-10-30 00:28:29 +01002283 int last;
2284
Willy Tarreau7838a792019-08-12 18:42:03 +02002285 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002286 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002287 if (b_data(&h2c->dbuf) < h2c->dfl) {
2288 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002289 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002290 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01002291
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002292 last = h2_get_n32(&h2c->dbuf, 0);
2293 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01002294 if (h2c->last_sid < 0)
2295 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02002296 h2_wake_some_streams(h2c, last);
Willy Tarreau7838a792019-08-12 18:42:03 +02002297 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002298 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002299}
2300
Willy Tarreau92153fc2017-12-03 19:46:19 +01002301/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01002302 * invalid. Returns > 0 on success or zero on missing data. It may return an
2303 * error in h2c. The caller must have already verified frame length and stream
2304 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01002305 */
2306static int h2c_handle_priority(struct h2c *h2c)
2307{
Willy Tarreau7838a792019-08-12 18:42:03 +02002308 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
2309
Willy Tarreau92153fc2017-12-03 19:46:19 +01002310 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002311 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002312 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002313 return 0;
2314
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002315 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01002316 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01002317 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002318 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002319 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002320 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002321 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002322 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002323}
2324
Willy Tarreaucd234e92017-08-18 10:59:39 +02002325/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01002326 * Returns > 0 on success or zero on missing data. The caller must have already
2327 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02002328 */
2329static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
2330{
Willy Tarreau7838a792019-08-12 18:42:03 +02002331 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
2332
Willy Tarreaucd234e92017-08-18 10:59:39 +02002333 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002334 if (b_data(&h2c->dbuf) < h2c->dfl) {
2335 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002336 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002337 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002338
2339 /* late RST, already handled */
Willy Tarreau7838a792019-08-12 18:42:03 +02002340 if (h2s->st == H2_SS_CLOSED) {
2341 TRACE_DEVEL("leaving on stream closed", H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002342 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002343 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002344
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002345 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002346 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002347
2348 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01002349 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01002350 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002351 }
2352
2353 h2s->flags |= H2_SF_RST_RCVD;
Willy Tarreau7838a792019-08-12 18:42:03 +02002354 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002355 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002356}
2357
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002358/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2359 * It may return an error in h2c or h2s. The caller must consider that the
2360 * return value is the new h2s in case one was allocated (most common case).
2361 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002362 * errors here are reported as connection errors since it's impossible to
2363 * recover from such errors after the compression context has been altered.
2364 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002365static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002366{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002367 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002368 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002369 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002370 int error;
2371
Willy Tarreau7838a792019-08-12 18:42:03 +02002372 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2373
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002374 if (!b_size(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002375 goto out; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02002376
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002377 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002378 goto out; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02002379
2380 /* now either the frame is complete or the buffer is complete */
2381 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002382 /* The stream exists/existed, this must be a trailers frame */
2383 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02002384 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
2385 /* unrecoverable error ? */
2386 if (h2c->st0 >= H2_CS_ERROR)
2387 goto out;
2388
2389 if (error == 0)
2390 goto out; // missing data
2391
2392 if (error < 0) {
2393 /* Failed to decode this frame (e.g. too large request)
2394 * but the HPACK decompressor is still synchronized.
2395 */
2396 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2397 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002398 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002399 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002400 goto done;
2401 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002402 /* the connection was already killed by an RST, let's consume
2403 * the data and send another RST.
2404 */
2405 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2406 h2s = (struct h2s*)h2_error_stream;
2407 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002408 }
2409 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2410 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2411 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002412 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002413 goto conn_err;
2414 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002415 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2416 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002417
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002418 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002419
Willy Tarreau25919232019-01-03 14:48:18 +01002420 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002421 if (h2c->st0 >= H2_CS_ERROR)
2422 goto out;
2423
Willy Tarreau25919232019-01-03 14:48:18 +01002424 if (error <= 0) {
2425 if (error == 0)
2426 goto out; // missing data
2427
2428 /* Failed to decode this stream (e.g. too large request)
2429 * but the HPACK decompressor is still synchronized.
2430 */
2431 h2s = (struct h2s*)h2_error_stream;
2432 goto send_rst;
2433 }
2434
Willy Tarreau22de8d32018-09-05 19:55:58 +02002435 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002436 * positively from h2c_frt_stream_new(), the stream will report the error,
2437 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002438 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02002439 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02002440 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002441 h2s = (struct h2s*)h2_refused_stream;
2442 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002443 }
2444
2445 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002446 h2s->rxbuf = rxbuf;
2447 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002448 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002449
Willy Tarreau88d138e2019-01-02 19:38:14 +01002450 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002451 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002452 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002453
2454 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002455 if (h2s->st == H2_SS_OPEN)
2456 h2s->st = H2_SS_HREM;
2457 else
2458 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002459 }
2460
Willy Tarreau3a429f02019-01-03 11:41:50 +01002461 /* update the max stream ID if the request is being processed */
2462 if (h2s->id > h2c->max_id)
2463 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002464
Willy Tarreau7838a792019-08-12 18:42:03 +02002465 TRACE_USER("received H2 request", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW, h2c->conn,, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002466 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002467
2468 conn_err:
2469 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002470 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002471
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002472 out:
2473 h2_release_buf(h2c, &rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002474 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002475 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002476
2477 send_rst:
2478 /* make the demux send an RST for the current stream. We may only
2479 * do this if we're certain that the HEADERS frame was properly
2480 * decompressed so that the HPACK decoder is still kept up to date.
2481 */
2482 h2_release_buf(h2c, &rxbuf);
2483 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002484
2485 TRACE_USER("rejected H2 request", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn,, &rxbuf);
2486 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau96a10c22018-12-23 18:30:44 +01002487 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002488}
2489
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002490/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2491 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2492 * errors here are reported as connection errors since it's impossible to
2493 * recover from such errors after the compression context has been altered.
2494 */
2495static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2496{
2497 int error;
2498
Willy Tarreau7838a792019-08-12 18:42:03 +02002499 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2500
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002501 if (!b_size(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002502 goto fail; // empty buffer
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002503
2504 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002505 goto fail; // incomplete frame
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002506
Willy Tarreau1915ca22019-01-24 11:49:37 +01002507 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002508
Willy Tarreau25919232019-01-03 14:48:18 +01002509 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002510 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002511 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002512
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002513 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2514 /* RFC7540#5.1 */
2515 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2516 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002517 goto fail;
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002518 }
2519
Willy Tarreau25919232019-01-03 14:48:18 +01002520 if (error <= 0) {
2521 if (error == 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002522 goto fail; // missing data
Willy Tarreau25919232019-01-03 14:48:18 +01002523
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002524 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002525 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002526 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002527 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002528 }
2529
Christopher Fauletfa922f02019-05-07 10:55:17 +02002530 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002531 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002532
Willy Tarreau927b88b2019-03-04 08:03:25 +01002533 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002534 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002535 else if (h2s->flags & H2_SF_ES_RCVD) {
2536 if (h2s->st == H2_SS_OPEN)
2537 h2s->st = H2_SS_HREM;
2538 else if (h2s->st == H2_SS_HLOC)
2539 h2s_close(h2s);
2540 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002541
Willy Tarreau7838a792019-08-12 18:42:03 +02002542 TRACE_USER("received H2 response", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn,, &h2s->rxbuf);
2543 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002544 return h2s;
Willy Tarreau7838a792019-08-12 18:42:03 +02002545 fail:
2546 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2547 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002548}
2549
Willy Tarreau454f9052017-10-26 19:40:35 +02002550/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2551 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2552 */
2553static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2554{
2555 int error;
2556
Willy Tarreau7838a792019-08-12 18:42:03 +02002557 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2558
Willy Tarreau454f9052017-10-26 19:40:35 +02002559 /* note that empty DATA frames are perfectly valid and sometimes used
2560 * to signal an end of stream (with the ES flag).
2561 */
2562
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002563 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002564 goto fail; // empty buffer
Willy Tarreau454f9052017-10-26 19:40:35 +02002565
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002566 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002567 goto fail; // incomplete frame
Willy Tarreau454f9052017-10-26 19:40:35 +02002568
2569 /* now either the frame is complete or the buffer is complete */
2570
Willy Tarreau454f9052017-10-26 19:40:35 +02002571 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2572 /* RFC7540#6.1 */
2573 error = H2_ERR_STREAM_CLOSED;
2574 goto strm_err;
2575 }
2576
Christopher Faulet4f09ec82019-06-19 09:25:58 +02002577 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002578 /* RFC7540#8.1.2 */
2579 error = H2_ERR_PROTOCOL_ERROR;
2580 goto strm_err;
2581 }
2582
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002583 if (!h2_frt_transfer_data(h2s))
Willy Tarreau7838a792019-08-12 18:42:03 +02002584 goto fail;
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002585
Willy Tarreau454f9052017-10-26 19:40:35 +02002586 /* call the upper layers to process the frame, then let the upper layer
2587 * notify the stream about any change.
2588 */
2589 if (!h2s->cs) {
Willy Tarreau082c4572019-08-06 10:11:02 +02002590 /* The upper layer has already closed, this may happen on
2591 * 4xx/redirects during POST, or when receiving a response
2592 * from an H2 server after the client has aborted.
2593 */
2594 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002595 goto strm_err;
2596 }
2597
Willy Tarreau8f650c32017-11-21 19:36:21 +01002598 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002599 goto fail;
Willy Tarreau8f650c32017-11-21 19:36:21 +01002600
Willy Tarreau721c9742017-11-07 11:05:42 +01002601 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002602 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002603 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002604 }
2605
2606 /* check for completion : the callee will change this to FRAME_A or
2607 * FRAME_H once done.
2608 */
2609 if (h2c->st0 == H2_CS_FRAME_P)
Willy Tarreau7838a792019-08-12 18:42:03 +02002610 goto fail;
Willy Tarreau454f9052017-10-26 19:40:35 +02002611
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002612 /* last frame */
2613 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002614 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002615 if (h2s->st == H2_SS_OPEN)
2616 h2s->st = H2_SS_HREM;
2617 else
2618 h2s_close(h2s);
2619
Willy Tarreau1915ca22019-01-24 11:49:37 +01002620 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2621 /* RFC7540#8.1.2 */
2622 error = H2_ERR_PROTOCOL_ERROR;
2623 goto strm_err;
2624 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002625 }
2626
Willy Tarreau7838a792019-08-12 18:42:03 +02002627 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02002628 return 1;
2629
Willy Tarreau454f9052017-10-26 19:40:35 +02002630 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002631 h2s_error(h2s, error);
2632 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002633 fail:
2634 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02002635 return 0;
2636}
2637
Willy Tarreau63864812019-08-07 14:25:20 +02002638/* check that the current frame described in h2c->{dsi,dft,dfl,dff,...} is
2639 * valid for the current stream state. This is needed only after parsing the
2640 * frame header but in practice it can be performed at any time during
2641 * H2_CS_FRAME_P since no state transition happens there. Returns >0 on success
2642 * or 0 in case of error, in which case either h2s or h2c will carry an error.
2643 */
2644static int h2_frame_check_vs_state(struct h2c *h2c, struct h2s *h2s)
2645{
Willy Tarreau7838a792019-08-12 18:42:03 +02002646 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
2647
Willy Tarreau63864812019-08-07 14:25:20 +02002648 if (h2s->st == H2_SS_IDLE &&
2649 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2650 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2651 * this state MUST be treated as a connection error
2652 */
2653 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2654 if (!h2c->nb_streams) {
2655 /* only log if no other stream can report the error */
2656 sess_log(h2c->conn->owner);
2657 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002658 TRACE_DEVEL("leaving in error (idle&!hdrs&!prio)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02002659 return 0;
2660 }
2661
2662 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2663 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2664 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2665 * this state MUST be treated as a stream error.
2666 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2667 * PUSH_PROMISE/CONTINUATION cause connection errors.
2668 */
2669 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2670 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2671 else
2672 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02002673 TRACE_DEVEL("leaving in error (hrem&!wu&!rst&!prio)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02002674 return 0;
2675 }
2676
2677 /* Below the management of frames received in closed state is a
2678 * bit hackish because the spec makes strong differences between
2679 * streams closed by receiving RST, sending RST, and seeing ES
2680 * in both directions. In addition to this, the creation of a
2681 * new stream reusing the identifier of a closed one will be
2682 * detected here. Given that we cannot keep track of all closed
2683 * streams forever, we consider that unknown closed streams were
2684 * closed on RST received, which allows us to respond with an
2685 * RST without breaking the connection (eg: to abort a transfer).
2686 * Some frames have to be silently ignored as well.
2687 */
2688 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2689 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
2690 /* #5.1.1: The identifier of a newly
2691 * established stream MUST be numerically
2692 * greater than all streams that the initiating
2693 * endpoint has opened or reserved. This
2694 * governs streams that are opened using a
2695 * HEADERS frame and streams that are reserved
2696 * using PUSH_PROMISE. An endpoint that
2697 * receives an unexpected stream identifier
2698 * MUST respond with a connection error.
2699 */
2700 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02002701 TRACE_DEVEL("leaving in error (closed&hdrmask)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02002702 return 0;
2703 }
2704
2705 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
2706 /* RFC7540#5.1:closed: an endpoint that
2707 * receives any frame other than PRIORITY after
2708 * receiving a RST_STREAM MUST treat that as a
2709 * stream error of type STREAM_CLOSED.
2710 *
2711 * Note that old streams fall into this category
2712 * and will lead to an RST being sent.
2713 *
2714 * However, we cannot generalize this to all frame types. Those
2715 * carrying compression state must still be processed before
2716 * being dropped or we'll desynchronize the decoder. This can
2717 * happen with request trailers received after sending an
2718 * RST_STREAM, or with header/trailers responses received after
2719 * sending RST_STREAM (aborted stream).
2720 */
2721 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2722 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002723 TRACE_DEVEL("leaving in error (rst_rcvd&hdrmask)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02002724 return 0;
2725 }
2726
2727 /* RFC7540#5.1:closed: if this state is reached as a
2728 * result of sending a RST_STREAM frame, the peer that
2729 * receives the RST_STREAM might have already sent
2730 * frames on the stream that cannot be withdrawn. An
2731 * endpoint MUST ignore frames that it receives on
2732 * closed streams after it has sent a RST_STREAM
2733 * frame. An endpoint MAY choose to limit the period
2734 * over which it ignores frames and treat frames that
2735 * arrive after this time as being in error.
2736 */
2737 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
2738 /* RFC7540#5.1:closed: any frame other than
2739 * PRIO/WU/RST in this state MUST be treated as
2740 * a connection error
2741 */
2742 if (h2c->dft != H2_FT_RST_STREAM &&
2743 h2c->dft != H2_FT_PRIORITY &&
2744 h2c->dft != H2_FT_WINDOW_UPDATE) {
2745 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02002746 TRACE_DEVEL("leaving in error (rst_sent&!rst&!prio&!wu)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02002747 return 0;
2748 }
2749 }
2750 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002751 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02002752 return 1;
2753}
2754
Willy Tarreaubc933932017-10-09 16:21:43 +02002755/* process Rx frames to be demultiplexed */
2756static void h2_process_demux(struct h2c *h2c)
2757{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002758 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002759 struct h2_fh hdr;
2760 unsigned int padlen = 0;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002761 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002762
Willy Tarreau7838a792019-08-12 18:42:03 +02002763 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
2764
Willy Tarreau081d4722017-05-16 21:51:05 +02002765 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002766 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02002767
2768 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2769 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002770 TRACE_STATE("expecting preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02002771 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau7838a792019-08-12 18:42:03 +02002772 goto out;
2773
Willy Tarreau52eed752017-09-22 15:05:09 +02002774 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2775 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002776 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002777 TRACE_PROTO("failed to receive preface", H2_EV_RX_PREFACE|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02002778 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002779 sess_log(h2c->conn->owner);
2780 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002781 goto fail;
2782 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002783 TRACE_PROTO("received preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02002784
2785 h2c->max_id = 0;
2786 h2c->st0 = H2_CS_SETTINGS1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002787 TRACE_STATE("switching to SETTINGS1", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02002788 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002789
2790 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002791 /* ensure that what is pending is a valid SETTINGS frame
2792 * without an ACK.
2793 */
Willy Tarreau7838a792019-08-12 18:42:03 +02002794 TRACE_STATE("expecting settings", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002795 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002796 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002797 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002798 TRACE_PROTO("failed to receive settings", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_SETTINGS|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002799 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002800 sess_log(h2c->conn->owner);
2801 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002802 goto fail;
2803 }
2804
2805 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2806 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau7838a792019-08-12 18:42:03 +02002807 TRACE_PROTO("unexpected frame type or flags", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_SETTINGS|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002808 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2809 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002810 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002811 goto fail;
2812 }
2813
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002814 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002815 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau7838a792019-08-12 18:42:03 +02002816 TRACE_PROTO("invalid settings frame length", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_SETTINGS|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002817 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2818 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002819 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002820 goto fail;
2821 }
2822
Willy Tarreau3bf69182018-12-21 15:34:50 +01002823 /* that's OK, switch to FRAME_P to process it. This is
2824 * a SETTINGS frame whose header has already been
2825 * deleted above.
2826 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002827 padlen = 0;
2828 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002829 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002830 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002831
2832 /* process as many incoming frames as possible below */
Willy Tarreau7838a792019-08-12 18:42:03 +02002833 while (1) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002834 int ret = 0;
2835
Willy Tarreau7838a792019-08-12 18:42:03 +02002836 if (!b_data(&h2c->dbuf)) {
2837 TRACE_DEVEL("no more Rx data", H2_EV_RX_FRAME, h2c->conn);
2838 break;
2839 }
2840
2841 if (h2c->st0 >= H2_CS_ERROR) {
2842 TRACE_STATE("end of connection reported", H2_EV_RX_FRAME|H2_EV_RX_EOI, h2c->conn);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002843 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02002844 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002845
2846 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreau30d05f32019-08-06 15:49:51 +02002847 h2c->rcvd_s = 0;
2848
Willy Tarreau7838a792019-08-12 18:42:03 +02002849 TRACE_STATE("expecting H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002850 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002851 break;
2852
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002853 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002854 TRACE_PROTO("invalid H2 frame length", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002855 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002856 if (!h2c->nb_streams) {
2857 /* only log if no other stream can report the error */
2858 sess_log(h2c->conn->owner);
2859 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002860 break;
2861 }
2862
Christopher Fauletdd2a5622019-06-18 12:22:38 +02002863 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002864 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2865 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2866 * we read the pad length and drop it from the remaining
2867 * payload (one byte + the 9 remaining ones = 10 total
2868 * removed), so we have a frame payload starting after the
2869 * pad len. Flow controlled frames (DATA) also count the
2870 * padlen in the flow control, so it must be adjusted.
2871 */
2872 if (hdr.len < 1) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002873 TRACE_PROTO("invalid H2 padded frame length", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau3bf69182018-12-21 15:34:50 +01002874 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2875 sess_log(h2c->conn->owner);
2876 goto fail;
2877 }
2878 hdr.len--;
2879
2880 if (b_data(&h2c->dbuf) < 10)
2881 break; // missing padlen
2882
2883 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2884
2885 if (padlen > hdr.len) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002886 TRACE_PROTO("invalid H2 padding length", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau3bf69182018-12-21 15:34:50 +01002887 /* RFC7540#6.1 : pad length = length of
2888 * frame payload or greater => error.
2889 */
2890 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2891 sess_log(h2c->conn->owner);
2892 goto fail;
2893 }
2894
2895 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2896 h2c->rcvd_c++;
2897 h2c->rcvd_s++;
2898 }
2899 b_del(&h2c->dbuf, 1);
2900 }
2901 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002902 TRACE_STATE("received H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002903
2904 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002905 h2c->dfl = hdr.len;
2906 h2c->dsi = hdr.sid;
2907 h2c->dft = hdr.ft;
2908 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002909 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002910 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau7838a792019-08-12 18:42:03 +02002911 TRACE_STATE("switching to FRAME_P state", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002912
2913 /* check for minimum basic frame format validity */
2914 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2915 if (ret != H2_ERR_NO_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002916 TRACE_PROTO("received invalid H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002917 h2c_error(h2c, ret);
2918 sess_log(h2c->conn->owner);
2919 goto fail;
2920 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002921 }
2922
Willy Tarreau9fd5aa82019-08-06 15:21:45 +02002923 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
2924 * H2_CS_FRAME_P indicates an incomplete previous operation
2925 * (most often the first attempt) and requires some validity
2926 * checks for the frame and the current state. The two other
2927 * ones are set after completion (or abortion) and must skip
2928 * validity checks.
2929 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002930 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2931
Willy Tarreau567beb82018-12-18 16:52:44 +01002932 if (tmp_h2s != h2s && h2s && h2s->cs &&
2933 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02002934 conn_xprt_read0_pending(h2c->conn) ||
2935 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02002936 (h2s->flags & H2_SF_ES_RCVD) ||
2937 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002938 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02002939 TRACE_DEVEL("notifying stream before switching SID", H2_EV_RX_FRAME|H2_EV_STRM_WAKE, h2c->conn, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002940 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002941 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002942 }
2943 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002944
Willy Tarreau63864812019-08-07 14:25:20 +02002945 if (h2c->st0 == H2_CS_FRAME_E ||
Willy Tarreau7838a792019-08-12 18:42:03 +02002946 (h2c->st0 == H2_CS_FRAME_P && !h2_frame_check_vs_state(h2c, h2s))) {
2947 TRACE_PROTO("stream error reported", H2_EV_RX_FRAME|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002948 goto strm_err;
Willy Tarreau7838a792019-08-12 18:42:03 +02002949 }
Willy Tarreauc0da1962017-10-30 18:38:00 +01002950
Willy Tarreau7e98c052017-10-10 15:56:59 +02002951 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002952 case H2_FT_SETTINGS:
Willy Tarreau7838a792019-08-12 18:42:03 +02002953 if (h2c->st0 == H2_CS_FRAME_P) {
2954 TRACE_PROTO("receiving H2 SETTINGS frame", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn, h2s);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002955 ret = h2c_handle_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02002956 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002957
Willy Tarreau7838a792019-08-12 18:42:03 +02002958 if (h2c->st0 == H2_CS_FRAME_A) {
2959 TRACE_PROTO("sending H2 SETTINGS ACK frame", H2_EV_TX_FRAME|H2_EV_RX_SETTINGS, h2c->conn, h2s);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002960 ret = h2c_ack_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02002961 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002962 break;
2963
Willy Tarreaucf68c782017-10-10 17:11:41 +02002964 case H2_FT_PING:
Willy Tarreau7838a792019-08-12 18:42:03 +02002965 if (h2c->st0 == H2_CS_FRAME_P) {
2966 TRACE_PROTO("receiving H2 PING frame", H2_EV_RX_FRAME|H2_EV_RX_PING, h2c->conn, h2s);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002967 ret = h2c_handle_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02002968 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02002969
Willy Tarreau7838a792019-08-12 18:42:03 +02002970 if (h2c->st0 == H2_CS_FRAME_A) {
2971 TRACE_PROTO("sending H2 PING ACK frame", H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn, h2s);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002972 ret = h2c_ack_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02002973 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02002974 break;
2975
Willy Tarreau26f95952017-07-27 17:18:30 +02002976 case H2_FT_WINDOW_UPDATE:
Willy Tarreau7838a792019-08-12 18:42:03 +02002977 if (h2c->st0 == H2_CS_FRAME_P) {
2978 TRACE_PROTO("receiving H2 WINDOW_UPDATE frame", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn, h2s);
Willy Tarreau26f95952017-07-27 17:18:30 +02002979 ret = h2c_handle_window_update(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02002980 }
Willy Tarreau26f95952017-07-27 17:18:30 +02002981 break;
2982
Willy Tarreau61290ec2017-10-17 08:19:21 +02002983 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002984 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2985 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2986 * frames' parsers consume all following CONTINUATION
2987 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002988 */
Willy Tarreau7838a792019-08-12 18:42:03 +02002989 TRACE_PROTO("received unexpected H2 CONTINUATION frame", H2_EV_RX_FRAME|H2_EV_RX_CONT|H2_EV_H2C_ERR, h2c->conn, h2s);
Willy Tarreauea18f862018-12-22 20:19:26 +01002990 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2991 sess_log(h2c->conn->owner);
2992 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002993
Willy Tarreau13278b42017-10-13 19:23:14 +02002994 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002995 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002996 TRACE_PROTO("receiving H2 HEADERS frame", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002997 if (h2c->flags & H2_CF_IS_BACK)
2998 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2999 else
3000 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003001 if (tmp_h2s) {
3002 h2s = tmp_h2s;
3003 ret = 1;
3004 }
3005 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003006 break;
3007
Willy Tarreau454f9052017-10-26 19:40:35 +02003008 case H2_FT_DATA:
Willy Tarreau7838a792019-08-12 18:42:03 +02003009 if (h2c->st0 == H2_CS_FRAME_P) {
3010 TRACE_PROTO("receiving H2 DATA frame", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02003011 ret = h2c_frt_handle_data(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003012 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003013
Willy Tarreau7838a792019-08-12 18:42:03 +02003014 if (h2c->st0 == H2_CS_FRAME_A) {
3015 TRACE_PROTO("sending stream WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02003016 ret = h2c_send_strm_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003017 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003018 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02003019
Willy Tarreau92153fc2017-12-03 19:46:19 +01003020 case H2_FT_PRIORITY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003021 if (h2c->st0 == H2_CS_FRAME_P) {
3022 TRACE_PROTO("receiving H2 PRIORITY frame", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn, h2s);
Willy Tarreau92153fc2017-12-03 19:46:19 +01003023 ret = h2c_handle_priority(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003024 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01003025 break;
3026
Willy Tarreaucd234e92017-08-18 10:59:39 +02003027 case H2_FT_RST_STREAM:
Willy Tarreau7838a792019-08-12 18:42:03 +02003028 if (h2c->st0 == H2_CS_FRAME_P) {
3029 TRACE_PROTO("receiving H2 RST_STREAM frame", H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02003030 ret = h2c_handle_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003031 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02003032 break;
3033
Willy Tarreaue96b0922017-10-30 00:28:29 +01003034 case H2_FT_GOAWAY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003035 if (h2c->st0 == H2_CS_FRAME_P) {
3036 TRACE_PROTO("receiving H2 GOAWAY frame", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn, h2s);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003037 ret = h2c_handle_goaway(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003038 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01003039 break;
3040
Willy Tarreau1c661982017-10-30 13:52:01 +01003041 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02003042 default:
Willy Tarreau7838a792019-08-12 18:42:03 +02003043 TRACE_PROTO("receiving H2 ignored frame", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003044 /* drop frames that we ignore. They may be larger than
3045 * the buffer so we drain all of their contents until
3046 * we reach the end.
3047 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003048 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3049 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003050 h2c->dfl -= ret;
3051 ret = h2c->dfl == 0;
3052 }
3053
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003054 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01003055 /* We may have to send an RST if not done yet */
Willy Tarreau7838a792019-08-12 18:42:03 +02003056 if (h2s->st == H2_SS_ERROR) {
3057 TRACE_STATE("stream error, switching to FRAME_E", H2_EV_RX_FRAME|H2_EV_H2S_ERR, h2c->conn, h2s);
Willy Tarreaua20a5192017-12-27 11:02:06 +01003058 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003059 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003060
Willy Tarreau7838a792019-08-12 18:42:03 +02003061 if (h2c->st0 == H2_CS_FRAME_E) {
3062 TRACE_PROTO("sending H2 RST_STREAM frame", H2_EV_TX_FRAME|H2_EV_TX_RST|H2_EV_TX_EOI, h2c->conn, h2s);
Willy Tarreaua20a5192017-12-27 11:02:06 +01003063 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003064 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003065
Willy Tarreau7e98c052017-10-10 15:56:59 +02003066 /* error or missing data condition met above ? */
Willy Tarreau7838a792019-08-12 18:42:03 +02003067 if (ret <= 0) {
3068 TRACE_DEVEL("insufficient data to proceed", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003069 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003070 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003071
3072 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003073 TRACE_STATE("switching to FRAME_H", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003074 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003075 h2c->st0 = H2_CS_FRAME_H;
3076 }
3077 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003078
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003079 if (h2c->rcvd_c > 0 &&
Willy Tarreau7838a792019-08-12 18:42:03 +02003080 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) {
3081 TRACE_PROTO("sending H2 WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003082 h2c_send_conn_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003083 }
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003084
Willy Tarreau52eed752017-09-22 15:05:09 +02003085 fail:
3086 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01003087 if (h2s && h2s->cs &&
3088 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003089 conn_xprt_read0_pending(h2c->conn) ||
3090 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003091 (h2s->flags & H2_SF_ES_RCVD) ||
3092 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003093 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003094 TRACE_DEVEL("notifying stream before switching SID", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003095 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01003096 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003097 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01003098
Willy Tarreau7838a792019-08-12 18:42:03 +02003099 if (old_iw != h2c->miw) {
3100 TRACE_STATE("notifying streams about SFCTL increase", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003101 h2c_unblock_sfctl(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003102 }
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003103
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003104 h2c_restart_reading(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003105 out:
3106 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreaubc933932017-10-09 16:21:43 +02003107}
3108
3109/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
3110 * the end.
3111 */
3112static int h2_process_mux(struct h2c *h2c)
3113{
Olivier Houchard998410a2019-04-15 19:23:37 +02003114 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003115
Willy Tarreau7838a792019-08-12 18:42:03 +02003116 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3117
Willy Tarreau01b44822018-10-03 14:26:37 +02003118 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3119 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
3120 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
3121 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
3122 if (h2c->st0 == H2_CS_ERROR) {
3123 h2c->st0 = H2_CS_ERROR2;
3124 sess_log(h2c->conn->owner);
3125 }
3126 goto fail;
3127 }
3128 h2c->st0 = H2_CS_SETTINGS1;
3129 }
3130 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01003131 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau7838a792019-08-12 18:42:03 +02003132 goto done;
Willy Tarreau01b44822018-10-03 14:26:37 +02003133 }
3134
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003135 /* start by sending possibly pending window updates */
Willy Tarreaue74679a2019-08-06 15:39:32 +02003136 if (h2c->rcvd_s > 0 &&
3137 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3138 h2c_send_strm_wu(h2c) < 0)
3139 goto fail;
3140
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003141 if (h2c->rcvd_c > 0 &&
3142 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3143 h2c_send_conn_wu(h2c) < 0)
3144 goto fail;
3145
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003146 /* First we always process the flow control list because the streams
3147 * waiting there were already elected for immediate emission but were
3148 * blocked just on this.
3149 */
3150
Olivier Houchard998410a2019-04-15 19:23:37 +02003151 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003152 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
3153 h2c->st0 >= H2_CS_ERROR)
3154 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02003155
Willy Tarreauc234ae32019-05-13 17:56:11 +02003156 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01003157 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003158
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003159 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02003160 /* For some reason, the upper layer failed to subsribe again,
3161 * so remove it from the send_list
3162 */
3163 if (!h2s->send_wait) {
3164 LIST_DEL_INIT(&h2s->list);
3165 continue;
3166 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003167 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01003168 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003169 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003170 }
3171
Olivier Houchard998410a2019-04-15 19:23:37 +02003172 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003173 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
3174 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02003175
Willy Tarreauc234ae32019-05-13 17:56:11 +02003176 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01003177 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003178
Olivier Houchard998410a2019-04-15 19:23:37 +02003179 /* For some reason, the upper layer failed to subsribe again,
3180 * so remove it from the send_list
3181 */
3182 if (!h2s->send_wait) {
3183 LIST_DEL_INIT(&h2s->list);
3184 continue;
3185 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003186 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003187 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01003188 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003189 tasklet_wakeup(h2s->send_wait->tasklet);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003190 }
3191
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003192 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01003193 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02003194 if (h2c->st0 == H2_CS_ERROR) {
3195 if (h2c->max_id >= 0) {
3196 h2c_send_goaway_error(h2c, NULL);
3197 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
Willy Tarreau7838a792019-08-12 18:42:03 +02003198 goto out0;
Willy Tarreau081d4722017-05-16 21:51:05 +02003199 }
3200
3201 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
3202 }
Willy Tarreau081d4722017-05-16 21:51:05 +02003203 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003204 done:
3205 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
3206 return 1;
3207 out0:
3208 TRACE_DEVEL("leaving in blocked situation", H2_EV_H2C_WAKE, h2c->conn);
3209 return 0;
Willy Tarreaubc933932017-10-09 16:21:43 +02003210}
3211
Willy Tarreau62f52692017-10-08 23:01:42 +02003212
Willy Tarreau479998a2018-11-18 06:30:59 +01003213/* Attempt to read data, and subscribe if none available.
3214 * The function returns 1 if data has been received, otherwise zero.
3215 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003216static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003217{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003218 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003219 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003220 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02003221 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003222
Willy Tarreau7838a792019-08-12 18:42:03 +02003223 TRACE_ENTER(H2_EV_H2C_RECV, h2c->conn);
3224
3225 if (h2c->wait_event.events & SUB_RETRY_RECV) {
3226 TRACE_DEVEL("leaving on sub_recv", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003227 return (b_data(&h2c->dbuf));
Willy Tarreau7838a792019-08-12 18:42:03 +02003228 }
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003229
Willy Tarreau7838a792019-08-12 18:42:03 +02003230 if (!h2_recv_allowed(h2c)) {
3231 TRACE_DEVEL("leaving on !recv_allowed", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003232 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003233 }
Willy Tarreaua2af5122017-10-09 11:56:46 +02003234
Willy Tarreau44e973f2018-03-01 17:49:30 +01003235 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003236 if (!buf) {
3237 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02003238 TRACE_DEVEL("leaving on !alloc", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003239 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003240 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003241
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003242 b_realign_if_empty(buf);
3243 if (!b_data(buf)) {
3244 /* try to pre-align the buffer like the
3245 * rxbufs will be to optimize memory copies. We'll make
3246 * sure that the frame header lands at the end of the
3247 * HTX block to alias it upon recv. We cannot use the
3248 * head because rcv_buf() will realign the buffer if
3249 * it's empty. Thus we cheat and pretend we already
3250 * have a few bytes there.
3251 */
3252 max = buf_room_for_htx_data(buf) + 9;
3253 buf->head = sizeof(struct htx) - 9;
3254 }
3255 else
3256 max = b_room(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01003257
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003258 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003259
Willy Tarreau7838a792019-08-12 18:42:03 +02003260 if (max && !ret && h2_recv_allowed(h2c)) {
3261 TRACE_DATA("failed to receive data, subscribing", H2_EV_H2C_RECV, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003262 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02003263 } else if (ret)
3264 TRACE_DATA("received data", H2_EV_H2C_RECV, h2c->conn,,, (void*)(long)ret);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003265
Olivier Houcharda1411e62018-08-17 18:42:48 +02003266 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01003267 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02003268 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard46677732018-11-29 17:06:17 +01003269 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02003270 }
3271
Willy Tarreau7838a792019-08-12 18:42:03 +02003272 if (b_data(buf) == buf->size) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003273 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02003274 TRACE_STATE("demux buffer full", H2_EV_H2C_RECV|H2_EV_H2C_BLK);
3275 }
3276
3277 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003278 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003279}
3280
Willy Tarreau479998a2018-11-18 06:30:59 +01003281/* Try to send data if possible.
3282 * The function returns 1 if data have been sent, otherwise zero.
3283 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003284static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003285{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003286 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02003287 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003288 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003289
Willy Tarreau7838a792019-08-12 18:42:03 +02003290 TRACE_ENTER(H2_EV_H2C_SEND, h2c->conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003291
Willy Tarreau7838a792019-08-12 18:42:03 +02003292 if (conn->flags & CO_FL_ERROR) {
3293 TRACE_DEVEL("leaving on error", H2_EV_H2C_SEND, h2c->conn);
3294 return 1;
3295 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003296
Willy Tarreaua2af5122017-10-09 11:56:46 +02003297 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
3298 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003299 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003300 }
3301
Willy Tarreaubc933932017-10-09 16:21:43 +02003302 /* This loop is quite simple : it tries to fill as much as it can from
3303 * pending streams into the existing buffer until it's reportedly full
3304 * or the end of send requests is reached. Then it tries to send this
3305 * buffer's contents out, marks it not full if at least one byte could
3306 * be sent, and tries again.
3307 *
3308 * The snd_buf() function normally takes a "flags" argument which may
3309 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
3310 * data immediately comes and CO_SFL_STREAMER to indicate that the
3311 * connection is streaming lots of data (used to increase TLS record
3312 * size at the expense of latency). The former can be sent any time
3313 * there's a buffer full flag, as it indicates at least one stream
3314 * attempted to send and failed so there are pending data. An
3315 * alternative would be to set it as long as there's an active stream
3316 * but that would be problematic for ACKs until we have an absolute
3317 * guarantee that all waiters have at least one byte to send. The
3318 * latter should possibly not be set for now.
3319 */
3320
3321 done = 0;
3322 while (!done) {
3323 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003324 unsigned int released = 0;
3325 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02003326
3327 /* fill as much as we can into the current buffer */
3328 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
3329 done = h2_process_mux(h2c);
3330
Olivier Houchard2b094432019-01-29 18:28:36 +01003331 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003332 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01003333
Willy Tarreaubc933932017-10-09 16:21:43 +02003334 if (conn->flags & CO_FL_ERROR)
3335 break;
3336
3337 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
3338 flags |= CO_SFL_MSG_MORE;
3339
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003340 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3341 if (b_data(buf)) {
3342 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003343 if (!ret) {
3344 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003345 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003346 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003347 sent = 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003348 TRACE_DATA("sent data", H2_EV_H2C_SEND, h2c->conn,, buf, (void*)(long)ret);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003349 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003350 if (b_data(buf)) {
3351 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003352 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003353 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003354 }
3355 b_free(buf);
3356 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02003357 }
Willy Tarreaubc933932017-10-09 16:21:43 +02003358
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003359 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003360 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003361
Willy Tarreaubc933932017-10-09 16:21:43 +02003362 /* wrote at least one byte, the buffer is not full anymore */
3363 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
3364 }
3365
Willy Tarreaua2af5122017-10-09 11:56:46 +02003366 if (conn->flags & CO_FL_SOCK_WR_SH) {
3367 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02003368 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02003369 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003370 /* We're not full anymore, so we can wake any task that are waiting
3371 * for us.
3372 */
3373 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01003374 struct h2s *h2s;
3375
3376 list_for_each_entry(h2s, &h2c->send_list, list) {
3377 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
3378 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02003379
Willy Tarreauc234ae32019-05-13 17:56:11 +02003380 if (LIST_ADDED(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01003381 continue;
3382
Olivier Houchard998410a2019-04-15 19:23:37 +02003383 /* For some reason, the upper layer failed to subsribe again,
3384 * so remove it from the send_list
3385 */
3386 if (!h2s->send_wait) {
3387 LIST_DEL_INIT(&h2s->list);
3388 continue;
3389 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01003390 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003391 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau7838a792019-08-12 18:42:03 +02003392 TRACE_DEVEL("waking up pending stream", H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn, h2s);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003393 tasklet_wakeup(h2s->send_wait->tasklet);
Olivier Houchardd360ac62019-03-22 17:37:16 +01003394 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003395 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003396 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003397 /* We're done, no more to send */
Willy Tarreau7838a792019-08-12 18:42:03 +02003398 if (!br_data(h2c->mbuf)) {
3399 TRACE_DEVEL("leaving with everything sent", H2_EV_H2C_SEND, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003400 return sent;
Willy Tarreau7838a792019-08-12 18:42:03 +02003401 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003402schedule:
Willy Tarreau7838a792019-08-12 18:42:03 +02003403 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) {
3404 TRACE_STATE("more data to send, subscribing", H2_EV_H2C_SEND, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003405 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02003406 }
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003407
Willy Tarreau7838a792019-08-12 18:42:03 +02003408 TRACE_DEVEL("leaving with some data left to send", H2_EV_H2C_SEND, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003409 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003410}
3411
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003412/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003413static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
3414{
3415 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02003416 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003417
Willy Tarreau7838a792019-08-12 18:42:03 +02003418 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3419
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003420 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02003421 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003422 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02003423 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01003424 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02003425 h2_process(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003426
3427 TRACE_LEAVE(H2_EV_H2C_WAKE);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003428 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003429}
Willy Tarreaua2af5122017-10-09 11:56:46 +02003430
Willy Tarreau62f52692017-10-08 23:01:42 +02003431/* callback called on any event by the connection handler.
3432 * It applies changes and returns zero, or < 0 if it wants immediate
3433 * destruction of the connection (which normally doesn not happen in h2).
3434 */
Olivier Houchard7505f942018-08-21 18:10:44 +02003435static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003436{
Olivier Houchard7505f942018-08-21 18:10:44 +02003437 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003438
Willy Tarreau7838a792019-08-12 18:42:03 +02003439 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3440
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003441 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01003442 h2_process_demux(h2c);
3443
3444 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003445 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003446
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003447 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01003448 h2c->flags &= ~H2_CF_DEM_DFULL;
3449 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003450 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003451
Willy Tarreau0b37d652018-10-03 10:33:02 +02003452 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01003453 /* frontend is stopping, reload likely in progress, let's try
3454 * to announce a graceful shutdown if not yet done. We don't
3455 * care if it fails, it will be tried again later.
3456 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003457 TRACE_STATE("proxy stopped, sending GOAWAY", H2_EV_H2C_WAKE|H2_EV_TX_FRAME, conn);
Willy Tarreau8ec14062017-12-30 18:08:13 +01003458 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3459 if (h2c->last_sid < 0)
3460 h2c->last_sid = (1U << 31) - 1;
3461 h2c_send_goaway_error(h2c, NULL);
3462 }
3463 }
3464
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003465 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003466 * If we received early data, and the handshake is done, wake
3467 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003468 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003469 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
3470 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
3471 struct eb32_node *node;
3472 struct h2s *h2s;
3473
3474 h2c->flags |= H2_CF_WAIT_FOR_HS;
3475 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
3476
3477 while (node) {
3478 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01003479 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01003480 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003481 node = eb32_next(node);
3482 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003483 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003484
Willy Tarreau26bd7612017-10-09 16:47:04 +02003485 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01003486 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
3487 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
3488 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02003489 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003490
3491 if (eb_is_empty(&h2c->streams_by_id)) {
3492 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003493 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003494 TRACE_DEVEL("leaving after releasing the connection", H2_EV_H2C_WAKE);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003495 return -1;
3496 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003497 }
3498
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003499 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003500 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003501
Olivier Houchard53216e72018-10-10 15:46:36 +02003502 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3503 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3504 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003505 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003506 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3507 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003508 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003509
Willy Tarreau3f133572017-10-31 19:21:06 +01003510 if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02003511 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3512 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003513 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003514
Olivier Houchard7505f942018-08-21 18:10:44 +02003515 h2_send(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003516 TRACE_LEAVE(H2_EV_H2C_WAKE, conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003517 return 0;
3518}
3519
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003520/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003521static int h2_wake(struct connection *conn)
3522{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003523 struct h2c *h2c = conn->ctx;
Willy Tarreau7838a792019-08-12 18:42:03 +02003524 int ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003525
Willy Tarreau7838a792019-08-12 18:42:03 +02003526 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3527 ret = h2_process(h2c);
3528 TRACE_LEAVE(H2_EV_H2C_WAKE);
3529 return ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003530}
3531
Willy Tarreauea392822017-10-31 10:02:25 +01003532/* Connection timeout management. The principle is that if there's no receipt
3533 * nor sending for a certain amount of time, the connection is closed. If the
3534 * MUX buffer still has lying data or is not allocatable, the connection is
3535 * immediately killed. If it's allocatable and empty, we attempt to send a
3536 * GOAWAY frame.
3537 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003538static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003539{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003540 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003541 int expired = tick_is_expired(t->expire, now_ms);
3542
Willy Tarreau7838a792019-08-12 18:42:03 +02003543 TRACE_ENTER(H2_EV_H2C_WAKE, h2c ? h2c->conn : NULL);
3544
3545 if (!expired && h2c) {
3546 TRACE_DEVEL("leaving (not expired)", H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01003547 return t;
Willy Tarreau7838a792019-08-12 18:42:03 +02003548 }
Willy Tarreauea392822017-10-31 10:02:25 +01003549
Olivier Houchard3f795f72019-04-17 22:51:06 +02003550 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003551
3552 if (!h2c) {
3553 /* resources were already deleted */
Willy Tarreau7838a792019-08-12 18:42:03 +02003554 TRACE_DEVEL("leaving (not more h2c)", H2_EV_H2C_WAKE);
Willy Tarreau0975f112018-03-29 15:22:59 +02003555 return NULL;
3556 }
3557
3558 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003559 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003560 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003561
Willy Tarreau662fafc2019-05-26 09:43:07 +02003562 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003563 /* don't even try to send a GOAWAY, the buffer is stuck */
3564 h2c->flags |= H2_CF_GOAWAY_FAILED;
3565 }
3566
3567 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003568 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003569 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3570 h2c->flags |= H2_CF_GOAWAY_FAILED;
3571
Willy Tarreau662fafc2019-05-26 09:43:07 +02003572 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003573 unsigned int released = 0;
3574 struct buffer *buf;
3575
3576 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3577 if (b_data(buf)) {
3578 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3579 if (!ret)
3580 break;
3581 b_del(buf, ret);
3582 if (b_data(buf))
3583 break;
3584 b_free(buf);
3585 released++;
3586 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003587 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003588
3589 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003590 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003591 }
Willy Tarreauea392822017-10-31 10:02:25 +01003592
Willy Tarreau0975f112018-03-29 15:22:59 +02003593 /* either we can release everything now or it will be done later once
3594 * the last stream closes.
3595 */
3596 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003597 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003598
Willy Tarreau7838a792019-08-12 18:42:03 +02003599 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreauea392822017-10-31 10:02:25 +01003600 return NULL;
3601}
3602
3603
Willy Tarreau62f52692017-10-08 23:01:42 +02003604/*******************************************/
3605/* functions below are used by the streams */
3606/*******************************************/
3607
3608/*
3609 * Attach a new stream to a connection
3610 * (Used for outgoing connections)
3611 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003612static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003613{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003614 struct conn_stream *cs;
3615 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003616 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003617
Willy Tarreau7838a792019-08-12 18:42:03 +02003618 TRACE_ENTER(H2_EV_H2S_NEW, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003619 cs = cs_new(conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003620 if (!cs) {
3621 TRACE_DEVEL("leaving on CS allocation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003622 return NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02003623 }
Olivier Houchardf502aca2018-12-14 19:42:40 +01003624 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003625 if (!h2s) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003626 TRACE_DEVEL("leaving on stream creation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003627 cs_free(cs);
3628 return NULL;
3629 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003630 TRACE_LEAVE(H2_EV_H2S_NEW, conn, h2s);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003631 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003632}
3633
Willy Tarreaufafd3982018-11-18 21:29:20 +01003634/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3635 * We have to scan because we may have some orphan streams. It might be
3636 * beneficial to scan backwards from the end to reduce the likeliness to find
3637 * orphans.
3638 */
3639static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3640{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003641 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003642 struct h2s *h2s;
3643 struct eb32_node *node;
3644
3645 node = eb32_first(&h2c->streams_by_id);
3646 while (node) {
3647 h2s = container_of(node, struct h2s, by_id);
3648 if (h2s->cs)
3649 return h2s->cs;
3650 node = eb32_next(node);
3651 }
3652 return NULL;
3653}
3654
Willy Tarreau62f52692017-10-08 23:01:42 +02003655/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003656 * Destroy the mux and the associated connection, if it is no longer used
3657 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003658static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003659{
Christopher Faulet73c12072019-04-08 11:23:22 +02003660 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003661
Willy Tarreau7838a792019-08-12 18:42:03 +02003662 TRACE_ENTER(H2_EV_H2C_END, h2c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003663 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003664 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003665 TRACE_LEAVE(H2_EV_H2C_END);
Olivier Houchard060ed432018-11-06 16:32:42 +01003666}
3667
3668/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003669 * Detach the stream from the connection and possibly release the connection.
3670 */
3671static void h2_detach(struct conn_stream *cs)
3672{
Willy Tarreau60935142017-10-16 18:11:19 +02003673 struct h2s *h2s = cs->ctx;
3674 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003675 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003676
Willy Tarreau7838a792019-08-12 18:42:03 +02003677 TRACE_ENTER(H2_EV_STRM_END, h2s ? h2s->h2c->conn : NULL, h2s);
3678
Willy Tarreau60935142017-10-16 18:11:19 +02003679 cs->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02003680 if (!h2s) {
3681 TRACE_LEAVE(H2_EV_STRM_END);
Willy Tarreau60935142017-10-16 18:11:19 +02003682 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02003683 }
Willy Tarreau60935142017-10-16 18:11:19 +02003684
Olivier Houchard998410a2019-04-15 19:23:37 +02003685 /* The stream is about to die, so no need to attempt to run its task */
Willy Tarreauc234ae32019-05-13 17:56:11 +02003686 if (LIST_ADDED(&h2s->sending_list) &&
Olivier Houchard998410a2019-04-15 19:23:37 +02003687 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02003688 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchard998410a2019-04-15 19:23:37 +02003689 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003690 /*
3691 * At this point, the stream_interface is supposed to have called
3692 * h2_unsubscribe(), so the only way there's still a
3693 * subscription that came from the stream_interface (as we
3694 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3695 * without the stream_interface involved) is that we subscribed
3696 * for sending, we woke the tasklet up and removed the
3697 * SUB_RETRY_SEND flag, so the stream_interface would not
3698 * know it has to unsubscribe for send, but the tasklet hasn't
3699 * run yet. Make sure to handle that by explicitely setting
3700 * send_wait to NULL, as nothing else will do it for us.
3701 */
3702 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003703 }
3704
Olivier Houchardf502aca2018-12-14 19:42:40 +01003705 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003706 h2c = h2s->h2c;
3707 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003708 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003709 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3710 !h2_frt_has_too_many_cs(h2c)) {
3711 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003712 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003713 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003714 }
Willy Tarreau60935142017-10-16 18:11:19 +02003715
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003716 /* this stream may be blocked waiting for some data to leave (possibly
3717 * an ES or RST frame), so orphan it in this case.
3718 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003719 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003720 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau7838a792019-08-12 18:42:03 +02003721 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait)) {
3722 TRACE_DEVEL("leaving on stream blocked", H2_EV_STRM_END|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003723 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02003724 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003725
Willy Tarreau45f752e2017-10-30 15:44:59 +01003726 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3727 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3728 /* unblock the connection if it was blocked on this
3729 * stream.
3730 */
3731 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3732 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003733 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003734 }
3735
Willy Tarreau71049cc2018-03-28 13:56:39 +02003736 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003737
Christopher Faulet9b79a102019-07-15 11:22:56 +02003738 if (h2c->flags & H2_CF_IS_BACK) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003739 if (!(h2c->conn->flags &
3740 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3741 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003742 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003743 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3744 h2c->conn->owner = NULL;
3745 if (eb_is_empty(&h2c->streams_by_id)) {
3746 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3747 /* The server doesn't want it, let's kill the connection right away */
3748 h2c->conn->mux->destroy(h2c->conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003749 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
Olivier Houchard351411f2018-12-27 17:20:54 +01003750 return;
3751 }
3752 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003753 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003754 if (eb_is_empty(&h2c->streams_by_id)) {
3755 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3756 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
Willy Tarreau7838a792019-08-12 18:42:03 +02003757 TRACE_DEVEL("leaving with non-reusable idle connection", H2_EV_STRM_END, h2c->conn);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003758 return;
3759 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003760 /* Never ever allow to reuse a connection from a non-reuse backend */
3761 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3762 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreauc234ae32019-05-13 17:56:11 +02003763 if (!LIST_ADDED(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003764 struct server *srv = objt_server(h2c->conn->target);
3765
3766 if (srv) {
3767 if (h2c->conn->flags & CO_FL_PRIVATE)
3768 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3769 else
3770 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3771 }
3772
3773 }
3774 }
3775 }
3776
Willy Tarreaue323f342018-03-28 13:51:45 +02003777 /* We don't want to close right now unless we're removing the
3778 * last stream, and either the connection is in error, or it
3779 * reached the ID already specified in a GOAWAY frame received
3780 * or sent (as seen by last_sid >= 0).
3781 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003782 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003783 /* no more stream will come, kill it now */
Willy Tarreau7838a792019-08-12 18:42:03 +02003784 TRACE_DEVEL("leaving and killing dead connection", H2_EV_STRM_END, h2c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003785 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003786 }
3787 else if (h2c->task) {
Willy Tarreau73481192019-06-07 08:20:46 +02003788 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3789 task_queue(h2c->task);
Willy Tarreau7838a792019-08-12 18:42:03 +02003790 TRACE_DEVEL("leaving, refreshing connection's timeout", H2_EV_STRM_END, h2c->conn);
Willy Tarreau60935142017-10-16 18:11:19 +02003791 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003792 else
3793 TRACE_DEVEL("leaving", H2_EV_STRM_END, h2c->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003794}
3795
Willy Tarreau88bdba32019-05-13 18:17:53 +02003796/* Performs a synchronous or asynchronous shutr(). */
3797static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003798{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003799 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003800 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003801
Willy Tarreauf983d002019-05-14 10:40:21 +02003802 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003803 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003804
Willy Tarreau7838a792019-08-12 18:42:03 +02003805 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
3806
Willy Tarreau18059042019-01-31 19:12:48 +01003807 /* a connstream may require us to immediately kill the whole connection
3808 * for example because of a "tcp-request content reject" rule that is
3809 * normally used to limit abuse. In this case we schedule a goaway to
3810 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003811 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003812 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003813 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003814 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01003815 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3816 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3817 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003818 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3819 /* Nothing was never sent for this stream, so reset with
3820 * REFUSED_STREAM error to let the client retry the
3821 * request.
3822 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003823 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01003824 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3825 }
Willy Tarreaucfba9d62019-08-06 10:30:58 +02003826 else {
3827 /* a final response was already provided, we don't want this
3828 * stream anymore. This may happen when the server responds
3829 * before the end of an upload and closes quickly (redirect,
3830 * deny, ...)
3831 */
3832 h2s_error(h2s, H2_ERR_CANCEL);
3833 }
Willy Tarreau18059042019-01-31 19:12:48 +01003834
Willy Tarreau90c32322017-11-24 08:00:30 +01003835 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003836 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003837 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003838
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003839 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003840 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003841 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003842 done:
3843 h2s->flags &= ~H2_SF_WANT_SHUTR;
Willy Tarreau7838a792019-08-12 18:42:03 +02003844 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003845 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003846add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003847 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003848 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003849 if (h2s->flags & H2_SF_BLK_MFCTL) {
3850 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3851 h2s->send_wait = sw;
3852 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3853 h2s->send_wait = sw;
3854 LIST_ADDQ(&h2c->send_list, &h2s->list);
3855 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003856 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003857 /* Let the handler know we want shutr */
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003858 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreau7838a792019-08-12 18:42:03 +02003859 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003860 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02003861}
3862
Willy Tarreau88bdba32019-05-13 18:17:53 +02003863/* Performs a synchronous or asynchronous shutw(). */
3864static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003865{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003866 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003867 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003868
Willy Tarreaucfba9d62019-08-06 10:30:58 +02003869 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003870 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003871
Willy Tarreau7838a792019-08-12 18:42:03 +02003872 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
3873
Willy Tarreaucfba9d62019-08-06 10:30:58 +02003874 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003875 /* we can cleanly close using an empty data frame only after headers */
3876
3877 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3878 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003879 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003880
3881 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003882 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003883 else
3884 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003885 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003886 /* a connstream may require us to immediately kill the whole connection
3887 * for example because of a "tcp-request content reject" rule that is
3888 * normally used to limit abuse. In this case we schedule a goaway to
3889 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003890 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003891 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01003892 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003893 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01003894 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3895 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3896 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003897 else {
3898 /* Nothing was never sent for this stream, so reset with
3899 * REFUSED_STREAM error to let the client retry the
3900 * request.
3901 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003902 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01003903 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3904 }
Willy Tarreau18059042019-01-31 19:12:48 +01003905
Willy Tarreau90c32322017-11-24 08:00:30 +01003906 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003907 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003908 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003909
Willy Tarreau00dd0782018-03-01 16:31:34 +01003910 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003911 }
3912
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003913 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003914 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau7838a792019-08-12 18:42:03 +02003915
3916 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
3917
Willy Tarreau88bdba32019-05-13 18:17:53 +02003918 done:
3919 h2s->flags &= ~H2_SF_WANT_SHUTW;
3920 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003921
3922 add_to_list:
Willy Tarreauc234ae32019-05-13 17:56:11 +02003923 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003924 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003925 if (h2s->flags & H2_SF_BLK_MFCTL) {
3926 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3927 h2s->send_wait = sw;
3928 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3929 h2s->send_wait = sw;
3930 LIST_ADDQ(&h2c->send_list, &h2s->list);
3931 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003932 }
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003933 /* let the handler know we want to shutw */
3934 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreau7838a792019-08-12 18:42:03 +02003935 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02003936 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003937}
3938
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003939/* This is the tasklet referenced in h2s->wait_event.tasklet, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003940 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3941 * and prevented the last frame from being emitted.
3942 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003943static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3944{
3945 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02003946 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003947
Willy Tarreau7838a792019-08-12 18:42:03 +02003948 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
3949
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003950 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003951 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003952 h2_do_shutw(h2s);
3953
Willy Tarreau2c249eb2019-05-13 18:06:17 +02003954 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02003955 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003956
Willy Tarreau88bdba32019-05-13 18:17:53 +02003957 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003958 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003959 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01003960
Willy Tarreau88bdba32019-05-13 18:17:53 +02003961 if (!h2s->cs) {
3962 h2s_destroy(h2s);
3963 if (h2c_is_dead(h2c))
3964 h2_release(h2c);
3965 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003966 }
3967
Willy Tarreau7838a792019-08-12 18:42:03 +02003968 TRACE_LEAVE(H2_EV_STRM_SHUT);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003969 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003970}
3971
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003972/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003973static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3974{
3975 struct h2s *h2s = cs->ctx;
3976
Willy Tarreau7838a792019-08-12 18:42:03 +02003977 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003978 if (cs->flags & CS_FL_KILL_CONN)
3979 h2s->flags |= H2_SF_KILL_CONN;
3980
Willy Tarreau7838a792019-08-12 18:42:03 +02003981 if (mode)
3982 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003983
Willy Tarreau7838a792019-08-12 18:42:03 +02003984 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003985}
3986
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003987/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003988static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3989{
3990 struct h2s *h2s = cs->ctx;
3991
Willy Tarreau7838a792019-08-12 18:42:03 +02003992 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02003993 if (cs->flags & CS_FL_KILL_CONN)
3994 h2s->flags |= H2_SF_KILL_CONN;
3995
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003996 h2_do_shutw(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003997 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003998}
3999
Christopher Faulet9b79a102019-07-15 11:22:56 +02004000/* Decode the payload of a HEADERS frame and produce the HTX request or response
4001 * depending on the connection's side. Returns a positive value on success, a
4002 * negative value on failure, or 0 if it couldn't proceed. May report connection
4003 * errors in h2c->errcode if the frame is non-decodable and the connection
4004 * unrecoverable. In absence of connection error when a failure is reported, the
4005 * caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01004006 *
4007 * The function may fold CONTINUATION frames into the initial HEADERS frame
4008 * by removing padding and next frame header, then moving the CONTINUATION
4009 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
4010 * leaving a hole between the main frame and the beginning of the next one.
4011 * The possibly remaining incomplete or next frame at the end may be moved
4012 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
4013 * HEADERS frames are unwrapped into a temporary buffer before decoding.
4014 *
4015 * A buffer at the beginning of processing may look like this :
4016 *
4017 * ,---.---------.-----.--------------.--------------.------.---.
4018 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
4019 * `---^---------^-----^--------------^--------------^------^---'
4020 * | | <-----> | |
4021 * area | dpl | wrap
4022 * |<--------------> |
4023 * | dfl |
4024 * |<-------------------------------------------------->|
4025 * head data
4026 *
4027 * Padding is automatically overwritten when folding, participating to the
4028 * hole size after dfl :
4029 *
4030 * ,---.------------------------.-----.--------------.------.---.
4031 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
4032 * `---^------------------------^-----^--------------^------^---'
4033 * | | <-----> | |
4034 * area | hole | wrap
4035 * |<-----------------------> |
4036 * | dfl |
4037 * |<-------------------------------------------------->|
4038 * head data
4039 *
4040 * Please note that the HEADERS frame is always deprived from its PADLEN byte
4041 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
4042 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004043 *
4044 * The <flags> field must point to either the stream's flags or to a copy of it
4045 * so that the function can update the following flags :
4046 * - H2_SF_DATA_CLEN when content-length is seen
4047 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
4048 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01004049 *
4050 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
4051 * decoding, in order to detect if we're dealing with a headers or a trailers
4052 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02004053 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01004054static 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 +02004055{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004056 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02004057 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004058 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02004059 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004060 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004061 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01004062 int flen; // header frame len
4063 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01004064 int ret = 0;
4065 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02004066 int wrap;
Willy Tarreau13278b42017-10-13 19:23:14 +02004067
Willy Tarreau7838a792019-08-12 18:42:03 +02004068 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
4069
Willy Tarreauea18f862018-12-22 20:19:26 +01004070next_frame:
4071 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
4072 goto leave; // incomplete input frame
4073
4074 /* No END_HEADERS means there's one or more CONTINUATION frames. In
4075 * this case, we'll try to paste it immediately after the initial
4076 * HEADERS frame payload and kill any possible padding. The initial
4077 * frame's length will be increased to represent the concatenation
4078 * of the two frames. The next frame is read from position <tlen>
4079 * and written at position <flen> (minus padding if some is present).
4080 */
4081 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
4082 struct h2_fh hdr;
4083 int clen; // CONTINUATION frame's payload length
4084
Willy Tarreau7838a792019-08-12 18:42:03 +02004085 TRACE_STATE("EH missing, expecting continuation frame", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_HDR, h2c->conn);
Willy Tarreauea18f862018-12-22 20:19:26 +01004086 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
4087 /* no more data, the buffer may be full, either due to
4088 * too large a frame or because of too large a hole that
4089 * we're going to compact at the end.
4090 */
4091 goto leave;
4092 }
4093
4094 if (hdr.ft != H2_FT_CONTINUATION) {
4095 /* RFC7540#6.10: frame of unexpected type */
Willy Tarreau7838a792019-08-12 18:42:03 +02004096 TRACE_STATE("not continuation!", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_HDR|H2_EV_RX_CONT|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreauea18f862018-12-22 20:19:26 +01004097 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
4098 goto fail;
4099 }
4100
4101 if (hdr.sid != h2c->dsi) {
4102 /* RFC7540#6.10: frame of different stream */
Willy Tarreau7838a792019-08-12 18:42:03 +02004103 TRACE_STATE("different stream ID!", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_HDR|H2_EV_RX_CONT|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreauea18f862018-12-22 20:19:26 +01004104 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
4105 goto fail;
4106 }
4107
4108 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
4109 /* RFC7540#4.2: invalid frame length */
Willy Tarreau7838a792019-08-12 18:42:03 +02004110 TRACE_STATE("too large frame!", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_HDR|H2_EV_RX_CONT|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreauea18f862018-12-22 20:19:26 +01004111 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4112 goto fail;
4113 }
4114
4115 /* detect when we must stop aggragating frames */
4116 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
4117
4118 /* Take as much as we can of the CONTINUATION frame's payload */
4119 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
4120 if (clen > hdr.len)
4121 clen = hdr.len;
4122
4123 /* Move the frame's payload over the padding, hole and frame
4124 * header. At least one of hole or dpl is null (see diagrams
4125 * above). The hole moves after the new aggragated frame.
4126 */
4127 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
4128 h2c->dfl += clen - h2c->dpl;
4129 hole += h2c->dpl + 9;
4130 h2c->dpl = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004131 TRACE_STATE("waiting for next continuation frame", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_RX_CONT|H2_EV_RX_HDR, h2c->conn);
Willy Tarreauea18f862018-12-22 20:19:26 +01004132 goto next_frame;
4133 }
4134
4135 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01004136
Willy Tarreau13278b42017-10-13 19:23:14 +02004137 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004138 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02004139 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02004140 copy = alloc_trash_chunk();
4141 if (!copy) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004142 TRACE_DEVEL("failed to allocate temporary buffer", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2C_ERR, h2c->conn);
Willy Tarreau68dd9852017-07-03 14:44:26 +02004143 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
4144 goto fail;
4145 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004146 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
4147 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
4148 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02004149 }
4150
Willy Tarreau13278b42017-10-13 19:23:14 +02004151 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
4152 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004153 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004154 /* RFC7540#5.3.1 : stream dep may not depend on itself */
Willy Tarreau7838a792019-08-12 18:42:03 +02004155 TRACE_STATE("invalid stream dependency!", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004156 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02004157 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004158 }
4159
Willy Tarreaua01f45e2018-12-31 07:41:24 +01004160 if (flen < 5) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004161 TRACE_STATE("frame too short for priority!", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreaua01f45e2018-12-31 07:41:24 +01004162 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4163 goto fail;
4164 }
4165
Willy Tarreau13278b42017-10-13 19:23:14 +02004166 hdrs += 5; // stream dep = 4, weight = 1
4167 flen -= 5;
4168 }
4169
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004170 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004171 TRACE_STATE("waiting for h2c rxbuf allocation", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2C_BLK, h2c->conn);
Willy Tarreau937f7602018-02-26 15:22:17 +01004172 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01004173 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004174 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004175
Willy Tarreau937f7602018-02-26 15:22:17 +01004176 /* we can't retry a failed decompression operation so we must be very
4177 * careful not to take any risks. In practice the output buffer is
4178 * always empty except maybe for trailers, in which case we simply have
4179 * to wait for the upper layer to finish consuming what is available.
4180 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004181 htx = htx_from_buf(rxbuf);
4182 if (!htx_is_empty(htx)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004183 TRACE_STATE("waiting for room in h2c rxbuf", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2C_BLK, h2c->conn);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004184 h2c->flags |= H2_CF_DEM_SFULL;
4185 goto leave;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004186 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004187
Willy Tarreau25919232019-01-03 14:48:18 +01004188 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004189 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
4190 sizeof(list)/sizeof(list[0]), tmp);
4191 if (outlen < 0) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004192 TRACE_STATE("failed to decompress HPACK", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004193 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
4194 goto fail;
4195 }
4196
Willy Tarreau25919232019-01-03 14:48:18 +01004197 /* The PACK decompressor was updated, let's update the input buffer and
4198 * the parser's state to commit these changes and allow us to later
4199 * fail solely on the stream if needed.
4200 */
4201 b_del(&h2c->dbuf, h2c->dfl + hole);
4202 h2c->dfl = hole = 0;
4203 h2c->st0 = H2_CS_FRAME_H;
4204
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004205 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01004206 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004207
Willy Tarreau88d138e2019-01-02 19:38:14 +01004208 if (*flags & H2_SF_HEADERS_RCVD)
4209 goto trailers;
4210
4211 /* This is the first HEADERS frame so it's a headers block */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004212 if (h2c->flags & H2_CF_IS_BACK)
4213 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
4214 else
4215 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004216
4217 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01004218 /* too large headers? this is a stream error only */
Willy Tarreau7838a792019-08-12 18:42:03 +02004219 TRACE_STATE("request headers too large", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2S_ERR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004220 goto fail;
4221 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004222
Willy Tarreau174b06a2018-04-25 18:13:58 +02004223 if (msgf & H2_MSGF_BODY) {
4224 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01004225 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004226 *flags |= H2_SF_DATA_CLEN;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004227 htx->extra = *body_len;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01004228 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01004229 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004230 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004231 }
4232
Willy Tarreau88d138e2019-01-02 19:38:14 +01004233 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01004234 /* indicate that a HEADERS frame was received for this stream, except
4235 * for 1xx responses. For 1xx responses, another HEADERS frame is
4236 * expected.
4237 */
4238 if (!(msgf & H2_MSGF_RSP_1XX))
4239 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004240
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02004241 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02004242 /* Mark the end of message using EOM */
Willy Tarreau7838a792019-08-12 18:42:03 +02004243 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
4244 TRACE_STATE("failed to append HTX EOM block into rxbuf", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2S_ERR, h2c->conn);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004245 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004246 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01004247 }
Willy Tarreau937f7602018-02-26 15:22:17 +01004248
Willy Tarreau86277d42019-01-02 15:36:11 +01004249 /* success */
4250 ret = 1;
4251
Willy Tarreau68dd9852017-07-03 14:44:26 +02004252 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01004253 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01004254 * move the remaining data over it.
4255 */
4256 if (hole) {
4257 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
4258 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
4259 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
4260 b_sub(&h2c->dbuf, hole);
4261 }
4262
Willy Tarreau97215ca2019-04-29 10:20:21 +02004263 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01004264 /* too large frames */
4265 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01004266 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01004267 }
4268
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004269 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004270 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02004271 free_trash_chunk(copy);
Willy Tarreau7838a792019-08-12 18:42:03 +02004272 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau86277d42019-01-02 15:36:11 +01004273 return ret;
4274
Willy Tarreau68dd9852017-07-03 14:44:26 +02004275 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01004276 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02004277 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01004278
4279 trailers:
4280 /* This is the last HEADERS frame hence a trailer */
Willy Tarreau88d138e2019-01-02 19:38:14 +01004281 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
4282 /* It's a trailer but it's missing ES flag */
Willy Tarreau7838a792019-08-12 18:42:03 +02004283 TRACE_STATE("missing EH on trailers frame", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2C_ERR|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau88d138e2019-01-02 19:38:14 +01004284 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
4285 goto fail;
4286 }
4287
Christopher Faulet9b79a102019-07-15 11:22:56 +02004288 /* Trailers terminate a DATA sequence */
Willy Tarreau7838a792019-08-12 18:42:03 +02004289 if (h2_make_htx_trailers(list, htx) <= 0) {
4290 TRACE_STATE("failed to append HTX trailers into rxbuf", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_H2S_ERR, h2c->conn);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004291 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004292 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01004293 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02004294}
4295
Christopher Faulet9b79a102019-07-15 11:22:56 +02004296/* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004297 * parser state is automatically updated. Returns > 0 if it could completely
4298 * send the current frame, 0 if it couldn't complete, in which case
4299 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
4300 * DATA frame can return 0 as a valid result). Stream errors are reported in
4301 * h2s->errcode and connection errors in h2c->errcode. The caller must already
4302 * have checked the frame header and ensured that the frame was complete or the
4303 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02004304 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01004305static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02004306{
4307 struct h2c *h2c = h2s->h2c;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004308 int block;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004309 unsigned int flen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004310 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004311 struct buffer *csbuf;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004312 unsigned int sent;
Willy Tarreau454f9052017-10-26 19:40:35 +02004313
Willy Tarreau7838a792019-08-12 18:42:03 +02004314 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
4315
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004316 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02004317
Olivier Houchard638b7992018-08-16 15:41:52 +02004318 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004319 if (!csbuf) {
4320 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02004321 TRACE_STATE("waiting for an h2s rxbuf", H2_EV_RX_FRAME|H2_EV_RX_DATA|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004322 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004323 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004324 htx = htx_from_buf(csbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004325
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004326try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004327 flen = h2c->dfl - h2c->dpl;
4328 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01004329 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004330
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004331 if (flen > b_data(&h2c->dbuf)) {
4332 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004333 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01004334 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004335 }
4336
Christopher Faulet9b79a102019-07-15 11:22:56 +02004337 block = htx_free_data_space(htx);
4338 if (!block) {
4339 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004340 TRACE_STATE("h2s rxbuf is full", H2_EV_RX_FRAME|H2_EV_RX_DATA|H2_EV_H2S_BLK, h2c->conn, h2s);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004341 goto fail;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004342 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004343 if (flen > block)
4344 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004345
Christopher Faulet9b79a102019-07-15 11:22:56 +02004346 /* here, flen is the max we can copy into the output buffer */
4347 block = b_contig_data(&h2c->dbuf, 0);
4348 if (flen > block)
4349 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004350
Christopher Faulet9b79a102019-07-15 11:22:56 +02004351 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau7838a792019-08-12 18:42:03 +02004352 TRACE_DATA("move some data to h2s rxbuf", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s,, (void *)(long)sent);
Willy Tarreau454f9052017-10-26 19:40:35 +02004353
Christopher Faulet9b79a102019-07-15 11:22:56 +02004354 b_del(&h2c->dbuf, sent);
4355 h2c->dfl -= sent;
4356 h2c->rcvd_c += sent;
4357 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau454f9052017-10-26 19:40:35 +02004358
Christopher Faulet9b79a102019-07-15 11:22:56 +02004359 if (h2s->flags & H2_SF_DATA_CLEN) {
4360 h2s->body_len -= sent;
4361 htx->extra = h2s->body_len;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004362 }
4363
Christopher Faulet9b79a102019-07-15 11:22:56 +02004364 if (sent < flen) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01004365 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004366 TRACE_STATE("h2s rxbuf is full", H2_EV_RX_FRAME|H2_EV_RX_DATA|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004367 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004368 }
4369
Christopher Faulet9b79a102019-07-15 11:22:56 +02004370 goto try_again;
4371
Willy Tarreau4a28da12018-01-04 14:41:00 +01004372 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004373 /* here we're done with the frame, all the payload (except padding) was
4374 * transferred.
4375 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02004376
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004377 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02004378 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004379 TRACE_STATE("h2s rxbuf is full, failed to add EOM", H2_EV_RX_FRAME|H2_EV_RX_DATA|H2_EV_H2S_BLK, h2c->conn, h2s);
Christopher Faulet9b79a102019-07-15 11:22:56 +02004380 h2c->flags |= H2_CF_DEM_SFULL;
4381 goto fail;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004382 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02004383 }
4384
Willy Tarreaud1023bb2018-03-22 16:53:12 +01004385 h2c->rcvd_c += h2c->dpl;
4386 h2c->rcvd_s += h2c->dpl;
4387 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004388 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet9b79a102019-07-15 11:22:56 +02004389 htx_to_buf(htx, csbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02004390 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004391 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004392 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004393 if (htx)
4394 htx_to_buf(htx, csbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02004395 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004396 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004397}
4398
Willy Tarreau115e83b2018-12-01 19:17:53 +01004399/* Try to send a HEADERS frame matching HTX response present in HTX message
4400 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4401 * must check the stream's status to detect any error which might have happened
4402 * subsequently to a successful send. The htx blocks are automatically removed
4403 * from the message. The htx message is assumed to be valid since produced from
4404 * the internal code, hence it contains a start line, an optional series of
4405 * header blocks and an end of header, otherwise an invalid frame could be
4406 * emitted and the resulting htx message could be left in an inconsistent state.
4407 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004408static size_t h2s_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004409{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004410 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004411 struct h2c *h2c = h2s->h2c;
4412 struct htx_blk *blk;
4413 struct htx_blk *blk_end;
4414 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004415 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004416 struct htx_sl *sl;
4417 enum htx_blk_type type;
4418 int es_now = 0;
4419 int ret = 0;
4420 int hdr;
4421 int idx;
4422
Willy Tarreau7838a792019-08-12 18:42:03 +02004423 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
4424
Willy Tarreau115e83b2018-12-01 19:17:53 +01004425 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004426 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004427 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02004428 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004429 return 0;
4430 }
4431
Willy Tarreau115e83b2018-12-01 19:17:53 +01004432 /* determine the first block which must not be deleted, blk_end may
4433 * be NULL if all blocks have to be deleted.
4434 */
4435 idx = htx_get_head(htx);
4436 blk_end = NULL;
4437 while (idx != -1) {
4438 type = htx_get_blk_type(htx_get_blk(htx, idx));
4439 idx = htx_get_next(htx, idx);
4440 if (type == HTX_BLK_EOH) {
4441 if (idx != -1)
4442 blk_end = htx_get_blk(htx, idx);
4443 break;
4444 }
4445 }
4446
4447 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004448 blk = htx_get_head_blk(htx);
4449 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_RES_SL);
4450 ALREADY_CHECKED(blk);
4451 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004452 h2s->status = sl->info.res.status;
Willy Tarreau7838a792019-08-12 18:42:03 +02004453 if (h2s->status < 100 || h2s->status > 999) {
4454 TRACE_PROTO("will not encode an invalid status code", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
Willy Tarreauaafdf582018-12-10 18:06:40 +01004455 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004456 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004457
4458 /* and the rest of the headers, that we dump starting at header 0 */
4459 hdr = 0;
4460
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004461 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004462 while ((idx = htx_get_next(htx, idx)) != -1) {
4463 blk = htx_get_blk(htx, idx);
4464 type = htx_get_blk_type(blk);
4465
4466 if (type == HTX_BLK_UNUSED)
4467 continue;
4468
4469 if (type != HTX_BLK_HDR)
4470 break;
4471
Willy Tarreau7838a792019-08-12 18:42:03 +02004472 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
4473 TRACE_PROTO("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004474 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004475 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004476
4477 list[hdr].n = htx_get_blk_name(htx, blk);
4478 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004479 hdr++;
4480 }
4481
4482 /* marker for end of headers */
4483 list[hdr].n = ist("");
4484
4485 if (h2s->status == 204 || h2s->status == 304) {
4486 /* no contents, claim c-len is present and set to zero */
4487 es_now = 1;
4488 }
4489
Willy Tarreau9c218e72019-05-26 10:08:28 +02004490 mbuf = br_tail(h2c->mbuf);
4491 retry:
4492 if (!h2_get_buf(h2c, mbuf)) {
4493 h2c->flags |= H2_CF_MUX_MALLOC;
4494 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02004495 TRACE_STATE("waiting for room in output buffer", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004496 return 0;
4497 }
4498
Willy Tarreau115e83b2018-12-01 19:17:53 +01004499 chunk_reset(&outbuf);
4500
4501 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004502 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4503 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004504 break;
4505 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004506 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004507 }
4508
4509 if (outbuf.size < 9)
4510 goto full;
4511
4512 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4513 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4514 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4515 outbuf.data = 9;
4516
4517 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004518 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004519 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004520 goto realign_again;
4521 goto full;
4522 }
4523
4524 /* encode all headers, stop at empty name */
4525 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4526 /* these ones do not exist in H2 and must be dropped. */
4527 if (isteq(list[hdr].n, ist("connection")) ||
4528 isteq(list[hdr].n, ist("proxy-connection")) ||
4529 isteq(list[hdr].n, ist("keep-alive")) ||
4530 isteq(list[hdr].n, ist("upgrade")) ||
4531 isteq(list[hdr].n, ist("transfer-encoding")))
4532 continue;
4533
4534 if (isteq(list[hdr].n, ist("")))
4535 break; // end
4536
4537 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4538 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004539 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004540 goto realign_again;
4541 goto full;
4542 }
4543 }
4544
Christopher Faulet0b465482019-02-19 15:14:23 +01004545 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004546 * FIXME: we should also set it when we know for sure that the
4547 * content-length is zero as well as on 204/304
4548 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004549 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4550 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004551 es_now = 1;
4552
Willy Tarreau927b88b2019-03-04 08:03:25 +01004553 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004554 es_now = 1;
4555
4556 /* update the frame's size */
4557 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4558
4559 if (es_now)
4560 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4561
4562 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02004563 TRACE_USER("sent H2 response", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
Willy Tarreaubcc45952019-05-26 10:05:50 +02004564 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004565
4566 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4567 * 1xx responses, another HEADERS frame is expected.
4568 */
4569 if (h2s->status >= 200 || h2s->status == 101)
4570 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004571
Willy Tarreau115e83b2018-12-01 19:17:53 +01004572 if (es_now) {
4573 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02004574 TRACE_PROTO("setting ES on HEADERS frame", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004575 if (h2s->st == H2_SS_OPEN)
4576 h2s->st = H2_SS_HLOC;
4577 else
4578 h2s_close(h2s);
4579 }
4580
4581 /* OK we could properly deliver the response */
4582
4583 /* remove all header blocks including the EOH and compute the
4584 * corresponding size.
4585 *
4586 * FIXME: We should remove everything when es_now is set.
4587 */
4588 ret = 0;
4589 idx = htx_get_head(htx);
4590 blk = htx_get_blk(htx, idx);
4591 while (blk != blk_end) {
4592 ret += htx_get_blksz(blk);
4593 blk = htx_remove_blk(htx, blk);
4594 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004595
Christopher Faulet316934d2019-05-15 14:22:48 +02004596 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4597 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004598 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004599 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004600 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02004601 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004602 return ret;
4603 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004604 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4605 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004606 h2c->flags |= H2_CF_MUX_MFULL;
4607 h2s->flags |= H2_SF_BLK_MROOM;
4608 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004609 TRACE_STATE("mux buffer full", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004610 goto end;
4611 fail:
4612 /* unparsable HTX messages, too large ones to be produced in the local
4613 * list etc go here (unrecoverable errors).
4614 */
4615 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4616 ret = 0;
4617 goto end;
4618}
4619
Willy Tarreau80739692018-10-05 11:35:57 +02004620/* Try to send a HEADERS frame matching HTX request present in HTX message
4621 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4622 * must check the stream's status to detect any error which might have happened
4623 * subsequently to a successful send. The htx blocks are automatically removed
4624 * from the message. The htx message is assumed to be valid since produced from
4625 * the internal code, hence it contains a start line, an optional series of
4626 * header blocks and an end of header, otherwise an invalid frame could be
4627 * emitted and the resulting htx message could be left in an inconsistent state.
4628 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004629static size_t h2s_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau80739692018-10-05 11:35:57 +02004630{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004631 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004632 struct h2c *h2c = h2s->h2c;
4633 struct htx_blk *blk;
4634 struct htx_blk *blk_end;
4635 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004636 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004637 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004638 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004639 enum htx_blk_type type;
4640 int es_now = 0;
4641 int ret = 0;
4642 int hdr;
4643 int idx;
4644
Willy Tarreau7838a792019-08-12 18:42:03 +02004645 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
4646
Willy Tarreau80739692018-10-05 11:35:57 +02004647 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004648 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02004649 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02004650 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02004651 return 0;
4652 }
4653
Willy Tarreau80739692018-10-05 11:35:57 +02004654 /* determine the first block which must not be deleted, blk_end may
4655 * be NULL if all blocks have to be deleted.
4656 */
4657 idx = htx_get_head(htx);
4658 blk_end = NULL;
4659 while (idx != -1) {
4660 type = htx_get_blk_type(htx_get_blk(htx, idx));
4661 idx = htx_get_next(htx, idx);
4662 if (type == HTX_BLK_EOH) {
4663 if (idx != -1)
4664 blk_end = htx_get_blk(htx, idx);
4665 break;
4666 }
4667 }
4668
4669 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004670 blk = htx_get_head_blk(htx);
4671 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
4672 ALREADY_CHECKED(blk);
4673 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004674 meth = htx_sl_req_meth(sl);
4675 path = htx_sl_req_uri(sl);
4676
4677 /* and the rest of the headers, that we dump starting at header 0 */
4678 hdr = 0;
4679
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004680 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004681 while ((idx = htx_get_next(htx, idx)) != -1) {
4682 blk = htx_get_blk(htx, idx);
4683 type = htx_get_blk_type(blk);
4684
4685 if (type == HTX_BLK_UNUSED)
4686 continue;
4687
4688 if (type != HTX_BLK_HDR)
4689 break;
4690
Willy Tarreau7838a792019-08-12 18:42:03 +02004691 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
4692 TRACE_PROTO("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02004693 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004694 }
Willy Tarreau80739692018-10-05 11:35:57 +02004695
4696 list[hdr].n = htx_get_blk_name(htx, blk);
4697 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004698 hdr++;
4699 }
4700
4701 /* marker for end of headers */
4702 list[hdr].n = ist("");
4703
Willy Tarreau9c218e72019-05-26 10:08:28 +02004704 mbuf = br_tail(h2c->mbuf);
4705 retry:
4706 if (!h2_get_buf(h2c, mbuf)) {
4707 h2c->flags |= H2_CF_MUX_MALLOC;
4708 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02004709 TRACE_STATE("waiting for room in output buffer", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004710 return 0;
4711 }
4712
Willy Tarreau80739692018-10-05 11:35:57 +02004713 chunk_reset(&outbuf);
4714
4715 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004716 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4717 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004718 break;
4719 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004720 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004721 }
4722
4723 if (outbuf.size < 9)
4724 goto full;
4725
4726 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4727 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4728 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4729 outbuf.data = 9;
4730
4731 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004732 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004733 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004734 goto realign_again;
4735 goto full;
4736 }
4737
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004738 /* RFC7540 #8.3: the CONNECT method must have :
4739 * - :authority set to the URI part (host:port)
4740 * - :method set to CONNECT
4741 * - :scheme and :path omitted
4742 */
4743 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4744 /* encode the scheme which is always "https" (or 0x86 for "http") */
Christopher Faulet3b44c542019-06-14 10:46:51 +02004745 struct ist scheme;
4746
4747 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
4748 scheme = ist("http");
4749 else
4750 scheme = ist("https");
4751
4752 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004753 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004754 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004755 goto realign_again;
4756 goto full;
4757 }
Willy Tarreau80739692018-10-05 11:35:57 +02004758
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004759 /* encode the path, which necessarily is the second one */
4760 if (!hpack_encode_path(&outbuf, path)) {
4761 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004762 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004763 goto realign_again;
4764 goto full;
4765 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004766
4767 /* look for the Host header and place it in :authority */
4768 auth = ist2(NULL, 0);
4769 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4770 if (isteq(list[hdr].n, ist("")))
4771 break; // end
4772
4773 if (isteq(list[hdr].n, ist("host"))) {
4774 auth = list[hdr].v;
4775 break;
4776 }
4777 }
4778 }
4779 else {
4780 /* for CONNECT, :authority is taken from the path */
4781 auth = path;
4782 }
4783
4784 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4785 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004786 if (b_space_wraps(mbuf))
Willy Tarreau053c1572019-02-01 16:13:59 +01004787 goto realign_again;
4788 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004789 }
4790
4791 /* encode all headers, stop at empty name */
4792 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4793 /* these ones do not exist in H2 and must be dropped. */
4794 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004795 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004796 isteq(list[hdr].n, ist("proxy-connection")) ||
4797 isteq(list[hdr].n, ist("keep-alive")) ||
4798 isteq(list[hdr].n, ist("upgrade")) ||
4799 isteq(list[hdr].n, ist("transfer-encoding")))
4800 continue;
4801
4802 if (isteq(list[hdr].n, ist("")))
4803 break; // end
4804
4805 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4806 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004807 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004808 goto realign_again;
4809 goto full;
4810 }
4811 }
4812
4813 /* we may need to add END_STREAM if we have no body :
4814 * - request already closed, or :
4815 * - no transfer-encoding, and :
4816 * - no content-length or content-length:0
4817 * Fixme: this doesn't take into account CONNECT requests.
4818 */
4819 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4820 es_now = 1;
4821
4822 if (sl->flags & HTX_SL_F_BODYLESS)
4823 es_now = 1;
4824
Willy Tarreau927b88b2019-03-04 08:03:25 +01004825 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004826 es_now = 1;
4827
4828 /* update the frame's size */
4829 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4830
4831 if (es_now)
4832 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4833
4834 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02004835 TRACE_USER("sent H2 request", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
Willy Tarreaubcc45952019-05-26 10:05:50 +02004836 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02004837 h2s->flags |= H2_SF_HEADERS_SENT;
4838 h2s->st = H2_SS_OPEN;
4839
Willy Tarreau80739692018-10-05 11:35:57 +02004840 if (es_now) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004841 TRACE_PROTO("setting ES on HEADERS frame", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02004842 // trim any possibly pending data (eg: inconsistent content-length)
4843 h2s->flags |= H2_SF_ES_SENT;
4844 h2s->st = H2_SS_HLOC;
4845 }
4846
4847 /* remove all header blocks including the EOH and compute the
4848 * corresponding size.
4849 *
4850 * FIXME: We should remove everything when es_now is set.
4851 */
4852 ret = 0;
4853 idx = htx_get_head(htx);
4854 blk = htx_get_blk(htx, idx);
4855 while (blk != blk_end) {
4856 ret += htx_get_blksz(blk);
4857 blk = htx_remove_blk(htx, blk);
4858 }
4859
Christopher Faulet316934d2019-05-15 14:22:48 +02004860 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4861 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02004862 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004863 }
Willy Tarreau80739692018-10-05 11:35:57 +02004864
4865 end:
4866 return ret;
4867 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004868 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4869 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02004870 h2c->flags |= H2_CF_MUX_MFULL;
4871 h2s->flags |= H2_SF_BLK_MROOM;
4872 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004873 TRACE_STATE("mux buffer full", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02004874 goto end;
4875 fail:
4876 /* unparsable HTX messages, too large ones to be produced in the local
4877 * list etc go here (unrecoverable errors).
4878 */
4879 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4880 ret = 0;
4881 goto end;
4882}
4883
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004884/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004885 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4886 * caller must check the stream's status to detect any error which might have
4887 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02004888 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004889 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004890static size_t h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004891{
4892 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004893 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004894 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004895 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004896 size_t total = 0;
4897 int es_now = 0;
4898 int bsize; /* htx block size */
4899 int fsize; /* h2 frame size */
4900 struct htx_blk *blk;
4901 enum htx_blk_type type;
4902 int idx;
4903
Willy Tarreau7838a792019-08-12 18:42:03 +02004904 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
4905
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004906 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004907 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004908 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02004909 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004910 goto end;
4911 }
4912
Willy Tarreau98de12a2018-12-12 07:03:00 +01004913 htx = htx_from_buf(buf);
4914
Christopher Faulet54b5e212019-06-04 10:08:28 +02004915 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
4916 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
4917 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004918 */
4919
4920 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004921 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004922 goto end;
4923
4924 idx = htx_get_head(htx);
4925 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02004926 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004927 bsize = htx_get_blksz(blk);
4928 fsize = bsize;
4929
Christopher Faulet54b5e212019-06-04 10:08:28 +02004930 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004931 if (h2s->flags & H2_SF_ES_SENT) {
4932 /* ES already sent */
4933 htx_remove_blk(htx, blk);
4934 total++; // EOM counts as one byte
4935 count--;
4936 goto end;
4937 }
4938 }
4939 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004940 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004941
Willy Tarreau9c218e72019-05-26 10:08:28 +02004942 mbuf = br_tail(h2c->mbuf);
4943 retry:
4944 if (!h2_get_buf(h2c, mbuf)) {
4945 h2c->flags |= H2_CF_MUX_MALLOC;
4946 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02004947 TRACE_STATE("waiting for room in output buffer", H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau9c218e72019-05-26 10:08:28 +02004948 goto end;
4949 }
4950
Willy Tarreau98de12a2018-12-12 07:03:00 +01004951 /* Perform some optimizations to reduce the number of buffer copies.
4952 * First, if the mux's buffer is empty and the htx area contains
4953 * exactly one data block of the same size as the requested count, and
4954 * this count fits within the frame size, the stream's window size, and
4955 * the connection's window size, then it's possible to simply swap the
4956 * caller's buffer with the mux's output buffer and adjust offsets and
4957 * length to match the entire DATA HTX block in the middle. In this
4958 * case we perform a true zero-copy operation from end-to-end. This is
4959 * the situation that happens all the time with large files. Second, if
4960 * this is not possible, but the mux's output buffer is empty, we still
4961 * have an opportunity to avoid the copy to the intermediary buffer, by
4962 * making the intermediary buffer's area point to the output buffer's
4963 * area. In this case we want to skip the HTX header to make sure that
4964 * copies remain aligned and that this operation remains possible all
4965 * the time. This goes for headers, data blocks and any data extracted
4966 * from the HTX blocks.
4967 */
4968 if (unlikely(fsize == count &&
Christopher Faulet192c6a22019-06-11 16:32:24 +02004969 htx_nbblks(htx) == 1 && type == HTX_BLK_DATA &&
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02004970 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004971 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004972
Willy Tarreaubcc45952019-05-26 10:05:50 +02004973 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004974 /* Too bad there are data left there. We're willing to memcpy/memmove
4975 * up to 1/4 of the buffer, which means that it's OK to copy a large
4976 * frame into a buffer containing few data if it needs to be realigned,
4977 * and that it's also OK to copy few data without realigning. Otherwise
4978 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004979 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004980 if (fsize + 9 <= b_room(mbuf) &&
4981 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Willy Tarreau7838a792019-08-12 18:42:03 +02004982 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) {
4983 TRACE_STATE("small data present in output buffer, appending", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau98de12a2018-12-12 07:03:00 +01004984 goto copy;
Willy Tarreau7838a792019-08-12 18:42:03 +02004985 }
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004986
Willy Tarreau9c218e72019-05-26 10:08:28 +02004987 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4988 goto retry;
4989
Willy Tarreau98de12a2018-12-12 07:03:00 +01004990 h2c->flags |= H2_CF_MUX_MFULL;
4991 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02004992 TRACE_STATE("too large data present in output buffer, waiting for emptiness", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau98de12a2018-12-12 07:03:00 +01004993 goto end;
4994 }
4995
4996 /* map an H2 frame to the HTX block so that we can put the
4997 * frame header there.
4998 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004999 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
5000 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005001
5002 /* prepend an H2 DATA frame header just before the DATA block */
5003 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5004 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5005 h2_set_frame_size(outbuf.area, fsize);
5006
5007 /* update windows */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005008 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005009 h2c->mws -= fsize;
5010
5011 /* and exchange with our old area */
5012 buf->area = old_area;
5013 buf->data = buf->head = 0;
5014 total += fsize;
Willy Tarreau7838a792019-08-12 18:42:03 +02005015
5016 TRACE_PROTO("sent H2 DATA frame (zero-copy)", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005017 goto end;
5018 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005019
Willy Tarreau98de12a2018-12-12 07:03:00 +01005020 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005021 /* for DATA and EOM we'll have to emit a frame, even if empty */
5022
5023 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005024 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5025 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005026 break;
5027 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005028 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005029 }
5030
5031 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005032 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5033 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005034 h2c->flags |= H2_CF_MUX_MFULL;
5035 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005036 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005037 goto end;
5038 }
5039
5040 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5041 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5042 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5043 outbuf.data = 9;
5044
5045 /* we have in <fsize> the exact number of bytes we need to copy from
5046 * the HTX buffer. We need to check this against the connection's and
5047 * the stream's send windows, and to ensure that this fits in the max
5048 * frame size and in the buffer's available space minus 9 bytes (for
5049 * the frame header). The connection's flow control is applied last so
5050 * that we can use a separate list of streams which are immediately
5051 * unblocked on window opening. Note: we don't implement padding.
5052 */
5053
5054 /* EOM is presented with bsize==1 but would lead to the emission of an
5055 * empty frame, thus we force it to zero here.
5056 */
5057 if (type == HTX_BLK_EOM)
5058 bsize = fsize = 0;
5059
5060 if (!fsize)
5061 goto send_empty;
5062
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005063 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005064 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005065 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005066 LIST_DEL_INIT(&h2s->list);
Willy Tarreau7838a792019-08-12 18:42:03 +02005067 TRACE_STATE("stream window <=0, flow-controlled", H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_H2S_FCTL, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005068 goto end;
5069 }
5070
Willy Tarreauee573762018-12-04 15:25:57 +01005071 if (fsize > count)
5072 fsize = count;
5073
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005074 if (fsize > h2s_mws(h2s))
5075 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005076
5077 if (h2c->mfs && fsize > h2c->mfs)
5078 fsize = h2c->mfs; // >0
5079
5080 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005081 /* It doesn't fit at once. If it at least fits once split and
5082 * the amount of data to move is low, let's defragment the
5083 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005084 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005085 if (b_space_wraps(mbuf) &&
5086 (fsize + 9 <= b_room(mbuf)) &&
5087 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005088 goto realign_again;
5089 fsize = outbuf.size - 9;
5090
5091 if (fsize <= 0) {
5092 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005093 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5094 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005095 h2c->flags |= H2_CF_MUX_MFULL;
5096 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005097 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005098 goto end;
5099 }
5100 }
5101
5102 if (h2c->mws <= 0) {
5103 h2s->flags |= H2_SF_BLK_MFCTL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005104 TRACE_STATE("connection window <=0, stream flow-controlled", H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_H2C_FCTL, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005105 goto end;
5106 }
5107
5108 if (fsize > h2c->mws)
5109 fsize = h2c->mws;
5110
5111 /* now let's copy this this into the output buffer */
5112 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005113 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005114 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005115 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005116
5117 send_empty:
5118 /* update the frame's size */
5119 h2_set_frame_size(outbuf.area, fsize);
5120
5121 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5122 * meeting EOM. We should optimize this later.
5123 */
5124 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005125 total++; // EOM counts as one byte
5126 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005127 es_now = 1;
5128 }
5129
5130 if (es_now)
5131 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5132
5133 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005134 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005135
5136 /* consume incoming HTX block, including EOM */
5137 total += fsize;
5138 if (fsize == bsize) {
5139 htx_remove_blk(htx, blk);
Willy Tarreau7838a792019-08-12 18:42:03 +02005140 if (fsize) {
5141 TRACE_DEVEL("more data available, trying to send another frame", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005142 goto new_frame;
Willy Tarreau7838a792019-08-12 18:42:03 +02005143 }
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005144 } else {
5145 /* we've truncated this block */
5146 htx_cut_data_blk(htx, blk, fsize);
5147 }
5148
5149 if (es_now) {
5150 if (h2s->st == H2_SS_OPEN)
5151 h2s->st = H2_SS_HLOC;
5152 else
5153 h2s_close(h2s);
5154
5155 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02005156 TRACE_PROTO("ES flag set on outgoing frame", H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005157 }
5158
5159 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005160 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005161 return total;
5162}
5163
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005164/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5165 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5166 * processed. The caller must check the stream's status to detect any error
5167 * which might have happened subsequently to a successful send. The htx blocks
5168 * are automatically removed from the message. The htx message is assumed to be
5169 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005170 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005171 * as a single frame. The ES flag is always set.
5172 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005173static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005174{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005175 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005176 struct h2c *h2c = h2s->h2c;
5177 struct htx_blk *blk;
5178 struct htx_blk *blk_end;
5179 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005180 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005181 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005182 int ret = 0;
5183 int hdr;
5184 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005185
Willy Tarreau7838a792019-08-12 18:42:03 +02005186 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5187
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005188 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005189 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005190 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02005191 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005192 goto end;
5193 }
5194
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005195 /* determine the first block which must not be deleted, blk_end may
5196 * be NULL if all blocks have to be deleted. also get trailers.
5197 */
5198 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005199 blk_end = NULL;
5200
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005201 hdr = 0;
5202 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005203 blk = htx_get_blk(htx, idx);
5204 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005205 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005206 if (type == HTX_BLK_UNUSED)
5207 continue;
5208
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005209 if (type == HTX_BLK_EOT) {
5210 if (idx != -1)
5211 blk_end = blk;
5212 break;
5213 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005214 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005215 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005216
Willy Tarreau7838a792019-08-12 18:42:03 +02005217 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5218 TRACE_PROTO("too many headers", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005219 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005220 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005221
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005222 list[hdr].n = htx_get_blk_name(htx, blk);
5223 list[hdr].v = htx_get_blk_value(htx, blk);
5224 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005225 }
5226
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005227 /* marker for end of trailers */
5228 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005229
Willy Tarreau9c218e72019-05-26 10:08:28 +02005230 mbuf = br_tail(h2c->mbuf);
5231 retry:
5232 if (!h2_get_buf(h2c, mbuf)) {
5233 h2c->flags |= H2_CF_MUX_MALLOC;
5234 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005235 TRACE_STATE("waiting for room in output buffer", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau9c218e72019-05-26 10:08:28 +02005236 goto end;
5237 }
5238
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005239 chunk_reset(&outbuf);
5240
5241 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005242 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5243 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005244 break;
5245 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005246 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005247 }
5248
5249 if (outbuf.size < 9)
5250 goto full;
5251
5252 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5253 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5254 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5255 outbuf.data = 9;
5256
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005257 /* encode all headers */
5258 for (idx = 0; idx < hdr; idx++) {
5259 /* these ones do not exist in H2 or must not appear in
5260 * trailers and must be dropped.
5261 */
5262 if (isteq(list[idx].n, ist("host")) ||
5263 isteq(list[idx].n, ist("content-length")) ||
5264 isteq(list[idx].n, ist("connection")) ||
5265 isteq(list[idx].n, ist("proxy-connection")) ||
5266 isteq(list[idx].n, ist("keep-alive")) ||
5267 isteq(list[idx].n, ist("upgrade")) ||
5268 isteq(list[idx].n, ist("te")) ||
5269 isteq(list[idx].n, ist("transfer-encoding")))
5270 continue;
5271
5272 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5273 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005274 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005275 goto realign_again;
5276 goto full;
5277 }
5278 }
5279
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005280 if (outbuf.data == 9) {
5281 /* here we have a problem, we have nothing to emit (either we
5282 * received an empty trailers block followed or we removed its
5283 * contents above). Because of this we can't send a HEADERS
5284 * frame, so we have to cheat and instead send an empty DATA
5285 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005286 */
5287 outbuf.area[3] = H2_FT_DATA;
5288 outbuf.area[4] = H2_F_DATA_END_STREAM;
5289 }
5290
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005291 /* update the frame's size */
5292 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5293
5294 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02005295 TRACE_PROTO("sent H2 trailers HEADERS frame", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_TX_EOI, h2c->conn, h2s);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005296 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005297 h2s->flags |= H2_SF_ES_SENT;
5298
5299 if (h2s->st == H2_SS_OPEN)
5300 h2s->st = H2_SS_HLOC;
5301 else
5302 h2s_close(h2s);
5303
5304 /* OK we could properly deliver the response */
5305 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005306 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005307 ret = 0;
5308 idx = htx_get_head(htx);
5309 blk = htx_get_blk(htx, idx);
5310 while (blk != blk_end) {
5311 ret += htx_get_blksz(blk);
5312 blk = htx_remove_blk(htx, blk);
5313 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005314
5315 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5316 ret += htx_get_blksz(blk_end);
5317 htx_remove_blk(htx, blk_end);
5318 }
5319
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005320 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005321 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005322 return ret;
5323 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005324 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5325 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005326 h2c->flags |= H2_CF_MUX_MFULL;
5327 h2s->flags |= H2_SF_BLK_MROOM;
5328 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005329 TRACE_STATE("mux buffer full", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005330 goto end;
5331 fail:
5332 /* unparsable HTX messages, too large ones to be produced in the local
5333 * list etc go here (unrecoverable errors).
5334 */
5335 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5336 ret = 0;
5337 goto end;
5338}
5339
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005340/* Called from the upper layer, to subscribe to events, such as being able to send.
5341 * The <param> argument here is supposed to be a pointer to a wait_event struct
5342 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5343 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5344 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5345 * returns 0 except for the error above.
5346 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005347static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5348{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005349 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005350 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005351 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005352
Willy Tarreau7838a792019-08-12 18:42:03 +02005353 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005354 if (event_type & SUB_RETRY_RECV) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005355 TRACE_DEVEL("subscribe(recv)", H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005356 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005357 BUG_ON(h2s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5358 sw->events |= SUB_RETRY_RECV;
5359 h2s->recv_wait = sw;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005360 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005361 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005362 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005363 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2c->conn, h2s);
Olivier Houchard6ff20392018-07-17 18:46:31 +02005364 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005365 BUG_ON(h2s->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5366 sw->events |= SUB_RETRY_SEND;
5367 h2s->send_wait = sw;
5368 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5369 !LIST_ADDED(&h2s->list)) {
5370 if (h2s->flags & H2_SF_BLK_MFCTL)
5371 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5372 else
5373 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005374 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005375 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005376 }
Willy Tarreau7838a792019-08-12 18:42:03 +02005377 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005378 if (event_type != 0)
5379 return -1;
5380 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005381}
5382
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005383/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5384 * The <param> argument here is supposed to be a pointer to the same wait_event
5385 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5386 * It always returns zero.
5387 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005388static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5389{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005390 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005391 struct h2s *h2s = cs->ctx;
5392
Willy Tarreau7838a792019-08-12 18:42:03 +02005393 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005394 if (event_type & SUB_RETRY_RECV) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005395 TRACE_DEVEL("unsubscribe(recv)", H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005396 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005397 BUG_ON(h2s->recv_wait != sw);
5398 sw->events &= ~SUB_RETRY_RECV;
5399 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005400 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005401 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005402 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005403 sw = param;
Olivier Houchardf8338152019-05-14 17:50:32 +02005404 BUG_ON(h2s->send_wait != sw);
5405 LIST_DEL(&h2s->list);
5406 LIST_INIT(&h2s->list);
5407 sw->events &= ~SUB_RETRY_SEND;
5408 /* We were about to send, make sure it does not happen */
5409 if (LIST_ADDED(&h2s->sending_list) &&
5410 h2s->send_wait != &h2s->wait_event) {
Willy Tarreau86eded62019-06-14 14:47:49 +02005411 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Olivier Houchardf8338152019-05-14 17:50:32 +02005412 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005413 }
Olivier Houchardf8338152019-05-14 17:50:32 +02005414 h2s->send_wait = NULL;
Olivier Houchardd846c262018-10-19 17:24:29 +02005415 }
Willy Tarreau7838a792019-08-12 18:42:03 +02005416 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005417 return 0;
5418}
5419
5420
Olivier Houchard511efea2018-08-16 15:30:32 +02005421/* Called from the upper layer, to receive data */
5422static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5423{
Olivier Houchard638b7992018-08-16 15:41:52 +02005424 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005425 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005426 struct htx *h2s_htx = NULL;
5427 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005428 size_t ret = 0;
5429
Willy Tarreau7838a792019-08-12 18:42:03 +02005430 TRACE_ENTER(H2_EV_STRM_RECV, h2c->conn, h2s);
5431
Olivier Houchard511efea2018-08-16 15:30:32 +02005432 /* transfer possibly pending data to the upper layer */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005433 h2s_htx = htx_from_buf(&h2s->rxbuf);
5434 if (htx_is_empty(h2s_htx)) {
5435 /* Here htx_to_buf() will set buffer data to 0 because
5436 * the HTX is empty.
5437 */
5438 htx_to_buf(h2s_htx, &h2s->rxbuf);
5439 goto end;
5440 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005441
Christopher Faulet9b79a102019-07-15 11:22:56 +02005442 ret = h2s_htx->data;
5443 buf_htx = htx_from_buf(buf);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005444
Christopher Faulet9b79a102019-07-15 11:22:56 +02005445 /* <buf> is empty and the message is small enough, swap the
5446 * buffers. */
5447 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005448 htx_to_buf(buf_htx, buf);
5449 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02005450 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5451 goto end;
Willy Tarreau86724e22018-12-01 23:19:43 +01005452 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02005453
5454 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
5455
5456 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
5457 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5458 if (htx_is_empty(buf_htx))
5459 cs->flags |= CS_FL_EOI;
Willy Tarreau86724e22018-12-01 23:19:43 +01005460 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005461
Christopher Faulet9b79a102019-07-15 11:22:56 +02005462 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
5463 htx_to_buf(buf_htx, buf);
5464 htx_to_buf(h2s_htx, &h2s->rxbuf);
5465 ret -= h2s_htx->data;
5466
Christopher Faulet37070b22019-02-14 15:12:14 +01005467 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005468 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005469 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005470 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005471 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005472 if (h2s->flags & H2_SF_ES_RCVD)
5473 cs->flags |= CS_FL_EOI;
Willy Tarreau76c83822019-06-15 09:55:50 +02005474 if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02005475 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005476 if (cs->flags & CS_FL_ERR_PENDING)
5477 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005478 if (b_size(&h2s->rxbuf)) {
5479 b_free(&h2s->rxbuf);
5480 offer_buffers(NULL, tasks_run_queue);
5481 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005482 }
5483
Willy Tarreau082f5592018-11-25 08:03:32 +01005484 if (ret && h2c->dsi == h2s->id) {
5485 /* demux is blocking on this stream's buffer */
5486 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005487 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005488 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005489
Willy Tarreau7838a792019-08-12 18:42:03 +02005490 TRACE_LEAVE(H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard511efea2018-08-16 15:30:32 +02005491 return ret;
5492}
5493
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005494/* stops all senders of this connection for example when the mux buffer is full.
5495 * They are moved from the sending_list to either fctl_list or send_list.
5496 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005497static void h2_stop_senders(struct h2c *h2c)
5498{
5499 struct h2s *h2s, *h2s_back;
5500
Olivier Houchardd360ac62019-03-22 17:37:16 +01005501 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5502 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreau86eded62019-06-14 14:47:49 +02005503 tasklet_remove_from_tasklet_list(h2s->send_wait->tasklet);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005504 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005505 }
5506}
5507
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005508/* Called from the upper layer, to send data from buffer <buf> for no more than
5509 * <count> bytes. Returns the number of bytes effectively sent. Some status
5510 * flags may be updated on the conn_stream.
5511 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005512static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005513{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005514 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005515 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005516 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005517 struct htx *htx;
5518 struct htx_blk *blk;
5519 enum htx_blk_type btype;
5520 uint32_t bsize;
5521 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005522
Willy Tarreau7838a792019-08-12 18:42:03 +02005523 TRACE_ENTER(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
5524
Olivier Houchardd360ac62019-03-22 17:37:16 +01005525 /* If we were not just woken because we wanted to send but couldn't,
5526 * and there's somebody else that is waiting to send, do nothing,
5527 * we will subscribe later and be put at the end of the list
5528 */
Willy Tarreauc234ae32019-05-13 17:56:11 +02005529 if (!LIST_ADDED(&h2s->sending_list) &&
Willy Tarreau7838a792019-08-12 18:42:03 +02005530 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) {
5531 TRACE_DEVEL("other streams already waiting, going to the queue and leaving", H2_EV_H2S_SEND|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005532 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005533 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005534 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005535
Olivier Houchard998410a2019-04-15 19:23:37 +02005536 /* We couldn't set it to NULL before, because we needed it in case
5537 * we had to cancel the tasklet
5538 */
5539 h2s->send_wait = NULL;
5540
Willy Tarreau7838a792019-08-12 18:42:03 +02005541 if (h2s->h2c->st0 < H2_CS_FRAME_H) {
5542 TRACE_DEVEL("connection not ready, leaving", H2_EV_H2S_SEND|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005543 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005544 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005545
Christopher Faulet9b79a102019-07-15 11:22:56 +02005546 htx = htx_from_buf(buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005547
Willy Tarreau0bad0432018-06-14 16:54:01 +02005548 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005549 h2s->flags |= H2_SF_OUTGOING_DATA;
5550
Willy Tarreau751f2d02018-10-05 09:35:00 +02005551 if (h2s->id == 0) {
5552 int32_t id = h2c_get_next_sid(h2s->h2c);
5553
5554 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005555 cs->flags |= CS_FL_ERROR;
Willy Tarreau7838a792019-08-12 18:42:03 +02005556 TRACE_DEVEL("couldn't get a stream ID, leaving in error", H2_EV_H2S_SEND|H2_EV_H2S_BLK|H2_EV_H2S_ERR|H2_EV_STRM_ERR, h2s->h2c->conn, h2s);
Willy Tarreau751f2d02018-10-05 09:35:00 +02005557 return 0;
5558 }
5559
5560 eb32_delete(&h2s->by_id);
5561 h2s->by_id.key = h2s->id = id;
5562 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005563 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005564 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5565 }
5566
Christopher Faulet9b79a102019-07-15 11:22:56 +02005567 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
5568 count && !htx_is_empty(htx)) {
5569 idx = htx_get_head(htx);
5570 blk = htx_get_blk(htx, idx);
5571 btype = htx_get_blk_type(blk);
5572 bsize = htx_get_blksz(blk);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005573
Christopher Faulet9b79a102019-07-15 11:22:56 +02005574 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005575 case HTX_BLK_REQ_SL:
5576 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005577 ret = h2s_bck_make_req_headers(h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02005578 if (ret > 0) {
5579 total += ret;
5580 count -= ret;
5581 if (ret < bsize)
5582 goto done;
5583 }
5584 break;
5585
Willy Tarreau115e83b2018-12-01 19:17:53 +01005586 case HTX_BLK_RES_SL:
5587 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005588 ret = h2s_frt_make_resp_headers(h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01005589 if (ret > 0) {
5590 total += ret;
5591 count -= ret;
5592 if (ret < bsize)
5593 goto done;
5594 }
5595 break;
5596
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005597 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005598 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005599 /* all these cause the emission of a DATA frame (possibly empty).
5600 * This EOM necessarily is one before trailers, as the EOM following
5601 * trailers would have been consumed by the trailers parser.
5602 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005603 ret = h2s_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005604 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005605 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005606 total += ret;
5607 count -= ret;
5608 if (ret < bsize)
5609 goto done;
5610 }
5611 break;
5612
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005613 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005614 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005615 /* This is the first trailers block, all the subsequent ones AND
5616 * the EOM will be swallowed by the parser.
5617 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005618 ret = h2s_make_trailers(h2s, htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005619 if (ret > 0) {
5620 total += ret;
5621 count -= ret;
5622 if (ret < bsize)
5623 goto done;
5624 }
5625 break;
5626
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005627 default:
5628 htx_remove_blk(htx, blk);
5629 total += bsize;
5630 count -= bsize;
5631 break;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005632 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005633 }
5634
Christopher Faulet9b79a102019-07-15 11:22:56 +02005635 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005636 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005637 /* trim any possibly pending data after we close (extra CR-LF,
5638 * unprocessed trailers, abnormal extra data, ...)
5639 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005640 total += count;
5641 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005642 }
5643
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005644 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005645 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005646 TRACE_DEVEL("reporting RST/error to the app-layer stream", H2_EV_H2S_SEND|H2_EV_H2S_ERR|H2_EV_STRM_ERR, h2s->h2c->conn, h2s);
Willy Tarreauec988c72018-12-19 18:00:29 +01005647 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005648 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005649 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005650 }
5651
Christopher Faulet9b79a102019-07-15 11:22:56 +02005652 htx_to_buf(htx, buf);
Olivier Houchardd846c262018-10-19 17:24:29 +02005653
5654 /* The mux is full, cancel the pending tasks */
5655 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
Willy Tarreau7838a792019-08-12 18:42:03 +02005656 (h2s->flags & H2_SF_BLK_MBUSY)) {
5657 TRACE_DEVEL("mux full, stopping senders", H2_EV_H2S_SEND|H2_EV_H2C_BLK|H2_EV_H2S_BLK, h2s->h2c->conn, h2s);
Olivier Houchardd846c262018-10-19 17:24:29 +02005658 h2_stop_senders(h2s->h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02005659 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005660
Olivier Houchard7505f942018-08-21 18:10:44 +02005661 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005662 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau7838a792019-08-12 18:42:03 +02005663 TRACE_DEVEL("data queued, waking up h2c sender", H2_EV_H2S_SEND|H2_EV_H2C_SEND, h2s->h2c->conn, h2s);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005664 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02005665
Olivier Houchard7505f942018-08-21 18:10:44 +02005666 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005667 /* If we're waiting for flow control, and we got a shutr on the
5668 * connection, we will never be unlocked, so add an error on
5669 * the conn_stream.
5670 */
5671 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5672 !b_data(&h2s->h2c->dbuf) &&
5673 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005674 TRACE_DEVEL("fctl with shutr, reporting error to app-layer", H2_EV_H2S_SEND|H2_EV_STRM_SEND|H2_EV_STRM_ERR, h2s->h2c->conn, h2s);
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005675 if (cs->flags & CS_FL_EOS)
5676 cs->flags |= CS_FL_ERROR;
5677 else
5678 cs->flags |= CS_FL_ERR_PENDING;
5679 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005680 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005681 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005682 LIST_DEL_INIT(&h2s->list);
5683 }
Willy Tarreau7838a792019-08-12 18:42:03 +02005684 TRACE_LEAVE(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005685 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005686}
5687
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005688/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005689static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005690{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005691 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005692 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005693 struct eb32_node *node;
5694 int fctl_cnt = 0;
5695 int send_cnt = 0;
5696 int tree_cnt = 0;
5697 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005698 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005699
5700 if (!h2c)
5701 return;
5702
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005703 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005704 fctl_cnt++;
5705
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005706 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005707 send_cnt++;
5708
Willy Tarreau3af37712018-12-18 14:34:41 +01005709 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005710 node = eb32_first(&h2c->streams_by_id);
5711 while (node) {
5712 h2s = container_of(node, struct h2s, by_id);
5713 tree_cnt++;
5714 if (!h2s->cs)
5715 orph_cnt++;
5716 node = eb32_next(node);
5717 }
5718
Willy Tarreau60f62682019-05-26 11:32:27 +02005719 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005720 tmbuf = br_tail(h2c->mbuf);
Willy Tarreauab2ec452019-08-30 07:07:08 +02005721 chunk_appendf(msg, " h2c.st0=%s .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
Willy Tarreau987c0632018-12-18 10:32:05 +01005722 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02005723 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
5724 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreauab2ec452019-08-30 07:07:08 +02005725 h2c_st_to_str(h2c->st0), h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
Willy Tarreau616ac812018-07-24 14:12:42 +02005726 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005727 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005728 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5729 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5730 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02005731 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
5732 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
5733 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02005734 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
5735 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01005736
5737 if (h2s) {
Willy Tarreauab2ec452019-08-30 07:07:08 +02005738 chunk_appendf(msg, " last_h2s=%p .id=%d .st=%s.flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5739 h2s, h2s->id, h2s_st_to_str(h2s->st), h2s->flags,
Willy Tarreau987c0632018-12-18 10:32:05 +01005740 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5741 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5742 h2s->cs);
5743 if (h2s->cs)
5744 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5745 h2s->cs->flags, h2s->cs->data);
5746 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005747}
Willy Tarreau62f52692017-10-08 23:01:42 +02005748
5749/*******************************************************/
5750/* functions below are dedicated to the config parsers */
5751/*******************************************************/
5752
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005753/* config parser for global "tune.h2.header-table-size" */
5754static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5755 struct proxy *defpx, const char *file, int line,
5756 char **err)
5757{
5758 if (too_many_args(1, args, err, NULL))
5759 return -1;
5760
5761 h2_settings_header_table_size = atoi(args[1]);
5762 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5763 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5764 return -1;
5765 }
5766 return 0;
5767}
Willy Tarreau62f52692017-10-08 23:01:42 +02005768
Willy Tarreaue6baec02017-07-27 11:45:11 +02005769/* config parser for global "tune.h2.initial-window-size" */
5770static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5771 struct proxy *defpx, const char *file, int line,
5772 char **err)
5773{
5774 if (too_many_args(1, args, err, NULL))
5775 return -1;
5776
5777 h2_settings_initial_window_size = atoi(args[1]);
5778 if (h2_settings_initial_window_size < 0) {
5779 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5780 return -1;
5781 }
5782 return 0;
5783}
5784
Willy Tarreau5242ef82017-07-27 11:47:28 +02005785/* config parser for global "tune.h2.max-concurrent-streams" */
5786static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5787 struct proxy *defpx, const char *file, int line,
5788 char **err)
5789{
5790 if (too_many_args(1, args, err, NULL))
5791 return -1;
5792
5793 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005794 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005795 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5796 return -1;
5797 }
5798 return 0;
5799}
5800
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005801/* config parser for global "tune.h2.max-frame-size" */
5802static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5803 struct proxy *defpx, const char *file, int line,
5804 char **err)
5805{
5806 if (too_many_args(1, args, err, NULL))
5807 return -1;
5808
5809 h2_settings_max_frame_size = atoi(args[1]);
5810 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5811 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5812 return -1;
5813 }
5814 return 0;
5815}
5816
Willy Tarreau62f52692017-10-08 23:01:42 +02005817
5818/****************************************/
5819/* MUX initialization and instanciation */
5820/***************************************/
5821
5822/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005823static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005824 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005825 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005826 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005827 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005828 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005829 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005830 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005831 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005832 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005833 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005834 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005835 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005836 .shutr = h2_shutr,
5837 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005838 .show_fd = h2_show_fd,
Christopher Faulet9b79a102019-07-15 11:22:56 +02005839 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Willy Tarreau62f52692017-10-08 23:01:42 +02005840 .name = "H2",
5841};
5842
Christopher Faulet32f61c02018-04-10 14:33:41 +02005843static struct mux_proto_list mux_proto_h2 =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02005844 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005845
Willy Tarreau0108d902018-11-25 19:14:37 +01005846INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5847
Willy Tarreau62f52692017-10-08 23:01:42 +02005848/* config keyword parsers */
5849static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005850 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005851 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005852 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005853 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005854 { 0, NULL, NULL }
5855}};
5856
Willy Tarreau0108d902018-11-25 19:14:37 +01005857INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);