blob: 102e74ee721e89e63b34bb7ff0ac6c013b15b894 [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
Willy Tarreaudfd3de82020-06-04 23:46:14 +020013#include <import/eb32tree.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020015#include <haproxy/istbuf.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020016#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020017#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020018#include <haproxy/h1.h>
Willy Tarreaubf073142020-06-03 12:04:01 +020019#include <haproxy/h2.h>
Willy Tarreaube327fa2020-06-03 09:09:57 +020020#include <haproxy/hpack-dec.h>
21#include <haproxy/hpack-enc.h>
22#include <haproxy/hpack-tbl.h>
Willy Tarreau87735332020-06-04 09:08:41 +020023#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020024#include <haproxy/htx.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020025#include <haproxy/net_helper.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020026#include <haproxy/session-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020028#include <haproxy/stream_interface.h>
Willy Tarreauc6d61d72020-06-04 19:02:42 +020029#include <haproxy/trace.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
Willy Tarreau51330962019-05-26 09:38:07 +020081
Willy Tarreau9c218e72019-05-26 10:08:28 +020082/* 32 buffers: one for the ring's root, rest for the mbuf itself */
83#define H2C_MBUF_CNT 32
Willy Tarreau51330962019-05-26 09:38:07 +020084
Willy Tarreau5ab6b572017-09-22 08:05:00 +020085/* H2 connection descriptor */
86struct h2c {
87 struct connection *conn;
88
89 enum h2_cs st0; /* mux state */
90 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
91
92 /* 16 bit hole here */
93 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010094 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020095 int32_t max_id; /* highest ID known on this connection, <0 before preface */
96 uint32_t rcvd_c; /* newly received data to ACK for the connection */
97 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
98
99 /* states for the demux direction */
100 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200101 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200102
103 int32_t dsi; /* demux stream ID (<0 = idle) */
104 int32_t dfl; /* demux frame length (if dsi >= 0) */
105 int8_t dft; /* demux frame type (if dsi >= 0) */
106 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100107 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
108 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
110
111 /* states for the mux direction */
Willy Tarreau51330962019-05-26 09:38:07 +0200112 struct buffer mbuf[H2C_MBUF_CNT]; /* mux buffers (ring) */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200113 int32_t msi; /* mux stream ID (<0 = idle) */
114 int32_t mfl; /* mux frame length (if dsi >= 0) */
115 int8_t mft; /* mux frame type (if dsi >= 0) */
116 int8_t mff; /* mux frame flags (if dsi >= 0) */
117 /* 16 bit hole here */
118 int32_t miw; /* mux initial window size for all new streams */
119 int32_t mws; /* mux window size. Can be negative. */
120 int32_t mfs; /* mux's max frame size */
121
Willy Tarreauea392822017-10-31 10:02:25 +0100122 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100123 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100124 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200125 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100126 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100127 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200128 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100129 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200130 struct eb_root streams_by_id; /* all active streams by their ID */
131 struct list send_list; /* list of blocked streams requesting to send */
132 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau9edf6db2019-10-02 10:49:59 +0200133 struct list blocked_list; /* list of streams blocked for other reasons (e.g. sfctl, dep) */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100134 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200135 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200136};
137
Willy Tarreau18312642017-10-11 07:57:07 +0200138/* H2 stream state, in h2s->st */
139enum h2_ss {
140 H2_SS_IDLE = 0, // idle
141 H2_SS_RLOC, // reserved(local)
142 H2_SS_RREM, // reserved(remote)
143 H2_SS_OPEN, // open
144 H2_SS_HREM, // half-closed(remote)
145 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200146 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200147 H2_SS_CLOSED, // closed
148 H2_SS_ENTRIES // must be last
149} __attribute__((packed));
150
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200151#define H2_SS_MASK(state) (1UL << (state))
152#define H2_SS_IDLE_BIT (1UL << H2_SS_IDLE)
153#define H2_SS_RLOC_BIT (1UL << H2_SS_RLOC)
154#define H2_SS_RREM_BIT (1UL << H2_SS_RREM)
155#define H2_SS_OPEN_BIT (1UL << H2_SS_OPEN)
156#define H2_SS_HREM_BIT (1UL << H2_SS_HREM)
157#define H2_SS_HLOC_BIT (1UL << H2_SS_HLOC)
158#define H2_SS_ERROR_BIT (1UL << H2_SS_ERROR)
159#define H2_SS_CLOSED_BIT (1UL << H2_SS_CLOSED)
Willy Tarreau4c688eb2019-05-14 11:44:03 +0200160
Willy Tarreau18312642017-10-11 07:57:07 +0200161/* HTTP/2 stream flags (32 bit), in h2s->flags */
162#define H2_SF_NONE 0x00000000
163#define H2_SF_ES_RCVD 0x00000001
164#define H2_SF_ES_SENT 0x00000002
165
166#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
167#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
168
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200169/* stream flags indicating the reason the stream is blocked */
170#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
Willy Tarreau9edf6db2019-10-02 10:49:59 +0200171#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux (must be in send list)
172#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl (must be in fctl list)
173#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl (must be in blocked list)
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200174#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
175
Willy Tarreau454f9052017-10-26 19:40:35 +0200176/* stream flags indicating how data is supposed to be sent */
177#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
Willy Tarreaud9464162020-01-10 18:25:07 +0100178/* unused flags: 0x00000200, 0x00000400 */
Willy Tarreau454f9052017-10-26 19:40:35 +0200179
Willy Tarreaud9464162020-01-10 18:25:07 +0100180#define H2_SF_NOTIFIED 0x00000800 // a paused stream was notified to try to send again
Willy Tarreau67434202017-11-06 20:20:51 +0100181#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100182#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100183
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100184#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
185
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200186#define H2_SF_WANT_SHUTR 0x00008000 // a stream couldn't shutr() (mux full/busy)
187#define H2_SF_WANT_SHUTW 0x00010000 // a stream couldn't shutw() (mux full/busy)
Willy Tarreau3cf69fe2019-05-14 10:44:40 +0200188#define H2_SF_KILL_CONN 0x00020000 // kill the whole connection with this stream
Willy Tarreau2c249eb2019-05-13 18:06:17 +0200189
190
Willy Tarreau18312642017-10-11 07:57:07 +0200191/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
192 * it is being processed in the internal HTTP representation (H1 for now).
193 */
194struct h2s {
195 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100196 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200197 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200198 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200199 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200200 int32_t id; /* stream ID */
201 uint32_t flags; /* H2_SF_* */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +0200202 int sws; /* stream window size, to be added to the mux's initial window size */
Willy Tarreau18312642017-10-11 07:57:07 +0200203 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
204 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200205 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100206 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200207 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Willy Tarreauf96508a2020-01-10 11:12:48 +0100208 struct wait_event *subs; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200209 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau5723f292020-01-10 15:16:57 +0100210 struct tasklet *shut_tl; /* deferred shutdown tasklet, to retry to send an RST after we failed to,
211 * in case there's no other subscription to do it */
Willy Tarreau18312642017-10-11 07:57:07 +0200212};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200213
Willy Tarreauc6405142017-09-21 20:23:50 +0200214/* descriptor for an h2 frame header */
215struct h2_fh {
216 uint32_t len; /* length, host order, 24 bits */
217 uint32_t sid; /* stream id, host order, 31 bits */
218 uint8_t ft; /* frame type */
219 uint8_t ff; /* frame flags */
220};
221
Willy Tarreau12ae2122019-08-08 18:23:12 +0200222/* trace source and events */
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200223static void h2_trace(enum trace_level level, uint64_t mask, \
224 const struct trace_source *src,
225 const struct ist where, const struct ist func,
226 const void *a1, const void *a2, const void *a3, const void *a4);
Willy Tarreau12ae2122019-08-08 18:23:12 +0200227
228/* The event representation is split like this :
229 * strm - application layer
230 * h2s - internal H2 stream
231 * h2c - internal H2 connection
232 * conn - external connection
233 *
234 */
235static const struct trace_event h2_trace_events[] = {
236#define H2_EV_H2C_NEW (1ULL << 0)
Willy Tarreau87951942019-08-30 07:34:36 +0200237 { .mask = H2_EV_H2C_NEW, .name = "h2c_new", .desc = "new H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200238#define H2_EV_H2C_RECV (1ULL << 1)
Willy Tarreau87951942019-08-30 07:34:36 +0200239 { .mask = H2_EV_H2C_RECV, .name = "h2c_recv", .desc = "Rx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200240#define H2_EV_H2C_SEND (1ULL << 2)
Willy Tarreau87951942019-08-30 07:34:36 +0200241 { .mask = H2_EV_H2C_SEND, .name = "h2c_send", .desc = "Tx on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200242#define H2_EV_H2C_FCTL (1ULL << 3)
Willy Tarreau87951942019-08-30 07:34:36 +0200243 { .mask = H2_EV_H2C_FCTL, .name = "h2c_fctl", .desc = "H2 connection flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200244#define H2_EV_H2C_BLK (1ULL << 4)
Willy Tarreau87951942019-08-30 07:34:36 +0200245 { .mask = H2_EV_H2C_BLK, .name = "h2c_blk", .desc = "H2 connection blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200246#define H2_EV_H2C_WAKE (1ULL << 5)
Willy Tarreau87951942019-08-30 07:34:36 +0200247 { .mask = H2_EV_H2C_WAKE, .name = "h2c_wake", .desc = "H2 connection woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200248#define H2_EV_H2C_END (1ULL << 6)
Willy Tarreau87951942019-08-30 07:34:36 +0200249 { .mask = H2_EV_H2C_END, .name = "h2c_end", .desc = "H2 connection terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200250#define H2_EV_H2C_ERR (1ULL << 7)
Willy Tarreau87951942019-08-30 07:34:36 +0200251 { .mask = H2_EV_H2C_ERR, .name = "h2c_err", .desc = "error on H2 connection" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200252#define H2_EV_RX_FHDR (1ULL << 8)
Willy Tarreau87951942019-08-30 07:34:36 +0200253 { .mask = H2_EV_RX_FHDR, .name = "rx_fhdr", .desc = "H2 frame header received" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200254#define H2_EV_RX_FRAME (1ULL << 9)
Willy Tarreau87951942019-08-30 07:34:36 +0200255 { .mask = H2_EV_RX_FRAME, .name = "rx_frame", .desc = "receipt of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200256#define H2_EV_RX_EOI (1ULL << 10)
Willy Tarreau87951942019-08-30 07:34:36 +0200257 { .mask = H2_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H2 input (ES or RST)" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200258#define H2_EV_RX_PREFACE (1ULL << 11)
Willy Tarreau87951942019-08-30 07:34:36 +0200259 { .mask = H2_EV_RX_PREFACE, .name = "rx_preface", .desc = "receipt of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200260#define H2_EV_RX_DATA (1ULL << 12)
Willy Tarreau87951942019-08-30 07:34:36 +0200261 { .mask = H2_EV_RX_DATA, .name = "rx_data", .desc = "receipt of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200262#define H2_EV_RX_HDR (1ULL << 13)
Willy Tarreau87951942019-08-30 07:34:36 +0200263 { .mask = H2_EV_RX_HDR, .name = "rx_hdr", .desc = "receipt of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200264#define H2_EV_RX_PRIO (1ULL << 14)
Willy Tarreau87951942019-08-30 07:34:36 +0200265 { .mask = H2_EV_RX_PRIO, .name = "rx_prio", .desc = "receipt of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200266#define H2_EV_RX_RST (1ULL << 15)
Willy Tarreau87951942019-08-30 07:34:36 +0200267 { .mask = H2_EV_RX_RST, .name = "rx_rst", .desc = "receipt of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200268#define H2_EV_RX_SETTINGS (1ULL << 16)
Willy Tarreau87951942019-08-30 07:34:36 +0200269 { .mask = H2_EV_RX_SETTINGS, .name = "rx_settings", .desc = "receipt of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200270#define H2_EV_RX_PUSH (1ULL << 17)
Willy Tarreau87951942019-08-30 07:34:36 +0200271 { .mask = H2_EV_RX_PUSH, .name = "rx_push", .desc = "receipt of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200272#define H2_EV_RX_PING (1ULL << 18)
Willy Tarreau87951942019-08-30 07:34:36 +0200273 { .mask = H2_EV_RX_PING, .name = "rx_ping", .desc = "receipt of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200274#define H2_EV_RX_GOAWAY (1ULL << 19)
Willy Tarreau87951942019-08-30 07:34:36 +0200275 { .mask = H2_EV_RX_GOAWAY, .name = "rx_goaway", .desc = "receipt of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200276#define H2_EV_RX_WU (1ULL << 20)
Willy Tarreau87951942019-08-30 07:34:36 +0200277 { .mask = H2_EV_RX_WU, .name = "rx_wu", .desc = "receipt of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200278#define H2_EV_RX_CONT (1ULL << 21)
Willy Tarreau87951942019-08-30 07:34:36 +0200279 { .mask = H2_EV_RX_CONT, .name = "rx_cont", .desc = "receipt of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200280#define H2_EV_TX_FRAME (1ULL << 22)
Willy Tarreau87951942019-08-30 07:34:36 +0200281 { .mask = H2_EV_TX_FRAME, .name = "tx_frame", .desc = "transmission of any H2 frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200282#define H2_EV_TX_EOI (1ULL << 23)
Willy Tarreau87951942019-08-30 07:34:36 +0200283 { .mask = H2_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of H2 end of input (ES or RST)" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200284#define H2_EV_TX_PREFACE (1ULL << 24)
Willy Tarreau87951942019-08-30 07:34:36 +0200285 { .mask = H2_EV_TX_PREFACE, .name = "tx_preface", .desc = "transmission of H2 preface" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200286#define H2_EV_TX_DATA (1ULL << 25)
Willy Tarreau87951942019-08-30 07:34:36 +0200287 { .mask = H2_EV_TX_DATA, .name = "tx_data", .desc = "transmission of H2 DATA frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200288#define H2_EV_TX_HDR (1ULL << 26)
Willy Tarreau87951942019-08-30 07:34:36 +0200289 { .mask = H2_EV_TX_HDR, .name = "tx_hdr", .desc = "transmission of H2 HEADERS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200290#define H2_EV_TX_PRIO (1ULL << 27)
Willy Tarreau87951942019-08-30 07:34:36 +0200291 { .mask = H2_EV_TX_PRIO, .name = "tx_prio", .desc = "transmission of H2 PRIORITY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200292#define H2_EV_TX_RST (1ULL << 28)
Willy Tarreau87951942019-08-30 07:34:36 +0200293 { .mask = H2_EV_TX_RST, .name = "tx_rst", .desc = "transmission of H2 RST_STREAM frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200294#define H2_EV_TX_SETTINGS (1ULL << 29)
Willy Tarreau87951942019-08-30 07:34:36 +0200295 { .mask = H2_EV_TX_SETTINGS, .name = "tx_settings", .desc = "transmission of H2 SETTINGS frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200296#define H2_EV_TX_PUSH (1ULL << 30)
Willy Tarreau87951942019-08-30 07:34:36 +0200297 { .mask = H2_EV_TX_PUSH, .name = "tx_push", .desc = "transmission of H2 PUSH_PROMISE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200298#define H2_EV_TX_PING (1ULL << 31)
Willy Tarreau87951942019-08-30 07:34:36 +0200299 { .mask = H2_EV_TX_PING, .name = "tx_ping", .desc = "transmission of H2 PING frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200300#define H2_EV_TX_GOAWAY (1ULL << 32)
Willy Tarreau87951942019-08-30 07:34:36 +0200301 { .mask = H2_EV_TX_GOAWAY, .name = "tx_goaway", .desc = "transmission of H2 GOAWAY frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200302#define H2_EV_TX_WU (1ULL << 33)
Willy Tarreau87951942019-08-30 07:34:36 +0200303 { .mask = H2_EV_TX_WU, .name = "tx_wu", .desc = "transmission of H2 WINDOW_UPDATE frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200304#define H2_EV_TX_CONT (1ULL << 34)
Willy Tarreau87951942019-08-30 07:34:36 +0200305 { .mask = H2_EV_TX_CONT, .name = "tx_cont", .desc = "transmission of H2 CONTINUATION frame" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200306#define H2_EV_H2S_NEW (1ULL << 35)
Willy Tarreau87951942019-08-30 07:34:36 +0200307 { .mask = H2_EV_H2S_NEW, .name = "h2s_new", .desc = "new H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200308#define H2_EV_H2S_RECV (1ULL << 36)
Willy Tarreau87951942019-08-30 07:34:36 +0200309 { .mask = H2_EV_H2S_RECV, .name = "h2s_recv", .desc = "Rx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200310#define H2_EV_H2S_SEND (1ULL << 37)
Willy Tarreau87951942019-08-30 07:34:36 +0200311 { .mask = H2_EV_H2S_SEND, .name = "h2s_send", .desc = "Tx for H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200312#define H2_EV_H2S_FCTL (1ULL << 38)
Willy Tarreau87951942019-08-30 07:34:36 +0200313 { .mask = H2_EV_H2S_FCTL, .name = "h2s_fctl", .desc = "H2 stream flow-controlled" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200314#define H2_EV_H2S_BLK (1ULL << 39)
Willy Tarreau87951942019-08-30 07:34:36 +0200315 { .mask = H2_EV_H2S_BLK, .name = "h2s_blk", .desc = "H2 stream blocked" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200316#define H2_EV_H2S_WAKE (1ULL << 40)
Willy Tarreau87951942019-08-30 07:34:36 +0200317 { .mask = H2_EV_H2S_WAKE, .name = "h2s_wake", .desc = "H2 stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200318#define H2_EV_H2S_END (1ULL << 41)
Willy Tarreau87951942019-08-30 07:34:36 +0200319 { .mask = H2_EV_H2S_END, .name = "h2s_end", .desc = "H2 stream terminated" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200320#define H2_EV_H2S_ERR (1ULL << 42)
Willy Tarreau87951942019-08-30 07:34:36 +0200321 { .mask = H2_EV_H2S_ERR, .name = "h2s_err", .desc = "error on H2 stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200322#define H2_EV_STRM_NEW (1ULL << 43)
Willy Tarreau87951942019-08-30 07:34:36 +0200323 { .mask = H2_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200324#define H2_EV_STRM_RECV (1ULL << 44)
Willy Tarreau87951942019-08-30 07:34:36 +0200325 { .mask = H2_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200326#define H2_EV_STRM_SEND (1ULL << 45)
Willy Tarreau87951942019-08-30 07:34:36 +0200327 { .mask = H2_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200328#define H2_EV_STRM_FULL (1ULL << 46)
Willy Tarreau87951942019-08-30 07:34:36 +0200329 { .mask = H2_EV_STRM_FULL, .name = "strm_full", .desc = "stream buffer full" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200330#define H2_EV_STRM_WAKE (1ULL << 47)
Willy Tarreau87951942019-08-30 07:34:36 +0200331 { .mask = H2_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200332#define H2_EV_STRM_SHUT (1ULL << 48)
Willy Tarreau87951942019-08-30 07:34:36 +0200333 { .mask = H2_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200334#define H2_EV_STRM_END (1ULL << 49)
Willy Tarreau87951942019-08-30 07:34:36 +0200335 { .mask = H2_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200336#define H2_EV_STRM_ERR (1ULL << 50)
Willy Tarreau87951942019-08-30 07:34:36 +0200337 { .mask = H2_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200338#define H2_EV_PROTO_ERR (1ULL << 51)
Willy Tarreau87951942019-08-30 07:34:36 +0200339 { .mask = H2_EV_PROTO_ERR, .name = "proto_err", .desc = "protocol error" },
Willy Tarreau12ae2122019-08-08 18:23:12 +0200340 { }
341};
342
343static const struct name_desc h2_trace_lockon_args[4] = {
344 /* arg1 */ { /* already used by the connection */ },
345 /* arg2 */ { .name="h2s", .desc="H2 stream" },
346 /* arg3 */ { },
347 /* arg4 */ { }
348};
349
350static const struct name_desc h2_trace_decoding[] = {
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200351#define H2_VERB_CLEAN 1
352 { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" },
353#define H2_VERB_MINIMAL 2
Willy Tarreau12ae2122019-08-08 18:23:12 +0200354 { .name="minimal", .desc="report only h2c/h2s state and flags, no real decoding" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200355#define H2_VERB_SIMPLE 3
Willy Tarreau12ae2122019-08-08 18:23:12 +0200356 { .name="simple", .desc="add request/response status line or frame info when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200357#define H2_VERB_ADVANCED 4
Willy Tarreau12ae2122019-08-08 18:23:12 +0200358 { .name="advanced", .desc="add header fields or frame decoding when available" },
Willy Tarreauf7dd5192019-08-30 07:21:18 +0200359#define H2_VERB_COMPLETE 5
Willy Tarreau12ae2122019-08-08 18:23:12 +0200360 { .name="complete", .desc="add full data dump when available" },
361 { /* end */ }
362};
363
364static struct trace_source trace_h2 = {
365 .name = IST("h2"),
366 .desc = "HTTP/2 multiplexer",
367 .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200368 .default_cb = h2_trace,
Willy Tarreau12ae2122019-08-08 18:23:12 +0200369 .known_events = h2_trace_events,
370 .lockon_args = h2_trace_lockon_args,
371 .decoding = h2_trace_decoding,
372 .report_events = ~0, // report everything by default
373};
374
375#define TRACE_SOURCE &trace_h2
376INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE);
377
Willy Tarreau8ceae722018-11-26 11:58:30 +0100378/* the h2c connection pool */
379DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
380
381/* the h2s stream pool */
382DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
383
Willy Tarreaudc572362018-12-12 08:08:05 +0100384/* The default connection window size is 65535, it may only be enlarged using
385 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
386 * we'll pretend we already received the difference between the two to send
387 * an equivalent window update to enlarge it to 2G-1.
388 */
389#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
390
Willy Tarreau455d5682019-05-24 19:42:18 +0200391/* maximum amount of data we're OK with re-aligning for buffer optimizations */
392#define MAX_DATA_REALIGN 1024
393
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200394/* a few settings from the global section */
395static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200396static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100397static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100398static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200399
Willy Tarreau2a856182017-05-16 15:20:39 +0200400/* a dmumy closed stream */
401static const struct h2s *h2_closed_stream = &(const struct h2s){
402 .cs = NULL,
403 .h2c = NULL,
404 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100405 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100406 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200407 .id = 0,
408};
409
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100410/* a dmumy closed stream returning a PROTOCOL_ERROR error */
411static const struct h2s *h2_error_stream = &(const struct h2s){
412 .cs = NULL,
413 .h2c = NULL,
414 .st = H2_SS_CLOSED,
415 .errcode = H2_ERR_PROTOCOL_ERROR,
416 .flags = 0,
417 .id = 0,
418};
419
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100420/* a dmumy closed stream returning a REFUSED_STREAM error */
421static const struct h2s *h2_refused_stream = &(const struct h2s){
422 .cs = NULL,
423 .h2c = NULL,
424 .st = H2_SS_CLOSED,
425 .errcode = H2_ERR_REFUSED_STREAM,
426 .flags = 0,
427 .id = 0,
428};
429
Willy Tarreau2a856182017-05-16 15:20:39 +0200430/* and a dummy idle stream for use with any unannounced stream */
431static const struct h2s *h2_idle_stream = &(const struct h2s){
432 .cs = NULL,
433 .h2c = NULL,
434 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100435 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200436 .id = 0,
437};
438
Olivier Houchard9f6af332018-05-25 14:04:04 +0200439static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200440static int h2_send(struct h2c *h2c);
441static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200442static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200443static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100444static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100445static 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 +0100446static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200447static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100448static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100449static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200450
Willy Tarreauab2ec452019-08-30 07:07:08 +0200451/* returns a h2c state as an abbreviated 3-letter string, or "???" if unknown */
452static inline const char *h2c_st_to_str(enum h2_cs st)
453{
454 switch (st) {
455 case H2_CS_PREFACE: return "PRF";
456 case H2_CS_SETTINGS1: return "STG";
457 case H2_CS_FRAME_H: return "FRH";
458 case H2_CS_FRAME_P: return "FRP";
459 case H2_CS_FRAME_A: return "FRA";
460 case H2_CS_FRAME_E: return "FRE";
461 case H2_CS_ERROR: return "ERR";
462 case H2_CS_ERROR2: return "ER2";
463 default: return "???";
464 }
465}
466
467/* returns a h2s state as an abbreviated 3-letter string, or "???" if unknown */
468static inline const char *h2s_st_to_str(enum h2_ss st)
469{
470 switch (st) {
471 case H2_SS_IDLE: return "IDL"; // idle
472 case H2_SS_RLOC: return "RSL"; // reserved local
473 case H2_SS_RREM: return "RSR"; // reserved remote
474 case H2_SS_OPEN: return "OPN"; // open
475 case H2_SS_HREM: return "HCR"; // half-closed remote
476 case H2_SS_HLOC: return "HCL"; // half-closed local
477 case H2_SS_ERROR : return "ERR"; // error
478 case H2_SS_CLOSED: return "CLO"; // closed
479 default: return "???";
480 }
481}
482
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200483/* the H2 traces always expect that arg1, if non-null, is of type connection
484 * (from which we can derive h2c), that arg2, if non-null, is of type h2s, and
485 * that arg3, if non-null, is either of type htx for tx headers, or of type
486 * buffer for everything else.
487 */
488static void h2_trace(enum trace_level level, uint64_t mask, const struct trace_source *src,
489 const struct ist where, const struct ist func,
490 const void *a1, const void *a2, const void *a3, const void *a4)
491{
492 const struct connection *conn = a1;
493 const struct h2c *h2c = conn ? conn->ctx : NULL;
494 const struct h2s *h2s = a2;
495 const struct buffer *buf = a3;
496 const struct htx *htx;
497 int pos;
498
499 if (!h2c) // nothing to add
500 return;
501
Willy Tarreau17104d42019-08-30 07:12:55 +0200502 if (src->verbosity > H2_VERB_CLEAN) {
Willy Tarreau73db4342019-09-25 07:28:44 +0200503 chunk_appendf(&trace_buf, " : h2c=%p(%c,%s)", h2c, conn_is_back(conn) ? 'B' : 'F', h2c_st_to_str(h2c->st0));
504
Willy Tarreauf3ce0412019-11-24 14:57:00 +0100505 if (h2c->errcode)
506 chunk_appendf(&trace_buf, " err=%s/%02x", h2_err_str(h2c->errcode), h2c->errcode);
507
Willy Tarreau73db4342019-09-25 07:28:44 +0200508 if (h2c->dsi >= 0 &&
509 (mask & (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) == (H2_EV_RX_FRAME|H2_EV_RX_FHDR)) {
510 chunk_appendf(&trace_buf, " dft=%s/%02x", h2_ft_str(h2c->dft), h2c->dff);
511 }
512
513 if (h2s) {
514 if (h2s->id <= 0)
515 chunk_appendf(&trace_buf, " dsi=%d", h2c->dsi);
516 chunk_appendf(&trace_buf, " h2s=%p(%d,%s)", h2s, h2s->id, h2s_st_to_str(h2s->st));
Willy Tarreauf3ce0412019-11-24 14:57:00 +0100517 if (h2s->id && h2s->errcode)
518 chunk_appendf(&trace_buf, " err=%s/%02x", h2_err_str(h2s->errcode), h2s->errcode);
Willy Tarreau73db4342019-09-25 07:28:44 +0200519 }
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200520 }
521
522 /* Let's dump decoded requests and responses right after parsing. They
523 * are traced at level USER with a few recognizable flags.
524 */
525 if ((mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW) ||
526 mask == (H2_EV_RX_FRAME|H2_EV_RX_HDR)) && buf)
527 htx = htxbuf(buf); // recv req/res
528 else if (mask == (H2_EV_TX_FRAME|H2_EV_TX_HDR))
529 htx = a3; // send req/res
530 else
531 htx = NULL;
532
Willy Tarreau94f1dcf2019-08-30 07:11:30 +0200533 if (level == TRACE_LEVEL_USER && src->verbosity != H2_VERB_MINIMAL && htx && (pos = htx_get_head(htx)) != -1) {
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200534 const struct htx_blk *blk = htx_get_blk(htx, pos);
535 const struct htx_sl *sl = htx_get_blk_ptr(htx, blk);
536 enum htx_blk_type type = htx_get_blk_type(blk);
537
538 if (type == HTX_BLK_REQ_SL)
539 chunk_appendf(&trace_buf, " : [%d] H2 REQ: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200540 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200541 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
542 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
543 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
544 else if (type == HTX_BLK_RES_SL)
545 chunk_appendf(&trace_buf, " : [%d] H2 RES: %.*s %.*s %.*s",
Willy Tarreauc067a3a2019-08-30 07:28:24 +0200546 h2s ? h2s->id : h2c->dsi,
Willy Tarreaudb3cfff2019-08-19 17:56:27 +0200547 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
548 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
549 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
550 }
551}
552
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200553/* returns true if the connection is allowed to expire, false otherwise. A
554 * connection may expire when:
555 * - it has no stream
556 * - it has data in the mux buffer
557 * - it has streams in the blocked list
558 * - it has streams in the fctl list
559 * - it has streams in the send list
560 * Otherwise it means some streams are waiting in the data layer and it should
561 * not expire.
562 */
563static inline int h2c_may_expire(const struct h2c *h2c)
564{
565 return eb_is_empty(&h2c->streams_by_id) ||
566 br_data(h2c->mbuf) ||
567 !LIST_ISEMPTY(&h2c->blocked_list) ||
568 !LIST_ISEMPTY(&h2c->fctl_list) ||
Willy Tarreaud9464162020-01-10 18:25:07 +0100569 !LIST_ISEMPTY(&h2c->send_list);
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200570}
571
Olivier Houchard7a977432019-03-21 15:47:13 +0100572static __inline int
Willy Tarreauc2ea47f2019-10-01 10:12:00 +0200573h2c_is_dead(const struct h2c *h2c)
Olivier Houchard7a977432019-03-21 15:47:13 +0100574{
575 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
576 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
577 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
578 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreau662fafc2019-05-26 09:43:07 +0200579 (!br_data(h2c->mbuf) && /* mux buffer empty, also process clean events below */
Olivier Houchard7a977432019-03-21 15:47:13 +0100580 (conn_xprt_read0_pending(h2c->conn) ||
581 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
582 return 1;
583
584 return 0;
Olivier Houchard7a977432019-03-21 15:47:13 +0100585}
586
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200587/*****************************************************/
588/* functions below are for dynamic buffer management */
589/*****************************************************/
590
Willy Tarreau315d8072017-12-10 22:17:57 +0100591/* indicates whether or not the we may call the h2_recv() function to attempt
592 * to receive data into the buffer and/or demux pending data. The condition is
593 * a bit complex due to some API limits for now. The rules are the following :
594 * - if an error or a shutdown was detected on the connection and the buffer
595 * is empty, we must not attempt to receive
596 * - if the demux buf failed to be allocated, we must not try to receive and
597 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100598 * - if no flag indicates a blocking condition, we may attempt to receive,
599 * regardless of whether the demux buffer is full or not, so that only
600 * de demux part decides whether or not to block. This is needed because
601 * the connection API indeed prevents us from re-enabling receipt that is
602 * already enabled in a polled state, so we must always immediately stop
603 * as soon as the demux can't proceed so as never to hit an end of read
604 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100605 * - otherwise must may not attempt
606 */
607static inline int h2_recv_allowed(const struct h2c *h2c)
608{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200609 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100610 (h2c->st0 >= H2_CS_ERROR ||
611 h2c->conn->flags & CO_FL_ERROR ||
612 conn_xprt_read0_pending(h2c->conn)))
613 return 0;
614
615 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100616 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100617 return 1;
618
619 return 0;
620}
621
Willy Tarreau47b515a2018-12-21 16:09:41 +0100622/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200623static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100624{
625 if (!h2_recv_allowed(h2c))
626 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200627 if ((!consider_buffer || !b_data(&h2c->dbuf))
628 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100629 return;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200630 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau47b515a2018-12-21 16:09:41 +0100631}
632
633
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100634/* returns true if the front connection has too many conn_streams attached */
635static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200636{
Willy Tarreaua8754662018-12-23 20:43:58 +0100637 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200638}
639
Willy Tarreau44e973f2018-03-01 17:49:30 +0100640/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
641 * flags are used to figure what buffer was requested. It returns 1 if the
642 * allocation succeeds, in which case the connection is woken up, or 0 if it's
643 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200644 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100645static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200646{
647 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100648 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200649
Willy Tarreau44e973f2018-03-01 17:49:30 +0100650 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200651 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200652 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200653 return 1;
654 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200655
Willy Tarreau51330962019-05-26 09:38:07 +0200656 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(br_tail(h2c->mbuf), 0)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100657 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200658
659 if (h2c->flags & H2_CF_DEM_MROOM) {
660 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200661 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200662 }
Willy Tarreau14398122017-09-22 14:26:04 +0200663 return 1;
664 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100665
666 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
667 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200668 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100669 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200670 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100671 return 1;
672 }
673
Willy Tarreau14398122017-09-22 14:26:04 +0200674 return 0;
675}
676
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200677static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200678{
679 struct buffer *buf = NULL;
680
Willy Tarreau21046592020-02-26 10:39:36 +0100681 if (likely(!MT_LIST_ADDED(&h2c->buf_wait.list)) &&
Willy Tarreau44e973f2018-03-01 17:49:30 +0100682 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
683 h2c->buf_wait.target = h2c;
684 h2c->buf_wait.wakeup_cb = h2_buf_available;
Willy Tarreau21046592020-02-26 10:39:36 +0100685 MT_LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200686 }
687 return buf;
688}
689
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200690static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200691{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200692 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100693 b_free(bptr);
Willy Tarreau201840a2019-05-29 17:50:48 +0200694 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200695 }
696}
697
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200698static inline void h2_release_mbuf(struct h2c *h2c)
699{
700 struct buffer *buf;
701 unsigned int count = 0;
702
703 while (b_size(buf = br_head_pick(h2c->mbuf))) {
704 b_free(buf);
705 count++;
706 }
707 if (count)
Willy Tarreau201840a2019-05-29 17:50:48 +0200708 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200709}
710
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100711/* returns the number of allocatable outgoing streams for the connection taking
712 * the last_sid and the reserved ones into account.
713 */
714static inline int h2_streams_left(const struct h2c *h2c)
715{
716 int ret;
717
718 /* consider the number of outgoing streams we're allowed to create before
719 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
720 * nb_reserved is the number of streams which don't yet have an ID.
721 */
722 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
723 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
724 if (ret < 0)
725 ret = 0;
726 return ret;
727}
728
Willy Tarreau00f18a32019-01-26 12:19:01 +0100729/* returns the number of streams in use on a connection to figure if it's
730 * idle or not. We check nb_cs and not nb_streams as the caller will want
731 * to know if it was the last one after a detach().
732 */
733static int h2_used_streams(struct connection *conn)
734{
735 struct h2c *h2c = conn->ctx;
736
737 return h2c->nb_cs;
738}
739
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100740/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100741static int h2_avail_streams(struct connection *conn)
742{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100743 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100744 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100745 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100746
Willy Tarreau6afec462019-01-28 06:40:19 +0100747 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
748 * streams on the connection.
749 */
750 if (h2c->last_sid >= 0)
751 return 0;
752
Willy Tarreauc61966f2019-10-31 15:10:03 +0100753 if (h2c->st0 >= H2_CS_ERROR)
754 return 0;
755
Willy Tarreau86949782019-01-31 10:42:05 +0100756 /* note: may be negative if a SETTINGS frame changes the limit */
757 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100758
759 /* we must also consider the limit imposed by stream IDs */
760 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100761 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100762 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100763 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
764 ret1 = MIN(ret1, ret2);
765 }
766 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100767}
768
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200769
Willy Tarreau62f52692017-10-08 23:01:42 +0200770/*****************************************************************/
771/* functions below are dedicated to the mux setup and management */
772/*****************************************************************/
773
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200774/* Initialize the mux once it's attached. For outgoing connections, the context
775 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200776 * connections from the fact that the context is still NULL (even during mux
777 * upgrades). <input> is always used as Input buffer and may contain data. It is
778 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200779 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200780static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
781 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200782{
783 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100784 struct task *t = NULL;
Christopher Fauletf81ef032019-10-04 15:19:43 +0200785 void *conn_ctx = conn->ctx;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200786
Christopher Fauletf81ef032019-10-04 15:19:43 +0200787 TRACE_ENTER(H2_EV_H2C_NEW);
Willy Tarreau7838a792019-08-12 18:42:03 +0200788
Willy Tarreaubafbe012017-11-24 17:34:44 +0100789 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200790 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200791 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200792
Christopher Faulete9b70722019-04-08 10:46:02 +0200793 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200794 h2c->flags = H2_CF_IS_BACK;
795 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
796 if (tick_isset(prx->timeout.serverfin))
797 h2c->shut_timeout = prx->timeout.serverfin;
798 } else {
799 h2c->flags = H2_CF_NONE;
800 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
801 if (tick_isset(prx->timeout.clientfin))
802 h2c->shut_timeout = prx->timeout.clientfin;
803 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100804
Willy Tarreau0b37d652018-10-03 10:33:02 +0200805 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100806 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100807 if (tick_isset(h2c->timeout)) {
808 t = task_new(tid_bit);
809 if (!t)
810 goto fail;
811
812 h2c->task = t;
813 t->process = h2_timeout_task;
814 t->context = h2c;
815 t->expire = tick_add(now_ms, h2c->timeout);
816 }
Willy Tarreauea392822017-10-31 10:02:25 +0100817
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200818 h2c->wait_event.tasklet = tasklet_new();
819 if (!h2c->wait_event.tasklet)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200820 goto fail;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200821 h2c->wait_event.tasklet->process = h2_io_cb;
822 h2c->wait_event.tasklet->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100823 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200824
Willy Tarreau2bdcc702020-05-19 11:31:11 +0200825 h2c->ddht = hpack_dht_alloc();
Willy Tarreau32218eb2017-09-22 08:07:25 +0200826 if (!h2c->ddht)
827 goto fail;
828
829 /* Initialise the context. */
830 h2c->st0 = H2_CS_PREFACE;
831 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100832 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200833 h2c->max_id = -1;
834 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100835 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200836 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100837 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200838 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100839 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100840 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200841
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200842 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200843 h2c->dsi = -1;
844 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100845
Willy Tarreau32218eb2017-09-22 08:07:25 +0200846 h2c->last_sid = -1;
847
Willy Tarreau51330962019-05-26 09:38:07 +0200848 br_init(h2c->mbuf, sizeof(h2c->mbuf) / sizeof(h2c->mbuf[0]));
Willy Tarreau32218eb2017-09-22 08:07:25 +0200849 h2c->miw = 65535; /* mux initial window size */
850 h2c->mws = 65535; /* mux window size */
851 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200852 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200853 LIST_INIT(&h2c->send_list);
854 LIST_INIT(&h2c->fctl_list);
Willy Tarreau9edf6db2019-10-02 10:49:59 +0200855 LIST_INIT(&h2c->blocked_list);
Willy Tarreau21046592020-02-26 10:39:36 +0100856 MT_LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200857
Christopher Fauletf81ef032019-10-04 15:19:43 +0200858 conn->ctx = h2c;
859
Willy Tarreau3f133572017-10-31 19:21:06 +0100860 if (t)
861 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100862
Willy Tarreau01b44822018-10-03 14:26:37 +0200863 if (h2c->flags & H2_CF_IS_BACK) {
864 /* FIXME: this is temporary, for outgoing connections we need
865 * to immediately allocate a stream until the code is modified
866 * so that the caller calls ->attach(). For now the outgoing cs
Christopher Fauletf81ef032019-10-04 15:19:43 +0200867 * is stored as conn->ctx by the caller and saved in conn_ctx.
Willy Tarreau01b44822018-10-03 14:26:37 +0200868 */
869 struct h2s *h2s;
870
Christopher Fauletf81ef032019-10-04 15:19:43 +0200871 h2s = h2c_bck_stream_new(h2c, conn_ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200872 if (!h2s)
873 goto fail_stream;
874 }
875
Willy Tarreau0f383582018-10-03 14:22:21 +0200876 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200877 h2c_restart_reading(h2c, 1);
Willy Tarreau7838a792019-08-12 18:42:03 +0200878 TRACE_LEAVE(H2_EV_H2C_NEW, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200879 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200880 fail_stream:
881 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200882 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200883 task_destroy(t);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200884 if (h2c->wait_event.tasklet)
885 tasklet_free(h2c->wait_event.tasklet);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100886 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200887 fail_no_h2c:
Christopher Fauletf81ef032019-10-04 15:19:43 +0200888 conn->ctx = conn_ctx; /* restore saved ctx */
889 TRACE_DEVEL("leaving in error", H2_EV_H2C_NEW|H2_EV_H2C_END|H2_EV_H2C_ERR);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200890 return -1;
891}
892
Willy Tarreau751f2d02018-10-05 09:35:00 +0200893/* returns the next allocatable outgoing stream ID for the H2 connection, or
894 * -1 if no more is allocatable.
895 */
896static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
897{
898 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100899
900 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200901 id = -1;
902 return id;
903}
904
Willy Tarreau2373acc2017-10-12 17:35:14 +0200905/* returns the stream associated with id <id> or NULL if not found */
906static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
907{
908 struct eb32_node *node;
909
Willy Tarreau751f2d02018-10-05 09:35:00 +0200910 if (id == 0)
911 return (struct h2s *)h2_closed_stream;
912
Willy Tarreau2a856182017-05-16 15:20:39 +0200913 if (id > h2c->max_id)
914 return (struct h2s *)h2_idle_stream;
915
Willy Tarreau2373acc2017-10-12 17:35:14 +0200916 node = eb32_lookup(&h2c->streams_by_id, id);
917 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200918 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200919
920 return container_of(node, struct h2s, by_id);
921}
922
Christopher Faulet73c12072019-04-08 11:23:22 +0200923/* release function. This one should be called to free all resources allocated
924 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200925 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200926static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200927{
Christopher Faulet61840e72019-04-15 09:33:32 +0200928 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200929
Willy Tarreau7838a792019-08-12 18:42:03 +0200930 TRACE_ENTER(H2_EV_H2C_END);
931
Willy Tarreau32218eb2017-09-22 08:07:25 +0200932 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200933 /* The connection must be aattached to this mux to be released */
934 if (h2c->conn && h2c->conn->ctx == h2c)
935 conn = h2c->conn;
936
Willy Tarreau7838a792019-08-12 18:42:03 +0200937 TRACE_DEVEL("freeing h2c", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200938 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200939
Willy Tarreau21046592020-02-26 10:39:36 +0100940 if (MT_LIST_ADDED(&h2c->buf_wait.list))
941 MT_LIST_DEL(&h2c->buf_wait.list);
Willy Tarreau14398122017-09-22 14:26:04 +0200942
Willy Tarreau44e973f2018-03-01 17:49:30 +0100943 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau2e3c0002019-05-26 09:45:23 +0200944 h2_release_mbuf(h2c);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100945
Willy Tarreauea392822017-10-31 10:02:25 +0100946 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200947 h2c->task->context = NULL;
948 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100949 h2c->task = NULL;
950 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +0200951 if (h2c->wait_event.tasklet)
952 tasklet_free(h2c->wait_event.tasklet);
Christopher Faulet21d849f2019-09-18 11:07:20 +0200953 if (conn && h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100954 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Christopher Faulet21d849f2019-09-18 11:07:20 +0200955 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100956
Willy Tarreaubafbe012017-11-24 17:34:44 +0100957 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200958 }
959
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200960 if (conn) {
961 conn->mux = NULL;
962 conn->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +0200963 TRACE_DEVEL("freeing conn", H2_EV_H2C_END, conn);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200964
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200965 conn_stop_tracking(conn);
966 conn_full_close(conn);
967 if (conn->destroy_cb)
968 conn->destroy_cb(conn);
969 conn_free(conn);
970 }
Willy Tarreau7838a792019-08-12 18:42:03 +0200971
972 TRACE_LEAVE(H2_EV_H2C_END);
Willy Tarreau62f52692017-10-08 23:01:42 +0200973}
974
975
Willy Tarreau71681172017-10-23 14:39:06 +0200976/******************************************************/
977/* functions below are for the H2 protocol processing */
978/******************************************************/
979
980/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100981static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200982{
983 return h2s ? h2s->id : 0;
984}
985
Willy Tarreau1d4a0f82019-08-02 07:52:08 +0200986/* returns the sum of the stream's own window size and the mux's initial
987 * window, which together form the stream's effective window size.
988 */
989static inline int h2s_mws(const struct h2s *h2s)
990{
991 return h2s->sws + h2s->h2c->miw;
992}
993
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200994/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100995static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200996{
997 if (h2c->msi < 0)
998 return 0;
999
1000 if (h2c->msi == h2s_id(h2s))
1001 return 0;
1002
1003 return 1;
1004}
1005
Willy Tarreau741d6df2017-10-17 08:00:59 +02001006/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +01001007static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +02001008{
Willy Tarreau7838a792019-08-12 18:42:03 +02001009 TRACE_POINT(H2_EV_H2C_ERR, h2c->conn,,, (void *)(long)(err));
Willy Tarreau741d6df2017-10-17 08:00:59 +02001010 h2c->errcode = err;
1011 h2c->st0 = H2_CS_ERROR;
1012}
1013
Willy Tarreau175cebb2019-01-24 10:02:24 +01001014/* marks an error on the stream. It may also update an already closed stream
1015 * (e.g. to report an error after an RST was received).
1016 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001017static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +02001018{
Willy Tarreau175cebb2019-01-24 10:02:24 +01001019 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001020 TRACE_POINT(H2_EV_H2S_ERR, h2s->h2c->conn, h2s,, (void *)(long)(err));
Willy Tarreau2e43f082017-10-17 08:03:59 +02001021 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +01001022 if (h2s->st < H2_SS_ERROR)
1023 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +01001024 if (h2s->cs)
1025 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +02001026 }
1027}
1028
Willy Tarreau7e094452018-12-19 18:08:52 +01001029/* attempt to notify the data layer of recv availability */
1030static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
1031{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001032 if (h2s->subs && h2s->subs->events & SUB_RETRY_RECV) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001033 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01001034 tasklet_wakeup(h2s->subs->tasklet);
1035 h2s->subs->events &= ~SUB_RETRY_RECV;
1036 if (!h2s->subs->events)
1037 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001038 }
1039}
1040
1041/* attempt to notify the data layer of send availability */
1042static void __maybe_unused h2s_notify_send(struct h2s *h2s)
1043{
Willy Tarreauf96508a2020-01-10 11:12:48 +01001044 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001045 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01001046 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01001047 tasklet_wakeup(h2s->subs->tasklet);
1048 h2s->subs->events &= ~SUB_RETRY_SEND;
1049 if (!h2s->subs->events)
1050 h2s->subs = NULL;
Willy Tarreau7e094452018-12-19 18:08:52 +01001051 }
Willy Tarreau5723f292020-01-10 15:16:57 +01001052 else if (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) {
1053 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
1054 tasklet_wakeup(h2s->shut_tl);
1055 }
Willy Tarreau7e094452018-12-19 18:08:52 +01001056}
1057
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001058/* alerts the data layer, trying to wake it up by all means, following
1059 * this sequence :
1060 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
1061 * - if its subscribed to send, then it's woken up for send
1062 * - if it was subscribed to neither, its ->wake() callback is called
1063 * It is safe to call this function with a closed stream which doesn't have a
1064 * conn_stream anymore.
1065 */
1066static void __maybe_unused h2s_alert(struct h2s *h2s)
1067{
Willy Tarreau7838a792019-08-12 18:42:03 +02001068 TRACE_ENTER(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
1069
Willy Tarreauf96508a2020-01-10 11:12:48 +01001070 if (h2s->subs ||
Willy Tarreau5723f292020-01-10 15:16:57 +01001071 (h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW))) {
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001072 h2s_notify_recv(h2s);
1073 h2s_notify_send(h2s);
1074 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001075 else if (h2s->cs && h2s->cs->data_cb->wake != NULL) {
1076 TRACE_POINT(H2_EV_STRM_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001077 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau7838a792019-08-12 18:42:03 +02001078 }
1079
1080 TRACE_LEAVE(H2_EV_H2S_WAKE, h2s->h2c->conn, h2s);
Willy Tarreau8b2757c2018-12-19 17:36:48 +01001081}
1082
Willy Tarreaue4820742017-07-27 13:37:23 +02001083/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +01001084static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +02001085{
1086 uint8_t *out = frame;
1087
1088 *out = len >> 16;
1089 write_n16(out + 1, len);
1090}
1091
Willy Tarreau54c15062017-10-10 17:10:03 +02001092/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
1093 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
1094 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001095 * available in the buffer's input prior to calling this function. The buffer
1096 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +02001097 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001098static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +02001099 const struct buffer *b, int o)
1100{
Willy Tarreau591d4452018-06-15 17:21:00 +02001101 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 +02001102}
1103
Willy Tarreau1f094672017-11-20 21:27:45 +01001104static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001105{
Willy Tarreau591d4452018-06-15 17:21:00 +02001106 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001107}
1108
Willy Tarreau1f094672017-11-20 21:27:45 +01001109static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001110{
Willy Tarreau591d4452018-06-15 17:21:00 +02001111 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001112}
1113
Willy Tarreau1f094672017-11-20 21:27:45 +01001114static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +02001115{
Willy Tarreau591d4452018-06-15 17:21:00 +02001116 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +02001117}
1118
1119
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001120/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
1121 * The algorithm is not obvious. It turns out that H2 headers are neither
1122 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
1123 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +02001124 *
1125 * b0 b1 b2 b3 b4 b5..b8
1126 * +----------+---------+--------+----+----+----------------------+
1127 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
1128 * +----------+---------+--------+----+----+----------------------+
1129 *
1130 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
1131 * we get the sid properly aligned and ordered, and 16 bits of len properly
1132 * ordered as well. The type and flags can be extracted using bit shifts from
1133 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +02001134 * Returns zero if some bytes are missing, otherwise non-zero on success. The
1135 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +02001136 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001137static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001138{
1139 uint64_t w;
1140
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001141 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +02001142 return 0;
1143
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001144 w = h2_get_n64(b, o + 1);
1145 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +02001146 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
1147 h->ff = w >> 32;
1148 h->ft = w >> 40;
1149 h->len += w >> 48;
1150 return 1;
1151}
1152
1153/* skip the next 9 bytes corresponding to the frame header possibly parsed by
1154 * h2_peek_frame_hdr() above.
1155 */
Willy Tarreau1f094672017-11-20 21:27:45 +01001156static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +02001157{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +02001158 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +02001159}
1160
1161/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +01001162static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +02001163{
1164 int ret;
1165
Willy Tarreaua4428bd2018-12-22 18:11:41 +01001166 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +02001167 if (ret > 0)
1168 h2_skip_frame_hdr(b);
1169 return ret;
1170}
1171
Willy Tarreaucb985a42019-10-07 16:56:34 +02001172
1173/* try to fragment the headers frame present at the beginning of buffer <b>,
1174 * enforcing a limit of <mfs> bytes per frame. Returns 0 on failure, 1 on
1175 * success. Typical causes of failure include a buffer not large enough to
1176 * add extra frame headers. The existing frame size is read in the current
1177 * frame. Its EH flag will be cleared if CONTINUATION frames need to be added,
1178 * and its length will be adjusted. The stream ID for continuation frames will
1179 * be copied from the initial frame's.
1180 */
1181static int h2_fragment_headers(struct buffer *b, uint32_t mfs)
1182{
1183 size_t remain = b->data - 9;
1184 int extra_frames = (remain - 1) / mfs;
1185 size_t fsize;
1186 char *fptr;
1187 int frame;
1188
1189 if (b->data <= mfs + 9)
1190 return 1;
1191
1192 /* Too large a frame, we need to fragment it using CONTINUATION
1193 * frames. We start from the end and move tails as needed.
1194 */
1195 if (b->data + extra_frames * 9 > b->size)
1196 return 0;
1197
1198 for (frame = extra_frames; frame; frame--) {
1199 fsize = ((remain - 1) % mfs) + 1;
1200 remain -= fsize;
1201
1202 /* move data */
1203 fptr = b->area + 9 + remain + (frame - 1) * 9;
1204 memmove(fptr + 9, b->area + 9 + remain, fsize);
1205 b->data += 9;
1206
1207 /* write new frame header */
1208 h2_set_frame_size(fptr, fsize);
1209 fptr[3] = H2_FT_CONTINUATION;
1210 fptr[4] = (frame == extra_frames) ? H2_F_HEADERS_END_HEADERS : 0;
1211 write_n32(fptr + 5, read_n32(b->area + 5));
1212 }
1213
1214 b->area[4] &= ~H2_F_HEADERS_END_HEADERS;
1215 h2_set_frame_size(b->area, remain);
1216 return 1;
1217}
1218
1219
Willy Tarreau00dd0782018-03-01 16:31:34 +01001220/* marks stream <h2s> as CLOSED and decrement the number of active streams for
1221 * its connection if the stream was not yet closed. Please use this exclusively
1222 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001223 */
Willy Tarreau00dd0782018-03-01 16:31:34 +01001224static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001225{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001226 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau7838a792019-08-12 18:42:03 +02001227 TRACE_ENTER(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001228 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001229 if (!h2s->id)
1230 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +01001231 if (h2s->cs) {
Willy Tarreaua27db382019-03-25 18:13:16 +01001232 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
1233 h2s_notify_recv(h2s);
1234 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001235 TRACE_LEAVE(H2_EV_H2S_END, h2s->h2c->conn, h2s);
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001236 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +01001237 h2s->st = H2_SS_CLOSED;
1238}
1239
Willy Tarreau71049cc2018-03-28 13:56:39 +02001240/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001241/* h2s_destroy should only ever be called by the thread that owns the stream,
1242 * that means that a tasklet should be used if we want to destroy the h2s
1243 * from another thread
1244 */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001245static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +01001246{
Willy Tarreau7838a792019-08-12 18:42:03 +02001247 struct connection *conn = h2s->h2c->conn;
1248
1249 TRACE_ENTER(H2_EV_H2S_END, conn, h2s);
1250
Willy Tarreau0a10de62018-03-01 16:27:53 +01001251 h2s_close(h2s);
1252 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +02001253 if (b_size(&h2s->rxbuf)) {
1254 b_free(&h2s->rxbuf);
1255 offer_buffers(NULL, tasks_run_queue);
1256 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001257
1258 if (h2s->subs)
1259 h2s->subs->events = 0;
1260
Joseph Herlantd77575d2018-11-25 10:54:45 -08001261 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001262 * reference left would be in the h2c send_list/fctl_list, and if
1263 * we're in it, we're getting out anyway
1264 */
Olivier Houchardd360ac62019-03-22 17:37:16 +01001265 LIST_DEL_INIT(&h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01001266
Olivier Houchard5a3671d2019-10-11 16:33:49 +02001267 /* ditto, calling tasklet_free() here should be ok */
Willy Tarreau5723f292020-01-10 15:16:57 +01001268 tasklet_free(h2s->shut_tl);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001269 pool_free(pool_head_h2s, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001270
1271 TRACE_LEAVE(H2_EV_H2S_END, conn);
Willy Tarreau0a10de62018-03-01 16:27:53 +01001272}
1273
Willy Tarreaua8e49542018-10-03 18:53:55 +02001274/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
1275 * stream tree. In case of error, nothing is added and NULL is returned. The
1276 * causes of errors can be any failed memory allocation. The caller is
1277 * responsible for checking if the connection may support an extra stream
1278 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001279 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001280static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001281{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001282 struct h2s *h2s;
1283
Willy Tarreau7838a792019-08-12 18:42:03 +02001284 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1285
Willy Tarreaubafbe012017-11-24 17:34:44 +01001286 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001287 if (!h2s)
1288 goto out;
1289
Willy Tarreau5723f292020-01-10 15:16:57 +01001290 h2s->shut_tl = tasklet_new();
1291 if (!h2s->shut_tl) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001292 pool_free(pool_head_h2s, h2s);
1293 goto out;
1294 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01001295 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01001296 h2s->shut_tl->process = h2_deferred_shut;
1297 h2s->shut_tl->context = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001298 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001299 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001300 h2s->cs = NULL;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001301 h2s->sws = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001302 h2s->flags = H2_SF_NONE;
1303 h2s->errcode = H2_ERR_NO_ERROR;
1304 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02001305 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001306 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +02001307 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001308
1309 if (h2c->flags & H2_CF_IS_BACK) {
1310 h1m_init_req(&h2s->h1m);
1311 h2s->h1m.err_pos = -1; // don't care about errors on the request path
1312 h2s->h1m.flags |= H1_MF_TOLOWER;
1313 } else {
1314 h1m_init_res(&h2s->h1m);
1315 h2s->h1m.err_pos = -1; // don't care about errors on the response path
1316 h2s->h1m.flags |= H1_MF_TOLOWER;
1317 }
1318
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001319 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001320 if (id > 0)
1321 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01001322 else
1323 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001324
1325 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +01001326 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +01001327 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001328
Willy Tarreau7838a792019-08-12 18:42:03 +02001329 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001330 return h2s;
Willy Tarreaua8e49542018-10-03 18:53:55 +02001331 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001332 TRACE_DEVEL("leaving in error", H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreaua8e49542018-10-03 18:53:55 +02001333 return NULL;
1334}
1335
1336/* creates a new stream <id> on the h2c connection and returns it, or NULL in
1337 * case of memory allocation error.
1338 */
1339static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
1340{
1341 struct session *sess = h2c->conn->owner;
1342 struct conn_stream *cs;
1343 struct h2s *h2s;
1344
Willy Tarreau7838a792019-08-12 18:42:03 +02001345 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1346
Willy Tarreaua8e49542018-10-03 18:53:55 +02001347 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
1348 goto out;
1349
1350 h2s = h2s_new(h2c, id);
1351 if (!h2s)
1352 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001353
1354 cs = cs_new(h2c->conn);
1355 if (!cs)
1356 goto out_close;
1357
Olivier Houchard746fb772018-12-15 19:42:00 +01001358 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001359 h2s->cs = cs;
1360 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001361 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001362
1363 if (stream_create_from_cs(cs) < 0)
1364 goto out_free_cs;
1365
Willy Tarreau590a0512018-09-05 11:56:48 +02001366 /* We want the accept date presented to the next stream to be the one
1367 * we have now, the handshake time to be null (since the next stream
1368 * is not delayed by a handshake), and the idle time to count since
1369 * right now.
1370 */
1371 sess->accept_date = date;
1372 sess->tv_accept = now;
1373 sess->t_handshake = 0;
1374
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001375 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +01001376 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +02001377 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001378 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001379 return h2s;
1380
1381 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +02001382 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001383 cs_free(cs);
Olivier Houchard58d87f32019-05-29 16:44:17 +02001384 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001385 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +02001386 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001387 out:
Willy Tarreau45efc072018-10-03 18:27:52 +02001388 sess_log(sess);
Willy Tarreau7838a792019-08-12 18:42:03 +02001389 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn);
Willy Tarreau45efc072018-10-03 18:27:52 +02001390 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001391}
1392
Willy Tarreau751f2d02018-10-05 09:35:00 +02001393/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1394 * and returns it, or NULL in case of memory allocation error or if the highest
1395 * possible stream ID was reached.
1396 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001397static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001398{
1399 struct h2s *h2s = NULL;
1400
Willy Tarreau7838a792019-08-12 18:42:03 +02001401 TRACE_ENTER(H2_EV_H2S_NEW, h2c->conn);
1402
Willy Tarreau86949782019-01-31 10:42:05 +01001403 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001404 goto out;
1405
Willy Tarreaua80dca82019-01-24 17:08:28 +01001406 if (h2_streams_left(h2c) < 1)
1407 goto out;
1408
Willy Tarreau751f2d02018-10-05 09:35:00 +02001409 /* Defer choosing the ID until we send the first message to create the stream */
1410 h2s = h2s_new(h2c, 0);
1411 if (!h2s)
1412 goto out;
1413
1414 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001415 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001416 cs->ctx = h2s;
1417 h2c->nb_cs++;
1418
Willy Tarreau751f2d02018-10-05 09:35:00 +02001419 out:
Willy Tarreau7838a792019-08-12 18:42:03 +02001420 if (likely(h2s))
1421 TRACE_LEAVE(H2_EV_H2S_NEW, h2c->conn, h2s);
1422 else
1423 TRACE_LEAVE(H2_EV_H2S_NEW|H2_EV_H2S_ERR|H2_EV_H2S_END, h2c->conn, h2s);
Willy Tarreau751f2d02018-10-05 09:35:00 +02001424 return h2s;
1425}
1426
Willy Tarreaube5b7152017-09-25 16:25:39 +02001427/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1428 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1429 * the various settings codes.
1430 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001431static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001432{
1433 struct buffer *res;
1434 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001435 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001436 int mfs;
Willy Tarreau7838a792019-08-12 18:42:03 +02001437 int ret = 0;
1438
1439 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001440
1441 if (h2c_mux_busy(h2c, NULL)) {
1442 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001443 goto out;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001444 }
1445
Willy Tarreaube5b7152017-09-25 16:25:39 +02001446 chunk_init(&buf, buf_data, sizeof(buf_data));
1447 chunk_memcpy(&buf,
1448 "\x00\x00\x00" /* length : 0 for now */
1449 "\x04\x00" /* type : 4 (settings), flags : 0 */
1450 "\x00\x00\x00\x00", /* stream ID : 0 */
1451 9);
1452
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001453 if (h2c->flags & H2_CF_IS_BACK) {
1454 /* send settings_enable_push=0 */
1455 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1456 }
1457
Willy Tarreaube5b7152017-09-25 16:25:39 +02001458 if (h2_settings_header_table_size != 4096) {
1459 char str[6] = "\x00\x01"; /* header_table_size */
1460
1461 write_n32(str + 2, h2_settings_header_table_size);
1462 chunk_memcat(&buf, str, 6);
1463 }
1464
1465 if (h2_settings_initial_window_size != 65535) {
1466 char str[6] = "\x00\x04"; /* initial_window_size */
1467
1468 write_n32(str + 2, h2_settings_initial_window_size);
1469 chunk_memcat(&buf, str, 6);
1470 }
1471
1472 if (h2_settings_max_concurrent_streams != 0) {
1473 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1474
1475 /* Note: 0 means "unlimited" for haproxy's config but not for
1476 * the protocol, so never send this value!
1477 */
1478 write_n32(str + 2, h2_settings_max_concurrent_streams);
1479 chunk_memcat(&buf, str, 6);
1480 }
1481
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001482 mfs = h2_settings_max_frame_size;
1483 if (mfs > global.tune.bufsize)
1484 mfs = global.tune.bufsize;
1485
1486 if (!mfs)
1487 mfs = global.tune.bufsize;
1488
1489 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001490 char str[6] = "\x00\x05"; /* max_frame_size */
1491
1492 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1493 * match bufsize - rewrite size, but at the moment it seems
1494 * that clients don't take care of it.
1495 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001496 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001497 chunk_memcat(&buf, str, 6);
1498 }
1499
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001500 h2_set_frame_size(buf.area, buf.data - 9);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001501
1502 res = br_tail(h2c->mbuf);
1503 retry:
1504 if (!h2_get_buf(h2c, res)) {
1505 h2c->flags |= H2_CF_MUX_MALLOC;
1506 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001507 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001508 }
1509
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001510 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001511 if (unlikely(ret <= 0)) {
1512 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001513 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1514 goto retry;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001515 h2c->flags |= H2_CF_MUX_MFULL;
1516 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001517 }
1518 else {
1519 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001520 ret = 0;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001521 }
1522 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001523 out:
1524 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001525 return ret;
1526}
1527
Willy Tarreau52eed752017-09-22 15:05:09 +02001528/* Try to receive a connection preface, then upon success try to send our
1529 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1530 * missing data. It may return an error in h2c.
1531 */
1532static int h2c_frt_recv_preface(struct h2c *h2c)
1533{
1534 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001535 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001536
Willy Tarreau7838a792019-08-12 18:42:03 +02001537 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
1538
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001539 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001540
1541 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001542 if (ret1 < 0)
1543 sess_log(h2c->conn->owner);
1544
Willy Tarreau52eed752017-09-22 15:05:09 +02001545 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1546 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001547 ret2 = 0;
1548 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02001549 }
1550
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001551 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001552 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001553 b_del(&h2c->dbuf, ret1);
Willy Tarreau7838a792019-08-12 18:42:03 +02001554 out:
1555 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001556 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001557}
1558
Willy Tarreau01b44822018-10-03 14:26:37 +02001559/* Try to send a connection preface, then upon success try to send our
1560 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1561 * missing data. It may return an error in h2c.
1562 */
1563static int h2c_bck_send_preface(struct h2c *h2c)
1564{
1565 struct buffer *res;
Willy Tarreau7838a792019-08-12 18:42:03 +02001566 int ret = 0;
1567
1568 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02001569
1570 if (h2c_mux_busy(h2c, NULL)) {
1571 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001572 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001573 }
1574
Willy Tarreaubcc45952019-05-26 10:05:50 +02001575 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001576 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001577 if (!h2_get_buf(h2c, res)) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001578 h2c->flags |= H2_CF_MUX_MALLOC;
1579 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001580 goto out;
Willy Tarreau01b44822018-10-03 14:26:37 +02001581 }
1582
1583 if (!b_data(res)) {
1584 /* preface not yet sent */
Willy Tarreau9c218e72019-05-26 10:08:28 +02001585 ret = b_istput(res, ist(H2_CONN_PREFACE));
1586 if (unlikely(ret <= 0)) {
1587 if (!ret) {
1588 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1589 goto retry;
1590 h2c->flags |= H2_CF_MUX_MFULL;
1591 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001592 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001593 }
1594 else {
1595 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001596 ret = 0;
1597 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02001598 }
1599 }
Willy Tarreau01b44822018-10-03 14:26:37 +02001600 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001601 ret = h2c_send_settings(h2c);
1602 out:
1603 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PREFACE, h2c->conn);
1604 return ret;
Willy Tarreau01b44822018-10-03 14:26:37 +02001605}
1606
Willy Tarreau081d4722017-05-16 21:51:05 +02001607/* try to send a GOAWAY frame on the connection to report an error or a graceful
1608 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1609 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1610 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1611 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1612 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1613 * on unrecoverable failure. It will not attempt to send one again in this last
1614 * case so that it is safe to use h2c_error() to report such errors.
1615 */
1616static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1617{
1618 struct buffer *res;
1619 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02001620 int ret = 0;
Willy Tarreau081d4722017-05-16 21:51:05 +02001621
Willy Tarreau7838a792019-08-12 18:42:03 +02001622 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
1623
1624 if (h2c->flags & H2_CF_GOAWAY_FAILED) {
1625 ret = 1; // claim that it worked
1626 goto out;
1627 }
Willy Tarreau081d4722017-05-16 21:51:05 +02001628
1629 if (h2c_mux_busy(h2c, h2s)) {
1630 if (h2s)
1631 h2s->flags |= H2_SF_BLK_MBUSY;
1632 else
1633 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001634 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001635 }
1636
Willy Tarreau9c218e72019-05-26 10:08:28 +02001637 /* len: 8, type: 7, flags: none, sid: 0 */
1638 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1639
1640 if (h2c->last_sid < 0)
1641 h2c->last_sid = h2c->max_id;
1642
1643 write_n32(str + 9, h2c->last_sid);
1644 write_n32(str + 13, h2c->errcode);
1645
Willy Tarreaubcc45952019-05-26 10:05:50 +02001646 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001647 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001648 if (!h2_get_buf(h2c, res)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02001649 h2c->flags |= H2_CF_MUX_MALLOC;
1650 if (h2s)
1651 h2s->flags |= H2_SF_BLK_MROOM;
1652 else
1653 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001654 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001655 }
1656
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001657 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001658 if (unlikely(ret <= 0)) {
1659 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001660 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1661 goto retry;
Willy Tarreau081d4722017-05-16 21:51:05 +02001662 h2c->flags |= H2_CF_MUX_MFULL;
1663 if (h2s)
1664 h2s->flags |= H2_SF_BLK_MROOM;
1665 else
1666 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001667 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001668 }
1669 else {
1670 /* we cannot report this error using GOAWAY, so we mark
1671 * it and claim a success.
1672 */
1673 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1674 h2c->flags |= H2_CF_GOAWAY_FAILED;
Willy Tarreau7838a792019-08-12 18:42:03 +02001675 ret = 1;
1676 goto out;
Willy Tarreau081d4722017-05-16 21:51:05 +02001677 }
1678 }
1679 h2c->flags |= H2_CF_GOAWAY_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02001680 out:
1681 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_GOAWAY, h2c->conn);
Willy Tarreau081d4722017-05-16 21:51:05 +02001682 return ret;
1683}
1684
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001685/* Try to send an RST_STREAM frame on the connection for the indicated stream
1686 * during mux operations. This stream must be valid and cannot be closed
1687 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1688 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1689 * not yet.
1690 *
1691 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1692 * to write the message, it subscribes the stream to future notifications.
1693 */
1694static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1695{
1696 struct buffer *res;
1697 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001698 int ret = 0;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001699
Willy Tarreau7838a792019-08-12 18:42:03 +02001700 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
1701
1702 if (!h2s || h2s->st == H2_SS_CLOSED) {
1703 ret = 1;
1704 goto out;
1705 }
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001706
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001707 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1708 * RST_STREAM in response to a RST_STREAM frame.
1709 */
Willy Tarreau231f6162019-08-06 10:01:40 +02001710 if (h2c->dsi == h2s->id && h2c->dft == H2_FT_RST_STREAM) {
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001711 ret = 1;
1712 goto ignore;
1713 }
1714
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001715 if (h2c_mux_busy(h2c, h2s)) {
1716 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001717 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001718 }
1719
Willy Tarreau9c218e72019-05-26 10:08:28 +02001720 /* len: 4, type: 3, flags: none */
1721 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1722 write_n32(str + 5, h2s->id);
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 Tarreau8c0ea7d2017-11-10 10:05:24 +01001728 h2c->flags |= H2_CF_MUX_MALLOC;
1729 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001730 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001731 }
1732
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001733 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001734 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 Tarreau8c0ea7d2017-11-10 10:05:24 +01001738 h2c->flags |= H2_CF_MUX_MFULL;
1739 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001740 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001741 }
1742 else {
1743 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001744 ret = 0;
1745 goto out;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001746 }
1747 }
1748
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001749 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001750 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001751 h2s_close(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001752 out:
1753 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001754 return ret;
1755}
1756
1757/* Try to send an RST_STREAM frame on the connection for the stream being
1758 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001759 * error code, even if the stream is one of the dummy ones, and will update
1760 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001761 *
1762 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1763 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001764 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001765 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001766 */
1767static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1768{
1769 struct buffer *res;
1770 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02001771 int ret = 0;
1772
1773 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001774
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001775 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1776 * RST_STREAM in response to a RST_STREAM frame.
1777 */
1778 if (h2c->dft == H2_FT_RST_STREAM) {
1779 ret = 1;
1780 goto ignore;
1781 }
1782
Willy Tarreau27a84c92017-10-17 08:10:17 +02001783 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001784 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001785 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001786 }
1787
Willy Tarreau9c218e72019-05-26 10:08:28 +02001788 /* len: 4, type: 3, flags: none */
1789 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1790
1791 write_n32(str + 5, h2c->dsi);
1792 write_n32(str + 9, h2s->errcode);
1793
Willy Tarreaubcc45952019-05-26 10:05:50 +02001794 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001795 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001796 if (!h2_get_buf(h2c, res)) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001797 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001798 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001799 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001800 }
1801
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001802 ret = b_istput(res, ist2(str, 13));
Willy Tarreau27a84c92017-10-17 08:10:17 +02001803 if (unlikely(ret <= 0)) {
1804 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001805 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1806 goto retry;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001807 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001808 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001809 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001810 }
1811 else {
1812 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001813 ret = 0;
1814 goto out;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001815 }
1816 }
1817
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001818 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001819 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001820 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001821 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001822 }
1823
Willy Tarreau7838a792019-08-12 18:42:03 +02001824 out:
1825 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_RST, h2c->conn, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001826 return ret;
1827}
1828
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001829/* try to send an empty DATA frame with the ES flag set to notify about the
1830 * end of stream and match a shutdown(write). If an ES was already sent as
1831 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1832 * on success or zero if nothing was done. In case of lack of room to write the
1833 * message, it subscribes the requesting stream to future notifications.
1834 */
1835static int h2_send_empty_data_es(struct h2s *h2s)
1836{
1837 struct h2c *h2c = h2s->h2c;
1838 struct buffer *res;
1839 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02001840 int ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001841
Willy Tarreau7838a792019-08-12 18:42:03 +02001842 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
1843
1844 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) {
1845 ret = 1;
1846 goto out;
1847 }
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001848
1849 if (h2c_mux_busy(h2c, h2s)) {
1850 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02001851 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001852 }
1853
Willy Tarreau9c218e72019-05-26 10:08:28 +02001854 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1855 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1856 write_n32(str + 5, h2s->id);
1857
Willy Tarreaubcc45952019-05-26 10:05:50 +02001858 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02001859 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02001860 if (!h2_get_buf(h2c, res)) {
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001861 h2c->flags |= H2_CF_MUX_MALLOC;
1862 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02001863 goto out;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001864 }
1865
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001866 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001867 if (likely(ret > 0)) {
1868 h2s->flags |= H2_SF_ES_SENT;
1869 }
1870 else if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02001871 if ((res = br_tail_add(h2c->mbuf)) != NULL)
1872 goto retry;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001873 h2c->flags |= H2_CF_MUX_MFULL;
1874 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001875 }
1876 else {
1877 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02001878 ret = 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001879 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001880 out:
1881 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA|H2_EV_TX_EOI, h2c->conn, h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001882 return ret;
1883}
1884
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001885/* wake a specific stream and assign its conn_stream som CS_FL_* flags among
Willy Tarreau99ad1b32019-05-14 11:46:28 +02001886 * CS_FL_ERR_PENDING and CS_FL_ERROR if needed. The stream's state
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001887 * is automatically updated accordingly. If the stream is orphaned, it is
1888 * destroyed.
Christopher Fauletf02ca002019-03-07 16:21:34 +01001889 */
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001890static void h2s_wake_one_stream(struct h2s *h2s)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001891{
Willy Tarreau7838a792019-08-12 18:42:03 +02001892 struct h2c *h2c = h2s->h2c;
1893
1894 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn, h2s);
1895
Christopher Fauletf02ca002019-03-07 16:21:34 +01001896 if (!h2s->cs) {
1897 /* this stream was already orphaned */
1898 h2s_destroy(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001899 TRACE_DEVEL("leaving with no h2s", H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001900 return;
1901 }
1902
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001903 if (conn_xprt_read0_pending(h2s->h2c->conn)) {
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001904 if (h2s->st == H2_SS_OPEN)
1905 h2s->st = H2_SS_HREM;
1906 else if (h2s->st == H2_SS_HLOC)
1907 h2s_close(h2s);
1908 }
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001909
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001910 if ((h2s->h2c->st0 >= H2_CS_ERROR || h2s->h2c->conn->flags & CO_FL_ERROR) ||
1911 (h2s->h2c->last_sid > 0 && (!h2s->id || h2s->id > h2s->h2c->last_sid))) {
1912 h2s->cs->flags |= CS_FL_ERR_PENDING;
1913 if (h2s->cs->flags & CS_FL_EOS)
1914 h2s->cs->flags |= CS_FL_ERROR;
Willy Tarreau23482912019-05-07 15:23:14 +02001915
Willy Tarreauaebbe5e2019-05-07 17:48:59 +02001916 if (h2s->st < H2_SS_ERROR)
1917 h2s->st = H2_SS_ERROR;
1918 }
Christopher Fauletf02ca002019-03-07 16:21:34 +01001919
1920 h2s_alert(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02001921 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001922}
1923
1924/* wake the streams attached to the connection, whose id is greater than <last>
1925 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001926 */
Willy Tarreau23482912019-05-07 15:23:14 +02001927static void h2_wake_some_streams(struct h2c *h2c, int last)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001928{
1929 struct eb32_node *node;
1930 struct h2s *h2s;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001931
Willy Tarreau7838a792019-08-12 18:42:03 +02001932 TRACE_ENTER(H2_EV_H2S_WAKE, h2c->conn);
1933
Christopher Fauletf02ca002019-03-07 16:21:34 +01001934 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001935 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1936 while (node) {
1937 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001938 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001939 h2s_wake_one_stream(h2s);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001940 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001941
Christopher Fauletf02ca002019-03-07 16:21:34 +01001942 /* Wake all streams with unassigned ID (ID == 0) */
1943 node = eb32_lookup(&h2c->streams_by_id, 0);
1944 while (node) {
1945 h2s = container_of(node, struct h2s, by_id);
1946 if (h2s->id > 0)
1947 break;
1948 node = eb32_next(node);
Willy Tarreau13b6c2e2019-05-07 17:26:05 +02001949 h2s_wake_one_stream(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001950 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001951
1952 TRACE_LEAVE(H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001953}
1954
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001955/* Wake up all blocked streams whose window size has become positive after the
1956 * mux's initial window was adjusted. This should be done after having processed
1957 * SETTINGS frames which have updated the mux's initial window size.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001958 */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001959static void h2c_unblock_sfctl(struct h2c *h2c)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001960{
1961 struct h2s *h2s;
1962 struct eb32_node *node;
1963
Willy Tarreau7838a792019-08-12 18:42:03 +02001964 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
1965
Willy Tarreau3421aba2017-07-27 15:41:03 +02001966 node = eb32_first(&h2c->streams_by_id);
1967 while (node) {
1968 h2s = container_of(node, struct h2s, by_id);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02001969 if (h2s->flags & H2_SF_BLK_SFCTL && h2s_mws(h2s) > 0) {
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001970 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02001971 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01001972 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
1973 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001974 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001975 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02001976 node = eb32_next(node);
1977 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001978
1979 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001980}
1981
1982/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1983 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001984 * return an error in h2c. The caller must have already verified frame length
1985 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001986 */
1987static int h2c_handle_settings(struct h2c *h2c)
1988{
1989 unsigned int offset;
1990 int error;
1991
Willy Tarreau7838a792019-08-12 18:42:03 +02001992 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
1993
Willy Tarreau3421aba2017-07-27 15:41:03 +02001994 if (h2c->dff & H2_F_SETTINGS_ACK) {
1995 if (h2c->dfl) {
1996 error = H2_ERR_FRAME_SIZE_ERROR;
1997 goto fail;
1998 }
Willy Tarreau7838a792019-08-12 18:42:03 +02001999 goto done;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002000 }
2001
Willy Tarreau3421aba2017-07-27 15:41:03 +02002002 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002003 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002004 goto out0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002005
2006 /* parse the frame */
2007 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002008 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
2009 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002010
2011 switch (type) {
2012 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
2013 /* we need to update all existing streams with the
2014 * difference from the previous iws.
2015 */
2016 if (arg < 0) { // RFC7540#6.5.2
2017 error = H2_ERR_FLOW_CONTROL_ERROR;
2018 goto fail;
2019 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02002020 h2c->miw = arg;
2021 break;
2022 case H2_SETTINGS_MAX_FRAME_SIZE:
2023 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
2024 error = H2_ERR_PROTOCOL_ERROR;
2025 goto fail;
2026 }
2027 h2c->mfs = arg;
2028 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01002029 case H2_SETTINGS_ENABLE_PUSH:
2030 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
2031 error = H2_ERR_PROTOCOL_ERROR;
2032 goto fail;
2033 }
2034 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01002035 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
2036 if (h2c->flags & H2_CF_IS_BACK) {
2037 /* the limit is only for the backend; for the frontend it is our limit */
2038 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
2039 arg = h2_settings_max_concurrent_streams;
2040 h2c->streams_limit = arg;
2041 }
2042 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002043 }
2044 }
2045
2046 /* need to ACK this frame now */
2047 h2c->st0 = H2_CS_FRAME_A;
Willy Tarreau7838a792019-08-12 18:42:03 +02002048 done:
2049 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002050 return 1;
2051 fail:
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002052 if (!(h2c->flags & H2_CF_IS_BACK))
2053 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002054 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002055 out0:
2056 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 +02002057 return 0;
2058}
2059
2060/* try to send an ACK for a settings frame on the connection. Returns > 0 on
2061 * success or one of the h2_status values.
2062 */
2063static int h2c_ack_settings(struct h2c *h2c)
2064{
2065 struct buffer *res;
2066 char str[9];
Willy Tarreau7838a792019-08-12 18:42:03 +02002067 int ret = 0;
2068
2069 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002070
2071 if (h2c_mux_busy(h2c, NULL)) {
2072 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002073 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002074 }
2075
Willy Tarreau9c218e72019-05-26 10:08:28 +02002076 memcpy(str,
2077 "\x00\x00\x00" /* length : 0 (no data) */
2078 "\x04" "\x01" /* type : 4, flags : ACK */
2079 "\x00\x00\x00\x00" /* stream ID */, 9);
2080
Willy Tarreaubcc45952019-05-26 10:05:50 +02002081 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002082 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002083 if (!h2_get_buf(h2c, res)) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002084 h2c->flags |= H2_CF_MUX_MALLOC;
2085 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002086 goto out;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002087 }
2088
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002089 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02002090 if (unlikely(ret <= 0)) {
2091 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002092 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2093 goto retry;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002094 h2c->flags |= H2_CF_MUX_MFULL;
2095 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002096 }
2097 else {
2098 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002099 ret = 0;
Willy Tarreau3421aba2017-07-27 15:41:03 +02002100 }
2101 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002102 out:
2103 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_SETTINGS, h2c->conn);
Willy Tarreau3421aba2017-07-27 15:41:03 +02002104 return ret;
2105}
2106
Willy Tarreaucf68c782017-10-10 17:11:41 +02002107/* processes a PING frame and schedules an ACK if needed. The caller must pass
2108 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01002109 * missing data. The caller must have already verified frame length
2110 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02002111 */
2112static int h2c_handle_ping(struct h2c *h2c)
2113{
Willy Tarreaucf68c782017-10-10 17:11:41 +02002114 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01002115 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02002116 h2c->st0 = H2_CS_FRAME_A;
2117 return 1;
2118}
2119
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002120/* Try to send a window update for stream id <sid> and value <increment>.
2121 * Returns > 0 on success or zero on missing room or failure. It may return an
2122 * error in h2c.
2123 */
2124static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
2125{
2126 struct buffer *res;
2127 char str[13];
Willy Tarreau7838a792019-08-12 18:42:03 +02002128 int ret = 0;
2129
2130 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002131
2132 if (h2c_mux_busy(h2c, NULL)) {
2133 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002134 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002135 }
2136
Willy Tarreau9c218e72019-05-26 10:08:28 +02002137 /* length: 4, type: 8, flags: none */
2138 memcpy(str, "\x00\x00\x04\x08\x00", 5);
2139 write_n32(str + 5, sid);
2140 write_n32(str + 9, increment);
2141
Willy Tarreaubcc45952019-05-26 10:05:50 +02002142 res = br_tail(h2c->mbuf);
Willy Tarreau9c218e72019-05-26 10:08:28 +02002143 retry:
Willy Tarreaubcc45952019-05-26 10:05:50 +02002144 if (!h2_get_buf(h2c, res)) {
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002145 h2c->flags |= H2_CF_MUX_MALLOC;
2146 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002147 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002148 }
2149
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002150 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002151 if (unlikely(ret <= 0)) {
2152 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002153 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2154 goto retry;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002155 h2c->flags |= H2_CF_MUX_MFULL;
2156 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002157 }
2158 else {
2159 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002160 ret = 0;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002161 }
2162 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002163 out:
2164 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002165 return ret;
2166}
2167
2168/* try to send pending window update for the connection. It's safe to call it
2169 * with no pending updates. Returns > 0 on success or zero on missing room or
2170 * failure. It may return an error in h2c.
2171 */
2172static int h2c_send_conn_wu(struct h2c *h2c)
2173{
2174 int ret = 1;
2175
Willy Tarreau7838a792019-08-12 18:42:03 +02002176 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2177
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002178 if (h2c->rcvd_c <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002179 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002180
Willy Tarreau97aaa672018-12-23 09:49:04 +01002181 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
2182 /* increase the advertised connection window to 2G on
2183 * first update.
2184 */
2185 h2c->flags |= H2_CF_WINDOW_OPENED;
2186 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
2187 }
2188
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002189 /* send WU for the connection */
2190 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
2191 if (ret > 0)
2192 h2c->rcvd_c = 0;
2193
Willy Tarreau7838a792019-08-12 18:42:03 +02002194 out:
2195 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002196 return ret;
2197}
2198
2199/* try to send pending window update for the current dmux stream. It's safe to
2200 * call it with no pending updates. Returns > 0 on success or zero on missing
2201 * room or failure. It may return an error in h2c.
2202 */
2203static int h2c_send_strm_wu(struct h2c *h2c)
2204{
2205 int ret = 1;
2206
Willy Tarreau7838a792019-08-12 18:42:03 +02002207 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
2208
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002209 if (h2c->rcvd_s <= 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002210 goto out;
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002211
2212 /* send WU for the stream */
2213 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
2214 if (ret > 0)
2215 h2c->rcvd_s = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002216 out:
2217 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002218 return ret;
2219}
2220
Willy Tarreaucf68c782017-10-10 17:11:41 +02002221/* try to send an ACK for a ping frame on the connection. Returns > 0 on
2222 * success, 0 on missing data or one of the h2_status values.
2223 */
2224static int h2c_ack_ping(struct h2c *h2c)
2225{
2226 struct buffer *res;
2227 char str[17];
Willy Tarreau7838a792019-08-12 18:42:03 +02002228 int ret = 0;
2229
2230 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002231
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002232 if (b_data(&h2c->dbuf) < 8)
Willy Tarreau7838a792019-08-12 18:42:03 +02002233 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002234
2235 if (h2c_mux_busy(h2c, NULL)) {
2236 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02002237 goto out;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002238 }
2239
Willy Tarreaucf68c782017-10-10 17:11:41 +02002240 memcpy(str,
2241 "\x00\x00\x08" /* length : 8 (same payload) */
2242 "\x06" "\x01" /* type : 6, flags : ACK */
2243 "\x00\x00\x00\x00" /* stream ID */, 9);
2244
2245 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002246 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002247
Willy Tarreau9c218e72019-05-26 10:08:28 +02002248 res = br_tail(h2c->mbuf);
2249 retry:
2250 if (!h2_get_buf(h2c, res)) {
2251 h2c->flags |= H2_CF_MUX_MALLOC;
2252 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02002253 goto out;
Willy Tarreau9c218e72019-05-26 10:08:28 +02002254 }
2255
Willy Tarreauea1b06d2018-07-12 09:02:47 +02002256 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02002257 if (unlikely(ret <= 0)) {
2258 if (!ret) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02002259 if ((res = br_tail_add(h2c->mbuf)) != NULL)
2260 goto retry;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002261 h2c->flags |= H2_CF_MUX_MFULL;
2262 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002263 }
2264 else {
2265 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002266 ret = 0;
Willy Tarreaucf68c782017-10-10 17:11:41 +02002267 }
2268 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002269 out:
2270 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_PING, h2c->conn);
Willy Tarreaucf68c782017-10-10 17:11:41 +02002271 return ret;
2272}
2273
Willy Tarreau26f95952017-07-27 17:18:30 +02002274/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
2275 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01002276 * h2c or h2s. The caller must have already verified frame length and stream ID
2277 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02002278 */
2279static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
2280{
2281 int32_t inc;
2282 int error;
2283
Willy Tarreau7838a792019-08-12 18:42:03 +02002284 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
2285
Willy Tarreau26f95952017-07-27 17:18:30 +02002286 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002287 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002288 goto out0;
Willy Tarreau26f95952017-07-27 17:18:30 +02002289
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002290 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02002291
2292 if (h2c->dsi != 0) {
2293 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02002294
2295 /* it's not an error to receive WU on a closed stream */
2296 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau7838a792019-08-12 18:42:03 +02002297 goto done;
Willy Tarreau26f95952017-07-27 17:18:30 +02002298
2299 if (!inc) {
2300 error = H2_ERR_PROTOCOL_ERROR;
2301 goto strm_err;
2302 }
2303
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002304 if (h2s_mws(h2s) >= 0 && h2s_mws(h2s) + inc < 0) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002305 error = H2_ERR_FLOW_CONTROL_ERROR;
2306 goto strm_err;
2307 }
2308
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002309 h2s->sws += inc;
2310 if (h2s_mws(h2s) > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
Willy Tarreau26f95952017-07-27 17:18:30 +02002311 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau9edf6db2019-10-02 10:49:59 +02002312 LIST_DEL_INIT(&h2s->list);
Willy Tarreauf96508a2020-01-10 11:12:48 +01002313 if ((h2s->subs && h2s->subs->events & SUB_RETRY_SEND) ||
2314 h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))
Olivier Houcharddddfe312018-10-10 18:51:00 +02002315 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02002316 }
2317 }
2318 else {
2319 /* connection window update */
2320 if (!inc) {
2321 error = H2_ERR_PROTOCOL_ERROR;
2322 goto conn_err;
2323 }
2324
2325 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
2326 error = H2_ERR_FLOW_CONTROL_ERROR;
2327 goto conn_err;
2328 }
2329
2330 h2c->mws += inc;
2331 }
2332
Willy Tarreau7838a792019-08-12 18:42:03 +02002333 done:
2334 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002335 return 1;
2336
2337 conn_err:
2338 h2c_error(h2c, error);
Willy Tarreau7838a792019-08-12 18:42:03 +02002339 out0:
2340 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 +02002341 return 0;
2342
2343 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002344 h2s_error(h2s, error);
2345 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002346 TRACE_DEVEL("leaving on stream error", H2_EV_RX_FRAME|H2_EV_RX_WU, h2c->conn);
Willy Tarreau26f95952017-07-27 17:18:30 +02002347 return 0;
2348}
2349
Willy Tarreaue96b0922017-10-30 00:28:29 +01002350/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01002351 * the last ID. Returns > 0 on success or zero on missing data. The caller must
2352 * have already verified frame length and stream ID validity. Described in
2353 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01002354 */
2355static int h2c_handle_goaway(struct h2c *h2c)
2356{
Willy Tarreaue96b0922017-10-30 00:28:29 +01002357 int last;
2358
Willy Tarreau7838a792019-08-12 18:42:03 +02002359 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002360 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002361 if (b_data(&h2c->dbuf) < h2c->dfl) {
2362 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002363 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002364 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01002365
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002366 last = h2_get_n32(&h2c->dbuf, 0);
2367 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01002368 if (h2c->last_sid < 0)
2369 h2c->last_sid = last;
Willy Tarreau23482912019-05-07 15:23:14 +02002370 h2_wake_some_streams(h2c, last);
Willy Tarreau7838a792019-08-12 18:42:03 +02002371 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn);
Willy Tarreaue96b0922017-10-30 00:28:29 +01002372 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01002373}
2374
Willy Tarreau92153fc2017-12-03 19:46:19 +01002375/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01002376 * invalid. Returns > 0 on success or zero on missing data. It may return an
2377 * error in h2c. The caller must have already verified frame length and stream
2378 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01002379 */
2380static int h2c_handle_priority(struct h2c *h2c)
2381{
Willy Tarreau7838a792019-08-12 18:42:03 +02002382 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
2383
Willy Tarreau92153fc2017-12-03 19:46:19 +01002384 /* process full frame only */
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002385 if (b_data(&h2c->dbuf) < h2c->dfl) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002386 TRACE_DEVEL("leaving on missing data", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002387 return 0;
Willy Tarreaue7bbbca2019-08-30 15:02:22 +02002388 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01002389
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002390 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01002391 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01002392 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau7838a792019-08-12 18:42:03 +02002393 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreaub860c732019-01-30 15:39:55 +01002394 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002395 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002396 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn);
Willy Tarreau92153fc2017-12-03 19:46:19 +01002397 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01002398}
2399
Willy Tarreaucd234e92017-08-18 10:59:39 +02002400/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01002401 * Returns > 0 on success or zero on missing data. The caller must have already
2402 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02002403 */
2404static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
2405{
Willy Tarreau7838a792019-08-12 18:42:03 +02002406 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
2407
Willy Tarreaucd234e92017-08-18 10:59:39 +02002408 /* process full frame only */
Willy Tarreau7838a792019-08-12 18:42:03 +02002409 if (b_data(&h2c->dbuf) < h2c->dfl) {
2410 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 +02002411 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02002412 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002413
2414 /* late RST, already handled */
Willy Tarreau7838a792019-08-12 18:42:03 +02002415 if (h2s->st == H2_SS_CLOSED) {
2416 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 +02002417 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002418 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02002419
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002420 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002421 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002422
2423 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01002424 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01002425 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002426 }
2427
2428 h2s->flags |= H2_SF_RST_RCVD;
Willy Tarreau7838a792019-08-12 18:42:03 +02002429 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_RST|H2_EV_RX_EOI, h2c->conn, h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02002430 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002431}
2432
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002433/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2434 * It may return an error in h2c or h2s. The caller must consider that the
2435 * return value is the new h2s in case one was allocated (most common case).
2436 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02002437 * errors here are reported as connection errors since it's impossible to
2438 * recover from such errors after the compression context has been altered.
2439 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002440static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002441{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002442 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002443 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002444 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002445 int error;
2446
Willy Tarreau7838a792019-08-12 18:42:03 +02002447 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2448
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002449 if (!b_size(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002450 goto out; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02002451
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002452 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002453 goto out; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02002454
2455 /* now either the frame is complete or the buffer is complete */
2456 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01002457 /* The stream exists/existed, this must be a trailers frame */
2458 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02002459 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
2460 /* unrecoverable error ? */
2461 if (h2c->st0 >= H2_CS_ERROR)
2462 goto out;
2463
2464 if (error == 0)
2465 goto out; // missing data
2466
2467 if (error < 0) {
2468 /* Failed to decode this frame (e.g. too large request)
2469 * but the HPACK decompressor is still synchronized.
2470 */
2471 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
2472 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01002473 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02002474 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01002475 goto done;
2476 }
Willy Tarreau1f035502019-01-30 11:44:07 +01002477 /* the connection was already killed by an RST, let's consume
2478 * the data and send another RST.
2479 */
2480 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
2481 h2s = (struct h2s*)h2_error_stream;
2482 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002483 }
2484 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
2485 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
2486 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002487 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02002488 goto conn_err;
2489 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01002490 else if (h2c->flags & H2_CF_DEM_TOOMANY)
2491 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02002492
Willy Tarreau4790f7c2019-01-24 11:33:02 +01002493 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002494
Willy Tarreau25919232019-01-03 14:48:18 +01002495 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002496 if (h2c->st0 >= H2_CS_ERROR)
2497 goto out;
2498
Willy Tarreau25919232019-01-03 14:48:18 +01002499 if (error <= 0) {
2500 if (error == 0)
2501 goto out; // missing data
2502
2503 /* Failed to decode this stream (e.g. too large request)
2504 * but the HPACK decompressor is still synchronized.
2505 */
2506 h2s = (struct h2s*)h2_error_stream;
2507 goto send_rst;
2508 }
2509
Willy Tarreau22de8d32018-09-05 19:55:58 +02002510 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02002511 * positively from h2c_frt_stream_new(), the stream will report the error,
2512 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02002513 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02002514 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02002515 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01002516 h2s = (struct h2s*)h2_refused_stream;
2517 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02002518 }
2519
2520 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002521 h2s->rxbuf = rxbuf;
2522 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002523 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002524
Willy Tarreau88d138e2019-01-02 19:38:14 +01002525 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002526 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02002527 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002528
2529 if (h2s->flags & H2_SF_ES_RCVD) {
Willy Tarreaufc10f592019-01-30 19:28:32 +01002530 if (h2s->st == H2_SS_OPEN)
2531 h2s->st = H2_SS_HREM;
2532 else
2533 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002534 }
2535
Willy Tarreau3a429f02019-01-03 11:41:50 +01002536 /* update the max stream ID if the request is being processed */
2537 if (h2s->id > h2c->max_id)
2538 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002539
Willy Tarreau8fecec22019-08-30 07:29:53 +02002540 TRACE_USER("rcvd H2 request ", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW, h2c->conn,, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002541 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002542
2543 conn_err:
2544 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002545 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002546
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002547 out:
2548 h2_release_buf(h2c, &rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002549 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 +01002550 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002551
2552 send_rst:
2553 /* make the demux send an RST for the current stream. We may only
2554 * do this if we're certain that the HEADERS frame was properly
2555 * decompressed so that the HPACK decoder is still kept up to date.
2556 */
2557 h2_release_buf(h2c, &rxbuf);
2558 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002559
2560 TRACE_USER("rejected H2 request", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn,, &rxbuf);
2561 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreau96a10c22018-12-23 18:30:44 +01002562 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002563}
2564
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002565/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2566 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2567 * errors here are reported as connection errors since it's impossible to
2568 * recover from such errors after the compression context has been altered.
2569 */
2570static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2571{
Christopher Faulet6884aa32019-09-23 15:28:20 +02002572 struct buffer rxbuf = BUF_NULL;
2573 unsigned long long body_len = 0;
2574 uint32_t flags = 0;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002575 int error;
2576
Willy Tarreau7838a792019-08-12 18:42:03 +02002577 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2578
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002579 if (!b_size(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002580 goto fail; // empty buffer
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002581
2582 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002583 goto fail; // incomplete frame
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002584
Christopher Faulet6884aa32019-09-23 15:28:20 +02002585 if (h2s->st != H2_SS_CLOSED) {
2586 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
2587 }
2588 else {
2589 /* the connection was already killed by an RST, let's consume
2590 * the data and send another RST.
2591 */
2592 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Christopher Fauletea7a7782019-09-26 16:19:13 +02002593 h2s = (struct h2s*)h2_error_stream;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002594 h2c->st0 = H2_CS_FRAME_E;
2595 goto send_rst;
2596 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002597
Willy Tarreau25919232019-01-03 14:48:18 +01002598 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002599 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002600 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002601
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002602 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2603 /* RFC7540#5.1 */
2604 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2605 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002606 goto fail;
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002607 }
2608
Willy Tarreau25919232019-01-03 14:48:18 +01002609 if (error <= 0) {
2610 if (error == 0)
Willy Tarreau7838a792019-08-12 18:42:03 +02002611 goto fail; // missing data
Willy Tarreau25919232019-01-03 14:48:18 +01002612
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002613 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002614 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002615 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002616 goto fail;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002617 }
2618
Christopher Fauletfa922f02019-05-07 10:55:17 +02002619 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002620 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002621
Willy Tarreau927b88b2019-03-04 08:03:25 +01002622 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002623 h2s->st = H2_SS_ERROR;
Christopher Fauletfa922f02019-05-07 10:55:17 +02002624 else if (h2s->flags & H2_SF_ES_RCVD) {
2625 if (h2s->st == H2_SS_OPEN)
2626 h2s->st = H2_SS_HREM;
2627 else if (h2s->st == H2_SS_HLOC)
2628 h2s_close(h2s);
2629 }
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002630
Willy Tarreau8fecec22019-08-30 07:29:53 +02002631 TRACE_USER("rcvd H2 response", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn,, &h2s->rxbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02002632 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002633 return h2s;
Willy Tarreau7838a792019-08-12 18:42:03 +02002634 fail:
2635 TRACE_DEVEL("leaving on missing data or error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2636 return NULL;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002637
2638 send_rst:
2639 /* make the demux send an RST for the current stream. We may only
2640 * do this if we're certain that the HEADERS frame was properly
2641 * decompressed so that the HPACK decoder is still kept up to date.
2642 */
2643 h2_release_buf(h2c, &rxbuf);
2644 h2c->st0 = H2_CS_FRAME_E;
2645
2646 TRACE_USER("rejected H2 response", H2_EV_RX_FRAME|H2_EV_RX_HDR|H2_EV_STRM_NEW|H2_EV_STRM_END, h2c->conn,, &rxbuf);
2647 TRACE_DEVEL("leaving on error", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
2648 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002649}
2650
Willy Tarreau454f9052017-10-26 19:40:35 +02002651/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2652 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2653 */
2654static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2655{
2656 int error;
2657
Willy Tarreau7838a792019-08-12 18:42:03 +02002658 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
2659
Willy Tarreau454f9052017-10-26 19:40:35 +02002660 /* note that empty DATA frames are perfectly valid and sometimes used
2661 * to signal an end of stream (with the ES flag).
2662 */
2663
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002664 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau7838a792019-08-12 18:42:03 +02002665 goto fail; // empty buffer
Willy Tarreau454f9052017-10-26 19:40:35 +02002666
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002667 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau7838a792019-08-12 18:42:03 +02002668 goto fail; // incomplete frame
Willy Tarreau454f9052017-10-26 19:40:35 +02002669
2670 /* now either the frame is complete or the buffer is complete */
2671
Willy Tarreau454f9052017-10-26 19:40:35 +02002672 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2673 /* RFC7540#6.1 */
2674 error = H2_ERR_STREAM_CLOSED;
2675 goto strm_err;
2676 }
2677
Christopher Faulet4f09ec82019-06-19 09:25:58 +02002678 if ((h2s->flags & H2_SF_DATA_CLEN) && (h2c->dfl - h2c->dpl) > h2s->body_len) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01002679 /* RFC7540#8.1.2 */
2680 error = H2_ERR_PROTOCOL_ERROR;
2681 goto strm_err;
2682 }
2683
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002684 if (!h2_frt_transfer_data(h2s))
Willy Tarreau7838a792019-08-12 18:42:03 +02002685 goto fail;
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002686
Willy Tarreau454f9052017-10-26 19:40:35 +02002687 /* call the upper layers to process the frame, then let the upper layer
2688 * notify the stream about any change.
2689 */
2690 if (!h2s->cs) {
Willy Tarreau082c4572019-08-06 10:11:02 +02002691 /* The upper layer has already closed, this may happen on
2692 * 4xx/redirects during POST, or when receiving a response
2693 * from an H2 server after the client has aborted.
2694 */
2695 error = H2_ERR_CANCEL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002696 goto strm_err;
2697 }
2698
Willy Tarreau8f650c32017-11-21 19:36:21 +01002699 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002700 goto fail;
Willy Tarreau8f650c32017-11-21 19:36:21 +01002701
Willy Tarreau721c9742017-11-07 11:05:42 +01002702 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002703 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002704 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002705 }
2706
2707 /* check for completion : the callee will change this to FRAME_A or
2708 * FRAME_H once done.
2709 */
2710 if (h2c->st0 == H2_CS_FRAME_P)
Willy Tarreau7838a792019-08-12 18:42:03 +02002711 goto fail;
Willy Tarreau454f9052017-10-26 19:40:35 +02002712
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002713 /* last frame */
2714 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Fauletfa922f02019-05-07 10:55:17 +02002715 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002716 if (h2s->st == H2_SS_OPEN)
2717 h2s->st = H2_SS_HREM;
2718 else
2719 h2s_close(h2s);
2720
Willy Tarreau1915ca22019-01-24 11:49:37 +01002721 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2722 /* RFC7540#8.1.2 */
2723 error = H2_ERR_PROTOCOL_ERROR;
2724 goto strm_err;
2725 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002726 }
2727
Willy Tarreau7838a792019-08-12 18:42:03 +02002728 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02002729 return 1;
2730
Willy Tarreau454f9052017-10-26 19:40:35 +02002731 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002732 h2s_error(h2s, error);
2733 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02002734 fail:
2735 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 +02002736 return 0;
2737}
2738
Willy Tarreau63864812019-08-07 14:25:20 +02002739/* check that the current frame described in h2c->{dsi,dft,dfl,dff,...} is
2740 * valid for the current stream state. This is needed only after parsing the
2741 * frame header but in practice it can be performed at any time during
2742 * H2_CS_FRAME_P since no state transition happens there. Returns >0 on success
2743 * or 0 in case of error, in which case either h2s or h2c will carry an error.
2744 */
2745static int h2_frame_check_vs_state(struct h2c *h2c, struct h2s *h2s)
2746{
Willy Tarreau7838a792019-08-12 18:42:03 +02002747 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
2748
Willy Tarreau63864812019-08-07 14:25:20 +02002749 if (h2s->st == H2_SS_IDLE &&
2750 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2751 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2752 * this state MUST be treated as a connection error
2753 */
2754 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002755 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau63864812019-08-07 14:25:20 +02002756 /* only log if no other stream can report the error */
2757 sess_log(h2c->conn->owner);
2758 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002759 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 +02002760 return 0;
2761 }
2762
Willy Tarreau57a18162019-11-24 14:57:53 +01002763 if (h2s->st == H2_SS_IDLE && (h2c->flags & H2_CF_IS_BACK)) {
2764 /* only PUSH_PROMISE would be permitted here */
2765 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2766 TRACE_DEVEL("leaving in error (idle&back)", H2_EV_RX_FRAME|H2_EV_RX_FHDR|H2_EV_PROTO_ERR, h2c->conn, h2s);
2767 return 0;
2768 }
2769
Willy Tarreau63864812019-08-07 14:25:20 +02002770 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2771 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2772 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2773 * this state MUST be treated as a stream error.
2774 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2775 * PUSH_PROMISE/CONTINUATION cause connection errors.
2776 */
2777 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2778 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2779 else
2780 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02002781 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 +02002782 return 0;
2783 }
2784
2785 /* Below the management of frames received in closed state is a
2786 * bit hackish because the spec makes strong differences between
2787 * streams closed by receiving RST, sending RST, and seeing ES
2788 * in both directions. In addition to this, the creation of a
2789 * new stream reusing the identifier of a closed one will be
2790 * detected here. Given that we cannot keep track of all closed
2791 * streams forever, we consider that unknown closed streams were
2792 * closed on RST received, which allows us to respond with an
2793 * RST without breaking the connection (eg: to abort a transfer).
2794 * Some frames have to be silently ignored as well.
2795 */
2796 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2797 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
2798 /* #5.1.1: The identifier of a newly
2799 * established stream MUST be numerically
2800 * greater than all streams that the initiating
2801 * endpoint has opened or reserved. This
2802 * governs streams that are opened using a
2803 * HEADERS frame and streams that are reserved
2804 * using PUSH_PROMISE. An endpoint that
2805 * receives an unexpected stream identifier
2806 * MUST respond with a connection error.
2807 */
2808 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02002809 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 +02002810 return 0;
2811 }
2812
Willy Tarreau4c08f122019-09-26 08:47:15 +02002813 if (h2s->flags & H2_SF_RST_RCVD &&
2814 !(h2_ft_bit(h2c->dft) & (H2_FT_HDR_MASK | H2_FT_RST_STREAM_BIT | H2_FT_PRIORITY_BIT | H2_FT_WINDOW_UPDATE_BIT))) {
Willy Tarreau63864812019-08-07 14:25:20 +02002815 /* RFC7540#5.1:closed: an endpoint that
2816 * receives any frame other than PRIORITY after
2817 * receiving a RST_STREAM MUST treat that as a
2818 * stream error of type STREAM_CLOSED.
2819 *
2820 * Note that old streams fall into this category
2821 * and will lead to an RST being sent.
2822 *
2823 * However, we cannot generalize this to all frame types. Those
2824 * carrying compression state must still be processed before
2825 * being dropped or we'll desynchronize the decoder. This can
2826 * happen with request trailers received after sending an
2827 * RST_STREAM, or with header/trailers responses received after
2828 * sending RST_STREAM (aborted stream).
Willy Tarreau4c08f122019-09-26 08:47:15 +02002829 *
2830 * In addition, since our CLOSED streams always carry the
2831 * RST_RCVD bit, we don't want to accidently catch valid
2832 * frames for a closed stream, i.e. RST/PRIO/WU.
Willy Tarreau63864812019-08-07 14:25:20 +02002833 */
2834 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2835 h2c->st0 = H2_CS_FRAME_E;
Christopher Faulet6884aa32019-09-23 15:28:20 +02002836 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 +02002837 return 0;
2838 }
2839
2840 /* RFC7540#5.1:closed: if this state is reached as a
2841 * result of sending a RST_STREAM frame, the peer that
2842 * receives the RST_STREAM might have already sent
2843 * frames on the stream that cannot be withdrawn. An
2844 * endpoint MUST ignore frames that it receives on
2845 * closed streams after it has sent a RST_STREAM
2846 * frame. An endpoint MAY choose to limit the period
2847 * over which it ignores frames and treat frames that
2848 * arrive after this time as being in error.
2849 */
2850 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
2851 /* RFC7540#5.1:closed: any frame other than
2852 * PRIO/WU/RST in this state MUST be treated as
2853 * a connection error
2854 */
2855 if (h2c->dft != H2_FT_RST_STREAM &&
2856 h2c->dft != H2_FT_PRIORITY &&
2857 h2c->dft != H2_FT_WINDOW_UPDATE) {
2858 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
Willy Tarreau7838a792019-08-12 18:42:03 +02002859 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 +02002860 return 0;
2861 }
2862 }
2863 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002864 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn, h2s);
Willy Tarreau63864812019-08-07 14:25:20 +02002865 return 1;
2866}
2867
Willy Tarreaubc933932017-10-09 16:21:43 +02002868/* process Rx frames to be demultiplexed */
2869static void h2_process_demux(struct h2c *h2c)
2870{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002871 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002872 struct h2_fh hdr;
2873 unsigned int padlen = 0;
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02002874 int32_t old_iw = h2c->miw;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002875
Willy Tarreau7838a792019-08-12 18:42:03 +02002876 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
2877
Willy Tarreau081d4722017-05-16 21:51:05 +02002878 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau7838a792019-08-12 18:42:03 +02002879 goto out;
Willy Tarreau52eed752017-09-22 15:05:09 +02002880
2881 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2882 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002883 TRACE_STATE("expecting preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau01b44822018-10-03 14:26:37 +02002884 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau7838a792019-08-12 18:42:03 +02002885 goto out;
2886
Willy Tarreau52eed752017-09-22 15:05:09 +02002887 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2888 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002889 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002890 TRACE_PROTO("failed to receive preface", H2_EV_RX_PREFACE|H2_EV_PROTO_ERR, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02002891 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002892 sess_log(h2c->conn->owner);
2893 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002894 goto fail;
2895 }
Willy Tarreau7838a792019-08-12 18:42:03 +02002896 TRACE_PROTO("received preface", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02002897
2898 h2c->max_id = 0;
2899 h2c->st0 = H2_CS_SETTINGS1;
Willy Tarreau7838a792019-08-12 18:42:03 +02002900 TRACE_STATE("switching to SETTINGS1", H2_EV_RX_PREFACE, h2c->conn);
Willy Tarreau52eed752017-09-22 15:05:09 +02002901 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002902
2903 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002904 /* ensure that what is pending is a valid SETTINGS frame
2905 * without an ACK.
2906 */
Willy Tarreau7838a792019-08-12 18:42:03 +02002907 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 +02002908 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002909 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002910 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002911 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 +02002912 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002913 if (!(h2c->flags & H2_CF_IS_BACK))
2914 sess_log(h2c->conn->owner);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002915 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002916 goto fail;
2917 }
2918
2919 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2920 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau7838a792019-08-12 18:42:03 +02002921 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 +02002922 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2923 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002924 if (!(h2c->flags & H2_CF_IS_BACK))
2925 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002926 goto fail;
2927 }
2928
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002929 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002930 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau7838a792019-08-12 18:42:03 +02002931 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 +02002932 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2933 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002934 if (!(h2c->flags & H2_CF_IS_BACK))
2935 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002936 goto fail;
2937 }
2938
Willy Tarreau3bf69182018-12-21 15:34:50 +01002939 /* that's OK, switch to FRAME_P to process it. This is
2940 * a SETTINGS frame whose header has already been
2941 * deleted above.
2942 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002943 padlen = 0;
2944 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002945 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002946 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002947
2948 /* process as many incoming frames as possible below */
Willy Tarreau7838a792019-08-12 18:42:03 +02002949 while (1) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002950 int ret = 0;
2951
Willy Tarreau7838a792019-08-12 18:42:03 +02002952 if (!b_data(&h2c->dbuf)) {
2953 TRACE_DEVEL("no more Rx data", H2_EV_RX_FRAME, h2c->conn);
2954 break;
2955 }
2956
2957 if (h2c->st0 >= H2_CS_ERROR) {
2958 TRACE_STATE("end of connection reported", H2_EV_RX_FRAME|H2_EV_RX_EOI, h2c->conn);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002959 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02002960 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002961
2962 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreau30d05f32019-08-06 15:49:51 +02002963 h2c->rcvd_s = 0;
2964
Willy Tarreau7838a792019-08-12 18:42:03 +02002965 TRACE_STATE("expecting H2 frame header", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002966 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002967 break;
2968
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002969 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002970 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 +02002971 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002972 if (!h2c->nb_streams && !(h2c->flags & H2_CF_IS_BACK)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02002973 /* only log if no other stream can report the error */
2974 sess_log(h2c->conn->owner);
2975 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002976 break;
2977 }
2978
Christopher Fauletdd2a5622019-06-18 12:22:38 +02002979 padlen = 0;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002980 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2981 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2982 * we read the pad length and drop it from the remaining
2983 * payload (one byte + the 9 remaining ones = 10 total
2984 * removed), so we have a frame payload starting after the
2985 * pad len. Flow controlled frames (DATA) also count the
2986 * padlen in the flow control, so it must be adjusted.
2987 */
2988 if (hdr.len < 1) {
Willy Tarreau7838a792019-08-12 18:42:03 +02002989 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 +01002990 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02002991 if (!(h2c->flags & H2_CF_IS_BACK))
2992 sess_log(h2c->conn->owner);
Willy Tarreau3bf69182018-12-21 15:34:50 +01002993 goto fail;
2994 }
2995 hdr.len--;
2996
2997 if (b_data(&h2c->dbuf) < 10)
2998 break; // missing padlen
2999
3000 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
3001
3002 if (padlen > hdr.len) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003003 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 +01003004 /* RFC7540#6.1 : pad length = length of
3005 * frame payload or greater => error.
3006 */
3007 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003008 if (!(h2c->flags & H2_CF_IS_BACK))
3009 sess_log(h2c->conn->owner);
Willy Tarreau3bf69182018-12-21 15:34:50 +01003010 goto fail;
3011 }
3012
3013 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
3014 h2c->rcvd_c++;
3015 h2c->rcvd_s++;
3016 }
3017 b_del(&h2c->dbuf, 1);
3018 }
3019 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003020
3021 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02003022 h2c->dfl = hdr.len;
3023 h2c->dsi = hdr.sid;
3024 h2c->dft = hdr.ft;
3025 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01003026 h2c->dpl = padlen;
Willy Tarreau73db4342019-09-25 07:28:44 +02003027 TRACE_STATE("rcvd H2 frame header, switching to FRAME_P state", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003028 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01003029
3030 /* check for minimum basic frame format validity */
3031 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
3032 if (ret != H2_ERR_NO_ERROR) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003033 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 +01003034 h2c_error(h2c, ret);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003035 if (!(h2c->flags & H2_CF_IS_BACK))
3036 sess_log(h2c->conn->owner);
Willy Tarreau54f46e52019-01-30 15:11:03 +01003037 goto fail;
3038 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003039 }
3040
Willy Tarreau9fd5aa82019-08-06 15:21:45 +02003041 /* Only H2_CS_FRAME_P, H2_CS_FRAME_A and H2_CS_FRAME_E here.
3042 * H2_CS_FRAME_P indicates an incomplete previous operation
3043 * (most often the first attempt) and requires some validity
3044 * checks for the frame and the current state. The two other
3045 * ones are set after completion (or abortion) and must skip
3046 * validity checks.
3047 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003048 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
3049
Willy Tarreau567beb82018-12-18 16:52:44 +01003050 if (tmp_h2s != h2s && h2s && h2s->cs &&
3051 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003052 conn_xprt_read0_pending(h2c->conn) ||
3053 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003054 (h2s->flags & H2_SF_ES_RCVD) ||
3055 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003056 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003057 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 +01003058 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01003059 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003060 }
3061 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02003062
Willy Tarreau63864812019-08-07 14:25:20 +02003063 if (h2c->st0 == H2_CS_FRAME_E ||
Willy Tarreau7838a792019-08-12 18:42:03 +02003064 (h2c->st0 == H2_CS_FRAME_P && !h2_frame_check_vs_state(h2c, h2s))) {
3065 TRACE_PROTO("stream error reported", H2_EV_RX_FRAME|H2_EV_PROTO_ERR, h2c->conn, h2s);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003066 goto strm_err;
Willy Tarreau7838a792019-08-12 18:42:03 +02003067 }
Willy Tarreauc0da1962017-10-30 18:38:00 +01003068
Willy Tarreau7e98c052017-10-10 15:56:59 +02003069 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02003070 case H2_FT_SETTINGS:
Willy Tarreau7838a792019-08-12 18:42:03 +02003071 if (h2c->st0 == H2_CS_FRAME_P) {
3072 TRACE_PROTO("receiving H2 SETTINGS frame", H2_EV_RX_FRAME|H2_EV_RX_SETTINGS, h2c->conn, h2s);
Willy Tarreau3421aba2017-07-27 15:41:03 +02003073 ret = h2c_handle_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003074 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02003075
Willy Tarreau7838a792019-08-12 18:42:03 +02003076 if (h2c->st0 == H2_CS_FRAME_A) {
3077 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 +02003078 ret = h2c_ack_settings(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003079 }
Willy Tarreau3421aba2017-07-27 15:41:03 +02003080 break;
3081
Willy Tarreaucf68c782017-10-10 17:11:41 +02003082 case H2_FT_PING:
Willy Tarreau7838a792019-08-12 18:42:03 +02003083 if (h2c->st0 == H2_CS_FRAME_P) {
3084 TRACE_PROTO("receiving H2 PING frame", H2_EV_RX_FRAME|H2_EV_RX_PING, h2c->conn, h2s);
Willy Tarreaucf68c782017-10-10 17:11:41 +02003085 ret = h2c_handle_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003086 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003087
Willy Tarreau7838a792019-08-12 18:42:03 +02003088 if (h2c->st0 == H2_CS_FRAME_A) {
3089 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 +02003090 ret = h2c_ack_ping(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003091 }
Willy Tarreaucf68c782017-10-10 17:11:41 +02003092 break;
3093
Willy Tarreau26f95952017-07-27 17:18:30 +02003094 case H2_FT_WINDOW_UPDATE:
Willy Tarreau7838a792019-08-12 18:42:03 +02003095 if (h2c->st0 == H2_CS_FRAME_P) {
3096 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 +02003097 ret = h2c_handle_window_update(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003098 }
Willy Tarreau26f95952017-07-27 17:18:30 +02003099 break;
3100
Willy Tarreau61290ec2017-10-17 08:19:21 +02003101 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01003102 /* RFC7540#6.10: CONTINUATION may only be preceeded by
3103 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
3104 * frames' parsers consume all following CONTINUATION
3105 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02003106 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003107 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 +01003108 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003109 if (!(h2c->flags & H2_CF_IS_BACK))
3110 sess_log(h2c->conn->owner);
Willy Tarreauea18f862018-12-22 20:19:26 +01003111 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02003112
Willy Tarreau13278b42017-10-13 19:23:14 +02003113 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003114 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003115 TRACE_PROTO("receiving H2 HEADERS frame", H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn, h2s);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02003116 if (h2c->flags & H2_CF_IS_BACK)
3117 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
3118 else
3119 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003120 if (tmp_h2s) {
3121 h2s = tmp_h2s;
3122 ret = 1;
3123 }
3124 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003125 break;
3126
Willy Tarreau454f9052017-10-26 19:40:35 +02003127 case H2_FT_DATA:
Willy Tarreau7838a792019-08-12 18:42:03 +02003128 if (h2c->st0 == H2_CS_FRAME_P) {
3129 TRACE_PROTO("receiving H2 DATA frame", H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454f9052017-10-26 19:40:35 +02003130 ret = h2c_frt_handle_data(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003131 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003132
Willy Tarreau7838a792019-08-12 18:42:03 +02003133 if (h2c->st0 == H2_CS_FRAME_A) {
3134 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 +02003135 ret = h2c_send_strm_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003136 }
Willy Tarreau454f9052017-10-26 19:40:35 +02003137 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02003138
Willy Tarreau92153fc2017-12-03 19:46:19 +01003139 case H2_FT_PRIORITY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003140 if (h2c->st0 == H2_CS_FRAME_P) {
3141 TRACE_PROTO("receiving H2 PRIORITY frame", H2_EV_RX_FRAME|H2_EV_RX_PRIO, h2c->conn, h2s);
Willy Tarreau92153fc2017-12-03 19:46:19 +01003142 ret = h2c_handle_priority(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003143 }
Willy Tarreau92153fc2017-12-03 19:46:19 +01003144 break;
3145
Willy Tarreaucd234e92017-08-18 10:59:39 +02003146 case H2_FT_RST_STREAM:
Willy Tarreau7838a792019-08-12 18:42:03 +02003147 if (h2c->st0 == H2_CS_FRAME_P) {
3148 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 +02003149 ret = h2c_handle_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003150 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02003151 break;
3152
Willy Tarreaue96b0922017-10-30 00:28:29 +01003153 case H2_FT_GOAWAY:
Willy Tarreau7838a792019-08-12 18:42:03 +02003154 if (h2c->st0 == H2_CS_FRAME_P) {
3155 TRACE_PROTO("receiving H2 GOAWAY frame", H2_EV_RX_FRAME|H2_EV_RX_GOAWAY, h2c->conn, h2s);
Willy Tarreaue96b0922017-10-30 00:28:29 +01003156 ret = h2c_handle_goaway(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003157 }
Willy Tarreaue96b0922017-10-30 00:28:29 +01003158 break;
3159
Willy Tarreau1c661982017-10-30 13:52:01 +01003160 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02003161 default:
Willy Tarreau7838a792019-08-12 18:42:03 +02003162 TRACE_PROTO("receiving H2 ignored frame", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003163 /* drop frames that we ignore. They may be larger than
3164 * the buffer so we drain all of their contents until
3165 * we reach the end.
3166 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003167 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3168 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003169 h2c->dfl -= ret;
3170 ret = h2c->dfl == 0;
3171 }
3172
Willy Tarreauf182a9a2017-10-30 12:03:50 +01003173 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01003174 /* We may have to send an RST if not done yet */
Willy Tarreau7838a792019-08-12 18:42:03 +02003175 if (h2s->st == H2_SS_ERROR) {
3176 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 +01003177 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau7838a792019-08-12 18:42:03 +02003178 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003179
Willy Tarreau7838a792019-08-12 18:42:03 +02003180 if (h2c->st0 == H2_CS_FRAME_E) {
3181 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 +01003182 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02003183 }
Willy Tarreau27a84c92017-10-17 08:10:17 +02003184
Willy Tarreau7e98c052017-10-10 15:56:59 +02003185 /* error or missing data condition met above ? */
Willy Tarreau7838a792019-08-12 18:42:03 +02003186 if (ret <= 0) {
3187 TRACE_DEVEL("insufficient data to proceed", H2_EV_RX_FRAME, h2c->conn, h2s);
Willy Tarreau7e98c052017-10-10 15:56:59 +02003188 break;
Willy Tarreau7838a792019-08-12 18:42:03 +02003189 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003190
3191 if (h2c->st0 != H2_CS_FRAME_H) {
Christopher Faulet5112a602019-09-26 16:38:28 +02003192 TRACE_DEVEL("stream error, skip frame payload", H2_EV_RX_FRAME, h2c->conn, h2s);
3193 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
3194 b_del(&h2c->dbuf, ret);
3195 h2c->dfl -= ret;
3196 if (!h2c->dfl) {
3197 TRACE_STATE("switching to FRAME_H", H2_EV_RX_FRAME|H2_EV_RX_FHDR, h2c->conn);
3198 h2c->st0 = H2_CS_FRAME_H;
3199 h2c->dsi = -1;
3200 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02003201 }
3202 }
Willy Tarreau52eed752017-09-22 15:05:09 +02003203
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003204 if (h2c->rcvd_c > 0 &&
Willy Tarreau7838a792019-08-12 18:42:03 +02003205 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) {
3206 TRACE_PROTO("sending H2 WINDOW_UPDATE frame", H2_EV_TX_FRAME|H2_EV_TX_WU, h2c->conn);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003207 h2c_send_conn_wu(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003208 }
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003209
Willy Tarreau52eed752017-09-22 15:05:09 +02003210 fail:
3211 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01003212 if (h2s && h2s->cs &&
3213 (b_data(&h2s->rxbuf) ||
Willy Tarreau76c83822019-06-15 09:55:50 +02003214 conn_xprt_read0_pending(h2c->conn) ||
3215 h2s->st == H2_SS_CLOSED ||
Christopher Fauletfa922f02019-05-07 10:55:17 +02003216 (h2s->flags & H2_SF_ES_RCVD) ||
3217 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003218 /* we may have to signal the upper layers */
Willy Tarreau7838a792019-08-12 18:42:03 +02003219 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 +01003220 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01003221 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01003222 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01003223
Willy Tarreau7838a792019-08-12 18:42:03 +02003224 if (old_iw != h2c->miw) {
3225 TRACE_STATE("notifying streams about SFCTL increase", H2_EV_RX_FRAME|H2_EV_H2S_WAKE, h2c->conn);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003226 h2c_unblock_sfctl(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003227 }
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02003228
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003229 h2c_restart_reading(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003230 out:
3231 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreaubc933932017-10-09 16:21:43 +02003232}
3233
Willy Tarreau989539b2020-01-10 17:01:29 +01003234/* resume each h2s eligible for sending in list head <head> */
3235static void h2_resume_each_sending_h2s(struct h2c *h2c, struct list *head)
3236{
3237 struct h2s *h2s, *h2s_back;
3238
3239 TRACE_ENTER(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3240
3241 list_for_each_entry_safe(h2s, h2s_back, head, list) {
3242 if (h2c->mws <= 0 ||
3243 h2c->flags & H2_CF_MUX_BLOCK_ANY ||
3244 h2c->st0 >= H2_CS_ERROR)
3245 break;
3246
3247 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003248
Willy Tarreaud9464162020-01-10 18:25:07 +01003249 if (h2s->flags & H2_SF_NOTIFIED)
Willy Tarreau70c5b0e2020-01-10 18:20:15 +01003250 continue;
3251
Willy Tarreau5723f292020-01-10 15:16:57 +01003252 /* If the sender changed his mind and unsubscribed, let's just
3253 * remove the stream from the send_list.
Willy Tarreau989539b2020-01-10 17:01:29 +01003254 */
Willy Tarreauf96508a2020-01-10 11:12:48 +01003255 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) &&
3256 (!h2s->subs || !(h2s->subs->events & SUB_RETRY_SEND))) {
Willy Tarreau989539b2020-01-10 17:01:29 +01003257 LIST_DEL_INIT(&h2s->list);
3258 continue;
3259 }
3260
Willy Tarreauf96508a2020-01-10 11:12:48 +01003261 if (h2s->subs && h2s->subs->events & SUB_RETRY_SEND) {
Willy Tarreau5723f292020-01-10 15:16:57 +01003262 h2s->flags |= H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01003263 tasklet_wakeup(h2s->subs->tasklet);
3264 h2s->subs->events &= ~SUB_RETRY_SEND;
3265 if (!h2s->subs->events)
3266 h2s->subs = NULL;
Willy Tarreau5723f292020-01-10 15:16:57 +01003267 }
3268 else if (h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW)) {
3269 tasklet_wakeup(h2s->shut_tl);
3270 }
Willy Tarreau989539b2020-01-10 17:01:29 +01003271 }
3272
3273 TRACE_LEAVE(H2_EV_H2C_SEND|H2_EV_H2S_WAKE, h2c->conn);
3274}
3275
Willy Tarreaubc933932017-10-09 16:21:43 +02003276/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
3277 * the end.
3278 */
3279static int h2_process_mux(struct h2c *h2c)
3280{
Willy Tarreau7838a792019-08-12 18:42:03 +02003281 TRACE_ENTER(H2_EV_H2C_WAKE, h2c->conn);
3282
Willy Tarreau01b44822018-10-03 14:26:37 +02003283 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
3284 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
3285 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
3286 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau9364a5f2019-10-23 11:06:35 +02003287 if (h2c->st0 == H2_CS_ERROR)
Willy Tarreau01b44822018-10-03 14:26:37 +02003288 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau01b44822018-10-03 14:26:37 +02003289 goto fail;
3290 }
3291 h2c->st0 = H2_CS_SETTINGS1;
3292 }
3293 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01003294 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau7838a792019-08-12 18:42:03 +02003295 goto done;
Willy Tarreau01b44822018-10-03 14:26:37 +02003296 }
3297
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003298 /* start by sending possibly pending window updates */
Willy Tarreaue74679a2019-08-06 15:39:32 +02003299 if (h2c->rcvd_s > 0 &&
3300 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3301 h2c_send_strm_wu(h2c) < 0)
3302 goto fail;
3303
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003304 if (h2c->rcvd_c > 0 &&
3305 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
3306 h2c_send_conn_wu(h2c) < 0)
3307 goto fail;
3308
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003309 /* First we always process the flow control list because the streams
3310 * waiting there were already elected for immediate emission but were
3311 * blocked just on this.
3312 */
Willy Tarreau989539b2020-01-10 17:01:29 +01003313 h2_resume_each_sending_h2s(h2c, &h2c->fctl_list);
3314 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02003315
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02003316 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01003317 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02003318 if (h2c->st0 == H2_CS_ERROR) {
3319 if (h2c->max_id >= 0) {
3320 h2c_send_goaway_error(h2c, NULL);
3321 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
Willy Tarreau7838a792019-08-12 18:42:03 +02003322 goto out0;
Willy Tarreau081d4722017-05-16 21:51:05 +02003323 }
3324
3325 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
3326 }
Willy Tarreau081d4722017-05-16 21:51:05 +02003327 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003328 done:
3329 TRACE_LEAVE(H2_EV_H2C_WAKE, h2c->conn);
3330 return 1;
3331 out0:
3332 TRACE_DEVEL("leaving in blocked situation", H2_EV_H2C_WAKE, h2c->conn);
3333 return 0;
Willy Tarreaubc933932017-10-09 16:21:43 +02003334}
3335
Willy Tarreau62f52692017-10-08 23:01:42 +02003336
Willy Tarreau479998a2018-11-18 06:30:59 +01003337/* Attempt to read data, and subscribe if none available.
3338 * The function returns 1 if data has been received, otherwise zero.
3339 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003340static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003341{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003342 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003343 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003344 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02003345 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003346
Willy Tarreau7838a792019-08-12 18:42:03 +02003347 TRACE_ENTER(H2_EV_H2C_RECV, h2c->conn);
3348
3349 if (h2c->wait_event.events & SUB_RETRY_RECV) {
3350 TRACE_DEVEL("leaving on sub_recv", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003351 return (b_data(&h2c->dbuf));
Willy Tarreau7838a792019-08-12 18:42:03 +02003352 }
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003353
Willy Tarreau7838a792019-08-12 18:42:03 +02003354 if (!h2_recv_allowed(h2c)) {
3355 TRACE_DEVEL("leaving on !recv_allowed", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003356 return 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003357 }
Willy Tarreaua2af5122017-10-09 11:56:46 +02003358
Willy Tarreau44e973f2018-03-01 17:49:30 +01003359 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003360 if (!buf) {
3361 h2c->flags |= H2_CF_DEM_DALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02003362 TRACE_DEVEL("leaving on !alloc", H2_EV_H2C_RECV, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003363 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02003364 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02003365
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003366 b_realign_if_empty(buf);
3367 if (!b_data(buf)) {
3368 /* try to pre-align the buffer like the
3369 * rxbufs will be to optimize memory copies. We'll make
3370 * sure that the frame header lands at the end of the
3371 * HTX block to alias it upon recv. We cannot use the
3372 * head because rcv_buf() will realign the buffer if
3373 * it's empty. Thus we cheat and pretend we already
3374 * have a few bytes there.
3375 */
3376 max = buf_room_for_htx_data(buf) + 9;
3377 buf->head = sizeof(struct htx) - 9;
3378 }
3379 else
3380 max = b_room(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01003381
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003382 ret = max ? conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0) : 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003383
Willy Tarreau7838a792019-08-12 18:42:03 +02003384 if (max && !ret && h2_recv_allowed(h2c)) {
3385 TRACE_DATA("failed to receive data, subscribing", H2_EV_H2C_RECV, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003386 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02003387 } else if (ret)
3388 TRACE_DATA("received data", H2_EV_H2C_RECV, h2c->conn,,, (void*)(long)ret);
Olivier Houchard81a15af2018-10-19 17:26:49 +02003389
Olivier Houcharda1411e62018-08-17 18:42:48 +02003390 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01003391 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02003392 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Olivier Houchard46677732018-11-29 17:06:17 +01003393 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02003394 }
3395
Willy Tarreau7838a792019-08-12 18:42:03 +02003396 if (b_data(buf) == buf->size) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003397 h2c->flags |= H2_CF_DEM_DFULL;
Willy Tarreau35fb8462019-10-02 11:05:46 +02003398 TRACE_STATE("demux buffer full", H2_EV_H2C_RECV|H2_EV_H2C_BLK, h2c->conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003399 }
3400
3401 TRACE_LEAVE(H2_EV_H2C_RECV, h2c->conn);
Willy Tarreau4d7a8842019-07-31 16:00:48 +02003402 return !!ret || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003403}
3404
Willy Tarreau479998a2018-11-18 06:30:59 +01003405/* Try to send data if possible.
3406 * The function returns 1 if data have been sent, otherwise zero.
3407 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003408static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003409{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003410 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02003411 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003412 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003413
Willy Tarreau7838a792019-08-12 18:42:03 +02003414 TRACE_ENTER(H2_EV_H2C_SEND, h2c->conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003415
Willy Tarreau7838a792019-08-12 18:42:03 +02003416 if (conn->flags & CO_FL_ERROR) {
3417 TRACE_DEVEL("leaving on error", H2_EV_H2C_SEND, h2c->conn);
3418 return 1;
3419 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003420
Willy Tarreau911db9b2020-01-23 16:27:54 +01003421 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreaua2af5122017-10-09 11:56:46 +02003422 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003423 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003424 }
3425
Willy Tarreaubc933932017-10-09 16:21:43 +02003426 /* This loop is quite simple : it tries to fill as much as it can from
3427 * pending streams into the existing buffer until it's reportedly full
3428 * or the end of send requests is reached. Then it tries to send this
3429 * buffer's contents out, marks it not full if at least one byte could
3430 * be sent, and tries again.
3431 *
3432 * The snd_buf() function normally takes a "flags" argument which may
3433 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
3434 * data immediately comes and CO_SFL_STREAMER to indicate that the
3435 * connection is streaming lots of data (used to increase TLS record
3436 * size at the expense of latency). The former can be sent any time
3437 * there's a buffer full flag, as it indicates at least one stream
3438 * attempted to send and failed so there are pending data. An
3439 * alternative would be to set it as long as there's an active stream
3440 * but that would be problematic for ACKs until we have an absolute
3441 * guarantee that all waiters have at least one byte to send. The
3442 * latter should possibly not be set for now.
3443 */
3444
3445 done = 0;
3446 while (!done) {
3447 unsigned int flags = 0;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003448 unsigned int released = 0;
3449 struct buffer *buf;
Willy Tarreaubc933932017-10-09 16:21:43 +02003450
3451 /* fill as much as we can into the current buffer */
3452 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
3453 done = h2_process_mux(h2c);
3454
Olivier Houchard2b094432019-01-29 18:28:36 +01003455 if (h2c->flags & H2_CF_MUX_MALLOC)
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003456 done = 1; // we won't go further without extra buffers
Olivier Houchard2b094432019-01-29 18:28:36 +01003457
Willy Tarreaubc933932017-10-09 16:21:43 +02003458 if (conn->flags & CO_FL_ERROR)
3459 break;
3460
3461 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
3462 flags |= CO_SFL_MSG_MORE;
3463
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003464 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3465 if (b_data(buf)) {
3466 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, b_data(buf), flags);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003467 if (!ret) {
3468 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003469 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003470 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003471 sent = 1;
Willy Tarreau7838a792019-08-12 18:42:03 +02003472 TRACE_DATA("sent data", H2_EV_H2C_SEND, h2c->conn,, buf, (void*)(long)ret);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003473 b_del(buf, ret);
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003474 if (b_data(buf)) {
3475 done = 1;
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003476 break;
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003477 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003478 }
3479 b_free(buf);
3480 released++;
Willy Tarreau787db9a2018-06-14 18:31:46 +02003481 }
Willy Tarreaubc933932017-10-09 16:21:43 +02003482
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003483 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003484 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003485
Willy Tarreaubc933932017-10-09 16:21:43 +02003486 /* wrote at least one byte, the buffer is not full anymore */
Christopher Faulet69fe5ce2019-10-24 10:31:01 +02003487 if (sent)
3488 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
Willy Tarreaubc933932017-10-09 16:21:43 +02003489 }
3490
Willy Tarreaua2af5122017-10-09 11:56:46 +02003491 if (conn->flags & CO_FL_SOCK_WR_SH) {
3492 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreau51330962019-05-26 09:38:07 +02003493 b_reset(br_tail(h2c->mbuf));
Willy Tarreaua2af5122017-10-09 11:56:46 +02003494 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003495 /* We're not full anymore, so we can wake any task that are waiting
3496 * for us.
3497 */
Willy Tarreau989539b2020-01-10 17:01:29 +01003498 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM)) && h2c->st0 >= H2_CS_FRAME_H)
3499 h2_resume_each_sending_h2s(h2c, &h2c->send_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01003500
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003501 /* We're done, no more to send */
Willy Tarreau7838a792019-08-12 18:42:03 +02003502 if (!br_data(h2c->mbuf)) {
3503 TRACE_DEVEL("leaving with everything sent", H2_EV_H2C_SEND, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003504 return sent;
Willy Tarreau7838a792019-08-12 18:42:03 +02003505 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003506schedule:
Willy Tarreau7838a792019-08-12 18:42:03 +02003507 if (!(conn->flags & CO_FL_ERROR) && !(h2c->wait_event.events & SUB_RETRY_SEND)) {
3508 TRACE_STATE("more data to send, subscribing", H2_EV_H2C_SEND, h2c->conn);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01003509 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Willy Tarreau7838a792019-08-12 18:42:03 +02003510 }
Willy Tarreau7f1265a2019-05-29 17:36:37 +02003511
Willy Tarreau7838a792019-08-12 18:42:03 +02003512 TRACE_DEVEL("leaving with some data left to send", H2_EV_H2C_SEND, h2c->conn);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02003513 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003514}
3515
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02003516/* this is the tasklet referenced in h2c->wait_event.tasklet */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003517static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
3518{
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003519 struct connection *conn;
3520 struct tasklet *tl = (struct tasklet *)t;
3521 int conn_in_list;
3522 struct h2c *h2c;
Olivier Houchard7505f942018-08-21 18:10:44 +02003523 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02003524
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003525
3526 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3527 if (t->context == NULL) {
3528 /* The connection has been taken over by another thread,
3529 * we're no longer responsible for it, so just free the
3530 * tasklet, and do nothing.
3531 */
3532 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3533 tasklet_free(tl);
3534 return NULL;
3535 }
3536 h2c = ctx;
3537 conn = h2c->conn;
3538
3539 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3540
3541 conn_in_list = conn->flags & CO_FL_LIST_MASK;
3542
3543 /* Remove the connection from the list, to be sure nobody attempts
3544 * to use it while we handle the I/O events
3545 */
3546 if (conn_in_list)
3547 MT_LIST_DEL(&conn->list);
3548
3549 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
Willy Tarreau7838a792019-08-12 18:42:03 +02003550
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003551 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02003552 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003553 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02003554 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01003555 if (ret || b_data(&h2c->dbuf))
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003556 ret = h2_process(h2c);
3557
3558 /* If we were in an idle list, we want to add it back into it,
3559 * unless h2_process() returned -1, which mean it has destroyed
3560 * the connection (testing !ret is enough, if h2_process() wasn't
3561 * called then ret will be 0 anyway.
3562 */
3563 if (!ret && conn_in_list) {
3564 struct server *srv = objt_server(conn->target);
3565
3566 if (conn_in_list == CO_FL_SAFE_LIST)
3567 MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list);
3568 else
3569 MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list);
3570 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003571
3572 TRACE_LEAVE(H2_EV_H2C_WAKE);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003573 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003574}
Willy Tarreaua2af5122017-10-09 11:56:46 +02003575
Willy Tarreau62f52692017-10-08 23:01:42 +02003576/* callback called on any event by the connection handler.
3577 * It applies changes and returns zero, or < 0 if it wants immediate
3578 * destruction of the connection (which normally doesn not happen in h2).
3579 */
Olivier Houchard7505f942018-08-21 18:10:44 +02003580static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02003581{
Olivier Houchard7505f942018-08-21 18:10:44 +02003582 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02003583
Willy Tarreau7838a792019-08-12 18:42:03 +02003584 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3585
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003586 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01003587 h2_process_demux(h2c);
3588
3589 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003590 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003591
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003592 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01003593 h2c->flags &= ~H2_CF_DEM_DFULL;
3594 }
Olivier Houchard7505f942018-08-21 18:10:44 +02003595 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01003596
Willy Tarreau0b37d652018-10-03 10:33:02 +02003597 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01003598 /* frontend is stopping, reload likely in progress, let's try
3599 * to announce a graceful shutdown if not yet done. We don't
3600 * care if it fails, it will be tried again later.
3601 */
Willy Tarreau7838a792019-08-12 18:42:03 +02003602 TRACE_STATE("proxy stopped, sending GOAWAY", H2_EV_H2C_WAKE|H2_EV_TX_FRAME, conn);
Willy Tarreau8ec14062017-12-30 18:08:13 +01003603 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3604 if (h2c->last_sid < 0)
3605 h2c->last_sid = (1U << 31) - 1;
3606 h2c_send_goaway_error(h2c, NULL);
3607 }
3608 }
3609
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003610 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003611 * If we received early data, and the handshake is done, wake
3612 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003613 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003614 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
Willy Tarreau911db9b2020-01-23 16:27:54 +01003615 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_WAIT_XPRT | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003616 struct eb32_node *node;
3617 struct h2s *h2s;
3618
3619 h2c->flags |= H2_CF_WAIT_FOR_HS;
3620 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
3621
3622 while (node) {
3623 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01003624 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01003625 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003626 node = eb32_next(node);
3627 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01003628 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01003629
Willy Tarreau26bd7612017-10-09 16:47:04 +02003630 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01003631 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
3632 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
3633 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23482912019-05-07 15:23:14 +02003634 h2_wake_some_streams(h2c, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003635
3636 if (eb_is_empty(&h2c->streams_by_id)) {
3637 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003638 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003639 TRACE_DEVEL("leaving after releasing the connection", H2_EV_H2C_WAKE);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003640 return -1;
3641 }
Willy Tarreau4481e262019-10-31 15:36:30 +01003642
3643 /* connections in error must be removed from the idle lists */
3644 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3645 MT_LIST_DEL((struct mt_list *)&conn->list);
3646 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3647 }
3648 else if (h2c->st0 == H2_CS_ERROR) {
3649 /* connections in error must be removed from the idle lists */
3650 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3651 MT_LIST_DEL((struct mt_list *)&conn->list);
3652 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003653 }
3654
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003655 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01003656 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02003657
Olivier Houchard53216e72018-10-10 15:46:36 +02003658 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
3659 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
3660 (h2c->st0 != H2_CS_ERROR &&
Willy Tarreau662fafc2019-05-26 09:43:07 +02003661 !br_data(h2c->mbuf) &&
Olivier Houchard53216e72018-10-10 15:46:36 +02003662 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
3663 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau2e3c0002019-05-26 09:45:23 +02003664 h2_release_mbuf(h2c);
Willy Tarreaua2af5122017-10-09 11:56:46 +02003665
Willy Tarreau3f133572017-10-31 19:21:06 +01003666 if (h2c->task) {
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02003667 if (h2c_may_expire(h2c))
3668 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3669 else
3670 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003671 task_queue(h2c->task);
Willy Tarreauea392822017-10-31 10:02:25 +01003672 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02003673
Olivier Houchard7505f942018-08-21 18:10:44 +02003674 h2_send(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003675 TRACE_LEAVE(H2_EV_H2C_WAKE, conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003676 return 0;
3677}
3678
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003679/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003680static int h2_wake(struct connection *conn)
3681{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003682 struct h2c *h2c = conn->ctx;
Willy Tarreau7838a792019-08-12 18:42:03 +02003683 int ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003684
Willy Tarreau7838a792019-08-12 18:42:03 +02003685 TRACE_ENTER(H2_EV_H2C_WAKE, conn);
3686 ret = h2_process(h2c);
Willy Tarreau508f9892020-02-11 04:38:56 +01003687 if (ret >= 0)
3688 h2_wake_some_streams(h2c, 0);
Willy Tarreau7838a792019-08-12 18:42:03 +02003689 TRACE_LEAVE(H2_EV_H2C_WAKE);
3690 return ret;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003691}
3692
Willy Tarreauea392822017-10-31 10:02:25 +01003693/* Connection timeout management. The principle is that if there's no receipt
3694 * nor sending for a certain amount of time, the connection is closed. If the
3695 * MUX buffer still has lying data or is not allocatable, the connection is
3696 * immediately killed. If it's allocatable and empty, we attempt to send a
3697 * GOAWAY frame.
3698 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02003699static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01003700{
Olivier Houchard9f6af332018-05-25 14:04:04 +02003701 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01003702 int expired = tick_is_expired(t->expire, now_ms);
3703
Willy Tarreau7838a792019-08-12 18:42:03 +02003704 TRACE_ENTER(H2_EV_H2C_WAKE, h2c ? h2c->conn : NULL);
3705
3706 if (!expired && h2c) {
3707 TRACE_DEVEL("leaving (not expired)", H2_EV_H2C_WAKE, h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01003708 return t;
Willy Tarreau7838a792019-08-12 18:42:03 +02003709 }
Willy Tarreauea392822017-10-31 10:02:25 +01003710
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02003711 if (h2c && !h2c_may_expire(h2c)) {
3712 /* we do still have streams but all of them are idle, waiting
3713 * for the data layer, so we must not enforce the timeout here.
3714 */
3715 t->expire = TICK_ETERNITY;
3716 return t;
3717 }
3718
Olivier Houchardcd4159f2020-03-10 18:39:42 +01003719 /* We're about to destroy the connection, so make sure nobody attempts
3720 * to steal it from us.
3721 */
3722 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3723
3724 if (h2c && h2c->conn->flags & CO_FL_LIST_MASK)
3725 MT_LIST_DEL(&h2c->conn->list);
3726
3727 /* Somebody already stole the connection from us, so we should not
3728 * free it, we just have to free the task.
3729 */
3730 if (!t->context)
3731 h2c = NULL;
3732
3733 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3734
Olivier Houchard3f795f72019-04-17 22:51:06 +02003735 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02003736
3737 if (!h2c) {
3738 /* resources were already deleted */
Willy Tarreau7838a792019-08-12 18:42:03 +02003739 TRACE_DEVEL("leaving (not more h2c)", H2_EV_H2C_WAKE);
Willy Tarreau0975f112018-03-29 15:22:59 +02003740 return NULL;
3741 }
3742
3743 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01003744 h2c_error(h2c, H2_ERR_NO_ERROR);
Willy Tarreau23482912019-05-07 15:23:14 +02003745 h2_wake_some_streams(h2c, 0);
Willy Tarreauea392822017-10-31 10:02:25 +01003746
Willy Tarreau662fafc2019-05-26 09:43:07 +02003747 if (br_data(h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01003748 /* don't even try to send a GOAWAY, the buffer is stuck */
3749 h2c->flags |= H2_CF_GOAWAY_FAILED;
3750 }
3751
3752 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01003753 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01003754 if (h2c_send_goaway_error(h2c, NULL) <= 0)
3755 h2c->flags |= H2_CF_GOAWAY_FAILED;
3756
Willy Tarreau662fafc2019-05-26 09:43:07 +02003757 if (br_data(h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003758 unsigned int released = 0;
3759 struct buffer *buf;
3760
3761 for (buf = br_head(h2c->mbuf); b_size(buf); buf = br_del_head(h2c->mbuf)) {
3762 if (b_data(buf)) {
3763 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, buf, b_data(buf), 0);
3764 if (!ret)
3765 break;
3766 b_del(buf, ret);
3767 if (b_data(buf))
3768 break;
3769 b_free(buf);
3770 released++;
3771 }
Willy Tarreau787db9a2018-06-14 18:31:46 +02003772 }
Willy Tarreau41c4d6a2019-05-26 09:49:17 +02003773
3774 if (released)
Willy Tarreau201840a2019-05-29 17:50:48 +02003775 offer_buffers(NULL, tasks_run_queue);
Willy Tarreau787db9a2018-06-14 18:31:46 +02003776 }
Willy Tarreauea392822017-10-31 10:02:25 +01003777
Willy Tarreau4481e262019-10-31 15:36:30 +01003778 /* in any case this connection must not be considered idle anymore */
3779 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[tid]);
3780 MT_LIST_DEL((struct mt_list *)&h2c->conn->list);
3781 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[tid]);
3782
Willy Tarreau0975f112018-03-29 15:22:59 +02003783 /* either we can release everything now or it will be done later once
3784 * the last stream closes.
3785 */
3786 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02003787 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01003788
Willy Tarreau7838a792019-08-12 18:42:03 +02003789 TRACE_LEAVE(H2_EV_H2C_WAKE);
Willy Tarreauea392822017-10-31 10:02:25 +01003790 return NULL;
3791}
3792
3793
Willy Tarreau62f52692017-10-08 23:01:42 +02003794/*******************************************/
3795/* functions below are used by the streams */
3796/*******************************************/
3797
3798/*
3799 * Attach a new stream to a connection
3800 * (Used for outgoing connections)
3801 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01003802static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02003803{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003804 struct conn_stream *cs;
3805 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003806 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003807
Willy Tarreau7838a792019-08-12 18:42:03 +02003808 TRACE_ENTER(H2_EV_H2S_NEW, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003809 cs = cs_new(conn);
Willy Tarreau7838a792019-08-12 18:42:03 +02003810 if (!cs) {
3811 TRACE_DEVEL("leaving on CS allocation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003812 return NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02003813 }
Olivier Houchardf502aca2018-12-14 19:42:40 +01003814 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003815 if (!h2s) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003816 TRACE_DEVEL("leaving on stream creation failure", H2_EV_H2S_NEW|H2_EV_H2S_ERR, conn);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003817 cs_free(cs);
3818 return NULL;
3819 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003820 TRACE_LEAVE(H2_EV_H2S_NEW, conn, h2s);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01003821 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003822}
3823
Willy Tarreaufafd3982018-11-18 21:29:20 +01003824/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3825 * We have to scan because we may have some orphan streams. It might be
3826 * beneficial to scan backwards from the end to reduce the likeliness to find
3827 * orphans.
3828 */
3829static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3830{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003831 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003832 struct h2s *h2s;
3833 struct eb32_node *node;
3834
3835 node = eb32_first(&h2c->streams_by_id);
3836 while (node) {
3837 h2s = container_of(node, struct h2s, by_id);
3838 if (h2s->cs)
3839 return h2s->cs;
3840 node = eb32_next(node);
3841 }
3842 return NULL;
3843}
3844
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02003845static int h2_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output)
3846{
3847 int ret = 0;
3848 struct h2c *h2c = conn->ctx;
3849
3850 switch (mux_ctl) {
3851 case MUX_STATUS:
3852 /* Only consider the mux to be ready if we're done with
3853 * the preface and settings, and we had no error.
3854 */
3855 if (h2c->st0 >= H2_CS_FRAME_H && h2c->st0 < H2_CS_ERROR)
3856 ret |= MUX_STATUS_READY;
3857 return ret;
3858 default:
3859 return -1;
3860 }
3861}
3862
Willy Tarreau62f52692017-10-08 23:01:42 +02003863/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003864 * Destroy the mux and the associated connection, if it is no longer used
3865 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003866static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003867{
Christopher Faulet73c12072019-04-08 11:23:22 +02003868 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003869
Willy Tarreau7838a792019-08-12 18:42:03 +02003870 TRACE_ENTER(H2_EV_H2C_END, h2c->conn);
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003871 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003872 h2_release(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003873 TRACE_LEAVE(H2_EV_H2C_END);
Olivier Houchard060ed432018-11-06 16:32:42 +01003874}
3875
3876/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003877 * Detach the stream from the connection and possibly release the connection.
3878 */
3879static void h2_detach(struct conn_stream *cs)
3880{
Willy Tarreau60935142017-10-16 18:11:19 +02003881 struct h2s *h2s = cs->ctx;
3882 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003883 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003884
Willy Tarreau7838a792019-08-12 18:42:03 +02003885 TRACE_ENTER(H2_EV_STRM_END, h2s ? h2s->h2c->conn : NULL, h2s);
3886
Willy Tarreau60935142017-10-16 18:11:19 +02003887 cs->ctx = NULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02003888 if (!h2s) {
3889 TRACE_LEAVE(H2_EV_STRM_END);
Willy Tarreau60935142017-10-16 18:11:19 +02003890 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02003891 }
Willy Tarreau60935142017-10-16 18:11:19 +02003892
Willy Tarreaud9464162020-01-10 18:25:07 +01003893 /* there's no txbuf so we're certain not to be able to send anything */
3894 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02003895
Olivier Houchardf502aca2018-12-14 19:42:40 +01003896 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003897 h2c = h2s->h2c;
3898 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003899 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003900 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3901 !h2_frt_has_too_many_cs(h2c)) {
3902 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003903 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003904 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003905 }
Willy Tarreau60935142017-10-16 18:11:19 +02003906
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003907 /* this stream may be blocked waiting for some data to leave (possibly
3908 * an ES or RST frame), so orphan it in this case.
3909 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003910 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003911 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau5723f292020-01-10 15:16:57 +01003912 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) &&
Willy Tarreauf96508a2020-01-10 11:12:48 +01003913 ((h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)) || h2s->subs)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02003914 TRACE_DEVEL("leaving on stream blocked", H2_EV_STRM_END|H2_EV_H2S_BLK, h2c->conn, h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003915 return;
Willy Tarreau7838a792019-08-12 18:42:03 +02003916 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003917
Willy Tarreau45f752e2017-10-30 15:44:59 +01003918 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3919 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3920 /* unblock the connection if it was blocked on this
3921 * stream.
3922 */
3923 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3924 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003925 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003926 }
3927
Willy Tarreau71049cc2018-03-28 13:56:39 +02003928 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003929
Christopher Faulet9b79a102019-07-15 11:22:56 +02003930 if (h2c->flags & H2_CF_IS_BACK) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003931 if (!(h2c->conn->flags &
3932 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003933 /* Never ever allow to reuse a connection from a non-reuse backend */
3934 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3935 h2c->conn->flags |= CO_FL_PRIVATE;
3936 if (!h2c->conn->owner && (h2c->conn->flags & CO_FL_PRIVATE)) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003937 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003938 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3939 h2c->conn->owner = NULL;
3940 if (eb_is_empty(&h2c->streams_by_id)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003941 h2c->conn->mux->destroy(h2c);
Willy Tarreau7838a792019-08-12 18:42:03 +02003942 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
Olivier Houchard351411f2018-12-27 17:20:54 +01003943 return;
3944 }
3945 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003946 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003947 if (eb_is_empty(&h2c->streams_by_id)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003948 if (sess && h2c->conn->owner == sess &&
3949 session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0) {
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003950 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
Willy Tarreaufe1c9082019-08-30 14:57:17 +02003951 TRACE_DEVEL("leaving without reusable idle connection", H2_EV_STRM_END);
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003952 return;
Christopher Fauletb2d930e2019-09-26 16:15:10 +02003953 }
Olivier Houchard2444aa52020-01-20 13:56:01 +01003954 if (!(h2c->conn->flags & CO_FL_PRIVATE)) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003955 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn, 1)) {
Olivier Houchard2444aa52020-01-20 13:56:01 +01003956 /* The server doesn't want it, let's kill the connection right away */
3957 h2c->conn->mux->destroy(h2c);
3958 TRACE_DEVEL("leaving on error after killing outgoing connection", H2_EV_STRM_END|H2_EV_H2C_ERR);
3959 return;
3960 }
Olivier Houchard199d4fa2020-03-22 23:25:51 +01003961 /* At this point, the connection has been added to the
3962 * server idle list, so another thread may already have
3963 * hijacked it, so we can't do anything with it.
3964 */
Olivier Houchard2444aa52020-01-20 13:56:01 +01003965 TRACE_DEVEL("reusable idle connection", H2_EV_STRM_END);
3966 return;
Olivier Houchard8a786902018-12-15 16:05:40 +01003967
Olivier Houchard8a786902018-12-15 16:05:40 +01003968 }
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003969 } else if (MT_LIST_ISEMPTY(&h2c->conn->list) &&
Olivier Houcharddc2f2752020-02-13 19:12:07 +01003970 h2_avail_streams(h2c->conn) > 0 && objt_server(h2c->conn->target)) {
Olivier Houchardf0d4dff2020-03-06 18:12:03 +01003971 LIST_ADD(&__objt_server(h2c->conn->target)->available_conns[tid], mt_list_to_list(&h2c->conn->list));
Olivier Houchard8a786902018-12-15 16:05:40 +01003972 }
3973 }
3974 }
3975
Willy Tarreaue323f342018-03-28 13:51:45 +02003976 /* We don't want to close right now unless we're removing the
3977 * last stream, and either the connection is in error, or it
3978 * reached the ID already specified in a GOAWAY frame received
3979 * or sent (as seen by last_sid >= 0).
3980 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003981 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003982 /* no more stream will come, kill it now */
Willy Tarreau7838a792019-08-12 18:42:03 +02003983 TRACE_DEVEL("leaving and killing dead connection", H2_EV_STRM_END, h2c->conn);
Christopher Faulet73c12072019-04-08 11:23:22 +02003984 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003985 }
3986 else if (h2c->task) {
Willy Tarreauc2ea47f2019-10-01 10:12:00 +02003987 if (h2c_may_expire(h2c))
3988 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3989 else
3990 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau73481192019-06-07 08:20:46 +02003991 task_queue(h2c->task);
Willy Tarreau7838a792019-08-12 18:42:03 +02003992 TRACE_DEVEL("leaving, refreshing connection's timeout", H2_EV_STRM_END, h2c->conn);
Willy Tarreau60935142017-10-16 18:11:19 +02003993 }
Willy Tarreau7838a792019-08-12 18:42:03 +02003994 else
3995 TRACE_DEVEL("leaving", H2_EV_STRM_END, h2c->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02003996}
3997
Willy Tarreau88bdba32019-05-13 18:17:53 +02003998/* Performs a synchronous or asynchronous shutr(). */
3999static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004000{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004001 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004002
Willy Tarreauf983d002019-05-14 10:40:21 +02004003 if (h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004004 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004005
Willy Tarreau7838a792019-08-12 18:42:03 +02004006 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4007
Willy Tarreau18059042019-01-31 19:12:48 +01004008 /* a connstream may require us to immediately kill the whole connection
4009 * for example because of a "tcp-request content reject" rule that is
4010 * normally used to limit abuse. In this case we schedule a goaway to
4011 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01004012 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004013 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004014 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004015 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004016 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4017 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4018 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004019 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
4020 /* Nothing was never sent for this stream, so reset with
4021 * REFUSED_STREAM error to let the client retry the
4022 * request.
4023 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004024 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004025 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4026 }
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004027 else {
4028 /* a final response was already provided, we don't want this
4029 * stream anymore. This may happen when the server responds
4030 * before the end of an upload and closes quickly (redirect,
4031 * deny, ...)
4032 */
4033 h2s_error(h2s, H2_ERR_CANCEL);
4034 }
Willy Tarreau18059042019-01-31 19:12:48 +01004035
Willy Tarreau90c32322017-11-24 08:00:30 +01004036 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004037 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004038 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004039
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004040 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004041 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau00dd0782018-03-01 16:31:34 +01004042 h2s_close(h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004043 done:
4044 h2s->flags &= ~H2_SF_WANT_SHUTR;
Willy Tarreau7838a792019-08-12 18:42:03 +02004045 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004046 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004047add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004048 /* Let the handler know we want to shutr, and add ourselves to the
4049 * most relevant list if not yet done. h2_deferred_shut() will be
4050 * automatically called via the shut_tl tasklet when there's room
4051 * again.
4052 */
4053 h2s->flags |= H2_SF_WANT_SHUTR;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004054 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004055 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004056 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004057 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004058 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004059 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004060 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004061 return;
Willy Tarreau62f52692017-10-08 23:01:42 +02004062}
4063
Willy Tarreau88bdba32019-05-13 18:17:53 +02004064/* Performs a synchronous or asynchronous shutw(). */
4065static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02004066{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004067 struct h2c *h2c = h2s->h2c;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004068
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004069 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_CLOSED)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004070 goto done;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004071
Willy Tarreau7838a792019-08-12 18:42:03 +02004072 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4073
Willy Tarreaucfba9d62019-08-06 10:30:58 +02004074 if (h2s->st != H2_SS_ERROR && (h2s->flags & H2_SF_HEADERS_SENT)) {
Willy Tarreau58e32082017-11-07 14:41:09 +01004075 /* we can cleanly close using an empty data frame only after headers */
4076
4077 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
4078 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004079 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01004080
4081 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004082 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01004083 else
4084 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004085 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01004086 /* a connstream may require us to immediately kill the whole connection
4087 * for example because of a "tcp-request content reject" rule that is
4088 * normally used to limit abuse. In this case we schedule a goaway to
4089 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01004090 */
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004091 if ((h2s->flags & H2_SF_KILL_CONN) &&
Willy Tarreau18059042019-01-31 19:12:48 +01004092 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004093 TRACE_STATE("stream wants to kill the connection", H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau18059042019-01-31 19:12:48 +01004094 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
4095 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
4096 }
Christopher Faulet35757d32019-03-07 15:51:33 +01004097 else {
4098 /* Nothing was never sent for this stream, so reset with
4099 * REFUSED_STREAM error to let the client retry the
4100 * request.
4101 */
Willy Tarreau7838a792019-08-12 18:42:03 +02004102 TRACE_STATE("no headers sent yet, trying a retryable abort", H2_EV_STRM_SHUT, h2c->conn, h2s);
Christopher Faulet35757d32019-03-07 15:51:33 +01004103 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
4104 }
Willy Tarreau18059042019-01-31 19:12:48 +01004105
Willy Tarreau90c32322017-11-24 08:00:30 +01004106 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004107 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004108 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01004109
Willy Tarreau00dd0782018-03-01 16:31:34 +01004110 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01004111 }
4112
Willy Tarreau4f6516d2018-12-19 13:59:17 +01004113 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004114 tasklet_wakeup(h2c->wait_event.tasklet);
Willy Tarreau7838a792019-08-12 18:42:03 +02004115
4116 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
4117
Willy Tarreau88bdba32019-05-13 18:17:53 +02004118 done:
4119 h2s->flags &= ~H2_SF_WANT_SHUTW;
4120 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004121
4122 add_to_list:
Willy Tarreau5723f292020-01-10 15:16:57 +01004123 /* Let the handler know we want to shutw, and add ourselves to the
4124 * most relevant list if not yet done. h2_deferred_shut() will be
4125 * automatically called via the shut_tl tasklet when there's room
4126 * again.
4127 */
4128 h2s->flags |= H2_SF_WANT_SHUTW;
Willy Tarreauc234ae32019-05-13 17:56:11 +02004129 if (!LIST_ADDED(&h2s->list)) {
Willy Tarreau5723f292020-01-10 15:16:57 +01004130 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004131 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
Willy Tarreau5723f292020-01-10 15:16:57 +01004132 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004133 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02004134 }
Willy Tarreau7838a792019-08-12 18:42:03 +02004135 TRACE_LEAVE(H2_EV_STRM_SHUT, h2c->conn, h2s);
Willy Tarreau88bdba32019-05-13 18:17:53 +02004136 return;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004137}
4138
Willy Tarreau5723f292020-01-10 15:16:57 +01004139/* This is the tasklet referenced in h2s->shut_tl, it is used for
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004140 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
4141 * and prevented the last frame from being emitted.
4142 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004143static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
4144{
4145 struct h2s *h2s = ctx;
Willy Tarreau88bdba32019-05-13 18:17:53 +02004146 struct h2c *h2c = h2s->h2c;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004147
Willy Tarreau7838a792019-08-12 18:42:03 +02004148 TRACE_ENTER(H2_EV_STRM_SHUT, h2c->conn, h2s);
4149
Willy Tarreau5723f292020-01-10 15:16:57 +01004150 if (h2s->flags & H2_SF_NOTIFIED) {
4151 /* some data processing remains to be done first */
4152 goto end;
4153 }
4154
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004155 if (h2s->flags & H2_SF_WANT_SHUTW)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004156 h2_do_shutw(h2s);
4157
Willy Tarreau2c249eb2019-05-13 18:06:17 +02004158 if (h2s->flags & H2_SF_WANT_SHUTR)
Willy Tarreau88bdba32019-05-13 18:17:53 +02004159 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004160
Willy Tarreau88bdba32019-05-13 18:17:53 +02004161 if (!(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Olivier Houchardafc7cb82019-03-25 14:08:01 +01004162 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01004163 LIST_DEL_INIT(&h2s->list);
Olivier Houchard7a977432019-03-21 15:47:13 +01004164
Willy Tarreau88bdba32019-05-13 18:17:53 +02004165 if (!h2s->cs) {
4166 h2s_destroy(h2s);
4167 if (h2c_is_dead(h2c))
4168 h2_release(h2c);
4169 }
Olivier Houchard7a977432019-03-21 15:47:13 +01004170 }
Willy Tarreau5723f292020-01-10 15:16:57 +01004171 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02004172 TRACE_LEAVE(H2_EV_STRM_SHUT);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004173 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02004174}
4175
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004176/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004177static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
4178{
4179 struct h2s *h2s = cs->ctx;
4180
Willy Tarreau7838a792019-08-12 18:42:03 +02004181 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004182 if (cs->flags & CS_FL_KILL_CONN)
4183 h2s->flags |= H2_SF_KILL_CONN;
4184
Willy Tarreau7838a792019-08-12 18:42:03 +02004185 if (mode)
4186 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004187
Willy Tarreau7838a792019-08-12 18:42:03 +02004188 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004189}
4190
Willy Tarreau749f5ca2019-03-21 19:19:36 +01004191/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004192static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
4193{
4194 struct h2s *h2s = cs->ctx;
4195
Willy Tarreau7838a792019-08-12 18:42:03 +02004196 TRACE_ENTER(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Willy Tarreau3cf69fe2019-05-14 10:44:40 +02004197 if (cs->flags & CS_FL_KILL_CONN)
4198 h2s->flags |= H2_SF_KILL_CONN;
4199
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004200 h2_do_shutw(h2s);
Willy Tarreau7838a792019-08-12 18:42:03 +02004201 TRACE_LEAVE(H2_EV_STRM_SHUT, h2s->h2c->conn, h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004202}
4203
Christopher Faulet9b79a102019-07-15 11:22:56 +02004204/* Decode the payload of a HEADERS frame and produce the HTX request or response
4205 * depending on the connection's side. Returns a positive value on success, a
4206 * negative value on failure, or 0 if it couldn't proceed. May report connection
4207 * errors in h2c->errcode if the frame is non-decodable and the connection
4208 * unrecoverable. In absence of connection error when a failure is reported, the
4209 * caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01004210 *
4211 * The function may fold CONTINUATION frames into the initial HEADERS frame
4212 * by removing padding and next frame header, then moving the CONTINUATION
4213 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
4214 * leaving a hole between the main frame and the beginning of the next one.
4215 * The possibly remaining incomplete or next frame at the end may be moved
4216 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
4217 * HEADERS frames are unwrapped into a temporary buffer before decoding.
4218 *
4219 * A buffer at the beginning of processing may look like this :
4220 *
4221 * ,---.---------.-----.--------------.--------------.------.---.
4222 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
4223 * `---^---------^-----^--------------^--------------^------^---'
4224 * | | <-----> | |
4225 * area | dpl | wrap
4226 * |<--------------> |
4227 * | dfl |
4228 * |<-------------------------------------------------->|
4229 * head data
4230 *
4231 * Padding is automatically overwritten when folding, participating to the
4232 * hole size after dfl :
4233 *
4234 * ,---.------------------------.-----.--------------.------.---.
4235 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
4236 * `---^------------------------^-----^--------------^------^---'
4237 * | | <-----> | |
4238 * area | hole | wrap
4239 * |<-----------------------> |
4240 * | dfl |
4241 * |<-------------------------------------------------->|
4242 * head data
4243 *
4244 * Please note that the HEADERS frame is always deprived from its PADLEN byte
4245 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
4246 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004247 *
4248 * The <flags> field must point to either the stream's flags or to a copy of it
4249 * so that the function can update the following flags :
4250 * - H2_SF_DATA_CLEN when content-length is seen
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004251 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01004252 *
4253 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
4254 * decoding, in order to detect if we're dealing with a headers or a trailers
4255 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02004256 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01004257static 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 +02004258{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004259 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02004260 struct buffer *tmp = get_trash_chunk();
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004261 struct http_hdr list[global.tune.max_http_hdr * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02004262 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02004263 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004264 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01004265 int flen; // header frame len
4266 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01004267 int ret = 0;
4268 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02004269 int wrap;
Willy Tarreau13278b42017-10-13 19:23:14 +02004270
Willy Tarreau7838a792019-08-12 18:42:03 +02004271 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
4272
Willy Tarreauea18f862018-12-22 20:19:26 +01004273next_frame:
4274 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
4275 goto leave; // incomplete input frame
4276
4277 /* No END_HEADERS means there's one or more CONTINUATION frames. In
4278 * this case, we'll try to paste it immediately after the initial
4279 * HEADERS frame payload and kill any possible padding. The initial
4280 * frame's length will be increased to represent the concatenation
4281 * of the two frames. The next frame is read from position <tlen>
4282 * and written at position <flen> (minus padding if some is present).
4283 */
4284 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
4285 struct h2_fh hdr;
4286 int clen; // CONTINUATION frame's payload length
4287
Willy Tarreau7838a792019-08-12 18:42:03 +02004288 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 +01004289 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
4290 /* no more data, the buffer may be full, either due to
4291 * too large a frame or because of too large a hole that
4292 * we're going to compact at the end.
4293 */
4294 goto leave;
4295 }
4296
4297 if (hdr.ft != H2_FT_CONTINUATION) {
4298 /* RFC7540#6.10: frame of unexpected type */
Willy Tarreau7838a792019-08-12 18:42:03 +02004299 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 +01004300 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
4301 goto fail;
4302 }
4303
4304 if (hdr.sid != h2c->dsi) {
4305 /* RFC7540#6.10: frame of different stream */
Willy Tarreau7838a792019-08-12 18:42:03 +02004306 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 +01004307 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
4308 goto fail;
4309 }
4310
4311 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
4312 /* RFC7540#4.2: invalid frame length */
Willy Tarreau7838a792019-08-12 18:42:03 +02004313 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 +01004314 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4315 goto fail;
4316 }
4317
4318 /* detect when we must stop aggragating frames */
4319 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
4320
4321 /* Take as much as we can of the CONTINUATION frame's payload */
4322 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
4323 if (clen > hdr.len)
4324 clen = hdr.len;
4325
4326 /* Move the frame's payload over the padding, hole and frame
4327 * header. At least one of hole or dpl is null (see diagrams
4328 * above). The hole moves after the new aggragated frame.
4329 */
4330 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
4331 h2c->dfl += clen - h2c->dpl;
4332 hole += h2c->dpl + 9;
4333 h2c->dpl = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004334 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 +01004335 goto next_frame;
4336 }
4337
4338 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01004339
Willy Tarreau13278b42017-10-13 19:23:14 +02004340 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004341 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02004342 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02004343 copy = alloc_trash_chunk();
4344 if (!copy) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004345 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 +02004346 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
4347 goto fail;
4348 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004349 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
4350 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
4351 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02004352 }
4353
Willy Tarreau13278b42017-10-13 19:23:14 +02004354 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
4355 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004356 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004357 /* RFC7540#5.3.1 : stream dep may not depend on itself */
Willy Tarreau7838a792019-08-12 18:42:03 +02004358 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 +01004359 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02004360 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01004361 }
4362
Willy Tarreaua01f45e2018-12-31 07:41:24 +01004363 if (flen < 5) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004364 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 +01004365 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
4366 goto fail;
4367 }
4368
Willy Tarreau13278b42017-10-13 19:23:14 +02004369 hdrs += 5; // stream dep = 4, weight = 1
4370 flen -= 5;
4371 }
4372
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004373 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004374 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 +01004375 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01004376 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004377 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004378
Willy Tarreau937f7602018-02-26 15:22:17 +01004379 /* we can't retry a failed decompression operation so we must be very
4380 * careful not to take any risks. In practice the output buffer is
4381 * always empty except maybe for trailers, in which case we simply have
4382 * to wait for the upper layer to finish consuming what is available.
4383 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004384 htx = htx_from_buf(rxbuf);
4385 if (!htx_is_empty(htx)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004386 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 +02004387 h2c->flags |= H2_CF_DEM_SFULL;
4388 goto leave;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004389 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004390
Willy Tarreau25919232019-01-03 14:48:18 +01004391 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004392 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
4393 sizeof(list)/sizeof(list[0]), tmp);
4394 if (outlen < 0) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004395 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 +01004396 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
4397 goto fail;
4398 }
4399
Willy Tarreau25919232019-01-03 14:48:18 +01004400 /* The PACK decompressor was updated, let's update the input buffer and
4401 * the parser's state to commit these changes and allow us to later
4402 * fail solely on the stream if needed.
4403 */
4404 b_del(&h2c->dbuf, h2c->dfl + hole);
4405 h2c->dfl = hole = 0;
4406 h2c->st0 = H2_CS_FRAME_H;
4407
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004408 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01004409 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01004410
Willy Tarreau88d138e2019-01-02 19:38:14 +01004411 if (*flags & H2_SF_HEADERS_RCVD)
4412 goto trailers;
4413
4414 /* This is the first HEADERS frame so it's a headers block */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004415 if (h2c->flags & H2_CF_IS_BACK)
4416 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
4417 else
4418 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01004419
4420 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01004421 /* too large headers? this is a stream error only */
Willy Tarreau7838a792019-08-12 18:42:03 +02004422 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 +01004423 goto fail;
4424 }
Willy Tarreau13278b42017-10-13 19:23:14 +02004425
Willy Tarreau174b06a2018-04-25 18:13:58 +02004426 if (msgf & H2_MSGF_BODY) {
4427 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01004428 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004429 *flags |= H2_SF_DATA_CLEN;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004430 htx->extra = *body_len;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01004431 }
Willy Tarreau174b06a2018-04-25 18:13:58 +02004432 }
4433
Willy Tarreau88d138e2019-01-02 19:38:14 +01004434 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01004435 /* indicate that a HEADERS frame was received for this stream, except
4436 * for 1xx responses. For 1xx responses, another HEADERS frame is
4437 * expected.
4438 */
4439 if (!(msgf & H2_MSGF_RSP_1XX))
4440 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01004441
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02004442 if ((h2c->dff & H2_F_HEADERS_END_STREAM)) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02004443 /* Mark the end of message using EOM */
Willy Tarreau7838a792019-08-12 18:42:03 +02004444 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
4445 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 +02004446 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004447 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01004448 }
Willy Tarreau937f7602018-02-26 15:22:17 +01004449
Willy Tarreau86277d42019-01-02 15:36:11 +01004450 /* success */
4451 ret = 1;
4452
Willy Tarreau68dd9852017-07-03 14:44:26 +02004453 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01004454 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01004455 * move the remaining data over it.
4456 */
4457 if (hole) {
4458 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
4459 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
4460 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
4461 b_sub(&h2c->dbuf, hole);
4462 }
4463
Willy Tarreau97215ca2019-04-29 10:20:21 +02004464 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01004465 /* too large frames */
4466 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01004467 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01004468 }
4469
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004470 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01004471 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02004472 free_trash_chunk(copy);
Willy Tarreau7838a792019-08-12 18:42:03 +02004473 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_HDR, h2c->conn);
Willy Tarreau86277d42019-01-02 15:36:11 +01004474 return ret;
4475
Willy Tarreau68dd9852017-07-03 14:44:26 +02004476 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01004477 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02004478 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01004479
4480 trailers:
4481 /* This is the last HEADERS frame hence a trailer */
Willy Tarreau88d138e2019-01-02 19:38:14 +01004482 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
4483 /* It's a trailer but it's missing ES flag */
Willy Tarreau7838a792019-08-12 18:42:03 +02004484 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 +01004485 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
4486 goto fail;
4487 }
4488
Christopher Faulet9b79a102019-07-15 11:22:56 +02004489 /* Trailers terminate a DATA sequence */
Willy Tarreau7838a792019-08-12 18:42:03 +02004490 if (h2_make_htx_trailers(list, htx) <= 0) {
4491 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 +02004492 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004493 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01004494 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02004495}
4496
Christopher Faulet9b79a102019-07-15 11:22:56 +02004497/* Transfer the payload of a DATA frame to the HTTP/1 side. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004498 * parser state is automatically updated. Returns > 0 if it could completely
4499 * send the current frame, 0 if it couldn't complete, in which case
4500 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
4501 * DATA frame can return 0 as a valid result). Stream errors are reported in
4502 * h2s->errcode and connection errors in h2c->errcode. The caller must already
4503 * have checked the frame header and ensured that the frame was complete or the
4504 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02004505 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01004506static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02004507{
4508 struct h2c *h2c = h2s->h2c;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004509 int block;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004510 unsigned int flen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004511 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004512 struct buffer *csbuf;
Christopher Faulet9b79a102019-07-15 11:22:56 +02004513 unsigned int sent;
Willy Tarreau454f9052017-10-26 19:40:35 +02004514
Willy Tarreau7838a792019-08-12 18:42:03 +02004515 TRACE_ENTER(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
4516
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004517 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02004518
Olivier Houchard638b7992018-08-16 15:41:52 +02004519 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004520 if (!csbuf) {
4521 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau7838a792019-08-12 18:42:03 +02004522 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 +01004523 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004524 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004525 htx = htx_from_buf(csbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01004526
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004527try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004528 flen = h2c->dfl - h2c->dpl;
4529 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01004530 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004531
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004532 if (flen > b_data(&h2c->dbuf)) {
4533 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004534 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01004535 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01004536 }
4537
Christopher Faulet9b79a102019-07-15 11:22:56 +02004538 block = htx_free_data_space(htx);
4539 if (!block) {
4540 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004541 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 +02004542 goto fail;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004543 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02004544 if (flen > block)
4545 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004546
Christopher Faulet9b79a102019-07-15 11:22:56 +02004547 /* here, flen is the max we can copy into the output buffer */
4548 block = b_contig_data(&h2c->dbuf, 0);
4549 if (flen > block)
4550 flen = block;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004551
Christopher Faulet9b79a102019-07-15 11:22:56 +02004552 sent = htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen));
Willy Tarreau7838a792019-08-12 18:42:03 +02004553 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 +02004554
Christopher Faulet9b79a102019-07-15 11:22:56 +02004555 b_del(&h2c->dbuf, sent);
4556 h2c->dfl -= sent;
4557 h2c->rcvd_c += sent;
4558 h2c->rcvd_s += sent; // warning, this can also affect the closed streams!
Willy Tarreau454f9052017-10-26 19:40:35 +02004559
Christopher Faulet9b79a102019-07-15 11:22:56 +02004560 if (h2s->flags & H2_SF_DATA_CLEN) {
4561 h2s->body_len -= sent;
4562 htx->extra = h2s->body_len;
Willy Tarreaueba10f22018-04-25 20:44:22 +02004563 }
4564
Christopher Faulet9b79a102019-07-15 11:22:56 +02004565 if (sent < flen) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01004566 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau7838a792019-08-12 18:42:03 +02004567 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 +01004568 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004569 }
4570
Christopher Faulet9b79a102019-07-15 11:22:56 +02004571 goto try_again;
4572
Willy Tarreau4a28da12018-01-04 14:41:00 +01004573 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01004574 /* here we're done with the frame, all the payload (except padding) was
4575 * transferred.
4576 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02004577
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004578 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet9b79a102019-07-15 11:22:56 +02004579 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004580 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 +02004581 h2c->flags |= H2_CF_DEM_SFULL;
4582 goto fail;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004583 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02004584 }
4585
Willy Tarreaud1023bb2018-03-22 16:53:12 +01004586 h2c->rcvd_c += h2c->dpl;
4587 h2c->rcvd_s += h2c->dpl;
4588 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004589 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet9b79a102019-07-15 11:22:56 +02004590 htx_to_buf(htx, csbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02004591 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01004592 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01004593 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004594 if (htx)
4595 htx_to_buf(htx, csbuf);
Willy Tarreau7838a792019-08-12 18:42:03 +02004596 TRACE_LEAVE(H2_EV_RX_FRAME|H2_EV_RX_DATA, h2c->conn, h2s);
Willy Tarreau454b57b2018-02-26 15:50:05 +01004597 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02004598}
4599
Willy Tarreau115e83b2018-12-01 19:17:53 +01004600/* Try to send a HEADERS frame matching HTX response present in HTX message
4601 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4602 * must check the stream's status to detect any error which might have happened
4603 * subsequently to a successful send. The htx blocks are automatically removed
4604 * from the message. The htx message is assumed to be valid since produced from
4605 * the internal code, hence it contains a start line, an optional series of
4606 * header blocks and an end of header, otherwise an invalid frame could be
4607 * emitted and the resulting htx message could be left in an inconsistent state.
4608 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004609static size_t h2s_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004610{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004611 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau115e83b2018-12-01 19:17:53 +01004612 struct h2c *h2c = h2s->h2c;
4613 struct htx_blk *blk;
4614 struct htx_blk *blk_end;
4615 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004616 struct buffer *mbuf;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004617 struct htx_sl *sl;
4618 enum htx_blk_type type;
4619 int es_now = 0;
4620 int ret = 0;
4621 int hdr;
4622 int idx;
4623
Willy Tarreau7838a792019-08-12 18:42:03 +02004624 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
4625
Willy Tarreau115e83b2018-12-01 19:17:53 +01004626 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004627 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004628 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02004629 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004630 return 0;
4631 }
4632
Willy Tarreau115e83b2018-12-01 19:17:53 +01004633 /* determine the first block which must not be deleted, blk_end may
4634 * be NULL if all blocks have to be deleted.
4635 */
4636 idx = htx_get_head(htx);
4637 blk_end = NULL;
4638 while (idx != -1) {
4639 type = htx_get_blk_type(htx_get_blk(htx, idx));
4640 idx = htx_get_next(htx, idx);
4641 if (type == HTX_BLK_EOH) {
4642 if (idx != -1)
4643 blk_end = htx_get_blk(htx, idx);
4644 break;
4645 }
4646 }
4647
4648 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004649 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004650 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_RES_SL);
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004651 ALREADY_CHECKED(blk);
4652 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004653 h2s->status = sl->info.res.status;
Willy Tarreau7838a792019-08-12 18:42:03 +02004654 if (h2s->status < 100 || h2s->status > 999) {
4655 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 +01004656 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004657 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004658
4659 /* and the rest of the headers, that we dump starting at header 0 */
4660 hdr = 0;
4661
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004662 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004663 while ((idx = htx_get_next(htx, idx)) != -1) {
4664 blk = htx_get_blk(htx, idx);
4665 type = htx_get_blk_type(blk);
4666
4667 if (type == HTX_BLK_UNUSED)
4668 continue;
4669
4670 if (type != HTX_BLK_HDR)
4671 break;
4672
Willy Tarreau7838a792019-08-12 18:42:03 +02004673 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
4674 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 +01004675 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004676 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004677
4678 list[hdr].n = htx_get_blk_name(htx, blk);
4679 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004680 hdr++;
4681 }
4682
4683 /* marker for end of headers */
4684 list[hdr].n = ist("");
4685
4686 if (h2s->status == 204 || h2s->status == 304) {
4687 /* no contents, claim c-len is present and set to zero */
4688 es_now = 1;
4689 }
4690
Willy Tarreau9c218e72019-05-26 10:08:28 +02004691 mbuf = br_tail(h2c->mbuf);
4692 retry:
4693 if (!h2_get_buf(h2c, mbuf)) {
4694 h2c->flags |= H2_CF_MUX_MALLOC;
4695 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02004696 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 +02004697 return 0;
4698 }
4699
Willy Tarreau115e83b2018-12-01 19:17:53 +01004700 chunk_reset(&outbuf);
4701
4702 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004703 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4704 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004705 break;
4706 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004707 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau115e83b2018-12-01 19:17:53 +01004708 }
4709
4710 if (outbuf.size < 9)
4711 goto full;
4712
4713 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4714 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4715 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4716 outbuf.data = 9;
4717
4718 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004719 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004720 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004721 goto realign_again;
4722 goto full;
4723 }
4724
4725 /* encode all headers, stop at empty name */
4726 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4727 /* these ones do not exist in H2 and must be dropped. */
4728 if (isteq(list[hdr].n, ist("connection")) ||
4729 isteq(list[hdr].n, ist("proxy-connection")) ||
4730 isteq(list[hdr].n, ist("keep-alive")) ||
4731 isteq(list[hdr].n, ist("upgrade")) ||
4732 isteq(list[hdr].n, ist("transfer-encoding")))
4733 continue;
4734
Christopher Faulet86d144c2019-08-14 16:32:25 +02004735 /* Skip all pseudo-headers */
4736 if (*(list[hdr].n.ptr) == ':')
4737 continue;
4738
Willy Tarreau115e83b2018-12-01 19:17:53 +01004739 if (isteq(list[hdr].n, ist("")))
4740 break; // end
4741
4742 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4743 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02004744 if (b_space_wraps(mbuf))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004745 goto realign_again;
4746 goto full;
4747 }
4748 }
4749
Willy Tarreaucb985a42019-10-07 16:56:34 +02004750 /* update the frame's size */
4751 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4752
4753 if (outbuf.data > h2c->mfs + 9) {
4754 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
4755 /* output full */
4756 if (b_space_wraps(mbuf))
4757 goto realign_again;
4758 goto full;
4759 }
4760 }
4761
Christopher Faulet0b465482019-02-19 15:14:23 +01004762 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004763 * FIXME: we should also set it when we know for sure that the
4764 * content-length is zero as well as on 204/304
4765 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004766 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4767 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004768 es_now = 1;
4769
Willy Tarreau927b88b2019-03-04 08:03:25 +01004770 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004771 es_now = 1;
4772
Willy Tarreau115e83b2018-12-01 19:17:53 +01004773 if (es_now)
4774 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4775
4776 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02004777 TRACE_USER("sent H2 response", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
Willy Tarreaubcc45952019-05-26 10:05:50 +02004778 b_add(mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004779
4780 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4781 * 1xx responses, another HEADERS frame is expected.
4782 */
4783 if (h2s->status >= 200 || h2s->status == 101)
4784 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004785
Willy Tarreau115e83b2018-12-01 19:17:53 +01004786 if (es_now) {
4787 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02004788 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 +01004789 if (h2s->st == H2_SS_OPEN)
4790 h2s->st = H2_SS_HLOC;
4791 else
4792 h2s_close(h2s);
4793 }
4794
4795 /* OK we could properly deliver the response */
4796
4797 /* remove all header blocks including the EOH and compute the
4798 * corresponding size.
4799 *
4800 * FIXME: We should remove everything when es_now is set.
4801 */
4802 ret = 0;
4803 idx = htx_get_head(htx);
4804 blk = htx_get_blk(htx, idx);
4805 while (blk != blk_end) {
4806 ret += htx_get_blksz(blk);
4807 blk = htx_remove_blk(htx, blk);
4808 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004809
Christopher Faulet316934d2019-05-15 14:22:48 +02004810 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
4811 ret += htx_get_blksz(blk_end);
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004812 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02004813 }
Willy Tarreau115e83b2018-12-01 19:17:53 +01004814 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02004815 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004816 return ret;
4817 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02004818 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
4819 goto retry;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004820 h2c->flags |= H2_CF_MUX_MFULL;
4821 h2s->flags |= H2_SF_BLK_MROOM;
4822 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02004823 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 +01004824 goto end;
4825 fail:
4826 /* unparsable HTX messages, too large ones to be produced in the local
4827 * list etc go here (unrecoverable errors).
4828 */
4829 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4830 ret = 0;
4831 goto end;
4832}
4833
Willy Tarreau80739692018-10-05 11:35:57 +02004834/* Try to send a HEADERS frame matching HTX request present in HTX message
4835 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4836 * must check the stream's status to detect any error which might have happened
4837 * subsequently to a successful send. The htx blocks are automatically removed
4838 * from the message. The htx message is assumed to be valid since produced from
4839 * the internal code, hence it contains a start line, an optional series of
4840 * header blocks and an end of header, otherwise an invalid frame could be
4841 * emitted and the resulting htx message could be left in an inconsistent state.
4842 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02004843static size_t h2s_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
Willy Tarreau80739692018-10-05 11:35:57 +02004844{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02004845 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau80739692018-10-05 11:35:57 +02004846 struct h2c *h2c = h2s->h2c;
4847 struct htx_blk *blk;
4848 struct htx_blk *blk_end;
4849 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02004850 struct buffer *mbuf;
Willy Tarreau80739692018-10-05 11:35:57 +02004851 struct htx_sl *sl;
Willy Tarreaub8ce8902019-10-08 18:16:18 +02004852 struct ist meth, uri, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004853 enum htx_blk_type type;
4854 int es_now = 0;
4855 int ret = 0;
4856 int hdr;
4857 int idx;
4858
Willy Tarreau7838a792019-08-12 18:42:03 +02004859 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
4860
Willy Tarreau80739692018-10-05 11:35:57 +02004861 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02004862 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02004863 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02004864 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau80739692018-10-05 11:35:57 +02004865 return 0;
4866 }
4867
Willy Tarreau80739692018-10-05 11:35:57 +02004868 /* determine the first block which must not be deleted, blk_end may
4869 * be NULL if all blocks have to be deleted.
4870 */
4871 idx = htx_get_head(htx);
4872 blk_end = NULL;
4873 while (idx != -1) {
4874 type = htx_get_blk_type(htx_get_blk(htx, idx));
4875 idx = htx_get_next(htx, idx);
4876 if (type == HTX_BLK_EOH) {
4877 if (idx != -1)
4878 blk_end = htx_get_blk(htx, idx);
4879 break;
4880 }
4881 }
4882
4883 /* get the start line, we do have one */
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004884 blk = htx_get_head_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004885 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauletb77a1d22019-05-13 11:55:10 +02004886 ALREADY_CHECKED(blk);
4887 sl = htx_get_blk_ptr(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004888 meth = htx_sl_req_meth(sl);
Willy Tarreaub8ce8902019-10-08 18:16:18 +02004889 uri = htx_sl_req_uri(sl);
4890 if (unlikely(uri.len == 0)) {
4891 TRACE_PROTO("no URI in HTX request", H2_EV_TX_FRAME|H2_EV_TX_HDR|H2_EV_H2S_ERR, h2c->conn, h2s);
4892 goto fail;
4893 }
Willy Tarreau80739692018-10-05 11:35:57 +02004894
4895 /* and the rest of the headers, that we dump starting at header 0 */
4896 hdr = 0;
4897
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004898 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004899 while ((idx = htx_get_next(htx, idx)) != -1) {
4900 blk = htx_get_blk(htx, idx);
4901 type = htx_get_blk_type(blk);
4902
4903 if (type == HTX_BLK_UNUSED)
4904 continue;
4905
4906 if (type != HTX_BLK_HDR)
4907 break;
4908
Willy Tarreau7838a792019-08-12 18:42:03 +02004909 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
4910 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 +02004911 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02004912 }
Willy Tarreau80739692018-10-05 11:35:57 +02004913
4914 list[hdr].n = htx_get_blk_name(htx, blk);
4915 list[hdr].v = htx_get_blk_value(htx, blk);
Christopher Faulet67d58092019-10-02 10:51:38 +02004916
4917 /* Skip header if same name is used to add the server name */
4918 if ((h2c->flags & H2_CF_IS_BACK) && h2c->proxy->server_id_hdr_name &&
4919 isteq(list[hdr].n, ist2(h2c->proxy->server_id_hdr_name, h2c->proxy->server_id_hdr_len)))
4920 continue;
4921
Willy Tarreau80739692018-10-05 11:35:57 +02004922 hdr++;
4923 }
4924
Christopher Faulet72ba6cd2019-09-24 16:20:05 +02004925 /* Now add the server name to a header (if requested) */
4926 if ((h2c->flags & H2_CF_IS_BACK) && h2c->proxy->server_id_hdr_name) {
4927 struct server *srv = objt_server(h2c->conn->target);
4928
4929 if (srv) {
4930 list[hdr].n = ist2(h2c->proxy->server_id_hdr_name, h2c->proxy->server_id_hdr_len);
4931 list[hdr].v = ist(srv->id);
4932 hdr++;
4933 }
4934 }
4935
Willy Tarreau80739692018-10-05 11:35:57 +02004936 /* marker for end of headers */
4937 list[hdr].n = ist("");
4938
Willy Tarreau9c218e72019-05-26 10:08:28 +02004939 mbuf = br_tail(h2c->mbuf);
4940 retry:
4941 if (!h2_get_buf(h2c, mbuf)) {
4942 h2c->flags |= H2_CF_MUX_MALLOC;
4943 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02004944 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 +02004945 return 0;
4946 }
4947
Willy Tarreau80739692018-10-05 11:35:57 +02004948 chunk_reset(&outbuf);
4949
4950 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004951 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
4952 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004953 break;
4954 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02004955 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau80739692018-10-05 11:35:57 +02004956 }
4957
4958 if (outbuf.size < 9)
4959 goto full;
4960
4961 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4962 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4963 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4964 outbuf.data = 9;
4965
4966 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004967 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02004968 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02004969 goto realign_again;
4970 goto full;
4971 }
4972
Willy Tarreaub8ce8902019-10-08 18:16:18 +02004973 auth = ist(NULL);
4974
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004975 /* RFC7540 #8.3: the CONNECT method must have :
4976 * - :authority set to the URI part (host:port)
4977 * - :method set to CONNECT
4978 * - :scheme and :path omitted
4979 */
Willy Tarreaub8ce8902019-10-08 18:16:18 +02004980 if (unlikely(sl->info.req.meth == HTTP_METH_CONNECT)) {
4981 auth = uri;
4982
4983 if (!hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4984 /* output full */
4985 if (b_space_wraps(mbuf))
4986 goto realign_again;
4987 goto full;
4988 }
4989 } else {
4990 /* other methods need a :scheme. If an authority is known from
4991 * the request line, it must be sent, otherwise only host is
4992 * sent. Host is never sent as the authority.
4993 */
4994 struct ist scheme = { };
Christopher Faulet3b44c542019-06-14 10:46:51 +02004995
Willy Tarreaub8ce8902019-10-08 18:16:18 +02004996 if (uri.ptr[0] != '/' && uri.ptr[0] != '*') {
4997 /* the URI seems to start with a scheme */
4998 int len = 1;
4999
5000 while (len < uri.len && uri.ptr[len] != ':')
5001 len++;
5002
5003 if (len + 2 < uri.len && uri.ptr[len + 1] == '/' && uri.ptr[len + 2] == '/') {
5004 /* make the uri start at the authority now */
5005 scheme.ptr = uri.ptr;
5006 scheme.len = len,
5007 uri.ptr += len + 3;
5008 uri.len -= len + 3;
5009
5010 /* find the auth part of the URI */
5011 auth.ptr = uri.ptr;
5012 auth.len = 0;
5013 while (auth.len < uri.len && auth.ptr[auth.len] != '/')
5014 auth.len++;
5015
5016 uri.ptr += auth.len;
5017 uri.len -= auth.len;
5018 }
5019 }
5020
5021 if (!scheme.len) {
5022 /* no explicit scheme, we're using an origin-form URI,
5023 * probably from an H1 request transcoded to H2 via an
5024 * external layer, then received as H2 without authority.
5025 * So we have to look up the scheme from the HTX flags.
5026 * In such a case only http and https are possible, and
5027 * https is the default (sent by browsers).
5028 */
5029 if ((sl->flags & (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP)) == (HTX_SL_F_HAS_SCHM|HTX_SL_F_SCHM_HTTP))
5030 scheme = ist("http");
5031 else
5032 scheme = ist("https");
5033 }
Christopher Faulet3b44c542019-06-14 10:46:51 +02005034
5035 if (!hpack_encode_scheme(&outbuf, scheme)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005036 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005037 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005038 goto realign_again;
5039 goto full;
5040 }
Willy Tarreau80739692018-10-05 11:35:57 +02005041
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005042 if (auth.len && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005043 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005044 if (b_space_wraps(mbuf))
Willy Tarreau5be92ff2019-02-01 15:51:59 +01005045 goto realign_again;
5046 goto full;
5047 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005048
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005049 /* encode the path. RFC7540#8.1.2.3: if path is empty it must
5050 * be sent as '/' or '*'.
5051 */
5052 if (unlikely(!uri.len)) {
5053 if (sl->info.req.meth == HTTP_METH_OPTIONS)
5054 uri = ist("*");
5055 else
5056 uri = ist("/");
Willy Tarreau053c1572019-02-01 16:13:59 +01005057 }
Willy Tarreau053c1572019-02-01 16:13:59 +01005058
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005059 if (!hpack_encode_path(&outbuf, uri)) {
5060 /* output full */
5061 if (b_space_wraps(mbuf))
5062 goto realign_again;
5063 goto full;
5064 }
Willy Tarreau80739692018-10-05 11:35:57 +02005065 }
5066
Willy Tarreaub8ce8902019-10-08 18:16:18 +02005067 /* encode all headers, stop at empty name. Host is only sent if we
5068 * do not provide an authority.
5069 */
Willy Tarreau80739692018-10-05 11:35:57 +02005070 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005071 struct ist n = list[hdr].n;
5072 struct ist v = list[hdr].v;
5073
Willy Tarreau80739692018-10-05 11:35:57 +02005074 /* these ones do not exist in H2 and must be dropped. */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005075 if (isteq(n, ist("connection")) ||
5076 (auth.len && isteq(n, ist("host"))) ||
5077 isteq(n, ist("proxy-connection")) ||
5078 isteq(n, ist("keep-alive")) ||
5079 isteq(n, ist("upgrade")) ||
5080 isteq(n, ist("transfer-encoding")))
Willy Tarreau80739692018-10-05 11:35:57 +02005081 continue;
5082
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005083 if (isteq(n, ist("te"))) {
5084 /* "te" may only be sent with "trailers" if this value
5085 * is present, otherwise it must be deleted.
5086 */
5087 v = istist(v, ist("trailers"));
5088 if (!v.ptr || (v.len > 8 && v.ptr[8] != ','))
5089 continue;
5090 v = ist("trailers");
5091 }
5092
Christopher Faulet86d144c2019-08-14 16:32:25 +02005093 /* Skip all pseudo-headers */
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005094 if (*(n.ptr) == ':')
Christopher Faulet86d144c2019-08-14 16:32:25 +02005095 continue;
5096
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005097 if (isteq(n, ist("")))
Willy Tarreau80739692018-10-05 11:35:57 +02005098 break; // end
5099
Willy Tarreaubb2c4ae2020-01-24 09:07:53 +01005100 if (!hpack_encode_header(&outbuf, n, v)) {
Willy Tarreau80739692018-10-05 11:35:57 +02005101 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005102 if (b_space_wraps(mbuf))
Willy Tarreau80739692018-10-05 11:35:57 +02005103 goto realign_again;
5104 goto full;
5105 }
5106 }
5107
Willy Tarreaucb985a42019-10-07 16:56:34 +02005108 /* update the frame's size */
5109 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5110
5111 if (outbuf.data > h2c->mfs + 9) {
5112 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5113 /* output full */
5114 if (b_space_wraps(mbuf))
5115 goto realign_again;
5116 goto full;
5117 }
5118 }
5119
Willy Tarreau80739692018-10-05 11:35:57 +02005120 /* we may need to add END_STREAM if we have no body :
5121 * - request already closed, or :
5122 * - no transfer-encoding, and :
5123 * - no content-length or content-length:0
5124 * Fixme: this doesn't take into account CONNECT requests.
5125 */
5126 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
5127 es_now = 1;
5128
5129 if (sl->flags & HTX_SL_F_BODYLESS)
5130 es_now = 1;
5131
Willy Tarreau927b88b2019-03-04 08:03:25 +01005132 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02005133 es_now = 1;
5134
Willy Tarreau80739692018-10-05 11:35:57 +02005135 if (es_now)
5136 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
5137
5138 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02005139 TRACE_USER("sent H2 request", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s, htx);
Willy Tarreaubcc45952019-05-26 10:05:50 +02005140 b_add(mbuf, outbuf.data);
Willy Tarreau80739692018-10-05 11:35:57 +02005141 h2s->flags |= H2_SF_HEADERS_SENT;
5142 h2s->st = H2_SS_OPEN;
5143
Willy Tarreau80739692018-10-05 11:35:57 +02005144 if (es_now) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005145 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 +02005146 // trim any possibly pending data (eg: inconsistent content-length)
5147 h2s->flags |= H2_SF_ES_SENT;
5148 h2s->st = H2_SS_HLOC;
5149 }
5150
5151 /* remove all header blocks including the EOH and compute the
5152 * corresponding size.
5153 *
5154 * FIXME: We should remove everything when es_now is set.
5155 */
5156 ret = 0;
5157 idx = htx_get_head(htx);
5158 blk = htx_get_blk(htx, idx);
5159 while (blk != blk_end) {
5160 ret += htx_get_blksz(blk);
5161 blk = htx_remove_blk(htx, blk);
5162 }
5163
Christopher Faulet316934d2019-05-15 14:22:48 +02005164 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5165 ret += htx_get_blksz(blk_end);
Willy Tarreau80739692018-10-05 11:35:57 +02005166 htx_remove_blk(htx, blk_end);
Christopher Faulet316934d2019-05-15 14:22:48 +02005167 }
Willy Tarreau80739692018-10-05 11:35:57 +02005168
5169 end:
5170 return ret;
5171 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005172 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5173 goto retry;
Willy Tarreau80739692018-10-05 11:35:57 +02005174 h2c->flags |= H2_CF_MUX_MFULL;
5175 h2s->flags |= H2_SF_BLK_MROOM;
5176 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005177 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 +02005178 goto end;
5179 fail:
5180 /* unparsable HTX messages, too large ones to be produced in the local
5181 * list etc go here (unrecoverable errors).
5182 */
5183 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5184 ret = 0;
5185 goto end;
5186}
5187
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005188/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01005189 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
5190 * caller must check the stream's status to detect any error which might have
5191 * happened subsequently to a successful send. Returns the number of data bytes
Christopher Faulet54b5e212019-06-04 10:08:28 +02005192 * consumed, or zero if nothing done. Note that EOM count for 1 byte.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005193 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005194static size_t h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005195{
5196 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005197 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005198 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005199 struct buffer *mbuf;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005200 size_t total = 0;
5201 int es_now = 0;
5202 int bsize; /* htx block size */
5203 int fsize; /* h2 frame size */
5204 struct htx_blk *blk;
5205 enum htx_blk_type type;
5206 int idx;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01005207 int trunc_out; /* non-zero if truncated on out buf */
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005208
Willy Tarreau7838a792019-08-12 18:42:03 +02005209 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
5210
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005211 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005212 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005213 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02005214 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005215 goto end;
5216 }
5217
Willy Tarreau98de12a2018-12-12 07:03:00 +01005218 htx = htx_from_buf(buf);
5219
Christopher Faulet54b5e212019-06-04 10:08:28 +02005220 /* We only come here with HTX_BLK_DATA blocks. However, while looping,
5221 * we can meet an HTX_BLK_EOM block that we'll leave to the caller to
5222 * handle.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005223 */
5224
5225 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01005226 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005227 goto end;
5228
5229 idx = htx_get_head(htx);
5230 blk = htx_get_blk(htx, idx);
Christopher Faulet54b5e212019-06-04 10:08:28 +02005231 type = htx_get_blk_type(blk); // DATA or EOM
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005232 bsize = htx_get_blksz(blk);
5233 fsize = bsize;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01005234 trunc_out = 0;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005235
Christopher Faulet54b5e212019-06-04 10:08:28 +02005236 if (type == HTX_BLK_EOM) {
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01005237 if (h2s->flags & H2_SF_ES_SENT) {
5238 /* ES already sent */
5239 htx_remove_blk(htx, blk);
5240 total++; // EOM counts as one byte
5241 count--;
5242 goto end;
5243 }
5244 }
5245 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005246 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005247
Willy Tarreau9c218e72019-05-26 10:08:28 +02005248 mbuf = br_tail(h2c->mbuf);
5249 retry:
5250 if (!h2_get_buf(h2c, mbuf)) {
5251 h2c->flags |= H2_CF_MUX_MALLOC;
5252 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005253 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 +02005254 goto end;
5255 }
5256
Willy Tarreau98de12a2018-12-12 07:03:00 +01005257 /* Perform some optimizations to reduce the number of buffer copies.
5258 * First, if the mux's buffer is empty and the htx area contains
5259 * exactly one data block of the same size as the requested count, and
5260 * this count fits within the frame size, the stream's window size, and
5261 * the connection's window size, then it's possible to simply swap the
5262 * caller's buffer with the mux's output buffer and adjust offsets and
5263 * length to match the entire DATA HTX block in the middle. In this
5264 * case we perform a true zero-copy operation from end-to-end. This is
5265 * the situation that happens all the time with large files. Second, if
5266 * this is not possible, but the mux's output buffer is empty, we still
5267 * have an opportunity to avoid the copy to the intermediary buffer, by
5268 * making the intermediary buffer's area point to the output buffer's
5269 * area. In this case we want to skip the HTX header to make sure that
5270 * copies remain aligned and that this operation remains possible all
5271 * the time. This goes for headers, data blocks and any data extracted
5272 * from the HTX blocks.
5273 */
5274 if (unlikely(fsize == count &&
Christopher Faulet192c6a22019-06-11 16:32:24 +02005275 htx_nbblks(htx) == 1 && type == HTX_BLK_DATA &&
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005276 fsize <= h2s_mws(h2s) && fsize <= h2c->mws && fsize <= h2c->mfs)) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005277 void *old_area = mbuf->area;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005278
Willy Tarreaubcc45952019-05-26 10:05:50 +02005279 if (b_data(mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005280 /* Too bad there are data left there. We're willing to memcpy/memmove
5281 * up to 1/4 of the buffer, which means that it's OK to copy a large
5282 * frame into a buffer containing few data if it needs to be realigned,
5283 * and that it's also OK to copy few data without realigning. Otherwise
5284 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01005285 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005286 if (fsize + 9 <= b_room(mbuf) &&
5287 (b_data(mbuf) <= b_size(mbuf) / 4 ||
Willy Tarreau7838a792019-08-12 18:42:03 +02005288 (fsize <= b_size(mbuf) / 4 && fsize + 9 <= b_contig_space(mbuf)))) {
5289 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 +01005290 goto copy;
Willy Tarreau7838a792019-08-12 18:42:03 +02005291 }
Willy Tarreau8ab128c2019-03-21 17:47:28 +01005292
Willy Tarreau9c218e72019-05-26 10:08:28 +02005293 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5294 goto retry;
5295
Willy Tarreau98de12a2018-12-12 07:03:00 +01005296 h2c->flags |= H2_CF_MUX_MFULL;
5297 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005298 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 +01005299 goto end;
5300 }
5301
5302 /* map an H2 frame to the HTX block so that we can put the
5303 * frame header there.
5304 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005305 *mbuf = b_make(buf->area, buf->size, sizeof(struct htx) + blk->addr - 9, fsize + 9);
5306 outbuf.area = b_head(mbuf);
Willy Tarreau98de12a2018-12-12 07:03:00 +01005307
5308 /* prepend an H2 DATA frame header just before the DATA block */
5309 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5310 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5311 h2_set_frame_size(outbuf.area, fsize);
5312
5313 /* update windows */
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005314 h2s->sws -= fsize;
Willy Tarreau98de12a2018-12-12 07:03:00 +01005315 h2c->mws -= fsize;
5316
5317 /* and exchange with our old area */
5318 buf->area = old_area;
5319 buf->data = buf->head = 0;
5320 total += fsize;
Willy Tarreau7838a792019-08-12 18:42:03 +02005321
5322 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 +01005323 goto end;
5324 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01005325
Willy Tarreau98de12a2018-12-12 07:03:00 +01005326 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005327 /* for DATA and EOM we'll have to emit a frame, even if empty */
5328
5329 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005330 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5331 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005332 break;
5333 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005334 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005335 }
5336
5337 if (outbuf.size < 9) {
Willy Tarreau9c218e72019-05-26 10:08:28 +02005338 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5339 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005340 h2c->flags |= H2_CF_MUX_MFULL;
5341 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005342 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005343 goto end;
5344 }
5345
5346 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
5347 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
5348 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5349 outbuf.data = 9;
5350
5351 /* we have in <fsize> the exact number of bytes we need to copy from
5352 * the HTX buffer. We need to check this against the connection's and
5353 * the stream's send windows, and to ensure that this fits in the max
5354 * frame size and in the buffer's available space minus 9 bytes (for
5355 * the frame header). The connection's flow control is applied last so
5356 * that we can use a separate list of streams which are immediately
5357 * unblocked on window opening. Note: we don't implement padding.
5358 */
5359
5360 /* EOM is presented with bsize==1 but would lead to the emission of an
5361 * empty frame, thus we force it to zero here.
5362 */
5363 if (type == HTX_BLK_EOM)
5364 bsize = fsize = 0;
5365
5366 if (!fsize)
5367 goto send_empty;
5368
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005369 if (h2s_mws(h2s) <= 0) {
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005370 h2s->flags |= H2_SF_BLK_SFCTL;
Willy Tarreauc234ae32019-05-13 17:56:11 +02005371 if (LIST_ADDED(&h2s->list))
Olivier Houchardbfe2a832019-05-10 14:02:21 +02005372 LIST_DEL_INIT(&h2s->list);
Willy Tarreau9edf6db2019-10-02 10:49:59 +02005373 LIST_ADDQ(&h2c->blocked_list, &h2s->list);
Willy Tarreau7838a792019-08-12 18:42:03 +02005374 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 +01005375 goto end;
5376 }
5377
Willy Tarreauee573762018-12-04 15:25:57 +01005378 if (fsize > count)
5379 fsize = count;
5380
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005381 if (fsize > h2s_mws(h2s))
5382 fsize = h2s_mws(h2s); // >0
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005383
5384 if (h2c->mfs && fsize > h2c->mfs)
5385 fsize = h2c->mfs; // >0
5386
5387 if (fsize + 9 > outbuf.size) {
Willy Tarreau455d5682019-05-24 19:42:18 +02005388 /* It doesn't fit at once. If it at least fits once split and
5389 * the amount of data to move is low, let's defragment the
5390 * buffer now.
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005391 */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005392 if (b_space_wraps(mbuf) &&
5393 (fsize + 9 <= b_room(mbuf)) &&
5394 b_data(mbuf) <= MAX_DATA_REALIGN)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005395 goto realign_again;
5396 fsize = outbuf.size - 9;
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01005397 trunc_out = 1;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005398
5399 if (fsize <= 0) {
5400 /* no need to send an empty frame here */
Willy Tarreau9c218e72019-05-26 10:08:28 +02005401 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5402 goto retry;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005403 h2c->flags |= H2_CF_MUX_MFULL;
5404 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005405 TRACE_STATE("output buffer full", H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005406 goto end;
5407 }
5408 }
5409
5410 if (h2c->mws <= 0) {
5411 h2s->flags |= H2_SF_BLK_MFCTL;
Willy Tarreau7838a792019-08-12 18:42:03 +02005412 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 +01005413 goto end;
5414 }
5415
5416 if (fsize > h2c->mws)
5417 fsize = h2c->mws;
5418
5419 /* now let's copy this this into the output buffer */
5420 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau1d4a0f82019-08-02 07:52:08 +02005421 h2s->sws -= fsize;
Willy Tarreau0f799ca2018-12-04 15:20:11 +01005422 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01005423 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005424
5425 send_empty:
5426 /* update the frame's size */
5427 h2_set_frame_size(outbuf.area, fsize);
5428
5429 /* FIXME: for now we only set the ES flag on empty DATA frames, once
5430 * meeting EOM. We should optimize this later.
5431 */
5432 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01005433 total++; // EOM counts as one byte
5434 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005435 es_now = 1;
5436 }
5437
5438 if (es_now)
5439 outbuf.area[4] |= H2_F_DATA_END_STREAM;
5440
5441 /* commit the H2 response */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005442 b_add(mbuf, fsize + 9);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005443
5444 /* consume incoming HTX block, including EOM */
5445 total += fsize;
5446 if (fsize == bsize) {
5447 htx_remove_blk(htx, blk);
Willy Tarreau7838a792019-08-12 18:42:03 +02005448 if (fsize) {
5449 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 +01005450 goto new_frame;
Willy Tarreau7838a792019-08-12 18:42:03 +02005451 }
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005452 } else {
5453 /* we've truncated this block */
5454 htx_cut_data_blk(htx, blk, fsize);
Willy Tarreauc7ce4e32020-01-14 11:42:59 +01005455 if (trunc_out)
5456 goto new_frame;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005457 }
5458
5459 if (es_now) {
5460 if (h2s->st == H2_SS_OPEN)
5461 h2s->st = H2_SS_HLOC;
5462 else
5463 h2s_close(h2s);
5464
5465 h2s->flags |= H2_SF_ES_SENT;
Willy Tarreau7838a792019-08-12 18:42:03 +02005466 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 +01005467 }
5468
5469 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005470 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_DATA, h2c->conn, h2s);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005471 return total;
5472}
5473
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005474/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
5475 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
5476 * processed. The caller must check the stream's status to detect any error
5477 * which might have happened subsequently to a successful send. The htx blocks
5478 * are automatically removed from the message. The htx message is assumed to be
5479 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005480 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005481 * as a single frame. The ES flag is always set.
5482 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005483static size_t h2s_make_trailers(struct h2s *h2s, struct htx *htx)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005484{
Christopher Faulete4ab11b2019-06-11 15:05:37 +02005485 struct http_hdr list[global.tune.max_http_hdr];
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005486 struct h2c *h2c = h2s->h2c;
5487 struct htx_blk *blk;
5488 struct htx_blk *blk_end;
5489 struct buffer outbuf;
Willy Tarreaubcc45952019-05-26 10:05:50 +02005490 struct buffer *mbuf;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005491 enum htx_blk_type type;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005492 int ret = 0;
5493 int hdr;
5494 int idx;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005495
Willy Tarreau7838a792019-08-12 18:42:03 +02005496 TRACE_ENTER(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
5497
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005498 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005499 TRACE_STATE("mux output busy", H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005500 h2s->flags |= H2_SF_BLK_MBUSY;
Willy Tarreau7838a792019-08-12 18:42:03 +02005501 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005502 goto end;
5503 }
5504
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005505 /* determine the first block which must not be deleted, blk_end may
5506 * be NULL if all blocks have to be deleted. also get trailers.
5507 */
5508 idx = htx_get_head(htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005509 blk_end = NULL;
5510
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005511 hdr = 0;
5512 while (idx != -1) {
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005513 blk = htx_get_blk(htx, idx);
5514 type = htx_get_blk_type(blk);
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005515 idx = htx_get_next(htx, idx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005516 if (type == HTX_BLK_UNUSED)
5517 continue;
5518
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005519 if (type == HTX_BLK_EOT) {
5520 if (idx != -1)
5521 blk_end = blk;
5522 break;
5523 }
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005524 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005525 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005526
Willy Tarreau7838a792019-08-12 18:42:03 +02005527 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1)) {
5528 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 +01005529 goto fail;
Willy Tarreau7838a792019-08-12 18:42:03 +02005530 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005531
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005532 list[hdr].n = htx_get_blk_name(htx, blk);
5533 list[hdr].v = htx_get_blk_value(htx, blk);
5534 hdr++;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005535 }
5536
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005537 /* marker for end of trailers */
5538 list[hdr].n = ist("");
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005539
Willy Tarreau9c218e72019-05-26 10:08:28 +02005540 mbuf = br_tail(h2c->mbuf);
5541 retry:
5542 if (!h2_get_buf(h2c, mbuf)) {
5543 h2c->flags |= H2_CF_MUX_MALLOC;
5544 h2s->flags |= H2_SF_BLK_MROOM;
Willy Tarreau7838a792019-08-12 18:42:03 +02005545 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 +02005546 goto end;
5547 }
5548
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005549 chunk_reset(&outbuf);
5550
5551 while (1) {
Willy Tarreaubcc45952019-05-26 10:05:50 +02005552 outbuf = b_make(b_tail(mbuf), b_contig_space(mbuf), 0, 0);
5553 if (outbuf.size >= 9 || !b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005554 break;
5555 realign_again:
Willy Tarreaubcc45952019-05-26 10:05:50 +02005556 b_slow_realign(mbuf, trash.area, b_data(mbuf));
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005557 }
5558
5559 if (outbuf.size < 9)
5560 goto full;
5561
5562 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5563 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5564 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5565 outbuf.data = 9;
5566
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005567 /* encode all headers */
5568 for (idx = 0; idx < hdr; idx++) {
5569 /* these ones do not exist in H2 or must not appear in
5570 * trailers and must be dropped.
5571 */
5572 if (isteq(list[idx].n, ist("host")) ||
5573 isteq(list[idx].n, ist("content-length")) ||
5574 isteq(list[idx].n, ist("connection")) ||
5575 isteq(list[idx].n, ist("proxy-connection")) ||
5576 isteq(list[idx].n, ist("keep-alive")) ||
5577 isteq(list[idx].n, ist("upgrade")) ||
5578 isteq(list[idx].n, ist("te")) ||
5579 isteq(list[idx].n, ist("transfer-encoding")))
5580 continue;
5581
Christopher Faulet86d144c2019-08-14 16:32:25 +02005582 /* Skip all pseudo-headers */
5583 if (*(list[idx].n.ptr) == ':')
5584 continue;
5585
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005586 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5587 /* output full */
Willy Tarreaubcc45952019-05-26 10:05:50 +02005588 if (b_space_wraps(mbuf))
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005589 goto realign_again;
5590 goto full;
5591 }
5592 }
5593
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005594 if (outbuf.data == 9) {
5595 /* here we have a problem, we have nothing to emit (either we
5596 * received an empty trailers block followed or we removed its
5597 * contents above). Because of this we can't send a HEADERS
5598 * frame, so we have to cheat and instead send an empty DATA
5599 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005600 */
5601 outbuf.area[3] = H2_FT_DATA;
5602 outbuf.area[4] = H2_F_DATA_END_STREAM;
5603 }
5604
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005605 /* update the frame's size */
5606 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5607
Willy Tarreau572d9f52019-10-11 16:58:37 +02005608 if (outbuf.data > h2c->mfs + 9) {
5609 if (!h2_fragment_headers(&outbuf, h2c->mfs)) {
5610 /* output full */
5611 if (b_space_wraps(mbuf))
5612 goto realign_again;
5613 goto full;
5614 }
5615 }
5616
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005617 /* commit the H2 response */
Willy Tarreau7838a792019-08-12 18:42:03 +02005618 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 +02005619 b_add(mbuf, outbuf.data);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005620 h2s->flags |= H2_SF_ES_SENT;
5621
5622 if (h2s->st == H2_SS_OPEN)
5623 h2s->st = H2_SS_HLOC;
5624 else
5625 h2s_close(h2s);
5626
5627 /* OK we could properly deliver the response */
5628 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005629 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005630 ret = 0;
5631 idx = htx_get_head(htx);
5632 blk = htx_get_blk(htx, idx);
5633 while (blk != blk_end) {
5634 ret += htx_get_blksz(blk);
5635 blk = htx_remove_blk(htx, blk);
5636 }
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005637
5638 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM) {
5639 ret += htx_get_blksz(blk_end);
5640 htx_remove_blk(htx, blk_end);
5641 }
5642
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005643 end:
Willy Tarreau7838a792019-08-12 18:42:03 +02005644 TRACE_LEAVE(H2_EV_TX_FRAME|H2_EV_TX_HDR, h2c->conn, h2s);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005645 return ret;
5646 full:
Willy Tarreau9c218e72019-05-26 10:08:28 +02005647 if ((mbuf = br_tail_add(h2c->mbuf)) != NULL)
5648 goto retry;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005649 h2c->flags |= H2_CF_MUX_MFULL;
5650 h2s->flags |= H2_SF_BLK_MROOM;
5651 ret = 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005652 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 +01005653 goto end;
5654 fail:
5655 /* unparsable HTX messages, too large ones to be produced in the local
5656 * list etc go here (unrecoverable errors).
5657 */
5658 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5659 ret = 0;
5660 goto end;
5661}
5662
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005663/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5664 * event subscriber <es> is not allowed to change from a previous call as long
5665 * as at least one event is still subscribed. The <event_type> must only be a
5666 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005667 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005668static int h2_subscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +02005669{
Olivier Houchard6ff20392018-07-17 18:46:31 +02005670 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005671 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005672
Willy Tarreau7838a792019-08-12 18:42:03 +02005673 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01005674
5675 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005676 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01005677
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005678 es->events |= event_type;
5679 h2s->subs = es;
Willy Tarreauf96508a2020-01-10 11:12:48 +01005680
5681 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02005682 TRACE_DEVEL("subscribe(recv)", H2_EV_STRM_RECV, h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01005683
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005684 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005685 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2c->conn, h2s);
Olivier Houchardf8338152019-05-14 17:50:32 +02005686 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5687 !LIST_ADDED(&h2s->list)) {
5688 if (h2s->flags & H2_SF_BLK_MFCTL)
5689 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5690 else
5691 LIST_ADDQ(&h2c->send_list, &h2s->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005692 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02005693 }
Willy Tarreau7838a792019-08-12 18:42:03 +02005694 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005695 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005696}
5697
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005698/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5699 * The <es> pointer is not allowed to differ from the one passed to the
5700 * subscribe() call. It always returns zero.
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005701 */
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005702static int h2_unsubscribe(struct conn_stream *cs, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005703{
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005704 struct h2s *h2s = cs->ctx;
5705
Willy Tarreau7838a792019-08-12 18:42:03 +02005706 TRACE_ENTER(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreauf96508a2020-01-10 11:12:48 +01005707
5708 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005709 BUG_ON(h2s->subs && h2s->subs != es);
Willy Tarreauf96508a2020-01-10 11:12:48 +01005710
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005711 es->events &= ~event_type;
5712 if (!es->events)
Willy Tarreauf96508a2020-01-10 11:12:48 +01005713 h2s->subs = NULL;
5714
5715 if (event_type & SUB_RETRY_RECV)
Willy Tarreau7838a792019-08-12 18:42:03 +02005716 TRACE_DEVEL("unsubscribe(recv)", H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01005717
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005718 if (event_type & SUB_RETRY_SEND) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005719 TRACE_DEVEL("subscribe(send)", H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreaud9464162020-01-10 18:25:07 +01005720 h2s->flags &= ~H2_SF_NOTIFIED;
Willy Tarreauf96508a2020-01-10 11:12:48 +01005721 if (!(h2s->flags & (H2_SF_WANT_SHUTR | H2_SF_WANT_SHUTW)))
5722 LIST_DEL_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02005723 }
Willy Tarreauf96508a2020-01-10 11:12:48 +01005724
Willy Tarreau7838a792019-08-12 18:42:03 +02005725 TRACE_LEAVE(H2_EV_STRM_SEND|H2_EV_STRM_RECV, h2s->h2c->conn, h2s);
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005726 return 0;
5727}
5728
5729
Olivier Houchard511efea2018-08-16 15:30:32 +02005730/* Called from the upper layer, to receive data */
5731static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5732{
Olivier Houchard638b7992018-08-16 15:41:52 +02005733 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005734 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005735 struct htx *h2s_htx = NULL;
5736 struct htx *buf_htx = NULL;
Olivier Houchard511efea2018-08-16 15:30:32 +02005737 size_t ret = 0;
5738
Willy Tarreau7838a792019-08-12 18:42:03 +02005739 TRACE_ENTER(H2_EV_STRM_RECV, h2c->conn, h2s);
5740
Olivier Houchard511efea2018-08-16 15:30:32 +02005741 /* transfer possibly pending data to the upper layer */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005742 h2s_htx = htx_from_buf(&h2s->rxbuf);
5743 if (htx_is_empty(h2s_htx)) {
5744 /* Here htx_to_buf() will set buffer data to 0 because
5745 * the HTX is empty.
5746 */
5747 htx_to_buf(h2s_htx, &h2s->rxbuf);
5748 goto end;
5749 }
Willy Tarreau7196dd62019-03-05 10:51:11 +01005750
Christopher Faulet9b79a102019-07-15 11:22:56 +02005751 ret = h2s_htx->data;
5752 buf_htx = htx_from_buf(buf);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005753
Christopher Faulet9b79a102019-07-15 11:22:56 +02005754 /* <buf> is empty and the message is small enough, swap the
5755 * buffers. */
5756 if (htx_is_empty(buf_htx) && htx_used_space(h2s_htx) <= count) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005757 htx_to_buf(buf_htx, buf);
5758 htx_to_buf(h2s_htx, &h2s->rxbuf);
Christopher Faulet9b79a102019-07-15 11:22:56 +02005759 b_xfer(buf, &h2s->rxbuf, b_data(&h2s->rxbuf));
5760 goto end;
Willy Tarreau86724e22018-12-01 23:19:43 +01005761 }
Christopher Faulet9b79a102019-07-15 11:22:56 +02005762
5763 htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
5764
5765 if (h2s_htx->flags & HTX_FL_PARSING_ERROR) {
5766 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5767 if (htx_is_empty(buf_htx))
5768 cs->flags |= CS_FL_EOI;
Willy Tarreau86724e22018-12-01 23:19:43 +01005769 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005770
Christopher Faulet9b79a102019-07-15 11:22:56 +02005771 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
5772 htx_to_buf(buf_htx, buf);
5773 htx_to_buf(h2s_htx, &h2s->rxbuf);
5774 ret -= h2s_htx->data;
5775
Christopher Faulet37070b22019-02-14 15:12:14 +01005776 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005777 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005778 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005779 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005780 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Christopher Fauletfa922f02019-05-07 10:55:17 +02005781 if (h2s->flags & H2_SF_ES_RCVD)
5782 cs->flags |= CS_FL_EOI;
Willy Tarreau76c83822019-06-15 09:55:50 +02005783 if (conn_xprt_read0_pending(h2c->conn) || h2s->st == H2_SS_CLOSED)
Olivier Houchard511efea2018-08-16 15:30:32 +02005784 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005785 if (cs->flags & CS_FL_ERR_PENDING)
5786 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005787 if (b_size(&h2s->rxbuf)) {
5788 b_free(&h2s->rxbuf);
5789 offer_buffers(NULL, tasks_run_queue);
5790 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005791 }
5792
Willy Tarreau082f5592018-11-25 08:03:32 +01005793 if (ret && h2c->dsi == h2s->id) {
5794 /* demux is blocking on this stream's buffer */
5795 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005796 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005797 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005798
Willy Tarreau7838a792019-08-12 18:42:03 +02005799 TRACE_LEAVE(H2_EV_STRM_RECV, h2c->conn, h2s);
Olivier Houchard511efea2018-08-16 15:30:32 +02005800 return ret;
5801}
5802
Olivier Houchardd846c262018-10-19 17:24:29 +02005803
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005804/* Called from the upper layer, to send data from buffer <buf> for no more than
5805 * <count> bytes. Returns the number of bytes effectively sent. Some status
5806 * flags may be updated on the conn_stream.
5807 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005808static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005809{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005810 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005811 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005812 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005813 struct htx *htx;
5814 struct htx_blk *blk;
5815 enum htx_blk_type btype;
5816 uint32_t bsize;
5817 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005818
Willy Tarreau7838a792019-08-12 18:42:03 +02005819 TRACE_ENTER(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
5820
Olivier Houchardd360ac62019-03-22 17:37:16 +01005821 /* If we were not just woken because we wanted to send but couldn't,
5822 * and there's somebody else that is waiting to send, do nothing,
5823 * we will subscribe later and be put at the end of the list
5824 */
Willy Tarreaud9464162020-01-10 18:25:07 +01005825 if (!(h2s->flags & H2_SF_NOTIFIED) &&
Willy Tarreau7838a792019-08-12 18:42:03 +02005826 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list))) {
5827 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 +01005828 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005829 }
Willy Tarreaud9464162020-01-10 18:25:07 +01005830 h2s->flags &= ~H2_SF_NOTIFIED;
Olivier Houchard998410a2019-04-15 19:23:37 +02005831
Willy Tarreau7838a792019-08-12 18:42:03 +02005832 if (h2s->h2c->st0 < H2_CS_FRAME_H) {
5833 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 +02005834 return 0;
Willy Tarreau7838a792019-08-12 18:42:03 +02005835 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005836
Willy Tarreaucab22952019-10-31 15:48:18 +01005837 if (h2s->h2c->st0 >= H2_CS_ERROR) {
5838 cs->flags |= CS_FL_ERROR;
5839 TRACE_DEVEL("connection is in error, leaving in error", H2_EV_H2S_SEND|H2_EV_H2S_BLK|H2_EV_H2S_ERR|H2_EV_STRM_ERR, h2s->h2c->conn, h2s);
5840 return 0;
5841 }
5842
Christopher Faulet9b79a102019-07-15 11:22:56 +02005843 htx = htx_from_buf(buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005844
Willy Tarreau0bad0432018-06-14 16:54:01 +02005845 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005846 h2s->flags |= H2_SF_OUTGOING_DATA;
5847
Willy Tarreau751f2d02018-10-05 09:35:00 +02005848 if (h2s->id == 0) {
5849 int32_t id = h2c_get_next_sid(h2s->h2c);
5850
5851 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005852 cs->flags |= CS_FL_ERROR;
Willy Tarreau7838a792019-08-12 18:42:03 +02005853 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 +02005854 return 0;
5855 }
5856
5857 eb32_delete(&h2s->by_id);
5858 h2s->by_id.key = h2s->id = id;
5859 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005860 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005861 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5862 }
5863
Christopher Faulet9b79a102019-07-15 11:22:56 +02005864 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
5865 count && !htx_is_empty(htx)) {
5866 idx = htx_get_head(htx);
5867 blk = htx_get_blk(htx, idx);
5868 btype = htx_get_blk_type(blk);
5869 bsize = htx_get_blksz(blk);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005870
Christopher Faulet9b79a102019-07-15 11:22:56 +02005871 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005872 case HTX_BLK_REQ_SL:
5873 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005874 ret = h2s_bck_make_req_headers(h2s, htx);
Willy Tarreau80739692018-10-05 11:35:57 +02005875 if (ret > 0) {
5876 total += ret;
5877 count -= ret;
5878 if (ret < bsize)
5879 goto done;
5880 }
5881 break;
5882
Willy Tarreau115e83b2018-12-01 19:17:53 +01005883 case HTX_BLK_RES_SL:
5884 /* start-line before headers */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005885 ret = h2s_frt_make_resp_headers(h2s, htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01005886 if (ret > 0) {
5887 total += ret;
5888 count -= ret;
5889 if (ret < bsize)
5890 goto done;
5891 }
5892 break;
5893
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005894 case HTX_BLK_DATA:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005895 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005896 /* all these cause the emission of a DATA frame (possibly empty).
5897 * This EOM necessarily is one before trailers, as the EOM following
5898 * trailers would have been consumed by the trailers parser.
5899 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005900 ret = h2s_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005901 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005902 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005903 total += ret;
5904 count -= ret;
5905 if (ret < bsize)
5906 goto done;
5907 }
5908 break;
5909
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005910 case HTX_BLK_TLR:
Christopher Faulet2d7c5392019-06-03 10:41:26 +02005911 case HTX_BLK_EOT:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005912 /* This is the first trailers block, all the subsequent ones AND
5913 * the EOM will be swallowed by the parser.
5914 */
Christopher Faulet9b79a102019-07-15 11:22:56 +02005915 ret = h2s_make_trailers(h2s, htx);
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005916 if (ret > 0) {
5917 total += ret;
5918 count -= ret;
5919 if (ret < bsize)
5920 goto done;
5921 }
5922 break;
5923
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005924 default:
5925 htx_remove_blk(htx, blk);
5926 total += bsize;
5927 count -= bsize;
5928 break;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005929 }
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005930 }
5931
Christopher Faulet9b79a102019-07-15 11:22:56 +02005932 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005933 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005934 /* trim any possibly pending data after we close (extra CR-LF,
5935 * unprocessed trailers, abnormal extra data, ...)
5936 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005937 total += count;
5938 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005939 }
5940
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005941 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005942 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005943 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 +01005944 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005945 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005946 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005947 }
5948
Christopher Faulet9b79a102019-07-15 11:22:56 +02005949 htx_to_buf(htx, buf);
Olivier Houchardd846c262018-10-19 17:24:29 +02005950
Olivier Houchard7505f942018-08-21 18:10:44 +02005951 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005952 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Willy Tarreau7838a792019-08-12 18:42:03 +02005953 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 +02005954 tasklet_wakeup(h2s->h2c->wait_event.tasklet);
Olivier Houchardd846c262018-10-19 17:24:29 +02005955
Olivier Houchard7505f942018-08-21 18:10:44 +02005956 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005957 /* If we're waiting for flow control, and we got a shutr on the
5958 * connection, we will never be unlocked, so add an error on
5959 * the conn_stream.
5960 */
5961 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5962 !b_data(&h2s->h2c->dbuf) &&
5963 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
Willy Tarreau7838a792019-08-12 18:42:03 +02005964 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 +01005965 if (cs->flags & CS_FL_EOS)
5966 cs->flags |= CS_FL_ERROR;
5967 else
5968 cs->flags |= CS_FL_ERR_PENDING;
5969 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02005970
Willy Tarreau5723f292020-01-10 15:16:57 +01005971 if (total > 0 && !(h2s->flags & H2_SF_BLK_SFCTL) &&
5972 !(h2s->flags & (H2_SF_WANT_SHUTR|H2_SF_WANT_SHUTW))) {
Willy Tarreau9edf6db2019-10-02 10:49:59 +02005973 /* Ok we managed to send something, leave the send_list if we were still there */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005974 LIST_DEL_INIT(&h2s->list);
5975 }
Willy Tarreau9edf6db2019-10-02 10:49:59 +02005976
Willy Tarreau7838a792019-08-12 18:42:03 +02005977 TRACE_LEAVE(H2_EV_H2S_SEND|H2_EV_STRM_SEND, h2s->h2c->conn, h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005978 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005979}
5980
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005981/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005982static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005983{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005984 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005985 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005986 struct eb32_node *node;
5987 int fctl_cnt = 0;
5988 int send_cnt = 0;
5989 int tree_cnt = 0;
5990 int orph_cnt = 0;
Willy Tarreau60f62682019-05-26 11:32:27 +02005991 struct buffer *hmbuf, *tmbuf;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005992
5993 if (!h2c)
5994 return;
5995
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005996 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005997 fctl_cnt++;
5998
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005999 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006000 send_cnt++;
6001
Willy Tarreau3af37712018-12-18 14:34:41 +01006002 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006003 node = eb32_first(&h2c->streams_by_id);
6004 while (node) {
6005 h2s = container_of(node, struct h2s, by_id);
6006 tree_cnt++;
6007 if (!h2s->cs)
6008 orph_cnt++;
6009 node = eb32_next(node);
6010 }
6011
Willy Tarreau60f62682019-05-26 11:32:27 +02006012 hmbuf = br_head(h2c->mbuf);
Willy Tarreaubcc45952019-05-26 10:05:50 +02006013 tmbuf = br_tail(h2c->mbuf);
Willy Tarreauab2ec452019-08-30 07:07:08 +02006014 chunk_appendf(msg, " h2c.st0=%s .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
Willy Tarreau987c0632018-12-18 10:32:05 +01006015 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
Willy Tarreau60f62682019-05-26 11:32:27 +02006016 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d"
6017 " .mbuf=[%u..%u|%u],h=[%u@%p+%u/%u],t=[%u@%p+%u/%u]",
Willy Tarreauab2ec452019-08-30 07:07:08 +02006018 h2c_st_to_str(h2c->st0), h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
Willy Tarreau616ac812018-07-24 14:12:42 +02006019 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01006020 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01006021 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
6022 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
6023 h2c->msi,
Willy Tarreau60f62682019-05-26 11:32:27 +02006024 br_head_idx(h2c->mbuf), br_tail_idx(h2c->mbuf), br_size(h2c->mbuf),
6025 (unsigned int)b_data(hmbuf), b_orig(hmbuf),
6026 (unsigned int)b_head_ofs(hmbuf), (unsigned int)b_size(hmbuf),
Willy Tarreaubcc45952019-05-26 10:05:50 +02006027 (unsigned int)b_data(tmbuf), b_orig(tmbuf),
6028 (unsigned int)b_head_ofs(tmbuf), (unsigned int)b_size(tmbuf));
Willy Tarreau987c0632018-12-18 10:32:05 +01006029
6030 if (h2s) {
Willy Tarreauab2ec452019-08-30 07:07:08 +02006031 chunk_appendf(msg, " last_h2s=%p .id=%d .st=%s.flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
6032 h2s, h2s->id, h2s_st_to_str(h2s->st), h2s->flags,
Willy Tarreau987c0632018-12-18 10:32:05 +01006033 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
6034 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
6035 h2s->cs);
6036 if (h2s->cs)
6037 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
6038 h2s->cs->flags, h2s->cs->data);
6039 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006040}
Willy Tarreau62f52692017-10-08 23:01:42 +02006041
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006042/* Migrate the the connection to the current thread.
6043 * Return 0 if successful, non-zero otherwise.
6044 * Expected to be called with the old thread lock held.
6045 */
6046static int h2_takeover(struct connection *conn)
6047{
6048 struct h2c *h2c = conn->ctx;
6049
6050 if (fd_takeover(conn->handle.fd, conn) != 0)
6051 return -1;
6052 if (h2c->wait_event.events)
6053 h2c->conn->xprt->unsubscribe(h2c->conn, h2c->conn->xprt_ctx,
6054 h2c->wait_event.events, &h2c->wait_event);
6055 /* To let the tasklet know it should free itself, and do nothing else,
6056 * set its context to NULL.
6057 */
6058 h2c->wait_event.tasklet->context = NULL;
6059 tasklet_wakeup(h2c->wait_event.tasklet);
6060 if (h2c->task) {
6061 h2c->task->context = NULL;
6062 /* Wake the task, to let it free itself */
6063 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
6064
6065 h2c->task = task_new(tid_bit);
6066 if (!h2c->task) {
6067 h2_release(h2c);
6068 return -1;
6069 }
6070 h2c->task->process = h2_timeout_task;
6071 h2c->task->context = h2c;
6072 }
6073 h2c->wait_event.tasklet = tasklet_new();
6074 if (!h2c->wait_event.tasklet) {
6075 h2_release(h2c);
6076 return -1;
6077 }
6078 h2c->wait_event.tasklet->process = h2_io_cb;
6079 h2c->wait_event.tasklet->context = h2c;
6080 h2c->conn->xprt->subscribe(h2c->conn, h2c->conn->xprt_ctx,
6081 SUB_RETRY_RECV, &h2c->wait_event);
6082
6083 return 0;
6084}
6085
Willy Tarreau62f52692017-10-08 23:01:42 +02006086/*******************************************************/
6087/* functions below are dedicated to the config parsers */
6088/*******************************************************/
6089
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02006090/* config parser for global "tune.h2.header-table-size" */
6091static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
6092 struct proxy *defpx, const char *file, int line,
6093 char **err)
6094{
6095 if (too_many_args(1, args, err, NULL))
6096 return -1;
6097
6098 h2_settings_header_table_size = atoi(args[1]);
6099 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
6100 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
6101 return -1;
6102 }
6103 return 0;
6104}
Willy Tarreau62f52692017-10-08 23:01:42 +02006105
Willy Tarreaue6baec02017-07-27 11:45:11 +02006106/* config parser for global "tune.h2.initial-window-size" */
6107static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
6108 struct proxy *defpx, const char *file, int line,
6109 char **err)
6110{
6111 if (too_many_args(1, args, err, NULL))
6112 return -1;
6113
6114 h2_settings_initial_window_size = atoi(args[1]);
6115 if (h2_settings_initial_window_size < 0) {
6116 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
6117 return -1;
6118 }
6119 return 0;
6120}
6121
Willy Tarreau5242ef82017-07-27 11:47:28 +02006122/* config parser for global "tune.h2.max-concurrent-streams" */
6123static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
6124 struct proxy *defpx, const char *file, int line,
6125 char **err)
6126{
6127 if (too_many_args(1, args, err, NULL))
6128 return -1;
6129
6130 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01006131 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02006132 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
6133 return -1;
6134 }
6135 return 0;
6136}
6137
Willy Tarreaua24b35c2019-02-21 13:24:36 +01006138/* config parser for global "tune.h2.max-frame-size" */
6139static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
6140 struct proxy *defpx, const char *file, int line,
6141 char **err)
6142{
6143 if (too_many_args(1, args, err, NULL))
6144 return -1;
6145
6146 h2_settings_max_frame_size = atoi(args[1]);
6147 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
6148 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
6149 return -1;
6150 }
6151 return 0;
6152}
6153
Willy Tarreau62f52692017-10-08 23:01:42 +02006154
6155/****************************************/
6156/* MUX initialization and instanciation */
6157/***************************************/
6158
6159/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01006160static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02006161 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02006162 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02006163 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02006164 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02006165 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02006166 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02006167 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01006168 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02006169 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01006170 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01006171 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01006172 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02006173 .shutr = h2_shutr,
6174 .shutw = h2_shutw,
Olivier Houchard9b8e11e2019-10-25 16:19:26 +02006175 .ctl = h2_ctl,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02006176 .show_fd = h2_show_fd,
Olivier Houchardcd4159f2020-03-10 18:39:42 +01006177 .takeover = h2_takeover,
Christopher Faulet9b79a102019-07-15 11:22:56 +02006178 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Willy Tarreau62f52692017-10-08 23:01:42 +02006179 .name = "H2",
6180};
6181
Christopher Faulet32f61c02018-04-10 14:33:41 +02006182static struct mux_proto_list mux_proto_h2 =
Christopher Fauletc985f6c2019-07-15 11:42:52 +02006183 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02006184
Willy Tarreau0108d902018-11-25 19:14:37 +01006185INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
6186
Willy Tarreau62f52692017-10-08 23:01:42 +02006187/* config keyword parsers */
6188static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02006189 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02006190 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02006191 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01006192 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02006193 { 0, NULL, NULL }
6194}};
6195
Willy Tarreau0108d902018-11-25 19:14:37 +01006196INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau2bdcc702020-05-19 11:31:11 +02006197
6198/* initialize internal structs after the config is parsed.
6199 * Returns zero on success, non-zero on error.
6200 */
6201static int init_h2()
6202{
6203 pool_head_hpack_tbl = create_pool("hpack_tbl",
6204 h2_settings_header_table_size,
6205 MEM_F_SHARED|MEM_F_EXACT);
6206 if (!pool_head_hpack_tbl)
6207 return -1;
6208 return 0;
6209}
6210
6211REGISTER_POST_CHECK(init_h2);